The TLB: A Cache of Translations
Two reads of memory before every access would make virtual memory useless. A tiny cache remembers the last few translations, and almost every access finds its answer there.
You Will Be Able To
- Explain what a TLB stores, and why it is small and fully associative.
- Follow a hit, a miss and a refill, and count the table reads that the TLB saves.
- Explain why the bits of the entry are checked on a hit as well.
- Lesson
- S3.8
- Time
- 25 min
- Runs on
- Your browser
- Check
- 6 questions
A loop adds up an array. Each step fetches an instruction and loads a word. With the tables of the last pages, each of those two accesses needs two more reads first. The processor reads memory six times, where it wanted to read twice.
Before You Start
You need S3.21: a walk of two tables costs two reads of memory. You need S3.22: the bits R, W and X of an entry.
You also need the fully associative cache from S3.4.
| Question | |
|---|---|
| 1 | What does a TLB store? |
| 2 | What happens on a TLB hit, and on a TLB miss? |
| 3 | Why is a TLB fully associative? |
| 4 | Why does a TLB entry hold the bits of the page? |
| 5 | What does a TLB cost when a program uses more pages than it has entries? |
The Idea: Remember the Answer of the Walk
A walk takes two reads of memory, and it gives the same answer every time: page 00403 lies in frame 09. Programs use the same few pages again and again. That is locality once more. So remember the answer.
The memory for these answers is called the TLB. The letters stand for translation lookaside buffer. It is a small cache. Each entry holds a page number, the frame number that the walk found, and the bits of the page.
Every access asks the TLB first. If an entry holds the page, that is a TLB hit. The frame number comes out at once, and no table is read. If no entry holds it, that is a TLB miss. The tables are walked, and the result is stored in an entry. Storing it is called a refill.
A TLB is tiny: this page uses four entries. So it can afford to be fully associative. Any entry may hold any page, and all entries are compared at once. That matters, because a miss here is dear. It costs a whole walk.
The bits of the page must come along into the entry. On a hit, the table is not read. If the TLB did not know that a page has W at 0, nothing would stop a store into it.
New Words
| Word | Meaning |
|---|---|
| TLB | Translation lookaside buffer. A small cache of translations: page numbers and their frame numbers. |
| Entry of a TLB | One remembered translation: a valid bit, a page number, a frame number and the bits of the page. |
| TLB hit | An access whose page number is found in the TLB. No table is read. |
| TLB miss | An access whose page number is not in the TLB. The tables must be walked. |
| Refill of a TLB | Storing the result of a walk in an entry of the TLB. |
| Reach of a TLB | The memory that its entries cover together: the number of entries times the size of a page. |
One Picture: Short Path, Long Path
- Processor
- Memory
- Control / FSM
- Bus
- Ask the TLB first
The page number is compared with all four entries at the same time, as in a fully associative cache.
- A hit: the short path
One entry holds this page. It gives the frame number and the bits of the page. No table is read.
- The bits are checked on every access
The entry carries R, W and X. A store into a page without W is refused here, on a hit too.
- A miss: the long path
No entry holds the page. The tables are walked, which costs two reads of memory.
- Refill
The result of the walk is stored in an entry of the TLB. The next access to this page takes the short path.
Read the diagram as text
- Page number: the top 20 bits of the address
- TLB: four entries, all compared at once
- Check the bits: R, W, X from the entry
- Frame number: joined with the offset
- Table walk: two reads of memory
- Page number → TLB: compare
- TLB → Check the bits: hit: frame and bits
- Check the bits → Frame number: allowed
- TLB → Table walk: miss
- Table walk → TLB: refill
The TLB is a cache. What it caches is not data: it is the last few answers of the table walk.
The Smallest Example, Worked by Hand
A program has its code in page 00403 and its data in page 00404. The TLB has four entries and starts empty. Follow six accesses, and count the reads of the tables.
| Access | Page | In the TLB? | Table reads | TLB after it |
|---|---|---|---|---|
| fetch 00403000 | 00403 | no: miss | 2 | 00403 |
| load 00404010 | 00404 | no: miss | 2 | 00403, 00404 |
| fetch 00403004 | 00403 | yes: hit | 0 | 00403, 00404 |
| load 00404014 | 00404 | yes: hit | 0 | 00403, 00404 |
| fetch 00403008 | 00403 | yes: hit | 0 | 00403, 00404 |
| store 00404018 | 00404 | yes: hit | 0 | 00403, 00404 |
Four table reads in all. Without a TLB the six accesses would need twelve.
Each page costs one walk, at its first use. After that the program runs as if there were no tables. A loop of a million steps on these two pages still needs four table reads.
Now a seventh access: a store to 00403abc. It is a hit, in the entry of the code page. That entry says R and X, and not W. The store is refused with a page fault, and no table was read to find that out.
The Rules
Rule 1: Every access asks the TLB first.
All entries are compared with the page number at the same time.
Rule 2: A hit gives the frame number with no read of the tables.
The offset is joined on, as always.
Rule 3: A miss starts a walk, and the result refills an entry.
A walk that ends in a page fault refills nothing.
Rule 4: An entry carries the bits of its page.
They are checked on every access, hit or miss.
Rule 5: A full TLB must give up an entry for a refill.
This page replaces the entry that was filled longest ago.
Rule 6: The TLB never knows more than the tables.
It holds copies. The tables stay the one true record.
Sources for These Rules
The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.7. See "Making Address Translation Fast: the TLB": a cache of recent translations, with the protection bits kept in each entry.
The textbook "Digital Design and Computer Architecture" by Harris and Harris, section 8.4.3: the translation lookaside buffer as a small fully associative cache.
Four entries and the replacement of the oldest entry are choices of this page. Real TLBs have more entries and various policies.
The tables behind the TLB follow the Sv32 scheme of RISC-V as commonly described. That layout is not checked against the specification.
Now Run It
The lab at the end of this page is test code. It puts a TLB of four entries in front of the tables of the last two pages. Each row is one access, and the last row counts.
fetch 00403000 | TLB miss, walk, entry 0 filled | physical 09000
load 00404010 | TLB miss, walk, entry 1 filled | physical 06010
fetch 00403004 | TLB hit in entry 0 | physical 09004
load 00404014 | TLB hit in entry 1 | physical 06014
fetch 00403008 | TLB hit in entry 0 | physical 09008
store 00404018 | TLB hit in entry 1 | physical 06018
store 00403abc | TLB hit in entry 0 | page fault: W is 0, and this is a store
load 00409000 | TLB miss, walk | page fault: no valid entry
accesses=8 hits=5 walks=3 table_reads=6Rules 1 to 3 at work: the first six rows are the hand table. Two misses fill entries 0 and 1. The next four accesses hit.
Rule 4 at work: row 7. The store to the code page is a hit, and it is still a page fault. The entry brought the bits along.
Rule 3 again: row 8. The page at 00409000 has no entry in the tables. The walk finds that out, and nothing is put into the TLB.
The count: 8 accesses and 6 table reads. Without the TLB there would be 16.
Now give the TLB more pages than it has entries. This run touches five pages in turn, and then the first one again:
fetch 00403000 | TLB miss, walk, entry 0 filled | physical 09000
load 00404000 | TLB miss, walk, entry 1 filled | physical 06000
load 00405000 | TLB miss, walk, entry 2 filled | physical 07000
load 00406000 | TLB miss, walk, entry 3 filled | physical 08000
load 00407000 | TLB miss, walk, entry 0 filled | physical 0a000
fetch 00403004 | TLB miss, walk, entry 1 filled | physical 09004
accesses=6 hits=0 walks=6 table_reads=12Rule 5 at work: the fifth row. A fifth page arrives, and entry 0 is filled again. That entry held the code page. So the last access is a miss, although the page was used moments ago.
This lab leaves out the time of each step and the hardware that walks. It also leaves out what happens when a table changes. The next two pages add those.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| A cache of translations | A walk for every access | Most accesses need no table read. A few dozen entries of hardware. |
| Fully associative | Direct-mapped | No two pages fight over an entry, so walks stay rare. A comparator for every entry. |
| The bits kept in the entry | The bits read from the table each time | Protection costs no read. The entry is wider. |
| Refill only after a good walk | Refill after every walk | A page fault leaves no trace in the TLB. A page that faults is walked again next time. |
The Trap: A Hit That Skips the Check
A team builds the TLB to hold page and frame numbers only. The bits R, W and X are checked by the walk, they say. Here is a store into the code page, tried twice:
store 00403abc | TLB miss, walk, entry 0 filled | page fault: W is 0, and this is a store
fetch 00403000 | TLB hit in entry 0 | physical 09000
store 00403abc | TLB hit in entry 0 | physical 09abcThe first row is right. The store misses the TLB, the walk reads the entry, and W at 0 refuses it.
The third row is the failure. It is the same store. By now a fetch has put the code page into the TLB. The store hits, no walk happens, and so nothing checks W. The word is written into the code.
The protection worked on the first try and failed on the second. Rule 4 is the check: test every refusal twice, once as a miss and once as a hit.
At Scale
This section looks ahead. Real TLBs are larger, and they still cover little of a large memory.
Reach
Four entries of pages of 4 KiB cover 16 KiB. A program that jumps around in more memory than that misses often.
Large pages
One entry for a large page covers far more memory. That is the main reason large pages exist. Common practice.
Two levels, two TLBs
Common practice is a small TLB for fetches, one for data, and a larger one behind both.
Who does the walk
In many processors a small state machine walks the tables: the next lesson. In others, software does it.
Try It
Run the lab. Check the first six rows against the hand table.
Add a second load of 00409000 before totals. Is it a hit? Explain the count of table reads.
Replace the body by five loads of five different data pages, twice round. Predict the hits before you run it.
fetch 00403000 | TLB miss, walk, entry 0 filled | physical 09000 load 00404010 | TLB miss, walk, entry 1 filled | physical 06010 fetch 00403004 | TLB hit in entry 0 | physical 09004 load 00404014 | TLB hit in entry 1 | physical 06014 fetch 00403008 | TLB hit in entry 0 | physical 09008 store 00404018 | TLB hit in entry 1 | physical 06018 store 00403abc | TLB hit in entry 0 | page fault: W is 0, and this is a store load 00409000 | TLB miss, walk | page fault: no valid entry accesses=8 hits=5 walks=3 table_reads=6Check Yourself
Common Mistakes
Wrong Keep only page and frame numbers in the TLB.
On a hit the table is not read, so nothing else can check the bits.
Fix Store R, W and X in the entry, and check them on every access.
Wrong Refill the TLB after a walk that faulted.
The entry would stand for a page that has no valid translation.
Fix Refill only with a valid entry of the last level.
Wrong Size the TLB by the number of programs.
What counts is how many pages a program uses at the same time.
Fix Compare the reach of the TLB with the memory that the inner loops touch.
Remember
- The TLB is a small cache of translations: page number in, frame number out.
- A hit needs no table read. A miss costs a walk, and the result refills an entry.
- The bits of the page are kept in the entry and checked on every access.
- The TLB holds copies. The tables stay the true record.