Week 10.a CS6640 11/07 2023 https://naizhengtan.github.io/23fall/ 1. RV32 page table intro 2. (manually) walking page table 3. OS implementation - trapping to kernel - message between - free all pages --- Admin: - final oral exam - hacking day on Thursday - midterm next week 1. Intro [show overview fig] * a toy example: VA: 0x80001fec * split it into L1/L2 index and offset: 0x80001fec => - l1: 1000 0000 00 => 0x200 => 2*16*16=512 - l2: 0x1 => 1 - offset: 0xfec detail info: (1) satp [handout] (2) PTE [handout] Q: what if a PTE has W but not R? [undefined behavior] (3) VA [handout] 2. walking page table * a real example take a look at helloworld.c int main(int args, char **argv) { m_uint32 data = 0xdeadbeef; printf("%p\n", &data); asm("ecall"); } * run it; it returns 0x80001fec // error Q: what do you think gdb> p/x *0x80001fec [A: 0] Q: Why? We're in M-mode. Only see physical memory. * simulate CPU: manual page walk Goal: see which physical address holds data "0xdeadbeef". Method: we will use gdb to simulate page walk. Steps: (1) split the VA to l1/l2 indexes and offest (2) get the root of page table (3) calc the L1 page (4) calc the L2 page (5) calc the physical address * now, let's do it: (1) split the va to l1/l2 indexes and offest 0x80001fec => l1: 0x200 (512) l2: 0x1 offset: 0xfec (2) get the root of page table gdb> p/x $satp // 0x80080048 (3) find the L1 page: Q: how to interpret 0x80080048? [the most significant bit is MODE] Q: what is the L1 page? gdb> p/x 0x80048 << 12 // 0x80048000 Q: is this physial or virtual? [physical] Q: what is the L1 PTE address? gdb> p/x (0x80048 << 12) + 0x200*4 // 0x200 is the L1 index in VA Q: what is the L1 PTE content? gdb> p/x *((0x80048 << 12) + 0x200*4) // get 0x20014401 (L1 PTE) Q: what does "0x1" in the end mean? [it is the valid bit] (4) find the L2 page: Q: what's the L2 page? gdb> p/x (0x2001401 & ~(0x3ff)) >> 10 << 12 // 0x80051000 Q: what's the L2 PTE address? gdb> p/x 0x80051000 + 0x1*4 // 0x1 is the L2 index in VA Q: what's the L2 PTE content? gdb> p/x *(0x80051000 + 0x1*4) // 0x200158df (5) find the physical address Q: what is "0xdf" in 0x200158df? [check out PTE fig] Q: what is the data page? gdb> p/x (0x200158df & ~(0x3ff)) << 2 // 0x80056000 Q: what is the PA? gdb> p/x ((0x200158df & ~(0x3ff)) << 2) + 0xfec // 0x80056fec // you will see 0xdeadbeef (the data) when inferencing the address 3. OS implementation A. trap to kernel setup: gdb> add-symbol-file build/release/helloworld.elf gdb> display proc_curr_idx gdb> display/x $satp gdb> display/x $sp gdb> display/x $pc Q: what do you think of "satp", "sp", "pc" when trapping? - "satp" and "sp" will not change - "pc" points to the "mtvec" - the virtual address space will be shut down (running in M-mode) B. message passing Q: how to copy message across processes? C. free page table Q: how to free all pages of a process?