Lecture: virtual memory applications
Preparation
- read OSPP §9, Caching and Virtual Memory and §10, Applications of Memory Management
- take a look at the lazy allocation exercise
Lab 2 questions
- make sure you understand exercise 8, lab 1
- it’s easy to “understand” the high-level concepts; implementing them is another story
- go to this week’s sections, office hours, or schedule extra hours
Virtual memory recap
- CPU asks OS to set up a data structure for VA → PA
- isolation: each process has its own address space
- per-process page table; flags (P/W/U/…)
- switch page table with process
- JOS:
inc/memlayout.h
- xv6
struct proc
in proc.h
scheduler()
→ switchuvm(p)
→ lcr3(v2p(p->pgdir))
- Linux
cat /proc/iomem
cat /proc/self/maps
(or replace self
with a PID)
- are these physical or virtual addresses
- re-read chapter 2 of the xv6 book
- what’s the initial setup in
entrypgdir
?
- focus on
kinit1()
and kvmalloc()
in main()
, main.c
kinit1()
- how does xv6 alloc/free physical memory
- read
kalloc()
and kfree()
in kalloc.c
- how does the free list work
kvmalloc()
- what does
setupkvm()
do
- are the permissions
PTE_P | PTE_W | PTE_U
set by walkpgdir()
correct
Examples: protection, virtualization, lazy allocation
- implement null pointer dereference exception
- how would you implement this for Java, say
obj->field
- trick: put a non-mapped page at VA zero
- useful for catching program bugs
- limitations?
- protect against stack overflow
- see Michael Barr’s Bookout v. Toyota, “Toyota’s major stack mistakes”
- trick: put a non-mapped page right below user stack
- JOS:
inc/memlayout.h
- limited physical memory
- applications need more memory than physical memory
- early days: two floppy drives
- strawman: applications store part of state to disk and load back later
- hard to write applications
- virtual memory: offer the illusion of a large, continuous memory
- swap space: OS pages out some pages to disk transparently
- distributed shared memory: access other machines’ memory across network
- memory-mapped files
mmap()
: map files, read/write files like memory
- simple programming interface
- when to page-in/page-out content?
- avoid data copying: send an mmaped file to network
- compare to using
read
/write
- no data transfer from kernel to user
- copy-on-write fork
- strawman fork: copy all pages from parent to child
- observation: child and parent share most of the data
- mark pages as copy-on-write
- make a copy on page fault
- lab 4: you will implement user-level copy-on-write fork
- other sharing
- multiple guest OSes running inside the same hypervisor
- shared objects:
.so
/.dll
files
- virtual linear page tables:
uvpt[n]
gives the PTE of page n
- self mapping: set one PDE to point to the page directory
- CPU walks the tree as usual, but ends up in one level up
- see UVPT for detail
- grow stack on demand: see the next in-class exercise