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

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 函数。

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

手把手教你分析 Linux 启动流程_嵌入式_02

2、mm_init

内存初始化函数

linux4.14/init/main.c

手把手教你分析 Linux 启动流程_嵌入式_03

3、sched_init

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

linux4.14/kernel/sched/core.c

手把手教你分析 Linux 启动流程_嵌入式_04

4、init_IRQ

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

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

手把手教你分析 Linux 启动流程_嵌入式_05


5、console_init

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

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

linux4.14/kernel/printk/printk.c

手把手教你分析 Linux 启动流程_嵌入式_06

6、vfs_caches_init

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

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

linux4.14/fs/dcache.c

手把手教你分析 Linux 启动流程_嵌入式_07

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

linux4.14/fs/namespace.c

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

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 启动流程_嵌入式_09

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

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

·················· END ··················