Lecture 7 TLB. Virtual Memory Approaches Time Sharing Static Relocation Base Base+Bounds Segmentation Paging.

Slides:



Advertisements
Similar presentations
Virtual Memory In this lecture, slides from lecture 16 from the course Computer Architecture ECE 201 by Professor Mike Schulte are used with permission.
Advertisements

Paging 1 CS502 Spring 2006 Paging CS-502 Operating Systems.
Lecture 7 Memory Management. Virtual Memory Approaches Time Sharing: one process uses RAM at a time Static Relocation: statically rewrite code before.
Chapter 19 Translation Lookaside Buffer Chien-Chung Shen CIS, UD
Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?
Lecture 34: Chapter 5 Today’s topic –Virtual Memories 1.
COMP 3221: Microprocessors and Embedded Systems Lectures 27: Virtual Memory - III Lecturer: Hui Wu Session 2, 2005 Modified.
Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University Virtual Memory 2 P & H Chapter
CS 153 Design of Operating Systems Spring 2015
Virtual Memory Adapted from lecture notes of Dr. Patterson and Dr. Kubiatowicz of UC Berkeley.
Virtual Memory Adapted from lecture notes of Dr. Patterson and Dr. Kubiatowicz of UC Berkeley and Rabi Mahapatra & Hank Walker.
Memory Management and Paging CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
CS-3013 & CS-502 Summer 2006 Paging1 CS-3013 & CS-502 Summer 2006.
Translation Buffers (TLB’s)
©UCB CS 161 Ch 7: Memory Hierarchy LECTURE 24 Instructor: L.N. Bhuyan
COMPSYS 304 Computer Architecture Memory Management Units Reefed down - heading for Great Barrier Island.
CS 153 Design of Operating Systems Spring 2015 Lecture 17: Paging.
Lecture 19: Virtual Memory
Computer Architecture Memory Management Units Iolanthe II - Reefed down, heading for Great Barrier Island.
Operating Systems ECE344 Ding Yuan Paging Lecture 8: Paging.
Chapter 18 Paging Chien-Chung Shen CIS, UD
PA0 due 60 hours. Lecture 4 Memory Management OSTEP Virtualization CPU: illusion of private CPU RAM: illusion of private memory Concurrency Persistence.
Virtual Memory. Virtual Memory: Topics Why virtual memory? Virtual to physical address translation Page Table Translation Lookaside Buffer (TLB)
2015/11/26\course\cpeg323-08F\Topic7e1 Virtual Memory.
Chapter 21 Swapping: Mechanisms Chien-Chung Shen CIS, UD
Review °Apply Principle of Locality Recursively °Manage memory to disk? Treat as cache Included protection as bonus, now critical Use Page Table of mappings.
PA1 due in one week TA office hour is 1-3 PM The grade would be available before that.
Multilevel Caches Microprocessors are getting faster and including a small high speed cache on the same chip.
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,
Lab4: Virtual Memory CS 3410 : Computer System Organization & Programming Spring 2015.
Constructive Computer Architecture Virtual Memory: From Address Translation to Demand Paging Arvind Computer Science & Artificial Intelligence Lab. Massachusetts.
LECTURE 12 Virtual Memory. VIRTUAL MEMORY Just as a cache can provide fast, easy access to recently-used code and data, main memory acts as a “cache”
Chapter 18 Paging Chien-Chung Shen CIS/UD
Chapter 21 Swapping: Mechanisms Chien-Chung Shen CIS/UD
Chapter 19 Translation Lookaside Buffer
Translation Lookaside Buffer
Paging Review Page tables store translations from virtual pages to physical frames Issues: Speed - TLBs Size – Multi-level page tables.
Review: Memory Virtualization
CS161 – Design and Architecture of Computer
Section 9: Virtual Memory (VM)
From Address Translation to Demand Paging
CS703 - Advanced Operating Systems
Paging COMP 755.
Section 9: Virtual Memory (VM)
Christo Wilson Lecture 7: Virtual Memory
From Address Translation to Demand Paging
Today How was the midterm review? Lab4 due today.
CS 704 Advanced Computer Architecture
PA1 is out Best by Feb , 10:00 pm Enjoy early
Paging Review: Virtual address spaces are broken into pages
Lecture 14 Virtual Memory and the Alpha Memory Hierarchy
Lecture 6 Memory Management
PA0 is due in 12 hours PA1 will be out in 12 hours
Lecture 33 Syed Mansoor Sarwar
Translation Lookaside Buffer
Morgan Kaufmann Publishers Memory Hierarchy: Virtual Memory
CSE 451: Operating Systems Autumn 2005 Memory Management
Translation Buffers (TLB’s)
Christo Wilson Lecture 7: Virtual Memory
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 2004 Page Tables, TLBs, and Other Pragmatics Hank Levy 1.
Translation Buffers (TLB’s)
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
CSE 451: Operating Systems Winter 2005 Page Tables, TLBs, and Other Pragmatics Steve Gribble 1.
Translation Buffers (TLBs)
Review What are the advantages/disadvantages of pages versus segments?
CS 444/544 Operating Systems II Virtual Memory Translation
Presentation transcript:

Lecture 7 TLB

Virtual Memory Approaches Time Sharing Static Relocation Base Base+Bounds Segmentation Paging

Basic Paging Flexible Addr Space don’t need to find contiguous RAM doesn’t waste whole data pages (valid bit) Easy to manage fixed size pages simple free list for unused pages no need to coalesce Too slow Too big

Page Mapping with Linear Page Table VirtMem PhysMem P1P2 P Page Tables 3 P P P

Where are Page Tables Stored? The size of a typical page table? assume 32-bit address space assume 4 KB pages assume 4 byte entries (or this could be less) 2 ^ (32 - log(4KB)) * 4 = 4 MB Store in memory, and CPU finds it via registers

Memory Accesses PT, load from 0x5000 Fetch instruction at 0x2010 PT, load from 0x5004 Exec, load from 0x0100 … 0x0010 movl 0x1100, %r8d 0x0014 addl $0x3, %r8d 0x0017 movl %r8d, 0x PT Assume 4KB pages Assume PTBR is 0x5000 Assume PTE’s are 4 bytes TOO SLOW

Other Information in Page Table What other data should go in page table entries besides translation? valid bit protection bits present bit reference bit dirty bit

// Extract the VPN from the virtual address VPN = (VirtualAddress & VPN_MASK) >> SHIFT // Form the address of the page-table entry (PTE) PTEAddr = PTBR + (VPN * sizeof(PTE)) // Fetch the PTE PTE = AccessMemory(PTEAddr) // Check if process can access the page if (PTE.Valid == False) RaiseException(SEGMENTATION_FAULT) else if (CanAccess(PTE.ProtectBits) == False) RaiseException(PROTECTION_FAULT) else // Access is OK: form physical address and fetch it offset = VirtualAddress & OFFSET_MASK PhysAddr = (PTE.PFN << PFN_SHIFT) | offset Register = AccessMemory(PhysAddr)

Translation Steps H/W: for each mem reference: 1. extract VPN (virt page num) from VA (virt addr) 2. calculate addr of PTE (page table entry) 3. fetch PTE 4. extract PFN (page frame num) 5. build PA (phys addr) 6. fetch PA to register

A Memory Trace int array[1000];... for (i = 0; i < 1000; i++) array[i] = 0; 0x1024 movl $0x0,(%edi,%eax,4) 0x1028 incl %eax 0x102c cmpl $0x03e8,%eax 0x1030 jne 0x1024

Array Iterator int sum = 0; for (i = 0; i < 10; i++) { sum += a[i]; } What is the memory trace?

Basic strategy Take advantage of repetition. Use a CPU cache. CPU TLB RAM PT

TLB Cache Type Fully-Associative: entries can go anywhere most common for TLBs must store whole key/value in cache search all in parallel There are other general cache types

TLB Contents VPN | PFN | other bits TLB valid bit whether the entry has a valid translation TLB protection bits rwx Address Space Identifier TLB dirty bit

A MIPS TLB Entry

1 VPN = (VirtualAddress & VPN_MASK) >> SHIFT 2 (Success, TlbEntry) = TLB_Lookup(VPN) 3 if (Success == True) // TLB Hit 4 if (CanAccess(TlbEntry.ProtectBits) == True) 5 Offset = VirtualAddress & OFFSET_MASK 6 PhysAddr = (TlbEntry.PFN << SHIFT) | Offset 7 AccessMemory(PhysAddr) 8 else 9 RaiseException(PROTECTION_FAULT) 10 else // TLB Miss 11 PTEAddr = PTBR + (VPN * sizeof(PTE)) 12 PTE = AccessMemory(PTEAddr) 13 if (PTE.Valid == False) 14 RaiseException(SEGMENTATION_FAULT) 15 else if (CanAccess(PTE.ProtectBits) == False) 16 RaiseException(PROTECTION_FAULT) 17 else 18 TLB_Insert(VPN, PTE.PFN, PTE.ProtectBits) 19 RetryInstruction()

Array Iterator with TLB int sum = 0; for (i = 0; i < 10; i++) { sum += a[i]; } How many TLB hits? How many TLB misses? Hit rate? Miss rate?

Reasoning about TLB Workload: series of loads/stores to accesses TLB: chooses entries to store in CPU Metric: performance (i.e., hit rate)

TLB Workloads Spatial locality Sequential array accesses can almost always hit in the TLB, and so are very fast! Temporal locality What pattern would be slow? highly random, with no repeat accesses

TLB Replacement Policies LRU: evict least-recently used a TLB slot is needed Random: randomly choose entries to evict When is each better? Sometimes random is better than a “smart” policy!

Who Handles The TLB Miss? H/W or OS? H/W: CPU must know where page tables are CR3 on x86 Page table structure not flexible OS: CPU traps into OS upon TLB miss

1 VPN = (VirtualAddress & VPN_MASK) >> SHIFT 2 (Success, TlbEntry) = TLB_Lookup(VPN) 3 if (Success == True) // TLB Hit 4 if (CanAccess(TlbEntry.ProtectBits) == True) 5 Offset = VirtualAddress & OFFSET_MASK 6 PhysAddr = (TlbEntry.PFN << SHIFT) | Offset 7 AccessMemory(PhysAddr) 8 else 9 RaiseException(PROTECTION_FAULT) 10 else // TLB Miss 11 PTEAddr = PTBR + (VPN * sizeof(PTE)) 12 PTE = AccessMemory(PTEAddr) 13 if (PTE.Valid == False) 14 RaiseException(SEGMENTATION_FAULT) 15 else if (CanAccess(PTE.ProtectBits) == False) 16 RaiseException(PROTECTION_FAULT) 17 else 18 TLB_Insert(VPN, PTE.PFN, PTE.ProtectBits) 19 RetryInstruction()

1 VPN = (VirtualAddress & VPN_MASK) >> SHIFT 2 (Success, TlbEntry) = TLB_Lookup(VPN) 3 if (Success == True) // TLB Hit 4 if (CanAccess(TlbEntry.ProtectBits) == True) 5 Offset = VirtualAddress & OFFSET_MASK 6 PhysAddr = (TlbEntry.PFN << SHIFT) | Offset 7 AccessMemory(PhysAddr) 8 else 9 RaiseException(PROTECTION_FAULT) 10 else // TLB Miss 11 RaiseException(TLB_MISS)

OS TLB Miss Handler OS: CPU traps into OS upon TLB miss 1.check page table for page table entry 2.if valid, extract PFN and update TLB w special inst 3.return from trap Where to resume execution? The instruction that caused the trap How to avoid double traps? keep TLB miss handlers in physical memory reserve some entries in the TLB for permanently-valid translations Modifying TLB entries is privileged

Context Switches What happens if a process uses the cached TLB entries from another process? Solutions? Flush TLB on each switch Remember which entries are for each process Address Space Identifier

You can think of the ASID as a process identifier (PID), but usually it has fewer bits 3 P1 (ASID 11) P2 (ASID 12) validVPNPFNASID ? 114? 103?

Next time: solving the too big problems