Virtual Memory I. Jo, Heeseung

Similar documents
Paging. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

VIRTUAL MEMORY. Operating Systems 2015 Spring by Euiseong Seo

Paging. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Memory Management. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Recap: Memory Management

Paging. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

CS153: Memory Management 1

Paging. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CS 153 Design of Operating Systems Winter 2016

Page Tables. 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

Address Translation. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

MEMORY MANAGEMENT. Jo, Heeseung

VIRTUAL MEMORY II. Jo, Heeseung

Memory Management. Jo, Heeseung

CSE 120 Principles of Operating Systems

Goals of memory management

CSE 120 Principles of Operating Systems Spring 2017

Virtual Memory. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CS 153 Design of Operating Systems Winter 2016

CS 318 Principles of Operating Systems

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

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

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

CS252 S05. Main memory management. Memory hardware. The scale of things. Memory hardware (cont.) Bottleneck

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

Chapter 8. Virtual Memory

Processes and Virtual Memory Concepts

Virtual or Logical. Logical Addr. MMU (Memory Mgt. Unit) Physical. Addr. 1. (50 ns access)

Lecture 8: Memory Management

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

Computer Systems. Virtual Memory. Han, Hwansoo

Memory Management. Goals of Memory Management. Mechanism. Policies

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

Memory Management - Demand Paging and Multi-level Page Tables

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

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

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

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

2-Level Page Tables. Virtual Address Space: 2 32 bytes. Offset or Displacement field in VA: 12 bits

Main Memory: Address Translation

How to create a process? What does process look like?

CSC 369 Operating Systems

Operating Systems. Memory Management. Lecture 9 Michael O Boyle

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

Basic Memory Management

virtual memory. March 23, Levels in Memory Hierarchy. DRAM vs. SRAM as a Cache. Page 1. Motivation #1: DRAM a Cache for Disk

CS 153 Design of Operating Systems Spring 18

Lecture 12: Demand Paging

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

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

Virtual Memory. Kevin Webb Swarthmore College March 8, 2018

Computer Architecture. Lecture 8: Virtual Memory

Goals of Memory Management

Operating Systems. Operating Systems Sina Meraji U of T

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

Reminder: Mechanics of address translation. Paged virtual memory. Reminder: Page Table Entries (PTEs) Demand paging. Page faults

MEMORY MANAGEMENT/1 CS 409, FALL 2013

Virtual Memory. Chapter 8

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

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Virtual Memory Management

Chapter 8 & Chapter 9 Main Memory & Virtual Memory

CSE 153 Design of Operating Systems

Memory Management! How the hardware and OS give application pgms:" The illusion of a large contiguous address space" Protection against each other"

The Virtual Memory Abstraction. Memory Management. Address spaces: Physical and Virtual. Address Translation

Memory Hierarchy Requirements. Three Advantages of Virtual Memory

Memory Hierarchy. Goal: Fast, unlimited storage at a reasonable cost per bit.

Random-Access Memory (RAM) Systemprogrammering 2007 Föreläsning 4 Virtual Memory. Locality. The CPU-Memory Gap. Topics

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

Memory management, part 3: outline

Random-Access Memory (RAM) Systemprogrammering 2009 Föreläsning 4 Virtual Memory. Locality. The CPU-Memory Gap. Topics! The memory hierarchy

198:231 Intro to Computer Organization. 198:231 Introduction to Computer Organization Lecture 14

CHAPTER 3 RESOURCE MANAGEMENT

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

Virtual Memory III. Jo, Heeseung

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

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

CS 153 Design of Operating Systems Winter 2016

CS399 New Beginnings. Jonathan Walpole

Memory Management! Goals of this Lecture!

Virtual Memory Virtual memory first used to relive programmers from the burden of managing overlays.

14 May 2012 Virtual Memory. Definition: A process is an instance of a running program

Operating System Labs. Yuanbin Wu

Memory Management Part 1. Operating Systems in Depth XX 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Opera&ng Systems ECE344

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

Virtual Memory Oct. 29, 2002

Chapter 9 Memory Management Main Memory Operating system concepts. Sixth Edition. Silberschatz, Galvin, and Gagne 8.1

Basic Memory Management. Basic Memory Management. Address Binding. Running a user program. Operating Systems 10/14/2018 CSC 256/456 1

Chapter 8: Main Memory

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

Memory Management. Goals of this Lecture. Motivation for Memory Hierarchy

Address spaces and memory management

UC Berkeley CS61C : Machine Structures

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

CS 550 Operating Systems Spring Memory Management: Paging

MEMORY: SWAPPING. Shivaram Venkataraman CS 537, Spring 2019

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

Transcription:

Virtual Memory I Jo, Heeseung

Today's Topics Virtual memory implementation Paging Segmentation 2

Paging Introduction Physical memory Process A Virtual memory Page 3 Page 2 Frame 11 Frame 10 Frame 9 4KB Page 1 Page 0 Page table A Frame 8 Frame 7 Frame 6 Frame 5 User's view - Contiguous - Large address space Frame 4 Frame 3 Frame 2 Frame 1 Frame 0 User space Kernel space 3

Paging (1) Paging Permits the physical address space of a process to be noncontiguous Divide physical memory into fixed-sized blocks called frames Divide logical memory into blocks of same size called pages - Page (or frame) size is power of 2 (typically, 512B - 8KB) - Mostly use 4K in modern OS To run a program of size n pages, need to find n free frames and load program OS keeps track of all free frames Set up a page table to translate virtual to physical addresses 4

Paging (2) Physical memory Process A Virtual memory Page 3 Page 2 Frame 11 Frame 10 Frame 9 Process B Page 1 Page 0 Page 5 Page 4 Page table A Frame 8 Frame 7 Frame 6 Frame 5 Frame 4 Page 3 Frame 3 Page 2 Frame 2 Page 1 Page 0 Page table B Frame 1 Frame 0 User space Kernel space 5

Paging (3) User's perspective Users (and processes) view memory as one contiguous address space from 0 through N - Virtual address space (VAS) In reality, pages are scattered throughout the physical memory - Virtual-to-physical mapping - This mapping is invisible to the program Protection is provided because a program cannot reference memory outside of its VAS - The virtual address 0xdeadcafe maps to different physical addresses for different processes 6

Virtual Memory (1) Example #include <stdio.h> int n = 0; int main () { printf ("&n = 0x%08x\n", &n); } %./a.out &n = 0x08049508 %./a.out &n = 0x08049508 What happens if two users simultaneously run this application? 7

Paging (4) Translating addresses A virtual address has two parts: <virtual page number (VPN)::offset> VPN is an index in a page table Page table determines page frame number (PFN) Physical address is <PFN::offset> Page tables Managed by OS Map VPN to PFN - VPN is the index into the table that determines PFN One page table entry (PTE) per page in virtual address space, i.e. one PTE per VPN 8

Paging (5) Address translation architecture 9

Paging (6) Paging example Virtual address (32bits) Virtual address: 32 bits (4G) Physical address: 20 bits (1M) Page size: 4KB Offset: 12 bits VPN: 20 bits Page table entries: 2 20 0 0 0 0 4 A F E VPN 2 7 D 0 3 1 F E B 6 A 4 Offset Page tables PFN B 6 A F E Physical address (20bits) 10

Translation Using a Page Table 11

Mapping Pages to Storage 12

Paging (7) Page Table Entries (PTEs) 1 1 1 2 20 V R M Prot Page Frame Number (PFN) Valid bit (V) says whether or not the PTE can be used - It is checked each time a virtual address is used Reference bit (R) says whether the page has been accessed - It is set when a read or write to the page occurs Modify bit (M) says whether or not the page is dirty - It is set when a write to the page occurs Protection bits (Prot) control which operations are allowed on the page - Read, Write, Execute, etc. Page frame number (PFN) determines physical page 13

Paging (8) Advantages Easy to allocate physical memory - Physical memory is allocated from free list of frames - To allocate a frame, just remove it from its free list No external fragmentation Easy to "page out" chunks of a program - All chunks are the same size (page size) - Use valid bit to detect reference to "paged-out" pages - Pages sizes are usually chosen to be convenient multiple of disk block sizes Easy to protect pages from illegal accesses Easy to share pages 14

Paging (9) Disadvantages Can still have internal fragmentation - Process may not use memory in exact multiple of pages Memory reference overhead (Performance overhead) - 2 references per address lookup (page table, then memory) - Solution: get a hardware support (TLB) Memory required to hold page tables can be large (Space overhead) - Need one PTE per page in virtual address space - 32-bit address space with 4KB pages = 2 20 PTEs - 4 bytes/pte = 4MB per page table - OS's typically have separate page tables per process (25 processes = 100MB of page tables) - Solution: page the page tables, multi-level page tables, inverted page tables, etc. 15

Paging Summary 1 1 1 2 20 V R M Prot Page Frame Number (PFN) Virtual memory Page 3 Page 2 Page 1 Page 0 17

Demand Paging (1) Demand paging Bring a page into memory only when it is needed - Less I/O needed - Less memory needed - Faster response - More users OS uses main memory as a (page) cache - Cache of all of the data allocated by processes in the system - When physical memory fills up, replacing (eviction and load) Evicted pages go to disk - Only need to write if they are dirty - Evict to a swap file on disk - Movement of pages between memory/disks is done by the OS - Transparent to the application 18

Demand Paging (2) Page faults Referencing a virtual address in a evicted page - When the page was evicted, the OS sets the PTE as invalid - Stores (in PTE) the location of the page in the swap file - Accessing the page cause an exception The OS will run the page fault handler in response - Locating the page in swap file via invalid PTE - Handler reads page into a physical frame - Updates PTE to point to it and to be valid - Handler restarts the faulted process Where does the page that's read in go? - Have to evict something else - Which one? -> page replacement algorithm - OS typically tries to keep a pool of free pages around so that allocations don't inevitably cause evictions 19

Demand Paging (3) Handling a page fault page_fault_handler() {...... } 20

Demand Paging (4) Why does this work? Locality - Temporal locality: locations referenced recently tend to be referenced again soon - Spatial locality: locations near recently referenced locations are likely to be referenced soon Locality means paging can be infrequent - Once you've paged something in, it will be used many times - On average, you use things that are paged in - But this depends on many things: Degree of locality in application Page replacement policy Amount of physical memory Application's reference pattern and memory footprint 21

Demand Paging (5) Why is this "demand" paging? When a process first starts up, it has a brand new page table, with all PTE valid bits "false" - All pages are empty - No pages are yet mapped to physical memory When the process starts executing: - Instructions immediately fault on both code and data pages (Cold miss / Cold page fault) - Faults stop when all necessary code/data pages are in memory - Only the code/data that is needed (demanded!!) by process needs to be loaded 22

Segmentation (1) Segmentation Partitions memory into logically related data units - Code, stack, heap, etc. Users view memory as a collection of variable-sized segments - With no necessary ordering among them - Virtual address: <Segment #::Offset> Different segments can grow or shrink independently - Without affecting each other Natural extension of variable-sized partitions - Variable-size partitions: 1 segment / process - Segmentation: many segments / process 23

Segmentation (2) User's view of a program 24

Segmentation (3) 25

Segmentation (4) Hardware support Multiple base/limit pairs, one per segment (segment table) Segments are named by segment #, used to index into table Physical memory limit base Segment# offset <? Yes + Virtual address No protection fault 26

Segmentation (5) Advantages Simplifies the handling of data structures that are growing or shrinking Easy to protect segments - With each entry in segment table, associate a valid bit - Protection bits (read/write/execute) are also associated with each segment table entry Easy to share segments - Put same translation into base/limit pair - Code/data sharing occurs at segment level - e.g. shared libraries 27

Segmentation (6) Disadvantages Cross-segment addresses - Segments need to have same segment # for pointers to them to be shared among processes - Otherwise, use indirect addressing only Large segment tables - Keep in main memory, use hardware cache for speed External fragmentation - Since segments vary in length, memory allocation is a dynamic storage-allocation problem 28

Paging vs. Segmentation (1) Paging Segmentation Block size Fixed (4KB to 64KB) Variable Linear address space 1 Many Memory addressing Replacement One word (page number + offset) Easy (all same size) Two words (segment + offset) Difficult (find where segment fits) Fragmentation Internal External Disk traffic Transparent to the programmers? Efficient (optimized for page size) Yes Inefficient (may have small or large transfers) No 29

Paging vs. Segmentation (2) Can the total address space exceed the size of physical memory? Can codes and data be distinguished and separately protected? Can tables whose size fluctuates be accommodated easily? Is sharing of codes between users facilitated? Why was this technique invented? Paging Yes No No No large linear address space Segmentation Yes Yes Yes Yes logically independent address spaces (sharing and protection) 30

Paging vs. Segmentation (3) Hybrid approaches Paged segments - Segmentation with Paging - Segments are a multiple of a page size Multiple page sizes - 4KB, 2MB, and 4MB page sizes are supported in IA32-8KB, 16KB, 32KB or 64KB in Alpha AXP Architecture (43, 47, 51, or 55 bits virtual address) 31

Segmentation with Paging (1) Combine segmentation and paging Use segments to manage logically related units - Code, data, heap, etc. - Segments vary in size, but usually large (multiple pages) Use pages to partition segments into fixed size chunks - Makes segments easier to manage with in physical memory - Segments become "pageable" rather than moving segments into and out of memory, just move page portions of segments - No external fragmentation The IA-32 supports segments and paging 32

Segmentation with Paging (2) IA-32 33