09 Virtualization Memory Address Space Translation
09 Virtualization Memory Address Space Translation
Operating System
(code, data, etc.)
64KB C u rre n t
P ro g ra m
(co d e , d a ta , e tc.)
Current
Program
(code, data, etc.)
max
Physical Memory
Multiprogramming and Time Sharing
Operating System
Execute one for a short while. 64KB F re e
(code, data, etc.)
Free
448KB F re e
Free
512KB
Physical Memory
Address Space
0KB
P ro g ra m
C o d e /D a ta
Program
Code /Data
1KB H eap
Heap
2KB (fre e )
(free)
15KB
S ta c k
Stack
16KB
Address Space
Address Space(Cont.)
Code
P ro g ra m
C o d e /D a ta
Heap
(free)
Dynamically allocate memory.
malloc in C language
new in object-oriented language S ta c k
Stack
Stack
Address Space
Store return addresses or values.
#include <stdio.h>
#include <stdlib.h>
return x;
}
#include <stdio.h>
#include <stdlib.h>
return x;
}
Address Space
The output in 64-bit Linux machine Code
(Tex t)
Code
D a ta
(Text)
Data
H eap
(fre e )
Heap
heap
(free)
stack
S ta c k
Stack
Example: pmap and cat /proc/<pid>/maps
0KB P ro g ra m C o d e
0KB
O p e r a t in g S y s t e m
16KB
H eap
( n o t in u s e )
Heap
(not in use)
(fre e )
32KB
Relocated Process
Code
Code
Heap
H eap
(a llo ca ted
b u t n o t in u se )
heap
(allocated
(free) but not in use)
Stack
Sta ck
stack 48KB ( n o t in u s e )
S ta c k
(not in use)
Stack
64KB
16KB Physical Memory
Address Space
Base and Bounds Register
0KB P ro g ra m C o d e
0KB
O p e r a t in g S y s t e m
16KB
H eap
( n o t in u s e )
Heap
(not in use) base register
(fre e )
32K B
32KB Code
Code
32KB
Heap
H eap
(a llo ca ted
b u t n o t in u se )
heap
(allocated
(free) but not in use)
Stack
Sta ck
stack 48KB ( n o t in u s e )
S ta c k
(not in use)
64KB
16KB Physical Memory
16KB Address Space
𝑝h𝑦𝑐𝑎𝑙𝑎𝑑𝑑𝑟𝑒𝑠𝑠=𝑣𝑖𝑟𝑡𝑢𝑎𝑙𝑎𝑑𝑑𝑟𝑒𝑠𝑠+𝑏𝑎𝑠𝑒
0≤𝑣𝑖𝑟𝑡𝑢𝑎𝑙𝑎𝑑𝑑𝑟𝑒𝑠𝑠<𝑏𝑜𝑢𝑛𝑑𝑠
𝑝h𝑦𝑐𝑎𝑙𝑎𝑑𝑑𝑟𝑒𝑠𝑠=𝑣𝑖𝑟𝑡𝑢𝑎𝑙𝑎𝑑𝑑𝑟𝑒𝑠𝑠+𝑏𝑎𝑠𝑒
Every virtual address must not be greater than bound and negative.
0≤𝑣𝑖𝑟𝑡𝑢𝑎𝑙 𝑎𝑑𝑑𝑟𝑒𝑠𝑠<𝑏𝑜𝑢𝑛𝑑𝑠
Two ways of Bounds Register
0KB P ro g ra m C o d e
0KB
O p e r a t in g S y s t e m
16KB
H eap
( n o t in u s e )
Heap
(not in use)
(fre e )
32KB Code
Code
Heap
H eap
(a llo c a te d
b u t n o t in u s e )
(allocated
(free) but not in use)
bounds bounds
Stack
Sta ck
16KB 48KB
16KB 48KB ( n o t in u s e )
48KB
S ta c k
(not in use)
Stack
64KB
Physical Memory
16KB Address Space
Base Register
Physical
Memory
0 KB
1 KB
P1
2 KB
3 KB same code
4 KB
P2
5 KB
6 KB
Base Register
0 KB
1 KB base register
P1
2 KB
3 KB
P1 is running
4 KB
P2
5 KB
6 KB
Base Register
0 KB
1 KB
P1
2 KB
3 KB
P2 is running
4 KB base register
P2
5 KB
6 KB
Base Register
(Decimal notation)
0 KB Virtual Physical
P1: load 100, R1
1 KB
P1
2 KB
3 KB
4 KB
P2
5 KB
6 KB
Base Register
0 KB Virtual Physical
P1: load 100, R1 load 1124, R1 (1024 + 100)
1 KB
P1
2 KB
3 KB
4 KB
P2
5 KB
6 KB
Base Register
0 KB Virtual Physical
P1: load 100, R1 load 1124, R1
1 KB
P1 P2: load 100, R1
2 KB
3 KB
4 KB
P2
5 KB
6 KB
Base Register
0 KB Virtual Physical
P1: load 100, R1 load 1124, R1
1 KB
P1 P2: load 100, R1 load 4196, R1 (4096 + 100)
2 KB
3 KB
4 KB
P2
5 KB
6 KB
Base Register
0 KB Virtual Physical
P1: load 100, R1 load 1124, R1
1 KB
P1 P2: load 100, R1 load 4196, R1
2 KB P2: load 1000, R1
3 KB
4 KB
P2
5 KB
6 KB
Base Register
0 KB Virtual Physical
P1: load 100, R1 load 1124, R1
1 KB
P1 P2: load 100, R1 load 4196, R1
2 KB P2: load 1000, R1 load 5096, R1
3 KB
4 KB
P2
5 KB
6 KB
Base Register
0 KB Virtual Physical
P1: load 100, R1 load 1124, R1
1 KB
P1 P2: load 100, R1 load 4196, R1
2 KB P2: load 1000, R1 load 5096, R1
P1: load 1000, R1
3 KB
4 KB
P2
5 KB
6 KB
Base Register
0 KB Virtual Physical
P1: load 100, R1 load 1124, R1
1 KB
P1 P2: load 100, R1 load 4196, R1
2 KB P2: load 1000, R1 load 5096, R1
P1: load 1000, R1 load 2024, R1
3 KB
4 KB
P2
5 KB
6 KB
Base Register
0 KB Virtual Physical
P1: load 100, R1 load 1124, R1
1 KB
P1 P2: load 100, R1 load 4196, R1
2 KB P2: load 1000, R1 load 5096, R1
P1: load 1000, R1 load 2024, R1
3 KB
4 KB
P2
5 KB Can P2 hurt P1?
Can P1 hurt P2?
6 KB
0 KB Virtual Physical
P1: load 100, R1 load 1124, R1
1 KB
P1 P2: load 100, R1 load 4196, R1
2 KB P2: load 1000, R1 load 5096, R1
P1: load 1000, R1 load 2024, R1
3 KB
P1: store 3072, R1 store 4096, R1 (3072 + 1024)
4 KB
P2
5 KB Can P2 hurt P1?
Can P1 hurt P2?
6 KB
0 KB
1 KB base register
P1
2 KB bounds register
3 KB
P1 is running
4 KB
P2
5 KB
6 KB
Base + Bounds
0 KB
1 KB
P1
2 KB
3 KB
P2 is running
4 KB base register
P2
5 KB bounds register
6 KB
Base + Bounds
0 KB Virtual Physical
P1: load 100, R1 load 1124, R1
1 KB
P1 P2: load 100, R1 load 4196, R1
2 KB P2: load 1000, R1 load 5096, R1
P1: load 1000, R1 load 2024, R1
3 KB
P1: store 3072, R1
4 KB
P2
5 KB
Can P1 hurt P2?
6 KB
Base + Bounds
0 KB Virtual Physical
P1: load 100, R1 load 1124, R1
1 KB
P1 P2: load 100, R1 load 4196, R1
2 KB P2: load 1000, R1 load 5096, R1
P1: load 1000, R1 load 2024, R1
3 KB
P1: store 3072, R1 Exception 3072 > 1024
4 KB
P2
5 KB
Can P1 hurt P2?
6 KB
Base + Bounds
0 KB Virtual Physical
P1: load 100, R1 load 1124, R1
1 KB
P1 P2: load 100, R1 load 4196, R1
2 KB P2: load 1000, R1 load 5096, R1
P1: load 1000, R1 load 2024, R1
3 KB
P1: store 3072, R1 Exception 3072 > 1024
4 KB
P2
5 KB
Can P1 hurt P2?
6 KB
OS Issues for Memory Virtualizing
0KB
O p e r a t in g S y s t e m
Operating System
The OS lookup the free list
16KB ( n o t in u s e )
Free list
(not in use)
16KB
Code
Heap
H eap
Stack
Sta ck
48KB 48KB ( n o t in u s e )
(not in use)
64KB
Physical Memory
OS Issues: When a Process Is Terminated
16KB
16KB
16KB
( n o t in u s e ) ( n o t in u s e )
16KB 16KB
(not in use) (not in use)
32KB
P ro c e s s A
32KB
32KB ( n o t in u s e )
48KB
48KB ( n o t in u s e )
(not in use)
64KB 48KB 64KB
Physical Memory Physical Memory
OS Issues: When Context Switch Occurs
0KB 0KB
Context Switching 48KB …
Operating System Operating System
16KB 16KB
( n o t in u s e ) ( n o t in u s e )
48KB
32KB
P ro c e s s A
C u r r e n t ly R u n n in g
32KB 32KB
P ro c e s s A
48KB
Process A
bounds Process A bounds
Currently Running 48KB
64KB
48KB
P ro c e s s B
48KB 48KB
P ro c e s s B
C u r r e n t ly R u n n in g
64KB
Process B
Process B
Currently Running
64KB 64KB
Next: Memory Management (Chapters 16, 17, 18, 19, 20, 21, 22, 23)