Opera&ng Systems ECE344

Similar documents
CSE 120 Principles of Operating Systems Spring 2017

CSE 120 Principles of Operating Systems

CS 318 Principles of Operating Systems

CS 153 Design of Operating Systems Winter 2016

Lecture 8: Memory Management

Lecture 12: Demand Paging

CS 153 Design of Operating Systems Winter 2016

Address Translation. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

VIRTUAL MEMORY II. Jo, Heeseung

Recap: Memory Management

CSE 451: Operating Systems Winter Page Table Management, TLBs and Other Pragmatics. Gary Kimura

Virtual Memory: Concepts

CSE 153 Design of Operating Systems

Virtual Memory: Concepts

Paging. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Paging. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

This lecture. Virtual Memory. Virtual memory (VM) CS Instructor: Sanjeev Se(a

Virtual Memory: Systems

Virtual Memory: From Address Translation to Demand Paging

CSE 351. Virtual Memory

Main Points. Address Transla+on Concept. Flexible Address Transla+on. Efficient Address Transla+on

CS 153 Design of Operating Systems

Memory Management. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Recall: Address Space Map. 13: Memory Management. Let s be reasonable. Processes Address Space. Send it to disk. Freeing up System Memory

Virtual Memory. Patterson & Hennessey Chapter 5 ELEC 5200/6200 1

Paging. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Learning Outcomes. An understanding of page-based virtual memory in depth. Including the R3000 s support for virtual memory.

Learning Outcomes. An understanding of page-based virtual memory in depth. Including the R3000 s support for virtual memory.

Multi-level Translation. CS 537 Lecture 9 Paging. Example two-level page table. Multi-level Translation Analysis

A Few Problems with Physical Addressing. Virtual Memory Process Abstraction, Part 2: Private Address Space

CS399 New Beginnings. Jonathan Walpole

Virtual Memory: From Address Translation to Demand Paging

Paging. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Translation Buffers (TLB s)

Virtual Memory 2. Today. Handling bigger address spaces Speeding translation

CS 152 Computer Architecture and Engineering CS252 Graduate Computer Architecture. Lecture 8 Address Transla>on

Virtual Memory 2. To do. q Handling bigger address spaces q Speeding translation

Virtual Memory. Today. Handling bigger address spaces Speeding translation

Virtual Memory III /18-243: Introduc3on to Computer Systems 17 th Lecture, 23 March Instructors: Bill Nace and Gregory Kesden

Virtual Memory Review. Page faults. Paging system summary (so far)

Lecture 21: Virtual Memory. Spring 2018 Jason Tang

CS 152 Computer Architecture and Engineering CS252 Graduate Computer Architecture. Lecture 9 Virtual Memory

Virtual Memory. Virtual Memory

Memory: Page Table Structure. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

Virtual Memory. Daniel Sanchez Computer Science & Artificial Intelligence Lab M.I.T. November 15, MIT Fall 2018 L20-1

Address spaces and memory management

VIRTUAL MEMORY. Operating Systems 2015 Spring by Euiseong Seo

Virtual Memory I. Jo, Heeseung

Chapter 8 Virtual Memory

CS 333 Introduction to Operating Systems. Class 11 Virtual Memory (1) Jonathan Walpole Computer Science Portland State University

Virtual Memory 2. q Handling bigger address spaces q Speeding translation

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 24

16 Sharing Main Memory Segmentation and Paging

Virtual Memory. Daniel Sanchez Computer Science & Artificial Intelligence Lab M.I.T. April 12, 2018 L16-1

15 Sharing Main Memory Segmentation and Paging

Virtual Memory III. Jo, Heeseung

ECE 571 Advanced Microprocessor-Based Design Lecture 13

PAGE REPLACEMENT. Operating Systems 2015 Spring by Euiseong Seo

Operating Systems. Operating Systems Sina Meraji U of T

CS 153 Design of Operating Systems

Topic 18: Virtual Memory

Virtual Memory. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

CS5460: Operating Systems Lecture 14: Memory Management (Chapter 8)

Transla'on, Protec'on, and Virtual Memory. 2/25/16 CS 152 Sec'on 6 Colin Schmidt

Virtual Memory: Systems

Segmentation (Apr 5 and 7, 2005)

CS 152 Computer Architecture and Engineering. Lecture 11 - Virtual Memory and Caches

CPS104 Computer Organization and Programming Lecture 16: Virtual Memory. Robert Wagner

CSE 120. Translation Lookaside Buffer (TLB) Implemented in Hardware. July 18, Day 5 Memory. Instructor: Neil Rhodes. Software TLB Management

CPS 104 Computer Organization and Programming Lecture 20: Virtual Memory

ECE232: Hardware Organization and Design

Virtual Memory. Samira Khan Apr 27, 2017

Memory Hierarchy Requirements. Three Advantages of Virtual Memory

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

CS 61C: Great Ideas in Computer Architecture. Lecture 23: Virtual Memory. Bernhard Boser & Randy Katz

Caching and Demand- Paged Virtual Memory

Virtual to physical address translation

Main Memory. CS 4410, Opera3ng Systems. Lorenzo Alvisi Anne Bracy. Spring 2017 Cornell University. See: Ch 8 & 9 in OSPP textbook

Multi-Process Systems: Memory (2) Memory & paging structures: free frames. Memory & paging structures. Physical memory

The memory of a program. Paging and Virtual Memory. Swapping. Paging. Physical to logical address mapping. Memory management: Review

Chapter 5. Large and Fast: Exploiting Memory Hierarchy. Part II Virtual Memory

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

Carnegie Mellon. 16 th Lecture, Mar. 20, Instructors: Todd C. Mowry & Anthony Rowe

Background. IBM sold expensive mainframes to large organiza<ons. Monitor sits between one or more OSes and HW

Virtual Memory. Computer Systems Principles

Virtual Memory. Motivations for VM Address translation Accelerating translation with TLBs

@2010 Badri Computer Architecture Assembly II. Virtual Memory. Topics (Chapter 9) Motivations for VM Address translation

Virtual Memory Oct. 29, 2002

CS 61C: Great Ideas in Computer Architecture. Lecture 23: Virtual Memory

Processes and Virtual Memory Concepts

Computer Systems. Virtual Memory. Han, Hwansoo

6.004 Tutorial Problems L20 Virtual Memory

Virtual Memory. Spring Instructors: Aykut and Erkut Erdem

Virtual Memory. CS61, Lecture 15. Prof. Stephen Chong October 20, 2011

Virtual Memory II. CSE 351 Autumn Instructor: Justin Hsia

Virtual Memory #2 Feb. 21, 2018

Memory Allocation. Copyright : University of Illinois CS 241 Staff 1

HY225 Lecture 12: DRAM and Virtual Memory

Virtual Memory. CS 351: Systems Programming Michael Saelee

Another View of the Memory Hierarchy. Lecture #25 Virtual Memory I Memory Hierarchy Requirements. Memory Hierarchy Requirements

Transcription:

Opera&ng Systems ECE344 Lecture 8: Paging Ding Yuan

Lecture Overview Today we ll cover more paging mechanisms: Op&miza&ons Managing page tables (space) Efficient transla&ons (TLBs) (&me) Demand paged virtual memory (space) Recap address transla&on Advanced Func&onality Sharing memory Copy on Write Mapped files ECE344 Lecture 8: Paging Ding Yuan 2

Review: Page Lookups Example: how do we load 0x00007468? Virtual Address Physical Memory 0 0 0 0 7 4 6 8 Virtual page number index........ 0x00006 Offset page frame Physical Address 0x0002 468 0x0002467 0x0002468........ A 0x00007........ 0x0002 Page Table ECE344 Lecture 8: Paging Ding Yuan 3

Review: Two- Level Page Tables Physical Memory Virtual Address Master page number Secondary Offset Page table Physical Address Page frame Offset Master Page Table Page frame Secondary Page Table Problem: each memory access requires several actual memory accesses ECE344 Lecture 8: Paging Ding Yuan 4

Problems with Efficiency If OS is involved in every memory access for PT look up - - - too slow (user<- >kernel mode switch) Every look- up requires mul&ple memory access Our original page table scheme already doubled the cost of doing memory lookups One lookup into the page table, another to fetch the data Now two- level page tables triple the cost! Two lookups into the page tables, a third to fetch the data And this assumes the page table is in memory ECE344 Lecture 8: Paging Ding Yuan 5

Solu&on to Efficient Transla&on How can we use paging but also have lookups cost about the same as fetching from memory? Hardware Support Cache transla&ons in hardware Transla&on Lookaside Buffer (TLB) TLB managed by Memory Management Unit (MMU) ECE344 Lecture 8: Paging Ding Yuan 6

Transla&on Look- aside Buffer (TLB) ECE344 Lecture 8: Paging Ding Yuan 7

TLBs Transla&on Lookaside Buffers Translate virtual page #s into PTEs (not physical addrs) Can be done in a single machine cycle TLBs implemented in hardware Fully associa&ve cache (all entries looked up in parallel) Cache tags are virtual page numbers Cache values are PTEs (entries from page tables) With PTE + offset, can directly calculate physical address TLBs exploit locality Processes only use a handful of pages at a &me 16-48 entries/pages (64-192K) Only need those pages to be mapped Hit rates are therefore very important ECE344 Lecture 8: Paging Ding Yuan 8

TLB Example (VPN,Offset) VPN Offset VPN PPN VPN- >PPN PPN Offset (PPN,Offset) TLB ECE344 Lecture 8: Paging Ding Yuan 9

TLB Example (VPN,Offset) 7- >2 7 246 1 12 7 2 19 3 3 7 2 246 (PPN,Offset) TLB ECE344 Lecture 8: Paging Ding Yuan 10

TLB Example: next reference (VPN,Offset) 7- >2 7 248 1 12 7 2 19 3 3 7 2 248 (PPN,Offset) TLB ECE344 Lecture 8: Paging Ding Yuan 11

Managing TLBs Address transla&ons for most instruc&ons are handled using the TLB >99% of transla&ons, but there are misses (TLB miss) Who places transla&ons into the TLB (loads the TLB)? Hardware (Memory Management Unit) [x86] Knows where page tables are in main memory OS maintains tables, HW accesses them directly Tables have to be in HW- defined format (inflexible) Sotware loaded TLB (OS) [MIPS, Alpha, Sparc, PowerPC] TLB faults to the OS, OS finds appropriate PTE, loads it in TLB Must be fast (but s&ll 20-200 cycles) CPU ISA has instruc&ons for manipula&ng TLB Tables can be in any format convenient for OS (flexible) ECE344 Lecture 8: Paging Ding Yuan 12

Managing TLBs (2) OS ensures that TLB and page tables are consistent When it changes the protec&on bits of a PTE, it needs to invalidate the PTE if it is in the TLB Reload TLB on a process context switch Invalidate all entries (called TLB flush) Why? What is one way to fix it? When the TLB misses and a new PTE has to be loaded, a cached PTE must be evicted Choosing PTE to evict is called the TLB replacement policy Implemented in hardware (for hardware controlled TLBs), oten simple (e.g., Last- Not- Used) ECE344 Lecture 8: Paging Ding Yuan 13

Bits in a TLB entry Common (necessary) bits Virtual page number Physical page number Valid Access bits: kernel and user, read/write/execute Op&onal (useful) bits Process tag Reference Modify ECE344 Lecture 8: Paging Ding Yuan 14

Now we switch gear We ve men&oned before that pages can be moved between memory and disk This process is called demand paging OS uses main memory as a page cache of all the data allocated by processes in the system Ini&ally, pages are allocated from memory When memory fills up, alloca&ng a page in memory requires some other page to be evicted from memory Evicted pages go to disk (where? the swap file/ backing store) The movement of pages between memory and disk is done by the OS, and is transparent to the applica&on ECE344 Lecture 8: Paging Ding Yuan 15

Demand paging Real Memory ECE344 Lecture 8: Paging Ding Yuan 16

Demand paging Real Memory ECE344 Lecture 8: Paging Ding Yuan 17

Demand paging Real Memory ECE344 Lecture 8: Paging Ding Yuan 18

Demand paging Real Memory ECE344 Lecture 8: Paging Ding Yuan 19

Demand paging Real Memory ECE344 Lecture 8: Paging Ding Yuan 20

Demand paging Real Memory ECE344 Lecture 8: Paging Ding Yuan 21

Page Faults What happens when a process accesses a page that has been evicted? 1. When it evicts a page, the OS sets the PTE as invalid and stores the loca&on of the page in the swap file in the PTE 2. When a process accesses the page, the invalid PTE will cause a trap (page fault) 3. The trap will run the OS page fault handler 4. Handler uses the invalid PTE to locate page in swap file 5. Reads page into a physical frame, updates PTE to point to it 6. Restarts process But where does it put it? Have to evict something else ECE344 Lecture 8: Paging Ding Yuan 22

Address Transla&on Redux We started this topic with the high- level problem of transla&ng virtual addresses into physical addresses We ve covered all of the pieces Virtual and physical addresses Virtual pages and physical page frames Page tables and page table entries (PTEs), protec&on TLBs Demand paging Now let s put it together, bouom to top ECE344 Lecture 8: Paging Ding Yuan 23

The Common Case Situa&on: Process is execu&ng on the CPU, and it issues a read to an address What kind of address is it? Virtual or physical? The read goes to the TLB in the MMU 1. TLB does a lookup using the page number of the address 2. Common case is that the page number matches, returning a page table entry (PTE) for the mapping for this address 3. TLB validates that the PTE protec&on allows reads (in this example) 4. PTE specifies which physical frame holds the page 5. MMU combines the physical frame and offset into a physical address 6. MMU then reads from that physical address, returns value to CPU Note: This is all done by the hardware ECE344 Lecture 8: Paging Ding Yuan 24

TLB Misses At this point, two other things can happen 1. TLB does not have a PTE mapping this virtual address 2. PTE exists, but memory access violates PTE protec&on bits We ll consider each in turn ECE344 Lecture 8: Paging Ding Yuan 25

Reloading the TLB If the TLB does not have mapping, two possibili&es: 1. MMU loads PTE from page table in memory Hardware managed TLB, OS not involved in this step OS has already set up the page tables so that the hardware can access it directly 2. Trap to the OS Sotware managed TLB, OS intervenes at this point OS does lookup in page table, loads PTE into TLB OS returns from excep&on, TLB con&nues A machine will only support one method or the other At this point, there is a PTE for the address in the TLB ECE344 Lecture 8: Paging Ding Yuan 26

Page Faults PTE can indicate a protec&on fault Read/write/execute opera&on not permiued on page Invalid virtual page not allocated, or page not in physical memory TLB traps to the OS (sotware takes over) R/W/E OS usually will send fault back up to process, or might be playing games (e.g., copy on write, mapped files) Invalid Virtual page not allocated in address space OS sends fault to process (e.g., segmenta&on fault) Page not in physical memory OS allocates frame, reads from disk, maps PTE to physical frame ECE344 Lecture 8: Paging Ding Yuan 27

Advanced Func&onality Now we re going to look at some advanced func&onality that the OS can provide applica&ons using virtual memory tricks Copy on Write Mapped files Shared memory ECE344 Lecture 8: Paging Ding Yuan 28

Copy on Write OSes spend a lot of &me copying data System call arguments between user/kernel space En&re address spaces to implement fork() Use Copy on Write (CoW) to defer large copies as long as possible, hoping to avoid them altogether Instead of copying pages, create shared mappings of parent pages in child virtual address space Shared pages are protected as read- only in parent and child Reads happen as usual Writes generate a protec&on fault, trap to OS, copy page, change page mapping in client page table, restart write instruc&on How does this help fork()? ECE344 Lecture 8: Paging Ding Yuan 29

Mapped Files Mapped files enable processes to do file I/O using loads and stores Instead of open, read into buffer, operate on buffer, Bind a file to a virtual memory region (mmap() in Unix) PTEs map virtual addresses to physical frames holding file data Virtual address base + N refers to offset N in file Ini&ally, all pages mapped to file are invalid OS reads a page from file when invalid page is accessed OS writes a page to file when evicted, or region unmapped If page is not dirty (has not been wriuen to), no write needed Another use of the dirty bit in PTE ECE344 Lecture 8: Paging Ding Yuan 30

Sharing Private virtual address spaces protect applica&ons from each other Usually exactly what we want But this makes it difficult to share data (have to copy) Parents and children in a forking Web server or proxy will want to share an in- memory cache without copying We can use shared memory to allow processes to share data using direct memory references Both processes see updates to the shared memory segment Process B can immediately read an update by process A How are we going to coordinate access to shared data? ECE344 Lecture 8: Paging Ding Yuan 31

Sharing (2) How can we implement sharing using page tables? Have PTEs in both tables map to the same physical frame Each PTE can have different protec&on values Must update both PTEs when page becomes invalid Can map shared memory at same or different virtual addresses in each process address space Different: Flexible (no address space conflicts), but pointers inside the shared memory segment are invalid (Why?) Same: Less flexible, but shared pointers are valid (Why?) ECE344 Lecture 8: Paging Ding Yuan 32

Summary Paging mechanisms: Op&miza&ons Managing page tables (space) Efficient transla&ons (TLBs) (&me) Demand paged virtual memory (space) Recap address transla&on Advanced Func&onality Sharing memory Copy on Write Mapped files Next &me: Paging policies ECE344 Lecture 8: Paging Ding Yuan 33