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

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

16 Sharing Main Memory Segmentation and Paging

Virtualizing Memory: Paging. Announcements

15 Sharing Main Memory Segmentation and Paging

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 13: Address Translation

Virtual Memory. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

Last Class: Deadlocks. Where we are in the course

12: Memory Management

CS399 New Beginnings. Jonathan Walpole

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

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

Introduction to Paging

Lecture 4: Memory Management & The Programming Interface

Introduction to Operating Systems

Memory Management (1) Memory Management

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

Memory Management (1) Memory Management. CPU vs. memory. No.8. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

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

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

11.1 Segmentation: Generalized Base/Bounds

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

Operating System Labs. Yuanbin Wu

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

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

memory management Vaibhav Bajpai

Lecture 7. Memory Management

Administrivia. Lab 1 due Friday 12pm. We give will give short extensions to groups that run into trouble. But us:

Protection. - Programmers typically assume machine has enough memory - Sum of sizes of all processes often greater than physical memory 1 / 36

Memory Hierarchy Requirements. Three Advantages of Virtual Memory

Course Outline. Processes CPU Scheduling Synchronization & Deadlock Memory Management File Systems & I/O Distributed Systems

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

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

EECS 470. Lecture 16 Virtual Memory. Fall 2018 Jon Beaumont

Chapter 8 & Chapter 9 Main Memory & Virtual Memory

UNIT III MEMORY MANAGEMENT

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

Address spaces and memory management

EECS 482 Introduction to Operating Systems

Memory management. Requirements. Relocation: program loading. Terms. Relocation. Protection. Sharing. Logical organization. Physical organization

CSE 120 Principles of Operating Systems

Roadmap. Tevfik Koşar. CSC Operating Systems Spring Lecture - XII Main Memory - II. Louisiana State University

Goals of Memory Management

PROCESS VIRTUAL MEMORY. CS124 Operating Systems Winter , Lecture 18

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

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

CSE 421/521 - Operating Systems Fall Lecture - XII Main Memory Management. Tevfik Koşar. University at Buffalo. October 18 th, 2012.

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

Memory Management. Contents: Memory Management. How to generate code? Background

CS 61C: Great Ideas in Computer Architecture Virtual Memory. Instructors: John Wawrzynek & Vladimir Stojanovic

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

Operating System Labs. Yuanbin Wu

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

Background. Contiguous Memory Allocation

File Systems. OS Overview I/O. Swap. Management. Operations CPU. Hard Drive. Management. Memory. Hard Drive. CSI3131 Topics. Structure.

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

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

Main Memory (Part I)

Memory Management. Memory Management

Chapter 9 Memory Management

- Established working set model - Led directly to virtual memory. Protection

Memory management. Knut Omang Ifi/Oracle 10 Oct, 2012

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

Principles of Operating Systems

Embedded Systems Dr. Santanu Chaudhury Department of Electrical Engineering Indian Institute of Technology, Delhi

Memory Management. Memory

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

Chapter 8: Main Memory

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

Virtual memory - Paging

The process. one problem

Memory Management. 3. What two registers can be used to provide a simple form of memory protection? Base register Limit Register

Module 29: Operating System Memory Protection

COSC Operating Systems Design, Fall 2001, Byunggu Yu. Chapter 9 Memory Management (Lecture Note #8) 1. Background

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

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

CS162 Operating Systems and Systems Programming Lecture 12. Protection (continued) Address Translation

A.Arpaci-Dusseau. Mapping from logical address space to physical address space. CS 537:Operating Systems lecture12.fm.2

Chapter 8: Memory-Management Strategies

Memory Management. Memory Management

Slide 6-1. Processes. Operating Systems: A Modern Perspective, Chapter 6. Copyright 2004 Pearson Education, Inc.

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

Process. Memory Management

Recap: Memory Management

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

Motivation for Dynamic Memory. Dynamic Memory Allocation. Stack Organization. Stack Discussion. Questions answered in this lecture:

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

CS162 Operating Systems and Systems Programming Lecture 12. Protection (continued) Address Translation

Outlook. Background Swapping Contiguous Memory Allocation Paging Structure of the Page Table Segmentation Example: The Intel Pentium

Operating Systems. Memory Management. Lecture 9 Michael O Boyle

CHAPTER 8 - MEMORY MANAGEMENT STRATEGIES

[07] SEGMENTATION 1. 1

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

CSE 120 Principles of Operating Systems Spring 2017

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

Main Memory Yi Shi Fall 2017 Xi an Jiaotong University

Segmentation. Multiple Segments. Lecture Notes Week 6

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

Memory management. Last modified: Adaptation of Silberschatz, Galvin, Gagne slides for the textbook Applied Operating Systems Concepts

Chapter 8 Memory Management

Transcription:

Introduction to Virtual Memory Nima Honarmand (Based on slides by Prof. Andrea Arpaci-Dusseau)

Motivating Virtual Memory (Very) old days: Uniprogramming only one process existed at a time OS was little more than a library occupying the beginning of the memory Physical Memory 0 OS Code Heap 2 n -1 Advantage: Simplicity No virtualization needed Disadvantages: Only one process runs at a time Process can destroy OS User Process Stack

Goals for Multiprogramming Transparency Processes are not aware that memory is shared Works regardless of number and/or location of processes Protection Cannot corrupt OS or other processes Privacy: Cannot read data of other processes Efficiency Low run-time overhead Do not waste memory resources Sharing Cooperating processes should be able to share portions of address space

Abstraction: Address Space Address space: Each process view of its own memory range Set of addresses that map to bytes Problem: how can OS provide illusion of private address space to each process? Address space has static and dynamic components Static: Code and some global variables Dynamic: Stack and Heap Code Heap Stack 0 2 n -1

How to Virtualize Memory? Problem: How to run multiple processes concurrently? Addresses are hard-coded into program binaries How to avoid collisions?

How to Virtualize Memory? Possible Solutions for Mechanisms: 1) Time Sharing 2) Base register 3) Base + Bound registers 4) Segmentation 5) Paging We ll first discuss the general ideas To motivate the historical progression Building insight/intuition into why things are the why they are Once familiar with the basic concepts, we ll take a look at x86 idiosyncrasies Of which, there are a lot

1) Time Sharing of Memory Try similar approach to how OS virtualizes CPU Observation: OS gives illusion of many virtual CPUs by saving CPU registers to memory when a process isn t running Could give illusion of many virtual memories by saving memory to disk when process isn t running

Problem w/ Time Sharing Memory Problem: Ridiculously poor performance Better Alternative: space sharing At same time, space of memory is divided across processes Remainder of solutions all use space sharing

2) Per-Process Base Register Goal: Allow processes to space-share the physical memory Requires hardware support Memory Management Unit (MMU) MMU dynamically changes process-generated address at every memory reference Process generates virtual addresses (in their address space) Memory hardware uses physical addresses Process runs here CPU Virtual address MMU Physical address Physical Memory

Hardware Support Two operating modes: Privileged (protected, kernel) mode: OS runs When enter OS (trap, system calls, interrupts, exceptions) Allows certain instructions to be executed Can manipulate contents of MMU Allows OS to access all of physical memory User mode: User processes run Perform translation of virtual address to physical address Minimal MMU contains base register for translation Base: start location for current address space

HW Implementation Translation on every memory access of user process MMU adds base register to virtual address to form physical address Each process has different value in base register Saved/restored on a context switch MMU 32 bits 1 bit virtual address registers mode == user? yes no base mode physical address + base

Quiz: Who Controls Base Register? What entity should do the address translation with base register? 1) User Process 2) OS 3) HW What entity should modify the base register? 1) User Process 2) OS 3) HW

Advantages & Disadvantages Advantages: Supports Dynamic Relocation Can place process at different locations in memory everytime Can even move process address space around in memory Simple, fast HW Simple OS support Disadvantages: Does not provide protection Does not enable sharing OS has to allocate the maximum address space up front Wastes a lot of memory space

3) Per-Process Base+Bounds Regs Idea: limit the address space with a bounds register To provide protection Base register: smallest physical addr (or starting location) Bounds register: size of this process s virtual address space Sometimes defined as largest physical address (base + size) OS kills process if process loads/stores beyond bounds

HW Implementation Translation on every memory access of user process MMU compares virtual address to bounds register if virtual address is greater, then generate exception MMU adds base to virtual addr to form physical address MMU virtual address registers mode == user? yes no 32 bits 32 bits 1 bit base bounds mode physical address Exception no < bounds? yes + base

Managing Processes w/ Base+Bounds Context-switch Add base and bounds registers to PCB Steps Change to privileged mode Save base and bounds registers of old process Load base and bounds registers of new process Change to user mode and jump to new process Protection requirement User process cannot change base and bounds registers User process cannot change to privileged mode

Base+Bounds Advantages Provides protection across address spaces Supports dynamic relocation Simple, inexpensive, fast HW implementation Few registers, little logic in MMU Simple context switching logic Save and restore a couple of registers

Base+Bounds Disadvantages Each process must be allocated contiguously in physical memory Must allocate memory that may not be used by process No partial sharing: Cannot share limited parts of address space Code Heap Stack 0 2 n -1

4) Segmentation Divide address space into logical segments Each segment corresponds to logical entity in address space E.g., code, stack, heap Each segment can independently: be placed in physical memory grow and shrink be protected (separate read/write/execute protection bits) Code Heap Stack 0 2 n -1

Segmented Addressing Code now specifies segment and offset within segment for every memory access How to designate a particular segment? Use part of virtual address Top bits of virtual address select segment Low bits of virtual address select offset within segment What if small address space, not enough bits? Implicitly by type of memory reference To fetch an instruction: user code segment To push/pop stack: use stack segment For everything else: use data segment Use special segment registers if you need to override the above Mechanism used in x86

HW Implementation MMU contains Segment Table Each segment has own base, bounds, protection bits MMU sores the segment table (or has a special register virtual address (segment:offset) pointing to the segment table stored in memory) registers mode == user? yes no MMU Segment Table 1 bit mode physical address Exception no segment in the table? yes offset < bounds? yes permission okay? yes offset + seg base

Example 14 bit virtual address, 4 segments How many bits for segment? How many bits for offset? Segment Base Bounds R W 0 0x2000 0x6ff 1 0 1 0x0000 0x4ff 1 1 2 0x3000 0xfff 1 1 3 0x0000 0x000 0 0 Translate virtual addresses (in hex) to physical addresses 0x0240: 0x1108: 0x265c: Seg 0; phys-addr: 0x2000 + 0x240 Seg 1; phys-addr: 0x0000 + 0x108 Seg 2; phys-addr: 0x3000 + 0x65c = 0x2240 = 0x108 = 0x365c 0x3002: Seg 3: out of bounds no translation

Advantages of Segmentation Enables sparse allocation of address space Stack and heap can grow independently Heap: If no objects on free list, dynamic memory allocator requests more from OS (e.g., UNIX: malloc calls sbrk()) Stack: OS recognizes reference outside legal segment, extends stack implicitly Different protection for different segments Read-only status for code Enables sharing of selected segments Supports dynamic relocation of each segment

Disadvantages of Segmentation If only a few segments allowed per process: coarsegrained segmentation Not very flexible Cannot easily accommodate sharing If many segments allowed per process: fine-grained segmentation Has to save/restore a large segment table on every context switch Remember: each process has its own set of segments Makes physical memory management much more complex Causes fragmentation

Conclusion HW & OS work together to virtualize memory Give illusion of private address space to each process Adding MMU registers for base+bounds so translation is fast OS not involved with every address translation, only on context switch or errors Dynamic relocation w/ segments is good building block Next lecture: Solve fragmentation with paging