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.
| Question | |
|---|---|
| 1 | Two entries cover one address. Which one decides? |
| 2 | What may machine mode do where no entry is locked? |
| 3 | What does the lock bit of an entry change? |
| 4 | How is a guard built below a stack? |
| 5 | What 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
| Word | Meaning |
|---|---|
| Overlap of regions | Two entries overlap when some address lies in the regions of both. |
| Order of entries | Entry 0 is asked first. The first entry that covers the address decides the access. |
| Guard | A small region with no rights, placed where a stack or a buffer would overflow. |
| Lock bit | Bit 7 of the configuration byte. It makes the entry bind machine mode, and freezes the entry until reset. |
| Locked entry | An entry whose lock bit is 1. |
One Picture: Down the List
- Processor
- Control / FSM
- Bus
- Memory
- 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.
- A store into the data
The address is above the guard. Entry 0 does not cover it. Entry 1 does, and it has W.
- 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.
- 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.
- Machine mode and a locked entry
Entry 2 is locked. It binds machine mode as well. The firmware cannot write into its own code.
- 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.
| Mode | Access | Entries that cover it | Decides | Why | Outcome |
|---|---|---|---|---|---|
| user | store 8000_8800 | 1 | 1 | it has W | allowed |
| user | store 8000_8020 | 0 and 1 | 0 | the guard has no rights | trap |
| user | load 1000_0000 | none | nothing | user code needs an entry | trap |
| machine | load 1000_0000 | none | nothing | machine mode needs none | allowed |
| machine | store 8000_8020 | 0 and 1 | 0 | entry 0 is not locked | allowed |
| machine | store 8000_0100 | 2 | 2 | locked, and it has no W | trap |
Row 2 is the stack overflow. It is caught because the guard has the lower number.
The Rules
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.
Rule 2: User code is bound by every entry.
With no entry, user code is refused.
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.
Rule 4: A locked entry cannot be written.
Only a reset clears the lock bit.
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 protected | From what | How | Rule |
|---|---|---|---|
| The code of the firmware | Any stray write, and an attacker | R and X, locked early | 3 and 4 |
| The data of the firmware | User tasks | No entry covers it for user code | 2 |
| A stack | Its own overflow | A guard below it | 1 and 5 |
| The device registers | User tasks | Entries only for the registers a task may use | 2 |
| The data of a task | Being run as code | R and W without X | 1 |
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.
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=4Rule 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 choice | The alternative | What the choice buys, and costs |
|---|---|---|
| The first entry decides | Add up the rights of all entries | A hole can be cut into a region. The order of the entries now matters. |
| Machine mode passes by default | Machine mode bound like user code | Firmware can set up anything. A bug in the firmware can write anywhere. |
| A lock that only a reset clears | A lock that software can clear | A promise that later code cannot break. A wrong locked value costs a reset. |
| The lock in the entry | One lock for all entries | Some 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:
user store 80008800 | entry 0 NAPOT 80008000 to 80008fff RW- | allowed
user store 80008020 | entry 0 NAPOT 80008000 to 80008fff RW- | allowedRead 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 section looks ahead. The same three questions return wherever regions protect memory.
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.
Locked at boot
Secure boot code locks its own regions before it starts anything else: secure boot.
Per-task entries
A kernel keeps a few entries for the running task and rewrites them at each switch. Common practice.
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.
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=4Check Yourself
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.