Protection Without Paging: PMP and the MPU

A small chip has no page tables, and it still must stop one bad pointer from wrecking everything. A handful of registers draw bands on the memory map, and each access is checked against them.

You Will Be Able To

  • Explain how a few region registers protect memory on a chip that has no page tables.
  • Work out the address word of an entry in each of the modes TOR, NA4 and NAPOT.
  • Decide from the entries whether a fetch, a load or a store of user code is allowed.
Lesson
S3.10
Time
25 min
Runs on
Your browser
Check
6 questions

A thermostat runs a small control loop and, beside it, a parser for commands from a phone. The parser has a bug. One long command makes it store past the end of its buffer. On this chip, that store can land in the valve settings.

Before You Start

You need the rights R, W and X from S3.22. You need addresses and the memory map from S0.2.

You do not need page tables here. This page is about chips that have none.

Five questions this page answers
Question
1How can a chip protect memory without page tables?
2What does one PMP entry hold?
3Which address mode fits a region of 12 KiB, of 4 KiB, of 4 bytes?
4How do you write a NAPOT address word?
5What happens when user code touches an address that no entry covers?

The Idea: Draw Bands on the Memory Map

Virtual memory protects well, and it costs a lot: tables in memory, a walker, a TLB. A microcontroller with 64 KiB of SRAM cannot pay that. It also does not need translation. Its programs are linked for fixed physical addresses.

What it needs is the protection alone. So the processor gets a few registers that describe protection regions, or regions for short. A region is one stretch of physical addresses with rights: R, W and X. Every access is compared with the regions, in the same cycle.

RISC-V calls this PMP, for physical memory protection. One region register pair is a PMP entry. A small processor has perhaps 4, 8 or 16 of them.

Two kinds of software live on such a chip. The firmware is the trusted program that starts first and sets things up. It runs in machine mode, the mode with the most rights. The other programs run in user mode, and they are the ones that the entries confine.

An entry does not bind machine mode, unless the firmware has locked that entry. Once locked, the entry binds every mode until the next reset. The next lesson shows the lock.

The firmware writes the entries, and then starts the user program. From then on, user code can touch what an entry allows. Any other access makes the processor jump into the firmware. That forced jump is called a processor trap, or a trap for short.

New Words

New words on this page
WordMeaning
Protection regionOne stretch of physical addresses, with rights of its own. Region for short.
PMPPhysical memory protection. The region registers of a RISC-V processor.
Configuration byteThe 8 bits of a PMP entry that hold its rights, and say how its address is read.
Address wordThe register of a PMP entry that says where its region lies. It holds an address divided by 4.
PMP entryOne region register pair: a configuration byte and an address word.
AlignedA region is aligned when its base is a multiple of its size. PMP says naturally aligned.
OFFThe setting of an entry that is not in use. It covers nothing.
TORTop of range. The region runs from the address word of the entry below, up to this one.
NA4Naturally aligned four bytes. The region is one word of 4 bytes.
NAPOTNaturally aligned power of two. The region has a size of 8 bytes or more that is a power of two.
Address modeHow the address word is read. There are four: OFF, TOR, NA4 and NAPOT.
FirmwareThe trusted program of a chip. It starts first, and it writes the entries.
Machine modeThe mode of a RISC-V processor with the most rights. The firmware runs in it. Only an entry that the firmware has locked binds it.
User modeThe mode with the fewest rights. Programs that are not trusted run in it.
Processor trapA forced jump into the firmware, made by the processor when an instruction is not allowed.
Access fault of PMPThe processor trap for an access that the entries refuse. There is one each for a fetch, a load and a store.
MPUMemory protection unit. The general name for region registers like these, on any processor.

One Picture: Bands on the Map

Four Entries Laid as Bands on the Memory MapStep through the picture. The right column is the memory map, from low addresses at the top. Each entry marks one band of it.
  • Control / FSM
  • Bus
  • Memory
  • Processor
Four Entries Laid as Bands on the Memory MapStep through the picture. The right column is the memory map, from low addresses at the top. Each entry marks one band of it. component Entry 3: NA4 one word, R and W component Entries 0 and 1:TOR bottom and top, R andX component Entry 2: NAPOT 4 KiB, R and W component UART0 register 1000_0000, 4 bytes component User code 8000_4000 to8000_6fff component User data 8000_8000 to8000_8fff component Other SRAM 8000_9000 up: noentry component User program every fetch, load andstore coverscoverscoversfetchload, storestray store
  1. component UART0 register1000_0000, 4 bytes
  2. component User code8000_4000 to 8000_6fff
  3. component User data8000_8000 to 8000_8fff
  4. component Other SRAM8000_9000 up: no entry
  5. component Entries 0 and 1: TORbottom and top, R and X
    • covers → User code
  6. component Entry 2: NAPOT4 KiB, R and W
    • covers → User data
  7. component Entry 3: NA4one word, R and W
    • covers → UART0 register
  8. component User programevery fetch, load and store
    • fetch → User code
    • load, store → User data
    • stray store → Other SRAM
  1. The map has no pages

    This is part of the SRAM of a small chip, and one register of its UART. The addresses are physical. Nothing translates them.

  2. A band for the code

    Entries 0 and 1 together mark the user code: from 8000_4000 up to 8000_7000. The band may be read and executed.

  3. A band for the data

    Entry 2 marks 4 KiB of user data. The band may be read and written, and not executed.

  4. A band of one word

    Entry 3 marks one register of UART0. A band can be as small as four bytes.

  5. Inside the bands

    The user program fetches from its code and stores into its data. Each access is checked against the band it lands in.

  6. Outside every band

    A stray pointer reaches the SRAM above the data. No band covers that address, so the store is refused.

Read the diagram as text
  • Entry 3: NA4: one word, R and W
  • Entries 0 and 1: TOR: bottom and top, R and X
  • Entry 2: NAPOT: 4 KiB, R and W
  • UART0 register: 1000_0000, 4 bytes
  • User code: 8000_4000 to 8000_6fff
  • User data: 8000_8000 to 8000_8fff
  • Other SRAM: 8000_9000 up: no entry
  • User program: every fetch, load and store
  • Entry 3: NA4 → UART0 register: covers
  • Entries 0 and 1: TOR → User code: covers
  • Entry 2: NAPOT → User data: covers
  • User program → User code: fetch
  • User program → User data: load, store
  • User program → Other SRAM: stray store

Four entries, three bands. Everything outside the bands is closed to the user program.

The Smallest Example, Worked by Hand

The picture needs three bands. Each one has a different shape, and so a different mode. Work out the address word of each.

The data: 4 KiB at 8000_8000. The size is a power of two, and the base is a multiple of the size. That is what aligned means. So this is NAPOT, and one entry is enough.

A NAPOT address word in three steps
StepSumResult
Divide the base by 48000_8000 / 42000_2000
Divide the size by 8, and take 1 away4096 / 8 - 1 = 5111ff: nine ones
Put the ones into the low bits2000_2000 OR 1ff2000_21ff

To read it back, count the low ones. Nine ones mean 2 to the power 9 + 3, which is 4096 bytes.

The low ones are free to hold the size. A base that is a multiple of 4096 has zeros there anyway.

The code: 8000_4000 up to 8000_7000. That is 12 KiB, and 12 is not a power of two. So this is TOR. The top goes into entry 1: 8000_7000 / 4 = 2000_1c00. The bottom comes from the address word of entry 0: 8000_4000 / 4 = 2000_1000. Entry 0 itself stays OFF. Only its address word is used.

The register: 4 bytes at 1000_0000. NAPOT cannot be this small: with no ones at all it already means 8 bytes. So this is NA4, and the address word is 1000_0000 / 4 = 0400_0000.

The Rules

  1. Rule 1: An entry is a configuration byte and an address word.

    Bits 4 and 3 of the byte are the mode. Bits 2, 1 and 0 are X, W and R.

  2. Rule 2: An address word holds an address divided by 4.

    So no region can begin or end inside a word.

  3. Rule 3: NAPOT: the base divided by 4, with the size divided by 8, less 1, in the low bits.

    The base must be a multiple of the size.

  4. Rule 4: TOR: the region ends just below this address word, and starts at the one of the entry below.

    For entry 0 the bottom is address 0.

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

    The entry that covers the address is asked.

  6. Rule 6: User code that touches an address outside every entry is refused.

    A refused access is a trap: an access fault.

Sources for These Rules

Every PMP fact on this page is stated as commonly described. That covers the four modes, the bit positions, the address divided by 4 and the fault names. The RISC-V privileged specification is not in the reference library. So none of these facts is checked against it.

The map is the SRAM and UART0 of ap_soc. The split of the SRAM into firmware, user code and user data is a choice of this page. ap_soc itself has no PMP.

Now Run It

The lab at the end of this page is test code. It holds four entries and a check, and it tries eight accesses as user code. The first four lines of its body write the entries of the picture.

Printed by the lab on Icarus. Each row is one access of four bytes: the entry that covers it, the region of that entry, its rights and the outcome.
user fetch 80004000 | entry 1 TOR   80004000 to 80006fff R-X | allowed
user load  80008010 | entry 2 NAPOT 80008000 to 80008fff RW- | allowed
user store 80008ffc | entry 2 NAPOT 80008000 to 80008fff RW- | allowed
user store 80009000 | no entry covers it | trap: store access fault
user store 80004100 | entry 1 TOR   80004000 to 80006fff R-X | trap: store access fault
user fetch 80008010 | entry 2 NAPOT 80008000 to 80008fff RW- | trap: instruction access fault
user store 10000000 | entry 3 NA4   10000000 to 10000003 RW- | allowed
user load  10000004 | no entry covers it | trap: load access fault
accesses=8 allowed=4 traps=4

Rules 3 and 4 at work: read the regions in rows 1 and 2. The lab worked them out from the address words alone. They are the bands of the picture.

Rule 6 at work: row 4. This is the stray pointer, one word past the data. No entry covers 8000_9000, and the store never reaches the SRAM.

Rule 5 at work: rows 5 and 6. The code may not be written, and the data may not be executed. Both are covered, and both are refused.

Rules 2 and 6 again: rows 7 and 8. The band of entry 3 is one word wide. The register beside it is closed.

This lab leaves out machine mode, and no address lies in two regions. The next lesson adds both.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Region registersPage tables and a TLBNo tables, no walker, no miss. Only a few regions, and no translation.
The size in the low bits (NAPOT)A base register and a size registerOne register for each region, and one compare of the high bits. Only sizes that are powers of two.
A range mode as well (TOR)NAPOT onlyA region of any length. Two address words, and two compares of magnitude.
Addresses divided by 4Byte addressesTwo more address bits fit in the word. No region smaller than 4 bytes.
The check in the processorA check in each deviceOne place to program, and the access is stopped before the bus. Only that processor is checked.

The Trap: The Region That Was Twice as Large

An engineer writes the data entry in a hurry. The size is 4096 bytes, and a word has 4 bytes, so the ones are 4096 / 4 - 1. That is 3ff, and the address word becomes 2000_23ff. The program runs, and every test of the data passes.

The same lab, with 2000_23ff in entry 2.
user store 80008ffc | entry 2 NAPOT 80008000 to 80009fff RW- | allowed
user store 80009000 | entry 2 NAPOT 80008000 to 80009fff RW- | allowed
user store 80009ffc | entry 2 NAPOT 80008000 to 80009fff RW- | allowed
user store 8000a000 | no entry covers it | trap: store access fault

Read the region in the first row: it ends at 8000_9fff. Ten ones mean 8 KiB, not 4 KiB.

Rows 2 and 3 are the failure. The stray store at 8000_9000 is now allowed. Whatever lies in that SRAM is no longer protected.

No test of the data can find this, because the data works. The check is rule 3, and one more test: try the first word past the end, and expect a trap.

At Scale

This page4 entries, 3 regions
A real microcontroller8 or 16 entries, shared by every task

This section looks ahead. The same few registers appear on every small processor, and again around large ones.

  1. Other processors

    Arm microcontrollers have an MPU with a base and a limit for each region. The idea is the same. The details are not checked here.

  2. Too few entries

    Entries run out fast. A system with many tasks rewrites some entries at every switch of task. Common practice.

  3. Beside page tables

    A large chip keeps PMP as well. The firmware uses it to fence off its own memory from the operating system.

  4. Other managers

    PMP checks one processor only. A block that reads and writes memory by itself needs a guard of its own: the security architecture.

Try It

Run the lab. Check the region of each row against the hand sums.

Add a store to 8000_7000, the first word past the code. Predict the row before you run it.

Change entry 2 so that the data has 8 KiB at 8000_8000. Which stray stores are still caught?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: user fetch 80004000 | entry 1 TOR 80004000 to 80006fff R-X | allowed user load 80008010 | entry 2 NAPOT 80008000 to 80008fff RW- | allowed user store 80008ffc | entry 2 NAPOT 80008000 to 80008fff RW- | allowed user store 80009000 | no entry covers it | trap: store access fault user store 80004100 | entry 1 TOR 80004000 to 80006fff R-X | trap: store access fault user fetch 80008010 | entry 2 NAPOT 80008000 to 80008fff RW- | trap: instruction access fault user store 10000000 | entry 3 NA4 10000000 to 10000003 RW- | allowed user load 10000004 | no entry covers it | trap: load access fault accesses=8 allowed=4 traps=4

Check Yourself

Predict the outputOne entry covers 256 bytes at 8000_c000 with R and W. User code stores to 8000_c0fc and then to 8000_c100. 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

  // The firmware writes one entry. Bits 4 and 3 of the byte are the mode. Bits 2, 1 and 0 are X, W and R.
  task automatic set_entry(input int i, input logic [1:0] mode, input logic [2:0] rights, input logic [31:0] word);
    cfg[i] = {3'b000, mode, rights};  addr[i] = word;
  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
      traps++;  $display("no entry covers it | trap: %s", fault);
    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" : "-");
      $write(" | ");
      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, NAPOT, R | W, 32'h2000_301f);     // 8000_c000 / 4 = 2000_3000, and 256 / 8 - 1 = 1f
    access("user", "store", 32'h8000_c0fc);
    access("user", "store", 32'h8000_c100);
    totals;
  end
endmodule
ConceptWhat does a PMP entry hold?
ConceptA region is 12 KiB long. Which mode fits it?
ConceptAn address word in NAPOT mode ends in seven ones, with a 0 above them. How large is the region?
ConceptUser code loads from an address that no entry covers. What happens?
ConceptWhy does a small chip use region registers and not page tables?

Common Mistakes

Wrong Write a byte address into the address word.

The word holds an address divided by 4. The region lands four times too high.

Fix Divide by 4 first, and then add the ones.

Wrong Divide the size by 4 for the ones of NAPOT.

That gives one more one, and a region twice as large.

Fix Divide by 8 and take 1 away. Then test the first word past the end.

Wrong Use NAPOT for a region of 12 KiB.

NAPOT can only say 8 KiB or 16 KiB. One is too small, the other opens 4 KiB too much.

Fix Use TOR, with the bottom in the address word of the entry below.

Remember

  • PMP protects physical memory with a few region registers. Nothing is translated.
  • An entry is a configuration byte (mode, X, W, R) and an address word (an address divided by 4).
  • NAPOT: one entry for a power of two. TOR: two address words for any range. NA4: one word.
  • User code outside every entry traps. Always test the first word past a region.

Practice

Report a problem with this page