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 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

• Benefts :

• Ease of programming

• Memory efciency

• 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 (code, data, etc.) 64KB

• Load a single process in memory

Current • Poor utilization and efciency 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 efciency 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 fle

• 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 fnishes, 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 diferent 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 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

• Difer 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)

can start at address 0, but correspond to diferent 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 : unused routine is never loaded

• All routines kept on disk in relocatable load format

• When is this useful? When large amounts of code are needed to handle infrequently occurring cases University of New Mexico Dynamic Linking

• Static Linking - system libraries and program code combined by the loader into the binary program image

• Dynamic linking - linking postponed until execution

• Small piece of code (stub) used to locate the appropriate library routine in memory

• Stub replaces itself with the address of the routine and executes

• OS checks if routine is in process’s memory (if not, add to address space)

• Shared library - can be shared between many processes

• How is it possible to shared library if it resides in other process’s memory? University of New Mexico Contiguous Allocation

• Main memory must support both OS and user processes

• Limited resource - must be allocated efciently… contiguous allocation is one early method

• Main memory : usually into two partitions:

• Resident operating system, usually held in low memory with vector

• User processes : held in high memory

• Each process contained in single contiguous section of memory University of New Mexico Contiguous Allocation (Cont.)

• Relocation registers used to protect user processes from each other, and from changing OS code and data

• Base register contains value of smallest physical address

• Limit register contains range of logical addresses - each logical address must be less than the limit register

• MMU maps logical address dynamically University of New Mexico Memory API : malloc()

• Main programmer-level API in C

• Language-level interface (not actual OS interface)

• Allocate a memory region on the heap

• Argument: Unsigned Integer

• size_t size : size of memory block (int bytes)

• Return:

• Success : a void type pointer to the memory block allocated by malloc

• Fail : a null pointer

• Similar methods : free, calloc, realloc University of New Mexico Many ways to go wrong with memory

• What are some issues you have had with memory in the past? University of New Mexico Many ways to go wrong with memory

• Not copying enough data (terminating nulls)

• Not allocating space for the data copied

• Not freeing data

• Accessing data after its freed

• Freeing an area multiple times

• What actually happens in each case? Requires understanding of how C API is built on top of OS memory API University of New Mexico University of New Mexico MemoryMemory ManagementManagement System System Calls Calls #include

int brk(void *addr) void *sbrk(intptr_t increment); • Malloc library call uses brk and mmap system calls

• brk malloc: called to expand the program’s break malloc library call use brk and/or mmap system calls. • Break : the location of the end of the heap in address space ▪ brk is called to expand the program’s break. • sbrk : additional▪ break call: The similar location to brk of the end of the heap in address space ▪ sbrk is an additional call similar with brk. • Programmers should never directly call either brk or sbrk ▪ Programmers should never directly call either brk or sbrk. • What does this method actually do? What does this actually do?

4 UniversityUniversity of of NewNew MexicoMexico AboutHow do brk brkand sbrkandwork? sbrk Address Space The greyed-out area of an address 0x400000 Code space is not actually allocated. (Text) 0x401000 To use it, the OS has to make it Data 0xcf2000 • Greyed-out area isavailable. not actually allocated Heap 0xd13000 Brk/sbrk sets the location of the To use it, OS has to make it available • boundary between allocated heap heap memory and unallocated memory! The methods brk/sbrk set the location of the • (free) boundary between allocated heap memory and

unallocated memory stack

0x7fff9ca28000 Stack 0x7fff9ca49000

5 UniversityUniversity of New of New Mexico Mexico System SystemCalls(Cont.) Calls (Cont.)

#include

void *mmap(void *ptr, size_t length, int port, int flags, int fd, off_t offset)

▪ • The systemmmap systemcall mmap call createscan create an anonymousan anonymous memory memory region region.

6 University of New Mexico

What about mmap? University of New Mexico

Address Space mmap lets the program request finer- 0x400000 About mmap Code grain allocation of parts of its address (Text) 0x401000 space Data 0xcf2000 • The system Morecall mmap than letsjust the moving program the program Heap request fne-grainbreak allocation of parts of its 0xd13000 address space Note that the address space is now heap • More than justdisjoint! moving the program break mmap region mmap can also do implicit file I/O – (free) • Note that thethat’s address a later space topic… is now disjoint stack • Mmap can do other methods (implicit fle I/O) that we will talk about later 0x7fff9ca28000 Stack 0x7fff9ca49000

7 University of New Mexico Malloc/New Implementation • Language runtime (libc) gets memory in chunks from OS

• Uses mmap or sbrk to get chunks for 4k bytes or more at a time

• Why not smaller pieces?

• Language runtime divides these big blocks up to satisfy malloc/new requests

• Basic data structure is a ‘free list’ (linked list of free chunks of memory)

• Malloc/new search list to fnd a chunk that satisfy an allocation request

• Free returns things to this linked list University of New Mexico OS Issues for Virtualizing Memory

• OS mus take action to implement base-and-bounds approach

• Three critical junctures :

• When a process starts running :

• Finding space for virtual address space in physical memory

• When a process is terminated :

• Reclaiming the memory for use

• When occurs :

• Saving and storing base and bounds pair University of New Mexico

OS Issues : When a process startsUniversity running of New Mexico OS Issues: When a Process Starts • OS must fnd aRunning room for the new address space • Free list : a listThe of the OS range must offind physical a room memory for a new which address are not space. in use ▪ free list : A list of the range of the physical memory which are not in use. 0KB Operating System The OS lookup 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

13 University of New Mexico University of New Mexico OS Issues: When a Process Is OSTerminated Issues : When a Process is Terminated • TheThe OS OS mustmust putput thethe memory memory back back in on the the free free list 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

14 University of New Mexico University of New Mexico

OSOS Issues: Issues When Context Switch : OccursContext Switch

• OS must save and restore base and bounds pair The OS must save and restore the base-and-bounds pair. • Saved in ▪processIn process structure structure of process or process control control block block( (PCB)PCB) Process A PCB … base : 32KB 0KB 0KB bounds : 48KB Context Switching … 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

15 University of New Mexico

University of New Mexico Why not just Base and Bound?

0KB Segmentation 1KB Program Code Big chunk of “free” space 2KB 3KB “free” space takes up physical memory. 4KB Hard to run when an address space does not 5KB Heap fit into physical memory 6KB

• Why not just base and bound? (free)

14KB 15KB Stack 16KB 2 University of New Mexico

University of New Mexico Why not just Base and Bound?

0KB Segmentation 1KB Program Code Big chunk of “free” space 2KB 3KB “free” space takes up physical memory. 4KB Hard to run when an address space does not 5KB Heap fit into physical memory • Why not just base and bound? 6KB

• Large chunk of free space

• Free space takes up physical memory (free)

• Hard to run when an address space does not ft into physical memory 14KB 15KB Stack 16KB 2 University of New Mexico Segmentation

• A contiguous portion of the address space of a particular length

• Logically-diferent segment : code, stack, heap

• Each segment can be placed in diferent part of physical memory

• Base and bounds exist per each segment University of New Mexico University of New Mexico PlacingPlacing Segment Segment In Physical in Physical Memory Memory

0KB

Operating System

16KB (not in use) Segment Base Size Stack Code 32K 2K (not in use) Heap 34K 2K 32KB Code Heap Stack 28K 2K

48KB (not in use)

64KB Physical Memory

4 University of New Mexico Example

• Consider program that is separated into two parts : code and data

• CPU knows whether it wants an instruction or data

• Two base-limit register pairs, one for each segment

• Can either of these be read only?

• What are the benefts of this scheme?

• What are the disadvantages? University of New Mexico Variable Partition

• Multiple-partition allocation

• Variable-partition sizes for efciency

• Hole - block of available memory

• Holes of various size scattered throughout memory

• When a process arrives, it is allocated memory from a hold large enough to accommodate it

• Process exiting frees its partition

• Adjacent free partitions can be combined

• OS maintains info about allocated partitions and free partitions University of New Mexico Dynamic Storage-Allocation Problem

• How to satisfy a request of size n form a list of free holes?

• First ft : allocate the frst hole that is big enough

• Best ft : allocate the smallest hole that is big enough

• Must search entire list unless ordered by size

• Produces smallest leftover hold

• Worst ft : allocate the largest hole

• Must search entire list unless ordered

• Produces largest leftover hole

• Which is best/worst for speed? For storage utilization? University of New Mexico Example

• Six partitions (in order) : 300KB, 600KB, 350KB, 200KB, 750KB, 125KB

• Processes to be placed (in order): 115KB, 500KB, 358KB, 200KB, 375KB University of New Mexico Address Translation on Segmentation University of New Mexico �ℎ������ ������� = ������ + ���� Address Translation on Segmentation The offset of virtual address 100 is 100. • Physical▪ The address code segment = ofset starts + base at virtual address 0 in address space.

Segment Base Size 16KB Code 32K 2K (not in use)

0KB 32KB ��� + ��� �� ����� 100 instruction Code is the desired Program Code physical address 2KB 34KB Heap 4KB

(not in use)

5 University of New Mexico Address Translation on Segmentation

• Is physical address = virtual address + base? University of New Mexico

Address Translation on Segmentation(Cont.)

University of New Mexico ������� ������� + ���� is not the correct physical address. Address Translation on Segmentation The offset of 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)

32KB Code 4KB 34KB ��� + ��� �� ����� 4200 data Heap is the desired Heap physical address 6KB 36KB

(not in use) Address Space

Physical Memory

6 University of New Mexico or Violation

If an illegal address such as 7KB which is beyond the end of heap is referenced, the OSUniversity occurs of New segmentation Mexico fault. Segmentation▪ The hardware detects Fault that address is out of bounds.

• If an illegal address such as 7KB which 4KB is beyond end of heap is referenced, OS Heap occurs segmentation fault 6KB 7KB • Hardware detects address is out of (not in use) bounds 8KB

Address Space

7 University of New Mexico University of New Mexico ReferringReferring to Segment to Segment

Explicit approach ▪ Chop up the address space into segments based on the top few bits of virtual address. 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Segment Offset

Example: virtual address 4200 (01000001101000)

Segment bits 13 12 11 10 9 8 7 6 5 4 3 2 1 0 Code 00 0 1 0 0 0 0 0 1 1 0 1 0 0 0 Heap 01 Stack 10 - 11 Segment Offset

8 University of New Mexico University of New Mexico Referring to Segment Referring to Segment(Cont.) 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)

▪ SEG_MASK = 0x3000(11000000000000) ▪ SEG_SHIFT = 12 ▪ OFFSET_MASK = 0xFFF (00111111111111)

9 University of New Mexico University of New Mexico ReferringReferring to Stackto Stack Segment Segment

• Stack grows backwards Stack grows backward. • Extra hardware support is needed for this Extra hardware support is need. • Hardware checks▪ The which hardware way segment checks grows which way the segment grows. • 1 : positive direction,▪ 1: positive 0: negative direction, direction 0: negative direction

Segment Register(with Negative-Growth Support)

Segment Base Size Grows Positive? (not in use) Code 32K 2K 1 Heap 34K 2K 1 26KB Stack 28K 2K 0 Stack 28KB (not in use)

Physical Memory

10 University of New Mexico “Half of Operating Systems is Stupid Tricks” - Prof. Bridges

• Now : multiple processes, each with own address space

• Lots of optimization opportunities and subtle questions

• How many copies of libc exist in memory of system at once?

• What is we want to run more programs than we have physical memory?

• Can physical memory be in multiple segments at same time? University of New Mexico Support for Sharing

• Segment can be shared between address spaces

• Code sharing : still in use in systems today (shared libraries)

• Need extra hardware support

• Extra hardware support is needed for protection bits University of New Mexico Support for Sharing A few more bits per segment to indicate permissions of read, write, execute • Segment can be shared between address space. ▪ Code sharing is still in use in systems today (shared libraries, etc.) ▪ Needs extra hardware support.

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 Register Values(with Protection) Segment Base Size Grows Positive? Protection Code 32K 2K 1 Read-Execute Heap 34K 2K 1 Read-Write Stack 28K 2K 0 Read-Write

Who maintains these bits?

12 University of New Mexico How many segments should we have?

• Coarse-grained (few segments) means segmentation in a small number of segments

• Code, head, stack

• Relatively easy to manage

• Fine grained (lots of segments) allows more fexibility for ‘stupid’ OS tricks

• OS can do lots of things with lots of segments

• Map multiple diferent shared libraries into multiple processes

• OS has to manage allocation of all these segments

• Typically supported with a hardware segment table University of New Mexico Fragmentation

• External Fragmentation : unused memory between partitions

• As processes are loaded and removed from memory, free memory space is broken into pieces

• Example : There are 24KB free, but not in one contiguous segment

• OS cannot satisfy 20KB request

• Internal Fragmentation : unused memory that is internal to a partition

• 50 Percent rule : for frst ft, given N blocks allocated, 0.5 N blocks lost to fragmentation (1/3 may be unusable) University of New Mexico Fragmentation (Cont.)

• Hole size : 18,464 bytes

• Requested allocation size : 18,462 bytes

• If allocated exact size, hole will be 2 bytes … overhead with keeping track of hole is not worth it

• Solution : break physical memory into fxed-size blocks and allocate memory based on block size University of New Mexico Compaction

• Reduce external fragmentation by :

• Shufing memory contents to place all free memory together in one large block

• Only possible if relocation is dynamic and is done at execution time

• Can be expensive to move address spaces to make larger holes

• Could permit logical address space to be noncontiguous (paging)

• Will discuss this later University of New Mexico University of New Mexico MemoryMemory Compaction Compaction

Not compacted Compacted 0KB 0KB

8KB Operating System 8KB Operating System

16KB 16KB (not in use) 24KB 24KB Allocated Allocated 32KB 32KB (not in use)

40KB Allocated 40KB

48KB 48KB (not in use) (not in use) 56KB 56KB Allocated 64KB 64KB

15 University of New Mexico Whence Segmentation

• Segmentation is variable length allocation

• Just like malloc free lists, with many of the same problems

• Useful and fexible, but hard to manage well

• Particularly hard when lots of segments

• Modern OS make only very limited use of segmentation

• 32-bit mode x86 : can use segments extensively, but most OS (Windows/Linux) don’t

• 64-bit mode x86 forces most segments to have base address of 0

• Usually used for -specifc data University of New Mexico For Next Class

• We will start talking about paging

• Class reading : Pg. 260 - 276