服务器之家:专注于服务器技术及软件下载分享
分类导航

Linux|Centos|Ubuntu|系统进程|Fedora|注册表|Bios|Solaris|Windows7|Windows10|Windows11|

服务器之家 - 服务器系统 - Linux - 手把手教你分析 Linux 启动流程

手把手教你分析 Linux 启动流程

2021-09-19 01:13嵌入式Linux系统开发Jasonangel Linux

本文主要讲解当从 bootloader 跳转到 Linux 系统的启动函数 start_kernel 后,此函数对系统初始化的流程。

手把手教你分析 Linux 启动流程

下载 Linux 内核网址:

https://www.kernel.org/

最新 Linux 内核是 5.15 版本。现在常用 Linux 内核源码为4.14、4.19、4.9 等版本,其中 4.14 版本源码压缩包大概 90+M,解压后 700+M,合计 61350 个文件。如此众多的文件,用 source insight 或者 VSCode 查看都会比较卡,所以可以采用在线查看的方式。

在线查看 Linux 内核源码网址:

https://elixir.bootlin.com/linux/latest/source

在线查看 Android 源码:

http://androidxref.com/

Android系统是基于Linux 内核的,最底层为Linux内核,源码量翻很多倍。所以用软件看安卓源码更卡,可以使用在线网址看源码。

我们知道,Linux 系统的启动,前面有一个启动引导程序 bootloader,比如常用的 uboot,本文不分析 uboot 的启动,只放一张流程图:

手把手教你分析 Linux 启动流程

本文主要讲解当从 bootloader 跳转到 Linux 系统的启动函数 start_kernel 后,此函数对系统初始化的流程。

在 linux4.14/arch/arm/kernel/head.S 文件中,是最后汇编阶段的初始化,而后会跳转到 main.c 文件的 start_kernel 函数,在此做 Linux 启动初始化,在这个函数中会调用将近100个函数去完成 Linux 系统的初始化,调用函数如下(不同内核版本,顺序和细节有变化):

linux4.14/init/main.c,start_kernel 函数。

  1. asmlinkage__visiblevoid__initstart_kernel(void)
  2. {
  3. char*command_line;
  4. char*after_dashes;
  5.  
  6. set_task_stack_end_magic(&init_task);
  7. smp_setup_processor_id();
  8. debug_objects_early_init();
  9.  
  10. cgroup_init_early();
  11.  
  12. local_irq_disable();
  13. early_boot_irqs_disabled=true;
  14. /*
  15. *Interruptsarestilldisabled.Donecessarysetups,then
  16. *enablethem.
  17. */
  18. boot_cpu_init();
  19. page_address_init();
  20. pr_notice("%s",linux_banner);
  21. setup_arch(&command_line);
  22. /*
  23. *Setupthetheinitialcanaryandentropyafterarch
  24. *andafteraddinglatentandcommandlineentropy.
  25. */
  26. add_latent_entropy();
  27. add_device_randomness(command_line,strlen(command_line));
  28. boot_init_stack_canary();
  29. mm_init_cpumask(&init_mm);
  30. setup_command_line(command_line);
  31. setup_nr_cpu_ids();
  32. setup_per_cpu_areas();
  33. smp_prepare_boot_cpu();/*arch-specificboot-cpuhooks*/
  34. boot_cpu_hotplug_init();
  35.  
  36. build_all_zonelists(NULL);
  37. page_alloc_init();
  38.  
  39. pr_notice("Kernelcommandline:%s\n",boot_command_line);
  40. /*parametersmaysetstatickeys*/
  41. jump_label_init();
  42. parse_early_param();
  43. after_dashes=parse_args("Bootingkernel",
  44. static_command_line,__start___param,
  45. __stop___param-__start___param,
  46. -1,-1,NULL,&unknown_bootoption);
  47. if(!IS_ERR_OR_NULL(after_dashes))
  48. parse_args("Settinginitargs",after_dashes,NULL,0,-1,-1,
  49. NULL,set_init_arg);
  50. /*
  51. *Theseuselargebootmemallocationsandmustprecede
  52. *kmem_cache_init()
  53. */
  54. setup_log_buf(0);
  55. pidhash_init();
  56. vfs_caches_init_early();
  57. sort_main_extable();
  58. trap_init();
  59. mm_init();
  60.  
  61. ftrace_init();
  62.  
  63. /*trace_printkcanbeenabledhere*/
  64. early_trace_init();
  65. /*
  66. *Setuptheschedulerpriorstartinganyinterrupts(suchasthe
  67. *timerinterrupt).Fulltopologysetuphappensatsmp_init()
  68. *time-butmeanwhilewestillhaveafunctioningscheduler.
  69. */
  70. sched_init();
  71. /*
  72. *Disablepreemption-earlybootupschedulingisextremely
  73. *fragileuntilwecpu_idle()forthefirsttime.
  74. */
  75. preempt_disable();
  76. if(WARN(!irqs_disabled(),
  77. "Interruptswereenabled*very*early,fixingit\n"))
  78. local_irq_disable();
  79. radix_tree_init();
  80. /*
  81. *Allowworkqueuecreationandworkitemqueueing/cancelling
  82. *early.Workitemexecutiondependsonkthreadsandstartsafter
  83. *workqueue_init().
  84. */
  85. workqueue_init_early();
  86.  
  87. rcu_init();
  88.  
  89. /*Traceeventsareavailableafterthis*/
  90. trace_init();
  91.  
  92. context_tracking_init();
  93. /*initsomelinksbeforeinit_ISA_irqs()*/
  94. early_irq_init();
  95. init_IRQ();
  96. tick_init();
  97. rcu_init_nohz();
  98. init_timers();
  99. hrtimers_init();
  100. softirq_init();
  101. timekeeping_init();
  102. time_init();
  103. sched_clock_postinit();
  104. printk_safe_init();
  105. perf_event_init();
  106. profile_init();
  107. call_function_init();
  108. WARN(!irqs_disabled(),"Interruptswereenabledearly\n");
  109. early_boot_irqs_disabled=false;
  110. local_irq_enable();
  111.  
  112. kmem_cache_init_late();
  113. /*
  114. *HACKALERT!Thisisearly.We'reenablingtheconsolebefore
  115. *we'vedonePCIsetupsetc,andconsole_init()mustbeawareof
  116. *this.Butwedowantoutputearly,incasesomethinggoeswrong.
  117. */
  118. console_init();
  119. if(panic_later)
  120. panic("Toomanyboot%svarsat`%s'",panic_later,
  121. panic_param);
  122.  
  123. lockdep_info();
  124. /*
  125. *Needtorunthiswhenirqsareenabled,becauseitwants
  126. *toself-test[hard/soft]-irqson/offlockinversionbugs
  127. *too:
  128. */
  129. locking_selftest();
  130. /*
  131. *ThisneedstobecalledbeforeanydevicesperformDMA
  132. *operationsthatmightusetheSWIOTLBbouncebuffers.Itwill
  133. *markthebouncebuffersasdecryptedsothattheirusagewill
  134. *notcause"plain-text"datatobedecryptedwhenaccessed.
  135. */
  136. mem_encrypt_init();
  137.  
  138. #ifdefCONFIG_BLK_DEV_INITRD
  139. if(initrd_start&&!initrd_below_start_ok&&
  140. page_to_pfn(virt_to_page((void*)initrd_start))
  141. pr_crit("initrdoverwritten(0x%08lx<0x%08lx)-disablingit.\n",
  142. page_to_pfn(virt_to_page((void*)initrd_start)),
  143. min_low_pfn);
  144. initrd_start=0;
  145. }
  146. #endif
  147. kmemleak_init();
  148. debug_objects_mem_init();
  149. setup_per_cpu_pageset();
  150. numa_policy_init();
  151. if(late_time_init)
  152. late_time_init();
  153. calibrate_delay();
  154. pidmap_init();
  155. anon_vma_init();
  156. acpi_early_init();
  157. #ifdefCONFIG_X86
  158. if(efi_enabled(EFI_RUNTIME_SERVICES))
  159. efi_enter_virtual_mode();
  160. #endif
  161. thread_stack_cache_init();
  162. cred_init();
  163. fork_init();
  164. proc_caches_init();
  165. buffer_init();
  166. key_init();
  167. security_init();
  168. dbg_late_init();
  169. vfs_caches_init();
  170. pagecache_init();
  171. signals_init();
  172. proc_root_init();
  173. nsfs_init();
  174. cpuset_init();
  175. cgroup_init();
  176. taskstats_init_early();
  177. delayacct_init();
  178.  
  179. check_bugs();
  180.  
  181. acpi_subsystem_init();
  182. arch_post_acpi_subsys_init();
  183. sfi_init_late();
  184.  
  185. if(efi_enabled(EFI_RUNTIME_SERVICES)){
  186. efi_free_boot_services();
  187. }
  188. /*Dotherestnon-__init'ed,we'renowalive*/
  189. rest_init();
  190.  
  191. prevent_tail_call_optimization();
  192. }

其中有七个函数较为重要,分别为:

  1. setup_arch(&command_line);
  2.  
  3. mm_init();
  4.  
  5. sched_init();
  6.  
  7. init_IRQ();
  8.  
  9. console_init();
  10.  
  11. vfs_caches_init();
  12.  
  13. rest_init();

1、setup_arch(&command_line)

此函数是系统架构初始化函数,处理 uboot 传递进来的参数,不同的架构进行不同的初始化,也就是说每个架构都会有一个 setup_arch 函数。

linux4.14/arch/arm/kernel/setup.c

手把手教你分析 Linux 启动流程

2、mm_init

内存初始化函数

linux4.14/init/main.c

手把手教你分析 Linux 启动流程

3、sched_init

核心进程调度器初始化。Linux 内核实现了四种调度方式,一般是采用 CFS 调度方式。作为一个普适性的操作系统,必须考虑各种需求,我们不能只按照中断优先级或者时间轮转片来规定进程运行的时间。作为一个多用户操作系统,必须考虑到每个用户的公平性。不能因为一个用户没有高级权限,就限制他的进程的运行时间,要考虑每个用户拥有公平的时间。

linux4.14/kernel/sched/core.c

手把手教你分析 Linux 启动流程

4、init_IRQ

中断初始化函数,这个很好理解,大家都用过中断。

linux4.14/arch/arm/kernel/irq.c

手把手教你分析 Linux 启动流程

5、console_init

在这个函数初始化之前,你所有写的内核打印函数 printk 都打印不出东西。在这个函数初始化之前,所有打印都会存在 buf 里,此函数初始化以后,会将 buf里面的数据打印出来,你才能在终端看到 printk 打印的东西。

tty 是 Linux 中的终端, _con_initcall_start 和_con_initcall_end 这两句的意思是执行所有两者之间的 initcall 函数。

linux4.14/kernel/printk/printk.c

手把手教你分析 Linux 启动流程

6、vfs_caches_init

虚拟文件系统初始化,比如 sysfs,根文件系统等,就是在这一步进行挂载,proc 是内核虚拟的,用来输出内核数据结构信息,不算在这里。

vfs虚拟文件系统,屏蔽了底层硬件的不同,提供了统一了接口,方便系统的移植和使用。使用户在不用更改应用代码的情况下直接移植代码到其他平台。

linux4.14/fs/dcache.c

手把手教你分析 Linux 启动流程

这里的挂载主要在mnt_init()函数中:

linux4.14/fs/namespace.c

手把手教你分析 Linux 启动流程

7、rest_init

这个函数可以算是 start_kernel函数调用的最后一个函数,在这里产生了最重要的两个内核进程 kernel_init 和 kthreadd,kernel_init后面会从内核空间跳转到用户空间,变成用户空间的 init 进程,PID=1,而 kthreadd ,PID=2,是内核进程,专门用来监听创建内核进程的请求,它维护了一个链表,如果有创建内核进程的需求,就会在链表上创建。

至此,用户空间最重要的 init 进程已经出来,后面用户空间的进程都由 init进程来 fork。如果是安卓系统,init 进程会 fork 出一个 zygote 进程,他是所有安卓系统进程的父进程。

linux4.14/init.main.c

手把手教你分析 Linux 启动流程

手把手教你分析 Linux 启动流程

上图,400 行创建了 kernel_init 进程,412 行创建了 kthreadd 进程,这两个都是内核进程。426 行通知 kernel_init 进程 kthreadd 已经创建完毕。也就是说,实际上是 kthreadd 先运行,kernel_init 再运行。

其余的函数大家可以参照下面的文章去理解:

https://www.cnblogs.com/andyfly/p/9410441.html

https://www.cnblogs.com/lifexy/p/7366782.html

https://www.cnblogs.com/yanzs/p/13910344.html#radix_tree:init

本文转载自微信公众号「嵌入式Linux系统开发」

手把手教你分析 Linux 启动流程

原文链接:https://mp.weixin.qq.com/s/HvkqcB9EQGvBDKzrOw4vhA

延伸 · 阅读

精彩推荐