“Linux内核分析”实验报告(三)跟踪分析Linux内核的启动过程

 

代码分析

asmlinkage __visible void __init start_kernel(void)
{ 
    char*command_line; 
    char *after_dashes;
    /*
     * Need to run as early as possible, to initialize the
     * lockdep hash:
     */
    lockdep_init();/*初始化内核依赖的关系表*/
    set_task_stack_end_magic(&init_task);
    smp_setup_processor_id();/*指定当前的cpu的逻辑号,这个函数对应于对称多处理器的设置,当系统中只有一个cpu的情况,此函数为空,什么也不做*/
    debug_objects_early_init();    /*
     * Set up the the initial canary ASAP:
     */
    boot_init_stack_canary();

    cgroup_init_early();

    local_irq_disable();/*关闭当前CUP中断*/
    early_boot_irqs_disabled = true;/*
 * Interrupts are still disabled. Do necessary setups, then
 * enable them
 */
    boot_cpu_init();/*对于多CPU核的系统来说,设置第一个CPU核为活跃  CPU核。对于单CPU核系统来说,设置CPU核为活跃  CPU核*/
    page_address_init();/*页表结构初始化*/
    pr_notice("%s", linux_banner);/*输出Linux版本信息*/
    setup_arch(&command_line);/*设置与体系结构相关的环境*/
    mm_init_cpumask(&init_mm);
    setup_command_line(command_line);/*保存未改变的comand_line到字符数组static_command_line[] 中。保存  boot_command_line到字符数组saved_command_line[]中*/
    setup_nr_cpu_ids();
    setup_per_cpu_areas();/*每个CPU分配pre-cpu结构内存, 并复制.data.percpu段的数据*/
    smp_prepare_boot_cpu();    /* arch-specific boot-cpu hooks */

    build_all_zonelists(NULL, NULL);
    page_alloc_init();

    pr_notice("Kernel command line: %s\n", boot_command_line);
    parse_early_param();
    after_dashes = parse_args("Booting kernel",
                  static_command_line, __start___param,
                  __stop___param - __start___param,
                  -1, -1, &unknown_bootoption);    if (!IS_ERR_OR_NULL(after_dashes))
        parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
               set_init_arg);

    jump_label_init();    /*
     * These use large bootmem allocations and must precede
     * kmem_cache_init()
     */
    setup_log_buf(0);
    pidhash_init();
    vfs_caches_init_early();
    sort_main_extable();
    trap_init();/*      * trap_init函数完成对系统保留中断向量(异常、非屏蔽中断以及系统调用)* 的初始化*/
    mm_init();    /*
     * Set up the scheduler prior starting any interrupts (such as the
     * timer interrupt). Full topology setup happens at smp_init()
     * time - but meanwhile we still have a functioning scheduler.
     */
    sched_init();/*核心进程调度器初始化,调度器的初始化的优先级要高于任何中断的建立,并且初始化进程0,即idle进程,但是并没有设置idle进程的NEED_RESCHED标志,所以还会继续完成内核初始化剩下的事情。*/
    /*
     * Disable preemption - early bootup scheduling is extremely
     * fragile until we cpu_idle() for the first time.
     */
    preempt_disable();/*进制内核的抢占。使当前进程的   struct thread_info结构  preempt_count成员的值增加1。*/
    if (WARN(!irqs_disabled(),         "Interrupts were enabled *very* early, fixing it\n"))
        local_irq_disable();/*关闭当前CUP中断*/
    idr_init_cache();
    rcu_init();
    context_tracking_init();
    radix_tree_init();    /* init some links before init_ISA_irqs() */
    early_irq_init();
    init_IRQ();/*初始化IRQ中断和终端描述符。*/
    tick_init();
    rcu_init_nohz();
    init_timers();
    hrtimers_init();  /* 对高精度时钟进行初始化 */ 
       softirq_init();/*初始化软中断。*/
    timekeeping_init();/*初始化资源和普通计时器*/
    time_init();/*时间、定时器初始化*/
    sched_clock_postinit();
    perf_event_init();
    profile_init();/*对内核的一个性能测试工具profile进行初始化*/
    call_function_init();
    WARN(!irqs_disabled(), "Interrupts were enabled early\n");
    early_boot_irqs_disabled = false;
    local_irq_enable();

    kmem_cache_init_late();    /*
     * HACK ALERT! This is early. We're enabling the console before
     * we've done PCI setups etc, and console_init() must be aware of
     * this. But we do want output early, in case something goes wrong.
     */
    console_init();/*控制台初始化*/
    if (panic_later)
        panic("Too many boot %s vars at `%s'", panic_later,
              panic_param);

    lockdep_info();/*打印锁依赖信息*/

    /*
     * Need to run this when irqs are enabled, because it wants
     * to self-test [hard/soft]-irqs on/off lock inversion bugs
     * too:
     */
    locking_selftest();#ifdef CONFIG_BLK_DEV_INITRD
    if (initrd_start && !initrd_below_start_ok &&
        page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
        pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n",
            page_to_pfn(virt_to_page((void *)initrd_start)),
            min_low_pfn);
        initrd_start = 0;
    }#endif
    page_cgroup_init();
    debug_objects_mem_init();
    kmemleak_init();
    setup_per_cpu_pageset();
    numa_policy_init();    if (late_time_init)
        late_time_init();
    sched_clock_init();
    calibrate_delay();
    pidmap_init();
    anon_vma_init();
    acpi_early_init();#ifdef CONFIG_X86
    if (efi_enabled(EFI_RUNTIME_SERVICES))
        efi_enter_virtual_mode();#endif#ifdef CONFIG_X86_ESPFIX64
    /* Should be run before the first non-init thread is created */
    init_espfix_bsp();#endif
    thread_info_cache_init();
    cred_init();
    fork_init(totalram_pages); /* 根据物理内存大小计算允许创建进程的数量 */ 
    proc_caches_init();
    buffer_init();
    key_init();
    security_init();
    dbg_late_init();
    vfs_caches_init(totalram_pages);
    signals_init();    /* rootfs populating might need page-writeback */
    page_writeback_init();
    proc_root_init();
    cgroup_init();
    cpuset_init();
    taskstats_init_early();
    delayacct_init();

    check_bugs();   /* 
         * 测试该CPU的各种缺陷,记录检测到的缺陷,以便于内核的其他部分以后可以 
         * 使用它们的工作。 
         */ 

    sfi_init_late();    if (efi_enabled(EFI_RUNTIME_SERVICES)) {
        efi_late_init();
        efi_free_boot_services();
    }

    ftrace_init();    /* Do the rest non-__init'ed, we're now alive */
    rest_init();/*从rest_init开始,Linux开始产生进程*/}

实验总结

从老师的视频,网上查找的资料。再通过做实验,已经对linux内核从开始初始化到init进程的建立有一个初步的概念,说道idle进程就不得不提到init_task.init_task进程在Linux中属于一个比较特殊的进程,它是内核开发者人为制造出来的,而不是其他进程通过do_fork来完成。init_task对象的初始化在内核代码来完成.Linux在无进程概念的情况下将一直从初始化部分的代码执行到start_kernel,然后再到其最后一个函数调用rest_init。从rest_init开始,Linux开始产生进程,因为init_task是静态制造出来的,pid=0,在rest_init函数中,内核将通过下面的代码产生第一个真正的进程(pid=1):

kernel_thread(kernel_init, NULL, CLONE_FS);

而此时init_task的任务基本上已经完全结束了,它将成为为一个idle task,事实上sched_init()函数中,通过init_idle(current, smp_processor_id())函数的调用就已经把init_task初始化成了一个idle task,init_idle函数的第一个参数current就是&init_task,在init_idle中将会把init_task加入到cpu的运行队列中,这样当运行队列中没有别的就绪进程时,init_task(也就是idle task)将会被调用.

 

“Linux内核分析”实验报告(三)跟踪分析Linux内核的启动过程_第1张图片

 

“Linux内核分析”实验报告(三)跟踪分析Linux内核的启动过程_第2张图片

“Linux内核分析”实验报告(三)跟踪分析Linux内核的启动过程_第3张图片

 

“Linux内核分析”实验报告(三)跟踪分析Linux内核的启动过程_第4张图片

转载于:https://my.oschina.net/doumenghan/blog/644016

你可能感兴趣的:(“Linux内核分析”实验报告(三)跟踪分析Linux内核的启动过程)