CS162 Operating Systems and Systems Programming Lecture 14

CS162 Operating Systems and Systems Programming Lecture 14

CS162 Operating Systems and Systems Programming Lecture 14 Demand Paging October 17, 2019 Prof. David E. Culler http://cs162.eecs.Berkeley.edu Read: A&D Ch 9 Recall: The Memory Hierarchy Managed in Hardware Processor TLB PT PT PT PT Secondary Secondary Storage Main TLB Storage (Disk) Memory (SSD) (DRAM) Accessed in Hardware 100,000 10,000,000 0.3 1 3 10-30 100 Speed (ns): (0.1 ms) (10 ms) Size (bytes):100Bs 10kBs 100kBs MBs GBs 100GBs TBs 10/17/2019 CS162 ©UCB Fa19 Lec 15.2 Recall: How is the Translation Accomplished? Virtual Physical Addresses CPU MMU Addresses • What does the MMU need to do to translate an address? • 1-level Page Table – Read PTE from memory, check valid, merge address – Set “accessed” bit in PTE, Set “dirty bit” on write • 2-level Page Table – Read and check first level – Read, check, and update PTE • N-level Page Table … • MMU does page table Tree Traversal to translate each address • How can we make this go REALLY fast? – Fraction of a processor cycle 10/17/2019 CS162 ©UCB Fa19 Lec 15.3 Recall: How do we make Address Translation Fast? • Cache results of recent translations ! – Different from a traditional cache – Cache Page Table Entries using Virtual Page # as the key X > m> Physical V_Addr Phs_Addr Memory Read < Read < Processor data MMU Cache(s) pgm (core) page PTBR Cache Lines tables V_Pg M1 : <Phs_Frame #1, V, … > V_Pg M2 : <Phs_Frame #2, V, … > V_Pg Mk : <Phs_Frame #k, V, … > 10/17/2019 CS162 ©UCB Fa19 Lec 15.4 How to organize the TLB? • Small – hit time must be fraction of cycle time – 100’s of entries (like registers) not K’s or M’s • High Associativity – Each entry acts like 4KB line (without the linear time to fill) – Conflict potential is high (many address per line, few lines) – Miss penalty is high (multi-level PT) • Split Instruction and Data TLBs 10/17/2019 CS162 ©UCB Fa19 Lec 15.5 Two Critical Issues in Address Translation • What to do if the translation fails? - a page fault • How to translate addresses fast enough? – Every instruction fetch – Plus every load / store – EVERY MEMORY REFERENCE ! – More than one translation for EVERY instruction 0x000… Processor Memory “virtual address” “physical address” translator Registers 0xFFF… 10/17/2019 CS162 ©UCB Fa19 Lec 15.6 Page Fault • The Virtual-to-Physical Translation fails – PTE marked invalid, Priv. Level Violation, Access violation, or does not exist – Causes an Fault / Trap » Not an interrupt because synchronous to instruction execution – May occur on instruction fetch or data access – Protection violations typically terminate the instruction • Other Page Faults engage operating system to fix the situation and retry the instruction – Allocate an additional stack page, or – Make the page accessible - Copy on Write, – Bring page in from secondary storage to memory – demand paging • Fundamental inversion of the hardware / software boundary 10/17/2019 CS162 ©UCB Fa19 Lec 15.7 What happens when … Process virtual address physical address page# instruction MMU frame# PT offset retry exception page fault frame# Operating System offset update PT entry Page Fault Handler load page from disk scheduler 10/17/2019 CS162 ©UCB Fa19 Lec 15.8 Inversion of the Hardware / Software Boundary • In order for an instruction to complete … • It requires the intervention of operating system software • Receive the page fault, remedy the situation – Load the page, create the page, copy-on-write – Update the PTE entry so the translation will succeed • Restart (or resume) the instruction – This is one of the huge simplifications in RISC instructions sets – Can be very complex when instruction modify state (x86) 10/17/2019 CS162 ©UCB Fa19 Lec 15.9 Precise Exceptions • Precise Þ state of the machine is preserved as if program executed up to the offending instruction – All previous instructions completed – Offending instruction and all following instructions act as if they have not even started – Same system code will work on different implementations – Difficult in the presence of pipelining, out-of-order execution, ... • Imprecise Þ system software has to figure out what is where and put it all back together • Performance goals may lead designers to forsake precise interrupts – system software developers, user, markets etc. usually wish they had not done this • Modern techniques for out-of-order execution and branch prediction help implement precise interrupts • Architectural support for OS is hard – Original M68000 had paging, but didn’t save fault address properly – Original Sun Unix workstation used two, running one-cycle apart !#%$! 10/17/2019 CS162 ©UCB Fa19 Lec 15.10 Demand Paging as Caching, … • What “block size”? - 1 page (e.g, 4 KB) • What “organization” ie. direct-mapped, set-assoc., fully-associative? – Any page in any frame of memory, i.e., fully associative: arbitrary virtual ® physical mapping • How do we locate a page? – First check TLB, then page-table traversal • What is page replacement policy? (i.e. LRU, Random…) – This requires more explanation… (kinda LRU) • What happens on a miss? – Go to lower level to fill miss (i.e. disk) • What happens on a write? (write-through, write back) – Definitely write-back – need dirty bit! 10/17/2019 CS162 ©UCB Fa19 Lec 15.11 Demand Paging • Modern programs require a lot of physical memory – Memory per system growing faster than 25%-30%/year • But they don’t use all their memory all of the time – 90-10 rule: programs spend 90% of their time in 10% of their code – Wasteful to require all of user’s code to be in memory • Solution: use main memory as “cache” for disk Processor paging Control caching Te r t i a r y Cache On Second Main Secondary Storage - Level Memory Storage (Tape) Chip Datapath Cache (DRAM) (Disk) (SRAM) 10/17/2019 CS162 ©UCB Fa19 Lec 15.12 Illusion of Infinite Memory ¥ TLB Page Table Physical Disk Virtual Memory 500GB Memory 512 MB 4 GB • Disk is larger than physical memory Þ – In-use virtual memory can be bigger than physical memory – Combined memory of running processes much larger than physical memory » More programs fit into memory, allowing more concurrency • Principle: Transparent Level of Indirection (page table) – Supports flexible placement of physical data » Data could be on disk or somewhere across network – Variable location of data transparent to user program » Performance issue, not correctness issue 10/17/2019 CS162 ©UCB Fa19 Lec 15.13 Review: What is in a PTE? • What is in a Page Table Entry (or PTE)? – Pointer to next-level page table or to actual page – Permission bits: valid, read-only, read-write, write-only • Example: Intel x86 architecture PTE: – 2-level page tabler (10, 10, 12-bit offset) – Intermediate page tables called “Directories” PCD PWT Page Frame Number Free 0 L D A U W P (Physical Page Number) (OS) 31-12 11-9 8 7 6 5 4 3 2 1 0 P: Present (same as “valid” bit in other architectures) W: Writeable U: User accessible PWT: Page write transparent: external cache write-through PCD: Page cache disabled (page cannot be cached) A: Accessed: page has been accessed recently D: Dirty (PTE only): page has been modified recently L: L=1Þ4MB page (directory only). Bottom 22 bits of virtual address serve as offset 10/17/2019 CS162 ©UCB Fa19 Lec 15.14 Origins of Paging Keep most of the Disks provide address space on most of the disk storage Actively swap pages to/from Relatively small memory, for many processes Keep memory full of the frequently accesses pages P Many clients on dumb terminals . running different programs 10/17/2019 CS162 ©UCB Fa19 Lec 15.15 Recall: Management of the Memory Hierarchy Managed in Managed in Software - OS ? Hardware Processor TLB PT PT PT PT Secondary Secondary Storage Main TLB Storage (Disk) Memory (SSD) (DRAM) Accessed in Hardware 100,000 10,000,000 0.3 1 3 10-30 100 Speed (ns): (0.1 ms) (10 ms) Size (bytes):100Bs 10kBs 100kBs MBs GBs 100GBs TBs 10/17/2019 CS162 ©UCB Fa19 Lec 15.16 Very Different Situation Today Powerful system Huge memory Huge disk Single user 10/17/2019 CS162 ©UCB Fa19 Lec 15.17 A Picture on one machine • Memory stays about 75% used, 25% for dynamics • A lot of it is shared 1.9 GB 10/17/2019 CS162 ©UCB Fa19 Lec 15.18 Many Uses of ”Demand Paging” … • Extend the stack – Allocate a page and zero it • Extend the heap (sbrk of old, today mmap) • Process Fork – Create a copy of the page table – Entries refer to parent pages – NO-WRITE – Shared read-only pages remain shared – Copy page on write • Exec ??? • MMAP to explicitly share region (or to access a file as RAM) 10/17/2019 CS162 ©UCB Fa19 Lec 15.19 Classic: Loading an executable into memory disk (huge) memory info data code exe • .exe – lives on disk in the file system – contains contents of code & data segments, relocation entries and symbols – OS loads it into memory, initializes registers (and initial stack pointer) – program sets up stack and heap upon initialization: crt0 (C runtime init) 10/17/2019 CS162 ©UCB Fa19 Lec 15.20 Create Virtual Address Space of the Process disk (huge) process VAS memory kernel user page frames stack sbrk user heap pagetable data kernel code code & data • Utilized pages in the VAS are backed by a page block on disk – Called the backing store or swap file – Typically in an optimized block store, but can think of it like a file 10/17/2019 CS162 ©UCB Fa19 Lec 15.21 Create Virtual Address Space of the Process disk (huge, TB) process VAS (GBs) memory kernel stack user page stack frames heap heap user data pagetable data code kernel code code & data • User Page table maps entire VAS • All the utilized regions are backed on disk – swapped into and out of memory

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    73 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