pte_t uvpt[] in JOS, revisited

To access data on x86, the CPU will go through page directory and page table. Sort of like this:

def load(addr):
pt = pd[(addr & M1) >> S1]
pg = pt[(addr & M2) >> S2]
v = pg[addr & M3]
return v

uvpt is defined as a constant. JOS did some trick with it:

// UVPT maps the env's own page table read-only.
// Permissions: kernel R, user R
e->env_pgdir[PDX(UVPT)] = PADDR(e->env_pgdir) | PTE_P | PTE_U;

Basically, it means:

pd[(uvpt & M1) >> S1] = pd

That changes the behavior of accessing data within the page table at uvpt when paging is enabled; it then works this way:

# because here pd == pd[(addr & M1) >> S1],
# now the original load() code is equivalent to:
def load(addr):
pt = pd[(addr & M2) >> S2]
pg = pt[addr & M3]
return pg

Remember that each pte_t (page table entry) corresponds to a page. That explains how uvpt works.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.