XV6 Virtual Memory
Last updated
Last updated
Paging provides a level of indirection for addressing
CPU -> MMU -> RAM
VA PA
use index bits of VA to find a page table entry (PTE) construct physical address using PPN from PTE + offset of VA
one page table per address space
RISC-V maps 4-KB โpagesโ and aligned โ start on 4 KB boundaries 4 KB = 12 bits the RISC-V used in xv6 has 64-bit for addresses thus page table index is top 64-12 = 52 bits of VA except that the top 25 of the top 52 are unused no RISC-V has that much memory now can grow in future so, index is 27 bits.
each PTE is 64 bits, but only 54 are used top 44 bits of PTE are top bits of physical address โphysical page numberโ low 10 bits of PTE flags Present, Writeable, &c
in RAM
No. Waste lots of memory for small program. 2^27 entry * 64 bits = 1 GB per page table. If one address space per app, too much wasting!
RISC-V 64 uses a โthree-level page tableโ to save space Each page directory page (PD) has 512 PTEs. PTEs point to another PD or is a leaf so 512512512 PTEs in total. PD entries can be invalid, and PTE pages do not exist. So a page table for a small app is small.
Register satp
holds the physical address of the root page table address. OS saves and rewrite satp when switching to another address space/application.
Page fault. Transfer to kernel. Kernel could output error, kill process, or install a PTE and resume. See: how to kill process, how to lazy allocate, how to copy on write.
Each process has its own address space, and its own page table. Kernel switches page tables when switching processes.
trampoline
and trapframe
arenโt writable by user process. both kernel and user map trampoline
and trapframe
page.
Two good reasons:
1. eases transition user -> kernel and back
2. kernel doesnโt map user applications
Not easy for kernel to r/w user memory. Need translate user virtual address to kernel virtual address But good for isolation (see spectre attacks)
shift a physical address to the right place for a PTE. #define PA2PTE(pa) ((((uint64)pa) >> 12) << 10)
Create PTEs for virtual addresses starting at va that refer to physical addresses starting at pa. va and size might not be page-aligned.
PX
changes va to the current index based on level. & 0x111111111
to set rest to 0s. The loop checks PTE entry, if it is valid, transform the PTE entry to physical address, and assign to pageable
array. If not valid, alloca new page table, convert the physical address to PTE format, and assign to PTEโs content. Finally, returns the PTE address of level 0 for given VA.
The purpose of virtual memory is isolation.
Each process has its own address space.
Lazy/on-demand page allocation.
Guard page to protect against stack overflow
one zero-filled page
Share kernel page tables in XV6
Copy-on-write fork
Demand paging: on page fault, read the page from file and update page table entry.
Memory-mapped files: Can read and write part of file. Can pay-in pages on demand, and page-out if memory is full. (Code it up!)
exec
nows loads entire file to memory, files reading is slow, and some parts are never used. The solution is to use demand paging (Code it up!)