2.2 Registers and the calling convention
The general-purpose register file
AArch64 has 31 general-purpose registers, X0–X30, each 64 bits wide. W0–W30 name their low 32 bits. Two rules matter in practice:
- A write to a
Wnregister zero-extends intoXn.mov w0, #-1leavesx0 = 0x00000000_ffffffff. Firmware that builds a 64-bit address in aWregister silently loses the top half, and a 32-bitldr w1, [x0]result is never sign-extended unless you useldrsw. - Register number 31 is not a register. Depending on the instruction it means the stack pointer (
SP/WSP, for example as the base of a load or inadd sp, sp, #16) or the zero register (XZR/WZR, which reads as zero and ignores writes, for examplestr xzr, [x0]orcmp x0, #0=subs xzr, x0, #0). Each encoding fixes which one, somov x0, spandmov x0, xzrare different instructions (add x0, sp, #0andorr x0, xzr, xzr).
There is no banking of X0–X30 by EL or Security state. An exception to EL3 arrives with the lower EL’s registers live, which is why the first instructions of any EL3 vector save registers to a per-CPU context before using them. TF-A’s cpu_context_t holds exactly this state.
The program counter
The PC is not a general-purpose register. No data-processing instruction can read or write it directly, and there is no mov x0, pc. It is read only implicitly: by PC-relative instructions (ADR, ADRP, literal LDR, branches), by BL/BLR, which write the return address to X30, and by the exception-entry logic, which writes ELR_ELx. It is written only by branches, exception entry and ERET. Instructions are 4-byte aligned. A branch to a misaligned target takes a PC alignment fault (EC 0x22) when the misaligned PC is used, not at the branch.
Stack pointers and SPSel
Each implemented EL has its own stack pointer: SP_EL0, SP_EL1, SP_EL2, SP_EL3. At EL0 the PE always uses SP_EL0. At EL1 and above, PSTATE.SP (written through the SPSel special register) chooses:
PSTATE.SP | SP used at ELx (x ≥ 1) | Vector table group used for exceptions taken from the current EL |
|---|---|---|
| 0 | SP_EL0 (“ELxt”) | “Current EL with SP0” |
| 1 | SP_ELx (“ELxh”) | “Current EL with SPx” |
Exception entry always sets PSTATE.SP = 1, so a handler starts on its own EL’s dedicated stack, whatever the interrupted code was using. SP_EL0 can be accessed from EL1 and above as a system register (mrs x0, sp_el0) when not currently selected. SP_ELx for the current EL cannot be accessed that way, and SP_EL1 is accessible from EL2 and EL3.
msr spsel, #0 // switch to SP_EL0 at the current EL
mov sp, x1 // initialise it
msr spsel, #1 // back to SP_ELx
TF-A uses the split deliberately. At EL3, SP_EL3 holds a pointer to the current CPU’s cpu_context_t, and the C runtime stack is SP_EL0. On entry from a lower EL, the handler saves the lower world’s GPRs through SP_EL3, then executes msr spsel, #MODE_SP_EL0 and calls C. Linux uses SP_EL0 at EL1 differently: while running in the kernel, sp_el0 holds the current task pointer.
SP alignment checking. When SCTLR_ELx.SA (bit [3]) is set, a load or store that uses SP as its base register at ELx with SP not 16-byte aligned takes an SP alignment fault (EC 0x26). SCTLR_EL1.SA0 (bit [4]) is the same check for EL0. The check is on the base register value at the time of the access, not on every arithmetic update of SP.
Bring-up note: TF-A sets
SCTLR_EL3.SAvery early inel3_entrypoint_common. If a hand-written assembly path pushes a single 8-byte register withstr x0, [sp, #-8]!, the next SP-based access faults. In a bare vector table with no handler yet installed, this shows up as a recursive exception loop atVBAR_EL3 + 0x200. Always push and pop in 16-byte pairs (stp/ldp).
AAPCS64 register roles
The Procedure Call Standard for the Arm 64-bit Architecture (AAPCS64, part of Arm’s ABI set) defines how compiled code uses the registers. The architecture itself gives no register a special role except X30 (written by BL/BLR, used by RET by default) and register 31.
| Register | AAPCS64 role | Preserved across a call? |
|---|---|---|
X0–X7 | Arguments and results | No |
X8 | Indirect result location (address of a large returned struct) | No |
X9–X15 | Temporaries | No |
X16, X17 (IP0, IP1) | Intra-procedure-call scratch: veneers and PLT stubs may clobber them | No |
X18 | Platform register (see below) | Platform-defined |
X19–X28 | Callee-saved | Yes |
X29 (FP) | Frame pointer | Yes |
X30 (LR) | Link register | Saved by any non-leaf function |
SP | Stack pointer, 16-byte aligned at any public interface | Yes |
V8–V15 | Only the low 64 bits (D8–D15) are callee-saved | Partially |
other V, Z, P | Caller-saved. The SVE and SME extensions to the PCS are in 15.2 | No |
Points that matter in firmware:
X16/X17and veneers. A linker can insert a veneer between aBLand a far target that usesX16/X17. Assembly that keeps live values in them across a call to another object file can break at link time. They are also the registers through whichBRreaches a BTIclanding pad from a PLT. Details are in 13.1.- No red zone in AAPCS64. Code may not use memory below SP. An exception taken to the same EL while that EL runs on
SP_ELxstarts its handler on the same stack and would overwrite it. Some platform ABIs (Apple’s) define one, but neither firmware nor Linux does. - SMCCC is not AAPCS64. The SMC calling convention passes up to
X0–X17as arguments and results. SMCCC v1.0 leftX4–X17unpredictable on return, v1.1 mandates their preservation, and v1.2 allows them to carry results (and otherwise still preserves them).X18–X30and the stack pointers are always callee-preserved. A C wrapper aroundsmc #0therefore has to declare its clobbers explicitly. See 12.3. - FP/SIMD in firmware. TF-A and most RTOS kernels build with
-mgeneral-regs-only, so they never touchVregisters. EL3 then has to save the lower world’s FP state only when a world switch can expose it (TF-A’sCTX_INCLUDE_FPREGS). See 15.3.
X18, the platform register
AAPCS64 leaves X18 to the platform. A platform either reserves it for a fixed purpose or declares it an ordinary temporary. Known uses:
- Linux kernel with Shadow Call Stack (
CONFIG_SHADOW_CALL_STACK):X18holds the shadow-stack pointer, and the kernel is compiled with-ffixed-x18. - Windows on Arm:
X18points to the TEB in user mode. - Apple platforms: reserved, and must not be used.
- Most bare-metal and firmware builds: an ordinary temporary.
Security note: The danger is at boundaries. A kernel that relies on SCS trusts
X18on return from any call. If firmware or a Secure partition clobbersX18across an SMC and the kernel’s SMC wrapper does not save it, the kernel loses its shadow-stack pointer. SMCCC requires the callee to preserveX18(withX19–X30) across calls, which is why EL3 must restore the full GPR context on every return to the Normal world, and why the reverse direction (OP-TEE or an SP calling into a hosted service) needs the same care.
Frame records
When a function establishes a frame, AAPCS64 asks it to store a frame record of two 64-bit words, the caller’s X29 followed by X30, and to point X29 at that record. The records form a linked list through the stack:
X29 ──► +0x00 previous X29 ──► (caller's frame record) ──► ... ──► 0
+0x08 saved X30 (return address into caller)
func:
stp x29, x30, [sp, #-32]! // push frame record (+16 bytes of locals)
mov x29, sp // FP points at the record
...
ldp x29, x30, [sp], #32
ret
A crash handler can walk the list from the faulting X29 to produce a backtrace without unwind tables. This is usually the only backtrace available in early firmware. Three caveats apply. Leaf functions and code built with -fomit-frame-pointer do not push records. The chain should end with a zero X29, so set x29 = 0 before the first C call at reset. With PAC-RET (-mbranch-protection=pac-ret), the saved X30 is signed, so the backtracer must strip the PAC (XPACI, or XPACLRI on X30) before symbolising it (13.1). Decoding a crash end to end, including ELR and FAR, is in 18.1.
Checkpoint
Q2.2.1 An EL1 kernel runs with PSTATE.SP = 0 (on SP_EL0) inside a critical section, and an IRQ is taken to EL1. Which vector-table entry is used, and which stack does the handler start on?
- A. “Current EL with SP0”, running on
SP_EL0 - B. “Current EL with SP0”, running on
SP_EL1 - C. “Current EL with SPx”, running on
SP_EL1 - D. “Lower EL using AArch64”, running on
SP_EL1
Answer
B. The vector group records the SP selection that was in use when the exception was taken, so it is “Current EL with SP0”. Exception entry then sets PSTATE.SP = 1, so the handler runs on SP_EL1. Option A assumes the handler inherits the interrupted SP selection. Option C names the right stack but the wrong vector group. The interrupted code was at EL1, not a lower EL, so D is wrong.
Read more: Arm ARM (DDI0487), “Exception vectors” and SPSel; see also 3.1
Q2.2.2 A Normal-world kernel built with Shadow Call Stack starts crashing on return from one particular SMC. The Secure-side handler is C code compiled as an ordinary bare-metal build. What is the most likely cause?
- A. The SMC handler did not preserve
X29, which breaks the frame-record chain - B.
X16/X17were clobbered by a linker veneer inside the Secure image - C. The handler left SP misaligned, so
SCTLR_EL1.SAfaulted after return - D.
X18was used as a temporary on the Secure side, and EL3 returned without restoring the Normal world’sX18
Answer
D. SCS keeps the shadow-stack pointer in X18, and in an ordinary bare-metal build X18 is a temporary. Registers are not banked by Security state, so unless EL3 restores the Normal world’s saved GPRs (SMCCC requires X18 to be preserved), the kernel resumes with a corrupted shadow-stack pointer and faults on its next shadow-stack pop. X29 would also need preserving, but losing it breaks backtraces, not returns. Veneers inside the Secure image cannot affect the Normal world’s registers once EL3 restores the context.
Read more: Arm SMC Calling Convention (DEN0028), register preservation; AAPCS64, “General-purpose registers”
Q2.2.3 Early boot code does mov w0, #0x80000000 and movk x0, #0x1, lsl #32, then ldr w1, [x0]. Which statement about the registers is correct?
- A.
x0 = 0x1_8000_0000and the upper 32 bits ofx1are zero after the load - B.
x0 = 0x1_8000_0000andx1holds the loaded word sign-extended, because bit 31 of the address is set - C.
x0 = 0x8000_0000, because writes tow0lock out the upper half until the nextXwrite - D.
x0 = 0xffff_ffff_8000_0000, becausemov w0sign-extends intox0
Answer
A. A write to w0 zero-extends, giving 0x0000_0000_8000_0000. MOVK then replaces bits [47:32] only and keeps the rest. ldr w1 also writes w1 and zero-extends into x1. Only ldrsw sign-extends, and it does so based on the loaded data, not the address, so B mixes up two things. D is the common belief that 32-bit writes sign-extend, and they never do.
Read more: Arm ARM (DDI0487), “Registers in AArch64 state” and the MOVK/LDRSW instruction descriptions