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

标签 assembly x86 paging bootloader

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

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 文档中所述(请参阅第 4 章有关 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。

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

问题如下。

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

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

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

有什么我错过的吗?

最佳答案

As stated in Intel's documentation ...

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 地址>.

关于assembly - 为什么 Bochs 上的分页不起作用并且获取物理地址不可用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63981828/

相关文章:

multithreading - CAS 碰撞的 CPU 内部特性是什么?

c - 为什么这段代码返回 "Segmentation fault"错误?

assembly - 计算JMP指令的地址

asp.net - 在没有数据库的情况下在 ASP.NET 中进行分页

c++ - 对不同的整数宽度使用 xadd

有人可以解释一下 exynos ARM 中的电源控制寄存器吗?

windows - 在 32 位 Windows 上使用 NASM 汇编创建一个 exe 文件

c++ - 对签名数据进行逻辑右移

ios - 在特定索引路径启动 UICollectionView

asp.net-core - Blazor 组件分页