Virtual Memory. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Similar documents
Paging. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Paging. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Memory Management. Jo, Heeseung

MEMORY MANAGEMENT. Jo, Heeseung

Page Tables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Paging. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

File System Implementation

Memory Management. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Virtual Memory I. Jo, Heeseung

Paging. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Operating Systems. Memory Management. Lecture 9 Michael O Boyle

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

Fall 2017 :: CSE 306. Introduction to. Virtual Memory. Nima Honarmand (Based on slides by Prof. Andrea Arpaci-Dusseau)

Introduction to Operating Systems

Recap: Memory Management

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

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

! What is main memory? ! What is static and dynamic allocation? ! What is segmentation? Maria Hybinette, UGA. High Address (0x7fffffff) !

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

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

VIRTUAL MEMORY. Operating Systems 2015 Spring by Euiseong Seo

Virtual Memory II CSE 351 Spring

Simple idea 1: load-time linking. Our main questions. Some terminology. Simple idea 2: base + bound register. Protection mechanics.

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

memory management Vaibhav Bajpai

Goals of Memory Management

Virtual Memory II. CSE 351 Autumn Instructor: Justin Hsia

Virtual Memory II. CSE 351 Autumn Instructor: Justin Hsia

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

16 Sharing Main Memory Segmentation and Paging

Process. One or more threads of execution Resources required for execution. Memory (RAM) Others

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

Processes, Address Spaces, and Memory Management. CS449 Spring 2016

Classifying Information Stored in Memory! Memory Management in a Uniprogrammed System! Segments of a Process! Processing a User Program!

CS399 New Beginnings. Jonathan Walpole

Computer Systems. Virtual Memory. Han, Hwansoo

Processes and Virtual Memory Concepts

Address Translation. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

8.1 Background. Part Four - Memory Management. Chapter 8: Memory-Management Management Strategies. Chapter 8: Memory Management

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

Chapter 8 & Chapter 9 Main Memory & Virtual Memory

15 Sharing Main Memory Segmentation and Paging

Part Three - Memory Management. Chapter 8: Memory-Management Strategies

Main Memory: Address Translation

EECS 482 Introduction to Operating Systems

Lecture 19: Virtual Memory: Concepts

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

Memory Management. To do. q Basic memory management q Swapping q Kernel memory allocation q Next Time: Virtual memory

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

Memory Management (Chaper 4, Tanenbaum)

Chapter 9 Memory Management

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

CSE 153 Design of Operating Systems

Multi-level Page Tables & Paging+ segmentation combined

Principles of Operating Systems

Operating System Labs. Yuanbin Wu

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

Memory: Overview. CS439: Principles of Computer Systems February 26, 2018

Memory Management. Memory Management

Page 1. Goals for Today" Virtualizing Resources" Important Aspects of Memory Multiplexing" CS162 Operating Systems and Systems Programming Lecture 20

Addresses in the source program are generally symbolic. A compiler will typically bind these symbolic addresses to re-locatable addresses.

Memory management OS

Chapter 7 Memory Management

Memory Management. Reading: Silberschatz chapter 9 Reading: Stallings. chapter 7 EEL 358

Chapter 8: Memory Management. Operating System Concepts with Java 8 th Edition

Process. One or more threads of execution Resources required for execution. Memory (RAM) Others

6. Which of the following operating systems was the predecessor of Microsoft MS-DOS? a. Dynix b. Minix c. CP/M

ECE 598 Advanced Operating Systems Lecture 10

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

Processes and Tasks What comprises the state of a running program (a process or task)?

Operating Systems, Fall

Operating Systems, Fall

Chapter 7: Main Memory. Operating System Concepts Essentials 8 th Edition

CSCI 4500 / 8506 Sample Questions for Quiz 5

Outline. V Computer Systems Organization II (Honors) (Introductory Operating Systems) Advantages of Multi-level Page Tables

Midterm 1. Administrivia. Virtualizing Resources. CS162 Operating Systems and Systems Programming Lecture 12. Address Translation

Memory Management. Goals of Memory Management. Mechanism. Policies

ECE 598 Advanced Operating Systems Lecture 12

Operating Systems. 09. Memory Management Part 1. Paul Krzyzanowski. Rutgers University. Spring 2015

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Chapter 8: Main Memory

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

UNIT III MEMORY MANAGEMENT

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

Virtual Memory: Concepts

Chapter 8: Main Memory

Process. One or more threads of execution Resources required for execution

Process. Memory Management

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Memory Management. CSCI 315 Operating Systems Design Department of Computer Science

CS153: Memory Management 1

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

CSC 369 Operating Systems

Virtual Memory 2: demand paging

18-447: Computer Architecture Lecture 16: Virtual Memory

Address Translation. Tore Larsen Material developed by: Kai Li, Princeton University

Virtual Memory Outline

Memory Management. Dr. Yingwu Zhu

Transcription:

Virtual Memory Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu SSE3044: Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu)

Virtual Memory: Goals Transparency Processes should not be aware that memory is shared Provides a convenient abstraction for programming (i.e. a large, contiguous memory space) Efficiency Minimizes fragmentation due to variable-sized requests (space) Gets some hardware support (time) Protection Protect processes and the OS from another process Isolation: a process can fail without affecting other processes Cooperating processes can share portions of memory SSE3044: Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 2

Accessing Memory Example #include <stdio.h> int n = 0; int main () { n++; printf ( &n = %p, n = %d\n, &n, n); } %./a.out &n = 0x0804a024, n = 1 %./a.out &n = 0x0804a024, n = 1 What happens if two users simultaneously run this program? SSE3044: Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 3

(Virtual) Address Space Process abstract view of memory OS provides illusion of private address space to each process Contains all of the memory state of the process Static area Allocated on exec() Code & Data Dynamic area Allocated at runtime Can grow or shrink Heap & Stack 0 N-1 unused read-only segment (.init,.text,.rodata) read/write segment (.data,.bss) run-time heap (managed by malloc) user stack (created at runtime) kernel virtual memory (code, data, heap, stack) brk stack pointer memory invisible to user code SSE3044: Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 4

Virtual Memory Each process has its own virtual address space Large and contiguous Use virtual addresses for memory references Virtual addresses are private to each process Address translation is performed at run time From a virtual address to the corresponding physical address Supports lazy allocation Physical memory is dynamically allocated or released on demand Programs execute without requiring their entire address space to be resident in physical memory SSE3044: Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 5

Virtual Memory 0 Code virtual address 0x100 Data Heap Stack P1 s address space 0 Code virtual address 0x100 Data Heap Stack P2 s address space address translation mechanism Physical memory Code Data physical address Heap Stack Code Data physical address Heap Stack SSE3044: Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 6

Static Relocation (1) Software-based relocation OS rewrites each program before loading it in memory Changes addresses of static data and functions 0 0x0010: movl 0x0200, %eax 0x0015: addl $1, %eax 0x0018: movl %eax, 0x0200 rewrite 0x1000 0x1010: movl 0x1200, %eax 0x1015: addl $1, %eax 0x1018: movl %eax, 0x1200 0x1200: 0 0x0200: 0 rewrite 0x5000 0x5010: movl 0x5200, %eax 0x5015: addl $1, %eax 0x5018: movl %eax, 0x5200 0x5200: 0 SSE3044: Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 7

Static Relocation (2) Pros No hardware support is required Cons No protection enforced A process can destroy memory regions of the OS or other processes No privacy: can read any memory address Cannot move address space after it has been placed May not be able to allocate a new process due to external fragmentation SSE3044: Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 8

Dynamic Relocation Hardware-based relocation MMU (Memory Management Unit) performs address translation on every memory reference instructions Protection is enforced by hardware: if the virtual address is invalid, the MMU raises an exception OS passes the information about the valid address space of the current process to the MMU Implementations Fixed or variable partitions Segmentation Paging SSE3044: Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 9

Fixed Partitions (1) Physical memory is broken up into fixed partitions Size of each partition is the same and fixed The number of partitions = degree of multiprogramming Virtual address 0x0362 Base register 0x2000 OS Partition 0 + 0x2362 Partition 1 Partition 2 Partition 3 0 0x1000 0x2000 0x3000 0x4000 SSE3044: Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 10

Fixed Partitions (2) Hardware requirements: base register Physical address = virtual address + base register Base register loaded by OS on context switch Pros Easy to implement Fast context switch Cons Internal fragmentation: unused area in a partition is wasted Partition size: one size does not fit all SSE3044: Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 11

Fixed Partitions (3) Improvement Partition size needs not be equal Allocation strategies A separate queue for each partition size A single queue + first fit A single queue + best fit Used in IBM OS/MFT (Multiprogramming with a Fixed number of Tasks) OS Partition 0 Partition 1 Partition 2 Partition 3 0 0x1000 0x2000 0x4000 0x8000 SSE3044: Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 12

Variable Partitions (1) Physical memory is divided into variable-sized partitions Used in IBM OS/MVT Limit register 0x2200 Base register 0x3200 OS Partition 0 0 0x1000 Virtual address 0x362 <? Yes + 0x3562 Partition 1 0x3200 No protection fault Partition 2 Partition 3 0x7000 0x8000 SSE3044: Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 13

Variable Partitions (2) Hardware requirements: base register + limit register The role of limit register: protection Pros Simple, inexpensive implementation No internal fragmentation Cons Each process must be allocated contiguously in physical mem External fragmentation: Holes are left scattered throughout physical memory Compaction can be used to reduce external fragmentation No partial sharing: cannot share parts of address space SSE3044: Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 14

Segmentation Divide address space into logical segments Each segment corresponds to logical entity in address space Code, data, stack, heap, etc. Users view memory as a collection of variable-sized segments, with no necessary ordering among them Virtual address: <Segment #, Offset> Each segment can independently be placed separately in physical memory grow or shrink be protected (separate read/write/execute protection bits) Natural extension of variable partitions Variable partitions: 1 segment / process Segmentation: many segments / process SSE3044: Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 15

Segmentation: Addressing Explicit approach Use a part of virtual address as a segment number The remaining bits mean the offset within the segment e.g. VAX/VMS system 31 0 00: P0 (User code, data, heap) 01: P1 (User stack) 1X: S (System) Implicit approach Determines the segment by the type of memory reference PC-based addressing: code segment SP- or BP-based addressing: stack segment SSE3044: Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 16

Segmentation: Implementation Segment registers or table (per process) Base Limit Dir Prot V Code 0 0x8000 0x0800 Down RO-X 1 Data 1 0x8800 0x0400 Down RW 1 Heap 2 0x9000 0x0800 Down RW 1 Stack 3 0x7000 0x0800 Up RW 1 Virtual address 0 1 0 0 0 0 1 1 0 1 1 0 0 0 1 0 0x0362 <? No Yes Segmentation violation + 0x8b62 OS Segment 3 Segment 0 Segment 1 Segment 2 0 0x7000 0x8000 0x8800 0x9000 SSE3044: Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 17

Segmentation: Pros Enables sparse allocation of address space Stack and heap can grow independently Easy to protect segments Valid bit Different protection bits for different segments e.g. Read-only status for code, Kernel-mode-only for system segment Easy to share segments Put the same translation into base/limit pair Code/data sharing at segment level (e.g. shared libraries) Supports dynamic relocation of each segment SSE3044: Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 18

Segmentation: Cons Each segment must be allocated contiguously External fragmentation May not have sufficient physical memory for large segments Large segment table Keep in main memory Use hardware cache for speed Cross-segment addresses Segments need to have same segment number for pointers to them to be shared among processes Otherwise, use indirect addressing only SSE3044: Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 19

Summary Separates user s virtual memory from physical memory Abstracts main memory into a large, uniform array of bytes Frees programmers from the concerns of memory limitations Physical memory locations can be moved transparently The virtual address space is overcommitted Allows the execution of processes that may not be completely in memory Physical memory is allocated on demand Views the physical memory as a cache for the disk Easy to protect and share memory regions among processes SSE3044: Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 20