Memory Management CMPU 334 – Operating Systems Jason Waterman Memory Virtualization • What is memory virtualization? • OS virtualizes its physical memory • OS provides an illusion of memory space per each process • It is as if each process can access the entire memory space • Benefits • 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 9/16/21 CMPU 335 -- Operating Systems 2 Early OSes 0KB • Load only one process in memory Operating System (code, data, etc.) • Poor utilization and efficiency 64KB Current Program (code, data, etc.) max Physical Memory 9/16/21 CMPU 335 -- Operating Systems 3 Multiprogramming and Time Sharing 0KB • Load multiple processes in memory Operating System (code, data, etc.) • Execute one for a short while 64KB Free • Switch processes between them in memory 128KB Process C • Increase utilization and efficiency (code, data, etc.) 192KB Process B (code, data, etc.) 256KB • Causes an important protection issue Free • 320KB Errant memory accesses from other processes Process A (code, data, etc.) 384KB Free 448KB Free 512KB Physical Memory 9/16/21 CMPU 335 -- Operating Systems 4 Address Space • The OS creates an abstraction of physical 0KB Program Code memory 1KB • The address space contains all the information Heap about a running process 2KB • program code, heap, stack and etc. (free) 15KB Stack 16KB Address Space 9/16/21 CMPU 335 -- Operating Systems 5 Address Space(Cont.) • Code 0KB Program Code • Where instructions live 1KB Heap • Heap 2KB • Dynamically allocate memory • malloc in C (free) • new in Java • Stack • Store return addresses or values 15KB • Contain local variables and arguments to functions Stack 16KB Address Space 9/16/21 CMPU 335 -- Operating Systems 6 Virtual Address • Every address in a running program is virtual • OS translates the virtual address to physical address #include <stdio.h> #include <stdlib.h> // Compile with: gcc -Og -no-pie -o print_address print_address.c int main(int argc, char *argv[]){ int x = 3; // allocated on the stack frame of main() printf("location of code : %p\n", (void *) main); printf("location of heap : %p\n", (void *) malloc(1)); printf("location of stack : %p\n", (void *) &x); return x; // so compiler doesn’t optimize x away } A simple program that prints out addresses 9/16/21 CMPU 335 -- Operating Systems 7 Virtual Address(Cont.) Address Space Lower memory • The output in 64-bit Linux machine Code (Text) Data location of code : 0x4005c7 location of heap : 0x1623670 Heap location of stack : 0x7fff633fbe54 heap (free) stack Stack 0x00007ffxxxxxxxxx 9/16/21 CMPU 335 -- Operating Systems 8 Memory Virtualization Overview • Just like the CPU, memory is virtualized • Every address generated by a user program is a virtual address • Virtual memory provides the illusion of a large private address space to programs • For instructions and data • The OS, with help from the hardware translates each virtual memory address to an actual physical address • Protects and isolates processes from each other • And from the kernel 9/16/21 CMPU 335 -- Operating Systems 9 Memory Virtualizing with Efficiency and Control • Memory virtualizing takes a similar strategy to virtualizing the CPU with limited direct execution (LDE) • Must have efficiency and control • In memory virtualizing, efficiency and control are attained by hardware support • E.g., registers, TLB (Translation Look-aside Buffer), page-table • Control means no process is allowed to access any memory but its own • Desire flexibility • Applications should be allowed to use their address space however they would like • How do we efficiently and flexibly virtualize memory? 9/16/21 CMPU 334 -- Operating Systems 10 Address Translation • Mechanism: hardware-based address translation • Hardware transforms a virtual address to a physical address • The desired code/data is actually stored in a physical address • The OS must get involved at key points to set up the hardware • The OS manages memory • Keeps track of which locations are free and which are in use 9/16/21 CMPU 334 -- Operating Systems 11 Example: Address Translation • C - Language code void func() int x = 3000; x = x + 3; // this is the line of code we are interested in ... • Load a value from memory • Increment it by three • Store the value back into memory 9/16/21 CMPU 334 -- Operating Systems 12 Example: Address Translation(Cont.) • Assembly 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 • Presume that the address of ‘x’ has been place in ebx register • Load the value at that address into eax register • Add 3 to eax register • Store the value in eax back into memory 9/16/21 CMPU 334 -- Operating Systems 13 Example: Address Translation(Cont.) 0KB 128 movl 0x0(%ebx),%eax 132 Addl 0x03,%eax • Fetch instruction at address 128 1KB 135 movl %eax,0x0(%ebx) Program Code • Execute this instruction (load from address 15KB) 2KB 3KB Heap • Fetch instruction at address 132 4KB • Execute this instruction (no memory reference) heap • Fetch the instruction at address 135 • Execute this instruction (store to address 15 KB) (free) stack • 5 memory references just to add 3 to a number! • It is extremely important that address translation is 14KB done efficiently 15KB 3000 Stack 16KB 9/16/21 CMPU 334 -- Operating Systems 14 Relocation Address Space • In this example, from the program’s perspective its address space starts at address 0 and grows to a maximum of 16 KB • All memory references should be within these bounds • The OS wants to place the process somewhere else in physical memory, not at address 0 • How can we provide this illusion? 9/16/21 CMPU 334 -- Operating Systems 15 A Single Relocated Process 0KB 0KB Program Code Operating System 16KB Heap (not in use) 32KB Code Heap heap (allocated (free) but not in use) Stack stack 48KB Relocated Process (not in use) Stack 64KB 16KB Physical Memory Address Space 9/16/21 CMPU 334 -- Operating Systems 16 Base and Bounds Register (Hardware Support) 0KB 0KB • Base and bounds also Program Code called dynamic relocation Operating System 16KB • Bounds register also called Heap limit register (not in use) base register 32KB 32KB Code Heap heap (allocated (free) but not in use) Stack stack 48KB (not in use) bounds register Stack 64KB 16KB Physical Memory 16KB Address Space 9/16/21 CMPU 334 -- Operating Systems 17 Dynamic (Hardware base) Relocation • When a program starts running, the OS decides where in physical memory a process should be loaded • Sets the base register accordingly �ℎ���� ������� = ������� ������� + ���� • Every virtual address must not be greater than bound and negative 0 ≤ ������� ������� < ������ 9/16/21 CMPU 334 -- Operating Systems 18 Relocation and Address Translation 0KB 128 movl 0x0(%ebx),%eax 128 : movl 0x0(%ebx), %eax 132 Addl 0x03,%eax 1KB 135 movl %eax,0x0(%ebx) Program Code 2KB • Fetch instruction at address 128 (virtual address) 3KB Heap 32896 = 128 + 32��(����) 4KB • Execute this instruction heap • Load from address 15KB (virtual address) 47�� = 15�� + 32��(����) (free) stack 14KB 15KB 3000 Stack 16KB 9/16/21 CMPU 334 -- Operating Systems 19 Two ways to check Bounds • Either hold the size 0KB 0KB of the address Program Code Operating System space or the 16KB Heap physical address of (not in use) the end of the 32KB Code address space Heap �������� ������� �� ��� ���� �� ��� ��� �� ������� ������ (allocated ������� ����� (free) but not in use) bounds bounds Stack • Depends on 16KB 48KB 48KB whether you bounds check (not in use) before or after Stack 64KB translation Physical Memory 16KB Address Space 9/16/21 CMPU 334 -- Operating Systems 20 OS Issues for Memory Virtualizing • The OS must take action to implement base-and-bounds approach at three critical junctures: 1. When a process starts running: • Finding space for address space in physical memory 2. When a process is terminated: • Reclaiming the memory for use 3. When context switch occurs: • Saving and storing the base-and-bounds pair 9/16/21 CMPU 334 -- Operating Systems 21 OS Issues: When a Process Starts Running • The OS must find a room for a new address space • free list : A list of the range of the physical memory which are not in use 0KB Operating System The OS looks in the free list 16KB Free list (not in use) 16KB 32KB Code Heap (allocated but not in use) Stack 48KB 48KB (not in use) 64KB Physical Memory 9/16/21 CMPU 334 -- Operating Systems 22 OS Issues: 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) 48KB 32KB 48KB 48KB (not in use) (not in use) 64KB 48KB 64KB Physical Memory Physical Memory 9/16/21 CMPU 334 -- Operating Systems 23 OS Issues: 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 0KB 0KB bounds : 48KB Context Switch … Operating System Operating System 16KB 16KB (not in use) base (not in use) base 32KB 32KB 32KB 48KB Process A Process A Currently Running bounds bounds 48KB 48KB 48KB 64KB Process B Process B Currently Running 64KB 64KB Physical Memory Physical Memory 9/16/21 CMPU 334 -- Operating Systems 24 Address Translation Overview • Extend limited direct execution with address translation • Can control each and every
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages37 Page
-
File Size-