Virtual Memory
CS-446/646
C. Papachristos
Robotic Workers (RoboWork) Lab
University of Nevada, Reno
Virtual Memory
Virtual Memory (VM)
Early computers
CS446/646 C. Papachristos
Virtual Memory
Virtual Memory (VM)
Issues of sharing Physical Memory
CS446/646 C. Papachristos
Virtual Memory
Virtual Memory (VM) Goals
Allow Programs to “see” more Memory than exists
CS446/646 C. Papachristos
Kernel-Space
Memory
User-Space
Memory
MMU
Physical
Memory
0x30408
0x1A408
load
MMU
0x30408
load
✗ Address Illegal
to Fault Handler
Virtual Memory
Virtual Memory (VM) Advantages
Can re-locate Program while running
Most of a Process’s Memory may be idle (“80/20 Rule” – more later)
Challenge:
CS446/646 C. Papachristos
Virtual Memory
Core Concept 1: Load-Time Dynamic Linking
i.e. Load-Time Dynamic Linking happens when Process executed (not at Compile-Time or at Link-Time)
Problems?
CS446/646 C. Papachristos
Virtual Memory
Core Concept 1: Load-Time Dynamic Linking
i.e. Load-Time Dynamic Linking happens when Process executed (not at Compile-Time or at Link-Time)
More Problems?
CS446/646 C. Papachristos
mov 0x200271(%rip),%rax # address to use: 200828 <_DYNAMIC+0x1a0>
mov (%rax),%eax # use that address’ contents
(Note: PLT populated lazily
during first function call)
0x200828 inside the .got Section of the ELF binary
value stored in 0x200828 will be populated with Dynamic Address to use
Virtual Memory
CS446/646 C. Papachristos
Base
Bound
Virtual Memory
Core Concept 3: Virtual Address Space (VAS)
The Virtual-to-Physical Memory-managing Hardware is the Memory Management Unit (MMU)
CS446/646 C. Papachristos
MMU
Physical
Memory
CPU
Physical
Addresses
Virtual
Addresses
Virtual Memory
VAS through Base & Bound Registers: Trade-offs
Advantages
Disadvantages
CS446/646 C. Papachristos
Virtual Memory
Core Concept 4: Segmentation
How?
CS446/646 C. Papachristos
Physical
Memory
Code Segment
Data�Segment
Stack Segment
P0
Extra Segment
Example Illustration:
8086 Segmentation Model
ES
SS
DS
CS
Corresponding�CPU� Register
Virtual Memory
Core Concept 4: Segmentation
Segmentation Mechanics
Virtual Address
Segment #
Offset
0x5 (DS)
0x128
Physical
Memory
0x1000
0x128 (Offset)
Physical
Address
<
+
MMU
C. Papachristos
CS
DS
SS
Segment Table
0x1512
Base | Len | Flags |
| | |
… | … | … |
| | |
0x1000 | 0x512 | r |
Virtual Memory
Core Concept 4: Segmentation
Segmentation Example
CS446/646 C. Papachristos
A Process’ Segment Table
0x0: CS
0x1: SS
0x2: DS
…
16-bit Virtual Address (0x0000 – 0xFFFF)
0x4FF
0xFFF
0x6FF
Virtual Memory
CS446/646 C. Papachristos
Virtual Memory
CS446/646 C. Papachristos
Allocated
?
External Fragmentation
Unused –
Internal Fragmentation
Virtual Memory
Core Concept 5: Paging for Virtual Memory Management
CS446/646 C. Papachristos
|
|
|
|
|
|
|
|
Page 1 |
Page 2 |
Page 3 |
… |
Page N-1 |
Virtual Memory
Physical Memory
Virtual Memory
Paging Trade-offs
CS446/646 C. Papachristos
|
|
|
|
|
|
|
P0 - CS
P1 - SS
Internal Fragmentation
Virtual Memory
Simplified Allocation
CS446/646 C. Papachristos
|
|
|
|
|
|
|
Disk
P0
P1
Virtual Memory
CS446/646 C. Papachristos
Virtual Memory
CS446/646 C. Papachristos
Virtual Address
VPN
Offset
PPN | Flags |
| |
… | … |
| |
0x1 | vprcd |
0x5
0x128
MMU
(1 << 12) | 0x128
0x1000 (=1 x 4KiB)
0x128 (Offset)
4 KiB
Page Table
Page
Table
Entries
Virtual Memory
Page Table Entries
CS446/646 C. Papachristos
Physical Page Number
V
P
R
C
D
20-bit
32-bit
Virtual Memory
CS446/646 C. Papachristos
Virtual Memory
Paging Advantages
CS446/646 C. Papachristos
Next Lecture Reading Preparation
Operating Systems – Three Easy Pieces (https://pages.cs.wisc.edu/~remzi/OSTEP/)
Virtualization
CS446/646 C. Papachristos
Virtual Memory
CS446/646 C. Papachristos
Virtual Memory
CS446/646 C. Papachristos
32-bit Virtual Address
Secondary VPN
Offset
Master VPN
10-bit long
10-bit long
12-bit long
Remaining 10 bits for
Secondary VPN
Virtual Memory
x86 Page Translation
CS446/646 C. Papachristos
Physical
Address
Physical Address
Physical Address
Note:
For every Page Directory, we need two 4KiB arrays:
Virtual Memory
Two-Level Page Tables
Evolution
CS446/646 C. Papachristos
Page Tables
Two-Level Page Tables
4 MiB
4 KiB
4 KiB
Virtual Memory
CS446/646 C. Papachristos
Two-Level Page Tables
4 KiB
4 KiB
Virtual Memory
Two-Level Page Tables
How to access (Secondary) Page Tables themselves?
Solution 1) Have 1 Page Table in the Page Directory reserved to point back to Page Directory
Solution 2) Keep 2 arrays for every Page Directory
CS446/646 C. Papachristos
Virtual Memory
CS446/646 C. Papachristos
Virtual Memory
x86 Paging and Segmentation
Step 1) Segment Base + Pointer Val = Linear Address (also, Segment Bound checking)
Step 2) Linear Addresses translated to Physical Address via usual Page Translation
CS446/646 C. Papachristos
Virtual Memory
x86 Paging and Segmentation
1) Segments for logically related units
2) Pages to partition Segments into fixed-size chunks� • Tends to be complex
CS446/646 C. Papachristos
Virtual Memory
Where does the OS live?
Note1: Kernel Sharing the total Addressable VAS limits User-Space (on 32-bit Linux, Kernel: 1GB, User: 3GB)
Note2: Recent Spectre and Meltdown CPU vulnerabilities force OSes to reconsider things
CS446/646 C. Papachristos
Virtual Memory
Where does the OS live?
CS446/646 C. Papachristos
Pintos Virtual Memory model
User VM
Kernel VM
User Stack
Heap
Uninitialized Data (BSS)
Initialized Data Segment
Code Segment
Kernel Pseudo-Physical �& Virtual Memory
0xFFFFFFFF
0xC0000000
0x00000000
0x08048000
PHYS_BASE
Invalid Virtual Addresses (for this Process)
Grows Downward
Grows Upward
“Identity Mapping”
Virtual Address Space�of a Process:
Virtual Memory
Where does the OS live?
Implications:
CS446/646 C. Papachristos
Virtual Memory
Where does the OS live?
How ?
CS446/646 C. Papachristos
struct thread {
tid_t tid; // Thread identifier
enum thread_status status; // Thread state
char name[16]; // Name (for debugging purposes)
uint8_t *stack; // Saved stack pointer
int priority; // Priority
struct list_elem allelem; // List element for
// all threads list
struct list_elem elem; // List element
#ifdef USERPROG
/* Owned by userprog/process.c. */
uint32_t *pagedir; // Page directory
#endif
/* Owned by thread.c. */
unsigned magic; // Detects stack overflow
};
bool load (const char *file_name, void (**eip) (void), void **esp) {
struct thread *t = thread_current ();
...
/* Allocate and activate page directory. */
t->pagedir = pagedir_create ();
if (t->pagedir == NULL)
goto done;
process_activate ();
/* Open executable file. */
file = filesys_open (file_name);
...
}
uint32_t * pagedir_create (void) {
uint32_t *pd = palloc_get_page (0);
if (pd != NULL)
memcpy (pd, init_page_dir , PGSIZE);
return pd;
}
init_page_dir: Initialized in paging_init() in thread.c
Virtual Memory
Where does the OS live?
How ?
CS446/646 C. Papachristos
bool load (const char *file_name,
void (**eip) (void),
void **esp) {
struct thread *t = thread_current ();
...
/* Allocate and activate page directory. */
t->pagedir = pagedir_create ();
if (t->pagedir == NULL)
goto done;
process_activate ();
/* Open executable file. */
file = filesys_open (file_name);
...
}
void process_activate (void) {
struct thread *t = thread_current ();
pagedir_activate (t->pagedir);
/* Set thread's kernel stack for use in processing interrupts. */
tss_update ();
}
void pagedir_activate (uint32_t *pd) {
if (pd == NULL)
pd = init_page_dir;
/* Store the physical address of the page directory
into CR3 aka PDBR (Page Directory Base Register).
This activates our new page tables immediately.
See [IA32-v2a] "MOV—Move to/from Control Registers“
and [IA32-v3a] 3.7.5 "Base Address of the Page Directory". */
asm volatile ("movl %0, %%cr3" : : "r" (vtop (pd)) : "memory");
}
After this point Virtual Memory mappings have changed
Virtual Memory
Addressing Page Tables
Where do we store the (Secondary) Page Tables (which Address Space)?
If we’re “Swapping-Out” (Secondary) Page Tables, could as well “Swap-Out” entire OS Address Space
CS446/646 C. Papachristos
Virtual Memory
Addressing Page Tables
CS446/646 C. Papachristos
Virtual Memory
Efficient Translations
The single Page Table approach already carries 2x the cost of Memory access
Now the Two-Level Page Tables 3x the cost!
How can we use Paging but also reduce Lookup cost?
CS446/646 C. Papachristos
Virtual Memory
Translation Lookaside Buffer (TLB)
CS446/646 C. Papachristos
Virtual Memory
Translation Lookaside Buffer (TLB)
CS446/646 C. Papachristos
Virtual Memory
TLB Management
Who loads Translations into the TLB?
CS446/646 C. Papachristos
Virtual Memory
TLB Management
When the TLB “Misses” a new Page Table Entry has to be loaded:
CS446/646 C. Papachristos
Note: “Global mappings” means they have the same Translation inde-pendently of the current Process Address Space (e.g. Kernel mappings)
Virtual Memory
“Swapped”(/“Paged”) Virtual Memory
CS446/646 C. Papachristos
Disk
P0
P1
|
|
|
|
|
|
|
Swap-Out
Swap-In
Virtual Memory
“Swapped”(/“Paged”) Virtual Memory
CS446/646 C. Papachristos
Virtual Memory
Page Faults
What happens when a Process accesses a Page that has been evicted?
CS446/646 C. Papachristos
Virtual Memory
Page Faults
CS446/646 C. Papachristos
Virtual Memory
All together – the “Normal Operation” case
A simple situation: Process is executing on the CPU, and it issues a read to an Address
The read goes to the TLB in the MMU
Note: This is all done by the Hardware
CS446/646 C. Papachristos
Virtual Memory
TLB Misses and Protection Faults
Two other things can also happen:
CS446/646 C. Papachristos
Virtual Memory
TLB Misses and Protection Faults
Reloading the TLB
If the TLB does not contain the Mapping, two possibilities:
2. Trap to the OS – Software-managed TLB
CS446/646 C. Papachristos
Virtual Memory
TLB Misses and Protection Faults
(Re-)loading the TLB
A Page Table Lookup (by Hardware or OS) can cause a recursive Fault if Page Table has been “Swapped-Out”
When TLB has Page Table Entry it restarts Translation
CS446/646 C. Papachristos
Virtual Memory
TLB Misses and Protection Faults
Protection Faults
Page Table Entry can indicate a Protection Fault
TLB Traps to the OS (Software takes over)
CS446/646 C. Papachristos
Virtual Memory
TLB Misses and Protection Faults
CS446/646 C. Papachristos
“Walk”
Page Table
Protection
Check
Page
Fault
Protection Check
Yes
Protection Fault
Segmentation
Fault
CPU Cache
No
“Page Replacement” Policies
Is P in memory
(Now have PTE & frame)
(Now have PTE & frame)
Virtual Memory
Advanced Functionalities through Virtual Memory “Tricks”
CS446/646 C. Papachristos
Virtual Memory
CS446/646 C. Papachristos
Virtual Memory
Copy-on-Write
CS446/646 C. Papachristos
Before fork()
After fork()
Virtual Memory
Copy-on-Write
CS446/646 C. Papachristos
After first write
Virtual Memory
Memory-Mapped Files
CS446/646 C. Papachristos
Virtual Memory
Memory-Mapped Files
CS446/646 C. Papachristos
File-backed Virtual Memory
Virtual Memory
Memory-Mapped Files
Advantages
Drawbacks
CS446/646 C. Papachristos
Virtual Memory
Memory-Mapped Files
Also very importantly:
CS446/646 C. Papachristos
Virtual Memory
Shared Memory
CS446/646 C. Papachristos
Virtual Memory
Shared Memory
CS446/646 C. Papachristos
Isolation: No Sharing
Sharing Pages
Virtual Memory
Shared Memory
Implement Inter-Process Communication (IPC) Memory Sharing using Page Tables :
Can map Shared Memory at same or different Virtual Addresses in each Process’ Address Space
CS446/646 C. Papachristos
typedef struct {
int * arr_p;
int arr[256];
} data_t;
inst shmid = shmget(KEY, sizeof(data_t),
IPC_CREAT | 0666);
data_p = (data_t *)shmat(shmid);
data_p->arr_p = data_p->arr; // Possible
// Problem!
Next Lecture Reading Preparation
Operating Systems – Three Easy Pieces (https://pages.cs.wisc.edu/~remzi/OSTEP/)
Virtualization
CS446/646 C. Papachristos
Time for Questions !
CS-446/646
CS446/646 C. Papachristos