Memory Management Address binding Linking, loading Logical vs. - - PDF document

memory management
SMART_READER_LITE
LIVE PREVIEW

Memory Management Address binding Linking, loading Logical vs. - - PDF document

CPSC 410/611 : Operating Systems Memory Management Address binding Linking, loading Logical vs. physical address space Fragmentation Paging Segmentation Reading: Silberschatz (8 th ed.), Chapter 8 Memory


slide-1
SLIDE 1

CPSC 410/611 : Operating Systems Memory Management: Paging / Segmentation 1

Memory Management

  • Address binding

– Linking, loading – Logical vs. physical address space

  • Fragmentation
  • Paging
  • Segmentation
  • Reading: Silberschatz (8th ed.), Chapter 8

Memory Management

  • Observations:

– Process needs at least CPU and memory to run. – CPU context switching is relatively cheap. – Swapping memory in/out from/to disk is expensive.

  • Need to subdivide memory to accommodate multiple processes!
  • How do we manage this memory?
slide-2
SLIDE 2

CPSC 410/611 : Operating Systems Memory Management: Paging / Segmentation 2

Requirements for Memory Management

  • Relocation

– We do not know a priori where memory of process will reside.

  • Protection

– No uncontrolled references to memory locations of other processes. – Memory references must be checked at run-time.

  • Sharing

– Data portions and program text portions.

  • Logical organization

– Take advantage of semantics of use. – Data portions (read/write) vs. program text portions (read

  • nly).
  • Memory hierarchy

– RAM vs. secondary storage – Swapping

S1 O1 Ltemp Lphys S2 S3 O2 O3

Preparing a Program for Execution

  • compiler: translates symbolic instructions, operands, and addresses

into numerical values.

  • linker: resolves external references; i.e. operands or branch

addresses referring to data or instructions within some other module

  • loader: brings program into main memory.

compiler linker loader Si : Source Program Oi: Object Module Ltemp: Load Module Lphys: Memory Image dynamically loaded system library system library

slide-3
SLIDE 3

CPSC 410/611 : Operating Systems Memory Management: Paging / Segmentation 3

Address Binding

  • Compile-time binding:
  • Load-time binding (static relocation):
  • Execution-time binding (dynamic relocation):

branch A A: ... branch 150 assembler 100 150 branch A A: ... branch 50 assembler 00 50 branch 150 linker 100 150

  • ther module

branch 1150 loader 1100 1150

  • ther module
  • ther progrms

A: ... branch A A: ... branch 50 assembler 00 50 branch 150 linker 100 150

  • ther module

branch 150 loader 1100 1150

  • ther module
  • ther progrms

MMU 1150

Dynamic Loading, Dynamic Linking

  • Dynamic Loading:

– load routine into memory only when it is needed – routines kept on disk in relocatable format

  • Load-time Linking:

– postpone linking until load time.

  • Dynamic Linking:

– postpone linking until execution time. – Problem: Need help from OS!

call x stub x: load routine link x to location of routine

slide-4
SLIDE 4

CPSC 410/611 : Operating Systems Memory Management: Paging / Segmentation 4

Logical vs. Physical Address Space

  • Logical address: address as seen by the process (i.e. as seen by the CPU).
  • Physical address: address as seen by the memory.
  • Example:

branch A A: ... branch 50 assembler 00 50 branch 150 linker 100 150 branch 1150 loader 1100 1150 A: ... logical = physical branch A A: ... branch 50 assembler 00 50 branch 150 linker 100 150 branch 150 loader 1100 1150 MMU 1150 physical logical execution time: load time:

Logical vs. Physical Memory Space

physical address space of process Pi process base size P1 28 1000 P2 1028 3000 P3 5034 250 CPU

< +

OS relocation register limit register addressing error! logical address space of process Pi Memory Management Unit

partition table

Physical Memory

  • Logical address: address as seen by the process (i.e. as seen by the CPU).
  • Physical address: address as seen by the memory.
slide-5
SLIDE 5

CPSC 410/611 : Operating Systems Memory Management: Paging / Segmentation 5

Swapping

waiting running start waiting_sw ready_sw ready jobs are in memory jobs are on disk OS swap_out swap_in swapping store memory

Fragmentation

OS 8MB 2MB

Internal Fragmentation

4MB 8MB 12MB

?!

External Fragmentation

P4

?

P1 P1 P2 P1 P2 P3 P1 P3

slide-6
SLIDE 6

CPSC 410/611 : Operating Systems Memory Management: Paging / Segmentation 6

Paging

  • Contiguous allocation causes (external) fragmentation.
  • Solution: Partition memory blocks into smaller subblocks (pages)

and allow them to be allocated non-contiguously.

Memory Management Unit logical memory physical memory

simple relocation

Memory Management Unit logical memory physical memory

paging

Basic Operations in Paging Hardware

Memory Management Unit CPU physical memory p d f d f p page table

d

Example: PDP-11 (16-bit address, 8kB pages)

slide-7
SLIDE 7

CPSC 410/611 : Operating Systems Memory Management: Paging / Segmentation 7

Internal Fragmentation in Paging

  • Example:

logical memory 13300B page size 4kB physical memory

  • Last frame allocated may not be completely full.
  • Average internal fragmentation per block is typically half frame size.
  • Large frames vs. small frames:
  • Large frames cause more fragmentation.
  • Small frames cause more overhead (page table size, disk I/O)

4084 bytes wasted!

Implementation of Page Table

  • Page table involved in every access to memory. Speed very

important.

  • Page table in registers?

– Example: 1MB logical address space, 2kB page size; needs a page table with 512 entries!

  • Page table in memory?

– Only keep a page table base register that points to location

  • f page table.

– Each access to memory would require two accesses to memory!

  • Cache portions of page table in registers?

– Use translation lookaside buffers (TLBs): typically a few dozens entries. – Hit ratio: Percentage of time an entry is found. Hit ratio must be high in order to minimize overhead.

slide-8
SLIDE 8

CPSC 410/611 : Operating Systems Memory Management: Paging / Segmentation 8

Hierarchical (Multilevel) Paging

  • Problem: Page tables can become very large! (e.g. 32-bit address space?)
  • Solution: Page the page table itself! (e.g. page directory vs. page table)
  • Two-level paging:

– Example: 32 bit logical address, page size 4kB

  • Three-level paging (SPARC), four-level paging (68030), ...
  • AMD64 (48-bit virtual addresses) has 4 levels.
  • Even deeper for 64 bit address spaces (5 to 6 levels)

f d f

page table (10)

  • ffset(12)

page directory (10) page table base register

Variations: Inverted Page Table

n … 3 2 1 page no process id page no proc id

  • ffset
  • ffset

3

  • Array of page numbers indexed by frame number.

– page lookup: search for matching frame entry

  • Size scales with physical memory.
  • Single table for system (not per process)
  • Used in early virt. memory systems, such as the

Atlas computer.

  • Not practical today. (Why?)
slide-9
SLIDE 9

CPSC 410/611 : Operating Systems Memory Management: Paging / Segmentation 9

Variations: Hashed Page Table

  • Used by many 64bit architectures:

– IBM POWER – HP PA-RISC – Itanium

  • Scales with physical memory
  • One table for whole system
  • Difficult to share memory between

processes

page number

  • ffset

hash function proc id page no chain

Software-loaded TLBs: Paging - MIPS Style

ASID VPN Address within page Address within frame PFN VPN/Mask ASID PFN Flags PFN Flags Process no. Program (virtual) address TLB Page table (in memory)

refill when necessary

Physical address

slide-10
SLIDE 10

CPSC 410/611 : Operating Systems Memory Management: Paging / Segmentation 10

Recap: Memory Translation -- “VAX style”

  • 1. Split virtual address
  • 2. Concatenate more-significant bits with Process ASID to

form page address.

  • 3. Look in the TLB to see if we find translation entry for

page.

  • 4. If YES, take high-order physical address bits.

– (Extra bits stored with PFN control the access to frame.)

  • 5. If NO, system must locate page entry in main-memory-

resident page table, load it into TLB, and start again.

Memory Translation -- MIPS Style

  • In principle: Do the same as VAX, but with as little

hardware as possible.

  • Apart from register with ASID, the MMU is just a TLB.
  • The rest is all implemented in software!
  • When TLB cannot translate an address, a special

exception (TLB refill) is raised.

  • Note: This is easy in principle, but tricky to do

efficiently.

slide-11
SLIDE 11

CPSC 410/611 : Operating Systems Memory Management: Paging / Segmentation 11

MIPS TLB Entry Fields

  • VPN: higher order bits of

virtual address

  • ASID: identifies the address

space

  • G: if set, disables the

matching with the ASID

VPN ASID G PFN Flags N D V

input

  • utput
  • PFN: Physical frame number
  • N: 0 - cacheable, 1 -

noncacheable

  • D: write-control bit (set to 1 if

writeable)

  • V: valid bit

MIPS Translation Process

1. CPU generates a program (virtual) address on a instruction fetch, a load, or a store.

  • 2. The 12 low-end bits are separated off.
  • 3. Case 1: TLB matches key:

1. Matching entry is selected, and PFN is glued to low-order bits

  • f the program address.
  • 2. Valid?: The V and D bits are checked. If problem, raise

exception, and set BadVAddr register with offending program address.

  • 3. Cached?: IF C bit is set, the CPU looks in the cache for a copy
  • f the physical location’s data. If C bit is cleared, it neither

looks in nor refills the cache.

  • 4. Case 2: TLB does not match: TLB Refill Exception (see next page)
slide-12
SLIDE 12

CPSC 410/611 : Operating Systems Memory Management: Paging / Segmentation 12

TLB Refill Exception

  • Figure out if this was a correct translation. If not, trap to

handling of address errors.

  • If translation correct, construct TLB entry.
  • If TLB already full, select an entry to discard.
  • Write the new entry into the TLB.

Segmentation

  • Users think of memory in terms of segments (data, code, stack, objects, ....)
  • Data within a segment typically has uniform access restrictions.

Memory Management Unit

logical memory physical memory

paging segmentation

Memory Management Unit

logical memory physical memory

slide-13
SLIDE 13

CPSC 410/611 : Operating Systems Memory Management: Paging / Segmentation 13

Segmentation Hardware

Memory Management Unit CPU physical memory s d s segment table limit base <? +

Advantages of Segmentation

  • Data in a segment typically semantically related
  • Protection can be associated with segments

– read/write protection – range checks for arrays

  • Data/code sharing
  • Disadvantages?

physical memory

s d

s

limit base

<? +

s d

s

limit base

<? +

sharing

slide-14
SLIDE 14

CPSC 410/611 : Operating Systems Memory Management: Paging / Segmentation 14

Solution: Paged Segmentation

  • Example: MULTICS

segment number

  • ffset

18bit 16bit Problem: 64kW segments -> external fragmentation! Solution: Page the segments. segment number 18bit 10bit page# page

  • ffset

6bit Problem: need 2^18 segment entries in segment table Solution: Page the segment table. 8bit 10bit page# page

  • ffset

6bit 10bit page# page

  • ffset