Page Translation and Protection Faults
Page Translation and Protection Faults
You are given a simplified page table and a list of memory accesses. Translate each virtual address into a physical address or report why the access cannot proceed.
This problem is the foundation for the rest of the module. Do not rush it. TLBs, page faults, and copy-on-write all depend on this distinction:
translation = VPN -> frame + same offset
permission = is this kind of access allowed?
Types
type PTE struct {
Frame int
Present bool
Read bool
Write bool
Exec bool
}
type Access struct {
VAddr int
Mode byte // 'r', 'w', or 'x'
}
type Result struct {
PAddr int
Fault string // "", "page", or "protection"
}
func Translate(pageSize int, table map[int]PTE, accesses []Access) []Result
Rules
For each access:
- Compute:
vpn = VAddr / pageSize
offset = VAddr % pageSize
- If
VAddr < 0, return a page fault. - If the page table has no entry for
vpn, return a page fault. - If the entry exists but
Present == false, return a page fault. - Check permissions:
- mode
'r'requiresRead == true - mode
'w'requiresWrite == true - mode
'x'requiresExec == true
- mode
- If permission fails, return a protection fault.
- Otherwise return:
PAddr = PTE.Frame * pageSize + offset
Fault = ""
For faults, use:
Result{PAddr: -1, Fault: "page"}
Result{PAddr: -1, Fault: "protection"}
Assume pageSize > 0.
Example
pageSize := 100
table := map[int]PTE{
0: {Frame: 5, Present: true, Read: true, Write: false},
1: {Frame: 9, Present: true, Read: true, Write: true},
}
accesses := []Access{
{VAddr: 12, Mode: 'r'}, // vpn=0 offset=12 -> 512
{VAddr: 12, Mode: 'w'}, // mapped but not writable
{VAddr: 250, Mode: 'r'}, // vpn=2 missing
}
Output:
[]Result{
{PAddr: 512, Fault: ""},
{PAddr: -1, Fault: "protection"},
{PAddr: -1, Fault: "page"},
}
Walkthrough ladder
Invariant
The offset is preserved. Only the page/frame number changes.
State design
You do not need extra state. The map is your page table.
Transition
Each access is independent:
split address -> find PTE -> check present -> check permission -> compute physical address
Common bug
Do not report a missing page as a protection fault. Permission is only meaningful after a present mapping exists.
Run tests to see results
No issues detected