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.
| Question | |
|---|---|
| 1 | How can a chip protect memory without page tables? |
| 2 | What does one PMP entry hold? |
| 3 | Which address mode fits a region of 12 KiB, of 4 KiB, of 4 bytes? |
| 4 | How do you write a NAPOT address word? |
| 5 | What 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
| Word | Meaning |
|---|---|
| Protection region | One stretch of physical addresses, with rights of its own. Region for short. |
| PMP | Physical memory protection. The region registers of a RISC-V processor. |
| Configuration byte | The 8 bits of a PMP entry that hold its rights, and say how its address is read. |
| Address word | The register of a PMP entry that says where its region lies. It holds an address divided by 4. |
| PMP entry | One region register pair: a configuration byte and an address word. |
| Aligned | A region is aligned when its base is a multiple of its size. PMP says naturally aligned. |
| OFF | The setting of an entry that is not in use. It covers nothing. |
| TOR | Top of range. The region runs from the address word of the entry below, up to this one. |
| NA4 | Naturally aligned four bytes. The region is one word of 4 bytes. |
| NAPOT | Naturally aligned power of two. The region has a size of 8 bytes or more that is a power of two. |
| Address mode | How the address word is read. There are four: OFF, TOR, NA4 and NAPOT. |
| Firmware | The trusted program of a chip. It starts first, and it writes the entries. |
| Machine mode | The 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 mode | The mode with the fewest rights. Programs that are not trusted run in it. |
| Processor trap | A forced jump into the firmware, made by the processor when an instruction is not allowed. |
| Access fault of PMP | The processor trap for an access that the entries refuse. There is one each for a fetch, a load and a store. |
| MPU | Memory protection unit. The general name for region registers like these, on any processor. |
One Picture: Bands on the Map
- Control / FSM
- Bus
- Memory
- Processor
- 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.
- 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.
- A band for the data
Entry 2 marks 4 KiB of user data. The band may be read and written, and not executed.
- A band of one word
Entry 3 marks one register of UART0. A band can be as small as four bytes.
- 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.
- 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.
| Step | Sum | Result |
|---|---|---|
| Divide the base by 4 | 8000_8000 / 4 | 2000_2000 |
| Divide the size by 8, and take 1 away | 4096 / 8 - 1 = 511 | 1ff: nine ones |
| Put the ones into the low bits | 2000_2000 OR 1ff | 2000_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
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.
Rule 2: An address word holds an address divided by 4.
So no region can begin or end inside a word.
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.
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.
Rule 5: A fetch needs X, a load needs R and a store needs W.
The entry that covers the address is asked.
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.
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=4Rules 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 choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Region registers | Page tables and a TLB | No 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 register | One 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 only | A region of any length. Two address words, and two compares of magnitude. |
| Addresses divided by 4 | Byte addresses | Two more address bits fit in the word. No region smaller than 4 bytes. |
| The check in the processor | A check in each device | One 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.
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 faultRead 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 section looks ahead. The same few registers appear on every small processor, and again around large ones.
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.
Too few entries
Entries run out fast. A system with many tasks rewrites some entries at every switch of task. Common practice.
Beside page tables
A large chip keeps PMP as well. The firmware uses it to fence off its own memory from the operating system.
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?
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=4Check Yourself
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.