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.
| Question | |
|---|---|
| 1 | Why not translate first and look in the cache afterwards? |
| 2 | Which bits of a virtual address can a cache use before the TLB answers? |
| 3 | How large may one way of such a cache be? |
| 4 | What is a synonym, and what goes wrong with it? |
| 5 | How 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
| Word | Meaning |
|---|---|
| Physically indexed cache | A cache whose index comes from the physical address. It must wait for the TLB. |
| Virtually indexed cache | A cache whose index comes from the virtual address. It can start while the TLB works. |
| Physical tag | A tag that is taken from the physical address. On this page it is the frame number. |
| Synonyms | Two virtual addresses that are translated to the same physical address. Also called aliases. |
| Page-size limit | Our name for the rule that index and block offset together may have no more bits than the page offset. |
| Size of a way | The number of sets times the size of a block: the bytes that one way holds. |
One Picture: Two Lanes
- Processor
- Memory
- Control / FSM
- Bus
- 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.
- Lane 1: the TLB
The page number goes to the TLB. That takes time.
- 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.
- 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.
| Virtual address | Index of 8 bits: bits 11 to 4 | Index of 9 bits: bits 12 to 4 |
|---|---|---|
| 00404010 | 01 | 001 |
| 00407010 | 01 | 101 |
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
Rule 1: A translation keeps the offset.
So the offset bits of a virtual address are physical bits already.
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.
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.
Rule 4: A larger cache within this limit needs more ways.
Eight ways of 4 KiB make 32 KiB.
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.
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.
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 11111111Rules 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 choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Index from the offset, during translation | Translate, then look up | The TLB is off the longest path. One way can be no larger than a page. |
| A physical tag | A virtual tag | Synonyms and other programs are told apart by the frame. The compare waits for the TLB. |
| More ways for more size | More sets for more size | The limit holds, and synonyms stay harmless. A comparator for every way. |
| Level 2 physically indexed | Level 2 indexed early too | No 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:
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 11111111The 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:
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 22222222One 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 section looks ahead. The page-size limit shapes the level-1 caches of real processors.
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.
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.
Level 2 does not care
A level-2 cache is looked up after translation. It is physically indexed, and its size is free.
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.
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 11111111Check Yourself
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.