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

17.2 Initialising EL3

EL3 is the root of every trap and routing decision in the system. Four registers set its own behaviour and the default treatment of everything below it: SCTLR_EL3, SCR_EL3, CPTR_EL3 and MDCR_EL3. This section explains what each one decides, the principle for choosing the initial values, where the MMU fits, and how EL3 builds the state for the first exception return to a lower EL. Per-feature enable bits are collected in 17.3, and the full handover contract is in 12.2. For the map of all trap registers across ELs, see 2.6.

The safe-defaults principle

Three rules cover almost every decision in this section:

  1. Write every field. Most of these registers reset to UNKNOWN values (17.1). The first write is a whole-register write from a constant built from named fields, with RES1 bits set and RES0 bits clear. Read-modify-write is allowed only after that.
  2. Deny by default, enable by evidence. Lower ELs start with every optional feature trapped or disabled. A feature is enabled only when the ID registers say it is implemented and the firmware is built to manage its state (17.3). Enabling something that firmware does not context-switch leaks state between worlds.
  3. Separate EL3’s own settings from per-world settings. Some bits configure EL3 itself and never change, such as SCTLR_EL3, SCR_EL3.EA while in EL3, and MDCR_EL3.SDD. Others describe the world EL3 is about to enter (SCR_EL3.NS, SCR_EL3.RW, most feature enables), and are rebuilt for every exception return. TF-A keeps the second kind in each world’s cpu_context_t and writes them in el3_exit.

SCTLR_EL3

SCTLR_EL3 controls only the EL3 translation regime. It does not affect lower ELs.

FieldBoot valueWhy
M, C0 until the EL3 tables exist, then 19.3
I1 earlyInstruction fetches with M=0 are to Normal memory; I=1 makes them cacheable, which speeds up ROM code considerably
A, SA1 (TF-A)Alignment and SP-alignment checks catch pointer bugs. Note that with M=0, data accesses are Device and unaligned accesses fault regardless of A
WXN0 early, optionally 1 with the MMU onForces writable memory to be execute-never
EEExplicitly 0 (little-endian)Its reset value is IMPLEMENTATION DEFINED
IESB1 when FEAT_IESB is usedImplicit error synchronization at exception entry and return (3.4); also required by errata workarounds on some Neoverse and Cortex-X cores
DSSBS0 in TF-AValue of PSTATE.SSBS on exception entry to EL3 (14.5)
EnIA, BTAs built (BRANCH_PROTECTION)PAuth and BTI for EL3 code itself (13.1)

The SCTLR_EL3 RES1 bits are 4, 5, 11, 16, 18, 22, 23, 28 and 29, which is why a “minimal” value looks like 0x30C50830 rather than 0. Writing 0 is not “everything off”: software must write RES1 bits as 1, and a value with them clear is not guaranteed to behave the same way on every implementation, or on the next architecture version, where a RES1 bit may become a real control.

SCR_EL3

SCR_EL3 is the register that defines a world. Its fields fall into four groups:

GroupFieldsTypical value
Security state and execution state of lower ELsNS, NSE (FEAT_RME), RW, EEL2 (FEAT_SEL2)Per world: NS=1, RW=1 for an AArch64 Normal world; NSE,NS = 1,1 for Realm
Routing to EL3 (3.3)IRQ, FIQ, EA, NMEA, EASEFIQ=1 when Group 0 or Secure interrupts must reach EL3 from a lower world; EA=1 for firmware-first RAS
Instruction controlsHCE (HVC enable), SMD (SMC disable), SIF (no Secure instruction fetch from Non-secure memory), TWE/TWI, ST (Secure EL1 access to CNTPS_*)HCE=1 for any world that has EL2 software; SMD=0; SIF=1
Feature enables and trapsHXEn, FGTEn, ECVEn, ATA, API/APK, TCR2En, …Only for implemented and managed features (17.3)

Points that the field names do not make obvious:

  • RW describes the Exception level immediately below EL3 in the target world (EL2 if it is enabled, otherwise EL1). HCR_EL2.RW then decides EL1. RW is writable only when AArch32 is supported at a lower EL; on a core with no AArch32 below EL3 it is RAO/WI, so a wrong value is silently corrected. Where AArch32 is supported at EL2, RW=0 with an AArch64 SPSR_EL3.M gives an illegal exception return at the first ERET (Q17.2.3).
  • SCR_EL3.IRQ/FIQ route physical interrupts when the PE is in a lower EL. At EL3 itself, an interrupt is taken only if it is routed to EL3 and unmasked in PSTATE. TF-A normally runs EL3 with interrupts masked, so these bits are part of the per-world routing model (get_scr_el3_from_routing_model()).
  • EEL2 enables Secure EL2. Changing it at run time interacts badly with some cores: TF-A notes errata (Neoverse V2 3099206, Neoverse N2 3099213) that require EEL2 to stay constant for the life of the core, and sets it once during el3_arch_init_common.
  • TID3/TID5 (with FEAT_IDTE3) trap ID-register reads to EL3, so that firmware can hide features from lower worlds.

CPTR_EL3

CPTR_EL3 controls access from all lower ELs, and from EL3 itself, to FP/SIMD, SVE, SME, the trace system registers and the Activity Monitors. The polarities are mixed, and mixed polarity is a common source of bugs:

Field1 meansBoot default
TCPAC (bit 31)Trap EL2 accesses to CPACR_EL1/CPTR_EL2 to EL30
TAM (bit 30)Trap Activity Monitor accesses1 until AMU support is enabled (16.4)
TTA (bit 20)Trap trace system register accesses1 unless trace is managed
ESM (bit 12)Enable SME (do not trap)0 unless SME is managed (15.3)
TFP (bit 10)Trap FP/SIMD0 for worlds that use FP
EZ (bit 8)Enable SVE (do not trap)0 unless SVE is managed

All fields are UNKNOWN at reset. In TF-A, CPTR_EL3 is a per-world value (per_world_context_t.ctx_cptr_el3) shared by all CPUs, because whether a world may use SVE or SME is a property of the world, not of a core.

MDCR_EL3

MDCR_EL3 decides who can debug, count and trace what. Its security-relevant defaults are prohibitions:

  • SDD=1 disables Secure self-hosted debug exceptions, apart from BRK (16.1).
  • SPME=0, together with SCCD (FEAT_PMUv3p5) and MCCD/MPMX (FEAT_PMUv3p7) where implemented, keeps the PMU from counting Secure and EL3 execution (16.3). TF-A additionally sets PMCR_EL0.DP, so that the cycle counter stops when event counting is prohibited, which covers PMUs where SCCD/MCCD don’t exist.
  • TDA, TDOSA and TPM trap debug, OS-lock/power-down and PMU register accesses to EL3. They are normally 0 for the Normal world, so that the kernel can use them.
  • NSPB, NSTB and SBRBE assign the SPE profiling buffer, the trace buffer and branch recording to a Security state (16.6).
  • EPMAD/EDAD disable access by a (Non-secure) external debugger to the PMU registers and to the breakpoint and watchpoint registers (16.2).

TF-A writes MDCR_EL3_RESET_VAL (which sets only MTPME) and then composes per-world values in each context. The Normal-world value grants SPE ownership, for example, while the Secure-world value removes access. A Secure world with Non-secure-owned SPE therefore cannot read the buffer registers, and its own execution is not profiled.

The rest of the early EL3 list

  • VBAR_EL3 before anything that can fault (17.1).
  • CNTFRQ_EL0. It is writable only at the highest implemented EL, so EL3 must program it on every core. The system counter itself (CNTCR.EN) is a memory-mapped, system-level control (4.6).
  • ICC_SRE_EL3: SRE=1 and Enable=1, so that EL3 can use the GIC system registers and lower ELs can enable them (4.5).
  • ACTLR_EL3 (IMPLEMENTATION DEFINED). On Arm cores it controls whether EL2 and EL1 may write the IMPLEMENTATION DEFINED ACTLR_EL2/ACTLR_EL1 and CPU*CTLR registers. Grant only what the kernel needs (usually nothing).
  • CPU-specific registers and errata workarounds, applied by the reset handler on every reset (1.3).
  • PSTATE.DIT at EL3 when FEAT_DIT is implemented, so that EL3’s crypto code runs with data-independent timing.

The early MMU

BL1 and BL31 turn on the EL3 MMU and caches as soon as they have a minimal memory map: ROM and SRAM as Normal, device regions as Device-nGnRnE, with execute-never everywhere except code. There are three reasons:

  • With M=0, data accesses are Device-nGnRnE, so C code that the compiler expects to do unaligned accesses or DC ZVA faults.
  • Image hashing during authentication is several times faster with the D-cache on.
  • Speculative accesses are confined to mapped regions only once the MMU is on, provided the map does not cover PA holes (14.2).

The sequence (invalidate TLBs, write MAIR_EL3, TCR_EL3 and TTBR0_EL3, DSB, ISB, then set SCTLR_EL3.{M,C}, ISB) and its failure modes are covered once, in 9.3.

Dropping to a lower EL

The only way down is an exception return. EL3 prepares the target state and executes ERET, which is a context synchronization event, so the system-register writes made before it are in effect for the code it returns to (7.8).

What EL3 must write for the target world:

  1. SCR_EL3 for that world, from the per-world context.
  2. SPSR_EL3: M[3:0] for the target EL and stack (EL2h = 0b1001, EL1h = 0b0101), M[4]=0 for AArch64, and DAIF all masked.
  3. ELR_EL3: the entry point.
  4. The target EL’s SCTLR_ELx, written to a clean MMU-off value. If EL2 is implemented but the world’s software starts at EL1, EL3 also writes the EL2 controls that still apply to EL1&0 (HCR_EL2, CNTHCTL_EL2, CNTVOFF_EL2, VTTBR_EL2, MDCR_EL2, CPTR_EL2, HCRX_EL2). See 12.2.
  5. The general-purpose registers the next stage expects as arguments. Everything else should be zeroed, so that no EL3 data leaks into the lower world.

TF-A splits this into cm_init_my_context() (build a cpu_context_t from an entry_point_info_t: SCR_EL3, SPSR_EL3, ELR_EL3, per-feature enables), cm_prepare_el3_exit() (write the EL2/EL1 system registers of that world) and el3_exit (restore SCR_EL3, MDCR_EL3, CPTR_EL3 and the GPRs from the context, then ERET). The same path runs for the first entry and for every later return from an SMC, so the boot-time values are the runtime values.

/* Normal-world SCR_EL3, as TF-A composes it (simplified) */
u_register_t scr = SCR_RES1_BITS;            /* bits 5:4 */
scr |= SCR_NS_BIT | SCR_RW_BIT;              /* Non-secure, lower EL is AArch64 */
scr |= SCR_HCE_BIT;                          /* BL33 runs at EL2 and may use HVC */
scr |= SCR_SIF_BIT;                          /* written at EL3 entry; kept */
scr |= get_scr_el3_from_routing_model(NON_SECURE); /* IRQ/FIQ/EA routing */
if (is_feat_hcx_supported())  scr |= SCR_HXEn_BIT;
if (is_feat_fgt_supported())  scr |= SCR_FGTEN_BIT;
/* ... one line per managed feature, see 17.3 ... */
write_ctx_reg(get_el3state_ctx(ctx), CTX_SCR_EL3, scr);

Bring-up note: if the core vanishes at the first ERET, decode the outcome from EL3’s point of view. An illegal exception return (SPSR_EL3.M naming an EL that is not implemented or not enabled, or SCR_EL3.RW disagreeing with M[4]) leaves the PE at EL3 with PSTATE.IL set. The instruction at ELR_EL3 then takes an Illegal Execution state exception (EC 0x0E) to EL3 itself, so it shows up in your vector with ELR_EL3 equal to the intended entry point (3.2). A clean return followed by a trap back into EL3 with EC 0x18 means a lower-EL register access hit an enable you forgot (17.3, 18.1).

Checkpoint

Q17.2.1 Firmware initialises CPTR_EL3 with msr cptr_el3, xzr on a core that implements SVE, SME and the AMU, and then lets the Normal world run Linux. What happens?

  • A. Nothing is trapped, because writing 0 clears every trap bit
  • B. FP/SIMD, trace and AMU accesses are not trapped (TFP=TTA=TAM=0), but SVE and SME are disabled (EZ=ESM=0), so their instructions and registers trap to EL3. The AMU is usable by the Normal world with no context management
  • C. All of FP, SVE, SME and AMU trap, because CPTR_EL3 bits are all enable bits
  • D. The write is CONSTRAINED UNPREDICTABLE, because CPTR_EL3 has RES1 bits
Answer

B. CPTR_EL3 mixes trap bits (TCPAC, TAM, TTA, TFP: 1 traps) and enable bits (EZ, ESM: 1 enables). Zero therefore enables FP, trace and AMU access and disables SVE and SME. A and C each assume a single polarity. Linux would see SVE in ID_AA64PFR0_EL1, and its first SVE access would trap to EL3 (EC 0x19). Leaving TAM=0 without AMU context handling is itself a bug (17.3).

Read more: Arm ARM (DDI0487), CPTR_EL3

Q17.2.2 Why does TF-A keep SCR_EL3 in each world’s cpu_context_t, instead of setting it once during boot?

  • A. SCR_EL3 is banked per Security state in hardware, so software must keep shadow copies
  • B. SCR_EL3 loses its value whenever the core executes WFI
  • C. The Arm ARM requires SCR_EL3 to be written before every ERET
  • D. Several fields describe the world being entered (NS, NSE, RW, interrupt routing, the feature enables that are allowed in that world), so they must change on every world switch. Keeping one value per world makes the boot-time handover and each later SMC return use the same path
Answer

D. There is one SCR_EL3, not a banked copy per state (A). It is not reset by WFI (B), though it is lost on power-down, which is another reason to keep it in memory. The architecture does not require a write per ERET (C). Firmware does that because the security state and permitted features depend on the destination world.

Read more: Arm ARM (DDI0487), SCR_EL3; TF-A firmware design, “CPU context management”

Q17.2.3 BL31 ERETs to BL33 with SPSR_EL3.M = 0b1001 (EL2h) on an Armv8-A core that implements EL2 and supports AArch32 at EL2 and EL1, but the Normal-world SCR_EL3 was built with NS=1, HCE=1 and, by mistake, RW=0. What does the hardware do?

  • A. It enters EL1 instead, because EL2 is not allowed in AArch32
  • B. It enters EL2 in AArch32 state, and the first A64 instruction is UNDEFINED
  • C. The ERET is an illegal exception return: SPSR_EL3.M[4]=0 (AArch64) conflicts with SCR_EL3.RW=0. The PE stays at EL3 with PSTATE.IL set, and the next instruction takes an Illegal Execution state exception
  • D. It enters AArch64 EL2 correctly, because RW only affects EL1
Answer

C. An exception return that targets an AArch64 EL whose execution state is configured as AArch32 is illegal. On an illegal return, PSTATE.EL and the execution state are left unchanged and PSTATE.IL is set, so the instruction at the ELR_EL3 target, executed at EL3, takes an Illegal Execution state exception (EC 0x0E). D is wrong because SCR_EL3.RW controls the EL below EL3, which is EL2 here. (On a core with no AArch32 at EL2 or EL1, RW would be RAO/WI and the return would succeed, which is why the question specifies AArch32 support.) No silent redirection to EL1 (A) or AArch32 (B) happens.

Read more: Arm ARM (DDI0487), Illegal return events from AArch64 state; SCR_EL3.RW