Switching between Secure and Non-secure states
The ARMv8-M Security Extensions allow direct calling between Secure and Non-secure software.
Several instructions are available for state transition handling in ARMv8-M processors:
Used for switching from Non-secure to Secure state at the first instruction of Secure entry point.
Branch with exchange to Non-secure state.
Used by Secure software to branch or return to Non-secure program.
Branch with link and exchange to Non-secure state.
Used by Secure software to call Non-secure functions.
The following figure shows the security state transitions.
A direct API function call from
Non-secure to Secure software entry points is allowed if the first instruction of the
entry point is
SG, and it is in a Non-secure callable memory
When a Non-secure program calls a Secure API, the API completes by returning to a
Non-secure state using a
BXNS instruction. If a Non-secure program attempts
to branch, or call a Secure program address without using a valid entry point, a fault
event is generated. In ARMv8-M architecture the HardFault in Secure state handles the
fault event. In ARMv8-M architecture with Main Extension, the SecureFault exception type
When a Non-secure program calls a Secure API, the API completes by
returning to a Non-secure state using a
BXNS instruction. If a Non-secure
program attempts to branch, or call a Secure program address without using a valid entry
point, a fault event is generated. In ARMv8-M Mainline, the SecureFault exception type
is used. For ARMv8-M Baseline, the HardFault in Secure state handles the fault
The ARMv8-M Security Extensions also allow a Secure program to call
Non-secure software. In such a case, the Secure program uses a
instruction to call a Non-secure program. During the state transition, the return
address and some processor state information are pushed onto the Secure stack, while the
return address on the Link Register (LR) is set to a special value called FNC_RETURN.
The Least Significant Bit (LSB) of the function address must be 0.
The following figure shows the software flow when a secure program calls a Non-secure function:
The Non-secure function completes by performing a branch to the FNC_RETURN address. This automatically triggers the unstacking of the true return address from the Secure stack and returns to the calling function. The state transition mechanism automatically hides the return address of the Secure software. Secure software can choose to transfer some of the register values to the Non-secure side as parameters, and clears other Secure data from the register banks before the function call.
State transitions can also happen due to exceptions and interrupts. Each interrupt can be configured as Secure or Non-secure, and is determined by the Interrupt Target Non-secure (NVIC_ITNS) register, which is only programmable in the Secure world. There are no restrictions regarding whether a Non-secure or Secure interrupt can take place when the processing is running Non-secure or Secure code.
If the arriving exception or interrupt has the same state as the current processor state, the exception sequence is almost identical to the current M-series processors, enabling low interrupt latency. The main difference occurs when a Non-secure interrupt takes place, and is handled by the processor during execution of Secure code.
In this case, the processor automatically pushes all Secure information onto the Secure stack and erases the contents from the register banks, therefore avoiding an information leak.
All existing interrupt handling features such as nesting of interrupts, vectored interrupt handling, and vector table relocation are supported. TrustZone technology for ARMv8-M maintains the low interrupt latency characteristics of the existing M-series processors, with Secure to Non-secure interrupts incurring a slightly longer interrupt latency due to the need to push all Secure contents to the Secure stack.
The enhancement of the exception model also works with the lazy stacking of registers in the Floating-Point Unit (FPU). Lazy stacking is used to reduce the interrupt latency in exception sequences so that stacking of floating-point registers is avoided unless the interrupt handler also uses the FPU. In the ARMv8-M architecture, the same concept is applied to avoid the stacking of the Secure floating-point context. In cases where the Secure software does use the FPU and the Non-secure interrupt handler does not use the FPU, the stacking and unstacking of FPU registers is skipped to provide faster interrupt handling sequences. The following table shows some information.
Security states of the processor
In a simplified view, the program address determines the security state of the processor, which can be either Secure or Non-secure.
- If the processor is running program code in Non-secure memory, the processor is in Non-secure state.
- If the processor is running program code in Secure memory, the processor is in Secure state.
- If the processor is in Secure state, it must fetch instructions from Secure memory.
The ARMv8-M architecture permits function calls or branching between Secure and Non-secure software. However, restrictions are imposed for Non-secure to Secure transitions to ensure that only valid Secure API entry points can be used for calling from the Normal world to Secure code, or when the transition is caused by returning from a Non-secure API back to Secure code.
There is special case when switching from Non-secure to Secure state. The first
instruction of the transition must be the
SG instruction, and the
processor must be in the Non-secure state when the SG instruction is executed
The design of TrustZone technology for ARMv8-M has several key characteristics.
These characteristics are:
- Non-secure code can call Secure functions using valid entry points only. There is no limitation on the number of entry points.
- Low switching overhead in cross security domain calls. There is only one extra
SG) when calling from the Non-secure to the Secure domain, and only a few extra clock cycles when calling from the Secure state to Non-secure functions.
- Non-secure interrupt requests can still be served during the execution of Secure code, with minimal impact on the interrupt latency, stacking of the full register banks is required instead of just the caller saving registers.
- The processor starts up in Secure state by default. This start up mode enables root-of-trust implementations such as Secure boot.
- Low power:
- There is no need for separate register banks for Secure and Non-secure states, while Non-secure interrupt handlers are still prevented from snooping into data used by Secure operations.
- Ease of use:
- Interrupt handlers remain programmable in C, and Non-secure software can access Secure APIs with standard C/C++ function calls.
- High flexibility:
- The design allows Secure software to call Non-secure functions. This function is often required when protected middleware on the Secure side must access device driver code in the Non-secure side. The Secure state can also have privileged and unprivileged execution states, so this state can support multiple Secure software components with a protection mechanism between them.