Which PMP Entry Wins: Order, Machine Mode and the Lock Bit

Regions may overlap, the firmware is checked differently from user code, and one bit can bind even the firmware until the next reset. Three short rules settle every case.

You Will Be Able To

  • Find the entry that decides an access when several regions cover the address.
  • Tell what machine mode may do with an entry that is not locked, with a locked entry, and with no entry.
  • Use overlap and the lock bit to build a guard and to protect the firmware.
Lesson
S3.25
Time
25 min
Runs on
Your browser
Check
6 questions

A stack grows downward. A deep chain of calls takes it below its own memory, into the variables of the motor control. Nothing notices. The motor notices later.

Before You Start

You need S3.10: a PMP entry, the NAPOT address word, user mode and machine mode.

Five questions this page answers
Question
1Two entries cover one address. Which one decides?
2What may machine mode do where no entry is locked?
3What does the lock bit of an entry change?
4How is a guard built below a stack?
5What does a microcontroller protect with its few entries?

The Idea: A List, Two Modes and a Lock

The entries of the last page never overlapped. Real ones do, and on purpose. Then one rule is needed: the entries are a list, and the first one that covers the address decides. That is the entry with the lowest number.

This order is a tool. A small region with fewer rights is put in front of a large one. It cuts a hole into the large region. A hole of no rights at the end of a stack is called a guard. A stack that grows too far runs into the guard and traps.

Machine mode is treated differently. The firmware must be able to set everything up. So an entry does not bind machine mode, and an address that no entry covers is open to it.

That leaves the firmware with no protection from its own bugs. The lock bit is the answer. It is bit 7 of the configuration byte. An entry with this bit at 1 is a locked entry. It binds machine mode too. And it cannot be changed again: writes to it are ignored until the next reset.

So early firmware can make a promise that later code cannot break. It locks its own code as read and execute only. After that, nothing on the chip can write there.

New Words

New words on this page
WordMeaning
Overlap of regionsTwo entries overlap when some address lies in the regions of both.
Order of entriesEntry 0 is asked first. The first entry that covers the address decides the access.
GuardA small region with no rights, placed where a stack or a buffer would overflow.
Lock bitBit 7 of the configuration byte. It makes the entry bind machine mode, and freezes the entry until reset.
Locked entryAn entry whose lock bit is 1.

One Picture: Down the List

One Address, Asked Down the List of EntriesStep through the picture. An address goes down the list. The first entry that covers it gives the answer, and the mode of the program decides whether that answer binds.
  • Processor
  • Control / FSM
  • Bus
  • Memory
One Address, Asked Down the List of EntriesStep through the picture. An address goes down the list. The first entry that covers it gives the answer, and the mode of the program decides whether that answer binds. component One access address, kind, mode component Entry 0 guard: 64 bytes, norights component Entry 1 data: 4 KiB, R and W component Entry 2 firmware: R and X,locked component No entry the end of the list component Allowed the access goesahead component Trap an access fault firstnot coverednot coverednot covereduser: no Wuser: has Wmachine: not lockedmachine: locked, no Wusermachine
  1. component One accessaddress, kind, mode
    • first → Entry 0
  2. component Entry 0guard: 64 bytes, no rights
    • not covered → Entry 1
    • user: no W → Trap
    • machine: not locked → Allowed
  3. component Entry 1data: 4 KiB, R and W
    • not covered → Entry 2
    • user: has W → Allowed
  4. component Entry 2firmware: R and X, locked
    • not covered → No entry
    • machine: locked, no W → Trap
  5. component No entrythe end of the list
    • user → Trap
    • machine → Allowed
  6. component Allowedthe access goes ahead
  7. component Trapan access fault
  1. The entries are a list

    Entry 0 is asked first, then entry 1, and so on. The first entry that covers the address decides. No later entry is asked.

  2. A store into the data

    The address is above the guard. Entry 0 does not cover it. Entry 1 does, and it has W.

  3. A store into the guard

    Entries 0 and 1 both cover the address. Entry 0 comes first, and it has no rights. The data entry is never asked.

  4. Machine mode and an entry that is not locked

    The same store from the firmware. Entry 0 covers it, and it is not locked. An entry that is not locked does not bind machine mode.

  5. Machine mode and a locked entry

    Entry 2 is locked. It binds machine mode as well. The firmware cannot write into its own code.

  6. The end of the list

    No entry covers the address. User code is refused. Machine mode is allowed.

Read the diagram as text
  • One access: address, kind, mode
  • Entry 0: guard: 64 bytes, no rights
  • Entry 1: data: 4 KiB, R and W
  • Entry 2: firmware: R and X, locked
  • No entry: the end of the list
  • Allowed: the access goes ahead
  • Trap: an access fault
  • One access → Entry 0: first
  • Entry 0 → Entry 1: not covered
  • Entry 1 → Entry 2: not covered
  • Entry 2 → No entry: not covered
  • Entry 0 → Trap: user: no W
  • Entry 1 → Allowed: user: has W
  • Entry 0 → Allowed: machine: not locked
  • Entry 2 → Trap: machine: locked, no W
  • No entry → Trap: user
  • No entry → Allowed: machine

Three questions settle every access: which entry comes first, which mode asks, and is the entry locked.

The Smallest Example, Worked by Hand

A user program has 4 KiB of data at 8000_8000. Its stack starts at the top and grows down. The firmware puts a guard of 64 bytes at the bottom: 8000_8000 to 8000_803f. The guard word is 2000_2000 with 64 / 8 - 1 = 7 in the low bits: 2000_2007.

The firmware lies in the 16 KiB from 8000_0000. Its entry has R and X, and it is locked.

Six accesses, settled by hand
ModeAccessEntries that cover itDecidesWhyOutcome
userstore 8000_880011it has Wallowed
userstore 8000_80200 and 10the guard has no rightstrap
userload 1000_0000nonenothinguser code needs an entrytrap
machineload 1000_0000nonenothingmachine mode needs noneallowed
machinestore 8000_80200 and 10entry 0 is not lockedallowed
machinestore 8000_010022locked, and it has no Wtrap

Row 2 is the stack overflow. It is caught because the guard has the lower number.

The Rules

  1. Rule 1: The entry with the lowest number that covers the address decides.

    Its rights are used alone. Rights of other entries are not added.

  2. Rule 2: User code is bound by every entry.

    With no entry, user code is refused.

  3. Rule 3: Machine mode is bound by locked entries only.

    An entry that is not locked lets machine mode pass. So does no entry at all.

  4. Rule 4: A locked entry cannot be written.

    Only a reset clears the lock bit.

  5. Rule 5: Put the small region in front of the large one.

    A guard or a read-only word needs the lower number.

Sources for These Rules

Every PMP fact on this page is stated as commonly described. That covers the order of entries, the treatment of machine mode, and the lock bit with its reset. The RISC-V privileged specification is not in the reference library. So none of these facts is checked against it.

Two finer points are left out, and are also not checked. An access that lies only partly in a region fails. Locking a TOR entry also freezes the address word below it.

The guard of 64 bytes and the split of the SRAM are choices of this page. ap_soc has no PMP.

What a Microcontroller Protects

A small chip has few entries, so each one must earn its place. These are the usual jobs. They are common practice, and each uses a rule of this page.

What is protectedFrom whatHowRule
The code of the firmwareAny stray write, and an attackerR and X, locked early3 and 4
The data of the firmwareUser tasksNo entry covers it for user code2
A stackIts own overflowA guard below it1 and 5
The device registersUser tasksEntries only for the registers a task may use2
The data of a taskBeing run as codeR and W without X1

Now Run It

The lab at the end of this page is test code. Its first lines write the guard, the data and the firmware entry, and lock the last one. Then it tries seven accesses, and one write to an entry.

Printed by the lab on Icarus. A row names the mode, the access, the entry that decides and the outcome.
user store 80008800 | entry 1 NAPOT 80008000 to 80008fff RW- | allowed
user store 80008020 | entry 0 NAPOT 80008000 to 8000803f --- | trap: store access fault
user load  10000000 | no entry covers it | trap: load access fault
machine load  10000000 | no entry covers it | allowed: machine mode needs no entry
machine store 80008020 | entry 0 NAPOT 80008000 to 8000803f --- | allowed: machine mode, and the entry is not locked
machine store 80000100 | entry 2 NAPOT 80000000 to 80003fff R-X locked | trap: store access fault
write to entry 2 | the entry is locked: nothing changes
machine store 80000100 | entry 2 NAPOT 80000000 to 80003fff R-X locked | trap: store access fault
accesses=7 allowed=3 traps=4

Rule 1 at work: rows 1 and 2. Both addresses lie in the data. The second also lies in the guard, and the guard has the lower number.

Rules 2 and 3 at work: rows 3 and 4. The same load of UART0, from two modes. Only user code needs an entry.

Rule 3 again: rows 5 and 6. Machine mode passes the guard, which is not locked. It does not pass the firmware entry, which is.

Rule 4 at work: row 7. The firmware tries to switch entry 2 off. Nothing changes, and row 8 traps as row 6 did.

The lab leaves out the entry values of the code and of the device registers. They are on the last page.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
The first entry decidesAdd up the rights of all entriesA hole can be cut into a region. The order of the entries now matters.
Machine mode passes by defaultMachine mode bound like user codeFirmware can set up anything. A bug in the firmware can write anywhere.
A lock that only a reset clearsA lock that software can clearA promise that later code cannot break. A wrong locked value costs a reset.
The lock in the entryOne lock for all entriesSome entries stay free for tasks. Each locked entry is one entry fewer.

The Trap: The Guard That Never Fired

A team adds the guard late. The data entry is already in entry 0, so the guard goes into entry 1. The values are right. A test overflows the stack on purpose:

The same lab, with the data in entry 0 and the guard in entry 1.
user store 80008800 | entry 0 NAPOT 80008000 to 80008fff RW- | allowed
user store 80008020 | entry 0 NAPOT 80008000 to 80008fff RW- | allowed

Read the entry in row 2. The store into the guard was decided by entry 0, the data. It has W, so the store went ahead.

The guard is in the list, and it is never asked. Rule 5 is the check: for every address in a small region, the small region must come first.

At Scale

This page3 entries, 2 of them overlapping
A real product16 entries: some locked at boot, some rewritten per task

This section looks ahead. The same three questions return wherever regions protect memory.

  1. The MPU of Arm microcontrollers

    It also has regions, rights and a privileged mode. Its rules for overlap differ between versions. Not checked here: read the manual of your processor.

  2. Locked at boot

    Secure boot code locks its own regions before it starts anything else: secure boot.

  3. Per-task entries

    A kernel keeps a few entries for the running task and rewrites them at each switch. Common practice.

  4. With page tables

    On a large chip the PMP check comes after translation, on the physical address. Both must allow the access. As commonly described.

Try It

Run the lab. Check each row against the hand table.

Lock entry 0 as well. Which row changes, and why?

Move the guard to the top of the data: 8000_8fc0 to 8000_8fff. Work out its address word first.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: user store 80008800 | entry 1 NAPOT 80008000 to 80008fff RW- | allowed user store 80008020 | entry 0 NAPOT 80008000 to 8000803f --- | trap: store access fault user load 10000000 | no entry covers it | trap: load access fault machine load 10000000 | no entry covers it | allowed: machine mode needs no entry machine store 80008020 | entry 0 NAPOT 80008000 to 8000803f --- | allowed: machine mode, and the entry is not locked machine store 80000100 | entry 2 NAPOT 80000000 to 80003fff R-X locked | trap: store access fault write to entry 2 | the entry is locked: nothing changes machine store 80000100 | entry 2 NAPOT 80000000 to 80003fff R-X locked | trap: store access fault accesses=7 allowed=3 traps=4

Check Yourself

Predict the outputEntry 0 is one locked word at 8000_8100 with R only. Entry 1 is 4 KiB at 8000_8000 with R and W. Three stores are tried. What does the last row say?
// Four PMP entries decide what a program may touch. There are no pages and no tables.
module tb;
  logic [7:0]  cfg  [0:3];            // one configuration byte for each entry
  logic [31:0] addr [0:3];            // one address word for each entry. It holds an address divided by 4
  int accesses = 0, traps = 0;
  localparam logic [1:0] OFF = 2'd0, TOR = 2'd1, NA4 = 2'd2, NAPOT = 2'd3;   // the four address modes
  localparam logic [2:0] R = 3'b001, W = 3'b010, X = 3'b100;                 // the three rights

  // Machine mode writes one entry. Bits 4 and 3 of the byte are the mode. Bits 2, 1 and 0 are X, W and R. Bit 7 is the lock.
  task automatic set_entry(input int i, input logic [1:0] mode, input logic [2:0] rights, input logic [31:0] word);
    if (cfg[i][7]) $display("write to entry %0d | the entry is locked: nothing changes", i);
    else begin cfg[i] = {3'b000, mode, rights};  addr[i] = word; end
  endtask
  task automatic lock_entry(input int i);                  // the lock bit can be set, and only a reset clears it
    cfg[i][7] = 1'b1;
  endtask

  // The first and the last byte that entry i covers. An entry that is OFF covers nothing.
  task automatic bounds(input int i, output bit on, output logic [31:0] first, output logic [31:0] last);
    logic [31:0] ones;
    on = 1;
    case (cfg[i][4:3])
      TOR:   begin                                           // from the address word below, up to this one
               first = (i == 0) ? 32'd0 : addr[i-1] << 2;
               last  = (addr[i] << 2) - 1;
               on    = (addr[i] << 2) > first;
             end
      NA4:   begin first = addr[i] << 2;  last = first + 3; end   // exactly one word of 4 bytes
      NAPOT: begin                                           // a power of two: the low ones give the size
               ones  = addr[i] ^ (addr[i] + 1);              // the low ones, and the 0 above them
               first = (addr[i] & ~ones) << 2;
               last  = first + (ones << 2) + 3;
             end
      default: on = 0;
    endcase
  endtask

  function automatic string mode_name(input logic [1:0] mode);
    return (mode == TOR) ? "TOR  " : (mode == NA4) ? "NA4  " : (mode == NAPOT) ? "NAPOT" : "OFF  ";
  endfunction

  // One access of four bytes. The entries are asked in order, and the first one that covers the address decides.
  task automatic access(input string who, input string what, input logic [31:0] a);
    bit on;
    logic [31:0] first, last;
    logic [2:0] need;
    int hit = -1;
    string fault;
    accesses++;
    need  = (what == "fetch") ? X : (what == "load ") ? R : W;
    fault = (what == "fetch") ? "instruction access fault" : (what == "load ") ? "load access fault" : "store access fault";
    for (int i = 0; i < 4 && hit < 0; i++) begin
      bounds(i, on, first, last);
      if (on && a >= first && a + 3 <= last) hit = i;
    end
    $write("%s %s %08h | ", who, what, a);
    if (hit < 0) begin
      if (who == "machine") $display("no entry covers it | allowed: machine mode needs no entry");
      else begin traps++;  $display("no entry covers it | trap: %s", fault); end
    end else begin
      $write("entry %0d %s %08h to %08h %s%s%s", hit, mode_name(cfg[hit][4:3]), first, last,
             cfg[hit][0] ? "R" : "-", cfg[hit][1] ? "W" : "-", cfg[hit][2] ? "X" : "-");
      if (cfg[hit][7]) $write(" locked");
      $write(" | ");
      if (who == "machine" && !cfg[hit][7]) $display("allowed: machine mode, and the entry is not locked");
      else if ((cfg[hit][2:0] & need) != 0) $display("allowed");
      else begin traps++;  $display("trap: %s", fault); end
    end
  endtask
  task automatic totals;
    $display("accesses=%0d allowed=%0d traps=%0d", accesses, accesses - traps, traps);
  endtask

  initial begin
    for (int i = 0; i < 4; i++) begin cfg[i] = 8'h00;  addr[i] = 32'd0; end   // after a reset every entry is OFF
    set_entry(0, NA4,   R,     32'h2000_2040);      // one word at 8000_8100: read only
    lock_entry(0);
    set_entry(1, NAPOT, R | W, 32'h2000_21ff);      // 4 KiB at 8000_8000
    access("user",    "store", 32'h8000_8104);
    access("user",    "store", 32'h8000_8100);
    access("machine", "store", 32'h8000_8100);
    totals;
  end
endmodule
ConceptTwo entries cover an address. Which one decides?
ConceptMachine mode stores to an address inside an entry with R only. The entry is not locked. What happens?
ConceptWhat ends the effect of a lock bit?
ConceptUser code loads from an address that no entry covers. Machine mode does the same. What happens?
ConceptWhere must a guard of no rights be put, relative to the region it lies in?

Common Mistakes

Wrong Add a small region after the large one that holds it.

The large one has the lower number and decides. The small one is never asked.

Fix Give the small region the lower number.

Wrong Trust an entry to stop the firmware.

An entry that is not locked does not bind machine mode.

Fix Lock the entries that must hold for the firmware too.

Wrong Lock an entry before its values are final.

A locked entry ignores every write until the next reset.

Fix Check the values first, and lock as the last step.

Remember

  • The entry with the lowest number that covers the address decides, alone.
  • User code is bound by every entry, and refused where there is none.
  • Machine mode is bound by locked entries only. A lock lasts until reset.
  • Small regions in front: a guard or a read-only word needs the lower number.

Practice

Report a problem with this page