Virtual Memory CS 351: Systems Programming Michael Saelee <lee@iit.edu>
Computer Science Science registers cache (SRAM) main memory (DRAM) local hard disk drive (HDD/SSD) remote storage (networked drive / cloud) previously: SRAM ⇔ DRAM
Computer Science Science registers cache (SRAM) main memory (DRAM) local hard disk drive (HDD/SSD) remote storage (networked drive / cloud) next: DRAM ⇔ HDD, SSD, etc. i.e., memory as a “cache” for disk
Computer Science Science main goals: 1. maximize memory throughput 2. maximize memory utilization 3. provide address space consistency & memory protection to processes
Computer Science Science throughput = # bytes per second - depends on access latencies (DRAM, HDD) and “hit rate”
Computer Science Science utilization = fraction of allocated memory that contains “user” data (aka payload ) - vs. metadata and other overhead required for memory management
Computer Science Science address space consistency → provide a uniform “view” of memory to each process
Computer 0xffffffff Science Science Memory Kernel virtual memory invisible to (code, data, heap, stack) user code 0xc0000000 User stack (created at runtime) %esp (stack pointer) Memory mapped region for shared libraries 0x40000000 brk Run-time heap (created by malloc ) Read/write segment ( .data , .bss ) Loaded from the executable file Read-only segment ( .init , .text , .rodata ) 0x08048000 Unused 0 address space consistency → provide a uniform “view” of memory to each process
Computer Science Science memory protection → prevent processes from directly accessing each other’s address space
Computer Science Science 0xffffffff 0xffffffff 0xffffffff Memory Memory Memory Kernel virtual memory Kernel virtual memory Kernel virtual memory invisible to invisible to invisible to (code, data, heap, stack) (code, data, heap, stack) (code, data, heap, stack) user code user code user code 0xc0000000 0xc0000000 0xc0000000 User stack User stack User stack (created at runtime) (created at runtime) (created at runtime) %esp (stack pointer) %esp (stack pointer) %esp (stack pointer) Memory mapped region for Memory mapped region for Memory mapped region for shared libraries shared libraries shared libraries 0x40000000 0x40000000 0x40000000 brk brk brk Run-time heap Run-time heap Run-time heap (created by malloc ) (created by malloc ) (created by malloc ) Read/write segment Read/write segment Read/write segment ( .data , .bss ) ( .data , .bss ) ( .data , .bss ) Loaded from the Loaded from the Loaded from the executable file executable file executable file Read-only segment Read-only segment Read-only segment ( .init , .text , .rodata ) ( .init , .text , .rodata ) ( .init , .text , .rodata ) 0x08048000 0x08048000 0x08048000 Unused Unused Unused 0 0 0 P 0 P 1 P 2 memory protection → prevent processes from directly accessing each other’s address space
Computer Science Science i.e., every process should be provided with a managed, virtualized address space
Computer Science Science “memory addresses”: what are they, really?
Computer Science Science Main Memory N (note: cache not shown) CPU address: N data “physical” address: (byte) index into DRAM
Computer Science Science (gdb) set detach-on-fork off (gdb) break main Breakpoint 1 at 0x400508: file memtest.c, line 7. (gdb) run Breakpoint 1, main () at memtest.c:7 7 fork(); (gdb) next [New process 7450] 8 glob += 1; (gdb) print &glob int glob = 0xDEADBEEE; $1 = (int *) 0x6008d4 parent (gdb) next main() { 9 } fork(); (gdb) print /x glob glob += 1; $2 = 0xdeadbeef } (gdb) inferior 2 [Switching to inferior 2 [process 7450] #0 0x000000310acac49d in __libc_fork () 131 pid = ARCH_FORK (); (gdb) finish Run till exit from #0 in __libc_fork () 8 glob += 1; (gdb) print /x glob child $4 = 0xdeadbeee (gdb) print &glob $5 = (int *) 0x6008d4
Computer Science Science Main Memory N CPU address: N data instructions executed by the CPU do not refer directly to physical addresses!
Computer Science Science processes reference virtual addresses, the CPU relays virtual address requests to the memory management unit (MMU), which are translated to physical addresses
Computer Science Science Main Memory MMU physical address address virtual address CPU translation unit disk address (note: cache not shown) “swap” space
Computer Science Science essential problem: translate request for a virtual address → physical address … this must be FAST , as every memory access from the CPU must be translated
Computer Science Science both hardware/software are involved: - MMU (hw) handles simple and fast operations (e.g., table lookups) - Kernel (sw) handles complex tasks (e.g., eviction policy)
Computer Science Science §Virtual Memory Implementations
Computer Science Science keep in mind goals: 1. maximize memory throughput 2. maximize memory utilization 3. provide address space consistency & memory protection to processes
Computer Science Science 1. simple relocation Main Memory N+B B P 0 N 0 0
Computer Science Science 1. simple relocation Main Memory CPU MMU N B relocation reg. B VA: N PA: N+B data - per-process relocation address is loaded by kernel on every context switch
Computer Science Science 1. simple relocation Main Memory CPU MMU N B relocation reg. B VA: N PA: N+B data - problem: processes may easily overextend their bounds and trample on each other
Computer Science Science 1. simple relocation Main Memory MMU B+L limit reg. process CPU L sandbox N B relocation reg. B VA: N PA: N+B data assert (0 ≤ N ≤ L ) - incorporate a limit register to provide memory protection
Computer Science Science 1. simple relocation Main Memory MMU B+L limit reg. process CPU L sandbox N B relocation reg. B VA: N PA: N+B data assert (0 ≤ N ≤ L ) - assertion failure triggers a fault, which summons kernel (which signals process)
Computer Science Science pros: - simple & fast! - provides protection
stack Computer Science Science heap Virtual Memory Main Memory Main Memory stack stack data data heap heap code code B vs. data data code code B but: available memory for mapping depends on value of base address i.e., address spaces are not consistent !
Computer Science Science Main Memory virtual address space stack L stack possibly unused! code B code 0 also: all of a process below the address limit must be loaded in memory i.e., memory may be vastly under-utilized
Computer Science Science 2. segmentation - partition virtual address space into multiple logical segments - individually map them onto physical memory with relocation registers
Computer Science Science Segmented Virtual Main Memory Address Space B 2 +L 2 MMU 0 B 2 Segment Table Seg # 3 : Stack Base Limit 0 B 0 L 0 1 B 3 +L 3 B 1 L 1 2 B 2 L 2 0 B 3 Seg # 2 : Heap 3 B 3 L 3 B 0 +L 0 B 0 0 Seg # 1 : Data B 1 +L 1 0 Seg # 0 : Code B 1 virtual address has form seg#:offset
Computer Science Science MMU Main Memory Segment Table B 2 +L 2 Base Limit 0 B 0 L 0 1 B 1 L 1 B 2 2 B 2 L 2 3 B 3 L 3 B 3 +L 3 B 3 VA: seg#:o ff set CPU B 0 +L 0 PA: o ff set + B 2 ⊕ B 0 B 1 +L 1 assert ( o ff set ≤ L 2 ) B 1 data
Computer Science Science Segment Table Base Limit 0 B 0 L 0 1 B 1 L 1 2 B 2 L 2 3 B 3 L 3 - implemented as MMU registers - part of kernel-maintained, per-process metadata (aka “process control block”) - re-populated on each context switch
Computer Science Science pros: - still very fast - translation = register access & addition - memory protection via limits - segmented addresses improve consistency
Computer Science Science Main Memory virtual address space simple stack L stack relocation: possibly unused! code B code 0 virtual Main Memory address space stack segmentation: stack 0 better! code code 0
Computer Science Science virtual virtual Main Memory address address space space 2 x stack stack x stack 0 x 0 code code 0 code 0 - variable segment sizes → memory fragmentation - fragmentation potentially lowers utilization - can fix through compaction, but expensive!
Computer Science Science 3. paging - partition virtual and physical address spaces into uniformly sized pages - virtual pages map onto physical pages
Computer Science Science stack physical memory heap data code
Computer Science Science stack physical memory heap data code - minimum mapping granularity = page - not all of a given segment need be mapped
Computer Science Science modified mapping problem: - a virtual address is broken down into virtual page number & page offset - determine which physical page (if any) a given virtual page is loaded into - if physical page is found, use page offset to access data
Computer Science Science Given page size = 2 p bytes p VA: virtual page number virtual page offset p PA: physical page number physical page offset
Recommend
More recommend