The Two-Level Page Table

A flat page table for 32-bit addresses needs a million entries, used or not. Cut the table in two levels, and the parts that nobody uses need not exist.

You Will Be Able To

  • Work out the size of a flat page table, and say why it is too large.
  • Split a 32-bit address into two indexes and an offset, and walk two tables by hand.
  • Explain what the two levels save, and what they cost.
Lesson
S3.21
Time
26 min
Runs on
Your browser
Check
6 questions

A small program uses three pages of memory. Its page table, built as on the last page, fills four million bytes. A hundred such programs spend four hundred million bytes on tables that are almost all empty.

Before You Start

You need S3.7: page, frame, offset, the page table and the page fault.

You also need hex and binary side by side: one hex digit is four bits.

Five questions this page answers
Question
1Why is one flat page table too large for addresses of 32 bits?
2How is a 32-bit address cut for a table of two levels?
3What does an entry of the root table hold?
4How many reads of memory does one translation need?
5What do the two levels save, and when?

The Idea: A Table of Tables

The table of the last page had 16 entries, one for each page. Real addresses have 32 bits. With pages of 4096 bytes that makes 1,048,576 pages. A flat table needs an entry for every one of them.

An entry has 4 bytes. MiB means 1024 times 1024 bytes. So the flat table of one program fills 4 MiB, whether the program uses a million pages or three. Most programs use a few pages near the bottom of their addresses and a few near the top. The rest of the table says "not valid", a million times.

The cure is to cut the table into pieces of 1024 entries, and to keep a short list of the pieces. That list is the root table. It has 1024 entries too. Each one says where one piece is stored, or that the piece does not exist.

A piece that would hold only "not valid" is simply left out. One entry of the root table, marked not valid, stands for all 1024 pages of it.

A translation now takes two steps. The first 10 bits of the address pick an entry of the root table. It leads to a second table. The next 10 bits pick an entry there. It holds the frame of the page. Going through the tables like this is called a table walk.

Each table has 1024 entries of 4 bytes. That is 4096 bytes: exactly one frame. So a table is stored in a frame like any page, and an entry can point to it with a frame number.

New Words

New words on this page
WordMeaning
Flat page tableA page table in one piece, with an entry for every page. The table of the last page.
Two-level page tableA page table built as a root table and second tables.
Root tableThe first table of a walk. Each valid entry holds the frame number of a second table.
Second tableA table of the last level. Each valid entry holds the frame number of a page.
Upper index and lower indexOur names for the two parts of the page number. The upper index picks an entry of the root table. The lower index picks an entry of the second table.
Table walkReading one entry at each level, to get from a virtual address to a frame. Walk for short.
KiB and MiBSizes of memory. A KiB is 1024 bytes, and a MiB is 1024 KiB.
Sv32The name that RISC-V gives its translation scheme for addresses of 32 bits, with two levels.

One Picture: Two Indexes, Two Tables

An Address Walks Two TablesStep through the picture. Each part of the address picks an entry, and each entry says where to go next.
  • Processor
  • Control / FSM
  • Memory
  • Bus
An Address Walks Two TablesStep through the picture. Each part of the address picks an entry, and each entry says where to go next. component Virtual address upper index, lowerindex, offset component Root table one frame, 1024entries component Second table one frame, 1024entries component Frame of the page 4096 bytes of theprogram component No second table the entry is not valid:nothing is stored the upper index picksan entryframe of the nexttablelower index, then theframe of the pagemost entries
  1. component Virtual addressupper index, lower index, offset
    • the upper index picks an entry → Root table
  2. component Root tableone frame, 1024 entries
    • frame of the next table → Second table
    • most entries → No second table
  3. component Second tableone frame, 1024 entries
    • lower index, then the frame of the page → Frame of the page
  4. component Frame of the page4096 bytes of the program
  5. component No second tablethe entry is not valid: nothing is stored
  1. Three parts

    The address is cut into three parts: two indexes of 10 bits and an offset of 12 bits.

  2. The first index

    The upper index picks one of 1024 entries of the root table. The entry holds the frame number of a second table.

  3. The second index

    The lower index picks one of 1024 entries of that second table. This entry holds the frame number of the page.

  4. Where nothing is mapped

    An entry of the root table covers 1024 pages. If the program uses none of them, the entry is not valid, and no second table is stored.

Read the diagram as text
  • Virtual address: upper index, lower index, offset
  • Root table: one frame, 1024 entries
  • Second table: one frame, 1024 entries
  • Frame of the page: 4096 bytes of the program
  • No second table: the entry is not valid: nothing is stored
  • Virtual address → Root table: the upper index picks an entry
  • Root table → Second table: frame of the next table
  • Second table → Frame of the page: lower index, then the frame of the page
  • Root table → No second table: most entries

Two reads of memory for one translation. In return, tables exist only where the program has pages.

The Smallest Example, Worked by Hand

Translate the address 00403abc. The root table is in frame 10 (hex), so it starts at physical address 10000.

First cut the address. In binary it is 0000000001, then 0000000011, then 101010111100. So the upper index is 1, the lower index is 3, and the offset is abc. The lab prints them as index1 and index0.

One walk, step by step
StepWhat is doneResult
1Find the entry of the root table: 10000 + 4 x 1address 10004
2Read it00004401
3Bit 0 is 1, so it is valid. Take bits 31 to 10frame 11: a second table, at 11000
4Find the entry of the second table: 11000 + 4 x 3address 1100c
5Read it0000245b
6Bit 0 is 1, so it is valid. Take bits 31 to 10frame 09: the page
7Put the offset behind the frame numberphysical address 09abc

The index is multiplied by 4 because an entry has 4 bytes. All numbers are hex, except the indexes.

How do bits 31 to 10 of 00004401 give 11? Drop the last 10 bits. In hex, 4401 is 0100 0100 0000 0001. Without its last ten bits, 010001 is left: 11 in hex.

How does the walk know that the first entry points to a table, and the second to a page? Three more bits of the entry say so. On this page it is enough that the tables are built correctly. The next page reads those bits.

The Rules

  1. Rule 1: A 32-bit address is cut into 10, 10 and 12 bits.

    The upper index, the lower index and the offset. Together the two indexes are the page number.

  2. Rule 2: An entry lies at the start of its table plus 4 times its index.

    An entry has 4 bytes.

  3. Rule 3: A valid entry of the root table holds the frame of a second table.

    A valid entry of a second table holds the frame of the page.

  4. Rule 4: An entry that is not valid ends the walk with a page fault.

    At the root this rules out 1024 pages with one read.

  5. Rule 5: A table is one frame.

    So tables are placed and found like pages, by frame number.

  6. Rule 6: Every level is one read of memory.

    A translation with two levels costs two reads before the access itself.

Sources for These Rules

The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.7. See "Virtual Memory for Large Virtual Addresses": the size of a flat table, and multiple levels of page tables as one cure.

The textbook "Digital Design and Computer Architecture" by Harris and Harris, section 8.4.6: multilevel page tables, with a table of two levels.

The exact layout is that of the Sv32 scheme of RISC-V: two indexes of 10 bits, entries of 4 bytes, the frame number in bits 31 to 10. We state it from common knowledge. The RISC-V privileged specification is not in our library, so these facts are not checked against it.

The names upper index and lower index are ours. RISC-V documents number the two parts of the page number 1 and 0.

Now Run It

The lab at the end of this page is test code. Its memory has 32 frames. The operating system of the lab builds a root table in frame 10, and second tables in frames 11 and 12. Three pages are mapped.

Then it translates five addresses. Each walk prints its reads.

Printed by the lab on Icarus. Each load shows the cut of the address, the entry read at each level, and the result.
load 00403abc: index1=1 index0=3 offset=abc
  level 1: entry 1 of the table in frame 10, at 10004 = 00004401 | valid, points to the table in frame 11
  level 0: entry 3 of that table, at 1100c = 0000245b | valid, the page is in frame 09
  physical 09abc | data da7a9abc
load 00404010: index1=1 index0=4 offset=010
  level 1: entry 1 of the table in frame 10, at 10004 = 00004401 | valid, points to the table in frame 11
  level 0: entry 4 of that table, at 11010 = 000018d7 | valid, the page is in frame 06
  physical 06010 | data da7a6010
load 00000ffc: index1=0 index0=0 offset=ffc
  level 1: entry 0 of the table in frame 10, at 10000 = 00004801 | valid, points to the table in frame 12
  level 0: entry 0 of that table, at 12000 = 000014d7 | valid, the page is in frame 05
  physical 05ffc | data da7a5ffc
load 00405000: index1=1 index0=5 offset=000
  level 1: entry 1 of the table in frame 10, at 10004 = 00004401 | valid, points to the table in frame 11
  level 0: entry 5 of that table, at 11014 = 00000000 | not valid
  page fault
load 00800000: index1=2 index0=0 offset=000
  level 1: entry 2 of the table in frame 10, at 10008 = 00000000 | not valid
  page fault

Rules 1 to 3 at work: the first four rows are the hand example. The entry at 10004 leads to the table in frame 11. The entry at 1100c leads to frame 09.

Rule 2 at work: the second load is one page further on. It uses the same second table and the next entry, at 11010.

Rule 4 at work: the last two loads. For 00405000 the second table has no valid entry. For 00800000 the root table has none. That one read settles 1024 pages, which are 4 MiB of addresses.

Now the saving. This program has three tables of one frame each: 12 KiB. A flat table would be 4 MiB. The price is rule 6: two reads of memory before every access. A later page removes most of them again.

This lab leaves out who may read or write a page. It also leaves out the hardware that does the walk.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Two levelsOne flat tableTables only where pages are in use. Two reads for a translation where one would do.
A table the size of a frameTables of any sizeThe operating system places tables like pages. The index widths are fixed by it.
Frame numbers in the entriesFull addresses in the entriesTwelve bits of each entry stay free for other uses. Tables must start at the start of a frame.
Not valid at the rootSecond tables for every partOne bit covers 4 MiB of unused addresses. Nothing: it is free.

The Trap: An Index Without Its Factor of Four

A team writes the walk, and adds the index to the start of the table as it is. They forget that an entry has 4 bytes. Here is that walk in the lab, for the address of the hand example:

The same lab, with the index added without the factor 4.
load 00403abc: index1=1 index0=3 offset=abc
  level 1: entry 1 of the table in frame 10, at 10001 = 00004801 | valid, points to the table in frame 12
  level 0: entry 3 of that table, at 12003 = 000014d7 | valid, the page is in frame 05
  physical 05abc | data da7a5abc

Nothing here looks like a failure. The walk reads at 10001, which lies inside entry 0 of the root table. It follows that entry to the table in frame 12. There it lands inside entry 0 again, which is valid.

The result is physical address 05abc. The right answer was 09abc. The program now reads a frame that belongs to another page, and no fault is raised.

Rule 2 is the check. Test a walk with indexes other than 0: with an index of 0, the wrong sum and the right sum are the same.

At Scale

This page2 levels, 3 tables
A 64-bit system3 or 4 levels, thousands of tables

This section looks ahead. Longer addresses need more levels, and the same idea carries on.

  1. More levels for longer addresses

    RISC-V names schemes with three and four levels Sv39 and Sv48. Each level takes 9 bits of the address there. This is not checked against the specification.

  2. Large pages

    An entry of the root table may hold a page itself, of 4 MiB. One entry then serves what would need 1024. Not checked.

  3. The walk in hardware

    A small state machine does these reads. It has a lesson of its own.

  4. Skipping the walk

    A small cache can remember recent translations. That is the subject of a later lesson.

Try It

Run the lab. Check the first four rows against the hand table.

Map the page at 00405000 to frame 07 with one more put. Which table gets the entry? Run the fourth load again.

Map the page at 00800000 to frame 03. How many puts do you need, and how many frames of tables exist then?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: load 00403abc: index1=1 index0=3 offset=abc level 1: entry 1 of the table in frame 10, at 10004 = 00004401 | valid, points to the table in frame 11 level 0: entry 3 of that table, at 1100c = 0000245b | valid, the page is in frame 09 physical 09abc | data da7a9abc load 00404010: index1=1 index0=4 offset=010 level 1: entry 1 of the table in frame 10, at 10004 = 00004401 | valid, points to the table in frame 11 level 0: entry 4 of that table, at 11010 = 000018d7 | valid, the page is in frame 06 physical 06010 | data da7a6010 load 00000ffc: index1=0 index0=0 offset=ffc level 1: entry 0 of the table in frame 10, at 10000 = 00004801 | valid, points to the table in frame 12 level 0: entry 0 of that table, at 12000 = 000014d7 | valid, the page is in frame 05 physical 05ffc | data da7a5ffc load 00405000: index1=1 index0=5 offset=000 level 1: entry 1 of the table in frame 10, at 10004 = 00004401 | valid, points to the table in frame 11 level 0: entry 5 of that table, at 11014 = 00000000 | not valid page fault load 00800000: index1=2 index0=0 offset=000 level 1: entry 2 of the table in frame 10, at 10008 = 00000000 | not valid page fault

Check Yourself

Predict the outputThe lab translates the address 00404ff8 with the same tables. What does the last row say?
// A two-level page table, stored in memory and walked by test code. Virtual addresses have 32 bits.
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 = 1;                        // 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

  task automatic load(input logic [31:0] virt);
    int why;
    logic [31:0] found, phys;
    $display("load %08h: index1=%0d index0=%0d offset=%03h", virt, virt[31:22], virt[21:12], virt[11:0]);
    walk(virt, why, found, phys);
    if (why == 0) $display("  physical %05h | data %08h", phys[19:0], mem[phys >> 2]);
    else          $display("  page fault");
  endtask

  initial begin
    for (int i = 0; i < 32768; i++) mem[i] = 32'h0;
    for (int f = 0; f < 16; f++) for (int w = 0; w < 1024; w++) mem[f * 1024 + w] = 32'hda7a_0000 + (f << 12) + 4 * w;   // data frames: da7a + the address of the word
    // The operating system builds the tables. Three frames hold tables: 10, 11 and 12.
    root = 22'h10;
    put(22'h10, 0, 22'h12, V);                       // index1 = 0: go on to the table in frame 12
    put(22'h10, 1, 22'h11, V);                       // index1 = 1: go on to the table in frame 11
    put(22'h11, 3, 22'h09, V | R | X | U | A);       // index1 = 1, index0 = 3: the page is in frame 09 (code: read and execute)
    put(22'h11, 4, 22'h06, V | R | W | U | A | D);
    put(22'h12, 0, 22'h05, V | R | W | U | A | D);
    load(32'h0040_4ff8);
  end
endmodule
ConceptPages have 4096 bytes and addresses 32 bits. How many entries has a flat page table?
ConceptHow is a 32-bit address cut for the two-level table of this page?
ConceptWhat does a valid entry of the root table hold?
ConceptThe root entry for an address is not valid. How many reads of memory does the walk make?
ConceptWhat do two levels save, compared with a flat table?

Common Mistakes

Wrong Add the index to the start of the table as it is.

An entry has 4 bytes, so entry n starts 4 times n bytes in.

Fix Multiply the index by the size of an entry.

Wrong Cut a 32-bit address at hex digits.

The indexes have 10 bits, and a hex digit has 4.

Fix Write the address in binary, and cut after 10 and after 20 bits.

Wrong Think that two levels make translation faster.

They add a read. What they save is memory for tables.

Fix Count the reads of a walk as a cost, and remove them with a cache of translations.

Remember

  • A flat table for 32-bit addresses has a million entries: 4 MiB for each program.
  • Two levels: the upper index picks a root entry, the lower index an entry of a second table.
  • An entry lies at the start of its table plus 4 times its index.
  • Unused parts cost one root entry. A walk costs two reads.

Practice

Report a problem with this page