Professional Documents
Culture Documents
CH 09
CH 09
Physical memory
0xFFFFFF OS
0x000000 Application1
0x20000 Application2
Physical memory
0xFFFFFF OS
Operating System Concepts – 10 th Edition 9.16 Silberschatz, Galvin and Gagne
Multiprogramming w/out protection (cont.)
Application
User mode
Application library
Portable OS layer
Kernel mode
Machine-dependent OS layer
Hardware
Operating System Concepts – 10 th Edition 9.18 Silberschatz, Galvin and Gagne
Address translation
Address space: literally, all the addresses a program can
touch. All the state that a program can affect or be affected
by.
Restrict what a program can do by restricting what it can
touch!
Hardware translates every memory reference from virtual
addresses to physical addresses; software sets up and
manages the mapping in the translation box.
Physical
Virtual address Translation address
Box (MMU) Physical
CPU memory
6250
Code
Data
limit
Virtual memory stack
6250 + limit
Physical memory
Operating System Concepts – 10 th Edition 9.53 Silberschatz, Galvin and Gagne
Base and Limit
Provides level of indirection: OS can move bits around behind
the program’s back, for instance, if program needs to grow
beyond its bounds, or if need to coalesce fragments of
memory.
Stop program, copy bits, change base and bounds registers,
restart.
Hardware cost:
2 registers, Adder, Comparator
Plus, slows down hardware because need to take time to
do add/compare on every memory reference.
Cons:
1. Hard to share between programs
For example, suppose two copies of “vi”
Want to share code
Want data and stack to be different
Can’t do this with base and bounds!
2. Complex memory allocation
3. Doesn’t allow heap, stack to grow dynamically – want to
put these as far apart as possible in virtual memory, so
that they can grow to whatever size is needed.
OS
Process 7
OS
50k
process 3
125k Process 9 ?
process 8
100k
OS OS OS
50k process 3
process 3 90k
125k Process 9 process 8
process 8 60k process 8
process 3
100k
process 2 process 2 process 2
Operating System Concepts – 10 th Edition 9.61 Silberschatz, Galvin and Gagne
Segmentation
Memory-management scheme that supports user view of
memory
A segment is a region of logically contiguous memory
A program is a collection of segments
A segment is a logical unit such as:
main program
procedure
function
method
object
local variables, global variables
common block
stack
symbol table
arrays
4
1
3 2
4
Hardware support:
Segment-table base register (STBR) points to the segment table’s
location in memory
Registers
Memory
Disk
Operating System Concepts – 10 th Edition 9.80 Silberschatz, Galvin and Gagne
Implementation of Page Table
Page table is kept in main memory
Page-table base register (PTBR) points to the page table
Page-table length register (PTLR) indicates size of the page table
In this scheme every data/instruction access requires two
memory accesses
One for the page table and one for the data / instruction
The two memory access problem can be solved by the use of a
special fast-lookup hardware cache called associative memory
or translation look-aside buffers (TLBs)
0 2 1
0
1 1
PTBR
Page table
2
Virtual
memory 3
Physical memory
Operating System Concepts – 10 th Edition 9.81 Silberschatz, Galvin and Gagne
Implementation of Page Table (Cont.)
Some TLBs store address-space identifiers (ASIDs) in each
TLB entry – uniquely identifies each process to provide
address-space protection for that process
Otherwise need to flush at every context switch
TLBs typically small (64 to 1,024 entries)
On a TLB miss, value is loaded into the TLB for faster access
next time
Replacement policies must be considered (LRU, random,
etc.)
Some entries can be wired down for permanent fast
access
Free
PWT
PCD
Physical Frame Number 0 L D A UW P
(OS)
31-12 (same as “valid”
P: Present 11-9bit 8
in 7other
6 5 4 3 2 1 0
architectures)
W: Writeable
U: User accessible
PWT: Page write transparent: external cache write-
through
PCD: Page cache disabled (page cannot be cached)
A: Accessed: page has been accessed recently
D: Dirty (PTE only): page has been modified
recently
L: L=14MB page (directory only).
Bottom 22 bits of virtual address Silberschatz,
serve as offset
Galvin and Gagne
Operating System Concepts – 10 th Edition 9.98
Examples of how to use a PTE
How do we use the PTE?
Invalid PTE can imply different things:
Region of address space is actually invalid or
Page/directory is just somewhere else than memory
Validity checked first
OS can use other (say) 31 bits for location info
Usage Example: Demand Paging
Keep only active pages in memory
Place others on disk and mark their PTEs invalid
Usage Example: Copy on Write
UNIX fork gives copy of parent address space to child
Address spaces disconnected after child created
How to do this cheaply?
Make copy of parent’s page tables (point at same memory)
Mark entries in both sets of page tables as read-only
Page fault on write creates two copies
Usage Example: Zero Fill On Demand
New data pages must carry no information (say be
zeroed)
Mark PTEs as invalid; page fault on use gets zeroed page
Often, OS creates zeroed pages in background
100 Processor-Memory
Performance Gap:
(grows 50% / year)
10
“Less’ Law?” DRAM
DRAM
9%/yr.
1 (2X/10
yrs)
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
Time
Operating System Concepts – 10 th Edition 9.102 Silberschatz, Galvin and Gagne
Hashed Page Tables
Common in address spaces > 32 bits
The virtual page number is hashed into a page table
This page table contains a chain of elements hashing to the
same location
Each element contains (1) the virtual page number (2) the value
of the mapped page frame (3) a pointer to the next element
Virtual page numbers are compared in this chain searching for a
match
If a match is found, the corresponding physical frame is
extracted
Variation for 64-bit addresses is clustered page tables
Similar to hashed but each entry refers to several pages
(such as 16) rather than 1
Especially useful for sparse address spaces (where memory
references are non-contiguous and scattered)
Entry consists of the virtual address of the page stored in that real
memory location, with information about the process that owns
that page
Virtual address
virt seg # virt page # offset Physical address
phys frame# offset
No
page-table page-table > error
base size
yes
Segment table
Physical memory
+ Phys frame #
Operating System Concepts – 10 th Edition 9.109Page table Silberschatz, Galvin and Gagne
Paging + segmentation
Questions:
1. What must be saved/restored on context switch?
2. How do we share memory? Can share entire
segment, or a single page.
Example: 24 bit virtual addresses = 4 bits of
segment #, 8 bits of virtual page #, and 12 bits of
offset.
Segment table Physical memory
Page-table base Page-table size 0x1000 0x6 What do the
0xb following addresses
0x2000 0x14 0x4 translate to?
– – …
0x1000 0xD 0x2000 0x13 0x002070?
– – 0x2a 0x201016 ?
0x3 0x14c684 ?
… 0x210014 ?
portions of the page tables
Operating System Concepts – 10 th Edition 9.110 for the segments Silberschatz, Galvin and Gagne
Multilevel translation
What must be saved/restored on context switch?
Contents of top-level segment registers (for this example)
Pointer to top-level table (page table)
Pro:
Only need to allocate as many page table entries as we
need.
In other words, sparse address spaces are easy.
Easy memory allocation
Share at segment or page level (need additional reference
counting)
Cons:
Pointer per page (typically 4KB - 16KB pages today)
Page tables need to be contiguous
Two (or more, if > 2 levels) lookups per memory reference
Probability
of reference
0 2n - 1
Address Space
Temporal Locality (Locality in Time):
Keep recently accessed data items closer to
processor
Spatial Locality (Locality in Space):
Move contiguous blocks to the upper levels
Lower Level
To Processor Upper Level Memory
Memory
Blk X
From Processor Blk Y
Processor
Control
Tertiary
Secondary
Storage
Second Storage
Main (Tape)
Level (Disk)
On-Chip
Registers
Memory
Cache
: :
0x50 Byte 63 Byte 33 Byte 32 1
2
3
: : :
Byte 1023 Byte 992 31
Operating System Concepts – 10 th Edition 9.117 : Silberschatz, Galvin and Gagne
Review: Set Associative Cache
31 8 4 0
Cache Tag Cache Index Byte Select
Valid Cache Tag Cache Data Cache Data Cache Tag Valid
Cache Block 0 Cache Block 0
: : : : : :
Compare Compare
Sel1 1 Mux 0 Sel0
OR
Operating System Concepts – 10 th Edition Hit 9.119 Cache Block Silberschatz, Galvin and Gagne
Review: Fully Associative
Cache
Fully Associative: Every block can hold any line
Address does not include a cache index
Compare Cache Tags of all Cache Entries in
Parallel
31 4 0
Cache Tag (27 bits long) Byte Select
Ex: 0x01
: :
= Byte 63 Byte 33 Byte 32
=
=
=
: : :
Block 1111111111222222222233
no. 01234567890123456789012345678901
TTBR register is the translation table base register and points to the
level 0 table for the current thread
If all four levels are used, the offset (bits 0–11) refers to the offset
within a 4-KB page.
Table entries for level 1 and level 2 may refer either to another table
or to a 1-GB region (level-1 table) or 2-MB region (level-2 table).
Operating System Concepts – 10 th Edition 9.143 Silberschatz, Galvin and Gagne
64-bit ARMv8 Architecture
The ARM architecture supports two levels of TLBs
Inner level has two micro TLBs (one data, one
instruction)
The micro TLB supports ASIDs as well
At the outer level is single main TLB
Address translation begins at the micro-TLB level.
In the case of a miss, the main TLB is then
checked.
If both TLBs yield misses, a page table walk must
be performed in hardware.