Vladimir Stojanovic and Nicholas Weaver

Slides:



Advertisements
Similar presentations
Virtual Memory Basics.
Advertisements

Operating Systems Lecture 10 Issues in Paging and Virtual Memory Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing.
16.317: Microprocessor System Design I
Memory Management Questions answered in this lecture: How do processes share memory? What is static relocation? What is dynamic relocation? What is segmentation?
OS Memory Addressing.
1 A Real Problem  What if you wanted to run a program that needs more memory than you have?
Memory Management Norman White Stern School of Business.
Memory Design Example. Selecting Memory Chip Selecting SRAM Memory Chip.
Operating System Support Focus on Architecture
Chapter 9 Virtual Memory Produced by Lemlem Kebede Monday, July 16, 2001.
©UCB CS 162 Ch 7: Virtual Memory LECTURE 13 Instructor: L.N. Bhuyan
Computer Organization and Architecture
Layers and Views of a Computer System Operating System Services Program creation Program execution Access to I/O devices Controlled access to files System.
Cosc 2150: Computer Organization Chapter 6, Part 2 Virtual Memory.
PA0 due 60 hours. Lecture 4 Memory Management OSTEP Virtualization CPU: illusion of private CPU RAM: illusion of private memory Concurrency Persistence.
Chapter 4 Memory Management Virtual Memory.
Virtual Memory Part 1 Li-Shiuan Peh Computer Science & Artificial Intelligence Lab. Massachusetts Institute of Technology May 2, 2012L22-1
Review °Apply Principle of Locality Recursively °Manage memory to disk? Treat as cache Included protection as bonus, now critical Use Page Table of mappings.
Constructive Computer Architecture Virtual Memory: From Address Translation to Demand Paging Arvind Computer Science & Artificial Intelligence Lab. Massachusetts.
OS Memory Addressing. Architecture CPU – Processing units – Caches – Interrupt controllers – MMU Memory Interconnect North bridge South bridge PCI, etc.
CS 110 Computer Architecture Lecture 23: Virtual Memory Instructor: Sören Schwertfeger School of Information Science and.
CS 61C: Great Ideas in Computer Architecture Virtual Memory Cont.
Bernhard Boser & Randy Katz
Non Contiguous Memory Allocation
Segmentation COMP 755.
ECE232: Hardware Organization and Design
Memory COMPUTER ARCHITECTURE
CS161 – Design and Architecture of Computer
From Address Translation to Demand Paging
CS703 - Advanced Operating Systems
From Address Translation to Demand Paging
Today How was the midterm review? Lab4 due today.
Swapping Segmented paging allows us to have non-contiguous allocations
CSE 153 Design of Operating Systems Winter 2018
Main Memory Management
Chapter 8: Main Memory.
CS 61C: Great Ideas in Computer Architecture Lecture 23: Virtual Memory Krste Asanović & Randy H. Katz 11/12/2018.
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy 11/12/2018.
O.S Lecture 13 Virtual Memory.
Lecture 14 Virtual Memory and the Alpha Memory Hierarchy
From Address Translation to Demand Paging
Memory Management 11/17/2018 A. Berrached:CS4315:UHD.
Chapter 9: Virtual-Memory Management
CS 105 “Tour of the Black Holes of Computing!”
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 8 11/24/2018.
Page that info back into your memory!
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 9 12/1/2018.
Main Memory Background Swapping Contiguous Allocation Paging
CS399 New Beginnings Jonathan Walpole.
Chapter 8: Memory management
Outline Module 1 and 2 dealt with processes, scheduling and synchronization Next two modules will deal with memory and storage Processes require data to.
Lecture 3: Main Memory.
Morgan Kaufmann Publishers Memory Hierarchy: Virtual Memory
Virtual Memory Nov 27, 2007 Slide Source:
CSCE 313 – Introduction to UNIx process
CSE 451: Operating Systems Autumn 2005 Memory Management
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 9 4/5/2019.
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CS 105 “Tour of the Black Holes of Computing!”
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
CS 105 “Tour of the Black Holes of Computing!”
CS703 - Advanced Operating Systems
CSE 153 Design of Operating Systems Winter 2019
COMP755 Advanced Operating Systems
Review What are the advantages/disadvantages of pages versus segments?
Instructor: Michael Greenbaum
Presentation transcript:

Vladimir Stojanovic and Nicholas Weaver 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/

Agenda Multiprogramming/time-sharing Introduction to Virtual Memory

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.

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.

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

What do we need Virtual Memory for 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 FFFFhex stack heap Location independent automatically static data code ~ 0000 0000hex

What do we need Virtual Memory for 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

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

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 because the programs will issue virtual addresses, but we need physical addresses to access data in DRAM

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!

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 Page 1 Block Block 31 Word 31 1 Memory Page 3 Page 2 Page 1 Page 0 Can think of a page as: - 32 Blocks OR - 1024 Words Can think of memory as: - 4 Pages OR - 128 Blocks - 4096 Words 16 KiB Block 0 Word 0

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 Physical Address Virtual Page Number Offset Physical Page Number Offset

Address Translation Virtual Address Physical Address Virtual Page Number Offset Address Translation Copy Bits Phys addr will then go to the cache just like before, cache will split that same addr into TIO. you can think of them completely separately for now. caches work just like before Physical Page Number Offset The rest of the lecture is all about implementing

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

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 The price is address translation on each memory reference OS useri Swapping Store Primary Memory Portability on machines with different memory configurations. mapping VA PA TLB CS252 S05

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 Physical Memory prog2 OS CS252 S05

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

Base and Bound Machine ≤ ≤ + + Prog. Bound Register Data Bound Register Bounds Violation? Bounds Violation? ≤ ≤ Logical Address Logical Address PC D E M W Decode Data Cache Inst. Cache + + + Physical Address Physical Address Program Base Register Data Base Register Physical Address Physical Address Memory Controller Physical Address 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) ] CS252 S05

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

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 2 3 1 1 Physical Memory 2 2 3 3 Relaxes the contiguous allocation requirement. Address Space of User-1 Page Table of User-1 Page tables make it possible to store the pages of a program non-contiguously. CS252 S05

Private Address Space per User Page Table User 2 User 3 Physical Memory free OS pages OS ensures that the page tables are disjoint. Each user has a page table Page table contains an entry for each user page CS252 S05

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) CS252 S05

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

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

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!

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

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

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