ecture 33 Virtual Memory Friedland and Weaver Computer Science 61C Spring 2017 April 12th, 2017

Similar documents
CS 61C: Great Ideas in Computer Architecture (Machine Structures) Intro to Virtual Memory

John Wawrzynek & Nick Weaver

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

CS 61C: Great Ideas in Computer Architecture. Lecture 23: Virtual Memory. Bernhard Boser & Randy Katz

CS 152 Computer Architecture and Engineering. Lecture 8 - Address Translation

CS 152 Computer Architecture and Engineering. Lecture 8 - Address Translation

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

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

CS 61C: Great Ideas in Computer Architecture. Lecture 23: Virtual Memory

New-School Machine Structures. Overarching Theme for Today. Agenda. Review: Memory Management. The Problem 8/1/2011

CS 152 Computer Architecture and Engineering. Lecture 9 - Address Translation

Chapter 5B. Large and Fast: Exploiting Memory Hierarchy

CS 152 Computer Architecture and Engineering. Lecture 9 - Address Translation

CS 61C: Great Ideas in Computer Architecture Lecture 22: Opera&ng Systems, Interrupts, and Virtual Memory Part 1

Raspberry Pi ($40 on Amazon)

Memory Hierarchy Requirements. Three Advantages of Virtual Memory

Virtual Memory: From Address Translation to Demand Paging

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

CS 61C: Great Ideas in Computer Architecture. Virtual Memory

CS 152 Computer Architecture and Engineering. Lecture 11 - Virtual Memory and Caches

COSC3330 Computer Architecture Lecture 20. Virtual Memory

Virtual Memory: From Address Translation to Demand Paging

Address spaces and memory management

Agenda. CS 61C: Great Ideas in Computer Architecture. Virtual Memory II. Goals of Virtual Memory. Memory Hierarchy Requirements

Cache Performance and Memory Management: From Absolute Addresses to Demand Paging. Cache Performance

Review. You Are Here! Precise Traps. Terminology 8/11/15. CS 61C: Great Ideas in Computer Architecture Lecture 23: Virtual Memory Part 2

CS 152 Computer Architecture and Engineering. Lecture 9 - Virtual Memory

CS 61C: Great Ideas in Computer Architecture Virtual Memory. Instructors: Krste Asanovic & Vladimir Stojanovic h>p://inst.eecs.berkeley.

EECS 482 Introduction to Operating Systems

Improving Cache Performance and Memory Management: From Absolute Addresses to Demand Paging. Highly-Associative Caches

Processes and Virtual Memory Concepts

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

OS, Base & Bounds, Virtual Memory Intro. Instructor: Steven Ho

Lecture 19: Virtual Memory: Concepts

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

UC Berkeley CS61C : Machine Structures

CS 104 Computer Organization and Design

CS 61C: Great Ideas in Computer Architecture (Machine Structures) Opera&ng Systems, Interrupts, Virtual Memory

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

CS 61C: Great Ideas in Computer Architecture. Virtual Memory III. Instructor: Dan Garcia

Raspberry Pi ($40 on Amazon)

Virtual Memory Oct. 29, 2002

Last =me in Lecture 7 3 C s of cache misses Compulsory, Capacity, Conflict

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

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

Motivations for Virtual Memory Virtual Memory Oct. 29, Why VM Works? Motivation #1: DRAM a Cache for Disk

Computer Systems. Virtual Memory. Han, Hwansoo

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

virtual memory Page 1 CSE 361S Disk Disk

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Last 2 Classes: Introduction to Operating Systems & C++ tutorial. Today: OS and Computer Architecture

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 23

Lecture 9 - Virtual Memory

Memory Management. Goals of Memory Management. Mechanism. Policies

CISC 360. Virtual Memory Dec. 4, 2008

Operating Systems. Memory Management. Lecture 9 Michael O Boyle

CSE 351. Virtual Memory

Virtual Memory: Concepts

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 24

16 Sharing Main Memory Segmentation and Paging

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

Virtual Memory. CS61, Lecture 15. Prof. Stephen Chong October 20, 2011

CSE 120 Principles of Operating Systems

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

CS 152 Computer Architecture and Engineering CS252 Graduate Computer Architecture. Lecture 8 Address Transla>on

memory management Vaibhav Bajpai

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

CSE 560 Computer Systems Architecture

15 Sharing Main Memory Segmentation and Paging

Lecture 9 Virtual Memory

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

CPS 104 Computer Organization and Programming Lecture 20: Virtual Memory

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

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

CSE 120 Principles of Operating Systems Spring 2017

CS399 New Beginnings. Jonathan Walpole

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 23

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

CS61C : Machine Structures

Memory Management! Goals of this Lecture!

Memory Hierarchy. Mehran Rezaei

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

Goals of memory management

CS307 Operating Systems Main Memory

Lecture 15: Virtual Memory

Virtual Memory II CSE 351 Spring

CS370 Operating Systems

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

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

PROCESS VIRTUAL MEMORY. CS124 Operating Systems Winter , Lecture 18

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

Chapter 5 B. Large and Fast: Exploiting Memory Hierarchy

CS 5523 Operating Systems: Memory Management (SGG-8)

Virtual Memory: Concepts

Frequently asked questions from the previous class survey

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

Virtual Memory: Mechanisms. CS439: Principles of Computer Systems February 28, 2018

Lecture 13: Address Translation

Main Memory: Address Translation

This Unit: Main Memory. Virtual Memory. Virtual Memory. Other Uses of Virtual Memory

Transcription:

ecture 33 Computer Science 61C Spring 2017 April 12th, 2017 Virtual Memory 1

Multiprogramming The OS runs multiple applications at the same time. But not really: have many more processes/threads than available cores Switches between processes very quickly. This is called a context switch. When jumping into process, set timer interrupt. When it expires, store PC, registers, etc. (process state). Pick a different process to run and load its state. Set timer, change to user mode, jump to the new PC. Deciding what process to run is called scheduling. 2

Protection, Translation, Paging Supervisor mode does things that normal mode can't But... Supervisor mode is not enough to fully isolate applications from each other or from the OS. Application could overwrite another application s memory. Also, may want to address more memory than we actually have (e.g., for sparse data structures). Solution: Virtual Memory. Gives each process the illusion of a full memory address space that it has completely for itself. 3

What do we need Virtual Memory for? Reason 1: Adding Disks to Hierarchy Need to devise a mechanism to connect memory and disk in the memory hierarchy 4

What do we need Virtual Memory for? Reason 2: Simplifying Memory for Apps Applications should see the ~ 7FFF FFFF hex stack straightforward memory layout we saw earlier - > User-space applications should think they own all of memory So we give them a virtual view of memory heap static data code ~ 0000 0000 hex 5

What do we need Virtual Memory for? Reason 3: Protection Between Processes With a bare system, addresses issued with loads/stores are real physical addresses This means any program can issue any address, therefore can access any part of memory, even areas which it doesn t own Ex: The OS data structures We should send all addresses through a mechanism that the OS controls, before they make it out to DRAM - a transla/on mechanism 6

VM + Supervisor Mode combine to Create Isolation Supervisor mode is only entered into at the trap handler So its always known (and hopefully correct) code that is part of the core operating system This is why "syscall" generates an exception Only Supervisor mode can change Virtual Memory mappings So only the core of the operating system can bypass the protections imposed on memory These are the invariants necessary for isolation Anything that can affect these invariants completely compromises the security of the system 7

Address Spaces The set of addresses labeling all of memory that we can access Now, 2 kinds: Virtual Address Space - the set of addresses that the user program knows about Physical Address Space - the set of addresses that map to actual physical cells in memory Hidden from user applications So, we need a way to map between these two address spaces 8

Blocks vs. Pages In caches, we dealt with individual blocks Usually ~64B on modern systems We could divide memory into a set of blocks In VM, we deal with individual pages Usually ~4 KB on modern systems Now, we ll divide memory into a set of pages Common point of confusion: Bytes, Words, Blocks, Pages are all just different ways of looking at memory! 9

Bytes, Words, Blocks, Pages Ex: 16kB DRAM, 1 Page 1 Block Block 31 Word 31 4kB Pages (for VM), 128B blocks (for caches), 4B words (for lw/sw) 16 KiB 1 Memory Page 3 Can think of Page memory 2 as: - 4 Pages OR Page - 1128 Blocks OR - 4096 Words Page 0 Can think of a page as: - 32 Blocks OR - 1024 Words Block 0 Word 0 10

Address Translation So, what do we want to achieve at the hardware level? Take a Virtual Address, that points to a spot in the Virtual Address Space of a particular program, and map it to a Physical Address, which points to a physical spot in DRAM of the whole machine Virtual Address Virtual Page Number Offset Physical Address Physical Page Number Offset 11

Address Translation Virtual Address Virtual Page Number Offset Address Translation Copy Bits Physical Address Physical Page Number Offset The rest of the lecture is all about implementing 12

Bare 5-Stage Pipeline In a bare machine, the only kind of address is a physical address PC Physical Address Inst. Cache D Decode E M + Physical Address Data Cache W Physical Address Memory Controller Main Memory (DRAM) Physical Address Physical Address 13

Modern Virtual Memory Systems Illusion of a large, private, uniform store Protection several users, each with their private address space and one or more shared address spaces OS user i Demand Paging Provides the ability to run programs larger than the primary memory Primary Memory Swapping Store Hides differences in machine configurations The price is address translation on each memory reference VA mapping PA TLB 14

Dynamic Address Translation Motivation Multiprogramming, multitasking: Desire to execute more than one process at a time (more than one process can reside in main memory at the same time). Location-independent programs Programming and storage management ease base register add offset to each address Protection Independent programs should not affect each other inadvertently bound register check range of access (Note: Multiprogramming drives requirement for resident supervisor (OS) software to manage context switches between multiple programs) prog1 prog2 OS Physical Memory 15

Simple Base and Bound Translation Segment Length Load X Bound Register Logical Address Base + Bounds Violation? Physical Address current segment Physical Memory Register Base Physical Address Program Address Space Base and bounds registers are visible/accessible only when processor is running in supervisor mode 16

Base and Bound Machine Prog. Bound Register Logical Address Bounds Violation? Data Bound Register Logical Address Bounds Violation? PC + Inst. Cache D Decode E M + + Data Cache W Physical Address Physical Address Program Base Register Physical Address [ Can fold addi<on of base register into (register+immediate) address calcula<on using a carry- save adder (sums three numbers with only a few gate delays more than adding two numbers) ] Memory Controller Main Memory (DRAM) Data Base Register Physical Address Physical Address 17

Memory Fragmentation Users 4 & 5 Users 2 & 5 free OS Space arrive OS Space leave OS Space user 1 16K user 1 16K user 1 16K user 2 24K user 2 24K 24K 24K user 4 16K 8K user 4 16K 8K user 3 32K user 3 32K user 3 32K 24K user 5 24K 24K As users come and go, the storage is fragmented. Therefore, at some stage programs have to be moved around to compact the storage. 18

Paged Memory Systems Processor-generated address can be split into: page number offset A page table contains the physical address of the base of each page 0 1 2 3 0 1 2 3 1 0 3 Physical Memory Address Space of User-1 Page Table of User-1 2 Page tables make it possible to store the pages of a program non-contiguously. 19

Private Address Space per User User 1 VA1 OS pages Page Table User 2 User 3 VA1 VA1 Page Table Physical Memory Page Table free Each user has a page table Page table contains an entry for each user page 20

Where Should Page Tables Reside? Space required by the page tables (PT) is proportional to the address space, number of users,... Too large to keep in cpu registers Idea: Keep PTs in the main memory Needs one reference to retrieve the page base address and another to access the data word doubles the number of memory references! Caching helps Automatic caching if the processor uses full page tables Manual caching controlled by the OS with the Transla<on Lookaside Buffer (TLB) 21

Page Tables in Physical Memory PT User 1 VA1 User 1 Virtual Address Space VA1 PT User 2 Physical Memory User 2 Virtual Address Space 22

Page Table Tricks Can actually have multiple processes referring to the same physical memory Enables "shared memory" between processes Page table entry can say "this exists on disk" When such memory is accessed it triggers an exception instead The operating system can copy the data into memory ("swap it in") and then resume the trapped instruction How it gives the illusion of infinite memory Can use that same method to efficiently read files File is "memory mapped", when read it is simply paged in like other Allows an efficient method to handle large files conveniently When data is changed can use the same method used to "swap out" unused memory 23

The Ultimate Page-Table Trick: Rowhammer An unspeakably cool security vulnerability DRAM (unless you pay for error correcting (ECC) memory) is actually unreliable Can repeatedly read/write the same location ("hammer the row" and eventually cause an error in some physically dis9nct memory loca9on Can tell the OS "I want to map this same block of memory at multiple addresses in my process " Which creates additional page table entries Enter Rowhammer It seems all vunerabilities get named now, but this one is cool enough to deserve a name! 24

How RowHammer Works Step 1: Allocate a single page of memory Physical Memory Process's Virtual Memory Step 2: Make the OS make a gazillion page-table entries pointing to the same page Step 3: Hammer the DRAM until one of those entries gets corrupted Now causes that memory page to point to a set of page table entries instead Step 4: Profit Well, the ability to read and write to any physical address in the system, same difference 25

Clicker Question So how cool is this? A -> Supercool E -> Eh, whatever 26

In Conclusion Once we have a basic machine, it s mostly up to the OS to use it and define application interfaces. Hardware helps by providing the right abstractions and features (e.g., Virtual Memory, I/O). If you want to learn more about operating systems, you should take CS162! What s next in CS61C? More details on I/O More about Virtual Memory 27