14.4 Spectre v2 and BHB
Spectre v2 (CVE-2017-5715, branch target injection) and Spectre-BHB (CVE-2022-23960, branch history injection) both make a victim’s indirect branch speculatively jump to a gadget the attacker chose. The difference is which predictor state the attacker controls:
- v2 poisons the target stored in the BTB or the indirect predictor. Code in one context trains an entry that aliases a victim branch in another context.
- BHB poisons the history that selects among targets. The targets the victim branch uses may all have been trained by the victim itself. The attacker shapes the global branch history just before the context switch, so the victim’s lookup picks a victim-trained target that happens to be a useful gadget.
That difference explains why CSV2, the architectural fix for v2, did not fix BHB, and why the BHB mitigations live at exception entry, where history crosses from one context to the next.
CSV2: the architectural fix for v2
ID_AA64PFR0_EL1.CSV2, bits [59:56], advertises restrictions on branch-target speculation between hardware-described contexts (Security state, EL, VMID, ASID, and with CSV2_2 also SCXTNUM_ELx; see 14.1).
CSV2 | Feature | Guarantee |
|---|---|---|
0b0000 | — | No statement. Assume cross-context target injection is possible, and check the core’s advisory |
0b0001 | FEAT_CSV2 | Branch targets trained in one hardware-described context cannot exploitatively control speculative execution in another. SCXTNUM_ELx is not part of the context. ID_AA64PFR1_EL1.CSV2_frac (bits [35:32]) refines this value: 0b0001 is CSV2_1p1 and 0b0010 is CSV2_1p2, which differ in whether SCXTNUM_ELx is accessible at all (14.7) |
0b0010 | FEAT_CSV2_2 | As above, with SCXTNUM_ELx implemented and part of the context |
0b0011 | FEAT_CSV2_3 | As CSV2_2, plus the same restriction for branch history: history created in one context cannot exploitatively control indirect branches in another. A CSV2_3 core is not affected by Spectre-BHB |
FEAT_CSV2 is permitted from v8.0 and mandatory from v8.5. For CSV2_2 and CSV2_3, see DDI0487 A2. Several earlier cores implement CSV2 from a particular revision onwards. Cortex-A72 reports CSV2 from r1p0, for example, and TF-A’s check_erratum_custom for CVE-2017-5715 on A72 reads the field at runtime instead of assuming.
Security note: “Exploitatively control” is the precise wording, and it is narrower than “isolate”. Under CSV2 a cross-context BTB hit can still happen. What is restricted is a cross-context entry steering the victim to an attacker-useful target. CSV2 also says nothing about timing side effects of predictor sharing, and it does not separate code that shares one hardware context.
SMCCC_ARCH_WORKAROUND_1: firmware help on pre-CSV2 cores
On a core without CSV2, a lower EL can train BTB entries that EL3 will use. A higher-privileged OS or hypervisor can also be attacked by its own lower EL. The architecture has no instruction that invalidates the branch predictor in AArch64, so the fix is core-specific and lives in EL3:
| Core | What TF-A does on entry from a lower EL | Vector table |
|---|---|---|
| Cortex-A57, Cortex-A72 (before CSV2) | Toggles SCTLR_EL3.M off and on. On these cores this invalidates the branch predictor, which is core-specific behaviour, not architectural | wa_cve_2017_5715_mmu_vbar |
| Cortex-A73, Cortex-A75 | Drops into AArch32 S-EL1 to execute the AArch32 BPIALL, then returns by SMC | wa_cve_2017_5715_bpiall_vbar |
The SMCCC call SMCCC_ARCH_WORKAROUND_1 (0x80008000, SMCCC v1.1 or later) lets a lower EL ask for the same invalidation at points where it is the victim. Examples are Linux on a switch between user processes, or KVM on guest exit. Discovery is by SMCCC_ARCH_FEATURES (0x80000001) with w1 = 0x80008000:
| Return value | Meaning |
|---|---|
| 0 | Implemented, and this PE needs it |
| 1 | Implemented, but this PE does not need it (Linux: SMCCC_ARCH_WORKAROUND_RET_UNAFFECTED) |
−1 (NOT_SUPPORTED) | Not implemented. The caller must assume it is vulnerable unless it can prove otherwise from CSV2 |
The answer is per PE. On a big.LITTLE system, each core type must be asked separately, and the OS must treat the system as affected if any core is.
BHB: the mitigation ladder
Spectre-BHB affects many cores that do have CSV2: Cortex-A57/A72 through A78, X1/X2, A710, Neoverse N1/N2/V1 and later parts (see Arm’s Spectre-BHB advisory for the authoritative list). The mitigation must run on exception entry from a less-privileged EL, before the first indirect branch in the more-privileged code. Choose the first mechanism the core supports, in this order. Linux’s spectre_bhb_enable_mitigation() in arch/arm64/kernel/proton-pack.c uses the same order.
| # | Mechanism | Discovery | Cost at entry |
|---|---|---|---|
| 0 | CSV2_3: not affected | ID_AA64PFR0_EL1.CSV2 = 0b0011 | None |
| 1 | FEAT_ECBHB: an exception to a higher EL prevents earlier branch history from exploitatively controlling indirect branches after the exception. v8.9; see DDI0487 A2 | ID_AA64MMFR1_EL1.ECBHB, bits [63:60] | None: hardware |
| 2 | FEAT_CLRBHB: the CLRBHB instruction (HINT #22) clears history for the current context. Permitted from v8.0, mandatory from v8.9 | ID_AA64ISAR2_EL1.CLRBHB, bits [31:28] | CLRBHB; ISB |
| 3 | Branch loop: execute k taken branches to overwrite the history, then a speculation barrier | MIDR lookup: the value of k is per core and comes from Arm’s advisory | k taken branches plus SB or DSB; ISB |
| 4 | SMCCC_ARCH_WORKAROUND_3 (0x80003FFF): firmware does a core-specific predictor invalidation that also covers WA_1 | SMCCC_ARCH_FEATURES | One SMC round trip |
Because CLRBHB is in the hint space, it executes as a NOP on a core that does not implement it, so shared code can carry it without faulting (but also without protection). Linux still selects the CLRBHB vector slot only on CPUs that advertise ID_AA64ISAR2_EL1.CLRBHB, and patches the KVM hyp copy to NOPs when no CPU uses it. The loop and the SMC are only used when the instruction is absent.
The loop, as TF-A writes it (lib/cpus/aarch64/wa_cve_2022_23960_bhb.S):
.macro apply_cve_2022_23960_bhb_wa _bhb_loop_count
str x2, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
mov x2, \_bhb_loop_count
1: b 2f // a taken branch: shifts one entry into the BHB
2: subs x2, x2, #1
bne 1b // a second taken branch per iteration
speculation_barrier // SB, or DSB SY; ISB
ldr x2, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
.endm
Linux’s __mitigate_spectre_bhb_loop uses b . + 4 in the same place, and patches the immediate at boot to the largest k among the system’s cores.
| k | Cores (from TF-A *_BHB_LOOP_COUNT) |
|---|---|
| 8 | Cortex-A72 (Linux also uses 8 for Cortex-A57; TF-A has no A57 count and uses the WA_1 MMU-toggle vectors there instead) |
| 24 | Cortex-A76, A76AE, A77, Neoverse N1 |
| 32 | Cortex-A78, A78AE, A78C, X1, X2, A710, Neoverse N2, V1 |
| 38 | Cortex-A715 |
| 132 | Cortex-A720, X3, X4, Neoverse V2, V3 |
These are the values in the TF-A tree as of 2025. Take them from the current TF-A source and Arm’s advisory, not from this table. Linux 6.6’s list stops at 32, plus 11 for Ampere-1. Later kernels added the larger counts, so an old kernel on a newer core may be under-mitigated while reporting that it is mitigated. Cortex-A73 and A75 cannot be fixed with a loop, and they use WA_3.
Bring-up note: In a heterogeneous system, the mitigation is per core type, but the vector code at a given EL is usually shared. TF-A solves this per core: each CPU’s reset function installs its own
VBAR_EL3(for examplewa_cve_vbar_cortex_a710), with that core’s k built in. Linux patches a single loop count, the system-wide maximum. If you add a new core type to a TF-A port, check that itscpu_opsreset function runs on every core, including warm boot. A core that comes back from power-down through a path that skips the override runs with the generic, unmitigated vectors.
How TF-A wires it into the EL3 vectors
- Per-core vector tables.
wa_cve_2022_23960_bhb_vector_table _count, _cpu(inwa_cve_2022_23960_bhb_vector.S) builds a full 2KB table. The current-EL entries (0x000–0x3FF) just branch to the generic handlers. The lower-EL AArch64 and AArch32 entries (0x400–0x7FF) runapply_cve_2022_23960_bhb_waand then branch to the generic handler. Only lower-EL entries are mitigated, because those are the only ones where the history came from less-privileged code. - Installation. The core’s reset function, under
WORKAROUND_CVE_2022_23960(default 1), usesoverride_vector_tableor writesVBAR_EL3directly. On A57/A72 before CSV2, the WA_1 MMU-toggle table is installed instead, and it covers both. On A76, one table (cortex_a76_wa_cve_vbar) combines the BHB loop with the CVE-2018-3639 dynamic workaround (14.5). - The WA_1/WA_3 fast path. In
wa_cve_2017_5715_mmu.S, the synchronous lower-EL entry checksw0 == WA_1 || w0 == WA_3andESR_EL3 == SMC #0with aCMP/CCMP/CCMPchain, and returns withexception_returnimmediately. The predictor invalidation has already happened on vector entry, so there is nothing left to do. On cores without a fast path, the SMC reachesarm_arch_svc_smc_handler(), which returnsSMC_RET0, because “the workaround has already been applied on affected PEs during entry to EL3”. - Discovery.
arm_arch_svc_setup.canswersSMCCC_ARCH_FEATURESfor WA_1 and WA_3 from each core’scheck_erratum_*functions. WA_3 reports “not required” only if neither CVE-2022-23960 nor CVE-2017-5715 applies, because a caller may use WA_3 in place of WA_1.
The same pattern applies at EL2 and EL1: KVM and Linux install mitigated vector slots (EL1_VECTOR_BHB_LOOP, EL1_VECTOR_BHB_CLEAR_INSN, EL1_VECTOR_BHB_FW) and choose among them per CPU at boot.
Validation note: For each core type, check three things. (1) Discovery is consistent. Compare
ID_AA64PFR0_EL1.CSV2,ID_AA64MMFR1_EL1.ECBHBandID_AA64ISAR2_EL1.CLRBHBwith the core’s TRM, and compare theSMCCC_ARCH_FEATURESanswers for WA_1 and WA_3 with the advisory. A WA_3 answer of 1 on an affected core is a firmware bug. (2) The vector is installed. On every core, after cold boot and after a CPU_SUSPEND power-down resume, readVBAR_EL3with a debugger and compare it with the symbol of the per-core table. (3) The loop runs. CountBR_RETIRED(0x21) orBR_IMMED_RETIRED(0x0D) across an SMC from Non-secure EL1, with EL3 counting enabled (by defaultMDCR_EL3.SPME=0 prohibits counting at EL3, so the loop is invisible). It should rise by about 2·k relative to a build without the workaround. On Linux,/sys/devices/system/cpu/vulnerabilities/spectre_v2reports the BHB state the kernel believes it has, which is only as good as its MIDR tables.
Checkpoint
Q14.4.1 A core reports CSV2 = 0b0010, ECBHB = 0 and CLRBHB = 1. A developer argues that because the core has CSV2_2, no Spectre-BHB mitigation is needed on EL3 entry. Which response is correct?
- A. Correct. CSV2_2 covers all branch predictor state, including history.
- B. Incorrect. CSV2 and CSV2_2 restrict cross-context use of branch targets. Only CSV2_3 or ECBHB covers branch history. Put
CLRBHB; ISBon the lower-EL vector entries, before the first indirect branch. - C. Incorrect. The core needs the k-iteration loop, because
CLRBHBis only a hint and might be a NOP. - D. Incorrect. Lower ELs must call
SMCCC_ARCH_WORKAROUND_3before every SMC.
Answer
B. BHB attacks use victim-trained targets selected by attacker-shaped history, which is outside CSV2_2’s guarantee. With ID_AA64ISAR2_EL1.CLRBHB = 1, the instruction is implemented and is the preferred software mitigation, ahead of the loop and the firmware call. C misreads the hint encoding: being a hint means the instruction is a NOP only on cores that do not advertise it. D puts the burden on the attacker’s side of the boundary, which the victim cannot rely on.
Read more: Arm ARM (DDI0487), ID_AA64PFR0_EL1.CSV2, ID_AA64ISAR2_EL1.CLRBHB, CLRBHB; Arm Spectre-BHB security advisory
Q14.4.2 In TF-A’s BHB vector table, why do the current-EL entries (0x000–0x3FF) skip the loop while the lower-EL entries (0x400–0x7FF) run it?
- A. The current-EL entries are only 32 instructions long, so there is no room for the loop.
- B. EL3 never takes exceptions from EL3.
- C. The loop would corrupt
SP_EL0in the current-EL, SP_EL0 entries. - D. BHB injection needs the branch history to have been shaped by less-privileged code. On a current-EL exception, the history was produced by EL3 itself, so there is no attacker-controlled history to clear.
Answer
D. The threat is history crossing from a less-privileged context into a more-privileged one, and that crossing only happens on lower-EL entries. A is false because every entry has the same 32-instruction size. B is false because EL3 can take synchronous exceptions and SErrors while running.
Read more: TF-A source, lib/cpus/aarch64/wa_cve_2022_23960_bhb_vector.S; Arm Spectre-BHB security advisory
Q14.4.3 A DynamIQ cluster mixes Cortex-A55 and Cortex-A78 cores. Linux runs on all of them. The A55 cores are not affected by Spectre-BHB. Which statement about Linux’s loop mitigation is correct?
- A. Linux uses a single patched loop count, the maximum k across all affected cores (32 here). Only the A78 cores have the loop vectors installed. The A55 cores are not affected and keep the default vectors.
- B. Linux disables the mitigation, because the system is heterogeneous.
- C. Linux uses k = 8 on every core, because that is the smallest safe value.
- D. Linux asks firmware for
SMCCC_ARCH_WORKAROUND_3on every core, because loop counts cannot be mixed.
Answer
A. spectre_bhb_loop_affected() records the maximum k across the cores it has seen, and that value is patched into the shared loop. Each affected CPU then selects the loop vector with this_cpu_set_vectors(). Unaffected CPUs return early. C has the logic backwards: a loop count below a core’s k leaves that core’s history only partly overwritten. The firmware call is only chosen when no instruction or loop mitigation exists for the core.
Read more: Linux arch/arm64/kernel/proton-pack.c, spectre_bhb_loop_affected(), spectre_bhb_enable_mitigation()