为什么在Bochs上分页不起作用并且获取物理地址不可用错误

问题描述

我有以下汇编代码来设置长模式分页

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。如Intel文档中所述(请参阅有关IA-32e分页的第四章https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-3a-part-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。

如果我在Bochs调试器中使用xp /512bx 0x8000,它将向我显示其读取的第一个表(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。

如果我在Bochs调试器中使用xp /512bx 0x9000,它将显示第二个表(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。

如果我在Bochs调试器中使用xp /512bx 0xa000,它将向我显示其读取的第三张表(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。

如果我在Bochs调试器中使用xp /4096bx 0xb000,它将向我显示最后读取的表(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内存进行身份映射。

第8个条目是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