The Page-Table Walker
Someone must read the tables when the TLB misses. In many processors it is a state machine of three states, and the processor stands still while it works.
You Will Be Able To
- Name the three states of a walker for two levels, and what each one reads.
- Follow a TLB miss edge by edge, and count the edges that the processor waits.
- List the answers of memory that end a walk with a page fault.
- Lesson
- S3.23
- Time
- 25 min
- Runs on
- Your browser
- Check
- 6 questions
A program fetches from page 00403, and the TLB misses. Hardware reads the tables. Afterwards the TLB says that page 00403 lies in frame 06, a frame of data. The tables were right all along.
Before You Start
You need S3.21: the walk of two tables by hand. You need S3.8: TLB miss and refill.
It helps to know S3.5: a controller that makes the processor wait during a miss.
| Question | |
|---|---|
| 1 | What starts the walker, and what does the processor do meanwhile? |
| 2 | What does the walker read in each of its states? |
| 3 | Why must the walker keep the page number? |
| 4 | Which answers of memory end a walk with a page fault? |
| 5 | How many edges does a TLB miss cost? |
The Idea: The Walk as a State Machine
You have done a table walk by hand: two reads, each at an address worked out from the address being translated. A walker is the hardware that does the same two reads.
It is a small state machine, much like the cache controller. It rests in IDLE. A TLB miss wakes it. It then makes one read of memory in each of two states. The first is called LEVEL1. The second is called LEVEL0.
In LEVEL1 it reads the entry of the root table. In LEVEL0 it reads the entry of the second table. Each read may take several edges, so each state waits until memory answers.
The walk needs the page number twice: its upper half for the first read, its lower half for the second. The processor is not bound to hold its address still for that long. So the walker stores the page number when it starts.
A walk ends in one of two ways. A valid entry for a page at the last level, a page entry, goes into the TLB: a refill. Anything else is a page fault, and the TLB is left alone.
All this time the processor waits. A hit costs it nothing. A miss costs it the whole walk.
New Words
| Word | Meaning |
|---|---|
| Walker | The hardware that reads the page tables after a TLB miss. Also called a page-table walker. |
| LEVEL1 | The state of this walker in which it reads the entry of the root table. |
| LEVEL0 | The state of this walker in which it reads the entry of the second table. |
| Kept page number | The copy of the page number that the walker stores when it starts. |
| Pointer entry | A valid entry that holds the frame of another table. Its bits R, W and X are all 0. |
| Page entry | A valid entry that holds the frame of a page. At least one of R and X is 1. |
One Picture: The States of the Walker
- Control / FSM
- Memory
- Bus
- A TLB miss starts it
The walker leaves IDLE. It keeps the page number, because it needs both halves of it later. The processor waits.
- LEVEL1: the first read
It reads the entry of the root table, and stays here until memory answers.
- A pointer leads on
A valid entry that points to a second table takes the walker to LEVEL0. It keeps the frame number of that table.
- LEVEL0: the second read
It reads the entry of the second table. A valid page ends the walk with a refill of the TLB.
- Every other answer is a fault
An entry that is not valid ends the walk. So does a pointer at the last level. Nothing is refilled.
Read the diagram as text
- IDLE: nothing to do
- LEVEL1: read the entry of the root table
- LEVEL0: read the entry of the second table
- Refill: page and frame go into the TLB
- Page fault: no refill
- IDLE → LEVEL1: TLB miss
- LEVEL1 → LEVEL0: valid, a pointer
- LEVEL0 → Refill: valid, a page
- LEVEL1 → Page fault: anything else
- LEVEL0 → Page fault: anything else
From every ending the walker returns to IDLE. The processor then tries the access again, or takes the page fault.
The Smallest Example, Worked by Hand
Take the tables of the earlier pages. The root table is in frame 10. A fetch from 00403000 misses the TLB. The page number is 00403: upper index 1, lower index 3. Memory answers a read in one edge.
| Edge | State | What the walker does | What it keeps |
|---|---|---|---|
| 1 | IDLE | sees the TLB miss | page 00403 |
| 2 | LEVEL1 | reads 10000 + 4 x 1 = 10004, gets 00004401: a pointer | frame 11 |
| 3 | LEVEL0 | reads 11000 + 4 x 3 = 1100c, gets 0000245b: a page | frame 09, into the TLB |
| 4 | IDLE | nothing: the access hits the TLB now |
Four edges from the miss to the hit. The processor waits for three of them.
| Clock edge | A TLB hit | A TLB miss |
|---|---|---|
| 1 | TLB hit: the access goes on | TLB miss: the walker starts |
| 2 | the next access | LEVEL1: memory gives the root entry |
| 3 | the next access | LEVEL0: memory gives the second entry, and the TLB is refilled |
| 4 | the next access | TLB hit: the access goes on |
With a memory that answers in one edge. The hit costs the processor nothing. The miss costs three edges of waiting.
Now a slower memory, which needs three edges for a read. The walker stays in LEVEL1 for three edges, and in LEVEL0 for three. The miss takes 1 + 3 + 3 + 1 = 8 edges.
The Rules
Rule 1: A TLB miss starts the walker, and the processor waits.
The walker stores the page number at that edge.
Rule 2: LEVEL1 reads the root table at 4 times the upper index.
It stays in LEVEL1 until memory answers.
Rule 3: A pointer entry leads to LEVEL0.
The walker stores the frame number of the second table.
Rule 4: LEVEL0 reads the second table at 4 times the lower index.
A page entry there ends the walk with a refill.
Rule 5: Every other answer ends the walk with a page fault.
An entry that is not valid, or a pointer at the last level. Nothing is refilled.
Rule 6: After a refill the access is tried again.
It now hits the TLB, and its bits are checked there.
Sources for These Rules
The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.7. See "Handling TLB Misses and Page Faults". A TLB miss can be handled in hardware or in software. It ends in a refill or in a page fault.
The textbook "Digital Design and Computer Architecture" by Harris and Harris, sections 8.4.3 and 8.4.6. They cover a TLB miss and the reads of a table of two levels.
The state names LEVEL1 and LEVEL0, and the wait of one edge to see the miss, are choices of this page.
This walker treats a page entry in the root table as a fault. The Sv32 scheme of RISC-V allows a large page there. That is stated from common knowledge and not checked.
Now Run It
The lab at the end of this page is a script in test code. It acts out the walker with one row for each clock edge. It is not the circuit: it has no state register. A TLB of one entry stands in front of it.
e1 IDLE | access 00403000: TLB miss. The walker keeps page 00403, and the processor waits
e2 LEVEL1 | memory: read 10004 = 00004401. Valid, points to the table in frame 11
e3 LEVEL0 | memory: read 1100c = 0000245b. Valid, a page in frame 09: refill the TLB for page 00403
e4 IDLE | access 00403000: TLB hit, frame 09. The processor goes on
e5 IDLE | access 00403004: TLB hit, frame 09. The processor does not wait
-- a miss took 4 edges and a hit took 1
e6 IDLE | access 00800000: TLB miss. The walker keeps page 00800, and the processor waits
e7 LEVEL1 | memory: read 10008 = 00000000. Not valid: page fault
e8 IDLE | access 00405000: TLB miss. The walker keeps page 00405, and the processor waits
e9 LEVEL1 | memory: read 10004 = 00004401. Valid, points to the table in frame 11
e10 LEVEL0 | memory: read 11014 = 00004c01. Points on, but there is no third level: page fault
-- 10 edges in allRules 1 to 4 at work: edges 1 to 4 are the hand table. The fetch is a TLB miss, the walker makes its two reads, and the TLB is refilled with frame 09.
Rule 6 at work: edge 4, and then edge 5. The same access hits. The next fetch from that page hits at once: one edge, where the miss took four.
Rule 5 at work, twice. At edge 7 the root entry is not valid, and the walk ends after one read. At edge 10 the last level holds a pointer. That is a mistake in the tables, and the walker must not follow it.
Now each memory read takes three edges.
e1 IDLE | access 00403000: TLB miss. The walker keeps page 00403, and the processor waits
e2 LEVEL1 | memory: read 10004, wait
e3 LEVEL1 | memory: read 10004, wait
e4 LEVEL1 | memory: read 10004 = 00004401. Valid, points to the table in frame 11
e5 LEVEL0 | memory: read 1100c, wait
e6 LEVEL0 | memory: read 1100c, wait
e7 LEVEL0 | memory: read 1100c = 0000245b. Valid, a page in frame 09: refill the TLB for page 00403
e8 IDLE | access 00403000: TLB hit, frame 09. The processor goes on
-- the miss took 8 edgesThe walker stays in LEVEL1 for edges 2 to 4. It stays in LEVEL0 for edges 5 to 7. The miss takes 8 edges. On a real chip the tables may lie in slow memory, and a miss can take far longer.
This lab leaves out the bits of the entry and the larger TLB. It also leaves out what the processor does with a fault.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| A walker in hardware | A handler in software | A miss costs a few reads, with no jump into the operating system. The table format is fixed in silicon. |
| The processor waits | The processor does other work | Nothing can use a half-finished translation. Every miss stops the program. |
| One state for each level | One state with a counter | Each state has one job. A third level would need a third state. |
| A stored page number | The address wires of the processor | The walk does not depend on what the processor does meanwhile. Twenty flip-flops. |
The Trap: A Walker That Forgets the Page Number
A team saves the twenty flip-flops. Their walker takes each index from the address wires when it needs it. In their processor those wires can change during a walk. Here they move on to page 00404:
e1 IDLE | access 00403000: TLB miss. The walker starts, and the processor waits
e2 LEVEL1 | memory: read 10004 = 00004401. Valid, points to the table in frame 11
e3 LEVEL0 | memory: read 11010 = 000018d7. Valid, a page in frame 06: refill the TLB for page 00403
e4 IDLE | access 00403000: TLB hit, frame 06. The processor goes on
e5 IDLE | access 00403000: TLB hit, frame 06. The processor does not waitEdge 3 is the failure. The first read used index 1 of page 00403. The second read used index 4, which belongs to page 00404. It found frame 06, the data page.
The TLB now says that page 00403 lies in frame 06. That is the story from the top of the page. The last row shows the damage. A fetch from the code page gets its instructions from a data frame, with a clean TLB hit.
No table was wrong, and no fault was raised. Rule 1 is the check: the walker stores the page number at the start, and uses only that copy.
At Scale
This section looks ahead. Real walkers do the same reads, with more levels and a few shortcuts.
More levels, more reads
A table of four levels needs four reads for a walk. Walkers keep the upper entries in a small cache of their own. Common practice.
The reads use the data cache
The entries are ordinary words of memory. Common practice is to fetch them through the cache, so most reads are fast.
Notes in the entry
Some walkers also set the accessed and dirty bits of an entry, with a write. Not checked.
When the tables change
The TLB keeps copies. The next lesson is about copies that have gone out of date.
Try It
Run the lab. Check edges 1 to 4 against the hand table.
Set MEM_EDGES to 2. Predict the edge number of the last row, and run it.
Add an access to 00404010 after the first two, with page 00404 as its second argument. At which address is its second read?
e1 IDLE | access 00403000: TLB miss. The walker keeps page 00403, and the processor waits e2 LEVEL1 | memory: read 10004 = 00004401. Valid, points to the table in frame 11 e3 LEVEL0 | memory: read 1100c = 0000245b. Valid, a page in frame 09: refill the TLB for page 00403 e4 IDLE | access 00403000: TLB hit, frame 09. The processor goes on e5 IDLE | access 00403004: TLB hit, frame 09. The processor does not wait -- a miss took 4 edges and a hit took 1 e6 IDLE | access 00800000: TLB miss. The walker keeps page 00800, and the processor waits e7 LEVEL1 | memory: read 10008 = 00000000. Not valid: page fault e8 IDLE | access 00405000: TLB miss. The walker keeps page 00405, and the processor waits e9 LEVEL1 | memory: read 10004 = 00004401. Valid, points to the table in frame 11 e10 LEVEL0 | memory: read 11014 = 00004c01. Points on, but there is no third level: page fault -- 10 edges in allCheck Yourself
Common Mistakes
Wrong Take the page number from the address wires at each level.
The wires may change during the walk, and the two reads then belong to different pages.
Fix Store the page number when the walk starts.
Wrong Follow any valid entry at the last level.
A pointer there leads into a frame that is not a table of this walk.
Fix Accept only a page entry at the last level. Everything else is a fault.
Wrong Refill the TLB before memory has answered.
The entry on the wires is not the answer yet.
Fix Stay in the state until memory answers, and only then use the entry.
Remember
- A walker has three states: IDLE, LEVEL1 and LEVEL0. Each level is one read.
- It stores the page number at the start, and the frame of the second table after the first read.
- A page entry at the last level refills the TLB. Every other answer is a page fault.
- A hit costs nothing. A miss costs the whole walk, and the processor waits.