CS 61C L7.1.1 VM I (1) K. Meinz, Summer 2004 © UCB CS61C : Machine Structures Lecture 7.1.1 VM I 2004-08-2 Kurt Meinz inst.eecs.berkeley.edu/~cs61c.

Slides:



Advertisements
Similar presentations
Memory Management: Overlays and Virtual Memory
Advertisements

Virtual Memory Adapted from lecture notes of Dr. Patterson and Dr. Kubiatowicz of UC Berkeley.
CS61C L35 VM I (1) Garcia © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture.
CS61C L34 Virtual Memory I (1) Garcia, Spring 2007 © UCB 3D chips from IBM  IBM claims to have found a way to build 3D chips by stacking them together.
Chap. 7.4: Virtual Memory. CS61C L35 VM I (2) Garcia © UCB Review: Caches Cache design choices: size of cache: speed v. capacity direct-mapped v. associative.
CS 61C L24 VM II (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures.
Virtual Memory Adapted from lecture notes of Dr. Patterson and Dr. Kubiatowicz of UC Berkeley and Rabi Mahapatra & Hank Walker.
Virtual Memory.
CS 61C L23 VM I (1) A Carle, Summer 2006 © UCB inst.eecs.berkeley.edu/~cs61c/su06 CS61C : Machine Structures Lecture #23: VM I Andy Carle.
COMP3221 lec36-vm-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 13: Virtual Memory - I
CS 61C L24 VM II (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #24: VM II Andy Carle.
CS61C L25 Virtual Memory I (1) Beamer, Summer 2007 © UCB Scott Beamer, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #25.
COMP 3221: Microprocessors and Embedded Systems Lectures 27: Virtual Memory - II Lecturer: Hui Wu Session 2, 2005 Modified.
CS61C L19 VM © UC Regents 1 CS61C - Machine Structures Lecture 19 - Virtual Memory November 3, 2000 David Patterson
OS Spring’03 Introduction Operating Systems Spring 2003.
CS-3013 & CS-502, Summer 2006 Memory Management1 CS-3013 & CS-502 Summer 2006.
CS61C L36 VM II (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures.
CS61C L33 Virtual Memory I (1) Garcia, Fall 2006 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c UC Berkeley.
©UCB CS 162 Ch 7: Virtual Memory LECTURE 13 Instructor: L.N. Bhuyan
CS 430 – Computer Architecture 1 CS 430 – Computer Architecture Virtual Memory William J. Taffe using slides of David Patterson.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 33 – Virtual Memory I OCZ has released a 1 TB solid state drive (the biggest.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 34 – Virtual Memory II Researchers at Stanford have developed “nanoscale.
COMP3221 lec37-vm-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 13: Virtual Memory - II
CSE378 Virtual memory.1 Evolution in memory management techniques In early days, single program ran on the whole machine –used all the memory available.
What are Exception and Interrupts? MIPS terminology Exception: any unexpected change in the internal control flow – Invoking an operating system service.
CS 61C L7.1.2 VM II (1) K. Meinz, Summer 2004 © UCB CS61C : Machine Structures Lecture VM II Kurt Meinz inst.eecs.berkeley.edu/~cs61c.
Recall: Three I/O Methods Synchronous: Wait for I/O operation to complete. Asynchronous: Post I/O request and switch to other work. DMA (Direct Memory.
Virtual Memory Part 1 Li-Shiuan Peh Computer Science & Artificial Intelligence Lab. Massachusetts Institute of Technology May 2, 2012L22-1
Virtual Memory. Virtual Memory: Topics Why virtual memory? Virtual to physical address translation Page Table Translation Lookaside Buffer (TLB)
Review (1/2) °Caches are NOT mandatory: Processor performs arithmetic Memory stores data Caches simply make data transfers go faster °Each level of memory.
1 Some Real Problem  What if a program needs more memory than the machine has? —even if individual programs fit in memory, how can we run multiple programs?
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
MIPS I/O and Interrupt.
Review °Apply Principle of Locality Recursively °Manage memory to disk? Treat as cache Included protection as bonus, now critical Use Page Table of mappings.
Processes and Virtual Memory
Lecture 26 Virtual Machine Monitors. Virtual Machines Goal: run an guest OS over an host OS Who has done this? Why might it be useful? Examples: Vmware,
IT 251 Computer Organization and Architecture
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
CS203 – Advanced Computer Architecture Virtual Memory.
CS161 – Design and Architecture of Computer
ECE232: Hardware Organization and Design
CS161 – Design and Architecture of Computer
A Real Problem What if you wanted to run a program that needs more memory than you have? September 11, 2018.
CS703 - Advanced Operating Systems
IT 251 Computer Organization and Architecture
CSE 153 Design of Operating Systems Winter 2018
Lecture 14 Virtual Memory and the Alpha Memory Hierarchy
Review (1/2) Caches are NOT mandatory:
Csci 211 Computer System Architecture – Review on Virtual Memory
Instructor Paul Pearce
CSE 451: Operating Systems Autumn 2005 Memory Management
Translation Buffers (TLB’s)
Interrupts and Exception Handling
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
Translation Buffers (TLB’s)
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
Albert Chae, Instructor
COMP3221: Microprocessors and Embedded Systems
Evolution in memory management techniques
CSE 153 Design of Operating Systems Winter 2019
Evolution in memory management techniques
Evolution in memory management techniques
Virtual Memory Lecture notes from MKP and S. Yalamanchili.
Translation Buffers (TLBs)
Review What are the advantages/disadvantages of pages versus segments?
Interrupts and Exception Handling
Presentation transcript:

CS 61C L7.1.1 VM I (1) K. Meinz, Summer 2004 © UCB CS61C : Machine Structures Lecture VM I Kurt Meinz inst.eecs.berkeley.edu/~cs61c

CS 61C L7.1.1 VM I (2) K. Meinz, Summer 2004 © UCB Outline Interrupts Review Virtual Memory

CS 61C L7.1.1 VM I (3) K. Meinz, Summer 2004 © UCB SPIM/Proj4 I/O Simulation Simulate 1 I/O device: memory-mapped terminal (keyboard + display) Read from keyboard (receiver); 2 device regs Writes to terminal (transmitter); 2 device regs Received Byte Receiver Data 0xffff0004 Unused ( ) (IE) Receiver Control 0xffff0000 Ready (I.E.) Unused ( ) Transmitted Byte Transmitter Control 0xffff0008 Transmitter Data 0xffff000c Ready (I.E.) Unused ( ) Unused

CS 61C L7.1.1 VM I (4) K. Meinz, Summer 2004 © UCB SPIM I/O Control register rightmost bit (0): Ready Receiver: Ready==1 means character in Data Register not yet been read; 1  0 when data is read from Data Reg Transmitter: Ready==1 means transmitter is ready to accept a new character; 0  Transmitter still busy writing last char -I.E. bit discussed later Data register rightmost byte has data Receiver: last char from keyboard; rest = 0 Transmitter: when write rightmost byte, writes char to display

CS 61C L7.1.1 VM I (5) K. Meinz, Summer 2004 © UCB Definitions for Clarification Exception: something “out of the ordinary” has happened and needs to be handled Synchronous: Triggered by a particular insttruction (overflow, syscall, etc.) Asyncronous: Not related to a particular instruction (hw failure, io) Interrupt: asynchronous exception Trap: synchronous exception

CS 61C L7.1.1 VM I (6) K. Meinz, Summer 2004 © UCB Interrupt Driven Data Transfer (1) I/O interrupt (2) save PC Memory add sub and or user program read store... jr interrupt service routine (3) jump to interrupt service routine (4) perform transfer (5)

CS 61C L7.1.1 VM I (7) K. Meinz, Summer 2004 © UCB SPIM I/O Simulation: Interrupt Driven I/O I.E. stands for Interrupt Enable Set Interrupt Enable bit to 1 have interrupt occur whenever Ready bit is set Received Byte Receiver Data 0xffff0004 Unused ( ) (IE) Receiver Control 0xffff0000 Ready (I.E.) Unused ( ) Transmitted Byte Transmitter Control 0xffff0008 Transmitter Data 0xffff000c Ready (I.E.) Unused ( ) Unused

CS 61C L7.1.1 VM I (8) K. Meinz, Summer 2004 © UCB OS: I/O Requirements The OS must be able to prevent: The user program from communicating with the I/O device directly If user programs could perform I/O directly: No protection to the shared I/O resources 3 types of communication are required: The OS must be able to give commands to the I/O devices The I/O device notifies OS when the I/O device has completed an operation or an error Data transfers between memory and I/O device

CS 61C L7.1.1 VM I (9) K. Meinz, Summer 2004 © UCB Kernel/User Mode OS Protection Generally restrict device access to OS Add a “mode bit” to the machine: K/U Only allow SW in “kernel mode” to access device registers If user programs could access device directly? -could destroy each others data,... -might break the devices, …

CS 61C L7.1.1 VM I (10) K. Meinz, Summer 2004 © UCB Crossing the System Boundary System loads user program into memory and ‘gives’ it use of the processor Switch back SYSCALL -request service -I/O TRAP (overflow) Interrupt Proc Mem I/O Bus cmd reg. data reg. System User

CS 61C L7.1.1 VM I (11) K. Meinz, Summer 2004 © UCB Syscall How does user invoke the OS? syscall instruction: invoke the kernel (Go to 0x , change to kernel mode) By software convention, $v0 has system service requested: OS performs request Spim manual has the interface.

CS 61C L7.1.1 VM I (12) K. Meinz, Summer 2004 © UCB Handling a Single Interrupt (1/3) An interrupt has occurred, then what? Automatically, the hardware copies PC into EPC ($14 on cop0) and puts correct code into Cause Reg ($13 on cop0) Automatically, PC is set to 0x , process enters kernel mode, and interrupt handler code begins execution Interrupt Handler code: Checks Cause Register (bits 5 to 2 of $13 in cop0) and jumps to portion of interrupt handler which handles the current exception

CS 61C L7.1.1 VM I (13) K. Meinz, Summer 2004 © UCB Handling a Single Interrupt (2/3) Sample Interrupt Handler Code.text 0x mfc0 $k0,$13 # $13 is Cause Reg sll $k0,$k0,26 # isolate srl $k0,$k0,28 # Cause bits Notes: Don’t need to save $k0 or $k1 -MIPS software convention to provide temp registers for operating system routines -Application software cannot use them Can only work on CPU, not on cop0

CS 61C L7.1.1 VM I (14) K. Meinz, Summer 2004 © UCB Handling a Single Interrupt (3/3) When the interrupt is handled, copy the value from EPC to the PC. Call instruction rfe (return from exception), which will return process to user mode and reset state to the way it was before the interrupt What about multiple interrupts?

CS 61C L7.1.1 VM I (15) K. Meinz, Summer 2004 © UCB Modified Interrupt Handler (1/3) Problem: When an interrupt comes in, EPC and Cause get overwritten immediately by hardware. Lost EPC means loss of user program. Solution: Modify interrupt handler. When first interrupt comes in: disable interrupts (in Status Register) save EPC, Cause, Status and Priority Level on Exception Stack re-enable interrupts continue handling current interrupt

CS 61C L7.1.1 VM I (16) K. Meinz, Summer 2004 © UCB Modified Interrupt Handler (2/3) When next (or any later) interrupt comes in: interrupt the first one disable interrupts (in Status Register) save EPC, Cause, Status and Priority Level (and maybe more) on Exception Stack determine whether new one preempts old one -if no, re-enable interrupts and continue with old one -if yes, may have to save state for the old one, then re-enable interrupts, then handle new one

CS 61C L7.1.1 VM I (17) K. Meinz, Summer 2004 © UCB Modified Interrupt Handler (3/3) Notes: Disabling interrupts is dangerous So we disable them for as short a time as possible: long enough to save vital info onto Exception Stack This new scheme allows us to handle many interrupts effectively.

CS 61C L7.1.1 VM I (18) K. Meinz, Summer 2004 © UCB Details not covered MIPS has a field to record all pending interrupts so that none are lost while interrupts are off; in Cause register The Interrupt Priority Level that the CPU is running at is set in memory MIPS has a field in that can mask interrupts of different priorities to implement priority levels; in Status register MIPS has limited nesting of saving KU,IE bits to recall in case higher priority interrupts; in Status Register

CS 61C L7.1.1 VM I (19) K. Meinz, Summer 2004 © UCB Things to Remember Kernel Mode v. User Mode: OS can provide security and fairness Syscall: provides a way for a programmer to avoid having to know details of each I/O device To be acceptable, interrupt handler must: service all interrupts (no drops) service by priority make all users believe that no interrupt has occurred

CS 61C L7.1.1 VM I (20) K. Meinz, Summer 2004 © UCB VM

CS 61C L7.1.1 VM I (21) K. Meinz, Summer 2004 © UCB Generalized Caching We’ve discussed memory caching in detail. Caching in general shows up over and over in computer systems Filesystem cache Web page cache Game Theory databases / tablebases Software memoization Others? Big idea: if something is expensive but we want to do it repeatedly, do it once and cache the result.

CS 61C L7.1.1 VM I (22) K. Meinz, Summer 2004 © UCB Another View of the Memory Hierarchy Regs L2 Cache Memory Disk Tape Instr. Operands Blocks Pages Files Upper Level Lower Level Faster Larger Cache Blocks Thus far { { Next: Virtual Memory

CS 61C L7.1.1 VM I (23) K. Meinz, Summer 2004 © UCB Memory Hierarchy Requirements What else might we want from our memory subsystem? … Share memory between multiple processes but still provide protection – don’t let one program read/write memory from another -Emacs on star Address space – give each process the illusion that it has its own private memory -Implicit in our model of a linker Called Virtual Memory

CS 61C L7.1.1 VM I (24) K. Meinz, Summer 2004 © UCB Virtual Memory Big Ideas Each address that a program uses (pc, $sp, $gp,.data, etc) is fake (even after linking)! Processor inserts new step: Every time we reference an address (in IF or MEM) … Translate fake address to real one. virtual physical

CS 61C L7.1.1 VM I (25) K. Meinz, Summer 2004 © UCB VM Ramifications Immediate consequences: Each program can operate in isolation! OS can decide where and when each goes in memory! HW/OS can grant different rights to different processes on same chunk of physical mem! Big question: How do we manage the VA  PA mappings? virtual address (inst. fetch load, store) Program operates in its virtual address space HW mapping physical address (inst. fetch load, store) Physical memory (caches)

CS 61C L7.1.1 VM I (26) K. Meinz, Summer 2004 © UCB (Weak) Analogy Book title like virtual address Library of Congress call number like physical address Card catalogue like page table, mapping from book title to call number On card for book, in local library vs. in another branch like valid bit indicating in main memory vs. on disk On card, available for 2-hour in library use (vs. 2-week checkout) like access rights

CS 61C L7.1.1 VM I (27) K. Meinz, Summer 2004 © UCB VM Ok, now how do we implement it? Simple solution: Linker assumes start addr at 0x0. Each process has a $base and $bound: -$base: start of physical address space -$bound: size of physical address space Algorithms: -VA  PA Mapping: PA = VA + $base -Bounds check: VA < $bound

CS 61C L7.1.1 VM I (28) K. Meinz, Summer 2004 © UCB Simple Example: Base and Bound Reg 0  OS User A User B User C $base $base+ $bound Same flaws as freelist malloc! Also: what if process size > mem What to do?? Enough space for User D, but discontinuous (“fragmentation problem”)  so what’s wrong?

CS 61C L7.1.1 VM I (29) K. Meinz, Summer 2004 © UCB VM Observations Working set of process is small, but distributed all over address space  Arbitrary mapping function, -keep working set in memory -rest on disk or unallocated. Fragmentation comes from variable- sized physical address spaces Allocate physical memory in fixed-sized chunks (1 mapping per chunk) FA placement of chunks -i.e. any V chunk of any process can map to any P chunk of memory.

CS 61C L7.1.1 VM I (30) K. Meinz, Summer 2004 © UCB Mapping Virtual Memory to Physical Memory 0 Physical Memory  Virtual Memory CodeStatic Heap Stack 64 MB Divide into equal sized chunks (about 4 KB - 8 KB) 0 Any chunk of Virtual Memory assigned to any chunk of Physical Memory (“page”)

CS 61C L7.1.1 VM I (31) K. Meinz, Summer 2004 © UCB Paging Organization Addr Trans MAP Page is unit of mapping Page also unit of transfer from disk to physical memory page 0 1K Virtual Memory VA page 1 page 31 1K 2048 page KB Pages VPN page PA Physical Memory 1K page 1 page 7... PPN

CS 61C L7.1.1 VM I (32) K. Meinz, Summer 2004 © UCB Virtual Memory Mapping Function Use table lookup (“Page Table”) for mappings: V Page number is index Mapping Function Physical Offset = Virtual Offset Physical Page Number = PageTable[Virtual Page Number] FYI: P.P.N. also called “Page Frame” or “Frame #”. Page Number Offset

CS 61C L7.1.1 VM I (33) K. Meinz, Summer 2004 © UCB Address Mapping: Page Table Virtual Address: VPNoffset Page Table located in physical memory index into page table PPN Physical Memory Address Page Table Val -id Access Rights Physical Page Address. V A.R. P. P. A.... offset

CS 61C L7.1.1 VM I (34) K. Meinz, Summer 2004 © UCB Page Table A page table: mapping function There are several different ways, all up to the operating system, to keep this data around. Each process running in the operating system has its own page table -Historically, OS changes page tables by changing contents of Page Table Base Register –Not anymore! We’ll explain soon.

CS 61C L7.1.1 VM I (35) K. Meinz, Summer 2004 © UCB Requirements revisited Remember the motivation for VM: Sharing memory with protection Different physical pages can be allocated to different processes (sharing) A process can only touch pages in its own page table (protection) Separate address spaces Since programs work only with virtual addresses, different programs can have different data/code at the same address!

CS 61C L7.1.1 VM I (36) K. Meinz, Summer 2004 © UCB Page Table Entry (PTE) Format Contains either Physical Page Number or indication not in Main Memory OS maps to disk if Not Valid (V = 0) If valid, also check if have permission to use page: Access Rights (A.R.) may be Read Only, Read/Write, Executable... Page Table Val -id Access Rights Physical Page Number V A.R. P. P. N. V A.R. P. P.N.... P.T.E.

CS 61C L7.1.1 VM I (37) K. Meinz, Summer 2004 © UCB Paging/Virtual Memory Multiple Processes User B: Virtual Memory  Code Static Heap Stack 0 Code Static Heap Stack A Page Table B Page Table User A: Virtual Memory  0 0 Physical Memory 64 MB

CS 61C L7.1.1 VM I (38) K. Meinz, Summer 2004 © UCB Comparing the 2 levels of hierarchy Cache VersionVirtual Memory vers. Block or LinePage MissPage Fault Block Size: 32-64BPage Size: 4K-8KB Placement:Fully Associative Direct Mapped, N-way Set Associative Replacement: Least Recently Used LRU or Random(LRU) Write Thru or BackWrite Back

CS 61C L7.1.1 VM I (39) K. Meinz, Summer 2004 © UCB Notes on Page Table OS must reserve “Swap Space” on disk for each process To grow a process, ask Operating System If unused pages, OS uses them first If not, OS swaps some old pages to disk (Least Recently Used to pick pages to swap) Will add details, but Page Table is essence of Virtual Memory