CS5460: Operating Systems Lecture 13: Memory Management (Chapter 8)
Where are we? Basic OS structure, HW/SW interface, interrupts, scheduling Concurrency Memory management Storage management Other topics
Example Virtual Address Space Typical address space has 4 parts : binary image of program Data: static variables (globals) : explicitly allocated data (malloc) : implicitly allocated data Kernel mapped into all processes MMU hardware: Remaps virtual addresses to physical Supports read-only, supervisor-only Detects accesses to unmapped regions How can we load two processes into memory at same time? Assume each has similar layout 0xFFFFFFFF SP HP Kernel User PC 0x80000000 User stack segment User heap User data segment User code segment Kernel heap Kernel data segment Kernel code segment 0x00000000
Mapping Addresses How can process (virtual) addresses be mapped to physical addresses? Compile time: Compiler generates physical addresses directly» Advantages: No MMU hardware, no runtime translation overhead» Disadvantages: Inflexible, hard to multiprogram, inefficient use of DRAM Load time: OS loader fixes addresses when it loads program» Advantages: Can support static multiprogramming» Disadvantages: MMU hardware, inflexible, hard to share data, Dynamic: Compiler generates address, but OS/HW reinterpret» Advantages: Very flexible, can use memory efficiently» Disadvantages: MMU hardware req d, runtime translation overhead For real OSes, processes only use virtual addresses Small-sized embedded systems use physical addresses
Uniprogramming (e.g., DOS) One process at a time User code compiled to sit in fixed range (e.g., [0,640 KB]) No hardware virtualization of addresses OS in separate addresses E.g., above 640KB Goals: Safety: None (good and bad) Efficiency: Poor (I/O and compute not overlapped, response time) 0xFFFFFFFF 0xA0000 SP HP PC 0x00000000 Reserved for DOS kernel (Dynamically allocated) Uninitialized data (BSS segment) Static data (Data segment) (Text segment)
Multiprogramming: Static Relocation OS loader relocates programs OS stored in reserved high region Compiler maps process starting at 0 When process started, OS loader:» Allocates contiguous physical memory» Uses relocation info in binary to fix up addresses to relocated region TSRs in DOS based on this technique Problems: Finding/creating contiguous holes Dealing with processes that grow/shrink Goals: Safety: None! à process can destroy other processes Efficiency: Poor à only one segment per process; slow load times; no sharing 0xFFFFFFFF SP 1 HP 1 PC 1 SP 0 HP 0 PC 0 0x00000000 Reserved for OS kernel Data Data
Idea: Dynamic Relocation Programs all laid out the same Relocate addresses when used Requires hardware support Two views of memory: Virtual: Process s view Physical: Machine s view Many variants Base and bounds Segmentation Paging Segmented paging Virtual Addresses 0xFFFFFFFF SP 0 HP 0 PC 0 0x00000000 0xFFFFFFFF SP 1 HP 1 PC 1 Data OS kernel Data OS kernel Physical Addresses Data Data OS kernel 0x00000000
Base and Bounds Each process mapped to contiguous physical region Two hardware registers Base: Starting physical address Bounds: Size in bytes On each reference: Check against bounds Add base to get physical address Evaluation: Good points: Bad points: OS handled specially Example: Cray-1 Virtual address P 1 VAs P 0 VAs Virtual Physical address 0x00000 0x7ffff 0x00000 0x7ffff 0x00000 Trap >? + OS kernel Data Data Bounds register Base register Physical 0xFFFFFFFF Bounds 1 Base 1 Bounds 0 Base 0 0x00000000
Base and Bounds Each process has private address space No relocation done at load time Operating system handled specially Runs with relocation turned off (i.e., ignores Base and Bounds) Only OS can modify Base and Bounds registers Good points: Very simple hardware Bad points: Only one contiguous segment per process à inhibits sharing External fragmentation à need to find or make holes Hard to grow segments
Segmentation Idea: Create N separate segments Each segment has separate base and bounds register Segment number is fixed portion of virtual address Seg# Offset >? Error! (Trap) Base Base Base Bounds Bounds Bounds + Physical address Base Base Bounds Bounds
Segmentation Example Virtual address space is 2000 bytes in size 4 segments up to 500 bytes each Starting at 0, 500, 1000, 1500 What if processor accesses VA 0 VA 1040 VA 1900 VA 920 VA 1898 What if we allocate: 100-byte segment 200-byte segment Base Bounds 0 1000 400 1 0 500 2 600 300 3 1500 400 Segment Table Virtual Address Physical Address 2000 Seg3 Seg2 Seg3 Seg0 1000 Seg1 Seg2 Seg0 Seg1 0
Good features: Segmentation Discussion More flexible than base and bounds à enables sharing (How?) Reduces severity of fragmentation (How?) Small hardware table (e.g., 8 segments) à can fit all in processor Problems: Still have fragmentation à How? What kind? Hard to grow segments à Why? Non-contiguous virtual address space à Real problem? Possible solutions: Fragmentation: Copy and compact Growing segments: Copy and compact Paging
Paging Problem w/ segmentation à variable-sized segments Solution à Paging! Insist that all chunks be the same size (typically 512-8K bytes) Call them pages rather than segments Allocation is done in terms of full page-aligned pages à no bounds MMU maps virtual page numbers to physical page numbers Virtual Page# Offset Wired concatenate Physical Page# Physical Page# Other Other Physical Page# Offset Physical address Physical Page# Physical Page# Other Other What other info?
How does this help? Paging Discussion No external fragmentation! No forced holes in virtual address space Easy translation à everything aligned on power-of-2 addresses Easy for OS to manage/allocate free memory pool What problems are introduced? What if you do not need entire page? Internal fragmentation Page table may be large» Where should we put it? How can we do fast translation if not stored in processor? How big should you make your pages?» Large: Smaller table, demand paging more efficient» Small: Less fragmentation, finer grained sharing, larger page table
Paging Examples Page Table Assume 1000-byte pages What if processor accesses: VA 0 VA 1040 VA 2900 VA 920 VA 4998 0 1 2 3 4 PPN Valid R/O Super 3 Y N Y 8 N N N 5 Y Y N 7 Y N N 1 N Y Y Virtual Address Physical Address VP1 VP3 VP4 VP3 VP2 VP1 VP0 VP2 VP0 VP4 Free List
x86 Paging x86 typically uses 4 KB pages Virtual addresses are 32 bits How big is the offset field of a virtual address? How big is the virtual page number field? How many pages are in a virtual address space? How big is a flat page table? Assume PTE (page table entry) is 32 bits
Key Idea From Today Address space virtualization Programs see virtual addresses Kernel can see both virtual and physical addresses Virtual and physical address spaces need not be the same size You must understand this to understand modern operating systems Kernel + HW supports the virtual to physical mapping Has to be fast There are different ways to do it Modern OSes use paging