CS162 Operating Systems and Systems Programming Lecture 12 Protection continued Address Translation February 25 2010 Ion Stoica http inst eecs berkeley edu cs162 Review Important Aspects of Memory Multiplexing Controlled overlap Separate state of threads should not collide in physical memory Obviously unexpected overlap causes chaos Conversely would like the ability to overlap when desired for communication Translation Ability to translate accesses from one address space virtual to a different one physical When translation exists processor uses virtual addresses physical memory uses physical addresses Side effects Can be used to avoid overlap Can be used to give uniform view of memory to programs Protection Prevent access to private memory of other processes Different pages of memory can be given special behavior Read Only Invisible to user programs etc Kernel data protected from User programs Programs protected from themselves 2 25 10 CS162 UCB Spring 2010 Lec 12 2 Review General Address Translation Data 2 Code Data Heap Stack Code Data Heap Stack Stack 1 Heap 1 Code 1 Stack 2 Prog 1 Virtual Address Space 1 Prog 2 Virtual Address Space 2 Data 1 Heap 2 Code 2 OS code Translation Map 1 OS data Translation Map 2 OS heap Stacks 2 25 10 Physical Address Space CS162 UCB Spring 2010 Lec 12 3 Review Simple Segmentation Base and Bounds CRAY 1 Base Virtual CPU Address DRAM Physical Address Limit Yes Error Can use base bounds limit for dynamic address translation Simple form of segmentation Alter every address by adding base Generate error if address bigger than limit This gives program the illusion that it is running on its own dedicated machine with memory starting at 0 Program gets continuous region of memory Addresses within program do not have to be relocated when program placed in different region of DRAM 2 25 10 CS162 UCB Spring 2010 Lec 12 4 Review Cons for Simple Segmentation Method Fragmentation problem complex memory allocation Not every process is the same size Over time memory space becomes fragmented Really bad if want space to grow dynamically e g heap process 6 process 6 process 6 process 6 process 5 process 5 process 5 process 5 process 9 process 9 process 2 OS process 10 OS OS OS Other problems for process maintenance Doesn t allow heap and stack to grow independently Want to put these as far apart in virtual memory space as possible so that they can grow as needed Hard to do inter process sharing Want to share code segments when possible Want to share memory between processes 2 25 10 CS162 UCB Spring 2010 Lec 12 5 Goals for Today Address Translation Schemes Segmentation Paging Multi level translation Paged page tables Inverted page tables Discussion of Dual Mode operation Comparison among options Note Some slides and or pictures in the following are adapted from slides 2005 Silberschatz Galvin and 2 25 10 CS162 UCB Spring Gagne Many slides generated Gagne from2010 lecture notes Lec by 12 6 More Flexible Segmentation 11 4 1 2 3 22 4 user view of memory space 3 physical memory space Logical View multiple separate segments Typical Code Data Stack Others memory sharing etc Each segment is given region of contiguous memory Has a base and limit Can reside anywhere in physical memory 2 25 10 CS162 UCB Spring 2010 Lec 12 7 Implementation of Multi Segment Model Virtual Seg Offset Address Base0Limit0 V Base1Limit1 V Base2Limit2 V Base3Limit3 N Base4Limit4 V Base5Limit5 N Base6Limit6 N Base7Limit7 V Error Physic al Addres s Segment map resides in processor Segment number mapped into base limit pair Base added to offset to generate physical address Error check catches offset out of range As many chunks of physical memory as entries Segment addressed by portion of virtual address However could be included in instruction instead x86 Example mov es bx ax What is V N Can mark segments as invalid requires check as well 2 25 10 CS162 UCB Spring 2010 Lec 12 8 Intel x86 Special Registers 80386 Special Registers Typical Segment Register Current Priority is RPL Of Code Segment CS 2 25 10 CS162 UCB Spring 2010 Lec 12 9 Example Four Segments 16 bit addresses Seg Offset 15 14 13 0x4000 Base Limit 0 code 0x400 0 0x080 0 1 data 0x480 0 0x140 0 2 0x0000 shared 0xF00 0 0x100 0 3 stack 0x000 0 0x300 0 Might be shared 0 Virtual Address Format 0x0000 Seg ID 0x4000 0x4800 0x5C00 0x8000 Space for Other Apps 0xC000 0xF000 Virtual Address Space 2 25 10 Physical Address Space CS162 UCB Spring 2010 Shared with Other Apps Lec 12 10 Example of segment translation 0x240 0x244 0x360 0x364 0x368 0x4050 main la a0 varx jal strlen strlen li v0 0 count loop lb t0 a0 beq r0 t1 done varx dw 0x314159 Seg ID Base Limit 0 code 0x400 0 0x080 0 1 data 0x480 0 0x140 0 2 shared 0xF00 0 0x100 0 Let s simulate a bit of this code to see what happens PC 0x240 3 stack Fetch 0x240 Virtual segment 0 Offset 0x2400x000 0x300 0 0 Physical address Base 0x4000 so physical addr 0x4240 Fetch instruction at 0x4240 Get la a0 varx Move 0x4050 a0 Move PC 4 PC 2 Fetch 0x244 Translated to Physical 0x4244 Get jal strlen Move 0x0248 ra return address Move 0x0360 PC 3 Fetch 0x360 Translated to Physical 0x4360 Get li v0 0 Move 0x0000 v0 Move PC 4 PC 4 Fetch 0x364 Translated to Physical 0x4364 Get lb t0 a0 Since a0 is 0x4050 try to load byte from 0x4050 Translate 0x4050 Virtual segment 1 Offset 0x50 Physical address Base 0x4800 Physical addr 0x4850 Load Byte from 0x4850 t0 Move PC 4 PC 2 25 10 CS162 UCB Spring 2010 Lec 12 11 Administrivia Midterm I coming up in 1 weeks Tuesday 3 9 3 30 6 30pm this room Should be 2 hour exam with extra time Closed book one page of hand written notes both sides No class on day of Midterm Extra Office Hours Mon 2 00 5 00 Midterm Topics Topics Everything up to Thursday 3 4 History Concurrency Multithreading Synchronization Protection Address Spaces TLBs Make sure to fill out Group Evaluations Project 2 Initial Design Document due Thursday 3 4 Look at the lecture schedule to keep up with due 2 25 10dates CS162 UCB Spring 2010 Lec 12 12 Observations about Segmentation Virtual address space has holes Segmentation efficient for sparse address spaces A correct program should never address gaps except as mentioned in moment If it does trap to kernel and dump core When it is OK to address outside valid range This is how the stack and heap are allowed to grow For instance stack takes fault system automatically increases size of stack Need protection mode in segment table For example code segment would be read only Data and
View Full Document
Unlocking...