Memory Management Disclaimer: some slides are adopted from book authors’ slides with permission 1
Recap: Virtual Memory (VM) Process A Process C Process B MMU 2 Physical Memory
Recap: MMU • Hardware that translates virtual addresses to physical addresses 1) base register 2) base + limit registers (segmentation); 3) paging 3
Recap: Page Table based Address Translation Virtual address 0x12345678 Page # Offset Ox12345 0x678 0x678 Page table 0x12345 Physical address 0xabcde678 offset frame #: 0xabcde frame # 4
Advantages of Paging • No external fragmentation – Efficient use of memory – Internal fragmentation (waste within a page) still exists 5
Issues of Paging • Translation speed – Each load/store instruction requires a translation – Table is stored in memory – Memory is slow to access • ~100 CPU cycles to access DRAM 6
Translation Lookaside Buffer (TLB) • Cache frequent address translations – So that CPU don’t need to access the page table all the time – Much faster 7
Issues of Paging • Page size – Small: minimize space waste, requires a large table – Big: can waste lots of space, the table size is small – Typical size: 4KB – How many pages are needed for 4GB (32bit)? • 4GB/4KB = 1M pages – What is the required page table size? • assume 1 page table entry (PTE) is 4bytes • 1M * 4bytes = 4MB – Btw, this is for each process. What if you have 100 processes? Or what if you have a 64bit address? 8
Paging • Advantages – No external fragmentation • Two main Issues – Translation speed can be slow • TLB – Table size is big 9
Multi-level Paging • Two-level paging 10
Two Level Address Translation Virtual address 1 st level 2 nd level offset Base ptr 2 nd level Page table 1 st level Physical address Page table Frame # Offset 11
Example 8 bits 8 bits 8 bits 1 st level 2 nd level offset Virtual address format (24bits) Vaddr: 0x072370 Vaddr: 0x082370 Vaddr: 0x0703FE 1 st level idx: __ 1 st level idx: __ 1 st level idx: 07 2 nd level idx: 03 2 nd level idx: __ 2 nd level idx: __ Offset: __ Offset: __ Offset: FE 12
Multi-level Paging • Can save table space • How, why? 13
Summary • MMU – Virtual address physical address – Various designs are possible, but • Paged MMU – Memory is divided into fixed-sized pages – Use page table to store the translation table – No external fragmentation: i.e., efficient space utilization 14
Virtual Memory (VM) • Abstraction – 4GB linear address space for each process • Reality – 1GB of actual physical memory shared with 20 other processes • Does each process use the (1) entire virtual memory (2) all the time? 15
Concepts to Learn • Demand paging 16
Demand Paging • Idea: instead of keeping the entire memory pages in memory all the time, keep only part of them on a on-demand basis 17
Page Table Entry (PTE) • PTE format (architecture specific) 1 1 1 2 20 bits V M R P Page Frame No – Valid bit (V): whether the page is in memory – Modify bit (M): whether the page is modified – Reference bit (R): whether the page is accessed – Protection bits(P): readable, writable, executable 18
Partial Memory Mapping • Not all pages are in memory (i.e., valid=1) 19
Page Fault • When a virtual address can not be translated to a physical address, MMU generates a trap to the OS • Page fault handling procedure – Step 1: allocate a free page frame – Step 2: bring the stored page on disk (if necessary) – Step 3: update the PTE (mapping and valid bit) – Step 4: restart the instruction 20
Page Fault Handling 21
Demand Paging 22
Starting Up a Process Stack Unmapped pages Heap Data Code 23
Starting Up a Process Stack Heap Access next instruction Data Code 24
Starting Up a Process Stack Heap Page fault Data Code 25
Starting Up a Process Stack OS 1) allocates free page frame 2) loads the missed page from the disk (exec file) 3) updates the page table entry Heap Data Code 26
Starting Up a Process Stack Over time, more pages are mapped as needed Heap Data Code 27
Anonymous Page • An executable file contains code (binary) – So we can read from the executable file • What about heap? – No backing storage (unless it is swapped out later) – Simply map a new free page (anonymous page) into the address space 28
Program Binary Sharing Bash #2 Bash #1 Physical memory Bash text • Multiple instances of the same program – E.g., 10 bash shells 29
Recommend
More recommend