Part 2: Create an Address Space
Create an Address Space
Create a direct-map page table for kernel
There is a walk() to find PTE for a given virtual address mappages() installs PTE for new mappings
In kvminit(), first allocate a page of physical memory to hold the root page-table page. Then calls to install a bunch of things that kernel needs, including: instructions, data, physical memory range, and memory range for devices.
The install mapping mappages
works as follow: 1. For given virtual address, find the pte 2. Convert physical address to pte and set permission bits 3. Return success or not.
static pte_t * walk(pagetable_t pagetable, uint64 va, int alloc)
Returns the address of the PTE in page table pagetable that corresponds to a virtual address.
Loop from level 2 to level 0
Find the pte entry using level, and va from current page table pageable.
Find page that pte points to.
If pte not exist or page not valid, allocate a new page.
End of loop.
Return the pte entry.
Turn on Paging
Write physical address of the root page-table page into per CPU register satp. After this, CPU will begin translate addr using kernel page table. Flush the TLB to clear cache.
Create Process Table
For process p, allocate 1 pages using buddy allocator for this process’s kernel stack.
Map it high in memory with permission X | W.
Set it to be followed by an invalid guard page. This virtual page does not allocated in physical memory, it just occupies some space in process VM address space. Any access to those will trigger kernel panic.
Reloads the kernel page table into satp so that hardware knows about the new PTEs. (Including flush TLB buffer cache)
Process has its own Page Table.
Different than the kernel one. The root page-table page needs to set in satp register when CPU is currently running this process. Process struct saves kernel root page-table address, so allows to switch to/from kernel.
Last updated