Professional Documents
Culture Documents
Virtual Memory
Virtual Memory
80 GB: ~$110
1GB: ~$200
4 MB: ~$500
3
Levels in Memory Hierarchy
cache virtual memory
C
CPU 8B a 32 B 4 KB
Memory disk
c
regs h
e DRAM
SRAM
Cache Cache
Register Cache Memory Disk Memory
size: 32 B 32 KB-4MB 1024 MB 100 GB
speed: 1 ns 2 ns 30 ns 8 ms
$/Mbyte: $125/MB $0.20/MB $0.001/MB
line size: 8B 32 B 4 KB
10X •100,000X slower
slower DRAM/disk is
•First byte is the extreme of
~100,000X slower
SRAM/DRAM
than successive
bytes on disk relation
5
Locating an Object in a “Cache”
SRAM Cache
– Tag stored with cache line
– No tag for block not in cache
– Hardware retrieves information
• can quickly match against multiple tags
“Cache”
Tag Data
6
Locating an Object in “Cache” (cont.)
DRAM Cache
– Each allocated page of virtual memory has entry in page table
– Page table entry even if page not in memory
• Specifies disk address
• Only way to indicate where to find page
– HW/OS retrieves information
CPU
N-1:
0:
Page Table 1:
Virtual Physical
Addresses 0: Addresses
1:
CPU
P-1:
N-1:
Disk
Disk
Disk
10
Servicing a Page Fault
(1) Initiate Block Read
Processor Signals Controller Processor
– Read block of length P starting Reg
(3) Read
at disk address X and store Done
starting at memory address Y
Cache
Read Occurs
– Direct Memory Access (DMA)
– Under control of I/O controller Memory-I/O bus
11
Motivation #2: Memory Management
Multiple processes can reside in physical memory.
How do we resolve address conflicts?
– what if two processes access something at the same
address?
memory invisible to
kernel virtual memory
user code
%esp stack
0
Virtual 0 Address Translation Physical
Address VP 1 PP 2 Address
Space for VP 2 Space
... (DRAM)
Process 1:
N-1
(e.g., read/only
PP 7
library code)
Virtual 0
VP 1
Address PP 10
VP 2
Space for ...
Process 2: M-1
N-1
13
Motivation #3: Protection
Page table entry contains access rights information
– hardware enforces this protection (trap into OS if violation oc
curs) Page Tables Memory
Read? Write? Physical Addr 0:
VP 0: Yes No PP 9 1:
Process i: VP 1: Yes Yes PP 4
VP 2: No No XXXXXXX
• • •
• • •
• • •
15
VM Address Translation: Hit
Processor
Hardware
Addr Trans Main
a Mechanism Memory
a'
16
VM Address Translation: Miss
page fault
fault
Processor handler
Hardware
Addr Trans Main Secondary
a Mechanism Memory memory
a'
OS performs
virtual address part of the physical address this transfer
on-chip (only if miss)
memory mgmt unit (MMU)
17
VM Address Translation
Parameters
– P = 2p = page size (bytes).
– N = 2n = Virtual address limit
– M = 2m = Physical address limit
n–1 p p–1 0
virtual page number page offset virtual address
address translation
m–1 p p–1 0
physical page number page offset physical address
19
Address Translation via Page Table
if valid=0
then page
not in memory m–1 p p–1 0
physical page number (PPN) page offset
physical address
20
Page Table Operation
Translation
– Separate (set of) page table(s) per process
– VPN forms index into page table (points to a page table entry)
if valid=0
then page
not in memory m–1 p p–1 0
physical page number (PPN) page offset
physical address
21
Page Table Operation
Computing Physical Address
– Page Table Entry (PTE) provides information about page
• if (valid bit = 1) then the page is in memory.
– Use physical page number (PPN) to construct address
• if (valid bit = 0) then the page is on disk
– Page fault
if valid=0
then page
not in memory m–1 p p–1 0
physical page number (PPN) page offset
physical address 22
Page Table Operation
Checking Protection
– Access rights field indicate allowable access
• e.g., read-only, read-write, execute-only
• typically support multiple protection modes (e.g., kernel vs. user)
– Protection violation fault if user doesn’t have necessary
permission
page table base register virtual address
n–1 p p–1 0
VPN acts virtual page number (VPN) page offset
as
table index
valid access physical page number (PPN)
if valid=0
then page
not in memory m–1 p p–1 0
physical page number (PPN) page offset
physical address 23
Integrating VM and Cache
VA PA miss
Trans- Main
CPU Cache
lation Memory
hit
data
24
Speeding up Translation with a TLB
“Translation Lookaside Buffer” (TLB)
– Small hardware cache in MMU
– Maps virtual page numbers to physical page numbers
– Contains complete page table entries for small number of
pages
hit
VA PA miss
TLB Main
CPU Cache
Lookup Memory
miss hit
Trans-
lation
data
25
Address Translation with a TLB
n–1 p p–1 0
virtual page number page offset virtual address
TLB hit
physical address
26
Simple Memory System Example
Addressing
– 14-bit virtual addresses
– 12-bit physical address
– Page size = 64 bytes
13 12 11 10 9 8 7 6 5 4 3 2 1 0
VPN VPO
(Virtual Page Number) (Virtual Page Offset)
11 10 9 8 7 6 5 4 3 2 1 0
PPN PPO
(Physical Page Number) (Physical Page Offset)
27
Simple Memory System Page Table
– Only show first 16 entries
28
Simple Memory System TLB
TLB
– 16 entries
– 4-way associative
TLBT TLBI
13 12 11 10 9 8 7 6 5 4 3 2 1 0
VPN VPO
Set Tag PPN Valid Tag PPN Valid Tag PPN Valid Tag PPN Valid
0 03 – 0 09 0D 1 00 – 0 07 02 1
1 03 2D 1 02 – 0 04 – 0 0A – 0
2 02 – 0 08 – 0 06 – 0 03 – 0
3 07 – 0 03 0D 1 0A 34 1 02 – 0
29
Simple Memory System Cache
Cache
– 16 lines
– 4-byte line size
– Direct mapped
CT CI CO
11 10 9 8 7 6 5 4 3 2 1 0
PPN PPO
Idx Tag Valid B0 B1 B2 B3 Idx Tag Valid B0 B1 B2 B3
0 19 1 99 11 23 11 8 24 1 3A 00 51 89
1 15 0 – – – – 9 2D 0 – – – –
2 1B 1 00 02 04 08 A 2D 1 93 15 DA 3B
3 36 0 – – – – B 0B 0 – – – –
4 32 1 43 6D 8F 09 C 12 0 – – – –
5 0D 1 36 72 F0 1D D 16 1 04 96 34 15
6 31 0 – – – – E 13 1 83 77 1B D3
7 16 1 11 C2 DF 03 F 14 0 – – – –
30
Address Translation Example #1
Virtual Address 0x03D4
TLBT TLBI
13 12 11 10 9 8 7 6 5 4 3 2 1 0
VPN VPO
VPN ___ TLBI ___ TLBT ____ TLB Hit? __ Page Fault? __ PPN: ____
Physical Address
CT CI CO
11 10 9 8 7 6 5 4 3 2 1 0
PPN PPO
Offset ___ CI___ CT ____ Hit? __ Byte: ____
31
Address Translation Example #1
Virtual Address 0x03D4
TLBT TLBI
13 12 11 10 9 8 7 6 5 4 3 2 1 0
0 0 0 0 1 1 1 1 0 1 0 1 0 0
VPN VPO
0F
VPN ___ 3 TLBT ____
TLBI ___ 03 h
TLB Hit? __ Page Fault? no 0D
__ PPN: ____
Physical Address
CT CI CO
11 10 9 8 7 6 5 4 3 2 1 0
0 0 1 1 0 1 0 1 0 1 0 0
PPN PPO
00
Offset ___ 5
CI___ 0D
CT ____ h
Hit? __ 36
Byte: ____
32
Address Translation Example #2
Virtual Address 0x038F
TLBT TLBI
13 12 11 10 9 8 7 6 5 4 3 2 1 0
VPN VPO
VPN ___ TLBI ___ TLBT ____ TLB Hit? __ Page Fault? __ PPN: ____
Physical Address
CT CI CO
11 10 9 8 7 6 5 4 3 2 1 0
PPN PPO
Offset ___ CI___ CT ____ Hit? __ Byte: ____
33
Address Translation Example #2
Virtual Address 0x038F
TLBT TLBI
13 12 11 10 9 8 7 6 5 4 3 2 1 0
0 0 0 0 1 1 1 0 0 0 1 1 1 1
VPN VPO
0E
VPN ___ 03
2 TLBT ____
TLBI ___ TLB Hit?m
__ Page Fault? no 11
__ PPN: ____
Physical Address
CT CI CO
11 10 9 8 7 6 5 4 3 2 1 0
0 1 0 0 0 1 0 0 1 1 1 1
PPN PPO
Offset 11
___ 3
CI___ 11
CT ____ Hit?m
__ in memory
Byte: ____
34
Address Translation Example #3
Virtual Address 0x0040
TLBT TLBI
13 12 11 10 9 8 7 6 5 4 3 2 1 0
VPN VPO
VPN ___ TLBI ___ TLBT ____ TLB Hit? __ Page Fault? __ PPN: ____
Physical Address
CT CI CO
11 10 9 8 7 6 5 4 3 2 1 0
PPN PPO
Offset ___ CI___ CT ____ Hit? __ Byte: ____
35
Address Translation Example #3
Virtual Address 0x0040
TLBT TLBI
13 12 11 10 9 8 7 6 5 4 3 2 1 0
0 0 0 0 0 0 0 1 0 0 0 0 0 0
VPN VPO
01
VPN ___ 00
1 TLBT ____
TLBI ___ TLB Hit?m
__ Page Fault? yes ??
__ PPN: ____
Physical Address
CT CI CO
11 10 9 8 7 6 5 4 3 2 1 0
PPN PPO
Offset ___ CI___ CT ____ Hit? __ Byte: ____
36
Multi-Level Page Tables
Given: Level 2
– 4KB (212) page size Tables
– 32-bit address space
– 4-byte PTE
Problem: Level 1
– Would need a 4 MB page table! Table
• 220 *4 bytes
Common solution
– multi-level page tables
– e.g., 2-level table (P6) ...
• Level 1 table: 1024 entries, each of
which points to a Level 2 page table.
• Level 2 table: 1024 entries, each of
which points to a page
37
Summary: Main Themes
Programmer’s View
– Large “flat” address space
• Can allocate large blocks of contiguous addresses
– Processor “owns” machine
• Has private address space
• Unaffected by behavior of other processes
System View
– User virtual address space created by mapping to set of
pages
• Need not be contiguous
• Allocated dynamically
• Enforce protection during address translation
– OS manages many processes simultaneously
• Continually switching among processes
• Especially when one must wait for resource
– E.g., disk I/O to handle page fault
38
Case Study:
The Pentium/Linux Memory system
39
P6 Memory System 32 bit address space
4 KB page size
L1, L2, and TLBs
DRAM 4-way set
associative
external
system bus inst TLB
(e.g. PCI) 32 entries
L2 8 sets
cache data TLB
cache bus 64 entries
16 sets
inst
bus interface unit
TLB L1 i-cache and d-cache
16 KB
data
32 Byte line size
instruction L1 TLB
128 sets
fetch unit i-cache L1
d-cache L2 cache
unified
processor package 128 KB -- 2 MB
40
Review of Abbreviations
Symbols:
– Components of the virtual address (VA)
• TLBI: TLB index
• TLBT: TLB tag
• VPO: virtual page offset
• VPN: virtual page number
– Components of the physical address (PA)
• PPO: physical page offset (same as VPO)
• PPN: physical page number
• CO: byte offset within cache line
• CI: cache index
• CT: cache tag
41
Overview of P6 Address Translation
CPU 32
result L2 and DRAM
20 12
virtual address (VA)
VPN VPO L1 L1
hit miss
16 4
TLBT TLBI data TLB
43
P6 Page Directory Entry (PDE)
31 12 11 9 8 7 6 5 4 3 2 1 0
Page table physical base addr Avail G PS A CD WT U/S R/W P=1
44
P6 Page Table Entry (PTE)
31 12 11 9 8 7 6 5 4 3 2 1 0
Page physical base address Avail G 0 D A CD WT U/S R/W P=1
45
How P6 Page Tables Map Virtual
Addresses to Physical Ones
10 10 12
VPN1 VPN2 VPO Virtual address
word offset into word offset into word offset into
page directory page table physical and virtual
page
PTE physical
PDE address
of page base
PDBR (if P=1)
physical address physical address
of page directory of page table base
(if P=1)
20 12
PPN PPO Physical address
46
Representation of Virtual Address Space
P=1, M=1 • Page 15
PT 3 P=0, M=0 •
•
Page 14
P=1, M=1
P=0, M=1 • Page 13
•
Page Directory Page 12
P=1, M=1
P=1, M=1 • P=0, M=0 • Page 11
P=1, M=1 • PT 2 P=1, M=1 • Page 10
P=0, M=0 • P=0, M=1 •
•
Page 9
P=0, M=1
P=0, M=1 • Page 8
PT 0 P=0, M=1 •
•
Page 7
P=0, M=0
P=0, M=0 • Page 6
Page 0 On Disk
– M: Has this part of VA space
been mapped? Unmapped
47
P6 TLB Translation
CPU 32
result L2 andDRAM
20 12
virtual address (VA)
VPN VPO L1 L1
hit miss
16 4
TLBT TLBI
CPU
1. Partition VPN into
TLBT and TLBI.
20 12 virtual address 2. Is the PTE for VPN
VPN VPO cached in set TLBI?
16 4 – 3. Yes: then
TLBT TLBI 1 2 build physical
TLB address.
TLB PDE PTE hit
3 4. No: then read PTE
miss ... 20 12 (and PDE if not
PPN PPO cached) from memory
physical and build physical
page table translation address address.
4
50
P6 page table translation
CPU 32
result L2 andDRAM
20 12
virtual address (VA)
VPN VPO L1 L1
hit miss
16 4
TLBT TLBI
52
Translating with the P6 Page Tables
(case 1/0)
20 12 Case 1/0: page
VPN VPO
table present but
page missing.
VPN1 VPN2 MMU Action:
– page fault exception
– handler receives the
PDE p=1 PTE p=0
Mem following args:
PDBR • VA that caused fault
Page Page • fault caused by
directory table
non-present page
or page-level
data protection violation
Disk • read/write
Data • user/supervisor
page
53
Translating with the P6 Page Tables
(case 1/0, cont)
20 12
OS Action:
VPN VPO – Check for a legal
virtual address.
20 12 – Read PTE through
VPN1 VPN2 PPN PPO
PDE.
– Find free physical
page (swapping out
PDE p=1 PTE p=1 data
Mem current page if
PDBR necessary)
Page Page Data
directory page – Read virtual page from
table
disk and copy to
virtual page
– Restart faulting
Disk
instruction by
returning from
exception handler. 54
Translating with the P6 Page Tables
(case 0/1)
20 12 Case 0/1: page
VPN VPO table missing but
page present.
VPN1 VPN2 Introduces
consistency issue.
– potentially every
PDE p=0 data
page out requires
Mem update of disk page
PDBR table.
Page
directory
Data
page Linux disallows this
– if a page table is
swapped out, then
PTE p=1 swap out its data
Disk pages too.
Page
table
55
Translating with the P6 Page Tables
(case 0/0)
20
VPN
12
VPO
Case 0/0: page
table and page
missing.
VPN1 VPN2
MMU Action:
– page fault
PDE p=0 exception
Mem
PDBR
Page
directory
data
Disk
Data
page
57
P6 L1 Cache Access
CPU 32
result L2 andDRAM
20 12
virtual address (VA)
VPN VPO L1 L1
hit miss
16 4
TLBT TLBI
32 Partition physical
data L2 andDRAM address into CO, CI,
and CT.
L1 L1
miss
Use CT to determine
hit
if line containing
word at address PA is
L1 (128 sets, 4 lines/set) cached in set CI.
... If no: check L2.
If yes: extract word at
byte offset CO and
20 7 5 return to processor.
CT CI CO
physical
address (PA)
59
Speeding Up L1 Access
Tag Check
20 7 5
CT CI CO
Physical address (PA)
PPN PPO
Addr. No
Trans. Change CI
virtual
VPN VPO
address (VA)
20 12
Observation
– Bits that determine CI identical in virtual and physical address
– Can index into cache while address translation taking place
– Then check with CT from physical address
– “Virtually indexed, physically tagged”
– Cache carefully sized to make this possible
60
Linux Organizes VM as Collection of “Areas”
vm_area_struct process virtual memory
task_struct
mm_struct vm_end
mm pgd vm_start
vm_prot
•PID
mmap vm_flags
•ptr to usr
stack shared libraries
•executable vm_next 0x40000000
obj file
name
•PC
vm_end
– pgd: vm_start
• page directory address vm_prot
data
vm_flags
– vm_prot: 0x0804a020
• read/write permissions vm_next
for this area
text
– vm_flags vm_end
• shared with other vm_start 0x08048000
processes or private to vm_prot
this process vm_flags
vm_next 0
61
Linux Page Fault Handling
When Page Hit
process virtual memory – Protection is checked by
vm_area_struct hardware’s page table
retrieval
vm_end
vm_start When Page Fault, page
r/o fault handler checks the
shared libraries following before page-in
vm_next – Is the VA legal?
1
read • i.e. is it in an area
vm_end defined by a
vm_area_struct? (checked
vm_start 3 by page fault handler)
r/w
data read • if not then signal
vm_next segmentation violation
2 (e.g. (1))
write – Is the operation legal?
vm_end text • i.e., can the process
vm_start read/write this area?
r/o • if not then signal
protection violation (e.g.,
vm_next (2))
0
– If OK, handle fault
• e.g., (3) 62
Memory Mapping
Creation of new VM area done via “memory mapping”
– create (1) new vm_area_struct and (2) page tables for area
– area can be backed by (i.e., get its initial values from) :
• regular file on disk (e.g., an executable object file)
– initial page bytes come from a section of a file
• nothing (e.g., bss)
– initial page bytes are zeros
– dirty pages are swapped back and forth between a special
swap file.
Key point: For a new VM area mapping, no virtual
pages are copied into physical memory until they are
referenced!
– known as “demand paging”
– crucial for time and space efficiency
63
Exec() Revisited
process-specific data
To run a new program p
structures in the current process
(page tables, using exec():
task and mm structs)
– free vm_area_struct’s and
same physical memory page tables for old areas.
for each
process
kernel code/data/stack
kernel – create new
0xc0
VM vm_area_struct’s and page
demand-zero
%esp stack tables for new areas.
process
VM • stack, bss, data, text,
shared libs.
Memory mapped region .data
for shared libraries .text
• text and data backed by
ELF executable object file.
libc.so
brk
• bss and stack initialized to
runtime heap (via malloc) zero.
uninitialized data (.bss)
demand-zero – set PC to entry point in
initialized data (.data) .data
.text
program text (.text) .text • Linux will swap in code and
forbidden p data pages as needed.
0
64
Fork() Revisited
To create a new process using fork():
– make copies of the old process’s mm_struct,
vm_area_struct’s, and page tables.
• at this point the two processes are sharing all of their pages.
• How to get separate spaces without copying all the virtual pages
from one space to another?
– “copy on write” technique.
– copy-on-write
• make pages of writeable areas read-only (in page table entry)
• flag vm_area_struct’s for these areas as private “copy-on-write”.
• writes by either process to these pages will cause protection
faults.
– fault handler recognizes copy-on-write, makes a copy of the page,
and restores write permissions.
– Net result:
• copies are deferred until absolutely necessary (i.e., when one of
the processes tries to modify a shared page).
65
User-Level Memory Mapping
void *mmap(void *start, int len,
int prot, int flags, int fd, int offset)
66
mmap() Example: Fast File Copy
#include <unistd.h> int main() {
#include <sys/mman.h> struct stat stat;
#include <sys/types.h> int i, fd, size;
#include <sys/stat.h>
#include <fcntl.h>
char *bufp;