Lecture 22
Lecture 22
This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under
Creative Commons Attribution 2.5 License. All rights reserved.
Based on slides and notes created by John Ousterhout, Jerry Cain, Chris Gregg, and others.
NOTICE RE UPLOADING TO WEBSITES: This content is protected and may not be shared, 1
uploaded, or distributed. (without expressed written permission)
CS111 Topic 4: Virtual Memory
Virtual Memory - How can one set of memory be shared among several processes?
How can the operating system manage access to a limited amount of system
memory?
Virtual Dynamic
Demand The Clock
Memory Address
Paging Algorithm
Introduction Translation
Lecture 21 Today Lecture 23 Lecture 24
3
Plan For Today
• Recap: virtual memory and dynamic address translation
• Approach #2: Multiple Segments
• Approach #3: Paging
4
Plan For Today
• Recap: virtual memory and dynamic address translation
• Approach #2: Multiple Segments
• Approach #3: Paging
5
Virtual memory is a
mechanism for multiple
processes to
simultaneously use system
memory.
6
Sharing Memory
We want to allow multiple processes to simultaneously use system memory.
Our goals are:
• Multitasking – allow multiple processes to be memory-resident at once
• Transparency – no process should need to know memory is shared. Each
must run regardless of the number and/or locations of processes in memory.
• Isolation – processes must not be able to corrupt each other
• Efficiency (both of CPU and memory) – shouldn’t be degraded badly by sharing
7
Load-Time Relocation
• When a process is loaded to run, place it in a
designated memory space. ∞
Operating
• That memory space is for everything for that process – System
stack/data/code
• Interesting fact – when a program is compiled, it is Process 3
compiled assuming its memory starts at address 0.
Therefore, we must update its addresses when we load
it to match its real starting address. Process 6
• Use first-fit or best-fit allocation to manage available
memory. Process 1
data 10
Dynamic Address Translation
Key question: how do the MMU / OS translate from virtual addresses to physical
ones? Three designs we’ll consider:
1. Base and bound
2. Multiple Segments
3. Paging
11
Approach #1: Base and Bound
• “base” is physical address starting point – corresponds to virtual address 0
• “bound” is one greater than highest allowable virtual memory address
• Each process has own base/bound. Stored in PCB and loaded into two
registers when running.
12
Approach #1: Base and Bound
• Key idea: each process appears to have a completely private memory whose
size is determined by the bound register.
• The only physical address is in the base register, controlled by the OS. Process
sees only virtual addresses!
• OS can update a process’s base/bound if needed! E.g. it could move physical
memory to a new location or increase bound.
• Benefits: inexpensive, little space needed, separation between virtual and
physical addresses.
• Drawbacks: physical space must be contiguous, fragmentation, growth only
upwards, no read-only region support
13
Base and Bound – Changing Bound
∞
bound
Stack
Process A
Data
Code
0 Process A base 0
Process A Virtual
Address Space Physical Address Space
14
Base and Bound – Changing Bound
∞
bound
Stack
Process A
Data
Code
0 Process A base 0
Process A Virtual
Address Space Physical Address Space
15
Base and Bound
One thought: Can we remove the ∞
requirement that the virtual address
space must be mapped
bound contiguously? Can we avoid
Stack mapping the unused space between
the stack and heap?
Process A
Data
Process A base
Code
0 0
Process A Virtual
Address Space Physical Address Space
16
Plan For Today
• Recap: virtual memory and dynamic address translation
• Approach #2: Multiple Segments
• Approach #3: Paging
17
Idea: what if we broke up
the virtual address space
into segments and mapped
each segment
independently?
18
Approach #2: Multiple Segments
Key Idea: Each process is split among several variable-size areas of memory,
called segments.
• E.g. one segment for code, one segment for data/heap, one segment for stack.
• The OS maps each segment individually – each segment would have its own
base and bound, and these are stored in a segment map for that process
• We can also store a protection bit for each segment; whether the process is
allowed to write to it or not in addition to reading
• Now each segment can have its own permissions, grow/shrink independently,
be swapped to disk independently, be moved independently, and even be
shared between processes (e.g. shared code).
19
Multiple Segments
∞ Stack
∞
Data
Code
0
Process A Virtual Address Space
∞ Stack
0
Data
Code
0
Process B Virtual Address Space Physical Address Space
20
Approach #2: Multiple Segments
On each memory reference:
• Look up info for the segment that address is in
• Compare virtual address to that segment’s bound, trap if >= (invalid memory
reference)
• Add segment’s base to virtual address to produce physical address
Segment # Offset
0x122 0x456
Virtual Address
Example: PDP-10 computer had design with 2 segments, and the most-
significant bit in addresses encoded which one was being referenced.
Another possibility: deduce from machine code instruction executing
22
Multiple Segments
• Do not need to initially map full ∞
Stack virtual address space, nor map it
contiguously.
• Instead, individually/contiguously
map each segment.
• Move an individual segment in
physical memory by modifying its
Data base (pinned to that segment’s
offset 0)
Code • Expand an individual segment’s
size by adjusting its bound.
0 0
Process A Virtual
Address Space Physical Address Space
23
Multiple Segments – Changing A Base
∞
Stack
data
bound
Data
0 0
Process A Virtual
Address Space Physical Address Space
24
Multiple Segments – Changing A Base
∞
Stack
data
bound
Data
Code
Process A data base
0 0
Process A Virtual
Address Space Physical Address Space
25
Multiple Segments – Changing A Bound
∞
Stack
data
bound
Data
Code
Process A data base
0 0
Process A Virtual
Address Space Physical Address Space
26
Approach #2: Multiple Segments
What are some benefits of this approach?
• Can move segments to compact memory and eliminate fragmentation
• Flexibility – can manage each segment independently
• Can share segments between processes
Code
Process A data base
0 0
Process A Virtual
Address Space Physical Address Space
28
Idea: what if we broke up
the virtual address space
not into variable-length
segments, but into fixed-
size chunks?
29
Plan For Today
• Recap: virtual memory and dynamic address translation
• Approach #2: Multiple Segments
• Approach #3: Paging
30
Approach #3: Paging
Key Idea: Each process’s virtual (and physical) memory is divided into fixed-size
chunks called pages. (Common size is 4KB pages).
• A “page” of virtual memory maps to a “page” of physical memory. No partial
pages
• The page number is a numerical ID for a page. We have virtual page numbers
and physical page numbers.
• A virtual address is comprised of the virtual page # and offset in that page.
• A physical address is comprised of the physical page # and offset in that page.
31
Paging
∞ Stack ∞
Data
Code
0 0
Process A Virtual
Address Space Physical Address Space
32
Paging
∞ Stack ∞
Data
Code
0 0
Process A Virtual
Address Space Physical Address Space
33
Paging
∞ Stack ∞
Data
Code
0
Process A Virtual Address Space
∞ Stack
Data
0 Code Physical Address Space
Process B Virtual Address Space 34
Paging
∞ • Do not need to map each ∞
Stack segment contiguously. Instead,
we map just one page at a time.
• We can later map more pages
either up or down, because the
start of the segment is not
pinned to a physical address.
Data • We can move each page
separately in physical memory as
Code well.
0 0
Process A Virtual
Address Space Physical Address Space
35
Paging
∞ Stack ∞
Data
Code
0 0
Process A Virtual
Address Space Physical Address Space
36
Paging
∞ ∞
Stack
Data
Code
0 0
Process A Virtual
Address Space Physical Address Space
37
Page Maps
How do we track, for a given process, which virtual page maps to which
physical page?
Each process has a page map (“page table”) with an entry for each virtual page,
mapping it to a physical page number and other info such as a protection bit
(read-only or read-write).
The page map is stored in contiguous memory.
38
Page Map
Index Physical page # Writeable?
… … …
3 0x2342 1
2 0x12625 1
1 0x13241 0
0 0x256 0
39
Page Map
Index Physical page # Writeable?
… … …
3 0x2342 1
2 0x12625 1
1 0x13241 0
0 0x256 0
12 bits 12 bits
Virtual page # Offset Physical page # Offset
Virtual Address Physical Address
40
Virtual Address Encodes Page + Offset
Key idea: if you pick a
page size that is a power
of the base, the upper 2000-2999
digits identify the page
#.
0000-0999
0
41
Virtual Address Encodes Page + Offset
Key idea: if you pick a
page size that is a power
of the base, the upper 0x2000-0x2fff
digits identify the page
#.
43
Page Map
Physical page # WR?
∞ Stack 12 3 1 ∞
11 X X
10 X X
9 X X
8 X X
7 X X
6 X X
Data
5 X X
4 2 1
Code
0 3 9 0 0
2 1 0
Process A Virtual 1 7 0
Address Space 0 5 0 Physical Address Space
44
Data
Page Map
Code Physical page # WR?
0 12 3 1 0
11 X X
Process A Virtual 10 X X
Address Space Physical Address Space
9 X X
8 X X
7 X X
6 X X
5 X X
4 2 1
3 9 0
2 1 0
1 7 0
0 5 0 45
Data
Page Map
Code Physical page # WR?
0 12 3 1 0
11 X X
Process A Virtual 10 X X
Address Space Physical Address Space
9 X X
8 X X
7 X X
6 X X
5 X X
4 2 1
3 9 0
2 1 0
1 7 0
0 5 0 46
Data
Page Map
Code Physical page # WR?
0 12 3 1 0
11 X X
Process A Virtual 10 X X
Address Space Physical Address Space
9 X X
8 X X
7 X X
6 X X
5 X X
4 2 1
3 9 0
2 1 0
Virtual Address 1 7 0
0x2223 0 5 0 47
Data
Page Map
Code Physical page # WR?
0 12 3 1 0
11 X X
Process A Virtual 10 X X
Address Space Physical Address Space
9 X X
8 X X
7 X X
6 X X
5 X X
4 2 1
Virtual page # Offset
3 9 0
0x2 0x223 2 1 0
Virtual Address 1 7 0
0x2223 0 5 0 48
Data
Page Map
Code Physical page # WR?
0 12 3 1 0
11 X X
Process A Virtual 10 X X
Address Space Physical Address Space
9 X X
8 X X
7 X X
6 X X
5 X X
4 2 1 Offset
Virtual page # Offset Physical page #
3 9 0
0x2 0x223 ??? ???
2 1 0
Virtual Address 1 7 0 Physical Address
0x2223 0 5 0 ??? 49
Data
Page Map
Code Physical page # WR?
0 12 3 1 0
11 X X
Process A Virtual 10 X X
Address Space Physical Address Space
9 X X
8 X X
7 X X
6 X X
5 X X
4 2 1 Offset
Virtual page # Offset Physical page #
3 9 0
0x2 0x223 0x1 ???
2 1 0
Virtual Address 1 7 0 Physical Address
0x2223 0 5 0 ??? 50
Data
Page Map
Code Physical page # WR?
0 12 3 1 0
11 X X
Process A Virtual 10 X X
Address Space Physical Address Space
9 X X
8 X X
7 X X
6 X X
5 X X
4 2 1 Offset
Virtual page # Offset Physical page #
3 9 0
0x2 0x223 0x1 0x223
2 1 0
Virtual Address 1 7 0 Physical Address
0x2223 0 5 0 ??? 51
Data
Page Map
Code Physical page # WR?
0 12 3 1 0
11 X X
Process A Virtual 10 X X
Address Space Physical Address Space
9 X X
8 X X
7 X X
6 X X
5 X X
4 2 1 Offset
Virtual page # Offset Physical page #
3 9 0
0x2 0x223 0x1 0x223
2 1 0
Virtual Address 1 7 0 Physical Address
0x2223 0 5 0 0x1223 52
Page Map
Index Physical page # Writeable?
… … …
3 0x2342 1
2 0x12625 1
1 0x13241 0
0 0x256 0
unused (16 bits) Virtual page # (36 bits) Offset (12 bits) Physical page # (40 bits) Offset (12 bits)
x86-64 64-bit Virtual Address x86-64 52-bit Physical Address
x86-64 with 4KB pages has 36-bit virtual page numbers and 40-bit physical page numbers. 61
Each Process Has A Page Map
∞
Physic al pa ge # W R? PR?
Stack 12
11
3
X
1
X
1
0
∞
10 X X 0
9 X X 0
8 X X 0
7 X X 0
6 X X 0
5 X X 0
4 2 1 1
Data 3
2
9
1
0
0
1
Code 1
0
7
5
0
0
1
0
Process A Virtual Address Space
∞ Stack
12
Physic al pa ge #
10
W R?
1
PR?
11 X X 0
10 X X 0
9 X X 0
8 X X 0
6
X
X
X
X
0
0
0
5 X X 0
4 X X 0
3 X X 0
2 X X 0
Data
Physical Address Space
1 8 1 1
0 Code 0 12 0 1
63
Requesting More Memory
∞ Stack ∞
Data
Code
0 0
Process A Virtual
Address Space Physical Address Space
64
Requesting More Memory
∞ ∞
Stack
Data
Code
0 0
Process A Virtual
Address Space Physical Address Space
65
Paging
Key Idea: Each process’s virtual (and physical) memory is divided into fixed-size
chunks called pages. (Common size is 4KB pages).
• A “page” of virtual memory maps to a “page” of physical memory. No partial
pages. No more external fragmentation! (but some internal fragmentation if
not all of a page is used).
• The page number is a numerical ID for a page. We have virtual page numbers
and physical page numbers.
• Each process has a page map (“page table”) with an entry for each virtual
page, mapping it to a physical page number and other info such as a protection
bit (read-only or read-write).
• A memory address can tell us the page number and offset within that page.
• Not all pages are mapped – unmapped pages cannot be accessed
66
Paging
On each memory reference:
• Look up info for that virtual page in the page map
• If it’s a valid virtual page number, get the physical page number it maps to, and
combine it with the specified offset to produce the physical address.
67
Recap
• Recap: virtual memory and dynamic Lecture 22 takeaway:
address translation
Dynamic Address translation
• Approach #2: Multiple Segments
means that the OS intercepts
• Approach #3: Paging and translates each memory
access. Initial approaches to
this include base+bound per
process, or expanding that to
Next time: demand paging be base+bound per variable-
length segment, or instead
dividing into fixed-size pages.
68