Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1078606
  • 博文数量: 165
  • 博客积分: 3900
  • 博客等级: 中校
  • 技术积分: 1887
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-06 15:15
文章分类

全部博文(165)

文章存档

2020年(3)

2019年(8)

2017年(2)

2016年(8)

2015年(14)

2013年(15)

2012年(32)

2011年(11)

2010年(14)

2009年(7)

2008年(20)

2007年(31)

分类:

2012-10-10 11:25:55

本文主要参考博客: tekkamanninja.blog.chinaunix.net

一、vmlinux、zImage与uImage之间的关系

vmlinux是编译出来的最原始的内核文件,未压缩;zImage是经过gzip压缩后的文件;uImage是u-boot专用的映像文件,它是在zImage之前加上一个长度为64字节的tag。
注:影响系统启动速度的关键因素分析
看到这,我本想不压缩以提升启动速度,但是实验发现,影响系统启动速度的关键因素还是数据从Nand Flash加载到内存的时间占了绝大多数。
a. NAND read这部分读取内核到内存,花了3秒多时间。
b.以上两部分之间虽然有很多打印信息,但是差不多只花了3秒多一点。
        
c.以上两部分之间的时间花的最多,9秒左右!其中第一部分打印出后停留了5秒才有打印信息数来。
经过以上分析实验,系统启动所花时间绝大多数花在内核加载到内存文件系统挂载上。

二、系统启动汇编部分
三、系统启动C语言部分
架构相关的汇编部分代码运行完之后,就是架构相关性比较小的C语言部分,位于init/main.c里的start_kernel函数。

1、start_kernel函数
start_kernel函数对于驱动工程师来说,需要重点关注的是内核启动参数的获取与处理、setup_arch(&command_line)函数、rest_init()函数。其它是内存管理的初始化和内核各个组件的数据结构的内存申请并初始化。

点击(此处)折叠或打开

  1. asmlinkage void __init start_kernel(void)
  2. {
  3.     char * command_line;
  4.     //地址指针,指向内核启动参数在内存中的位置(虚拟地址)
  5.     extern const struct kernel_param __start___param[], __stop___param[];

  6.     smp_setup_processor_id(); //针对SMP处理器,如果不是,则是弱引用函数

  7.     /*
  8.      * Need to run as early as possible, to initialize the
  9.      * lockdep hash:
  10.      */
  11.     lockdep_init(); //内核调试模块,用于检查内核互斥机制潜在的死锁问题
  12.     debug_objects_early_init();

  13.     /*
  14.      * Set up the the initial canary ASAP:
  15.      */
  16.     boot_init_stack_canary(); //初始化栈canary值,canary值用于防止栈溢出攻击的堆栈的保护字

  17.     cgroup_init_early(); //一组进程的行为控制,做数据结构和其中链表的初始化

  18.     local_irq_disable(); //关闭系统总中断
  19.     early_boot_irqs_disabled = true;

  20. /*
  21.  * Interrupts are still disabled. Do necessary setups, then
  22.  * enable them
  23.  */
  24.     tick_init(); //初始化内核时钟系统
  25.     boot_cpu_init(); //激活当前CPU
  26.     page_address_init(); //高端内存相关,未定义的话为空函数
  27.     printk(KERN_NOTICE "%s", linux_banner); //打印内核版本信息,内核启动的第一行信息就来自这
  28.     setup_arch(&command_line); //内核架构相关初始化函数
  29.     mm_init_owner(&init_mm, &init_task); //初始化init_mm结构体
  30.     mm_init_cpumask(&init_mm);
  31.     setup_command_line(command_line); //对command_line进行备份与保存
  32.     setup_nr_cpu_ids(); //一下三个函数针对SMP处理器,不是SMP处理器都为空函数
  33.     setup_per_cpu_areas();
  34.     smp_prepare_boot_cpu();    /* arch-specific boot-cpu hooks */

  35.     build_all_zonelists(NULL); //设置内存相关节点和其中的内存域数据结构
  36.     page_alloc_init();
  37.     //2 打印与解析内核启动参数
  38.     printk(KERN_NOTICE "Kernel command line: %s\n", boot_command_line);
  39.     parse_early_param();
  40.     parse_args("Booting kernel", static_command_line, __start___param,
  41.          __stop___param - __start___param,
  42.          &unknown_bootoption);

  43.     jump_label_init();

  44.     /*
  45.      * These use large bootmem allocations and must precede
  46.      * kmem_cache_init()
  47.      */
  48.     setup_log_buf(0); //使用bootmem分配一个启动信息的缓冲区
  49.     pidhash_init(); //使用bootmem分配并初始化PID散列表
  50.     vfs_caches_init_early(); //前期VFS缓存初始化
  51.     sort_main_extable(); //对内核异常表进行排序
  52.     trap_init(); //对内核陷阱异常经行初始化,ARM架构中位空函数
  53.     mm_init(); //初始化内核内存分配器,启动信息中的内存信息来自此函数中的mem_init函数

  54.     /*
  55.      * Set up the scheduler prior starting any interrupts (such as the
  56.      * timer interrupt). Full topology setup happens at smp_init()
  57.      * time - but meanwhile we still have a functioning scheduler.
  58.      */
  59.     sched_init(); //初始化调度器数据结构并创建运行队列
  60.     /*
  61.      * Disable preemption - early bootup scheduling is extremely
  62.      * fragile until we cpu_idle() for the first time.
  63.      */
  64.     preempt_disable(); //禁用抢占和中断,早期启动时期,调度是极其脆弱的
  65.     if (!irqs_disabled()) {
  66.         printk(KERN_WARNING "start_kernel(): bug: interrupts were "
  67.                 "enabled *very* early, fixing it\n");
  68.         local_irq_disable();
  69.     }
  70.     idr_init_cache(); //为IDR机制分配缓存
  71.     perf_event_init(); //CPU性能检测机制初始化
  72.     rcu_init(); //内核RCU机制初始化
  73.     radix_tree_init(); //内核radix树算法初始化
  74.     /* init some links before init_ISA_irqs() */
  75.     early_irq_init(); //前期外部中断描述符初始化
  76.     init_IRQ(); //架构相关中断初始化
  77.     prio_tree_init(); //基于radix树的优先级搜索树(PST)初始化
  78.     init_timers(); //以下5个函数是软中断和内核时钟机制初始化
  79.     hrtimers_init();
  80.     softirq_init();
  81.     timekeeping_init();
  82.     time_init();
  83.     profile_init(); //profile子系统初始化,内核的性能调试工具
  84.     call_function_init();
  85.     if (!irqs_disabled())
  86.         printk(KERN_CRIT "start_kernel(): bug: interrupts were "
  87.                  "enabled early\n");
  88.     early_boot_irqs_disabled = false;
  89.     local_irq_enable(); //开启总中断

  90.     /* Interrupts are enabled now so all GFP allocations are safe. */
  91.     gfp_allowed_mask = __GFP_BITS_MASK;

  92.     kmem_cache_init_late(); //slab分配器后期初始化

  93.     /*
  94.      * HACK This is early. We're enabling the console before
  95.      * we've done PCI setups etc, and console_init() must be aware of
  96.      * this. But we do want output early, in case something goes wrong.
  97.      */
  98.     console_init(); //初始化控制台
  99.     if (panic_later) //检查内核恐慌标准,如果有问题,打印信息
  100.         panic(panic_later, panic_param);

  101.     lockdep_info(); //打印lockdep调试模块信息

  102.     /*
  103.      * Need to run this when irqs are enabled, because it wants
  104.      * to self-test [hard/soft]-irqs on/off lock inversion bugs
  105.      * too:
  106.      */
  107.     locking_selftest();
  108.  
  109. //检查initrd的位置是否符合要求
  110. #ifdef CONFIG_BLK_DEV_INITRD
  111.     if (initrd_start && !initrd_below_start_ok &&
  112.      page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
  113.         printk(KERN_CRIT "initrd overwritten (0x%08lx < 0x%08lx) - "
  114.          "disabling it.\n",
  115.          page_to_pfn(virt_to_page((void *)initrd_start)),
  116.          min_low_pfn);
  117.         initrd_start = 0;
  118.     }
  119. #endif
  120.     page_cgroup_init();
  121.     enable_debug_pagealloc(); //使能页分配的调试标志
  122.     debug_objects_mem_init();
  123.     kmemleak_init(); //内存泄露检测机制的初始化
  124.     setup_per_cpu_pageset(); //设置每个CPU的页组并初始化
  125.     numa_policy_init(); //分一致性内存访问(NUMA)初始化
  126.     if (late_time_init)
  127.         late_time_init();
  128.     sched_clock_init(); //初始化调度时钟
  129.     calibrate_delay();
  130.     pidmap_init(); //PID分配映射初始化
  131.     anon_vma_init(); //匿名虚拟内存域初始化
  132. #ifdef CONFIG_X86
  133.     if (efi_enabled)
  134.         efi_enter_virtual_mode();
  135. #endif
  136.     thread_info_cache_init();
  137.     cred_init(); //任务信用系统初始化
  138.     fork_init(totalram_pages); //进程创建机制初始化
  139.     proc_caches_init();
  140.     buffer_init(); //缓存系统初始化,创建缓存头空间,并检查其大小限制
  141.     key_init(); //内核密钥管理系统初始化
  142.     security_init(); //内核安全框架初始化
  143.     dbg_late_init(); //内核调试系统后期初始化
  144.     vfs_caches_init(totalram_pages); //虚拟文件系统缓存初始化
  145.     signals_init(); //信号管理系统初始化
  146.     /* rootfs populating might need page-writeback */
  147.     page_writeback_init(); //页回写机制初始化
  148. #ifdef CONFIG_PROC_FS
  149.     proc_root_init(); //proc文件系统初始化
  150. #endif
  151.     cgroup_init(); //control group正式初始化
  152.     cpuset_init(); //CPUSET初始化
  153.     taskstats_init_early(); //任务状态早期初始化函数,为任务获取高速缓存并初始化互斥机制
  154.     delayacct_init(); //任务延迟机制初始化

  155.     check_bugs();

  156.     acpi_early_init(); /* before LAPIC and SMP init */
  157.     sfi_init_late();

  158.     ftrace_init();

  159.     /* Do the rest non-__init'ed, we're now alive */
  160.     rest_init(); //3 剩余的初始化
  161. }
2、setup_arch函数
下面分析架构相关的初始化函数setup_arch函数。

点击(此处)折叠或打开

  1. void __init setup_arch(char **cmdline_p)
  2. {
  3.     struct machine_desc *mdesc; //设备描述结构体

  4.     setup_processor(); //再次检测处理器类型,并初始化处理器相关的底层变量,内核启动时处理器信息就是通过该函数打印
  5.     mdesc = setup_machine_fdt(__atags_pointer); //检测bootloader传递的Machine ID与机器ID是否匹配。若匹配打印Machine ID,处理tagged_list;若不匹配,给出出错信息,死循环。
  6.     if (!mdesc)
  7.         mdesc = setup_machine_tags(machine_arch_type);
  8.     machine_desc = mdesc;
  9.     machine_name = mdesc->name;

  10. #ifdef CONFIG_ZONE_DMA
  11.     if (mdesc->dma_zone_size) {
  12.         extern unsigned long arm_dma_zone_size;
  13.         arm_dma_zone_size = mdesc->dma_zone_size;
  14.     }
  15. #endif
  16.     if (mdesc->soft_reboot) //设置重启类型,“s”软件重启,“h”硬件重启
  17.         reboot_setup("s");

  18.     init_mm.start_code = (unsigned long) _text; //init_mm部分数据初始化
  19.     init_mm.end_code = (unsigned long) _etext;
  20.     init_mm.end_data = (unsigned long) _edata;
  21.     init_mm.brk     = (unsigned long) _end;

  22.     /* populate cmd_line too for later use, preserving boot_command_line */
  23.     strlcpy(cmd_line, boot_command_line, COMMAND_LINE_SIZE);
  24.     *cmdline_p = cmd_line;

  25.     parse_early_param();

  26.     sanity_check_meminfo();
  27.     arm_memblock_init(&meminfo, mdesc);

  28.     paging_init(mdesc);
  29.     request_standard_resources(mdesc);

  30.     unflatten_device_tree();

  31. #ifdef CONFIG_SMP
  32.     if (is_smp())
  33.         smp_init_cpus();
  34. #endif
  35.     reserve_crashkernel();

  36.     tcm_init();

  37. #ifdef CONFIG_MULTI_IRQ_HANDLER
  38.     handle_arch_irq = mdesc->handle_irq;
  39. #endif

  40. #ifdef CONFIG_VT
  41. #if defined(CONFIG_VGA_CONSOLE)
  42.     conswitchp = &vga_con;
  43. #elif defined(CONFIG_DUMMY_CONSOLE)
  44.     conswitchp = &dummy_con;
  45. #endif
  46. #endif
  47.     early_trap_init();

  48.     if (mdesc->init_early)
  49.         mdesc->init_early();
  50. }
3、rest_init函数
rest_init函数的主要功能是创建并启动内核线程init

点击(此处)折叠或打开

  1. /*
  2.  * We need to finalize in a non-__init function or else race conditions
  3.  * between the root thread and the init thread may cause start_kernel to
  4.  * be reaped by free_initmem before the root thread has proceeded to
  5.  * cpu_idle.
  6.  *
  7.  * gcc-3.4 accidentally inlines this function, so use noinline.
  8.  */
  9.  
  10. //定义一个complete变量告诉init线程:kthreads线程已经创建完成。
  11. static __initdata DECLARE_COMPLETION(kthreadd_done);

  12. static noinline void __init_refok rest_init(void)
  13. {
  14.     int pid;

  15.     rcu_scheduler_starting(); //内核RCU锁机制调度启动
  16.     /*
  17.      * We need to spawn init first so that it obtains pid 1, however
  18.      * the init task will end up wanting to create kthreads, which, if
  19.      * we schedule it before we create kthreadd, will OOPS.
  20.      */
  21.     kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND); //创建kernel_init内核线程,PID=1
  22.     numa_default_policy();
  23.     pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES); //创建kthread内核线程,PID=2
  24.     rcu_read_lock();
  25.     kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns); //获取kthread线程信息
  26.     rcu_read_unlock();
  27.     complete(&kthreadd_done); //通过complete通知kernel_init线程kthread线程已创建成功

  28.     /*
  29.      * The boot idle thread must execute schedule()
  30.      * at least once to get things moving:
  31.      */
  32.     init_idle_bootup_task(current); //设置当前进程为idle进程类
  33.     preempt_enable_no_resched(); //使能抢占,但不重新调度
  34.     schedule(); //执行调度,切换进程

  35.     /* Call into cpu_idle with preempt disabled */
  36.     preempt_disable(); //进程调度完成,禁用抢占
  37.     cpu_idle(); //内核本体进入idle状态,用循环消耗空闲的CPU时间
  38. }
4、kernel_init函数
kernel_init函数是内核init线程运行的函数:完成设备驱动程序的初始化,并调用init_post函数启动用户空间的init进程

点击(此处)折叠或打开

  1. static int __init kernel_init(void * unused)
  2. {
  3.     /*
  4.      * Wait until kthreadd is all set-up.
  5.      */
  6.     wait_for_completion(&kthreadd_done);
  7.     /*
  8.      * init can allocate pages on any node
  9.      */
  10.     set_mems_allowed(node_states[N_HIGH_MEMORY]);
  11.     /*
  12.      * init can run on any cpu.
  13.      */
  14.     set_cpus_allowed_ptr(current, cpu_all_mask);

  15.     cad_pid = task_pid(current);

  16.     smp_prepare_cpus(setup_max_cpus);

  17.     do_pre_smp_initcalls();
  18.     lockup_detector_init();

  19.     smp_init();
  20.     sched_init_smp();

  21.     do_basic_setup(); //重要函数,主要是初始化设备驱动程序

  22.     /* Open the /dev/console on the rootfs, this should never fail */
  23.     //这里要打开/dev/console节点,在此出错可能的原因是:
  24.     //1. 制作文件系统时忘记创建/dev/console节点;
  25.     //2. 文件系统挂载问题,挂载的文件系统不是什么都没有就是挂错了节点。
  26.     if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
  27.         printk(KERN_WARNING "Warning: unable to open an initial console.\n");
  28.  
  29.     //复制两次标准输入(0),一个作为标准输出(1),一个作为标准出错(2)。 
  30.     //完成后,标准输入、标准输出、标准出错都是/dev/console了
  31.     (void) sys_dup(0);
  32.     (void) sys_dup(0);
  33.     /*
  34.      * check if there is an early userspace init. If yes, let it do all
  35.      * the work
  36.      */

  37.     if (!ramdisk_execute_command)
  38.         ramdisk_execute_command = "/init";

  39.     if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0) {
  40.         ramdisk_execute_command = NULL;
  41.         prepare_namespace();
  42.     }

  43.     /*
  44.      * Ok, we have completed the initial bootup, and
  45.      * we're essentially up and running. Get rid of the
  46.      * initmem segments and start the user-mode stuff..
  47.      */

  48.     init_post(); //启动用户空间init进程
  49.     return 0;
  50. }
5、init_post函数
init_post真正启动用户空间init进程。

点击(此处)折叠或打开

  1. /* This is a non __init function. Force it to be noinline otherwise gcc
  2.  * makes it inline to init() and it becomes part of init.text section
  3.  */
  4. static noinline int init_post(void)
  5. {
  6.     /* need to finish all async __init code before freeing the memory */
  7.     async_synchronize_full();
  8.     free_initmem(); //释放所有init.*段中的内存
  9.     mark_rodata_ro();
  10.     system_state = SYSTEM_RUNNING; //设置系统状态为运行状态
  11.     numa_default_policy();


  12.     current->signal->flags |= SIGNAL_UNKILLABLE; //设置当前进程(init)为不可杀进程

  13.     if (ramdisk_execute_command) {
  14.         run_init_process(ramdisk_execute_command);
  15.         printk(KERN_WARNING "Failed to execute %s\n",
  16.                 ramdisk_execute_command);
  17.     }

  18.     /*
  19.      * We try each of these until one succeeds.
  20.      *
  21.      * The Bourne shell can be used instead of init if we are
  22.      * trying to recover a really broken machine.
  23.      */
  24.     if (execute_command) {
  25.         run_init_process(execute_command);
  26.         printk(KERN_WARNING "Failed to execute %s. Attempting "
  27.                     "defaults...\n", execute_command);
  28.     }
  29.     run_init_process("/sbin/init");
  30.     run_init_process("/etc/init");
  31.     run_init_process("/bin/init");
  32.     run_init_process("/bin/sh");
  33.  
  34.  
  35.     //检查完ramdisk_execute_command和execute_command为空的情况下,顺序执行四个初始化程序。
  36.     //如果都没有找到就打印出错信息,出现该错误的可能原因是:
  37.     //1. 启动参数配置有问题:指定了init进程,但是没找到,默认四个程序不在文件系统中;
  38.     //2. 文件系统挂载有问题;
  39.     //3. init程序没有执行权限。
  40.     panic("No init found. Try passing init= option to kernel. "
  41.      "See Linux Documentation/init.txt for guidance.");
  42. }
6、do_basic_setup函数
内核init线程调用了do_basic_setup函数,这个函数也做了很多内核与驱动的初始化工作

点击(此处)折叠或打开

  1. /*
  2.  * Ok, the machine is now initialized. None of the devices
  3.  * have been touched yet, but the CPU subsystem is up and
  4.  * running, and memory and process management works.
  5.  *
  6.  * Now we can finally start doing some real work..
  7.  */
  8. static void __init do_basic_setup(void)
  9. {
  10.     cpuset_init_smp(); //对于非SMP处理器,该函数为空
  11.     usermodehelper_init(); //创建单线程工作队列khelper
  12.     shmem_init();
  13.     driver_init(); //初始化驱动模型中的各个子系统
  14.     init_irq_proc(); //在proc文件系统中创建irq目录
  15.     do_ctors(); //调用链接到所有的构造函数
  16.     usermodehelper_enable(); //使能单线程工作队列khelper
  17.  
  18.     /*
  19.      * 1. 调用所有编译进内核的驱动的初始化函数,按照各个内核模块初始化函数所定义的启动级别(1~7), 按顺序调用初始化函数;
  20.      * 2. 对于同一级别的初始化函数,安装编译是链接的顺序调用,和内核的Makefile编写有关;
  21.      * 3. 基于某一子系统的驱动,其初始化函数的级别必须低于该子系统初始化函数的级别,如果编写的模块必须和依赖的模块在同一级,就必须Makefile的编写了。
  22.      */
  23.     do_initcalls();
  24. }
7、driver_init函数
driver_init函数的作用是驱动模型的子系统初始化,位于drivers/base/init.c。

点击(此处)折叠或打开

  1. /**
  2.  * driver_init - initialize driver model.
  3.  *
  4.  * Call the driver model init functions to initialize their
  5.  * subsystems. Called early from init/main.c.
  6.  */
  7. void __init driver_init(void)
  8. {
  9.     /* These are the core pieces */  
  10.     devtmpfs_init();
  11. 点击(此处)折叠或打开

    1. 初始化devtmpfs文件系统,驱动核心设备将在这个文件系统中添加它们的设备节点。
    2. 这个文件系统可以由内核在挂载根文件系统之后自动挂载到/dev下,也可以在文件系统的启动脚本中手动挂载。
  12.      devices_init();
  13. 点击(此处)折叠或打开

    1. 初始化驱动模型中的部分子系统和kobject:
    2. devices
    3. dev
    4. dev/block
    5. dev/char
  14.     buses_init(); //初始化驱动模型中的bus子系统
  15.     classes_init(); //初始化驱动模型中的class子系统
  16.     firmware_init(); //初始化驱动模型中的firmware子系统
  17.     hypervisor_init(); //初始化驱动模型中的hypervisor子系统

  18.     /* These are also core pieces, but must come after the
  19.      * core core pieces.
  20.      */
  21.     platform_bus_init(); //初始化驱动模型中的bus/platform子系统
  22.     system_bus_init(); //初始化驱动模型中的devices/system子系统
  23.     cpu_dev_init(); //初始化驱动模型中的devices/system/cpu子系统
  24.     memory_dev_init(); //初始化驱动模型中的devices/system/memory子系统
  25. }










阅读(2165) | 评论(0) | 转发(0) |
0

上一篇:巧记韩语元音和辅音

下一篇:kernel KSM

给主人留下些什么吧!~~