CS 153 Design of Operating Systems Spring 2015 Lecture 17: Paging.

Slides:



Advertisements
Similar presentations
Paging 1 CS502 Spring 2006 Paging CS-502 Operating Systems.
Advertisements

EECS 470 Virtual Memory Lecture 15. Why Use Virtual Memory? Decouples size of physical memory from programmer visible virtual memory Provides a convenient.
OS Fall’02 Virtual Memory Operating Systems Fall 2002.
Segmentation and Paging Considerations
May 7, A Real Problem  What if you wanted to run a program that needs more memory than you have?
Computer Organization CS224 Fall 2012 Lesson 44. Virtual Memory  Use main memory as a “cache” for secondary (disk) storage l Managed jointly by CPU hardware.
Lecture 34: Chapter 5 Today’s topic –Virtual Memories 1.
1 A Real Problem  What if you wanted to run a program that needs more memory than you have?
CSE 490/590, Spring 2011 CSE 490/590 Computer Architecture Virtual Memory I Steve Ko Computer Sciences and Engineering University at Buffalo.
CS 153 Design of Operating Systems Spring 2015
CS 153 Design of Operating Systems Spring 2015
Memory/Storage Architecture Lab Computer Architecture Virtual Memory.
OS Spring ‘04 Paging and Virtual Memory Operating Systems Spring 2004.
Paging and Virtual Memory. Memory management: Review  Fixed partitioning, dynamic partitioning  Problems Internal/external fragmentation A process can.
Memory Management. 2 How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines.
CS-3013 & CS-502 Summer 2006 Paging1 CS-3013 & CS-502 Summer 2006.
Memory Management CSE451 Andrew Whitaker. Big Picture Up till now, we’ve focused on how multiple programs share the CPU Now: how do multiple programs.
Memory Management ◦ Operating Systems ◦ CS550. Paging and Segmentation  Non-contiguous memory allocation  Fragmentation is a serious problem with contiguous.
CS 153 Design of Operating Systems Spring 2015 Final Review.
1 Chapter 3.2 : Virtual Memory What is virtual memory? What is virtual memory? Virtual memory management schemes Virtual memory management schemes Paging.
Operating Systems ECE344 Ding Yuan Paging Lecture 8: Paging.
Chapter 9: Virtual Memory Background Demand Paging Copy-on-Write Page Replacement Allocation of Frames Thrashing Memory-Mapped Files Allocating Kernel.
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming  To allocate scarce memory resources.
Chapter 8 – Main Memory (Pgs ). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.
© 2004, D. J. Foreman 1 Virtual Memory. © 2004, D. J. Foreman 2 Objectives  Avoid copy/restore entire address space  Avoid unusable holes in memory.
Fundamentals of Programming Languages-II Subject Code: Teaching SchemeExamination Scheme Theory: 1 Hr./WeekOnline Examination: 50 Marks Practical:
CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming.  To allocate scarce memory.
CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY Memory Management and Virtual.
Paging (continued) & Caching CS-3013 A-term Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Demand Paging.
Processes and Virtual Memory
CS307 Operating Systems Virtual Memory Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2012.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Virtual Memory Implementation.
CS2100 Computer Organisation Virtual Memory – Own reading only (AY2015/6) Semester 1.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Virtual Memory: Concepts Slides adapted from Bryant.
Virtual Memory Ch. 8 & 9 Silberschatz Operating Systems Book.
Memory Management Continued Questions answered in this lecture: What is paging? How can segmentation and paging be combined? How can one speed up address.
1 Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples (not covered.
Memory Management. 2 How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines.
Computer Architecture Lecture 12: Virtual Memory I
Translation Lookaside Buffer
Virtual Memory CSSE 332 Operating Systems
CSE 120 Principles of Operating
A Real Problem What if you wanted to run a program that needs more memory than you have? September 11, 2018.
Virtual Memory - Part II
CS703 - Advanced Operating Systems
CS 704 Advanced Computer Architecture
Memory Management and Virtual Memory
CSE 120 Principles of Operating
CS510 Operating System Foundations
CSE 153 Design of Operating Systems Winter 2018
CSE 153 Design of Operating Systems Winter 2018
Lecture 28: Virtual Memory-Address Translation
Translation Lookaside Buffer
Morgan Kaufmann Publishers Memory Hierarchy: Virtual Memory
CSE 451: Operating Systems Autumn 2005 Memory Management
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE451 Virtual Memory Paging Autumn 2002
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
CSE 451: Operating Systems Autumn 2004 Page Tables, TLBs, and Other Pragmatics Hank Levy 1.
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
Computer Architecture
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
CSE 153 Design of Operating Systems Winter 2019
Paging and Segmentation
CSE 451: Operating Systems Lecture 10 Paging & TLBs
CSE 451: Operating Systems Winter 2005 Page Tables, TLBs, and Other Pragmatics Steve Gribble 1.
CSE 153 Design of Operating Systems Winter 2019
Virtual Memory Use main memory as a “cache” for secondary (disk) storage Managed jointly by CPU hardware and the operating system (OS) Programs share main.
Presentation transcript:

CS 153 Design of Operating Systems Spring 2015 Lecture 17: Paging

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

3 Paged Virtual Memory l We’ve mentioned before that pages can be moved between memory and disk u This process is called demand paging l OS uses main memory as a page cache of all the data allocated by processes in the system u Initially, pages are allocated from memory u When memory fills up, allocating a page in memory requires some other page to be evicted from memory u Evicted pages go to disk (where? the swap file/backing store) u The movement of pages between memory and disk is done by the OS, and is transparent to the application

4 Page Faults l 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 l But where does it put it? Have to evict something else u OS usually keeps a pool of free pages around so that allocations do not always cause evictions

5 Address Translation Redux l We started this topic with the high-level problem of translating virtual addresses into physical addresses l We’ve covered all of the pieces u Virtual and physical addresses u Virtual pages and physical page frames u Page tables and page table entries (PTEs), protection u TLBs u Demand paging l Now let’s put it together, bottom to top

6 The Common Case l Situation: Process is executing on the CPU, and it issues a read to an address u What kind of address is it? Virtual or physical? l 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 l Note: This is all done by the hardware

7 TLB Misses l 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 l We’ll consider each in turn

8 Reloading the TLB l If the TLB does not have mapping, 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 l A machine will only support one method or the other l At this point, there is a PTE for the address in the TLB

9 TLB Misses (2) Note that: l Page table lookup (by HW or OS) can cause a recursive fault if page table is paged out u Assuming page tables are in OS virtual address space u Not a problem if tables are in physical memory u Yes, this is a complicated situation! l When TLB has PTE, it restarts translation u 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 u Uncommon case is that TLB faults again on PTE because of PTE protection/valid bits (e.g., page is invalid) »Becomes a page fault…

TLB Misses (3) MMU Cache/ Memory PA Data CPU VA CPU Chip and 3 can both trigger a fault TLB VPN 10 3 VA of PT

11 Page Faults l PTE can indicate a protection fault u Read/write/execute – operation not permitted on page u Invalid – virtual page not allocated, or page not in physical memory l TLB traps to the OS (software takes over) u R/W/E – OS usually will send fault back up to process, or might be playing games (e.g., copy on write, mapped files) u Invalid »Virtual page not allocated in address space n OS sends fault to process (e.g., segmentation fault) »Page not in physical memory n OS allocates frame, reads from disk, maps PTE to physical frame

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

13 Sharing l Private virtual address spaces protect applications from each other u Usually exactly what we want l But this makes it difficult to share data (have to copy) u Parents and children in a forking Web server or proxy will want to share an in-memory cache without copying l We can use shared memory to allow processes to share data using direct memory references u Both processes see updates to the shared memory segment »Process B can immediately read an update by process A

l How can we implement sharing using page tables? u Have PTEs in both tables map to the same physical frame u Each PTE can have different protection values u Must update both PTEs when page becomes invalid 14 Sharing (2) Page frame P2’s Page Table Physical Memory Page frame P1’s Page Table How are we going to coordinate access to shared data?

Process perspective 15 Page 2 P1 Page 1 P2 Physical Memory

16 Sharing (3) l Can map shared memory at same or different virtual addresses in each process’ address space u 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 n What happens if it points to data inside/outside the segment? u Same: »2 nd physical page corresponds to the 10 th virtual page in both P1 and P2 »Less flexible, but shared pointers are valid

17 Copy on Write l OSes spend a lot of time copying data u System call arguments between user/kernel space u Entire address spaces to implement fork() l Use Copy on Write (CoW) to defer large copies as long as possible, hoping to avoid them altogether u Instead of copying pages, create shared mappings of parent pages in child virtual address space u 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 u How does this help fork()?

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

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

20 Mapped Files l Mapped files enable processes to do file I/O using loads and stores u Instead of “open, read into buffer, operate on buffer, …” l Bind a file to a virtual memory region (mmap() in Unix) u PTEs map virtual addresses to physical frames holding file data u Virtual address base + N refers to offset N in file l Initially, all pages mapped to file are invalid u OS reads a page from file when invalid page is accessed

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

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

23 Mapped Files (2) l File is essentially backing store for that region of the virtual address space (instead of using the swap file) u Virtual address space not backed by “real” files also called Anonymous VM l Advantages u Uniform access for files and memory (just use pointers) u Less copying l Drawbacks u Process has less control over data movement »OS handles faults transparently u Does not generalize to streamed I/O (pipes, sockets, etc.)

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

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

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