Memory Management Walk Through
Page Tables
Page tables determine what memory address mean, what part of physical memory can be accessed.
A page table gives the operating system control over virtual to-physical address translations at the granularity of aligned chunks of 4096 (2^12) bytes.
pagePage TableCreating kernel address space
See process allocation section.
Allocate a kernel stack for each process
procinit
(kernel/proc.c:24), which is called from main
, allocates a kernel stack for each process. It maps each stack at the virtual address generated by KSTACK, which leaves room for the invalid stack-guard pages. kvmmap
adds the mapping PTEs to the kernel page table, and the call to kvminithart
reloads the kernel page table into satp
so that the hardware knows about the new PTEs.
Diagram of kernel stack address space
Stack grows downwards.
TLB cache
Each RISC-V core caches page table entries in a Translation Look-aside Buffer (TLB), and when xv6 changes a page table, it must tell the CPU to invalidate corresponding cached TLB entries.
The RISC-V has an instruction sfence.vma
that flushes the current core’s TLB. xv6 executes sfence.vma
in kvminithart
after reloading the satp
register, and in the trampoline code that switches to a user page table before returning to user space (kernel/trampoline.S:79).
Physical Memory Allocation
The allocator sometimes treats addresses as integers in order to perform arithmetic on them (e.g., traversing all pages in freerange
), and sometimes uses addresses as pointers to read and write memory (e.g., manipulating the run
structure stored in each page).
Process Address Space
TODO
How exec
works
pageHow exec() worksexec
worksFind
inode
by pathCheck ELF header
Load program by memory.
uvmalloc
to allocate new size.loadseg
load file to memory address atph.vaddr
. Note: Exec loads bytes from the ELF file into memory at addresses specified by the ELF file.Allocate 2 pages. 2nd one is user stack.
Push args to stack. Prepare
ustack
to save each argument with its address.Push the array of argv[] pointers (
ustack
) to stack.Set
sp
toa1
.Commit to user image. Free old process’ page table.
Set
epc
,sp
,sz
.return argc
which setargc
in rega0
. Now we havemain(args, args)
fromentry, a0, a1
.
How stack look after pushing args?
Find PA from VA
This code is to find a physical address from virtual address. The PA from *pte
is at start of page. We also need to add offset to it when return.
Exercises
Explain how the following functions work:
Last updated