What is the reason to extend page-directory-pointer-table base address in cr3 from 20 to 27 bits in AMD64 legacy mode (PAE)?

huangapple go评论58阅读模式
英文:

What is the reason to extend page-directory-pointer-table base address in cr3 from 20 to 27 bits in AMD64 legacy mode (PAE)?

问题

我对PAE和分页的理解如下,可能不完全,所以可能有错误。在AMD64系统编程手册中有这样一张图片:

What is the reason to extend page-directory-pointer-table base address in cr3 from 20 to 27 bits in AMD64 legacy mode (PAE)?

其中说,即使没有启用PAE,但启用了PSE,也可以访问40位物理地址,启用PAE(以及可选的PSE)后,可以访问52位物理地址,这没问题。

根据我后来看到的启用PAE的PTE,

What is the reason to extend page-directory-pointer-table base address in cr3 from 20 to 27 bits in AMD64 legacy mode (PAE)?

物理页基地址字段从20位扩展到了40位,因此可以使用52个物理地址(使用40位页框号)。

然后我查看了启用和未启用PAE的CR3寄存器:

What is the reason to extend page-directory-pointer-table base address in cr3 from 20 to 27 bits in AMD64 legacy mode (PAE)?

没有启用PAE时,它有20位用于页目录表地址,这意味着我们可以指定4G地址空间内的任何页面:

// 最大页框号
2^20 - 1 = 1048575

// PAGE_SIZE = 4096
1048576 * 4096 = 4 GiB

因此,可以将PDT基地址存储在4G内,看起来足够了。即使我们打算访问40位物理地址空间,从理论上讲,我们也可以在内存中保存所有页表,因为启用PSE时,页表大小为4 KiB

// 最大地址空间
2^40 = 1 TiB

// 我们需要多少个页表
// 来访问全部2^40物理地址空间
1 TiB / 4 GiB = 256

// 我们需要1 MiB内存
// 能够存储所有页表
// 启用PSE以访问2^40
// 物理地址空间。
//
// 由于PDE是4 KiB对齐的内存空间
// 页表大小恰好为4 KiB
256 * 4 KiB = 1 MiB

一旦启用PAE,无论是否使用PSE,都可以访问所有52位物理地址。唯一困扰我的时刻是CR3寄存器中的PDT基地址从20位扩展到了27位:

// 最大物理地址空间
2^52 = 4 PiB

// 最大页表数
4 PiB / 4 GiB = 1048576

// 未启用PSE时,页表大小约为8 KiB
1048576 * 8 KiB = 8 GiB

// 启用PSE时,页表大小约为4 KiB
1048576 * 4 KiB = 4 GiB

因此,从理论上讲,如果启用PAE但未启用PSE,则需要约8 GiB的内存来存储所有页表,而启用PSE则需要约4 GiB。由于这只是理论上的事情,那些年代没有人能够使用4 PiB的物理地址空间,所以似乎将CR3中的PDT基地址扩展到27位是一种浪费,没有真正的用途,因为在生产基础系统中没有人以1:1的比例存储所有页表(也许我错了),对我来说这看起来很奇怪。

因此,归根结底,我的问题与标题中的相同 - “为什么要在AMD64传统模式(PAE)中将页目录指针表基地址从20位扩展到27位?”

英文:

My understanding of PAE and paging is following and may be incomplete, so I may be wrong. In AMD64 system programming manual there's such a picture:

What is the reason to extend page-directory-pointer-table base address in cr3 from 20 to 27 bits in AMD64 legacy mode (PAE)?

that says it is possible to address 40 bits physical address without PAE enabled, but with PSE enabled, and to address 52 bits physical address using PAE (and optional PSE), that's OK.

As I can see later for PTE with PAE enabled

What is the reason to extend page-directory-pointer-table base address in cr3 from 20 to 27 bits in AMD64 legacy mode (PAE)?

Physical page base address field was extended from 20 bits to 40 bits, so it is possible to use 52 physical addresses (using 40 bits page frame numbers).

Then I took a look at CR3 register with and without PAE enabled:

What is the reason to extend page-directory-pointer-table base address in cr3 from 20 to 27 bits in AMD64 legacy mode (PAE)?

Without PAE it has 20 bits for page directory table address, which means that we may specify any page within 4G address space:

// max page frame number
2^20 - 1 = 1048575

// PAGE_SIZE = 4096
1048576 * 4096 = 4 GiB

So, it is possible to store PDT base address within 4G, looks like it's fairly enough. Even if we're going to address 40 bits physical address space we can, theoretically, hold all page tables in memory, since with PSE enabled page table size is 4 KiB:

// max address space
2^40 = 1 TiB

// how many page tables we need
// to address ALL 2^40 physical address space
1 TiB / 4 GiB = 256

// we need 1 MiB of memory
// to be able to store ALL page tables
// with PSE enabled to address 2^40
// physical address space.
//
// Since PDE is 4 KiB aligned memory space
// the page table size will be exactly 4 KiB
256 * 4 KiB = 1 MiB

Once, PAE enabled it doesn't matter whether PSE is used or not, we can address all 52 bits physical addresses. The only moment where I'm stuck is that PDT base address in CR3 register was extended from 20 to 27 bits:

// max physical address space
2^52 = 4 PiB

// max number of page tables
4 PiB / 4 GiB = 1048576

// without PSE page table size is about 8 KiB
1048576 * 8 KiB = 8 GiB

// with PSE enabled page table size is about 4 KiB
1048576 * 4 KiB = 4 GiB

So, theoretically, we need about 8 GiB memory to store all page tables if PAE enabled without PSE and about 4 GiB with PSE enabled. Since, this is only theoretical things and nobody in those years could use 4 PiB physical address space, so looks like extending PDT base address to 27 bits in CR3 is a waste and has no real usage, since nobody stores ALL page tables 1:1 in production based systems (maybe I'm wrong), as for me this looks weird.

So, after all, my question is the same as in the title -- "what is the reason to extend page-directory-pointer-table base address in cr3 from 20 to 27 bits in AMD64 legacy mode (PAE)?"

答案1

得分: 1

在传统模式下,使用PAE进行32位虚拟地址的翻译需要3级,而不是传统的2级,因为PAE每级只翻译9位,使用512x 64位PTEs,而不是使用1024x 32位PTEs填充4K页面,这种情况下需要2级PAE进行翻译,它会翻译12 + 9 + 9 = 30位,意味着只需要通过顶层进行额外的2位虚拟地址位的翻译,而CR3指向的是PDPEs表,这个顶层表只需要4个条目,因此只需要32字节,而不是整个4096字节的页面。

因此,在传统模式下的PAE中,CR3指向一个32字节(自然对齐)的PDPEs迷你表。这些表不需要从4K页面的开头开始,因此它们需要更多的低端地址位以获得更精细的粒度。

log2(32) = 5,而32-5 = 27位用于寻址低32位中的任何32字节物理内存块。

(PDPEs需要位于物理地址空间的低4GiB,以便CR3可以指向它们。PDEs和PTEs可以位于任何位置,因为它们分别只被8字节的PDPEs和PDEs指向)。

英文:

In legacy mode with PAE, translating 32-bit virtual addresses takes 3 levels instead of the traditional 2, since PAE translates only 9 bits per level with 512x 64-bit PTEs, instead of 10 with 1024x 32-bit PTEs filling a 4K page.

Two levels of PAE translates 12 + 9 + 9 = 30 bits, meaning only 2 more virtual-address bits need to be translated by the top level, the table of PDPEs that CR3 points to. This top-level table only needs 4 entries, thus 32 bytes, not a full 4096 B page.

So in legacy-mode PAE, CR3 points to a 32-byte (naturally-aligned) mini table of PDPEs. These don't have to start at the beginning of a 4K page, so they need more address bits at the low end for finer granularity.

log2(32) = 5, and 32-5 = 27 bits to address any 32-byte chunk of phys mem in the low 32.

(The PDPEs need to be the low 4GiB of physical address space so CR3 can point to them. PDEs and PTEs can be anywhere because they're only pointed-to by 8-byte PDPEs and PDEs respectively).

huangapple
  • 本文由 发表于 2023年6月1日 17:28:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定