Dan Ports drkp@mit.edu 6.828 Lab 2 ------------ Exercise 3: 1. 1024 TOP ---| .. |-- Physical memory 960 0xf0000000 ---| 959 0xefc00000 VPT: page directory 958 0xef800000 Kernel stack (only top KSTKSIZE mapped) 957 0xef400000 UVPT: r/o page directory 956 0xef000000 UPAGES: r/o pages array 955 ... unmapped 0 2. We map the first entry in the page table to the first 4MB of RAM in order to account for the fact that paging is enabled before segmentation is disabled. An access to virtual address KERNBASE should be mapped to physical address 0. Before paging is enabled, it is translated by segmentation to 0. When paging is enabled, segmentation still maps it to linear address 0, so we must make sure that paging maps linear address 0 to physical address 0. Once segmentation is disabled, virtual address KERNBASE will map to linear address KERNBASE which will map to physical address 0. Since only one page directory entry (4MB) is mapped, any memory addresses accessed by the kernel before segmentation is disable must lie within the first 4MB of physical memory. If the kernel is larger than 4MB, we must map more than one page directory entry, or ensure that the code being executed lies in the first 4MB. 3. The user flag is only set in the page table entries of pages that the user process has access to, so when the user process is running (CPL=3), it cannot access the kernel memory. This is implemented on the PDP/11 by having two sets of PARs and PDRs so that user and kernel code have access to separate address spaces. Exercise 4: 1. The OS can only support 256MB of physical memory. This is the amount of memory mapped above KERNBASE, and so the amount of memory the kernel can access. One could envision mapping more than 256MB of physical memory below KERNBASE, but this would require other changes to the code, as physical addresses could no longer always be translated into kernel virtual addresses (i.e. with the KADDR macro). 2. 256 MB is 65536 physical pages. So the kernel will need 65536 * sizeof(struct Page) to keep track of whether these pages are allocated. We also require enough page tables to store the mappings for the kernel address space; each page table is 4K and maps 1024 pages, so 256K of pages tables will be required. More page tables will, of course, be required to map any of this memory for the user processes. Challenge: Figuring that any extra commands added to the kernel monitor would come in handy, I implemented the following commands: - showmappings displays the physical page mappings and flags set for all pages between virtual addresses start and end - pageperm [(kern|user) (rw|ro)] displays the permissions on the page containing virtual address va, or sets the PTE_U and PTE_W permission bits in the PTE. - dumpmem prints (in hexadecimal) the contents of all memory between virtual addresses start and end - allocpage allocate a page of physical memory and print the corresponding virtual address - freepage free the page of physical memory containing pa - pagestatus indicate whether the page of physical memory containing pa is mapped, and if so its refcount.