3.3 MPU control, overlap faults and the banked Secure/Non-secure MPUs
MPU_CTRL has three bits:
- ENABLE[0] turns the MPU on.
- HFNMIENA[1] controls whether the MPU stays enabled when execution priority is negative (HardFault, NMI, or FAULTMASK set). If it is 0, the MPU is ignored at those priorities and the default map is used. HFNMIENA=1 with ENABLE=0 is UNPREDICTABLE.
- PRIVDEFENA[2] makes the default map act as a background region for privileged accesses that match no enabled region. When the MPU is applied, an unprivileged access that matches no region faults.
Overlaps: In PMSAv7, overlapping regions were allowed and the higher-numbered region won. In PMSAv8, an access whose address matches more than one enabled region generates a MemManage fault. Baseline has no MemManage exception, so there it is a HardFault. The RTOS trick of putting a small, more permissive region on top of a larger one no longer works. You have to divide the address space into regions that do not overlap.
Security Extension: When it is implemented, the MPU is banked. There are separate Secure and Non-secure MPUs, each with its own MPU_TYPE, MPU_CTRL, MPU_RNR, MPU_RBAR, MPU_RLAR and MPU_MAIRn. They can have different region counts, read from each bank’s MPU_TYPE.DREGION. Secure software reaches the Non-secure copy through the SCS Non-secure alias (MPU_TYPE_NS at 0xE002ED90, MPU_CTRL_NS at 0xE002ED94, and so on). The SAU/IDAU decides the security attribute of an address. The MPU for the security state decides access permissions and memory attributes. These are two separate checks.
A common reconfiguration sequence, used by CMSIS, is: DMB so that earlier explicit accesses are ordered before the MPU register writes, an optional disable, program the regions, set ENABLE, then DSB followed by ISB. The next section explains the barrier rules behind this sequence.
Coming from A-profile: On TrustZone-A, the output address space comes from the translation regime and the NS bit in Secure-state descriptors, and EL3 firmware context-switches the unbanked AArch64 EL1 registers. v8-M instead banks the whole MPU in hardware and takes address security from the SAU/IDAU, not from MPU attributes.
Checkpoint
Q3.3.1 An RTOS ported from v7-M configures region 0 as all of SRAM (privileged RW) and region 5 as a 1 KB task stack inside it (unprivileged RW), both enabled. On Armv8-M Mainline, what happens when the unprivileged task writes to its stack?
- A. The write succeeds, because the higher-numbered region 5 takes priority
- B. The write faults with MemManage, because the address matches two enabled regions
- C. The write succeeds, because PMSAv8 combines overlapping regions by taking the most permissive
- D. The write faults only if PRIVDEFENA=0
Answer
B. In PMSAv8, an access that matches more than one enabled region is a MemManage fault, whatever the region numbers. The rule that the higher region number wins is PMSAv7 behaviour, which is exactly what a v7-M port would wrongly expect.
Read more: Armv8-M ARM (DDI0553), PMSAv8 region matching / MPU fault conditions
Q3.3.2 MPU_CTRL = ENABLE=1, PRIVDEFENA=1, HFNMIENA=0. Which access is guaranteed to fault?
- A. A privileged load from an address no region covers
- B. Any access by the HardFault handler to an address no region covers
- C. An unprivileged Thread-mode load (FAULTMASK clear) from an address no region covers
- D. A privileged load inside an enabled region with AP=0b00
Answer
C. PRIVDEFENA gives only privileged accesses a background region, so an unprivileged access that matches no region faults while the MPU is applied. The HardFault handler runs at negative priority with HFNMIENA=0, so the MPU is ignored and its access uses the default map instead of faulting.
Read more: Armv8-M ARM (DDI0553), MPU_CTRL register description
Q3.3.3 With the Security Extension, Secure firmware needs to set up the Non-secure world’s MPU. How does it do that?
- A. It writes the MPU_xxx_NS registers in the Non-secure alias of the SCS (0xE002ED90 onwards)
- B. It writes the normal MPU addresses after setting CONTROL.NS
- C. It cannot: only Non-secure code can program the Non-secure MPU
- D. It sets the NS bit in each MPU_RBAR entry
Answer
A. The MPU is banked by security state, and Secure code reaches the Non-secure bank through the SCS Non-secure alias at 0xE002xxxx. CONTROL has no NS bit, and MPU_RBAR has none either, because security comes from the SAU/IDAU, not from MPU regions.
Read more: Armv8-M ARM (DDI0553), banked MPU registers / SCS Non-secure alias