Linux内核分析课程实验三:跟踪分析Linux内核的启动过程

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跟踪调试的过程如下:
break start_kernelbreak rest_init
break kernel_threadbreak kernel_initbreak run_init_process完成启动

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值