2 * Copyright (C) 1994 Linus Torvalds
4 * Cyrix stuff, June 1998 by:
5 * - Rafael R. Reilova (moved everything from head.S),
6 * <rreilova@ececs.uc.edu>
7 * - Channing Corn (tests & fixes),
8 * - Andrew D. Balsa (code cleanup).
10 #include <linux/init.h>
11 #include <linux/utsname.h>
12 #include <linux/cpu.h>
13 #include <linux/module.h>
14 #include <linux/nospec.h>
15 #include <linux/prctl.h>
16 #include <linux/sched/smt.h>
18 #include <asm/spec-ctrl.h>
19 #include <asm/cmdline.h>
21 #include <asm/processor.h>
22 #include <asm/processor-flags.h>
23 #include <asm/fpu/internal.h>
26 #include <asm/paravirt.h>
27 #include <asm/alternative.h>
28 #include <asm/hypervisor.h>
29 #include <asm/pgtable.h>
30 #include <asm/cacheflush.h>
31 #include <asm/intel-family.h>
36 static void __init spectre_v1_select_mitigation(void);
37 static void __init spectre_v2_select_mitigation(void);
38 static void __init ssb_select_mitigation(void);
39 static void __init l1tf_select_mitigation(void);
40 static void __init mds_select_mitigation(void);
41 static void __init mds_print_mitigation(void);
42 static void __init taa_select_mitigation(void);
43 static void __init srbds_select_mitigation(void);
45 /* The base value of the SPEC_CTRL MSR that always has to be preserved. */
46 u64 x86_spec_ctrl_base;
47 EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
48 static DEFINE_MUTEX(spec_ctrl_mutex);
51 * The vendor and possibly platform specific bits which can be modified in
54 static u64 __ro_after_init x86_spec_ctrl_mask = SPEC_CTRL_IBRS;
57 * AMD specific MSR info for Speculative Store Bypass control.
58 * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu().
60 u64 __ro_after_init x86_amd_ls_cfg_base;
61 u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask;
63 /* Control conditional STIBP in switch_to() */
64 DEFINE_STATIC_KEY_FALSE(switch_to_cond_stibp);
65 /* Control conditional IBPB in switch_mm() */
66 DEFINE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
67 /* Control unconditional IBPB in switch_mm() */
68 DEFINE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
70 /* Control MDS CPU buffer clear before returning to user space */
71 DEFINE_STATIC_KEY_FALSE(mds_user_clear);
72 EXPORT_SYMBOL_GPL(mds_user_clear);
73 /* Control MDS CPU buffer clear before idling (halt, mwait) */
74 DEFINE_STATIC_KEY_FALSE(mds_idle_clear);
75 EXPORT_SYMBOL_GPL(mds_idle_clear);
77 void __init check_bugs(void)
82 * identify_boot_cpu() initialized SMT support information, let the
85 cpu_smt_check_topology_early();
87 if (!IS_ENABLED(CONFIG_SMP)) {
89 print_cpu_info(&boot_cpu_data);
93 * Read the SPEC_CTRL MSR to account for reserved bits which may
94 * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
95 * init code as it is not enumerated and depends on the family.
97 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
98 rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
100 /* Allow STIBP in MSR_SPEC_CTRL if supported */
101 if (boot_cpu_has(X86_FEATURE_STIBP))
102 x86_spec_ctrl_mask |= SPEC_CTRL_STIBP;
104 /* Select the proper CPU mitigations before patching alternatives: */
105 spectre_v1_select_mitigation();
106 spectre_v2_select_mitigation();
107 ssb_select_mitigation();
108 l1tf_select_mitigation();
109 mds_select_mitigation();
110 taa_select_mitigation();
111 srbds_select_mitigation();
114 * As MDS and TAA mitigations are inter-related, print MDS
115 * mitigation until after TAA mitigation selection is done.
117 mds_print_mitigation();
123 * Check whether we are able to run this kernel safely on SMP.
125 * - i386 is no longer supported.
126 * - In order to run on anything without a TSC, we need to be
127 * compiled for a i486.
129 if (boot_cpu_data.x86 < 4)
130 panic("Kernel requires i486+ for 'invlpg' and other features");
132 init_utsname()->machine[1] =
133 '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
134 alternative_instructions();
136 fpu__init_check_bugs();
137 #else /* CONFIG_X86_64 */
138 alternative_instructions();
141 * Make sure the first 2MB area is not mapped by huge pages
142 * There are typically fixed size MTRRs in there and overlapping
143 * MTRRs into large pages causes slow downs.
145 * Right now we don't do that with gbpages because there seems
146 * very little benefit for that case.
149 set_memory_4k((unsigned long)__va(0), 1);
154 x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest)
156 u64 msrval, guestval, hostval = x86_spec_ctrl_base;
157 struct thread_info *ti = current_thread_info();
159 /* Is MSR_SPEC_CTRL implemented ? */
160 if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) {
162 * Restrict guest_spec_ctrl to supported values. Clear the
163 * modifiable bits in the host base value and or the
164 * modifiable bits from the guest value.
166 guestval = hostval & ~x86_spec_ctrl_mask;
167 guestval |= guest_spec_ctrl & x86_spec_ctrl_mask;
169 /* SSBD controlled in MSR_SPEC_CTRL */
170 if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
171 static_cpu_has(X86_FEATURE_AMD_SSBD))
172 hostval |= ssbd_tif_to_spec_ctrl(ti->flags);
174 /* Conditional STIBP enabled? */
175 if (static_branch_unlikely(&switch_to_cond_stibp))
176 hostval |= stibp_tif_to_spec_ctrl(ti->flags);
178 if (hostval != guestval) {
179 msrval = setguest ? guestval : hostval;
180 wrmsrl(MSR_IA32_SPEC_CTRL, msrval);
185 * If SSBD is not handled in MSR_SPEC_CTRL on AMD, update
186 * MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported.
188 if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
189 !static_cpu_has(X86_FEATURE_VIRT_SSBD))
193 * If the host has SSBD mitigation enabled, force it in the host's
194 * virtual MSR value. If its not permanently enabled, evaluate
195 * current's TIF_SSBD thread flag.
197 if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE))
198 hostval = SPEC_CTRL_SSBD;
200 hostval = ssbd_tif_to_spec_ctrl(ti->flags);
202 /* Sanitize the guest value */
203 guestval = guest_virt_spec_ctrl & SPEC_CTRL_SSBD;
205 if (hostval != guestval) {
208 tif = setguest ? ssbd_spec_ctrl_to_tif(guestval) :
209 ssbd_spec_ctrl_to_tif(hostval);
211 speculation_ctrl_update(tif);
214 EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl);
216 static void x86_amd_ssb_disable(void)
218 u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_ssbd_mask;
220 if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
221 wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, SPEC_CTRL_SSBD);
222 else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
223 wrmsrl(MSR_AMD64_LS_CFG, msrval);
227 #define pr_fmt(fmt) "MDS: " fmt
229 /* Default mitigation for MDS-affected CPUs */
230 static enum mds_mitigations mds_mitigation __ro_after_init = MDS_MITIGATION_FULL;
231 static bool mds_nosmt __ro_after_init = false;
233 static const char * const mds_strings[] = {
234 [MDS_MITIGATION_OFF] = "Vulnerable",
235 [MDS_MITIGATION_FULL] = "Mitigation: Clear CPU buffers",
236 [MDS_MITIGATION_VMWERV] = "Vulnerable: Clear CPU buffers attempted, no microcode",
239 static void __init mds_select_mitigation(void)
241 if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off()) {
242 mds_mitigation = MDS_MITIGATION_OFF;
246 if (mds_mitigation == MDS_MITIGATION_FULL) {
247 if (!boot_cpu_has(X86_FEATURE_MD_CLEAR))
248 mds_mitigation = MDS_MITIGATION_VMWERV;
250 static_branch_enable(&mds_user_clear);
252 if (!boot_cpu_has(X86_BUG_MSBDS_ONLY) &&
253 (mds_nosmt || cpu_mitigations_auto_nosmt()))
254 cpu_smt_disable(false);
258 static void __init mds_print_mitigation(void)
260 if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off())
263 pr_info("%s\n", mds_strings[mds_mitigation]);
266 static int __init mds_cmdline(char *str)
268 if (!boot_cpu_has_bug(X86_BUG_MDS))
274 if (!strcmp(str, "off"))
275 mds_mitigation = MDS_MITIGATION_OFF;
276 else if (!strcmp(str, "full"))
277 mds_mitigation = MDS_MITIGATION_FULL;
278 else if (!strcmp(str, "full,nosmt")) {
279 mds_mitigation = MDS_MITIGATION_FULL;
285 early_param("mds", mds_cmdline);
288 #define pr_fmt(fmt) "TAA: " fmt
290 /* Default mitigation for TAA-affected CPUs */
291 static enum taa_mitigations taa_mitigation __ro_after_init = TAA_MITIGATION_VERW;
292 static bool taa_nosmt __ro_after_init;
294 static const char * const taa_strings[] = {
295 [TAA_MITIGATION_OFF] = "Vulnerable",
296 [TAA_MITIGATION_UCODE_NEEDED] = "Vulnerable: Clear CPU buffers attempted, no microcode",
297 [TAA_MITIGATION_VERW] = "Mitigation: Clear CPU buffers",
298 [TAA_MITIGATION_TSX_DISABLED] = "Mitigation: TSX disabled",
301 static void __init taa_select_mitigation(void)
305 if (!boot_cpu_has_bug(X86_BUG_TAA)) {
306 taa_mitigation = TAA_MITIGATION_OFF;
310 /* TSX previously disabled by tsx=off */
311 if (!boot_cpu_has(X86_FEATURE_RTM)) {
312 taa_mitigation = TAA_MITIGATION_TSX_DISABLED;
316 if (cpu_mitigations_off()) {
317 taa_mitigation = TAA_MITIGATION_OFF;
322 * TAA mitigation via VERW is turned off if both
323 * tsx_async_abort=off and mds=off are specified.
325 if (taa_mitigation == TAA_MITIGATION_OFF &&
326 mds_mitigation == MDS_MITIGATION_OFF)
329 if (boot_cpu_has(X86_FEATURE_MD_CLEAR))
330 taa_mitigation = TAA_MITIGATION_VERW;
332 taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
335 * VERW doesn't clear the CPU buffers when MD_CLEAR=1 and MDS_NO=1.
336 * A microcode update fixes this behavior to clear CPU buffers. It also
337 * adds support for MSR_IA32_TSX_CTRL which is enumerated by the
338 * ARCH_CAP_TSX_CTRL_MSR bit.
340 * On MDS_NO=1 CPUs if ARCH_CAP_TSX_CTRL_MSR is not set, microcode
341 * update is required.
343 ia32_cap = x86_read_arch_cap_msr();
344 if ( (ia32_cap & ARCH_CAP_MDS_NO) &&
345 !(ia32_cap & ARCH_CAP_TSX_CTRL_MSR))
346 taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
349 * TSX is enabled, select alternate mitigation for TAA which is
350 * the same as MDS. Enable MDS static branch to clear CPU buffers.
352 * For guests that can't determine whether the correct microcode is
353 * present on host, enable the mitigation for UCODE_NEEDED as well.
355 static_branch_enable(&mds_user_clear);
357 if (taa_nosmt || cpu_mitigations_auto_nosmt())
358 cpu_smt_disable(false);
361 * Update MDS mitigation, if necessary, as the mds_user_clear is
362 * now enabled for TAA mitigation.
364 if (mds_mitigation == MDS_MITIGATION_OFF &&
365 boot_cpu_has_bug(X86_BUG_MDS)) {
366 mds_mitigation = MDS_MITIGATION_FULL;
367 mds_select_mitigation();
370 pr_info("%s\n", taa_strings[taa_mitigation]);
373 static int __init tsx_async_abort_parse_cmdline(char *str)
375 if (!boot_cpu_has_bug(X86_BUG_TAA))
381 if (!strcmp(str, "off")) {
382 taa_mitigation = TAA_MITIGATION_OFF;
383 } else if (!strcmp(str, "full")) {
384 taa_mitigation = TAA_MITIGATION_VERW;
385 } else if (!strcmp(str, "full,nosmt")) {
386 taa_mitigation = TAA_MITIGATION_VERW;
392 early_param("tsx_async_abort", tsx_async_abort_parse_cmdline);
395 #define pr_fmt(fmt) "SRBDS: " fmt
397 enum srbds_mitigations {
398 SRBDS_MITIGATION_OFF,
399 SRBDS_MITIGATION_UCODE_NEEDED,
400 SRBDS_MITIGATION_FULL,
401 SRBDS_MITIGATION_TSX_OFF,
402 SRBDS_MITIGATION_HYPERVISOR,
405 static enum srbds_mitigations srbds_mitigation __ro_after_init = SRBDS_MITIGATION_FULL;
407 static const char * const srbds_strings[] = {
408 [SRBDS_MITIGATION_OFF] = "Vulnerable",
409 [SRBDS_MITIGATION_UCODE_NEEDED] = "Vulnerable: No microcode",
410 [SRBDS_MITIGATION_FULL] = "Mitigation: Microcode",
411 [SRBDS_MITIGATION_TSX_OFF] = "Mitigation: TSX disabled",
412 [SRBDS_MITIGATION_HYPERVISOR] = "Unknown: Dependent on hypervisor status",
415 static bool srbds_off;
417 void update_srbds_msr(void)
421 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
424 if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
427 if (srbds_mitigation == SRBDS_MITIGATION_UCODE_NEEDED)
430 rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
432 switch (srbds_mitigation) {
433 case SRBDS_MITIGATION_OFF:
434 case SRBDS_MITIGATION_TSX_OFF:
435 mcu_ctrl |= RNGDS_MITG_DIS;
437 case SRBDS_MITIGATION_FULL:
438 mcu_ctrl &= ~RNGDS_MITG_DIS;
444 wrmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
447 static void __init srbds_select_mitigation(void)
451 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
455 * Check to see if this is one of the MDS_NO systems supporting
456 * TSX that are only exposed to SRBDS when TSX is enabled.
458 ia32_cap = x86_read_arch_cap_msr();
459 if ((ia32_cap & ARCH_CAP_MDS_NO) && !boot_cpu_has(X86_FEATURE_RTM))
460 srbds_mitigation = SRBDS_MITIGATION_TSX_OFF;
461 else if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
462 srbds_mitigation = SRBDS_MITIGATION_HYPERVISOR;
463 else if (!boot_cpu_has(X86_FEATURE_SRBDS_CTRL))
464 srbds_mitigation = SRBDS_MITIGATION_UCODE_NEEDED;
465 else if (cpu_mitigations_off() || srbds_off)
466 srbds_mitigation = SRBDS_MITIGATION_OFF;
469 pr_info("%s\n", srbds_strings[srbds_mitigation]);
472 static int __init srbds_parse_cmdline(char *str)
477 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
480 srbds_off = !strcmp(str, "off");
483 early_param("srbds", srbds_parse_cmdline);
486 #define pr_fmt(fmt) "Spectre V1 : " fmt
488 enum spectre_v1_mitigation {
489 SPECTRE_V1_MITIGATION_NONE,
490 SPECTRE_V1_MITIGATION_AUTO,
493 static enum spectre_v1_mitigation spectre_v1_mitigation __ro_after_init =
494 SPECTRE_V1_MITIGATION_AUTO;
496 static const char * const spectre_v1_strings[] = {
497 [SPECTRE_V1_MITIGATION_NONE] = "Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers",
498 [SPECTRE_V1_MITIGATION_AUTO] = "Mitigation: usercopy/swapgs barriers and __user pointer sanitization",
502 * Does SMAP provide full mitigation against speculative kernel access to
505 static bool smap_works_speculatively(void)
507 if (!boot_cpu_has(X86_FEATURE_SMAP))
511 * On CPUs which are vulnerable to Meltdown, SMAP does not
512 * prevent speculative access to user data in the L1 cache.
513 * Consider SMAP to be non-functional as a mitigation on these
516 if (boot_cpu_has(X86_BUG_CPU_MELTDOWN))
522 static void __init spectre_v1_select_mitigation(void)
524 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1) || cpu_mitigations_off()) {
525 spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
529 if (spectre_v1_mitigation == SPECTRE_V1_MITIGATION_AUTO) {
531 * With Spectre v1, a user can speculatively control either
532 * path of a conditional swapgs with a user-controlled GS
533 * value. The mitigation is to add lfences to both code paths.
535 * If FSGSBASE is enabled, the user can put a kernel address in
536 * GS, in which case SMAP provides no protection.
538 * [ NOTE: Don't check for X86_FEATURE_FSGSBASE until the
539 * FSGSBASE enablement patches have been merged. ]
541 * If FSGSBASE is disabled, the user can only put a user space
542 * address in GS. That makes an attack harder, but still
543 * possible if there's no SMAP protection.
545 if (!smap_works_speculatively()) {
547 * Mitigation can be provided from SWAPGS itself or
548 * PTI as the CR3 write in the Meltdown mitigation
551 * If neither is there, mitigate with an LFENCE to
552 * stop speculation through swapgs.
554 if (boot_cpu_has_bug(X86_BUG_SWAPGS) &&
555 !boot_cpu_has(X86_FEATURE_KAISER))
556 setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_USER);
559 * Enable lfences in the kernel entry (non-swapgs)
560 * paths, to prevent user entry from speculatively
563 setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_KERNEL);
567 pr_info("%s\n", spectre_v1_strings[spectre_v1_mitigation]);
570 static int __init nospectre_v1_cmdline(char *str)
572 spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
575 early_param("nospectre_v1", nospectre_v1_cmdline);
578 #define pr_fmt(fmt) "Spectre V2 : " fmt
580 static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init =
583 static enum spectre_v2_user_mitigation spectre_v2_user_stibp __ro_after_init =
584 SPECTRE_V2_USER_NONE;
585 static enum spectre_v2_user_mitigation spectre_v2_user_ibpb __ro_after_init =
586 SPECTRE_V2_USER_NONE;
589 static bool spectre_v2_bad_module;
591 bool retpoline_module_ok(bool has_retpoline)
593 if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
596 pr_err("System may be vulnerable to spectre v2\n");
597 spectre_v2_bad_module = true;
601 static inline const char *spectre_v2_module_string(void)
603 return spectre_v2_bad_module ? " - vulnerable module loaded" : "";
606 static inline const char *spectre_v2_module_string(void) { return ""; }
609 static inline bool match_option(const char *arg, int arglen, const char *opt)
611 int len = strlen(opt);
613 return len == arglen && !strncmp(arg, opt, len);
616 /* The kernel command line selection for spectre v2 */
617 enum spectre_v2_mitigation_cmd {
620 SPECTRE_V2_CMD_FORCE,
621 SPECTRE_V2_CMD_RETPOLINE,
622 SPECTRE_V2_CMD_RETPOLINE_GENERIC,
623 SPECTRE_V2_CMD_RETPOLINE_AMD,
626 enum spectre_v2_user_cmd {
627 SPECTRE_V2_USER_CMD_NONE,
628 SPECTRE_V2_USER_CMD_AUTO,
629 SPECTRE_V2_USER_CMD_FORCE,
630 SPECTRE_V2_USER_CMD_PRCTL,
631 SPECTRE_V2_USER_CMD_PRCTL_IBPB,
632 SPECTRE_V2_USER_CMD_SECCOMP,
633 SPECTRE_V2_USER_CMD_SECCOMP_IBPB,
636 static const char * const spectre_v2_user_strings[] = {
637 [SPECTRE_V2_USER_NONE] = "User space: Vulnerable",
638 [SPECTRE_V2_USER_STRICT] = "User space: Mitigation: STIBP protection",
639 [SPECTRE_V2_USER_STRICT_PREFERRED] = "User space: Mitigation: STIBP always-on protection",
640 [SPECTRE_V2_USER_PRCTL] = "User space: Mitigation: STIBP via prctl",
641 [SPECTRE_V2_USER_SECCOMP] = "User space: Mitigation: STIBP via seccomp and prctl",
644 static const struct {
646 enum spectre_v2_user_cmd cmd;
648 } v2_user_options[] __initconst = {
649 { "auto", SPECTRE_V2_USER_CMD_AUTO, false },
650 { "off", SPECTRE_V2_USER_CMD_NONE, false },
651 { "on", SPECTRE_V2_USER_CMD_FORCE, true },
652 { "prctl", SPECTRE_V2_USER_CMD_PRCTL, false },
653 { "prctl,ibpb", SPECTRE_V2_USER_CMD_PRCTL_IBPB, false },
654 { "seccomp", SPECTRE_V2_USER_CMD_SECCOMP, false },
655 { "seccomp,ibpb", SPECTRE_V2_USER_CMD_SECCOMP_IBPB, false },
658 static void __init spec_v2_user_print_cond(const char *reason, bool secure)
660 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
661 pr_info("spectre_v2_user=%s forced on command line.\n", reason);
664 static enum spectre_v2_user_cmd __init
665 spectre_v2_parse_user_cmdline(enum spectre_v2_mitigation_cmd v2_cmd)
671 case SPECTRE_V2_CMD_NONE:
672 return SPECTRE_V2_USER_CMD_NONE;
673 case SPECTRE_V2_CMD_FORCE:
674 return SPECTRE_V2_USER_CMD_FORCE;
679 ret = cmdline_find_option(boot_command_line, "spectre_v2_user",
682 return SPECTRE_V2_USER_CMD_AUTO;
684 for (i = 0; i < ARRAY_SIZE(v2_user_options); i++) {
685 if (match_option(arg, ret, v2_user_options[i].option)) {
686 spec_v2_user_print_cond(v2_user_options[i].option,
687 v2_user_options[i].secure);
688 return v2_user_options[i].cmd;
692 pr_err("Unknown user space protection option (%s). Switching to AUTO select\n", arg);
693 return SPECTRE_V2_USER_CMD_AUTO;
697 spectre_v2_user_select_mitigation(enum spectre_v2_mitigation_cmd v2_cmd)
699 enum spectre_v2_user_mitigation mode = SPECTRE_V2_USER_NONE;
700 bool smt_possible = IS_ENABLED(CONFIG_SMP);
701 enum spectre_v2_user_cmd cmd;
703 if (!boot_cpu_has(X86_FEATURE_IBPB) && !boot_cpu_has(X86_FEATURE_STIBP))
706 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
707 cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
708 smt_possible = false;
710 cmd = spectre_v2_parse_user_cmdline(v2_cmd);
712 case SPECTRE_V2_USER_CMD_NONE:
714 case SPECTRE_V2_USER_CMD_FORCE:
715 mode = SPECTRE_V2_USER_STRICT;
717 case SPECTRE_V2_USER_CMD_PRCTL:
718 case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
719 mode = SPECTRE_V2_USER_PRCTL;
721 case SPECTRE_V2_USER_CMD_AUTO:
722 case SPECTRE_V2_USER_CMD_SECCOMP:
723 case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
724 if (IS_ENABLED(CONFIG_SECCOMP))
725 mode = SPECTRE_V2_USER_SECCOMP;
727 mode = SPECTRE_V2_USER_PRCTL;
731 /* Initialize Indirect Branch Prediction Barrier */
732 if (boot_cpu_has(X86_FEATURE_IBPB)) {
733 setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
735 spectre_v2_user_ibpb = mode;
737 case SPECTRE_V2_USER_CMD_FORCE:
738 case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
739 case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
740 static_branch_enable(&switch_mm_always_ibpb);
741 spectre_v2_user_ibpb = SPECTRE_V2_USER_STRICT;
743 case SPECTRE_V2_USER_CMD_PRCTL:
744 case SPECTRE_V2_USER_CMD_AUTO:
745 case SPECTRE_V2_USER_CMD_SECCOMP:
746 static_branch_enable(&switch_mm_cond_ibpb);
752 pr_info("mitigation: Enabling %s Indirect Branch Prediction Barrier\n",
753 static_key_enabled(&switch_mm_always_ibpb) ?
754 "always-on" : "conditional");
758 * If enhanced IBRS is enabled or SMT impossible, STIBP is not
761 if (!smt_possible || spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
765 * At this point, an STIBP mode other than "off" has been set.
766 * If STIBP support is not being forced, check if STIBP always-on
769 if (mode != SPECTRE_V2_USER_STRICT &&
770 boot_cpu_has(X86_FEATURE_AMD_STIBP_ALWAYS_ON))
771 mode = SPECTRE_V2_USER_STRICT_PREFERRED;
774 * If STIBP is not available, clear the STIBP mode.
776 if (!boot_cpu_has(X86_FEATURE_STIBP))
777 mode = SPECTRE_V2_USER_NONE;
779 spectre_v2_user_stibp = mode;
782 pr_info("%s\n", spectre_v2_user_strings[mode]);
785 static const char * const spectre_v2_strings[] = {
786 [SPECTRE_V2_NONE] = "Vulnerable",
787 [SPECTRE_V2_RETPOLINE_MINIMAL] = "Vulnerable: Minimal generic ASM retpoline",
788 [SPECTRE_V2_RETPOLINE_MINIMAL_AMD] = "Vulnerable: Minimal AMD ASM retpoline",
789 [SPECTRE_V2_RETPOLINE_GENERIC] = "Mitigation: Full generic retpoline",
790 [SPECTRE_V2_RETPOLINE_AMD] = "Mitigation: Full AMD retpoline",
791 [SPECTRE_V2_IBRS_ENHANCED] = "Mitigation: Enhanced IBRS",
794 static const struct {
796 enum spectre_v2_mitigation_cmd cmd;
798 } mitigation_options[] __initconst = {
799 { "off", SPECTRE_V2_CMD_NONE, false },
800 { "on", SPECTRE_V2_CMD_FORCE, true },
801 { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false },
802 { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_AMD, false },
803 { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
804 { "auto", SPECTRE_V2_CMD_AUTO, false },
807 static void __init spec_v2_print_cond(const char *reason, bool secure)
809 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
810 pr_info("%s selected on command line.\n", reason);
813 static inline bool retp_compiler(void)
815 return __is_defined(RETPOLINE);
818 static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
820 enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
824 if (cmdline_find_option_bool(boot_command_line, "nospectre_v2") ||
825 cpu_mitigations_off())
826 return SPECTRE_V2_CMD_NONE;
828 ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
830 return SPECTRE_V2_CMD_AUTO;
832 for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
833 if (!match_option(arg, ret, mitigation_options[i].option))
835 cmd = mitigation_options[i].cmd;
839 if (i >= ARRAY_SIZE(mitigation_options)) {
840 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
841 return SPECTRE_V2_CMD_AUTO;
844 if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
845 cmd == SPECTRE_V2_CMD_RETPOLINE_AMD ||
846 cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC) &&
847 !IS_ENABLED(CONFIG_RETPOLINE)) {
848 pr_err("%s selected but not compiled in. Switching to AUTO select\n", mitigation_options[i].option);
849 return SPECTRE_V2_CMD_AUTO;
852 if (cmd == SPECTRE_V2_CMD_RETPOLINE_AMD &&
853 boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
854 pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n");
855 return SPECTRE_V2_CMD_AUTO;
858 spec_v2_print_cond(mitigation_options[i].option,
859 mitigation_options[i].secure);
863 static void __init spectre_v2_select_mitigation(void)
865 enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
866 enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
869 * If the CPU is not affected and the command line mode is NONE or AUTO
870 * then nothing to do.
872 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
873 (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
877 case SPECTRE_V2_CMD_NONE:
880 case SPECTRE_V2_CMD_FORCE:
881 case SPECTRE_V2_CMD_AUTO:
882 if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
883 mode = SPECTRE_V2_IBRS_ENHANCED;
884 /* Force it so VMEXIT will restore correctly */
885 x86_spec_ctrl_base |= SPEC_CTRL_IBRS;
886 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
887 goto specv2_set_mode;
889 if (IS_ENABLED(CONFIG_RETPOLINE))
892 case SPECTRE_V2_CMD_RETPOLINE_AMD:
893 if (IS_ENABLED(CONFIG_RETPOLINE))
896 case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
897 if (IS_ENABLED(CONFIG_RETPOLINE))
898 goto retpoline_generic;
900 case SPECTRE_V2_CMD_RETPOLINE:
901 if (IS_ENABLED(CONFIG_RETPOLINE))
905 pr_err("Spectre mitigation: kernel not compiled with retpoline; no mitigation available!");
909 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
911 if (!boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
912 pr_err("Spectre mitigation: LFENCE not serializing, switching to generic retpoline\n");
913 goto retpoline_generic;
915 mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_AMD :
916 SPECTRE_V2_RETPOLINE_MINIMAL_AMD;
917 setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD);
918 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
921 mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_GENERIC :
922 SPECTRE_V2_RETPOLINE_MINIMAL;
923 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
927 spectre_v2_enabled = mode;
928 pr_info("%s\n", spectre_v2_strings[mode]);
931 * If spectre v2 protection has been enabled, unconditionally fill
932 * RSB during a context switch; this protects against two independent
935 * - RSB underflow (and switch to BTB) on Skylake+
936 * - SpectreRSB variant of spectre v2 on X86_BUG_SPECTRE_V2 CPUs
938 setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
939 pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n");
942 * Retpoline means the kernel is safe because it has no indirect
943 * branches. Enhanced IBRS protects firmware too, so, enable restricted
944 * speculation around firmware calls only when Enhanced IBRS isn't
947 * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because
948 * the user might select retpoline on the kernel command line and if
949 * the CPU supports Enhanced IBRS, kernel might un-intentionally not
950 * enable IBRS around firmware calls.
952 if (boot_cpu_has(X86_FEATURE_IBRS) && mode != SPECTRE_V2_IBRS_ENHANCED) {
953 setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
954 pr_info("Enabling Restricted Speculation for firmware calls\n");
957 /* Set up IBPB and STIBP depending on the general spectre V2 command */
958 spectre_v2_user_select_mitigation(cmd);
961 static void update_stibp_msr(void * __unused)
963 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
966 /* Update x86_spec_ctrl_base in case SMT state changed. */
967 static void update_stibp_strict(void)
969 u64 mask = x86_spec_ctrl_base & ~SPEC_CTRL_STIBP;
971 if (sched_smt_active())
972 mask |= SPEC_CTRL_STIBP;
974 if (mask == x86_spec_ctrl_base)
977 pr_info("Update user space SMT mitigation: STIBP %s\n",
978 mask & SPEC_CTRL_STIBP ? "always-on" : "off");
979 x86_spec_ctrl_base = mask;
980 on_each_cpu(update_stibp_msr, NULL, 1);
983 /* Update the static key controlling the evaluation of TIF_SPEC_IB */
984 static void update_indir_branch_cond(void)
986 if (sched_smt_active())
987 static_branch_enable(&switch_to_cond_stibp);
989 static_branch_disable(&switch_to_cond_stibp);
993 #define pr_fmt(fmt) fmt
995 /* Update the static key controlling the MDS CPU buffer clear in idle */
996 static void update_mds_branch_idle(void)
999 * Enable the idle clearing if SMT is active on CPUs which are
1000 * affected only by MSBDS and not any other MDS variant.
1002 * The other variants cannot be mitigated when SMT is enabled, so
1003 * clearing the buffers on idle just to prevent the Store Buffer
1004 * repartitioning leak would be a window dressing exercise.
1006 if (!boot_cpu_has_bug(X86_BUG_MSBDS_ONLY))
1009 if (sched_smt_active())
1010 static_branch_enable(&mds_idle_clear);
1012 static_branch_disable(&mds_idle_clear);
1015 #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"
1016 #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"
1018 void arch_smt_update(void)
1020 mutex_lock(&spec_ctrl_mutex);
1022 switch (spectre_v2_user_stibp) {
1023 case SPECTRE_V2_USER_NONE:
1025 case SPECTRE_V2_USER_STRICT:
1026 case SPECTRE_V2_USER_STRICT_PREFERRED:
1027 update_stibp_strict();
1029 case SPECTRE_V2_USER_PRCTL:
1030 case SPECTRE_V2_USER_SECCOMP:
1031 update_indir_branch_cond();
1035 switch (mds_mitigation) {
1036 case MDS_MITIGATION_FULL:
1037 case MDS_MITIGATION_VMWERV:
1038 if (sched_smt_active() && !boot_cpu_has(X86_BUG_MSBDS_ONLY))
1039 pr_warn_once(MDS_MSG_SMT);
1040 update_mds_branch_idle();
1042 case MDS_MITIGATION_OFF:
1046 switch (taa_mitigation) {
1047 case TAA_MITIGATION_VERW:
1048 case TAA_MITIGATION_UCODE_NEEDED:
1049 if (sched_smt_active())
1050 pr_warn_once(TAA_MSG_SMT);
1052 case TAA_MITIGATION_TSX_DISABLED:
1053 case TAA_MITIGATION_OFF:
1057 mutex_unlock(&spec_ctrl_mutex);
1061 #define pr_fmt(fmt) "Speculative Store Bypass: " fmt
1063 static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
1065 /* The kernel command line selection */
1066 enum ssb_mitigation_cmd {
1067 SPEC_STORE_BYPASS_CMD_NONE,
1068 SPEC_STORE_BYPASS_CMD_AUTO,
1069 SPEC_STORE_BYPASS_CMD_ON,
1070 SPEC_STORE_BYPASS_CMD_PRCTL,
1071 SPEC_STORE_BYPASS_CMD_SECCOMP,
1074 static const char * const ssb_strings[] = {
1075 [SPEC_STORE_BYPASS_NONE] = "Vulnerable",
1076 [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass disabled",
1077 [SPEC_STORE_BYPASS_PRCTL] = "Mitigation: Speculative Store Bypass disabled via prctl",
1078 [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
1081 static const struct {
1083 enum ssb_mitigation_cmd cmd;
1084 } ssb_mitigation_options[] __initconst = {
1085 { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */
1086 { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */
1087 { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */
1088 { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative Store Bypass via prctl */
1089 { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */
1092 static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
1094 enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO;
1098 if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable") ||
1099 cpu_mitigations_off()) {
1100 return SPEC_STORE_BYPASS_CMD_NONE;
1102 ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable",
1105 return SPEC_STORE_BYPASS_CMD_AUTO;
1107 for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) {
1108 if (!match_option(arg, ret, ssb_mitigation_options[i].option))
1111 cmd = ssb_mitigation_options[i].cmd;
1115 if (i >= ARRAY_SIZE(ssb_mitigation_options)) {
1116 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
1117 return SPEC_STORE_BYPASS_CMD_AUTO;
1124 static enum ssb_mitigation __init __ssb_select_mitigation(void)
1126 enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
1127 enum ssb_mitigation_cmd cmd;
1129 if (!boot_cpu_has(X86_FEATURE_SSBD))
1132 cmd = ssb_parse_cmdline();
1133 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) &&
1134 (cmd == SPEC_STORE_BYPASS_CMD_NONE ||
1135 cmd == SPEC_STORE_BYPASS_CMD_AUTO))
1139 case SPEC_STORE_BYPASS_CMD_AUTO:
1140 case SPEC_STORE_BYPASS_CMD_SECCOMP:
1142 * Choose prctl+seccomp as the default mode if seccomp is
1145 if (IS_ENABLED(CONFIG_SECCOMP))
1146 mode = SPEC_STORE_BYPASS_SECCOMP;
1148 mode = SPEC_STORE_BYPASS_PRCTL;
1150 case SPEC_STORE_BYPASS_CMD_ON:
1151 mode = SPEC_STORE_BYPASS_DISABLE;
1153 case SPEC_STORE_BYPASS_CMD_PRCTL:
1154 mode = SPEC_STORE_BYPASS_PRCTL;
1156 case SPEC_STORE_BYPASS_CMD_NONE:
1161 * If SSBD is controlled by the SPEC_CTRL MSR, then set the proper
1162 * bit in the mask to allow guests to use the mitigation even in the
1163 * case where the host does not enable it.
1165 if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
1166 static_cpu_has(X86_FEATURE_AMD_SSBD)) {
1167 x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
1171 * We have three CPU feature flags that are in play here:
1172 * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
1173 * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
1174 * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
1176 if (mode == SPEC_STORE_BYPASS_DISABLE) {
1177 setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
1179 * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
1180 * use a completely different MSR and bit dependent on family.
1182 if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
1183 !static_cpu_has(X86_FEATURE_AMD_SSBD)) {
1184 x86_amd_ssb_disable();
1186 x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
1187 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
1194 static void ssb_select_mitigation(void)
1196 ssb_mode = __ssb_select_mitigation();
1198 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1199 pr_info("%s\n", ssb_strings[ssb_mode]);
1203 #define pr_fmt(fmt) "Speculation prctl: " fmt
1205 static void task_update_spec_tif(struct task_struct *tsk)
1207 /* Force the update of the real TIF bits */
1208 set_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE);
1211 * Immediately update the speculation control MSRs for the current
1212 * task, but for a non-current task delay setting the CPU
1213 * mitigation until it is scheduled next.
1215 * This can only happen for SECCOMP mitigation. For PRCTL it's
1216 * always the current task.
1219 speculation_ctrl_update_current();
1222 static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
1224 if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
1225 ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
1229 case PR_SPEC_ENABLE:
1230 /* If speculation is force disabled, enable is not allowed */
1231 if (task_spec_ssb_force_disable(task))
1233 task_clear_spec_ssb_disable(task);
1234 task_update_spec_tif(task);
1236 case PR_SPEC_DISABLE:
1237 task_set_spec_ssb_disable(task);
1238 task_update_spec_tif(task);
1240 case PR_SPEC_FORCE_DISABLE:
1241 task_set_spec_ssb_disable(task);
1242 task_set_spec_ssb_force_disable(task);
1243 task_update_spec_tif(task);
1251 static bool is_spec_ib_user_controlled(void)
1253 return spectre_v2_user_ibpb == SPECTRE_V2_USER_PRCTL ||
1254 spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
1255 spectre_v2_user_stibp == SPECTRE_V2_USER_PRCTL ||
1256 spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP;
1259 static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)
1262 case PR_SPEC_ENABLE:
1263 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1264 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
1268 * With strict mode for both IBPB and STIBP, the instruction
1269 * code paths avoid checking this task flag and instead,
1270 * unconditionally run the instruction. However, STIBP and IBPB
1271 * are independent and either can be set to conditionally
1272 * enabled regardless of the mode of the other.
1274 * If either is set to conditional, allow the task flag to be
1275 * updated, unless it was force-disabled by a previous prctl
1276 * call. Currently, this is possible on an AMD CPU which has the
1277 * feature X86_FEATURE_AMD_STIBP_ALWAYS_ON. In this case, if the
1278 * kernel is booted with 'spectre_v2_user=seccomp', then
1279 * spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP and
1280 * spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED.
1282 if (!is_spec_ib_user_controlled() ||
1283 task_spec_ib_force_disable(task))
1286 task_clear_spec_ib_disable(task);
1287 task_update_spec_tif(task);
1289 case PR_SPEC_DISABLE:
1290 case PR_SPEC_FORCE_DISABLE:
1292 * Indirect branch speculation is always allowed when
1293 * mitigation is force disabled.
1295 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1296 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
1299 if (!is_spec_ib_user_controlled())
1302 task_set_spec_ib_disable(task);
1303 if (ctrl == PR_SPEC_FORCE_DISABLE)
1304 task_set_spec_ib_force_disable(task);
1305 task_update_spec_tif(task);
1313 int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
1317 case PR_SPEC_STORE_BYPASS:
1318 return ssb_prctl_set(task, ctrl);
1319 case PR_SPEC_INDIRECT_BRANCH:
1320 return ib_prctl_set(task, ctrl);
1326 #ifdef CONFIG_SECCOMP
1327 void arch_seccomp_spec_mitigate(struct task_struct *task)
1329 if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP)
1330 ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
1331 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
1332 spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP)
1333 ib_prctl_set(task, PR_SPEC_FORCE_DISABLE);
1337 static int ssb_prctl_get(struct task_struct *task)
1340 case SPEC_STORE_BYPASS_DISABLE:
1341 return PR_SPEC_DISABLE;
1342 case SPEC_STORE_BYPASS_SECCOMP:
1343 case SPEC_STORE_BYPASS_PRCTL:
1344 if (task_spec_ssb_force_disable(task))
1345 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
1346 if (task_spec_ssb_disable(task))
1347 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
1348 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
1350 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1351 return PR_SPEC_ENABLE;
1352 return PR_SPEC_NOT_AFFECTED;
1356 static int ib_prctl_get(struct task_struct *task)
1358 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
1359 return PR_SPEC_NOT_AFFECTED;
1361 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1362 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
1363 return PR_SPEC_ENABLE;
1364 else if (is_spec_ib_user_controlled()) {
1365 if (task_spec_ib_force_disable(task))
1366 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
1367 if (task_spec_ib_disable(task))
1368 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
1369 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
1370 } else if (spectre_v2_user_ibpb == SPECTRE_V2_USER_STRICT ||
1371 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
1372 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED)
1373 return PR_SPEC_DISABLE;
1375 return PR_SPEC_NOT_AFFECTED;
1378 int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
1381 case PR_SPEC_STORE_BYPASS:
1382 return ssb_prctl_get(task);
1383 case PR_SPEC_INDIRECT_BRANCH:
1384 return ib_prctl_get(task);
1390 void x86_spec_ctrl_setup_ap(void)
1392 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
1393 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
1395 if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
1396 x86_amd_ssb_disable();
1399 bool itlb_multihit_kvm_mitigation;
1400 EXPORT_SYMBOL_GPL(itlb_multihit_kvm_mitigation);
1403 #define pr_fmt(fmt) "L1TF: " fmt
1405 /* Default mitigation for L1TF-affected CPUs */
1406 enum l1tf_mitigations l1tf_mitigation __ro_after_init = L1TF_MITIGATION_FLUSH;
1407 #if IS_ENABLED(CONFIG_KVM_INTEL)
1408 EXPORT_SYMBOL_GPL(l1tf_mitigation);
1410 enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
1411 EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation);
1414 * These CPUs all support 44bits physical address space internally in the
1415 * cache but CPUID can report a smaller number of physical address bits.
1417 * The L1TF mitigation uses the top most address bit for the inversion of
1418 * non present PTEs. When the installed memory reaches into the top most
1419 * address bit due to memory holes, which has been observed on machines
1420 * which report 36bits physical address bits and have 32G RAM installed,
1421 * then the mitigation range check in l1tf_select_mitigation() triggers.
1422 * This is a false positive because the mitigation is still possible due to
1423 * the fact that the cache uses 44bit internally. Use the cache bits
1424 * instead of the reported physical bits and adjust them on the affected
1425 * machines to 44bit if the reported bits are less than 44.
1427 static void override_cache_bits(struct cpuinfo_x86 *c)
1432 switch (c->x86_model) {
1433 case INTEL_FAM6_NEHALEM:
1434 case INTEL_FAM6_WESTMERE:
1435 case INTEL_FAM6_SANDYBRIDGE:
1436 case INTEL_FAM6_IVYBRIDGE:
1437 case INTEL_FAM6_HASWELL_CORE:
1438 case INTEL_FAM6_HASWELL_ULT:
1439 case INTEL_FAM6_HASWELL_GT3E:
1440 case INTEL_FAM6_BROADWELL_CORE:
1441 case INTEL_FAM6_BROADWELL_GT3E:
1442 case INTEL_FAM6_SKYLAKE_MOBILE:
1443 case INTEL_FAM6_SKYLAKE_DESKTOP:
1444 case INTEL_FAM6_KABYLAKE_MOBILE:
1445 case INTEL_FAM6_KABYLAKE_DESKTOP:
1446 if (c->x86_cache_bits < 44)
1447 c->x86_cache_bits = 44;
1452 static void __init l1tf_select_mitigation(void)
1456 if (!boot_cpu_has_bug(X86_BUG_L1TF))
1459 if (cpu_mitigations_off())
1460 l1tf_mitigation = L1TF_MITIGATION_OFF;
1461 else if (cpu_mitigations_auto_nosmt())
1462 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
1464 override_cache_bits(&boot_cpu_data);
1466 switch (l1tf_mitigation) {
1467 case L1TF_MITIGATION_OFF:
1468 case L1TF_MITIGATION_FLUSH_NOWARN:
1469 case L1TF_MITIGATION_FLUSH:
1471 case L1TF_MITIGATION_FLUSH_NOSMT:
1472 case L1TF_MITIGATION_FULL:
1473 cpu_smt_disable(false);
1475 case L1TF_MITIGATION_FULL_FORCE:
1476 cpu_smt_disable(true);
1480 #if CONFIG_PGTABLE_LEVELS == 2
1481 pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
1485 half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT;
1486 if (l1tf_mitigation != L1TF_MITIGATION_OFF &&
1487 e820_any_mapped(half_pa, ULLONG_MAX - half_pa, E820_RAM)) {
1488 pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
1489 pr_info("You may make it effective by booting the kernel with mem=%llu parameter.\n",
1491 pr_info("However, doing so will make a part of your RAM unusable.\n");
1492 pr_info("Reading https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html might help you decide.\n");
1496 setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV);
1499 static int __init l1tf_cmdline(char *str)
1501 if (!boot_cpu_has_bug(X86_BUG_L1TF))
1507 if (!strcmp(str, "off"))
1508 l1tf_mitigation = L1TF_MITIGATION_OFF;
1509 else if (!strcmp(str, "flush,nowarn"))
1510 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOWARN;
1511 else if (!strcmp(str, "flush"))
1512 l1tf_mitigation = L1TF_MITIGATION_FLUSH;
1513 else if (!strcmp(str, "flush,nosmt"))
1514 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
1515 else if (!strcmp(str, "full"))
1516 l1tf_mitigation = L1TF_MITIGATION_FULL;
1517 else if (!strcmp(str, "full,force"))
1518 l1tf_mitigation = L1TF_MITIGATION_FULL_FORCE;
1522 early_param("l1tf", l1tf_cmdline);
1525 #define pr_fmt(fmt) fmt
1529 #define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
1531 #if IS_ENABLED(CONFIG_KVM_INTEL)
1532 static const char * const l1tf_vmx_states[] = {
1533 [VMENTER_L1D_FLUSH_AUTO] = "auto",
1534 [VMENTER_L1D_FLUSH_NEVER] = "vulnerable",
1535 [VMENTER_L1D_FLUSH_COND] = "conditional cache flushes",
1536 [VMENTER_L1D_FLUSH_ALWAYS] = "cache flushes",
1537 [VMENTER_L1D_FLUSH_EPT_DISABLED] = "EPT disabled",
1538 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = "flush not necessary"
1541 static ssize_t l1tf_show_state(char *buf)
1543 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO)
1544 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
1546 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_EPT_DISABLED ||
1547 (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER &&
1548 sched_smt_active())) {
1549 return sprintf(buf, "%s; VMX: %s\n", L1TF_DEFAULT_MSG,
1550 l1tf_vmx_states[l1tf_vmx_mitigation]);
1553 return sprintf(buf, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG,
1554 l1tf_vmx_states[l1tf_vmx_mitigation],
1555 sched_smt_active() ? "vulnerable" : "disabled");
1558 static ssize_t itlb_multihit_show_state(char *buf)
1560 if (itlb_multihit_kvm_mitigation)
1561 return sprintf(buf, "KVM: Mitigation: Split huge pages\n");
1563 return sprintf(buf, "KVM: Vulnerable\n");
1566 static ssize_t l1tf_show_state(char *buf)
1568 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
1571 static ssize_t itlb_multihit_show_state(char *buf)
1573 return sprintf(buf, "Processor vulnerable\n");
1577 static ssize_t mds_show_state(char *buf)
1579 #ifdef CONFIG_HYPERVISOR_GUEST
1580 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
1581 return sprintf(buf, "%s; SMT Host state unknown\n",
1582 mds_strings[mds_mitigation]);
1586 if (boot_cpu_has(X86_BUG_MSBDS_ONLY)) {
1587 return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
1588 (mds_mitigation == MDS_MITIGATION_OFF ? "vulnerable" :
1589 sched_smt_active() ? "mitigated" : "disabled"));
1592 return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
1593 sched_smt_active() ? "vulnerable" : "disabled");
1596 static ssize_t tsx_async_abort_show_state(char *buf)
1598 if ((taa_mitigation == TAA_MITIGATION_TSX_DISABLED) ||
1599 (taa_mitigation == TAA_MITIGATION_OFF))
1600 return sprintf(buf, "%s\n", taa_strings[taa_mitigation]);
1602 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
1603 return sprintf(buf, "%s; SMT Host state unknown\n",
1604 taa_strings[taa_mitigation]);
1607 return sprintf(buf, "%s; SMT %s\n", taa_strings[taa_mitigation],
1608 sched_smt_active() ? "vulnerable" : "disabled");
1611 static char *stibp_state(void)
1613 if (spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
1616 switch (spectre_v2_user_stibp) {
1617 case SPECTRE_V2_USER_NONE:
1618 return ", STIBP: disabled";
1619 case SPECTRE_V2_USER_STRICT:
1620 return ", STIBP: forced";
1621 case SPECTRE_V2_USER_STRICT_PREFERRED:
1622 return ", STIBP: always-on";
1623 case SPECTRE_V2_USER_PRCTL:
1624 case SPECTRE_V2_USER_SECCOMP:
1625 if (static_key_enabled(&switch_to_cond_stibp))
1626 return ", STIBP: conditional";
1631 static char *ibpb_state(void)
1633 if (boot_cpu_has(X86_FEATURE_IBPB)) {
1634 if (static_key_enabled(&switch_mm_always_ibpb))
1635 return ", IBPB: always-on";
1636 if (static_key_enabled(&switch_mm_cond_ibpb))
1637 return ", IBPB: conditional";
1638 return ", IBPB: disabled";
1643 static ssize_t srbds_show_state(char *buf)
1645 return sprintf(buf, "%s\n", srbds_strings[srbds_mitigation]);
1648 static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
1649 char *buf, unsigned int bug)
1651 if (!boot_cpu_has_bug(bug))
1652 return sprintf(buf, "Not affected\n");
1655 case X86_BUG_CPU_MELTDOWN:
1656 if (boot_cpu_has(X86_FEATURE_KAISER))
1657 return sprintf(buf, "Mitigation: PTI\n");
1661 case X86_BUG_SPECTRE_V1:
1662 return sprintf(buf, "%s\n", spectre_v1_strings[spectre_v1_mitigation]);
1664 case X86_BUG_SPECTRE_V2:
1665 return sprintf(buf, "%s%s%s%s%s%s\n", spectre_v2_strings[spectre_v2_enabled],
1667 boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
1669 boot_cpu_has(X86_FEATURE_RSB_CTXSW) ? ", RSB filling" : "",
1670 spectre_v2_module_string());
1672 case X86_BUG_SPEC_STORE_BYPASS:
1673 return sprintf(buf, "%s\n", ssb_strings[ssb_mode]);
1676 if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV))
1677 return l1tf_show_state(buf);
1681 return mds_show_state(buf);
1684 return tsx_async_abort_show_state(buf);
1686 case X86_BUG_ITLB_MULTIHIT:
1687 return itlb_multihit_show_state(buf);
1690 return srbds_show_state(buf);
1696 return sprintf(buf, "Vulnerable\n");
1699 ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
1701 return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
1704 ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
1706 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
1709 ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
1711 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
1714 ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
1716 return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS);
1719 ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
1721 return cpu_show_common(dev, attr, buf, X86_BUG_L1TF);
1724 ssize_t cpu_show_mds(struct device *dev, struct device_attribute *attr, char *buf)
1726 return cpu_show_common(dev, attr, buf, X86_BUG_MDS);
1729 ssize_t cpu_show_tsx_async_abort(struct device *dev, struct device_attribute *attr, char *buf)
1731 return cpu_show_common(dev, attr, buf, X86_BUG_TAA);
1734 ssize_t cpu_show_itlb_multihit(struct device *dev, struct device_attribute *attr, char *buf)
1736 return cpu_show_common(dev, attr, buf, X86_BUG_ITLB_MULTIHIT);
1739 ssize_t cpu_show_srbds(struct device *dev, struct device_attribute *attr, char *buf)
1741 return cpu_show_common(dev, attr, buf, X86_BUG_SRBDS);