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