Lecture 21
Lecture 21
Virtual Memory
Virtual Memory Page Table Hard Drive
…
of Process 1
…
of Process 1
99 Unallocated Invalid
100 Data 1 1
101 … A
103 … B B …
Physical/Main
104 … C C …
Memory
105 … D D …
… … 1 Data 1
2
Virtual Memory Page Table 3 Data 3
of Process 2
… …
of Process 2 4 Data 2
99 … O …
O
100 Unallocated P …
Unallocated
101 … P Q …
102 Data 3 3 R …
103 Data 2 4
Process 2
104 … Q
105 … R
… …
2
Carnegie Mellon
M-1:
Data word
3
Carnegie Mellon
4
Carnegie Mellon
4
Carnegie Mellon
4
Carnegie Mellon
4
Carnegie Mellon
Today
• VM basic concepts and operation
• Other critical benefits of VM
• Address translation
5
Carnegie Mellon
So Far…
User 1
VA PA
Magic Memory
User 2 data Management
Unit (Part of OS)
data
User n
6
Carnegie Mellon
m-1 p p-1 0
m-1 p p-1 0
m-1 p p-1 0
m-1 p p-1 0
m-1 p p-1 0
Valid bit = 0:
Page not in memory
(page fault)
m-1 p p-1 0
Valid bit = 0:
Page not in memory
Valid bit = 1
(page fault)
m-1 p p-1 0
CPU MMU
Memory
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
8
Carnegie Mellon
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
8
Carnegie Mellon
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
8
Carnegie Mellon
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
8
Carnegie Mellon
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
8
Carnegie Mellon
Data
5
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
8
Carnegie Mellon
CPU Chip
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
9
Carnegie Mellon
CPU Chip
1
VA
CPU MMU Memory Disk
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
9
Carnegie Mellon
2
CPU Chip
PTEA
1
VA
CPU MMU Memory Disk
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
9
Carnegie Mellon
2
CPU Chip
PTEA
1
VA PTE
CPU MMU Memory Disk
3
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
9
Carnegie Mellon
2
CPU Chip
PTEA
1
VA PTE
CPU MMU Memory Disk
3
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
9
Carnegie Mellon
2
CPU Chip Victim page
PTEA
1
5
VA PTE
CPU MMU Memory Disk
3
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
9
Carnegie Mellon
2
CPU Chip Victim page
PTEA
1
5
VA PTE
CPU MMU Memory Disk
3
New page
6
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
9
Carnegie Mellon
2
CPU Chip Victim page
PTEA
1
5
VA PTE
CPU MMU Memory Disk
7 3
New page
6
CPU Chip
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
10
Carnegie Mellon
CPU Chip
Cache
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
10
Carnegie Mellon
CPU Chip
Cache
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
10
Carnegie Mellon
CPU Chip
PTEA
CPU MMU Memory
VA
Cache
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
10
Carnegie Mellon
CPU Chip
Cache
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
10
Carnegie Mellon
Cache
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
10
Carnegie Mellon
PTE
Cache
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
10
Carnegie Mellon
PTE
Cache
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
10
Carnegie Mellon
PTE
Cache
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
10
Carnegie Mellon
PTE
Data
Cache
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
10
Carnegie Mellon
PTE
PA Data
hit
Data Cache
VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address
10
Carnegie Mellon
Today
• Three Virtual Memory Optimizations
• TLB
• Virtually-indexed, physically-tagged cache
• Page the page table (a.k.a., multi-level page table)
11
Carnegie Mellon
12
Carnegie Mellon
12
Carnegie Mellon
• Page table entries (PTEs) are already cached in cache like any
other memory data. But:
• PTEs may be evicted by other data references
• PTE hit still requires a small cache delay
12
Carnegie Mellon
13
Carnegie Mellon
13
Carnegie Mellon
n-1 p p-1 0
TLB tag Virtual
(TLBT) Page
TLBNumber
index (TLBI) Offset
TLB Hit
CPU Chip
TLB
CPU MMU
Cache/
Memory
15
Carnegie Mellon
TLB Hit
CPU Chip
TLB
1
VA
CPU MMU
Cache/
Memory
15
Carnegie Mellon
TLB Hit
CPU Chip
TLB
2
VPN
1
VA
CPU MMU
Cache/
Memory
15
Carnegie Mellon
TLB Hit
CPU Chip
TLB
2 PTE
VPN 3
1
VA
CPU MMU
Cache/
Memory
15
Carnegie Mellon
TLB Hit
CPU Chip
TLB
2 PTE
VPN 3
1
VA PA
CPU MMU
4 Cache/
Memory
15
Carnegie Mellon
TLB Hit
CPU Chip
TLB
2 PTE
VPN 3
1
VA PA
CPU MMU
4 Cache/
Memory
Data
5
15
Carnegie Mellon
TLB Hit
CPU Chip
TLB
2 PTE
VPN 3
1
VA PA
CPU MMU
4 Cache/
Memory
Data
5
TLB Miss
CPU Chip
TLB
2
VPN
1
VA
CPU MMU
Cache/
Memory
16
Carnegie Mellon
TLB Miss
CPU Chip
TLB
2
VPN
1 3
VA PTEA
CPU MMU
Cache/
Memory
16
Carnegie Mellon
TLB Miss
CPU Chip
TLB
4
2 PTE
VPN
1 3
VA PTEA
CPU MMU
Cache/
Memory
16
Carnegie Mellon
TLB Miss
CPU Chip
TLB
4
2 PTE
VPN
1 3
VA PTEA
CPU MMU
Cache/
PA Memory
5
16
Carnegie Mellon
TLB Miss
CPU Chip
TLB
4
2 PTE
VPN
1 3
VA PTEA
CPU MMU
Cache/
PA Memory
5
Data
6
16
Carnegie Mellon
Today
• Three Virtual Memory Optimizations
• TLB
• Virtually-indexed, physically-tagged cache
• Page the page table (a.k.a., multi-level page table)
17
Carnegie Mellon
Performance Issue in VM
• Address translation and cache accesses are serialized
• First translate from VA to PA
• Then use PA to access cache
• Slow! Can we speed it up?
CPU Chip
PA Data
hit
Data Cache
18
Carnegie Mellon
Performance Issue in VM
19
Carnegie Mellon
Performance Issue in VM
Unchanged!!
19
Carnegie Mellon
Performance Issue in VM
Unchanged!!
=
Set Cache Line Cache
Tag Tag Set Index
Index Offset
19
Carnegie Mellon
Performance Issue in VM
Unchanged!!
=
Set Cache Line Cache
Tag Tag Set Index
Index Offset
Performance Issue in VM
Unchanged!!
=
Set Cache Line Cache
Tag Tag Set Index
Index Offset
Any Implications?
20
Carnegie Mellon
Any Implications?
12 bits
Virtual Virtual page number
Page Offset
Address (VPN)
4 bits
20
Carnegie Mellon
Any Implications?
12 bits
Virtual Virtual page number
Page Offset
Address (VPN)
8 bits 4 bits
20
Carnegie Mellon
Any Implications?
12 bits
Virtual Virtual page number
Page Offset
Address (VPN)
8 bits 4 bits
20
Carnegie Mellon
Any Implications?
12 bits
Virtual Virtual page number
Page Offset
Address (VPN)
8 bits 4 bits
20
Carnegie Mellon
Any Implications?
12 bits
Virtual Virtual page number
Page Offset
Address (VPN)
8 bits 4 bits
Any Implications?
12 bits
Virtual Virtual page number
Page Offset
Address (VPN)
9 bits 4 bits
21
Carnegie Mellon
Any Implications?
12 bits
Virtual Virtual page number
Page Offset
Address (VPN)
9 bits 4 bits
21
Carnegie Mellon
Any Implications?
12 bits
Virtual Virtual page number
Page Offset
Address (VPN)
9 bits 4 bits
21
Carnegie Mellon
Any Implications?
12 bits
Virtual Virtual page number
Page Offset
Address (VPN)
9 bits 4 bits
21
Carnegie Mellon
Today
• Three Virtual Memory Optimizations
• TLB
• Virtually-indexed, physically-tagged cache
• Page the page table (a.k.a., multi-level page table)
22
Carnegie Mellon
23
Carnegie Mellon
23
Carnegie Mellon
23
Carnegie Mellon
23
Carnegie Mellon
24
Carnegie Mellon
24
Carnegie Mellon
24
Carnegie Mellon
24
Carnegie Mellon
24
Carnegie Mellon
24
Carnegie Mellon
24
Carnegie Mellon
24
Carnegie Mellon
...
25
Carnegie Mellon
unallocated
pages
unallocated
pages
VP 9215
32 bit addresses, 4KB pages, 4-byte PTEs
... 26
Carnegie Mellon
1023 null
PTEs
PTE 1023 unallocated
pages
VP 9215
32 bit addresses, 4KB pages, 4-byte PTEs
... 26
Carnegie Mellon
Page table
base register
(PTBR)
VIRTUAL ADDRESS
n-1 p-1 0
VPN VPO
page table
PPN
m-1 p-1 0
PPN PPO
PHYSICAL ADDRESS
27
Carnegie Mellon
Page table
base register
(PTBR)
VIRTUAL ADDRESS
n-1 p-1 0
VPN 1 VPN 2 VPO
Level 1 Level 2
page table page table
PPN
m-1 p-1 0
PPN PPO
PHYSICAL ADDRESS
28
Carnegie Mellon
Page table
base register
(PTBR)
VIRTUAL ADDRESS
n-1 p-1 0
VPN 1 VPN 2 ... VPN k VPO
PPN
m-1 p-1 0
PPN PPO
PHYSICAL ADDRESS
29
Carnegie Mellon
Today
• Three Virtual Memory Optimizations
• TLB
• Virtually-indexed, physically-tagged cache
• Page the page table (a.k.a., multi-level page table)
30
Carnegie Mellon
Main memory
31
Carnegie Mellon
32
Carnegie Mellon
32 4
TLBT TLBI
32
Carnegie Mellon
32 4
TLBT TLBI
...
32
Carnegie Mellon
32 4
TLBT TLBI
TLB
hit
...
40
PPN
32
Carnegie Mellon
32 4
TLBT TLBI
TLB
hit
TLB
miss ...
CR3
PTE PTE PTE PTE
Page tables 32
Carnegie Mellon
32 4
TLBT TLBI
TLB
hit
TLB
miss ...
CR3
PTE PTE PTE PTE
Page tables 32
Carnegie Mellon
32 4
TLBT TLBI
TLB
hit
TLB
miss ...
Page tables 32
Carnegie Mellon
32 4
TLBT TLBI
TLB
hit
TLB
miss ...
Page tables 32
Carnegie Mellon
32 4
TLBT TLBI
L1 d-cache
TLB (64 sets, 8 lines/set)
hit
TLB
miss ... ...
Page tables 32
Carnegie Mellon
Page tables 32
Carnegie Mellon
Page tables 32
Carnegie Mellon
33