virtual memory
play

Virtual Memory CS 351: Systems Programming Michael Saelee - PowerPoint PPT Presentation

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


  1. Virtual Memory CS 351: Systems Programming Michael Saelee <lee@iit.edu>

  2. Computer Science Science registers cache (SRAM) main memory (DRAM) local hard disk drive (HDD/SSD) remote storage (networked drive / cloud) previously: SRAM ⇔ DRAM

  3. 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

  4. Computer Science Science main goals: 1. maximize memory throughput 2. maximize memory utilization 3. provide address space consistency & memory protection to processes

  5. Computer Science Science throughput = # bytes per second - depends on access latencies (DRAM, HDD) and “hit rate”

  6. Computer Science Science utilization = fraction of allocated memory that contains “user” data (aka payload ) - vs. metadata and other overhead required for memory management

  7. Computer Science Science address space consistency → provide a uniform “view” of memory to each process

  8. 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

  9. Computer Science Science memory protection → prevent processes from directly accessing each other’s address space

  10. 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

  11. Computer Science Science i.e., every process should be provided with a managed, virtualized address space

  12. Computer Science Science “memory addresses”: what are they, really?

  13. Computer Science Science Main Memory N (note: cache not shown) CPU address: N data “physical” address: (byte) index into DRAM

  14. 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

  15. Computer Science Science Main Memory N CPU address: N data instructions executed by the CPU do not refer directly to physical addresses!

  16. 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

  17. Computer Science Science Main Memory MMU physical address address virtual address CPU translation unit disk address (note: cache not shown) “swap” space

  18. 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

  19. 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)

  20. Computer Science Science §Virtual Memory Implementations

  21. Computer Science Science keep in mind goals: 1. maximize memory throughput 2. maximize memory utilization 3. provide address space consistency & memory protection to processes

  22. Computer Science Science 1. simple relocation Main Memory N+B B P 0 N 0 0

  23. 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

  24. 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

  25. 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

  26. 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)

  27. Computer Science Science pros: - simple & fast! - provides protection

  28. 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 !

  29. 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

  30. Computer Science Science 2. segmentation - partition virtual address space into multiple logical segments - individually map them onto physical memory with relocation registers

  31. 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

  32. 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

  33. 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

  34. Computer Science Science pros: - still very fast - translation = register access & addition - memory protection via limits - segmented addresses improve consistency

  35. 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

  36. 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!

  37. Computer Science Science 3. paging - partition virtual and physical address spaces into uniformly sized pages - virtual pages map onto physical pages

  38. Computer Science Science stack physical memory heap data code

  39. Computer Science Science stack physical memory heap data code - minimum mapping granularity = page - not all of a given segment need be mapped

  40. 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

  41. 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend