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

7.8 Context synchronization and ISB

DMB and DSB concern memory accesses. Context synchronization concerns context: system registers, PSTATE, and the translation and cache state that the PE uses to fetch and execute instructions. These are different axes. An ISB neither orders memory accesses (beyond the control+ISB idiom of 7.4) nor waits for anything to complete, and a DSB never makes a system-register write take effect. Most “barrier” bugs in early boot code mix up these two axes.

What counts as a context synchronization event

The Arm ARM defines a context synchronization event (CSE) as one of:

EventCondition
Executing an ISBalways
Taking an exceptionunless FEAT_ExS is implemented and SCTLR_ELx.EIS == 0, where ELx is the target Exception level
Returning from an exception (ERET, ERETAA/ERETAB)unless FEAT_ExS is implemented and SCTLR_ELx.EOS == 0, where ELx is the level the return is executed from
Exit from Debug statealways
DCPS<n> or DRPS executed in Debug statealways

Things that are not CSEs: DSB, DMB, branches (including BR to a freshly written address), WFI/WFE wake-up, and MSR itself.

FEAT_ExS (optional from v8.5, reported by ID_AA64MMFR0_EL1.EXS) adds SCTLR_ELx.EIS (bit 22) and SCTLR_ELx.EOS (bit 11). Without FEAT_ExS those bits are RES1, which is why the v8.0 RES1 masks for SCTLR_ELx include bits 22 and 11. With EIS/EOS cleared, exception entry or return no longer serialises the pipeline. That is cheaper for a hypervisor or EL3 that takes very frequent exceptions. The cost is that the handler must issue its own ISB wherever it depends on context written before the boundary. Formal studies of exception ordering (for example arXiv 2412.15140) treat the EIS/EOS = 0 configuration as rare and hard to program correctly. On a FEAT_ExS implementation the reset value of these bits is architecturally UNKNOWN, so boot code must set them explicitly.

Bring-up note: TF-A writes SCTLR_EL3 from a value that includes the architectural RES1 bits, so EIS = EOS = 1 by construction. If a firmware port computes SCTLR_EL3 from scratch (mov x0, #(SCTLR_M | SCTLR_C | ...)), it clears bits 22 and 11. On a FEAT_ExS core this silently makes every SMC entry and every ERET non-synchronizing. The resulting bugs are timing-dependent and appear only on that core.

What a CSE guarantees

For a context-changing operation (a direct write to a system register, a PSTATE change, a completed TLB or instruction cache invalidation) the rule is:

  • Every instruction that appears in program order after the CSE is fetched, decoded and executed using the new context. None of them can have been observably fetched or executed early under the old one.
  • Instructions between the context-changing operation and the CSE may observe either the old or the new context. Which one they see is not defined.
  • The CSE does not wait for memory accesses or maintenance to complete. A TLBI or IC IVAU becomes a context change only once a DSB has completed it. The ISB then guarantees that later instructions see the result.
  • The guarantee is local to the PE that executes the CSE. Another PE needs its own CSE (see the cross-modifying-code rules in 8.6).

The second point explains why enabling the MMU needs an identity map. Between MSR SCTLR_EL3, x0 and the following ISB, instructions may be fetched with or without translation. Both views must therefore give the same address (9.3).

Direct and indirect reads and writes

The Arm ARM’s “Synchronization requirements for AArch64 System registers” are written in terms of four kinds of access:

  • Direct read / direct write: MRS / MSR (and SYS/SYSL forms) naming the register.
  • Indirect read: the hardware uses the register’s value as a side effect of executing some other instruction. Examples: the translation walk reading TTBR0_EL1 and TCR_EL1, the trap check for an FP instruction reading CPACR_EL1.FPEN, exception entry reading VBAR_EL3, and an ERET reading SPSR_EL3/ELR_EL3 (a special case: a direct write to these is visible to a later ERET without a CSE).
  • Indirect write: the hardware updates the register as a side effect. Examples: exception entry writing ESR_ELx/FAR_ELx/ELR_ELx, PMU counters counting, the counter CNTVCT_EL0 advancing, and acknowledging an interrupt changing the GIC running priority.

The rules that matter:

  1. A direct read after a direct write to the same register, in program order, returns the written value. No synchronization is needed.
  2. Direct writes to the same register take effect in program order.
  3. An indirect read after a direct write needs a CSE between them. This is the common case. Writing CPACR_EL1.FPEN and then executing FMOV reads FPEN indirectly, so an ISB is required.
  4. A direct read is not ordered against the instruction stream around it without a CSE. That is why reading CNTVCT_EL0 or a PMU counter at a precise point needs an ISB before the MRS (4.7, 16.5).
  5. Memory-mapped registers (GIC Distributor/Redistributor, CNTControlBase, SMMU, UART) are memory. They follow 7.9, and a CSE does nothing for them.

Some registers have extra rules stated in their own descriptions. Writes to PSTATE fields with MSR (immediate) (PAN, UAO, SSBS, DIT, TCO and the DAIF set/clear forms) are specified to affect subsequent instructions. That is why Linux toggles PAN without an ISB. When unmasking an interrupt with MSR DAIFClr, an ISB is what bounds the point by which a pending interrupt is taken. Check each register’s “Synchronization” or “Accessing” text in DDI0487 rather than generalising.

Which changes need one

ChangeMinimal sequenceReason
SCTLR_ELx.M, .C, .IMSR; ISBfetch and translation read these indirectly
TTBRn_ELx, TCR_ELx, MAIR_ELx, TCR2_ELx, PIR_ELxMSR…; ISB (plus TLB maintenance per 9.1)walks are indirect reads
TLBIDSB ISH (completion); ISBthe DSB completes the invalidate; the ISB stops later instructions from having been fetched or translated with stale entries
new code in memoryDC CVAU; DSB ISH; IC IVAU; DSB ISH; ISB8.6
CPACR_EL1, CPTR_EL2, CPTR_EL3 (FP/SVE/SME enables)MSR; ISB before the first FP/SIMD/SVE instructiontrap checks are indirect reads
ZCR_ELx.LEN, SMCR_ELxMSR; ISBvector length is read indirectly
VBAR_ELxMSR; ISB before any exception that must use the new tableexception entry reads it indirectly
SCR_EL3, HCR_EL2 routing and trap bitsMSR; ISB if they affect the current level; ERET suffices for the lower levelindirect reads at exception generation
ICC_SRE_ELxMSR; ISB before any other ICC_* accessrequired by the GICv3 spec (4.5)
ICC_SGI1R_EL1 (sending an SGI)DSB ISHST before, ISB after (Linux practice)data visible before the IPI; force the write to issue
PMCR_EL0, PMCNTENSET_EL0 around a measured regionMSR; ISBprevents measured instructions from executing before the counters are enabled
MDSCR_EL1, breakpoint/watchpoint registersMSR; ISBdebug events are generated by indirect reads

The world-switch case deserves a precise statement. In an EL3 exit path, restoring SCTLR_EL1, TTBR0_EL1, SCR_EL3.NS and so on, followed by ERET, needs no ISB: the ERET is the CSE, and nothing at EL3 depends on those values. Only a register that affects EL3’s own execution before the ERET needs an explicit ISB. Examples are CPTR_EL3 before EL3 saves FP state, or SCR_EL3.EA before EL3 unmasks SError.

// Enabling FP/SIMD at EL3, then saving the lower-EL FP context
    mrs   x0, cptr_el3
    bic   x0, x0, #(1 << 10)        // CPTR_EL3.TFP = 0
    msr   cptr_el3, x0
    isb                             // trap check is an indirect read
    stp   q0, q1, [x1], #32         // without the ISB this may trap (EC 0x07)

Validation note: Missing-ISB bugs hide very well on models. QEMU TCG typically ends a translation block after a system-register write with side effects. The FVP executes instructions in order and with no visible speculation. Either way, the “new context” is effectively visible immediately. On silicon the same code may run with the old context for some instructions. Symptoms include an FP trap with ESR_EL3.EC = 0x07 straight after enabling FP, exceptions vectoring to the old VBAR, or a translation fault whose FAR points into the stale map. A directed test cannot prove the ISB is needed, because either outcome is architecturally allowed. Treat this as a static review rule instead: for every MSR to a register in the table, find the CSE before the first dependent instruction. A Tarmac trace from the FVP still helps confirm that the ISB is present on the path that actually executes.

Security note: Exception return to a lower EL is the CSE that makes SCR_EL3/HCR_EL2 trap configuration and the lower EL’s translation registers take effect. If an EL3 handler changes those values and then returns along a path where EOS = 0 on a FEAT_ExS core, the lower EL may run some instructions under the previous world’s context. Keep EOS = 1 at EL3.

Checkpoint

Q7.8.1 EL1 code updates TTBR0_EL1 to a new table, issues TLBI ASIDE1IS, and then executes DSB ISH. Which statement is correct?

  • A. The sequence is complete. DSB is a context synchronization event for TLB maintenance
  • B. An ISB is still required before relying on the new translation for instructions on this PE, because DSB completes the invalidation but is not a CSE
  • C. An ISB is required instead of the DSB, because ISB waits for the TLBI to complete
  • D. No barrier is needed, because the MSR TTBR0_EL1 is self-synchronizing
Answer

B. The two barriers do different jobs. DSB makes the TLBI complete (completion axis), and ISB makes later instructions fetch and translate using the new context (context axis). A is the common misconception. C reverses the roles, because an ISB waits for nothing.

Read more: Arm ARM (DDI0487), TLB maintenance requirements and the context synchronization event; Synchronization requirements for AArch64 System registers

Q7.8.2 EL3 restores a Non-secure EL1 context (SCTLR_EL1, TCR_EL1, TTBR0_EL1, VBAR_EL1), sets SCR_EL3.NS, and executes ERET. FEAT_ExS is not implemented. Where is an ISB needed?

  • A. After every MSR, because each system-register write must be synchronised individually
  • B. Immediately before the ERET, because ERET reads SPSR_EL3 indirectly
  • C. After the ERET, at the first instruction of EL1
  • D. Nowhere for these registers. The ERET is itself a context synchronization event, and nothing at EL3 uses the restored values before it
Answer

D. Exception return is a CSE whenever FEAT_ExS is absent, or present with EOS = 1. The lower-EL registers are only read indirectly after the return. B is tempting, but direct writes to SPSR_EL3/ELR_EL3 are consumed by the ERET itself with no CSE required. With FEAT_ExS and SCTLR_EL3.EOS = 0 the answer would change.

Read more: Arm ARM (DDI0487), Context synchronization event; SCTLR_EL3 (EOS, EIS)

Q7.8.3 After writing the GIC Distributor’s GICD_CTLR (memory-mapped), firmware executes ISB and immediately enables interrupts, expecting the Distributor to be configured. Why is this reasoning wrong?

  • A. GICD_CTLR is a memory-mapped register, so its write is a Device memory access. It needs ordering or completion (DSB, and the RWP poll the GIC defines), and a CSE does not provide that
  • B. ISB is correct, but must come before the write
  • C. The ISB is right, but DMB ISH is also needed because GIC registers are Inner Shareable
  • D. It is correct. Every register that configures the interrupt controller is synchronised by ISB
Answer

A. A CSE applies to system registers and PSTATE. Memory-mapped registers follow Device memory ordering (7.9), and the GIC adds its own completion indicator (GICD_CTLR.RWP). C is wrong twice: Device memory is treated as Outer Shareable, and a DMB would only order the write, not complete it.

Read more: Arm ARM (DDI0487), Context synchronization event; GICv3 (IHI0069), GICD_CTLR.RWP