Definition
A memory protection unit (MPU) is a hardware block in a processor that checks each memory access against a small table of address ranges, called regions, and blocks any access the matching region forbids. Each region has a start address, a size, and rights to read, to write and to execute instructions, set separately for code at the higher privilege level §6.10, which writes the table, and for code at the lower level. Unlike a memory management unit §6.9 (MMU), an MPU never maps one address to another; it only checks.
The idea
Most microcontrollers §6.1, the small single-chip computers inside devices, run all their software in one address space: every piece of code can name every byte of memory. Their software is usually split into threads, independent sequences of instructions that take turns on the processor, each with its own stack, the memory for its local variables. A library with a buffer overflow §4.4, or a thread whose stack grows too deep, silently overwrites data of the kernel, the core of the operating system, or of another thread. A memory management unit, which gives each program its own translated view of memory, would prevent this, but it costs chip area, energy and time on every access, so most microcontrollers do not have one.
An MPU is the cheap answer: it leaves addresses unchanged and only checks them. Picture a building in which the rooms keep their numbers and some doors have card readers. The building manager, standing for privileged code (code at the higher privilege level), decides which card opens which door. A visitor, standing for unprivileged code, is stopped at any door that the visitor’s card does not open, and the attempt is reported. The picture fails in two ways. A typical MPU has only 8 or 16 card readers for the whole building, and one card reader can enclose only a block of rooms whose size and position follow fixed rules. Those two limits decide how finely an MPU can separate one object from its neighbors.
How it works
This section follows the MPU of the Armv7-M architecture, used in the Arm Cortex-M3, Cortex-M4 §6.3 and Cortex-M7 processors. The register names are Arm’s.
Discover. The read-only register
MPU_TYPEreports how many regions the MPU has. Zero means the chip has no MPU.Describe each region. Privileged code writes the region number to
MPU_RNR, the region’s start address toMPU_RBAR, and its size, rights and enable bit toMPU_RASR. The rights fieldAP(access permissions) selects one combination of rights for privileged and unprivileged code, such as “privileged code may read and write, unprivileged code may do nothing” or “both may only read”. The bitXN(execute never) forbids running instructions fetched from the region. Further bits describe the memory type, for example whether a cache, a small fast copy of recently used memory, may hold the region’s data.Enable. Privileged code sets the enable bit in
MPU_CTRL. The bitPRIVDEFENAin the same register decides what happens to a privileged access that falls in no region: if the bit is 1, the access follows the default memory map, the fixed rules that apply with the MPU off; if it is 0, the access faults. An unprivileged access that falls in no region always faults.Drop privilege. The confined code is started at the lower privilege level. Unprivileged code cannot write the MPU registers, so it cannot undo the configuration.
Check every access. For each instruction fetch, read and write, the MPU finds the enabled regions that contain the address. If several regions overlap, the one with the highest region number decides. The MPU compares the kind of access and the current privilege level with that region’s rights.
Fault. A forbidden access does not take place. The processor raises a MemManage fault, an exception (a forced jump to a handler routine) reserved for memory protection errors, records the cause in a status register and, for a data access, records the address in the register
MMFAR. The fault handler §6.15 then decides what to do, for example stop the offending thread or reset the chip. If the MemManage fault is disabled, or its priority does not let it run at that moment, the processor raises HardFault, the catch-all fault, instead.Switch. A real-time operating system (RTOS) that gives each thread its own regions rewrites those regions every time it switches from one thread to another.
Four rules of the Armv7-M MPU limit which sets of bytes one region can describe.
Region count. If present, the MPU of a Cortex-M3 or Cortex-M4 has 8 regions. A Cortex-M7 has 8 or 16, as the chip designer chooses. The system itself uses some of them, for example one for the program code in flash memory §6.6 and one small no-access region at the end of a stack, which turns a stack overflow §4.10 into a fault.
Size. A region’s size is a power of two from 32 bytes to 4 GiB. The
SIZEfield holds a number , and the region is bytes long.Alignment §4.22. A region’s start address is a multiple of its size.
Subregions. A region of 256 bytes or more is split into 8 equal subregions. Each bit of the 8-bit field
SRD(subregion disable) switches off one subregion: bit controls subregion , counting from the lowest address. An address in a switched-off subregion is treated as outside that region. Regions of 32, 64 and 128 bytes have no usable subregions.
Overlap is a tool as well as a rule: a higher-numbered no-access region placed inside a larger region cuts a hole in it. Other designs differ. The Armv8-M architecture (Cortex-M23, Cortex-M33 and later) describes a region by a start address and an end address, both on 32-byte boundaries, so the power-of-two rules are gone; overlapping regions are not allowed there, and an access that matches two regions faults. RISC-V, an openly licensed processor architecture (RISC stands for reduced instruction set computer), has the same kind of unit and calls it physical memory protection (PMP).
Worked example
Take an Armv7-M MPU. An untrusted thread must be allowed to write one 600-byte buffer and nothing else in RAM. RAM starts at address 0x20000000 (the prefix 0x marks hexadecimal), and the example gives every position as an offset in bytes from that start. The linker §12.4, the tool that places code and data in memory, has put the buffer at offset 2560 (0xA00). The buffer therefore covers offsets 2560 to 3159, because is the first offset after it.
Step 1: one region. The smallest power of two that holds 600 bytes is 1024, because . A 1024-byte region must start at a multiple of 1024. The largest multiple of 1024 at or below 2560 is 2048, and that region ends before offset , so it misses the last bytes of the buffer. The next size, 2048 bytes, must start at a multiple of 2048. The region from offset 2048 to 4095 contains the whole buffer; its SIZE value is 10, because . The thread can now write 2048 bytes, of which belong to other objects.
Step 2: subregions. Each subregion of the 2048-byte region holds bytes, so subregion starts at offset . The buffer touches subregion 2 (offsets 2560 to 2815), subregion 3 (2816 to 3071) and subregion 4 (3072 to 3327). Switching off subregions 0, 1, 5, 6 and 7 sets SRD to , which is 0xE3. The writable area shrinks to bytes. That is still bytes more than the buffer, at offsets 3160 to 3327. A kernel variable placed at offset 3200 is writable by the untrusted thread, although it lies outside the buffer.
Step 3: change the layout. Move the buffer to offset 3072 (0xC00), a multiple of 1024, and describe it with one 1024-byte region whose subregions hold bytes. The buffer needs subregions, rounded up to 5, which is bytes. Switch off subregions 5, 6 and 7, which sets SRD to , or 0xE0. The remaining bytes, offsets 3672 to 3711, are reserved as padding that no other object uses. The thread can now write its buffer and nothing that anyone else relies on, at the cost of one region and 40 bytes.
Stated precisely
Write an access as an address , a kind (instruction fetch, read or write) and a level (privileged or unprivileged). Let be the set of enabled regions that contain in a subregion that is switched on. With the Armv7-M MPU enabled:
If is empty, the access is allowed only when is privileged and
PRIVDEFENAis 1; the default memory map then applies. Otherwise the access faults.If is not empty, let be the region in with the highest number. The access is allowed exactly when the
APfield of grants read access (for a fetch or a read) or write access (for a write) and, for a fetch, theXNbit of is 0. Otherwise the access faults.
The processor’s system address range, from 0xE0000000 upward, which holds the system control registers, has further fixed rules that this statement leaves out.
For isolation, compare two sets of bytes for a confined component . Let be the set of bytes that is meant to write, and let be the set of bytes that the MPU lets write while runs unprivileged. Call the bytes of that are not in , written , the excess. The MPU lets do its work when is contained in , and it confines the writes of exactly when no byte of the excess belongs to any other object. The same comparison with read rights decides whether can read data that should stay secret from it. In the worked example, is 600 bytes and is 2048, then 768, then 640 bytes; only the last layout makes the excess pure padding.
This guarantee rests on five assumptions.
runs at the unprivileged level for every instruction it executes.
cannot write the MPU registers, the vector table §6.14 (the table of exception handler addresses) or any kernel data from which the region settings are loaded.
Privileged code programs the regions correctly, including at every thread switch.
No privileged service writes memory on behalf of without checking the address that supplied.
No other device on the chip that reads and writes memory by itself, such as a direct memory access §4.36 (DMA) controller, does so on behalf of : the MPU checks only the processor’s own accesses.
Limits and common mistakes
Having an MPU is not the same as using it. The MPU is off after reset. Many small systems run all their code privileged, and privileged code can rewrite the MPU at will, so the MPU then catches accidents such as a stack overflow but cannot confine code that sets out to switch it off.
The MPU sees only the processor’s accesses. A DMA controller, a radio or a cryptographic engine that reads and writes memory by itself is not checked. Protecting memory from those devices needs a separate mechanism, if the chip has one.
Region granularity is not object granularity. An MPU cannot protect one field of a structure, a 10-byte array §14.5 between two others, or thousands of small objects. Finer protection needs software checks, such as a runtime bounds check §2.27 before each access.
The region count runs out quickly. A thread whose code, stack, data, a shared buffer and two devices’ registers each need separate rights already needs 6 of 8 regions, and the RTOS must reprogram them at every thread switch, which costs time.
Overlap can cancel protection by mistake. On Armv7-M the highest-numbered region wins, so a large read-write region with a higher number than a small no-access region silently removes the no-access hole.
A fault stops one access; it does not decide what happens next. The fault handler must stop or restart the offending part. A handler that resets the whole chip on every fault turns each fault into a denial of service §1.28.
An MPU limits memory, not time. An unprivileged thread in an endless loop still holds the processor until the scheduler takes it away.
Questions answered
What granularity and region-count limits constrain isolation?
Three properties of the hardware constrain isolation with a memory protection unit (MPU): how many regions exist, which address ranges one region can describe, and which region applies where regions overlap. On the Armv7-M architecture (Cortex-M3, Cortex-M4, Cortex-M7) there are 8 or 16 regions for the whole system, and the system itself uses some of them. Every region is a power of two between 32 bytes and 4 GiB in size and starts at a multiple of its size. A region of 256 bytes or more can switch off any of its eight equal subregions. Where regions overlap, the highest-numbered region decides.
These limits decide which separations can be expressed. An object whose size or position does not fit the rules can only be covered by a larger region, and the extra bytes become writable too: a 600-byte buffer at an unaligned address may need a 2048-byte region, and still 768 bytes with subregions. Every group of objects that needs its own rights needs its own region, so more isolated components than free regions forces merging components or reprogramming regions at every thread switch. The repairs are layout choices: align and pad isolated objects to sizes the MPU can describe, place objects with the same rights next to each other, and use software checks for anything finer. Armv8-M MPUs accept any start and end address on 32-byte boundaries, which removes most of the power-of-two waste but not the region-count limit. In every case the isolation exists only if the confined code runs unprivileged and the regions are actually configured.
Related entries: Privilege level (§6.10), Spatial memory safety (§2.28), Direct memory access (DMA) (§4.36), Memory management unit (MMU) (§6.9), Exception and fault handler (§6.15), Stack overflow (§4.10). Further reading: [81, 174].