Required reading: Chapter 3 (Protected-Mode Memory)
Handout: x86 translation diagram
PC block diagram
Translation
Segmentation
The pseudo-code is in some sense not faithful to the actions of the MMU. For example, the hardware doesn't really do shifts; it is just a question of running wires differently.
void
access (uint x, bool kernel, bool write)
{
if (!(x & PG_P)
=> page fault -- page not present
if (!(x & PG_U) && user)
=> page fault -- not access for user
if (write && !(x & PG_W))
if (user)
=> page fault -- not writable
else if (!(x & PG_U))
=> page fault -- not writable
else if (%CR0 & CR0_WP)
=> page fault -- not writable
}
// maps va => pa
uint
translate (uint va, bool kernel, bool write)
{
uint pde;
pde = read_mem (%CR3 + 4*(va >> 22));
access (pde, kernel, read);
pte = read_mem ( (pde & ~0xfff) + 4*((va >> 12) & 0x3ff));
access (pte, kernel, read);
return (pte & ~0xfff) + (va & 0xfff);
}
Virtual Physical Address Address Space Space +------+ +------+ |2^20-1| /----------------> |2^20-1| +------+ / +------+ |2^20-2|\ / |2^20-2| +------+ \/ +------+ |2^20-3| / \ |2^20-3| +------+ \ +------+ . \ . . \ . . \ . +------+ \ +------+ | 2 | \--> | 2 | +------+ +------+ |vpn=1 | -----------------------------> |ppn=1 | +------+ +------+ |vpn=0 | |ppn=0 | +------+ +------+
arbitrary mapping from VPN to PPN (pg offset is untouched).
VPN = virtual page number
PPN = physical page number