Cache and TLB Together

Every access needs the TLB and the cache. One after the other is too slow. Side by side works, as long as the cache asks for no bit that the TLB has yet to deliver.

You Will Be Able To

  • Explain why a cache that waits for the TLB has a long hit path.
  • Work out how large one way may be if the index must come from the page offset.
  • Explain how one frame with two virtual names can end up in two lines, and how designs prevent it.
Lesson
S3.9
Time
26 min
Runs on
Your browser
Check
6 questions

A team doubles its level-1 cache from 4 KiB to 8 KiB. Every test of the cache passes. Weeks later, two programs that share a page disagree about what is in it.

Before You Start

You need S3.3: tag, index, offset and the hit path. You need S3.8: the TLB turns a page number into a frame number.

You also need ways from S3.4, and the rule of S3.7 that a translation keeps the offset.

Five questions this page answers
Question
1Why not translate first and look in the cache afterwards?
2Which bits of a virtual address can a cache use before the TLB answers?
3How large may one way of such a cache be?
4What is a synonym, and what goes wrong with it?
5How do designs make a larger cache safe?

The Idea: Start the Cache Before the Translation Ends

The cache holds copies of physical memory. So the plain design translates first. The TLB turns the page number into a frame number, and then the cache is looked up with the physical address. A cache used this way is physically indexed.

That puts two lookups in a row on the path of every access: first the TLB, then the cache. The hit time of level 1 decides the clock of the processor. It has no room for both.

Look at what the TLB really changes. It swaps the page number. The offset passes through untouched. So the low bits of the address are known at once, with no translation.

If the cache takes its index from those bits only, it can start at once. While the TLB works, the cache reads out a line. Its tag is a frame number. When the TLB delivers the frame number, the two are compared. A cache used this way is virtually indexed and physically tagged.

The price is a limit. The index and the block offset together must fit into the page offset: 12 bits for pages of 4 KiB. So one way of the cache can hold 4 KiB at most.

A cache that needs one more index bit must take it from the page number. Then a frame that has two virtual names can land in two different lines. The two names of one frame are called synonyms. One line is written, and the other still holds the old data.

New Words

New words on this page
WordMeaning
Physically indexed cacheA cache whose index comes from the physical address. It must wait for the TLB.
Virtually indexed cacheA cache whose index comes from the virtual address. It can start while the TLB works.
Physical tagA tag that is taken from the physical address. On this page it is the frame number.
SynonymsTwo virtual addresses that are translated to the same physical address. Also called aliases.
Page-size limitOur name for the rule that index and block offset together may have no more bits than the page offset.
Size of a wayThe number of sets times the size of a block: the bytes that one way holds.

One Picture: Two Lanes

Two Lanes That Meet at the Tag CompareStep through the picture. The address splits. One half goes to the TLB, the other half to the cache, at the same time.
  • Processor
  • Memory
  • Control / FSM
  • Bus
Two Lanes That Meet at the Tag CompareStep through the picture. The address splits. One half goes to the TLB, the other half to the cache, at the same time. component Virtual address page number andoffset component TLB page number in, framenumber out component Cache lines the offset bits pick aline component Tag compare stored tag againstthe frame number component Hit and data in one cycle page numberoffset: needs notranslationframe numberthe tag of the lineequal
  1. component Virtual addresspage number and offset
    • page number → TLB
    • offset: needs no translation → Cache lines
  2. component TLBpage number in, frame number out
    • frame number → Tag compare
  3. component Cache linesthe offset bits pick a line
    • the tag of the line → Tag compare
  4. component Tag comparestored tag against the frame number
    • equal → Hit and data
  5. component Hit and datain one cycle
  1. The offset is the same in both worlds

    A translation never changes the offset. So the low 12 bits of the virtual address are already bits of the physical address.

  2. Lane 1: the TLB

    The page number goes to the TLB. That takes time.

  3. Lane 2: the cache, at the same time

    The cache does not wait. It takes its index from the offset bits and reads out a line with its tag.

  4. The lanes meet

    The tag of the line is a frame number. It is compared with the frame number from the TLB. Equal means a hit.

Read the diagram as text
  • Virtual address: page number and offset
  • TLB: page number in, frame number out
  • Cache lines: the offset bits pick a line
  • Tag compare: stored tag against the frame number
  • Hit and data: in one cycle
  • Virtual address → TLB: page number
  • Virtual address → Cache lines: offset: needs no translation
  • TLB → Tag compare: frame number
  • Cache lines → Tag compare: the tag of the line
  • Tag compare → Hit and data: equal

This works only while the index fits into the offset. That fixes how large one way of the cache may be.

The Smallest Example, Worked by Hand

Pages have 4096 bytes, so the offset has 12 bits. A direct-mapped cache has blocks of 16 bytes, which take 4 bits. That leaves 8 bits of the offset for the index: 256 lines. The cache holds 256 x 16 = 4096 bytes, exactly one page.

Two virtual pages, 00404 and 00407, are names for one frame, 06. Two programs share it. Follow the block at offset 010 through both names.

One block, two names, in a cache of 256 lines and of 512 lines
Virtual addressIndex of 8 bits: bits 11 to 4Index of 9 bits: bits 12 to 4
0040401001001
0040701001101

Bit 12 is the lowest bit of the page number. It is 0 in page 00404 and 1 in page 00407.

With 256 lines, both names pick line 01. The line has the tag 06, and both accesses find it. There is one copy of the block.

With 512 lines, the index needs bit 12. The two names pick lines 001 and 101. Each misses, each fetches the block, and each stores the tag 06. Now there are two copies. A store through one name changes one of them.

The Rules

  1. Rule 1: A translation keeps the offset.

    So the offset bits of a virtual address are physical bits already.

  2. Rule 2: A cache that uses only offset bits for its index can start with the TLB.

    Its tag is the frame number, compared when the TLB answers.

  3. Rule 3: Index bits plus block-offset bits may not exceed the offset bits of a page.

    So the size of a way may not exceed the size of a page.

  4. Rule 4: A larger cache within this limit needs more ways.

    Eight ways of 4 KiB make 32 KiB.

  5. Rule 5: An index bit from the page number lets synonyms pick different lines.

    Two copies of one block then exist, and a store updates one.

  6. Rule 6: A design that breaks the limit must deal with synonyms another way.

    The operating system can give all names of a frame the same index bits. Or the hardware checks the other lines.

Sources for These Rules

The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.7. See "Integrating Virtual Memory, TLBs, and Caches": physically and virtually addressed caches, aliasing, and the cache that is indexed with the page offset and tagged physically.

The name page-size limit is ours. The textbook speaks of coordination between page size, cache size and associativity.

A cache can also be tagged with virtual addresses. Then entries of several programs must be told apart, as in the TLB. See the lesson on address-space IDs.

Now Run It

The lab at the end of this page is test code. Its cache is the one of the hand example: 256 lines and blocks of 16 bytes. The index is bits 11 to 4 of the virtual address. Its tag is the frame number.

Pages 00404 and 00407 are two names of frame 06. Memory holds 11111111 everywhere at the start.

Printed by the lab on Icarus. Each row shows the line that the virtual address picked and the frame from the TLB.
load  00404010 | line 001 | frame 06 | miss | data 11111111
load  00407010 | line 001 | frame 06 | hit  | data 11111111
-- lines that hold the block at physical 06010: 1
store 00404010 | line 001 | frame 06 | hit  | data 22222222
load  00407010 | line 001 | frame 06 | hit  | data 22222222
load  00405010 | line 001 | frame 07 | miss | data 11111111

Rules 1 and 2 at work: the first two rows. Both names pick line 001, from offset bits alone. The second load is a hit, because the tag is the frame number, and both names have frame 06.

The third row counts the lines that hold this block: one.

Rule 5 cannot strike here. Row 4 stores through the first name. Row 5 loads through the second name and gets the new value, 22222222.

The last row is another frame at the same offset. It picks the same line and misses on the tag. The physical tag keeps two frames apart, just as in an ordinary cache.

This lab leaves out the time of each lane and the ways. It leaves out tags that are virtual.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Index from the offset, during translationTranslate, then look upThe TLB is off the longest path. One way can be no larger than a page.
A physical tagA virtual tagSynonyms and other programs are told apart by the frame. The compare waits for the TLB.
More ways for more sizeMore sets for more sizeThe limit holds, and synonyms stay harmless. A comparator for every way.
Level 2 physically indexedLevel 2 indexed early tooNo limit on its size and no synonyms. It is slower, which level 2 can afford.

The Trap: One Index Bit Too Many

This is the team from the top of the page. They doubled the cache to 512 lines and left it direct-mapped. The index now needs bit 12. Here is the same run:

The same lab, with 512 lines and an index of bits 12 to 4.
load  00404010 | line 001 | frame 06 | miss | data 11111111
load  00407010 | line 101 | frame 06 | miss | data 11111111
-- lines that hold the block at physical 06010: 2
store 00404010 | line 001 | frame 06 | hit  | data 22222222
load  00407010 | line 101 | frame 06 | hit  | data 11111111

The second row is where it starts. The second name picks line 101, where the first picked 001. It misses and fetches the block again. The third row counts two lines with the same block.

The last row is the failure. The store went into line 001. The load through the other name hits in line 101 and returns 11111111. That is old data, from a clean hit.

A test with one name for each page never shows this. It needs two names that differ in bit 12. Here are two names that agree in it, on the same cache:

The cache of 512 lines again. The second name is now page 00406, with bit 12 at 0 like the first.
load  00404010 | line 001 | frame 06 | miss | data 11111111
load  00406010 | line 001 | frame 06 | hit  | data 11111111
-- lines that hold the block at physical 06010: 1
store 00404010 | line 001 | frame 06 | hit  | data 22222222
load  00406010 | line 001 | frame 06 | hit  | data 22222222

One line, one copy, the right data. This is the second half of rule 6: the operating system can make the large cache safe by its choice of names. The hardware then depends on software for its correctness.

At Scale

This page256 lines of 16 bytes: one way of 4 KiB
A real level 1many ways of 4 KiB each, side by side

This section looks ahead. The page-size limit shapes the level-1 caches of real processors.

  1. Why level 1 has so many ways

    With pages of 4 KiB, a level 1 of 32 KiB that indexes early needs 8 ways. The limit, not the miss rate, sets that number.

  2. Larger pages, larger ways

    A system whose smallest page is 16 KiB can use ways of 16 KiB. The textbook names a processor that does this.

  3. Level 2 does not care

    A level-2 cache is looked up after translation. It is physically indexed, and its size is free.

  4. Shared levels

    The larger levels of a big chip are shared by cores: The Cache Hierarchy of a Core Cluster.

Try It

Run the lab. Check the lines of the first two rows against the hand table.

Change INDEX_BITS to 9. Which rows change, and which data is now wrong?

With INDEX_BITS at 9, change the second name to 00406010 in both rows. Explain why the data is right again.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: load 00404010 | line 001 | frame 06 | miss | data 11111111 load 00407010 | line 001 | frame 06 | hit | data 11111111 -- lines that hold the block at physical 06010: 1 store 00404010 | line 001 | frame 06 | hit | data 22222222 load 00407010 | line 001 | frame 06 | hit | data 22222222 load 00405010 | line 001 | frame 07 | miss | data 11111111

Check Yourself

Predict the outputThe cache has 512 lines, and every store also goes to memory. A store of 33333333 goes through the name 00407020. Then the same word is loaded through 00406020, and through 00404020. What does the last row say?
// A cache whose index comes from the virtual address, and whose tag is the frame number from the TLB.
module tb;
  localparam INDEX_BITS = 9;                   // bits 12 to 4 of the address: bit 12 belongs to the page number
  localparam LINES = 1 << INDEX_BITS;
  logic        valid [0:LINES-1];
  logic [21:0] tag   [0:LINES-1];         // the frame number: a physical tag
  logic [31:0] block [0:LINES-1][0:3];    // four words
  logic [31:0] mem [0:32767];             // physical memory in words

  // The TLB of this lab, as a list. Pages 00404, 00406 and 00407 are three names for frame 06: shared memory.
  function automatic logic [21:0] frame_of(input logic [19:0] page);
    case (page)
      20'h00404, 20'h00406, 20'h00407: frame_of = 22'h06;
      20'h00405:                       frame_of = 22'h07;
      default:                         frame_of = 22'h1f;
    endcase
  endfunction

  task automatic access(input string kind, input logic [31:0] virt, input logic [31:0] value);
    logic [INDEX_BITS-1:0] index;
    logic [21:0] frame;
    logic [31:0] phys;
    logic [1:0]  word;
    bit hit;
    index = virt[4 + INDEX_BITS - 1 : 4];   // lane 1: the index, straight from the virtual address
    frame = frame_of(virt[31:12]);          // lane 2: the TLB, at the same time
    phys  = {frame[19:0], virt[11:0]};
    word  = virt[3:2];
    hit   = valid[index] && tag[index] == frame;       // the two lanes meet at the tag compare
    if (!hit) begin
      for (int w = 0; w < 4; w++) block[index][w] = mem[{phys[31:4], 4'h0} / 4 + w];
      valid[index] = 1;  tag[index] = frame;
    end
    if (kind == "store") begin
      block[index][word] = value;  mem[phys >> 2] = value;   // write-through: the line and memory
    end
    $display("%-5s %08h | line %03h | frame %02h | %s | data %08h", kind, virt, index, frame[7:0], hit ? "hit " : "miss", block[index][word]);
  endtask

  task automatic copies(input logic [31:0] phys);      // how many lines hold the block of this physical address?
    int n;
    n = 0;
    for (int i = 0; i < LINES; i++) if (valid[i] && tag[i] == phys[31:12] && (i % 256) == phys[11:4]) n++;
    $display("-- lines that hold the block at physical %05h: %0d", phys[19:0], n);
  endtask

  initial begin
    for (int i = 0; i < 32768; i++) mem[i] = 32'h1111_1111;
    for (int i = 0; i < LINES; i++) valid[i] = 0;
    access("store", 32'h0040_7020, 32'h3333_3333);
    access("load",  32'h0040_6020, 0);
    access("load",  32'h0040_4020, 0);
  end
endmodule
ConceptWhy can the index come from the virtual address before the TLB answers?
ConceptPages have 4 KiB. A cache indexes with offset bits only. How large may one way be?
ConceptA level-1 cache of 32 KiB must index during translation, with pages of 4 KiB. How many ways does it need at least?
ConceptWhat are synonyms?
ConceptA direct-mapped cache of 8 KiB indexes with a bit of the page number. What can go wrong?

Common Mistakes

Wrong Grow a level-1 cache by adding sets.

More sets need more index bits, and those come from the page number.

Fix Keep each way within the page size, and add ways.

Wrong Test a cache with one virtual name for each frame.

Synonyms only show when two names of one frame differ in an index bit.

Fix Test with two names that differ in the lowest bits of the page number.

Wrong Use a virtual tag to save the wait for the TLB.

The same page number means different frames in different programs.

Fix Tag with the frame number, or tell the programs apart with an ID.

Remember

  • A translation keeps the offset, so offset bits can index the cache while the TLB works.
  • The tag is the frame number. The two lanes meet at the tag compare.
  • One way may be no larger than a page. More size needs more ways.
  • An index bit from the page number lets synonyms create two copies of one block.

Practice

Report a problem with this page