Page Table Translation

medium · os, memory, paging

Page Table Translation

Simulate a 2-level page table translation for a 32-bit virtual address.

Function

func Translate(va uint32, pd []int, pts [][]int) (uint32, bool)

Address format

  • Page size: 4096 bytes
  • VA layout: [10 bits dir][10 bits table][12 bits offset]

Table format

  • pd length is 1024.
  • pd[i] is the index of the page table in pts, or -1 if not present.
  • Each page table in pts has length 1024.
  • A page table entry is a frame number, or -1 if not present.
  • Present frame numbers are in 0..2^20-1, so the physical address fits in uint32. Frame zero is valid.

Behavior

  • If any level is not present, return (0, false).
  • Otherwise, return physical address frame*4096 + offset and true.
  • Do not modify either table. A directory entry is an index into pts, not a physical frame number.

Worked example

For va=0x00c0502d, extract directory index 3, table index 5, and offset 45. If pd[3]=1 and pts[1][5]=12, return (49197,true) because 12*4096+45=49197.

If pd[3] or pts[1][5] is -1, return (0,false). Mapping frame zero at offset zero instead returns (0,true): do not infer presence from the numeric address.

Run tests to see results
No issues detected
    Join Discord