CSCC69: Operating Systems Tutorial 7 Some slides are borrowed from CSCC69 offered in winter 2012.

Slides:



Advertisements
Similar presentations
Tutorial 8 March 9, 2012 TA: Europa Shang
Advertisements

CSCC69: Operating Systems
1 Pintos Project #3 Virtual Memory The following slides were created by Xiaomo Liu and others for CS 3204 Fall And Modified by Nick Ryan for Spring.
EECS 470 Virtual Memory Lecture 15. Why Use Virtual Memory? Decouples size of physical memory from programmer visible virtual memory Provides a convenient.
Virtual Memory Introduction to Operating Systems: Module 9.
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.
Lecture 34: Chapter 5 Today’s topic –Virtual Memories 1.
The Memory Hierarchy (Lectures #24) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer Organization.
Memory Management Design & Implementation Segmentation Chapter 4.
Jaishankar Sundararaman
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 and Paging CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Solution to the Gaming Parlor Programming Project.
Translation Buffers (TLB’s)
Virtual Memory Management B.Ramamurthy. Paging (2) The relation between virtual addresses and physical memory addres- ses given by page table.
03/22/2004CSCI 315 Operating Systems Design1 Virtual Memory Notice: The slides for this lecture have been largely based on those accompanying the textbook.
1 Virtual Memory Management B.Ramamurthy Chapter 10.
CS 333 Introduction to Operating Systems Class 13 - Virtual Memory (3) Jonathan Walpole Computer Science Portland State University.
Operating Systems Lecture 11 MIPS TLB Structure Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software.
Operating Systems Chapter 8
CS 153 Design of Operating Systems Spring 2015 Lecture 17: Paging.
Operating Systems ECE344 Ding Yuan Paging Lecture 8: Paging.
Chapter 9: Virtual Memory Background Demand Paging Copy-on-Write Page Replacement Allocation of Frames Thrashing Memory-Mapped Files Allocating Kernel.
Lecture Topics: 11/17 Page tables TLBs Virtual memory flat page tables
Virtual Memory Expanding Memory Multiple Concurrent Processes.
© 2004, D. J. Foreman 1 Virtual Memory. © 2004, D. J. Foreman 2 Objectives  Avoid copy/restore entire address space  Avoid unusable holes in memory.
Chapter 4 Memory Management Virtual Memory.
Memory Management Fundamentals Virtual Memory. Outline Introduction Motivation for virtual memory Paging – general concepts –Principle of locality, demand.
Fundamentals of Programming Languages-II Subject Code: Teaching SchemeExamination Scheme Theory: 1 Hr./WeekOnline Examination: 50 Marks Practical:
Virtual Memory Part 1 Li-Shiuan Peh Computer Science & Artificial Intelligence Lab. Massachusetts Institute of Technology May 2, 2012L22-1
Virtual Memory 1 1.
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?
CPS110: Page replacement Landon Cox. Replacement  Think of physical memory as a cache  What happens on a cache miss?  Page fault  Must decide what.
Lecture Topics: 11/24 Sharing Pages Demand Paging (and alternative) Page Replacement –optimal algorithm –implementable algorithms.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Demand Paging.
Processes and Virtual Memory
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Virtual Memory Implementation.
CS2100 Computer Organisation Virtual Memory – Own reading only (AY2015/6) Semester 1.
Virtual Memory Ch. 8 & 9 Silberschatz Operating Systems Book.
Pintos project 3: Virtual Memory Management
Lecture 14 PA2. Lab 2: Demand Paging Implement the following syscalls xmmap, xmunmap, vcreate, vgetmem/vfreemem, srpolicy Deadline: November , 10:00.
Virtual Memory 1 Computer Organization II © McQuain Virtual Memory Use main memory as a “cache” for secondary (disk) storage – Managed jointly.
1 Contents Memory types & memory hierarchy Virtual memory (VM) Page replacement algorithms in case of VM.
Virtual Memory.
Translation Lookaside Buffer
Lecture 11 Virtual Memory
Virtual Memory So, how is this possible?
A Real Problem What if you wanted to run a program that needs more memory than you have? September 11, 2018.
Virtual Memory User memory model so far:
Chapter 9: Virtual Memory
CSE 153 Design of Operating Systems Winter 2018
CSE 153 Design of Operating Systems Winter 2018
Evolution in Memory Management Techniques
Translation Lookaside Buffer
Chapter 9: Virtual Memory
Translation Buffers (TLB’s)
CS510 Operating System Foundations
Contents Memory types & memory hierarchy Virtual memory (VM)
CSE451 Virtual Memory Paging Autumn 2002
Translation Buffers (TLB’s)
CSE 153 Design of Operating Systems Winter 2019
Paging and Segmentation
CSE 153 Design of Operating Systems Winter 2019
Translation Buffers (TLBs)
Virtual Memory Use main memory as a “cache” for secondary (disk) storage Managed jointly by CPU hardware and the operating system (OS) Programs share main.
Virtual Memory.
Review What are the advantages/disadvantages of pages versus segments?
Virtual Memory.
Virtual Memory 1 1.
Presentation transcript:

CSCC69: Operating Systems Tutorial 7 Some slides are borrowed from CSCC69 offered in winter 2012

Virtual Memory Allowing a program to be designed as though there is only one kind of memory, "virtual" memory, which behaves like directly addressable read/write memory (RAM). – Need to manage what is in memory (TLB, page table, coremap) – Swap pages on request

Virtual Memory When a page is requested but not in memory? Page fault When there is no more space in main memory to bring in pages? – Replace, evict

OS 161 Page Tables OS/161 paging uses virtual memory objects struct vm_object defined in src/kern/include/vmprivate.h – A VM object defines a region of an address space – Contains a base virtual address and an array of pages – Redzone - A possible guard band against other vm_objects struct vm_object { struct lpage_array *vmo_lpages; vaddr_t vmo_base; size_t vmo_lower_redzone; };

OS 161 Page Tables Each VM object has an array of logical pages (lpages), one for each virtual page in the region lpage stores where the page is in physical memory (lp_paddr), and where the page is stored in swap when not in main memory (lp_swapaddr) – If the page is not in RAM, lp_paddr is INVALID_PADDR. – If no swap has been allocated, lp_swapaddr is INVALID_SWAPADDR. Low bits of lp_addr used to hold flags (DIRTY, PINNED) Read comments in src/kern/include/vmprivate.h struct lpage { volatile paddr_t lp_paddr; off_t lp_swapaddr; struct spinlock lp_spinlock; };

Lpage Operations lpage_create - creates an lpage object. lpage_destroy - deallocates an lpage, releases any RAM or swap pages involved lpage_lock/unlock - for exclusive access to an lpage lpage_copy - clones an lpage, including the contents lpage_zerofill - materializes an lpage and zero- fill it

Coremap Logical pages are nice, but we ultimately need to work with physical memory Need to keep track of physical pages Coremap contains an entry per page frame to indicate its status

Coremap Inverted page table: Maps pages in memory to their virtual addresses – It allows you to use the physical address to find the logical page that inhabits it (NULL if empty) – Has bit flags that indicate if pages are kernel pages, pinned (busy), etc. struct coremap_entry { struct lpage *cm_lpage; /* logical page we hold, or NULL */... /*flags*/ };

Coremap Functions tlb_replace tlb_invalidate(i): If you know the slot tlb_unmap(vaddr) mipstlb_getslot: None available? Call tlb_replace coremap_alloc_one_page(lp, pin): called when a page is needed. None free? Call do_page_replace coremap_{pin, unpin}: pin/unpin a page page_replace: returns number of the page to be evicted – Replacement algorithm do_evict: performs the page eviction do_page_replace: starting point for page replacements

Coremap_entry vs. lpage Each lpage entry is a logical piece of memory – That memory may be in memory – It may also be in swap (on disk) – Each lpage points to the location of its data The coremap maps physical memory usage – When you need physical memory, consult the coremap to see what memory is free – Each entry points to an lpage

Coremap_entry vs. lpage

MIPS TLB Entry (Tanslation Lookahead Buffer) TLB keeps track of mapping from virtual to physical pages High-order word – Virtual page number for lookup (TLBHI_VPAGE) : 20 bits (mask 0xffff000) – Also has 6 bits for hardware PID; 6 bits unused in OS/161 Low-order word – Physical page number (TLBLO_PPAGE) : 20 bits – Also has 4 status bits, and 8 unused bits – Eg: V for “valid”, D for “dirty” (“writable”/”referenced”) src/kern/arch/mips/include/tlb.h

MIPS TLB Status Bits VPN (abbr. for Virtual Page Frame Number) is the high 20 bits of a virtual address. PPN is the high 20 bits of a physical address space. When Dirty bit is 1, it means this page is writable, otherwise, it’s read-only. When Valid bit is 1, it means this TLB entry contains a valid translation. In OS161, we can just ignore the ASID part and Global bit

Address Translation Process

MIPS TLB In our case the TLB is software-managed (by the OS) On memory read/write, checks the entries in the TLB in parallel: – Entry is found - TLB hit – Entry not found - TLB miss Causes EX_TLBL for loads (reads) Causes EX_TLBS for stores (writes) – Protection fault - trying to write to read-only memory causes EX_MOD (modify)

TLB Exceptions vm_fault is called from mips_trap in trap.c for any TLB exception – Different types of VM_FAULT_* are passed on Eventually gets to lpage_fault (this is where you come in) On a TLB miss: – Look up the page in the page table Implemented in src/kern/vm/addrspace: as_fault – Choose an entry in the TLB to replace it In src/kern/arch/mips/mips/coremap.c: tlb_replace – Update TLB entry with PTE from page table In src/kern/arch/mips/mips/coremap.c: mmu_map

How to manipulate the TLB entries? The functions that access TLB can be found at /kern/arch/mips/include/tlb.h. tlb_probe: to query the TLB bank. tlb_read/tlb_write: to read/write a specific TLB entry. tlb_random: to let the hardware decide which entry to write to.

Page Faults – High Level

Page Faults Minor fault: TLB does not contain a requested PTE (but it is in memory) – Find the Page Table Entry for it and insert the new mapping into the TLB and the coremap – See coremap.c for a function you can use! (mmu_map) Major fault: the desired page is not in main memory (it’s either in swap space, or hasn’t been created yet) – How do we know if it’s a major fault? – lp_paddr field of the lpage struct will tell you – lp_paddr is INVALID_PADDR if the page is not in memory

How to handle TLB Miss? For VM_FAULT_READ or VM_FAULT_WRITE, check address space page table if that page actually exists (PTE_P bit). If not, allocate a new page and modify the page table entry to insert the mapping. – A newly allocated page should be readable and writable. If related attributes are already set, then leave them alone. Use tlb_random to insert this mapping to TLB. – You can implement some other algorithm. Find the functions in tlb.h

How to handle TLB Miss? VM_FAULT_READONLY pages are already in memory and the mapping is already in TLB bank, the dirty bit is 0 and user try to write this page. First check if user can really write this page – by the access bits in the low 12 bits of page table entry. – In as_define_region, user have passed in some attributes like readable, writable and executable. Store them there and use them to check here. – Check access right violation. Panic or kill current process, if it happens. If user can actually write this page, then first query TLB to get the index of the TLB entry, set the dirty bit and write it back using tlb_write(). Change the physical page state to PAGE_STATE_DIRTY.

Page Faults Major fault: desired page is not in memory – Page hasn’t been created yet A new page is allocated to the process and initialized (zero- filled) in src/kern/vm/addrspace.c: as_fault – Page is in swap We need to swap the page into memory from swap space Need a page of physical memory for the page – look at lpage_copy for ideas Set lp_*addr to INVALID_* when appropriate - to indicate page is not in main memory or swap

Assignment 2 Implement paging by writing the following functions: – lpage_fault --- handles a page fault – lpage_evict --- evicts an lpage from physical memory – page_replace --- implements page replacement policy sequential replacement random replacement Much of the system is already provided

Page Eviction lpage_evict – evicts an lpage from physical memory Really, evict the contents of the page at lp_paddr by writing it to lp_swapaddr on the swap device (if it is dirty), and marklp_paddr invalid Called by do_evict when a physical page is chosen for eviction

Page Replacement Updating the victim’s PTE to show that it is in swap – swap functions src/kern/vm/swap.c Copying it to disk (iff it is dirty) Evicting (/invalidating) victim’s PTE from the TLB Loading the new page into memory Updating the new page’s PTE and inserting it into the TLB

Swapping You do not need to implement functions to move a page from disk to memory and from memory to disk (see kern/vm/swap.c).

Assignment 2 base-design.txt: read it starter code: read it Write test programs? – Access lots of memory to test your paging Take notes

Synchronization OS161 assumes that lpages, vm_objects and address spaces are not shared. – But one thread may access an lpage belonging to another thread, in order to evict a page – Thus you need not use locks when accessing address spaces and vm_objects, but lpages do need synchronization Bit lock is used to save space (see lpage_lock/lpage_unlock)

Synchronization global_paging_lock, limits number of pages pinned at any one time swaplock: used by swap_alloc, swap_free, swap_reserve, swap_unreserve cm_pinned locks pages that are in transit. one bit lock per lpage Lock Ordering (i.e you should acquire in this order): – global_paging_lock BEFORE coremap pages BEFORElpages