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

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

[[424755]]

下载 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 的启动,只放一张流程图:

本文主要讲解当从 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 __visible void __init start_kernel(void) 
  2.  char *command_line; 
  3.  char *after_dashes; 
  4.  
  5.  set_task_stack_end_magic(&init_task); 
  6.  smp_setup_processor_id(); 
  7.  debug_objects_early_init(); 
  8.  
  9.  cgroup_init_early(); 
  10.  
  11.  local_irq_disable(); 
  12.  early_boot_irqs_disabled = true
  13.  /* 
  14.   * Interrupts are still disabled. Do necessary setups, then 
  15.   * enable them. 
  16.   */ 
  17.  boot_cpu_init(); 
  18.  page_address_init(); 
  19.  pr_notice("%s", linux_banner); 
  20.  setup_arch(&command_line); 
  21.  /* 
  22.   * Set up the the initial canary and entropy after arch 
  23.   * and after adding latent and command line entropy. 
  24.   */ 
  25.  add_latent_entropy(); 
  26.  add_device_randomness(command_line, strlen(command_line)); 
  27.  boot_init_stack_canary(); 
  28.  mm_init_cpumask(&init_mm); 
  29.  setup_command_line(command_line); 
  30.  setup_nr_cpu_ids(); 
  31.  setup_per_cpu_areas(); 
  32.  smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */ 
  33.  boot_cpu_hotplug_init(); 
  34.  
  35.  build_all_zonelists(NULL); 
  36.  page_alloc_init(); 
  37.  
  38.  pr_notice("Kernel command line: %s\n", boot_command_line); 
  39.  /* parameters may set static keys */ 
  40.  jump_label_init(); 
  41.  parse_early_param(); 
  42.  after_dashes = parse_args("Booting kernel"
  43.       static_command_line, __start___param, 
  44.       __stop___param - __start___param, 
  45.       -1, -1, NULL, &unknown_bootoption); 
  46.  if (!IS_ERR_OR_NULL(after_dashes)) 
  47.   parse_args("Setting init args", after_dashes, NULL, 0, -1, -1, 
  48.       NULL, set_init_arg); 
  49.  /* 
  50.   * These use large bootmem allocations and must precede 
  51.   * kmem_cache_init() 
  52.   */ 
  53.  setup_log_buf(0); 
  54.  pidhash_init(); 
  55.  vfs_caches_init_early(); 
  56.  sort_main_extable(); 
  57.  trap_init(); 
  58.  mm_init(); 
  59.  
  60.  ftrace_init(); 
  61.  
  62.  /* trace_printk can be enabled here */ 
  63.  early_trace_init(); 
  64.  /* 
  65.   * Set up the scheduler prior starting any interrupts (such as the 
  66.   * timer interrupt). Full topology setup happens at smp_init() 
  67.   * time - but meanwhile we still have a functioning scheduler. 
  68.   */ 
  69.  sched_init(); 
  70.  /* 
  71.   * Disable preemption - early bootup scheduling is extremely 
  72.   * fragile until we cpu_idle() for the first time
  73.   */ 
  74.  preempt_disable(); 
  75.  if (WARN(!irqs_disabled(), 
  76.    "Interrupts were enabled *very* early, fixing it\n")) 
  77.   local_irq_disable(); 
  78.  radix_tree_init(); 
  79.  /* 
  80.   * Allow workqueue creation and work item queueing/cancelling 
  81.   * early.  Work item execution depends on kthreads and starts after 
  82.   * workqueue_init(). 
  83.   */ 
  84.  workqueue_init_early(); 
  85.  
  86.  rcu_init(); 
  87.  
  88.  /* Trace events are available after this */ 
  89.  trace_init(); 
  90.  
  91.  context_tracking_init(); 
  92.  /* init some links before init_ISA_irqs() */ 
  93.  early_irq_init(); 
  94.  init_IRQ(); 
  95.  tick_init(); 
  96.  rcu_init_nohz(); 
  97.  init_timers(); 
  98.  hrtimers_init(); 
  99.  softirq_init(); 
  100.  timekeeping_init(); 
  101.  time_init(); 
  102.  sched_clock_postinit(); 
  103.  printk_safe_init(); 
  104.  perf_event_init(); 
  105.  profile_init(); 
  106.  call_function_init(); 
  107.  WARN(!irqs_disabled(), "Interrupts were enabled early\n"); 
  108.  early_boot_irqs_disabled = false
  109.  local_irq_enable(); 
  110.  
  111.  kmem_cache_init_late(); 
  112.  /* 
  113.   * HACK ALERT! This is early. We're enabling the console before 
  114.   * we've done PCI setups etc, and console_init() must be aware of 
  115.   * this. But we do want output early, in case something goes wrong. 
  116.   */ 
  117.  console_init(); 
  118.  if (panic_later) 
  119.   panic("Too many boot %s vars at `%s'", panic_later, 
  120.         panic_param); 
  121.  
  122.  lockdep_info(); 
  123.  /* 
  124.   * Need to run this when irqs are enabled, because it wants 
  125.   * to self-test [hard/soft]-irqs on/off lock inversion bugs 
  126.   * too: 
  127.   */ 
  128.  locking_selftest(); 
  129.  /* 
  130.   * This needs to be called before any devices perform DMA 
  131.   * operations that might use the SWIOTLB bounce buffers. It will 
  132.   * mark the bounce buffers as decrypted so that their usage will 
  133.   * not cause "plain-text" data to be decrypted when accessed. 
  134.   */ 
  135.  mem_encrypt_init(); 
  136.  
  137. #ifdef CONFIG_BLK_DEV_INITRD 
  138.  if (initrd_start && !initrd_below_start_ok && 
  139.      page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) { 
  140.   pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n"
  141.       page_to_pfn(virt_to_page((void *)initrd_start)), 
  142.       min_low_pfn); 
  143.   initrd_start = 0; 
  144.  } 
  145. #endif 
  146.  kmemleak_init(); 
  147.  debug_objects_mem_init(); 
  148.  setup_per_cpu_pageset(); 
  149.  numa_policy_init(); 
  150.  if (late_time_init) 
  151.   late_time_init(); 
  152.  calibrate_delay(); 
  153.  pidmap_init(); 
  154.  anon_vma_init(); 
  155.  acpi_early_init(); 
  156. #ifdef CONFIG_X86 
  157.  if (efi_enabled(EFI_RUNTIME_SERVICES)) 
  158.   efi_enter_virtual_mode(); 
  159. #endif 
  160.  thread_stack_cache_init(); 
  161.  cred_init(); 
  162.  fork_init(); 
  163.  proc_caches_init(); 
  164.  buffer_init(); 
  165.  key_init(); 
  166.  security_init(); 
  167.  dbg_late_init(); 
  168.  vfs_caches_init(); 
  169.  pagecache_init(); 
  170.  signals_init(); 
  171.  proc_root_init(); 
  172.  nsfs_init(); 
  173.  cpuset_init(); 
  174.  cgroup_init(); 
  175.  taskstats_init_early(); 
  176.  delayacct_init(); 
  177.  
  178.  check_bugs(); 
  179.  
  180.  acpi_subsystem_init(); 
  181.  arch_post_acpi_subsys_init(); 
  182.  sfi_init_late(); 
  183.  
  184.  if (efi_enabled(EFI_RUNTIME_SERVICES)) { 
  185.   efi_free_boot_services(); 
  186.  } 
  187.  /* Do the rest non-__init'ed, we're now alive */ 
  188.  rest_init(); 
  189.  
  190.  prevent_tail_call_optimization(); 

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

  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

2、mm_init

内存初始化函数

linux4.14/init/main.c

3、sched_init

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

linux4.14/kernel/sched/core.c

4、init_IRQ

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

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

5、console_init

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

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

linux4.14/kernel/printk/printk.c

6、vfs_caches_init

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

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

linux4.14/fs/dcache.c

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

linux4.14/fs/namespace.c

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

上图,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系统开发
相关推荐

2021-09-22 08:51:34

Android

2011-01-10 14:41:26

2020-04-14 10:20:12

MySQL数据库死锁

2022-01-17 07:50:37

Linux Patch项目

2011-05-03 15:59:00

黑盒打印机

2017-05-18 12:45:35

数据分析数据理解数据

2009-06-15 16:58:57

Java安装Linux

2020-06-01 16:25:43

WindowsLinux命令

2011-02-22 13:46:27

微软SQL.NET

2021-02-26 11:54:38

MyBatis 插件接口

2021-12-28 08:38:26

Linux 中断唤醒系统Linux 系统

2015-10-26 09:24:30

微信公众号数据分析

2021-08-02 23:15:20

Pandas数据采集

2021-07-14 09:00:00

JavaFX开发应用

2011-03-25 12:45:49

Oracle SOA

2020-07-09 08:59:52

if else模板Service

2021-09-30 18:27:38

数据仓库ETL

2009-11-09 14:57:37

WCF上传文件

2011-01-06 10:39:25

.NET程序打包

2016-04-27 09:49:16

用户模型产品总结
点赞
收藏

51CTO技术栈公众号