Cache Associativity and Virtual Memory: Prof. Dr. E. Damiani
Cache Associativity and Virtual Memory: Prof. Dr. E. Damiani
Block address
31 9 4 0
Cache Tag Example: 0x50 Cache Index Byte Select
Ex: 0x01 Ex: 0x00
Stored as part
of the cache “state”
: :
0x50 Byte 63 Byte 33 Byte 32 1
2
3
: : :
Byte 1023 Byte 992 31
:
Set Associative Cache
• N-way set associative: N entries for each Cache
Index
– N direct mapped caches operate in parallel
• Example: Two-way set associative cache
– Cache Index selects a “set” from the cache
– The two tags in the set are compared to the address tag in parallel
– Data is selected based on which tags match and the valid bits
Cache Index
Valid Cache Tag Cache Data Cache Data Cache Tag Valid
Cache Block 0 Cache Block 0
: : : : : :
OR
Cache Block
Hit
Disadvantage of Set Associative Cache
• N-way Set Associative Cache versus Direct Mapped
Cache:
– N comparators vs. 1
– Extra MUX delay for the data
– Data comes AFTER Hit/Miss decision and set selection
• In a direct mapped cache, Cache Block is available
BEFORE Hit/Miss:
– Possible to assume a hit and continue. Recover later if miss.
Cache Index
Valid Cache Tag Cache Data Cache Data Cache Tag Valid
Cache Block 0 Cache Block 0
: : : : : :
OR
Cache Block
Hit
Example: Fully
Associative
• Fully Associative Cache
– Forget about the Cache Index
– Compare the Cache Tags of all cache entries in parallel
– Example: Block Size = 32 B blocks, we need N 27-bit comparators
– Often implemented using content addressable memory (CAM)
– By definition: Conflict Miss = 0 for a fully associative cache
31 4 0
Cache Tag (27 bits long) Byte Select
Ex: 0x01
: :
= Byte 63 Byte 33 Byte 32
=
=
=
: : :
What is virtual memory?
Virtual Physical
Address Space Address Space Virtual Address (from processor)
10
V page no. offset
Page Table
Page Table
Base Reg Access
index V Rights PA
into
page
table table located
in physical P page no. offset
memory 10
Physical Address
(main memory)
• Virtual memory => treat memory as a cache for the disk
• Terminology: blocks in this cache are called “Pages”
– Typical size of a page: 1K — 8K
• Page table maps virtual page numbers to physical frames
– “PTE” = Page Table Entry
Virtual Memory
• Virtual memory (VM) allows main memory (DRAM) to
act like a cache for secondary storage (magnetic disk).
• VM address translation a provides a mapping from the
virtual address of the processor to the physical
address in main memory and secondary storage.
• VM provides the following benefits
– Allows multiple programs to share the same physical memory
– Allows programmers to write code (or compilers to generate code) as
though they have a very large amount of main memory
– Automatically handles bringing in data from disk
• Cache terms vs. VM terms
– Cache block => page
– Cache Miss => page fault
Cache and Main Memory
Parameters
Parameter L1 Cache Main Memory
Memory size 16 KB to 1 MB 16 MB to 8 GB
4 Qs for Virtual Memory
• Q1: Where can a block be placed in the upper level?
– Miss penalty for virtual memory is very high
– Have software determine location of block while accessing disk
– Allow blocks to be place anywhere in memory (fully assocative) to reduce miss
rate.
• Q2: How is a block found if it is in the upper level?
– Address divided into page number and page offset
– Page table and translation buffer used for address translation
20 bits 12 bits
Virtual Page Number Page offset Virtual Address
Translation
18 bits 12 bits
Address Translation with
Page Tables
• A page table translates a virtual page number into a
physical page number.
• A page table register indicates the start of the page
table.
• The virtual page number is used as an index into the
page table that contains
– The physical page number
– A valid bit that indicates if the page is present in main memory
– A dirty bit to indicate if the page has been written
– Protection information about the page (read only, read/write, etc.)
• Since page tables contain a mapping for every virtual
page, no tags are required.
Page Table Diagram
(See Figure 7.22 on page 584)
Accessing Main Memory or Disk
• If the valid bit of the page table is zero, this
means that the page is not in main memory.
• In this case, a page fault occurs, and the
missing page is read in from disk.
Determining Page Table Size
• Assume
– 32-bit virtual address
– 30-bit physical address
– 4 KB pages => 12 bit page offset
– Each page table entry is one word (4 bytes)
• How large is the page table?
– Virtual page number = 32 - 12 = 20 bytes
– Number of entries = number of pages = 2^20
– Total size = number of entries x bytes/entry
= 2^20 x 4 = 4 Mbytes
– Each process running needs its own page table
• Since page tables are very large, they are almost always
stored in main memory, which makes them slow.
Large Address Spaces
Two-level Page Tables
1K
PTEs 4KB
32-bit address:
10 10 12
P1 index P2 index page offest
4 bytes
4 bytes
What about a 48-64 bit address space?
Caching Virtual Addresses
VA PA miss
Trans- Main
CPU Cache
lation Memory
hit
data
1
3
physical address
page off
TLB
frame page
2 2
0 5
Translation-Lookaside Buffer (TLB)
(See Figure 7.24 on page 591)
TLB
64 entry, on-chip, fully associative, software TLB fault handler
Cache
hit
data
nightmare for update: must update all cache entries with same
physical address or memory becomes inconsistent