分类: LINUX
2008-05-04 00:17:54
一般情况下,内核镜像文件zImage是压缩形式存在的,最初镜像存在于flash之中,而且镜像形成时的链接起始地址TEXT_START=ZTEXADDR=0。bootloader负责将flash中的镜像原本的拷贝到内存之中,然后就利用长跳转指令跳入内存中执行。
mov pc, r2 @其中r2就是内存中镜像的地址,0x30008000
下面我们具体叙述进入linux后的空间映射过程。此时内核没有解压缩,根据$(TOPDIR)/arch/arm/boot中的Makefile和其子目录compressed目录下的文件,我们可以知道head.S是整个压缩镜像的入口。从compressed/vmlinux.lds文件中我们得知解压后的内核起始地址为0x30008000,从相应的Makefile中也知道LOAD_ADDR=ZRELADDR=0x30008000。在head.S中进行了内核解压,同时调用解压的内核开始运行内核。
mov pc, r4 @其中r4就是0x30008000
大家注意在head.S中的技巧,在进入head.S时寄存器pc的值为0x30008000,在整个程序中adr,ldr伪指令,以及ldr,str,b,bl等指令中对于标号或是符号的寻址都是基于pc的,所以整个压缩镜像的程序的标号虽然是以0开始,但还是能够在0x30008000后的空间中运行的很好。代码中多处涉及到空间的修正,请大家仔细斟酌,这个特性在更重要的内核启动的空间切换中至关重要。
adr r0, LC0 @LC0本身是以0为基址的偏移,而adr的基于pc寻址导致r0是相对于0x30008000的
ldmia r0, {r1, r2, r3, r4, r5, r6, ip, sp} @r1,r2等的值都是对于0的偏移
subs r0, r0, r1 @ calculate the delta offset
teq r0, #0 @ if delta is zero, we're,一定不等于0
beq not_relocated
.type LC0, #object
LC0: .word LC0 @ r1
.word __bss_start @ r2
.word _end @ r3
.word _load_addr @ r4
.word _start @ r5
.word _got_start @ r6
.word _got_end @ ip
.word user_stack+4096 @ sp
我们再看进入内核的映射过程,进入内核后,pc=0x30008000,而代码连接地址是以TEXTADDR=0xc0008000开始的,这就意味着上述情况依然存在,实际程序中标号和符号都是基于0xc0008000的,但是通过pc我们可以使程序运行的很好,这称作location indepence。也就是说,不管程序的链接地址如何,只要用基于pc寻址的指令(ldr,str,b,bl,adr等),就没有问题。
进入$(TOPDIR)/arch/arm/kernel/head_armv.S
stext=0xc0008000,pc=0x30008000
ENTRY(stext)
mov r12, r0
mov r0, #0
mov r1, #MACH_TYPE_S3C2440
mov r0, #F_BIT | I_BIT | MODE_SVC @ make sure svc mode
msr cpsr_c, r0 @ and all irqs disabled
bl __lookup_processor_type @利用pc寻址
teq r10, #0 @ invalid processor?
moveq r0, #'p' @ yes, error 'p'
beq __error @利用pc寻址
bl __lookup_architecture_type @利用pc寻址
teq r7, #0 @ invalid architecture?
moveq r0, #'a' @ yes, error 'a'
beq __error @利用pc寻址
bl __create_page_tables @利用pc寻址
adr lr, __ret @ return address @页表建立,MMU未用,利用pc寻址,lr在0x30008000之中偏移
add pc, r10, #12 @ initialise processor,执行__arm920_setup,在文件$(TOPDIR)/arch/arm/mm/proc-arm920.S
.type __switch_data, %object
__switch_data: .long __mmap_switched @__mmap_switched是以0xc0008000为偏移的
.type __ret, %function
__ret: ldr lr, __switch_data @此时lr=0xc0008000中__mmap_switched的偏移
mcr p15, 0, r0, c1, c0 @开启MMU
mrc p15, 0, r0, c1, c0, 0 @ 此时pc还在0x30008000的空间中,通过0x30004c00的页表项映射成本身
mov r0, r0
mov r0, r0
mov pc, lr @质的飞越,真正跳入内核虚空间,pc=0xc0008000+__mmap_switched的偏移
/*
* The following fragment of code is executed with the MMU on, and uses
* absolute addresses; this is not position independent.
*
* r0 = processor control register
* r1 = machine ID
* r9 = processor ID
*/
.align 5
__mmap_switched:
adr r3, __switch_data + 4 @此时所有相对于pc寻址的指令都会在0xc0000000的虚空间中
ldmia r3, {r4, r5, r6, r7, r8, sp}@ r2 = compat
@ sp = stack pointer
mov fp, #0 @ Clear BSS (and zero fp)
1: cmp r4, r5
strcc fp, [r4],#4
bcc 1b
str r9, [r6] @ Save processor ID
str r1, [r7] @ Save machine type
#ifdef CONFIG_ALIGNMENT_TRAP
orr r0, r0, #2 @ ...........A.
#endif
bic r2, r0, #2 @ Clear 'A' bit
stmia r8, {r0, r2} @ Save control register values
b SYMBOL_NAME(start_kernel)
__create_page_tables:
pgtbl r4, r5 @ page table address宏,返回页表物理地址r4=0x30004000
/*
* Clear the 16K level 1 swapper page table
*/
mov r0, r4
mov r3, #0
add r2, r0, #0x4000
1: str r3, [r0], #4
str r3, [r0], #4
str r3, [r0], #4
str r3, [r0], #4
teq r0, r2
bne 1b
/*
* Create identity mapping for first MB of kernel to
* cater for the MMU enable. This identity mapping
* will be removed by paging_init()
*/
krnladr r2, r4, r5 @ start of kernel宏,返回kernel空间的物理起始地址r2=0x30000000
add r3, r8, r2 @ flags + kernel base,r3=0x30000c1e
str r3, [r4, r2, lsr #18] @ identity mapping,为了使得MMU开启后,pc在未转换到虚地址0xc0008000的空间中之前,还能够继续映射原空间,即在0x30004c00中填入0x30000c1e,把0x30000000的虚拟空间映射到0x30000000的物理空间之中
/*
* Now setup the pagetables for our kernel direct
* mapped region. We round TEXTADDR down to the
* nearest megabyte boundary.
*/
add r0, r4, #(TEXTADDR & 0xff000000) >> 18 @ start of kernel,r0=0x30007000,计算第一级入口地址
bic r2, r3, #0x00f00000 @r2=0x30000c1e
str r2, [r0] @ PAGE_OFFSET + 0MB
add r0, r0, #(TEXTADDR & 0x00f00000) >> 18
str r3, [r0], #4 @ KERNEL + 0MB @在0x30007000填入第1M区域,c0000000==>30000000
add r3, r3, #1 << 20
str r3, [r0], #4 @ KERNEL + 1MB @在0x30007004填入第2M区域,c0100000==>30100000
add r3, r3, #1 << 20
str r3, [r0], #4 @ KERNEL + 2MB @在0x30007008填入第3M区域,c0200000==>30200000
add r3, r3, #1 << 20
str r3, [r0], #4 @ KERNEL + 3MB @在0x3000700c填入第4M区域,c0300000==>30300000
bic r8, r8, #0x0c
mov pc, lr
__arm920_setup:
mov r0, #0
mcr p15, 0, r0, c7, c7 @ invalidate I,D caches on v4
mcr p15, 0, r0, c7, c10, 4 @ drain write buffer on v4
mcr p15, 0, r0, c8, c7 @ invalidate I,D TLBs on v4
mcr p15, 0, r4, c2, c0 @ load page table pointer
mov r0, #0x1f @ Domains 0, 1 = client
mcr p15, 0, r0, c3, c0 @ load domain access register
mrc p15, 0, r0, c1, c0 @ get control register v4
/*
* Clear out 'unwanted' bits (then put them in if we need them)
*/
@ VI ZFRS BLDP WCAM
bic r0, r0, #0x0e00
bic r0, r0, #0x0002
bic r0, r0, #0x000c
bic r0, r0, #0x1000 @ ...0 000. .... 000.
/*
* Turn on what we want
*/
orr r0, r0, #0x0031
orr r0, r0, #0x2100 @ ..1. ...1 ..11 ...1
#ifndef CONFIG_CPU_DCACHE_DISABLE
orr r0, r0, #0x0004 @ .... .... .... .1..
#endif
#ifndef CONFIG_CPU_ICACHE_DISABLE
orr r0, r0, #0x1000 @ ...1 .... .... ....
#endif
mov pc, lr