What is TrustZone for Cortex-M?
Short answer: TrustZone for Armv8-M (Cortex-M23/M33/M35P/M55/M85) splits the system into a Secure world and a Non-secure world on the same core. Memory, peripherals and interrupts are assigned to one world; Non-secure code cannot access Secure resources, and can call Secure code only through defined entry points. It protects keys, secure boot, cryptographic services and firmware update logic from bugs or attacks in the application.
How it works
- SAU (Security Attribution Unit, programmed by secure code) and IDAU (fixed by the chip designer) mark each address as Secure, Non-secure, or Non-secure Callable (NSC).
- Entry from Non-secure to Secure only at NSC addresses that start with the
SG(secure gateway) instruction; the compiler generates these veneers for functions marked__attribute__((cmse_nonsecure_entry)). - Calls from Secure to Non-secure use
BLXNS, with registers cleared so secrets do not leak. - Banked resources: separate stack pointers (MSP_S/PSP_S, MSP_NS/PSP_NS), stack limits, MPUs, SysTick timers, VTORs, and interrupt masks per world. Each interrupt is assigned to one world (
NVIC->ITNS). - Peripheral and memory protection outside the core by the chip's security controllers (e.g. TZC, MPC/PPC), so even DMA from the Non-secure side cannot read Secure memory.
- Faults from security violations go to the SecureFault handler.
Software stack: the Secure side typically runs Trusted Firmware-M (TF-M), which provides PSA services (crypto, secure storage, attestation, firmware update), and the Non-secure side runs the RTOS and application (Zephyr, FreeRTOS).
Compared with TrustZone for Cortex-A: the same idea, but Cortex-A switches worlds through EL3 secure monitor calls (SMC) and runs a full secure OS (OP-TEE). Cortex-M switching is a fast function call, designed for microcontrollers.
Design rules: keep the Secure world small, validate every pointer and length passed from the Non-secure side (it may point into Secure memory: use cmse_check_address_range), and lock the configuration (SAU, debug access) before starting Non-secure code.