Virtual Memory II
Philipp Koehn 11 November 2019
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
Virtual Memory II Philipp Koehn 11 November 2019 Philipp Koehn - - PowerPoint PPT Presentation
Virtual Memory II Philipp Koehn 11 November 2019 Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019 Address Space 1 N = 2 n bytes Virtual memory size: M = 2 m bytes Physical memory size: P = 2 p bytes
Philipp Koehn 11 November 2019
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
1
N = 2n bytes
M = 2m bytes
P = 2p bytes
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
2
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 II 11 November 2019
3
page table base register
Virtual address Physical address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
4
page table base register
Valid Physical page number
Virtual address Physical address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
5
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 II 11 November 2019
6
page table base register virtual page number page offset physical page number page offset
Valid Physical page number valid = 0?
Virtual address Physical address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
7
CPU MMU Memory
VA CPU chip Data PTEA PTE PA
returns data from memory to CPU
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
8
CPU MMU Memory
VA CPU chip PTEA PTE
Page fault exception handler
Exception Data
page not in physical memory
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
9
CPU MMU Memory
VA CPU chip PTEA PTE
Disk
Victim page New page
Page fault exception handler
Exception Data
page not in physical memory
– victim page to disk – new page to memory – update page table entries
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
10
CPU MMU Memory
VA CPU chip Data PTEA PTE PA
Disk
Victim page New page
Page fault exception handler
Exception
page not in physical memory
– victim page to disk – new page to memory – update page table entries
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
11
– identify which page to remove from RAM (victim page) – load page from disk to RAM – update page table entry – trigger do-over of instruction that caused exception
– loading into RAM very slow – added complexity of handling in software no big deal
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
12
→ integrate cache and virtual memory
→ use translation lookahead buffer (TLB)
→ multi-level page table
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
13
→ integrate cache and virtual memory
→ use translation lookahead buffer (TLB)
→ multi-level page table
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
14
– we claim that using on-disk memory is too slow – having data in RAM only practical solution
– we previously claimed that using RAM is too slow – having data in cache only practical solution
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
15
CPU MMU L1 Cache
VA CPU chip Data PTEA PTE PA
DRAM
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
16
CPU MMU L1 Cache
VA CPU chip Data PTEA PTE PA
DRAM
PTEA PTE miss?
⇒ Get page table from memory
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
17
CPU MMU L1 Cache
VA CPU chip Data PTEA PTE PA
DRAM
PTEA PTE PA Data miss? miss?
⇒ Get data from memory
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
18
→ integrate cache and virtual memory
→ use translation lookahead buffer (TLB)
→ multi-level page table
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
19
(virtual memory look-up)
– subsequent look-ups in same area of memory – look-up for a page can be cached
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
20
– lowest bits:
– middle bits: index (location) in cache – highest bits: tag in cache
more than one entry per index
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
21
CPU MMU Memory
VA CPU chip Data PA
TLB
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
22
CPU MMU Memory
VA CPU chip Data PA
TLB
PTEA PTE
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
23
CPU MMU Memory
VA CPU chip Data PTE PTEA PA
TLB
PTEA
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
24
→ integrate cache and virtual memory
→ use translation lookahead buffer (TLB)
→ multi-level page table
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
25
– 32 bit address space: 4GB – Page size: 4KB – Size of page table entry: 4 bytes → Number of pages: 1M → Size of page table: 4MB
most of the address space is not used
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
26
L2 PT 0 L2 PT 1 null null null
Valid Level 2 page table Level 1 page table
null null null L2 PT 8 null null PTE 0 PTE 1023
Valid Physical page Level 2 page table Physical memory
PTE 0 PTE 1023
Valid Physical page
PTE 1023
Valid Physical page
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
27
1M entries
→ each level 1K entry (1K2=1M)
→ each level 32 entry (324=1M)
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
28
→ integrate cache and virtual memory
→ use translation lookahead buffer (TLB)
→ multi-level page table
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
29
CPU
VPN VPO TLBT TLBI
TLB
PPN PPO VPN1 VPN2 VPN3 VPN4
PTE PTE PTE PTE
CR3 CT CI CO
L1 Cache
Data
RAM
L1 hit L1 miss TLB hit TLB miss Virtual address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
30
CPU
VPN VPO TLBT TLBI
TLB
PPN PPO VPN1 VPN2 VPN3 VPN4
PTE PTE PTE PTE
CR3 CT CI CO
L1 Cache
Data
RAM
L1 hit L1 miss TLB hit TLB miss Virtual address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
31
CPU
VPN VPO TLBT TLBI
TLB
PPN PPO VPN1 VPN2 VPN3 VPN4
PTE PTE PTE PTE
CR3 CT CI CO
L1 Cache
Data
RAM
L1 hit L1 miss TLB hit TLB miss Virtual address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
32
CPU
VPN VPO TLBT TLBI
TLB
PPN PPO VPN1 VPN2 VPN3 VPN4
PTE PTE PTE PTE
CR3 CT CI CO
L1 Cache
Data
RAM
L1 hit L1 miss TLB hit TLB miss Virtual address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
33
CPU
VPN VPO TLBT TLBI
TLB
PPN PPO VPN1 VPN2 VPN3 VPN4
PTE PTE PTE PTE
CR3 CT CI CO
L1 Cache
Data
RAM
L1 hit L1 miss TLB hit TLB miss Virtual address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
34
CPU
VPN VPO TLBT TLBI
TLB
PPN PPO VPN1 VPN2 VPN3 VPN4
PTE PTE PTE PTE
CR3 CT CI CO
L1 Cache
Data
RAM
L1 hit L1 miss TLB hit TLB miss Virtual address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
35
CPU
VPN VPO TLBT TLBI
TLB
PPN PPO VPN1 VPN2 VPN3 VPN4
PTE PTE PTE PTE
CR3 CT CI CO
L1 Cache
Data
RAM
L1 hit L1 miss TLB hit TLB miss Virtual address
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
36
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
37
L1 data cache 32 KB, 8-way L1 instruction cache 32 KB, 8-way L2 unified cache 256 KB, 8-way Registers Instruction fetch L1 data TLB 64 entries, 4-way L1 instruction TLB 128 entries, 4-way MMU (address translation) L2 unified TLB 512 entries, 4-way L3 unified cache 8 MB, 16-way (shared by all cores) DDR3 memory controller (shared by all cores) DDR3 memory
Single Core Chip with 4 cores
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
38
48 bit (→ 248 = 256TB address space)
52 bit (→ 252 = 4PB address space)
12 bit (→ 212 = 4KB) ⇒ 236 = 64G entries, split in 4 levels (512 entries each)
8-way associative, 64 sets, 64 byte blocks (32 KB)
8-way associative, 512 sets, 64 byte blocks (256 KB)
16-way associative, 8K sets, 64 byte blocks (8 MB)
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
39
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
40
when switching processes → flush
when switching processes → update pointer to top-level page table
→ pointers to page table do not require translation
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
41
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
42
mm pgd mmap vm_end vm_start vm_prot vm_flags vm_next vm_end vm_start vm_prot vm_flags vm_next vm_end vm_start vm_prot vm_flags vm_next Shared Libraries Data Text vm_end vm_start vm_prot vm_flags vm_next task_struct mm_struct vm_area_struct Process VM
address of page table
private, shared
read, write
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
43
vm_end vm_start vm_prot vm_flags vm_next vm_end vm_start vm_prot vm_flags vm_next vm_end vm_start vm_prot vm_flags vm_next Shared Libraries Data Text vm_end vm_start vm_prot vm_flags vm_next vm_area_struct Process VM
Segmentation fault Normal page fault (-> load page) Protection exception (if write)
Kernel walks through vm area struct list to resolve page fault
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
44
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
45
– file divided up into pages – demand loading: just mapped to addresses, not actually loaded – could be code, shared library, data file
– typically allocated memory – when used for the first time: set all values to zero – never really on disk, except when swapped out
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
46
– only its process can read/write – changes not visible to other processes
– multiple processes can read/write – changes visible to other processes
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
47
– virtual memory area structures – memory mapping structures – page tables
to existing 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) Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
48
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) Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
49
(may be loaded from file)
– executable code – read – write – inaccessible
– anonymous: data object initially zeroed out – private – shared
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
50
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
51
– allocate specified amount of data – return pointer to (virtual) address – memory is allocated on heap
– frees memory allocated at pointer location – may be between other allocated memory
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
52
p1
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
53
p1 p1 p2
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
54
p1 p1 p2 p1 p2 p3
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
55
p1 p1 p2 p1 p2 p3 p1 p3
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
56
p1 p1 p2 p1 p2 p3 p1 p3 p1 p3 p4
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019
57
– internal: frequent malloc() and free() creates internal: fragmented memory use – external: new malloc() exceeds heap space → is split
– need to maintain a list of free memory areas – implicit: space between allocated memory – explicit: maintain a separate list
Philipp Koehn Computer Systems Fundamentals: Virtual Memory II 11 November 2019