Lab4: Virtual Memory CS 3410 : Computer System Organization & Programming Spring 2015.

Slides:



Advertisements
Similar presentations
Memory Management: Overlays and Virtual Memory
Advertisements

Computer Organization CS224 Fall 2012 Lesson 44. Virtual Memory  Use main memory as a “cache” for secondary (disk) storage l Managed jointly by CPU hardware.
Virtual Memory 3 Hakim Weatherspoon CS 3410, Spring 2011 Computer Science Cornell University P & H Chapter
CSE 490/590, Spring 2011 CSE 490/590 Computer Architecture Virtual Memory I Steve Ko Computer Sciences and Engineering University at Buffalo.
Virtual Memory 2 Hakim Weatherspoon CS 3410, Spring 2011 Computer Science Cornell University P & H Chapter
Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University Virtual Memory 1 P & H Chapter 5.4 (up to TLBs)
Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University Virtual Memory 2 P & H Chapter
CS 153 Design of Operating Systems Spring 2015
CS 153 Design of Operating Systems Spring 2015
CS 333 Introduction to Operating Systems Class 12 - Virtual Memory (2) Jonathan Walpole Computer Science Portland State University.
CS 333 Introduction to Operating Systems Class 11 – Virtual Memory (1)
Memory Management (II)
CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Memory Management -3 CS 342 – Operating Systems Ibrahim Korpeoglu Bilkent.
Recap. The Memory Hierarchy Increasing distance from the processor in access time L1$ L2$ Main Memory Secondary Memory Processor (Relative) size of the.
Memory Management and Paging CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Computer ArchitectureFall 2007 © November 21, 2007 Karem A. Sakallah Lecture 23 Virtual Memory (2) CS : Computer Architecture.
CS 333 Introduction to Operating Systems Class 11 – Virtual Memory (1)
Chapter 3.2 : Virtual Memory
EECE476: Computer Architecture Lecture 27: Virtual Memory, TLBs, and Caches Chapter 7 The University of British ColumbiaEECE 476© 2005 Guy Lemieux.
Virtual Memory 2 Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University P & H Chapter 5.4.
CS 241 Section Week #12 (04/22/10).
Virtual Memory By: Dinouje Fahih. Definition of Virtual Memory Virtual memory is a concept that, allows a computer and its operating system, to use a.
Address Translation Mechanism of 80386
CS333 Intro to Operating Systems Jonathan Walpole.
1 Chapter 3.2 : Virtual Memory What is virtual memory? What is virtual memory? Virtual memory management schemes Virtual memory management schemes Paging.
Chapter 8 – Main Memory (Pgs ). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.
Virtual Memory Expanding Memory Multiple Concurrent Processes.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-8 Memory Management (2) Department of Computer Science and Software.
Virtual Memory. Virtual Memory: Topics Why virtual memory? Virtual to physical address translation Page Table Translation Lookaside Buffer (TLB)
Virtual Memory 1 1.
1 Memory Management. 2 Fixed Partitions Legend Free Space 0k 4k 16k 64k 128k Internal fragmentation (cannot be reallocated) Divide memory into n (possible.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Virtual Memory Hardware.
4.3 Virtual Memory. Virtual memory  Want to run programs (code+stack+data) larger than available memory.  Overlays programmer divides program into pieces.
Virtual Memory 3 Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University P & H Chapter 5.4.
CS2100 Computer Organisation Virtual Memory – Own reading only (AY2015/6) Semester 1.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Virtual Memory: Concepts Slides adapted from Bryant.
Carnegie Mellon Introduction to Computer Systems / Spring 2009 March 23, 2009 Virtual Memory.
CHAPTER 3-3: PAGE MAPPING MEMORY MANAGEMENT. VIRTUAL MEMORY Key Idea Disassociate addresses referenced in a running process from addresses available in.
Virtual Memory Review Goal: give illusion of a large memory Allow many processes to share single memory Strategy Break physical memory up into blocks (pages)
Constructive Computer Architecture Virtual Memory: From Address Translation to Demand Paging Arvind Computer Science & Artificial Intelligence Lab. Massachusetts.
Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University P & H Chapter 5.7.
CS203 – Advanced Computer Architecture Virtual Memory.
Chapter 7: Main Memory CS 170, Fall Program Execution & Memory Management Program execution Swapping Contiguous Memory Allocation Paging Structure.
Memory Management memory hierarchy programs exhibit locality of reference - non-uniform reference patterns temporal locality - a program that references.
CS161 – Design and Architecture of Computer
Translation Lookaside Buffer
Virtual Memory Acknowledgment
ECE232: Hardware Organization and Design
CS161 – Design and Architecture of Computer
Section 9: Virtual Memory (VM)
Address Translation Mechanism of 80386
Virtual Memory Hakim Weatherspoon CS 3410, Spring 2013
CSCI206 - Computer Organization & Programming
Virtual Memory 3 Hakim Weatherspoon CS 3410, Spring 2011
Virtual Memory Hakim Weatherspoon CS 3410, Spring 2012
CS399 New Beginnings Jonathan Walpole.
Virtual Memory Hardware
Translation Lookaside Buffer
Morgan Kaufmann Publishers Memory Hierarchy: Virtual Memory
CSE 451: Operating Systems Autumn 2005 Memory Management
Prof. Kavita Bala and Prof. Hakim Weatherspoon
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE451 Virtual Memory Paging Autumn 2002
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
CS703 - Advanced Operating Systems
Virtual Memory Hakim Weatherspoon CS 3410 Computer Science
4.3 Virtual Memory.
Virtual Memory 1 1.
Presentation transcript:

Lab4: Virtual Memory CS 3410 : Computer System Organization & Programming Spring 2015

Why do we need Virtual Memory? We want to run multiple processes at the same time! Even if we have multiple CPUs, addresses used by multiple processes will conflict! Even if they do not conflict, they may not fit in memory together! Solution: Use a Mapping! Map a Virtual Address (generated by CPU) to a Physical Address (in memory)!

How does Virtual Memory work? How do we create the “map” that maps a virtual address generated by the CPU to a physical address used by main memory?

How does Virtual Memory work? Each process has its own virtual address space – A process is a program being executed – Programmer can code as if they own all of memory On-the-fly at runtime, for each memory access – all accesses are indirect through a virtual address – translate fake virtual address to a real physical address – redirect load/store to the physical address Addresses are mapped in chunks called Pages! – 4KB – 16KB pages are typical.

MMU (Memory Management Unit) Responsible for translating on the fly Keeps a Page Table Essentially, just a big array of integers: – paddr = PageTable[vaddr]; – Page tables are indexed by virtual page number!

Address Translation Page size: 4 kB = 2 12 vaddrPage OffsetVirtual page number Page offsetPhysical page numberpaddr CPU generated Main Memory Lookup in page table

Page Table Size Where is the page table stored? – In Memory! – PTBR (Page Table Base Register) holds the base address for the page table of the current process. How large is the page table? page table size = logical address space size page size × page table entry size

Page Table Size Page table size: – Many processors have 32-bit logical addresses, which results in a 4GB logical address space size. The page table entries size is usually 4B. If the page size is 4KB then the page table size is: – 4 MB in memory just for the page table is a too much! 4 GB 4 kB × 4 B Page table size = = 4 MB

Multi-level Page Tables Used to reduce page table size! 10 bits PTBR 10 bits vaddr PDEntry Page Directory Page Table PTEntry Page Word

Virtual Memory with 3 Processes Each process has its own page tables! PTBR Page Directory 1 Page Directory 2 Page Directory 3 Page Table 1 Page Table 1 Page Table 2 Page Table 1 … … Memory Page … Process 1 Page … … Process 2 Page … Process 3 … CPU

Paging! How can we run processes larger than the physical memory? – The OS copies pages from disk to main memory as they are needed! – Memory is the cache of disk!

Page Table Performance Every load/store translated to physical addresses. To write to virtual memory: – Look up vaddr to paddr translation in page table. – Then write to physical memory. Page Table Miss: Page fault – load the page on disk and retry instruction – kill program if the page really doesn’t exist – tell the program it made a mistake

Paging is too slow!! Solution: Instead of looking up translations on Page Tables, cache them: TLB (Translation Lookaside Buffer) – A small, very fast cache of recent address mappings TLB hit: avoids PageTable lookup TLB miss: do PageTable lookup, cache result for later

Address Lookup Procedure

Lab4 Modify the MIPS simulator to simulate virtual memory! – Including a shared memory system call with copy- on-write semantics.

A quick reminder: C Operators Leftshift a variable A to the left by 5 bits and store it in A. A = A << 5; Rightshift a variable A to the left by 5 bits and store it in A. A = A >> 5; AND, OR, and XOR two variables and store them into a third variable. z = x & y;z = x | y;z = x ^ y; Change a variable into its negation. z = ~z;

Two-level Page Table Structure The Context Register (c0r4) contains a pointer to a 4096-byte page directory – 4096 Bytes = 2 12 Bytes  We need 20 bits! Each of the 4-byte page directory entries (PDEs) points to a 4096-byte page table. – 4096/4 = 1024 entries  We need 10 bits! Each of the 4-byte page table entries (PTEs) points to a 4096-byte page of data. 4096/4 = 1024 entries  We need 10 bits!

Two-level Page Table 10 bits c0r4 10 bits vaddr PDEntry Page Directory Page Table PTEntry Page Word

Create address space per process! unsigned int create_address_space() { return allocate_physical_page()<<12; } allocate_physical_page() returns a 32-bit physical page number for a free physical page We only need 20 bits to map into a page. Discard the left 12-bits and use the remaining 20 bits! Ex. If we had 1,000 things to refer to and ids from 0 to 10,000, we would only use the last 3 digits! Same logic.

Map page! Returns the physical page number for a given virtual address. unsigned int map_page( unsigned int context, /*page number for the PageDir*/ unsigned int vaddr, /* virtual address*/ int writable, /* zero for read-only, non-zero for writable */ int executable /* zero for no-execute, non-zero for executable */ ); Check where map_page is called: – Context is CPR[0][4]: Context Register – Upper 20 bits of CPR[0][4] is the page number for the PageDir

Get the indexes first! We have a 32-bit Virtual Address vaddr Get the index of the page directory entry and the index of the page table entry unsigned int pd_index = vaddr >> 22; // We only need the left 10 bits! unsigned int pt_index =(vaddr > 22; // We only need the second left 10 bits!

Two-level Page Table 10 bits c0r4 10 bits vaddr PDEntry Page Directory Page Table PTEntry Page Word

Get the PDE! Get the page directory entry! unsigned int *directory = (unsigned int*)dereference_physical_page(context >> 12); // Page number for page dir is in c0r4 (context) // We only need the left 20 bits: rest is reserved! unsigned int pde = directory[pd_index]; // Get the page directory entry!

Is the PDE valid? NO?  Allocate a fresh page for the Page Table! Update the Page Directory! if(!(pde & 1)){ unsigned int newptbl = allocate_physical_page(); pde = (newptbl << 12) | 1; // We only care about the right 20 bits! directory[pd_index] = pde; }

Now we know that PDE valid! Get the page table entry! unsigned int *page_table = (unsigned int*)dereference_physical_page(pde >> 12); // We need only the next 10 bits in pde! unsigned int pte = page_table[pt_index]; // We have the Page Table Entry now!

Is the PTE valid? NO?  Allocate a fresh page! if(!(pte & 1)){ unsigned int page = allocate_physical_page(); pte = (page << 12); // We only care about the right 20 bits! }

We are so close! Set the permissions, update the page table entry! page_table[pt_index]= pte | (executable << 2)| (writable << 1) | 1; // Should be executable for instruction fetch! // Should be writable for writes! // Bits can be found in mem.c

And done! Return the Physical Address for this Page! // Shift it back before returning! return pte >> 12;

More? map_shared_page – To support shared, copy-on-write virtual memory. pfault – To support killing processes and shared, copy-on- write virtual memory.