MEMORY MANAGEMENT UNITS

Similar documents
Caches. Cache Memory. memory hierarchy. CPU memory request presented to first-level cache first

VIRTUAL MEMORY II. Jo, Heeseung

Address Translation. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Virtual Memory. CS 351: Systems Programming Michael Saelee

Virtual Memory Virtual memory first used to relive programmers from the burden of managing overlays.

Chapter 8 Memory Management

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

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

ADDRESS TRANSLATION AND TLB

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

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, SPRING 2013

1. Creates the illusion of an address space much larger than the physical memory

Computer Structure. X86 Virtual Memory and TLB

CS 153 Design of Operating Systems Winter 2016

Translation Buffers (TLB s)

Virtual Memory. Motivation:

ADDRESS TRANSLATION AND TLB

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

Virtual Memory: From Address Translation to Demand Paging

Virtual Memory: From Address Translation to Demand Paging

Modern Virtual Memory Systems. Modern Virtual Memory Systems

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

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

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

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

Virtual Memory II CSE 351 Spring

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 23

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

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

CS370 Operating Systems

Processes and Virtual Memory Concepts

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

CPS 104 Computer Organization and Programming Lecture 20: Virtual Memory

Virtual Memory. Virtual Memory

CS 318 Principles of Operating Systems

CS399 New Beginnings. Jonathan Walpole

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

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

Chapter 8. Virtual Memory

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

Virtual to physical address translation

CSE 120 Principles of Operating Systems

A Typical Memory Hierarchy

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

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

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

Lecture 8 Memory Management Strategies (chapter 8)

CISC 7310X. C08: Virtual Memory. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 3/22/2018 CUNY Brooklyn College

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

CMSC 313 Lecture 13 Project 4 Questions Reminder: Midterm Exam next Thursday 10/16 Virtual Memory

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

1. Memory technology & Hierarchy

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

Lecture 21: Virtual Memory. Spring 2018 Jason Tang

Handout 4 Memory Hierarchy

virtual memory Page 1 CSE 361S Disk Disk

CS 3733 Operating Systems:

Virtual memory Paging

Chapter 5B. Large and Fast: Exploiting Memory Hierarchy

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

Paging. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Chapter 8: Memory-Management Strategies

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

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

Computer Science 146. Computer Architecture

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

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

Chapter 8 Virtual Memory

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

CPS104 Computer Organization and Programming Lecture 16: Virtual Memory. Robert Wagner

Virtual Memory Nov 9, 2009"

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

Computer Systems. Virtual Memory. Han, Hwansoo

Basic Memory Management

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 23

A Typical Memory Hierarchy

CSE 153 Design of Operating Systems

Virtual Memory Oct. 29, 2002

Processes and Tasks What comprises the state of a running program (a process or task)?

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

CS252 S05. Main memory management. Memory hardware. The scale of things. Memory hardware (cont.) Bottleneck

John Wawrzynek & Nick Weaver

The Virtual Memory Abstraction. Memory Management. Address spaces: Physical and Virtual. Address Translation

CHAPTER 8 - MEMORY MANAGEMENT STRATEGIES

CS 550 Operating Systems Spring Memory Management: Paging

Operating Systems. 09. Memory Management Part 1. Paul Krzyzanowski. Rutgers University. Spring 2015

Chapter 3 - Memory Management

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

CS370 Operating Systems

Operating Systems. IV. Memory Management

Systems Programming and Computer Architecture ( ) Timothy Roscoe

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

Module 2: Virtual Memory and Caches Lecture 3: Virtual Memory and Caches. The Lecture Contains:

Virtual Memory. Kevin Webb Swarthmore College March 8, 2018

DAT (cont d) Assume a page size of 256 bytes. physical addresses. Note: Virtual address (page #) is not stored, but is used as an index into the table

CS162 Operating Systems and Systems Programming Lecture 14. Caching and Demand Paging

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

Page 1. Review: Address Segmentation " Review: Address Segmentation " Review: Address Segmentation "

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

Transcription:

Memory Management Units memory management unit (MMU) simply converts a virtual address generated by a CPU into a physical address which is applied to the memory system address space divided into fixed sized pages [eg. 4Kbytes] low order address bits [offset within a page] not effected by MMU operation virtual page # converted into a physical page # CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 1

Memory Management Units MMUs integrated on-chip with the CPU each CPU core will typically have separate MMUs for instruction and data accesses examples as per IA32 2 32 byte [4GByte] address space divided into 2 20 [1,048,576] 2 12 [4K] byte pages virtual and physical address spaces need NOT be the same size which would you prefer? virtual > physical OR physical > virtual CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 2

Mapping Virtual Address Spaces onto Physical Memory [IA32] each process runs in own 4GB virtual address space pages in each virtual address space mapped by MMU onto real physical pages in memory pages allocated and mapped on demand by Operating System (OS) virtual pages [in a process] may be not allocated/mapped [probably because process hasn t accessed virtual page yet] allocated in physical memory allocated on paging disk typical Windows 7 process memory usage Word 43MB, IE 15MB, Firefox 27MB, small fraction of 4GB virtual address space CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 3

Mapping Virtual Address Spaces onto Physical Memory Atlas Computer 1962 [Manchester University] first to support virtual memory 48bit CPU, 24bit virtual and physical address spaces, 96KB RAM, 576KB drum [disk] OS normally attempts to keep the "working set" of a process in physical memory to minimise the page-fault rate [thrashing] every page used in a process' virtual address space requires an equivalent page either in physical memory or on the paging disk 4GB [total] of physical memory and paging disk space needed for a program which uses/accesses all 4GB of its virtual address space [e.g. large array] can view physical memory as acting as a cache to the paging disk! CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 4

Memory Cruncher consider the following program outline #define GB (1024*1024*1024) char *p = malloc(4*gb); // just moves internal OS pointer for (size_t i = 0; i < 4*GB; i += PAGESIZE, p += PAGESIZE) *p = 0; // access causes physical memory to be allocated a more complete version of Memory Cruncher.cpp is on the CS3021/3421 website designed to run as a Win32 [32 bit] or x64 [64 bit] process size_t is the size of an address [Win32 32 bits, x64 64 bits] Windows PAGESIZE is 4K CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 5

Memory Cruncher what is the largest contiguous memory block that can be allocated? Windows 7 Win32 4GB virtual address space, bottom 2GB for user and top 2GB for OS can malloc() a 1535MB contiguous memory block right click on project name [Properties][Linker][System][EnableLargeAddresses] can now malloc() a 2047MB contiguous memory block Windows 7 x64 program reports it can allocate a contiguous memory block of 8191GB or 8TB [2 43 ] mallocing a block much greater than size of physical memory [16GB] results in PC becoming extremely unresponsive [had to reboot by turning off power] RUN with caution CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 6

Memory Cruncher CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 7

Generic MMU Operation [IA32, x64, MIPS, ] virtual page # converted to a physical page # by table look-up virtual page # used as an index into a page table stored in physical memory. page table per process [and sometimes one for OS] page table base register PTB [CR3 in IA32] contains the physical address of the page table of the currently running process 4MB physical memory [1,048,576 x 4] needed for page table of every process IMPRACTICAL CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 8

N-level Page Table in order to reduce the size of the page table structure that needs to be allocated to a process, a n-level look-up table is used a n-level page table means that the "larger" the process [in terms of its use of its virtual address space], the more memory is needed for its page tables consider a 2-level scheme index1 is used to index into a primary page table, index2 into a secondary page table and so on CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 9

1024 1024 MEMORY MANAGEMENT UNITS N-Level Page Table PTB points to primary page table a valid primary page table entry points to a secondary page table PTB index1 primary page table 4KB pointer to... V NB: page tables are page aligned each process has one primary page table + multiple secondary page tables AS MANY AS NEEDED secondary page table 4KB secondary page tables created on demand [depends on how much of its virtual address space the process uses] ONE ONLY index2 physical page # V NB: size of page tables is 4KB - the page size itself offset physical address CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 10

Generic MMU Operation when MMU accesses a page table entry it checks the Valid bit if V ==0 and accessing a primary page table entry then NO physical memory allocated for corresponding secondary page table if V == 0 and accessing a secondary page table entry then NO physical memory allocated for referenced page [i.e. virtual address NOT mapped to physical memory] in both cases a "page fault" occurs, the instruction is aborted and the MMU interrupts the CPU CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 11

Page fault handling OS must resolve page fault by performing one OR more of the following actions: allocating a page of physical memory for use as a secondary page table [from an OS maintained list of free memory pages] allocating a page of physical memory for the referenced page updating the associated page table entry/entries reading code or initialised data from disk to initialise the page contents [context switches to another process while waiting] signalling an access violation [eg. writing to a read-only code page] restarting [or continuing] the faulting instruction CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 12

Process Page Table Structure example small process needs 3 code pages [12K], 1 data page [4K] and 2 stack pages [8K] code and data pages start at virtual address 0 with the stack at top of virtual address space require 2 secondary page tables to map code and stack areas [as at opposite ends of the virtual address space] 4GB unix/windows process 0 code code code data stack stack virtual address space PTB primary page table 1022 INVALID entries secondary page table code code code data 1020 INVALID entries secondary page table 1022 INVALID entries stack stack memory page memory page memory page memory page memory page memory page code and data stack a secondary page table can map 1024 4K pages = 4MB need ONLY 2 secondary page tables providing program doesn't use more than 4MB of code/data and 4MB of stack space CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 13

Translation Look Aside Buffer [TLB] without an internal TLB, each virtual to physical address translation requires 1 memory access for each level of page table [2 accesses for a 2 level scheme] MMU contains an m-entry onchip translation cache [TLB] which provides direct mappings for the m most recently accessed virtual pages CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 14

Translation Look Aside Buffer [TLB] when a virtual address is sent to the TLB, the virtual page # is compared with ALL m tag entries in the TLB in parallel [a fully associative cache] if a match is found [TLB hit], the corresponding cached secondary page table entry is output by the TLB/MMU to provide the physical address the address translation is completed "instantaneously" if a match is NOT found [TLB miss], page tables walked by CPU/MMU IA32/x64 page tables walked by a hardware state machine hardwired into CPU/MMU the "least recently used" [LRU] TLB entry is replaced with the new mapping how can the hardware find the LRU entry SIMPLY and QUICKLY? CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 15

RISC TLB Miss Handling REMEMBER that the page tables are just data structures held in main memory and can be walked by a CPU using ordinary instructions this is the approach taken by many RISCs, a TLB miss generates an interrupt and the CPU walks the page table using ordinary instructions [TLB miss page fault] in such cases the organisation of page table structure is more flexible since it can be set by software and is NOT hard-wired into CPU/MMU [eg. could implement a hash table] need a CPU instruction to replace the LRU TLB entry TLBs are normally small a typical 64 entry fully associative TLB has a hit rate > 90% a CPU would typically have a MMU for instruction accesses and a MMU for data accesses [needed for parallel accesses to the instruction and data caches] CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 16

TLB Coherency OS implications what happens on a process switch? TLB looked up by virtual address ALL processes use the same virtual addresses e.g. process 0 virtual address 0x1000 is NOT mapped to the same physical memory location as process N virtual address 0x1000 unless the page really is shared ALL TLB entries referring to the old process must be invalidated on a context switch otherwise the new process will access the memory pages of the old process normally the OS [if it runs in its own virtual address space] and one user process can share the entries in the TLB user/supervisor bit appended to TLB tag [see diagram slide 14] CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 17

TLB Coherency OS implications whenever the page table base register [eg. PTB0 for OS or PTB1 for the user process] is changed ALL corresponding TLB entries are invalidated PTB1 changed every time there is a context switch between processes PTB0 unlikely to change if a page table entry is changed in main memory [when handling a page fault], the OS must make sure that this change is reflected in the TLB must be able to invalidate old PTEs in the TLB by executing an instruction CPUs have an instruction to do this [eg. IA32 "INVAL va" will invalidate PTE entry corresponding to the eprocesses' virtual address va if present in TLB] also need to keep TLBs in a multicore CPU coherent CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 18

Multiple Processes sharing TLB possible for processes to share TLB if a process ID is appended to the virtual page # as part of the TLB tag extension of user/supervisor bit as part of tag need to handle PID reuse as number of bits used for PID limited [e.g. 8 bits] CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 19

Referenced and Modified Bits CPU/MMU automatically updates the PTE Referenced and Modified bits [IA32/x64 Accessed and Dirty bits] in the PTEs PTE changes "written through" to corresponding PTE in physical memory IA32/x64 CPU/MMU automatically executes these bus cycles IA32/x64 CPU/MMU never clears the reference and modified bits up to the OS [eg. a background process regularly clearing the referenced bits?] OS can use the Referenced and Modified bits to determine which pages are good candidates for being paged out [ones that have not been referenced for a while] whether pages have to be written to the paging disk [may be unchanged since last saved] CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 20

Support for Different Page Sizes often useful if MMU supports a number of different page sizes one reason is that a TLB typically contains very few entries [32 or 64] large pages allows a single TLB entry map a large virtual page onto similar sized area of contiguous physical memory OS could be loaded into a contiguous area of physical memory which could then be mapped using a single TLB entry similarly for a memory mapped graphics buffer IA32 solution first level PTE points to a 4MB page of physical memory [not a 2 nd level page table] bit set in primary PTE to indicate that it points to a large page [not a 2 nd level page table] CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 21

IA32 Support for Large Pages corresponding TLB entry maps 4MB virtual page to a 4MB page of physical memory 4MB page aligned on a 4MB boundary in virtual and physical address spaces TLB operation needs to be modified to accommodate these large 4MB TLB entries CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 22

Breakpoints Registers the MMU typically supports a number of breakpoint address registers and breakpoint control registers the MMU can generate an interrupt if the breakpoint address [virtual or physical] is read or written [watchpoint] or executed [breakpoint] debugger normal sets breakpoints and watchpoints using virtual addresses used to implement real-time debugger breakpoints and watchpoints hardware support needed to set breakpoints in ROM and for watchpoints MMU breakpoint registers are part of the process state save/restored as part of the context switch hence more than one processes can be debugged at the same time used by Linux ptrace system call CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 23

Integrating MMU and Operating System page table entries normally have a number of bits set aside for use by the OS implementer [i.e. not altered by hardware] IA32 PTEs have 3 such bits use spare bits to store OS specific PTE types consider the OS specific PTE types used in a hypothetical Unix implementation [closely modelled on GENIX for the NS32000 microprocessor which was the first demand paged microprocessor Unix implementation] uses 2 spare bits in PTE to define four PTE types when V == 0 and four when V == 1 CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 24

Types when V == 1 [VALID] MEM - maps virtual address to a physical address LOCK - same as MEM except page is locked into physical memory vlock(va) system call [superuser ONLY] software, not hardware, locking really a hint to OS SPY - maps virtual address [va] to a specific physical address [pa] can be used to map hardware device registers into a user process' virtual address space vspy(va, pa) system call [superuser ONLY] allows user level device drivers to be implemented CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 25

Types when V == 0 [INVALID] NULL - page NOT yet mapped to physical memory DISK - page not mapped to physical memory, but when mapped the page must be initialised using data stored on disk when V == 0, the PTE physical page # field contains a disk block number where the data is located on disk assuming a 20 bit physical page # field, a 4K page size and a 4K disk block size it is possible to accommodate a 2 20 x 2 12 = 4GB disk [limiting with current disk sizes] IOP - indicates that the disk I/O is in progress SPT shared PTE [explained in next section of notes] allows code to be shared between processes contains a pointer to a PTE in another page table CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 26

Initial Mapping of Unix/Windows Process need to create a virtual address space + page table for process CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 27

Initial Mapping of Unix/Windows Process CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 28

Initial Mapping of Unix/Windows Process text and initialised data PTEs initialised to type DISK disk block number allows data to be quickly located on disk enough real stack pages allocated [type MEM] to hold the arguments and environmental data passed to the process ALL remaining PTEs initialised to type NULL process allocated ONLY 5 pages of physical memory initially primary page table 2 secondary page tables 2 stack pages further pages allocated to process on demand CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 29

Initial Execution of Unix/Windows Process after the initial page table is created the process starts execution [start address in.exe header] will instantly generate a page fault as page containing first instruction is still on disk page faults will continue to occur as the process executes and each PTE type fault will be handled as follows: PTE type DISK action allocate a page of physical memory [OS maintains a free list] and fill with data read from disk [context switch while waiting for disk] code pages normally read ONLY, initialised data pages typically read/write code and initialised data paged in "on demand" DISK IOP MEM CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 30

Initial Execution of Unix/Windows Process PTE type NULL action physical memory has not yet been allocated the virtual fault address is checked to see if it's sensible / in range if page fault virtual address not in uninitialised data, heap or stack then it is considered to be an illegal memory access and a memory access violation is signalled otherwise a page of [zeroed] physical memory is allocated by OS MEM IOP SPY LOCK NULL MEM protection level fault [e.g. writing to text via a NULL pointer] wait for I/O to complete [see DISK type fault] protection level fault? protection level fault? if OS gets confused and cannot resolve page fault it calls panic() which reboots OS CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 31

Page Table Snapshot after Process has Started to Execute Diagram shows page table after the following pages have been accessed by the process 1 code page (1) 1 initialised data page (2) 1 uninitialised data page (3) 1 stack page (4) CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 32

Text/Code Sharing if the same process is executing more then once, ONLY a single shared copy of the code need be in memory NB: each process still needs its own pages for its data, heap and stack NB: initialised data can be shared if read-only consider the following technique for sharing pages when a process is executed for the first time, a master page table is created the PTEs corresponding to the code and initialised data are initialised to type DISK remaining PTEs set to type NULL CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 33

Text/Code Sharing.. a process page table is created by initialising its code and initialised data PTEs to type SPT the SPT PTEs point to their corresponding entries in the master page table physical pages for its initial stack are attached to the process page table remaining PTEs set to type NULL CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 34

Text/Code Sharing.. on a SPT page fault, the OS follows the SPT entry to the corresponding PTE in the master page table action performed depends on master page table PTE type DISK CODE allocate page of physical memory fill with data read from disk update PTEs in master and process page tables to point to allocated page [MEM CODE ] CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 35

Text/Code Sharing.. DISK IDATA allocate page of physical memory (1) fill with data read from disk attach to master page table [MEM] now have a read-only master copy of the initialised data page allocate page of physical memory (2) copy data from master copy attach to process page table [MEM] process now has its own copy of the initialised data page which it is free to over write could implement copy-on-write instead of copy-on-access CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 36

Text/Code Sharing.. diagram shows how another process page table is created from the master page table the MEM code entries are copied thus sharing the code the remaining PTEs for the code and initialised data are set to the SPT type and point to corresponding entry in the master page table the remaining PTEs are initialised as per the non-shared case since each instance needs its own its uninitialised data, heap and stack CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 37

Text/Code Sharing.. if all processes terminate, the OS will try to keep the master table and its attached pages in memory if another instance of the process is then created, it can quickly attach to the code pages already in memory it can also make its own copies of the initialised data pages, as needed, from the master copies attached to the master page table this is why when a process runs for a second time, it often starts up more quickly CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 38

IA-32e address spaces > 2 32 bytes [x64] pragmatic implementation [not currently realistic to implement 2 64 virtual and physical address spaces just think of the cost of 2 64 bytes RAM] CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 39

IA-32e address spaces > 2 32 bytes 2 48 byte virtual [linear in Intel terminology] and 2 52 byte physical address spaces 4 level page table structure 9-9-9-9-12 [Intel naming: PML4, Directory Ptr, Directory, Table] page table sizes 2 9 * 8 as each PTE is 64 bits [4K] PTE comprises 52 bit physical address + 12 house keeping bits [64 bits] how many bits of the 52 bit physical address actually used depends on CPU model [2 40 = 1TB, 2 42 = 4TB, 2 50 = 1PB and 2 52 = 4PB] CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 40

Summary you are now able to: explain the concept and benefits of virtual memory explain the operation of an n-level page table construct the contents of an n-level page table explain the operation of a TLB calculate the TLB hit rate explain how a MMU and an OS together support on-demand paging explain how code and initialised data can be shared between processes CS3021/3421 2017 jones@tcd.ie School of Computer Science and Statistics, Trinity College Dublin 27-Nov-17 41