Top Banner
Main Memory CISC3595, Fall 09 1
67

Main Memory CISC3595, Fall 09 1. Memory Management Background Contiguous Memory Allocation Paging Structure of the Page Table Segmentation

Dec 17, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Main Memory

CISC3595, Fall 09

1

Page 2: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Memory Management

Background Contiguous Memory Allocation Paging Structure of the Page Table Segmentation Example: The Intel Pentium

2

Page 3: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Background

Main memory and registers are only storage CPU can access directly Register access: one CPU clock (or less) Main memory access: many CPU cycles Cache sits between main memory and CPU

registers Memory is cheap today, and getting

cheaper But applications are demanding more and

more memory, there is never enough!

3

Page 4: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Review: Logic View of a Computer

4

Main memory:a large array of words or bytes. Each words has its own address.

Page 5: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Review: Main (Primary) Memory

5

Main memory – the only large storage media that CPU can access directly Capacity: Bandwidth

Peak: one word per bus cycle

CPU access main memory directly through local bus, i.e., system bus

Page 6: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Review: Performance of different storage

6

ns: nanosecond (10-9 seconds), 1 billionth second

< 16 GB

Page 7: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Program execution

Program must be brought (from disk) into memory and placed within a process for it to be run A program: a sequence of instructions stored in memory (main

memory or disk)

Multiprogramming: multiple processes are loaded into memory to run simultaneously Increase CPU utilization

Swapping: a process might be temporarily swapped to disk, and reloaded back into memory

7

Page 8: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

The need for memory management Memory Management, involves swapping

blocks of data from secondary storage Bring processes into main memory for execution

by processor Protection of memory required to ensure correct

operation Memory needs to be allocated to ensure a

reasonable supply of ready processes to consume available processor time

8

Page 9: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Memory Management Requirements Relocation Protection Sharing Logical organisation Physical organisation

9

Page 10: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Requirements: Relocation Programmer does not know where the

program will be placed in memory when it is executed it may be swapped to disk and return to main

memory at a different location (relocated)

Memory references in the program must be translated to actual physical memory address

10

Page 11: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Multistep Processing of a User Program

11

Programmer’s view of a program/process?

Page 12: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Multistep Processing of a User Program

12

Compiler seldom knows where anobject will reside, it assumes a fixed base location (for example, zero).

Page 13: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Multistep Processing of a User Program

13

Linker/Linkage editor* combines multiple objectfiles into a executable program, resolving symbols as it goes along.• arranges objects in a program's address space. Relocating code that assumes a specific base addressto another base

Page 14: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Dynamic Linking

Linking postponed until execution time Small piece of code, stub, used to locate

appropriate memory-resident library routine Stub replaces itself with the address of the

routine, and executes routine Operating system needed to check if routine

is in other processes’ memory address Dynamic linking is particularly useful for

libraries also known as shared libraries

14

Page 15: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Multistep Processing of a User Program

15

Loader start up programs: read executable files into memory, carry out other required preparatory tasks so that the process can run

Unix loader, i.e., handler for execve() :* validation (permissions, memory requirements etc.);•Copy program image from disk into memor•Copy command-line arguments on stack• Initialize registers (e.g., stack pointer)• Jump to program entry point (_start)

Page 16: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Dynamic Loading Mechanism Allow a program to load, execute and unload

library (or binary code) at run time A program, at runtime,

loads a library (or other binary) into memory retrieves addresses of functions and variables

contained in the library executes those functions or access those

variables unloads library from memory.

Better memory-space utilization: unused routine is never loaded Useful when large amounts of code are needed

to handle infrequently occurring cases16

Page 17: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Dynamic Loading (cont’d) Standard POSIX/UNIX API

#include <dlfcn.h> Loading the library: dlopen() Extracting contents: dlsym() Unload: dlclose()

Microsoft Windows API #include <windows.h> LoadLibrary:LoadLibraryEx Extracting contents: GetProcAddress Unloading the library: FreeLibrary

17

Page 18: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Address Binding

A program must be brought to main memory. instructions that use addresses in a program,

must be bound to proper address space in main memory

Address binding: a mapping from one address space to another Complier: bind symbolic (variable names) to

relocatable address, i.e., 14 byte from beginning of this module

Linker: bind relocatable addresse to absolute address (74014) space of a process address space

18

Page 19: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Final Address Binding

Final address binding: the final mapping of instructions/data to physical memory address 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. Need hardware support for address maps (e.g., base and limit registers)

19

Page 20: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Logical vs. Physical Address Space

Logical address – generated by CPU; also referred to as virtual address User program deals with logical addresses; it

never sees the real physical addresses Physical address – address seen by

memory unit Memory-management Unit (MMU): hardware

for run time mapping from logical address space to a physical address space Many different schemes for the mapping First let’s see an simple example…

20

Page 21: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Memory-Management Unit: an example

Value in relocation register is added to every address generated by a user process at the time it is sent to memory

Example of a simple MMU scheme21

Page 22: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Requirements: Protection Processes should not be able to reference

memory locations in another process without permission Unless special arrangement is made, e.g., shared

memory segment is setup Memory protection checking

performed at run time Impossible to check absolute addresses to ensure protection

at compile time Program might calculate address at run time, e.g., access

array element, pointer to a data structure By the processor (hardware)

OS cannot anticipate all memory reference of a program Too time consuming if done by software

22

Page 23: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Address protection: base and limit registers

23

Page 24: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Requirements: Sharing Allow several cooperating processes to access

same portion of memory Allow multiple processes running same

program to access same copy of the program rather than have their own separate copy

24

Page 25: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Requirements: Logical Organization Programs are written in

modules Modules can be written and

compiled independently Different degrees of

protection given to modules: read-only, readable/writable

Share modules among processes

OS and hardware provide support for deal with such requirement through segmentation

25

Page 26: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Requirements: Physical Organization Keep track of available memory blocks, allocate

and reclaim them as processes come and go… Cannot leave programmer with the

responsibility to manage memory Memory available for a program plus its data may

be insufficient Overlaying: program and data are organized in such a

way that various modules can be assigned same region of memory, main program responsible for switching modules in and out => costly for programmer

Programmer does not know how much space will be available, and where

26

Page 27: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Roadmap: Memory Allocation Schemes Contiguous Allocation Paging Segmentation

27

Page 28: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Contiguous Allocation

Main memory usually divided into two partitions: Resident operating system, usually held in low memory

with interrupt vector User processes held in high memory

Relocation registers used to protect user processes from each other, and from changing operating-system 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

28

Page 29: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Contiguous Allocation (Cont.) Multiple-partition allocation

Hole – block of available memory; holes of various size are scattered throughout memory

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

Operating system maintains information about:a) allocated partitions b) free partitions (hole)

OS

process 5

process 8

process 2

OS

process 5

process 2

OS

process 5

process 2

OS

process 5

process 9

process 2

process 9

process 10

29

Page 30: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Dynamic Storage-Allocation Problem

First-fit: Allocate the first hole that is big enough

Best-fit: Allocate the smallest hole that is big enough; must search entire list, unless ordered by size Produces the smallest leftover hole

Worst-fit: Allocate the largest hole; must also search entire list Produces the largest leftover hole

How to satisfy a request of size n from a list of free holes

First-fit and best-fit better than worst-fit in terms of speed and storage utilization

30

Page 31: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Fragmentation problem External Fragmentation – total memory space

exists to satisfy a request, but it is not contiguous Internal Fragmentation – allocated memory may

be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used

Reduce external fragmentation by compaction Shuffle memory contents to place all free memory

together in one large block Possible only if relocation (final binding to memory

address) is done at execution time To solve external fragmentation problem

Allocate noncontiguous memory to process Paging, Segmentation, and Combined

31

Page 32: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Paging

Physical address space of a process can be noncontiguous process is allocated physical memory wherever it is available Logical address space of a process is still contiguous

Physical memory divided into fixed-sized blocks called frames Frame size is power of 2, between 512 bytes and 8,192 bytes

Logical memory divided into blocks of same size called pages

System keeps track of all free frames To run a program of size n pages, need to find n free frames Set up a page table for logical => physical addresses translation

Paging scheme address external fragmentation internal fragmentation is still possible

32

Page 33: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Paging Model of Logical and Physical Memory

33

A process’s page table: for logical => physical addresses translation * contains base address of each page in physical memory

Page 34: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Logic address under Paging scheme

Logical address (address generated by CPU) is divided into: Page number (p) –an index into a page table

which contains base address of each page in physical memory

Page offset (d) – relative address (within page), combined with base address to define physical memory address that is sent to the memory unit

For given logical address space 2m and page size 2n

page number page offset

p d

m - n n

34

Page 35: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

MMU: Paging

35

Page 36: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Paging Example

32-byte memory and 4-byte pages

36

Page 37: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Free Frames

Before allocation After allocation

37

Page 38: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Implementation of Page Table Where to store the page tables ?

Register? too small Page table is kept in main memory

Page-table base register (PTBR) points to the page table

Page-table length register (PRLR) indicates size of the page table

Every data/instruction access requires two memory accesses

One for page table and one for the actual data/instruction.

To avoid two memory access problem….

38

Page 39: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Associative Memory a special fast-lookup hardware cache called

associative memory or translation look-aside buffers (TLBs) Some TLBs store address-space identifiers (ASIDs) in each

entry – uniquely identifies each process to provide address-space protection

Associative memory – parallel search for all entries

Address translation (p, d) If p is in associative register, get frame # out Otherwise get frame # from page table in memory

Page # Frame #

39

Page 40: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Paging Hardware With TLB

40

Page 41: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Effective Access Time Associative Lookup = time unit (e.g. 20 ns) Hit ratio – percentage of times that a page number is

found in the associative registers; ratio related to number of associative registers Hit ratio = (e.g. 80%)

Time to access memory (e.g. 100 ns) Effective Access Time (EAT) e.g. EAT = 0.80 x 120 + 0.20 x (100+100+20) (found in TLB) (not found in TLB) =140 ns

41

Page 42: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Memory Protection

Memory protection implemented by associating protection bit with each frame

Valid-invalid bit attached to each entry in the page table: “valid” indicates that the associated page is in

the process’ logical address space, and is thus a legal page

“invalid” indicates that the page is not in the process’ logical address space

42

Page 43: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Valid (v) or Invalid (i) Bit In A Page Table

43

Most processes do not use whole logic address space

Address space: 214 (0, …, 16383), Process use only 0 to 10468Page size: 2KB

Alternative: page-table length register (PTLR)

Page 44: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Shared Pages

Useful for shared code (reentrant code, pure code) One copy of read-only code shared among

processes (i.e., text editors, compilers, window systems).

Each process has its own copy of registers and data storage to hold its data

Shared code page and private data page

44

Page 45: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Shared Pages Example

45

Page 46: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Structure of the Page Table

To support large logic address space up to 232 or 264 page table too large to put in a contiguous

memory Solutions

Hierarchical Paging

Hashed Page Tables

Inverted Page Tables

46

Page 47: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Hierarchical Page Tables

Break up the logical address space into multiple page tables

A simple technique is a two-level page table

e.g. for large logical address spaces 232 to 264

47

Page 48: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Two-Level Page-Table Scheme

48

Page 49: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Two-Level Paging Example A logical address (on 32-bit machine with 1K page size) is divided into:

a page number consisting of 22 bits a page offset consisting of 10 bits

Since the page table is paged, the page number is further divided into: a 12-bit page number a 10-bit page offset

Thus, a logical address is as follows:

where pi is an index into the outer page table, and p2 is the displacement within the page of the outer page table

page number page offset

pi p2 d

12 10 10

49

Page 50: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Address-Translation Scheme

50

Page 51: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Three-level Paging Scheme

51

Page 52: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Hashed Page Tables

Common in address spaces > 32 bits

Virtual page number is hashed into a page table. This page table contains a chain of elements hashing to the same location.

Virtual page numbers are compared in this chain searching for a match. If a match is found, the corresponding physical frame is extracted.

52

Page 53: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Hashed Page Table

53

Page 54: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Inverted Page Table

One entry for each real page of memory Entry consists of virtual address of the

page stored in that real memory location, with information about process that owns that page

Decreases memory needed to store each page table, but increases time needed to search the table when a page reference occurs

Use hash table to limit the search to one — or at most a few — page-table entries

54

Page 55: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Inverted Page Table Architecture

55

Page 56: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Segmentation Memory-management scheme that supports

user view of memory A program is a collection of segments. A

segment is a logical unit such as:main program,procedure, function,method,object,local variables, global variables,common block,stack,symbol table, arrays

56

Page 57: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Logical View of Segmentation

1

3

2

4

1

4

2

3

user space physical memory space

57

Page 58: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Segmentation Architecture

A process’s logical address consists of a two-tuple:

<segment-number, offset>, Segment table – maps two-dimensional

physical addresses; each table entry has: base – contains starting physical address where

the segments reside in memory limit – specifies length of the segment

Segment-table base register (STBR) points to segment table’s location in memory

Segment-table length register (STLR) indicates number of segments used by a program segment number s is legal if s < STLR

58

Page 59: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Segmentation Architecture (Cont.)

Protection With each entry in segment table associate:

validation bit = 0 illegal segment read/write/execute privileges

Protection bits associated with segments; code sharing occurs at segment level

Since segments vary in length, memory allocation is a dynamic storage-allocation problem

59

Page 60: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Segmentation Hardware

60

Page 61: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Example of Segmentation

61

Page 62: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Example: The Intel Pentium Supports both segmentation, and

segmentation with paging CPU generates logical address

Given to segmentation unit Which produces linear addresses

Linear address given to paging unit Which generates physical address in main memory

62

Page 63: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Logical => Physical Address: in Pentium

63

Page 64: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Intel Pentium Segmentation

64

Page 65: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Pentium Paging Architecture

65

Page 66: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Linux on Pentium Systems

Uses six segments:1. A segment for kernel code2. A segment for kernel data3. A segment for user code4. A segment for user data5. A task-state segment (TSS)6. A default local descriptor table (LDT) segment

66

Page 67: Main Memory CISC3595, Fall 09 1. Memory Management  Background  Contiguous Memory Allocation  Paging  Structure of the Page Table  Segmentation

Three-level Paging in Linux

67

Linear Address in Linux