CS 4284 Systems Capstone. Virtual Memory Page Tables Godmar Back

Similar documents
Virtual Memory: From Address Translation to Demand Paging

Virtual Memory. Daniel Sanchez Computer Science & Artificial Intelligence Lab M.I.T. November 15, MIT Fall 2018 L20-1

Learning Outcomes. An understanding of page-based virtual memory in depth. Including the R3000 s support for virtual memory.

Address Translation. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Learning Outcomes. An understanding of page-based virtual memory in depth. Including the R3000 s support for virtual memory.

VIRTUAL MEMORY II. Jo, Heeseung

Virtual Memory. Daniel Sanchez Computer Science & Artificial Intelligence Lab M.I.T. April 12, 2018 L16-1

Virtual memory Paging

Virtual Memory: From Address Translation to Demand Paging

Multi-level Translation. CS 537 Lecture 9 Paging. Example two-level page table. Multi-level Translation Analysis

CSE 451: Operating Systems Winter Page Table Management, TLBs and Other Pragmatics. Gary Kimura

Administrivia. Lab 1 due Friday 12pm. We give will give short extensions to groups that run into trouble. But us:

PROCESS VIRTUAL MEMORY. CS124 Operating Systems Winter , Lecture 18

Virtual Memory. CS 351: Systems Programming Michael Saelee

CS 152 Computer Architecture and Engineering. Lecture 11 - Virtual Memory and Caches

CS 318 Principles of Operating Systems

CSE 120 Principles of Operating Systems

PROCESS VIRTUAL MEMORY PART 2. CS124 Operating Systems Winter , Lecture 19

Virtual memory - Paging

The process. one problem

John Wawrzynek & Nick Weaver

Modern Virtual Memory Systems. Modern Virtual Memory Systems

Virtual Memory 2. To do. q Handling bigger address spaces q Speeding translation

Chapter 8: Memory-Management Strategies

Chapter 8 Memory Management

Virtual Memory. CS 3410 Computer System Organization & Programming. [K. Bala, A. Bracy, E. Sirer, and H. Weatherspoon]

Virtual Memory. Virtual Memory

16 Sharing Main Memory Segmentation and Paging

Virtual Memory. CS 3410 Computer System Organization & Programming

CS 61C: Great Ideas in Computer Architecture. Lecture 23: Virtual Memory. Bernhard Boser & Randy Katz

CSE 351. Virtual Memory

Virtual Memory 2. Today. Handling bigger address spaces Speeding translation

CS 153 Design of Operating Systems Winter 2016

CHAPTER 8 - MEMORY MANAGEMENT STRATEGIES

Chapter 8: Main Memory

ECE 598 Advanced Operating Systems Lecture 14

CS5460: Operating Systems Lecture 14: Memory Management (Chapter 8)

Chapter 8: Main Memory. Operating System Concepts 9 th Edition

Translation Buffers (TLB s)

Chapter 8: Main Memory

CS 61C: Great Ideas in Computer Architecture. Lecture 23: Virtual Memory

Memory management. Last modified: Adaptation of Silberschatz, Galvin, Gagne slides for the textbook Applied Operating Systems Concepts

ECE 571 Advanced Microprocessor-Based Design Lecture 12

Basic Memory Management

CHAPTER 8: MEMORY MANAGEMENT. By I-Chen Lin Textbook: Operating System Concepts 9th Ed.

Basic Memory Management. Basic Memory Management. Address Binding. Running a user program. Operating Systems 10/14/2018 CSC 256/456 1

Chapter 8. Virtual Memory

Virtual Memory. Today. Handling bigger address spaces Speeding translation

Computer Structure. X86 Virtual Memory and TLB

Address Translation. Tore Larsen Material developed by: Kai Li, Princeton University

CS162 - Operating Systems and Systems Programming. Address Translation => Paging"

Virtual Memory: Mechanisms. CS439: Principles of Computer Systems February 28, 2018

Memory Hierarchy Requirements. Three Advantages of Virtual Memory

CSE 120 Principles of Operating Systems Spring 2017

Virtual Memory 2. q Handling bigger address spaces q Speeding translation

Recall: Address Space Map. 13: Memory Management. Let s be reasonable. Processes Address Space. Send it to disk. Freeing up System Memory

CS162 Operating Systems and Systems Programming Lecture 12. Address Translation. Page 1

COSC3330 Computer Architecture Lecture 20. Virtual Memory

Virtual Memory. Samira Khan Apr 27, 2017

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 23

Main Memory: Address Translation

Memory Management. An expensive way to run multiple processes: Swapping. CPSC 410/611 : Operating Systems. Memory Management: Paging / Segmentation 1

Virtual Memory #2 Feb. 21, 2018

Virtual Memory. Patterson & Hennessey Chapter 5 ELEC 5200/6200 1

CS 152 Computer Architecture and Engineering. Lecture 9 - Virtual Memory

Opera&ng Systems ECE344

Address spaces and memory management

CSE 153 Design of Operating Systems

CS370 Operating Systems

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 27, SPRING 2013

Operating Systems (1DT020 & 1TT802) Lecture 9 Memory Management : Demand paging & page replacement. Léon Mugwaneza

Virtual Memory. Motivation:

Virtual Memory. CS61, Lecture 15. Prof. Stephen Chong October 20, 2011

Memory Management. Memory

CSE 120. Translation Lookaside Buffer (TLB) Implemented in Hardware. July 18, Day 5 Memory. Instructor: Neil Rhodes. Software TLB Management

HY225 Lecture 12: DRAM and Virtual Memory

CS 5523 Operating Systems: Memory Management (SGG-8)

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 24

Virtual Memory. Motivations for VM Address translation Accelerating translation with TLBs

CS252 Spring 2017 Graduate Computer Architecture. Lecture 17: Virtual Memory and Caches

The memory of a program. Paging and Virtual Memory. Swapping. Paging. Physical to logical address mapping. Memory management: Review

Lecture 19: Virtual Memory: Concepts

CS 333 Introduction to Operating Systems. Class 11 Virtual Memory (1) Jonathan Walpole Computer Science Portland State University

CPS 104 Computer Organization and Programming Lecture 20: Virtual Memory

ECE 571 Advanced Microprocessor-Based Design Lecture 13

Chapter 5 (Part II) Large and Fast: Exploiting Memory Hierarchy. Baback Izadi Division of Engineering Programs

A Typical Memory Hierarchy

Virtual Memory, Address Translation

15 Sharing Main Memory Segmentation and Paging

@2010 Badri Computer Architecture Assembly II. Virtual Memory. Topics (Chapter 9) Motivations for VM Address translation

Virtual Memory. Stefanos Kaxiras. Credits: Some material and/or diagrams adapted from Hennessy & Patterson, Hill, online sources.

A Few Problems with Physical Addressing. Virtual Memory Process Abstraction, Part 2: Private Address Space

Topics: Memory Management (SGG, Chapter 08) 8.1, 8.2, 8.3, 8.5, 8.6 CS 3733 Operating Systems

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

CS 152 Computer Architecture and Engineering. Lecture 9 - Address Translation

Processes and Virtual Memory Concepts

UC Berkeley CS61C : Machine Structures

CS 152 Computer Architecture and Engineering. Lecture 9 - Address Translation

Memory Management Topics. CS 537 Lecture 11 Memory. Virtualizing Resources

Virtual Memory Review. Page faults. Paging system summary (so far)

Transcription:

CS 4284 Systems Capstone Virtual Memory Page Tables Godmar Back

Virtual Memory

Virtual Memory Is not a kind of memory Is a technique that combines one or more of the following concepts: Address translation (always) Paging (not always) Protection (not always, but usually) Can make storage that isn t physical random access memory appear as though it were

Goals for Virtual Memory Virtualization Maintain illusion that each process has entire memory to itself Allow processes access to more memory than is really in the machine (or: sum of all memory used by all processes > physical memory) Protection make sure there s no way for one process to access another process s data

Context Switching Process 1 Process 2 user mode kernel mode Kernel P1 starts P2 starts P1 exits P2 exits

3 GB 1 GB FFFFFFFF C0400000 P1 Process 1 Active in user mode C0000000 0 kheap kbss kdata kcode ustack (1) udata (1) ucode (1) access possible in user mode user (2) user (2) user (2) user (1) user (1) user (1) kernel kernel kernel kernel free used

3 GB 1 GB FFFFFFFF C0400000 P1 Process 1 Active in kernel mode access requires kernel mode C0000000 0 kheap kbss kdata kcode ustack (1) udata (1) ucode (1) access possible in user mode user (2) user (2) user (2) user (1) user (1) user (1) kernel kernel kernel kernel free used

3 GB 1 GB FFFFFFFF C0400000 P2 Process 2 Active in kernel mode access requires kernel mode C0000000 0 kheap kbss kdata kcode ustack (2) udata (2) ucode (2) access possible in user mode user (2) user (2) user (2) user (1) user (1) user (1) kernel kernel kernel kernel free used

3 GB 1 GB FFFFFFFF C0400000 P2 Process 2 Active in user mode C0000000 0 kheap kbss kdata kcode ustack (2) udata (2) ucode (2) access possible in user mode user (2) user (2) user (2) user (1) user (1) user (1) kernel kernel kernel kernel free used

Page Tables How are the arrows in previous pictures represented? Page Table: mathematical function Trans Trans: { Process Ids } { Virtual Addresses } { user, kernel } ({ read, write, execute }) { Physical Addresses } { INVALID } Typically (though not on all architectures) have Trans(p i, v a, user, *) = Trans(p i, v a, kernel, *) OR Trans(p i, v a, user, *) = INVALID E.g., user virtual addresses can be accessed in kernel mode

Sharing Variations We get user-level sharing between processes p 1 and p 2 if Trans(p 1, v a, user, *) = Trans(p 2, v a, user, *) Shared physical address doesn t need to be mapped at same virtual address, could be mapped at v a in p 1 and v b in p 2 : Trans(p 1, v a, user, *) = Trans(p 2, v b, user, *) Can also map with different permissions: say p 1 can read & write, p 2 can only read Trans(p 1, v a, user, {read, write}) = Trans(p 2, v b, user, {read}) In Pintos (and many OS) the kernel virtual address space is shared among all processes & mapped at the same address: Trans(p i, v a, kernel, *) = Trans(p k, v a, kernel, *) for all processes p i and p k and v a in [0xC0000000, 0xFFFFFFFF]

Per-Process Page Tables Can either keep track of all mappings in a single table, or can split information between tables one for each process mathematically: a projection onto a single process For each process pi define a function PTransi as PTransi (va, *, *) = Trans(pi, va, user, *) Implementation: associate representation of this function with PCB, e.g., per-process hash table Entries are called page table entries or PTEs Nice side-effect: reduced need for synchronization if every process only adds/removes entries to its own page table

Per-Process Page Tables (2) Common misconception User processes use user page table and kernel uses kernel page table as if those were two tables Not so (on x86): mode switch (interrupt, system call) does not change the page table that is used It only activates those entries that require kernel mode within the current process s page table Consequence: kernel code also cannot access user addresses that aren t mapped

Non-Resident Pages When implementing virtual memory, some of a process s pages may be swapped out Or may not yet have been faulted in Need to record that in page table: Trans (with paging): { Process Ids } { Virtual Addresses } { user, kernel } ({ read, write, execute }) { Physical Addresses } { INVALID } { Some Location On Disk }

Implementing Page Tables Many, many variations possible Done in combination of hardware & software Hardware part: dictated by architecture Software part: up to OS designer Machine-dependent layer that implements architectural constraints (what hardware expects) Machine-independent layer that manages page tables Must understand how TLB works first

Page Tables Function & TLB Trans (with paging): { Process Ids } { Virtual Addresses } { user, kernel } { read, write, execute } { Physical Addresses } { INVALID } { Some Location On Disk } For each combination (process id, virtual_addr, mode, type of access) must decide If access is permitted If permitted: if page is resident, use physical address if page is non-resident, page table has information on how to get the page in memory CPU uses TLB for actual translation page table feeds the TLB on a TLB miss

TLB

TLB: Translation Look-Aside Buffer Virtual-to-physical translation is part of every instruction (why not only load/store instructions?) Thus must execute at CPU pipeline speed TLB caches a number of translations in fast, fully-associative memory typical: 95% hit rate (locality of reference principle) Perm VPN PPN RWX K 0xC0000 RWX K 0xC0001 R-X K 0xC0002 R-- K 0xC0003 0x00000 0x00001 0x00002 0x00003 0xC0002345 VPN: Virtual Page Number TLB 0x00002345 Offset PPN: Physical Page Number

TLB Management Note: on previous slide example, TLB entries did not have a process id As is true for x86 Then: if process changes, some or all TLB entries may become invalid X86: flush entire TLB on process switch (refilling adds to cost!) Updates to TLB are immediately written to PTE (dirty bit, access bit) Some architectures store process id in TLB entry (MIPS) Flushing (some) entries only necessary when process id reused

Address Translation & TLB Virtual Address done in hardware restart instruction machine-dependent miss TLB Lookup hit done in OS software done in software or hardware Page Table Walk Check Permissions page present else denied ok TLB Reload machine-independent logic Page Fault Exception Page Not Present Load Page Page Fault Exception Protection Fault Terminate Process Physical Address

TLB Reloaded TLB small: typically only caches 64-2,048 entries What happens on a miss? must consult ( walk ) page table TLB Reload or Refill TLB Reload in software (MIPS) Via TLB miss handlers OS designer can pick any page table layout page table is only read & written by OS TLB Reload in hardware (x86) Hardware & software must agree on page table layout inasmuch as TLB miss handling is concerned page table is read by CPU, written by OS Some architectures allow either (PPC)

Page Tables vs TLB Consistency No matter which method is used, OS must ensure that TLB & page tables are consistent On multiprocessor, this may require TLB shootdown For software-reloaded TLB: relatively easy TLB will only contain what OS handlers place into it For hardware-reloaded TLB: two choices Use same data structures for page table walk & page loading (hardware designers reserved bits for OS s use in page table) Use a layer on top (facilitates machine-independent implementation) this is the recommended approach for Pintos Project 3 In this case, must update actual page table (on x86: page directory ) that is consulted by MMU during page table walk Code is already written for you in pagedir.c

Hardware/Software Split in Pintos Machine-independent Layer: your code & data structures supplemental page table Machine-dependent Layer: pagedir.c code CPU cr3

Layers in Address Translation adds and updates entries for resident pages refills TLB OS Data Structures Hardware Page Table updates PTE keeps track of all virtual pages for each process, resident or non-resident retrieves feedback about accesses (Does not exist if TLB reload is implemented in software) TLB Virtual address provides translations MMU updates access/dirty bits Physical address

Representing Page Tables Choice impacts speed of access vs size needed to store mapping information: Simple arrays (PDP-11, VAX) Fast, but required space makes it infeasible for large, non-continuous address spaces Search trees (aka hierarchical or multilevel page tables) Hash table

Two-level Page Table This is the original page design on i386 Next slide provides details on how address bits index tree levels If address space is sparse, interior nodes/paths do not exist (parent contains a NULL) Picture shown shows how tree divides virtual address space The kernel virtual pages that hold the page table information need not be contiguous Q.: how many pages are needed in Minimum case Worst case? (what is the worst case?)

Example: x86 Address Translation Two-level page table Source: [IA32-v3] 3.7.1

Example: x86 Page Table Entry Note: if bit 0 is 0 ( page not present ) MMU will ignore bits 1-31 OS can use those at will

Page Table Management on Linux Interesting history: Linux was originally x86 only with 32bit physical addresses. Its page table matched the one used by x86 hardware Since: Linux has been ported to other architectures x86 has grown to support 36bit physical addresses (PAE) required 3-level page table Linux s now uses 4-level page table to support 64-bit architectures

Linux Page Tables (2) On x86 hardware == software On 32-bit (no PAE) middle directory disappears With four-level, PUD page upper directory is added (not shown)

Inverted Page Tables Alternative to multilevel page tables size is O(physical memory)

Page Tables - Summary Page tables store mapping information from virtual to physical addresses, or to find non-resident pages Input is: process id, current mode (user/kernel) and kind of access (read/write/execute) TLBs cache such mappings Page tables are consulted when TLB miss occurs Either all software, or in hardware OS must maintain its page table(s) and, if hardware TLB reload is used, the page table (on x86 aka page directory + table ) that is consulted by MMU These two tables may or may not be one and the same The OS page table must have sufficient information to load a page s content from disk