1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <linux/arm-smccc.h>
3*4882a593Smuzhiyun #include <linux/kernel.h>
4*4882a593Smuzhiyun #include <linux/smp.h>
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <asm/cp15.h>
7*4882a593Smuzhiyun #include <asm/cputype.h>
8*4882a593Smuzhiyun #include <asm/proc-fns.h>
9*4882a593Smuzhiyun #include <asm/spectre.h>
10*4882a593Smuzhiyun #include <asm/system_misc.h>
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #ifdef CONFIG_ARM_PSCI
spectre_v2_get_cpu_fw_mitigation_state(void)13*4882a593Smuzhiyun static int __maybe_unused spectre_v2_get_cpu_fw_mitigation_state(void)
14*4882a593Smuzhiyun {
15*4882a593Smuzhiyun struct arm_smccc_res res;
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
18*4882a593Smuzhiyun ARM_SMCCC_ARCH_WORKAROUND_1, &res);
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun switch ((int)res.a0) {
21*4882a593Smuzhiyun case SMCCC_RET_SUCCESS:
22*4882a593Smuzhiyun return SPECTRE_MITIGATED;
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun case SMCCC_ARCH_WORKAROUND_RET_UNAFFECTED:
25*4882a593Smuzhiyun return SPECTRE_UNAFFECTED;
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun default:
28*4882a593Smuzhiyun return SPECTRE_VULNERABLE;
29*4882a593Smuzhiyun }
30*4882a593Smuzhiyun }
31*4882a593Smuzhiyun #else
spectre_v2_get_cpu_fw_mitigation_state(void)32*4882a593Smuzhiyun static int __maybe_unused spectre_v2_get_cpu_fw_mitigation_state(void)
33*4882a593Smuzhiyun {
34*4882a593Smuzhiyun return SPECTRE_VULNERABLE;
35*4882a593Smuzhiyun }
36*4882a593Smuzhiyun #endif
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun #ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
39*4882a593Smuzhiyun DEFINE_PER_CPU(harden_branch_predictor_fn_t, harden_branch_predictor_fn);
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun extern void cpu_v7_iciallu_switch_mm(phys_addr_t pgd_phys, struct mm_struct *mm);
42*4882a593Smuzhiyun extern void cpu_v7_bpiall_switch_mm(phys_addr_t pgd_phys, struct mm_struct *mm);
43*4882a593Smuzhiyun extern void cpu_v7_smc_switch_mm(phys_addr_t pgd_phys, struct mm_struct *mm);
44*4882a593Smuzhiyun extern void cpu_v7_hvc_switch_mm(phys_addr_t pgd_phys, struct mm_struct *mm);
45*4882a593Smuzhiyun
harden_branch_predictor_bpiall(void)46*4882a593Smuzhiyun static void harden_branch_predictor_bpiall(void)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun write_sysreg(0, BPIALL);
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun
harden_branch_predictor_iciallu(void)51*4882a593Smuzhiyun static void harden_branch_predictor_iciallu(void)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun write_sysreg(0, ICIALLU);
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun
call_smc_arch_workaround_1(void)56*4882a593Smuzhiyun static void __maybe_unused call_smc_arch_workaround_1(void)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun
call_hvc_arch_workaround_1(void)61*4882a593Smuzhiyun static void __maybe_unused call_hvc_arch_workaround_1(void)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun
spectre_v2_install_workaround(unsigned int method)66*4882a593Smuzhiyun static unsigned int spectre_v2_install_workaround(unsigned int method)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun const char *spectre_v2_method = NULL;
69*4882a593Smuzhiyun int cpu = smp_processor_id();
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun if (per_cpu(harden_branch_predictor_fn, cpu))
72*4882a593Smuzhiyun return SPECTRE_MITIGATED;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun switch (method) {
75*4882a593Smuzhiyun case SPECTRE_V2_METHOD_BPIALL:
76*4882a593Smuzhiyun per_cpu(harden_branch_predictor_fn, cpu) =
77*4882a593Smuzhiyun harden_branch_predictor_bpiall;
78*4882a593Smuzhiyun spectre_v2_method = "BPIALL";
79*4882a593Smuzhiyun break;
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun case SPECTRE_V2_METHOD_ICIALLU:
82*4882a593Smuzhiyun per_cpu(harden_branch_predictor_fn, cpu) =
83*4882a593Smuzhiyun harden_branch_predictor_iciallu;
84*4882a593Smuzhiyun spectre_v2_method = "ICIALLU";
85*4882a593Smuzhiyun break;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun case SPECTRE_V2_METHOD_HVC:
88*4882a593Smuzhiyun per_cpu(harden_branch_predictor_fn, cpu) =
89*4882a593Smuzhiyun call_hvc_arch_workaround_1;
90*4882a593Smuzhiyun cpu_do_switch_mm = cpu_v7_hvc_switch_mm;
91*4882a593Smuzhiyun spectre_v2_method = "hypervisor";
92*4882a593Smuzhiyun break;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun case SPECTRE_V2_METHOD_SMC:
95*4882a593Smuzhiyun per_cpu(harden_branch_predictor_fn, cpu) =
96*4882a593Smuzhiyun call_smc_arch_workaround_1;
97*4882a593Smuzhiyun cpu_do_switch_mm = cpu_v7_smc_switch_mm;
98*4882a593Smuzhiyun spectre_v2_method = "firmware";
99*4882a593Smuzhiyun break;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun if (spectre_v2_method)
103*4882a593Smuzhiyun pr_info("CPU%u: Spectre v2: using %s workaround\n",
104*4882a593Smuzhiyun smp_processor_id(), spectre_v2_method);
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun return SPECTRE_MITIGATED;
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun #else
spectre_v2_install_workaround(unsigned int method)109*4882a593Smuzhiyun static unsigned int spectre_v2_install_workaround(unsigned int method)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun pr_info_once("Spectre V2: workarounds disabled by configuration\n");
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun return SPECTRE_VULNERABLE;
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun #endif
116*4882a593Smuzhiyun
cpu_v7_spectre_v2_init(void)117*4882a593Smuzhiyun static void cpu_v7_spectre_v2_init(void)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun unsigned int state, method = 0;
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun switch (read_cpuid_part()) {
122*4882a593Smuzhiyun case ARM_CPU_PART_CORTEX_A8:
123*4882a593Smuzhiyun case ARM_CPU_PART_CORTEX_A9:
124*4882a593Smuzhiyun case ARM_CPU_PART_CORTEX_A12:
125*4882a593Smuzhiyun case ARM_CPU_PART_CORTEX_A17:
126*4882a593Smuzhiyun case ARM_CPU_PART_CORTEX_A73:
127*4882a593Smuzhiyun case ARM_CPU_PART_CORTEX_A75:
128*4882a593Smuzhiyun state = SPECTRE_MITIGATED;
129*4882a593Smuzhiyun method = SPECTRE_V2_METHOD_BPIALL;
130*4882a593Smuzhiyun break;
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun case ARM_CPU_PART_CORTEX_A15:
133*4882a593Smuzhiyun case ARM_CPU_PART_BRAHMA_B15:
134*4882a593Smuzhiyun state = SPECTRE_MITIGATED;
135*4882a593Smuzhiyun method = SPECTRE_V2_METHOD_ICIALLU;
136*4882a593Smuzhiyun break;
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun case ARM_CPU_PART_BRAHMA_B53:
139*4882a593Smuzhiyun /* Requires no workaround */
140*4882a593Smuzhiyun state = SPECTRE_UNAFFECTED;
141*4882a593Smuzhiyun break;
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun default:
144*4882a593Smuzhiyun /* Other ARM CPUs require no workaround */
145*4882a593Smuzhiyun if (read_cpuid_implementor() == ARM_CPU_IMP_ARM) {
146*4882a593Smuzhiyun state = SPECTRE_UNAFFECTED;
147*4882a593Smuzhiyun break;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun fallthrough;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun /* Cortex A57/A72 require firmware workaround */
153*4882a593Smuzhiyun case ARM_CPU_PART_CORTEX_A57:
154*4882a593Smuzhiyun case ARM_CPU_PART_CORTEX_A72:
155*4882a593Smuzhiyun state = spectre_v2_get_cpu_fw_mitigation_state();
156*4882a593Smuzhiyun if (state != SPECTRE_MITIGATED)
157*4882a593Smuzhiyun break;
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun switch (arm_smccc_1_1_get_conduit()) {
160*4882a593Smuzhiyun case SMCCC_CONDUIT_HVC:
161*4882a593Smuzhiyun method = SPECTRE_V2_METHOD_HVC;
162*4882a593Smuzhiyun break;
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun case SMCCC_CONDUIT_SMC:
165*4882a593Smuzhiyun method = SPECTRE_V2_METHOD_SMC;
166*4882a593Smuzhiyun break;
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun default:
169*4882a593Smuzhiyun state = SPECTRE_VULNERABLE;
170*4882a593Smuzhiyun break;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun if (state == SPECTRE_MITIGATED)
175*4882a593Smuzhiyun state = spectre_v2_install_workaround(method);
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun spectre_v2_update_state(state, method);
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun #ifdef CONFIG_HARDEN_BRANCH_HISTORY
181*4882a593Smuzhiyun static int spectre_bhb_method;
182*4882a593Smuzhiyun
spectre_bhb_method_name(int method)183*4882a593Smuzhiyun static const char *spectre_bhb_method_name(int method)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun switch (method) {
186*4882a593Smuzhiyun case SPECTRE_V2_METHOD_LOOP8:
187*4882a593Smuzhiyun return "loop";
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun case SPECTRE_V2_METHOD_BPIALL:
190*4882a593Smuzhiyun return "BPIALL";
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun default:
193*4882a593Smuzhiyun return "unknown";
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun
spectre_bhb_install_workaround(int method)197*4882a593Smuzhiyun static int spectre_bhb_install_workaround(int method)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun if (spectre_bhb_method != method) {
200*4882a593Smuzhiyun if (spectre_bhb_method) {
201*4882a593Smuzhiyun pr_err("CPU%u: Spectre BHB: method disagreement, system vulnerable\n",
202*4882a593Smuzhiyun smp_processor_id());
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun return SPECTRE_VULNERABLE;
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun if (spectre_bhb_update_vectors(method) == SPECTRE_VULNERABLE)
208*4882a593Smuzhiyun return SPECTRE_VULNERABLE;
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun spectre_bhb_method = method;
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun pr_info("CPU%u: Spectre BHB: enabling %s workaround for all CPUs\n",
213*4882a593Smuzhiyun smp_processor_id(), spectre_bhb_method_name(method));
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun return SPECTRE_MITIGATED;
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun #else
spectre_bhb_install_workaround(int method)219*4882a593Smuzhiyun static int spectre_bhb_install_workaround(int method)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun return SPECTRE_VULNERABLE;
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun #endif
224*4882a593Smuzhiyun
cpu_v7_spectre_bhb_init(void)225*4882a593Smuzhiyun static void cpu_v7_spectre_bhb_init(void)
226*4882a593Smuzhiyun {
227*4882a593Smuzhiyun unsigned int state, method = 0;
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun switch (read_cpuid_part()) {
230*4882a593Smuzhiyun case ARM_CPU_PART_CORTEX_A15:
231*4882a593Smuzhiyun case ARM_CPU_PART_BRAHMA_B15:
232*4882a593Smuzhiyun case ARM_CPU_PART_CORTEX_A57:
233*4882a593Smuzhiyun case ARM_CPU_PART_CORTEX_A72:
234*4882a593Smuzhiyun state = SPECTRE_MITIGATED;
235*4882a593Smuzhiyun method = SPECTRE_V2_METHOD_LOOP8;
236*4882a593Smuzhiyun break;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun case ARM_CPU_PART_CORTEX_A73:
239*4882a593Smuzhiyun case ARM_CPU_PART_CORTEX_A75:
240*4882a593Smuzhiyun state = SPECTRE_MITIGATED;
241*4882a593Smuzhiyun method = SPECTRE_V2_METHOD_BPIALL;
242*4882a593Smuzhiyun break;
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun default:
245*4882a593Smuzhiyun state = SPECTRE_UNAFFECTED;
246*4882a593Smuzhiyun break;
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun if (state == SPECTRE_MITIGATED)
250*4882a593Smuzhiyun state = spectre_bhb_install_workaround(method);
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun spectre_v2_update_state(state, method);
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun
cpu_v7_check_auxcr_set(bool * warned,u32 mask,const char * msg)255*4882a593Smuzhiyun static __maybe_unused bool cpu_v7_check_auxcr_set(bool *warned,
256*4882a593Smuzhiyun u32 mask, const char *msg)
257*4882a593Smuzhiyun {
258*4882a593Smuzhiyun u32 aux_cr;
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun asm("mrc p15, 0, %0, c1, c0, 1" : "=r" (aux_cr));
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun if ((aux_cr & mask) != mask) {
263*4882a593Smuzhiyun if (!*warned)
264*4882a593Smuzhiyun pr_err("CPU%u: %s", smp_processor_id(), msg);
265*4882a593Smuzhiyun *warned = true;
266*4882a593Smuzhiyun return false;
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun return true;
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun static DEFINE_PER_CPU(bool, spectre_warned);
272*4882a593Smuzhiyun
check_spectre_auxcr(bool * warned,u32 bit)273*4882a593Smuzhiyun static bool check_spectre_auxcr(bool *warned, u32 bit)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun return IS_ENABLED(CONFIG_HARDEN_BRANCH_PREDICTOR) &&
276*4882a593Smuzhiyun cpu_v7_check_auxcr_set(warned, bit,
277*4882a593Smuzhiyun "Spectre v2: firmware did not set auxiliary control register IBE bit, system vulnerable\n");
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun
cpu_v7_ca8_ibe(void)280*4882a593Smuzhiyun void cpu_v7_ca8_ibe(void)
281*4882a593Smuzhiyun {
282*4882a593Smuzhiyun if (check_spectre_auxcr(this_cpu_ptr(&spectre_warned), BIT(6)))
283*4882a593Smuzhiyun cpu_v7_spectre_v2_init();
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun
cpu_v7_ca15_ibe(void)286*4882a593Smuzhiyun void cpu_v7_ca15_ibe(void)
287*4882a593Smuzhiyun {
288*4882a593Smuzhiyun if (check_spectre_auxcr(this_cpu_ptr(&spectre_warned), BIT(0)))
289*4882a593Smuzhiyun cpu_v7_spectre_v2_init();
290*4882a593Smuzhiyun cpu_v7_spectre_bhb_init();
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun
cpu_v7_bugs_init(void)293*4882a593Smuzhiyun void cpu_v7_bugs_init(void)
294*4882a593Smuzhiyun {
295*4882a593Smuzhiyun cpu_v7_spectre_v2_init();
296*4882a593Smuzhiyun cpu_v7_spectre_bhb_init();
297*4882a593Smuzhiyun }
298