Presentation is loading. Please wait.

Presentation is loading. Please wait.

Virtualization Virtualize hardware resources through abstraction CPU

Similar presentations


Presentation on theme: "Virtualization Virtualize hardware resources through abstraction CPU"— Presentation transcript:

1 Virtualization Virtualize hardware resources through abstraction CPU
Abstraction: Process Mechanism: Limited direct execution Efficient – process runs directly on the CPU Hardware support for traps, context switching Control – User mode vs. kernel mode Timer interrupts to regain control from a process Policies: Scheduling MLFQ Minimize turnaround time and response time

2 Memory Virtualization
What is memory virtualization? OS virtualizes its physical memory. OS provides an illusion of a unique memory space per process. It seems to the process that it can use the whole memory . Transparent to the process

3 Benefits of Memory Virtualization
Ease of use in programming Memory efficiency in terms of time and space The guarantee of isolation for processes as well as OS Protection from errant accesses of other processes

4 Start Simple: Early OS Load only one process in memory.
Poor utilization and efficiency 0KB 64KB max Operating System (code, data, etc.) Current Program Physical Memory

5 Multiprogramming and Time Sharing
0KB Operating System (code, data, etc.) Load multiple processes in memory. Execute one for a short time. Next process to execute is already in memory Increase utilization and efficiency. Protection issues Errant memory accesses from other processes 64KB Free 128KB Process C (code, data, etc.) 192KB Process B (code, data, etc.) 256KB Free 320KB Process A (code, data, etc.) 384KB Free 448KB Free 512KB Physical Memory

6 Address Space OS creates an abstraction of physical memory.
Contains all of a running process’s memory Program code, heap, stack and etc. 0KB Program Code (free) 1KB 2KB 15KB 16KB Heap Stack Address Space

7 Address Space(Cont.) Code Heap Stack Instructions of the program
Program Code (free) Heap Stack Code Instructions of the program Heap Dynamically allocated memory malloc in C language Stack Store return addresses or values Local variables Passed parameters Address Space

8 Virtual Address Every address in a running program is virtual
OS translates the virtual address to physical address #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]){ printf("location of code : %p\n", (void *) main); printf("location of heap : %p\n", (void *) malloc(1)); int x = 3; printf("location of stack : %p\n", (void *) &x); return x; } A simple program that prints out addresses

9 Virtual Address(Cont.)
Address Space 0x400000 Code (Text) Output in 64-bit Linux machine 0x401000 Data 0xcf2000 Heap 0xd13000 (free) location of code : 0x40057d location of heap : 0xcf2010 location of stack : 0x7fff9ca45fcc heap stack 0x7fff9ca28000 Stack 0x7fff9ca49000

10 Address Translation Memory virtualization goals:
Efficiency – fast address translation Control – process isolation Attained via hardware support Registers TLBs Page-table

11 Address Translation Hardware translates a virtual address to a physical address. The desired information is actually stored in a physical address. The OS must manage hardware

12 Example: Address Translation
C - Language code Load a value from memory Increment it by three Store the value back into memory void func() int x; ... x = x + 3; // this is the line of code we are interested in

13 Example: Address Translation(Cont.)
Assembly Address of ‘x’ has been placed in ebx Load the value at that address into eax Add 3 to the value in eax Store the value in eax back into memory 128 : movl 0x0(%ebx), %eax ; load 0+ebx into eax 132 : addl $0x03, %eax ; add 3 to eax register 135 : movl %eax, 0x0(%ebx) ; store eax back to mem

14 Example: Address Translation(Cont.)
(free) 3000 Stack stack heap Heap 14KB Program Code 16KB 15KB 0KB 1KB 2KB 3KB 4KB 128 132 135 movl 0x0(%ebx),%eax Addl 0x03,%eax movl %eax,0x0(%ebx) Fetch instruction at address 128 Execute this instruction (load from address 15KB) Fetch instruction at address 132 Execute this instruction (no memory reference) Fetch the instruction at address 135 Execute this instruction (store to address 15 KB)

15 Example: Address Translation(Cont.)
(free) 3000 Stack stack heap Heap 14KB Program Code 16KB 15KB 0KB 1KB 2KB 3KB 4KB 128 132 135 movl 0x0(%ebx),%eax Addl 0x03,%eax movl %eax,0x0(%ebx) Fetch instruction at address 128 Execute this instruction (load from address 15KB) Fetch instruction at address 132 Execute this instruction (no memory reference) Fetch the instruction at address 135 Execute this instruction (store to address 15 KB) Address Translation happens all the time! It must be efficient.

16 Location of Address Spaces in Physical Memory
Each process’s address space starts at 0 Physical memory starting at 0 is generally reserved for the OS So…A process’s address space must be located somewhere else in physical memory

17 (allocated but not in use)
A Single Process 0KB 0KB Program Code Operating System 16KB Heap (not in use) (free) 32KB Relocated Process Code Heap heap (allocated but not in use) stack 48KB Stack (not in use) Stack 64KB 16KB Physical Memory Address Space

18 Base and Bounds Register
0KB 0KB Program Code Operating System 16KB Heap (not in use) base register (free) 32KB 32KB Code Heap heap (allocated but not in use) 48KB Stack stack (not in use) Stack bounds register 64KB 16KB Physical Memory 16KB Address Space

19 Dynamic (Hardware base) Relocation
When a program starts running, the OS decides where in physical memory a process should be loaded. Set the base register to that value. Control through isolation Every virtual address must in [base, base + bound) Efficiency Simple translation: Paddr = base + Vaddr

20 Relocation and Address Translation
0KB 128 132 135 movl 0x0(%ebx),%eax Addl 0x03,%eax movl %eax,0x0(%ebx) Program Code 128 : movl 0x0(%ebx), %eax 1KB Fetch instruction at address 128 Execute Load from address 15KB 2KB Heap 3KB 4KB (free) heap stack 14KB 3000 Stack Base: 32KB Bounds: 16KB 15KB 16KB

21 Bounds register as end of interval
0KB 0KB Program Code Operating System 16KB Heap (not in use) (free) 32KB Code Heap (allocated but not in use) bounds bounds 16KB 48KB Stack 48KB (not in use) Stack 64KB Physical Memory 16KB Address Space

22 OS Responsibilities The OS must take action to implement base-and-bounds approach. Three critical junctures: When a process starts running: Find space for the address space in physical memory When a process is terminated: Reclaim the memory for reuse When context switch occurs: Save and store the base-and-bounds pair Restore the next process’s base-and-bounds pair

23 When a Process Starts Running
The OS must find room for the new address space. free list : Chunks of physical memory not in use 0KB Operating System 16KB Free list (not in use) 16KB 32KB Code Heap (allocated but not in use) 48KB 48KB Stack (not in use) 64KB Physical Memory

24 When a Process Is Terminated
The OS must put the memory back on the free list. 0KB Free list 0KB Free list Operating System Operating System 16KB 16KB 16KB 16KB (not in use) (not in use) 32KB 32KB Process A (not in use) 32KB 48KB 48KB 48KB (not in use) (not in use) 48KB 64KB 64KB Physical Memory Physical Memory

25 When Context Switch Occurs
The OS must save and restore the base-and-bounds pair. In process structure or process control block(PCB) Process A PCB base : 32KB bounds : 48KB … 0KB Operating System Context Switching 0KB Operating System 16KB 16KB (not in use) (not in use) base base 32KB 48KB 32KB 32KB Process A Currently Running Process A bounds bounds 48KB 64KB 48KB 48KB Process B Process B Currently Running 64KB 64KB Physical Memory Physical Memory

26 Inefficiency of the Base and Bound
(free) 14KB Program Code 16KB 0KB 2KB 4KB Heap Stack 6KB 15KB 5KB 3KB 1KB Big chunk of “free” space “free” space takes up physical memory. Hard to run when an address space does not fit into physical memory

27 Segmentation Segment is a contiguous portion of the address space
Logically-different segments: code, stack, heap Each segment can be placed in a different part of physical memory Base and bounds exist per segment.

28 Placing Segments In Physical Memory
0KB Operating System 16KB (not in use) Segment Base Size Code 32K 2K Heap 34K 2K Stack 28K 2K Stack (not in use) 32KB Code Heap (not in use) 48KB 64KB Physical Memory

29 Address Translation on Segmentation
The offset of the virtual address 100 is 100. The code segment starts at virtual address 0 in address space. Segment Base Size Code 32K 2K 16KB (not in use) 0KB 2KB Program Code 4KB 32KB Location in Phys. memory Code 100 instruction 34KB Heap (not in use)

30 Address Translation: Segmentation
The offset of the virtual address 4200 is 104. The heap segment starts at virtual address 4096 in address space. Segment Base Size Heap 34K 2K (not in use) 6KB Heap 4KB 32KB Code 34KB Location in Phys. memory 4200 data Heap 36KB (not in use) Address Space Physical Memory

31 Address Translation: Segmentation
The offset of the virtual address 4200 is 104. The heap segment starts at virtual address 4096 in address space. Must use offset into the segment for address translation!! Segment Base Size Heap 34K 2K (not in use) 6KB Heap 4KB 32KB Code 34KB Location in Phys. memory 4200 data Heap 36KB (not in use) Address Space Physical Memory

32 Segmentation Fault or Violation
If an illegal address (ex. 7KB) which is beyond the end of heap is referenced, the OS issues a segmentation fault The hardware detects that address is out of bounds. 4KB Heap 6KB (not in use) 7KB 8KB Address Space

33 Referring to Segment Explicit approach
Chop up the address space into segments based on the top few bits of virtual address. Example: virtual address 4200 ( ) 13 1 12 2 11 3 10 4 9 8 7 6 5 Segment Offset Segment bits Code 00 Heap 01 Stack 10 - 11 13 1 12 2 11 3 10 4 9 8 7 6 5 Segment Offset

34 Referring to Segment(Cont.)
SEG_MASK = 0x3000( ) SEG_SHIFT = 12 OFFSET_MASK = 0xFFF ( ) 1 // get top 2 bits of 14-bit VA 2 Segment = (VirtualAddress & SEG_MASK) >> SEG_SHIFT 3 // now get offset 4 Offset = VirtualAddress & OFFSET_MASK 5 if (Offset >= Bounds[Segment]) 6 RaiseException(PROTECTION_FAULT) 7 else 8 PhysAddr = Base[Segment] + Offset 9 Register = AccessMemory(PhysAddr)

35 Referring to Stack Segment
Stack grows backward Extra hardware support is needed The hardware checks which way the segment grows. 1: positive direction, 0: negative direction (not in use) Segment Base Size Grows Positive? Code 32K 2K Heap 34K 2K Stack 28K 2K 26KB Stack 28KB (not in use) Segment Register(with Negative-Growth Support) Physical Memory

36 Support for Sharing Segments can be shared between address spaces
Code sharing is still in use in systems today. Extra hardware support is need for form of Protection bits A few more bits per segment to indicate permissions of read, write and execute. Segment Base Size Grows Positive? Protection Code 32K 2K Read-Execute Heap 34K 2K Read-Write Stack 28K 2K Read-Write Segment Register Values (with Protection)

37 Fine-Grained and Coarse-Grained Segmentation
Coarse-Grained – small number of segments e.g., code, heap, stack. Fine-Grained – many different segments To support fine-grained segmentation, hardware support is required

38 OS support: Fragmentation
External Fragmentation: little holes of free space in physical memory There is 24KB free, but not in one contiguous segment The OS cannot satisfy a 24KB request Compaction: rearranging the existing segments in physical memory Compaction is costly Stop running process Copy data somewhere else Change segment register values

39 Memory Compaction Not compacted Compacted 0KB 0KB Operating System
(not in use) Allocated 24KB 24KB Allocated 32KB 32KB (not in use) Allocated 40KB 40KB (not in use) (not in use) 48KB 48KB 56KB 56KB Allocated 64KB 64KB

40 Next: Paging


Download ppt "Virtualization Virtualize hardware resources through abstraction CPU"

Similar presentations


Ads by Google