我有以下汇编代码来设置长模式分页。
org 0x7e00
bits 32
mov eax, 0x08000008
mov cr3, eax
pml4t:
mov dword [0x8000], 0x0900000f
mov dword [0x8004], 0x0
pdpt:
mov dword [0x9000], 0x0a00000f
mov dword [0x9004], 0x0
pdt:
mov dword [0xa000], 0x0b00000f
mov dword [0xa004], 0x0
pt:
mov eax, 0x200
mov ebx, 0x0000000f
mov ecx, 0xb000
next_entry:
mov dword [ecx], ebx
add ecx, 0x4
mov dword [ecx], 0x0
add ebx, 0x1000000 ;add 4096 to the adress pointed to by ebx (the next physical page)
add ecx, 0x4
sub eax, 0x1
cmp eax, 0x0
jne next_entry
mov eax, cr4 ;enable PAE-paging
or eax, 1 << 5
mov cr4, eax
mov ecx, 0xC0000080 ;set long mode bit in EFER MSR
rdmsr
or eax, 1 << 8
wrmsr
mov eax, cr0 ;enable paging
or eax, 1 << 31
mov cr0, eax
halt:
hlt
jmp halt
之前的引导加载程序代码会将此代码加载到地址0x7e00。然后它将在段0x8中跳转到该地址。我知道这可以正常工作,因为我已经测试了。
我在Bochs中收到以下错误,指出未找到hlt指令的物理地址(0x7e91)(位于RIP寄存器中)。
Booting from 0000:7c00
00017470660i[CPU0 ] CPU is in compatibility mode (active)
00017470660i[CPU0 ] CS.mode = 32 bit
00017470660i[CPU0 ] SS.mode = 16 bit
00017470660i[CPU0 ] EFER = 0x00000500
00017470660i[CPU0 ] | RAX=00000000e0000011 RBX=000000000000000f
00017470660i[CPU0 ] | RCX=00000000c0000080 RDX=0000000000000000
00017470660i[CPU0 ] | RSP=0000000000008000 RBP=0000000000000000
00017470660i[CPU0 ] | RSI=00000000000e0000 RDI=000000000000ffac
00017470660i[CPU0 ] | R8=0000000000000000 R9=0000000000000000
00017470660i[CPU0 ] | R10=0000000000000000 R11=0000000000000000
00017470660i[CPU0 ] | R12=0000000000000000 R13=0000000000000000
00017470660i[CPU0 ] | R14=0000000000000000 R15=0000000000000000
00017470660i[CPU0 ] | IOPL=0 id vip vif ac vm RF nt of df if tf SF zf af PF cf
00017470660i[CPU0 ] | SEG sltr(index|ti|rpl) base limit G D
00017470660i[CPU0 ] | CS:0008( 0001| 0| 0) 00000000 ffffffff 1 1
00017470660i[CPU0 ] | DS:0010( 0002| 0| 0) 00000000 ffffffff 1 1
00017470660i[CPU0 ] | SS:0010( 0005| 0| 0) 00000100 0000ffff 0 0
00017470660i[CPU0 ] | ES:0000( 0005| 0| 0) 00000000 0000ffff 0 0
00017470660i[CPU0 ] | FS:0000( 0005| 0| 0) 00000000 0000ffff 0 0
00017470660i[CPU0 ] | GS:0000( 0005| 0| 0) 00000000 0000ffff 0 0
00017470660i[CPU0 ] | MSR_FS_BASE:0000000000000000
00017470660i[CPU0 ] | MSR_GS_BASE:0000000000000000
00017470660i[CPU0 ] | RIP=0000000000007e91 (0000000000007e91)
00017470660i[CPU0 ] | CR0=0xe0000011 CR2=0x0000000000000080
00017470660i[CPU0 ] | CR3=0x0000000008000008 CR4=0x00000020
(0).[17470660] ??? (physical address not available)
00017470660e[CPU0 ] exception(): 3rd (14) exception with no resolution, shutdown status is 00h, resetting
00017470660i[SYS ] bx_pc_system_c::Reset(HARDWARE) called
00017470660i[CPU0 ] cpu hardware reset
00017470660i[APIC0 ] allocate APIC id=0 (MMIO enabled) to 0x0000fee00000
00017470660i[CPU0 ] CPUID[0x00000000]: 0000000b 756e6547 6c65746e 49656e69
00017470660i[CPU0 ] CPUID[0x00000001]: 00020652 00010800 0298e3ff bfebfbff
00017470660i[CPU0 ] CPUID[0x00000002]: 55035a01 00f0b2dd 00000000 09ca212c
00017470660i[CPU0 ] CPUID[0x00000003]: 00000000 00000000 00000000 00000000
00017470660i[CPU0 ] CPUID[0x00000004]: 1c004121 01c0003f 0000003f 00000000
00017470660i[CPU0 ] CPUID[0x00000005]: 00000040 00000040 00000003 00001120
00017470660i[CPU0 ] CPUID[0x00000006]: 00000007 00000002 00000001 00000000
00017470660i[CPU0 ] CPUID[0x00000007]: 00000000 00000000 00000000 00000000
00017470660i[CPU0 ] CPUID[0x00000008]: 00000000 00000000 00000000 00000000
00017470660i[CPU0 ] CPUID[0x00000009]: 00000000 00000000 00000000 00000000
00017470660i[CPU0 ] WARNING: Architectural Performance Monitoring is not implemented
00017470660i[CPU0 ] CPUID[0x0000000a]: 07300403 00000004 00000000 00000603
00017470660i[CPU0 ] CPUID[0x0000000b]: 00000000 00000000 00000000 00000000
00017470660i[CPU0 ] CPUID[0x80000000]: 80000008 00000000 00000000 00000000
00017470660i[CPU0 ] CPUID[0x80000001]: 00000000 00000000 00000001 28100000
00017470660i[CPU0 ] CPUID[0x80000002]: 65746e49 2952286c 726f4320 4d542865
00017470660i[CPU0 ] CPUID[0x80000003]: 35692029 55504320 20202020 4d202020
00017470660i[CPU0 ] CPUID[0x80000004]: 30323520 20402020 30342e32 007a4847
00017470660i[CPU0 ] CPUID[0x80000005]: 00000000 00000000 00000000 00000000
00017470660i[CPU0 ] CPUID[0x80000006]: 00000000 00000000 01006040 00000000
00017470660i[CPU0 ] CPUID[0x80000007]: 00000000 00000000 00000000 00000100
00017470660i[CPU0 ] CPUID[0x80000008]: 00003028 00000000 00000000 00000000
00017470660i[CPU0 ] CPU Features supported:
00017470660i[CPU0 ] x87
00017470660i[CPU0 ] 486ni
00017470660i[CPU0 ] pentium_ni
00017470660i[CPU0 ] p6ni
00017470660i[CPU0 ] mmx
00017470660i[CPU0 ] debugext
00017470660i[CPU0 ] vme
00017470660i[CPU0 ] pse
00017470660i[CPU0 ] pae
00017470660i[CPU0 ] pge
00017470660i[CPU0 ] pse36
00017470660i[CPU0 ] mtrr
00017470660i[CPU0 ] pat
00017470660i[CPU0 ] sysenter_sysexit
00017470660i[CPU0 ] clflush
00017470660i[CPU0 ] sse
00017470660i[CPU0 ] sse2
00017470660i[CPU0 ] sse3
00017470660i[CPU0 ] ssse3
00017470660i[CPU0 ] sse4_1
00017470660i[CPU0 ] sse4_2
00017470660i[CPU0 ] popcnt
00017470660i[CPU0 ] mwait
00017470660i[CPU0 ] vmx
00017470660i[CPU0 ] smx
00017470660i[CPU0 ] longmode
00017470660i[CPU0 ] lm_lahf_sahf
00017470660i[CPU0 ] nx
00017470660i[CPU0 ] cmpxhg16b
00017470660i[CPU0 ] rdtscp
00017470660i[CPU0 ] aes_pclmulqdq
00017470660i[CPU0 ] x2apic
00017470660i[PLUGIN] reset of 'pci' plugin device by virtual method
00017470660i[PLUGIN] reset of 'pci2isa' plugin device by virtual method
00017470660i[PLUGIN] reset of 'cmos' plugin device by virtual method
00017470660i[PLUGIN] reset of 'dma' plugin device by virtual method
00017470660i[PLUGIN] reset of 'pic' plugin device by virtual method
00017470660i[PLUGIN] reset of 'pit' plugin device by virtual method
00017470660i[PLUGIN] reset of 'vga' plugin device by virtual method
00017470660i[PLUGIN] reset of 'floppy' plugin device by virtual method
00017470660i[PLUGIN] reset of 'acpi' plugin device by virtual method
00017470660i[PLUGIN] reset of 'hpet' plugin device by virtual method
00017470660i[PLUGIN] reset of 'ioapic' plugin device by virtual method
00017470660i[PLUGIN] reset of 'keyboard' plugin device by virtual method
00017470660i[PLUGIN] reset of 'harddrv' plugin device by virtual method
00017470660i[PLUGIN] reset of 'pci_ide' plugin device by virtual method
00017470660i[PLUGIN] reset of 'unmapped' plugin device by virtual method
00017470660i[PLUGIN] reset of 'biosdev' plugin device by virtual method
00017470660i[PLUGIN] reset of 'speaker' plugin device by virtual method
00017470660i[PLUGIN] reset of 'extfpuirq' plugin device by virtual method
00017470660i[PLUGIN] reset of 'parallel' plugin device by virtual method
00017470660i[PLUGIN] reset of 'serial' plugin device by virtual method
00017470660i[PLUGIN] reset of 'gameport' plugin device by virtual method
00017470660i[PLUGIN] reset of 'iodebug' plugin device by virtual method
00017470660i[PLUGIN] reset of 'usb_uhci' plugin device by virtual method
Next at t=17470661
(0) [0x0000fffffff0] f000:fff0 (unk. ctxt): jmpf 0xf000:e05b ; ea5be000f0
我为每个表设置一个条目,直到到达包含512个条目的最后一个表。它应该标识映射2MB的内存。
如调试器CR3所示,0x0000000008000008
它引用内存中的地址0x8000。如英特尔文档中所述(请参阅https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-3a-part关于IA-32e分页的第4章-1-manual.pdf)
CR3 bits
Bit Contents
Position(s)
2:0 Ignored
3 (PWT) Page-level write-through
4 (PCD) Page-level cache disable
11:5 Ignored
M–1:12 Physical address of the 4-KByte aligned PML4 table used for linear-address translation
63:M Reserved (must be 0)
Note: M is 52
在CR3中,除第3位外,所有位均为零,因此我启用了直写。PML4表的地址从第12位开始,找到的地址为0x8000。
如果xp /512bx 0x8000
在Bochs调试器中使用它,则会在读取的第一个表(0x8000)处显示内存:
0x0000000000008000 <bogus+ 0>: 0x0f 0x00 0x00 0x09 0x00 0x00 0x00 0x00
结果是0f 00 00 09 00 00 00 00
小尾数法则转换成00 00 00 00 09 00 00 0f
可读的大尾数法。
该条目的格式类似于CR3(地址从第12位开始)。此处引用的地址是0x9000。
如果xp /512bx 0x9000
在Bochs调试器中使用它,则会在第二个表(0x9000)上向我显示内存:
0x0000000000009000 <bogus+ 0>: 0x0f 0x00 0x00 0x0a 0x00 0x00 0x00 0x00
结果是0f 00 00 0a 00 00 00 00
小端序,00 00 00 00 0a 00 00 0f
大端序。
引用的地址是0xa000。
如果xp /512bx 0xa000
在Bochs调试器中使用它,则会在读取的第三个表(0xa000)处显示内存:
0x000000000000a000 <bogus+ 0>: 0x0f 0x00 0x00 0x0b 0x00 0x00 0x00 0x00
结果是0f 00 00 0b 00 00 00 00
小端序,00 00 00 00 0b 00 00 0f
大端序。
引用的地址是0xb000。
如果xp /4096bx 0xb000
在Bochs调试器中使用它,则会向我显示最后读取的表(0xb000)的内存:
<bochs:5> xp /4096bx 0xb000
[bochs]:
0x000000000000b000 <bogus+ 0>: 0x0f 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x000000000000b008 <bogus+ 8>: 0x0f 0x00 0x00 0x01 0x00 0x00 0x00 0x00
0x000000000000b010 <bogus+ 16>: 0x0f 0x00 0x00 0x02 0x00 0x00 0x00 0x00
0x000000000000b018 <bogus+ 24>: 0x0f 0x00 0x00 0x03 0x00 0x00 0x00 0x00
0x000000000000b020 <bogus+ 32>: 0x0f 0x00 0x00 0x04 0x00 0x00 0x00 0x00
0x000000000000b028 <bogus+ 40>: 0x0f 0x00 0x00 0x05 0x00 0x00 0x00 0x00
0x000000000000b030 <bogus+ 48>: 0x0f 0x00 0x00 0x06 0x00 0x00 0x00 0x00
0x000000000000b038 <bogus+ 56>: 0x0f 0x00 0x00 0x07 0x00 0x00 0x00 0x00
0x000000000000b040 <bogus+ 64>: 0x0f 0x00 0x00 0x08 0x00 0x00 0x00 0x00
第一个条目0x0f 0x00 0x00 0x00 0x00 0x00 0x00 0x00
翻译为00 00 00 00 00 00 00 0f
大端。它引用地址0x0。这意味着虚拟地址0x00 00 00 00 00 00 01 23
应当映射到物理RAM中的0x123。应该为2MB内存进行身份映射。
第八项是0x0f 0x00 0x00 0x07 0x00 0x00 0x00 0x00
其翻译成00 00 00 00 07 00 00 0f
。这应该引用从地址0x7000开始的4KB物理页面。
如果我使用地址0x7e91,它将转换为0x00 00 00 00 00 00 7e 91
。由于启用了分页,该地址现在应该是虚拟的。它引用了指向0x9000的PLM4条目0。它引用了指向0xa000的PDPT条目0。它引用了指向0xb000的PDT条目0。它引用页表的条目7(第8个),该条目指向0x7000。最后12位(0xe91)是页面中的偏移量。因此,虚拟地址应进行身份映射,因为0x7e91转换为0x7e91。
问题如下。
为什么我的分页实施不起作用?
我是否错过了内存中的位排序顺序(小字节序与大字节序)?
为什么应该包含导致页面错误的虚拟地址的CR2包含0x80?
有什么我想念的吗?
如英特尔文档中所述...
11:5 Ignored M–1:12 Physical address of ...
此描述似乎具有误导性:
在早期的x86-32 CPU中,CR3的CR3(而不是CR3的位[32:12])是指向页面目录表(物理地址)的指针。
由于该地址的低12位始终必须为0,因此其中某些位(PCD和PWT)在以后的CPU中用于其他目的。
如果对于64位模式仍然如此,CR3=0x8000008
则不指定地址0x8000
,而是指定地址0x8000000
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句