]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1353ebb4 | 2 | /* |
1353ebb4 JF |
3 | * Copyright (C) 1994 Linus Torvalds |
4 | * | |
5 | * Cyrix stuff, June 1998 by: | |
6 | * - Rafael R. Reilova (moved everything from head.S), | |
7 | * <rreilova@ececs.uc.edu> | |
8 | * - Channing Corn (tests & fixes), | |
9 | * - Andrew D. Balsa (code cleanup). | |
10 | */ | |
11 | #include <linux/init.h> | |
12 | #include <linux/utsname.h> | |
61dc0f55 | 13 | #include <linux/cpu.h> |
caf7501a | 14 | #include <linux/module.h> |
a73ec77e TG |
15 | #include <linux/nospec.h> |
16 | #include <linux/prctl.h> | |
a74cfffb | 17 | #include <linux/sched/smt.h> |
65fddcfc | 18 | #include <linux/pgtable.h> |
534fcc9a | 19 | #include <linux/bpf.h> |
da285121 | 20 | |
28a27752 | 21 | #include <asm/spec-ctrl.h> |
da285121 | 22 | #include <asm/cmdline.h> |
91eb1b79 | 23 | #include <asm/bugs.h> |
1353ebb4 | 24 | #include <asm/processor.h> |
7ebad705 | 25 | #include <asm/processor-flags.h> |
4e85b53e | 26 | #include <asm/fpu/api.h> |
1353ebb4 | 27 | #include <asm/msr.h> |
72c6d2db | 28 | #include <asm/vmx.h> |
1353ebb4 JF |
29 | #include <asm/paravirt.h> |
30 | #include <asm/alternative.h> | |
d1163651 | 31 | #include <asm/set_memory.h> |
c995efd5 | 32 | #include <asm/intel-family.h> |
17dbca11 | 33 | #include <asm/e820/api.h> |
6cb2b08f | 34 | #include <asm/hypervisor.h> |
f29dfa53 | 35 | #include <asm/tlbflush.h> |
1353ebb4 | 36 | |
ad3bc25a BP |
37 | #include "cpu.h" |
38 | ||
a2059825 | 39 | static void __init spectre_v1_select_mitigation(void); |
da285121 | 40 | static void __init spectre_v2_select_mitigation(void); |
24f7fc83 | 41 | static void __init ssb_select_mitigation(void); |
17dbca11 | 42 | static void __init l1tf_select_mitigation(void); |
bc124170 | 43 | static void __init mds_select_mitigation(void); |
6a9231bf | 44 | static void __init md_clear_update_mitigation(void); |
1b42f017 | 45 | static void __init taa_select_mitigation(void); |
2418c6b1 | 46 | static void __init mmio_select_mitigation(void); |
7e5b3c26 | 47 | static void __init srbds_select_mitigation(void); |
b5f06f64 | 48 | static void __init l1d_flush_select_mitigation(void); |
da285121 | 49 | |
53c613fe JK |
50 | /* The base value of the SPEC_CTRL MSR that always has to be preserved. */ |
51 | u64 x86_spec_ctrl_base; | |
fa8ac498 | 52 | EXPORT_SYMBOL_GPL(x86_spec_ctrl_base); |
53c613fe | 53 | static DEFINE_MUTEX(spec_ctrl_mutex); |
1b86883c | 54 | |
1115a859 KRW |
55 | /* |
56 | * The vendor and possibly platform specific bits which can be modified in | |
57 | * x86_spec_ctrl_base. | |
58 | */ | |
be6fcb54 | 59 | static u64 __ro_after_init x86_spec_ctrl_mask = SPEC_CTRL_IBRS; |
1115a859 | 60 | |
764f3c21 KRW |
61 | /* |
62 | * AMD specific MSR info for Speculative Store Bypass control. | |
9f65fb29 | 63 | * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu(). |
764f3c21 KRW |
64 | */ |
65 | u64 __ro_after_init x86_amd_ls_cfg_base; | |
9f65fb29 | 66 | u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask; |
764f3c21 | 67 | |
aa77bfb3 | 68 | /* Control conditional STIBP in switch_to() */ |
fa1202ef | 69 | DEFINE_STATIC_KEY_FALSE(switch_to_cond_stibp); |
4c71a2b6 TG |
70 | /* Control conditional IBPB in switch_mm() */ |
71 | DEFINE_STATIC_KEY_FALSE(switch_mm_cond_ibpb); | |
72 | /* Control unconditional IBPB in switch_mm() */ | |
73 | DEFINE_STATIC_KEY_FALSE(switch_mm_always_ibpb); | |
fa1202ef | 74 | |
04dcbdb8 TG |
75 | /* Control MDS CPU buffer clear before returning to user space */ |
76 | DEFINE_STATIC_KEY_FALSE(mds_user_clear); | |
650b68a0 | 77 | EXPORT_SYMBOL_GPL(mds_user_clear); |
07f07f55 TG |
78 | /* Control MDS CPU buffer clear before idling (halt, mwait) */ |
79 | DEFINE_STATIC_KEY_FALSE(mds_idle_clear); | |
80 | EXPORT_SYMBOL_GPL(mds_idle_clear); | |
04dcbdb8 | 81 | |
b5f06f64 BS |
82 | /* |
83 | * Controls whether l1d flush based mitigations are enabled, | |
84 | * based on hw features and admin setting via boot parameter | |
85 | * defaults to false | |
86 | */ | |
87 | DEFINE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush); | |
88 | ||
2418c6b1 PG |
89 | /* Controls CPU Fill buffer clear before KVM guest MMIO accesses */ |
90 | DEFINE_STATIC_KEY_FALSE(mmio_stale_data_clear); | |
91 | EXPORT_SYMBOL_GPL(mmio_stale_data_clear); | |
92 | ||
1353ebb4 JF |
93 | void __init check_bugs(void) |
94 | { | |
95 | identify_boot_cpu(); | |
55a36b65 | 96 | |
fee0aede TG |
97 | /* |
98 | * identify_boot_cpu() initialized SMT support information, let the | |
99 | * core code know. | |
100 | */ | |
b284909a | 101 | cpu_smt_check_topology(); |
fee0aede | 102 | |
62a67e12 BP |
103 | if (!IS_ENABLED(CONFIG_SMP)) { |
104 | pr_info("CPU: "); | |
105 | print_cpu_info(&boot_cpu_data); | |
106 | } | |
107 | ||
1b86883c KRW |
108 | /* |
109 | * Read the SPEC_CTRL MSR to account for reserved bits which may | |
764f3c21 KRW |
110 | * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD |
111 | * init code as it is not enumerated and depends on the family. | |
1b86883c | 112 | */ |
7eb8956a | 113 | if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) |
1b86883c KRW |
114 | rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base); |
115 | ||
be6fcb54 TG |
116 | /* Allow STIBP in MSR_SPEC_CTRL if supported */ |
117 | if (boot_cpu_has(X86_FEATURE_STIBP)) | |
118 | x86_spec_ctrl_mask |= SPEC_CTRL_STIBP; | |
119 | ||
a2059825 JP |
120 | /* Select the proper CPU mitigations before patching alternatives: */ |
121 | spectre_v1_select_mitigation(); | |
da285121 | 122 | spectre_v2_select_mitigation(); |
24f7fc83 | 123 | ssb_select_mitigation(); |
17dbca11 | 124 | l1tf_select_mitigation(); |
bc124170 | 125 | mds_select_mitigation(); |
1b42f017 | 126 | taa_select_mitigation(); |
2418c6b1 | 127 | mmio_select_mitigation(); |
7e5b3c26 | 128 | srbds_select_mitigation(); |
b5f06f64 | 129 | l1d_flush_select_mitigation(); |
bc124170 | 130 | |
cd5a2aa8 | 131 | /* |
2418c6b1 PG |
132 | * As MDS, TAA and MMIO Stale Data mitigations are inter-related, update |
133 | * and print their mitigation after MDS, TAA and MMIO Stale Data | |
134 | * mitigation selection is done. | |
cd5a2aa8 | 135 | */ |
6a9231bf | 136 | md_clear_update_mitigation(); |
cd5a2aa8 | 137 | |
7c3658b2 JP |
138 | arch_smt_update(); |
139 | ||
62a67e12 | 140 | #ifdef CONFIG_X86_32 |
55a36b65 BP |
141 | /* |
142 | * Check whether we are able to run this kernel safely on SMP. | |
143 | * | |
144 | * - i386 is no longer supported. | |
145 | * - In order to run on anything without a TSC, we need to be | |
146 | * compiled for a i486. | |
147 | */ | |
148 | if (boot_cpu_data.x86 < 4) | |
149 | panic("Kernel requires i486+ for 'invlpg' and other features"); | |
150 | ||
bfe4bb15 MV |
151 | init_utsname()->machine[1] = |
152 | '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86); | |
1353ebb4 | 153 | alternative_instructions(); |
304bceda | 154 | |
4d164092 | 155 | fpu__init_check_bugs(); |
62a67e12 BP |
156 | #else /* CONFIG_X86_64 */ |
157 | alternative_instructions(); | |
158 | ||
159 | /* | |
160 | * Make sure the first 2MB area is not mapped by huge pages | |
161 | * There are typically fixed size MTRRs in there and overlapping | |
162 | * MTRRs into large pages causes slow downs. | |
163 | * | |
164 | * Right now we don't do that with gbpages because there seems | |
165 | * very little benefit for that case. | |
166 | */ | |
167 | if (!direct_gbpages) | |
168 | set_memory_4k((unsigned long)__va(0), 1); | |
169 | #endif | |
1353ebb4 | 170 | } |
61dc0f55 | 171 | |
cc69b349 BP |
172 | void |
173 | x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest) | |
5cf68754 | 174 | { |
be6fcb54 | 175 | u64 msrval, guestval, hostval = x86_spec_ctrl_base; |
cc69b349 | 176 | struct thread_info *ti = current_thread_info(); |
885f82bf | 177 | |
7eb8956a | 178 | /* Is MSR_SPEC_CTRL implemented ? */ |
cc69b349 | 179 | if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) { |
be6fcb54 TG |
180 | /* |
181 | * Restrict guest_spec_ctrl to supported values. Clear the | |
182 | * modifiable bits in the host base value and or the | |
183 | * modifiable bits from the guest value. | |
184 | */ | |
185 | guestval = hostval & ~x86_spec_ctrl_mask; | |
186 | guestval |= guest_spec_ctrl & x86_spec_ctrl_mask; | |
187 | ||
cc69b349 | 188 | /* SSBD controlled in MSR_SPEC_CTRL */ |
612bc3b3 TL |
189 | if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) || |
190 | static_cpu_has(X86_FEATURE_AMD_SSBD)) | |
be6fcb54 | 191 | hostval |= ssbd_tif_to_spec_ctrl(ti->flags); |
cc69b349 | 192 | |
5bfbe3ad TC |
193 | /* Conditional STIBP enabled? */ |
194 | if (static_branch_unlikely(&switch_to_cond_stibp)) | |
195 | hostval |= stibp_tif_to_spec_ctrl(ti->flags); | |
196 | ||
be6fcb54 TG |
197 | if (hostval != guestval) { |
198 | msrval = setguest ? guestval : hostval; | |
199 | wrmsrl(MSR_IA32_SPEC_CTRL, msrval); | |
cc69b349 BP |
200 | } |
201 | } | |
47c61b39 TG |
202 | |
203 | /* | |
204 | * If SSBD is not handled in MSR_SPEC_CTRL on AMD, update | |
205 | * MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported. | |
206 | */ | |
207 | if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD) && | |
208 | !static_cpu_has(X86_FEATURE_VIRT_SSBD)) | |
209 | return; | |
210 | ||
211 | /* | |
212 | * If the host has SSBD mitigation enabled, force it in the host's | |
213 | * virtual MSR value. If its not permanently enabled, evaluate | |
214 | * current's TIF_SSBD thread flag. | |
215 | */ | |
216 | if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE)) | |
217 | hostval = SPEC_CTRL_SSBD; | |
218 | else | |
219 | hostval = ssbd_tif_to_spec_ctrl(ti->flags); | |
220 | ||
221 | /* Sanitize the guest value */ | |
222 | guestval = guest_virt_spec_ctrl & SPEC_CTRL_SSBD; | |
223 | ||
224 | if (hostval != guestval) { | |
225 | unsigned long tif; | |
226 | ||
227 | tif = setguest ? ssbd_spec_ctrl_to_tif(guestval) : | |
228 | ssbd_spec_ctrl_to_tif(hostval); | |
229 | ||
26c4d75b | 230 | speculation_ctrl_update(tif); |
47c61b39 | 231 | } |
5cf68754 | 232 | } |
cc69b349 | 233 | EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl); |
5cf68754 | 234 | |
9f65fb29 | 235 | static void x86_amd_ssb_disable(void) |
764f3c21 | 236 | { |
9f65fb29 | 237 | u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_ssbd_mask; |
764f3c21 | 238 | |
11fb0683 TL |
239 | if (boot_cpu_has(X86_FEATURE_VIRT_SSBD)) |
240 | wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, SPEC_CTRL_SSBD); | |
241 | else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD)) | |
764f3c21 KRW |
242 | wrmsrl(MSR_AMD64_LS_CFG, msrval); |
243 | } | |
244 | ||
bc124170 TG |
245 | #undef pr_fmt |
246 | #define pr_fmt(fmt) "MDS: " fmt | |
247 | ||
cae5ec34 | 248 | /* Default mitigation for MDS-affected CPUs */ |
bc124170 | 249 | static enum mds_mitigations mds_mitigation __ro_after_init = MDS_MITIGATION_FULL; |
d71eb0ce | 250 | static bool mds_nosmt __ro_after_init = false; |
bc124170 TG |
251 | |
252 | static const char * const mds_strings[] = { | |
253 | [MDS_MITIGATION_OFF] = "Vulnerable", | |
22dd8365 TG |
254 | [MDS_MITIGATION_FULL] = "Mitigation: Clear CPU buffers", |
255 | [MDS_MITIGATION_VMWERV] = "Vulnerable: Clear CPU buffers attempted, no microcode", | |
bc124170 TG |
256 | }; |
257 | ||
258 | static void __init mds_select_mitigation(void) | |
259 | { | |
5c14068f | 260 | if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off()) { |
bc124170 TG |
261 | mds_mitigation = MDS_MITIGATION_OFF; |
262 | return; | |
263 | } | |
264 | ||
265 | if (mds_mitigation == MDS_MITIGATION_FULL) { | |
22dd8365 TG |
266 | if (!boot_cpu_has(X86_FEATURE_MD_CLEAR)) |
267 | mds_mitigation = MDS_MITIGATION_VMWERV; | |
d71eb0ce | 268 | |
22dd8365 | 269 | static_branch_enable(&mds_user_clear); |
d71eb0ce | 270 | |
5c14068f JP |
271 | if (!boot_cpu_has(X86_BUG_MSBDS_ONLY) && |
272 | (mds_nosmt || cpu_mitigations_auto_nosmt())) | |
d71eb0ce | 273 | cpu_smt_disable(false); |
bc124170 | 274 | } |
cd5a2aa8 WL |
275 | } |
276 | ||
bc124170 TG |
277 | static int __init mds_cmdline(char *str) |
278 | { | |
279 | if (!boot_cpu_has_bug(X86_BUG_MDS)) | |
280 | return 0; | |
281 | ||
282 | if (!str) | |
283 | return -EINVAL; | |
284 | ||
285 | if (!strcmp(str, "off")) | |
286 | mds_mitigation = MDS_MITIGATION_OFF; | |
287 | else if (!strcmp(str, "full")) | |
288 | mds_mitigation = MDS_MITIGATION_FULL; | |
d71eb0ce JP |
289 | else if (!strcmp(str, "full,nosmt")) { |
290 | mds_mitigation = MDS_MITIGATION_FULL; | |
291 | mds_nosmt = true; | |
292 | } | |
bc124170 TG |
293 | |
294 | return 0; | |
295 | } | |
296 | early_param("mds", mds_cmdline); | |
297 | ||
1b42f017 PG |
298 | #undef pr_fmt |
299 | #define pr_fmt(fmt) "TAA: " fmt | |
300 | ||
72c2ce98 BP |
301 | enum taa_mitigations { |
302 | TAA_MITIGATION_OFF, | |
303 | TAA_MITIGATION_UCODE_NEEDED, | |
304 | TAA_MITIGATION_VERW, | |
305 | TAA_MITIGATION_TSX_DISABLED, | |
306 | }; | |
307 | ||
1b42f017 PG |
308 | /* Default mitigation for TAA-affected CPUs */ |
309 | static enum taa_mitigations taa_mitigation __ro_after_init = TAA_MITIGATION_VERW; | |
310 | static bool taa_nosmt __ro_after_init; | |
311 | ||
312 | static const char * const taa_strings[] = { | |
313 | [TAA_MITIGATION_OFF] = "Vulnerable", | |
314 | [TAA_MITIGATION_UCODE_NEEDED] = "Vulnerable: Clear CPU buffers attempted, no microcode", | |
315 | [TAA_MITIGATION_VERW] = "Mitigation: Clear CPU buffers", | |
316 | [TAA_MITIGATION_TSX_DISABLED] = "Mitigation: TSX disabled", | |
317 | }; | |
318 | ||
319 | static void __init taa_select_mitigation(void) | |
320 | { | |
321 | u64 ia32_cap; | |
322 | ||
323 | if (!boot_cpu_has_bug(X86_BUG_TAA)) { | |
324 | taa_mitigation = TAA_MITIGATION_OFF; | |
325 | return; | |
326 | } | |
327 | ||
328 | /* TSX previously disabled by tsx=off */ | |
329 | if (!boot_cpu_has(X86_FEATURE_RTM)) { | |
330 | taa_mitigation = TAA_MITIGATION_TSX_DISABLED; | |
6a9231bf | 331 | return; |
1b42f017 PG |
332 | } |
333 | ||
334 | if (cpu_mitigations_off()) { | |
335 | taa_mitigation = TAA_MITIGATION_OFF; | |
336 | return; | |
337 | } | |
338 | ||
64870ed1 WL |
339 | /* |
340 | * TAA mitigation via VERW is turned off if both | |
341 | * tsx_async_abort=off and mds=off are specified. | |
342 | */ | |
343 | if (taa_mitigation == TAA_MITIGATION_OFF && | |
344 | mds_mitigation == MDS_MITIGATION_OFF) | |
6a9231bf | 345 | return; |
1b42f017 PG |
346 | |
347 | if (boot_cpu_has(X86_FEATURE_MD_CLEAR)) | |
348 | taa_mitigation = TAA_MITIGATION_VERW; | |
349 | else | |
350 | taa_mitigation = TAA_MITIGATION_UCODE_NEEDED; | |
351 | ||
352 | /* | |
353 | * VERW doesn't clear the CPU buffers when MD_CLEAR=1 and MDS_NO=1. | |
354 | * A microcode update fixes this behavior to clear CPU buffers. It also | |
355 | * adds support for MSR_IA32_TSX_CTRL which is enumerated by the | |
356 | * ARCH_CAP_TSX_CTRL_MSR bit. | |
357 | * | |
358 | * On MDS_NO=1 CPUs if ARCH_CAP_TSX_CTRL_MSR is not set, microcode | |
359 | * update is required. | |
360 | */ | |
361 | ia32_cap = x86_read_arch_cap_msr(); | |
362 | if ( (ia32_cap & ARCH_CAP_MDS_NO) && | |
363 | !(ia32_cap & ARCH_CAP_TSX_CTRL_MSR)) | |
364 | taa_mitigation = TAA_MITIGATION_UCODE_NEEDED; | |
365 | ||
366 | /* | |
367 | * TSX is enabled, select alternate mitigation for TAA which is | |
368 | * the same as MDS. Enable MDS static branch to clear CPU buffers. | |
369 | * | |
370 | * For guests that can't determine whether the correct microcode is | |
371 | * present on host, enable the mitigation for UCODE_NEEDED as well. | |
372 | */ | |
373 | static_branch_enable(&mds_user_clear); | |
374 | ||
375 | if (taa_nosmt || cpu_mitigations_auto_nosmt()) | |
376 | cpu_smt_disable(false); | |
1b42f017 PG |
377 | } |
378 | ||
379 | static int __init tsx_async_abort_parse_cmdline(char *str) | |
380 | { | |
381 | if (!boot_cpu_has_bug(X86_BUG_TAA)) | |
382 | return 0; | |
383 | ||
384 | if (!str) | |
385 | return -EINVAL; | |
386 | ||
387 | if (!strcmp(str, "off")) { | |
388 | taa_mitigation = TAA_MITIGATION_OFF; | |
389 | } else if (!strcmp(str, "full")) { | |
390 | taa_mitigation = TAA_MITIGATION_VERW; | |
391 | } else if (!strcmp(str, "full,nosmt")) { | |
392 | taa_mitigation = TAA_MITIGATION_VERW; | |
393 | taa_nosmt = true; | |
394 | } | |
395 | ||
396 | return 0; | |
397 | } | |
398 | early_param("tsx_async_abort", tsx_async_abort_parse_cmdline); | |
399 | ||
2418c6b1 PG |
400 | #undef pr_fmt |
401 | #define pr_fmt(fmt) "MMIO Stale Data: " fmt | |
402 | ||
403 | enum mmio_mitigations { | |
404 | MMIO_MITIGATION_OFF, | |
405 | MMIO_MITIGATION_UCODE_NEEDED, | |
406 | MMIO_MITIGATION_VERW, | |
407 | }; | |
408 | ||
409 | /* Default mitigation for Processor MMIO Stale Data vulnerabilities */ | |
410 | static enum mmio_mitigations mmio_mitigation __ro_after_init = MMIO_MITIGATION_VERW; | |
411 | static bool mmio_nosmt __ro_after_init = false; | |
412 | ||
413 | static const char * const mmio_strings[] = { | |
414 | [MMIO_MITIGATION_OFF] = "Vulnerable", | |
415 | [MMIO_MITIGATION_UCODE_NEEDED] = "Vulnerable: Clear CPU buffers attempted, no microcode", | |
416 | [MMIO_MITIGATION_VERW] = "Mitigation: Clear CPU buffers", | |
417 | }; | |
418 | ||
419 | static void __init mmio_select_mitigation(void) | |
420 | { | |
421 | u64 ia32_cap; | |
422 | ||
423 | if (!boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA) || | |
424 | cpu_mitigations_off()) { | |
425 | mmio_mitigation = MMIO_MITIGATION_OFF; | |
426 | return; | |
427 | } | |
428 | ||
429 | if (mmio_mitigation == MMIO_MITIGATION_OFF) | |
430 | return; | |
431 | ||
432 | ia32_cap = x86_read_arch_cap_msr(); | |
433 | ||
434 | /* | |
435 | * Enable CPU buffer clear mitigation for host and VMM, if also affected | |
436 | * by MDS or TAA. Otherwise, enable mitigation for VMM only. | |
437 | */ | |
438 | if (boot_cpu_has_bug(X86_BUG_MDS) || (boot_cpu_has_bug(X86_BUG_TAA) && | |
439 | boot_cpu_has(X86_FEATURE_RTM))) | |
440 | static_branch_enable(&mds_user_clear); | |
441 | else | |
442 | static_branch_enable(&mmio_stale_data_clear); | |
443 | ||
444 | /* | |
445 | * Check if the system has the right microcode. | |
446 | * | |
447 | * CPU Fill buffer clear mitigation is enumerated by either an explicit | |
448 | * FB_CLEAR or by the presence of both MD_CLEAR and L1D_FLUSH on MDS | |
449 | * affected systems. | |
450 | */ | |
451 | if ((ia32_cap & ARCH_CAP_FB_CLEAR) || | |
452 | (boot_cpu_has(X86_FEATURE_MD_CLEAR) && | |
453 | boot_cpu_has(X86_FEATURE_FLUSH_L1D) && | |
454 | !(ia32_cap & ARCH_CAP_MDS_NO))) | |
455 | mmio_mitigation = MMIO_MITIGATION_VERW; | |
456 | else | |
457 | mmio_mitigation = MMIO_MITIGATION_UCODE_NEEDED; | |
458 | ||
459 | if (mmio_nosmt || cpu_mitigations_auto_nosmt()) | |
460 | cpu_smt_disable(false); | |
461 | } | |
462 | ||
463 | static int __init mmio_stale_data_parse_cmdline(char *str) | |
464 | { | |
465 | if (!boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA)) | |
466 | return 0; | |
467 | ||
468 | if (!str) | |
469 | return -EINVAL; | |
470 | ||
471 | if (!strcmp(str, "off")) { | |
472 | mmio_mitigation = MMIO_MITIGATION_OFF; | |
473 | } else if (!strcmp(str, "full")) { | |
474 | mmio_mitigation = MMIO_MITIGATION_VERW; | |
475 | } else if (!strcmp(str, "full,nosmt")) { | |
476 | mmio_mitigation = MMIO_MITIGATION_VERW; | |
477 | mmio_nosmt = true; | |
478 | } | |
479 | ||
480 | return 0; | |
481 | } | |
482 | early_param("mmio_stale_data", mmio_stale_data_parse_cmdline); | |
483 | ||
6a9231bf PG |
484 | #undef pr_fmt |
485 | #define pr_fmt(fmt) "" fmt | |
486 | ||
487 | static void __init md_clear_update_mitigation(void) | |
488 | { | |
489 | if (cpu_mitigations_off()) | |
490 | return; | |
491 | ||
492 | if (!static_key_enabled(&mds_user_clear)) | |
493 | goto out; | |
494 | ||
495 | /* | |
2418c6b1 PG |
496 | * mds_user_clear is now enabled. Update MDS, TAA and MMIO Stale Data |
497 | * mitigation, if necessary. | |
6a9231bf PG |
498 | */ |
499 | if (mds_mitigation == MDS_MITIGATION_OFF && | |
500 | boot_cpu_has_bug(X86_BUG_MDS)) { | |
501 | mds_mitigation = MDS_MITIGATION_FULL; | |
502 | mds_select_mitigation(); | |
503 | } | |
2418c6b1 PG |
504 | if (taa_mitigation == TAA_MITIGATION_OFF && |
505 | boot_cpu_has_bug(X86_BUG_TAA)) { | |
506 | taa_mitigation = TAA_MITIGATION_VERW; | |
507 | taa_select_mitigation(); | |
508 | } | |
509 | if (mmio_mitigation == MMIO_MITIGATION_OFF && | |
510 | boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA)) { | |
511 | mmio_mitigation = MMIO_MITIGATION_VERW; | |
512 | mmio_select_mitigation(); | |
513 | } | |
6a9231bf PG |
514 | out: |
515 | if (boot_cpu_has_bug(X86_BUG_MDS)) | |
516 | pr_info("MDS: %s\n", mds_strings[mds_mitigation]); | |
517 | if (boot_cpu_has_bug(X86_BUG_TAA)) | |
518 | pr_info("TAA: %s\n", taa_strings[taa_mitigation]); | |
2418c6b1 PG |
519 | if (boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA)) |
520 | pr_info("MMIO Stale Data: %s\n", mmio_strings[mmio_mitigation]); | |
6a9231bf PG |
521 | } |
522 | ||
7e5b3c26 MG |
523 | #undef pr_fmt |
524 | #define pr_fmt(fmt) "SRBDS: " fmt | |
525 | ||
526 | enum srbds_mitigations { | |
527 | SRBDS_MITIGATION_OFF, | |
528 | SRBDS_MITIGATION_UCODE_NEEDED, | |
529 | SRBDS_MITIGATION_FULL, | |
530 | SRBDS_MITIGATION_TSX_OFF, | |
531 | SRBDS_MITIGATION_HYPERVISOR, | |
532 | }; | |
533 | ||
534 | static enum srbds_mitigations srbds_mitigation __ro_after_init = SRBDS_MITIGATION_FULL; | |
535 | ||
536 | static const char * const srbds_strings[] = { | |
537 | [SRBDS_MITIGATION_OFF] = "Vulnerable", | |
538 | [SRBDS_MITIGATION_UCODE_NEEDED] = "Vulnerable: No microcode", | |
539 | [SRBDS_MITIGATION_FULL] = "Mitigation: Microcode", | |
540 | [SRBDS_MITIGATION_TSX_OFF] = "Mitigation: TSX disabled", | |
541 | [SRBDS_MITIGATION_HYPERVISOR] = "Unknown: Dependent on hypervisor status", | |
542 | }; | |
543 | ||
544 | static bool srbds_off; | |
545 | ||
546 | void update_srbds_msr(void) | |
547 | { | |
548 | u64 mcu_ctrl; | |
549 | ||
550 | if (!boot_cpu_has_bug(X86_BUG_SRBDS)) | |
551 | return; | |
552 | ||
553 | if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) | |
554 | return; | |
555 | ||
556 | if (srbds_mitigation == SRBDS_MITIGATION_UCODE_NEEDED) | |
557 | return; | |
558 | ||
559 | rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl); | |
560 | ||
561 | switch (srbds_mitigation) { | |
562 | case SRBDS_MITIGATION_OFF: | |
563 | case SRBDS_MITIGATION_TSX_OFF: | |
564 | mcu_ctrl |= RNGDS_MITG_DIS; | |
565 | break; | |
566 | case SRBDS_MITIGATION_FULL: | |
567 | mcu_ctrl &= ~RNGDS_MITG_DIS; | |
568 | break; | |
569 | default: | |
570 | break; | |
571 | } | |
572 | ||
573 | wrmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl); | |
574 | } | |
575 | ||
576 | static void __init srbds_select_mitigation(void) | |
577 | { | |
578 | u64 ia32_cap; | |
579 | ||
580 | if (!boot_cpu_has_bug(X86_BUG_SRBDS)) | |
581 | return; | |
582 | ||
583 | /* | |
584 | * Check to see if this is one of the MDS_NO systems supporting | |
585 | * TSX that are only exposed to SRBDS when TSX is enabled. | |
586 | */ | |
587 | ia32_cap = x86_read_arch_cap_msr(); | |
588 | if ((ia32_cap & ARCH_CAP_MDS_NO) && !boot_cpu_has(X86_FEATURE_RTM)) | |
589 | srbds_mitigation = SRBDS_MITIGATION_TSX_OFF; | |
590 | else if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) | |
591 | srbds_mitigation = SRBDS_MITIGATION_HYPERVISOR; | |
592 | else if (!boot_cpu_has(X86_FEATURE_SRBDS_CTRL)) | |
593 | srbds_mitigation = SRBDS_MITIGATION_UCODE_NEEDED; | |
594 | else if (cpu_mitigations_off() || srbds_off) | |
595 | srbds_mitigation = SRBDS_MITIGATION_OFF; | |
596 | ||
597 | update_srbds_msr(); | |
598 | pr_info("%s\n", srbds_strings[srbds_mitigation]); | |
599 | } | |
600 | ||
601 | static int __init srbds_parse_cmdline(char *str) | |
602 | { | |
603 | if (!str) | |
604 | return -EINVAL; | |
605 | ||
606 | if (!boot_cpu_has_bug(X86_BUG_SRBDS)) | |
607 | return 0; | |
608 | ||
609 | srbds_off = !strcmp(str, "off"); | |
610 | return 0; | |
611 | } | |
612 | early_param("srbds", srbds_parse_cmdline); | |
613 | ||
b5f06f64 BS |
614 | #undef pr_fmt |
615 | #define pr_fmt(fmt) "L1D Flush : " fmt | |
616 | ||
617 | enum l1d_flush_mitigations { | |
618 | L1D_FLUSH_OFF = 0, | |
619 | L1D_FLUSH_ON, | |
620 | }; | |
621 | ||
622 | static enum l1d_flush_mitigations l1d_flush_mitigation __initdata = L1D_FLUSH_OFF; | |
623 | ||
624 | static void __init l1d_flush_select_mitigation(void) | |
625 | { | |
626 | if (!l1d_flush_mitigation || !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) | |
627 | return; | |
628 | ||
629 | static_branch_enable(&switch_mm_cond_l1d_flush); | |
630 | pr_info("Conditional flush on switch_mm() enabled\n"); | |
631 | } | |
632 | ||
633 | static int __init l1d_flush_parse_cmdline(char *str) | |
634 | { | |
635 | if (!strcmp(str, "on")) | |
636 | l1d_flush_mitigation = L1D_FLUSH_ON; | |
637 | ||
638 | return 0; | |
639 | } | |
640 | early_param("l1d_flush", l1d_flush_parse_cmdline); | |
641 | ||
a2059825 JP |
642 | #undef pr_fmt |
643 | #define pr_fmt(fmt) "Spectre V1 : " fmt | |
644 | ||
645 | enum spectre_v1_mitigation { | |
646 | SPECTRE_V1_MITIGATION_NONE, | |
647 | SPECTRE_V1_MITIGATION_AUTO, | |
648 | }; | |
649 | ||
650 | static enum spectre_v1_mitigation spectre_v1_mitigation __ro_after_init = | |
651 | SPECTRE_V1_MITIGATION_AUTO; | |
652 | ||
653 | static const char * const spectre_v1_strings[] = { | |
654 | [SPECTRE_V1_MITIGATION_NONE] = "Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers", | |
655 | [SPECTRE_V1_MITIGATION_AUTO] = "Mitigation: usercopy/swapgs barriers and __user pointer sanitization", | |
656 | }; | |
657 | ||
a2059825 JP |
658 | /* |
659 | * Does SMAP provide full mitigation against speculative kernel access to | |
660 | * userspace? | |
661 | */ | |
662 | static bool smap_works_speculatively(void) | |
663 | { | |
664 | if (!boot_cpu_has(X86_FEATURE_SMAP)) | |
665 | return false; | |
666 | ||
667 | /* | |
668 | * On CPUs which are vulnerable to Meltdown, SMAP does not | |
669 | * prevent speculative access to user data in the L1 cache. | |
670 | * Consider SMAP to be non-functional as a mitigation on these | |
671 | * CPUs. | |
672 | */ | |
673 | if (boot_cpu_has(X86_BUG_CPU_MELTDOWN)) | |
674 | return false; | |
675 | ||
676 | return true; | |
677 | } | |
678 | ||
679 | static void __init spectre_v1_select_mitigation(void) | |
680 | { | |
681 | if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1) || cpu_mitigations_off()) { | |
682 | spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE; | |
683 | return; | |
684 | } | |
685 | ||
686 | if (spectre_v1_mitigation == SPECTRE_V1_MITIGATION_AUTO) { | |
687 | /* | |
688 | * With Spectre v1, a user can speculatively control either | |
689 | * path of a conditional swapgs with a user-controlled GS | |
690 | * value. The mitigation is to add lfences to both code paths. | |
691 | * | |
692 | * If FSGSBASE is enabled, the user can put a kernel address in | |
693 | * GS, in which case SMAP provides no protection. | |
694 | * | |
a2059825 JP |
695 | * If FSGSBASE is disabled, the user can only put a user space |
696 | * address in GS. That makes an attack harder, but still | |
697 | * possible if there's no SMAP protection. | |
698 | */ | |
978e1342 TL |
699 | if (boot_cpu_has(X86_FEATURE_FSGSBASE) || |
700 | !smap_works_speculatively()) { | |
a2059825 JP |
701 | /* |
702 | * Mitigation can be provided from SWAPGS itself or | |
703 | * PTI as the CR3 write in the Meltdown mitigation | |
704 | * is serializing. | |
705 | * | |
f36cf386 TG |
706 | * If neither is there, mitigate with an LFENCE to |
707 | * stop speculation through swapgs. | |
a2059825 | 708 | */ |
f36cf386 TG |
709 | if (boot_cpu_has_bug(X86_BUG_SWAPGS) && |
710 | !boot_cpu_has(X86_FEATURE_PTI)) | |
a2059825 JP |
711 | setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_USER); |
712 | ||
713 | /* | |
714 | * Enable lfences in the kernel entry (non-swapgs) | |
715 | * paths, to prevent user entry from speculatively | |
716 | * skipping swapgs. | |
717 | */ | |
718 | setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_KERNEL); | |
719 | } | |
720 | } | |
721 | ||
722 | pr_info("%s\n", spectre_v1_strings[spectre_v1_mitigation]); | |
723 | } | |
724 | ||
725 | static int __init nospectre_v1_cmdline(char *str) | |
726 | { | |
727 | spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE; | |
728 | return 0; | |
729 | } | |
730 | early_param("nospectre_v1", nospectre_v1_cmdline); | |
731 | ||
15d6b7aa TG |
732 | #undef pr_fmt |
733 | #define pr_fmt(fmt) "Spectre V2 : " fmt | |
734 | ||
735 | static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init = | |
736 | SPECTRE_V2_NONE; | |
737 | ||
21998a35 AS |
738 | static enum spectre_v2_user_mitigation spectre_v2_user_stibp __ro_after_init = |
739 | SPECTRE_V2_USER_NONE; | |
740 | static enum spectre_v2_user_mitigation spectre_v2_user_ibpb __ro_after_init = | |
fa1202ef TG |
741 | SPECTRE_V2_USER_NONE; |
742 | ||
e4f35891 | 743 | #ifdef CONFIG_RETPOLINE |
e383095c TG |
744 | static bool spectre_v2_bad_module; |
745 | ||
caf7501a AK |
746 | bool retpoline_module_ok(bool has_retpoline) |
747 | { | |
748 | if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline) | |
749 | return true; | |
750 | ||
e698dcdf | 751 | pr_err("System may be vulnerable to spectre v2\n"); |
caf7501a AK |
752 | spectre_v2_bad_module = true; |
753 | return false; | |
754 | } | |
e383095c TG |
755 | |
756 | static inline const char *spectre_v2_module_string(void) | |
757 | { | |
758 | return spectre_v2_bad_module ? " - vulnerable module loaded" : ""; | |
759 | } | |
760 | #else | |
761 | static inline const char *spectre_v2_module_string(void) { return ""; } | |
caf7501a | 762 | #endif |
da285121 | 763 | |
92cf97b2 | 764 | #define SPECTRE_V2_LFENCE_MSG "WARNING: LFENCE mitigation is not recommended for this CPU, data leaks possible!\n" |
534fcc9a | 765 | #define SPECTRE_V2_EIBRS_EBPF_MSG "WARNING: Unprivileged eBPF is enabled with eIBRS on, data leaks possible via Spectre v2 BHB attacks!\n" |
810643a7 | 766 | #define SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG "WARNING: Unprivileged eBPF is enabled with eIBRS+LFENCE mitigation and SMT, data leaks possible via Spectre v2 BHB attacks!\n" |
534fcc9a JP |
767 | |
768 | #ifdef CONFIG_BPF_SYSCALL | |
769 | void unpriv_ebpf_notify(int new_state) | |
770 | { | |
810643a7 JP |
771 | if (new_state) |
772 | return; | |
773 | ||
774 | /* Unprivileged eBPF is enabled */ | |
775 | ||
776 | switch (spectre_v2_enabled) { | |
777 | case SPECTRE_V2_EIBRS: | |
534fcc9a | 778 | pr_err(SPECTRE_V2_EIBRS_EBPF_MSG); |
810643a7 JP |
779 | break; |
780 | case SPECTRE_V2_EIBRS_LFENCE: | |
781 | if (sched_smt_active()) | |
782 | pr_err(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG); | |
783 | break; | |
784 | default: | |
785 | break; | |
786 | } | |
534fcc9a JP |
787 | } |
788 | #endif | |
789 | ||
da285121 DW |
790 | static inline bool match_option(const char *arg, int arglen, const char *opt) |
791 | { | |
792 | int len = strlen(opt); | |
793 | ||
794 | return len == arglen && !strncmp(arg, opt, len); | |
795 | } | |
796 | ||
15d6b7aa TG |
797 | /* The kernel command line selection for spectre v2 */ |
798 | enum spectre_v2_mitigation_cmd { | |
799 | SPECTRE_V2_CMD_NONE, | |
800 | SPECTRE_V2_CMD_AUTO, | |
801 | SPECTRE_V2_CMD_FORCE, | |
802 | SPECTRE_V2_CMD_RETPOLINE, | |
803 | SPECTRE_V2_CMD_RETPOLINE_GENERIC, | |
a24bd1da | 804 | SPECTRE_V2_CMD_RETPOLINE_LFENCE, |
a94faf3f PZ |
805 | SPECTRE_V2_CMD_EIBRS, |
806 | SPECTRE_V2_CMD_EIBRS_RETPOLINE, | |
807 | SPECTRE_V2_CMD_EIBRS_LFENCE, | |
15d6b7aa TG |
808 | }; |
809 | ||
fa1202ef TG |
810 | enum spectre_v2_user_cmd { |
811 | SPECTRE_V2_USER_CMD_NONE, | |
812 | SPECTRE_V2_USER_CMD_AUTO, | |
813 | SPECTRE_V2_USER_CMD_FORCE, | |
7cc765a6 | 814 | SPECTRE_V2_USER_CMD_PRCTL, |
55a97402 | 815 | SPECTRE_V2_USER_CMD_PRCTL_IBPB, |
6b3e64c2 | 816 | SPECTRE_V2_USER_CMD_SECCOMP, |
55a97402 | 817 | SPECTRE_V2_USER_CMD_SECCOMP_IBPB, |
fa1202ef TG |
818 | }; |
819 | ||
820 | static const char * const spectre_v2_user_strings[] = { | |
20c3a2c3 TL |
821 | [SPECTRE_V2_USER_NONE] = "User space: Vulnerable", |
822 | [SPECTRE_V2_USER_STRICT] = "User space: Mitigation: STIBP protection", | |
823 | [SPECTRE_V2_USER_STRICT_PREFERRED] = "User space: Mitigation: STIBP always-on protection", | |
824 | [SPECTRE_V2_USER_PRCTL] = "User space: Mitigation: STIBP via prctl", | |
825 | [SPECTRE_V2_USER_SECCOMP] = "User space: Mitigation: STIBP via seccomp and prctl", | |
fa1202ef TG |
826 | }; |
827 | ||
828 | static const struct { | |
829 | const char *option; | |
830 | enum spectre_v2_user_cmd cmd; | |
831 | bool secure; | |
1de7edbb | 832 | } v2_user_options[] __initconst = { |
55a97402 TG |
833 | { "auto", SPECTRE_V2_USER_CMD_AUTO, false }, |
834 | { "off", SPECTRE_V2_USER_CMD_NONE, false }, | |
835 | { "on", SPECTRE_V2_USER_CMD_FORCE, true }, | |
836 | { "prctl", SPECTRE_V2_USER_CMD_PRCTL, false }, | |
837 | { "prctl,ibpb", SPECTRE_V2_USER_CMD_PRCTL_IBPB, false }, | |
838 | { "seccomp", SPECTRE_V2_USER_CMD_SECCOMP, false }, | |
839 | { "seccomp,ibpb", SPECTRE_V2_USER_CMD_SECCOMP_IBPB, false }, | |
fa1202ef TG |
840 | }; |
841 | ||
842 | static void __init spec_v2_user_print_cond(const char *reason, bool secure) | |
843 | { | |
844 | if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure) | |
845 | pr_info("spectre_v2_user=%s forced on command line.\n", reason); | |
846 | } | |
847 | ||
848 | static enum spectre_v2_user_cmd __init | |
849 | spectre_v2_parse_user_cmdline(enum spectre_v2_mitigation_cmd v2_cmd) | |
850 | { | |
851 | char arg[20]; | |
852 | int ret, i; | |
853 | ||
854 | switch (v2_cmd) { | |
855 | case SPECTRE_V2_CMD_NONE: | |
856 | return SPECTRE_V2_USER_CMD_NONE; | |
857 | case SPECTRE_V2_CMD_FORCE: | |
858 | return SPECTRE_V2_USER_CMD_FORCE; | |
859 | default: | |
860 | break; | |
861 | } | |
862 | ||
863 | ret = cmdline_find_option(boot_command_line, "spectre_v2_user", | |
864 | arg, sizeof(arg)); | |
865 | if (ret < 0) | |
866 | return SPECTRE_V2_USER_CMD_AUTO; | |
867 | ||
868 | for (i = 0; i < ARRAY_SIZE(v2_user_options); i++) { | |
869 | if (match_option(arg, ret, v2_user_options[i].option)) { | |
870 | spec_v2_user_print_cond(v2_user_options[i].option, | |
871 | v2_user_options[i].secure); | |
872 | return v2_user_options[i].cmd; | |
873 | } | |
874 | } | |
875 | ||
876 | pr_err("Unknown user space protection option (%s). Switching to AUTO select\n", arg); | |
877 | return SPECTRE_V2_USER_CMD_AUTO; | |
878 | } | |
879 | ||
a94faf3f PZ |
880 | static inline bool spectre_v2_in_eibrs_mode(enum spectre_v2_mitigation mode) |
881 | { | |
882 | return (mode == SPECTRE_V2_EIBRS || | |
883 | mode == SPECTRE_V2_EIBRS_RETPOLINE || | |
884 | mode == SPECTRE_V2_EIBRS_LFENCE); | |
885 | } | |
886 | ||
fa1202ef TG |
887 | static void __init |
888 | spectre_v2_user_select_mitigation(enum spectre_v2_mitigation_cmd v2_cmd) | |
889 | { | |
890 | enum spectre_v2_user_mitigation mode = SPECTRE_V2_USER_NONE; | |
891 | bool smt_possible = IS_ENABLED(CONFIG_SMP); | |
55a97402 | 892 | enum spectre_v2_user_cmd cmd; |
fa1202ef TG |
893 | |
894 | if (!boot_cpu_has(X86_FEATURE_IBPB) && !boot_cpu_has(X86_FEATURE_STIBP)) | |
895 | return; | |
896 | ||
897 | if (cpu_smt_control == CPU_SMT_FORCE_DISABLED || | |
898 | cpu_smt_control == CPU_SMT_NOT_SUPPORTED) | |
899 | smt_possible = false; | |
900 | ||
55a97402 TG |
901 | cmd = spectre_v2_parse_user_cmdline(v2_cmd); |
902 | switch (cmd) { | |
fa1202ef TG |
903 | case SPECTRE_V2_USER_CMD_NONE: |
904 | goto set_mode; | |
905 | case SPECTRE_V2_USER_CMD_FORCE: | |
906 | mode = SPECTRE_V2_USER_STRICT; | |
907 | break; | |
7cc765a6 | 908 | case SPECTRE_V2_USER_CMD_PRCTL: |
55a97402 | 909 | case SPECTRE_V2_USER_CMD_PRCTL_IBPB: |
7cc765a6 TG |
910 | mode = SPECTRE_V2_USER_PRCTL; |
911 | break; | |
6b3e64c2 TG |
912 | case SPECTRE_V2_USER_CMD_AUTO: |
913 | case SPECTRE_V2_USER_CMD_SECCOMP: | |
55a97402 | 914 | case SPECTRE_V2_USER_CMD_SECCOMP_IBPB: |
6b3e64c2 TG |
915 | if (IS_ENABLED(CONFIG_SECCOMP)) |
916 | mode = SPECTRE_V2_USER_SECCOMP; | |
917 | else | |
918 | mode = SPECTRE_V2_USER_PRCTL; | |
919 | break; | |
fa1202ef TG |
920 | } |
921 | ||
922 | /* Initialize Indirect Branch Prediction Barrier */ | |
923 | if (boot_cpu_has(X86_FEATURE_IBPB)) { | |
924 | setup_force_cpu_cap(X86_FEATURE_USE_IBPB); | |
4c71a2b6 | 925 | |
33fc379d | 926 | spectre_v2_user_ibpb = mode; |
55a97402 TG |
927 | switch (cmd) { |
928 | case SPECTRE_V2_USER_CMD_FORCE: | |
929 | case SPECTRE_V2_USER_CMD_PRCTL_IBPB: | |
930 | case SPECTRE_V2_USER_CMD_SECCOMP_IBPB: | |
4c71a2b6 | 931 | static_branch_enable(&switch_mm_always_ibpb); |
33fc379d | 932 | spectre_v2_user_ibpb = SPECTRE_V2_USER_STRICT; |
4c71a2b6 | 933 | break; |
55a97402 TG |
934 | case SPECTRE_V2_USER_CMD_PRCTL: |
935 | case SPECTRE_V2_USER_CMD_AUTO: | |
936 | case SPECTRE_V2_USER_CMD_SECCOMP: | |
7cc765a6 TG |
937 | static_branch_enable(&switch_mm_cond_ibpb); |
938 | break; | |
4c71a2b6 TG |
939 | default: |
940 | break; | |
941 | } | |
942 | ||
943 | pr_info("mitigation: Enabling %s Indirect Branch Prediction Barrier\n", | |
55a97402 TG |
944 | static_key_enabled(&switch_mm_always_ibpb) ? |
945 | "always-on" : "conditional"); | |
fa1202ef TG |
946 | } |
947 | ||
21998a35 | 948 | /* |
a5ce9f2b | 949 | * If no STIBP, enhanced IBRS is enabled or SMT impossible, STIBP is not |
21998a35 AS |
950 | * required. |
951 | */ | |
a5ce9f2b BP |
952 | if (!boot_cpu_has(X86_FEATURE_STIBP) || |
953 | !smt_possible || | |
a94faf3f | 954 | spectre_v2_in_eibrs_mode(spectre_v2_enabled)) |
fa1202ef TG |
955 | return; |
956 | ||
7cc765a6 | 957 | /* |
21998a35 AS |
958 | * At this point, an STIBP mode other than "off" has been set. |
959 | * If STIBP support is not being forced, check if STIBP always-on | |
960 | * is preferred. | |
7cc765a6 | 961 | */ |
21998a35 AS |
962 | if (mode != SPECTRE_V2_USER_STRICT && |
963 | boot_cpu_has(X86_FEATURE_AMD_STIBP_ALWAYS_ON)) | |
964 | mode = SPECTRE_V2_USER_STRICT_PREFERRED; | |
965 | ||
21998a35 AS |
966 | spectre_v2_user_stibp = mode; |
967 | ||
fa1202ef | 968 | set_mode: |
21998a35 | 969 | pr_info("%s\n", spectre_v2_user_strings[mode]); |
fa1202ef TG |
970 | } |
971 | ||
8770709f | 972 | static const char * const spectre_v2_strings[] = { |
15d6b7aa | 973 | [SPECTRE_V2_NONE] = "Vulnerable", |
a24bd1da PZI |
974 | [SPECTRE_V2_RETPOLINE] = "Mitigation: Retpolines", |
975 | [SPECTRE_V2_LFENCE] = "Mitigation: LFENCE", | |
a94faf3f PZ |
976 | [SPECTRE_V2_EIBRS] = "Mitigation: Enhanced IBRS", |
977 | [SPECTRE_V2_EIBRS_LFENCE] = "Mitigation: Enhanced IBRS + LFENCE", | |
978 | [SPECTRE_V2_EIBRS_RETPOLINE] = "Mitigation: Enhanced IBRS + Retpolines", | |
15d6b7aa TG |
979 | }; |
980 | ||
9005c683 KA |
981 | static const struct { |
982 | const char *option; | |
983 | enum spectre_v2_mitigation_cmd cmd; | |
984 | bool secure; | |
1de7edbb | 985 | } mitigation_options[] __initconst = { |
15d6b7aa TG |
986 | { "off", SPECTRE_V2_CMD_NONE, false }, |
987 | { "on", SPECTRE_V2_CMD_FORCE, true }, | |
988 | { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false }, | |
a24bd1da PZI |
989 | { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_LFENCE, false }, |
990 | { "retpoline,lfence", SPECTRE_V2_CMD_RETPOLINE_LFENCE, false }, | |
15d6b7aa | 991 | { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false }, |
a94faf3f PZ |
992 | { "eibrs", SPECTRE_V2_CMD_EIBRS, false }, |
993 | { "eibrs,lfence", SPECTRE_V2_CMD_EIBRS_LFENCE, false }, | |
994 | { "eibrs,retpoline", SPECTRE_V2_CMD_EIBRS_RETPOLINE, false }, | |
15d6b7aa | 995 | { "auto", SPECTRE_V2_CMD_AUTO, false }, |
9005c683 KA |
996 | }; |
997 | ||
495d470e | 998 | static void __init spec_v2_print_cond(const char *reason, bool secure) |
15d6b7aa | 999 | { |
495d470e | 1000 | if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure) |
15d6b7aa TG |
1001 | pr_info("%s selected on command line.\n", reason); |
1002 | } | |
1003 | ||
da285121 DW |
1004 | static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void) |
1005 | { | |
15d6b7aa | 1006 | enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO; |
da285121 | 1007 | char arg[20]; |
9005c683 | 1008 | int ret, i; |
9005c683 | 1009 | |
d68be4c4 JP |
1010 | if (cmdline_find_option_bool(boot_command_line, "nospectre_v2") || |
1011 | cpu_mitigations_off()) | |
9005c683 | 1012 | return SPECTRE_V2_CMD_NONE; |
9005c683 | 1013 | |
24848509 TC |
1014 | ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg)); |
1015 | if (ret < 0) | |
1016 | return SPECTRE_V2_CMD_AUTO; | |
1017 | ||
1018 | for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) { | |
1019 | if (!match_option(arg, ret, mitigation_options[i].option)) | |
1020 | continue; | |
1021 | cmd = mitigation_options[i].cmd; | |
1022 | break; | |
1023 | } | |
1024 | ||
1025 | if (i >= ARRAY_SIZE(mitigation_options)) { | |
1026 | pr_err("unknown option (%s). Switching to AUTO select\n", arg); | |
1027 | return SPECTRE_V2_CMD_AUTO; | |
da285121 DW |
1028 | } |
1029 | ||
9005c683 | 1030 | if ((cmd == SPECTRE_V2_CMD_RETPOLINE || |
a24bd1da | 1031 | cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE || |
a94faf3f PZ |
1032 | cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC || |
1033 | cmd == SPECTRE_V2_CMD_EIBRS_LFENCE || | |
1034 | cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) && | |
9005c683 | 1035 | !IS_ENABLED(CONFIG_RETPOLINE)) { |
a94faf3f PZ |
1036 | pr_err("%s selected but not compiled in. Switching to AUTO select\n", |
1037 | mitigation_options[i].option); | |
1038 | return SPECTRE_V2_CMD_AUTO; | |
1039 | } | |
1040 | ||
1041 | if ((cmd == SPECTRE_V2_CMD_EIBRS || | |
1042 | cmd == SPECTRE_V2_CMD_EIBRS_LFENCE || | |
1043 | cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) && | |
1044 | !boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) { | |
1045 | pr_err("%s selected but CPU doesn't have eIBRS. Switching to AUTO select\n", | |
1046 | mitigation_options[i].option); | |
da285121 | 1047 | return SPECTRE_V2_CMD_AUTO; |
9005c683 KA |
1048 | } |
1049 | ||
a94faf3f PZ |
1050 | if ((cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE || |
1051 | cmd == SPECTRE_V2_CMD_EIBRS_LFENCE) && | |
a24bd1da | 1052 | !boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) { |
a94faf3f PZ |
1053 | pr_err("%s selected, but CPU doesn't have a serializing LFENCE. Switching to AUTO select\n", |
1054 | mitigation_options[i].option); | |
a24bd1da PZI |
1055 | return SPECTRE_V2_CMD_AUTO; |
1056 | } | |
1057 | ||
495d470e TG |
1058 | spec_v2_print_cond(mitigation_options[i].option, |
1059 | mitigation_options[i].secure); | |
9005c683 | 1060 | return cmd; |
da285121 DW |
1061 | } |
1062 | ||
a94faf3f PZ |
1063 | static enum spectre_v2_mitigation __init spectre_v2_select_retpoline(void) |
1064 | { | |
1065 | if (!IS_ENABLED(CONFIG_RETPOLINE)) { | |
1066 | pr_err("Kernel not compiled with retpoline; no mitigation available!"); | |
1067 | return SPECTRE_V2_NONE; | |
1068 | } | |
1069 | ||
a94faf3f PZ |
1070 | return SPECTRE_V2_RETPOLINE; |
1071 | } | |
1072 | ||
da285121 DW |
1073 | static void __init spectre_v2_select_mitigation(void) |
1074 | { | |
1075 | enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline(); | |
1076 | enum spectre_v2_mitigation mode = SPECTRE_V2_NONE; | |
1077 | ||
1078 | /* | |
1079 | * If the CPU is not affected and the command line mode is NONE or AUTO | |
1080 | * then nothing to do. | |
1081 | */ | |
1082 | if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) && | |
1083 | (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO)) | |
1084 | return; | |
1085 | ||
1086 | switch (cmd) { | |
1087 | case SPECTRE_V2_CMD_NONE: | |
1088 | return; | |
1089 | ||
1090 | case SPECTRE_V2_CMD_FORCE: | |
da285121 | 1091 | case SPECTRE_V2_CMD_AUTO: |
706d5168 | 1092 | if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) { |
a94faf3f PZ |
1093 | mode = SPECTRE_V2_EIBRS; |
1094 | break; | |
706d5168 | 1095 | } |
a94faf3f PZ |
1096 | |
1097 | mode = spectre_v2_select_retpoline(); | |
9471eee9 | 1098 | break; |
a94faf3f | 1099 | |
a24bd1da | 1100 | case SPECTRE_V2_CMD_RETPOLINE_LFENCE: |
92cf97b2 | 1101 | pr_err(SPECTRE_V2_LFENCE_MSG); |
a94faf3f | 1102 | mode = SPECTRE_V2_LFENCE; |
da285121 | 1103 | break; |
a94faf3f | 1104 | |
da285121 | 1105 | case SPECTRE_V2_CMD_RETPOLINE_GENERIC: |
a94faf3f | 1106 | mode = SPECTRE_V2_RETPOLINE; |
da285121 | 1107 | break; |
a94faf3f | 1108 | |
da285121 | 1109 | case SPECTRE_V2_CMD_RETPOLINE: |
a94faf3f PZ |
1110 | mode = spectre_v2_select_retpoline(); |
1111 | break; | |
1112 | ||
1113 | case SPECTRE_V2_CMD_EIBRS: | |
1114 | mode = SPECTRE_V2_EIBRS; | |
1115 | break; | |
1116 | ||
1117 | case SPECTRE_V2_CMD_EIBRS_LFENCE: | |
1118 | mode = SPECTRE_V2_EIBRS_LFENCE; | |
1119 | break; | |
1120 | ||
1121 | case SPECTRE_V2_CMD_EIBRS_RETPOLINE: | |
1122 | mode = SPECTRE_V2_EIBRS_RETPOLINE; | |
da285121 DW |
1123 | break; |
1124 | } | |
da285121 | 1125 | |
534fcc9a JP |
1126 | if (mode == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled()) |
1127 | pr_err(SPECTRE_V2_EIBRS_EBPF_MSG); | |
1128 | ||
a94faf3f PZ |
1129 | if (spectre_v2_in_eibrs_mode(mode)) { |
1130 | /* Force it so VMEXIT will restore correctly */ | |
1131 | x86_spec_ctrl_base |= SPEC_CTRL_IBRS; | |
1132 | wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base); | |
1133 | } | |
1134 | ||
1135 | switch (mode) { | |
1136 | case SPECTRE_V2_NONE: | |
1137 | case SPECTRE_V2_EIBRS: | |
1138 | break; | |
1139 | ||
1140 | case SPECTRE_V2_LFENCE: | |
1141 | case SPECTRE_V2_EIBRS_LFENCE: | |
a24bd1da | 1142 | setup_force_cpu_cap(X86_FEATURE_RETPOLINE_LFENCE); |
a94faf3f PZ |
1143 | fallthrough; |
1144 | ||
1145 | case SPECTRE_V2_RETPOLINE: | |
1146 | case SPECTRE_V2_EIBRS_RETPOLINE: | |
da285121 | 1147 | setup_force_cpu_cap(X86_FEATURE_RETPOLINE); |
a94faf3f | 1148 | break; |
da285121 DW |
1149 | } |
1150 | ||
1151 | spectre_v2_enabled = mode; | |
1152 | pr_info("%s\n", spectre_v2_strings[mode]); | |
c995efd5 DW |
1153 | |
1154 | /* | |
fdf82a78 JK |
1155 | * If spectre v2 protection has been enabled, unconditionally fill |
1156 | * RSB during a context switch; this protects against two independent | |
1157 | * issues: | |
c995efd5 | 1158 | * |
fdf82a78 JK |
1159 | * - RSB underflow (and switch to BTB) on Skylake+ |
1160 | * - SpectreRSB variant of spectre v2 on X86_BUG_SPECTRE_V2 CPUs | |
c995efd5 | 1161 | */ |
fdf82a78 JK |
1162 | setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW); |
1163 | pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n"); | |
20ffa1ca | 1164 | |
dd84441a DW |
1165 | /* |
1166 | * Retpoline means the kernel is safe because it has no indirect | |
706d5168 SP |
1167 | * branches. Enhanced IBRS protects firmware too, so, enable restricted |
1168 | * speculation around firmware calls only when Enhanced IBRS isn't | |
1169 | * supported. | |
1170 | * | |
1171 | * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because | |
1172 | * the user might select retpoline on the kernel command line and if | |
1173 | * the CPU supports Enhanced IBRS, kernel might un-intentionally not | |
1174 | * enable IBRS around firmware calls. | |
dd84441a | 1175 | */ |
a94faf3f | 1176 | if (boot_cpu_has(X86_FEATURE_IBRS) && !spectre_v2_in_eibrs_mode(mode)) { |
dd84441a DW |
1177 | setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW); |
1178 | pr_info("Enabling Restricted Speculation for firmware calls\n"); | |
1179 | } | |
53c613fe | 1180 | |
fa1202ef TG |
1181 | /* Set up IBPB and STIBP depending on the general spectre V2 command */ |
1182 | spectre_v2_user_select_mitigation(cmd); | |
da285121 DW |
1183 | } |
1184 | ||
6893a959 | 1185 | static void update_stibp_msr(void * __unused) |
15d6b7aa | 1186 | { |
6893a959 | 1187 | wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base); |
15d6b7aa TG |
1188 | } |
1189 | ||
6893a959 TG |
1190 | /* Update x86_spec_ctrl_base in case SMT state changed. */ |
1191 | static void update_stibp_strict(void) | |
15d6b7aa | 1192 | { |
6893a959 TG |
1193 | u64 mask = x86_spec_ctrl_base & ~SPEC_CTRL_STIBP; |
1194 | ||
1195 | if (sched_smt_active()) | |
1196 | mask |= SPEC_CTRL_STIBP; | |
1197 | ||
1198 | if (mask == x86_spec_ctrl_base) | |
1199 | return; | |
1200 | ||
1201 | pr_info("Update user space SMT mitigation: STIBP %s\n", | |
1202 | mask & SPEC_CTRL_STIBP ? "always-on" : "off"); | |
1203 | x86_spec_ctrl_base = mask; | |
1204 | on_each_cpu(update_stibp_msr, NULL, 1); | |
15d6b7aa TG |
1205 | } |
1206 | ||
7cc765a6 TG |
1207 | /* Update the static key controlling the evaluation of TIF_SPEC_IB */ |
1208 | static void update_indir_branch_cond(void) | |
1209 | { | |
1210 | if (sched_smt_active()) | |
1211 | static_branch_enable(&switch_to_cond_stibp); | |
1212 | else | |
1213 | static_branch_disable(&switch_to_cond_stibp); | |
1214 | } | |
1215 | ||
39226ef0 JP |
1216 | #undef pr_fmt |
1217 | #define pr_fmt(fmt) fmt | |
1218 | ||
bc124170 TG |
1219 | /* Update the static key controlling the MDS CPU buffer clear in idle */ |
1220 | static void update_mds_branch_idle(void) | |
1221 | { | |
1222 | /* | |
1223 | * Enable the idle clearing if SMT is active on CPUs which are | |
1224 | * affected only by MSBDS and not any other MDS variant. | |
1225 | * | |
1226 | * The other variants cannot be mitigated when SMT is enabled, so | |
1227 | * clearing the buffers on idle just to prevent the Store Buffer | |
1228 | * repartitioning leak would be a window dressing exercise. | |
1229 | */ | |
1230 | if (!boot_cpu_has_bug(X86_BUG_MSBDS_ONLY)) | |
1231 | return; | |
1232 | ||
1233 | if (sched_smt_active()) | |
1234 | static_branch_enable(&mds_idle_clear); | |
1235 | else | |
1236 | static_branch_disable(&mds_idle_clear); | |
1237 | } | |
1238 | ||
39226ef0 | 1239 | #define MDS_MSG_SMT "MDS CPU bug present and SMT on, data leak possible. See https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/mds.html for more details.\n" |
1b42f017 | 1240 | #define TAA_MSG_SMT "TAA CPU bug present and SMT on, data leak possible. See https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/tsx_async_abort.html for more details.\n" |
39226ef0 | 1241 | |
9c92374b | 1242 | void cpu_bugs_smt_update(void) |
15d6b7aa | 1243 | { |
15d6b7aa TG |
1244 | mutex_lock(&spec_ctrl_mutex); |
1245 | ||
810643a7 JP |
1246 | if (sched_smt_active() && unprivileged_ebpf_enabled() && |
1247 | spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE) | |
1248 | pr_warn_once(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG); | |
1249 | ||
21998a35 | 1250 | switch (spectre_v2_user_stibp) { |
6893a959 TG |
1251 | case SPECTRE_V2_USER_NONE: |
1252 | break; | |
1253 | case SPECTRE_V2_USER_STRICT: | |
20c3a2c3 | 1254 | case SPECTRE_V2_USER_STRICT_PREFERRED: |
6893a959 TG |
1255 | update_stibp_strict(); |
1256 | break; | |
9137bb27 | 1257 | case SPECTRE_V2_USER_PRCTL: |
6b3e64c2 | 1258 | case SPECTRE_V2_USER_SECCOMP: |
7cc765a6 | 1259 | update_indir_branch_cond(); |
9137bb27 | 1260 | break; |
15d6b7aa | 1261 | } |
6893a959 | 1262 | |
22dd8365 TG |
1263 | switch (mds_mitigation) { |
1264 | case MDS_MITIGATION_FULL: | |
1265 | case MDS_MITIGATION_VMWERV: | |
39226ef0 JP |
1266 | if (sched_smt_active() && !boot_cpu_has(X86_BUG_MSBDS_ONLY)) |
1267 | pr_warn_once(MDS_MSG_SMT); | |
bc124170 | 1268 | update_mds_branch_idle(); |
22dd8365 TG |
1269 | break; |
1270 | case MDS_MITIGATION_OFF: | |
1271 | break; | |
1272 | } | |
bc124170 | 1273 | |
1b42f017 PG |
1274 | switch (taa_mitigation) { |
1275 | case TAA_MITIGATION_VERW: | |
1276 | case TAA_MITIGATION_UCODE_NEEDED: | |
1277 | if (sched_smt_active()) | |
1278 | pr_warn_once(TAA_MSG_SMT); | |
1279 | break; | |
1280 | case TAA_MITIGATION_TSX_DISABLED: | |
1281 | case TAA_MITIGATION_OFF: | |
1282 | break; | |
1283 | } | |
1284 | ||
15d6b7aa TG |
1285 | mutex_unlock(&spec_ctrl_mutex); |
1286 | } | |
1287 | ||
24f7fc83 KRW |
1288 | #undef pr_fmt |
1289 | #define pr_fmt(fmt) "Speculative Store Bypass: " fmt | |
1290 | ||
f9544b2b | 1291 | static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE; |
24f7fc83 KRW |
1292 | |
1293 | /* The kernel command line selection */ | |
1294 | enum ssb_mitigation_cmd { | |
1295 | SPEC_STORE_BYPASS_CMD_NONE, | |
1296 | SPEC_STORE_BYPASS_CMD_AUTO, | |
1297 | SPEC_STORE_BYPASS_CMD_ON, | |
a73ec77e | 1298 | SPEC_STORE_BYPASS_CMD_PRCTL, |
f21b53b2 | 1299 | SPEC_STORE_BYPASS_CMD_SECCOMP, |
24f7fc83 KRW |
1300 | }; |
1301 | ||
8770709f | 1302 | static const char * const ssb_strings[] = { |
24f7fc83 | 1303 | [SPEC_STORE_BYPASS_NONE] = "Vulnerable", |
a73ec77e | 1304 | [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass disabled", |
f21b53b2 KC |
1305 | [SPEC_STORE_BYPASS_PRCTL] = "Mitigation: Speculative Store Bypass disabled via prctl", |
1306 | [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp", | |
24f7fc83 KRW |
1307 | }; |
1308 | ||
1309 | static const struct { | |
1310 | const char *option; | |
1311 | enum ssb_mitigation_cmd cmd; | |
1de7edbb | 1312 | } ssb_mitigation_options[] __initconst = { |
f21b53b2 KC |
1313 | { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */ |
1314 | { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */ | |
1315 | { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */ | |
1316 | { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative Store Bypass via prctl */ | |
1317 | { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */ | |
24f7fc83 KRW |
1318 | }; |
1319 | ||
1320 | static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void) | |
1321 | { | |
1322 | enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO; | |
1323 | char arg[20]; | |
1324 | int ret, i; | |
1325 | ||
d68be4c4 JP |
1326 | if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable") || |
1327 | cpu_mitigations_off()) { | |
24f7fc83 KRW |
1328 | return SPEC_STORE_BYPASS_CMD_NONE; |
1329 | } else { | |
1330 | ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable", | |
1331 | arg, sizeof(arg)); | |
1332 | if (ret < 0) | |
1333 | return SPEC_STORE_BYPASS_CMD_AUTO; | |
1334 | ||
1335 | for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) { | |
1336 | if (!match_option(arg, ret, ssb_mitigation_options[i].option)) | |
1337 | continue; | |
1338 | ||
1339 | cmd = ssb_mitigation_options[i].cmd; | |
1340 | break; | |
1341 | } | |
1342 | ||
1343 | if (i >= ARRAY_SIZE(ssb_mitigation_options)) { | |
1344 | pr_err("unknown option (%s). Switching to AUTO select\n", arg); | |
1345 | return SPEC_STORE_BYPASS_CMD_AUTO; | |
1346 | } | |
1347 | } | |
1348 | ||
1349 | return cmd; | |
1350 | } | |
1351 | ||
d66d8ff3 | 1352 | static enum ssb_mitigation __init __ssb_select_mitigation(void) |
24f7fc83 KRW |
1353 | { |
1354 | enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE; | |
1355 | enum ssb_mitigation_cmd cmd; | |
1356 | ||
9f65fb29 | 1357 | if (!boot_cpu_has(X86_FEATURE_SSBD)) |
24f7fc83 KRW |
1358 | return mode; |
1359 | ||
1360 | cmd = ssb_parse_cmdline(); | |
1361 | if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) && | |
1362 | (cmd == SPEC_STORE_BYPASS_CMD_NONE || | |
1363 | cmd == SPEC_STORE_BYPASS_CMD_AUTO)) | |
1364 | return mode; | |
1365 | ||
1366 | switch (cmd) { | |
1367 | case SPEC_STORE_BYPASS_CMD_AUTO: | |
f21b53b2 KC |
1368 | case SPEC_STORE_BYPASS_CMD_SECCOMP: |
1369 | /* | |
1370 | * Choose prctl+seccomp as the default mode if seccomp is | |
1371 | * enabled. | |
1372 | */ | |
1373 | if (IS_ENABLED(CONFIG_SECCOMP)) | |
1374 | mode = SPEC_STORE_BYPASS_SECCOMP; | |
1375 | else | |
1376 | mode = SPEC_STORE_BYPASS_PRCTL; | |
a73ec77e | 1377 | break; |
24f7fc83 KRW |
1378 | case SPEC_STORE_BYPASS_CMD_ON: |
1379 | mode = SPEC_STORE_BYPASS_DISABLE; | |
1380 | break; | |
a73ec77e TG |
1381 | case SPEC_STORE_BYPASS_CMD_PRCTL: |
1382 | mode = SPEC_STORE_BYPASS_PRCTL; | |
1383 | break; | |
24f7fc83 KRW |
1384 | case SPEC_STORE_BYPASS_CMD_NONE: |
1385 | break; | |
1386 | } | |
1387 | ||
c1f7fec1 AJ |
1388 | /* |
1389 | * If SSBD is controlled by the SPEC_CTRL MSR, then set the proper | |
1390 | * bit in the mask to allow guests to use the mitigation even in the | |
1391 | * case where the host does not enable it. | |
1392 | */ | |
1393 | if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) || | |
1394 | static_cpu_has(X86_FEATURE_AMD_SSBD)) { | |
1395 | x86_spec_ctrl_mask |= SPEC_CTRL_SSBD; | |
1396 | } | |
1397 | ||
77243971 KRW |
1398 | /* |
1399 | * We have three CPU feature flags that are in play here: | |
1400 | * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible. | |
9f65fb29 | 1401 | * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass |
77243971 KRW |
1402 | * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation |
1403 | */ | |
a73ec77e | 1404 | if (mode == SPEC_STORE_BYPASS_DISABLE) { |
24f7fc83 | 1405 | setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE); |
77243971 | 1406 | /* |
6ac2f49e KRW |
1407 | * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may |
1408 | * use a completely different MSR and bit dependent on family. | |
77243971 | 1409 | */ |
612bc3b3 TL |
1410 | if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) && |
1411 | !static_cpu_has(X86_FEATURE_AMD_SSBD)) { | |
108fab4b | 1412 | x86_amd_ssb_disable(); |
612bc3b3 | 1413 | } else { |
9f65fb29 | 1414 | x86_spec_ctrl_base |= SPEC_CTRL_SSBD; |
4b59bdb5 | 1415 | wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base); |
77243971 KRW |
1416 | } |
1417 | } | |
1418 | ||
24f7fc83 KRW |
1419 | return mode; |
1420 | } | |
1421 | ||
ffed645e | 1422 | static void ssb_select_mitigation(void) |
24f7fc83 KRW |
1423 | { |
1424 | ssb_mode = __ssb_select_mitigation(); | |
1425 | ||
1426 | if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS)) | |
1427 | pr_info("%s\n", ssb_strings[ssb_mode]); | |
1428 | } | |
1429 | ||
da285121 | 1430 | #undef pr_fmt |
f21b53b2 | 1431 | #define pr_fmt(fmt) "Speculation prctl: " fmt |
da285121 | 1432 | |
6d991ba5 | 1433 | static void task_update_spec_tif(struct task_struct *tsk) |
a73ec77e | 1434 | { |
6d991ba5 TG |
1435 | /* Force the update of the real TIF bits */ |
1436 | set_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE); | |
e6da8bb6 TG |
1437 | |
1438 | /* | |
1439 | * Immediately update the speculation control MSRs for the current | |
1440 | * task, but for a non-current task delay setting the CPU | |
1441 | * mitigation until it is scheduled next. | |
1442 | * | |
1443 | * This can only happen for SECCOMP mitigation. For PRCTL it's | |
1444 | * always the current task. | |
1445 | */ | |
6d991ba5 | 1446 | if (tsk == current) |
e6da8bb6 TG |
1447 | speculation_ctrl_update_current(); |
1448 | } | |
1449 | ||
e893bb1b BS |
1450 | static int l1d_flush_prctl_set(struct task_struct *task, unsigned long ctrl) |
1451 | { | |
1452 | ||
1453 | if (!static_branch_unlikely(&switch_mm_cond_l1d_flush)) | |
1454 | return -EPERM; | |
1455 | ||
1456 | switch (ctrl) { | |
1457 | case PR_SPEC_ENABLE: | |
1458 | set_ti_thread_flag(&task->thread_info, TIF_SPEC_L1D_FLUSH); | |
1459 | return 0; | |
1460 | case PR_SPEC_DISABLE: | |
1461 | clear_ti_thread_flag(&task->thread_info, TIF_SPEC_L1D_FLUSH); | |
1462 | return 0; | |
1463 | default: | |
1464 | return -ERANGE; | |
1465 | } | |
1466 | } | |
1467 | ||
e6da8bb6 TG |
1468 | static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl) |
1469 | { | |
f21b53b2 KC |
1470 | if (ssb_mode != SPEC_STORE_BYPASS_PRCTL && |
1471 | ssb_mode != SPEC_STORE_BYPASS_SECCOMP) | |
a73ec77e TG |
1472 | return -ENXIO; |
1473 | ||
356e4bff TG |
1474 | switch (ctrl) { |
1475 | case PR_SPEC_ENABLE: | |
1476 | /* If speculation is force disabled, enable is not allowed */ | |
1477 | if (task_spec_ssb_force_disable(task)) | |
1478 | return -EPERM; | |
1479 | task_clear_spec_ssb_disable(task); | |
71368af9 | 1480 | task_clear_spec_ssb_noexec(task); |
6d991ba5 | 1481 | task_update_spec_tif(task); |
356e4bff TG |
1482 | break; |
1483 | case PR_SPEC_DISABLE: | |
1484 | task_set_spec_ssb_disable(task); | |
71368af9 | 1485 | task_clear_spec_ssb_noexec(task); |
6d991ba5 | 1486 | task_update_spec_tif(task); |
356e4bff TG |
1487 | break; |
1488 | case PR_SPEC_FORCE_DISABLE: | |
1489 | task_set_spec_ssb_disable(task); | |
1490 | task_set_spec_ssb_force_disable(task); | |
71368af9 WL |
1491 | task_clear_spec_ssb_noexec(task); |
1492 | task_update_spec_tif(task); | |
1493 | break; | |
1494 | case PR_SPEC_DISABLE_NOEXEC: | |
1495 | if (task_spec_ssb_force_disable(task)) | |
1496 | return -EPERM; | |
1497 | task_set_spec_ssb_disable(task); | |
1498 | task_set_spec_ssb_noexec(task); | |
6d991ba5 | 1499 | task_update_spec_tif(task); |
356e4bff TG |
1500 | break; |
1501 | default: | |
1502 | return -ERANGE; | |
1503 | } | |
a73ec77e TG |
1504 | return 0; |
1505 | } | |
1506 | ||
1978b3a5 AM |
1507 | static bool is_spec_ib_user_controlled(void) |
1508 | { | |
1509 | return spectre_v2_user_ibpb == SPECTRE_V2_USER_PRCTL || | |
1510 | spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP || | |
1511 | spectre_v2_user_stibp == SPECTRE_V2_USER_PRCTL || | |
1512 | spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP; | |
1513 | } | |
1514 | ||
9137bb27 TG |
1515 | static int ib_prctl_set(struct task_struct *task, unsigned long ctrl) |
1516 | { | |
1517 | switch (ctrl) { | |
1518 | case PR_SPEC_ENABLE: | |
21998a35 AS |
1519 | if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE && |
1520 | spectre_v2_user_stibp == SPECTRE_V2_USER_NONE) | |
9137bb27 | 1521 | return 0; |
1978b3a5 | 1522 | |
9137bb27 | 1523 | /* |
1978b3a5 AM |
1524 | * With strict mode for both IBPB and STIBP, the instruction |
1525 | * code paths avoid checking this task flag and instead, | |
1526 | * unconditionally run the instruction. However, STIBP and IBPB | |
1527 | * are independent and either can be set to conditionally | |
1528 | * enabled regardless of the mode of the other. | |
1529 | * | |
1530 | * If either is set to conditional, allow the task flag to be | |
1531 | * updated, unless it was force-disabled by a previous prctl | |
1532 | * call. Currently, this is possible on an AMD CPU which has the | |
1533 | * feature X86_FEATURE_AMD_STIBP_ALWAYS_ON. In this case, if the | |
1534 | * kernel is booted with 'spectre_v2_user=seccomp', then | |
1535 | * spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP and | |
1536 | * spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED. | |
9137bb27 | 1537 | */ |
1978b3a5 | 1538 | if (!is_spec_ib_user_controlled() || |
4d8df8cb | 1539 | task_spec_ib_force_disable(task)) |
9137bb27 | 1540 | return -EPERM; |
1978b3a5 | 1541 | |
9137bb27 TG |
1542 | task_clear_spec_ib_disable(task); |
1543 | task_update_spec_tif(task); | |
1544 | break; | |
1545 | case PR_SPEC_DISABLE: | |
1546 | case PR_SPEC_FORCE_DISABLE: | |
1547 | /* | |
1548 | * Indirect branch speculation is always allowed when | |
1549 | * mitigation is force disabled. | |
1550 | */ | |
21998a35 AS |
1551 | if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE && |
1552 | spectre_v2_user_stibp == SPECTRE_V2_USER_NONE) | |
9137bb27 | 1553 | return -EPERM; |
1978b3a5 AM |
1554 | |
1555 | if (!is_spec_ib_user_controlled()) | |
9137bb27 | 1556 | return 0; |
1978b3a5 | 1557 | |
9137bb27 TG |
1558 | task_set_spec_ib_disable(task); |
1559 | if (ctrl == PR_SPEC_FORCE_DISABLE) | |
1560 | task_set_spec_ib_force_disable(task); | |
1561 | task_update_spec_tif(task); | |
1562 | break; | |
1563 | default: | |
1564 | return -ERANGE; | |
1565 | } | |
1566 | return 0; | |
1567 | } | |
1568 | ||
8bf37d8c TG |
1569 | int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which, |
1570 | unsigned long ctrl) | |
1571 | { | |
1572 | switch (which) { | |
1573 | case PR_SPEC_STORE_BYPASS: | |
1574 | return ssb_prctl_set(task, ctrl); | |
9137bb27 TG |
1575 | case PR_SPEC_INDIRECT_BRANCH: |
1576 | return ib_prctl_set(task, ctrl); | |
e893bb1b BS |
1577 | case PR_SPEC_L1D_FLUSH: |
1578 | return l1d_flush_prctl_set(task, ctrl); | |
8bf37d8c TG |
1579 | default: |
1580 | return -ENODEV; | |
1581 | } | |
1582 | } | |
1583 | ||
1584 | #ifdef CONFIG_SECCOMP | |
1585 | void arch_seccomp_spec_mitigate(struct task_struct *task) | |
1586 | { | |
f21b53b2 KC |
1587 | if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP) |
1588 | ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE); | |
21998a35 AS |
1589 | if (spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP || |
1590 | spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP) | |
6b3e64c2 | 1591 | ib_prctl_set(task, PR_SPEC_FORCE_DISABLE); |
8bf37d8c TG |
1592 | } |
1593 | #endif | |
1594 | ||
e893bb1b BS |
1595 | static int l1d_flush_prctl_get(struct task_struct *task) |
1596 | { | |
1597 | if (!static_branch_unlikely(&switch_mm_cond_l1d_flush)) | |
1598 | return PR_SPEC_FORCE_DISABLE; | |
1599 | ||
1600 | if (test_ti_thread_flag(&task->thread_info, TIF_SPEC_L1D_FLUSH)) | |
1601 | return PR_SPEC_PRCTL | PR_SPEC_ENABLE; | |
1602 | else | |
1603 | return PR_SPEC_PRCTL | PR_SPEC_DISABLE; | |
1604 | } | |
1605 | ||
7bbf1373 | 1606 | static int ssb_prctl_get(struct task_struct *task) |
a73ec77e TG |
1607 | { |
1608 | switch (ssb_mode) { | |
1609 | case SPEC_STORE_BYPASS_DISABLE: | |
1610 | return PR_SPEC_DISABLE; | |
f21b53b2 | 1611 | case SPEC_STORE_BYPASS_SECCOMP: |
a73ec77e | 1612 | case SPEC_STORE_BYPASS_PRCTL: |
356e4bff TG |
1613 | if (task_spec_ssb_force_disable(task)) |
1614 | return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE; | |
71368af9 WL |
1615 | if (task_spec_ssb_noexec(task)) |
1616 | return PR_SPEC_PRCTL | PR_SPEC_DISABLE_NOEXEC; | |
356e4bff | 1617 | if (task_spec_ssb_disable(task)) |
a73ec77e TG |
1618 | return PR_SPEC_PRCTL | PR_SPEC_DISABLE; |
1619 | return PR_SPEC_PRCTL | PR_SPEC_ENABLE; | |
1620 | default: | |
1621 | if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS)) | |
1622 | return PR_SPEC_ENABLE; | |
1623 | return PR_SPEC_NOT_AFFECTED; | |
1624 | } | |
1625 | } | |
1626 | ||
9137bb27 TG |
1627 | static int ib_prctl_get(struct task_struct *task) |
1628 | { | |
1629 | if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2)) | |
1630 | return PR_SPEC_NOT_AFFECTED; | |
1631 | ||
21998a35 AS |
1632 | if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE && |
1633 | spectre_v2_user_stibp == SPECTRE_V2_USER_NONE) | |
9137bb27 | 1634 | return PR_SPEC_ENABLE; |
1978b3a5 | 1635 | else if (is_spec_ib_user_controlled()) { |
9137bb27 TG |
1636 | if (task_spec_ib_force_disable(task)) |
1637 | return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE; | |
1638 | if (task_spec_ib_disable(task)) | |
1639 | return PR_SPEC_PRCTL | PR_SPEC_DISABLE; | |
1640 | return PR_SPEC_PRCTL | PR_SPEC_ENABLE; | |
1978b3a5 AM |
1641 | } else if (spectre_v2_user_ibpb == SPECTRE_V2_USER_STRICT || |
1642 | spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT || | |
1643 | spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED) | |
1644 | return PR_SPEC_DISABLE; | |
1645 | else | |
9137bb27 | 1646 | return PR_SPEC_NOT_AFFECTED; |
9137bb27 TG |
1647 | } |
1648 | ||
7bbf1373 | 1649 | int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which) |
a73ec77e TG |
1650 | { |
1651 | switch (which) { | |
1652 | case PR_SPEC_STORE_BYPASS: | |
7bbf1373 | 1653 | return ssb_prctl_get(task); |
9137bb27 TG |
1654 | case PR_SPEC_INDIRECT_BRANCH: |
1655 | return ib_prctl_get(task); | |
e893bb1b BS |
1656 | case PR_SPEC_L1D_FLUSH: |
1657 | return l1d_flush_prctl_get(task); | |
a73ec77e TG |
1658 | default: |
1659 | return -ENODEV; | |
1660 | } | |
1661 | } | |
1662 | ||
77243971 KRW |
1663 | void x86_spec_ctrl_setup_ap(void) |
1664 | { | |
7eb8956a | 1665 | if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) |
4b59bdb5 | 1666 | wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base); |
764f3c21 KRW |
1667 | |
1668 | if (ssb_mode == SPEC_STORE_BYPASS_DISABLE) | |
9f65fb29 | 1669 | x86_amd_ssb_disable(); |
77243971 KRW |
1670 | } |
1671 | ||
b8e8c830 PB |
1672 | bool itlb_multihit_kvm_mitigation; |
1673 | EXPORT_SYMBOL_GPL(itlb_multihit_kvm_mitigation); | |
1674 | ||
56563f53 KRW |
1675 | #undef pr_fmt |
1676 | #define pr_fmt(fmt) "L1TF: " fmt | |
72c6d2db | 1677 | |
d90a7a0e JK |
1678 | /* Default mitigation for L1TF-affected CPUs */ |
1679 | enum l1tf_mitigations l1tf_mitigation __ro_after_init = L1TF_MITIGATION_FLUSH; | |
72c6d2db | 1680 | #if IS_ENABLED(CONFIG_KVM_INTEL) |
d90a7a0e | 1681 | EXPORT_SYMBOL_GPL(l1tf_mitigation); |
1eb46908 | 1682 | #endif |
895ae47f | 1683 | enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO; |
72c6d2db | 1684 | EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation); |
72c6d2db | 1685 | |
cc51e542 AK |
1686 | /* |
1687 | * These CPUs all support 44bits physical address space internally in the | |
1688 | * cache but CPUID can report a smaller number of physical address bits. | |
1689 | * | |
1690 | * The L1TF mitigation uses the top most address bit for the inversion of | |
1691 | * non present PTEs. When the installed memory reaches into the top most | |
1692 | * address bit due to memory holes, which has been observed on machines | |
1693 | * which report 36bits physical address bits and have 32G RAM installed, | |
1694 | * then the mitigation range check in l1tf_select_mitigation() triggers. | |
1695 | * This is a false positive because the mitigation is still possible due to | |
1696 | * the fact that the cache uses 44bit internally. Use the cache bits | |
1697 | * instead of the reported physical bits and adjust them on the affected | |
1698 | * machines to 44bit if the reported bits are less than 44. | |
1699 | */ | |
1700 | static void override_cache_bits(struct cpuinfo_x86 *c) | |
1701 | { | |
1702 | if (c->x86 != 6) | |
1703 | return; | |
1704 | ||
1705 | switch (c->x86_model) { | |
1706 | case INTEL_FAM6_NEHALEM: | |
1707 | case INTEL_FAM6_WESTMERE: | |
1708 | case INTEL_FAM6_SANDYBRIDGE: | |
1709 | case INTEL_FAM6_IVYBRIDGE: | |
c66f78a6 | 1710 | case INTEL_FAM6_HASWELL: |
af239c44 | 1711 | case INTEL_FAM6_HASWELL_L: |
5e741407 | 1712 | case INTEL_FAM6_HASWELL_G: |
c66f78a6 | 1713 | case INTEL_FAM6_BROADWELL: |
5e741407 | 1714 | case INTEL_FAM6_BROADWELL_G: |
af239c44 | 1715 | case INTEL_FAM6_SKYLAKE_L: |
c66f78a6 | 1716 | case INTEL_FAM6_SKYLAKE: |
af239c44 | 1717 | case INTEL_FAM6_KABYLAKE_L: |
c66f78a6 | 1718 | case INTEL_FAM6_KABYLAKE: |
cc51e542 AK |
1719 | if (c->x86_cache_bits < 44) |
1720 | c->x86_cache_bits = 44; | |
1721 | break; | |
1722 | } | |
1723 | } | |
1724 | ||
56563f53 KRW |
1725 | static void __init l1tf_select_mitigation(void) |
1726 | { | |
1727 | u64 half_pa; | |
1728 | ||
1729 | if (!boot_cpu_has_bug(X86_BUG_L1TF)) | |
1730 | return; | |
1731 | ||
d68be4c4 JP |
1732 | if (cpu_mitigations_off()) |
1733 | l1tf_mitigation = L1TF_MITIGATION_OFF; | |
1734 | else if (cpu_mitigations_auto_nosmt()) | |
1735 | l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT; | |
1736 | ||
cc51e542 AK |
1737 | override_cache_bits(&boot_cpu_data); |
1738 | ||
d90a7a0e JK |
1739 | switch (l1tf_mitigation) { |
1740 | case L1TF_MITIGATION_OFF: | |
1741 | case L1TF_MITIGATION_FLUSH_NOWARN: | |
1742 | case L1TF_MITIGATION_FLUSH: | |
1743 | break; | |
1744 | case L1TF_MITIGATION_FLUSH_NOSMT: | |
1745 | case L1TF_MITIGATION_FULL: | |
1746 | cpu_smt_disable(false); | |
1747 | break; | |
1748 | case L1TF_MITIGATION_FULL_FORCE: | |
1749 | cpu_smt_disable(true); | |
1750 | break; | |
1751 | } | |
1752 | ||
56563f53 KRW |
1753 | #if CONFIG_PGTABLE_LEVELS == 2 |
1754 | pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n"); | |
1755 | return; | |
1756 | #endif | |
1757 | ||
56563f53 | 1758 | half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT; |
5b5e4d62 MH |
1759 | if (l1tf_mitigation != L1TF_MITIGATION_OFF && |
1760 | e820__mapped_any(half_pa, ULLONG_MAX - half_pa, E820_TYPE_RAM)) { | |
56563f53 | 1761 | pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n"); |
6a012288 VB |
1762 | pr_info("You may make it effective by booting the kernel with mem=%llu parameter.\n", |
1763 | half_pa); | |
1764 | pr_info("However, doing so will make a part of your RAM unusable.\n"); | |
65fd4cb6 | 1765 | pr_info("Reading https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html might help you decide.\n"); |
56563f53 KRW |
1766 | return; |
1767 | } | |
1768 | ||
1769 | setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV); | |
1770 | } | |
d90a7a0e JK |
1771 | |
1772 | static int __init l1tf_cmdline(char *str) | |
1773 | { | |
1774 | if (!boot_cpu_has_bug(X86_BUG_L1TF)) | |
1775 | return 0; | |
1776 | ||
1777 | if (!str) | |
1778 | return -EINVAL; | |
1779 | ||
1780 | if (!strcmp(str, "off")) | |
1781 | l1tf_mitigation = L1TF_MITIGATION_OFF; | |
1782 | else if (!strcmp(str, "flush,nowarn")) | |
1783 | l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOWARN; | |
1784 | else if (!strcmp(str, "flush")) | |
1785 | l1tf_mitigation = L1TF_MITIGATION_FLUSH; | |
1786 | else if (!strcmp(str, "flush,nosmt")) | |
1787 | l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT; | |
1788 | else if (!strcmp(str, "full")) | |
1789 | l1tf_mitigation = L1TF_MITIGATION_FULL; | |
1790 | else if (!strcmp(str, "full,force")) | |
1791 | l1tf_mitigation = L1TF_MITIGATION_FULL_FORCE; | |
1792 | ||
1793 | return 0; | |
1794 | } | |
1795 | early_param("l1tf", l1tf_cmdline); | |
1796 | ||
56563f53 | 1797 | #undef pr_fmt |
39226ef0 | 1798 | #define pr_fmt(fmt) fmt |
56563f53 | 1799 | |
61dc0f55 | 1800 | #ifdef CONFIG_SYSFS |
d1059518 | 1801 | |
72c6d2db TG |
1802 | #define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion" |
1803 | ||
1804 | #if IS_ENABLED(CONFIG_KVM_INTEL) | |
8770709f | 1805 | static const char * const l1tf_vmx_states[] = { |
a7b9020b TG |
1806 | [VMENTER_L1D_FLUSH_AUTO] = "auto", |
1807 | [VMENTER_L1D_FLUSH_NEVER] = "vulnerable", | |
1808 | [VMENTER_L1D_FLUSH_COND] = "conditional cache flushes", | |
1809 | [VMENTER_L1D_FLUSH_ALWAYS] = "cache flushes", | |
1810 | [VMENTER_L1D_FLUSH_EPT_DISABLED] = "EPT disabled", | |
8e0b2b91 | 1811 | [VMENTER_L1D_FLUSH_NOT_REQUIRED] = "flush not necessary" |
72c6d2db TG |
1812 | }; |
1813 | ||
1814 | static ssize_t l1tf_show_state(char *buf) | |
1815 | { | |
1816 | if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) | |
1817 | return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG); | |
1818 | ||
ea156d19 PB |
1819 | if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_EPT_DISABLED || |
1820 | (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER && | |
130d6f94 | 1821 | sched_smt_active())) { |
ea156d19 PB |
1822 | return sprintf(buf, "%s; VMX: %s\n", L1TF_DEFAULT_MSG, |
1823 | l1tf_vmx_states[l1tf_vmx_mitigation]); | |
130d6f94 | 1824 | } |
ea156d19 PB |
1825 | |
1826 | return sprintf(buf, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG, | |
1827 | l1tf_vmx_states[l1tf_vmx_mitigation], | |
130d6f94 | 1828 | sched_smt_active() ? "vulnerable" : "disabled"); |
72c6d2db | 1829 | } |
b8e8c830 PB |
1830 | |
1831 | static ssize_t itlb_multihit_show_state(char *buf) | |
1832 | { | |
f29dfa53 PG |
1833 | if (!boot_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) || |
1834 | !boot_cpu_has(X86_FEATURE_VMX)) | |
1835 | return sprintf(buf, "KVM: Mitigation: VMX unsupported\n"); | |
1836 | else if (!(cr4_read_shadow() & X86_CR4_VMXE)) | |
1837 | return sprintf(buf, "KVM: Mitigation: VMX disabled\n"); | |
1838 | else if (itlb_multihit_kvm_mitigation) | |
b8e8c830 PB |
1839 | return sprintf(buf, "KVM: Mitigation: Split huge pages\n"); |
1840 | else | |
1841 | return sprintf(buf, "KVM: Vulnerable\n"); | |
1842 | } | |
72c6d2db TG |
1843 | #else |
1844 | static ssize_t l1tf_show_state(char *buf) | |
1845 | { | |
1846 | return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG); | |
1847 | } | |
72c6d2db | 1848 | |
db4d30fb VT |
1849 | static ssize_t itlb_multihit_show_state(char *buf) |
1850 | { | |
1851 | return sprintf(buf, "Processor vulnerable\n"); | |
1852 | } | |
b8e8c830 | 1853 | #endif |
db4d30fb | 1854 | |
8a4b06d3 TG |
1855 | static ssize_t mds_show_state(char *buf) |
1856 | { | |
517c3ba0 | 1857 | if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) { |
8a4b06d3 TG |
1858 | return sprintf(buf, "%s; SMT Host state unknown\n", |
1859 | mds_strings[mds_mitigation]); | |
1860 | } | |
1861 | ||
1862 | if (boot_cpu_has(X86_BUG_MSBDS_ONLY)) { | |
1863 | return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation], | |
e2c3c947 KRW |
1864 | (mds_mitigation == MDS_MITIGATION_OFF ? "vulnerable" : |
1865 | sched_smt_active() ? "mitigated" : "disabled")); | |
8a4b06d3 TG |
1866 | } |
1867 | ||
1868 | return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation], | |
1869 | sched_smt_active() ? "vulnerable" : "disabled"); | |
1870 | } | |
1871 | ||
6608b45a PG |
1872 | static ssize_t tsx_async_abort_show_state(char *buf) |
1873 | { | |
1874 | if ((taa_mitigation == TAA_MITIGATION_TSX_DISABLED) || | |
1875 | (taa_mitigation == TAA_MITIGATION_OFF)) | |
1876 | return sprintf(buf, "%s\n", taa_strings[taa_mitigation]); | |
1877 | ||
1878 | if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) { | |
1879 | return sprintf(buf, "%s; SMT Host state unknown\n", | |
1880 | taa_strings[taa_mitigation]); | |
1881 | } | |
1882 | ||
1883 | return sprintf(buf, "%s; SMT %s\n", taa_strings[taa_mitigation], | |
1884 | sched_smt_active() ? "vulnerable" : "disabled"); | |
1885 | } | |
1886 | ||
a8f76ae4 TC |
1887 | static char *stibp_state(void) |
1888 | { | |
a94faf3f | 1889 | if (spectre_v2_in_eibrs_mode(spectre_v2_enabled)) |
34bce7c9 TC |
1890 | return ""; |
1891 | ||
21998a35 | 1892 | switch (spectre_v2_user_stibp) { |
fa1202ef TG |
1893 | case SPECTRE_V2_USER_NONE: |
1894 | return ", STIBP: disabled"; | |
1895 | case SPECTRE_V2_USER_STRICT: | |
1896 | return ", STIBP: forced"; | |
20c3a2c3 TL |
1897 | case SPECTRE_V2_USER_STRICT_PREFERRED: |
1898 | return ", STIBP: always-on"; | |
9137bb27 | 1899 | case SPECTRE_V2_USER_PRCTL: |
6b3e64c2 | 1900 | case SPECTRE_V2_USER_SECCOMP: |
7cc765a6 TG |
1901 | if (static_key_enabled(&switch_to_cond_stibp)) |
1902 | return ", STIBP: conditional"; | |
fa1202ef TG |
1903 | } |
1904 | return ""; | |
a8f76ae4 TC |
1905 | } |
1906 | ||
1907 | static char *ibpb_state(void) | |
1908 | { | |
4c71a2b6 | 1909 | if (boot_cpu_has(X86_FEATURE_IBPB)) { |
7cc765a6 | 1910 | if (static_key_enabled(&switch_mm_always_ibpb)) |
4c71a2b6 | 1911 | return ", IBPB: always-on"; |
7cc765a6 TG |
1912 | if (static_key_enabled(&switch_mm_cond_ibpb)) |
1913 | return ", IBPB: conditional"; | |
1914 | return ", IBPB: disabled"; | |
4c71a2b6 TG |
1915 | } |
1916 | return ""; | |
a8f76ae4 TC |
1917 | } |
1918 | ||
534fcc9a JP |
1919 | static ssize_t spectre_v2_show_state(char *buf) |
1920 | { | |
92cf97b2 JP |
1921 | if (spectre_v2_enabled == SPECTRE_V2_LFENCE) |
1922 | return sprintf(buf, "Vulnerable: LFENCE\n"); | |
1923 | ||
534fcc9a | 1924 | if (spectre_v2_enabled == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled()) |
810643a7 JP |
1925 | return sprintf(buf, "Vulnerable: eIBRS with unprivileged eBPF\n"); |
1926 | ||
1927 | if (sched_smt_active() && unprivileged_ebpf_enabled() && | |
1928 | spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE) | |
1929 | return sprintf(buf, "Vulnerable: eIBRS+LFENCE with unprivileged eBPF and SMT\n"); | |
534fcc9a JP |
1930 | |
1931 | return sprintf(buf, "%s%s%s%s%s%s\n", | |
1932 | spectre_v2_strings[spectre_v2_enabled], | |
1933 | ibpb_state(), | |
1934 | boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "", | |
1935 | stibp_state(), | |
1936 | boot_cpu_has(X86_FEATURE_RSB_CTXSW) ? ", RSB filling" : "", | |
1937 | spectre_v2_module_string()); | |
1938 | } | |
1939 | ||
7e5b3c26 MG |
1940 | static ssize_t srbds_show_state(char *buf) |
1941 | { | |
1942 | return sprintf(buf, "%s\n", srbds_strings[srbds_mitigation]); | |
1943 | } | |
1944 | ||
7bb4d366 | 1945 | static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr, |
ffed645e | 1946 | char *buf, unsigned int bug) |
61dc0f55 | 1947 | { |
d1059518 | 1948 | if (!boot_cpu_has_bug(bug)) |
61dc0f55 | 1949 | return sprintf(buf, "Not affected\n"); |
d1059518 KRW |
1950 | |
1951 | switch (bug) { | |
1952 | case X86_BUG_CPU_MELTDOWN: | |
1953 | if (boot_cpu_has(X86_FEATURE_PTI)) | |
1954 | return sprintf(buf, "Mitigation: PTI\n"); | |
1955 | ||
6cb2b08f JK |
1956 | if (hypervisor_is_type(X86_HYPER_XEN_PV)) |
1957 | return sprintf(buf, "Unknown (XEN PV detected, hypervisor mitigation required)\n"); | |
1958 | ||
d1059518 KRW |
1959 | break; |
1960 | ||
1961 | case X86_BUG_SPECTRE_V1: | |
a2059825 | 1962 | return sprintf(buf, "%s\n", spectre_v1_strings[spectre_v1_mitigation]); |
d1059518 KRW |
1963 | |
1964 | case X86_BUG_SPECTRE_V2: | |
534fcc9a | 1965 | return spectre_v2_show_state(buf); |
d1059518 | 1966 | |
24f7fc83 KRW |
1967 | case X86_BUG_SPEC_STORE_BYPASS: |
1968 | return sprintf(buf, "%s\n", ssb_strings[ssb_mode]); | |
1969 | ||
17dbca11 AK |
1970 | case X86_BUG_L1TF: |
1971 | if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV)) | |
72c6d2db | 1972 | return l1tf_show_state(buf); |
17dbca11 | 1973 | break; |
8a4b06d3 TG |
1974 | |
1975 | case X86_BUG_MDS: | |
1976 | return mds_show_state(buf); | |
1977 | ||
6608b45a PG |
1978 | case X86_BUG_TAA: |
1979 | return tsx_async_abort_show_state(buf); | |
1980 | ||
db4d30fb VT |
1981 | case X86_BUG_ITLB_MULTIHIT: |
1982 | return itlb_multihit_show_state(buf); | |
1983 | ||
7e5b3c26 MG |
1984 | case X86_BUG_SRBDS: |
1985 | return srbds_show_state(buf); | |
1986 | ||
d1059518 KRW |
1987 | default: |
1988 | break; | |
1989 | } | |
1990 | ||
61dc0f55 TG |
1991 | return sprintf(buf, "Vulnerable\n"); |
1992 | } | |
1993 | ||
d1059518 KRW |
1994 | ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf) |
1995 | { | |
1996 | return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN); | |
1997 | } | |
1998 | ||
21e433bd | 1999 | ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf) |
61dc0f55 | 2000 | { |
d1059518 | 2001 | return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1); |
61dc0f55 TG |
2002 | } |
2003 | ||
21e433bd | 2004 | ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf) |
61dc0f55 | 2005 | { |
d1059518 | 2006 | return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2); |
61dc0f55 | 2007 | } |
c456442c KRW |
2008 | |
2009 | ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf) | |
2010 | { | |
2011 | return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS); | |
2012 | } | |
17dbca11 AK |
2013 | |
2014 | ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf) | |
2015 | { | |
2016 | return cpu_show_common(dev, attr, buf, X86_BUG_L1TF); | |
2017 | } | |
8a4b06d3 TG |
2018 | |
2019 | ssize_t cpu_show_mds(struct device *dev, struct device_attribute *attr, char *buf) | |
2020 | { | |
2021 | return cpu_show_common(dev, attr, buf, X86_BUG_MDS); | |
2022 | } | |
6608b45a PG |
2023 | |
2024 | ssize_t cpu_show_tsx_async_abort(struct device *dev, struct device_attribute *attr, char *buf) | |
2025 | { | |
2026 | return cpu_show_common(dev, attr, buf, X86_BUG_TAA); | |
2027 | } | |
db4d30fb VT |
2028 | |
2029 | ssize_t cpu_show_itlb_multihit(struct device *dev, struct device_attribute *attr, char *buf) | |
2030 | { | |
2031 | return cpu_show_common(dev, attr, buf, X86_BUG_ITLB_MULTIHIT); | |
2032 | } | |
7e5b3c26 MG |
2033 | |
2034 | ssize_t cpu_show_srbds(struct device *dev, struct device_attribute *attr, char *buf) | |
2035 | { | |
2036 | return cpu_show_common(dev, attr, buf, X86_BUG_SRBDS); | |
2037 | } | |
61dc0f55 | 2038 | #endif |