1.4 Modes, privilege and the two stack pointers
Armv8-M replaces exception levels with two orthogonal ideas:
- Mode: Thread mode runs application or RTOS task code. Handler mode is the mode while executing an exception handler. The core enters Handler mode on exception entry and returns to Thread mode when the last active exception returns. IPSR is non-zero in Handler mode.
- Privilege: Handler mode is always privileged. Thread mode is privileged or unprivileged, according to
CONTROL.nPRIV.
Unprivileged Thread code cannot clear nPRIV itself, because the write is ignored. The way up is an exception, typically SVC. The handler, which is privileged, does the work or modifies CONTROL and then returns. That is the RTOS equivalent of an EL0 to EL1 syscall.
The stack pointer is banked:
- MSP (Main stack): always used in Handler mode. Also used in Thread mode when
CONTROL.SPSEL= 0, which is the reset state. - PSP (Process stack): used in Thread mode when
CONTROL.SPSEL= 1.
With the Security Extension, each Security state has its own MSP, PSP and CONTROL.SPSEL/nPRIV, and the core uses the copies of the current state.
A typical RTOS layout:
- Each task runs in Thread mode on its own PSP.
- Every handler and the kernel run on MSP.
- On exception entry the hardware pushes the caller-saved frame (R0-R3, R12, LR, return address, xPSR, plus FP state if used) onto whichever stack was active. A Secure-to-Non-secure exception also stacks the callee-saved registers and an integrity signature.
- The EXC_RETURN value records which stack that was, so the exception return pops from the right one.
- A context switch (usually in PendSV) saves the remaining callee-saved registers to the old task’s PSP, updates PSP, and returns.
You name the banked registers explicitly with MRS/MSR as MSP and PSP, and with the Security Extension as MSP_NS/PSP_NS from Secure state. Plain SP (R13) always refers to the currently selected one.
Coming from A-profile: Think of MSP as SP_EL1 used by handlers, and PSP as SP_EL0 for tasks. The difference is that the hardware itself stacks the caller-saved context on entry and selects the stack from CONTROL and EXC_RETURN. No vector code saves registers manually or switches SPSel.
Checkpoint
Q1.4.1 A task running unprivileged in Thread mode on PSP takes an interrupt. Inside the ISR, which stack pointer does PUSH {r4, lr} use, and at what privilege?
- A. PSP, privileged, because the ISR inherits the interrupted context’s stack
- B. MSP, unprivileged, because privilege is inherited from the interrupted task
- C. PSP, unprivileged, because CONTROL is unchanged by exception entry
- D. MSP, privileged, because Handler mode always uses MSP and is always privileged
Answer
D. Handler mode always selects the main stack, and Handler mode is always privileged, whatever CONTROL.nPRIV says. The hardware-stacked frame went onto PSP, because that was the active stack at the time of the exception, but the handler’s own pushes go to MSP.
Read more: Armv8-M ARM (DDI0553), Modes, privilege and stacks; CONTROL.SPSEL
Q1.4.2 What is the architecturally intended way for unprivileged Thread-mode code to obtain a privileged service?
- A. Execute
MSR CONTROLwith nPRIV = 0 and then an ISB - B. Write to the SCS to request privilege
- C. Execute an
SVC. The privileged SVCall handler performs the operation, or changes CONTROL.nPRIV, before returning. - D. Execute an
SMC
Answer
C. Unprivileged writes to CONTROL are ignored, so the only way in is an exception, and SVC is the synchronous system-call exception. SMC does not exist on M-profile, where Security state changes use SG/BXNS/BLXNS instead. Unprivileged SCS accesses fault (apart from narrow cases such as STIR), so they cannot grant privilege.
Read more: Armv8-M ARM (DDI0553), SVCall exception; CONTROL.nPRIV