至此就可以正常的进行调试了,接下来将展现linux操作系统启动的方方面面,看到这里,你可能会疑惑为什么是从start_kernel开始调试,你可能会说它就是内核的起点,一切的一切都从这里开始,说的对,但也不对,因为如果说一切的一切都从这里开始的话,那进入start_kernel的栈是哪里来的?断点打在start_kernel上按下c的时候为什么处在冻结状态的内核会闪过一些字符?所以它并不是一切的起点,再次之前还有很多用汇编编写的代码来完成硬件系统的初始化工作,为 C 代码的运行设置环境,那为啥还是得从start_kernel开始调试呢?因为之前都是硬件层面的初始化,在这我们真正想研究或者说想弄明白的是如此庞大的操作系统到底是怎么样启动起来的!并且在这留下几个问题:
/* * 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(); 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(); /* * Disable preemption - early bootup scheduling is extremely * fragile until we cpu_idle() for the first time. */ preempt_disable(); if (WARN(!irqs_disabled(), "Interrupts were enabled *very* early, fixing it\n")) local_irq_disable(); idr_init_cache(); rcu_init(); context_tracking_init(); radix_tree_init(); /* init some links before init_ISA_irqs() */ early_irq_init(); init_IRQ(); tick_init(); rcu_init_nohz(); init_timers(); hrtimers_init(); softirq_init(); timekeeping_init(); time_init(); sched_clock_postinit(); perf_event_init(); profile_init(); 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();
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();
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(); }
for (i = 0; i < CLASSHASH_SIZE; i++) INIT_LIST_HEAD(classhash_table + i);
for (i = 0; i < CHAINHASH_SIZE; i++) INIT_LIST_HEAD(chainhash_table + i);
lockdep_initialized = 1; }
在源码中会有Need to run as early as possible, to initialize the lockdep hash这条注释,翻译过来就是说lockdep_init需要尽可能的最早运行,为什么呢?其实往下看下一条指令(set_task_stack_end_magic(&init_task))就知道了,步入之后它就去fork.c了,但此进程并不是fork出来的,回过头看它初始化死锁也是挺有道理的,回头想想这岂不是第一个进程?答案确实是的!
staticvoid __init boot_cpu_init(void) { int cpu = smp_processor_id(); /* Mark the boot cpu "present", "online" etc for SMP and UP case */ set_cpu_online(cpu, true); set_cpu_active(cpu, true); set_cpu_present(cpu, true); set_cpu_possible(cpu, true); }
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); }
staticint __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) return0; 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) return0; 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")) return0;
panic("No working init found. Try passing init= option to kernel. " "See Linux Documentation/init.txt for guidance."); }
if (ramdisk_execute_command) { ret = run_init_process(ramdisk_execute_command); if (!ret) return0; pr_err("Failed to execute %s (error %d)\n",ramdisk_execute_command, ret); }
挂载完成之后,如果在命令行中有指定init的程序就去执行这个,如果没有就去一些固定的目录去找:
1 2 3 4 5 6 7
if (execute_command) { ret = run_init_process(execute_command); if (!ret) return0; pr_err("Failed to execute %s (error %d). Attempting defaults...\n", execute_command, ret); }
开始寻找用户态的文件系统中的init进程,只要这四个只要一个就可以了:
1 2 3 4 5
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")) return0;