Lecture: Virtual memory
Preparation
- read the xv6 book: §2, Page tables
Questions
- Exercise: system calls
- “RFC 3339 format, 2016-9-30T0:23:17Z” - not strictly correct
- “Oct 6 1:57:11 UTC 2016”
- “the day of week was not directly provided in rtcdate” - see “Appendix B. Day of the Week” in RFC 3339
- CMOS
- network time
- can we directly call
cmostime
from date.c
? if not, what will prevent us from doing that?
- Lab 2
- see the tools guide
- enter/exit QEMU monitor using
Ctrl-a c
; try info pg
Paging
- pointers: virtual addresses
- kernel: set up a page table
- MMU: VA → PA translation & permission checking
- TLB: cache translation results
- now you understand what happens when you dereference a pointer,
access an object field, etc.
- basis for process isolation - see xv6
- per-process page table:
pgdir
field in struct proc
in proc.h
- switch:
switchuvm
in vm.c
- x86 page table example: 4KB page size, two-level tree
- see the xv6 book, Figure 2-1, x86 page table hardware
- top-level: page directory
- 1024 entries (PDE)
- each PDE is 32-bit: address of page table page (20 bits) & flags (12 bits)
- second-level: page table pages
- each has 1024 entries (PTE)
- each PTE is 32-bit: physical page address (20 bits) & flags (12 bits)
- questions
- would the physical RAM size affect the size of the page directory
- how would you implement our QEMU’s info pg in JOS kernel monitor
- x86 programming interface
- control registers
- CR0: enable/disable paging
- CR2: page fault address
- CR3: physical address of the page directory
invlpg
: invalidate TLB
- TLB shootdown (multiprocessor)
Demos in JOS
- invalid read/write
- add two lines of code in JOS to read an invalid address
- with unpatched QEMU: reboot
- with patched QEMU: stop & print registers
- useful for debugging
- what are the values of EIP, CR2, CR3
- read address 0 vs. read
KERNBASE
(0xf0000000
)
- the same value - why?
- how about write? what makes the difference?
- kernel runs in high virtual addresses
- why
- how does the kernel set this up