通过设置页表和对应的页表项,可建立虚拟内存地址和物理内存地址的对应关系。其中的get_pte函数是设置页表项环节中的一个重要步骤。此函数找到一个虚地址对应的二级页表项的内核虚地址,如果此二级页表项不存在,则分配一个包含此项的二级页表。本练习需要补全get_pte函数 in kern/mm/pmm.c,实现其功能。
// File: pmm.c//get_pte - get pte and return the kernel virtual address of this pte for la// - if the PT contians this pte didn't exist, alloc a page for PT// parameter:// pgdir: the kernel virtual base address of PDT// la: the linear address need to map// create: a logical value to decide if alloc a page for PT// return vaule: the kernel virtual address of this ptepte_t*get_pte(pde_t*pgdir,uintptr_t la,bool create) { /* LAB2 EXERCISE 2: YOUR CODE * * If you need to visit a physical address, please use KADDR() * please read pmm.h for useful macros * * Maybe you want help comment, BELOW comments can help you finish the code * * Some Useful MACROs and DEFINEs, you can use them in below implementation. * MACROs or Functions: * PDX(la) = the index of page directory entry of VIRTUAL ADDRESS la. * KADDR(pa) : takes a physical address and returns the corresponding kernel virtual address. * set_page_ref(page,1) : means the page be referenced by one time * page2pa(page): get the physical address of memory which this (struct Page *) page manages * struct Page * alloc_page() : allocation a page * memset(void *s, char c, size_t n) : sets the first n bytes of the memory area pointed by s * to the specified value c. * DEFINEs: * PTE_P 0x001 // page table/directory entry flags bit : Present * PTE_W 0x002 // page table/directory entry flags bit : Writeable * PTE_U 0x004 // page table/directory entry flags bit : User can access */#if0 pde_t *pdep = NULL; // (1) find page directory entry if (0) { // (2) check if entry is not present // (3) check if creating is needed, then alloc page for page table // CAUTION: this page is used for page table, not for common data page // (4) set page reference uintptr_t pa = 0; // (5) get linear address of page // (6) clear page content using memset // (7) set page directory entry's permission } return NULL; // (8) return page table entry#endif}
// File: mmu.h// Line: 190-201// A linear address 'la' has a three-part structure as follows://// +--------10------+-------10-------+---------12----------+// | Page Directory | Page Table | Offset within Page |// | Index | Index | |// +----------------+----------------+---------------------+// \--- PDX(la) --/ \--- PTX(la) --/ \---- PGOFF(la) ----/// \----------- PPN(la) -----------///// The PDX, PTX, PGOFF, and PPN macros decompose linear addresses as shown.// To construct a linear address la from PDX(la), PTX(la), and PGOFF(la),// use PGADDR(PDX(la), PTX(la), PGOFF(la)).