Virtual Memory Classnotes
Virtual Memory Classnotes
Virtualization:
Announcement
• Quiz next week
• On CV onwards excluding today’s lecture
• MCQ
• Assignment 2 released
• Bonus from Assignment 1
• MLFQ Question
• My Teaching philosophy and grading remarks
Motivation for
Virtualization
Uniprogramming: One process runs at a time
0
OS Code
Physical
Memory
Heap
User Address
Process Space
Stack
2n-1
Disadvantages:
• Only one process runs at a time
• Process can destroy OS
Multiprogramming
Goals
Transparency
• Processes are not aware that memory is shared
• Works regardless of number and/or location of processes
Protection
• Cannot corrupt OS or other processes
• Privacy: Cannot read data of other processes
Efficiency
• Do not waste memory resources (minimize fragmentation)
Sharing
• Cooperating processes can share portions of address space
Abstraction: Address SPace
Address space: Each process has set of addresses that map to bytes
Problem: 0
Code
How can OS provide illusion of private address space to each process?
Heap
Review: What is in an address space?
Recursive procedures
• Do not know how many times procedure will be nested
No fragmentation
Where Are stacks Used?
OS uses stack for procedure call frames (local variables and parameters)
main () {
int A = 0;
foo (A);
printf(“A: %d\n”, A);
}
Address Location
Observation:
OS gives illusion of many virtual CPUs by saving CPU registers to memory
when a process isn’t running
Could give illusion of many virtual memories by saving memory to disk when
process isn’t running
Memory
code
data
Program
code
data
heap
stack
Process 1
Memory
code
data
Program
code
data
heap
stack
Process 1
Memory
code create
code
data data2
Program heap2
stack2
Process 2
code
data
heap
stack
Process 1
Memory
code
code
data data2
Program heap2
stack2
Process 2
code
data
heap
stack
Process 1
Memory
code code
data data2
Program heap2
stack2
Process 2
code
data
heap
stack
Process 1
Memory
code code
data data2
Program heap2
stack2
Process 2
code
data
heap
stack
Process 1
Memory
code code
data2 code
data data
Program heap2
heap
stack2
stack
Process 2
Process 1
Memory
code code
data2 code
data data
Program heap2
heap
stack2
stack
Process 2
Process 1
Problems with
Time Sharing Memory
Problem: Ridiculously poor performance
• Each rewrite for different process uses different addresses and pointers
stack
8 KB
(free)
16 KB
stack
why didn’t OS rewrite stack addr?
(free)
Static Relocation:
Disadvantages
No protection
• Process can destroy OS or other processes
• No privacy
CPU MMU
Memory
Physical address
Logical address
Hardware Support for
Dynamic Relocation
Two operating modes
• Privileged (protected, kernel) mode: OS runs
• When enter OS (trap, system calls, interrupts, exceptions)
• Allows certain instructions to be executed
• Can manipulate contents of MMU
• Allows OS to access all of physical memory
• User mode: User processes run
• Perform translation of logical address to physical address
MMU
32 bits 1 bit
registers base mode
logical physical
address mode no address
=
user?
yes
+
base
Dynamic Relocation with
Base Register
Idea: translate virtual addresses to physical by adding a fixed offset each time.
1 KB
P1
2 KB
3 KB same code
4 KB
P2
5 KB
6 KB
1 KB base register
P1
2 KB
3 KB
P1 is running
4 KB
P2
5 KB
6 KB
0 KB
1 KB
P1
2 KB
3 KB
P2 is running
4 KB base register
P2
5 KB
6 KB
(Decimal notation)
0 KB Virtual Physical
P1: load 100, R1
1 KB
P1
2 KB
3 KB
4 KB
P2
5 KB
6 KB
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
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
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
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
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 5196, R1
3 KB
4 KB
P2
5 KB
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 5196, R1
P1: load 100, R1
3 KB
4 KB
P2
5 KB
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 5196, R1
P1: load 1000, R1 load 2024, R1
3 KB
4 KB
P2
5 KB
6 KB
Quiz: Who Controls the
Base Register?
What entity should do translation of addresses with base register?
(1) process, (2) OS, or (3) HW
4 KB
P2
5 KB Can P2 hurt P1?
Can P1 hurt P2?
6 KB
How well does dynamic relocation do with base register for protection?
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 5196, R1
P1: load 100, 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
How well does dynamic relocation do with base register for protection?
4) Dynamic with Base+Bounds
< yes +
bounds? base
error
no
0 KB
1 KB base register
P1
2 KB bounds register
3 KB
P1 is running
4 KB
P2
5 KB
6 KB
0 KB
1 KB
P1
2 KB
3 KB
P2 is running
4 KB base register
P2
5 KB bounds register
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 5196, R1
P1: load 100, R1 load 2024, R1
3 KB
P1: store 3072, R1
4 KB
P2
5 KB
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 5196, R1
P1: load 100, R1 load 2024, R1
3 KB
P1: store 3072, R1 interrupt OS! 3072 > 1024
4 KB
P2
5 KB
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 5196, R1
P1: load 100, R1 load 2024, R1
3 KB
P1: store 3072, R1 interrupt OS!
4 KB
P2
5 KB
Can P1 hurt P2?
6 KB
Managing Processes
with Base and Bounds
Context-switch
• Add base and bounds registers to PCB
• Steps
• Change to privileged mode
• Save base and bounds registers of old process
• Load base and bounds registers of new process
• Change to user mode and jump to new process
Protection requirement
• User process cannot change base and bounds registers
• User process cannot change to privileged mode
Base and Bounds
Advantages
Advantages
• Provides protection (both read and write) across address spaces
• Supports dynamic relocation
• Can place process at different locations initially and also move address spaces
Heap
Stack
2n-1
5) Segmentation
Divide address space into logical segments
• Each segment corresponds to logical entity in address space
• code, stack, heap
Stack
2n-1
Segmented Addressing
Process now specifies segment and offset within segment
0x1200
0x1600
stack (seg2)
0x2000
0x2400
Segment numbers:
0: code+data
1: heap
2: stack
0x00 Virtual (hex) Physical
load 0x2010, R1 0x1600 + 0x010 = 0x1610
0x400
heap (seg1)
0x800
0x1200
0x1600
stack (seg2)
0x2000
0x2400
Segment numbers:
0: code+data
1: heap
2: stack
0x00 Virtual (hex) Physical
load 0x2010, R1 0x1600 + 0x010 = 0x1610
0x400
heap (seg1) load 0x1010, R1
0x800
0x1200
0x1600
stack (seg2)
0x2000
0x2400
Segment numbers:
0: code+data
1: heap
2: stack
0x00 Virtual (hex) Physical
load 0x2010, R1 0x1600 + 0x010 = 0x1610
0x400
heap (seg1) load 0x1010, R1 0x400 + 0x010 = 0x410
0x800
0x1200
0x1600
stack (seg2)
0x2000
0x2400
Segment numbers:
0: code+data
1: heap
2: stack
0x00 Virtual Physical
load 0x2010, R1 0x1600 + 0x010 = 0x1610
0x400
heap (seg1) load 0x1010, R1 0x400 + 0x010 = 0x410
0x800 load 0x1100, R1
0x1200
0x1600
stack (seg2)
0x2000
0x2400
Segment numbers:
0: code+data
1: heap
2: stack
0x00 Virtual Physical
load 0x2010, R1 0x1600 + 0x010 = 0x1610
0x400
heap (seg1) load 0x1010, R1 0x400 + 0x010 = 0x410
0x800 load 0x1100, R1 0x400 + 0x100 = 0x500
0x1200
0x1600
stack (seg2)
0x2000
0x2400
Segment numbers:
0: code+data
1: heap
2: stack
Advantages of
Segmentation
• Enables sparse allocation of address space
• Stack and heap can grow independently
• Heap: If no data on free list, dynamic memory allocator requests more from OS (e.g.,
UNIX: malloc calls sbrk())
• Stack: OS recognizes reference outside legal segment, extends stack implicitly
Stack
Disadvantages of
Segmentation
Each segment must be allocated contiguously
• May not have sufficient physical memory for large segments