![Page 1: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/1.jpg)
Virtual Memory
Philipp Koehn
25 April 2018
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 2: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/2.jpg)
1Recall: Process Address Space
Kernel memory
User stack
Memory-mapped region for shared libraries
Run time heap (created by malloc)
Read/write segment (.data / .bss)
Read-only code segment (.init, .text., .rodata)
Stack pointer
Loaded fromexecutable
0
400000
ffffffff
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 3: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/3.jpg)
2Virtual Memory
• Abstraction of physical memory
• Purpose
– appearance of more available memory than physically exists (DRAM)
– handles disk caching / loading
– insulates memory of each process
• Page table: maps from virtual address to physical addresses
• Memory management unit (MMU):hardware implementation of address translation
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 4: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/4.jpg)
3Warning
• This is going to get very complex
• Closely tied with multi-tasking (multiple processes)
• Partly managed by hardware,partly managed by software
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 5: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/5.jpg)
4
virtual addressing
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 6: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/6.jpg)
5Physical Addressing
CPU…
0:1:2:3:4:5:6:7:
Physical address (PA)
Main memory
Data
CPU chip
• So far, assumed CPU addresses physical memory
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 7: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/7.jpg)
6Virtual Addressing
CPU MMU…
0:1:2:3:4:5:6:7:
Addresstranslation
Virtualaddress
(VA)
Physicaladdress
(PA)
CPU chip Main memory
Data
• Memory management unit (MMU): maps virtual to physical addresses
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 8: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/8.jpg)
7Address Space
• Virtual memory size: N = 2n bytes, e.g., 256TB
• Physical memory size: M = 2m bytes, e.g., 16GB
• Page (block of memory): P = 2p bytes, e.g., 4KB
• A virtual address can be encoded in n bits
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 9: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/9.jpg)
8
caching
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 10: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/10.jpg)
9Caching... Again?
• Yes, we already discussed cachingbut: for on-chip cache of DRAM memory
• Now
– caching between RAM and disk
– driven by a large virtual memory address space
– to avoid unnecessary and duplicate loading
• Jargon
– previously "block", now "page"
– now: "swapping" or "paging"
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 11: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/11.jpg)
10Mapping
emptyempty
empty
emptyempty
Physical memory
unallocatedcached
uncachedcached
unallocateduncachedcached
unallocatedunallocated
unallocated
…
0:1:2:3:4:5:6:7:
Virtual memory
8:
15:
Virtual pages (VP)stored on disk
Physical pages (PP)cached in DRAM
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 12: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/12.jpg)
11State of Virtual Memory Page
• Cached
– allocated page
– stored in physical memory
• Uncached
– allocated page
– not in physical memory
• Unallocated
– not used by virtual memory system so far
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 13: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/13.jpg)
12Page Table
• Array of page table entries (PTE)
• Valid bit
– set if PTE currently maps to physical address (cached)
– not set otherwise (uncached or unallocated)
• Mapped address
– if cached: physical address in DRAM
– if not cached: physical address on disk
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 14: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/14.jpg)
13Page Table
VP1VP6VP3VP7
VP0VP1VP2VP3
Physical memoryDRAM
null
null
0:1:2:3:4:5:6:7:
Page table
Disk
01010011
VP4VP5VP6VP7
Valid Address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 15: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/15.jpg)
14Page Hit
VP1VP6VP3VP7
VP0VP1VP2VP3
Physical memoryDRAM
null
null
0:1:2:3:4:5:6:7:
Page table
Disk
01010011
VP4VP5VP6VP7
Valid Address
Virtual address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 16: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/16.jpg)
15Page Fault
VP1VP6VP3VP7
VP0VP1VP2VP3
Physical memoryDRAM
null
null
0:1:2:3:4:5:6:7:
Page table
Disk
01010011
VP4VP5VP6VP7
Valid Address
Virtual address
• Valid bit = 0
• Page not in RAM
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 17: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/17.jpg)
16Page Fault
VP1VP6VP3VP7
VP0VP1VP2VP3
Physical memoryDRAM
null
null
0:1:2:3:4:5:6:7:
Page table
Disk
01010011
VP4VP5VP6VP7
Valid Address
Virtual address
• Page is on disk
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 18: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/18.jpg)
17Page Fault
VP1
VP3VP7
VP0VP1VP2VP3
Physical memoryDRAM
null
null
0:1:2:3:4:5:6:7:
Page table
Disk
01010011
VP4VP5VP6VP7
Valid Address
Virtual address
• Make space in RAM
• Pre-empt "victim" page
• Typically out-dated cached page
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 19: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/19.jpg)
18Page Fault
VP1VP2VP3VP7
VP0VP1VP2VP3
Physical memoryDRAM
null
null
0:1:2:3:4:5:6:7:
Page table
Disk
01010011
VP4VP5VP6VP7
Valid Address
Virtual address
• Load page into RAM
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 20: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/20.jpg)
19Page Fault
VP1VP2VP3VP7
VP0VP1VP2VP3
Physical memoryDRAM
null
null
0:1:2:3:4:5:6:7:
Page table
Disk
01110011
VP4VP5VP6VP7
Valid Address
Virtual address
• Update page table entry
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 21: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/21.jpg)
20Allocating Pages
• What happens when we load a program?
• We need to load its executable into memory
• Similar: create data objects when program is running
("allocating" memory)
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 22: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/22.jpg)
21Allocating Page
VP1VP6VP3VP7
VP0VP1VP2VP3
Physical memoryDRAM
null
null
0:1:2:3:4:5:6:7:
Page table
Disk
01010011
VP4VP5VP6VP7
Valid Address
• Identify space in virtual memory
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 23: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/23.jpg)
22Allocating Page
VP1VP6VP3VP7
VP0VP1VP2VP3
Physical memoryDRAM
null0:1:2:3:4:5:6:7:
Page table
Disk
01010011
VP4VP5VP6VP7
Valid Address
• Map to data on disk– do not actual load– just create page table entries– let virtual memory system handle loading
⇒ On-demand loading
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 24: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/24.jpg)
23Process Memory
• Nothing loaded at startup
• Working set (or resident set)
– pages of a process that are currently in DRAM
– loaded by virtual memory system on demand
• Thrashing
– memory actively required by all processeslarger than physically available
– frequent swapping of memory to/from disk
– very bad: slows down machine dramatically
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 25: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/25.jpg)
24
memory management
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 26: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/26.jpg)
25One Page Table per Process
PP10
VP1VP2
0:1:2:3:
Process 10110
VP1VP2
0:1:2:3:
Process 20110
PP2
PP7
Physical memory
Shared page
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 27: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/27.jpg)
26Process Address Space
Kernel memory
User stack
Memory-mapped region for shared libraries
Run time heap (created by malloc)
Read/write segment (.data / .bss)
Read-only code segment (.init, .text., .rodata)
Stack pointer
Loaded fromexecutable
0
400000
ffffffff
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 28: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/28.jpg)
27Simplified Linking
DataCode
400000
• Each process has its code in address 0x400000
• Easy linking: Linker can establish fixed addresses
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 29: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/29.jpg)
28Simplified Loading
• When loading process into memory...
• Enter .data and .text section into page table
• Mark them as invalid (= not actually in RAM)
• Called memory mapping (more on that later)
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 30: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/30.jpg)
29Simplified Sharing
Shared libraries
Shared libraries
Physical memory
• Shared librariesused by several processes
e.g., stdio providing printf,
e.g., scanf, open, close, ...
• Not copied multiple timesinto RAM
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 31: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/31.jpg)
30Simplified Memory Allocation
• Process may need more memory (e.g., malloc call)
⇒ New entry in page table
• Mapped to arbitrary pages in physical memory
• Do not have to be contiguous
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 32: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/32.jpg)
31Memory Protection
Physical memory
yes nono PP 6yes yesno PP 4
VP 0VP 1
yes yesyes PP 2VP 2
READ WRTSUP Address
yes nono PP 9yes yesyes PP 6
VP 0VP 1
yes yesno PP 11VP 2
READ WRTSUP Address
Process 1
Process 2
PP 0
PP 2
PP 4
PP 6
PP 9
PP 11
• Page may be kernel only: SUP=yes
• Page may be read-only (e.g., code)
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 33: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/33.jpg)
32
address translation
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 34: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/34.jpg)
33Address Space
• Virtual memory size: N = 2n bytes
• Physical memory size: M = 2m bytes
• Page (block of memory): P = 2p bytes
• A virtual address can be encoded in n bits
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 35: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/35.jpg)
34Address Translation
• Task: mapping virtual address to physical address
– virtual address (VA): used by machine code instructions
– physical address (PA): location in RAM
• FormallyMAP: VA → PA ∪ 0
where:MAP(A) = PA if in RAM
xxxxx = 0 otherwise
• Note: this happens very frequently in machine code
• We will do this in hardware: Memory Management Unit (MMU)
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 36: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/36.jpg)
35Basic Architecture
page table base register
Virtual address
Physical address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 37: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/37.jpg)
36Basic Architecture
page table base register
Valid Physical page number
Virtual address
Physical address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 38: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/38.jpg)
37Basic Architecture
page table base register
virtual page number page offset
physical page number page offset
Valid Physical page number
Virtual address
Physical address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
![Page 39: Virtual Memory - Department of Computer Sciencephi/csf/slides/lecture-virtual-memory.pdf · { loaded by virtual memory system on demand Thrashing { memory actively required by all](https://reader036.vdocuments.net/reader036/viewer/2022062602/5ede2863ad6a402d66697486/html5/thumbnails/39.jpg)
38Basic Architecture
page table base register
virtual page number page offset
physical page number page offset
Valid Physical page number
valid = 0?-> page fault
Virtual address
Physical address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018