Caching Techniques for Rapid Provisioning of Virtual Servers
Virtual Memory - University of Washington · 2009. 5. 27. · » Virtual memory enables using...
Transcript of Virtual Memory - University of Washington · 2009. 5. 27. · » Virtual memory enables using...
Virtual Memory
CSE 410 Spring 2009CSE 410, Spring 2009Computer Systems
http://www.cs.washington.edu/410
5/26/2009 cse410-14-virtual-memory © 2006-09, Perkins, DW Johnson and University of Washington 1
Reading and Referencesg
• Reading • Computer Organization and Design, Patterson and Hennessy
» Section 5.4 Virtual Memory» Section 5.5 A Common Framework for Memory Hierarchies
5/26/2009 cse410-14-virtual-memory © 2006-09, Perkins, DW Johnson and University of Washington 2
Memory Management Goalsy g
We want to share main memory such that:y• Each process thinks it has a private memory of
2-4 GB (or more), even if it doesn’t use it all2 4 GB (or more), even if it doesn t use it all• Real memory is allocated efficiently to parts of
process memory actually being used (locality)process memory actually being used (locality)• No process can interfere with or even see
b l i t thmemory belonging to another» Unless we want that to happen
5/26/2009 cse410-14-virtual-memory © 2006-09, Perkins, DW Johnson and University of Washington 3
Layout of program memoryy p g y
7FFF EFFF stack (grows down)
reserved (4KB)7FFF FFFF
stack (grows down)
~1792 MB
1000 FFFF
Not toScale!heap (grows up)1001 0000
0FFF FFFF1000 00001000 FFFF
program (252 MB)
global data (64 KB)
reserved (4 MB)0000 0000
003F FFFF
0040 0000
5/26/2009 cse410-14-virtual-memory © 2006-09, Perkins, DW Johnson and University of Washington 4
The Big Ideag
• Separate program notion of memory addresses p p g yfrom actual physical memory locations» Program memory = virtual addressesg y» Physical memory = real addresses» Use hardware to map between the two» Use hardware to map between the two
5/26/2009 cse410-14-virtual-memory © 2006-09, Perkins, DW Johnson and University of Washington 5
Memory Mappingy pp g
programdd
physicaldd
memoryi
heap
stackaddresses addressesmapping
programphysical
memorystack
heapprogram
stack
heapprogram
stack
disk
5/26/2009 cse410-14-virtual-memory © 2006-09, Perkins, DW Johnson and University of Washington 6
Virtual Physical Paging01
01
Virtual Page #
Physical Page #
0x0000 0x0000
• Divide a process's virtual address space into fixed-size chunks (called pages)
1234
1234size chunks (called pages)
• Divide physical memory into pages of the same sizeA i t l b
45
4567
0x6000
0 0000• Any virtual page can be located at any physical page
012
78910
0x0000
• Translation box converts from virtual pages to physical pages
310111213Translation
0x4000
0 00013 0xE000
Multiple Processes Vi t l Ph i l Multiple ProcessesShare Memory
01
01
Virtual Page #
Physical Page #
0x0000 0x0000
• Each process thinks it t t t dd
1234
1234starts at address
0x0000 and has all of memory
45
4567
0x6000
memory• A process doesn't
know anything about
012
78910
0x0000
know anything about physical addresses and doesn't care
310111213Translation
0x4000
and doesn t care 13 0xE000
Protection
• A process can only use its 0 0
Virtual Page #
Physical Page #
0x0000 0x0000p yown virtual addresses
• A process can't corrupt
123
123
another process's memory» It has no address to refer to it
45
456
0x6000
• How can Blue write to Green's page 2?
d dd f
012
789
0x0000
» needs an address to refer to physical page 7, but it doesn't have one
23
101112
Translation
0x4000
13Translation0xE000
Sparse AddressSpaces
0
Virtual Page #
Physical Page #
0x0000 0x00000• Memory addresses that
aren't being used at all don't have to be
0123
0x0000 0x00000123don't have to be
assigned real addresses» Code can start at a very
3456
30x4000
Unusedylow logical address
» Stack can start at a very high logical address
6789 0xA000
997998
0x0FFC000
high logical address» No physical pages
allocated for unused addresses in bet een
9
Translation0x1000000
0xA0009991000
addresses in between
Sh i M Virtual Physical Sharing Memory• Two processes can share 0
1
Virtual Page #
Physical Page #
0x0000 0x000001
memory by mapping two virtual pages to the same physical page
1234
1234physical page
• The code for Word can be shared for two Word
45
Word0x6000
00x0000
4567
processes» code pages are read only
h h i
0123
0x0000 78910• Each process has its own
data pages» possible to share data pages Translation
0 000
345
W d0x6000
10111213» possible to share data pages
too, but less common0xE000Word 13
Store Memory on DiskStore Memory on Disk
• Memory that isn't being 0
Virtual Page #
Physical Page #
0x0000 0x00000used can be saved on disk» swapped back in when it is
referenced via page fault
0123
0123referenced via page fault
• Programs can address more memory than is
456
456
physically available• This is one important
f i t l
789 0xA000
789reason for virtual memory
» too hard for programs to do this on their own
101112
Translation0xE000 Disk13
Memory Hierarchy Revisitedy y
• Once the translation hardware is there we have a caching problem again» Want size ≈ disk, performance ≈ memory
K i di k l i 100 000 i• Key issue: disk latency is 100,000 times memory, so design motivation is to avoid accessing disk
• Minimizing miss rate (“page faults”):• Minimizing miss rate (“page faults”):» VM “pages” are much larger than cache blocks = size
of disk blocks, usually 4K or 8K or more, y» Use fully associative lookup with approximate LRU» Question: should it be write-back or write-through?
5/26/2009 cse410-14-virtual-memory © 2006-09, Perkins, DW Johnson and University of Washington 13
Finding the Right Page (frame)g g g ( )• If fully associative, how do we find the right page
ith t i ll f ?without scanning all of memory?• Answer: index is called the page table
» Each process has a separate page table» Each process has a separate page table• Processor “page table register” points to active one – part of
process state» Page table indexed with virtual page number (VPN)» Page table indexed with virtual page number (VPN)
• The bits that aren’t part of the page offset» Each entry contains a valid bit and a physical page
b (PPN)number (PPN)• PPN is concatenated with page offset to get physical address
» No index tag needed – full VPN is index
5/26/2009 cse410-14-virtual-memory © 2006-09, Perkins, DW Johnson and University of Washington 14
Page Table pictureg pVirtual address
Page table register
31 30 29 28 27 15 14 13 12 11 10 9 8 3 2 1 0
Page offsetVirtual page number
Physical page numberValid
20 12
Page tableg
18
Page offsetPhysical page number
If 0 then page is not�present in memory
29 28 27 15 14 13 12 11 10 9 8 3 2 1 0
15
Physical address
How big is the page table?g p g
• From the previous slide:p» Virtual page number is 20 bits.» Physical page number is 18 bits + valid bit -> y p g
round up to 32 bits.• Or 20 bits + valid bit if 32-bit physical addressing
16
Dealing with large page tablesg g p g• Multi-level page tables
» “Any problem in CS can be solved by adding a level of indirection”or two…
Page Table Base Pointer
1st
2nd
3rd
A 3-level page table
VPN1 VPN2 VPN3 ff t
PPN
PPN offset
• Since most processes don’t use the whole address space, you don’t allocate the tables that aren’t needed
VPN1 VPN2 VPN3 offset
17
» Also, the 2nd and 3rd level page tables can be “paged” to disk.
Waitaminute!• We’ve just replaced every memory access MEM[addr] with:
MEM[MEM[MEM[MEM[PTBR VPN1 2] VPN2 2] VPN3 2] ff ]MEM[MEM[MEM[MEM[PTBR + VPN1<<2] + VPN2<<2] + VPN3<<2] + offset]
» i.e., 4 memory accesses
A d h ’t t lk d b t th b d t (i f lt )• And we haven’t talked about the bad case yet (i.e., page faults)…
“A bl i CS b l d b ddi l l f i di i ”“Any problem in CS can be solved by adding a level of indirection”» except too many levels of indirection…
H d d l ith t l l f i di ti ?• How do we deal with too many levels of indirection?
18
Caching Translationsg• Virtual to Physical translations are cached in a Translation Lookaside
Buffer (TLB).Page offsetVirtual page number
Virtual address
1220
31 30 29 15 14 13 12 11 10 9 8 3 2 1 0
Physical page numberValid Dirty Tag
TLB hit
TLB
Page offset
20
Cache index Byte�
Physical page number
Physical address tag
�
�
Physical address
Valid Tag Data
16 14
Cache index
2offsetPhysical address tag
32
Cache
19
DataCache hit
What about a TLB miss?
• If we miss in the TLB, we need to “walk the page t bl ”table”» In MIPS, an exception is raised and software fills the TLB» In x86, a “hardware page table walker” fills the TLB» In x86, a hardware page table walker fills the TLB
• What if the page is not in memory?» This situation is called a page fault.» The operating system will have to read the page from disk.» It will need to select a page to replace» It will need to select a page to replace.
• The O/S tries to approximate LRU (coming next)» The replaced page will need to be written back if dirty.
20
Summaryy• Virtual memory is great:
» It means that we don’t have to manage our own memory» It means that we don t have to manage our own memory.» It allows different programs to use the same physical memory.» It provides protect between different processes.
It ll t ll d h i b t ( lb it h t» It allows controlled sharing between processes (albeit somewhat inflexibly).
• The key technique is indirection:Y t th l i CS t i k ’ i thi l» Yet another classic CS trick you’ve seen in this class.
» Many problems can be solved with indirection.• Caching made a few appearances, too:
» Virtual memory enables using physical memory as a cache for disk.
» We used caching (in the form of the Translation LookasideBuffer) to make Virtual Memory’s indirection fast
21
Buffer) to make Virtual Memory s indirection fast.