CS 153 Design of Operating Systems Winter 2016

Similar documents
CSE 120 Principles of Operating Systems Spring 2017

CS 318 Principles of Operating Systems

CSE 120 Principles of Operating Systems

Opera&ng Systems ECE344

Lecture 12: Demand Paging

Paging. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Recap: Memory Management

Paging. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CSE 153 Design of Operating Systems

Address Translation. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CS 153 Design of Operating Systems Winter 2016

VIRTUAL MEMORY II. Jo, Heeseung

Paging. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS 153 Design of Operating Systems

Paging. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CSE120 Principles of Operating Systems. Prof Yuanyuan (YY) Zhou Advanced Memory Management

VIRTUAL MEMORY. Operating Systems 2015 Spring by Euiseong Seo

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

16 Sharing Main Memory Segmentation and Paging

15 Sharing Main Memory Segmentation and Paging

Virtual Memory I. Jo, Heeseung

Virtual Memory III. Jo, Heeseung

PAGE REPLACEMENT. Operating Systems 2015 Spring by Euiseong Seo

CS399 New Beginnings. Jonathan Walpole

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

Memory Management. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Address spaces and memory management

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

Memory Management Topics. CS 537 Lecture 11 Memory. Virtualizing Resources

Virtual Memory 1. To do. q Segmentation q Paging q A hybrid system

Memory Management - Demand Paging and Multi-level Page Tables

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

Modeling Page Replacement: Stack Algorithms. Design Issues for Paging Systems

Lecture 21: Virtual Memory. Spring 2018 Jason Tang

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

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

More on Address Translation. CS170 Fall T. Yang Some of slides from UCB CS162 by Kubiatowicz

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

The cow and Zaphod... Virtual Memory #2 Feb. 21, 2007

Memory Management. Goals of Memory Management. Mechanism. Policies

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

PROCESS VIRTUAL MEMORY PART 2. CS124 Operating Systems Winter , Lecture 19

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

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

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Memory Management

Virtual Memory. Today. Segmentation Paging A good, if common example

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

CS162 Operating Systems and Systems Programming Lecture 12. Address Translation. Page 1

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

Segmentation (Apr 5 and 7, 2005)

CS399 New Beginnings. Jonathan Walpole

Translation Buffers (TLB s)

CS370 Operating Systems

Topic 18: Virtual Memory

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

Page 1. Goals for Today" TLB organization" CS162 Operating Systems and Systems Programming Lecture 11. Page Allocation and Replacement"

Recall from Tuesday. Our solution to fragmentation is to split up a process s address space into smaller chunks. Physical Memory OS.

CS162 Operating Systems and Systems Programming Lecture 10 Caches and TLBs"

Memory management OS

CSE 351. Virtual Memory

Main Memory: Address Translation

CS 31: Intro to Systems Virtual Memory. Kevin Webb Swarthmore College November 15, 2018

CS162 Operating Systems and Systems Programming Lecture 11 Page Allocation and Replacement"

Virtual Memory. Virtual Memory

Paging! 2/22! Anthony D. Joseph and Ion Stoica CS162 UCB Fall 2012! " (0xE0)" " " " (0x70)" " (0x50)"

CS162 - Operating Systems and Systems Programming. Address Translation => Paging"

Chapter 8 & Chapter 9 Main Memory & Virtual Memory

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 24

Computer Systems II. Memory Management" Subdividing memory to accommodate many processes. A program is loaded in main memory to be executed

Chapters 9 & 10: Memory Management and Virtual Memory

Memory management: outline

Memory management: outline

Chapter 8: Main Memory

Basic Memory Management

Operating Systems. Paging... Memory Management 2 Overview. Lecture 6 Memory management 2. Paging (contd.)

Chapter 8: Memory-Management Strategies

Virtual Memory. Computer Systems Principles

Lecture 4: Memory Management & The Programming Interface

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

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

CPS 104 Computer Organization and Programming Lecture 20: Virtual Memory

EECS 482 Introduction to Operating Systems

Performance of Various Levels of Storage. Movement between levels of storage hierarchy can be explicit or implicit

Chapter 6: Demand Paging

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

Topics: Memory Management (SGG, Chapter 08) 8.1, 8.2, 8.3, 8.5, 8.6 CS 3733 Operating Systems

CHAPTER 8 - MEMORY MANAGEMENT STRATEGIES

CS162 Operating Systems and Systems Programming Lecture 14. Caching (Finished), Demand Paging

CHAPTER 8: MEMORY MANAGEMENT. By I-Chen Lin Textbook: Operating System Concepts 9th Ed.

Virtual Memory. CS 351: Systems Programming Michael Saelee

Chapter 8: Main Memory. Operating System Concepts 9 th Edition

CIS Operating Systems Memory Management Cache and Demand Paging. Professor Qiang Zeng Spring 2018

Virtual Memory Management

Processes and Virtual Memory Concepts

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

Chapter 8: Main Memory

Chapter 5 (Part II) Large and Fast: Exploiting Memory Hierarchy. Baback Izadi Division of Engineering Programs

Page 1. Review: Address Segmentation " Review: Address Segmentation " Review: Address Segmentation "

Transcription:

CS 153 Design of Operating Systems Winter 2016 Lecture 17: Paging

Lecture Overview Recap: Today: Goal of virtual memory management: map 2^32 byte address space to physical memory Internal fragmentation with fixed size partitions External fragmentation with variable size partitions Paging is a good trade-off implemented in most Oses Segmentation (possibly combined with paging) How to reduce space overhead of page tables? How to make page table lookups fast? Advanced functionality with pages 2

Paged Virtual Memory We ve mentioned 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 Initially, pages are allocated from memory When memory fills up, allocating 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 application 3

Memory Hierarchy Virtual memory space Physical memory (cache) Disk 4

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 location of the page in the swap file in the PTE 2. When a process accesses the page, the invalid PTE will cause a page fault 3. This will result in the execution of the OS page fault handler 4. Handler uses the invalid PTE to locate page in swap file 5. Reads page into memory, updates PTE to point to it 6. Restarts process But where does it put it? Have to evict something else OS usually keeps a pool of free pages around so that allocations do not always cause evictions 5

Address Translation Redux We started this topic with the high-level problem of translating 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), protection TLBs Demand paging Now let s put it together, bottom to top 6

The Common Case Situation: Process is executing 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 protection 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 7

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 valid/protection bits We ll consider each in turn 8

Case 1: Reloading the TLB If the TLB does not have mapping (page fault), two possibilities: 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» Software managed TLB, OS intervenes at this point» OS does lookup in page table, loads PTE into TLB» OS returns from exception, TLB continues A machine will only support one method or the other At this point, there is a PTE for the address in the TLB 9

Case 2: Second Page Fault Note that: Page table lookup (by HW or OS) can cause a recursive fault if page table is paged out Assuming page tables are in OS virtual address space Not a problem if tables are in physical memory Yes, this is a complicated situation! When TLB has PTE, it restarts translation Common case is that the PTE refers to a valid page in memory» These faults are handled quickly, just read PTE from the page table in memory and load into TLB Uncommon case is that TLB faults again on PTE because of PTE protection/valid bits (e.g., page is invalid)» Becomes a page fault 10

Page Faults PTE can indicate a protection fault Read/write/execute operation not permitted on page Invalid virtual page not allocated, or page not in physical memory TLB traps to the OS (software 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., segmentation fault)» Page not in physical memory OS allocates frame, reads from disk, maps PTE to physical frame 11

Advanced Functionality Now we re going to look at some advanced functionality that the OS can provide applications using virtual memory tricks Shared memory Copy on Write Mapped files 12

Sharing Private virtual address spaces protect applications 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 13

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 protection values Must update both PTEs when page becomes invalid P1 s Page Table Physical Memory Page frame Page frame P2 s Page Table How are we going to coordinate access to shared data? 14

Process perspective P1 Page 1 Physical Memory Page 2 P2 15

Sharing (3) Can map shared memory at same or different virtual addresses in each process address space Different:» 10 th virtual page in P1 and 7 th virtual page in P2 correspond to the 2 nd physical page» Flexible (no address space conflicts), but pointers inside the shared memory segment are invalid Same: What happens if it points to data inside/outside the segment?» 2 nd physical page corresponds to the 10 th virtual page in both P1 and P2» Less flexible, but shared pointers are valid 16

Copy on Write OSes spend a lot of time copying data System call arguments between user/kernel space Entire 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 protection fault, trap to OS, copy page, change page mapping in client page table, restart write instruction How does this help fork()? 17

Execution of fork() Parent process s page table Page 1 Page 2 Physical Memory Page 1 Page 2 Child process s page table 18

fork() with Copy on Write Parent process s page table Page 1 Page 2 When either process modifies Page 1, page fault handler allocates new page and updates PTE in child process Physical Memory Protection bits set to prevent either process from writing to any page Page 1 Page 2 Child process s page table Under what circumstances such copies can be deferred forever? 19

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 Initially, all pages mapped to file are invalid OS reads a page from file when invalid page is accessed 20

Memory-Mapped Files Pages are all invalid initially P1 Physical Memory A read occurs A read occurs Page 1 Page 2 File Content 1 File Content 2 What happens if we unmap the memory? How do we know whether we need to write changes back to file? 21

Writing Back to File OS writes a page to file when evicted, or region unmapped If page is not dirty (has not been written to), no write needed Dirty bit trick (not protection bits) 22

Summary Paging mechanisms: Optimizations Managing page tables (space) Efficient translations (TLBs) (time) Demand paged virtual memory (space) Recap address translation Advanced Functionality Sharing memory Copy on Write Mapped files Next time: Paging policies 24

Next time Read chapter on page replacement policies linked from course web page 25

Todo Add picture showing how TLB is used Picture for copy-on-write 26