- 论坛徽章:
- 0
|
本帖最后由 junnyg 于 2013-04-13 16:13 编辑
回复 1# qxhgd
个人理解:
start_kernel的最后一步会执行rest_init函数
- asmlinkage void __init start_kernel(void)
- {
- ... ...
- /* Do the rest non-__init'ed, we're now alive */
- rest_init();
- }
复制代码 在rest_init函数中会启动一个内核线程执行kernel_init函数,这个内核线程将成为我们所说的init进程- static noinline void __init_refok rest_init(void)
- __releases(kernel_lock)
- {
- ... ...
- kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND);
- ... ...
- /* Call into cpu_idle with preempt disabled */
- cpu_idle();
- }
复制代码 kernel_init执行initrd的加载,并最终调用initrd中的指向的init程序(默认/init,或者启动参数中加“init=/xxx"指定)
- static int __init kernel_init(void * unused)
- {
- ......
- /*
- * check if there is an early userspace init. If yes, let it do all
- * the work
- */
- if (!ramdisk_execute_command)
- ramdisk_execute_command = "/init";
- if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0) {
- ramdisk_execute_command = NULL;
- prepare_namespace();
- }
- /*
- * Ok, we have completed the initial bootup, and
- * we're essentially up and running. Get rid of the
- * initmem segments and start the user-mode stuff..
- */
- init_post();
- return 0;
- }
- /* This is a non __init function. Force it to be noinline otherwise gcc
- * makes it inline to init() and it becomes part of init.text section
- */
- static noinline int init_post(void)
- __releases(kernel_lock)
- {
- /* need to finish all async __init code before freeing the memory */
- async_synchronize_full();
- free_initmem();
- unlock_kernel();
- mark_rodata_ro();
- system_state = SYSTEM_RUNNING;
- numa_default_policy();
- current->signal->flags |= SIGNAL_UNKILLABLE;
- if (ramdisk_execute_command) {
- run_init_process(ramdisk_execute_command);
- printk(KERN_WARNING "Failed to execute %s\n",
- ramdisk_execute_command);
- }
- /*
- * We try each of these until one succeeds.
- *
- * The Bourne shell can be used instead of init if we are
- * trying to recover a really broken machine.
- */
- if (execute_command) {
- run_init_process(execute_command);
- printk(KERN_WARNING "Failed to execute %s. Attempting "
- "defaults...\n", execute_command);
- }
- run_init_process("/sbin/init");
- run_init_process("/etc/init");
- run_init_process("/bin/init");
- run_init_process("/bin/sh");
- panic("No init found. Try passing init= option to kernel. "
- "See Linux Documentation/init.txt for guidance.");
- }
复制代码 而start_kernle函数的最终流程是调用cpu_idle()函数进入死循环,成为系统中的idle进程
|
|