電子工程網(wǎng)
標(biāo)題: Linux內(nèi)核啟動流程-迅為IMX6ULL開發(fā)板(二) [打印本頁]
作者: 就是塔塔 時間: 2020-7-20 10:18
標(biāo)題: Linux內(nèi)核啟動流程-迅為IMX6ULL開發(fā)板(二)
基于迅為-IMX6ULL開發(fā)板 Linux內(nèi)核啟動(三):內(nèi)核初始化
start_kernel是所有Linux平臺進入系統(tǒng)內(nèi)核初始化后的入口函數(shù),它主要完成剩余的與 硬件平臺相關(guān)的初始化工作,在進行一系列與內(nèi)核相關(guān)的初始化后,調(diào)用第一個用戶進程- init 進程并等待用戶進程的執(zhí)行,這樣整個 Linux內(nèi)核便啟動完畢。
30.3.1 start_kernel 函數(shù)start_kernel 通過調(diào)用眾多的子函數(shù)來完成 Linux 啟動之前的一些初始化工作,由于start_kernel 函數(shù)里面調(diào)用的子函數(shù)太多,而這些子函數(shù)又很復(fù)雜,因此我們簡單介紹一下一些重要的子函數(shù)。start_kernel 函數(shù)定義在文件 init/main.c中。精簡并添加注釋后的 start_kernel 函數(shù)內(nèi)容如下:
asmlinkage __visible void __init start_kernel(void)
{
char *command_line;
char *after_dashes;
lockdep_init(); /* lockdep 是死鎖檢測模塊,此函數(shù)會初始化
* 兩個 hash 表。此函數(shù)要求盡可能早的執(zhí)行!
*/
set_task_stack_end_magic(&init_task); /* 設(shè)置任務(wù)棧結(jié)束魔術(shù)數(shù),
*用于棧溢出檢測
*/
smp_setup_processor_id(); /* 跟 SMP 有關(guān)(多核處理器),設(shè)置處理器 ID。
* 有很多資料說 ARM 架構(gòu)下此函數(shù)為空函數(shù),那是因
* 為他們用的老版本 Linux,而那時候 ARM 還沒有多
* 核處理器。
*/
debug_objects_early_init(); /* 做一些和 debug 有關(guān)的初始化 */
boot_init_stack_canary(); /* 棧溢出檢測初始化 */
cgroup_init_early(); /* cgroup 初始化,cgroup 用于控制 Linux 系統(tǒng)資源*/
local_irq_disable(); /* 關(guān)閉當(dāng)前 CPU 中斷 */
early_boot_irqs_disabled = true;
/*
* 中斷關(guān)閉期間做一些重要的操作,然后打開中斷
*/
boot_cpu_init(); /* 跟 CPU 有關(guān)的初始化 */
page_address_init(); /* 頁地址相關(guān)的初始化 */
pr_notice("%s", linux_banner);/* 打印 Linux 版本號、編譯時間等信息 */
setup_arch(&command_line); /* 架構(gòu)相關(guān)的初始化,此函數(shù)會解析傳遞進來的
* ATAGS 或者設(shè)備樹(DTB)文件。會根據(jù)設(shè)備樹里面
* 的 model 和 compatible 這兩個屬性值來查找
* Linux 是否支持這個單板。此函數(shù)也會獲取設(shè)備樹
* 中 chosen 節(jié)點下的 bootargs 屬性值來得到命令
* 行參數(shù),也就是 uboot 中的 bootargs 環(huán)境變量的
* 值,獲取到的命令行參數(shù)會保存到
*command_line 中。
*/
mm_init_cpumask(&init_mm); /* 看名字,應(yīng)該是和內(nèi)存有關(guān)的初始化 */
setup_command_line(command_line); /* 好像是存儲命令行參數(shù) */
setup_nr_cpu_ids(); /* 如果只是 SMP(多核 CPU)的話,此函數(shù)用于獲取
* CPU 核心數(shù)量,CPU 數(shù)量保存在變量
* nr_cpu_ids 中。
*/
setup_per_cpu_areas(); /* 在 SMP 系統(tǒng)中有用,設(shè)置每個 CPU 的 per-cpu 數(shù)據(jù) */
smp_prepare_boot_cpu();
build_all_zonelists(NULL, NULL); /* 建立系統(tǒng)內(nèi)存頁區(qū)(zone)鏈表 */
page_alloc_init(); /* 處理用于熱插拔 CPU 的頁 */
/* 打印命令行信息 */
pr_notice("Kernel command line: %s\n", boot_command_line);
parse_early_param(); /* 解析命令行中的 console 參數(shù) */
after_dashes = parse_args("Booting kernel",
static_command_line, __start___param,
__stop___param - __start___param,
-1, -1, &unknown_bootoption);
if (!IS_ERR_OR_NULL(after_dashes))
parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
set_init_arg);
jump_label_init();
setup_log_buf(0); /* 設(shè)置 log 使用的緩沖區(qū)*/
pidhash_init(); /* 構(gòu)建 PID 哈希表,Linux 中每個進程都有一個 ID,
* 這個 ID 叫做 PID。通過構(gòu)建哈希表可以快速搜索進程
* 信息結(jié)構(gòu)體。
*/
vfs_caches_init_early(); /* 預(yù)先初始化 vfs(虛擬文件系統(tǒng))的目錄項和
* 索引節(jié)點緩存
*/
sort_main_extable(); /* 定義內(nèi)核異常列表 */
trap_init(); /* 完成對系統(tǒng)保留中斷向量的初始化 */
mm_init(); /* 內(nèi)存管理初始化 */
sched_init(); /* 初始化調(diào)度器,主要是初始化一些結(jié)構(gòu)體 */
preempt_disable(); /* 關(guān)閉優(yōu)先級搶占 */
if (WARN(!irqs_disabled(), /* 檢查中斷是否關(guān)閉,如果沒有的話就關(guān)閉中斷 */
"Interrupts were enabled *very* early, fixing it\n"))
local_irq_disable();
idr_init_cache(); /* IDR 初始化,IDR 是 Linux 內(nèi)核的整數(shù)管理機
* 制,也就是將一個整數(shù) ID 與一個指針關(guān)聯(lián)起來。
*/
rcu_init(); /* 初始化 RCU,RCU 全稱為 Read Copy Update(讀-拷貝修改) */
trace_init(); /* 跟蹤調(diào)試相關(guān)初始化 */
context_tracking_init();
radix_tree_init(); /* 基數(shù)樹相關(guān)數(shù)據(jù)結(jié)構(gòu)初始化 */
early_irq_init(); /* 初始中斷相關(guān)初始化,主要是注冊 irq_desc 結(jié)構(gòu)體變
* 量,因為 Linux 內(nèi)核使用 irq_desc 來描述一個中斷。
*/
init_IRQ(); /* 中斷初始化 */
tick_init(); /* tick 初始化 */
rcu_init_nohz();
init_timers(); /* 初始化定時器 */
hrtimers_init(); /* 初始化高精度定時器 */
softirq_init(); /* 軟中斷初始化 */
timekeeping_init();
time_init(); /* 初始化系統(tǒng)時間 */
sched_clock_postinit();
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(); /* slab 初始化,slab 是 Linux 內(nèi)存分配器 */
console_init(); /* 初始化控制臺,之前 printk 打印的信息都存放
* 緩沖區(qū)中,并沒有打印出來。只有調(diào)用此函數(shù)
* 初始化控制臺以后才能在控制臺上打印信息。
*/
if (panic_later)
panic("Too many boot %s vars at `%s'", panic_later,
panic_param);
lockdep_info(); /* 如果定義了宏 CONFIG_LOCKDEP,那么此函數(shù)打印一些信息。*/
locking_selftest() /* 鎖自測 */
......
page_ext_init();
debug_objects_mem_init();
kmemleak_init(); /* kmemleak 初始化,kmemleak 用于檢查內(nèi)存泄漏 */
setup_per_cpu_pageset();
numa_policy_init();
if (late_time_init)
late_time_init();
sched_clock_init();
calibrate_delay();/* 測定 BogoMIPS 值,可以通過 BogoMIPS 來判斷 CPU 的性能
* BogoMIPS 設(shè)置越大,說明 CPU 性能越好。
*/
pidmap_init(); /* PID 位圖初始化 */
anon_vma_init(); /* 生成 anon_vma slab 緩存 */
acpi_early_init();
......
thread_info_cache_init();
cred_init(); /* 為對象的每個用于賦予資格(憑證) */
fork_init(); /* 初始化一些結(jié)構(gòu)體以使用 fork 函數(shù) */
proc_caches_init(); /* 給各種資源管理結(jié)構(gòu)分配緩存 */
buffer_init(); /* 初始化緩沖緩存*/
key_init(); /* 初始化密鑰*/
security_init(); /* 安全相關(guān)初始化*/
dbg_late_init();
vfs_caches_init(totalram_pages); /* 為 VFS 創(chuàng)建緩存 */
signals_init(); /* 初始化信號*/
page_writeback_init(); /* 頁回寫初始化*/
proc_root_init(); /* 注冊并掛載 proc 文件系統(tǒng) */
nsfs_init();
cpuset_init(); /* 初始化 cpuset,cpuset 是將 CPU 和內(nèi)存資源以邏輯性
* 和層次性集成的一種機制,是 cgroup 使用的子系統(tǒng)之一
*/
cgroup_init(); /* 初始化 cgroup */
taskstats_init_early(); /* 進程狀態(tài)初始化 */
delayacct_init();
check_bugs(); /* 檢查寫緩沖一致性 */
acpi_subsystem_init();
sfi_init_late();
if (efi_enabled(EFI_RUNTIME_SERVICES)) {
efi_late_init();
efi_free_boot_services();
}
ftrace_init();
rest_init(); /* rest_init 函數(shù) */
}
start_kernel 里面調(diào)用了大量的函數(shù),每一個函數(shù)都是一個龐大的知識點,如果想要學(xué)習(xí)Linux 內(nèi)核,那么這些函數(shù)就需要去詳細的研究。本教程注重于嵌入式 Linux 入門,因此不會去講太多關(guān)于 Linux 內(nèi)核的知識。start_kernel 函數(shù)最后調(diào)用了 rest_init,接下來簡單看一下rest_init函數(shù)。
30.3.2 rest_init 函數(shù)rest_init 函數(shù)定義在文件 init/main.c 中,函數(shù)內(nèi)容如下:
383 static noinline void __init_refok rest_init(void)
384 {
385 int pid;
386
387 rcu_scheduler_starting();
388 smpboot_thread_init();
389 /*
390 * We need to spawn init first so that it obtains pid 1, however
391 * the init task will end up wanting to create kthreads, which,
392 * if we schedule it before we create kthreadd, will OOPS.
393 */
394 kernel_thread(kernel_init, NULL, CLONE_FS);
395 numa_default_policy();
396 pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
397 rcu_read_lock();
398 kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
399 rcu_read_unlock();
400 complete(&kthreadd_done);
401
402 /*
403 * The boot idle thread must execute schedule()
404 * at least once to get things moving:
405 */
406 init_idle_bootup_task(current);
407 schedule_preempt_disabled();
408 /* Call into cpu_idle with preempt disabled */
409 cpu_startup_entry(CPUHP_ONLINE);
410 }
第 387 行,調(diào)用函數(shù) rcu_scheduler_starting,啟動 RCU 鎖調(diào)度器。
第 394 行,調(diào)用函數(shù) kernel_thread 創(chuàng)建 kernel_init 線程,也就是大名鼎鼎的 init 內(nèi)核進程。init 進程的 PID 為 1。init 進程一開始是內(nèi)核進程(也就是運行在內(nèi)核態(tài)),后面 init 進程會在根文件系統(tǒng)中查找名為“init”這個程序,這個“init”程序處于用戶態(tài),通過運行這個“init”程序,init 進程就會實現(xiàn)從內(nèi)核態(tài)到用戶態(tài)的轉(zhuǎn)變。
第 396 行,調(diào)用函數(shù) kernel_thread 創(chuàng)建 kthreadd 內(nèi)核進程,此內(nèi)核進程的 PID 為 2。kthreadd進程負責(zé)所有內(nèi)核進程的調(diào)度和管理。
第 409 行,最后調(diào)用函數(shù) cpu_startup_entry 來進入 idle 進程,cpu_startup_entry 會調(diào)用cpu_idle_loop,cpu_idle_loop 是個 while 循環(huán),也就是 idle 進程代碼。idle 進程的 PID 為 0,idle進程叫做空閑進程,如果學(xué)過 FreeRTOS 或者 UCOS 的話應(yīng)該聽說過空閑任務(wù)。idle 空閑進程就和空閑任務(wù)一樣,當(dāng) CPU 沒有事情做的時候就在 idle 空閑進程里面“瞎逛游”,反正就是給CPU 找點事做。當(dāng)其他進程要工作的時候就會搶占 idle 進程,從而奪取 CPU 使用權(quán)。其實大家應(yīng)該可以看到 idle 進程并沒有使用 kernel_thread 或者 fork 函數(shù)來創(chuàng)建,因為它是有主進程演變而來的。
30.3.3 init 進程kernel_init 函數(shù)就是 init 進程具體做的工作,定義在文件 init/main.c 中,函數(shù)內(nèi)容如下:
928 static int __ref kernel_init(void *unused)
929 {
930 int ret;
931
932 kernel_init_freeable(); /* init 進程的一些其他初始化工作 */
933 /* need to finish all async __init code before freeing the memory */
934 async_synchronize_full(); /* 等待所有的異步調(diào)用執(zhí)行完成 */
935 free_initmem(); /* 釋放 init 段內(nèi)存*/
936 mark_rodata_ro();
937 system_state = SYSTEM_RUNNING; /* 標(biāo)記系統(tǒng)正在運行*/
938 numa_default_policy();
939
940 flush_delayed_fput();
941
942 if (ramdisk_execute_command) {
943 ret = run_init_process(ramdisk_execute_command);
944 if (!ret)
945 return 0;
946 pr_err("Failed to execute %s (error %d)\n",
947 ramdisk_execute_command, ret);
948 }
949
950 /*
951 * We try each of these until one succeeds.
952 *
953 * The Bourne shell can be used instead of init if we are
954 * trying to recover a really broken machine.
955 */
956 if (execute_command) {
957 ret = run_init_process(execute_command);
958 if (!ret)
959 return 0;
960 panic("Requested init %s failed (error %d).",
961 execute_command, ret);
962 }
963 if (!try_to_run_init_process("/sbin/init") ||
964 !try_to_run_init_process("/etc/init") ||
965 !try_to_run_init_process("/bin/init") ||
966 !try_to_run_init_process("/bin/sh"))
967 return 0;
968
969 panic("No working init found. Try passing init= option to
kernel. "
970 "See Linux Documentation/init.txt for guidance.");
971 }
第 932 行,kernel_init_freeable 函數(shù)用于完成 init 進程的一些其他初始化工作,稍后再來具體看一下此函數(shù)。
第 940 行,ramdisk_execute_command 是一個全局的 char 指針變量,此變量值“/init”,也就是根目錄下的 init 程序。ramdisk_execute_command 也可以通過 uboot 傳遞,在bootargs 中使用“rdinit=xxx”即可,xxx 為具體的 init 程序名字。
第 943 行,如果存在“/init”程序的話就通過函數(shù) run_init_process 來運行此程序。
第 956 行,如果 ramdisk_execute_command 為空的話就看 execute_command 是否為空,反正不管如何一定要在根文件系統(tǒng)中找到一個可運行的 init 程序。execute_command 的值是通過uboot 傳遞,在 bootargs 中使用“init=xxxx”就可以了,比如“init=/linuxrc”表示根文件系統(tǒng)中的 linuxrc 就是要執(zhí)行的用戶空間 init 程序。
第 963~966 行,如果 ramdisk_execute_command 和 execute_command 都為空,那么就依次查找“/sbin/init”、“/etc/init”、“/bin/init”和“/bin/sh”,這四個相當(dāng)于備用 init 程序,如果這四個也不存在,那么 Linux 啟動失敗!
第 969 行,如果以上步驟都沒有找到用戶空間的 init 程序,那么就提示錯誤發(fā)生!
最后來簡單看一下 kernel_init_freeable 函數(shù),前面說了,kernel_init 會調(diào)用此函數(shù)來做一些init 進程初始化工作。kernel_init_freeable 定義在文件 init/main.c 中,縮減后的函數(shù)內(nèi)容如下:
973 static noinline void __init kernel_init_freeable(void)
974 {
975 /*
976 * Wait until kthreadd is all set-up.
977 */
978 wait_for_completion(&kthreadd_done);/* 等待 kthreadd 進程準備就緒 */
......
998
999 smp_init(); /* SMP 初始化 */
1000 sched_init_smp(); /* 多核(SMP)調(diào)度初始化 */
1001
1002 do_basic_setup(); /* 設(shè)備初始化都在此函數(shù)中完成 */
1003
1004 /* Open the /dev/console on the rootfs, this should never fail */
1005 if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
1006 pr_err("Warning: unable to open an initial console.\n");
1007
1008 (void) sys_dup(0);
1009 (void) sys_dup(0);
1010 /*
1011 * check if there is an early userspace init. If yes, let it do
1012 * all the work
1013 */
1014
1015 if (!ramdisk_execute_command)
1016 ramdisk_execute_command = "/init";
1017
1018 if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0) {
1019 ramdisk_execute_command = NULL;
1020 prepare_namespace();
1021 }
1022
1023 /*
1024 * Ok, we have completed the initial bootup, and
1025 * we're essentially up and running. Get rid of the
1026 * initmem segments and start the user-mode stuff..
1027 *
1028 * rootfs is available now, try loading the public keys
1029 * and default modules
1030 */
1031
1032 integrity_load_keys();
1033 load_default_modules();
1034 }
第 1002 行,do_basic_setup 函數(shù)用于完成 Linux 下設(shè)備驅(qū)動初始化工作!非常重要。 do_basic_setup 會調(diào)用 driver_init 函數(shù)完成 Linux 下驅(qū)動模型子系統(tǒng)的初始化。
第 1005 行,打開設(shè)備“/dev/console”,在 Linux 中一切皆為文件!因此“/dev/console”也是一個文件,此文件為控制臺設(shè)備。每個文件都有一個文件描述符,此處打開的“/dev/console”文件描述符為 0,作為標(biāo)準輸入(0)。
第 1008 和 1009 行,sys_dup 函數(shù)將標(biāo)準輸入(0)的文件描述符復(fù)制了 2 次,一個作為標(biāo)準輸出(1),一個作為標(biāo)準錯誤(2)。這樣標(biāo)準輸入、輸出、錯誤都是/dev/console 了。console 通過uboot 的 bootargs 環(huán)境變量設(shè)置,“console=ttymxc0,115200”表示將/dev/ttymxc0 設(shè)置為 console,也就是 I.MX6U 的串口 1。當(dāng)然,也可以設(shè)置其他的設(shè)備為 console,比如虛擬控制臺 tty1,設(shè)置 tty1 為 console 就可以在 LCD 屏幕上看到系統(tǒng)的提示信息。
第 1020 行,調(diào)用函數(shù) prepare_namespace 來掛載根文件系統(tǒng)。跟文件系統(tǒng)也是由命令行參數(shù)指定的,也就是 uboot 的 bootargs 環(huán)境變量。比如“root=/dev/mmcblk1p2 rootwait rw”就表示根文件系統(tǒng)在/dev/mmcblk1p2 中,也就是 EMMC 的分區(qū) 2 中。
Linux 內(nèi)核啟動流程就分析到這里,Linux 內(nèi)核最終是需要和根文件系統(tǒng)打交道的,需要掛載根文件系統(tǒng),并且執(zhí)行根文件系統(tǒng)中的 init 程序,以此來進去用戶態(tài)。這里就正式引出了根文件系統(tǒng),根文件系統(tǒng)也是我們系統(tǒng)移植的最后一片拼圖。Linux 移植三巨頭:uboot、Linux kernel、rootfs(根文件系統(tǒng))。關(guān)于根文件系統(tǒng)后面章節(jié)會詳細的講解,這里我們只需要知道 Linux內(nèi)核移植完成以后還需要構(gòu)建根文件系統(tǒng)即可。
歡迎光臨 電子工程網(wǎng) (http://m.qingdxww.cn/) |
Powered by Discuz! X3.4 |
主站蜘蛛池模板:
亚洲高清在线观看视频
|
国产做国产爱免费视频
|
真实国产乱视频国语
|
狠狠丁香激情久久综合
|
狠狠狠色丁香婷婷综合久久五月
|
亚洲三级国产
|
依欧美视频
|
国内精品久久久久激情影院
|
日韩国产午夜一区二区三区
|
91精品免费视频
|
日本一区不卡视频
|
亚洲永久视频
|
青青青久久
|
国产三级精品三级在专区
|
扒开粉嫩进进出出
|
玖玖成人
|
国产亚洲欧美视频
|
四库国产精品成人
|
色综色天天综合网
|
两个人免费观看视频
|
欧美日韩91
|
欧美另类在线观看
|
日韩视频91
|
免费观看男生自噜视频
|
亚洲欧美日韩精品一区
|
亚洲欧洲精品成人久久曰影片
|
日韩欧美在线播放
|
久久亚洲这里只有精品18
|
老司机免费精品视频
|
欧美黑人双插
|
四虎影酷|
国产在线观看精品香蕉v区
国产在线观看精品
|
黄色在线观看视频网站
|
亚洲欧美日韩视频一区
|
女男羞羞视频网站免费
|
天堂网在线.www天堂在线视频
|
精品久久一区二区三区
|
亚洲国产日韩在线一区
|
久久国产精品一区二区三区
|
国产最新自拍
|
在线观看日本www
|