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