linux启动init过程分析

init 进程号称天字一号进程,做系统的小伙伴应该都有了解。那么,这个一号进程是如何被启动的呢? 今天就来一起简单看看这个进程的前世今生。
因为目前运行Linux最多的就是ARM设备了,我们就基于Linux的master分支简单分析下ARM 32架构下的启动过程。
kernel的执行起点是stext函数,定义于arch/arm/kernel/head.S。我们就从这个函数开始看看init如何被启动的。

ENTRY(stext)
......
    /*这里将__mmap_switched 函数的地址赋给了r13寄存器,
    这个函数会在MMU被使能之后执行。
    MMU是Memory Management Unit的缩写,中文名是内存管理单元,
    同时也负责虚拟地址映射为物理地址,以及提供硬件机制的内存访问授权*/
    ldr r13, =__mmap_switched       @ address to jump to after
                        @ mmu has been enabled
    badr    lr, 1f              @ return (PIC) address
......
/*接下来执行__enable_mmu 函数*/
1:  b   __enable_mmu
ENDPROC(stext)

下面来看__enable_mmu 函数做了什么

__enable_mmu:
......
/*主要调用了__turn_mmu_on 来打开MMU ,让CPU进入虚拟内存的运行阶段。*/
        b       __turn_mmu_on
ENDPROC(__enable_mmu)

ENTRY(__turn_mmu_on)
......
/*这里将r13保存的函数地址赋给PC指针,即IP寄存器。 RET指令的内部操作是:栈顶字单元出栈,其值赋给IP寄存器*/
        mov     r3, r13
        ret     r3
__turn_mmu_on_end:
ENDPROC(__turn_mmu_on)

接下来就是运行__mmap_switched 方法的事情了。那我们就来看看__mmap_switched 是怎样的。这个方法位于arch/arm/kernel/head-common.S

        __INIT
__mmap_switched:
......
/*这么愉快地就要跳转到start_kernel 了,马上就进入了C的世界*/
        b       start_kernel
ENDPROC(__mmap_switched)

start_kernel 位于init/main.c

asmlinkage __visible void __init start_kernel(void)
{
......
    /*start_kernel会进行一堆初始化的工作。最后通过rest_init来启动init进程*/
    /* Do the rest non-__init'ed, we're now alive */
    rest_init();
}

static noinline void __ref rest_init(void)
{
    struct task_struct *tsk;
    int pid;

    rcu_scheduler_starting();
    //这里会先创建init进程,保证它的PID为1,不过会停下等待kthreadd的创建。
    //kernel_init-》kernel_init_freeable-》wait_for_completion(&kthreadd_done)
    //kthreadd进程由idle通过kernel_thread创建,并始终运行在内核空间, 负责所有内核线程的调度和管理 
    /*
     * 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.
     */
    pid = kernel_thread(kernel_init, NULL, CLONE_FS);
    /*
     * Pin init on the boot CPU. Task migration is not properly working
     * until sched_init_smp() has been run. It will set the allowed
     * CPUs for init to the non isolated CPUs.
     */
    rcu_read_lock();
    tsk = find_task_by_pid_ns(pid, &init_pid_ns);
    set_cpus_allowed_ptr(tsk, cpumask_of(smp_processor_id()));
    rcu_read_unlock();
    //创建kthreadd,获取kthreadd线程信息,获取完成,说明创建成功
    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();

    /*
     * Enable might_sleep() and smp_processor_id() checks.
     * They cannot be enabled earlier because with CONFIG_PRREMPT=y
     * kernel_thread() would trigger might_sleep() splats. With
     * CONFIG_PREEMPT_VOLUNTARY=y the init task might have scheduled
     * already, but it's stuck on the kthreadd_done completion.
     */
    system_state = SYSTEM_SCHEDULING;
//通知kernel_init kthreadd创建完成
    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_init如何启动init

static int __ref kernel_init(void *unused)
{
    int ret;

    kernel_init_freeable();
    /* 在释放内存前,必须完成所有的异步 __init 代码
    need to finish all async __init code before freeing the memory */
    async_synchronize_full();
//释放init内存
    free_initmem();
    mark_readonly();
    system_state = SYSTEM_RUNNING;
    //设置numa内存访问策略为默认, NUMA技术主要用于服务器
    numa_default_policy();

    rcu_end_inkernel_boot();

    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);
    }
//下面就是运行init程序了,会尝试所有可能的情况,包括init启动时候的参数,和各个目录下的init文件/sbin/init  /etc/init等, 最终调用do_execve去执行init文件。 
    /*
     * 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;
        panic("Requested init %s failed (error %d).",
              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/admin-guide/init.rst for guidance.");
}

就这样, init进程就被执行起来了。 不过到目前为止,init进程依然是一个内核进程,怎么变成用户空间进程的事情是在init程序里面实现的,这个每个不同的Linux发行版有不同的操作。不在本文讨论范围内。

完。

你可能感兴趣的:(linux启动init过程分析)