Linux分页:linux如何折叠Page上层目录和Page中层目录?

Linux paging: How does linux fold Page Upper Directory and Page Middle Directory?

我正在阅读“了解 Linux 内核”一书,我想知道 Linux 如何在 3 级或 2 级分页中折叠页面上层目录或页面中层目录系统.

根据我的理解:

  1. Linux有4级分页:Page Global Directory, Page Upper Directory, Page Middle Directory and Page Table.
  2. 一旦页面table建立后,硬件(Paging Unit)通过分析线性地址中的相应字段并查询相应的页面table条目来自动定位页面框架。
  3. 对于 2 级分页系统,Page Upper Directory 和 Page Middle Directory 在 Page Global Directory 中各有一个特定条目。

这对我来说似乎是矛盾的:如果上述 3 个假设成立,则意味着地址转换将通过这些条目进行:

Page Global Directory Entry -> (Singleton) Page Upper Directory Entry -> (Singleton) Page Middle Directory Entry -> Page Table Entry

但是,为什么同一个Page Middle Directory Entry可以指向不同的Page Table Entries?

我不太记得这本书了。我想他们的意思是页面上层目录和页面中层目录没有被使用,页面全局目录是选择页面table的table。你的其他假设似乎是正确的。假设您在 32 位模式下有虚拟地址 0x12345678。 0x678 是物理页中的偏移量(12 位)。中间的 10 位是页面 Table 中的偏移量,最高 10 位是页面全局目录中的偏移量。

For 32-bit architectures with no Physical Address Extension, two paging levels are sufficient. Linux essentially eliminates the Page Upper Directory and the Page Middle Directory fields by saying that they contain zero bits. However, the positions of the Page Upper Directory and the Page Middle Directory in the sequence of pointers are kept so that the same code can work on 32-bit and 64-bit architectures. The kernel keeps a position for the Page Upper Directory and the Page Middle Directory by setting the number of entries in them to 1 and mapping these two entries into the proper entry of the Page Global Directory.

作者这里的意思是中间的2关被淘汰了。更改 table 时(交换进程时)仅维护指针序列。这是为了在32位或64位上保持相同的代码CPUs.

你不明白的地方是要使用32位模式,CPU需要处于32位模式。如果您使用 32 位 Linux,Linux 将不会以 64 位模式启动处理器。因此 CPU 将处于 32 位模式和 64 位模式(长模式)之间的“过渡”状态。这是由 Intel 和 AMD 完成的,以便它们的 CPU 保持向后兼容性。因此,您可以将 CPU 启动到 32 位模式,并决定不切换到 64 位模式。这是通过将某些位设置到处理器的控制寄存器中来完成的。当处理器处于 32 位模式时,它仅使用 2 级页面 table 而不是 4.

来转换虚拟地址