Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

2.3 Exception entry, the stack frame and EXC_RETURN

On exception entry the PE stacks the caller-saved state, so the handler can be a plain AAPCS function. The frame goes on the stack that was in use at the moment of the exception:

  • the Process stack (PSP) if the PE was in Thread mode with CONTROL.SPSEL = 1;
  • otherwise the Main stack (MSP).

Both are the stack pointers of the interrupted context’s Security state. The handler always runs on the MSP of the Security state that the exception is taken to.

basic frame (lowest address first)
+0x00 R0   +0x04 R1   +0x08 R2   +0x0C R3
+0x10 R12  +0x14 LR   +0x18 ReturnAddress
+0x1C RETPSR
extended frame adds S0-S15, FPSCR, then a
reserved word (VPR on v8.1-M with MVE)
  • Alignment: the frame is always 8-byte aligned (CCR.STKALIGN is RES1 in v8-M). If SP was only 4-byte aligned, the PE inserts one padding word and sets RETPSR[9] (SPREALIGN), so the return can undo it.
  • In addition to stacking, the PE writes the exception number to IPSR and switches to Handler mode. It loads LR with an EXC_RETURN value and branches to the vector.
  • If a Secure context is interrupted by a Non-secure exception, the PE also stacks the additional state context (an integrity signature, a reserved word and R4-R11), then clears the registers. EXC_RETURN.DCRS records this. With Secure FP context and FPCCR.TS = 1, S16-S31 are stacked as well.

EXC_RETURN = 0xFF in bits [31:24], bits [23:7] RES1, bit[1] RES0:

  • bit[0] ES: Security state the exception was taken to (1 = Secure)
  • bit[2] SPSEL: frame on Main (0) or Process (1) stack
  • bit[3] Mode: return to Thread (1) or Handler (0)
  • bit[4] FType: 1 = basic frame, 0 = extended frame with FP state
  • bit[5] DCRS: 1 = default rules (callee-saved registers not stacked), 0 = callee-saved registers were stacked
  • bit[6] S: frame on Secure (1) or Non-secure (0) stack

Without the Security Extension, ES and S read as 0 and DCRS as 1.

Return: there is no ERET. An exception return happens when an EXC_RETURN value is loaded into PC in Handler mode, by BX LR, POP {..., PC}, LDM or LDR PC. An inconsistent EXC_RETURN raises a fault, usually an INVPC UsageFault, or a SecureFault (for example INVER) for Security-state inconsistencies.

Coming from A-profile: In A-profile, ELR_ELx and SPSR_ELx are system registers and the handler must save its own GPRs before ERET. On M-profile, hardware puts ReturnAddress and RETPSR in memory on the stack with R0-R3/R12/LR, and LR holds only a special token that describes that frame.

Checkpoint

Q2.3.1 An exception is taken from Thread mode on MSP with SP = 0x2000_1004 and no FP context active. Where is the basic frame placed, and what does RETPSR record?

  • A. 0x2000_0FE4 to 0x2000_1003, with RETPSR[9] = 0
  • B. An UNALIGNED UsageFault is raised, because the stack must be 8-byte aligned before entry
  • C. The frame is placed only if CCR.STKALIGN was set by software, otherwise at 0x2000_0FE4
  • D. 0x2000_0FE0 to 0x2000_0FFF with one padding word above it, and RETPSR[9] (SPREALIGN) = 1
Answer

D. 0x2000_1004 - 32 = 0x2000_0FE4, which is not 8-byte aligned. The PE inserts a 4-byte pad, so the frame starts at 0x2000_0FE0, and it sets RETPSR[9]. On return the PE adds the pad back to restore the original SP. CCR.STKALIGN is RES1 in v8-M, so software cannot turn this off.

Read more: Armv8-M ARM (DDI0553), Exception entry: stack alignment, RETPSR

Q2.3.2 The FreeRTOS Armv8-M Non-secure port seeds each new task with EXC_RETURN = 0xFFFFFFBC. Decode bits [6:0].

  • A. S = 0 (Non-secure stack), DCRS = 1, FType = 1 (basic frame), Mode = 1 (Thread), SPSEL = 1 (PSP), ES = 0 (exception taken to Non-secure)
  • B. S = 1, DCRS = 0, FType = 0 (extended frame), Mode = 0 (Handler), SPSEL = 0 (MSP), ES = 1
  • C. S = 0, DCRS = 1, FType = 1, Mode = 1, SPSEL = 0 (MSP), ES = 0
  • D. It is not a valid EXC_RETURN, because bit[7] must be 0
Answer

A. 0xBC = 0b1011_1100: bit6 S = 0, bit5 DCRS = 1, bit4 FType = 1, bit3 Mode = 1, bit2 SPSEL = 1, bit1 = 0, bit0 ES = 0. That means return to Non-secure Thread mode on PSP with a basic frame. Bit[7] is RES1, so the last option is backwards. The tempting misreading swaps the polarity of FType: FType = 1 means the basic frame without FP state.

Read more: Armv8-M ARM (DDI0553), EXC_RETURN payload

Q2.3.3 Why can an exception handler written as an ordinary C function (no special attribute) return correctly on Armv8-M?

  • A. The compiler detects the vector table and emits an ERET in place of the function’s return
  • B. The C epilogue (BX LR or POP {..., PC}) loads the EXC_RETURN value from LR into PC in Handler mode, which the hardware treats as an exception return, and R0-R3/R12 were already stacked
  • C. The NVIC watches for the handler’s stack pointer to return to its entry value
  • D. Handlers must call a library routine that writes an EOI register before returning
Answer

B. Hardware stacked every caller-saved register, and LR holds EXC_RETURN. The AAPCS epilogue therefore performs the exception return without modification. M-profile has no ERET instruction, and there is no EOI register to write.

Read more: Armv8-M ARM (DDI0553), Exception return