Skip to content

Commit 1011510

Browse files
author
Russell King
committed
ARM: spectre-v2: add firmware based hardening
Add firmware based hardening for cores that require more complex handling in firmware. Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk> Boot-tested-by: Tony Lindgren <tony@atomide.com> Reviewed-by: Tony Lindgren <tony@atomide.com> Reviewed-by: Marc Zyngier <marc.zyngier@arm.com>
1 parent f5fe12b commit 1011510

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

arch/arm/mm/proc-v7-bugs.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
// SPDX-License-Identifier: GPL-2.0
2+
#include <linux/arm-smccc.h>
23
#include <linux/kernel.h>
4+
#include <linux/psci.h>
35
#include <linux/smp.h>
46

57
#include <asm/cp15.h>
68
#include <asm/cputype.h>
9+
#include <asm/proc-fns.h>
710
#include <asm/system_misc.h>
811

912
#ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
1013
DEFINE_PER_CPU(harden_branch_predictor_fn_t, harden_branch_predictor_fn);
1114

15+
extern void cpu_v7_smc_switch_mm(phys_addr_t pgd_phys, struct mm_struct *mm);
16+
extern void cpu_v7_hvc_switch_mm(phys_addr_t pgd_phys, struct mm_struct *mm);
17+
1218
static void harden_branch_predictor_bpiall(void)
1319
{
1420
write_sysreg(0, BPIALL);
@@ -19,6 +25,16 @@ static void harden_branch_predictor_iciallu(void)
1925
write_sysreg(0, ICIALLU);
2026
}
2127

28+
static void __maybe_unused call_smc_arch_workaround_1(void)
29+
{
30+
arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
31+
}
32+
33+
static void __maybe_unused call_hvc_arch_workaround_1(void)
34+
{
35+
arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
36+
}
37+
2238
static void cpu_v7_spectre_init(void)
2339
{
2440
const char *spectre_v2_method = NULL;
@@ -45,7 +61,51 @@ static void cpu_v7_spectre_init(void)
4561
harden_branch_predictor_iciallu;
4662
spectre_v2_method = "ICIALLU";
4763
break;
64+
65+
#ifdef CONFIG_ARM_PSCI
66+
default:
67+
/* Other ARM CPUs require no workaround */
68+
if (read_cpuid_implementor() == ARM_CPU_IMP_ARM)
69+
break;
70+
/* fallthrough */
71+
/* Cortex A57/A72 require firmware workaround */
72+
case ARM_CPU_PART_CORTEX_A57:
73+
case ARM_CPU_PART_CORTEX_A72: {
74+
struct arm_smccc_res res;
75+
76+
if (psci_ops.smccc_version == SMCCC_VERSION_1_0)
77+
break;
78+
79+
switch (psci_ops.conduit) {
80+
case PSCI_CONDUIT_HVC:
81+
arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
82+
ARM_SMCCC_ARCH_WORKAROUND_1, &res);
83+
if ((int)res.a0 != 0)
84+
break;
85+
per_cpu(harden_branch_predictor_fn, cpu) =
86+
call_hvc_arch_workaround_1;
87+
processor.switch_mm = cpu_v7_hvc_switch_mm;
88+
spectre_v2_method = "hypervisor";
89+
break;
90+
91+
case PSCI_CONDUIT_SMC:
92+
arm_smccc_1_1_smc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
93+
ARM_SMCCC_ARCH_WORKAROUND_1, &res);
94+
if ((int)res.a0 != 0)
95+
break;
96+
per_cpu(harden_branch_predictor_fn, cpu) =
97+
call_smc_arch_workaround_1;
98+
processor.switch_mm = cpu_v7_smc_switch_mm;
99+
spectre_v2_method = "firmware";
100+
break;
101+
102+
default:
103+
break;
104+
}
48105
}
106+
#endif
107+
}
108+
49109
if (spectre_v2_method)
50110
pr_info("CPU%u: Spectre v2: using %s workaround\n",
51111
smp_processor_id(), spectre_v2_method);

arch/arm/mm/proc-v7.S

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*
1010
* This is the "shell" of the ARMv7 processor support.
1111
*/
12+
#include <linux/arm-smccc.h>
1213
#include <linux/init.h>
1314
#include <linux/linkage.h>
1415
#include <asm/assembler.h>
@@ -93,6 +94,26 @@ ENTRY(cpu_v7_dcache_clean_area)
9394
ret lr
9495
ENDPROC(cpu_v7_dcache_clean_area)
9596

97+
#ifdef CONFIG_ARM_PSCI
98+
.arch_extension sec
99+
ENTRY(cpu_v7_smc_switch_mm)
100+
stmfd sp!, {r0 - r3}
101+
movw r0, #:lower16:ARM_SMCCC_ARCH_WORKAROUND_1
102+
movt r0, #:upper16:ARM_SMCCC_ARCH_WORKAROUND_1
103+
smc #0
104+
ldmfd sp!, {r0 - r3}
105+
b cpu_v7_switch_mm
106+
ENDPROC(cpu_v7_smc_switch_mm)
107+
.arch_extension virt
108+
ENTRY(cpu_v7_hvc_switch_mm)
109+
stmfd sp!, {r0 - r3}
110+
movw r0, #:lower16:ARM_SMCCC_ARCH_WORKAROUND_1
111+
movt r0, #:upper16:ARM_SMCCC_ARCH_WORKAROUND_1
112+
hvc #0
113+
ldmfd sp!, {r0 - r3}
114+
b cpu_v7_switch_mm
115+
ENDPROC(cpu_v7_smc_switch_mm)
116+
#endif
96117
ENTRY(cpu_v7_iciallu_switch_mm)
97118
mov r3, #0
98119
mcr p15, 0, r3, c7, c5, 0 @ ICIALLU

0 commit comments

Comments
 (0)