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.3 Firmware responsibilities

FP, SVE and SME follow the rule set out in 2.6: the feature arrives trapped or disabled, and each higher Exception level must enable it for the levels below. EL3 is at the top of that chain. Its registers reset to UNKNOWN values, so nothing works until EL3 writes them. EL3 also owns the hard part, because all worlds share one register file and only EL3 sees every world switch. This section covers what EL3 and EL2 must program, how TF-A organises the per-world state, and what a missing bit looks like. 17.3 has the full FEAT_-to-enable-bit table; only the FP/SVE/SME rows are covered here.

The EL3 controls

FeaturePresent ifEL3 enableAlso programTrap when missing
FP/Advanced SIMDID_AA64PFR0_EL1.FP/AdvSIMD ≠ 0b1111CPTR_EL3.TFP=0 (bit 10)EC 0x07 to EL3
SVEID_AA64PFR0_EL1.SVE ≠ 0CPTR_EL3.EZ=1 (bit 8)ZCR_EL3.LEN (VL cap)EC 0x19 to EL3
SMEID_AA64PFR1_EL1.SME ≠ 0CPTR_EL3.ESM=1 (bit 12)SMCR_EL3.LEN, .FA64EC 0x1D, SMTC 0, to EL3
SME2 (ZT0)ID_AA64PFR1_EL1.SME ≥ 2SMCR_EL3.EZT0=1 (bit 30)EC 0x1D, SMTC 4, to EL3
TPIDR2_EL0SMESCR_EL3.EnTP2=1 (bit 41)EC 0x18 to EL3
FPMRID_AA64PFR2_EL1.FPMR ≠ 0SCR_EL3.EnFPM=1 (bit 50)EC 0x18 to EL3 (direct access); an FP8 instruction is UNDEFINED instead (EC 0x00, taken to the current EL, or EL1/EL2 from EL0)

Several details in this table are commonly got wrong:

  • Opposite polarities. TFP traps when 1. EZ and ESM enable when 1. A zeroed CPTR_EL3 therefore enables FP but traps SVE and SME.
  • The traps also apply to EL3 itself. The pseudocode for these checks tests CPTR_EL3 whatever the current EL is. With EZ=0, an MSR ZCR_EL3 or an SVE instruction executed at EL3 traps to EL3. So the order is: write CPTR_EL3, then ISB, then program ZCR_EL3/SMCR_EL3. The same applies to ESM and SMCR_EL3.
  • ZCR_EL3.LEN and SMCR_EL3.LEN cap every lower EL. EL3 should normally set the maximum value, 0xF, and leave lower ELs to restrict further. A lower cap at EL3 is a platform decision: TF-A exposes it as SVE_VECTOR_LEN. One reason to use it is a system whose cores support different VLs, because Linux requires every CPU to support the vector lengths it has already committed to and rejects a late CPU that does not.
  • SVE implies FP. SVE instructions are checked against the FP controls as well (15.2), so EZ=1 with TFP=1 still traps SVE, with EC 0x07.
  • FA64 is a policy choice, not a fix. Setting it where FEAT_SME_FA64 is implemented lets streaming code use the full A64 ISA. Where it is not implemented the bit is RES0, so firmware must check ID_AA64SMFR0_EL1.FA64 rather than set it blindly.

A minimal cold- and warm-boot sequence, before any lower EL runs, looks like this:

    mrs     x0, id_aa64pfr0_el1
    mrs     x1, id_aa64pfr1_el1
    mov     x2, #0                      // CPTR_EL3 resets UNKNOWN: build it from zero
                                        // TFP = 0: FP/SIMD not trapped
                                        // (TAM/TTA/TCPAC policy: see 17.2)
    ubfx    x3, x0, #32, #4             // ID_AA64PFR0_EL1.SVE
    cbz     x3, 1f
    orr     x2, x2, #(1 << 8)           // EZ = 1
1:  ubfx    x4, x1, #24, #4             // ID_AA64PFR1_EL1.SME
    cbz     x4, 2f
    orr     x2, x2, #(1 << 12)          // ESM = 1
2:  msr     cptr_el3, x2
    isb                                 // CPTR_EL3 write visible before ZCR/SMCR access
    cbz     x3, 3f
    mov     x5, #0xf
    msr     S3_6_C1_C2_0, x5            // ZCR_EL3.LEN = max
3:  cbz     x4, 4f
    mov     x5, #0xf                    // SMCR_EL3.LEN = max
    mrs     x6, S3_0_C0_C4_5            // ID_AA64SMFR0_EL1
    tbz     x6, #63, 5f                 // FA64 implemented?
    orr     x5, x5, #(1 << 31)          // FA64 = 1 (policy choice)
5:  cmp     x4, #2                      // SME2 or later?
    b.lo    6f
    orr     x5, x5, #(1 << 30)          // EZT0 = 1
6:  msr     S3_6_C1_C2_6, x5            // SMCR_EL3
4:  isb

SCR_EL3.EnTP2 and EnFPM are per-world values. They belong in the SCR_EL3 that EL3 installs before each ERET, not in a one-off write at boot. Both CPTR_EL3 and these SCR_EL3 bits must be reprogrammed on every warm boot, because they are lost when the core powers down (17.4).

EL2: present or unused

When a hypervisor runs at EL2, it owns CPTR_EL2, ZCR_EL2, SMCR_EL2 and HCRX_EL2.EnFPM, and it typically uses them for lazy switching of guest state (15.1). EL3’s only job is to leave EL2 able to use them. When EL2 is implemented but no EL2 software will run, and the Normal world enters at EL1, EL3 must program EL2 itself, because the EL2 controls still apply to EL1 and EL0 and they reset to UNKNOWN values. TF-A does this under INIT_UNUSED_NS_EL2.

Bring-up note: With HCR_EL2.E2H=0, CPTR_EL2 bits [9:0] and 12–13 are RES1 except where a feature gives them a meaning: bit 8 is TZ when SVE is implemented and bit 12 is TSM when SME is implemented. Firmware that writes “the RES1 mask” from the register description therefore sets TZ=1 and TSM=1, and traps SVE and SME. TF-A writes that mask and then clears TZ explicitly in sve_init_el2_unused(). If an EL1 kernel takes EC 0x19 or 0x1D to an EL2 that nobody installed, look here first.

Per-world context in TF-A

TF-A splits EL3-owned state into two kinds. Per-world values are identical on every CPU for a given world and live in per_world_context[] (lib/el3_runtime/aarch64/context_mgmt.c). They are currently ctx_cptr_el3 and ctx_mpam3_el3. Per-CPU, per-world values live in each cpu_context_t. They include the SCR_EL3 image that carries EnTP2 and EnFPM. While EL3 itself runs, TF-A installs a separate root context: CPTR_EL3 has TFP=1, so stray FP use in EL3 C code traps, and EZ=1/ESM=1, so EL3 can still access ZCR_EL3/SMCR_EL3. On exit, TF-A writes the target world’s ctx_cptr_el3 before the ERET.

The feature modules set the per-world values:

FunctionEffect
sve_init_el3()ZCR_EL3.LEN from SVE_VECTOR_LEN
sve_enable_per_world() / sve_disable_per_world()set / clear CPTR_EL3.EZ for a world
sme_init_el3()SMCR_EL3.LEN = max, FA64 and EZT0 if implemented
sme_enable_per_world() / sme_disable_per_world()set ESM / clear ESM and EZ
sme_enable(ctx) / sme_disable(ctx)set / clear SCR_EL3.EnTP2 in a CPU context

The build options choose the policy:

OptionMeaning
ENABLE_SVE_FOR_NS, ENABLE_SME_FOR_NS, ENABLE_SME2_FOR_NSEnable the feature for the Normal world (SME requires SVE). Values 0/1/2 follow the FEATURE_DETECTION convention: 2 means “check the ID register at runtime”.
ENABLE_SVE_FOR_SWD, ENABLE_SME_FOR_SWDAlso enable it for the Secure world. The SPMC or Secure OS must then manage the state.
ENABLE_FEAT_FPMRSet SCR_EL3.EnFPM
CTX_INCLUDE_FPREGSEL3 saves and restores V0–V31, FPSR, FPCR on world switch
CTX_INCLUDE_SVE_REGSEL3 saves Z, P and FFR at SVE_VECTOR_LEN. Requires ENABLE_SVE_FOR_SWD, and is mandatory when CTX_INCLUDE_FPREGS and ENABLE_SVE_FOR_NS are both set
SVE_VECTOR_LENThe ZCR_EL3 cap, default 2048

The default Secure-world policy is worth reading closely. With ENABLE_SVE_FOR_SWD=0, TF-A clears EZ (and ESM) for the Secure world but leaves TFP=0. Secure code can still execute FP/Advanced SIMD instructions, and each such write zeroes the upper bits of the Normal world’s Z registers (15.1). The policy is safe only if the Secure world never touches FP, or saves it at the full SVE width (which needs SVE enabled for it, ENABLE_SVE_FOR_SWD=1), or EL3 does (CTX_INCLUDE_SVE_REGS, which also requires ENABLE_SVE_FOR_SWD=1). TF-A’s documentation recommends doing the save in the lower EL, because the EL3 buffers are large: at a VL of 2048 bits, each world needs about 8.7 KB per CPU for SVE alone.

When the SPMD sits between the worlds, a cheaper option exists. SMCCC v1.3 defines bit 16 of the function ID as the SVE live-state hint. A caller that sets it declares that no SVE state above the 128-bit V registers, and no P or FFR state, needs preserving across the call. The SPMD reads the hint (is_sve_hint_set()), passes it to the SPMC, and uses it to skip the wide part of the save. For Realms, TF-A enables SVE and SME in the Realm world’s CPTR_EL3, and the RMM saves and restores whatever Normal-world state it overwrites (12.4).

SME across a world switch

TF-A’s SIMD context (simd_regs_t) covers V/Z, P, FFR, FPSR, FPCR and, for AArch32, FPEXC32. It does not cover SVCR, ZA, ZT0 or FPMR. The SME state is left to the lower-EL managers. This has three consequences for anyone porting a Secure OS or SPMC to an SME part:

  • An SMC can arrive with PSTATE.SM=1 and/or PSTATE.ZA=1, because exceptions do not change them (15.2). The Secure world then starts in streaming mode. Its first Advanced SIMD vector instruction traps (SMTC 1, unless FA64 is enabled), and a Z-register save runs at SVL instead of VL.
  • A Secure-world SMSTOP that leaves streaming mode zeroes the Normal world’s Z, P, FFR and FPMR and rewrites its FPSR. A Secure-world SMSTOP ZA discards the Normal world’s ZA. Either must be preceded by a save, at SVL, of everything the Normal world could own.
  • TPIDR2_EL0 is an EL0 register that EL3 does not switch. A Secure world that runs S-EL0 code using SME must save and restore it, or the Normal world’s lazy-save chain is corrupted.

The simplest correct policy for most products is the default: Secure and Realm code that does not need SME does not get it (ESM=0 for that world), and whichever manager first uses FP state in another world saves the Normal world’s state at the widest length currently in effect.

What a missing bit looks like

SymptomLikely cause
Normal-world kernel hangs or panics at EL3 during early boot, with an unhandled EC 0x19 in the EL3 crash dumpCPTR_EL3.EZ=0 while Linux probes SVE: it reads ZCR_EL1 and executes RDVL at boot if the ID registers report SVE
EC 0x1D with SMTC 0 at EL3CPTR_EL3.ESM=0 with ID_AA64PFR1_EL1.SME reporting SME
EC 0x18 at EL3 with the TPIDR2_EL0 or FPMR encoding in the ISSSCR_EL3.EnTP2 or EnFPM missing from that world’s SCR_EL3
Works on cold boot, fails after CPU_SUSPEND or hotplugEnable bits written once at boot, not restored on the warm-boot path
Normal-world SVE data randomly loses its high bitsA Secure or Realm service uses FP with no SVE-width save anywhere
Linux reports a smaller VL than the core supportsZCR_EL3.LEN/SVE_VECTOR_LEN or SMCR_EL3.LEN capped at EL3, or a hypervisor cap in ZCR_EL2

Linux sanitises the ID registers (1.1), but it trusts them. If the ID register reports SVE and EL3 traps it, the kernel does not degrade gracefully: it takes the trap at the first probe. A hypervisor can hide the feature from a guest by trapping and emulating ID_AA64PFR0_EL1 (HCR_EL2.TID3). EL3 cannot hide a feature from EL2 that way unless FEAT_IDTE3 is implemented (17.3). The practical rule is therefore: enable every feature that the ID registers advertise, or make the platform’s ID values match what you enable.

Validation note: For each world, run a directed test at the lowest EL that executes one FP instruction, one SVE instruction (RDVL), SMSTART/SMSTOP, one ZT0 access, an MRS of TPIDR2_EL0 and an MRS of FPMR. Expect either success or the documented trap for that world’s policy, never a trap to EL3. Repeat after CPU_SUSPEND to power-down and after CPU_OFF/CPU_ON. Then test cross-world integrity. Fill Z0–Z31 with patterns in the Normal world at the maximum VL, call an SMC handled by a Secure service that executes Advanced SIMD instructions, and compare. A mismatch above bit 127 shows that the save policy is broken, not the hardware.

Checkpoint

Q15.3.1 EL3 boot code executes MOV x0, #0xF; MSR ZCR_EL3, x0 and only afterwards sets CPTR_EL3.EZ=1. On the first boot of an SVE part it takes an exception straight away. Why?

  • A. ZCR_EL3 is write-once after reset and was already written by the boot ROM
  • B. CPTR_EL3.EZ=0 traps accesses to ZCR_EL3 even from EL3, so the write traps to EL3 (EC 0x19) before EZ is set
  • C. The ISB between the two writes is missing, so the ZCR_EL3 write is UNPREDICTABLE
  • D. ZCR_EL3.LEN=0xF requests 2048 bits, which is larger than the implementation supports, and an unsupported length faults
Answer

B. The SVE enable check tests CPTR_EL3.EZ regardless of the current EL, and the CPTR_EL3 reset value is UNKNOWN, so it may well be 0. EL3 must set EZ first, then ISB, then write ZCR_EL3. D is wrong because LEN is a request: the PE uses the largest supported length that does not exceed it, and setting the maximum is the recommended EL3 value.

Read more: Arm ARM (DDI0487), CPTR_EL3, ZCR_EL3, CheckSVEEnabled() pseudocode

Q15.3.2 TF-A is built with ENABLE_SVE_FOR_NS=1, ENABLE_SVE_FOR_SWD=0, CTX_INCLUDE_FPREGS=0. OP-TEE at S-EL1 uses Neon for a hash. A Normal-world SVE application occasionally sees corrupted vectors. Which change actually fixes the problem?

  • A. Set CTX_INCLUDE_FPREGS=1, so that EL3 saves V0–V31 around the Secure call
  • B. Set ENABLE_SVE_FOR_SWD=1, so that OP-TEE’s Neon instructions trap
  • C. Make Normal-world Z, P and FFR state survive at full width: either OP-TEE saves and restores it before using Neon (it then needs SVE access, ENABLE_SVE_FOR_SWD=1), or EL3 does (CTX_INCLUDE_SVE_REGS=1, which requires ENABLE_SVE_FOR_SWD=1)
  • D. Set SVE_VECTOR_LEN=128, so that Z and V are the same size
Answer

C. With SVE disabled for the Secure world, TF-A clears only EZ, and Advanced SIMD remains usable. Each Neon write zeroes the bits of the Normal world’s Z registers above bit 127. A saves only the 128-bit view, which is exactly the combination TF-A forbids without CTX_INCLUDE_SVE_REGS. B gets the effect backwards: enabling SVE for the Secure world removes a trap, it does not add one, and Neon is governed by TFP in any case. D hides the problem by throwing away the SVE width for everyone, at a large performance cost, so it is not a fix.

Read more: TF-A build options, ENABLE_SVE_FOR_SWD, CTX_INCLUDE_SVE_REGS; Arm ARM (DDI0487), SVE registers

Q15.3.3 A product enables SME for the Normal world only. A Normal-world thread in Streaming SVE mode, with ZA active, makes an FF-A call. The SPMC at S-EL2 uses Advanced SIMD vector instructions in its message-copy path. What happens first, and why?

  • A. The SPMC’s first Advanced SIMD vector instruction traps with EC 0x1D, SMTC 1, unless FA64 is enabled, because PSTATE.SM is still 1 after the exception and the world switch
  • B. Nothing goes wrong, because EL3 clears PSTATE.SM and PSTATE.ZA on every world switch
  • C. The SPMC’s instruction traps with EC 0x1D, SMTC 0, because ESM=0 for the Secure world, and that check takes priority over everything
  • D. The SMC itself is rejected, because SMC is illegal in Streaming SVE mode
Answer

A. Exception entry, ERET and EL3’s world switch leave SVCR unchanged, and TF-A does not save or restore it. The Secure world therefore runs in streaming mode, where most Advanced SIMD vector instructions are illegal. C is tempting, but ESM is checked only for SVE and SME instructions and SME register accesses. The pseudocode checks an Advanced SIMD instruction against the FP controls (FPEN/TFP, which allow it here) and then for legality in Streaming SVE mode, and that second check raises SMTC 1 at the current EL. A Secure-world SVE instruction would instead trap to EL3 with SMTC 0. Either way, the Secure-world code must handle entry with SM=1. B is false for TF-A. D is false: SMC is legal in streaming mode.

Read more: Arm ARM (DDI0487), SVCR, Streaming SVE mode instruction legality; TF-A simd_ctx.h