CS162 Operating Systems and Systems Programming Lecture 9 Address Translation February 16 2011 Ion Stoica http inst eecs berkeley edu cs162 Review Important Aspects of Memory Multiplexing Controlled overlap Ability to explicitly control whether to processes should share or not a region of memory Protection Prevent access to private memory of other processes Kernel data protected from User programs Programs protected from themselves Different pages of memory can be given special behavior Read Only Invisible to user programs etc 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 2 16 Can be used to avoid overlap Can be used to give uniform view of memory to programs Ion Stoica CS162 UCB Spring 2011 Lec 9 2 Goals for Today Address Translation Schemes Segmentation Paging Multi level translation Paged page tables Inverted page tables Discussion of Dual Mode operation Note Some slides and or pictures in the following are adapted from slides 2005 Silberschatz Galvin and Gagne Many slides generated from lecture notes by Kubiatowicz 2 16 Ion Stoica CS162 UCB Spring 2011 Lec 9 3 Uniprograming Loading Program in Memory Memory 0x0000 0x0300 00000020 Program view of memory data1 dw 32 start lw r1 0 data1 jal checkit loop addi r1 r1 1 bnz r1 r0 loop checkit 0x300 0x900 0x904 0x908 0x90C 00000020 8C2000C0 0C000340 2021FFFF 1420FFFF 0x0900 8C2000C0 0C000340 2021FFFF 1420FFFF 0xFFFF 2 16 Ion Stoica CS162 UCB Spring 2011 Lec 9 4 Multiprograming Where do you load program Memory 0x0000 0x0300 Program view of memory data1 dw 32 start lw r1 0 data1 jal checkit loop addi r1 r1 1 bnz r1 r0 loop checkit 0x300 0x900 0x904 0x908 0x90C 00000020 8C2000C0 0C000340 2021FFFF 1420FFFF Need address translation 2 16 Ion Stoica CS162 UCB Spring 2011 0x0900 App X 0xFFFF Lec 9 5 Example of 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 16 Physical Address Space Ion Stoica CS162 UCB Spring 2011 Lec 9 6 Segmentation with Base and Limit Registers CPU Base Virtual Address Limit Physical Address DRAM No Error Could use base limit for dynamic address translation often called segmentation Alter address of every load store by adding base User allowed to read write within segment Accesses are relative to segment so don t have to be relocated when program moved to different segment User may have multiple segments available e g x86 2 16 Loads and stores include segment ID in opcode x86 Example mov es bx ax Operating system moves around segment base pointers as necessary Ion Stoica CS162 UCB Spring 2011 Lec 9 7 More Flexible Segmentation 1 1 4 1 2 3 4 user view of memory space 2 2 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 2 16 Has a base and limit in physical memory Can reside anywhere Ion Stoica CS162 UCB Spring 2011 Lec 9 8 Implementation of Multi Segment Model Virtual Seg Address Offset Base0 Base1 Base2 Base3 Base4 Base5 Base6 Base7 Limit0 Limit1 Limit2 Limit3 Limit4 Limit5 Limit6 Limit7 V V V N V N N V Error Physical Address 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 2 16 Can mark segments as invalid requires check as well Ion Stoica CS162 UCB Spring 2011 Lec 9 9 Example Four Segments 16 bit addresses Seg ID Seg Offset 15 14 13 0 Virtual Address Format Base Limit 0 code 0x4000 0x0800 1 data 0x4800 0x1400 2 shared 0xF000 0x1000 3 stack 0x0000 0x3000 0x0000 0x0000 0x4000 0x4800 0x5C00 0x4000 Might be shared 0x8000 Space for Other Apps 0xC000 0xF000 2 16 Virtual Address Space Physical Address Space Ion Stoica CS162 UCB Spring 2011 Shared with Other Apps Lec 9 10 Example of segment translation 0x240 0x244 0x360 0x364 0x368 0x4050 main strlen loop varx la a0 varx jal strlen li v0 0 count lb t0 a0 beq r0 t1 done dw 0x314159 Seg ID Base Limit 0 code 0x4000 0x0800 1 data 0x4800 0x1400 2 shared 0xF000 0x1000 3 stack 0x0000 0x3000 Let s simulate a bit of this code to see what happens PC 0x240 1 Fetch 0x240 Virtual segment 0 Offset 0x240 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 16 Ion Stoica CS162 UCB Spring 2011 Lec 9 11 Issues with simple segmentation method 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 Fragmentation problem Not every process is the same size Over time memory space becomes fragmented Hard to do inter process sharing Want to share code segments when possible Want to share memory between processes Helped by providing multiple segments per process Need enough physical memory for every process 2 16 Ion Stoica CS162 UCB Spring 2011 Lec 9 12 Observations about Segmentation 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 stack would be read write stores allowed Shared segment could be read only or read write What must be saved restored on context switch Segment table stored in CPU not in memory small Might store all of processes memory onto disk when switched called swapping 2 16 Ion Stoica CS162 UCB Spring 2011 Lec 9 13
View Full Document
Unlocking...