4_Memory
%rbp
%rbp is the base pointer: points to base of current stack frame
base pointer
%rbp is the base pointer: points to base of current stack frame
? is instruction pointer (or program counter)
%rip is instruction pointer (or program counter)
program counter (PC)
%rip is instruction pointer IP (or program counter PC)
? contains Segment Table
- *MMU* contains Segment Table (per process) - Each segment has own base and bounds, protection bits - N segments => log(N) segment bits
Stack Organization
- Definition: Memory is freed in opposite order from allocation (stack grows backwards) - No fragmentation - Simple and efficient implementation: Pointer separates allocated and freed space => Allocate: Increment pointer => Free: Decrement pointer alloc(A); alloc(B); alloc(C); free(C); alloc(D); free(D); free(B); free(A);
Who Controls the Base Register?
- HW does translation of addresses with base register - OS modifies the base register
Uniprogramming
- One process runs at a time - motivation for virtualization - Disadvantages: Only one process runs at a time; Process can destroy OS
Problems with Time Sharing Memory
- Problem: Ridiculously poor performance - Better Alternative: space sharing- space of memory is divided across processes
Which of the following system resources is time shared in modern systems? - disk - memory - cpu - all of these
- cpu
what variables are stored in the stack?
- local - parameters - return values
Problem: How to run multiple processes simultaneously?
1. Time Sharing 2. Static Relocation 3. Base 4. Base+Bounds 5. Segmentation
Multiprogramming Goals (4)
1. Transparency • Processes are not aware that memory is shared • Works regardless of number and/or location of processes 2. Protection • Cannot corrupt OS or other processes • Privacy: Cannot read data of other processes 3. Efficiency • Do not waste memory resources (minimize fragmentation) 4. Sharing • Cooperating processes can share portions of address space
What entity should modify the base register? (1) process, (2) OS, or (3) HW
2 - OS ie each new process puts values in base register
What entity should do translation of addresses with base register? (1) process, (2) OS, or (3) HW
3 - HW ie cpu or memory
Match Address Location: int x; int main(int argc, char *argv[]) { int y; int *z = malloc(sizeof(int));); }
Address : Location x : Static Data /code main : code y : stack z : stack *z : heap
Each process has set of addresses that map to bytes
Address space
Advantage/Disadvantage of Heap Organization
Advantage • Works for all data structures Disadvantages • Allocation can be slow • End up with small chunks of free space - fragmentation • Where to allocate 12 bytes? 16 bytes? 24 bytes??
Major Complication in modern systems (hint what do systems have to share)
All processes have to share: • A single CPU (prior slides - CPU virtualization) • A single physical memory (This, and following slide sets, Memory virtualization)
what is dynamic relocation
Can place process at different locations initially each time program runs and also move address spaces of running processes
Managing Processes with Base+Bounds
Context-switch => Add base and bounds registers to PCB • Change to privileged mode (CPU does this on interrupt, trap, or fault) • Save base and bounds registers of old process to PCB • Load base and bounds registers of new process from PCB to MMU • Change to user mode (mode bit) and jump to new process Protection requirement: • User process cannot change base and bounds registers (instructions for this are privileged) • User process cannot change to privileged mode (user process cannot change mode bit)
Heap Organization
Definition: Allocate from any random location: malloc(), new() • Heap memory consists of allocated areas and free areas (holes) • Order of allocation and free is unpredictable Advantage • Works for all data structures Disadvantages • Allocation can be slow • End up with small chunks of free space - fragmentation • Where to allocate 12 bytes? 16 bytes? 24 bytes??
Segmentation
Divide address space into logical segments • Each segment corresponds to logical entity in address space • Code, stack, heap (minimum) Each segment can independently: • be placed separately in physical memory • grow and shrink • be protected (separate read/write/execute protection bits)
Address space
Each process has set of addresses that map to bytes
In segmentation, each segment corresponds to logical entity in ?
Each segment corresponds to logical entity in address space
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 - Different protection for different segments (Read-only status for code) - Enables sharing of selected segments (two processes can have same base and bounds values for a shared segment) - Supports dynamic relocation of each segment
True/False: Heap organization does not have fragmentation
False: *Stack* organization *does not* have fragmentation *Heap* organization *does* have fragmentation
True/False: The hardware controls the MMU
False: OS controls MMU
Dynamic Relocation
Goal: Protect processes from one another; enable movement of process address space after it starts running Requires hardware support • Memory Management Unit (MMU)
HW+OS work together to ? memory
HW+OS work together to virtualize memory • Give illusion of private address space to each process
Dynamic with Base+Bounds
Idea: limit the upper bound of address space with a bounds register • Base register: starting location • Bounds register: size of this process's virtual address space OS kills process if process tries to access beyond bounds
Implementation of Dynamic Relocation: BASE REG
Idea: translate virtual addresses to physical by adding a fixed offset each time. OS stores offset in base register whenever process is going to run Each process has different value in base register • Translation on every memory access by user process • MMU adds base register (memory offset) to logical address to form physical address
MMU dynamically...
MMU dynamically changes process address at every memory reference • Process generates logical or virtual addresses (in their address space) • Memory hardware uses physical or real addresses
Memory hardware uses physical/virtual addresses
Memory hardware uses *physical* addresses
Minimal MMU for Dynamic Location
Minimal MMU contains base register for translation • base: start location for physical address space
Static Relocation: Disadvantages
No protection • Process can destroy OS or other processes • No privacy Cannot move address space after it has been placed • relocation is STATIC, that is, before process starts executing instructions • May not be able to allocate new process, if do not have a free space (hole) in memory which is large enough, with all the processes currently running.
? controls MMU
OS controls MMU
What happens if process tries to access beyond bounds in dynamic relocation base+bounds?
OS kills process if process tries to access beyond bounds
Where are stacks used?
OS uses stack for procedure call frames - local variables - parameters
Privacy
Privacy: Cannot read data of other processes
Segmented Addressing
Process now specifies *segment & offset* within segment How does process designate a particular segment? • Use part of logical address • Top bits (msbs) of logical address select segment • Low bits (lsbs) of logical address select offset within segment Translation of addresses • Special registers (base and bounds) used to translate virtual addresses to physical addresses. • One pair of base and bounds registers used for each segment.
What are the different ways that that OS can virtualize memory?
Time sharing, static relocation, dynamic relocation (base, base + bounds, segmentation)
What separates allocated and free space?
Top of Stack Pointer
Top of stack vs Bottom of stack pointers
Top of Stack Pointer = separation of freed and allocated space Bottom of Stack Pointer = %rbp or base pointer
Implementation of BASE+BOUNDS
Translation on every memory access of user process • MMU compares logical address to bounds register • if logical address is >= bounds, then generate error, trap to OS (segmentation fault) • MMU adds base register to logical address to form physical address
True/False: OS gives illusion of many virtual CPUs by saving CPU registers to memory when a process isn't running
True
True/False: Stack organization does not have fragmentation
True
•True/False: You do not know amount of memory needed at compile time
True: • Must be pessimistic when allocate memory statically • Allocate enough for worst possible case; Storage is used inefficiently • Recursive programs • Complex structures (trees and lists)
True/False: Address space has static and dynamic components
True: • Static: Code and some global variables • Dynamic: Stack and Heap
True/False: Adding MMU registers for base+bounds makes translation fast
True: • OS not involved with every address translation, only involved on context switch or errors
True/False: User process cannot change base and bounds registers
True: (instructions for this are privileged)
True/False: User process cannot change to privileged mode
True: (user process cannot change mode bit)
Hardware Support for Dynamic Relocation
Two operating modes: 1. 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 2. User mode: User processes run • Perform translation of each logical address to physical address Minimal MMU contains base register for translation • base: start location for physical address space
Two types of dynamic allocation
Two types of dynamic allocation • Stack • Heap
Purpose of MMU
converts virtual or logical addresses to physical addresses when a process is executing on the cpu
if logical address is >= bounds, then ?
if logical address is >= bounds, then generate error, trap to OS (segmentation fault)
When does system change to privileged mode?
interrupt, trap, fault
memory offset in dynamic relocation
mem offset = base register
Where is the process located when it is running? ready? blocked?
running = memory ready = disk blocked = disk
partial sharing
share limited parts of address space
Steps for Managing Base+Bounds
• Change to privileged mode (CPU does this on interrupt, trap, or fault) • Save base and bounds registers of old process to PCB • Load base and bounds registers of new process from PCB to MMU • Change to user mode (mode bit) and jump to new process
Sharing
• Cooperating processes can share portions of address space
Why do processes need dynamic allocation of memory?
• Do not know amount of memory needed at compile time • Must be pessimistic when allocate memory statically • Allocate enough for worst possible case; Storage is used inefficiently
Efficiency
• Do not waste memory resources (minimize fragmentation)
Base and Bounds DISADVANTAGES
• Each process must be allocated contiguously in physical memory • Must allocate memory that may not be used by process (e.g., hole between stack and heap) • No partial sharing: Cannot share limited parts of address space
Disadvantages of Segmentation
• Each segment must be allocated contiguously • May not have sufficient physical memory for large segments ...Fix in next lecture with paging...
Protection
• Goal of multiprogramming • Cannot corrupt OS or other processes • Privacy: Cannot read data of other processes
Transparency
• Goal of multiprogramming • Processes are not aware that memory is shared • Works regardless of number and/or location of processes
Static Relocation
• Idea: OS rewrites each program before loading it as a processing memory (by adding address where it will be loaded to address in program) • Each rewrite for different process uses different addresses and pointers • Change jumps, loads of static data • Disadvantages: No protection, cannot move address space after placed
What is OS's role in managing heap?
• OS gives big chunk of free memory to process • library manages individual allocations
Base and Bounds ADVANTAGES
• Provides protection (both read and write) across address spaces • Supports dynamic relocation • Simple, inexpensive implementation (Few registers, little logic in MMU) • Fast (Add and compare in parallel)
multiprogramming (or multitasking)
• more than one program can be run as a process at any given time • modern systems