分析 Linux 启动流程基本实现

简介: 分析 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 的启动,只放一张流程图:

本文主要讲解当从 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 函数。

asmlinkage __visible void __init start_kernel(void)
{
 char *command_line;
 char *after_dashes;
 set_task_stack_end_magic(&init_task);
 smp_setup_processor_id();
 debug_objects_early_init();
 cgroup_init_early();
 local_irq_disable();
 early_boot_irqs_disabled = true;
 /*
  * Interrupts are still disabled. Do necessary setups, then
  * enable them.
  */
 boot_cpu_init();
 page_address_init();
 pr_notice("%s", linux_banner);
 setup_arch(&command_line);
 /*
  * Set up the the initial canary and entropy after arch
  * and after adding latent and command line entropy.
  */
 add_latent_entropy();
 add_device_randomness(command_line, strlen(command_line));
 boot_init_stack_canary();
 mm_init_cpumask(&init_mm);
 setup_command_line(command_line);
 setup_nr_cpu_ids();
 setup_per_cpu_areas();
 smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
 boot_cpu_hotplug_init();
 build_all_zonelists(NULL);
 page_alloc_init();
 pr_notice("Kernel command line: %s\n", boot_command_line);
 /* parameters may set static keys */
 jump_label_init();
 parse_early_param();
 after_dashes = parse_args("Booting kernel",
      static_command_line, __start___param,
      __stop___param - __start___param,
      -1, -1, NULL, &unknown_bootoption);
 if (!IS_ERR_OR_NULL(after_dashes))
  parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
      NULL, set_init_arg);
 /*
  * These use large bootmem allocations and must precede
  * kmem_cache_init()
  */
 setup_log_buf(0);
 pidhash_init();
 vfs_caches_init_early();
 sort_main_extable();
 trap_init();
 mm_init();
 ftrace_init();
 /* trace_printk can be enabled here */
 early_trace_init();
 /*
  * Set up the scheduler prior starting any interrupts (such as the
  * timer interrupt). Full topology setup happens at smp_init()
  * time - but meanwhile we still have a functioning scheduler.
  */
 sched_init();
 /*
  * Disable preemption - early bootup scheduling is extremely
  * fragile until we cpu_idle() for the first time.
  */
 preempt_disable();
 if (WARN(!irqs_disabled(),
   "Interrupts were enabled *very* early, fixing it\n"))
  local_irq_disable();
 radix_tree_init();
 /*
  * Allow workqueue creation and work item queueing/cancelling
  * early.  Work item execution depends on kthreads and starts after
  * workqueue_init().
  */
 workqueue_init_early();
 rcu_init();
 /* Trace events are available after this */
 trace_init();
 context_tracking_init();
 /* init some links before init_ISA_irqs() */
 early_irq_init();
 init_IRQ();
 tick_init();
 rcu_init_nohz();
 init_timers();
 hrtimers_init();
 softirq_init();
 timekeeping_init();
 time_init();
 sched_clock_postinit();
 printk_safe_init();
 perf_event_init();
 profile_init();
 call_function_init();
 WARN(!irqs_disabled(), "Interrupts were enabled early\n");
 early_boot_irqs_disabled = false;
 local_irq_enable();
 kmem_cache_init_late();
 /*
  * HACK ALERT! This is early. We're enabling the console before
  * we've done PCI setups etc, and console_init() must be aware of
  * this. But we do want output early, in case something goes wrong.
  */
 console_init();
 if (panic_later)
  panic("Too many boot %s vars at `%s'", panic_later,
        panic_param);
 lockdep_info();
 /*
  * Need to run this when irqs are enabled, because it wants
  * to self-test [hard/soft]-irqs on/off lock inversion bugs
  * too:
  */
 locking_selftest();
 /*
  * This needs to be called before any devices perform DMA
  * operations that might use the SWIOTLB bounce buffers. It will
  * mark the bounce buffers as decrypted so that their usage will
  * not cause "plain-text" data to be decrypted when accessed.
  */
 mem_encrypt_init();
#ifdef CONFIG_BLK_DEV_INITRD
 if (initrd_start && !initrd_below_start_ok &&
     page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
  pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n",
      page_to_pfn(virt_to_page((void *)initrd_start)),
      min_low_pfn);
  initrd_start = 0;
 }
#endif
 kmemleak_init();
 debug_objects_mem_init();
 setup_per_cpu_pageset();
 numa_policy_init();
 if (late_time_init)
  late_time_init();
 calibrate_delay();
 pidmap_init();
 anon_vma_init();
 acpi_early_init();
#ifdef CONFIG_X86
 if (efi_enabled(EFI_RUNTIME_SERVICES))
  efi_enter_virtual_mode();
#endif
 thread_stack_cache_init();
 cred_init();
 fork_init();
 proc_caches_init();
 buffer_init();
 key_init();
 security_init();
 dbg_late_init();
 vfs_caches_init();
 pagecache_init();
 signals_init();
 proc_root_init();
 nsfs_init();
 cpuset_init();
 cgroup_init();
 taskstats_init_early();
 delayacct_init();
 check_bugs();
 acpi_subsystem_init();
 arch_post_acpi_subsys_init();
 sfi_init_late();
 if (efi_enabled(EFI_RUNTIME_SERVICES)) {
  efi_free_boot_services();
 }
 /* Do the rest non-__init'ed, we're now alive */
 rest_init();
 prevent_tail_call_optimization();
}

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

setup_arch(&command_line);

mm_init();

sched_init();

init_IRQ();

console_init();

vfs_caches_init();

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 iOS开发
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
1008 53
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
|
Linux API iOS开发
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
944 14
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
|
存储 Linux
Linux环境下删除大文件后磁盘空间未释放问题诊断流程。
以上诊断流程涉及Linux底层机制与高级管理技能结合之处,并需要管理员根据实际环境灵活调整诊断策略与解决方案。
1040 8
|
数据管理 Linux iOS开发
Splunk Enterprise 9.4.5 (macOS, Linux, Windows) - 机器数据管理和分析
Splunk Enterprise 9.4.5 (macOS, Linux, Windows) - 机器数据管理和分析
546 0
|
Unix Linux iOS开发
Splunk Enterprise 10.0.0 (macOS, Linux, Windows) - 搜索、分析和可视化,数据全面洞察平台
Splunk Enterprise 10.0.0 (macOS, Linux, Windows) - 搜索、分析和可视化,数据全面洞察平台
437 0
|
缓存 网络协议 Linux
PCIe 以太网芯片 RTL8125B 的 spec 和 Linux driver 分析备忘
本文详细介绍了 Realtek RTL8125B PCIe 以太网芯片的规格以及在 Linux 中的驱动安装和配置方法。通过深入分析驱动源码,可以更好地理解其工作原理和优化方法。在实际应用中,合理配置和优化驱动程序可以显著提升网络性能和稳定性。希望本文能帮助您更好地使用和管理 RTL8125B,以满足各种网络应用需求。
2487 33
|
监控 Linux
Linux基础:文件和目录类命令分析。
总的来说,这些基础命令,像是Linux中藏匿的小矮人,每一次我们使用他们,他们就把我们的指令准确的传递给Linux,让我们的指令变为现实。所以,现在就开始你的Linux之旅,挥动你的命令之剑,探索这个充满神秘而又奇妙的世界吧!
389 19
|
监控 Shell Linux
Android调试终极指南:ADB安装+多设备连接+ANR日志抓取全流程解析,覆盖环境变量配置/多设备调试/ANR日志分析全流程,附Win/Mac/Linux三平台解决方案
ADB(Android Debug Bridge)是安卓开发中的重要工具,用于连接电脑与安卓设备,实现文件传输、应用管理、日志抓取等功能。本文介绍了 ADB 的基本概念、安装配置及常用命令。包括:1) 基本命令如 `adb version` 和 `adb devices`;2) 权限操作如 `adb root` 和 `adb shell`;3) APK 操作如安装、卸载应用;4) 文件传输如 `adb push` 和 `adb pull`;5) 日志记录如 `adb logcat`;6) 系统信息获取如屏幕截图和录屏。通过这些功能,用户可高效调试和管理安卓设备。
11987 2
|
数据管理 Linux iOS开发
Splunk Enterprise 9.4.1 (macOS, Linux, Windows) 发布 - 机器数据管理和分析
Splunk Enterprise 9.4.1 (macOS, Linux, Windows) 发布 - 机器数据管理和分析
506 0
Splunk Enterprise 9.4.1 (macOS, Linux, Windows) 发布 - 机器数据管理和分析
|
存储 运维 监控
Linux--深入理与解linux文件系统与日志文件分析
深入理解 Linux 文件系统和日志文件分析,对于系统管理员和运维工程师来说至关重要。文件系统管理涉及到文件的组织、存储和检索,而日志文件则记录了系统和应用的运行状态,是排查故障和维护系统的重要依据。通过掌握文件系统和日志文件的管理和分析技能,可以有效提升系统的稳定性和安全性。
599 7