13-vm-notes
13-vm-notes
Vishal Shrivastav
CS 3410
Computer System Organization & Programming
These slides are the product of many rounds of teaching CS 3410 by Professors Weatherspoon, Bala, Bracy, and Sirer.
Where are we now and where are we going?
• How many programs do you run at once?
• a) 1
• b) 2
• c) 3-5
• d) 6-10
• e) 11+
Big Picture: Multiple Processes
How to run multiple processes?
Heap
Data
Text
0x000…0
Memory
Multiple Processes
• Q: What happens when another program is
executed concurrently on another processor?
0xfff…f
CPU
0x7ff…f
Stack
$$ Stack
$$
Heap
Heap
CPU
Data
Data
• A: The addresses will conflict Text
Text
Even though, CPUs may take 0x000…0
turns using memory bus Memory
Multiple Processes
• Q: Can we relocate second program?
0xfff…f
CPU
0x7ff…f
Stack
Stack
Heap
Heap
CPU
Data
Data
Text
Text
0x000…0
Memory
Solution? Multiple processes/processors
Process 1 3 A
2 B
1 C
0 D Give each process an illusion
that it has exclusive access to
entire main memory
Process 2
3 E
2 F
1 G
0 H
But In Reality…
14
D 13
12
Process 1 E 11
10
C 9
8
7
B
G 6
H 5
4
Process 2
A 3
2
F 1
0
Physical Memory
How do we create the illusion?
14
D 13
12
Process 1 3 A E 11
2 B 10
1 C 9
0 D C
8
7
B
G 6
H 5
Process 2
3 E 4
2 F A 3
1 G 2
0 H
F 1
0
Physical Memory
How do we create the illusion?
14
D 13
12
Process 1 3 A E 11
2 B 10
1 C 9
All problems
0 in computer
D science can be solved by C
another
8
level of indirection. 7
B
– David G
Wheeler
6
5
H
Process 2
3 E 4
2 F A 3
1 G 2
0 H
F 1
0
Physical Memory
How do we create the illusion?
14
D 13
12
Process 1 3 A E 11
2 B 10
1 C Map virtual
Physical address
address to C 9
0 D physical address 8
Virtual address 7
Memory B
G 6
management unit
(MMU) takes care H 5
Process 2
3 E of the mapping 4
2 F A 3
1 G 2
0 H
F 1
Virtual Memory
0
(just a concept; does not exist physically)
Physical Memory
How do we create the illusion?
14
Process 1 wants to 13
access data C D
12
Process 1 thinks it
Process 1 3 A is stored at addr 1 E 11
2 B So CPU generates 10
1 C addr 1
Physical address
C 9
0 D This addr is 8
Virtual address intercepted by 7
MMU B
G 6
MMU knows this
is a virtual addr H 5
Process 2
3 E MMU looks at the 4
2 F mapping
A 3
1 G Virtual addr 1 ->
Physical addr 9 2
0 H Data at Physical
addr 9 is sent to F 1
Virtual Memory
CPUphysically) 0
(just a concept; does not exist
And that data is
indeed C!!! Physical Memory
How do we create the illusion?
14
D 13
12
Process 1 3 A E 11
2 B 10
1 C Map virtual
address to C 9
0 D physical address 8
7
Memory B
G 6
management unit
(MMU) takes care H 5
Process 2
3 E of the mapping 4
2 F 3
1 G 2
0 H
1
Virtual Memory
0
A F Physical Memory
Disk
Big Picture: (Virtual) Memory
• From a process’s perspective –
20
Picture Memory as… ?
Byte Array: Segments: New! Page Array:
addr data 0xfffffffc
0xfffffff xaa system page n
0xfffff000
f 0x80000000 reserved
… 0x7ffffffc 0xffffe000
… stack
0xffffd000
x00
h s eg m ent ...
eac
s es s ome #
u
of pages
heap
0x00004000
0x10000000 data ...
0x00003000
text 0x00002000
page 2
x00
0x00400000 page 1
xef 0x00001000
system
xcd
0x00000000 reserved 0x00000000
page 0
xab 21
A Little More About Pages
Page Array: Memory size = depends on system
4KB say 4GB
0xfffff000
... paddr
0x9000000c
0x4123B 0xABC
0xC20A3
0x90000008 0x4123B 0x90000000
0x90000004 0x10044
0x90000000 0x00000 0x4123BABC
0x4123B000
31 12 11 0
vaddr 0x00002 0xABC
0x10045000
index into the page table page offset
0x10044000
Clicker Question:
Page size is 16KB how many bits is page offset?
(a) 12 (b) 13 (c) 14 (d) 15 (e)
16
• What if Main Memory is not 4GB?
Physical page number is no longer 20 bits
Clicker Question:
Page size 4KB, Main Memory 512 MB
how many bits is PPN?
(a) 15 (b) 16 (c) 17 (d) 18 (e)
19
26
Virtual Memory: Summary
Virtual Memory: a Solution for All Problems
32
Page Table Overhead
• How large is PageTable?
• Virtual address space (for each process):
Given: total virtual memory: 232 bytes = 4GB
Given: page size: 212 bytes = 4KB
# entries in PageTable? 220 = 1 million entries
size of PageTable? PTE size = 4 bytes
• Physical address space: PageTable size = 4 x 220 = 4MB
total physical memory: 229 bytes = 512MB
overhead for 10 processes?
10 x 4MB = 40 MB of overhead!
• 40 MB /512 MB = 7.8% overhead,
space due to PageTable 33
But Wait... There’s more!
• Page Table Entry won’t be just an integer
• Meta-Data
Valid Bits
• What PPN means “not mapped”? No such number…
• At first: not all virtual pages will be in physical memory
• Later: might not have enough physical memory to map
all virtual pages
Page Permissions
• R/W/X permission bits for each PTE
• Code: read-only, executable
• Data: writeable, not executable
34
Less Simple Page Table
Physical Page
V R W X Number
0 0xC20A3000
1 1 1 0 0xC20A3 r t ual
ra l vi e
0 s eve l pag
0 p i ng ys i ca
: m ap e ph
1 1 0 0 0xC20A3 s i ng
s am 0x90000000
i a
A l es s es
1 0x4123B r
add
1 0x10044
0
0x4123B000
Process tries to access a page without
proper permissions 0x10045000
Segmentation Fault
0x10044000
Example:
Write to read-only? process killed 0x00000000 35
Now how big is this Page Table?
struct pte_t page_table[220]
Each PTE = 8 bytes
How many pages in memory will the page table take
up?
36
Assuming each page = 4KB
Now how big is this Page Table?
struct pte_t page_table[220]
Each PTE = 8 bytes
How many pages in memory will the page table take
up?
37
Assuming each page = 4KB
Wait, how big is this Page Table?
page_table[220] = 8x220 =223 bytes
(Page Table = 8 MB in size)
How many pages in memory will the page table take up?
223 /212 =211 2K pages!
is m y
h er e
W ica l
phy s
?
p age Total size = 220 * 4 bytes
= 4MB
PTEntry
PPN
PTBR
Page Table
42
Multi-Level Page Table
10 bits 10 bits 12 bits vaddr
31 22 21 12 11 0
is m y
h er e
W ica l
phy s
?
e r e is m y page
W h n ?
s la ti o
tr a n PTEntry
PPN
PDEntry
Page Table
Also referred to as
PTBR
Page Directory Level 1 and Level 2
* Indirection to the Rescue, AGAIN! Page Tables43
Multi-Level Page Table
10 bits 10 bits 12 bits vaddr
31 22 21 12 11 0
Assuming each entry is
is y
m 4bytes,What is the size of
r e
Whe ical Page Directory?
phys
? A: 2KB B: 2MB
e r e is m y page
W h n ? C: 4KB D: 4MB
s la ti o
tr a n PTEntry
PPN
PDEntry
Page Table
Also referred to as
PTBR
Page Directory Level 1 and Level 2
* Indirection to the Rescue, AGAIN! Page Tables44
Multi-Level Page Table
10 bits 10 bits 12 bits vaddr
31 22 21 12 11 0
PDEntry
Page Table
Also referred to as
PTBR
Page Directory Level 1 and Level 2
* Indirection to the Rescue, AGAIN! Page Tables45
Multi-Level Page Table
10 bits 10 bits 12 bits vaddr
31 22 21 12 11 0
is m y
h er e
W ica l
phy s
?
e r e is m y page
W h n ?
s la ti o
tr a n PTEntry
PPN
PDEntry
Page Table
PTBR Size = 210 * 210 *4 bytes = 4MB
Page Directory # entries per page table
# page tables
Size = 210 * 4 bytes = 4KB 46
Multi-Level Page Table
Doesn’t this take up more memory than before?
- YES, but..
Benefits
• Don’t need 4MB contiguous physical memory
• Don’t need to allocate every PageTable, only
those containing valid PTEs
Drawbacks
• Performance: Longer lookups
47
Virtual Memory Agenda
What is Virtual Memory?
How does Virtual memory Work?
• Address Translation
• Overhead
• Paging
• Performance
48
Paging
What if process requirements > physical memory?
Virtual starts earning its name
More Meta-Data:
• Dirty Bit, Recently Used, etc.
• OS may access this meta-data to choose a victim 49
Paging
Physical Page
V RWX D Number 0xC20A3000
0 --
1 1 0 1 0 0x10045
0 -- 0x90000000
0 --
0 0 disk sector 200 0x4123B000
0 0 disk sector 25
1 1 1 0 1 0x00000
0x10045000
0 --
0x00000000
Example: accessing address beginning
with 0x00003 (PageTable[3]) results in
a Page Fault which will page the data 200
in from disk sector 200 25
50
Page Fault
Valid bit in Page Table = 0
means page is not in memory
OS takes over:
• Choose a physical page to replace
“Working set”: refined LRU, tracks page usage
• If dirty, write to disk
• Read missing page from disk
Takes so long (~10ms), OS schedules another task
52
Watch Your Performance Tank!
For every instruction:
• MMU translates address (virtual physical)
Uses PTBR to find Page Table in memory
Looks up entry for that virtual page
• Fetch the instruction using physical address
Access Memory Hierarchy (I$ L2 Memory)
53
Performance
• Virtual Memory Summary
• PageTable for each process:
Page
• Single-level (e.g. 4MB contiguous in physical memory)
• or multi-level (e.g. less mem overhead due to page table),
•…
every load/store translated to physical addresses
page table miss: load a swapped-out page and retry
instruction, or kill program
• Performance?
terrible: memory is already slow
translation makes it slower
• Solution?
Next Goal
• How do we speedup address translation?
Translation Lookaside Buffer (TLB)
• Small, fast cache
• Holds VPNPPN translations
• Exploits temporal locality in pagetable
• TLB Hit: huge performance savings
• TLB Miss: invoke TLB miss handler
• Put translation in TLB for later
CPU VA
“tag” “data”
VPN PPN
VA VPN PPN
VA VPN PPN
MMU TLB
PA PA
56
TLB Parameters
Typical
• very small (64 – 256 entries) very fast
• fully associative, or at least set associative
57
TLB to the Rescue!
For every instruction:
• Translate the address (virtual physical)
CPU checks TLB
That failing, walk the Page Table
• Use PTBR to find Page Table in memory
• Look up entry for that virtual page
• Cache the result in the TLB
• Fetch the instruction using physical address
Access Memory Hierarchy (I$ L2 Memory)
no
DRAM
Access
A page is constant size block of virtual memory. Often ~4KB to reduce the
number of entries in a PageTable.