CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 24

Similar documents
CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 23

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 23

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

P6/Linux Memory System Nov 11, 2009"

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

Virtual Memory: Systems

Computer Systems. Virtual Memory. Han, Hwansoo

Lecture 21: Virtual Memory. Spring 2018 Jason Tang

Processes and Virtual Memory Concepts

Memory System Case Studies Oct. 13, 2008

Pentium/Linux Memory System March 17, 2005

Virtual Memory II CSE 351 Spring

virtual memory. March 23, Levels in Memory Hierarchy. DRAM vs. SRAM as a Cache. Page 1. Motivation #1: DRAM a Cache for Disk

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

Virtual Memory: Concepts

198:231 Intro to Computer Organization. 198:231 Introduction to Computer Organization Lecture 14

Systems Programming and Computer Architecture ( ) Timothy Roscoe

Virtual Memory II. CSE 351 Autumn Instructor: Justin Hsia

Virtual Memory Nov 9, 2009"

Foundations of Computer Systems

Virtual Memory. Samira Khan Apr 27, 2017

Virtual Memory II. CSE 351 Autumn Instructor: Justin Hsia

Virtual Memory Oct. 29, 2002

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

Lecture 19: Virtual Memory: Concepts

CSE 153 Design of Operating Systems

14 May 2012 Virtual Memory. Definition: A process is an instance of a running program

VM as a cache for disk

Carnegie Mellon. 16 th Lecture, Mar. 20, Instructors: Todd C. Mowry & Anthony Rowe

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 25

Motivations for Virtual Memory Virtual Memory Oct. 29, Why VM Works? Motivation #1: DRAM a Cache for Disk

CSE 351. Virtual Memory

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

P6 memory system P6/Linux Memory System October 31, Overview of P6 address translation. Review of abbreviations. Topics. Symbols: ...

CISC 360. Virtual Memory Dec. 4, 2008

Understanding the Design of Virtual Memory. Zhiqiang Lin

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

Intel P The course that gives CMU its Zip! P6/Linux Memory System November 1, P6 memory system. Review of abbreviations

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

Intel P6 (Bob Colwell s Chip, CMU Alumni) The course that gives CMU its Zip! Memory System Case Studies November 7, 2007.

Virtual Memory. Alan L. Cox Some slides adapted from CMU slides

This lecture. Virtual Memory. Virtual memory (VM) CS Instructor: Sanjeev Se(a

Virtual Memory: Concepts

virtual memory Page 1 CSE 361S Disk Disk

Virtual Memory: Systems

Random-Access Memory (RAM) Systemprogrammering 2007 Föreläsning 4 Virtual Memory. Locality. The CPU-Memory Gap. Topics

CS 153 Design of Operating Systems

Random-Access Memory (RAM) Systemprogrammering 2009 Föreläsning 4 Virtual Memory. Locality. The CPU-Memory Gap. Topics! The memory hierarchy

Virtual Memory: Concepts

Page Which had internal designation P5

Virtual Memory. Computer Systems Principles

Computer Architecture Lecture 13: Virtual Memory II

18-447: Computer Architecture Lecture 18: Virtual Memory III. Yoongu Kim Carnegie Mellon University Spring 2013, 3/1

Agenda. CS 61C: Great Ideas in Computer Architecture. Virtual Memory II. Goals of Virtual Memory. Memory Hierarchy Requirements

Virtual Memory. Physical Addressing. Problem 2: Capacity. Problem 1: Memory Management 11/20/15

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

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

Virtual Memory: From Address Translation to Demand Paging

Virtual memory Paging

Translation Buffers (TLB s)

Virtual to physical address translation

CSE 120 Principles of Operating Systems

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

John Wawrzynek & Nick Weaver

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.

University of Washington Virtual memory (VM)

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

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

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 27, FALL 2012

Address spaces and memory management

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

Address Translation. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Chapter 5. Large and Fast: Exploiting Memory Hierarchy. Part II Virtual Memory

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

Virtual Memory I. CSE 351 Spring Instructor: Ruth Anderson

Memory: Page Table Structure. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 26

Memory Allocation. Copyright : University of Illinois CS 241 Staff 1

Virtual Memory. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

CSE 560 Computer Systems Architecture

CSE 120 Principles of Operating Systems Spring 2017

CS 318 Principles of Operating Systems

CS162 Operating Systems and Systems Programming Lecture 14. Caching (Finished), Demand Paging

CS 261 Fall Mike Lam, Professor. Virtual Memory

CSE 141 Computer Architecture Spring Lectures 17 Virtual Memory. Announcements Office Hour

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

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

Memory Hierarchy. Goal: Fast, unlimited storage at a reasonable cost per bit.

HY225 Lecture 12: DRAM and Virtual Memory

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

VIRTUAL MEMORY II. Jo, Heeseung

Virtual Memory. Reading. Sections 5.4, 5.5, 5.6, 5.8, 5.10 (2) Lecture notes from MKP and S. Yalamanchili

Virtual Memory III /18-243: Introduc3on to Computer Systems 17 th Lecture, 23 March Instructors: Bill Nace and Gregory Kesden

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

Chapter 5: Memory Management

Operating Systems: Internals and Design. Chapter 8. Seventh Edition William Stallings

Virtual Memory. Yannis Smaragdakis, U. Athens

CS 153 Design of Operating Systems Winter 2016

Transcription:

CS24: INTRODUCTION TO COMPUTING SYSTEMS Spring 2018 Lecture 24

LAST TIME Extended virtual memory concept to be a cache of memory stored on disk DRAM becomes L4 cache of data stored on L5 disk Extend page table entries with more details Entries have a valid (x86-64 present ) flag specifying if the page is in memory, and if not, where it resides Also permission flags, e.g. read/write, supervisor Requires hardware and software support: CPU performs address translation in hardware to make it as fast as possible CPU raises page fault and general protection fault exceptions when it requires the kernel s intervention Operating system handles situations where pages must be moved into and out of memory 2

LAST TIME (2) Can now implement many useful features! Previously could: Isolate address spaces of different processes Perform fast context-switches by changing the page table that the MMU uses Share memory regions between processes, such as shared libraries, kernel code, working areas Now we can also: Memory-map disk files into virtual memory, to load programs, and to perform fast and easy IO Set permissions on memory pages to make some pages read-only, or inaccessible by user code 3

X86-64 VIRTUAL MEMORY SUPPORT Intel64 family processors provide hardware support for virtual memory Virtual and physical address spaces are 48 bits Can address up to 256TiB of memory (Eventually will be extended to 64-bit address space) Pages are 4KiB in size (2 12 = 4096) Pages are identified by topmost 36 bits in address (Processor can also use 2MiB pages) Intel64 family processors implement a four-level page table hierarchy 12 bits for offset within page Remaining 36 bits are divided among the four levels; 9 bits to index into each level 4

X86-64 VIRTUAL MEMORY SUPPORT (2) x86-64 page-table hierarchy level names: Level 1 = Page Global Directory Level 2 = Page Upper Directory Level 3 = Page Middle Directory Level 4 = Page Table Each level uses 9 bits of virtual address to index into the next level 2 9 = 512 entries per page Each page table entry is 64 bits 8 bytes 512 entries in page table node = 4096 bytes (one page) 5

X86-64 VIRTUAL MEMORY SUPPORT (3) 48-bit virtual address translation: Bottom 12 bits are virtual page offset directly brought over into physical page offset Remaining 36 bits of virtual address are broken into four 9-bit indexes Used to traverse the sparse page-table structure Virtual Address 47 39 38 30 29 21 20 12 11 0 Page Global Page Upper Page Middle Page Table Virtual Page Offset Page Directory Base Register Pg. Global Directory Pg. Upper Directory Pg. Middle Directory Page Table Physical Address 47 12 11 60 Physical Page Number Physical Page Offset

X86-64 VIRTUAL MEMORY SUPPORT (4) Each process has its own page directory Each process has its own virtual address space, isolated from all other processes Page directory also maps some kernel code and shared library code into the process address space Virtual Address 47 39 38 30 29 21 20 12 11 0 Page Global Page Upper Page Middle Page Table Virtual Page Offset Page Directory Base Register Pg. Global Directory Pg. Upper Directory Pg. Middle Directory Page Table Physical Address 47 12 11 70 Physical Page Number Physical Page Offset

X86-64 VIRTUAL MEMORY SUPPORT (5) Current page directory is specified by the Page Directory Base Register On x86-64, this is %cr3, or Control Register 3 Only the kernel can change this control register! Virtual Address 47 39 38 30 29 21 20 12 11 0 Page Global Page Upper Page Middle Page Table Virtual Page Offset Page Directory Base Register Pg. Global Directory Pg. Upper Directory Pg. Middle Directory Page Table Physical Address 47 12 11 80 Physical Page Number Physical Page Offset

PAGE DIRECTORY/TABLE ENTRIES x86-64 page directory / table entries are 64 bits 36 bits used to specify physical address of either a page table, or a virtual memory page Other bits contain additional details about the entry Bit 0 (least-significant bit) is the Present bit (i.e. the valid bit from last lecture) When 1, the referenced page is cached in memory When 0, the referenced page is not in memory (e.g. page is stored on disk) When Present = 0, all other bits are available for the kernel to use 63 1 Available for Operating System Specifies location on disk of where the page is stored 0 P=0 9

PAGE DIRECTORY/TABLE ENTRIES (2) When Present bit is 1, page directory and page table entries contain several bookkeeping values Page directory entry (all directory levels are roughly like this): 63 62 12 11 6 5 4 3 2 1 0 XD Page Directory/Table Base Address A PCD PWT U/S R/W P=1 Page table entry: 63 62 12 11 7 6 5 4 3 2 1 0 XD Page Base Address D A PCD PWT U/S R/W P=1 Very similar contents for both kinds of entries 10

PAGE DIRECTORY/TABLE ENTRIES (3) 63 62 12 11 7 6 5 4 3 2 1 0 XD Page Base Address D A PCD PWT U/S R/W P=1 Bits 1 and 2 specify access permissions R/W = 1 is read/write, R/W = 0 is read-only U/S = 1 is user access, U/S = 0 is kernel access only x86-64 introduces an execute permission (bit 63) XD ( execute disable ) = 1 disallows instruction fetches from the memory region Dramatically reduces potential for bufferoverflow exploits! Set stack and data pages to not be executable Set code pages to be executable and read-only 11

PAGE DIRECTORY/TABLE ENTRIES (4) 63 62 12 11 7 6 5 4 3 2 1 0 XD Page Base Address D A PCD PWT U/S R/W P=1 Bits 3 and 4 specify caching policies for the page PWT specifies write-through or write-back PCD specifies whether cache is enabled or disabled Some peripherals are mapped directly into the computer s memory address space Technique is called memory-mapped I/O CPU interacts with the peripheral by reading and writing specific memory locations Memory addresses read/write directly to the I/O device These addresses are called I/O ports Definitely don t want to cache the memory page in these cases! 12

PAGE DIRECTORY/TABLE ENTRIES (5) 63 62 12 11 7 6 5 4 3 2 1 0 XD Page Base Address D A PCD PWT U/S R/W P=1 Bit 5 is the Accessed bit MMU sets this to 1 when the page is read or written The kernel is responsible for clearing this bit! Accessed bit used to track what pages have been used Helps kernel decide which page to evict when it needs to free up space in physical memory Bit 6 is the Dirty bit Only in page table entries, not in page directory entries! MMU sets this to 1 when the page is written to Dirty bit allows kernel to know when a victim page must be written back to the disk before it is evicted Kernel is responsible for handling and clearing this bit 13

PAGE DIRECTORY/TABLE ENTRIES (6) 63 62 12 11 7 6 5 4 3 2 1 0 XD Page Base Address D A PCD PWT U/S R/W P=1 Processor causes faults in certain situations If Present = 0 when a page is accessed, the CPU raises a page-fault exception Kernel page-fault handler can load the page into memory if it s on disk Or, if the page is unallocated, generate an error If R/W or U/S or XD bits prohibit an access, the CPU raises a general protection fault Kernel general protection fault handler can respond in various ways, but typically process is terminated 14

X86-64 ADDRESS TRANSLATION AND TLBS Page directory and page tables are stored in DRAM main memory Worst case: 50-100ns access penalty If needed block is in L1 cache, 1-3 clock hit-time CPU includes a Translation Lookaside Buffer (TLB) to eliminate even this lookup penalty A hardware cache with same design as SRAM caches x86-64 family processors: TLB is a set-associative cache; architecture varies Input to TLB cache is the virtual page number Each cache line holds a details for a page table entry, including the physical page number and permissions 15

X86-64 ADDRESS TRANSLATION, TLBS (2) Address translation logic works something like this: CPU Virtual Address VPN 36b 12b VPO TLB Tag TLBI Some bits from VPN are used to identify the cache-set index PGDE PUDE PMDE PTE TLB Hit TLB Miss 9b VPN1 9b VPN2 Partial TLB Hit 9b 9b VPN3 VPN4 Cache can also match parts of tag, to allow for partial TLB hits PPN 36b 12b PPO Physical Address PGDE PUDE PMDE PTE 16 PDBR (cr3) Page Global Directory Page Upper Directory Page Middle Directory Page Table

X86-64 ADDRESS TRANSLATION, TLBS (3) In case of a TLB miss: Virtual page number is broken into indexes into the page directories and the page table Incurs full lookup penalty, but the TLB cache is also updated with the results of the lookup CPU Virtual Address VPN 36b 12b VPO TLB Tag TLBI PGDE PUDE PMDE PTE TLB Hit Partial TLB Hit TLB Miss 9b VPN1 PGDE 9b VPN2 PUDE 9b VPN3 PMDE 9b VPN4 PTE PPN 36b 12b PPO Physical Address 33 17 PDBR (cr3)

X86-64 ADDRESS TRANSLATION, TLBS (4) Ideally, we want a TLB hit: Virtual page number is broken into a tag and a cache-set index (TLBI), as usual If TLB cache line contains page table entry (PTE), use this for the physical page number (PPN) CPU Virtual Address VPN 36b 12b VPO TLB Tag TLBI PGDE PUDE PMDE PTE TLB Hit Partial TLB Hit TLB Miss 9b VPN1 PGDE 9b VPN2 PUDE 9b VPN3 PMDE 9b VPN4 PTE PPN 36b 12b PPO Physical Address 33 18 PDBR (cr3)

X86-64 ADDRESS TRANSLATION, TLBS (5) Sometimes, we get a partial TLB hit Some part of the page-directory sequence is present in the TLB, but not the page table entry itself Use available info to shorten page-table traversal, and cache result back into TLB CPU Virtual Address VPN 36b 12b VPO TLB Tag TLBI PGDE PUDE PMDE PTE TLB Hit Partial TLB Hit TLB Miss 9b VPN1 PGDE 9b VPN2 PUDE 9b VPN3 PMDE 9b VPN4 PTE PPN 36b 12b PPO Physical Address 33 19 PDBR (cr3)

KERNEL AND VIRTUAL MEMORY SYSTEM The kernel plays an important role in the virtual memory system Manages the page directories and page tables of running processes Handles page faults and general protection faults Each process has its own virtual address space Each process has its own page directory that specifies the process virtual memory layout On x86-64, only the kernel can change the current page directory being used Requires level 0 (highest) privilege Page tables are also only updatable by the kernel 20

PROCESS MEMORY LAYOUT Each process has its own virtual address space Part of virtual address space is devoted to kernel Region starting at address 0x800000000000 (2 47 ) This memory only accessible by the kernel Includes functionality and data structures necessary for all processes Simply map these physical pages into every process virtual address space Different for each process Identical for each process 0x800000000000 %rsp 0x???????? brk 0x400000 0 Process-specific data structures Kernel stack Mapping to physical memory Kernel code and global data User stack Memory mapped region for shared libraries Run-time heap (via malloc) Uninitialized data (.bss) Initialized data (.data) Program text (.text) Forbidden Kernel virtual memory Process virtual memory 21

THE KERNEL AND SYSTEM CALLS Kernel code is mapped into each process address space Easy to make system calls! Can call kernel code via syscall trap Allows a change directly to privilege level 0 syscall instruction uses values in special control registers to jump to kernel The kernel sets these control registers during OS initialization Requires that the kernel has some code mapped into user process address space Different for each process Identical for each process 0x800000000000 %rsp 0x???????? brk 0x400000 0 Process-specific data structures Kernel stack Mapping to physical memory Kernel code and global data User stack Memory mapped region for shared libraries Run-time heap (via malloc) Uninitialized data (.bss) Initialized data (.data) Program text (.text) Forbidden Kernel virtual memory Process virtual memory 22

PROCESS-SPECIFIC KERNEL DATA Each process also includes process-specific structures, managed by the kernel Also accessible only by kernel Kernel stack: Every protection level has its own stack When a process makes system calls, the kernel-stack used is also only within that process address space Several other data structures are also managed per-process e.g. page directory/tables for the process, memory mapping info Different for each process Identical for each process 0x800000000000 %rsp 0x???????? brk 0x400000 0 Process-specific data structures Kernel stack Mapping to physical memory Kernel code and global data User stack Memory mapped region for shared libraries Run-time heap (via malloc) Uninitialized data (.bss) Initialized data (.data) Program text (.text) Forbidden Kernel virtual memory Process virtual memory 23

PROCESS VIRTUAL MEMORY AREAS Kernel manages several data structures to track virtual memory regions within each process Regions are called areas or segments (Not related to old 8086 segmented memory model!) task_struct mm mm_struct pgd mmap vm_area_struct Process Virtual Memory Shared Libraries mm_struct characterizes current state of process virtual memory pgd is the page directory for the process mmap is a list of memory areas within the process Data (.data,.bss) Run-time heap Program text (.text)

PROCESS VIRTUAL MEMORY AREAS (2) vm_area_struct fields specify details of each memory area, specify extent of the memory area specifies read/write permissions for the memory area specifies whether memory area is shared among processes, or private to this process Normal memory accesses: (Page is in memory, and the operation is allowed) No intervention needed from the kernel CPU and MMU handle these accesses without any trouble vm_area_struct Process Virtual Memory Shared Libraries Data (.data,.bss) Run-time heap Program text (.text)

FAULTS! When a page fault or general protection fault occurs, the kernel must handle the situation! Faults can occur for many different reasons vm_area_struct Invalid accesses: Program tried to write read-only memory Program tried to access kernel-only memory Valid accesses: Accessed page is in the swap device, not DRAM Accessed page hasn t been allocated to program Kernel must decide how to handle each fault Process Virtual Memory Shared Libraries Data (.data,.bss) Run-time heap Program text (.text) access? access? access? write? 26

FAULTS! (2) The process vm_area_struct-list tells the kernel how each fault should be handled If the program accesses vm_area_struct a non-existent page: MMU raises a page fault Kernel must check all area structs to see if the address itself is valid Does it fall within some and? If not a valid address, a segmentation-fault signal is sent to the process Default handler: terminate the process! Process Virtual Memory Shared Libraries Data (.data,.bss) Run-time heap Program text (.text) access 27

FAULTS! (3) If kernel determines that the address is valid, it must next check if the operation is valid Is the process writing to read-only memory? Is the process accessing kernel-only memory? MMU raises a general protection fault If operation is invalid, a segmentation-fault signal is delivered Again, the process gets terminated. vm_area_struct Process Virtual Memory Shared Libraries Data (.data,.bss) Run-time heap Program text (.text) write 28

FAULTS! (4) At this point, the kernel knows that the address is valid, and the operation is allowed Perform normal page-load vm_area_struct operations: Select victim page to evict If victim page is dirty, write it back to disk Load requested virtual page into memory Return from fault handler CPU restarts instruction that caused the fault This time, the instruction succeeds, since page is now in main memory Process Virtual Memory Shared Libraries Data (.data,.bss) Run-time heap Program text (.text) access 29

NEXT TIME We have covered most of how the kernel can provide a useful virtual memory abstraction Left out a few higher-level abstractions that operating systems build on top of virtual memory See Lecture 25 material on Moodle for details Next time: Final Exam Review (on Wednesday) No class on Friday 30