Main Memory 02/04/2021 Professor Amanda Bienz Textbook Pages 349-360
Total Page:16
File Type:pdf, Size:1020Kb
University of New Mexico Main Memory 02/04/2021 Professor Amanda Bienz Textbook pages 349-360 Operating System Concepts - 10th Edition Enhanced eText Portions from Prof. Patrick Bridges’ Slides University of New Mexico Background • Program starts in disk • Brought into memory and placed within a process for it to be run • Register access is done in one CPU clock cycle (or less) • Main memory can take many cycles (hundreds) - causing a stall • Cache : sits between main memory and CPU registers University of New Mexico Memory Virtualization • OS virtualizes physical memory, providing illusion of separate memory space per process • Seems like each process uses entire memory space • Benefits : • Ease of programming • Memory efficiency • Guarantee isolation for user processes and OS (protection) University of New Mexico OS in early eystems University of New Mexico Early¢ LoadOperating only one process in memory.Systems ▪ Poor utilization and efficiency 0KB Operating System (code, data, etc.) 64KB • Load a single process in memory Current • Poor utilization and efficiency Program (code, data, etc.) max Physical Memory 4 University of New Mexico University of New Mexico MultiprogrammingMultiprogramming and and Time Time Sharing Sharing 0KB Operating System ¢ Load multiple processes in memory. (code, data, etc.) • Load multiple processes in memory 64KB ▪ Execute one for a short while. Free Execute one▪ Switchfor short processes time between them in memory. 128KB • Process C ▪ Increase utilization and efficiency. (code, data, etc.) 192KB Switch between processes in memory Process B • (code, data, etc.) 256KB ¢ Cause an important protection issue. Increase utilization and efficiency Free • ▪ 320KB Errant memory accesses from other processes Process A (code, data, etc.) • Protection issue: 384KB Free 448KB • Errant memory accesses from other processes Free 512KB Physical Memory 5 University of New Mexico Address Space ¢ OS creates an abstraction ofUniversity physical of New memory. Mexico ▪ The address space contains all about a running process. Address▪ That is consistSpace of program code, heap, stack and etc. 0KB • OS creates abstraction of physical memory Program Code 1KB • Address space contains all about a running Heap process 2KB • Consist of program code, heap, stack, etc • Text/Data : where instruction and global variables live (free) • Heap : dynamically allocated memory (malloc or new) • Stack : return addresses/values, local variables 15KB • We can print out what the addresses are for each Stack 16KB Address Space 6 University of New Mexico Address Binding • Program resides on disk as a binary executable file • Must be brought into memory and placed within the context of a process • Then, eligible for execution on an available CPU • Process executes - reads instructions and data from memory • When process finishes, memory is reclaimed for other processes University of New Mexico Address Binding • Address binding : mapping from one address space to another • Addresses in source program are typically symbolic • i.e. the variable count • Compiler binds these symbolic addresses to relocatable addresses • i.e. 14 bytes from beginning of this module • Linker or loader binds relocatable addresses to absolute addresses University of New Mexico Address Binding • Address binding of instructions and data to memory addresses can happen at three different stages: • Compile time: If memory location known a priori, absolute code can be generated… must recompile code if starting location changes • Load time: must generate relocatable code if memory location is not known at compile time • Execution time: binding delayed until run time if the process can be moved during its execution from one memory segment to another University of New Mexico Logical vs Physical Address Space • Logical address (virtual address) : generated by CPU • Physical address : address seen by the memory unit • Logical and physical addresses are same in compile-time and load-time address-binding schemes • Differ in execution-time address binding • Logical address space : set of all logical addresses generated by a program • Physical address space : the set of all physical addresses generated by a program University of New Mexico Address Translation • Address spaces are virtual addresses • Must be transparently translated to actual physical memory addresses used by the underlying hardware • Hardware transforms virtual address to a physical address • OS must get involved at key points to set up hardware University of New Mexico University of New Mexico Example:Example Address Address Translation Translation ¢ C - Language code void func() int x; ... x = x + 3; // this is the line of code we are interested in • Example▪ Load in Ca value: from memory ▪ Increment it by three • Load a value from memory ▪ Store the value back into memory • Increment value by 3 • Store value back into memory 3 University of New Mexico University of New Mexico Example: Address Translation(Cont.) Example Address Translation ¢ 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 • ▪ExamplePresume in assembly that the : address of ‘x’ has been place in ebx register. ▪ Load the value at that address into eax register. • Presume that address of ‘x’ has been placed in ebx register ▪ Add 3 to eax register. ▪• StoreLoad valuethe valueat that inaddress eax back into eax into register memory. • Add 3 to eax register • Store value in eax back into memory 4 University of New Mexico University of New Mexico Example:Example Address Translation(Cont.) Address Translation 0KB 128 movl 0x0(%ebx),%eax 132 Addl 0x03,%eax • Fetch instruction at address 128 1KB 135 movl %eax,0x0(%ebx) • Execute this instruction (load from address 15KB) Program Code • Fetch instruction at address 128 2KB • Fetch instruction at address 132 • Execute this instruction (no memory reference) 3KB Heap • Execute this instruction (load from address 15kb) 4KB • Fetch the instruction at address 135 • Execute this instruction (store to address 15 KB) heap • Fetch instruction at address 132 (free) • Execute this instruction stack • Fetch instruction at address 135 14KB • Execution this instruction (store to address 15kb) 15KB 3000 Stack 16KB 5 University of New Mexico Relocation Address Space • OS wants to place process somewhere else in physical memory (not at address 0) • Virtual memory can start at address 0, but correspond to different location in physical memory University of New Mexico Mapping Physical to Virtual Memory • Memory-Management Unit (MMU) : hardware device that at run time maps virtual to physical address • Many methods possible for doing this University of New Mexico Relocated ProcessUniversity of New Mexico 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 Process Relocated (not in use) Stack 64KB 16KB Physical Memory Address Space 7 University of New Mexico University of New Mexico Base and Bounds Register Base and¢ Simple Idea Limit 1: Base to offset Registers virtual to physical, bound to limit access within the address space itself 0KB 0KB Program Code Operating System 16KB Must ensure process can Heap • (not in use) base register only access addresses in it’s 32KB 32KB Code address space Heap heap (allocated • Provide this protection using (free) but not in use) Stack base and limit (bounds) stack 48KB registers (not in use) bounds register Stack 64KB 16KB Physical Memory 16KB Address Space 8 University of New Mexico Base and Limit Registers • CPU checks every memory access generated in user mode to ensure it’s between base and limit • What operation is privileged (requires kernel mode)? University of New Mexico Base and Limit Registers • CPU checks every memory access generated in user mode to ensure it’s between base and limit • What operation is privileged (requires kernel mode)? Loading base and limit registers University of New Mexico Dynamic (Hardware Base) Relocation • When a program starts running, OS decides where in physical memory a process should be loaded • Set base register : • physical address = virtual address + base • Every virtual address must not be great than bound or negative: • 0 <= virtual address < bounds University of New Mexico Memory-Management Unit (MMU) • Relocation register : the value is added to every address generated by a user process at the time it is sent to memory • User program deals with logical addresses (never sees real physical addresses) University of New Mexico Hardware Support for Relocation University of New Mexico Relocation and Address TranslationUniversity of New Mexico 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 3KB ▪ Fetch instruction at address 128 Heap 4KB 32896 = 128 + 32��(����) ▪ Execute this instruction heap ▪ Load from address 15KB 47�� = 15�� + 32��(����) (free) stack 14KB 15KB 3000 Stack 16KB 10 University of New Mexico Dynamic Loading • Entire program does not need to be in memory to execute • Routine is not loaded until it is called • Better memory-space utilization : unused routine is never loaded • All routines kept on disk in relocatable load format • When is this useful? University of New Mexico Dynamic Loading • Entire program does not need to be in memory to execute • Routine is not loaded until it is called • Better memory-space utilization