Professional Documents
Culture Documents
#4 (Memory Management Intro)
#4 (Memory Management Intro)
#4 (Memory Management Intro)
Operating Systems
Adil Ahmad
Lecture #4: Memory management (introduction)
Let’s finish up the general OS concepts
Process interacts with the kernel using system calls
▪ Requests made to the OS from a user process for a certain operation (e.g., file
system access, device access, etc.)
User mode
(U-mode/ring-3)
syscall sysret
Supervisor mode
(S-mode/ring-0)
▪ Process executes SYSCALL which tells the CPU a system call is made
▪ Arguments are passed between process and kernel using CPU registers
What are the different parts (or segments) within a process’ memory?
▪ Process will access several memory regions while executing its code
Assembly instructions
Location (address)
of an instruction
Understanding memory access at assembly level
Memory accesses:
The OS’ job is to make sure all these memory accesses work “correctly”!
Memory management basics and history
High-level overview of memory management
Process: Process: OS
NGINX Memcached kernel
DRAM
Disk NGINX Memcached
Process A Process B
▪ Rewrite the two process’ addresses to be distinct and load the processes
at those new distinct locations
▪ You can do this using a compiler or binary rewriter (ask me offline!)
An example static system memory layout
▪ Each process is rewritten to use its own entirely reserved region that is
decided when it is loaded
Start End
0x0 0x10000
Process Process
DRAM A B
0x1fff 0x2fff
0x1000 0x2000
Can you think of some problems with the static rewriting approach?
▪ Each process is rewritten to use its own entirely reserved region that is
decided when it is loaded
Start End
0x0 0x10000
Process Process
DRAM A B
0x1fff 0x2fff
0x1000 0x2000
Problem #1: relocation at runtime is very hard
Process Process
A A
DRAM
Problem #2: security problems due to no isolation
0x0 0x1000
Process Process
A B
DRAM
int* malicious_ptr = (int*) 0x2000
*malicious_ptr = 0x100;
▪ Problems with static rewriting: relocation and security, let’s tackle those
Let’s see how this solves the relocation and security problems!
Solving the relocation hurdle with “base”
Base1 Base2
Process Process
A B
DRAM
*(ptr+base1) *(ptr+base2)
Before After
Solving the security hurdle with “bounds”
Process Process
A B
DRAM
Caught/rejected by the bounds
int* malicious_ptr = (int*) 0x2000
*malicious_ptr = 0x100 check made by the system!
Note on the virtualization of “memory address”
▪ Programs are not contiguous and have chunks of “free spaces” to grow
▪ E.g., free space between stack and heap regions
Base Bounds
Free space!
▪ Simple base and bounds wastes a lot of memory for each process!
▪ Must reserve all the free space beforehand
How exactly would you solve this problem?
▪ Programs are not contiguous and have chunks of “free spaces” to grow
▪ E.g., free space between stack and heap regions
Base Bounds
Free space!
▪ Simple base and bounds wastes a lot of memory for each process!
▪ Must reserve all the free space beforehand
Take-3: segmentation introduced by Intel x86
▪ Different program regions (e.g., heap, stack, etc.) can have a segment
▪ Individual segments can be efficiently resized (e.g., grow the stack, etc.)
Examining the logical view of segments
Segmentation requires further changes to the MMU
▪ Any problems?
▪ Require many (expensive) registers and cannot dynamically adjust over fixed set
of hardware registers