AMD64 页面条目基地址字段如何将 52 位地址编码为 40 位?

How do AMD64 page entry base address fields encode a 52-bit address in 40 bits?

我正在尝试手动遍历分页结构,以便将虚拟地址转换为其物理地址。我对存储在 PML4E、PDPE、PDE 和 PTE 中的物理基址字段有疑问。我系统上的页面大小是 4KB。我在 Windows 内核模式下执行此操作。

正如amd64手册所说,cr3的第51-12位包含PML4的物理基地址。但是,它说第 11-0 位应假定为 0。我想知道是否同样的事情适用于其他分页结构的基地址字段,因为描述转换过程的图表说 52,但实际大小是只有 40(位 51-12)。

我如何用 C 进行翻译的示例:

// clear out everything except base address field
ULONG_PTR pPml4 = __readcr3() & ~0xFFF0000000000FFF,
    dataEntry;

copyAddress.PhysicalAddress.QuadPart = pPml4 + (sourceAddress.Hard.PageMapLevel4Index * 8);

if (MmCopyMemory(&dataEntry, copyAddress, 8, MM_COPY_MEMORY_PHYSICAL, &trans) != STATUS_SUCCESS) {
    ...
}

// dataEntry now has correct PML4E

// clear out everything except base address field
dataEntry &= ~0xFFF0000000000FFF;

// do I skip this?
dataEntry >>= 12;

来自手册第 5.4 节:

Translation-Table Base Address Field. The translation-table base-address field points to the physical base address of the next-lower-level table in the page-translation hierarchy. Page datastructure tables are always aligned on 4-Kbyte boundaries, so only the address bits above bit 11 are stored in the translation-table base-address field. Bits 11:0 are assumed to be 0. The size of the field depends on the mode...

所以是的,低 12 位为 0 以构成 52 位物理地址。