Review session 6 Outline 1. Page table walk (10 min) 2. Page permissions (5 min) 3. Lab5 overview (30 min) 1. Page table walk (10 min) The goal of page table walking is to translate a 32-bit virtual address to a 32-bit physical address which can be addressed on main memory. Virtual address layout: [page directory index][page table index][offset] 10 bits 10 bits 12bits 0x0 -> 0000 0x1 -> 0001 0x2 -> 0010 0x3 -> 0011 0x4 -> 0100 0x5 -> 0101 0x6 -> 0110 0x7 -> 0111 0x8 -> 1000 0x9 -> 1001 0xa -> 1010 0xb -> 1011 0xc -> 1100 0xd -> 1101 0xe -> 1110 0xf -> 1111 Use 0x80000ffc to demonstrate how to split the virtual address to three parts. Let student do exercise 1 and fill the blank. [Handout Exercise 1. 0x80fcf0f0: 1000000011|1111001111|000011110000 -> [515][975][240] 0x802ff000: __________|__________|____________ -> [____][____][_____] ] A page table is the data structure used by a virtual memory system in a computer operating system to store the mapping between virtual addresses and physical addresses. Demonstrate the page walk on board using 0x802ffffc as an example. [Handout Exercise 2. +--------------------+ +------------+ | +------------+ | +------------+ | 0xf0003007 |--+ | 0xf0040007 | | | 0xf0009007 | (entry: 1023) +------------+ +------------+ | +------------+ | ... | | ... | | | ... | +------------+ +------------+ | +------------+ | 0xf0002007 |--+ | 0xf0005007 | | | 0xf000a007 | (entry: 512) +------------+ | +------------+ | +------------+ | ... | | | ... | | | ... | +------------+ | +------------+ | +------------+ | 0xc5202000 | | | 0xf0006007 | | | 0xf000b000 | (entry: 0) +-> +------------+ +--> +------------+ +--> +------------+ | 0xf0001000 0xf0002000 0xf0003000 | | +------------+ +--| 0xf0001000 | CR3 +------------+ Q: What's the physical addresses after translation? 0xffd00ffc: 1111111111|1000000000|111111111100 -> [1023][512][4092] -> 0xf000affc 0x803ff000: __________|__________|____________ -> [____][____][____] -> __________ ] Answer: 0x803ff000: 1000000000|1111111111|000000000000 -> [512][1023][0] -> 0xf0040000 [draw on board the pages in a tree-like way from cr3 to data page] Use 0x802ffffc as an example: (1) Split the virtual address into three parts (10:10:12 bit). (2) From cr3, follow the virtual address and find the data page. (3) Use last 12 bits as index to find the real data. 2. Page permissions (5 min) There are two level page tables for 80386. Page directory page and page table page. Their permissions are almost the same (refer the manual for minor difference). Important ones (related to Lab5) are: PTE_U: 3rd bit user PTE_W: 2nd bit writable PTE_P: 1st bit present [Handout Exercise 3. Q: What's the the meaning of following permissions? (1) PTE_P|PTE_U|PTE_W: (2) PTE_P|PTE_U: (3) PTE_W|PTE_U: ] Answers: (1) User can read/write (2) User readonly (3) There will be a page fault 3. Lab5 overview (30 min) - Introduction (5 min) Intorduce the goal of lab5 and the meaning of the figure. Show figure on screen. .: empty page R: reserved page K: kernel page number: process id reverse video: have the write permission - Code overview (10 min) Go through the kernel architecture, and brief introduce the functionality of the functions in lab5's exercises. [kernel():kernel.c] initialize virtual memory create processes [process_setup():kernel.c] initialize process's virtual memory (exercise 1) [exception():kernel.c] sys_page_alloc (exercise 3) fork (exercise 5) - Utility functions (5 min) debug functions: (short demo) debug_printf() log_printf() [Handout Exercise 4. print on screen: __________ print on log file: __________ ] - Important data structure (10 min) pageinfo array (5min) and page table (5 min) [Handout Exercise 5. How do kernel memorize which pages are available? [kernel.c] typedef struct physical_pageinfo { int8_t owner; int8_t refcount; } physical_pageinfo; static physical_pageinfo pageinfo[PAGENUMBER(MEMSIZE_PHYSICAL)]; ] Use an array to represent the physical memory pages. For each node in array, "owner" is the page's owner (kernel,reserve,proc). Refcount represents how many virtual addresses mapped to this physical page. [Handout Exercise 6. What's the page table's data structure? [x86.h] typedef uint32_t x86_pageentry_t; typedef struct __attribute__((aligned(PAGESIZE))) x86_pagetable { x86_pageentry_t entry[PAGETABLE_NENTRIES]; } x86_pagetable; ] This the data structure of page table pages. [draw on board a page table page] Page table page has 1024 entries. Each entry is 4 bytes. The layout of the entry is like this: physical_page_number(20 bits) + permissions(12 bits)