函数__create_page_tables介绍:
假设内核起始物理地址是0xA0008000,虚拟地址是0xC0008000,下面的代码是建立内核起始处4MB空间的映射,
采用了一级映射方式,即段式(section)映射方式,每段映射范围为1MB空间。于是需要建立4个表项,实现:
虚拟地址0xC0000000~0xC0300000,映射到物理地址0xA0000000~0xA0300000。
.macro pgtbl, reg, rambase
adr \reg, stext
sub \reg, \reg, #0x4000
.endm
.macro krnladr, rd, pgtable, rambase
bic \rd, \pgtable, #0x000ff000
.endm
/*
* Setup the initial page tables. We only setup the barest
* amount which are required to get the kernel running, which
* generally means mapping in the kernel code.
*
* We only map in 4MB of RAM, which should be sufficient in
* all cases.
*
* r5 = physical address of start of RAM
* r6 = physical IO address
* r7 = byte offset into page tables for IO
* r8 = page table flags
*/
1 __create_page_tables:
/* r5中存放着内核启动的地址0xa0008000 */
/* pgtbl将启动地址减去0x4000,存放到r4=0xa0004000 */
2 pgtbl r4, r5 @ page table address
/*
* Clear the 16K level 1 swapper page table
*/
/* r0 = 0xa0004000 */
3 mov r0, r4
4 mov r3, #0
/* r2 = 0xa0008000 */
5 add r2, r0, #0x4000
/* 清除16k空间,addr 0xa0004000: 0xa0008000 is page table, total 16K*/
6 1: str r3, [r0], #4
7 str r3, [r0], #4
8 str r3, [r0], #4
9 str r3, [r0], #4
10 teq r0, r2
11 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()
*/
/* r2 = 0xa0040000 & 0x000ff000 = 0xa00000000 */
12 krnladr r2, r4, r5 @ start of kernel
/* r3 = 0xa0000000 + 0x00000c0e = 0xa00000c0e */
/* r8 = 0x00000c0e在__lookup_processor_type函数中初始化 */
13 add r3, r8, r2 @ flags + kernel base
/* value r3=0xa0000c0e store to addr 0xa0006800*/
/* r4 = 0xa0006800 */
14 str r3, [r4, r2, lsr #18] @ identity mapping
/*
* Now setup the pagetables for our kernel direct
* mapped region. We round TEXTADDR down to the
* nearest megabyte boundary.
*/
/* TEXTADDR= 0xC0008000 有关TEXTADDR参考<> */
/* start of kernel, r0=0xa0007000 */
15 add r0, r4, #(TEXTADDR & 0xff000000) >> 18 @ start of kernel
/* r2=0xa0000c0e */
16 bic r2, r3, #0x00f00000
/* 0xa0000c0e的数据写入到0xa00070000 */
17 str r2, [r0] @ PAGE_OFFSET + 0MB
/* r0=0xa0007000, no change */
18 add r0, r0, #(TEXTADDR & 0x00f00000) >> 18
19 str r3, [r0], #4 @ KERNEL + 0MB
20 add r3, r3, #1 << 20
21 str r3, [r0], #4 @ KERNEL + 1MB
22 add r3, r3, #1 << 20
23 str r3, [r0], #4 @ KERNEL + 2MB
24 add r3, r3, #1 << 20
25 str r3, [r0], #4 @ KERNEL + 3MB
/*
* Ensure that the first section of RAM is present.
* we assume that:
* 1. the RAM is aligned to a 32MB boundary
* 2. the kernel is executing in the same 32MB chunk
* as the start of RAM.
*/
26 bic r0, r0, #0x01f00000 >> 18 @ round down
27 and r2, r5, #0xfe000000 @ round down
28 add r3, r8, r2 @ flags + rambase
29 str r3, [r0]
30 bic r8, r8, #0x0c @ turn off cacheable
31 mov pc, lr
我已经把每一步涉及的地址详细列出了,读者可以自行对照阅读。第11~16行,清空页表项从0xA0004000到0xA00,8000,共16KB。
第28行,取得__cpu_mmu_flags。第35~45行,填写页表项,共4项。读者可以对照XScale的地址映射手册,
因为采用的是段式映射方式,所以每1MB虚拟空间映射到相同的页表表项,根据手册说明,段式映射只有一级表索引,
是虚拟地址的前12位;而页式映射的页目录表是前12位,页表是接着的8位,最后12位才是页内偏移,
读者一定不要和386的10位页目录表,10位页表的机制相混淆。我们举个例子说明,对于虚拟地址0xC00x,xxxxx,
其前12位为C00,页表基址为0xA000,4000,所以表项地址为0xA000,4000+0xC00<<2=0xA000,7000,
而这个地址内容为0xA0000C0E,其前12位0xA00为段基地址,后20位为一些flags,这是从刚才__bva0_proc_info中取得的。
阅读(693) | 评论(0) | 转发(0) |