CS 153 Design of Operating Systems Spring 18 Lectre 15: Virtal Address Space Instrctor: Chengy Song Slide contribtions from Nael Ab-Ghazaleh, Harsha Madhyvasta and Zhiyn Qian
OS Abstractions Applications Process File system Virtal memory Operating System CPU Hardware Disk RAM CS 153 Lectre 15 Virtal Address Space 2
Need for Virtal Memory Rewind to the days of second-generation compters Programs se physical addresses directly OS loads job, rns it, nloads it Mltiprogramming changes all of this Want mltiple processes in memory at once» Overlap I/O and CPU of mltiple jobs How to share physical memory across mltiple processes?» Many programs do not need all of their code and data at once (or ever) no need to allocate memory for it» A program can rn on machine with less memory than it needs CS 153 Lectre 15 Virtal Address Space 3
Virtal Addresses To make it easier to manage the memory of processes rnning in the system, we re going to make them se virtal addresses (logical addresses) Virtal addresses are independent of the actal physical location of the data referenced OS determines location of data in physical memory Instrctions exected by the CPU isse virtal addresses Virtal addresses are translated by hardware into physical addresses (with help from OS) The set of virtal addresses that can be sed by a process comprises its virtal address space CS 153 Lectre 15 Virtal Address Space 4
Virtal Addresses virtal addresses physical addresses processor vmap physical memory Many ways to do this translation Need hardware spport and OS management algorithms Reqirements Need protection restrict which addresses jobs can se Fast translation lookps need to be fast Fast change pdating memory hardware on context switch CS 153 Lectre 15 Virtal Address Space 5
Fixed Partitions Physical memory is broken p into fixed partitions Size of each partition is the same and fixed Hardware reqirements: base register Physical address = virtal address + base register Base register loaded by OS when it switches to a process Physical Memory P1 P2 P3 P4 P5 CS 153 Lectre 15 Virtal Address Space 6
Fixed Partitions Physical Memory Base Register P4 s Base P1 P2 Virtal Address Offset + P3 P4 How do we provide protection? P5 CS 153 Lectre 15 Virtal Address Space 7
Fixed Partitions Advantages Easy to implement» Need base register» Verify that offset is less than fixed partition size Fast context switch Problems? Internal fragmentation: memory in a partition not sed by a process is not available to other processes Partition size: one size does not fit all (very large processes?) CS 153 Lectre 15 Virtal Address Space 8
Variable Partitions Natral extension physical memory is broken p into variable sized partitions Hardware reqirements: base register and limit register Physical address = virtal address + base register Why do we need the limit register? Protection: if (virtal address > limit) then falt CS 153 Lectre 15 Virtal Address Space 9
Variable Partitions Base Register P3 s Base Limit Register P3 s Limit P1 P2 Virtal Address Offset < Yes? + P3 No? Protection Falt CS 153 Lectre 15 Virtal Address Space 10
Variable Partitions Advantages No internal fragmentation: allocate jst enogh for process Problems? External fragmentation: job loading and nloading prodces empty holes scattered throghot memory P1 P2 P3 P4 CS 153 Lectre 15 Virtal Address Space 11
Segmentation Segmentation: partition memory into logically related nits Modle, procedre, stack, data, file, etc. Units of memory from ser s perspective Natral extension of variable-sized partitions Variable-sized partitions = 1 segment/process Segmentation = many segments/process Fixed partition : Paging :: Variable partition : Segmentation Hardware spport Mltiple base/limit pairs, one per segment (segment table) Segments named by #, sed to index into table Virtal addresses become <segment #, offset> CS 153 Lectre 15 Virtal Address Space 12
Segment Lookps Segment Table Physical Memory limit base Segment # Offset Virtal Address < Yes? + No? Protection Falt CS 153 Lectre 15 Virtal Address Space 13
Paging New Idea: split virtal address space into mltiple partitions Each can go anywhere! Virtal Memory Page 1 Page 2 Page 3 Physical Memory Page N Paging solves the external fragmentation problem by sing fixed sized nits in both physical and virtal memory CS 153 Lectre 15 Virtal Address Space Bt need to keep track of where things are! 14
Process Perspective Processes view memory as one contigos address space from 0 throgh N Virtal address space (VAS) In reality, pages are scattered throghot physical storage The mapping is invisible to the program Protection is provided becase a program cannot reference memory otside of its VAS The address 0x1000 maps to different physical addresses in different processes CS 153 Lectre 15 Virtal Address Space 15
Paging Translating addresses Virtal address has two parts: virtal page nmber and offset Virtal page nmber (VPN) is an index into a page table Page table determines page frame nmber (PFN) Physical address is PFN::offset Page tables Map virtal page nmber (VPN) to page frame nmber (PFN)» VPN is the index into the table that determines PFN One page table entry (PTE) per page in virtal address space» Or, one PTE per VPN CS 153 Lectre 15 Virtal Address Space 16
Page Lookps Physical Memory Virtal Address Page nmber Offset Page Table Physical Address Page frame Offset Page frame CS 153 Lectre 15 Virtal Address Space 17
Paging Example Pages are 4KB Offset is 12 bits (becase 4KB = 2 12 bytes) VPN is 20 bits (32 bits is the length of every virtal address) Virtal address is 0x7468 Virtal page is 0x7, offset is 0x468 Page table entry 0x7 contains 0x2000 Page frame nmber is 0x2000 Seventh virtal page is at address 0x2000 (2nd physical page) Physical address = 0x2000 + 0x468 = 0x2468 CS 153 Lectre 15 Virtal Address Space 18
Page Table Entries (PTEs) 1 1 1 2 20 M R V Prot Page Frame Nmber Page table entries control mapping The Modify bit says whether or not the page has been written» It is set when a write to the page occrs The Reference bit says whether the page has been accessed» It is set when a read or write to the page occrs The Valid bit says whether or not the PTE can be sed» It is checked each time the virtal address is sed (Why?) The Protection bits say what operations are allowed on page» Read, write, execte (Why do we need these?) The page frame nmber (PFN) determines physical page CS 153 Lectre 15 Virtal Address Space 19
Paging Advantages Easy to allocate memory Memory comes from a free list of fixed size chnks Allocating a page is jst removing it from the list External fragmentation not a problem» All pages of the same size Simplifies protection All chnks are the same size Like fixed partitions, don t need a limit register Simplifies virtal memory later CS 153 Lectre 15 Virtal Address Space 20
Paging Limitations Can still have internal fragmentation Process may not se memory in mltiples of a page Memory reference overhead 2 references per address lookp (page table, then memory) What can we do? Memory reqired to hold page table can be significant Need one PTE per page 32 bit address space w/ 4KB pages = 2 20 PTEs 4 bytes/pte = 4MB/page table 25 processes = 100MB jst for page tables! What can we do? CS 153 Lectre 15 Virtal Address Space 21
Paging Qestion Can we serve a process asking for more memory than we physically have? Virtal Memory of Process 1 Page 1 Physical Memory Page 2 Page 3 Page N CS 153 Lectre 15 Virtal Address Space 22
Segmentation and Paging Can combine segmentation and paging The x86 spports segments and paging Use segments to manage logically related nits Modle, procedre, stack, file, data, etc. Segments vary in size, bt sally large (mltiple pages) Use pages to partition segments into fixed size chnks Makes segments easier to manage within physical memory» Segments become pageable rather than moving segments into and ot of memory, jst move page portions of segment Need to allocate page table entries only for those pieces of the segments that have themselves been allocated Tends to be complex CS 153 Lectre 15 Virtal Address Space 23
Smmary Virtal address space Developers se virtal address Processes se virtal address OS + hardware translate VA into PA Varios techniqes Fixed partitions Variable partitions Segmentation Paging CS 153 Lectre 15 Virtal Address Space 24
Next class More on Paging Preparation Read Modle 18 and 19 CS 153 Lectre 15 Virtal Address Space 25