Abstraction is our Business What I have A single (or a finite number) of CPUs Memory Management Many programs I would like to run What I want : a Thread Each program has full control of one or more CPUs 1 2 Abstraction Address Space is our Business Set of all names used to identify and What I have manipulate unique instances of a given resource A certain amount of physical memory memory locations (determined by the size of the Multiple programs I would like to run machine’ s word) together, they may need more than the available physical memory for 32-bit-register machine, the address space goes from 0x00000000 to 0xFFFFFFFF What I want : an Address Space phone numbers (XXX) (YYY-YYYY) Each program has as much memory as the machine’ s architecture will allow to name colors: R (8 bits) + G (8 bits) + B (8 bits) All for itself 3 4
Virtual Address Space: Physical Address Space: An Abstraction for Memory How memory may actually look 0 Program Code Virtual addresses start at 0 Processes loaded in memory at some 0 OS memory location Heap Heap and stack can be placed far free virtual address 0 is not loaded at away from each other, so they can physical address 0 code, nicely grow Process 2 data, etc Multiple processes may be loaded in free free Addresses are all contiguous memory at the same time, and yet… free Size is independent of physical code, …physical memory may be too small Process 1 data, memory on the machine etc to hold even a single virtual address code, Process 3 data, 63KB space in its entirety etc Stack free 64KB 64-bit registers, anyone? 512K 5 6 II. Memory Isolation Isolation Step 2: Address Translation At all times, functions used by different processes map to disjoint ranges — aka “Stay in your room!” Implement a function mapping into ⟨ pid, virtual address ⟩ physical address p i Virtual Physical p i Advantages: a486d9 p j isolation relocation 5e3a07 data sharing multiplexing
Relocation Relocation The range of the function used by a process The range of the function used by a process can change over time can change over time — “Move to a new room!” p i p i Data Sharing Multiplexing Create illusion of almost infinite memory by Map different virtual addresses of distinct changing domain (set of virtual addresses) that processes to the same physical address — maps to a given range of physical addresses — “Share the kitchen!” ever lived in a studio? p i p i 04d26a 5e3a07 p j 119af3
Multiplexing Multiplexing The domain (set of virtual addresses) that map The domain (set of virtual addresses) that map to a given range of physical addresses can to a given range of physical addresses can change over time change over time p i p i Multiplexing Multiplexing The domain (set of virtual addresses) that map The domain (set of virtual addresses) that map to a given range of physical addresses can to a given range of physical addresses can change over time change over time p i p i
More Multiplexing More Multiplexing At different times, different processes can map At different times, different processes can map part of their virtual address space into the part of their virtual address space into the same physical memory — change tenants! same physical memory — change tenants! p i p i p j p j Contiguity Contiguity Contiguous virtual addresses need not map to Contiguous virtual addresses need not map to contiguous physical addresses contiguous physical addresses p i p i 19 20
More sophisticated The Identity Mapping address translation Map each virtual address onto the 0 How to perform the mapping efficiently? identical physical address OS So that it can be represented concisely? 16KB Virtual and physical address spaces Heap So that it can be computed quickly? have the same size So that it makes efficient use of the limited Run a single program at a time physical memory? OS can be a simple library free So that multiple processes coexist in physical very early computers memory while guaranteeing isolation? Friendly amendment: leave some of So that it decouples the size of the virtual and the physical address space for the OS physical addresses? Use loader to relocate process Stack Ask hardware for help! Max early PCs 21 22 Base & Bound Base & Bound 0 P 1 P 1 : Base = 1000; Bound = 300 1000 300 Goal: allow multiple processes to coexist in memory while guaranteeing isolation P 2 : Base = 500; Bound = 400 Needed hardware Memory Exception two registers: Base and Bound (a.k.a. Limit) 1000 no Stored in the PCB 150 CPU ≤ + 1300 yes Mapping Virtual Physical address address pa = va + Base as long as 0 ≤ va ≤ Bound On context switch, change B&B (privileged instruction) Bound Base Register Register 23 24 MAX sys
Base & Bound Base & Bound 0 0 P 1 : Base = 1000; Bound = 300 P 1 : Base = 1000; Bound = 300 P 2 : Base = 500; Bound = 400 P 2 : Base = 500; Bound = 400 Memory Memory Exception Exception 1000 1000 no no 150 150 CPU 1300 CPU 1150 1300 ≤ + ≤ + yes yes Virtual Physical Virtual Physical address address address address P 1 P 1 300 300 1000 1000 Bound Base Bound Base Register Register Register Register 25 26 MAX sys MAX sys Base & Bound Base & Bound 0 0 P 1 : Base = 1000; Bound = 300 P 1 : Base = 1000; Bound = 300 P 2 : Base = 500; Bound = 400 P 2 500 400 P 2 : Base = 500; Bound = 400 Memory Memory Exception Exception 1000 1000 no no 1150 CPU ≤ + 1300 CPU ≤ + 1300 yes yes Virtual Physical Virtual Physical address address address address P 1 P 2 300 1000 400 500 Context Switch Context Switch Bound Base Bound Base Base & Bound saved in P 1 ’ s PCB Register Register Register Register 27 MAX sys 28 MAX sys
On Base & Bound E Pluribus Unum Contiguous Allocation 0 Program Code 1 contiguous virtual addresses are mapped to Address spaces have structure! Heap 2 contiguous physical addresses An address space comprises But mapping entire address space to physical multiple segments memory contiguous sets of virtual free is wasteful addresses, logically connected lots of free space between heap and stack… heap, code, stack, (and also globals, libraries…) makes sharing hard each segment can be of a does not work if the address space is larger 63KB different size than physical memory Stack 64KB think 64-bit registers… 30 29 Segmentation: Segmentation Generalizing Base & Bound Goal: Supporting large address spaces (while 0KB Base & Bound registers to allowing multiple processes to coexist in memory) each segment free Needed hardware each segment is independently mapped to a two registers (Base and Bound) per segment 28KB set of contiguous addresses Stack Stored in the PCB 31KB in physical memory 32KB Program Code no need to map unused 34KB a segment table, stored in memory, at an address Heap virtual addresses 37KB pointed to by a Segment Table Register (STBR) Segment Base Bound STBR stored in the PCB free Code 32K 2K Heap 34K 3K 64KB Stack 28K 3K (not to scale) 31 32
Recommend
More recommend