1. 程式人生 > >linux下kernel啟動流程

linux下kernel啟動流程

                                                       先上總流程圖,一圖勝千言!


點選檢視大圖

head.S 初始化完成後跳轉到 start_kernel 入口:

kernel-3.18/init/main.c:505:asmlinkage __visible void __init start_kernel(void)
      

START => 原始碼分析:

這裡要乾的事情非常多而複雜,如果需要全部理解整個過程的話需要強大的知識背景做支撐以及對核心有著深入的理解才行,基於學習的循序漸進的考量,這裡我們重點關注device driver的啟動載入流程機制.


      
  1. asmlinkage __visible void __ init start_kernel(void)
  2. {
  3.   char
    *command_line;
  4.   char *after_dashes;
  5.   / *
  6.   * Need to run as early as possible, to initialize the
  7.   * lockdep hash:
  8.   * /
  9.  
  10. / * 有些體系結構有自己的start_kernel入口,這裡保證只初始化一次
  11.    系統雜湊表chainhash_table * /
  12.  lockdep_init();
  13.  set_task_stack_end_magic(&init_task);
  14.  
  15. / * 獲取當前執行cpu的id * /
  16.  smp_setup_processor_id();
  17.  
  18. / * 物件除錯支援初始化 * /
  19.  debug_objects_early_init();
  20.   / *
  21.   * Set up the the initial canary ASAP:
  22.   * /
  23.  
  24. / * 初始化棧canary值,canary值用於防止棧溢位攻擊,這裡不太明白詳細... * /
  25.  boot_init_stack_canary();
  26. / * cgrop :將一組任務在一個或多個子系統中與一組引數關聯,機制有點複雜... * /
  27.  cgroup_init_early();
  28.  
  29. / * 關閉中斷,因為很多初始化的工作不能被中斷 * /
  30.  local_irq_disable();
  31.  early_boot_irqs_disabled = true;
  32. / *
  33. * Interrupts are still disabled. Do necessary setups, then
  34. * enable them
  35. * /
  36. / * 獲取當前cpu id,啟用之 * /
  37.  boot_cpu_init();
  38.  
  39. / * 高階記憶體相關,建立核心對映所需的散列表 * /
  40.  page_address_init();
  41.  pr_notice( "%s", linux_banner);
  42.  
  43. / * 體系結構相關初始化,沒種體系結構都有特有的初始化入口 * /
  44.  setup_arch(&command_line);
  45.  
  46. / * cpu遮蔽位清零 * /
  47.  mm_init_cpumask(&init_mm);
  48.  
  49. / * 將命令列引數儲存到 static_command_line 中 * /
  50.  setup_command_line(command_line);
  51.  
  52.  setup_nr_cpu_ids();
  53.  setup_per_cpu_areas();
  54.  smp_prepare_boot_cpu();  / * arch-specific boot-cpu hooks * /
  55. / * 建立系統記憶體頁區(zone)連結串列 * /
  56.  build_all_zonelists( NULL, NULL);
  57.  
  58. / * cpu熱拔插相關, 這名字取得... * /
  59.  page_alloc_init();
  60.  pr_notice( "Kernel command line: %s\n", boot_command_line);
  61.  
  62. / * 解析啟動命令列引數 * /
  63.  parse_early_param();
  64.  after_dashes = parse_args( "Booting kernel",
  65.       static_command_line, __start___param,
  66.       __stop___param - __start___param,
  67.       -1, -1, &unknown_bootoption);
  68.   if (!IS_ERR_OR_NULL(after_dashes))
  69.   parse_args( "Setting init args", after_dashes, NULL, 0, -1, -1,
  70.       set_init_arg);
  71. / * 執行跳轉指令? * /
  72.  jump_label_init();
  73.   / *
  74.   * These use large bootmem allocations and must precede
  75.   * kmem_cache_init()
  76.   * /
  77.  
  78. / * 設定 log輸出緩衝buf * /
  79.  setup_log_buf( 0);
  80.  
  81. / * 初始化和分配pid散列表 * /
  82.  pidhash_init();
  83. / * 建立虛擬檔案系統(vfs)需要各種資料結構的快取 * /
  84.  vfs_caches_init_early();
  85.  
  86. / * 核心異常表排序 * /
  87.  sort_main_extable();
  88.  
  89. / * 異常捕獲設定初始化,跟體系結構相關,arm架構的實現是空函式 * /
  90.  trap_init();
  91.  
  92. / * 核心記憶體分配器初始化,初始化slab機制分配器和vmalloc機制 * /
  93.  mm_init();
  94.   / *
  95.   * Set up the scheduler prior starting any interrupts (such as the
  96.   * timer interrupt). Full topology setup happens at smp_init()
  97.   * time - but meanwhile we still have a functioning scheduler.
  98.   * /
  99.  
  100. / * 排程器資料結構初始化 * /
  101.  sched_init();
  102.   / *
  103.   * Disable preemption - early bootup scheduling is extremely
  104.   * fragile until we cpu_idle() for the first time.
  105.   * /
  106.  
  107. / * 關搶斷和中斷,啟動期間不允許排程和中斷 * /
  108.  preempt_disable();
  109.   if (WARN(!irqs_disabled(),
  110.    "Interrupts were enabled *very* early, fixing it\n"))
  111.   local_irq_disable();
  112.   
  113. / * 為 idr(一種將一個整數ID號和一個指標關聯在一起的機制) 機制建立cache * /
  114.  idr_init_cache();
  115.  
  116. / * rcu(read-copy-update, 核心鎖機制一類)機制初始化 * /
  117.  rcu_init();
  118.  
  119. / * 上下文tracking機制 ? 該機制被 CONFIG_CONTEXT_TRACKING_FORCE 包住了 * /
  120.  context_tracking_init();
  121.  
  122. / * 為核心基數樹演算法分配記憶體,運用於記憶體頁查詢 * /
  123.  radix_tree_init();
  124.   / * init some links before init_ISA_irqs() * /
  125.  
  126. / * 初始化體系結構相關irq,建立irq描述符,插入到基數屬連結串列 irq_desc_tree 中管理 * /
  127.  early_irq_init();
  128.  init_IRQ();
  129.  
  130. / * 時鐘相關初始化 * /
  131.  tick_init();
  132.  rcu_init_nohz();
  133.  init_timers();
  134.  hrtimers_init();
  135.  softirq_init();
  136.  timekeeping_init();
  137.  time_init();
  138.  
  139. / * 程序排程時鐘初始化 * /
  140.  sched_clock_postinit();
  141. / * cpu 效能相關monitor * /
  142.  perf_event_init();
  143.  
  144. / * gdb等debug工具設定相關 * /
  145.  profile_init();
  146. / * smp下跨cpu的函式傳遞初始化 * /
  147.  call_function_init();
  148.  WARN(!irqs_disabled(), "Interrupts were enabled early\n");
  149.  early_boot_irqs_disabled = false;
  150.  
  151. / * 使能中斷 * /
  152.  local_irq_enable();
  153. / * slab 分配器後期初始化 * /
  154.  kmem_cache_init_late();
  155.   / *
  156.   * HACK ALERT! This is early. We're enabling the console before
  157.   * we've done PCI setups etc, and console_init() must be aware of
  158.   * this. But we do want output early, in case something goes wrong.
  159.   * /
  160.  
  161. / * 終端控制檯輸出初始化 * /