start_kernel分析

来源:互联网 发布:studio美图软件 编辑:程序博客网 时间:2024/06/11 21:58

start_kernel是linux内核的入口函数,定义在init/main.c里面。

start_kernel里面包含了一系列的初始化代码。

<span style="color:#666666;">asmlinkage void __init </span><strong style="color: rgb(102, 102, 102);">start_kernel</strong><span style="color:#666666;">(void){    char * command_line;    extern const struct kernel_param __start___param[], __stop___param[];    smp_setup_processor_id(); //确定SMP系统中每个CPU的id    /*     * Need to run as early as possible, to initialize the     * lockdep hash:     */    lockdep_init(); //初始化互斥锁的dependency。    debug_objects_early_init(); //初始化debug kernel相关    /*     * Set up the the initial canary ASAP:     */    boot_init_stack_canary(); //stack_canary的是带防止栈溢出攻击保护的堆栈。    cgroup_init_early();     local_irq_disable(); //这个太直白了。    early_boot_irqs_off(); //这个也很直白。/* * Interrupts are still disabled. Do necessary setups, then * enable them */    tick_init(); //初始化time ticket,时钟    boot_cpu_init(); //用以启动的CPU进行初始化。也就是初始化CPU0    page_address_init();//初始化页面    printk(KERN_NOTICE "%s", linux_banner);    setup_arch(&command_line); //CPU架构相关的初始化    mm_init_owner(&init_mm, &init_task); //初始化内存管理    setup_command_line(command_line); //处理启动命令行    setup_nr_cpu_ids();    setup_per_cpu_areas();     smp_prepare_boot_cpu();//准备boot_cpu.   /* arch-specific boot-cpu hooks */     build_all_zonelists(NULL); //建立什么样的zone??    page_alloc_init(); //初始化page allocation相关结构    printk(KERN_NOTICE "Kernel command line: %s\n", boot_command_line);    parse_early_param();     parse_args("Booting kernel", static_command_line, __start___param,         __stop___param - __start___param,         &unknown_bootoption);//解析启动参数    /*     * These use large bootmem allocations and must precede     * kmem_cache_init()     */    pidhash_init();//初始化process ID hash表    vfs_caches_init_early(); //文件系统caches预初始化    sort_main_extable(); //初始化exception table    trap_init(); //初始化trap,用以处理错误执行代码    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.     */    </span><strong style="color: rgb(102, 102, 102);">sched_init();</strong><span style="color:#666666;"> //进程调度初始化    /*     * Disable preemption - early bootup scheduling is extremely     * fragile until we cpu_idle() for the first time.     */    preempt_disable();     if (!irqs_disabled()) {        printk(KERN_WARNING "start_kernel(): bug: interrupts were "                "enabled *very* early, fixing it\n");        local_irq_disable();    }    rcu_init(); //Read_Copy_Update机制初始    radix_tree_init();     /* init some links before init_ISA_irqs() */    early_irq_init();    init_IRQ(); //初始化中断    prio_tree_init(); //这是啥?以后要弄懂    init_timers(); //初始化时钟    hrtimers_init();//初始化高精时钟    softirq_init();//初始化软中断    timekeeping_init();//初始化时钟源    time_init();//初始化时间例程    profile_init(); //Profile?这是什么profile?    if (!irqs_disabled())        printk(KERN_CRIT "start_kernel(): bug: interrupts were "                 "enabled early\n");    early_boot_irqs_on();    local_irq_enable();    /* Interrupts are enabled now so all GFP allocations are safe. */    gfp_allowed_mask = __GFP_BITS_MASK;    kmem_cache_init_late();//初始化CPU Cache    /*     * 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(); //初始化console    if (panic_later)        panic(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) {        printk(KERN_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(); //页面初始    enable_debug_pagealloc(); //页面分配debug启用    kmemleak_init(); //memory leak 侦测初始化    debug_objects_mem_init();     idr_init_cache();     setup_per_cpu_pageset(); //设置每个CPU的页面集合    numa_policy_init();    if (late_time_init)        late_time_init();    sched_clock_init();//初始化调度时钟    calibrate_delay(); //协同不同CPU的时钟    pidmap_init();//pid是process id还是processor id?    anon_vma_init();//anonymous page?什么意思?#ifdef CONFIG_X86    if (efi_enabled)        efi_enter_virtual_mode();#endif    thread_info_cache_init(); //初始化thread info    cred_init(); //credential    fork_init(totalram_pages); //初始化fork    proc_caches_init(); //初始化/proc的cache?    buffer_init(); // buffer    key_init(); //key    security_init();//security    dbg_late_init();//debug    vfs_caches_init(totalram_pages);//文件系统cache初始化    signals_init();//signal    /* rootfs populating might need page-writeback */    page_writeback_init();page_writeback#ifdef CONFIG_PROC_FS    proc_root_init();#endif    cgroup_init();     cpuset_init(); //cpuset    taskstats_init_early(); //task    delayacct_init();    check_bugs();     acpi_early_init();//acpi /* before LAPIC and SMP init */     sfi_init_late(); //simple firmware interface    ftrace_init();    /* Do the rest non-__init'ed, we're now alive */   </span><strong><span style="color:#ff0000;"> rest_init();</span></strong><span style="color:#666666;">}</span>


将这些函数归归类,以后就按照类型来读:

CPU初始化
smp_setup_processor_id()
boot_cpu_init()
setup_arch(&command_line);
setup_nr_cpu_ids()
setup_per_cpu_areas()
smp_prepare_boot_cpu()
setup_per_cpu_pageset(); 
calibrate_delay();
cpuset_init();
内存管理初始化
boot_init_stack_canary()
page_address_init();
mm_init_owner();
page_alloc_init(); 
mm_init();
rcu_init();
kmem_cache_init_late();
page_cgroup_init(); 
kmemleak_init(); 
numa_policy_init();
anon_vma_init();
page_writeback_init();
进程管理
pidhash_init();
sched_init(); 
sched_clock_init()
pidmap_init();
fork_init(totalram_pages); 
taskstats_init_early();
文件系统
vfs_caches_init_early(); 
thread_info_cache_init();
vfs_caches_init(totalram_pages);
中断
early_irq_init();
init_IRQ(); 
softirq_init();
同步互斥
lockdep_init();
lockdep_info();
locking_selftest(); 
时钟
tick_init(); 
init_timers(); 
hrtimers_init();
timekeeping_init();
time_init();
调试
debug_objects_early_init();
console_init();
enable_debug_pagealloc(); 
debug_objects_mem_init();
dbg_late_init();
其他
sort_main_extable();
trap_init(); 
efi_enter_virtual_mode();
cred_init(); 
proc_caches_init(); 
buffer_init();
key_init(); 
security_init();
signals_init();
proc_root_init();
delayacct_init();
check_bugs();
acpi_early_init();
sfi_init_late(); 
未知
cgroup_init_early();
build_all_zonelists(NULL); 
preempt_disable();
radix_tree_init();
prio_tree_init(); 
profile_init();
idr_init_cache(); 
cgroup_init();
ftrace_init();
<span style="color:#666666;">static noinline void __init_refok </span><strong><span style="color:#ff0000;">rest_init</span></strong><span style="color:#666666;">(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(</span><strong><span style="color:#ff0000;">kernel_init</span></strong><span style="color:#666666;">, NULL, CLONE_FS);//创建1号进程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);}</span>
static int __ref <strong><span style="color:#ff0000;">kernel_init</span></strong>(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.");}



0 0
原创粉丝点击