4.3 Crossing the boundary: SG, BXNS, BLXNS and FNC_RETURN
NS to S (calling a Secure entry point). Non-secure code branches, typically with BL, to an address in NSC memory. The first instruction there must be SG (encoding 0xE97FE97F). When SG runs in Non-secure state from NSC memory, the PE:
- switches to Secure state, and
- clears
LR[0], which marks the return target as Non-secure.
The veneer then branches to the real function body in Secure memory. If Non-secure code branches or falls through anywhere else in Secure or NSC memory (a Secure address that is not NSC, or an NSC address that does not hold SG), the PE raises a SecureFault with SFSR.INVEP (Mainline) or a Secure HardFault (Baseline). Executed in Secure state, SG acts as a NOP.
Returning from the entry function. The function returns with BXNS LR. BXNS moves to Non-secure state when bit 0 of the target is 0, and stays Secure when bit 0 is 1. Because SG cleared LR[0] only when the caller was Non-secure, the same entry function returns correctly whether the caller was Secure or Non-secure. Before BXNS, the code must clear every register that might hold Secure data.
S to NS (calling Non-secure code such as a callback). Secure code uses BLXNS Rm with Rm[0]=0. The PE:
- pushes the real return address and partial PSR onto the current Secure stack,
- sets
LRto the magic valueFNC_RETURN(0xFEFFFFFE/0xFEFFFFFF), and - branches into Non-secure state.
Non-secure code never sees the Secure return address. When it returns by branching to FNC_RETURN (an ordinary BX LR, or for example POP {pc}), the PE recognises the value, pops the return address from the Secure stack, checks the partial PSR, and resumes in Secure state.
An ordinary BX/BLX from Secure state to a Non-secure address is not a transition. It faults: SecureFault with SFSR.INVTRAN on Mainline, Secure HardFault on Baseline.
Coming from A-profile: There is no SMC function ID, no dispatcher and no context save in a monitor. An SG veneer is the entry point, and FNC_RETURN does the job of the ERET back into the caller, with the real return address kept where Non-secure code cannot reach it.
Checkpoint
Q4.3.1 Non-secure code reads a Secure function’s address from a symbol file and issues BL directly to that address. The address is in ordinary Secure memory, not NSC. What happens on Mainline?
- A. The function runs in Secure state, but with Non-secure privilege
- B. The function runs in Non-secure state, so its accesses to Secure data fault
- C. A MemManage fault is raised to the Non-secure handler
- D. A SecureFault is raised with SFSR.INVEP set, because Non-secure code may enter Secure state only through an SG instruction in NSC memory
Answer
D. Non-secure code fetching from a Secure address that is not a valid entry point (SG in NSC memory) raises SecureFault with INVEP, an invalid entry point. On Baseline it becomes a Secure HardFault. The MemManage distractor matches the A-profile habit of treating this as a translation or permission fault. On v8-M it is an attribution failure, and SecureFault is always handled in Secure state.
Read more: Armv8-M ARM (DDI0553), SG instruction; SFSR (INVEP)
Q4.3.2 One Secure entry function is called from both Secure and Non-secure code. How does its single BXNS LR return always go to the right state?
- A. The PE tracks the caller’s Security state in CONTROL_S and restores it on any BX
- B. BXNS always goes to Non-secure state, so Secure callers must use a separate non-veneer symbol
- C. SG clears LR[0] when it runs in Non-secure state, so BXNS sees bit 0 = 0 and goes Non-secure, while a Secure caller’s BL leaves LR[0] = 1 and BXNS stays Secure
- D. The veneer pushes an EXC_RETURN value that records the caller’s state
Answer
C. BXNS uses the target’s bit 0: 0 means switch to Non-secure, 1 means stay Secure. SG clears LR[0] only on a Non-secure to Secure entry, so LR itself carries the caller’s state. The distractor “BXNS always goes NS” is tempting because of the mnemonic, but BXNS with bit 0 = 1 is an ordinary Secure branch.
Read more: Armv8-M ARM (DDI0553), BXNS, SG
Q4.3.3 Secure code calls a Non-secure callback with BLXNS. Where is the Secure return address while the callback runs?
- A. On the current Secure stack, pushed by BLXNS together with a partial PSR, while LR holds FNC_RETURN
- B. In LR, with bit 0 cleared, so the callback can BXNS back
- C. In a banked LR_S register that Non-secure code cannot read
- D. On the Non-secure stack, protected by an integrity signature
Answer
A. BLXNS pushes the real return address (and partial PSR) onto the Secure stack and loads LR with FNC_RETURN, so the Secure return address never reaches Non-secure code. The Non-secure branch to FNC_RETURN pops that frame. A banked LR is tempting by analogy with A-profile banked ELR_ELx, but LR is not banked in v8-M. Because the hardware trusts whatever frame sits on the Secure stack, an unsealed Secure stack is a weakness (section 5).
Read more: Armv8-M ARM (DDI0553), BLXNS, FNC_RETURN