Lab link: https://pdos.csail.mit.edu/6.828/2018/labs/lab2/
Memory management has two components:
Some critical variables in JOS:
struct PageInfo *pages; // whole physical memory
struct PageInfo *page_free_list; // free physical memory
pde_t *kern_pgdir; // whole virtual memory space
1 PageInfo struct represents 1 physical page, 1 pte represents 1 page.
given 1 va, you can determine whether it has been mapped by looking at whether the corresponding pte present or not.
The operating system keep track of which parts of physical RAM are free and which are currently in use. JOS manages the PC‘s physical memory with page granularity.
This part implememts the physical page allocator. It keeps track of which pages are free with a linked list of struct PageInfo objects, each corresponding to a physical page.
// If n>0, allocates enough pages of CONTIGUOUS physical memory to hold ‘n‘ bytes.
// This simple physical memory allocator is used only while JOS is setting up its virtual memory system. page_alloc() is the real allocator. So this function is
// static.
// This function may ONLY be used during initialization, before the **page_free_list** list has been set up.
static void * boot_alloc(uint32_t n)
// Initialize page structure and memory free list. After this is done, boot_alloc() will NEVER be used again.
void page_init(void)
// Allocates a physical page. Does NOT increment the reference count of the page.
struct PageInfo * page_alloc(int alloc_flags)
// Return a page to the free list.
void page_free(struct PageInfo *pp)
Address translation scheme:
// returns a pointer to the PTE for linear address ‘va‘.
pte_t * pgdir_walk(pde_t *pgdir, const void *va, int create)
// Map [va, va+size) of virtual address space to physical [pa, pa+size) in the page table rooted at pgdir.
static void boot_map_region(pde_t *pgdir, uintptr_t va, size_t size, physaddr_t pa, int perm)
// Unmaps the physical page at virtual address ‘va‘.
void page_remove(pde_t *pgdir, void *va)
// Map the physical page ‘pp‘ at virtual address ‘va‘.
int page_insert(pde_t *pgdir, struct PageInfo *pp, void *va, int perm)
[OS][MIT 6.828] Lab 2: Memory Management
原文:https://www.cnblogs.com/-zyq/p/13417424.html