Virtual Memory
Philipp Koehn 25 April 2018
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
Virtual Memory Philipp Koehn 25 April 2018 Philipp Koehn Computer - - PowerPoint PPT Presentation
Virtual Memory Philipp Koehn 25 April 2018 Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018 Recall: Process Address Space 1 Kernel memory ffffffff User stack Stack pointer Memory-mapped region for shared
Philipp Koehn 25 April 2018
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
1
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 from executable 400000 ffffffff
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
2
– appearance of more available memory than physically exists (DRAM) – handles disk caching / loading – insulates memory of each process
maps from virtual address to physical addresses
hardware implementation of address translation
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
3
partly managed by software
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
4
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
5
0: 1: 2: 3: 4: 5: 6: 7:
Physical address (PA) Main memory Data CPU chip
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
6
0: 1: 2: 3: 4: 5: 6: 7:
Address translation Virtual address (VA) Physical address (PA) CPU chip Main memory Data
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
7
N = 2n bytes, e.g., 256TB
M = 2m bytes, e.g., 16GB
P = 2p bytes, e.g., 4KB
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
8
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
9
but: for on-chip cache of DRAM memory
– caching between RAM and disk – driven by a large virtual memory address space – to avoid unnecessary and duplicate loading
– previously "block", now "page" – now: "swapping" or "paging"
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
10
empty empty empty empty empty Physical memory unallocated cached uncached cached unallocated uncached cached unallocated unallocated 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
11
– allocated page – stored in physical memory
– allocated page – not in physical memory
– not used by virtual memory system so far
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
12
– set if PTE currently maps to physical address (cached) – not set otherwise (uncached or unallocated)
– if cached: physical address in DRAM – if not cached: physical address on disk
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
13
VP1 VP6 VP3 VP7 VP0 VP1 VP2 VP3 Physical memory DRAM null null
0: 1: 2: 3: 4: 5: 6: 7:
Page table Disk 1 1 1 1 VP4 VP5 VP6 VP7 Valid Address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
14
VP1 VP6 VP3 VP7 VP0 VP1 VP2 VP3 Physical memory DRAM null null
0: 1: 2: 3: 4: 5: 6: 7:
Page table Disk 1 1 1 1 VP4 VP5 VP6 VP7 Valid Address Virtual address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
15
VP1 VP6 VP3 VP7 VP0 VP1 VP2 VP3 Physical memory DRAM null null
0: 1: 2: 3: 4: 5: 6: 7:
Page table Disk 1 1 1 1 VP4 VP5 VP6 VP7 Valid Address Virtual address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
16
VP1 VP6 VP3 VP7 VP0 VP1 VP2 VP3 Physical memory DRAM null null
0: 1: 2: 3: 4: 5: 6: 7:
Page table Disk 1 1 1 1 VP4 VP5 VP6 VP7 Valid Address Virtual address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
17
VP1 VP3 VP7 VP0 VP1 VP2 VP3 Physical memory DRAM null null
0: 1: 2: 3: 4: 5: 6: 7:
Page table Disk 1 1 1 1 VP4 VP5 VP6 VP7 Valid Address Virtual address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
18
VP1 VP2 VP3 VP7 VP0 VP1 VP2 VP3 Physical memory DRAM null null
0: 1: 2: 3: 4: 5: 6: 7:
Page table Disk 1 1 1 1 VP4 VP5 VP6 VP7 Valid Address Virtual address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
19
VP1 VP2 VP3 VP7 VP0 VP1 VP2 VP3 Physical memory DRAM null null
0: 1: 2: 3: 4: 5: 6: 7:
Page table Disk 1 1 1 1 1 VP4 VP5 VP6 VP7 Valid Address Virtual address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
20
create data objects when program is running ("allocating" memory)
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
21
VP1 VP6 VP3 VP7 VP0 VP1 VP2 VP3 Physical memory DRAM null null
0: 1: 2: 3: 4: 5: 6: 7:
Page table Disk 1 1 1 1 VP4 VP5 VP6 VP7 Valid Address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
22
VP1 VP6 VP3 VP7 VP0 VP1 VP2 VP3 Physical memory DRAM null
0: 1: 2: 3: 4: 5: 6: 7:
Page table Disk 1 1 1 1 VP4 VP5 VP6 VP7 Valid Address
– 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
23
– pages of a process that are currently in DRAM – loaded by virtual memory system on demand
– memory actively required by all processes larger 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
24
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
25
PP10 VP1 VP2
0: 1: 2: 3:
Process 1 1 1 VP1 VP2
0: 1: 2: 3:
Process 2 1 1 PP2 PP7 Physical memory Shared page
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
26
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 from executable 400000 ffffffff
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
27
Data Code
400000
Linker can establish fixed addresses
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
28
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
29
Shared libraries Shared libraries
Physical memory
used by several processes e.g., stdio providing printf, e.g., scanf, open, close, ...
into RAM
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
30
⇒ New entry in page table
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
31
Physical memory
yes no no PP 6 yes yes no PP 4 VP 0 VP 1 yes yes yes PP 2 VP 2 READ WRT SUP Address yes no no PP 9 yes yes yes PP 6 VP 0 VP 1 yes yes no PP 11 VP 2 READ WRT SUP Address
Process 1 Process 2
PP 0 PP 2 PP 4 PP 6 PP 9 PP 11
SUP=yes
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
32
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
33
N = 2n bytes
M = 2m bytes
P = 2p bytes
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
34
mapping virtual address to physical address – virtual address (VA): used by machine code instructions – physical address (PA): location in RAM
MAP: VA → PA ∪ 0 where: MAP(A) = PA if in RAM xxxxx = 0 otherwise
this happens very frequently in machine code
Memory Management Unit (MMU)
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
35
page table base register
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
36
page table base register
Valid Physical page number
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
37
page table base register virtual page number page offset physical page number page offset
Valid Physical page number
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018
38
page table base register virtual page number page offset physical page number page offset
Valid Physical page number valid = 0?
Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018