remzi-arpacidusseau / ostep-homework

2.1k stars 999 forks source link

Does PFN in vm-paging is fault? #49

Closed clamyang closed 1 year ago

clamyang commented 2 years ago

From paging-linear-translate.py i got a Page Table as follows:

Page Table (from entry 0 down to the max size)
  [       0]  0x8000000c
  [       1]  0x00000000
  [       2]  0x00000000
  [       3]  0x80000006

After calculate the VPN is 3, the PFN is 0x80000006. But in README.md the PFN is 0x6. Does it wrong?

tessapower commented 1 year ago

@clamyang when you run the program, you'll see a section above the Page Table which says:

The format of the page table is simple: The high-order (left-most) bit is the VALID bit. ⭐ If the bit is 1, the rest of the entry is the PFN. If the bit is 0, the page is not valid.

This ultimately means that a page table entry contains two pieces of information - whether it is valid and then also the Physical Frame Number (PFN). Pay attention to the line above with the ⭐, it states that if the Most Significant Bit (MSB) is 1, then the rest of the number, excluding that bit, represents the PFN.

The example in the README 0x80000006 is represented in hexadecimal, and if we wrote this out in binary it would be 1000 0000 0000 0000 0000 0000 0000 0110, which clearly shows that the MSB is 1 and therefore this page entry is valid.

To get the PFN, we ignore the MSB and look only at the rest of the number to get the PFN, which is 000 0000 0000 0000 0000 0000 0000 0110. Since it is binary, we can safely ignore leading zeroes and write it more compactly as 0110, or 0x6 in hexadecimal.

Hope that helps! 😸