手把手教你分析 Linux 啟動(dòng)流程
下載 Linux 內(nèi)核網(wǎng)址:
https://www.kernel.org/
最新 Linux 內(nèi)核是 5.15 版本?,F(xiàn)在常用 Linux 內(nèi)核源碼為4.14、4.19、4.9 等版本,其中 4.14 版本源碼壓縮包大概 90+M,解壓后 700+M,合計(jì) 61350 個(gè)文件。如此眾多的文件,用 source insight 或者 VSCode 查看都會(huì)比較卡,所以可以采用在線(xiàn)查看的方式。
在線(xiàn)查看 Linux 內(nèi)核源碼網(wǎng)址:
https://elixir.bootlin.com/linux/latest/source
在線(xiàn)查看 Android 源碼:
http://androidxref.com/
Android系統(tǒng)是基于Linux 內(nèi)核的,最底層為L(zhǎng)inux內(nèi)核,源碼量翻很多倍。所以用軟件看安卓源碼更卡,可以使用在線(xiàn)網(wǎng)址看源碼。
我們知道,Linux 系統(tǒng)的啟動(dòng),前面有一個(gè)啟動(dòng)引導(dǎo)程序 bootloader,比如常用的 uboot,本文不分析 uboot 的啟動(dòng),只放一張流程圖:
本文主要講解當(dāng)從 bootloader 跳轉(zhuǎn)到 Linux 系統(tǒng)的啟動(dòng)函數(shù) start_kernel 后,此函數(shù)對(duì)系統(tǒng)初始化的流程。
在 linux4.14/arch/arm/kernel/head.S 文件中,是最后匯編階段的初始化,而后會(huì)跳轉(zhuǎn)到 main.c 文件的 start_kernel 函數(shù),在此做 Linux 啟動(dòng)初始化,在這個(gè)函數(shù)中會(huì)調(diào)用將近100個(gè)函數(shù)去完成 Linux 系統(tǒng)的初始化,調(diào)用函數(shù)如下(不同內(nèi)核版本,順序和細(xì)節(jié)有變化):
linux4.14/init/main.c,start_kernel 函數(shù)。
- 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();
- }
其中有七個(gè)函數(shù)較為重要,分別為:
- setup_arch(&command_line);
- mm_init();
- sched_init();
- init_IRQ();
- console_init();
- vfs_caches_init();
- rest_init();
1、setup_arch(&command_line)
此函數(shù)是系統(tǒng)架構(gòu)初始化函數(shù),處理 uboot 傳遞進(jìn)來(lái)的參數(shù),不同的架構(gòu)進(jìn)行不同的初始化,也就是說(shuō)每個(gè)架構(gòu)都會(huì)有一個(gè) setup_arch 函數(shù)。
linux4.14/arch/arm/kernel/setup.c
2、mm_init
內(nèi)存初始化函數(shù)
linux4.14/init/main.c
3、sched_init
核心進(jìn)程調(diào)度器初始化。Linux 內(nèi)核實(shí)現(xiàn)了四種調(diào)度方式,一般是采用 CFS 調(diào)度方式。作為一個(gè)普適性的操作系統(tǒng),必須考慮各種需求,我們不能只按照中斷優(yōu)先級(jí)或者時(shí)間輪轉(zhuǎn)片來(lái)規(guī)定進(jìn)程運(yùn)行的時(shí)間。作為一個(gè)多用戶(hù)操作系統(tǒng),必須考慮到每個(gè)用戶(hù)的公平性。不能因?yàn)橐粋€(gè)用戶(hù)沒(méi)有高級(jí)權(quán)限,就限制他的進(jìn)程的運(yùn)行時(shí)間,要考慮每個(gè)用戶(hù)擁有公平的時(shí)間。
linux4.14/kernel/sched/core.c
4、init_IRQ
中斷初始化函數(shù),這個(gè)很好理解,大家都用過(guò)中斷。
linux4.14/arch/arm/kernel/irq.c
5、console_init
在這個(gè)函數(shù)初始化之前,你所有寫(xiě)的內(nèi)核打印函數(shù) printk 都打印不出東西。在這個(gè)函數(shù)初始化之前,所有打印都會(huì)存在 buf 里,此函數(shù)初始化以后,會(huì)將 buf里面的數(shù)據(jù)打印出來(lái),你才能在終端看到 printk 打印的東西。
tty 是 Linux 中的終端, _con_initcall_start 和_con_initcall_end 這兩句的意思是執(zhí)行所有兩者之間的 initcall 函數(shù)。
linux4.14/kernel/printk/printk.c
6、vfs_caches_init
虛擬文件系統(tǒng)初始化,比如 sysfs,根文件系統(tǒng)等,就是在這一步進(jìn)行掛載,proc 是內(nèi)核虛擬的,用來(lái)輸出內(nèi)核數(shù)據(jù)結(jié)構(gòu)信息,不算在這里。
vfs虛擬文件系統(tǒng),屏蔽了底層硬件的不同,提供了統(tǒng)一了接口,方便系統(tǒng)的移植和使用。使用戶(hù)在不用更改應(yīng)用代碼的情況下直接移植代碼到其他平臺(tái)。
linux4.14/fs/dcache.c
這里的掛載主要在mnt_init()函數(shù)中:
linux4.14/fs/namespace.c
7、rest_init
這個(gè)函數(shù)可以算是 start_kernel函數(shù)調(diào)用的最后一個(gè)函數(shù),在這里產(chǎn)生了最重要的兩個(gè)內(nèi)核進(jìn)程 kernel_init 和 kthreadd,kernel_init后面會(huì)從內(nèi)核空間跳轉(zhuǎn)到用戶(hù)空間,變成用戶(hù)空間的 init 進(jìn)程,PID=1,而 kthreadd ,PID=2,是內(nèi)核進(jìn)程,專(zhuān)門(mén)用來(lái)監(jiān)聽(tīng)創(chuàng)建內(nèi)核進(jìn)程的請(qǐng)求,它維護(hù)了一個(gè)鏈表,如果有創(chuàng)建內(nèi)核進(jìn)程的需求,就會(huì)在鏈表上創(chuàng)建。
至此,用戶(hù)空間最重要的 init 進(jìn)程已經(jīng)出來(lái),后面用戶(hù)空間的進(jìn)程都由 init進(jìn)程來(lái) fork。如果是安卓系統(tǒng),init 進(jìn)程會(huì) fork 出一個(gè) zygote 進(jìn)程,他是所有安卓系統(tǒng)進(jìn)程的父進(jìn)程。
linux4.14/init.main.c
上圖,400 行創(chuàng)建了 kernel_init 進(jìn)程,412 行創(chuàng)建了 kthreadd 進(jìn)程,這兩個(gè)都是內(nèi)核進(jìn)程。426 行通知 kernel_init 進(jìn)程 kthreadd 已經(jīng)創(chuàng)建完畢。也就是說(shuō),實(shí)際上是 kthreadd 先運(yùn)行,kernel_init 再運(yùn)行。
其余的函數(shù)大家可以參照下面的文章去理解:
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
本文轉(zhuǎn)載自微信公眾號(hào)「嵌入式Linux系統(tǒng)開(kāi)發(fā)」