Lecture 15: Virtual Memory

Lecture 15: Virtual Memory

Lecture 15: Virtual Memory Fall 2018 Jason Tang Slides based upon Operating System Concept slides, http://codex.cs.yale.edu/avi/os-book/OS9/slide-dir/index.html Copyright Silberschatz, Galvin, and Gagne, 2013 "1 Topics • Multi-level Paging! • Virtual Memory! • Page Fault "2 Structure of the Page Table • Many architectures default to 4 KiB pages (12 bits)! • For a 32-bit address, that means upper 20 bits give a page number, and lower 12 bits are an o$set into the resulting physical page! • To fully utilize a 4 GiB address space (32 bits), number of page table entries is% % • If each page table entry is 32 bits (4 bytes), then entire page table requires 222 bytes = 4 MiB (which is greater than a page itself)! • Solution is to have Hierarchical Paging or Demand Paging "3 Hierarchical Page Tables "4 Two-Level Paging Example • For example, on a 32-bit machine, let page frames be 1 KiB large! • Thus page offset = 10 bits, so page numbers = 22 bits! • If page table is also paged (into 1 KiB pages), then page number can be split as page offset2 = 10 bits and page number1 = 12 bits! • This is a forward-mapped page table "5 Address-Translation Scheme • p1 = outer page table, p2 = inner page table! • 64-bit addressing may have 3 or 4 levels of paging! • More paging means longer e$ective access times "6 Partially-Loaded Programs • Entire program does not need to be in physical memory to execute! • Error code, unusual routines, large data structures can be excluded, for now! • Partially-loaded program can start running! • Program not constrained by limits of physical memory! • Can have more simultaneous processes "7 Overlays • Old technique where a process partially replaces itself with overlays! • Overlay manager loads overlays from secondary storage and overwrites unneeded memory! • Requires lots of work by programmer to ensure that overlays do not interact! • Still used in some embedded systems and systems lacking paging hardware "8 Virtual Memory • Separation of logical memory from physical memory! • Logical address space can be (and usually) larger than physical address space! • Virtual address space: logical view of how process is stored in memory! • Usually starts at virtual address 0! • Kernel maps virtual pages to physical page frames "9 Virtual Memory "10 Virtual Address Space • Usually, heap grows upwards towards max logical address, while stack grows downwards towards zero! • Hole between the two is unused, and no physical memory needed until heap or stack grows to a new page (sparse addressing)! • System libraries shared via mapping into multiple virtual address spaces! • Shared memory implemented by mapping pages into multiple virtual address spaces "11 Shared Library "12 Demand Paging • Load process from secondary storage into memory only when needed! • Less initial I/O, faster startup time! • When page is needed, suspend process until loaded! • Lazy swapper: never swap a page into memory unless page will be needed! • Kernel swapper deals entire processes! • Kernel pager deals only with pages "13 Paging • Every page entry has valid-invalid bit:! • Valid means page is legal and in memory (memory resident)! • Invalid means page illegal or page has been swapped out (not memory resident)! • Each process’s control block has a list of all valid pages for that process! • Upon reference to an invalid page, MMU sends a page fault interrupt to CPU "14 Page Fault • Upon page fault, OS examines process’s memory map to decide:! • If invalid reference, abort process (segmentation fault, Windows BSOD)! • If page is not memory-resident, then OS:! • Finds free frame! • Swaps page into frame! • Updates page table, this time marking page entry as valid! • Restarts process at instruction that caused page fault "15 Handling Page Fault "16 Demand Paging • Pure demand paging: start process with no pages in memory, and use page faults to load program incrementally! • A single instruction could access multiple pages (and thus cause multiple page faults)! • Example: Fetch 2 numbers, add them, and write to di$erent location! • Locality of reference: instructions tend to refer to spatially nearby addresses! • Hardware required to support demand paging! • Page table with valid/invalid bit, secondary storage, instruction restart "17 Restarting Instructions • Example: Fetch 2 numbers, add them, and write to di$erent location! • If page fault occurred during writing, should hardware refetch operands?! • What if source values are in shared memory and that memory is overwritten?! • Depends on hardware:! • Raise page fault if any part of instruction will access an invalid page! • Use temporary registers to store partial computations "18 Demand Paging Performance • Servicing a page fault interrupt involves:! • Context switch overhead! • Find and load page (OS can do other things while data swapped back in)! • Restart process! • Let p = page fault rate, where if p = 0 then no page faults and if p =1 then every reference is a fault! • E$ective access time = (1 - p) & memory access + p (page fault overhead + swap page out + swap page in) "19 Demand Paging Example • Let memory access time = 200 ns and sum of page-fault service time = 8 ms! • EAT = (1 - p) & 200 ns + p (8,000,000 ns)! • If one access out of 1000 causes page fault (p = 0.1%), then EAT = 8.2 µs! • As compared to 200 ns access time, this is a slowdown by a factor of 40! • To get performance degradation less than 10%, then:! • 2000 ns = (1 - p) & 200 ns + p (8,000,000 ns), so p ' 0.02475%! • Less than 1 page fault in every 400,000 memory accesses "20 Copy-on-Write • Page entries can also be marked as copy-on-write (COW)! • When writing to a COW page, OS will first copy the target frame’s contents to a new frame, and then update page entry to new frame! • Used for vfork() and threading "21 Copy-on-Write Implementation • Linux implements vfork() and threads via paging! • Reading from a COW page permitted as normal! • Writing to a COW page causes page fault! • Kernel duplicates page, updates page table, then restarts instruction! • Very e(cient, because programs tend to perform many more reading than writing operations! • Race condition in older Linux kernel led to Dirty COW exploit "22 Copy-on-Write Implementation • Before process attempts to modify Page C! • Result of modification, after Copy-on-Write handling "23.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    23 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us