Expand description
Retpoline / Spectre Mitigations
Provides software and hardware mitigations for Spectre-class speculative execution vulnerabilities.
§Spectre v1 (Bounds Check Bypass)
bounds_mask: branchless index clamping that produces a safe index even under mis-speculation.speculation_barrier: serialising instruction that halts speculative execution past this point.
§Spectre v2 (Branch Target Injection)
- IBRS (Indirect Branch Restricted Speculation): restricts speculative targets of indirect branches to a curated set.
- IBPB (Indirect Branch Prediction Barrier): flushes the Branch Target Buffer on context switches.
- STIBP (Single Thread Indirect Branch Predictors): prevents cross-SMT branch-target poisoning.
- Retpoline: compiler-based mitigation that replaces indirect calls with a construct that never speculatively follows the real target.
§Architecture Support
- x86_64: IBRS/IBPB/STIBP via IA32_SPEC_CTRL (MSR 0x48) and IA32_PRED_CMD (MSR 0x49). Feature detection through CPUID leaf 7.
- AArch64: CSV2 (Cache Speculation Variant 2) detection via ID_AA64PFR0_EL1. Barriers via DSB SY + ISB.
- RISC-V: FENCE.I as speculation barrier.
Constants§
- RETPOLINE_
ENABLED - Whether retpoline (compiler-level Spectre v2 mitigation) is active.
Functions§
- bounds_
mask - Branchless bounds mask for Spectre v1 mitigation.
- enable_
ibrs - Enable IBRS (Indirect Branch Restricted Speculation).
- enable_
stibp - Enable STIBP (Single Thread Indirect Branch Predictors).
- flush_
btb - Flush the Branch Target Buffer (issue IBPB).
- has_
ibpb - Returns
trueif IBPB is supported by the CPU. - has_
ibrs - Returns
trueif IBRS is supported by the CPU. - has_
stibp - Returns
trueif STIBP is supported by the CPU. - init
- Detect and enable Spectre mitigations.
- is_
hw_ mitigated - Returns
trueif the CPU has hardware-level Spectre mitigations. - is_
ibrs_ enabled - Returns
trueif IBRS is currently enabled. - safe_
index - Safe array index that clamps to zero under mis-speculation.
- speculation_
barrier - Insert a full speculation barrier.