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.

Five questions this page answers
Question
1What starts the walker, and what does the processor do meanwhile?
2What does the walker read in each of its states?
3Why must the walker keep the page number?
4Which answers of memory end a walk with a page fault?
5How 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

New words on this page
WordMeaning
WalkerThe hardware that reads the page tables after a TLB miss. Also called a page-table walker.
LEVEL1The state of this walker in which it reads the entry of the root table.
LEVEL0The state of this walker in which it reads the entry of the second table.
Kept page numberThe copy of the page number that the walker stores when it starts.
Pointer entryA valid entry that holds the frame of another table. Its bits R, W and X are all 0.
Page entryA 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

Three States, Two ReadsStep through the picture. The walker leaves IDLE on a TLB miss, and comes back with a refill or with a fault.
  • Control / FSM
  • Memory
  • Bus
Three States, Two ReadsStep through the picture. The walker leaves IDLE on a TLB miss, and comes back with a refill or with a fault. component IDLE nothing to do component LEVEL1 read the entry of theroot table component LEVEL0 read the entry of thesecond table component Refill page and frame gointo the TLB component Page fault no refill TLB missvalid, a pointervalid, a pageanything elseanything else
  1. component IDLEnothing to do
    • TLB miss → LEVEL1
  2. component LEVEL1read the entry of the root table
    • valid, a pointer → LEVEL0
    • anything else → Page fault
  3. component LEVEL0read the entry of the second table
    • valid, a page → Refill
    • anything else → Page fault
  4. component Refillpage and frame go into the TLB
  5. component Page faultno refill
  1. 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.

  2. LEVEL1: the first read

    It reads the entry of the root table, and stays here until memory answers.

  3. 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.

  4. LEVEL0: the second read

    It reads the entry of the second table. A valid page ends the walk with a refill of the TLB.

  5. 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.

One TLB miss, edge by edge
EdgeStateWhat the walker doesWhat it keeps
1IDLEsees the TLB misspage 00403
2LEVEL1reads 10000 + 4 x 1 = 10004, gets 00004401: a pointerframe 11
3LEVEL0reads 11000 + 4 x 3 = 1100c, gets 0000245b: a pageframe 09, into the TLB
4IDLEnothing: the access hits the TLB now

Four edges from the miss to the hit. The processor waits for three of them.

A hit and a miss on one timeline
Clock edgeA TLB hitA TLB miss
1TLB hit: the access goes onTLB miss: the walker starts
2the next accessLEVEL1: memory gives the root entry
3the next accessLEVEL0: memory gives the second entry, and the TLB is refilled
4the next accessTLB 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

  1. Rule 1: A TLB miss starts the walker, and the processor waits.

    The walker stores the page number at that edge.

  2. Rule 2: LEVEL1 reads the root table at 4 times the upper index.

    It stays in LEVEL1 until memory answers.

  3. Rule 3: A pointer entry leads to LEVEL0.

    The walker stores the frame number of the second table.

  4. Rule 4: LEVEL0 reads the second table at 4 times the lower index.

    A page entry there ends the walk with a refill.

  5. 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.

  6. 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.

Printed by the lab on Icarus. The second column is the state of the walker at that edge.
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 all

Rules 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.

The same lab, with a slower memory.
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 edges

The 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 choiceThe alternativeWhat the choice buys, and costs
A walker in hardwareA handler in softwareA miss costs a few reads, with no jump into the operating system. The table format is fixed in silicon.
The processor waitsThe processor does other workNothing can use a half-finished translation. Every miss stops the program.
One state for each levelOne state with a counterEach state has one job. A third level would need a third state.
A stored page numberThe address wires of the processorThe 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:

The same lab, with a walker that reads the page number from the wires at each level.
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 wait

Edge 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 page2 levels, 2 reads
A 64-bit processor3 or 4 levels, and caches inside the walker

This section looks ahead. Real walkers do the same reads, with more levels and a few shortcuts.

  1. 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.

  2. 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.

  3. Notes in the entry

    Some walkers also set the accessed and dirty bits of an entry, with a write. Not checked.

  4. 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?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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 all

Check Yourself

Predict the outputMemory needs 2 edges for a read. The script runs a load from page 00404, a second load from that page, and a load from page 00000. What does the last row say?
// A page-table walker, acted out one clock edge at a time. This is a script in test code, not the circuit.
module tb;
  logic [31:0] mem [0:32767];          // physical memory in words: 32 frames of 4096 bytes
  logic [21:0] root;                   // the frame that holds the first table
  bit show = 0;                        // 1: print a row for each table read
  // The low bits of an entry. D A G U X W R V are bits 7 to 0.
  localparam logic [7:0] V = 8'h01, R = 8'h02, W = 8'h04, X = 8'h08, U = 8'h10, A = 8'h40, D = 8'h80;

  task automatic put(input logic [21:0] table_frame, input int index, input logic [21:0] frame, input logic [7:0] bits);
    mem[table_frame * 1024 + index] = {frame, 2'b00, bits};      // an entry has 4 bytes, so a table of 1024 entries fills one frame
  endtask

  // Walk the two levels for one virtual address. why: 0 found, 1 no valid entry at level 1, 2 no valid entry at level 0,
  // 3 the last level points on to another table.
  task automatic walk(input logic [31:0] virt, output int why, output logic [31:0] found, output logic [31:0] phys);
    logic [9:0]  index1, index0;
    logic [31:0] at, e;
    index1 = virt[31:22];  index0 = virt[21:12];
    why = 0;  found = 0;  phys = 0;
    at = {root, 12'h000} + 4 * index1;
    e  = mem[at >> 2];
    if (!(e & V)) begin
      if (show) $display("  level 1: entry %0d of the table in frame %02h, at %05h = %08h | not valid", index1, root[7:0], at[19:0], e);
      why = 1;
    end else if (e & (R | W | X)) begin                           // a leaf already: one entry for 4 MiB (1024 pages)
      if (show) $display("  level 1: entry %0d of the table in frame %02h, at %05h = %08h | valid, a large page that starts at frame %03h", index1, root[7:0], at[19:0], e, e[21:10]);
      found = e;  phys = {e[31:20], virt[21:0]};
    end else begin
      if (show) $display("  level 1: entry %0d of the table in frame %02h, at %05h = %08h | valid, points to the table in frame %02h", index1, root[7:0], at[19:0], e, e[17:10]);
      at = {e[31:10], 12'h000} + 4 * index0;
      e  = mem[at >> 2];
      if (!(e & V)) begin
        if (show) $display("  level 0: entry %0d of that table, at %05h = %08h | not valid", index0, at[19:0], e);
        why = 2;
      end else if (!(e & (R | W | X))) begin
        if (show) $display("  level 0: entry %0d of that table, at %05h = %08h | points on, but there is no third level", index0, at[19:0], e);
        why = 3;
      end else begin
        if (show) $display("  level 0: entry %0d of that table, at %05h = %08h | valid, the page is in frame %02h", index0, at[19:0], e, e[17:10]);
        found = e;  phys = {e[31:10], virt[11:0]};
      end
    end
  endtask

  localparam MEM_EDGES = 2;            // the edges that memory needs to answer one read
  logic        t_valid = 0;            // a TLB of one entry is enough for this script
  logic [19:0] t_page;
  logic [21:0] t_frame;
  string state = "IDLE";
  int edge_no = 0;

  task automatic tick(input string what);          // one clock edge, and one row
    edge_no++;
    $display("e%0d %-6s | %s", edge_no, state, what);
    #10;
  endtask

  task automatic read_entry(input logic [31:0] at, output logic [31:0] e);   // the read lasts MEM_EDGES edges
    for (int k = 1; k < MEM_EDGES; k++) tick($sformatf("memory: read %05h, wait", at[19:0]));
    e = mem[at >> 2];
  endtask

  // One access of the processor. wires_later: the page number on the address wires after the first read.
  task automatic access(input logic [31:0] virt, input logic [19:0] wires_later);
    logic [19:0] kept;                 // the page number that the walker keeps for the whole walk
    logic [31:0] at, e;
    state = "IDLE";
    if (t_valid && t_page == virt[31:12]) begin
      tick($sformatf("access %08h: TLB hit, frame %02h. The processor does not wait", virt, t_frame[7:0]));
    end else begin
      kept = virt[31:12];
      tick($sformatf("access %08h: TLB miss. The walker keeps page %05h, and the processor waits", virt, kept));
      state = "LEVEL1";
      at = {root, 12'h000} + 4 * kept[19:10];
      read_entry(at, e);
      if (!(e & V))               begin tick($sformatf("memory: read %05h = %08h. Not valid: page fault", at[19:0], e));  state = "IDLE"; end
      else if (e & (R | W | X))   begin tick($sformatf("memory: read %05h = %08h. A page at the first level: this walker has no large pages, page fault", at[19:0], e));  state = "IDLE"; end
      else begin
        tick($sformatf("memory: read %05h = %08h. Valid, points to the table in frame %02h", at[19:0], e, e[17:10]));
        state = "LEVEL0";
        at = {e[31:10], 12'h000} + 4 * kept[9:0];
        read_entry(at, e);
        if (!(e & V))               begin tick($sformatf("memory: read %05h = %08h. Not valid: page fault", at[19:0], e));  state = "IDLE"; end
        else if (!(e & (R | W | X))) begin tick($sformatf("memory: read %05h = %08h. Points on, but there is no third level: page fault", at[19:0], e));  state = "IDLE"; end
        else begin
          tick($sformatf("memory: read %05h = %08h. Valid, a page in frame %02h: refill the TLB for page %05h", at[19:0], e, e[17:10], kept));
          t_valid = 1;  t_page = kept;  t_frame = e[31:10];
          state = "IDLE";
          tick($sformatf("access %08h: TLB hit, frame %02h. The processor goes on", virt, t_frame[7:0]));
        end
      end
    end
  endtask

  initial begin
    for (int i = 0; i < 32768; i++) mem[i] = 32'h0;
    root = 22'h10;
    put(22'h10, 0, 22'h12, V);
    put(22'h10, 1, 22'h11, V);
    put(22'h11, 3, 22'h09, V | R | X | U | A);
    put(22'h11, 4, 22'h06, V | R | W | U | A | D);
    put(22'h11, 5, 22'h13, V);                       // a mistake in the tables: the last level points on
    put(22'h12, 0, 22'h05, V | R | W | U | A | D);
    access(32'h0040_4010, 20'h00404);
    access(32'h0040_4014, 20'h00404);
    access(32'h0000_0ffc, 20'h00000);
    $display("3 accesses took %0d edges", edge_no);
  end
endmodule
ConceptWhat wakes the walker?
ConceptWhat does the walker read in LEVEL0?
ConceptWhy does the walker store the page number at the start?
ConceptThe entry read in LEVEL0 is valid and has R, W and X all at 0. What does the walker do?
ConceptWhat does the processor do during a walk?

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.

Practice

Report a problem with this page