下载 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();
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
其中有七个函数较为重要,分别为:
setup_arch(&command_line);
mm_init();
sched_init();
init_IRQ();
console_init();
vfs_caches_init();
rest_init();
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
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系统开发」