Warm tip: This article is reproduced from serverfault.com, please click

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

发布于 2020-11-28 19:32:05

I'm reading the book "Understanding the Linux Kernel" and I'm wondering how Linux folds the Page Upper Directory or Page Middle directory in a 3- or 2-level paging system.

Based on my understanding:

  1. Linux has 4 levels of paging: Page Global Directory, Page Upper Directory, Page Middle Directory and Page Table.
  2. The hardware (Paging Unit) automatically locates the page frame once the page tables are set up, by analyzing the corresponding fields in linear address and querying the corresponding page table entries.
  3. For a 2-level paging system, Page Upper Directory and Page Middle Directory each gets a single specific entry in Page Global Directory.

This seems contradictory to me: if the 3 assumptions above are true, it means that the address translation will be conducted through these entries:

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

However, how come that the same Page Middle Directory Entry can point to different Page Table Entries?

Questioner
Changda Li
Viewed
0
user123 2020-11-29 23:32:24

I don't remember the book very well. I think what they mean is that the Page Upper Directory and Page Middle Directory are not used and that Page Global Directory is the table which selects the page table. The rest of your assumptions seem correct. Let's say you have virtual address 0x12345678 in 32 bits mode. 0x678 is the offset in the physical page (12 bits). The 10 bits in the middle are the offset in Page Table and the 10 most significant bits are the offset in Page Global Directory.

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.

What the author means here is that the 2 levels in the middle are eliminated. Only the sequence of pointers is maintained when changing the tables (when swapping the process). This is to maintain the same code on 32 bits or 64 bits CPUs.

Where you don't understand is that to use 32 bits mode, the CPU needs to be in 32 bits mode. If you use 32 bits Linux, Linux won't boot the processor in 64 bits mode. The CPU will thus be in a "transition" state between 32 bits mode and 64 bits mode (long mode). This was done by Intel and AMD so that their CPUs maintain backward compatibility. You can thus boot the CPU into 32 bits mode and decide not to switch in 64 bits mode. This is done by setting certain bits into control registers of the processor. When the processor is in 32 bits mode, it translates virtual addresses using only 2 levels of page tables instead of 4.