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

3.2 Entry and return

What the PE writes on entry to AArch64 ELx

When an exception is taken to an AArch64 target ELx, the PE performs these steps, all as one atomic architectural action:

  1. SPSR_ELx ← PSTATE of the interrupted context (the AArch64 or AArch32 format, depending on where it came from).
  2. ELR_ELx ← preferred return address (3.1). If the exception came from AArch32, bits [63:32] are zero.
  3. ESR_ELx ← syndrome, for synchronous exceptions and SError. IRQ and FIQ report no syndrome, so ESR_ELx holds nothing meaningful for them. It is not guaranteed to be “unchanged” either: the register description makes ESR_ELx UNKNOWN on any exception return from ELx.
  4. FAR_ELx ← faulting virtual address, only for the exception types listed below. Otherwise FAR_ELx becomes UNKNOWN.
  5. PSTATE updated as in the table below, and the PC is set to the vector address.

Exception entry does not save or modify the general-purpose registers, and saving them is the handler’s job. On entry from AArch32, do not rely on the upper 32 bits of the X registers that held AArch32 state. The Arm ARM constrains what they may contain (see the AArch32-to-AArch64 register mapping rules), but software should treat them as not meaningful.

PSTATE on entry

FieldValue after entry to ELxNote
ELtarget EL
nRW0 (AArch64)
SP1 (use SP_ELx)
D, A, I, Fall set to 1regardless of which class was taken
SS0software step state, see 16.1
IL0
BTYPE0FEAT_BTI
PANset to 1 if SCTLR_ELx.SPAN == 0, otherwise unchangedFEAT_PAN. Applies to entry to EL1, and to EL2 when HCR_EL2.{E2H,TGE} == {1,1}
UAO0FEAT_UAO
SSBSSCTLR_ELx.DSSBSFEAT_SSBS
TCO1FEAT_MTE. Tag checks are suppressed until software clears TCO
ALLINTNOT SCTLR_ELx.SPINTMASK (applied whenever FEAT_NMI is implemented, even if SCTLR_ELx.NMI == 0)FEAT_NMI, see 4.3
EXLOCKper GCSCR_ELx.EXLOCKENFEAT_GCS, see 13.2
DIT, NZCV, SM, ZAunchangedSME streaming mode survives exception entry

For the newer fields (PM, UINJ and the like), see 2.4 and the Arm ARM pseudocode for AArch64.TakeException(), which is the authoritative list.

SPSR_ELx (AArch64 format)

BitsFieldBitsField
[3:0]M[3:0]: EL and SP (0b0000 EL0t, 0b0100 EL1t, 0b0101 EL1h, 0b1000 EL2t, 0b1001 EL2h, 0b1100 EL3t, 0b1101 EL3h)[20]IL
[4]M[4]: 0 = AArch64[21]SS
[9:6]D A I F[22]PAN
[11:10]BTYPE[23]UAO
[12]SSBS[24]DIT
[13]ALLINT[25]TCO
[31:28]N Z C V[63:32]newer fields (PM, EXLOCK, …), see DDI0487

ESR_ELx: EC, IL, ISS

 63      56 55          32 31    26 25  24                     0
+----------+--------------+--------+---+------------------------+
|   RES0   |     ISS2     |   EC   |IL |          ISS           |
+----------+--------------+--------+---+------------------------+
  • EC [31:26] is the exception class. It decides how to parse ISS.
  • IL [25] is the instruction length: 1 = 32-bit. It is 0 only for a 16-bit T32 instruction trap. It is also 1 for SError, Instruction Aborts, PC and SP alignment faults, Data Aborts with ISV == 0, Illegal Execution state, debug exceptions other than BRK/BKPT, and EC 0x00.
  • ISS [24:0] is class-specific. For Data Aborts, ISS[24] is ISV (the SAS/SRT/… instruction-syndrome fields are valid). For many trap classes it is CV, which only says that the COND field (for a conditional AArch32 instruction) is valid.
  • ISS2 [55:32] is used by FEAT_LS64 (Xs), FEAT_GCS, FEAT_THE and FEAT_S1POE/S2POE fault reporting, among others.

EC summary

ECMeaningECMeaning
0x00Unknown reason (UNDEFINED instruction)0x1FIMPLEMENTATION DEFINED, to EL3
0x01Trapped WFI/WFE/WFIT/WFET0x20Instruction Abort, lower EL
0x07Trapped SVE/SIMD/FP access (CPACR/CPTR)0x21Instruction Abort, same EL
0x09Trapped PAuth instruction (HCR_EL2.API/SCR_EL3.API)0x22PC alignment fault
0x0DBranch Target exception (BTI)0x24Data Abort, lower EL
0x0EIllegal Execution state0x25Data Abort, same EL
0x15SVC (AArch64)0x26SP alignment fault
0x16HVC (AArch64)0x27Memory Operation exception (FEAT_MOPS)
0x17SMC (AArch64)0x2CTrapped floating-point exception (AArch64)
0x18Trapped MSR/MRS/system instruction0x2DGCS exception
0x19Trapped SVE access0x2FSError
0x1CPAC authentication failure (FEAT_FPAC)0x30/0x31Breakpoint, lower/same EL
0x1DTrapped SME access0x32/0x33Software step, lower/same EL
0x1EGranule Protection Check (FEAT_RME)0x34/0x35Watchpoint, lower/same EL
0x11/0x12/0x13SVC/HVC/SMC from AArch320x3CBRK (AArch64)

The full table, with the ISS layout of each class and the DFSC/IFSC values, is Appendix D. Abort ISS fields (WnR, S1PTW, CM, FnV, ISV/SAS/SRT) are in 5.8.

FAR_ELx validity

FAR_ELx is written only on:

  • Instruction Abort and Data Abort (EC 0x20, 0x21, 0x24, 0x25);
  • PC alignment fault (0x22);
  • Watchpoint (0x34, 0x35), where the recorded address is within the watched range but which byte is recorded is constrained, not exact;
  • Granule Protection Check exceptions on an instruction fetch or data access;
  • SError with FEAT_RASv2, but only when ISS.VFV == 1 (3.4).

For every other exception, FAR_ELx is UNKNOWN. That means it is not even guaranteed to keep its old value. Even for aborts, three rules apply:

  • For a synchronous External abort that is not on a translation table walk, ISS.FnV == 1 means FAR_ELx is UNKNOWN.
  • When the address was tagged (TBI), the top byte of the reported address can be UNKNOWN for some fault types. Treat FAR[63:56] as reliable only where DDI0487 says so for the implemented features (FEAT_MTE changes this).
  • FAR_ELx holds a VA. For a stage-2 fault the IPA is in HPFAR_EL2, and with FEAT_PFAR a PA can be in PFAR_ELx. See 5.8.

ERET and exception return

ERET (and ERETAA/ERETAB with PAuth) at ELx restores PC ← ELR_ELx and PSTATE ← SPSR_ELx, subject to a legality check. It is UNDEFINED at EL0. A return is an illegal exception return if SPSR_ELx asks for any of the following:

  • an EL higher than the current EL;
  • an EL that is not implemented, or an M[3:0] encoding that is reserved (for example 0b0001, EL0 with SP_ELx);
  • EL2 when EL2 is not enabled in the current Security state (for example Secure EL2 with SCR_EL3.EEL2 == 0);
  • EL1 when EL2 is enabled and HCR_EL2.TGE == 1 (EL1 is not in use);
  • AArch32 (M[4] == 1) at a level that SCR_EL3.RW/HCR_EL2.RW define as AArch64, or AArch64 at a level defined as AArch32, or AArch32 where AArch32 is not implemented.

On an illegal return, PSTATE.EL, PSTATE.nRW and PSTATE.SP are unchanged, PSTATE.IL is set to 1, the other PSTATE fields are restored from SPSR, and the PC is restored from ELR_ELx. The return itself does not fault. The next instruction the PE attempts to execute takes an Illegal Execution state exception (EC 0x0E) to the current EL, with ELR pointing at that instruction (unless a higher-priority synchronous exception on that instruction, such as a PC alignment fault or an Instruction Abort, is taken first). This design guarantees that software never runs at an EL or Execution state it could not legally reach. A mis-built SPSR therefore shows up as an EC 0x0E exception at the same EL as the ERET, at the address you were trying to return to.

A return to AArch64 with ELR[1:0] != 0 is not illegal. It produces a PC alignment fault at the target when the first instruction is fetched.

    // EL3 -> Non-secure EL2h, AArch64, DAIF masked
    mov  x0, #(0b1001)              // M = EL2h
    orr  x0, x0, #(0xF << 6)        // D A I F
    msr  SPSR_EL3, x0
    adr  x1, ns_el2_entry
    msr  ELR_EL3, x1
    // SCR_EL3.NS=1, RW=1, HCE=1 already programmed (see 17.2)
    eret

Context synchronization, and FEAT_ExS

By default, exception entry and exception return are context synchronization events. System register writes made before them are visible to instructions after them, without an ISB. That is why “write SCTLR_EL1 at EL2, then ERET” works.

FEAT_ExS (permitted from v8.5) makes this configurable per EL:

ControlBit01
SCTLR_ELx.EIS22Exception entry to ELx is not a context synchronization eventEntry is a context synchronization event
SCTLR_ELx.EOS11ERET from ELx is not a context synchronization eventERET is a context synchronization event

Without FEAT_ExS these bits are RES1. With FEAT_ExS they reset to an architecturally UNKNOWN value, so software must write them to 1 explicitly: firmware that writes the whole register with the v8.0 RES1 pattern (for example TF-A’s SCTLR_EL3 reset value) keeps the synchronizing behaviour, but code that only read-modify-writes SCTLR_ELx from reset does not get it guaranteed. Clearing them is an optimization for hypervisors and kernels with hot entry paths. Clearing them also means that any system register write whose effect the next context depends on, such as TTBR, TCR or SCTLR before an ERET, needs an explicit ISB. The PSTATE values that entry itself writes still apply to the handler. What is lost is the synchronization of earlier context-changing operations, such as system-register writes not yet followed by an ISB. See 7.8 for the full context-synchronization rules, and note that neither entry nor return is a DSB: outstanding memory accesses are not waited for.

Bring-up note: when you see EC 0x0E immediately after a hand-off ERET, decode SPSR_ELx.M against SCR_EL3.{RW,NS,EEL2} and HCR_EL2.{RW,E2H,TGE} before anything else. ELR will equal the entry point you returned to, which makes the bug look like “the next image crashed on its first instruction”.

Checkpoint

Q3.2.1 On a core that implements AArch32 at EL1, EL3 firmware writes SPSR_EL3 = 0x3C5 (EL1h, DAIF set) and ERETs to a Non-secure kernel, but SCR_EL3.RW == 0. What happens?

  • A. The ERET itself takes a synchronous exception to EL3 with EC 0x0E
  • B. The return is illegal: the PE stays at EL3 with PSTATE.IL == 1, the PC becomes ELR_EL3, and the first instruction there takes an EC 0x0E exception to EL3
  • C. The PE enters Non-secure EL1 in AArch32 state, because SCR_EL3.RW overrides SPSR.M[4]
  • D. Behaviour is UNPREDICTABLE
Answer

B. SPSR.M[4] == 0 requests AArch64, which conflicts with SCR_EL3.RW == 0, so this is an illegal exception return. EL, nRW and SP are kept, IL is set, and the PC is loaded from ELR. The Illegal Execution state exception is taken when the next instruction is executed, not by the ERET (A). The architecture defines this behaviour exactly, so it is neither “silently fixed up” (C) nor UNPREDICTABLE (D).

Read more: Arm ARM (DDI0487), Illegal return events from AArch64 state; The Illegal Execution state exception

Q3.2.2 A hypervisor on a core with FEAT_ExS sets SCTLR_EL2.EOS = 0. Before ERET to a guest it writes VTTBR_EL2 and HCR_EL2. Which statement is correct?

  • A. No change is needed, because ERET always synchronizes system-register writes made at the same EL
  • B. A DSB SY before ERET is sufficient
  • C. Only the HCR_EL2 write needs an ISB, because VTTBR_EL2 is only used by the walker
  • D. An ISB is required before ERET, because with EOS == 0 the return is no longer a context synchronization event
Answer

D. EOS == 0 removes the guarantee that the ERET synchronizes earlier context-changing writes, so the guest’s first instructions could run under the old HCR_EL2 or stage-2 base. An ISB restores the guarantee. A DSB waits for memory accesses, not for system-register context (B). The walker is part of the context that must be synchronized too (C).

Read more: Arm ARM (DDI0487), SCTLR_EL2.EOS; Context synchronization event

Q3.2.3 An IRQ handler at EL1 logs ESR_EL1 and FAR_EL1, and both contain plausible values from a page fault handled earlier. What can you conclude?

  • A. Nothing: an IRQ writes no syndrome or address, and both registers are UNKNOWN by then, so stale-looking values carry no information
  • B. The IRQ was raised by the earlier page fault
  • C. The hardware preserved them, which is guaranteed for asynchronous exceptions
  • D. ESR_EL1.IL == 0 would distinguish the two cases
Answer

A. The syndrome registers are written only for exception types that report a syndrome or address. IRQ and FIQ write neither, FAR_EL1 is UNKNOWN for any exception other than the listed abort, alignment and watchpoint types, and both registers are made UNKNOWN by every exception return from EL1, including the one that ended the earlier page-fault handler. An implementation may happen to leave the old value, but software must not rely on it (C). Logging them in an IRQ path, or comparing them, is a frequent source of misleading crash reports.

Read more: Arm ARM (DDI0487), ESR_EL1; FAR_EL1; Exception entry