OS secondary page table

Learning the secondary page table is always unclear about the details of the logical address structure and the content of physical memory storage. This article is my own little confusion and answer.

insert image description here

Preconditions:

The page size is 4KB, the page table entry is 4B, the logical address is 32 bits, and it is addressed by byte.

  1. For a page table entry of 4B size, it can represent 2 32 2^{32}232 page numbers.
  2. In the second-level page table, the top-level page table only occupies a maximum of one page, which is 4KB in this question.
    a. The page occupied by this top-level page table can accommodate up to 4KB/4B=1024 page table entries.
  3. Therefore, in the logical address space structure, 10 bits need to be used to represent the page number (offset in the table) of the first-level page table, which is used for the page addressing of the page occupied by the top-level page table (used to find the corresponding page table entry, which is the corresponding secondary page table). (Why not 12bit, because the pages occupied by this top-level page table are not stored as 1 byte and 1 byte, but are stored as 4 bytes and 4 bytes, and there are a total of 1024 page table entries)
  4. For the secondary page number, it is used to implement the function of the page table - to find the physical block number corresponding to the logical address (also equivalent to the page frame number)
  5. In-page offset: Because it is addressed by bytes, a page has 4K bytes, and 12 bits are needed to represent so many bytes, so 12 bits are occupied.
  6. Well, just right, in the logical address structure, the length of the secondary page number can also be determined, which is 32-10-12=10

Replenish:
insert image description here

So how to convert from logical address to physical address?

Conditions: 32-bit logical address space, page size 4KB, page table entry size 4B, byte as addressing unit
page size is 4KB, page offset address is log24K= 12 bits, page number part is 20 bits, if not Using hierarchical page tables, only one page table will occupy 20x4B/4KB=1024 pages=4MB.
The page table entry is only used to store the physical block number corresponding to the page number, so 4B just corresponds to this 32-bit logical space, and 32 bits need to be used to store the 32-bit address space

Input: 32-bit virtual address 0x01234567

Output: 32-bit actual physical address

Second-level page table address splitting rules: the upper 10 bits are the page directory (level 2) index, and the middle 10 bits are the (level 1) page table index. The last 12 lower bits are the offset within the physical page (actual physical address).

For example: input the virtual address 0x01234567 to split it into binary, we know that the relationship between hexadecimal and binary is 1 to 4 bits, that is, 0x0 = 0000, 0x1 = 0001, 0x2 = 0010,

So split to get
0000 0001 00 = 2 D
10 0011 0100 = 564 D
0101 0110 0111 = 567 H

In this way, the index value of the page directory (secondary page table) is 4, the index value of the page table (first-level page table) is 564, and the physical offset is 567H.

Step 1: Find the page table (level 1) address in the page directory (level 2), that is, 4*4=16, which means that the page table address is 16 bytes away from the head address of the page directory. Fetch the page table (level 1) address from here.

The second step: go to the page table (level 1) to find the actual physical page address. The offset is 564*4=2256=0x8d0, plus the first address of the page table found in the first step. Finally, the actual physical page address is obtained.

Step 3: Add the physical page address to the lower 12 offset of the original virtual address to obtain the final actual physical address. This address is the final output result, use it to access the memory to fetch data.
insert image description here

Guess you like

Origin blog.csdn.net/weixin_45827203/article/details/126284702