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

15.1 Advanced SIMD and FP

In AArch64, floating-point and Advanced SIMD (Neon) are one register file, one pair of control/status registers and one set of trap controls. The architecture lets ID_AA64PFR0_EL1 report them as absent, but they are implemented together or not at all, and every Arm-designed A-profile core and every OS ABI for general-purpose software assumes them. Firmware cares about three things here: where the state lives, what controls its behaviour, and how the trap controls let software (an OS, a hypervisor or EL3) defer saving it.

The register file

There are 32 registers, V0–V31, each 128 bits wide. The register name chooses how many low-order bits an instruction accesses:

NameWidthTypical use
Bn8Scalar byte (SIMD scalar ops)
Hn16Half-precision (FEAT_FP16) or BFloat16 scalar
Sn32Single precision
Dn64Double precision, or a 64-bit vector (Vn.8B, Vn.4H, Vn.2S)
Qn128Full 128-bit vector (Vn.16B, Vn.8H, Vn.4S, Vn.2D)

A write to a scalar view zeroes the rest of the 128-bit register: FMOV S0, W1 clears bits [127:32] of V0. It does not merge with them, as AArch32 did with S0/S1 packed in D0. When SVE is implemented, Vn is the low 128 bits of Zn, and an Advanced SIMD or FP write zeroes the bits of Zn above bit 127 as well (15.2). That zeroing is what makes FP context handling and SVE context handling inseparable.

The minimum context for a thread or world is therefore 32 × 16 = 512 bytes of vector state plus FPCR and FPSR. For an AArch32 EL1 or EL0, add FPEXC32_EL2. TF-A’s simd_regs_t (in include/lib/el3_runtime/simd_ctx.h) has that layout when CTX_INCLUDE_FPREGS=1 and SVE context is not included, with FPSR and FPCR each in an 8-byte slot (528 bytes before the AArch32 extras).

Under AAPCS64, the low 64 bits of V8–V15 are callee-saved and everything else is caller-saved (2.2). Exception entry saves no FP state. If any code between the vector and the ERET may touch the registers, the handler must save them itself, or arrange for that code never to touch them.

FPCR: control

FPCR is accessed with MRS/MSR FPCR and is readable and writable at EL0. Its main fields:

FieldBitsMeaning
AHP26Alternative half-precision format (no infinities or NaNs) for FP16 conversions
DN25Default NaN: NaN results are the default NaN instead of propagating the input payload
FZ24Flush denormal inputs and outputs to zero (single and double precision)
RMode23:22Rounding: 00 RN (nearest), 01 RP (towards +inf), 10 RM (towards -inf), 11 RZ
FZ1619Flush-to-zero for half precision (FEAT_FP16)
IDE, IXE, UFE, OFE, DZE, IOE15, 12–8Trap enables for input-denormal, inexact, underflow, overflow, divide-by-zero, invalid
EBF13Extended BFloat16 behaviour (FEAT_EBF16)
NEP, AH, FIZ2, 1, 0FEAT_AFP: output element preservation for scalar ops, alternate (x86-compatible) handling, flush inputs to zero

Bits [21:20] and [18:16] (Stride, Len) exist only for AArch32 compatibility. Two points are commonly misremembered:

  • Floating-point exception trapping is optional. Whether the trap-enable bits are implemented is IMPLEMENTATION DEFINED. If they are not, they are RAZ/WI, and software has to write a 1 and read the bit back to find out. When they are implemented and enabled, an FP exception is taken as a synchronous exception with EC 0x2C (AArch64) or 0x28 (AArch32). Most Cortex and Neoverse cores do not implement it, so the cumulative flags in FPSR are the only reporting mechanism.
  • FPCR is part of the thread context. An exception handler that changes the rounding mode or FZ and does not restore them silently changes the numerics of the interrupted code.

FPSR: status

FPSR holds the cumulative (sticky) exception flags: IOC (bit 0), DZC (1), OFC (2), UFC (3), IXC (4), IDC (7), and QC (bit 27), the Advanced SIMD saturation flag. Bits [31:28] (N, Z, C, V) are used only by AArch32 VMRS APSR_nzcv. In AArch64, FP compares write PSTATE.NZCV directly. The flags are sticky: hardware sets them and only software clears them. They therefore show whether anything since the last clear raised the condition, which makes them useful as cheap test oracles.

Discovery

ID_AA64PFR0_EL1.FP (bits [19:16]) and .AdvSIMD (bits [23:20]) are signed fields: 0b0000 means implemented, 0b0001 means implemented with half-precision arithmetic (FEAT_FP16, permitted from v8.2), and 0b1111 means not implemented. The many optional extensions to the instruction set (FEAT_DotProd, FEAT_FHM, FEAT_FCMA, FEAT_JSCVT, FEAT_BF16, FEAT_I8MM, FEAT_FRINTTS, FEAT_AFP, FEAT_RPRES and others) are reported in ID_AA64ISAR0_EL1/ISAR1_EL1. None of them adds architectural state except FEAT_AFP and FEAT_EBF16, which add the FPCR bits above. Their version information is in Appendix A.

The trap controls

Each Exception level above EL0 can trap FP/SIMD use at the levels below it, and in some cases at its own level:

ControlTrapsEncoding
CPACR_EL1.FPEN [21:20]EL0 and EL1 to EL1 (no effect when HCR_EL2.{E2H,TGE}={1,1}; CPTR_EL2.FPEN then controls EL0)0b00/0b10: trap EL0 and EL1; 0b01: trap EL0 only; 0b11: no trap
CPTR_EL2.TFP bit 10 (E2H=0)EL0, EL1 and EL2 to EL21 traps
CPTR_EL2.FPEN [21:20] (E2H=1)EL0, EL1 and EL2 to EL20b00/0b10: trap EL0, EL1 and EL2; 0b01: trap EL0 only when HCR_EL2.TGE=1, otherwise no trap; 0b11: no trap
CPTR_EL3.TFP bit 10All ELs to EL31 traps

A trapped instruction, or a trapped access to FPCR/FPSR, is reported with EC 0x07. For a trap at EL2 or EL1 the ISS gives no further detail in AArch64. At EL3 a trapped FP access appears to the monitor as an exception from whatever world was running, and TF-A’s default handling of an unexpected EC 0x07 is a panic.

The register-by-register overview is in 2.6. EL3 programming is in 15.3.

Bring-up note: CPACR_EL1 and CPTR_ELx are system registers. After an MSR that removes a trap, the change is only guaranteed to affect later instructions after a context synchronization event, so an ISB must come before the first FP instruction (7.8). Compilers also emit FP/SIMD instructions for plain C, such as memcpy/memset expansion, struct copies and vectorised loops. Firmware that runs with FP trapped must be built with -mgeneral-regs-only (TF-A does this). If an early-boot C function suddenly takes EC 0x07, the most likely cause is a new compiler or a missing flag, not the hardware.

Lazy context switching

FP state is large compared with the integer context and many threads never touch it. The traps above let a kernel or hypervisor defer the save and restore:

  1. On a context switch, leave the outgoing thread’s FP state in the registers and record that the registers hold thread A’s state. Set the trap (FPEN = 0b01 or 0b00).
  2. If thread B executes an FP instruction, it takes EC 0x07. The handler saves the registers into A’s context, loads B’s context, records B as the owner, clears the trap and returns to re-execute the faulting instruction. Because the exception is synchronous and precise, no partial state exists.
  3. If B never touches FP, nothing is saved or restored, and A finds its state intact if it is scheduled again on the same CPU.

The bookkeeping that matters is “which context currently owns this CPU’s registers” (per CPU) and “is this context’s state live in some CPU’s registers” (per context). Migration of a thread to another CPU, and power-down of a CPU (8.8, 17.4), must force the save first, because the registers are lost.

Real systems use variants of this scheme:

  • Linux arm64 now restores FP state lazily on the return to user space (a per-task “foreign FP state” flag), not on a trap. It uses trap-on-first-use only to detect when a task first uses SVE or SME, so that the larger buffers are allocated on demand.
  • KVM keeps the guest’s FP/SIMD trapped on entry and switches host and guest state on the first guest FP trap. This is the classic trap-based form, because many exits never touch FP.
  • TF-A does not switch lazily. With CTX_INCLUDE_FPREGS=1 it saves and restores eagerly on world switch. By default it saves nothing and relies on the secure world not touching the registers (15.3).

Security note: Lazy switching leaves another context’s values in the registers. That is safe only while the trap reliably fires before any read. Two classes of bug recur. The first is a trap window: code at a higher EL, or a different world, uses FP without saving first. Neither CPACR_EL1 nor the lower EL’s lazy state machine traps EL3, so an EL3 or S-EL1 service that uses Neon for crypto leaks or corrupts the Normal-world state unless it saves and restores. The second is speculative reads of trapped state. That issue was disclosed for another architecture in 2018 as “Lazy FP state restore” (CVE-2018-3665). Check the core’s security advisories before relying on lazy switching across a trust boundary. For a cross-world boundary, eager save/restore or zeroing is the defensible default.

Checkpoint

Q15.1.1 An OS uses trap-based lazy FP switching with CPACR_EL1.FPEN=0b01 for user threads. A secure-world service at S-EL1 called through EL3 uses Neon for AES and does not save the registers. TF-A is built with CTX_INCLUDE_FPREGS=0. What happens to a Normal-world user thread whose FP state was live in the registers?

  • A. Nothing, because the S-EL1 FP instructions take an EC 0x07 trap to the Normal-world kernel, which saves the state first
  • B. Nothing, because CPACR_EL1 is banked per Security state and the Secure copy traps the access to EL3
  • C. Its FP state is silently corrupted, because no Normal-world trap applies to S-EL1 execution and nobody saved the registers
  • D. The S-EL1 access faults with EC 0x07 to S-EL1, because FPEN=0b01 traps EL0 and EL1
Answer

C. The register file is shared by all worlds. The Normal-world CPACR_EL1 value does not apply while S-EL1 runs: EL3 switches the EL1 system registers, so S-EL1 runs with its own CPACR_EL1 value. No trap returns control to the Normal-world kernel. Unless EL3 saves FP state per world, or the S-EL1 code saves what it overwrites, the Normal-world values are lost. A is the tempting answer, but a trap is always taken to the controlling EL of the regime that is executing, never to another world’s kernel. D misreads 0b01, which traps EL0 only.

Read more: Arm ARM (DDI0487), CPACR_EL1, CPTR_EL3; TF-A build options, CTX_INCLUDE_FPREGS

Q15.1.2 A test writes 1 to FPCR.IOE, executes FDIV of 0.0 by 0.0, and expects an EC 0x2C exception. None is taken, but FPSR.IOC reads 1. What is the most likely explanation?

  • A. The implementation does not support FP exception trapping, so IOE is RAZ/WI; reading FPCR back would show 0
  • B. An ISB was missing between the MSR FPCR and the FDIV, so the old trap-enable value was used
  • C. EC 0x2C traps are taken only when CPTR_EL3.TFP=1
  • D. 0.0/0.0 raises divide-by-zero, not invalid operation, so DZE should have been set
Answer

A. Support for FP exception trapping is IMPLEMENTATION DEFINED, and most Arm-designed cores do not implement it. The enable bits then read as zero and writes are ignored, so only the sticky FPSR flag records the event. Reading the register back is the standard detection method. D is wrong because 0/0 is an invalid operation (it yields a NaN); DZC is for a finite non-zero value divided by zero. C confuses the access trap (EC 0x07) with the FP exception trap. B is wrong because FPCR is a special-purpose register, not a System register that needs an ISB: an MSR FPCR affects the following instructions directly.

Read more: Arm ARM (DDI0487), FPCR, FPSR, Floating-point exceptions and exception traps

Q15.1.3 SVE is implemented with a 256-bit vector length. A Normal-world thread holds live data in Z0[255:128]. A secure service, allowed to use FP but not SVE, executes MOV V0.16B, V1.16B and then restores the low 128 bits of V0 exactly. What does the Normal-world thread observe?

  • A. All 256 bits intact, because the restore puts V0 back
  • B. All bits intact, because SVE state is banked between Security states
  • C. An EC 0x19 trap when the secure service executes the Advanced SIMD instruction, because SVE is disabled for it
  • D. Z0[255:128] reads as zero, because an Advanced SIMD write zeroes the bits of Z0 above bit 127
Answer

D. V0 is the low 128 bits of Z0, and any Advanced SIMD or FP write zeroes the higher bits. Saving and restoring only the 128-bit view does not preserve SVE state. This is why TF-A requires CTX_INCLUDE_SVE_REGS when CTX_INCLUDE_FPREGS and ENABLE_SVE_FOR_NS are both set. C is wrong because Advanced SIMD instructions are governed by the FP controls, not the SVE ones, even when SVE is trapped.

Read more: Arm ARM (DDI0487), The Scalable Vector Extension, SVE registers (relationship of Z and V registers); TF-A build options, CTX_INCLUDE_SVE_REGS