Virtual Memory (2) 1

Similar documents
Changelog. Virtual Memory (2) exercise: 64-bit system. exercise: 64-bit system

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

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

CS 537: Introduction to Operating Systems Fall 2016: Midterm Exam #1. All cell phones must be turned off and put away.

CSE 120 Principles of Operating Systems

Virtual Memory 1. Virtual Memory

ENCM 369 Winter 2016 Lab 11 for the Week of April 4

Virtual Memory 1. Virtual Memory

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

CS 3330 Exam 3 Fall 2017 Computing ID:

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

CS 31: Intro to Systems Virtual Memory. Kevin Webb Swarthmore College November 15, 2018

Simple idea 1: load-time linking. Our main questions. Some terminology. Simple idea 2: base + bound register. Protection mechanics.

This Unit: Main Memory. Virtual Memory. Virtual Memory. Other Uses of Virtual Memory

CSE 153 Design of Operating Systems

How to create a process? What does process look like?

Address spaces and memory management

Virtual Memory. CS 351: Systems Programming Michael Saelee

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 23

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

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

CS 537: Introduction to Operating Systems Fall 2015: Midterm Exam #1

CSE 120 Principles of Operating Systems Spring 2017

Chapter 8 :: Topics. Chapter 8 :: Memory Systems. Introduction Memory System Performance Analysis Caches Virtual Memory Memory-Mapped I/O Summary

CS 153 Design of Operating Systems Winter 2016

Memory Hierarchy. Mehran Rezaei

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

Lecture 19: Virtual Memory: Concepts

exercise 4 byte blocks, 4 sets index valid tag value 00

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

CS 537: Introduction to Operating Systems Fall 2015: Midterm Exam #1

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

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 23

ECE 571 Advanced Microprocessor-Based Design Lecture 12

CSE 351. Virtual Memory

Virtual Memory. CS 3410 Computer System Organization & Programming

Virtual Memory I. CSE 351 Spring Instructor: Ruth Anderson

Chapter 8 & Chapter 9 Main Memory & Virtual Memory

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

Computer Systems II. Memory Management" Subdividing memory to accommodate many processes. A program is loaded in main memory to be executed

Chapter 3 - Memory Management

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

CS 318 Principles of Operating Systems

Digital Logic & Computer Design CS Professor Dan Moldovan Spring Copyright 2007 Elsevier 8-<1>

CS 153 Design of Operating Systems Winter 2016

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

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

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

Tutorial on Memory Management, Deadlock and Operating System Types

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

Computer Systems. Virtual Memory. Han, Hwansoo

CSE Computer Architecture I Fall 2011 Homework 07 Memory Hierarchies Assigned: November 8, 2011, Due: November 22, 2011, Total Points: 100

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

The cache is 4-way set associative, with 4-byte blocks, and 16 total lines

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 24

Sarah L. Harris and David Money Harris. Digital Design and Computer Architecture: ARM Edition Chapter 8 <1>

ECE 571 Advanced Microprocessor-Based Design Lecture 13

Virtual Memory. Kevin Webb Swarthmore College March 8, 2018

Virtual Memory: Systems

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

CS 153 Design of Operating Systems

COSC3330 Computer Architecture Lecture 20. Virtual Memory

Processes and Virtual Memory Concepts

Lecture 21: Virtual Memory. Spring 2018 Jason Tang

CSE 560 Computer Systems Architecture

EECS 482 Introduction to Operating Systems

CIS Operating Systems Memory Management Cache and Demand Paging. Professor Qiang Zeng Spring 2018

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

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

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

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

Opera&ng Systems ECE344

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

Virtual Memory Oct. 29, 2002

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

Memory Management - Demand Paging and Multi-level Page Tables

Virtual Memory: Concepts

Chapter 8 Memory Management

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

Topic 18: Virtual Memory

CIS Operating Systems Memory Management Cache. Professor Qiang Zeng Fall 2017

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

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

CS162 Operating Systems and Systems Programming Lecture 10 Caches and TLBs"

Chapter 8 Virtual Memory

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

Paging! 2/22! Anthony D. Joseph and Ion Stoica CS162 UCB Fall 2012! " (0xE0)" " " " (0x70)" " (0x50)"

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

HY225 Lecture 12: DRAM and Virtual Memory

COMP 273 Winter physical vs. virtual mem Mar. 15, 2012

Memory Hierarchy Requirements. Three Advantages of Virtual Memory

Systems Programming and Computer Architecture ( ) Timothy Roscoe

16 Sharing Main Memory Segmentation and Paging

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

Another View of the Memory Hierarchy. Lecture #25 Virtual Memory I Memory Hierarchy Requirements. Memory Hierarchy Requirements

ECE 598 Advanced Operating Systems Lecture 14


Virtualizing Memory: Paging. Announcements

15 Sharing Main Memory Segmentation and Paging

Transcription:

Virtual Memory (2) 1

Changelog 1 Changes made in this version not seen in first lecture: 21 November 2017: 1-level example: added final answer of memory value, not just location 21 November 2017: two-level example: answer was 0x0A not 0xBA 21 November 2017: do we really need a complete copy?: even size of memory regions between copies 21 November 2017: swapping components: add (if modified) as condition on when we swap out

exercise: 64-bit system 2 my desktop: 39-bit physical addresses; 48-bit virtual addresses 4096 byte pages

exercise: 64-bit system 2 my desktop: 39-bit physical addresses; 48-bit virtual addresses 4096 byte pages top 16 bits of address not used for translation

exercise: 64-bit system 2 my desktop: 39-bit physical addresses; 48-bit virtual addresses 4096 byte pages exercise: how many page table entries? exercise: how large are physical page numbers?

exercise: 64-bit system 2 my desktop: 39-bit physical addresses; 48-bit virtual addresses 4096 byte pages exercise: how many page table entries? 2 48 /2 12 = 2 36 entries exercise: how large are physical page numbers? 39 12 = 27 bits

exercise: 64-bit system 2 my desktop: 39-bit physical addresses; 48-bit virtual addresses 4096 byte pages exercise: how many page table entries? 2 48 /2 12 = 2 36 entries exercise: how large are physical page numbers? 39 12 = 27 bits page table entries are 8 bytes (room for expansion, metadata) would take up 2 39 bytes?? (512GB??)

3

exam and conflicts 4 final exam 7 December 2017 Gilmer 130 fill out conflict form (today, please?) if you can t make it linked off schedule also Collab announcement

rotate HW and cache blocking 5 many of you seemed to think you were doing just loop unrolling but generally changed the order of memory accesses in the process basically cache blocking // unroll + cache blocking for (int i =...; i += 2) for (int j =...) { f(i + 0, j); f(i + 1, j); } // unroll loop in i only // (probably not very helpful) for (int i =...; i += 2) { for (int j =...) f(i, j); for (int j =...) f(i + 1, j); }

page table tricks 6 page tables let operating systems do magic with memory key idea: OS doesn t need to crash on a fault! instead: change page tables and rerun memory access similar idea to trap-and-emulate

space on demand 7 Program Memory Used by OS Stack Heap / other dynamic Writable data Code + Constants

space on demand 7 Program Memory Used by OS Stack used stack space (12 KB) Heap / other dynamic Writable data Code + Constants wasted space? (huge??)

space on demand 7 Program Memory Used by OS Stack used stack space (12 KB) OS would like to allocate space only if needed Heap / other dynamic Writable data wasted space? (huge??) Code + Constants

allocating space on demand 8 %rsp = 0x7FFFC000... // requires more stack space A: pushq %rbx B: movq 8(%rcx), %rbx C: addq %rbx, %rax... VPN valid? physical page 0x7FFFB 0 --- 0x7FFFC 1 0x200DF 0x7FFFD 1 0x12340 0x7FFFE 1 0x12347 0x7FFFF 1 0x12345

allocating space on demand 8 %rsp = 0x7FFFC000... // requires more stack space A: pushq %rbx page fault! B: movq 8(%rcx), %rbx C: addq %rbx, %rax... VPN valid? physical page 0x7FFFB 0 --- 0x7FFFC 1 0x200DF 0x7FFFD 1 0x12340 0x7FFFE 1 0x12347 0x7FFFF 1 0x12345 pushq triggers exception hardware says accessing address 0x7FFFBFF8 OS looks up what s should be there stack

allocating space on demand 8 %rsp = 0x7FFFC000... // requires more stack space A: pushq %rbx B: movq 8(%rcx), %rbx C: addq %rbx, %rax... restarted VPN valid? physical page 0x7FFFB 1 0x200D8 0x7FFFC 1 0x200DF 0x7FFFD 1 0x12340 0x7FFFE 1 0x12347 0x7FFFF 1 0x12345 in exception handler, OS allocates more stack space OS updates the page table then returns to retry the instruction

allocating space on demand 9 note: the space doesn t have to be initially empty only change: load from file, etc. instead of allocating empty page loading program can be merely creating empty page table everything else can be handled in response to page faults no time/space spent loading/allocating unneeded space

page tricks generally 10 deliberately make program trigger page/protection fault but don t assume page/protection fault is an error have seperate data structures represent logically allocated memory e.g. addresses 0x7FFF8000 to 0x7FFFFFFFF are the stack might talk about Linux data structures later (book section 9.7) page table is for the hardware and not the OS

hardware help for page table tricks 11 information about the address causing the fault e.g. special register with memory address accessed harder alternative: OS disassembles instruction, look at registers (by default) rerun faulting instruction when returning from exception precise exceptions: no side effects from faulting instruction or after e.g. pushq that caused did not change %rsp before fault e.g. instructions reordered after faulting instruction not visible

swapping 12 our textbook presents virtual memory to support swapping using disk (or SSD, ) as the next level of the memory hierarchy OS allocates space on disk DRAM is a cache for disk

swapping versus caching 13 cache block physical page fully associative every virtual page can be stored in any physical page replacement is managed by the OS normal cache hits happen without OS common case that needs to be fast

swapping components 14 swap in a page exactly like allocating on demand! OS gets page fault invalid in page table check where page actually is (from virtual address) read from disk eventually restart process swap out a page OS marks as invalid in the page table(s) copy to disk (if modified)

HDD/SDDs are slow 15 HDD reads and writes: milliseconds to tens of milliseconds minimum size: 512 bytes writing tens of kilobytes basically as fast as writing 512 bytes SSD writes and writes: hundreds of microseconds designed for writes/reads of kilobytes (not much smaller)

HDD/SDDs are slow 15 HDD reads and writes: milliseconds to tens of milliseconds minimum size: 512 bytes writing tens of kilobytes basically as fast as writing 512 bytes SSD writes and writes: hundreds of microseconds designed for writes/reads of kilobytes (not much smaller)

HDD/SDDs are slow 15 HDD reads and writes: milliseconds to tens of milliseconds minimum size: 512 bytes writing tens of kilobytes basically as fast as writing 512 bytes SSD writes and writes: hundreds of microseconds designed for writes/reads of kilobytes (not much smaller)

swapping timeline program A pages program B pages page fault program A 16

swapping timeline program A pages OS needs to choose page to replace program B pages hopefully copy on disk is already up-to-date? loaded evicted page fault start read program A OS 16

swapping timeline program A pages first step of replacement: mark evicted page invalid in page table program B pages loaded evicted page fault start read program A OS 16

swapping timeline program A pagesother processes can run while reading pageprogram B pages OS will get interrupt when disk is done loaded evicted page fault start read interrupt program A OS 16

swapping timeline program A pages process A s page table updated and restarted from point of fault program B pages loaded evicted page fault start read interrupt program A OS 16

swapping decisions 17 write policy replacement policy

swapping decisions 18 write policy replacement policy

swapping is writeback 19 implementing write-through is hard when fault happens physical page not written when OS resumes process no chance to forward write HW itself doesn t know how to write to disk write-through would also be really slow HDD/SSD perform best if one writes at least a whole page at a time

implementing writeback 20 need a dirty bit per page ( was page modified ) often kept in the page table! option 1 (most common): hardware sets dirty bit in page table entry (on write) bit means physical page was modified using this PTE option 2: OS sets page read-only, flips read-only+dirty bit on fault

swapping decisions 21 write policy replacement policy

replacement policies really matter 22 huge cost for miss on swapping (milliseconds!) many millions of computations on modern processor much much worse than even L3 caches replacement policy implemented in software a lot more room for fancy policies

LRU replacement? 23 problem: need to identify when pages are used ideally every single time not practical to do this exactly HW would need to keep a list of when each page was accessed, or SW would need to force every access to trigger a fault

approximating LRU 24 one policy: not recently used OS periodically marks all pages as unreadable/writeable when page fault happens: make page accessible again put on list of used pages OS replaces pages not on used list tiebreaker: when was page last on used list?

hardware help for not-recently-used 25 hardware usually implements accessed bit in page table entry whenever page is read/written hardware sets this bit (if not set) makes it easier (faster?) for OS to maintain used list OS can periodically scan/clear accessed bit instead of marking pages invalid temporarily construct lists of used pages

accessed/dirty bits 26 information about how a page table entry was used indirectly about underlying physical page kept in page table entries themselves

accessed/dirty bits 26 information about how a page table entry was used indirectly about underlying physical page kept in page table entries themselves multiple page table entries refer to same page separate valid/accessed bits for each OS needs to consolidate them all

mmap 27 Linux/Unix has a function to map a file to memory int file = open("somefile.dat", O_RDWR); // data is region of memory that represents file char *data = mmap(..., file, 0); // read byte 6 from somefile.dat char seventh_char = data[6]; // modifies byte 100 of somefile.dat data[100] = 'x'; // can continue to use 'data' like an array

swapping almost mmap 28 access mapped file for first time, read from disk (like swapping when memory was swapped out) write mapped memory, write to disk eventually (like writeback policy in swapping) use dirty bit extra detail: other processes should see changes all accesses to file use same physical memory

fast copies 29 Unix mechanism for starting a new process: fork() creates a copy of an entire program! (usually, the copy then calls execve replaces itself with another program) how isn t this really slow?

do we really need a complete copy? 30 bash new copy of bash Used by OS Used by OS Stack Stack Heap / other dynamic Writable data Code + Constants Heap / other dynamic Writable data Code + Constants

do we really need a complete copy? 30 bash new copy of bash Used by OS Used by OS Stack Stack Heap / other dynamic Heap / other dynamic Writable data Writable data Code + Constants Code + Constants shared as read-only

do we really need a complete copy? 30 bash new copy of bash Used by OS Used by OS Stack Stack Heap / other dynamic Heap / other dynamic Writable data Writable data Code + Constants can t be shared? Code + Constants

trick for extra sharing 31 sharing writeable data is fine until either process modifies the copy can we detect modifications? trick: tell CPU (via page table) shared part is read-only processor will trigger a fault when it s written

copy-on-write and page tables 32 VPN valid? write? physical page 0x00601 1 1 0x12345 0x00602 1 1 0x12347 0x00603 1 1 0x12340 0x00604 1 1 0x200DF 0x00605 1 1 0x200AF

copy-on-write and page tables 32 VPN valid? write? physical page 0x00601 1 0 0x12345 0x00602 1 0 0x12347 0x00603 1 0 0x12340 0x00604 1 0 0x200DF 0x00605 1 0 0x200AF VPN valid? write? physical page 0x00601 1 0 0x12345 0x00602 1 0 0x12347 0x00603 1 0 0x12340 0x00604 1 0 0x200DF 0x00605 1 0 0x200AF copy operation actually duplicates page table both processes share all physical pages but marks pages in both copies as read-only

copy-on-write and page tables 32 VPN valid? write? physical page 0x00601 1 0 0x12345 0x00602 1 0 0x12347 0x00603 1 0 0x12340 0x00604 1 0 0x200DF 0x00605 1 0 0x200AF VPN valid? write? physical page 0x00601 1 0 0x12345 0x00602 1 0 0x12347 0x00603 1 0 0x12340 0x00604 1 0 0x200DF 0x00605 1 0 0x200AF when either process tries to write read-only page triggers a fault OS actually copies the page

copy-on-write and page tables 32 VPN valid? write? physical page 0x00601 1 0 0x12345 0x00602 1 0 0x12347 0x00603 1 0 0x12340 0x00604 1 0 0x200DF 0x00605 1 0 0x200AF VPN valid? write? physical page 0x00601 1 0 0x12345 0x00602 1 0 0x12347 0x00603 1 0 0x12340 0x00604 1 0 0x200DF 0x00605 1 1 0x300FD after allocating a copy, OS reruns the write instruction

exercise: 64-bit system 33 my desktop: 39-bit physical addresses; 48-bit virtual addresses 4096 byte pages exercise: how many page table entries? 2 48 /2 12 = 2 36 entries exercise: how large are physical page numbers? 39 12 = 27 bits page table entries are 8 bytes (room for expansion, metadata) would take up 2 39 bytes?? (512GB??)

huge page tables 34 huge virtual address spaces! impossible to store PTE for every page how can we save space?

holes 35 Used by OS Stack most pages are invalid Heap / other dynamic Writable data Code + Constants

saving space 36 basic idea: don t store (most) invalid page table entries use a data structure other than a flat array want a map lookup key (virtual page number), get value (PTE) options?

saving space 36 basic idea: don t store (most) invalid page table entries use a data structure other than a flat array want a map lookup key (virtual page number), get value (PTE) options? hashtable actually used by some historical processors but never common

saving space 36 basic idea: don t store (most) invalid page table entries use a data structure other than a flat array want a map lookup key (virtual page number), get value (PTE) options? hashtable actually used by some historical processors but never common tree data structure but not quite a search tree

search tree tradeoffs 37 lookup usually implemented in hardware lookup should be simple lookup should not involve many memory accesses doing two memory accesses is already very slow

two-level page tables two-level page table for 65536 pages (16-bit VPN) second-level page tables first-level page table for VPN 0x0-0xFF for VPN 0x100-0x1FF for VPN 0x200-0x2FF for VPN 0x300-0x300 for VPN 0xFF00-0xFFFF PTE for VPN 0x00 PTE for VPN 0x01 PTE for VPN 0x02 PTE for VPN 0x03 PTE for VPN 0xFF actual data for pa (if PTE valid) PTE for VPN 0x300 PTE for VPN 0x301 PTE for VPN 0x302 PTE for VPN 0x303 PTE for VPN 0x3FF 38

two-level page tables two-level page table for 65536 pages (16-bit VPN) second-level page tables first-level page table for VPN 0x0-0xFF for VPN 0x100-0x1FF for VPN 0x200-0x2FF for VPN 0x300-0x300 for VPN 0xFF00-0xFFFF PTE for VPN 0x00 PTE for VPN 0x01 PTE for VPN 0x02 PTE for VPN 0x03 PTE for VPN 0xFF invalid entries represent big holes PTE for VPN 0x300 PTE for VPN 0x301 PTE for VPN 0x302 PTE for VPN 0x303 PTE for VPN 0x3FF actual data for pa (if PTE valid) 38

two-level page tables two-level page table for 65536 pages (16-bit VPN) second-level first-level page page tables table actual data for pa PTE for VPN 0x00 physical page # VPN range PTE for valid VPN kernel 0x01 write (if PTE valid) (of next page table) first-level page0x0000-0x00ff table PTE for1 VPN 00x02 1 0x22343 0x0100-0x01FF PTE for0 VPN 0x03 for VPN 0x0-0xFF 0 1 0x00000 0x0200-0x02FF 0 0 0 0x00000 for VPN 0x100-0x1FF 0x0300-0x03FF PTE for 1 VPN 1 0xFF 0 0x33454 for VPN 0x200-0x2FF for VPN 0x300-0x300 0x0400-0x04FF PTE 1for VPN 1 0x300 0 0xFF043 PTE for VPN 0x301 for VPN 0xFF00-0xFFFF 0xFF00-0xFFFF PTE 1for VPN 1 0x302 0 0xFF045 PTE for VPN 0x303 PTE for VPN 0x3FF 38

two-level page tables two-level page table for 65536 pages (16-bit VPN) second-level first-level page page tables table actual data for pa PTE for VPN 0x00 physical page # VPN range PTE for valid VPN kernel 0x01 write (if PTE valid) (of next page table) first-level page0x0000-0x00ff table PTE for1 VPN 00x02 1 0x22343 0x0100-0x01FF PTE for0 VPN 0x03 for VPN 0x0-0xFF 0 1 0x00000 0x0200-0x02FF 0 0 0 0x00000 for VPN 0x100-0x1FF 0x0300-0x03FF PTE for 1 VPN 1 0xFF 0 0x33454 for VPN 0x200-0x2FF for VPN 0x300-0x300 0x0400-0x04FF PTE 1for VPN 1 0x300 0 0xFF043 PTE for VPN 0x301 for VPN 0xFF00-0xFFFF 0xFF00-0xFFFF PTE 1for VPN 1 0x302 0 0xFF045 PTE for VPN 0x303 PTE for VPN 0x3FF 38

two-level page tables two-level page table for 65536 pages (16-bit VPN) second-level first-level page page tables table actual data for pa PTE for VPN 0x00 physical page # VPN range PTE for valid VPN kernel 0x01 write (if PTE valid) (of next page table) first-level page0x0000-0x00ff table PTE for1 VPN 00x02 1 0x22343 0x0100-0x01FF PTE for0 VPN 0x03 for VPN 0x0-0xFF 0 1 0x00000 0x0200-0x02FF 0 0 0 0x00000 for VPN 0x100-0x1FF 0x0300-0x03FF PTE for 1 VPN 1 0xFF 0 0x33454 for VPN 0x200-0x2FF for VPN 0x300-0x300 0x0400-0x04FF PTE 1for VPN 1 0x300 0 0xFF043 PTE for VPN 0x301 for VPN 0xFF00-0xFFFF 0xFF00-0xFFFF PTE 1for VPN 1 0x302 0 0xFF045 PTE for VPN 0x303 PTE for VPN 0x3FF 38

two-level page tables two-level page table for 65536 pages (16-bit VPN) second-level a second-level page tablespage table actual data for pa PTE for VPN 0x00 physical page # VPN PTE for valid VPN kernel 0x01 write (if PTE valid) (of data) first-level page table 0x300 PTE for 1VPN 0x02 1 0 0x42443 0x301 PTE for 1VPN 0x03 for VPN 0x0-0xFF 1 0 0x4A9DE 0x302 1 1 0 0x5C001 for VPN 0x100-0x1FF 0x303 PTE for 0 VPN 0xFF 0 0 0x00000 for VPN 0x200-0x2FF for VPN 0x300-0x300 0x304 PTE for 1 VPN1 0x3000 0x6C223 PTE for VPN 0x301 for VPN 0xFF00-0xFFFF 0x3FF PTE for 0 VPN0 0x3020 0x00000 PTE for VPN 0x303 PTE for VPN 0x3FF 38

two-level page tables two-level page table for 65536 pages (16-bit VPN) second-level a second-level page tablespage table actual data for pa PTE for VPN 0x00 physical page # VPN PTE for valid VPN kernel 0x01 write (if PTE valid) (of data) first-level page table 0x300 PTE for 1VPN 0x02 1 0 0x42443 0x301 PTE for 1VPN 0x03 for VPN 0x0-0xFF 1 0 0x4A9DE 0x302 1 1 0 0x5C001 for VPN 0x100-0x1FF 0x303 PTE for 0 VPN 0xFF 0 0 0x00000 for VPN 0x200-0x2FF for VPN 0x300-0x300 0x304 PTE for 1 VPN1 0x3000 0x6C223 PTE for VPN 0x301 for VPN 0xFF00-0xFFFF 0x3FF PTE for 0 VPN0 0x3020 0x00000 PTE for VPN 0x303 PTE for VPN 0x3FF 38

two-level page tables two-level page table for 65536 pages (16-bit VPN) second-level page tables first-level page table for VPN 0x0-0xFF for VPN 0x100-0x1FF for VPN 0x200-0x2FF for VPN 0x300-0x300 for VPN 0xFF00-0xFFFF PTE for VPN 0x00 PTE for VPN 0x01 PTE for VPN 0x02 PTE for VPN 0x03 PTE for VPN 0xFF actual data for pa (if PTE valid) PTE for VPN 0x300 PTE for VPN 0x301 PTE for VPN 0x302 PTE for VPN 0x303 PTE for VPN 0x3FF 38

two-level page table lookup 39 virtual address 11 0101 01 00 1011 00 00 1101 1111 VPN split into two parts (one per level)

two-level page table lookup 39 page table base register 0x10000 virtual address 11 0101 01 00 1011 00 00 1101 1111 PTE size +

two-level page table lookup page table base register 0x10000 virtual address 11 0101 01 00 1011 00 00 1101 1111 cause fault? PTE size valid, etc? + 1st PTE addr. split PTE parts data or instruction cache physical address 39

two-level page table lookup page table base register 0x10000 virtual address 11 0101 01 00 1011 00 00 1101 1111 cause fault? PTE size valid, etc? PTE size + 1st PTE addr. split PTE parts page size + 2nd PTE addr. physical address data or instruction cache 39

two-level page table lookup page table base register 0x10000 virtual address 11 0101 01 00 1011 00 00 1101 1111 cause fault? cause fault? PTE size valid, etc? PTE size valid, etc? + 1st PTE addr. split PTE parts page size + 2nd PTE addr. split PTE parts 1101 0011 11 physical address data or instruction cache 39

two-level page table lookup page table base register 0x10000 virtual address 11 0101 01 00 1011 00 00 1101 1111 cause fault? cause fault? PTE size + 1st PTE addr. valid, etc? split PTE parts page size PTE size + 2nd PTE addr. data or instruction cache valid, etc? split PTE parts 1101 0011 11 00 1101 1111 physical address 39

two-level page table lookup page table base register 0x10000 virtual address 11 0101 01 00 1011 00 00 1101 1111 cause fault? cause fault? PTE size + 1st PTE addr. valid, etc? split PTE parts page size PTE size + 2nd PTE addr. data or instruction cache valid, etc? split PTE parts 1101 0011 11 00 1101 1111 physical address 39

two-level page table lookup page table base register 0x10000 virtual address 11 0101 01 00 1011 00 00 1101 1111 cause fault? cause fault? PTE size + 1st PTE addr. valid, etc? split PTE parts first-level page table lookup page size PTE size + 2nd PTE addr. data or instruction cache valid, etc? split PTE parts 1101 0011 11 00 1101 1111 physical address 39

two-level page table lookup page table base register 0x10000 virtual address 11 0101 01 00 1011 00 00 1101 1111 cause fault? cause fault? PTE size + 1st PTE addr. valid, etc? split PTE parts page size PTE size + 2nd PTE addr. data or instruction cache valid, etc? split PTE parts second-level page table lookup 1101 0011 11 00 1101 1111 physical address 39

two-level page table lookup page table base register 0x10000 virtual address 11 0101 01 00 1011 00 00 1101 1111 cause fault? cause fault? PTE size + 1st PTE addr. valid, etc? split PTE parts first-level page size PTE size + 2nd PTE addr. data or instruction cache valid, etc? split PTE parts second-level 1101 0011 11 00 1101 1111 physical address 39

two-level page table lookup page table base register 0x10000 virtual address 11 0101 01 00 1011 00 00 1101 1111 cause fault? cause fault? PTE size + 1st PTE addr. valid, etc? split PTE parts page size PTE size + 2nd PTE addr. data or instruction cache valid, etc? split PTE parts 1101 0011 11 00 1101 1111 physical address 39

two-level page table lookup page table base register 0x10000 virtual address 11 0101 01 00 1011 00 00 1101 1111 cause fault? cause fault? PTE size + 1st PTE addr. valid, etc? split PTE parts page size MMU PTE size + 2nd PTE addr. data or instruction cache valid, etc? split PTE parts 1101 0011 11 00 1101 1111 physical address 39

another view 40 VPN part 1 VPN part 2 page offset first-level page table page table entry page table entry second-level page table physical page page table base register

multi-level page tables 41 VPN split into pieces for each level of page table top levels: page table entries point to next page table usually using physical page number of next page table bottom level: page table entry points to destination page validity and permission checks at each level

note on VPN splitting 42 textbook labels it VPN 1 and VPN 2 and so on these are parts of the virtual page number (there are not multiple VPNs)

splitting addresses for levels 43 x86-32 32-bit physical address; 32-bit virtual address 2 12 byte page size 2-levels of page tables; each page table is one page 4 byte page table entries how is address 0x12345678 split up?

splitting addresses for levels 43 x86-32 32-bit physical address; 32-bit virtual address 2 12 byte page size 12-bit page offset 2-levels of page tables; each page table is one page 4 byte page table entries how is address 0x12345678 split up?

splitting addresses for levels 43 x86-32 32-bit physical address; 32-bit virtual address 2 12 byte page size 12-bit page offset 2-levels of page tables; each page table is one page 4 byte page table entries 2 12 /4 = 2 10 PTEs/page table; 10-bit VPN parts how is address 0x12345678 split up?

splitting addresses for levels x86-32 32-bit physical address; 32-bit virtual address 2 12 byte page size 12-bit page offset 2-levels of page tables; each page table is one page 4 byte page table entries 2 12 /4 = 2 10 PTEs/page table; 10-bit VPN parts how is address 0x12345678 split up? 10-bit VPN part 1: 0001 0010 00 (0x48); 10-bit VPN part 2: 11 0100 0101 (0x345); 12-bit page offset: 0x678 43

1-level example 6-bit virtual addresses, 6-bit physical; 8 byte pages, 1 byte PTE page tables 1 page; PTE: 3 bit PPN (MSB), 1 valid bit, 4 other bits; page table base register 0x20; translate virtual address 0x30 physical addresses bytes 0x00-3 00 11 22 33 0x04-7 44 55 66 77 0x08-B 88 99 AA BB 0x0C-F CC DD EE FF 0x10-3 1A 2A 3A 4A 0x14-7 1B 2B 3B 4B 0x18-B 1C 2C 3C 4C 0x1C-F 1C 2C 3C 4C physical addresses bytes 0x20-3 D0 D1 D2 D3 0x24-7 D4 D5 D6 D7 0x28-B 89 9A AB BC 0x2C-F CD DE EF F0 0x30-3 BA 0A BA 0A 0x34-7 CB 0B CB 0B 0x38-B DC 0C DC 0C 0x3C-F EC 0C EC 0C 44

1-level example 6-bit virtual addresses, 6-bit physical; 8 byte pages, 1 byte PTE page tables 1 page; PTE: 3 bit PPN (MSB), 1 valid bit, 4 other bits; page table base register 0x20; translate virtual address 0x30 physical addresses bytes 0x00-3 00 11 22 33 0x04-7 44 55 66 77 0x08-B 88 99 AA BB 0x0C-F CC DD EE FF 0x10-3 1A 2A 3A 4A 0x14-7 1B 2B 3B 4B 0x18-B 1C 2C 3C 4C 0x1C-F 1C 2C 3C 4C physical addresses bytes 0x20-3 D0 D1 D2 D3 0x24-7 D4 D5 D6 D7 0x28-B 89 9A AB BC 0x2C-F CD DE EF F0 0x30-3 BA 0A BA 0A 0x34-7 CB 0B CB 0B 0x38-B DC 0C DC 0C 0x3C-F EC 0C EC 0C 0x30 = 11 0000 PTE addr: 0x20 + 6 1 = 0x26 PTE value: 0xD6 = 1101 0110 PPN 110, valid 1 M[110 000] = M[0x30] 44

1-level example 6-bit virtual addresses, 6-bit physical; 8 byte pages, 1 byte PTE page tables 1 page; PTE: 3 bit PPN (MSB), 1 valid bit, 4 other bits; page table base register 0x20; translate virtual address 0x30 physical addresses bytes 0x00-3 00 11 22 33 0x04-7 44 55 66 77 0x08-B 88 99 AA BB 0x0C-F CC DD EE FF 0x10-3 1A 2A 3A 4A 0x14-7 1B 2B 3B 4B 0x18-B 1C 2C 3C 4C 0x1C-F 1C 2C 3C 4C physical addresses bytes 0x20-3 D0 D1 D2 D3 0x24-7 D4 D5 D6 D7 0x28-B 89 9A AB BC 0x2C-F CD DE EF F0 0x30-3 BA 0A BA 0A 0x34-7 CB 0B CB 0B 0x38-B DC 0C DC 0C 0x3C-F EC 0C EC 0C 0x30 = 11 0000 PTE addr: 0x20 + 6 1 = 0x26 PTE value: 0xD6 = 1101 0110 PPN 110, valid 1 M[110 000] = M[0x30] 44

1-level example 6-bit virtual addresses, 6-bit physical; 8 byte pages, 1 byte PTE page tables 1 page; PTE: 3 bit PPN (MSB), 1 valid bit, 4 other bits; page table base register 0x20; translate virtual address 0x30 physical addresses bytes 0x00-3 00 11 22 33 0x04-7 44 55 66 77 0x08-B 88 99 AA BB 0x0C-F CC DD EE FF 0x10-3 1A 2A 3A 4A 0x14-7 1B 2B 3B 4B 0x18-B 1C 2C 3C 4C 0x1C-F 1C 2C 3C 4C physical addresses bytes 0x20-3 D0 D1 D2 D3 0x24-7 D4 D5 D6 D7 0x28-B 89 9A AB BC 0x2C-F CD DE EF F0 0x30-3 BA 0A BA 0A 0x34-7 CB 0B CB 0B 0x38-B DC 0C DC 0C 0x3C-F EC 0C EC 0C 0x30 = 11 0000 PTE addr: 0x20 + 6 1 = 0x26 PTE value: 0xD6 = 1101 0110 PPN 110, valid 1 M[110 000] = M[0x30] 44

2-level example 9-bit virtual addresses, 6-bit physical; 8 byte pages, 1 byte PTE page tables 1 page; PTE: 3 bit PPN (MSB), 1 valid bit, 4 unused page table base register 0x20; translate virtual address 0x131 physical addresses bytes 0x00-3 00 11 22 33 0x04-7 44 55 66 77 0x08-B 88 99 AA BB 0x0C-F CC DD EE FF 0x10-3 1A 2A 3A 4A 0x14-7 1B 2B 3B 4B 0x18-B 1C 2C 3C 4C 0x1C-F 1C 2C 3C 4C physical addresses bytes 0x20-3 D0 D1 D2 D3 0x24-7 D4 D5 D6 D7 0x28-B 89 9A AB BC 0x2C-F CD DE EF F0 0x30-3 BA 0A BA 0A 0x34-7 DB 0B DB 0B 0x38-B EC 0C EC 0C 0x3C-F FC 0C FC 0C 45

2-level example 9-bit virtual addresses, 6-bit physical; 8 byte pages, 1 byte PTE page tables 1 page; PTE: 3 bit PPN (MSB), 1 valid bit, 4 unused page table base register 0x20; translate virtual address 0x131 physical addresses bytes 0x00-3 00 11 22 33 0x04-7 44 55 66 77 0x08-B 88 99 AA BB 0x0C-F CC DD EE FF 0x10-3 1A 2A 3A 4A 0x14-7 1B 2B 3B 4B 0x18-B 1C 2C 3C 4C 0x1C-F 1C 2C 3C 4C physical addresses bytes 0x20-3 D0 D1 D2 D3 0x24-7 D4 D5 D6 D7 0x28-B 89 9A AB BC 0x2C-F CD DE EF F0 0x30-3 BA 0A BA 0A 0x34-7 DB 0B DB 0B 0x38-B EC 0C EC 0C 0x3C-F FC 0C FC 0C 0x131 = 1 0011 0001 0x20 + 4 1 = 0x24 PTE 1 value: 0xD4 = 1101 0100 PPN 110, valid 1 45

2-level example 9-bit virtual addresses, 6-bit physical; 8 byte pages, 1 byte PTE page tables 1 page; PTE: 3 bit PPN (MSB), 1 valid bit, 4 unused page table base register 0x20; translate virtual address 0x131 physical addresses bytes 0x00-3 00 11 22 33 0x04-7 44 55 66 77 0x08-B 88 99 AA BB 0x0C-F CC DD EE FF 0x10-3 1A 2A 3A 4A 0x14-7 1B 2B 3B 4B 0x18-B 1C 2C 3C 4C 0x1C-F 1C 2C 3C 4C physical addresses bytes 0x20-3 D0 D1 D2 D3 0x24-7 D4 D5 D6 D7 0x28-B 89 9A AB BC 0x2C-F CD DE EF F0 0x30-3 BA 0A BA 0A 0x34-7 DB 0B DB 0B 0x38-B EC 0C EC 0C 0x3C-F FC 0C FC 0C 0x131 = 1 0011 0001 0x20 + 4 1 = 0x24 PTE 1 value: 0xD4 = 1101 0100 PPN 110, valid 1 PTE 2 addr: 110 000 + 110 = 0x36 PTE 2 value: 0xDB 45

2-level example 9-bit virtual addresses, 6-bit physical; 8 byte pages, 1 byte PTE page tables 1 page; PTE: 3 bit PPN (MSB), 1 valid bit, 4 unused page table base register 0x20; translate virtual address 0x131 physical addresses bytes 0x00-3 00 11 22 33 0x04-7 44 55 66 77 0x08-B 88 99 AA BB 0x0C-F CC DD EE FF 0x10-3 1A 2A 3A 4A 0x14-7 1B 2B 3B 4B 0x18-B 1C 2C 3C 4C 0x1C-F 1C 2C 3C 4C physical addresses bytes 0x20-3 D0 D1 D2 D3 0x24-7 D4 D5 D6 D7 0x28-B 89 9A AB BC 0x2C-F CD DE EF F0 0x30-3 BA 0A BA 0A 0x34-7 DB 0B DB 0B 0x38-B EC 0C EC 0C 0x3C-F FC 0C FC 0C 0x131 = 1 0011 0001 0x20 + 4 1 = 0x24 PTE 1 value: 0xD4 = 1101 0100 PPN 110, valid 1 PTE 2 addr: 110 000 + 110 = 0x36 PTE 2 value: 0xDB PPN 110; valid 1 M[110 001 (0x31)] = 0x0A 45

2-level example 9-bit virtual addresses, 6-bit physical; 8 byte pages, 1 byte PTE page tables 1 page; PTE: 3 bit PPN (MSB), 1 valid bit, 4 unused page table base register 0x20; translate virtual address 0x131 physical addresses bytes 0x00-3 00 11 22 33 0x04-7 44 55 66 77 0x08-B 88 99 AA BB 0x0C-F CC DD EE FF 0x10-3 1A 2A 3A 4A 0x14-7 1B 2B 3B 4B 0x18-B 1C 2C 3C 4C 0x1C-F 1C 2C 3C 4C physical addresses bytes 0x20-3 D0 D1 D2 D3 0x24-7 D4 D5 D6 D7 0x28-B 89 9A AB BC 0x2C-F CD DE EF F0 0x30-3 BA 0A BA 0A 0x34-7 DB 0B DB 0B 0x38-B EC 0C EC 0C 0x3C-F FC 0C FC 0C 0x131 = 1 0011 0001 0x20 + 4 1 = 0x24 PTE 1 value: 0xD4 = 1101 0100 PPN 110, valid 1 PTE 2 addr: 110 000 + 110 = 0x36 PTE 2 value: 0xDB PPN 110; valid 1 M[110 001 (0x31)] = 0x0A 45

2-level example 9-bit virtual addresses, 6-bit physical; 8 byte pages, 1 byte PTE page tables 1 page; PTE: 3 bit PPN (MSB), 1 valid bit, 4 unused page table base register 0x20; translate virtual address 0x131 physical addresses bytes 0x00-3 00 11 22 33 0x04-7 44 55 66 77 0x08-B 88 99 AA BB 0x0C-F CC DD EE FF 0x10-3 1A 2A 3A 4A 0x14-7 1B 2B 3B 4B 0x18-B 1C 2C 3C 4C 0x1C-F 1C 2C 3C 4C physical addresses bytes 0x20-3 D0 D1 D2 D3 0x24-7 D4 D5 D6 D7 0x28-B 89 9A AB BC 0x2C-F CD DE EF F0 0x30-3 BA 0A BA 0A 0x34-7 DB 0B DB 0B 0x38-B EC 0C EC 0C 0x3C-F FC 0C FC 0C 0x131 = 1 0011 0001 0x20 + 4 1 = 0x24 PTE 1 value: 0xD4 = 1101 0100 PPN 110, valid 1 PTE 2 addr: 110 000 + 110 = 0x36 PTE 2 value: 0xDB PPN 110; valid 1 M[110 001 (0x31)] = 0x0A 45

2-level exercise (1) 9-bit virtual addresses, 6-bit physical; 8 byte pages, 1 byte PTE page tables 1 page; PTE: 3 bit PPN (MSB), 1 valid bit, 4 unused; page table base register 0x08; translate virtual address 0x0FB physical addresses bytes 0x00-3 00 11 22 33 0x04-7 44 55 66 77 0x08-B 88 99 AA BB 0x0C-F CC DD EE FF 0x10-3 1A 2A 3A 4A 0x14-7 1B 2B 3B 4B 0x18-B 1C 2C 3C 4C 0x1C-F 1C 2C 3C 4C physical addresses bytes 0x20-3 D0 D1 D2 D3 0x24-7 D4 D5 D6 D7 0x28-B 89 9A AB BC 0x2C-F CD DE EF F0 0x30-3 BA 0A BA 0A 0x34-7 DB 0B DB 0B 0x38-B EC 0C EC 0C 0x3C-F FC 0C FC 0C 46

2-level exercise (1) 9-bit virtual addresses, 6-bit physical; 8 byte pages, 1 byte PTE page tables 1 page; PTE: 3 bit PPN (MSB), 1 valid bit, 4 unused; page table base register 0x08; translate virtual address 0x0FB physical addresses bytes 0x00-3 00 11 22 33 0x04-7 44 55 66 77 0x08-B 88 99 AA BB 0x0C-F CC DD EE FF 0x10-3 1A 2A 3A 4A 0x14-7 1B 2B 3B 4B 0x18-B 1C 2C 3C 4C 0x1C-F 1C 2C 3C 4C physical addresses bytes 0x20-3 D0 D1 D2 D3 0x24-7 D4 D5 D6 D7 0x28-B 89 9A AB BC 0x2C-F CD DE EF F0 0x30-3 BA 0A BA 0A 0x34-7 DB 0B DB 0B 0x38-B EC 0C EC 0C 0x3C-F FC 0C FC 0C 0x0F3 = 011 111 011 PTE 1: 0xBB at 0x0B PTE 1: PPN 101 (5) valid 1 PTE 2: 0xF0 at 0x2F PTE 2: PPN 111 (7) valid 1 111 011 = 0x3B 0x0C 46

2-level exercise (1) 9-bit virtual addresses, 6-bit physical; 8 byte pages, 1 byte PTE page tables 1 page; PTE: 3 bit PPN (MSB), 1 valid bit, 4 unused; page table base register 0x08; translate virtual address 0x0FB physical addresses bytes 0x00-3 00 11 22 33 0x04-7 44 55 66 77 0x08-B 88 99 AA BB 0x0C-F CC DD EE FF 0x10-3 1A 2A 3A 4A 0x14-7 1B 2B 3B 4B 0x18-B 1C 2C 3C 4C 0x1C-F 1C 2C 3C 4C physical addresses bytes 0x20-3 D0 D1 D2 D3 0x24-7 D4 D5 D6 D7 0x28-B 89 9A AB BC 0x2C-F CD DE EF F0 0x30-3 BA 0A BA 0A 0x34-7 DB 0B DB 0B 0x38-B EC 0C EC 0C 0x3C-F FC 0C FC 0C 0x0F3 = 011 111 011 PTE 1: 0xBB at 0x0B PTE 1: PPN 101 (5) valid 1 PTE 2: 0xF0 at 0x2F PTE 2: PPN 111 (7) valid 1 111 011 = 0x3B 0x0C 46

2-level exercise (1) 9-bit virtual addresses, 6-bit physical; 8 byte pages, 1 byte PTE page tables 1 page; PTE: 3 bit PPN (MSB), 1 valid bit, 4 unused; page table base register 0x08; translate virtual address 0x0FB physical addresses bytes 0x00-3 00 11 22 33 0x04-7 44 55 66 77 0x08-B 88 99 AA BB 0x0C-F CC DD EE FF 0x10-3 1A 2A 3A 4A 0x14-7 1B 2B 3B 4B 0x18-B 1C 2C 3C 4C 0x1C-F 1C 2C 3C 4C physical addresses bytes 0x20-3 D0 D1 D2 D3 0x24-7 D4 D5 D6 D7 0x28-B 89 9A AB BC 0x2C-F CD DE EF F0 0x30-3 BA 0A BA 0A 0x34-7 DB 0B DB 0B 0x38-B EC 0C EC 0C 0x3C-F FC 0C FC 0C 0x0F3 = 011 111 011 PTE 1: 0xBB at 0x0B PTE 1: PPN 101 (5) valid 1 PTE 2: 0xF0 at 0x2F PTE 2: PPN 111 (7) valid 1 111 011 = 0x3B 0x0C 46

2-level exercise (1) 9-bit virtual addresses, 6-bit physical; 8 byte pages, 1 byte PTE page tables 1 page; PTE: 3 bit PPN (MSB), 1 valid bit, 4 unused; page table base register 0x08; translate virtual address 0x0FB physical addresses bytes 0x00-3 00 11 22 33 0x04-7 44 55 66 77 0x08-B 88 99 AA BB 0x0C-F CC DD EE FF 0x10-3 1A 2A 3A 4A 0x14-7 1B 2B 3B 4B 0x18-B 1C 2C 3C 4C 0x1C-F 1C 2C 3C 4C physical addresses bytes 0x20-3 D0 D1 D2 D3 0x24-7 D4 D5 D6 D7 0x28-B 89 9A AB BC 0x2C-F CD DE EF F0 0x30-3 BA 0A BA 0A 0x34-7 DB 0B DB 0B 0x38-B EC 0C EC 0C 0x3C-F FC 0C FC 0C 0x0F3 = 011 111 011 PTE 1: 0xBB at 0x0B PTE 1: PPN 101 (5) valid 1 PTE 2: 0xF0 at 0x2F PTE 2: PPN 111 (7) valid 1 111 011 = 0x3B 0x0C 46