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

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

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

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

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

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)

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

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

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

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

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

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

Memory Hierarchy Requirements. Three Advantages of Virtual Memory

Virtual Memory: From Address Translation to Demand Paging

COSC3330 Computer Architecture Lecture 20. Virtual Memory

Address spaces and memory management

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

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

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

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

Raspberry Pi ($40 on Amazon)

Virtual Memory: From Address Translation to Demand Paging

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

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

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

CS 104 Computer Organization and Design

EECS 482 Introduction to Operating Systems

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

Processes and Virtual Memory Concepts

Lecture 9 Virtual Memory

Virtual Memory Oct. 29, 2002

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

Lecture 19: Virtual Memory: Concepts

Lecture 9 - Virtual Memory

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

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

UC Berkeley CS61C : Machine Structures

virtual memory Page 1 CSE 361S Disk Disk

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

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

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

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

Computer Systems. Virtual Memory. Han, Hwansoo

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

Virtual Memory: Concepts

CISC 360. Virtual Memory Dec. 4, 2008

CS399 New Beginnings. Jonathan Walpole

CSE 560 Computer Systems Architecture

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

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

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

15 Sharing Main Memory Segmentation and Paging

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

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

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

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

Operating Systems. Memory Management. Lecture 9 Michael O Boyle

CPS 104 Computer Organization and Programming Lecture 20: Virtual Memory

CSE 351. Virtual Memory

memory management Vaibhav Bajpai

CS61C : Machine Structures

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 23

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

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

CS252 Spring 2017 Graduate Computer Architecture. Lecture 17: Virtual Memory and Caches

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

Memory Hierarchy. Mehran Rezaei

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 24

Modern Virtual Memory Systems. Modern Virtual Memory Systems

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

Memory Management. Goals of Memory Management. Mechanism. Policies

CSE 120 Principles of Operating Systems

CS 61C: Great Ideas in Computer Architecture. Lecture 22: Operating Systems. Krste Asanović & Randy H. Katz

PROCESS VIRTUAL MEMORY. CS124 Operating Systems Winter , Lecture 18

18-447: Computer Architecture Lecture 16: Virtual Memory

CS450/550 Operating Systems

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

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

Main Memory: Address Translation

Storage Management 1

Virtual Memory II CSE 351 Spring

CS 318 Principles of Operating Systems

Memory Management! Goals of this Lecture!

Topic 18 (updated): Virtual Memory

Topic 18: Virtual Memory

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

CS 61C: Great Ideas in Computer Architecture (Machine Structures) Caches Part 1

11/13/17. CS61C so far. Adding I/O C Programs. So How is a Laptop Any Different? It s a Real Computer! Raspberry Pi (Less than $40 on Amazon in 2017)

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

Goals of memory management

Transcription:

CS 61C: Great Ideas in Computer Architecture (Machine Structures) Intro to Virtual Memory Instructors: Vladimir Stojanovic and Nicholas Weaver http://inst.eecs.berkeley.edu/~cs61c/ 1

Agenda Multiprogramming/time-sharing Introduction to Virtual Memory 2

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. 3

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. 4

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 5

What do we need Virtual Memory for? Reason 2: Simplifying Memory for Apps Applications should see the 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 ~ 7FFF FFFF hex stack heap static data code ~ 0000 0000 hex 6

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 translation mechanism 7

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 8

Spaces The set of addresses labeling all of memory that we can access Now, 2 kinds: Virtual Space - the set of addresses that the user program knows about Physical 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 9

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! 10

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

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

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

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

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 Demand Paging Provides the ability to run programs larger than the primary memory Hides differences in machine configurations Primary Memory OS user i Swapping Store The price is address translation on each memory reference VA mapping TLB PA 15

Dynamic 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 16

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

Separate Areas for Program and Data (Scheme used on all Cray vector supercomputers prior to X1, 2002) Load X Program Space Data Bound Register Mem. Register Data Base Register Program Bound Register Program Counter Program Base Register Logical Logical + + Bounds Violation? Physical Bounds Violation? Physical data segment program segment Main Memory What is an advantage of this separation? 18

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

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

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 1 0 1 2 3 0 1 2 3 0 3 Physical Memory Space of User-1 Page Table of User-1 2 Page tables make it possible to store the pages of a program non-contiguously. 21

Private Space per User User 1 VA1 Page Table OS pages 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 22

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 Translation Lookaside Buffer (TLB) 23

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

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 25

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 distinct memory location 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! 26

How RowHammer Works Step 1: Allocate a single page of 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 Physical Memory Process's Virtual Memory 27

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

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 29