Skip to content

Commit ec82b56

Browse files
Shanker Donthinenictmarinas
Shanker Donthineni
authored andcommitted
arm64: Implement branch predictor hardening for Falkor
Falkor is susceptible to branch predictor aliasing and can theoretically be attacked by malicious code. This patch implements a mitigation for these attacks, preventing any malicious entries from affecting other victim contexts. Signed-off-by: Shanker Donthineni <shankerd@codeaurora.org> [will: fix label name when !CONFIG_KVM and remove references to MIDR_FALKOR] Signed-off-by: Will Deacon <will.deacon@arm.com> Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent aa6acde commit ec82b56

File tree

6 files changed

+70
-3
lines changed

6 files changed

+70
-3
lines changed

arch/arm64/include/asm/cpucaps.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
#define ARM64_SVE 22
4444
#define ARM64_UNMAP_KERNEL_AT_EL0 23
4545
#define ARM64_HARDEN_BRANCH_PREDICTOR 24
46+
#define ARM64_HARDEN_BP_POST_GUEST_EXIT 25
4647

47-
#define ARM64_NCAPS 25
48+
#define ARM64_NCAPS 26
4849

4950
#endif /* __ASM_CPUCAPS_H */

arch/arm64/include/asm/kvm_asm.h

+2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ extern u32 __kvm_get_mdcr_el2(void);
6868

6969
extern u32 __init_stage2_translation(void);
7070

71+
extern void __qcom_hyp_sanitize_btac_predictors(void);
72+
7173
#endif
7274

7375
#endif /* __ARM_KVM_ASM_H__ */

arch/arm64/kernel/bpi.S

+8
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,11 @@ ENTRY(__psci_hyp_bp_inval_start)
7777
ldp x0, x1, [sp, #(16 * 8)]
7878
add sp, sp, #(8 * 18)
7979
ENTRY(__psci_hyp_bp_inval_end)
80+
81+
ENTRY(__qcom_hyp_sanitize_link_stack_start)
82+
stp x29, x30, [sp, #-16]!
83+
.rept 16
84+
bl . + 4
85+
.endr
86+
ldp x29, x30, [sp], #16
87+
ENTRY(__qcom_hyp_sanitize_link_stack_end)

arch/arm64/kernel/cpu_errata.c

+38-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ DEFINE_PER_CPU_READ_MOSTLY(struct bp_hardening_data, bp_hardening_data);
5454

5555
#ifdef CONFIG_KVM
5656
extern char __psci_hyp_bp_inval_start[], __psci_hyp_bp_inval_end[];
57+
extern char __qcom_hyp_sanitize_link_stack_start[];
58+
extern char __qcom_hyp_sanitize_link_stack_end[];
5759

5860
static void __copy_hyp_vect_bpi(int slot, const char *hyp_vecs_start,
5961
const char *hyp_vecs_end)
@@ -96,8 +98,10 @@ static void __install_bp_hardening_cb(bp_hardening_cb_t fn,
9698
spin_unlock(&bp_lock);
9799
}
98100
#else
99-
#define __psci_hyp_bp_inval_start NULL
100-
#define __psci_hyp_bp_inval_end NULL
101+
#define __psci_hyp_bp_inval_start NULL
102+
#define __psci_hyp_bp_inval_end NULL
103+
#define __qcom_hyp_sanitize_link_stack_start NULL
104+
#define __qcom_hyp_sanitize_link_stack_end NULL
101105

102106
static void __install_bp_hardening_cb(bp_hardening_cb_t fn,
103107
const char *hyp_vecs_start,
@@ -138,6 +142,29 @@ static int enable_psci_bp_hardening(void *data)
138142

139143
return 0;
140144
}
145+
146+
static void qcom_link_stack_sanitization(void)
147+
{
148+
u64 tmp;
149+
150+
asm volatile("mov %0, x30 \n"
151+
".rept 16 \n"
152+
"bl . + 4 \n"
153+
".endr \n"
154+
"mov x30, %0 \n"
155+
: "=&r" (tmp));
156+
}
157+
158+
static int qcom_enable_link_stack_sanitization(void *data)
159+
{
160+
const struct arm64_cpu_capabilities *entry = data;
161+
162+
install_bp_hardening_cb(entry, qcom_link_stack_sanitization,
163+
__qcom_hyp_sanitize_link_stack_start,
164+
__qcom_hyp_sanitize_link_stack_end);
165+
166+
return 0;
167+
}
141168
#endif /* CONFIG_HARDEN_BRANCH_PREDICTOR */
142169

143170
#define MIDR_RANGE(model, min, max) \
@@ -302,6 +329,15 @@ const struct arm64_cpu_capabilities arm64_errata[] = {
302329
MIDR_ALL_VERSIONS(MIDR_CORTEX_A75),
303330
.enable = enable_psci_bp_hardening,
304331
},
332+
{
333+
.capability = ARM64_HARDEN_BRANCH_PREDICTOR,
334+
MIDR_ALL_VERSIONS(MIDR_QCOM_FALKOR_V1),
335+
.enable = qcom_enable_link_stack_sanitization,
336+
},
337+
{
338+
.capability = ARM64_HARDEN_BP_POST_GUEST_EXIT,
339+
MIDR_ALL_VERSIONS(MIDR_QCOM_FALKOR_V1),
340+
},
305341
#endif
306342
{
307343
}

arch/arm64/kvm/hyp/entry.S

+12
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,15 @@ alternative_endif
196196

197197
eret
198198
ENDPROC(__fpsimd_guest_restore)
199+
200+
ENTRY(__qcom_hyp_sanitize_btac_predictors)
201+
/**
202+
* Call SMC64 with Silicon provider serviceID 23<<8 (0xc2001700)
203+
* 0xC2000000-0xC200FFFF: assigned to SiP Service Calls
204+
* b15-b0: contains SiP functionID
205+
*/
206+
movz x0, #0x1700
207+
movk x0, #0xc200, lsl #16
208+
smc #0
209+
ret
210+
ENDPROC(__qcom_hyp_sanitize_btac_predictors)

arch/arm64/kvm/hyp/switch.c

+8
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,14 @@ int __hyp_text __kvm_vcpu_run(struct kvm_vcpu *vcpu)
406406
/* 0 falls through to be handled out of EL2 */
407407
}
408408

409+
if (cpus_have_const_cap(ARM64_HARDEN_BP_POST_GUEST_EXIT)) {
410+
u32 midr = read_cpuid_id();
411+
412+
/* Apply BTAC predictors mitigation to all Falkor chips */
413+
if ((midr & MIDR_CPU_MODEL_MASK) == MIDR_QCOM_FALKOR_V1)
414+
__qcom_hyp_sanitize_btac_predictors();
415+
}
416+
409417
fp_enabled = __fpsimd_enabled();
410418

411419
__sysreg_save_guest_state(guest_ctxt);

0 commit comments

Comments
 (0)