Linux内核分析课程实验三:跟踪分析Linux内核的启动过程
白杰 原创作品转载请注明出处《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000
内核使用的是课程视屏中使用的linux-3.18.6
x86 CPU 上电后从0xffff0读第一条指令,CS:EIP=0xf000:fff0,这是一条跳转指令,跳转到BIOS中。BIOS完成硬件检测和初始化,然后寻找主引导记录,引导程序bootloader负责操作系统初始化,启动操作系统。
1.内核启动过程包括start-kernel()之前和之后,之前做的全部是初始化的汇编指令,完成一些最基本的初始化与环境设置工作,比如内核代码载入内存并解压缩,CPU的最基本初始化,为c代码的运行设置环境。在start_kernel()中linux将完成整个系统内核的初始化,内核初始化的最后一步是启动init进程,这是系统的0号进程,是所有进程的父进程。
2. 跟踪分析linux内核的启动过程从init/main.c中的start_kernel()函数开始。
asmlinkage __visible void __init start_kernel(void)//内核函数入口
{
......
......
/* Do the rest non-__init'ed, we're now alive */
rest_init();
}
在rest_init()
之前的代码完成一些初始化设置的工作,如trap_init()
涉及中断相关的设置;set_task_stack_end_magic(&init_task)
涉及堆栈相关的设置;mm_init()
涉及内存管理相关的设置; sched_init()
涉及进程调度相关的设置。其他的很多函数功能比较繁琐,不做讨论。rest_init()
其他初始化函数,该函数将创建linux系统中的1号进程,默认的是为根目录下的init程序。
代码分析如下:
static noinline void __init_refok rest_init(void)
{
int pid;
rcu_scheduler_starting();
/*
* We need to spawn init first so that it obtains pid 1, however
* the init task will end up wanting to create kthreads, which, if
* we schedule it before we create kthreadd, will OOPS.
*/
kernel_thread(kernel_init, NULL, CLONE_FS);
numa_default_policy();
pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
rcu_read_lock();
kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
rcu_read_unlock();
complete(&kthreadd_done);
/*
* The boot idle thread must execute schedule()
* at least once to get things moving:
*/
init_idle_bootup_task(current);
schedule_preempt_disabled();
/* Call into cpu_idle with preempt disabled */
cpu_startup_entry(CPUHP_ONLINE);
}
kernel_thread(kernel_init, NULL, CLONE_FS)
创建一个线程kernel_init
static int __ref kernel_init(void *unused)
{
int ret;
kernel_init_freeable();
/* need to finish all async __init code before freeing the memory */
async_synchronize_full();
free_initmem();
mark_rodata_ro();
system_state = SYSTEM_RUNNING;
numa_default_policy();
flush_delayed_fput();
if (ramdisk_execute_command) {
ret = run_init_process(ramdisk_execute_command);
if (!ret)
return 0;
pr_err("Failed to execute %s (error %d)\n",
ramdisk_execute_command, ret);
}
/*
* 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) {
ret = run_init_process(execute_command);
if (!ret)
return 0;
pr_err("Failed to execute %s (error %d). Attempting defaults...\n",
execute_command, ret);
}
if (!try_to_run_init_process("/sbin/init") ||
!try_to_run_init_process("/etc/init") ||
!try_to_run_init_process("/bin/init") ||
!try_to_run_init_process("/bin/sh"))
return 0;
panic("No working init found. Try passing init= option to kernel. "
"See Linux Documentation/init.txt for guidance.");
}
kernel_init
通过run_init_process(execute_command)
和 try_to_run_init_process()
来启动init,根据if条件的判断只有一个init会被启动。
static int run_init_process(const char *init_filename)
{
argv_init[0] = init_filename;
return do_execve(getname_kernel(init_filename),
(const char __user *const __user *)argv_init,
(const char __user *const __user *)envp_init);
}
static int try_to_run_init_process(const char *init_filename)
{
int ret;
ret = run_init_process(init_filename);
if (ret && ret != -ENOENT) {
pr_err("Starting init: %s exists but couldn't execute it (error %d)\n",
init_filename, ret);
}
return ret;
}
run_init_process(execute_command)
和 try_to_run_init_process()
通过do_execve()
系统调用来启动。
3.按照实验指导 的步骤完成gdb跟踪调试的过程如下: