
Lecture 8: Memory Management CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda Announcements • PeerWise quesons due tomorrow. • Project 2 is due on Friday. – Milestone on Tuesday night. Tonight. • Homework 3 is due next Monday. 2 PeerWise • A base register contains: – The first available physical memory address for the system – The beginning physical memory address of a process’s address space – The base (i.e. base 2 for binary, etc) that is used for determining page sizes – The minimum size of physical memory that will be assigned to each process 3 PeerWise • If the base register holds 640000 and the limit register is 200000, then what range of addresses can the program legally access? – 440000 through 640000 – 200000 through 640000 – 0 through 200000 – 640000 through 840000 4 Goals for Today • Understand Paging – Address translaon – Page Tables • Understand Segmentaon – How it combines features of other techniques 5 Recap: Virtual Memory • Memory Management Unit (MMU) – Hardware unit that translates a virtual address to a physical address – Each memory reference is passed through the MMU – Translate a virtual address to a physical address • Translaon Lookaside Buffer (TLB) – Essenally a cache for the MMU’s virtual‐to‐physical translaons table – Not needed for correctness but source of significant performance gain Virtual Address Translaon Physical CPU MMU Table Address Memory TLB 6 Recap: Paging • Paging solves the external fragmentaon problem by using fixed sized units in both physical and virtual memory Physical Memory Virtual Memory Page 1 Page 1 Page 2 Page 2 ….. Page 3 Page 4 Page N Page 5 7 Memory Management in Nachos Virtual Memory Physical Memory 0x00000000 0x00000000 Code Page 1 0x00000400 Code Page 2 Data Page 3 ….. ... Stack Page P 0x….. .. Page N 0x00040000 8 Memory Management in Nachos Virtual Memory Physical Memory 0x00000000 0x00000000 Code Page 1 0x00000400 Code Page 2 Data Page 3 ….. ... Stack Page P 0x….. .. • What if code secon isn’t a mulple of page size? • What if 0x….. is larger than physical memory? Page N • What if 0x….. is smaller than physical memory? 0x00040000 9 Memory Management in Nachos • How do we know how large a program is? – A lot is done for you in userprog/UserProcess.java • Your applicaons will be wrien in “C” • Your “C” programs compile into .coff files • Coff.java and CoffSecon.java are provided to paron the coff file – If not enough physical memory, exec() returns error • Nachos exec() is different from Unix exec()!!!!! • Virtual Memory maps exactly to Physical Memory? – Who needs a page table, TLB or MMU? – What are the implicaons for this in terms of mulprogramming? 10 Project 2: Mulprogramming • Need to support for mulprogamming – Programs coexist on physical memory – How do we do this? Physical Memory Physical Memory Base Register P4’s Base P1 P1 P2 P2 Limit Register Base Register P3’s Base P4’s Base P3 Virtual Address < Yes? Virtual Address + P3 P4 Offset Offset + P5 No? Would Fixed Parons work? Protecon Fault Would Variable Parons work? 11 Paging Process 1’s VAS Physical Memory Page 1 0x00000000 Page 1 Page 2 0x00000400 Process 2’s VAS Page 2 Page 3 MMU ….. Page 1 TLB Page 3 Page ... Page M Page 2 Table Page P Page 3 .. ….. Page N Page Q 0x00040000 12 MMU and TLB • Memory Management Unit (MMU) – Hardware unit that translates a virtual address to a physical address – Each memory reference is passed through the MMU – Translate a virtual address to a physical address • Translaon Lookaside Buffer (TLB) – Essenally a cache for the MMU’s virtual‐to‐physical translaons table – Not needed for correctness but source of significant performance gain Virtual Address Translaon Physical CPU MMU Table Address Memory TLB 13 Paging: Translaons • Translang addresses – Virtual address has two parts: virtual page number and offset – Virtual page number (VPN) is an index into a page table – Page table determines page frame number (PFN) – Physical address is PFN::offset 0xBAADF00D = 0xBAADF 0x00D virtual address virtual page number offset 0xBAADF Translaon 0x900DF Table virtual page number physical page number (page frame number) page table 14 Page Table Entries (PTEs) M R V Prot Page Frame Number 1 1 1 2 20 • Page table entries control mapping – The Modify bit says whether or not the page has been wrien • It is set when a write to the page occurs – The Reference bit says whether the page has been accessed • It is set when a read or write to the page occurs – The Valid bit says whether or not the PTE can be used • It is checked each me the virtual address is used – The Protecon bits say what operaons are allowed on page • Read, write, execute – The page frame number (PFN) determines physical page • Note: when you do exercises in exams or hw, we’ll oen tell you to ignore counng M,R,V,Prot bits when calculang size of structures 15 Paging Example Revisited 0xBAADF00D Physical Memory 0x00000000 Virtual Address Page number Offset Page 1 0xBAADF 0xF00D Physical Address Page 2 Page frame Offset Page Table 0x900DF 0xF00D Page 3 ….. Page number Page table entry 0x900DF00D Page N 0xFFFFFFFF M R V Prot Page Frame Number 16 V Page Frame Paging 1 0x04 Process 1’s VAS 1 0x01 Physical Memory 1 0x05 Page 1 0x00000000 1 0x07 Page 1 Page 2 0x00000400 Process 2’s VAS Page 2 Page 3 MMU ….. Page 1 TLB Page 3 Page ... Page M Page 2 Table Page P Page 3 V Page Frame .. ….. 1 0x04 Page N 1 0x01 Page Q 0x00040000 17 1 0x05 1 0x07 Example • Assume we are using Paging and: – Memory access = 5us – TLB search = 500ns • What is the avg. memory access me without the TLB? • What is the avg. memory access me with 50% TLB hit rate? • What is the avg. memory access me with 90% TLB hit rate? Virtual Address Translaon Physical CPU MMU Table Address Memory TLB 18 Paging Advantages • Easy to allocate memory – Memory comes from a free list of fixed‐size chunks – Allocang a page is just removing it from the list – External fragmentaon is not a problem • Easy to swap out chunks of a program – All chunks are the same size – Pages are a convenient mulple of the disk block size – How do we know if a page is in memory or not? 19 Paging Limitaons • Can sll have internal fragmentaon – Process may not use memory in mulples of pages • Memory reference overhead – 2 references per address lookup (page table, then memory) – Soluon – use a hardware cache of lookups (TLB) • Memory required to hold page table can be significant – Need one PTE per page – 32‐bit address space w/4KB pages = up to ____ PTEs – 4 bytes/PTE = _____ MB page table – 25 processes = ______ MB just for page tables! – Soluon: page the page tables (more later) 20 Paging: Linear Address Space 0xFFF….. (Ending Address) Stack Heap Data Segment Text Segment 0x00……. (Starng Address) 21 Segmentaon: Mulple Address Spaces Heap Stack Text Data 22 Segmentaon Physical Memory Segment Table Virtual Address limit base Segment # Offset Yes? < + No? Protecon Fault 23 Segmentaon • Segmentaon is a technique that parons memory into logically related data units – Module, procedure, stack, data, file, etc. – Virtual addresses become <segment #, offset> – Units of memory from user’s perspecve • Natural extension of variable‐sized parons – Variable‐sized parons = 1 segment/process – Segmentaon = many segments/process • Hardware support – Mulple base/limit pairs, one per segment (segment table) – Segments named by #, used to index into table 24 Segment Table • Extensions – Can have one segment table per process • Segment #s are then process‐relave (why do this?) – Can easily share memory • Put same translaon into base/limit pair • Can share with different protecons (same base/limit, diff prot) • Why is this different from pure paging? – Can define protecon by segment • Problems – Cross‐segment addresses • Segments need to have same #s for pointers to them to be shared among processes – Large segment tables • Keep in main memory, use hardware cache for speed 25 Segmentaon Example External Fragmentaon in Segmentaon Note: Image courtesy of Tanenbaum, MOS 3/e 26 Paging vs Segmentaon Consideraon Paging Segmentaon Need the programmer be aware that this technique is being used? How many linear address spaces are there? Can the total address space exceed the size of physical memory? Can procedures and data be disnguished and separately protected? Is sharing of procedures between users facilitated? Note: Image adapted from Tanenbaum, MOS 3/e 27 Segmentaon and Paging • Can combine segmentaon and paging – The x86 supports segments and paging • Use segments to manage logically related units – Module, procedure, stack, file, data, etc. – Segments vary in size, but usually large (mulple pages) • Use pages to paron segments into fixed sized chunks – Makes segments easier to manage within physical memory • Segments become “pageable” – rather than moving segments into and out of memory, just move page porons of segment – Need to allocate page table entries only for those pieces of the segments that have themselves been allocated • Tends to be complex… 28 Virtual Memory Summary • Virtual memory – Processes use virtual addresses – OS + hardware translates virtual addresses into physical addresses • Various techniques – Fixed parons – easy to use, but internal fragmentaon – Variable parons – more efficient, but external fragmentaon – Paging – use small, fixed size chunks, efficient for OS – Segmentaon – manage in chunks from user’s perspecve – Combine paging and segmentaon to get benefits of both 29 Managing Page Tables • We computed the size of the page table for a 32‐bit address space with 4K pages to be ___ MB – This
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages42 Page
-
File Size-