Outline
Memory Management ❚ Virtual memory ❚ Page-based memory management Ø Page table and address translation ❚ Multi-level page table Instructor: Dr. Tongping Liu ❚ Translation lookaside buffer (TLB) ❚ Demand paging ❚ Thrashing and working set ❚ Page replacement ❚ Kernel memory management ❚ User-space memory management
Department of Computer Science @ UTSA 1 Department of Computer Science @ UTSA 2
Memory Virtual Memory
N ❚ The ideal memory is ❚ Basic idea: allow OS to allocate 2 Ø Very large more memory than the real Auxiliary Ø Very fast regions Ø Non-volatile (doesn’t go away when power is turned off) ❚ Program uses virtual addresses ❚ The real memory is Ø Addresses local to the process Ø Limited by # of bits in address (32/64) Ø Not very large Stack Ø 32 bits: 4GB Ø Not very fast Ø 64 bits (actually 48 bits): 256TB Ø Affordable (cost) ! ❚ Virtual memory >> physical memory Heap ⇒ Pick any two… ❚ Memory management goal: make the reality Text similar to the idea world as much as possible J 0
Department of Computer Science @ UTSA 3 Department of Computer Science @ UTSA 4
1 Motivations for Virtual Memory Virtual and Physical Addresses
❚ Efficient Use of Limited Memory ❚ Virtual address space CPU chip Ø Keep only active address space in the memory Ø Determined by the instruction width Ø Some non-active parts can be stored on the disk CPU MMU Ø Same for all processes ❚ Simplify Memory Management Virtual addresses ❚ Physical memory indexed by Ø Each process “gets” the same full, linear address space from CPU to MMU physical addresses ❚ Isolates Executions of Multiple Processes Memory Ø Limited by bus size (# of bits) Ø One process can’t interfere with another’s memory Ø Amount of available memory ü They operate in different address spaces Physical addresses Ø User process cannot access privileged information on bus, in memory ü Different sections of address spaces have different permissions Disk controller
Department of Computer Science @ UTSA 6
Outline Page-Based Memory Management
❚ Virtual memory ❚ Virtual address 60–64K - 56–60K - ❚ Page-based memory management Ø Divided into pages 52–56K - ❚ Physical memory 48–52K - Ø Page table and address translation 44–48K 5 Ø Divided into frames 40–44K 1 36–40K - ❚ Multi-level page table 32–36K - 28–32K 3 7 28–32K ❚ Translation lookaside buffer (TLB) ❚ Page vs. Frame 24–28K - 6 24–28K Ø Same size address block 20–24K - 5 20–24K ❚ Demand paging 16–20K 0 4 16–20K Ø Unit of mapping/allocation 12–16K - 3 12–16K 8–12K - 2 8–12K ❚ Thrashing and working set 4–8K 4 1 4–8K ❚ Page replacement ❚ A page is mapped to a frame 0–4K 7 0 0–4K Virtual space Physical Ø All addresses in the same virtual memory ❚ Kernel memory management page are in the same physical frame ❚ User-space memory management à offset in a page
Department of Computer Science @ UTSA 7 Department of Computer Science @ UTSA 8
2 Page Table Translate Virtual to Physical Address
❚ Each process has one page table 15 ❚ Split virtual address (from CPU) into two pieces Ø Map page number à physical frame number Ø Page number (p) ❚ Number of PTEs in page table Ø Page offset (d) Ø Number of total pages in virtual space Frame 6 Ø Not just the pages in use Frame 1 ❚ Page number ❚ Page table is checked for Ø Index into an entry of the page table that holds the every address translation Frame 3 corresponding physical frame number Ø Where to store page table? ❚ Page offset Frame 0 ❚ Not all pages need to be mapped to frames Ø Position inside a page at the same time ❚ Page size = 2d bytes: determined by offset size ❚ Not all physical frames need to be used 1 Frame 4 0 Frame 7 Page table
Department of Computer Science @ UTSA 9 Department of Computer Science @ UTSA 10
Logic Address An Example of Virtual/Physical Addresses n Suppose logical address space is 2m and page size is ❚ Example: 2n, so the number of pages is 2m / 2 n , which is 2m-n Ø 64 KB virtual memory Ø 32 KB physical memory ❚ Logical Address (m bits) is divided into: Ø 4 KB page/frame size à 12 bits as offset (d) Ø Page number (p) – used as an index into a page table which contains frame number of physical memory Page #:4bits Offset: 12 bits Ø Page offset (d) – combined with base address to define the Virtual address: 16 bits How many virtual pages? physical memory address that is sent to the memory unit
page number page offset Frame #:3bits Offset: 12 bits p d physical address: 15 bits m - n! n! How many physical frames? Department of Computer Science @ UTSA 12
3 In-Class Exercise In-Class Exercise
❚ For an embedded small computer supporting up ❚ For an embedded small computer supporting up to 1k bytes physical memory, where its virtual to 1k bytes physical memory, where its virtual address is 12 bits. Suppose that the size of address is 12 bits. Suppose that the size of page/frame is 128 bytes. Use a figure to page/frame is 128 bytes. Use a figure to illustrate the physical and virtual address that illustrate the physical and virtual address that shows the number of bits for offset and shows the number of bits for offset and frame/page number for one level page table. frame/page number for one level page table. • What is the number of virtual pages for each process? • How many physical frames in total? • How many entries in page table for each process?
Department of Computer Science @ UTSA 13 Department of Computer Science @ UTSA 14
In-Class Exercise Address Translation
❚ # of virtual pages: ❚ Example: Page #:5bits Offset: 7 bits 2^12/128= 2^5 = 32 Ø 64 KB virtual memory Virtual address: 12 bits Ø 32 KB physical memory Ø 4 KB page/frame size à 12 bits as offset (d) ❚ # of physical frames 1K/128=8 Page #:4bits Offset: 12 bits Frame #:3bits Offset: 7 bits Virtual address: 16 bits How many virtual pages? physical address: 10 bits Address ❚ # of page table entries Translation 32 Frame #:3bits Offset: 12 bits physical address: 15 bits How many physical frames? Department of Computer Science @ UTSA 15 Department of Computer Science @ UTSA 16
4 Address Translation Architecture Computing Physical Address
frame number page number page offset 1. Compute the page number
0 2. Check the page table, get physical CPU p d f d 1 frame number . page table . 0 f-1 1 3. Compute the starting address of Virtual address f . physical frame . physical address f+1 p-1 f+2 . p f . 4. Compute the physical address . . . physical memory
17 Department of Computer Science @ UTSA 18
Computing Physical Address Computing Physical Address
Virtual address 0x44, offset 0x44 Virtual address 0x224, offset 0x24 1. Compute the page number 1. Compute the page number Page number is #0 Page number is #4 2. Check the page table, get physical 2. Check the page table, get physical frame number frame number Frame number is #2 Frame number is #3 3. Compute the starting address of 3. Compute the starting address of physical frame physical frame Start Address = 2 * 0x80 = 0x100 Start Address = 3 * 0x80 = 0x180 4. Compute the physical address 4. Compute the physical address PhysicalAddress = 0x100 + 0x44 = 0x144 PhysicalAddress = 0x180 + 0x24 = 0x1A4
Department of Computer Science @ UTSA 19 Department of Computer Science @ UTSA 20
5 . Limitations of One-level Page Table Page/Frame Size
❚ Page table should contain all pages of virtual space ❚ Determine the number of bits in offset! Ø Otherwise, some addresses can’t be accessed. ❚ Smaller page size Ø + Less internal fragmentation ❚ All entries should be physically continuous Ø - Larger page table: require more than one frame, hard to allocate! Ø Proof by contradiction. If the index is not continuous, we will need another mechanism to tell us where has a hole or ❚ Larger page size not Ø + Smaller page table and less overhead to track them Ø + More efficient to transfer to/from disk Ø - More internal fragmentation, wasting memory
Department of Computer Science @ UTSA 21
. Size of Page Table Size of Page Table
❚ If a computer has 22 bits of logical address (4 MB), ❚ Modern Machine and supports the physical memory up to 1 MB. The Ø 32 bits virtual address page/frame size is 1KB. Ø Size of one page/frame is 4KB (12 bits) ❚ Page table size Virtual addr: P#:12 bits Offset: 10 bits Ø # of virtual pages: 32 – 12 = 20 bits à 220 PTEs Ø Page table size = PTE size * 220 = 4 MB per process à 210 frames Physical addr: F#:10 bits Offset: 10 bits Ø Number of pages: 12 bits à 4K pages ❚ If there are 128 processes, and physical memory is 1 GB Ø Size of page table: Ø Page tables occupy 128 * 4MB = 512 MB § Each page entry must have at least 10 bits à 2 bytes Ø 50% of memory will be used by page tables § 4K * 2 bytes = 8KB à requires 8 consecutive frames How can we get smaller page table?!
Department of Computer Science @ UTSA 24
6 Outline Two-Level Page Tables . . ❚ Solution: multi-level page tables 220 ❚ Virtual memory . ❚ Virtual address: three parts 657 . ❚ Page-based memory management . Ø Level-one page number (10 bits) . . . . Ø Page table and address translation Ø Level-two page number (10 bits) 401. . . ❚ Multi-level page table Ø Offset (12 bits) 125 . . . ❚ Translation lookaside buffer (TLB) . 613 ❚ PTE in 1st level page table . . . . . ❚ Demand paging contains the address of physical . frame for 2nd level page table 961 . 1st level . ❚ Thrashing and working set . . page table . . ❚ Page replacement ❚ PTE in 2nd level page table has the . . ❚ Kernel memory management actual physical frame number 884 . main 2nd level 960 ❚ User-space memory management . memory page tables . Department of Computer Science @ UTSA 25 Department of Computer Science @ UTSA 955 26
Two-Level Page Tables Example: 2-level Address Translation . . ❚ We don’t have to allocate all 220 page number page offset . entries initially 657 . . p1 = 10 bits p2 = 10 bits offset = 12 bits . . frame ❚ They don’t have to continuous . number 401. . . 0 125 . Page physical address 1 . . . 613 table 18 12 . . . . . . base . . 0 961 . 0 1st level . 1 . page table . . 1 . . . p . main memory . 1 884 . . p2 18 main . . 2nd level 960 . . . memory . Which tables should page tables . 1st level page table 2nd level page table Department of Computer Science @ UTSA be in memory? 28 955
7 Memory Requirement of Page Tables Memory Requirement of Page Tables
❚ Only the 1st level page table and the required 2nd ❚ Only the 1st level page table and the required 2nd level page tables need to be in memory level page tables need to be in memory
❚ Example: if a process accesses 32 MB virtual memory on such a machine, what is the minimum ❚ 32bit machine, page size 4k, each entry 4 bytes, memory required for the page table using two-level page table (with 10bit-10bit-12bit Ø 4KB / page à process has 8K (8*210) virtual pages design), what is the size for the full page table Ø One 2nd level page table maps 210 pages; Ø Level-0: 1024 entries*4bytes Ø Number (minimum) of 2nd level page table needed: Ø Level-1: 1024*1024entries=1M entries * 4bytes 8*2^10/2^10 = 8 Ø Total: 4M+4K Ø Total (minimum) memory for page table: 1st level page table + 8; in total of 9 page tables à 9 X 4KB = 36 KB
Department of Computer Science @ UTSA 29 Department of Computer Science @ UTSA 30
Memory Requirement of Page Tables Other Questions
❚ Only the 1st level page table and the required 2nd level page tables need to be in memory ❚ If we have two pages (0x00000000 and 0x0020100)), what is the size of page table? ❚ Example: if a process accesses 32 MB virtual memory on such a machine, what is the maximum ❚ 32bit machine, page size 4k, each entry 4 bytes, memory required for the page table two level page table (two pages:0x0000000, and Ø 4KB / page à process has 8K (8*210) virtual pages 0xFFFFF000) Ø We will have at most 2^10 (defining by 10 bits of 1st level) pages/frames in 2nd level Ø Therefore, all of these frames at minimum can have one entry inside. Ø Total (maximum) memory for page table: 1st level page table + 1024 2nd level page tableà 1025 X 4KB 31 Department of Computer Science @ UTSA 32
8 Linux’s 3 level page table Designing of 2-level page table
Linear Address converted to physical address using 3 levels 1. Determine number of bits of page offset (12Bità4KB) 2. Determine the size of each page table entry 3. Determine the number of bits for 2nd level page table Index into Index into Index into Page One principle: page table à fit into one frame Page Dir. Page Middle Page Table Offset Dir. 4. Determine the number of bits for 1st level page table
What are the benefits to use 3-level page table? What are the shortcomings?
33 Department of Computer Science @ UTSA 34
Page Table Entry (PTE) Designing of 2-level Page Table
❚ Each entry in the page table contains ❚ Suppose that a system has a 24-bit logical address Ø Frame number: bits depends on # of frames in physical memory space and is byte-addressable. Physical address Ø Valid bit: set if this logical page number has a corresponding physical frame in memory has 20 bits, and the size of a page/frame is 1K ü If not valid, remainder of this PTE is irrelevant bytes. How to design a two-level page table? Ø Referenced bit: set if data on the page has been accessed 1. The size of page table entry will be 2 bytes (larger Ø Dirty (modified) bit: set if data on the page has been modified Ø Protection information: read-only/read-write than 15 bits) ❚ Size of each PTE: at least frame number plus 4/5 bits 2. One page can hold 512 entries (1K/2=512) nd Protection D R V Frame number 3. Thus, we need 9 bits for the 2 level page table
5 bits 9 bits 10 bits Dirty bit Referenced bit Valid bit
Department of Computer Science @ UTSA 35 Department of Computer Science @ UTSA 36
9 Designing of 2-level Page Table Designing of 2-level Page Table (2)
❚ Suppose that a system has a 24-bit logical address ❚ Suppose that a system has a 18-bit logical address space and is byte-addressable. Physical address space and is byte-addressable. The amount of has 20 bits, and the size of a page/frame is 1K physical memory is 64KB (i.e., the physical bytes. How to design a two-level page table? address has 16 bits) and the size of a page/frame is 128 bytes. How to design a two-level page table? 4 bits 10 bits 10 bits 1. The size of page table entry will be 2 bytes (larger than 11 bits) Second level 10 bits, can’t be fitted into one page. 2. One page can hold 64 entries (128/2=64) Then we may need to have multiple pages (2 pages) 3. Thus, we need 6 bits for the 2nd level page table that are continuous, which increases the complexity of OS design 5 bits 6 bits 7 bits
Department of Computer Science @ UTSA 37 Department of Computer Science @ UTSA 38
How Many Levels are Needed? Outline
❚ New architecture with 64-bits address? ❚ Virtual memory Ø Suppose 4KB page(frame) size (12-bit offset) ❚ Page-based memory management Ø One-level page table, with 264 / 212 = 252 entries!!!! Ø Two-level page table, then inner page table could be one page, Ø Page table and address translation containing 1K entries (10-bit) (assuming PTE size is 4 bytes), then outer page ❚ Multi-level page table needs to contain 242 entries !!!
Ø In total, we will have 6-level paging (7 memory accesses)! ❚ Translation lookaside buffer (TLB) ❚ Problems with multiple-level page table ❚ Demand paging Ø One additional memory access for each level added! ❚ Thrashing and working set Ø Multiple levels are possible; but prohibitive after a few levels ❚ Page replacement Are there any other alternatives to manage the page table, in order to reduce access time? ❚ Kernel memory management ❚ User-space memory management
Department of Computer Science @ UTSA 40
10 Translation Look-ahead Buffers (TLB) Translation Lookaside Buffer (TLB)
❚ Translation Look-ahead buffers (TLB) ❚ Small Hardware: fast Logical Physical page # frame # Ø A special fast-lookup hardware cache: associative memory! ❚ Store recent mapping of page à frame (64 ~ 1024 entries) 8 3 Ø Access by content à parallel search: if in TLB, get frame # ❚ If desired logical page number is unused Ø Otherwise, access the page table in memory, and get frame # found, get frame number from TLB 2 1 and put it into TLB ❚ If not, 3 0 ü (if TLB is full, replace an old entry.) Ø Get frame number from page table in 12 12 memory 29 6 Page # Frame # Ø Use standard cache technique 22 11 Ø Replace an entry in the TLB with the logical & physical page number from 7 4 this reference If found Example TLB Target Page # Target Frame # Ø May contain complete page table entries for processes with small number of pages
Department of Computer Science @ UTSA 42
Paging Hardware With TLB Address Translation with TLB
page number page offset frame number frame number CPU p d 0 Virtual address f d 1 . p f physical address . Base reg 0 f-1 in PCB TLB/hardware 1 f . . f+1 f+2 p f . P+1 . n Page fault: valid bit physical memory page table (in mem) What happens when cpu performs a context switch? 44
11 Integrating VM and Cache Integrating TLB, Cache with Memory
VA PA miss “ ” Trans- Main ❚ Translation Lookaside Buffer (TLB) Cache CPU lation Memory hit hit data VA PA miss TLB Main ❚ Modern caches are “physically addressed” CPU Cache Ø Allows multiple processes to have data in the cache Lookup Memory simultaneously. Otherwise, every context switch will require to miss hit flush the cache Trans- Ø Cache doesn’t need to be associated with protection issues lation ü Access rights checked as part of address translation ❚ Perform address translation before cache lookup data Ø Could involve memory access (to get PTE) Ø So page table entries can also be cached
Basic overflow Memory Accesses Time
1. CPU will generate a virtual address ❚ Assuming: 2. Check the TLB at first to find the mapping of this page. If the Ø TLB lookup time = a corresponding entry exists, then we could get the PA Ø Memory access time = m address directly. Otherwise, we will go through the slow address translation, and then save the mapping into TLB ❚ Hit ratio (h) is percentage of time that a logical page (typically after the translation). number is found in the TLB 3. After getting a PA address, check whether the Ø More TLB entries usually means higher h corresponding cache line is in the cache or not. If yes, then ❚ Effective Access Time (EAT) is calculated (don’t we will have a cache hit. The value of the corresponding include cache effect) memory unit will be returned to CPU. Ø EAT = (m + a)h + (m + m + a)(1-h) = a + (2-h)m 4. Otherwise, it is a cache miss. The cache line will be fetched into the cache at first. Then the value will be sent back to ❚ Interpretation back to the CPU Ø Reference always requires TLB lookup, 1 memory access Department of Computer Science @ UTSA 47 Ø TLB misses also requireDepartment ofan Computer additional Science @ UTSA memory reference 48
12 Effective Access Time: An Example Outline
n TLB Lookup = 20 nanosecond ❚ Virtual memory n Memory cycle time is 100 nanosecond ❚ Page-based memory management n Hit ratio = 80% Ø Page table and address translation n Effective Access Time (EAT) would be EAT = (20+100) 0.8 + ❚ Multi-level page table (20+100+100)(1 – 0.8) ❚ Translation lookaside buffer (TLB) = 140 nanosecond ❚ Demand paging n 40% slow down ❚ Thrashing and working set n What is the EAT with 98% hit rate? ❚ Page replacement ❚ Kernel memory management ❚ User-space memory management
Department of Computer Science @ UTSA 50
Page Table Entry (PTE) Valid (v) or Invalid (i) Bit
❚ Each entry in the page table contains Ø Frame number: bits depends on # of frames in physical memory Ø Valid bit: set if this logical page number has a corresponding physical frame in memory ü If not valid, remainder of this PTE is irrelevant Ø Referenced bit: set if data on the page has been accessed Ø Dirty (modified) bit: set if data on the page has been modified Ø Protection information: read-only/read-write ❚ Size of each PTE: at least frame number plus 4/5 bits
Protection D R V Frame number
Dirty bit Referenced bit Valid bit
Department of Computer Science @ UTSA 51
13 Paging and Demand Paging Demand Paging
❚ Paging: ❚ Bring a page into memory Ø Paging is a memory management scheme by which a only when it is needed computer stores and retrieves data from secondary storage Ø Less I/O needed for use in main memory with the same-size blocks called Ø Less memory needed pages. Ø Faster response Ø Paging lets programs exceed the size of available physical Ø More users memory. ❚ Demand Paging: ❚ Page is needed ⇒ Ø Pages are loaded only when they are referenced. reference to it Ø invalid reference ⇒ abort The valid bit allows the implementation of demand paging. Ø not-in-memory ⇒ bring to memory
Department of Computer Science @ UTSA 53
Valid-Invalid Bit Page Fault
❚ With each page table entry 1. Reference to a page, a valid–invalid bit is If Invalid reference ⇒ abort associated, 2. If not in memory, page ❚ v ⇒ in-memory, fault occurs (trap to OS) !i ⇒ not-in-memory 3. OS allocates an empty ❚ Initially bit is set to i on all frame
entries 4. Swap page into frame ❚ During address translation, 5. Reset page tables, if valid–invalid bit in page set validation bit = v table entry 6. Restart the instruction is i ⇒ page fault (trap) that caused the page fault
14 Performance of Demand Paging Demand Paging Example
❚ Page Fault Rate 0 ≤ p ≤ 1.0 ❚ Memory access time = 200 nanoseconds
Ø if p = 0 no page faults ❚ Average page-fault service time = 8 milliseconds Ø if p = 1, every reference is a fault ❚ EAT = (1 – p) x 200 + p (8 milliseconds) ❚ Effective Access Time (EAT) = (1 – p) x 200 + p x 8,000,000