v. primary & secondary memory - ida > home725g84/include/725g84-6-memory.pdf · v. primary...

66
V. Primary & Secondary Memory Computer Architecture and Operating Systems & Operating Systems: 725G84 Ahmed Rezine 1 Computer Architecture & Operating Systems (725G84 - HT16)

Upload: others

Post on 08-Jun-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

V. Primary & Secondary Memory�

Computer Architecture and

Operating Systems & Operating

Systems: 725G84

Ahmed Rezine

1 Computer Architecture & Operating Systems (725G84 - HT16)

Page 2: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Memory Technology

Computer Architecture & Operating Systems (725G84 - HT16)

  Static RAM (SRAM)   0.5ns – 2.5ns, $2000 – $5000 per GB

  Dynamic RAM (DRAM)   50ns – 70ns, $20 – $75 per GB

  Magnetic disk   5ms – 20ms, $0.20 – $2 per GB

  Ideal memory   Access time of SRAM   Capacity and cost/GB of disk

Page 3: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Memory

Computer Architecture & Operating Systems (725G84 - HT16)

  It is ‘impossible’ to have memory that is both   Unlimited (large in capacity)   And fast

  We create an illusion for the programmer   Before that, let us look at the way programs

access memory

Page 4: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Principle of Locality

Computer Architecture & Operating Systems (725G84 - HT16)

  Programs access a small proportion of their address space at any time

  Temporal locality   Items accessed recently are likely to be accessed again

soon   e.g., instructions in a loop

  Spatial locality   Items near those accessed recently are likely to be

accessed soon   E.g., sequential instruction access, array data

Page 5: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

To Take Advantage of Locality

Computer Architecture & Operating Systems (725G84 - HT16)

  Employ memory hierarchy   Use multiple levels of memories   ‘Larger’ distance from processor =>

  larger size   larger access time

Page 6: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Taking Advantage of Locality

  Memory hierarchy

  Store everything on disk

  Copy recently accessed (and nearby) items from disk to smaller DRAM memory   Main memory

  Copy more recently accessed (and nearby) items from DRAM to smaller SRAM memory   Cache memory attached to CPU

Computer Architecture & Operating Systems (725G84 - HT16)

Page 7: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Memory hierarchy

Computer Architecture & Operating Systems (725G84 - HT16)

Page 8: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Memory Hierarchy Levels   Block (aka line): unit of copying

  May be multiple words

  If accessed data is present in upper level   Hit: access satisfied by upper

level   Hit ratio: hits/accesses

  If accessed data is absent   Miss: block copied from lower

level   Time taken: miss penalty   Miss ratio: misses/accesses�

= 1 – hit ratio   Then accessed data supplied from

upper level Computer Architecture & Operating Systems (725G84 - HT16)

Page 9: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Memory Hierarchy Levels

Computer Architecture & Operating Systems (725G84 - HT16)

Page 10: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Cache Memory

Computer Architecture & Operating Systems (725G84 - HT16)

  Cache memory   The level of the memory hierarchy closest to the CPU

  Given accesses X1, …, Xn–1, Xn

!  How do we know if the data is present?

!  Where do we look?

Page 11: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Direct Mapped Cache

Computer Architecture & Operating Systems (725G84 - HT16)

  Location determined by address

  Direct mapped: only one choice   (Block address) modulo (#Blocks in cache)

Page 12: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

(Block address) modulo (#Blocks in cache)

Computer Architecture & Operating Systems (725G84 - HT16)

Direct Mapped Cache

Page 13: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Direct Mapped Cache   If #Blocks is a power of 2

  Use low-order address bits to compute index

Computer Architecture & Operating Systems (725G84 - HT16)

Page 14: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Tag Bits

Computer Architecture & Operating Systems (725G84 - HT16)

  Each cache location can store the contents of more than one memory location (yet only one at a time)

  How do we know which particular block is stored in a cache location?   Add a set of tag bits to the cache   Tags only need the high-order bits of the address

Page 15: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Valid Bits

Computer Architecture & Operating Systems (725G84 - HT16)

  What if there is no data in a location?   Valid bit: 1 = present, 0 = not present   Initially 0 because when the processor starts

up, the cache does not have any valid data

Page 16: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Cache Example

Computer Architecture & Operating Systems (725G84 - HT16)

  8-blocks, 1 word/block, direct mapped

  Initial state Index V Tag Data 000 N 001 N 010 N 011 N 100 N 101 N 110 N 111 N

Page 17: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Cache Example

Computer Architecture & Operating Systems (725G84 - HT16)

Index V Tag Data 000 N 001 N 010 N 011 N 100 N 101 N 110 Y 10 Mem[10110] 111 N

Word addr Binary addr Hit/miss Cache block 22 10 110 Miss 110

Page 18: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Cache Example

Computer Architecture & Operating Systems (725G84 - HT16)

Index V Tag Data 000 N 001 N 010 Y 11 Mem[11010] 011 N 100 N 101 N 110 Y 10 Mem[10110] 111 N

Word addr Binary addr Hit/miss Cache block 26 11 010 Miss 010

Page 19: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Cache Example

Computer Architecture & Operating Systems (725G84 - HT16)

Index V Tag Data 000 N 001 N 010 Y 11 Mem[11010] 011 N 100 N 101 N 110 Y 10 Mem[10110] 111 N

Word addr Binary addr Hit/miss Cache block 22 10 110 Hit 110 26 11 010 Hit 010

Page 20: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Cache Example

Computer Architecture & Operating Systems (725G84 - HT16)

Index V Tag Data 000 Y 10 Mem[10000] 001 N 010 Y 11 Mem[11010] 011 Y 00 Mem[00011] 100 N 101 N 110 Y 10 Mem[10110] 111 N

Word addr Binary addr Hit/miss Cache block 16 10 000 Miss 000 3 00 011 Miss 011

16 10 000 Hit 000

Page 21: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Cache Example

Computer Architecture & Operating Systems (725G84 - HT16)

Index V Tag Data 000 Y 10 Mem[10000] 001 N 010 Y 11 Mem[11010] 011 Y 00 Mem[00011] 100 N 101 N 110 Y 10 Mem[10110] 111 N

Word addr Binary addr Hit/miss Cache block 18 10 010 Miss 010

Page 22: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Cache Example

Computer Architecture & Operating Systems (725G84 - HT16)

Index V Tag Data 000 Y 10 Mem[10000] 001 N 010 Y 10 Mem[10010] 011 Y 00 Mem[00011] 100 N 101 N 110 Y 10 Mem[10110] 111 N

Word addr Binary addr Hit/miss Cache block 18 10 010 Miss 010

Page 23: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Address Subdivision

Computer Architecture & Operating Systems (725G84 - HT16)

Page 24: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Address Subdivision

Computer Architecture & Operating Systems (725G84 - HT16)

Cache holds 1024 blocks Block size is 1 word So, cache size is 1024 words or 2^10 words Or (2^10) x 4 bytes Or 4KB

Page 25: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Address Subdivision

Computer Architecture & Operating Systems (725G84 - HT16)

Since Cache holds 1024 blocks, we need 10 bits to

index the cache

Page 26: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Address Subdivision

Computer Architecture & Operating Systems (725G84 - HT16)

1 word or 4 bytes are fetched into each cache block, so no

need to index them

Page 27: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Address Subdivision

Computer Architecture & Operating Systems (725G84 - HT16)

That leaves 20 bits upper bits as tag bits

Page 28: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Example: Larger Block Size

  64 blocks, 16 bytes/block   (instead of 4 bytes per block)   To what block number does byte address 1200

map?

  Block address = ⎣1200/16⎦ = 75

  Block number = 75 modulo 64 = 11

Computer Architecture & Operating Systems (725G84 - HT16)

Tag Index Offset 0 3 4 9 10 31

4 bits 6 bits 22 bits

Page 29: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Reading from Cache   On cache hit, CPU proceeds normally

  On cache miss   Stall the CPU pipeline   Fetch block from next level of hierarchy   Instruction cache miss

  Restart instruction fetch   Data cache miss

  Complete data access

Computer Architecture & Operating Systems (725G84 - HT16)

Page 30: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Writing to Cache   On each data-write hit, could just update the

block in cache   But then cache and memory would be

inconsistent

  3 schemes to fix this

Page 31: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Write-Through   Write through: also update memory,

always

  But makes writes take longer

Page 32: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Write-Buffer   Solution: write buffer

  Holds data waiting to be written to memory

  CPU continues immediately after writing to write-buffer   Write-buffer is freed later when memory write is

completed

  But CPU stalls on write if write buffer is already full

Page 33: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Write-Back   Alternative: On data-write hit, just update the

block in cache   Keep track of whether each block is dirty

  When a dirty block is replaced   Write it back to memory

Page 34: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Measuring Cache Performance

  Components of CPU time   Program execution cycles

  Includes cache hit time   Memory stall cycles

  Mainly from cache misses

Page 35: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Measuring Cache Performance

  Components of CPU time   Program execution cycles

  Includes cache hit time   Memory stall cycles

  Mainly from cache misses

  With simplifying assumptions:

Page 36: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Cache Performance Example

  Given   I-cache miss rate = 2%   D-cache miss rate = 4%   Miss penalty = 100 cycles   Base CPI (ideal cache) = 2   Load & stores are 36% of instructions

  How much faster would be a processor with a perfect cache that never misses ?

Page 37: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Cache Performance Example

  Miss cycles for all instructions if I is the instruction count   I-cache: I ₒ 0.02 ₒ 100 = 2 I   D-cache: I ₒ 0.36 ₒ 0.04 ₒ 100 = 1.44 I

  Miss cycle per instruction is 2 + 1.44

  Total cycles per instruction is 2 + 2 + 1.44

  So, actual CPI = 2 + 2 + 1.44 = 5.44   Ideal CPU is 5.44/2 =2.72 times faster

Page 38: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

So far   Direct mapped only

  Actually, there is a full spectrum

  At the other end, lies fully associative

Page 39: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Associative Caches   Fully associative

  Allow a given block to go in any cache entry   Requires all entries to be searched at once   Comparator per entry (expensive)

Page 40: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Associative Caches   n-way set associative

  Each set contains n entries   Block number determines which set

  (Block number) modulo (#Sets in cache)   Search all entries in a given set at once   n comparators (less expensive)

Page 41: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Associative Cache Example

FIGURE 5.13 The location of a memory block whose address is 12 in a cache with eight blocks varies for directmapped, set-associative, and fully associative placement. In direct-mapped placement, there is only one cache block where memory block 12 can be found, and that block is given by (12 modulo 8) = 4. In a two-way set-associative cache, there would be four sets, and memory block 12 must be in set (12 mod 4) = 0; the memory block could be in either element of the set. In a fully associative placement, the memory block for block address 12 can appear in any of the eight cache blocks. Copyright © 2009 Elsevier, Inc. All rights reserved.

Page 42: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Spectrum of Associativity

Page 43: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Which block to replace?   In direct mapped cache: there is only one choice

  In set associative: all blocks in selected set are candidates

  In fully associative: all blocks are candidates

Page 44: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Which block to replace?   Least Recently Used is the most common strategy

  The block replaced is the one that has been unused the longest time

Page 45: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Virtual Memory   VM is the technique of using main memory as a “cache”

for secondary (disk) storage   Managed jointly by CPU hardware and the operating

system (OS)

  Same underlying concept as in cache but different in terminologies

Page 46: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

VM Terms

  Virtual address is the address produced by the program

  Physical address is an address in the main memory

  CPU and OS translate virtual addresses to physical addresses   VM “block” is called a page   VM translation “miss” is called a page fault

Page 47: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Page Fault Penalty   On page fault, the page must be fetched from disk

  Takes millions of clock cycles. Main memory latency is around 100,000 times better than the disk latency

  Try to minimize page fault rate   Smart replacement algorithms implemented in software in

the OS   Reading from disk is slow enough and software overhead is

negligible

Page 48: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Motivation 1   Multiple programs share main memory and they can

change dynamically

  To avoid writing into each other’s data, we would like separate address space for each program

  With VM, each gets a private virtual address space holding its frequently used code and data

  VM translates the virtual address into physical address allowing protection from other programs

Page 49: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Motivation 2   A large program cannot fit into the main memory

  VM automatically maps addresses into disk space if the main memory is not sufficient

Page 50: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Address Translation   Address translation: the process by which

the virtual address is mapped to a physical address

Page 51: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Address Translation

Computer Architecture & Operating Systems (725G84 - HT16)

Two components of virtual address

Page offset does not change and the number of bits determines the

size of the page

"  The number of pages addressable with virtual address might be larger than the number of pages addressable with the physical address which gives the illusion of unbounded amount of virtual memory

Page 52: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Translation Using a Page Table

Page 53: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Translation Using a Page Table

Computer Architecture & Operating Systems (725G84 - HT16)

A page table, that resides on the memory, is used for

address translation

"  Each program has its own page table

Page 54: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Translation Using a Page Table

Computer Architecture & Operating Systems (725G84 - HT16)

To indicate the location of the page table in the

memory, a hardware register points to the start of the

page table

"  Each program has its own page table

Page 55: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Translation Using a Page Table

Computer Architecture & Operating Systems (725G84 - HT16)

Note the use of a Valid bit

Page 56: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Page Tables   Stores placement information

  Array of page table entries, indexed by virtual page number   Page table register in CPU points to page table in physical

memory

  If page is present in memory   it stores the physical page number   Plus other status bits

  If page is not present   a page fault occurs and the OS is given control   Next few slides, we recap some OS concepts

Page 57: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

OS concept   A process (a program in execution), has a context

defined by the values in its program counter, registers, and page table

  If another process preempts this process, this context must be saved   Rather than save the entire page table, only the page

table register is saved

  To restart the process in the ‘running’ state, the operating system reloads the context

Page 58: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Role of the OS   The OS is responsible for allocating the physical

memory and updating the page tables

  It maintains that virtual address of different processes do not collide thus providing protection

  Page fault is handled by OS

Page 59: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Role of the OS   The OS creates a space on the disk (not main

memory) for all pages of a process when the process is created   called swap space

  OS also creates a record of where each virtual address of the process is located in the disk

Page 60: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Mapping Pages to Storage

Page 61: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Page Fault   Handled by OS

  If all pages in main memory are in use (it is full), the OS must choose a page to replace

  The replaced page must be written to the swap space on the disk

Page 62: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Writes   Disk writes take millions of cycles

  Write through is impractical because it takes millions of cycles to write to the disk   Building a write buffer is impractical

  VM uses write-back

Page 63: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Fast Translation Using a TLB

  Address translation would appear to require extra memory references   One to access the page table itself   Then the actual memory access

  But access to page tables has good locality   So use a fast cache of recently used

translations   Called a Translation Look-aside Buffer (TLB)

Page 64: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Memory Protection   VM allows different processes to share the

same main memory   Need to protect against errant access   Requires OS assistance

Page 65: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Memory Protection   Hardware support for OS protection

  Support two modes   Privileged supervisor mode (aka kernel mode) meaning

that the OS is running   User mode

  Privileged instructions that only the OS can use   Allow it to write to the supervisor bit, and page table

pointer   Allow mechanisms (e.g., special instructions) to

switch between supervisor mode and the user mode (e.g., syscall in MIPS)

  These features allow the OS to change page tables while preventing a user process from changing them

Page 66: V. Primary & Secondary Memory - IDA > Home725G84/include/725G84-6-memory.pdf · V. Primary & Secondary Memory! Computer Architecture and Operating Systems & Operating Systems: 725G84

Computer Architecture & Operating Systems (725G84 - HT16)

Concluding Remarks   Fast memories are small, large memories

are slow   We really want fast, large memories #   Caching gives this illusion ☺

  Principle of locality   Programs use a small part of their memory

space frequently

  Memory hierarchy   L1 cache ↔ L2 cache ↔ … ↔ DRAM memory�↔ disk