Virtual Memory for Programmers
Virtual Memory for Programmers
SYSTEMS PROGRAMMING
VIRTUAL MEMORIES
(based on chapter 9)
…
Exit() Load
Program- Print() necessary Sub()
Calculator Main() pages only
Print()
…
Page V
(empty)
Page v
Logical Memory Physical Memory
Disk (=Virtual memory) (=Main memory)
o Issues:
• How to allocate and recognize pages: address
• How to discover pages in main memory: page table
Computer Systems: A Programmer’s Perspective 3rd Edition 4
AGENDA
oAddress spaces
oVM as a tool for caching
oVM as a tool for memory management and protection
oAddress translation
oMemory Mapping
...
M-1:
Data word
...
M-1:
Data word
o Consequences
• Large page (block) size: typically 4 KB, sometimes 4 MB
• Fully associative
Any VP can be placed in any PP
Requires a “large” mapping function – different from cache memories
• Highly sophisticated, expensive replacement algorithms
Too complicated and open-ended to be implemented in hardware
• Write-back rather than write-through
0 Address 0
Virtual Physical
Address Space
translation Address
VP 1
for Process 1: VP 2 PP 2
Space
... (DRAM)
N-1
(e.g., read-only
PP 6
• Each virtual page can be mapped to library code)
0 Address 0
Virtual Physical
Address Space
translation Address
VP 1
for Process 1: VP 2 PP 2
Space
... (DRAM)
N-1
(e.g., read-only
PP 6
library code)
0
Virtual
PP 8
Address Space VP 1
for Process 2: VP 2
... ...
N-1 M-1
Computer Systems: A Programmer’s Perspective 3rd Edition 25
SIMPLIFYING LINKING AND LOADING
oLinking
• Each program has similar virtual Kernel virtual memory
Memory
invisible to
address space User stack user code
• Code, data, and heap always start (created at runtime)
%rsp
at the same addresses. (stack
pointer)
•
shared libraries
execve allocates virtual pages
for .text and .data sections &
creates PTEs marked as invalid
brk
• The .text and .data sections Run-time heap
(created by malloc)
are copied, page by page, on
Read/write segment
demand by the virtual memory (.data, .bss) Loaded from
system Read-only segment the executable
(.init, .text, .rodata) file
0x400000
Unused
0
• M=P P = 2n-p
𝑃 = {0, 1, … , 𝑀– 1}
o Address Translation
• 𝑴𝑨𝑷: 𝑽 → 𝑷 𝑼 {∅}
• For virtual address a:
𝑴𝑨𝑷(𝒂) = 𝒂’ if data at virtual address a is at physical address a’ in P
𝑴𝑨𝑷(𝒂) = ∅ if data at virtual address a is not in physical memory
Either invalid or stored on disk
Page table
Valid Physical page number (PPN)
Physical page table
address for the current
process
Valid bit = 0:
Page not in memory
Valid bit = 1
(page fault)
m-1 p p-1 0
Physical page number (PPN) Physical page offset (PPO)
Physical address
Data
5
2
CPU Chip PTEA Victim page
1
5
VA PTE Cache/
CPU MMU Disk
7 3 Memory
New page
6
PTE
PA Data
hit
Data
CPU Chip
TLB
2 PTE
VPN 3
1
VA PA
CPU MMU
4 Cache/
Memory
Data
5
CPU Chip
TLB
4
2 PTE
VPN
1 3
VA PTEA
CPU MMU
Cache/
PA Memory
5
Data
6
...
oExample: 2-level page table
• Level 1 table: each PTE points to a page table
...
(always memory resident)
• Level 2 table: each PTE points to a page
(paged in and out like any other data)
PTE 0 ...
PTE 0
... VP 1023 2K allocated VM pages
PTE 1 for code and data
PTE 1023 VP 1024
PTE 2 (null)
...
PTE 3 (null)
VP 2047
PTE 4 (null) PTE 0
PTE 5 (null) ...
PTE 6 (null) PTE 1023
PTE 7 (null) Gap 6K unallocated VM pages
PTE 8
1023 null
(1K - 9) PTEs
null PTEs PTE 1023 1023
unallocated 1023 unallocated pages
pages
VP 9215 1 allocated VM page
32 bit addresses, 4KB pages, 4-byte PTEs for the stack
...
Computer Systems: A Programmer’s Perspective 3rd Edition 40
TRANSLATING WITH A K-LEVEL PAGE TABLE
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
oDirty pages are copied back and forth between memory and
a special swap file.
Shared
object
Shared
object
Private
copy-on-write object
o Instruction writing to
Process 1 Physical Process 2
virtual memory memory virtual memory
private page triggers
protection fault.
Copy-on-write
• Handler creates new R/W
page.
• Instruction restarts upon
handler return.
Write to private
copy-on-write
page
o Copying deferred as long
as possible!
Private
copy-on-write object
o Create vm_area_struct’s
and page tables for new areas
Runtime heap (via malloc) Private, demand-zero • Programs and initialized data
backed by object files.
a.out
Uninitialized data (.bss) Private, demand-zero
• .bss and stack backed by
.data Initialized data (.data) anonymous files .
Private, file-backed
.text Program text (.text) o Set PC to entry point in .text
0 • Linux will fault in code and
data pages as needed.
len bytes
start
(or address
len bytes chosen by kernel)
offset
(bytes)
0 0
Disk file specified by Process virtual memory
file descriptor fd
Computer Systems: A Programmer’s Perspective 3rd Edition 51
USING MMAP TO COPY FILES