内核初始化流程分析

来源:互联网 发布:mg动画用什么软件 编辑:程序博客网 时间:2024/05/21 21:49

分类: LINUX

asmlinkage void __init start_kernel(void)
{
 char * command_line;
 extern struct kernel_param __start___param[], __stop___param[];
/*
 * Interrupts are still disabled. Do necessary setups, then
 * enable them
 */
 lock_kernel();
 page_address_init();
 printk(KERN_NOTICE);
 printk(linux_banner);
 setup_arch(&command_line);
 setup_per_cpu_areas();
 /*
  * Mark the boot cpu "online" so that it can call console drivers in
  * printk() and can access its per-cpu storage.
  */
 smp_prepare_boot_cpu();
 /*
  * 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();
 build_all_zonelists();
 page_alloc_init();
 printk(KERN_NOTICE "Kernel command line: %s\n", saved_command_line);
 parse_early_param();
 parse_args("Booting kernel", command_line, __start___param,
     __stop___param - __start___param,
     &unknown_bootoption);
 sort_main_extable();
 trap_init();
 rcu_init();
 init_IRQ();
 pidhash_init();
 init_timers();
 softirq_init();
 time_init();
 /*
  * 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(panic_later, panic_param);
 profile_init();
 local_irq_enable();
#ifdef CONFIG_BLK_DEV_INITRD
 if (initrd_start && !initrd_below_start_ok &&
   initrd_start < min_low_pfn << PAGE_SHIFT) {
  printk(KERN_CRIT "initrd overwritten (0x%08lx < 0x%08lx) - "
      "disabling it.\n",initrd_start,min_low_pfn << PAGE_SHIFT);
  initrd_start = 0;
 }
#endif
 vfs_caches_init_early();
 mem_init();
 kmem_cache_init();
 setup_per_cpu_pageset();
 numa_policy_init();
 if (late_time_init)
  late_time_init();
 calibrate_delay();
 pidmap_init();
 pgtable_cache_init();
 prio_tree_init();
 anon_vma_init();
#ifdef CONFIG_X86
 if (efi_enabled)
  efi_enter_virtual_mode();
#endif
 fork_init(num_physpages);
 proc_caches_init();
 buffer_init();
 unnamed_dev_init();
 key_init();
 security_init();
 vfs_caches_init(num_physpages);
 radix_tree_init();
 signals_init();
 /* rootfs populating might need page-writeback */
 page_writeback_init();
#ifdef CONFIG_PROC_FS
 proc_root_init();
#endif
 cpuset_init();
 check_bugs();
 acpi_early_init(); /* before LAPIC and SMP init */
 /* Do the rest non-__init'ed, we're now alive */
 rest_init();
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
static void noinline rest_init(void)
 __releases(kernel_lock)
{
/*********************************************************
int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags);
****************************************************************/
 kernel_thread(init, NULL, CLONE_FS | CLONE_SIGHAND);//通过函数指针调用init函数

 numa_default_policy();
 unlock_kernel();
 preempt_enable_no_resched();
 /*
  * The boot idle thread must execute schedule()
  * at least one to get things moving:
  */
 schedule();
 cpu_idle();
}
 
 
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
 
 
static int init(void * unused)
{
 lock_kernel();
 /*
  * init can run on any cpu.
  */
 set_cpus_allowed(current, CPU_MASK_ALL);
 /*
  * Tell the world that we're going to be the grim
  * reaper of innocent orphaned children.
  *
  * We don't want people to have to make incorrect
  * assumptions about where in the task array this
  * can be found.
  */
 child_reaper = current;
 /* Sets up cpus_possible() */
 smp_prepare_cpus(max_cpus);
 do_pre_smp_initcalls();
 fixup_cpu_present_map();
 smp_init();
 sched_init_smp();
 cpuset_init_smp();
 /*
  * Do this before initcalls, because some drivers want to access
  * firmware files.
  */
 populate_rootfs();
 do_basic_setup();
 /*
  * check if there is an early userspace init.  If yes, let it do all
  * the work
  */
 if (!ramdisk_execute_command)
  ramdisk_execute_command = "/init";
 if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0) {
  ramdisk_execute_command = NULL;
  prepare_namespace();
 }
 /*
  * Ok, we have completed the initial bootup, and
  * we're essentially up and running. Get rid of the
  * initmem segments and start the user-mode stuff..
  */
 free_initmem();
 unlock_kernel();
 system_state = SYSTEM_RUNNING;
 numa_default_policy();
 if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
  printk(KERN_WARNING "Warning: unable to open an initial console.\n");
 (void) sys_dup(0);
 (void) sys_dup(0);
 if (ramdisk_execute_command) {
  run_init_process(ramdisk_execute_command);
  printk(KERN_WARNING "Failed to execute %s\n",
    ramdisk_execute_command);
 }
 /*
  * 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) {
  run_init_process(execute_command);
  printk(KERN_WARNING "Failed to execute %s.  Attempting "
     "defaults...\n", execute_command);
 }
 run_init_process("/sbin/init");
 run_init_process("/etc/init");
 run_init_process("/bin/init");
 run_init_process("/bin/sh");
 panic("No init found.  Try passing init= option to kernel.");
}
 
 
 
static void __init do_basic_setup(void)
{
 /* drivers will send hotplug events */
 init_workqueues();
 usermodehelper_init();
 driver_init();
#ifdef CONFIG_SYSCTL
 sysctl_init();
#endif
 /* Networking initialization needs a process context */
 sock_init();
 do_initcalls();
//init内核进程调用了do_basic_setup 函数,这个函数依次调用do_initcalls函数遍历由_initcall或者module_init宏注册的函数列表,并调用他们
}
 
 
static void __init do_initcalls(void)
{
 initcall_t *call;
 int count = preempt_count();
 for (call = __initcall_start; call < __initcall_end; call++) {
  char *msg;
  if (initcall_debug) {
   printk(KERN_DEBUG "Calling initcall 0x%p", *call);
   print_fn_descriptor_symbol(": %s()", (unsigned long) *call);
   printk("\n");
  }
  (*call)();//顺序调用__init段中的init_module注册的初始化函数
  msg = NULL;
  if (preempt_count() != count) {
   msg = "preemption imbalance";
   preempt_count() = count;
  }
  if (irqs_disabled()) {
   msg = "disabled interrupts";
   local_irq_enable();
  }
  if (msg) {
   printk(KERN_WARNING "error in initcall at 0x%p: "
    "returned with %s\n", *call, msg);
  }
 }
 /* Make sure there is no pending stuff from the initcall sequence */
 flush_scheduled_work();
}
0 0