- 论坛徽章:
- 0
|
下面看一看 init_page() 的代码:
-------------------------------------------------------------------------------------------------
init_page:
PG_P equ 0x01
PG_W equ 0x02
PG_USER equ 0x04
PG_PWT equ 0x08
PG_PCD equ 0x10
PG_PS equ 0x80
;PDP-table structure initialization
; PDPE0: virtual address: 0x00000000 ~ 0x3fffffff
mov dword [PDPE_BASE], 0x3fc01000 | PG_P
mov dword [PDPE_BASE+4], 0
; PDPE1: virtual address: 0x40000000 ~ 0x7fffffff (not support)
mov dword [PDPE_BASE+8], 0
mov dword [PDPE_BASE+8+4],0
; PDPE2: virtual address: 0x80000000 ~ 0xbfffffff
mov dword [PDPE_BASE+16], 0x3fc02000 | PG_P
mov dword [PDPE_BASE+16+4], 0
; PDPE3: virtual address: 0xc0000000 ~ 0xffffffff
mov dword [PDPE_BASE+24], 0x3fc03000 | PG_P
mov dword [PDPE_BASE+24+4], 0
; PD-table structure initialization
;0x00400000~0x00bfffff(virtual address) map into 0x00400000~0x00bffffff(physical address)
;------- PDE[0] map 0x6000 ~ 7fff and 0xb8000 ~ b9fff, 0x10000 ~ 0x10fff --------------
; PDE[0](00000000~xxxxxxxx): r/w=1, u/s=1,p=1
mov dword [0x3fc01000], 0x3fd01000 | PG_P | PG_USER | PG_W
mov dword [0x3fc01000+4], 0
;----------------------kernel's 2M page -----------------------
; 0x80000000~0x801fffff(virtual address) map into physical page(0x30000000~0x301fffff)
mov dword [0x3fc02000], 0x30000000 | PG_P | PG_PS | PG_W
mov dword [0x3fc02004], 0
; PDE[1](00200000~xxxxxxxx) not support
;------- PDE[2] ~ PDE[5] map 0x00400000~0x00bfffff -------------------
; PDE[2](00400000~xxxxxxxx): r/w=1, u/s=1,p=1
mov dword [0x3fc01000+2*8], (0x3fd02000) | PG_P | PG_USER | PG_W
mov dword [0x3fc01000+2*8+4], 0
; PDE[3](00600000~xxxxxxxx): r/w=1, u/s=1,p=1
mov dword [0x3fc01000+3*8], (0x3fd03000) | PG_P | PG_USER | PG_W
mov dword [0x3fc01000+3*8+4], 0
; PDE[4](00800000~xxxxxxxx): r/w=1, u/s=1,p=1
mov dword [0x3fc01000+4*8], (0x3fd04000) | PG_P |PG_USER | PG_W
mov dword [0x3fc01000+4*8+4], 0
; PDE[5](00a00000~xxxxxxxx): r/w=1,u/s=1,p=1
mov dword [0x3fc01000+5*8], (0x3fd05000) | PG_P | PG_USER | PG_W
mov dword [0x3fc01000+5*8+4], 0
;-------- PDE[510] map 0xffc00000 ~ 0xffdfffff ---------------------
;--------- 0xffc00000~0xffdfffff(virtual address) map into physical page(0x3fc00000~ 0x3fdfffff)
; PDE[510](ffc00000~xxxxxxxx):r/w=1, u/s=0,p=1,ps=1 (2M page)
mov dword [0x3fc03000+510*8], 0x3fc00000 | PG_P | PG_W | PG_PS
mov dword [0x3fc03000+510*8+4], 0
; Page Table structure
;
;----------- PTE[6](0x6000~0x6fff) map into 0x6000~0x6fff(physical address) -------------
mov dword [0x3fd01000+6*8], 0x6000 | PG_P | PG_USER | PG_W
mov dword [0x3fd01000+6*8+4], 0
; PTE[7](0x7000~0x7fff) map into 0x7000~0x7fff(physical address)
mov dword [0x3fd01000+7*8], 0x7000 | PG_P | PG_USER | PG_W
mov dword [0x3fd01000+7*8+4], 0
; PTE[0x10](0x10000~0x10fff) map into 0x10000~0x10fff(physical address)
mov dword [0x3fd01000+0x10*8], 0x10000 | PG_P | PG_USER | PG_W
mov dword [0x3fd01000+0x10*8+4], 0
; PTE[0xb8](0xb8000~0xb8fff) map into 0xb8000~0xb8fff(physcial address)
mov dword [0x3fd01000+0xb8*8], 0xb8000| PG_P | PG_USER | PG_W
mov dword [0x3fd01000+0xb8*8+4], 0
;PTE[0xb9](0xb9000~0xb9fff) map into 0xb9000~0xb9fff(physical address)
mov dword [0x3fd01000+0xb9*8], 0xb9000 | PG_P | PG_USER | PG_W
mov dword [0x3fd01000+0xb9*8+4], 0
;----------------------------------------------------------------
; PTE[0] (0x00400000~00400fff) map into 0x400000~0x400fff(physical address)
; mov dword [0x3fd02000], 0x400000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+4], 0
; PTE[1] (0x401000~0x401fff) map into 0x401000~0x401fff
; mov dword [0x3fd02000+8], 0x401000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+8+4], 0
; PTE[2] (0x402000~0x402fff) map into 0x402000~0x402fff
; mov dword [0x3fd02000+2*8], 0x402000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+2*8+4], 0
; PTE[3] (0x403000~0x403fff) map into 0x403000~0x403fff
; mov dword [0x3fd02000+3*8], 0x403000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+3*8+4], 0
; PTE[4] (0x404000~0x404fff) map into 0x404000~0x404fff
; mov dword [0x3fd02000+4*8], 0x404000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+4*8+4], 0
; PTE[5] (0x405000~0x405fff) map into 0x405000~0x405fff
; mov dword [0x3fd02000+5*8], 0x405000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+5*8+4], 0
; PTE[6] (0x406000~0x406fff) map into 0x406000~0x406fff
; mov dword [0x3fd02000+6*8], 0x406000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+6*8+4], 0
; PTE[7] (0x407000~0x407fff) map into 0x407000~0x407fff
; mov dword [0x3fd02000+7*8], 0x407000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+7*8+4], 0
; PTE[8] (0x408000~0x408fff) map into 0x408000~0x408fff
; mov dword [0x3fd02000+8*8], 0x408000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+8*8+4], 0
; PTE[9] (0x409000~0x409fff) map into 0x409000~0x409fff
; mov dword [0x3fd02000+9*8], 0x409000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+9*8+4], 0
; PTE[0xa] (0x40a000~0x40afff) map into 0x40a000~0x40afff
; mov dword [0x3fd02000+0xa*8], 0x40a000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+0xa*8+4], 0
; PTE[0xb] (0x40b000~0x40bfff) map into 0x40b000~0x40bfff
; mov dword [0x3fd02000+0xb*8], 0x40b000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+0xb*8+4], 0
; PTE[0xc] (0x40c000~0x40cfff) map into 0x40c000~0x40cfff
; mov dword [0x3fd02000+0xc*8], 0x40c000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+0xc*8+4], 0
; PTE[0xd] (0x40d000~0x40dfff) map into 0x40d000~0x40dfff
; mov dword [0x3fd02000+0xd*8], 0x40d000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+0xd*8+4], 0
; PTE[0xe] (0x40e000~0x40efff) map into 0x40e000~0x40efff
; mov dword [0x3fd02000+0xe*8], 0x40e000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+0xe*8+4], 0
; PTE[0xf] (0x40f000~0x40ffff) map into 0x40f000~0x40ffff
; mov dword [0x3fd02000+0xf*8], 0x40f000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+0xf*8+4], 0
; PTE[0x10] (0x00410000~00410fff) map into 0x410000~0x410fff(physical address)
; mov dword [0x3fd02000+0x10*8], 0x410000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+0x10*8+4], 0
; PTE[0x11] (0x411000~0x411fff) map into 0x411000~0x411fff
; mov dword [0x3fd02000+0x11*8], 0x411000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+0x11*8+4], 0
; PTE[0x12] (0x412000~0x412fff) map into 0x412000~0x412fff
; mov dword [0x3fd02000+0x12*8], 0x412000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+0x12*8+4], 0
; PTE[0x13] (0x413000~0x413fff) map into 0x413000~0x413fff
; mov dword [0x3fd02000+0x13*8], 0x413000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+0x13*8+4], 0
; PTE[0x14] (0x414000~0x414fff) map into 0x414100~0x414fff
; mov dword [0x3fd02000+0x14*8], 0x414000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+0x14*8+4], 0
; PTE[0x15] (0x415000~0x415fff) map into 0x415000~0x415fff
; mov dword [0x3fd02000+0x15*8], 0x415000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+0x15*8+4], 0
; PTE[0x16] (0x416000~0x416fff) map into 0x416000~0x416fff
; mov dword [0x3fd02000+0x16*8], 0x416000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+0x16*8+4], 0
; PTE[0x17] (0x417000~0x417fff) map into 0x417000~0x417fff
; mov dword [0x3fd02000+0x17*8], 0x417000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+0x17*8+4], 0
; PTE[0x18] (0x418000~0x418fff) map into 0x418000~0x418fff
; mov dword [0x3fd02000+0x18*8], 0x418000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+0x18*8+4], 0
;
; PTE[0x19] (0x419000~0x419fff) map into 0x419000~0x419fff
; mov dword [0x3fd02000+0x19*8], 0x419000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+0x19*8+4], 0
; PTE[0x1a] (0x41a000~0x41afff) map into 0x41a000~0x41afff
; mov dword [0x3fd02000+0x1a*8], 0x41a000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+0x1a*8+4], 0
; PTE[0x1b] (0x41b000~0x41bfff) map into 0x41b000~0x41bfff
; mov dword [0x3fd02000+0x1b*8], 0x41b000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+0x1b*8+4], 0
; PTE[0x1c] (0x41c000~0x41cfff) map into 0x41c000~0x41cfff
; mov dword [0x3fd02000+0x1c*8], 0x41c000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+0x1c*8+4], 0
; PTE[0x1d] (0x41d000~0x41dfff) map into 0x41d000~0x41dfff
; mov dword [0x3fd02000+0x1d*8], 0x41d000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+0x1d*8+4], 0
; PTE[0x1e] (0x41e000~0x41efff) map into 0x41e000~0x41efff
; mov dword [0x3fd02000+0x1e*8], 0x41e000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+0x1e*8+4], 0
; PTE[0x1f] (0x41f000~0x41ffff) map into 0x41f000~0x41ffff
; mov dword [0x3fd02000+0x1f*8], 0x41f000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+0x1f*8+4], 0
; virtual address(0x00400000~0x00bfffff )
; -> (map into)
; physical address(0x00400000~0x00bfffff)
push 0 ; nxe flages
push PG_P|PG_USER|PG_W ; page attribute
push 0x00400000 ; physical page
push 0xbff-0x400 ; size of pages(0x00400000~0x00bfffff)
push 0x3fd02000 ; pte base of va(0x00400000)
call init_pae4K_pte ; init_pae4K_pte()
ret
; init_pae4K_pte(pte_base, size, physcial_page, attribute, nxe)
init_pae4K_pte:
push ebp
mov ebp,esp
mov ebx, [ebp+8] ; pte_base
mov edi, [ebp+16] ; physcial_page
mov eax, [ebp+20] ; attribute
mov edx, [ebp+24] ; NXE flags
mov esi, [ebp+12] ; size
xor ecx, ecx
or edi, eax ; physcial page | attribute
shl edx,31 ; NXE for pte[63]
init_pae4K_pte_l1:
test esi, esi
jz init_pae4K_pte_done
mov dword [ebx+ecx*8], edi ; pte[31:0]
mov dword [ebx+ecx*8+4], edx ; pte[63:0]
add edi, 0x1000 ; next 4K-page
inc ecx
dec esi
jmp init_pae4K_pte_l1
init_pae4K_pte_done:
mov esp, ebp
pop ebp
ret 20 |
注释部分用颜色标出
1、看一看 0x8010 0000(virtual address)是如何映射到 phyiscal address 的(2M page)
0x80100000 分解为:
PDPT PDT PT offset
-------------------------------------------
10 000000000 10000000 XXX
即: PDPT[2] + PDT[0] + offset (2M)
(1) PDPT 的地址
CR3 装载的是 PDPT(page directory pointer table)的地址,它的值是 0x3fc00000
因此: 0x80100000 的 PDPE (page directory pointer entry)是 PDPT[2]
PDPT[2] = 0x3fc00000 + 2*8
即代码中的:
mov dword [PDPE_BASE+16], 0x3fc02000 | PG_P
mov dword [PDPE_BASE+16+4], 0
---------------------------------------------------------------
PDPE[31:0] = 0x3fc02000 | PG_P
PDPE[63:32] = 0x00000000
此处:PDT 基址被设为: 0x3fc02000
(2) PDT (page directory table)的地址
由 (1) 步所得,PDT 的地址在 0x3fc02000 处,来看一看 0x80100000 的 PDE 是多少
PDE = PDT[0] = 0x3fc02000 处
代码中:
;----------------------kernel's 2M page -----------------------
; 0x80000000~0x801fffff(virtual address) map into physical page(0x30000000~0x301fffff)
mov dword [0x3fc02000], 0x30000000 | PG_P | PG_PS | PG_W
mov dword [0x3fc02004], 0
因此:0x80100000 的 PDE 是:
PDE[31:0] = 0x30000000 | PG_P | PG_PS | PG_W
PDE[63:32] = 0x00000000
---------------------------------------------------------------
代码中,开启了 2M pgae 结构。因此,0x80100000 将被直接映射到 0x30000000(2M page)上
即,标志中的 PG_PS 标志位。
在此,PDE 也就等于它的 PTE 结构。
2、再来看一看 0x00404000(virtual address)是如何映射的(4K page)
0x00404000 分解为:
PDPT PDT PT offset
-----------------------------------------
00 000000010 000000100 XXX
即: PDPT[0] + PDT[2] + PT[4] + offset(4K)
(1) PDPE 的值
由上面得 PDE = PDPT[0],
因此,在 0x3fc00000 地址上
代码中:
mov dword [PDPE_BASE], 0x3fc01000 | PG_P
mov dword [PDPE_BASE+4], 0
因此,PDPE[31:0] = 0x3fc01000 | PG_P
PDPE[63:32] = 0
(2) PDE 的值
由 (1) 步得,PDE = PDT[2],PDT 的地址在 0x3fc01000 上
因此,PDE = PDT[2] = 0x3fc01000 + 2*8
代码中:
; PDE[2](00400000~xxxxxxxx): r/w=1, u/s=1,p=1
mov dword [0x3fc01000+2*8], 0x3fd02000 | PG_P | PG_USER | PG_W
mov dword [0x3fc01000+2*8+4], 0
------------------------------------------------------------
PDE[31:0] = 0x3fd02000 | PG_P | PG_USER | PG_W
PDE[63:32] = 0
即,page table 地址在 0x3fd02000 上
(3) PTE 的值
PTE 的值为 PT[4]。由上可得 PT 为 0x3fd02000
因此,PTE 的值应为:0x3fd02000 + 4*8 上
代码中:
; PTE[4] (0x404000~0x404fff) map into 0x404000~0x404fff
; mov dword [0x3fd02000+4*8], 0x404000 | PG_P | P_USER | PG_W
; mov dword [0x3fd02000+4*8+4], 0
(4) page 的值
由 (3) 得,0x00404000 最终映射到 0x00404000 page 上
在蓝色注释部分,是一条条写的方式,若从 0x00400000 ~ 0x00bfffff 一条条写映射表,几乎是不可能的事情
所以:定了一个 init_pae4K_pte() 函数,自动完成填写从 0x400000 ~ 0xbfffff 映射表的填写
[ 本帖最后由 mik 于 2009-7-19 19:49 编辑 ] |
|