Page-Table Entry Bits and Page Faults

An entry of a page table holds more than a frame number. Seven small bits say who may do what with the page, and what has been done with it so far.

You Will Be Able To

  • Decode the bits V, R, W, X, U, A and D of an entry.
  • Decide whether an access is allowed, and name the bit that refuses it.
  • Describe what happens at a page fault, and three ways it can end.
Lesson
S3.22
Time
26 min
Runs on
Your browser
Check
6 questions

A program has a loop that runs one step too far. It writes one word past the end of its data, into its own code. Without a guard, the program now runs an instruction that nobody wrote.

Before You Start

You need S3.21: an entry of 32 bits. Its valid bit is bit 0, and its frame number is bits 31 to 10.

You also need the page fault from S3.7, and fetch, load and store from S0.6.

Five questions this page answers
Question
1Which bits of an entry say what a page may be used for?
2What does the U bit decide?
3What do the A bit and the D bit record, and who needs them?
4What does the processor do at a page fault?
5How can a page fault end?

The Idea: The Entry Is Also a Guard

So far an entry answered one question: where is the page? But every access goes through the entry. That makes it the right place for a second question: is this access allowed?

Three bits say what a page may be used for. R allows a load. W allows a store. X allows the fetch of an instruction. Code gets R and X, and no W. Data gets R and W, and no X. The loop from the top of the page is then stopped at its first wrong store.

One bit says whose page it is. U marks a page of a user program: an ordinary program, with fewer rights than the operating system. The pages of the operating system have U at 0, and a user program cannot touch them.

Two bits are notes. A, for accessed, says that the page has been used. D, for dirty, says that it has been written. The operating system reads them when it must take a frame away. A page that was never written need not be saved first.

When a check fails, the access does not happen. The processor stops the instruction and hands control to the operating system. That is the page fault of the earlier pages, now with many possible causes.

A fault is a question to the operating system, and it has three kinds of answer. Fix the entry and run the instruction again. Fetch the page from somewhere, and then run it again. Or decide that the program is wrong, and end it.

New Words

New words on this page
WordMeaning
Permission bitsBits 1, 2 and 3 of an entry, named R, W and X. The page may be read, written, and fetched from as instructions.
User bitBit 4 of an entry, named U. The page belongs to a user program.
Accessed bitBit 6 of an entry, named A. The page has been used since the bit was last cleared.
Dirty bit of a pageBit 7 of an entry, named D. The page has been written since the bit was last cleared.
User programAn ordinary program. It runs with fewer rights than the operating system.
Fault handlerThe code of the operating system that runs when a page fault happens.
Faulting addressThe virtual address whose access was refused. The processor keeps it in a register for the handler.

One Picture: Four Checks, One Refusal

Each Check Reads One Bit of the EntryStep through the picture. An access passes four checks in turn. The first bit that says no stops it.
  • Processor
  • Control / FSM
  • Memory
  • Bus
Each Check Reads One Bit of the EntryStep through the picture. An access passes four checks in turn. The first bit that says no stops it. component An access fetch, load or store;user or system component V: valid? is there a frame at all component R, W, X: thiskind? R for a load, W for astore, X for a fetch component U: for this asker? user pages for userprograms component A, D: noted? used before, writtenbefore component Allowed the access goes tothe frame component Page fault the operating systemtakes over entry foundV is 1bit is 1U fitsA, D setV is 0bit is 0wrong askernot noted yet
  1. component An accessfetch, load or store; user or system
    • entry found → V: valid?
  2. component V: valid?is there a frame at all
    • V is 1 → R, W, X: this kind?
    • V is 0 → Page fault
  3. component Page faultthe operating system takes over
  4. component R, W, X: this kind?R for a load, W for a store, X for a fetch
    • bit is 1 → U: for this asker?
    • bit is 0 → Page fault
  5. component U: for this asker?user pages for user programs
    • U fits → A, D: noted?
    • wrong asker → Page fault
  6. component A, D: noted?used before, written before
    • A, D set → Allowed
    • not noted yet → Page fault
  7. component Allowedthe access goes to the frame
  1. Is there a frame?

    V is the valid bit. With V at 0 there is nothing to check. The access faults.

  2. Is this kind of access allowed?

    A load needs R. A store needs W. A fetch of an instruction needs X. A store into a page without W faults here.

  3. Is the asker allowed?

    U marks a page of a user program. A user program is refused on a page without U.

  4. Has the use been noted?

    A says that the page was used. D says that it was written. If the bit is still 0, this lab faults, so that the operating system can note it.

  5. All checks passed

    Only now does the access reach the frame.

Read the diagram as text
  • An access: fetch, load or store; user or system
  • V: valid?: is there a frame at all
  • R, W, X: this kind?: R for a load, W for a store, X for a fetch
  • U: for this asker?: user pages for user programs
  • A, D: noted?: used before, written before
  • Allowed: the access goes to the frame
  • Page fault: the operating system takes over
  • An access → V: valid?: entry found
  • V: valid? → R, W, X: this kind?: V is 1
  • R, W, X: this kind? → U: for this asker?: bit is 1
  • U: for this asker? → A, D: noted?: U fits
  • A, D: noted? → Allowed: A, D set
  • V: valid? → Page fault: V is 0
  • R, W, X: this kind? → Page fault: bit is 0
  • U: for this asker? → Page fault: wrong asker
  • A, D: noted? → Page fault: not noted yet

Seven bits, four questions. A page fault always names the access, and the entry shows which bit said no.

The Smallest Example, Worked by Hand

Take the entry 0000245b. Its frame number is bits 31 to 10: frame 09. Its last two hex digits, 5b, hold the bits of this page. In binary, 5b is 0101 1011.

The low byte 5b, bit by bit
Bit76543210
NameDA(not used here)UXWRV
Value01011011

Valid, readable, executable, a user page, used before, never written. W is 0.

This is a page of code. Now three accesses by a user program:

Three accesses to this page
AccessNeedsThe entry hasResult
fetch an instructionV, X, U, Aall fourallowed
load a wordV, R, U, Aall fourallowed
store a wordV, W, U, A, DW is 0page fault

The store is refused by one bit. The frame is never touched.

The handler now looks at the faulting address and at the entry. W is 0 on purpose: nobody may write code. So this fault ends the program.

The Rules

  1. Rule 1: A load needs R, a store needs W, and a fetch needs X.

    One bit for each kind of access.

  2. Rule 2: A user program needs U at 1.

    In this scheme the operating system is refused on such a page too, unless it sets a switch of its own.

  3. Rule 3: V at 0 refuses everything.

    The other bits of the entry then mean nothing.

  4. Rule 4: A is set at the first use of a page, and D at its first store.

    This lab faults when the bit is still 0, and the handler sets it. Hardware may also set it by itself.

  5. Rule 5: A refused access does not happen.

    No byte of the frame is read or written.

  6. Rule 6: The handler gets the faulting address and the kind of access.

    It fixes the entry and runs the instruction again, or it ends the program.

Sources for These Rules

The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.7. See "Page Faults" and "Implementing Protection with Virtual Memory". They cover the valid bit, the reference and dirty bits, and the work of the operating system on a fault.

The textbook "Digital Design and Computer Architecture" by Harris and Harris, section 8.4.4: memory protection with bits in the page table.

The names and places of the seven bits follow the Sv32 scheme of RISC-V as commonly described. So does the refusal of the operating system on a user page. The RISC-V privileged specification is not in our library, so these facts are not checked against it.

Rule 4 has two forms in practice: a fault that lets software set the bit, or hardware that sets it. This lab uses the first. That is a choice.

The order of the checks in the lab is a choice too. Each access of the lab fails at most one check.

Now Run It

The lab at the end of this page is test code. It uses the tables of the last page. A user program gets three pages: code, data, and data that was never used. A fourth page belongs to the operating system.

Each row is one access. It shows the entry, its bits as letters, and the verdict. A dash stands for a bit at 0.

Printed by the lab on Icarus. The letters are D, A, a spare bit, U, X, W, R and V, in the order of bits 7 to 0.
user fetch 00403000 | entry 0000245b -A-UX-RV | allowed, physical 09000
user store 00403abc | entry 0000245b -A-UX-RV | page fault: W is 0, and this is a store
user load  00404010 | entry 000018d7 DA-U-WRV | allowed, physical 06010
user store 00404010 | entry 000018d7 DA-U-WRV | allowed, physical 06010
user load  00406000 | entry 000020c7 DA---WRV | page fault: U is 0, and a user program asks
os   load  00406000 | entry 000020c7 DA---WRV | allowed, physical 08000
user load  00407000 | no valid entry | page fault: V is 0
user load  00405000 | entry 00001c17 ---U-WRV | page fault: A is 0, the first use of this page
user load  00405000 | entry 00001c57 -A-U-WRV | allowed, physical 07000
user store 00405000 | entry 00001c57 -A-U-WRV | page fault: D is 0, the first store to this page
user store 00405000 | entry 00001cd7 DA-U-WRV | allowed, physical 07000

Rule 1 at work: the first two rows. The fetch from the code page is allowed. The store into it is refused, because W is 0. That is the loop from the top of the page, stopped.

Rule 2 at work: rows 5 and 6. The user program is refused on the page of the operating system. The operating system itself may read it.

Rule 3 at work: row 7. The page at 00407000 has no entry.

Rules 4 and 6 at work: the last four rows. The first load of a fresh page faults on A. The handler sets A, and the same load is allowed. The first store faults on D. The handler sets D, and the store is allowed.

Look at the entry in those four rows: 1c17, then 1c57, then 1cd7. The page did not move. Only its notes changed.

This lab leaves out the handler itself: one line of test code stands for it. It also leaves out the time that each check takes.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Permission bits in the entryA separate table of rightsThe check costs no extra read: the entry is already there. Rights exist only for whole pages.
Separate R, W and XOne bit for "allowed"Code cannot be overwritten, and data cannot be run. Three bits in every entry.
A fault, with software decidingHardware that decidesThe operating system can use any policy. A fault costs far more time than a check.
A and D as notesNo record of useClean pages need not be saved, and idle pages can be found. Someone must set the bits.

The Trap: A Page That Is Writable and Executable

A team gives the data page all three bits, R, W and X, "to be safe". A user program has a bug that lets an attacker choose the bytes it stores. Here is what the entry allows:

The same lab, with X added to the data page.
user store 00404010 | entry 000018df DA-UXWRV | allowed, physical 06010
user fetch 00404010 | entry 000018df DA-UXWRV | allowed, physical 06010

Neither row is a fault, and that is the failure. The first row stores bytes that the attacker chose. The second row runs them as instructions. Every check passed.

With W and no X on data, the second row would have been a page fault. The checks can refuse only what the entry forbids. Rule 1 is the test: for every page, ask which of the three bits it truly needs.

At Scale

This page4 pages, 11 accesses
A real systemmillions of pages, thousands of faults a second

This section looks ahead. A real operating system builds most of its memory tricks on these bits and on the fault.

  1. Pages on demand

    A system can leave V at 0 until a page is first used. The fault then supplies a frame. That is common practice.

  2. Copy when written

    Two programs can share a frame with W at 0. The first store faults, and the handler gives the writer a copy. Common practice.

  3. The stack guard

    A page with V at 0 below the stack turns a stack that grows too far into a clean fault.

  4. The cost of a walk

    Every access needs its entry. Reading two tables each time is too slow. The next lesson removes most of those reads.

Try It

Run the lab. For each fault, find the bit in the letters that caused it.

Make the code page readable only by removing X in its put. Predict the first row, and run it.

Add access(0, "load", 32'h0040_4010): the operating system reads a user page. Predict the verdict.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: user fetch 00403000 | entry 0000245b -A-UX-RV | allowed, physical 09000 user store 00403abc | entry 0000245b -A-UX-RV | page fault: W is 0, and this is a store user load 00404010 | entry 000018d7 DA-U-WRV | allowed, physical 06010 user store 00404010 | entry 000018d7 DA-U-WRV | allowed, physical 06010 user load 00406000 | entry 000020c7 DA---WRV | page fault: U is 0, and a user program asks os load 00406000 | entry 000020c7 DA---WRV | allowed, physical 08000 user load 00407000 | no valid entry | page fault: V is 0 user load 00405000 | entry 00001c17 ---U-WRV | page fault: A is 0, the first use of this page user load 00405000 | entry 00001c57 -A-U-WRV | allowed, physical 07000 user store 00405000 | entry 00001c57 -A-U-WRV | page fault: D is 0, the first store to this page user store 00405000 | entry 00001cd7 DA-U-WRV | allowed, physical 07000

Check Yourself

Predict the outputA user program loads from its code page. Then the operating system stores to its own page, and then to the data page of the user program. What does the last row say?
// The bits of a page-table entry, checked on every access. The tables are those of a two-level walk.
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

  function automatic string letters(input logic [7:0] b);       // the bits of an entry as letters, or a dash for a 0
    letters = {b[7] ? "D" : "-", b[6] ? "A" : "-", "-", b[4] ? "U" : "-", b[3] ? "X" : "-", b[2] ? "W" : "-", b[1] ? "R" : "-", b[0] ? "V" : "-"};
  endfunction

  // One access. kind: "fetch", "load" or "store". user: 1 for a user program, 0 for the operating system.
  task automatic access(input bit user, input string kind, input logic [31:0] virt);
    int why;
    logic [31:0] e, phys;
    string who, verdict;
    who = user ? "user" : "os  ";
    walk(virt, why, e, phys);
    if (why != 0)                              verdict = "page fault: V is 0";
    else if (kind == "fetch" && !(e & X))      verdict = "page fault: X is 0, and this is a fetch";
    else if (kind == "load"  && !(e & R))      verdict = "page fault: R is 0, and this is a load";
    else if (kind == "store" && !(e & W))      verdict = "page fault: W is 0, and this is a store";
    else if (user && !(e & U))                 verdict = "page fault: U is 0, and a user program asks";
    else if (!user && (e & U))                 verdict = "page fault: U is 1, and the operating system asks";
    else if (!(e & A))                         verdict = "page fault: A is 0, the first use of this page";
    else if (kind == "store" && !(e & D))      verdict = "page fault: D is 0, the first store to this page";
    else                                       verdict = $sformatf("allowed, physical %05h", phys[19:0]);
    if (why != 0) $display("%s %-5s %08h | no valid entry | %s", who, kind, virt, verdict);
    else          $display("%s %-5s %08h | entry %08h %s | %s", who, kind, virt, e, letters(e[7:0]), verdict);
  endtask

  task automatic set_bits(input logic [31:0] virt, input logic [7:0] more);   // the operating system sets bits in the entry of a page
    logic [31:0] first;
    first = mem[root * 1024 + virt[31:22]];
    mem[first[31:10] * 1024 + virt[21:12]] |= more;
  endtask

  initial begin
    for (int i = 0; i < 32768; i++) mem[i] = 32'h0;
    root = 22'h10;
    put(22'h10, 1, 22'h11, V);
    put(22'h11, 3, 22'h09, V | R | X | U | A);           // 00403000: code of the user program. Read and execute
    put(22'h11, 4, 22'h06, V | R | W | U | A | D);       // 00404000: its data. Read and write
    put(22'h11, 5, 22'h07, V | R | W | U);               // 00405000: data that was never used. A and D are still 0
    put(22'h11, 6, 22'h08, V | R | W | A | D);           // 00406000: data of the operating system. U is 0
                                                         // 00407000: no entry
    access(1, "load",  32'h0040_3abc);
    access(0, "store", 32'h0040_6ff0);
    access(0, "store", 32'h0040_4010);
  end
endmodule
ConceptWhich bit must an entry have for the fetch of an instruction?
ConceptA code page has R and X, and no W. A user program stores a word into it. What happens?
ConceptWhat does the D bit record?
ConceptWhat does the processor hand to the fault handler?
ConceptA page fault on the first store to a page has just been handled. What does a typical handler do?

Common Mistakes

Wrong Give every page R, W and X.

Then no access is ever refused, and bytes that were stored can be run.

Fix Give each page only the bits it needs: code R and X, data R and W.

Wrong Read the frame number without checking V.

With V at 0 the other bits are junk.

Fix Check V first. An entry that is not valid gives a fault and nothing else.

Wrong Treat every page fault as an error.

Many faults are planned: a first use, a first store, a page that is supplied late.

Fix Let the handler decide from the address, the access and the entry.

Remember

  • R, W and X allow a load, a store and a fetch. U marks a page of a user program.
  • A and D record that a page was used, and that it was written.
  • A refused access does not happen. The processor raises a page fault.
  • The handler fixes the entry and repeats the instruction, or ends the program.

Practice

Report a problem with this page