1.5 Stack limit checking: MSPLIM and PSPLIM
Armv8-M introduces hardware stack limit registers, so stack overflow detection no longer depends on an MPU guard region. Each register holds the lowest permitted address for its stack. Stacks grow down. Bits[2:0] are RES0, so a limit is 8-byte aligned.
If an instruction that updates SP, or the push during exception entry, would set the stack pointer below its limit, the core raises a stack overflow fault:
- On Mainline it is a synchronous UsageFault with
UFSR.STKOFset. It escalates to HardFault if UsageFault is not enabled. - On Baseline, which has no UsageFault, it is a HardFault.
The violating update is not allowed to move the stack pointer below the limit. This lets you detect the overflow before the stack overwrites the memory below it. The check is on SP updates, not on every access: a store through another base register, or an SP-relative access without writeback, that reaches below the limit is not checked, and an MSR write to MSP or PSP is not limit-checked either.
Which limit registers exist depends on the profile:
| Secure MSPLIM/PSPLIM | Non-secure MSPLIM/PSPLIM | |
|---|---|---|
| Mainline | Yes (with Security Ext) | Yes |
| Baseline | Yes (with Security Ext) | Not implemented (RAZ/WI) |
So a Baseline core like the Cortex-M23 checks stack limits only for Secure software, and a Baseline core without the Security Extension has no stack limit checking at all. Non-secure stacks on such cores still need MPU guard regions or software checking.
Operationally:
- An RTOS on Mainline programs
PSPLIMper task in the context switch, together withPSP. - Boot code sets
MSPLIMonce for the main stack. - Like the other special registers, they are written with
MSRfrom privileged code.
Coming from A-profile: On A-profile you detect overflow with an unmapped guard page: a translation fault on any access into it, at page granularity. Here the check compares new SP values against a register at 8-byte granularity. It also covers M-profile’s hardware exception stacking, but unlike a guard page it does not catch accesses that do not update SP.
Checkpoint
Q1.5.1 A Cortex-M23 with the Security Extension runs a Non-secure RTOS. Its authors want hardware stack overflow detection for Non-secure task stacks. What does the architecture give them?
- A. Nothing from the stack limit registers. On Baseline only the Secure MSPLIM/PSPLIM exist, and the Non-secure ones are RAZ/WI, so they need MPU guard regions or software checks.
- B. PSPLIM_NS, which raises a STKOF UsageFault on overflow
- C. PSPLIM_NS, which raises a HardFault on overflow, because Baseline has no UsageFault
- D. Stack limits apply automatically at the boundary of the SAU region that contains the stack.
Answer
A. Armv8-M Baseline implements stack limit registers only for the Secure state. The Non-secure ones are not implemented. Expecting PSPLIM_NS to produce a HardFault mixes up two true facts: Baseline does use HardFault for STKOF, but only Secure stacks are checked.
Read more: Armv8-M ARM (DDI0553), Stack limit registers (MSPLIM, PSPLIM) and Baseline differences
Q1.5.2 On a Mainline core with UsageFault enabled, a task’s SUB sp, sp, #256 would move PSP below PSPLIM. What is the architected outcome?
- A. The SUB completes, and a BusFault is raised later when the stack write reaches unmapped memory.
- B. PSP is clamped to PSPLIM silently and execution continues.
- C. A synchronous UsageFault with UFSR.STKOF is raised, and the violating stack pointer update is not allowed to move SP below the limit.
- D. An asynchronous imprecise fault is raised after the instruction retires.
Answer
C. Stack limit violations are detected synchronously on the instruction that would breach the limit, and they are reported as UsageFault STKOF on Mainline. The ‘later BusFault’ option describes what can happen without limit checking, and there is no silent clamping that lets execution continue.
Read more: Armv8-M ARM (DDI0553), Stack limit checking; UFSR.STKOF