首页 > 解决方案 > 为什么分页不起作用并且在 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。如英特尔文档中所述(请参阅https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-3a-part -1-manual.pdf第 4 章关于 IA-32e 分页)

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 内存进行身份映射。

第 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。它引用指向 0x7000 的页表条目 7(第 8 个)。最后 12 位 (0xe91) 是页面中的偏移量。因此,虚拟地址应该是标识映射的,因为 0x7e91 转换为 0x7e91。

问题如下。

为什么我的分页实现不起作用?

我是否错过了内存中的位排序(小端与大端)?

为什么应该包含导致页面错误的虚拟地址的CR2包含0x80?

有什么我错过的吗?

标签: assemblyx86pagingbootloader

解决方案


如英特尔文档中所述...

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


推荐阅读