基于Linux2.6.38.8内核启动过程完全解析

字体大小: 中小 标准 ->行高大小: 标准

一: Linux kernel内存存布局

在ARM平台中zImage.bin是一个压缩镜像,它用于将被压缩的kernel解压缩到KERNEL_RAM_PADDR开始的一段内存中,接着跳进真正的kernel去执行。该kernel的执行起点是stext函数,定义于arch/arm/kernel/head.S。在分析ENTRY(stext)前,先介绍此时内存的布局如下图所示:

图一:内存布局[此处借用下网络上已有的图片,自己不想弄图片了]

在我的YL-E2410的平台上,SDRAM的开始内存地址是0x30000000,大小为64M,即0x20000000。 Linux2.6.38.8 ARM kernel将SDRAM的开始地址定义为PHYS_OFFSET。经bootloader加载kernel并由自解压部分代码运行后,最终kernel被放置到KERNEL_RAM_PADDR(=PHYS_OFFSET + TEXT_OFFSET,即0x30008000)地址上的一段内存,经此放置后,kernel代码以后均不会被移动。在arch\arm\mach-s3c2410\Makefile.boot文件,其内容如下:

 zreladdr-y  := 0x30008000
 params_phys-y := 0x30000100

这也验证了为什么内核会被放置在0x30008000的地方了,这个地址必须由Makefile.boot指定。而params_phys-y则是linux Kernel的taglist等参数的起始地址。在进入kernel代码前,即自解压缩阶段,ARM未开启MMU功能。因此启动代码一个重要功能是设置好相应的页表,并开启MMU功能。为了支持MMU功能,kernel镜像中的所有符号,包括代码段和数据段的符号,在链接时都生成了它在开启MMU时,所在物理内存地址映射到的虚拟内存地址。Kernel第一个符号stext为例,在编译链接,它生成的虚拟地址是0xc0008000,而放置它的物理地址为0x30008000。实际上这个变换可以利用简单的公式进行表示:va = pa – PHYS_OFFSET + PAGE_OFFSET。Arm linux最终的kernel空间的页表,就是按照这个关系来建立。之所提及linux的内存映射,原因是在进入kernel代码,里面所有符号地址值为0xCxxxxxxx地址,而此时ARM未开启MMU功能,故在执行stext函数第一条执行时,它的PC值就是stext所在的内存地址(即物理地址,0x30008000)。

二:stext函数详解

stext函数定义在arch/arm/kernel/head.S,它的功能是获取处理器类型和机器类型信息,并创建临时的页表,然后开启MMU功能,并跳进第一个C语言函数start_kernel。stext函数的在前置条件是:MMU, D-cache, 关闭; r0 = 0, r1 = machine nr, r2 = atags prointer.

[cpp]
  1. /*   * This program is free software; you can redistribute it and/or modify 
  2.  * it under the terms of the GNU General Public License version 2 as   * published by the Free Software Foundation. 
  3.  *   *  Kernel startup code for all 32-bit CPUs 
  4.  */   #include <linux/linkage.h>   
  5. #include <linux/init.h>      
  6. #include <asm/assembler.h>    #include <asm/domain.h>   
  7. #include <asm/ptrace.h>    #include <asm/asm-offsets.h>   
  8. #include <asm/memory.h>    #include <asm/thread_info.h>   
  9. #include <asm/system.h>      
  10. #ifdef CONFIG_DEBUG_LL    #include <mach/debug-macro.S>   
  11. #endif      
  12. #if (PHYS_OFFSET & 0x001fffff)    #error "PHYS_OFFSET must be at an even 2MiB boundary!"   
  13. #endif      
  14. #define KERNEL_RAM_VADDR    (PAGE_OFFSET + TEXT_OFFSET)    #define KERNEL_RAM_PADDR    (PHYS_OFFSET + TEXT_OFFSET)   
  15.      
  16. /*   * swapper_pg_dir is the virtual address of the initial page table. 
  17.  * We place the page tables 16K below KERNEL_RAM_VADDR.  Therefore, we must   * make sure that KERNEL_RAM_VADDR is correctly set.  Currently, we expect 
  18.  * the least significant 16 bits to be 0x8000, but we could probably   * relax this restriction to KERNEL_RAM_VADDR >= PAGE_OFFSET + 0x4000. 
  19.  */   #if (KERNEL_RAM_VADDR & 0xffff) != 0x8000   
  20. #error KERNEL_RAM_VADDR must start at 0xXXXX8000    #endif   
  21.        .globl  swapper_pg_dir  
  22.     .equ    swapper_pg_dir, KERNEL_RAM_VADDR - 0x4000     
  23.     .macro  pgtbl, rd       ldr \rd, =(KERNEL_RAM_PADDR - 0x4000)  
  24.     .endm     
  25. #ifdef CONFIG_XIP_KERNEL    #define KERNEL_START    XIP_VIRT_ADDR(CONFIG_XIP_PHYS_ADDR)   
  26. #define KERNEL_END  _edata_loc    #else   
  27. #define KERNEL_START    KERNEL_RAM_VADDR    #define KERNEL_END  _end   
  28. #endif      
  29. /*   * Kernel startup entry point. 
  30.  * ---------------------------   * 
  31.  * This is normally called from the decompressor code.  The requirements   * are: MMU = off, D-cache = off, I-cache = dont care, r0 = 0, 
  32.  * r1 = machine nr, r2 = atags pointer.   * 
  33.  * This code is mostly position independent, so if you link the kernel at   * 0xc0008000, you call this at __pa(0xc0008000). 
  34.  *   * See linux/arch/arm/tools/mach-types for the complete list of machine 
  35.  * numbers for r1.   * 
  36.  * We're trying to keep crap to a minimum; DO NOT add any machine specific   * crap here - that's what the boot loader (or in extreme, well justified 
  37.  * circumstances, zImage) is for.   */  
  38.     __HEAD   ENTRY(stext)  
  39.          /* 设置CPU运行模式为SVC,并关中断 */       setmode PSR_F_BIT | PSR_I_BIT | SVC_MODE, r9 @ ensure svc mode  
  40.                         @ and irqs disabled       mrc p15, 0, r9, c0, c0      @ get processor id  
  41.     bl  __lookup_processor_type     @ r5=procinfo r9=cpuid            * r10指向cpu对应的proc_info记录 */  
  42.     movs    r10, r5             @ invalid processor (r5=0)?    THUMB( it  eq )        @ force fixup-able long branch encoding  
  43.     beq __error_p           @ yes, error 'p'       bl  __lookup_machine_type       @ r5=machinfo  
  44.          /* r8 指向开发板对应的arch_info记录 */        movs    r8, r5              @ invalid machine (r5=0)?  
  45.  THUMB( it  eq )        @ force fixup-able long branch encoding       beq __error_a           @ yes, error 'a'  
  46.          /* __vet_atags函数涉及bootloader造知kernel物理内存的情况 */       /* 
  47.      * r1 = machine no, r2 = atags,       * r8 = machinfo, r9 = cpuid, r10 = procinfo 
  48.      */       bl  __vet_atags  
  49. #ifdef CONFIG_SMP_ON_UP        bl  __fixup_smp  
  50. #endif             /*  创建临时页表 */  
  51.     bl  __create_page_tables     
  52.     /*       * The following calls CPU specific code in a position independent 
  53.      * manner.  See arch/arm/mm/proc-*.S for details.  r10 = base of       * xxx_proc_info structure selected by __lookup_machine_type 
  54.      * above.  On return, the CPU will be ready for the MMU to be       * turned on, and r0 will hold the CPU control register value. 
  55.           * 这里的逻辑关系相当复杂,先是从proc_info结构中的中跳进__arm920_setup函数,             * 然后执__enable_mmu 函数。最后在__enable_mmu函数通过mov pc, r13来执行__mmap_switched,   
  56.           * __mmap_switched函数在最后一条语句,鱼跃龙门,跳进第一个C语言函数start_kernel       */  
  57.     ldr r13, =__mmap_switched       @ address to jump to after                           @ mmu has been enabled  
  58.     adr lr, BSYM(1f)            @ return (PIC) address    ARM(   add pc, r10, #PROCINFO_INITFUNC )  
  59.  THUMB( add r12, r10, #PROCINFO_INITFUNC    )    THUMB( mov pc, r12             )  
  60. 1:  b   __enable_mmu   ENDPROC(stext)  
  61.     .ltorg  

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/57678.html