内核的启动过程

来源:互联网 发布:photoshop cs6 for mac 编辑:程序博客网 时间:2024/06/10 08:38


内核的启动过程——Linux内核分析课程

主要了解了CPU内核源码,看了大概的结构。

cd LinuxKernel/qemu -kernel linux-3.18.6/arch/x86/boot/bzImage -initrd rootfs.img

内核启动完成后进入menu程序
执行结果如图

使用gdb跟踪调试内核

然后就是通过

(gdb)file linux-3.18.6/vmlinux

在gdb界面中targe remote之前加载符号表

(gdb)target remote:1234 

建立gdb和gdbserver之间的连接,按c 让qemu上的Linux继续运行

(gdb)break start_kernel 

设置断点
下面分析一下代码,了解内核的启动过程

393static noinline void __init_refok rest_init(void)394{395 int pid;396397 rcu_scheduler_starting();398 /*399  * We need to spawn init first so that it obtains pid 1, however400  * the init task will end up wanting to create kthreads, which, if401  * we schedule it before we create kthreadd, will OOPS.402  */403 kernel_thread(kernel_init, NULL, CLONE_FS);404 numa_default_policy();405 pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);406 rcu_read_lock();407 kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);408 rcu_read_unlock();409 complete(&kthreadd_done);410411 /*412  * The boot idle thread must execute schedule()413  * at least once to get things moving:414  */415 init_idle_bootup_task(current);416 schedule_preempt_disabled();417 /* Call into cpu_idle with preempt disabled */418 cpu_startup_entry(CPUHP_ONLINE);419}

这里面包含了三个进程idle,kernel_init,kthreadd。

其中idle进程(PID = 0), kernel_init进程(PID = 1),kthreadd(PID = 2)
kernel_init是所有用户态进程的祖先,kthreadd是所有内核线程的祖先。
三个进程之间的关系

  • idle进程由系统自动创建, 运行在内核态;
  • kernel_init进程由idle通过kernel_thread创建,在内核空间完成初始化后, 加载init程序, 并最终返回到用户空间;
  • kthreadd进程由idle通过kernel_thread创建,并始终运行在内核空间, 负责所有内核线程的调度和管理;
0 0
原创粉丝点击