1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 1994 Linus Torvalds
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).
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>
19 #include <asm/spec-ctrl.h>
20 #include <asm/cmdline.h>
22 #include <asm/processor.h>
23 #include <asm/processor-flags.h>
24 #include <asm/fpu/internal.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>
37 static void __init spectre_v1_select_mitigation(void);
38 static void __init spectre_v2_select_mitigation(void);
39 static void __init ssb_select_mitigation(void);
40 static void __init l1tf_select_mitigation(void);
41 static void __init mds_select_mitigation(void);
42 static void __init mds_print_mitigation(void);
43 static void __init taa_select_mitigation(void);
44 static void __init srbds_select_mitigation(void);
46 /* The base value of the SPEC_CTRL MSR that always has to be preserved. */
47 u64 x86_spec_ctrl_base;
48 EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
49 static DEFINE_MUTEX(spec_ctrl_mutex);
52 * The vendor and possibly platform specific bits which can be modified in
55 static u64 __ro_after_init x86_spec_ctrl_mask = SPEC_CTRL_IBRS;
58 * AMD specific MSR info for Speculative Store Bypass control.
59 * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu().
61 u64 __ro_after_init x86_amd_ls_cfg_base;
62 u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask;
64 /* Control conditional STIBP in switch_to() */
65 DEFINE_STATIC_KEY_FALSE(switch_to_cond_stibp);
66 /* Control conditional IBPB in switch_mm() */
67 DEFINE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
68 /* Control unconditional IBPB in switch_mm() */
69 DEFINE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
71 /* Control MDS CPU buffer clear before returning to user space */
72 DEFINE_STATIC_KEY_FALSE(mds_user_clear);
73 EXPORT_SYMBOL_GPL(mds_user_clear);
74 /* Control MDS CPU buffer clear before idling (halt, mwait) */
75 DEFINE_STATIC_KEY_FALSE(mds_idle_clear);
76 EXPORT_SYMBOL_GPL(mds_idle_clear);
78 void __init check_bugs(void)
83 * identify_boot_cpu() initialized SMT support information, let the
86 cpu_smt_check_topology();
88 if (!IS_ENABLED(CONFIG_SMP)) {
90 print_cpu_info(&boot_cpu_data);
94 * Read the SPEC_CTRL MSR to account for reserved bits which may
95 * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
96 * init code as it is not enumerated and depends on the family.
98 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
99 rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
101 /* Allow STIBP in MSR_SPEC_CTRL if supported */
102 if (boot_cpu_has(X86_FEATURE_STIBP))
103 x86_spec_ctrl_mask |= SPEC_CTRL_STIBP;
105 /* Select the proper CPU mitigations before patching alternatives: */
106 spectre_v1_select_mitigation();
107 spectre_v2_select_mitigation();
108 ssb_select_mitigation();
109 l1tf_select_mitigation();
110 mds_select_mitigation();
111 taa_select_mitigation();
112 srbds_select_mitigation();
115 * As MDS and TAA mitigations are inter-related, print MDS
116 * mitigation until after TAA mitigation selection is done.
118 mds_print_mitigation();
124 * Check whether we are able to run this kernel safely on SMP.
126 * - i386 is no longer supported.
127 * - In order to run on anything without a TSC, we need to be
128 * compiled for a i486.
130 if (boot_cpu_data.x86 < 4)
131 panic("Kernel requires i486+ for 'invlpg' and other features");
133 init_utsname()->machine[1] =
134 '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
135 alternative_instructions();
137 fpu__init_check_bugs();
138 #else /* CONFIG_X86_64 */
139 alternative_instructions();
142 * Make sure the first 2MB area is not mapped by huge pages
143 * There are typically fixed size MTRRs in there and overlapping
144 * MTRRs into large pages causes slow downs.
146 * Right now we don't do that with gbpages because there seems
147 * very little benefit for that case.
150 set_memory_4k((unsigned long)__va(0), 1);
155 x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest)
157 u64 msrval, guestval, hostval = x86_spec_ctrl_base;
158 struct thread_info *ti = current_thread_info();
160 /* Is MSR_SPEC_CTRL implemented ? */
161 if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) {
163 * Restrict guest_spec_ctrl to supported values. Clear the
164 * modifiable bits in the host base value and or the
165 * modifiable bits from the guest value.
167 guestval = hostval & ~x86_spec_ctrl_mask;
168 guestval |= guest_spec_ctrl & x86_spec_ctrl_mask;
170 /* SSBD controlled in MSR_SPEC_CTRL */
171 if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
172 static_cpu_has(X86_FEATURE_AMD_SSBD))
173 hostval |= ssbd_tif_to_spec_ctrl(ti->flags);
175 /* Conditional STIBP enabled? */
176 if (static_branch_unlikely(&switch_to_cond_stibp))
177 hostval |= stibp_tif_to_spec_ctrl(ti->flags);
179 if (hostval != guestval) {
180 msrval = setguest ? guestval : hostval;
181 wrmsrl(MSR_IA32_SPEC_CTRL, msrval);
186 * If SSBD is not handled in MSR_SPEC_CTRL on AMD, update
187 * MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported.
189 if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
190 !static_cpu_has(X86_FEATURE_VIRT_SSBD))
194 * If the host has SSBD mitigation enabled, force it in the host's
195 * virtual MSR value. If its not permanently enabled, evaluate
196 * current's TIF_SSBD thread flag.
198 if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE))
199 hostval = SPEC_CTRL_SSBD;
201 hostval = ssbd_tif_to_spec_ctrl(ti->flags);
203 /* Sanitize the guest value */
204 guestval = guest_virt_spec_ctrl & SPEC_CTRL_SSBD;
206 if (hostval != guestval) {
209 tif = setguest ? ssbd_spec_ctrl_to_tif(guestval) :
210 ssbd_spec_ctrl_to_tif(hostval);
212 speculation_ctrl_update(tif);
215 EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl);
217 static void x86_amd_ssb_disable(void)
219 u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_ssbd_mask;
221 if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
222 wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, SPEC_CTRL_SSBD);
223 else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
224 wrmsrl(MSR_AMD64_LS_CFG, msrval);
228 #define pr_fmt(fmt) "MDS: " fmt
230 /* Default mitigation for MDS-affected CPUs */
231 static enum mds_mitigations mds_mitigation __ro_after_init = MDS_MITIGATION_FULL;
232 static bool mds_nosmt __ro_after_init = false;
234 static const char * const mds_strings[] = {
235 [MDS_MITIGATION_OFF] = "Vulnerable",
236 [MDS_MITIGATION_FULL] = "Mitigation: Clear CPU buffers",
237 [MDS_MITIGATION_VMWERV] = "Vulnerable: Clear CPU buffers attempted, no microcode",
240 static void __init mds_select_mitigation(void)
242 if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off()) {
243 mds_mitigation = MDS_MITIGATION_OFF;
247 if (mds_mitigation == MDS_MITIGATION_FULL) {
248 if (!boot_cpu_has(X86_FEATURE_MD_CLEAR))
249 mds_mitigation = MDS_MITIGATION_VMWERV;
251 static_branch_enable(&mds_user_clear);
253 if (!boot_cpu_has(X86_BUG_MSBDS_ONLY) &&
254 (mds_nosmt || cpu_mitigations_auto_nosmt()))
255 cpu_smt_disable(false);
259 static void __init mds_print_mitigation(void)
261 if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off())
264 pr_info("%s\n", mds_strings[mds_mitigation]);
267 static int __init mds_cmdline(char *str)
269 if (!boot_cpu_has_bug(X86_BUG_MDS))
275 if (!strcmp(str, "off"))
276 mds_mitigation = MDS_MITIGATION_OFF;
277 else if (!strcmp(str, "full"))
278 mds_mitigation = MDS_MITIGATION_FULL;
279 else if (!strcmp(str, "full,nosmt")) {
280 mds_mitigation = MDS_MITIGATION_FULL;
286 early_param("mds", mds_cmdline);
289 #define pr_fmt(fmt) "TAA: " fmt
291 enum taa_mitigations {
293 TAA_MITIGATION_UCODE_NEEDED,
295 TAA_MITIGATION_TSX_DISABLED,
298 /* Default mitigation for TAA-affected CPUs */
299 static enum taa_mitigations taa_mitigation __ro_after_init = TAA_MITIGATION_VERW;
300 static bool taa_nosmt __ro_after_init;
302 static const char * const taa_strings[] = {
303 [TAA_MITIGATION_OFF] = "Vulnerable",
304 [TAA_MITIGATION_UCODE_NEEDED] = "Vulnerable: Clear CPU buffers attempted, no microcode",
305 [TAA_MITIGATION_VERW] = "Mitigation: Clear CPU buffers",
306 [TAA_MITIGATION_TSX_DISABLED] = "Mitigation: TSX disabled",
309 static void __init taa_select_mitigation(void)
313 if (!boot_cpu_has_bug(X86_BUG_TAA)) {
314 taa_mitigation = TAA_MITIGATION_OFF;
318 /* TSX previously disabled by tsx=off */
319 if (!boot_cpu_has(X86_FEATURE_RTM)) {
320 taa_mitigation = TAA_MITIGATION_TSX_DISABLED;
324 if (cpu_mitigations_off()) {
325 taa_mitigation = TAA_MITIGATION_OFF;
330 * TAA mitigation via VERW is turned off if both
331 * tsx_async_abort=off and mds=off are specified.
333 if (taa_mitigation == TAA_MITIGATION_OFF &&
334 mds_mitigation == MDS_MITIGATION_OFF)
337 if (boot_cpu_has(X86_FEATURE_MD_CLEAR))
338 taa_mitigation = TAA_MITIGATION_VERW;
340 taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
343 * VERW doesn't clear the CPU buffers when MD_CLEAR=1 and MDS_NO=1.
344 * A microcode update fixes this behavior to clear CPU buffers. It also
345 * adds support for MSR_IA32_TSX_CTRL which is enumerated by the
346 * ARCH_CAP_TSX_CTRL_MSR bit.
348 * On MDS_NO=1 CPUs if ARCH_CAP_TSX_CTRL_MSR is not set, microcode
349 * update is required.
351 ia32_cap = x86_read_arch_cap_msr();
352 if ( (ia32_cap & ARCH_CAP_MDS_NO) &&
353 !(ia32_cap & ARCH_CAP_TSX_CTRL_MSR))
354 taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
357 * TSX is enabled, select alternate mitigation for TAA which is
358 * the same as MDS. Enable MDS static branch to clear CPU buffers.
360 * For guests that can't determine whether the correct microcode is
361 * present on host, enable the mitigation for UCODE_NEEDED as well.
363 static_branch_enable(&mds_user_clear);
365 if (taa_nosmt || cpu_mitigations_auto_nosmt())
366 cpu_smt_disable(false);
369 * Update MDS mitigation, if necessary, as the mds_user_clear is
370 * now enabled for TAA mitigation.
372 if (mds_mitigation == MDS_MITIGATION_OFF &&
373 boot_cpu_has_bug(X86_BUG_MDS)) {
374 mds_mitigation = MDS_MITIGATION_FULL;
375 mds_select_mitigation();
378 pr_info("%s\n", taa_strings[taa_mitigation]);
381 static int __init tsx_async_abort_parse_cmdline(char *str)
383 if (!boot_cpu_has_bug(X86_BUG_TAA))
389 if (!strcmp(str, "off")) {
390 taa_mitigation = TAA_MITIGATION_OFF;
391 } else if (!strcmp(str, "full")) {
392 taa_mitigation = TAA_MITIGATION_VERW;
393 } else if (!strcmp(str, "full,nosmt")) {
394 taa_mitigation = TAA_MITIGATION_VERW;
400 early_param("tsx_async_abort", tsx_async_abort_parse_cmdline);
403 #define pr_fmt(fmt) "SRBDS: " fmt
405 enum srbds_mitigations {
406 SRBDS_MITIGATION_OFF,
407 SRBDS_MITIGATION_UCODE_NEEDED,
408 SRBDS_MITIGATION_FULL,
409 SRBDS_MITIGATION_TSX_OFF,
410 SRBDS_MITIGATION_HYPERVISOR,
413 static enum srbds_mitigations srbds_mitigation __ro_after_init = SRBDS_MITIGATION_FULL;
415 static const char * const srbds_strings[] = {
416 [SRBDS_MITIGATION_OFF] = "Vulnerable",
417 [SRBDS_MITIGATION_UCODE_NEEDED] = "Vulnerable: No microcode",
418 [SRBDS_MITIGATION_FULL] = "Mitigation: Microcode",
419 [SRBDS_MITIGATION_TSX_OFF] = "Mitigation: TSX disabled",
420 [SRBDS_MITIGATION_HYPERVISOR] = "Unknown: Dependent on hypervisor status",
423 static bool srbds_off;
425 void update_srbds_msr(void)
429 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
432 if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
435 if (srbds_mitigation == SRBDS_MITIGATION_UCODE_NEEDED)
438 rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
440 switch (srbds_mitigation) {
441 case SRBDS_MITIGATION_OFF:
442 case SRBDS_MITIGATION_TSX_OFF:
443 mcu_ctrl |= RNGDS_MITG_DIS;
445 case SRBDS_MITIGATION_FULL:
446 mcu_ctrl &= ~RNGDS_MITG_DIS;
452 wrmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
455 static void __init srbds_select_mitigation(void)
459 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
463 * Check to see if this is one of the MDS_NO systems supporting
464 * TSX that are only exposed to SRBDS when TSX is enabled.
466 ia32_cap = x86_read_arch_cap_msr();
467 if ((ia32_cap & ARCH_CAP_MDS_NO) && !boot_cpu_has(X86_FEATURE_RTM))
468 srbds_mitigation = SRBDS_MITIGATION_TSX_OFF;
469 else if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
470 srbds_mitigation = SRBDS_MITIGATION_HYPERVISOR;
471 else if (!boot_cpu_has(X86_FEATURE_SRBDS_CTRL))
472 srbds_mitigation = SRBDS_MITIGATION_UCODE_NEEDED;
473 else if (cpu_mitigations_off() || srbds_off)
474 srbds_mitigation = SRBDS_MITIGATION_OFF;
477 pr_info("%s\n", srbds_strings[srbds_mitigation]);
480 static int __init srbds_parse_cmdline(char *str)
485 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
488 srbds_off = !strcmp(str, "off");
491 early_param("srbds", srbds_parse_cmdline);
494 #define pr_fmt(fmt) "Spectre V1 : " fmt
496 enum spectre_v1_mitigation {
497 SPECTRE_V1_MITIGATION_NONE,
498 SPECTRE_V1_MITIGATION_AUTO,
501 static enum spectre_v1_mitigation spectre_v1_mitigation __ro_after_init =
502 SPECTRE_V1_MITIGATION_AUTO;
504 static const char * const spectre_v1_strings[] = {
505 [SPECTRE_V1_MITIGATION_NONE] = "Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers",
506 [SPECTRE_V1_MITIGATION_AUTO] = "Mitigation: usercopy/swapgs barriers and __user pointer sanitization",
510 * Does SMAP provide full mitigation against speculative kernel access to
513 static bool smap_works_speculatively(void)
515 if (!boot_cpu_has(X86_FEATURE_SMAP))
519 * On CPUs which are vulnerable to Meltdown, SMAP does not
520 * prevent speculative access to user data in the L1 cache.
521 * Consider SMAP to be non-functional as a mitigation on these
524 if (boot_cpu_has(X86_BUG_CPU_MELTDOWN))
530 static void __init spectre_v1_select_mitigation(void)
532 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1) || cpu_mitigations_off()) {
533 spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
537 if (spectre_v1_mitigation == SPECTRE_V1_MITIGATION_AUTO) {
539 * With Spectre v1, a user can speculatively control either
540 * path of a conditional swapgs with a user-controlled GS
541 * value. The mitigation is to add lfences to both code paths.
543 * If FSGSBASE is enabled, the user can put a kernel address in
544 * GS, in which case SMAP provides no protection.
546 * [ NOTE: Don't check for X86_FEATURE_FSGSBASE until the
547 * FSGSBASE enablement patches have been merged. ]
549 * If FSGSBASE is disabled, the user can only put a user space
550 * address in GS. That makes an attack harder, but still
551 * possible if there's no SMAP protection.
553 if (!smap_works_speculatively()) {
555 * Mitigation can be provided from SWAPGS itself or
556 * PTI as the CR3 write in the Meltdown mitigation
559 * If neither is there, mitigate with an LFENCE to
560 * stop speculation through swapgs.
562 if (boot_cpu_has_bug(X86_BUG_SWAPGS) &&
563 !boot_cpu_has(X86_FEATURE_PTI))
564 setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_USER);
567 * Enable lfences in the kernel entry (non-swapgs)
568 * paths, to prevent user entry from speculatively
571 setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_KERNEL);
575 pr_info("%s\n", spectre_v1_strings[spectre_v1_mitigation]);
578 static int __init nospectre_v1_cmdline(char *str)
580 spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
583 early_param("nospectre_v1", nospectre_v1_cmdline);
586 #define pr_fmt(fmt) "Spectre V2 : " fmt
588 static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init =
591 static enum spectre_v2_user_mitigation spectre_v2_user_stibp __ro_after_init =
592 SPECTRE_V2_USER_NONE;
593 static enum spectre_v2_user_mitigation spectre_v2_user_ibpb __ro_after_init =
594 SPECTRE_V2_USER_NONE;
596 #ifdef CONFIG_RETPOLINE
597 static bool spectre_v2_bad_module;
599 bool retpoline_module_ok(bool has_retpoline)
601 if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
604 pr_err("System may be vulnerable to spectre v2\n");
605 spectre_v2_bad_module = true;
609 static inline const char *spectre_v2_module_string(void)
611 return spectre_v2_bad_module ? " - vulnerable module loaded" : "";
614 static inline const char *spectre_v2_module_string(void) { return ""; }
617 static inline bool match_option(const char *arg, int arglen, const char *opt)
619 int len = strlen(opt);
621 return len == arglen && !strncmp(arg, opt, len);
624 /* The kernel command line selection for spectre v2 */
625 enum spectre_v2_mitigation_cmd {
628 SPECTRE_V2_CMD_FORCE,
629 SPECTRE_V2_CMD_RETPOLINE,
630 SPECTRE_V2_CMD_RETPOLINE_GENERIC,
631 SPECTRE_V2_CMD_RETPOLINE_AMD,
634 enum spectre_v2_user_cmd {
635 SPECTRE_V2_USER_CMD_NONE,
636 SPECTRE_V2_USER_CMD_AUTO,
637 SPECTRE_V2_USER_CMD_FORCE,
638 SPECTRE_V2_USER_CMD_PRCTL,
639 SPECTRE_V2_USER_CMD_PRCTL_IBPB,
640 SPECTRE_V2_USER_CMD_SECCOMP,
641 SPECTRE_V2_USER_CMD_SECCOMP_IBPB,
644 static const char * const spectre_v2_user_strings[] = {
645 [SPECTRE_V2_USER_NONE] = "User space: Vulnerable",
646 [SPECTRE_V2_USER_STRICT] = "User space: Mitigation: STIBP protection",
647 [SPECTRE_V2_USER_STRICT_PREFERRED] = "User space: Mitigation: STIBP always-on protection",
648 [SPECTRE_V2_USER_PRCTL] = "User space: Mitigation: STIBP via prctl",
649 [SPECTRE_V2_USER_SECCOMP] = "User space: Mitigation: STIBP via seccomp and prctl",
652 static const struct {
654 enum spectre_v2_user_cmd cmd;
656 } v2_user_options[] __initconst = {
657 { "auto", SPECTRE_V2_USER_CMD_AUTO, false },
658 { "off", SPECTRE_V2_USER_CMD_NONE, false },
659 { "on", SPECTRE_V2_USER_CMD_FORCE, true },
660 { "prctl", SPECTRE_V2_USER_CMD_PRCTL, false },
661 { "prctl,ibpb", SPECTRE_V2_USER_CMD_PRCTL_IBPB, false },
662 { "seccomp", SPECTRE_V2_USER_CMD_SECCOMP, false },
663 { "seccomp,ibpb", SPECTRE_V2_USER_CMD_SECCOMP_IBPB, false },
666 static void __init spec_v2_user_print_cond(const char *reason, bool secure)
668 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
669 pr_info("spectre_v2_user=%s forced on command line.\n", reason);
672 static enum spectre_v2_user_cmd __init
673 spectre_v2_parse_user_cmdline(enum spectre_v2_mitigation_cmd v2_cmd)
679 case SPECTRE_V2_CMD_NONE:
680 return SPECTRE_V2_USER_CMD_NONE;
681 case SPECTRE_V2_CMD_FORCE:
682 return SPECTRE_V2_USER_CMD_FORCE;
687 ret = cmdline_find_option(boot_command_line, "spectre_v2_user",
690 return SPECTRE_V2_USER_CMD_AUTO;
692 for (i = 0; i < ARRAY_SIZE(v2_user_options); i++) {
693 if (match_option(arg, ret, v2_user_options[i].option)) {
694 spec_v2_user_print_cond(v2_user_options[i].option,
695 v2_user_options[i].secure);
696 return v2_user_options[i].cmd;
700 pr_err("Unknown user space protection option (%s). Switching to AUTO select\n", arg);
701 return SPECTRE_V2_USER_CMD_AUTO;
705 spectre_v2_user_select_mitigation(enum spectre_v2_mitigation_cmd v2_cmd)
707 enum spectre_v2_user_mitigation mode = SPECTRE_V2_USER_NONE;
708 bool smt_possible = IS_ENABLED(CONFIG_SMP);
709 enum spectre_v2_user_cmd cmd;
711 if (!boot_cpu_has(X86_FEATURE_IBPB) && !boot_cpu_has(X86_FEATURE_STIBP))
714 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
715 cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
716 smt_possible = false;
718 cmd = spectre_v2_parse_user_cmdline(v2_cmd);
720 case SPECTRE_V2_USER_CMD_NONE:
722 case SPECTRE_V2_USER_CMD_FORCE:
723 mode = SPECTRE_V2_USER_STRICT;
725 case SPECTRE_V2_USER_CMD_PRCTL:
726 case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
727 mode = SPECTRE_V2_USER_PRCTL;
729 case SPECTRE_V2_USER_CMD_AUTO:
730 case SPECTRE_V2_USER_CMD_SECCOMP:
731 case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
732 if (IS_ENABLED(CONFIG_SECCOMP))
733 mode = SPECTRE_V2_USER_SECCOMP;
735 mode = SPECTRE_V2_USER_PRCTL;
739 /* Initialize Indirect Branch Prediction Barrier */
740 if (boot_cpu_has(X86_FEATURE_IBPB)) {
741 setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
744 case SPECTRE_V2_USER_CMD_FORCE:
745 case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
746 case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
747 static_branch_enable(&switch_mm_always_ibpb);
749 case SPECTRE_V2_USER_CMD_PRCTL:
750 case SPECTRE_V2_USER_CMD_AUTO:
751 case SPECTRE_V2_USER_CMD_SECCOMP:
752 static_branch_enable(&switch_mm_cond_ibpb);
758 pr_info("mitigation: Enabling %s Indirect Branch Prediction Barrier\n",
759 static_key_enabled(&switch_mm_always_ibpb) ?
760 "always-on" : "conditional");
762 spectre_v2_user_ibpb = mode;
766 * If enhanced IBRS is enabled or SMT impossible, STIBP is not
769 if (!smt_possible || spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
773 * At this point, an STIBP mode other than "off" has been set.
774 * If STIBP support is not being forced, check if STIBP always-on
777 if (mode != SPECTRE_V2_USER_STRICT &&
778 boot_cpu_has(X86_FEATURE_AMD_STIBP_ALWAYS_ON))
779 mode = SPECTRE_V2_USER_STRICT_PREFERRED;
782 * If STIBP is not available, clear the STIBP mode.
784 if (!boot_cpu_has(X86_FEATURE_STIBP))
785 mode = SPECTRE_V2_USER_NONE;
787 spectre_v2_user_stibp = mode;
790 pr_info("%s\n", spectre_v2_user_strings[mode]);
793 static const char * const spectre_v2_strings[] = {
794 [SPECTRE_V2_NONE] = "Vulnerable",
795 [SPECTRE_V2_RETPOLINE_GENERIC] = "Mitigation: Full generic retpoline",
796 [SPECTRE_V2_RETPOLINE_AMD] = "Mitigation: Full AMD retpoline",
797 [SPECTRE_V2_IBRS_ENHANCED] = "Mitigation: Enhanced IBRS",
800 static const struct {
802 enum spectre_v2_mitigation_cmd cmd;
804 } mitigation_options[] __initconst = {
805 { "off", SPECTRE_V2_CMD_NONE, false },
806 { "on", SPECTRE_V2_CMD_FORCE, true },
807 { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false },
808 { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_AMD, false },
809 { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
810 { "auto", SPECTRE_V2_CMD_AUTO, false },
813 static void __init spec_v2_print_cond(const char *reason, bool secure)
815 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
816 pr_info("%s selected on command line.\n", reason);
819 static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
821 enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
825 if (cmdline_find_option_bool(boot_command_line, "nospectre_v2") ||
826 cpu_mitigations_off())
827 return SPECTRE_V2_CMD_NONE;
829 ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
831 return SPECTRE_V2_CMD_AUTO;
833 for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
834 if (!match_option(arg, ret, mitigation_options[i].option))
836 cmd = mitigation_options[i].cmd;
840 if (i >= ARRAY_SIZE(mitigation_options)) {
841 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
842 return SPECTRE_V2_CMD_AUTO;
845 if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
846 cmd == SPECTRE_V2_CMD_RETPOLINE_AMD ||
847 cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC) &&
848 !IS_ENABLED(CONFIG_RETPOLINE)) {
849 pr_err("%s selected but not compiled in. Switching to AUTO select\n", mitigation_options[i].option);
850 return SPECTRE_V2_CMD_AUTO;
853 if (cmd == SPECTRE_V2_CMD_RETPOLINE_AMD &&
854 boot_cpu_data.x86_vendor != X86_VENDOR_HYGON &&
855 boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
856 pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n");
857 return SPECTRE_V2_CMD_AUTO;
860 spec_v2_print_cond(mitigation_options[i].option,
861 mitigation_options[i].secure);
865 static void __init spectre_v2_select_mitigation(void)
867 enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
868 enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
871 * If the CPU is not affected and the command line mode is NONE or AUTO
872 * then nothing to do.
874 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
875 (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
879 case SPECTRE_V2_CMD_NONE:
882 case SPECTRE_V2_CMD_FORCE:
883 case SPECTRE_V2_CMD_AUTO:
884 if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
885 mode = SPECTRE_V2_IBRS_ENHANCED;
886 /* Force it so VMEXIT will restore correctly */
887 x86_spec_ctrl_base |= SPEC_CTRL_IBRS;
888 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
889 goto specv2_set_mode;
891 if (IS_ENABLED(CONFIG_RETPOLINE))
894 case SPECTRE_V2_CMD_RETPOLINE_AMD:
895 if (IS_ENABLED(CONFIG_RETPOLINE))
898 case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
899 if (IS_ENABLED(CONFIG_RETPOLINE))
900 goto retpoline_generic;
902 case SPECTRE_V2_CMD_RETPOLINE:
903 if (IS_ENABLED(CONFIG_RETPOLINE))
907 pr_err("Spectre mitigation: kernel not compiled with retpoline; no mitigation available!");
911 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
912 boot_cpu_data.x86_vendor == X86_VENDOR_HYGON) {
914 if (!boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
915 pr_err("Spectre mitigation: LFENCE not serializing, switching to generic retpoline\n");
916 goto retpoline_generic;
918 mode = SPECTRE_V2_RETPOLINE_AMD;
919 setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD);
920 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
923 mode = SPECTRE_V2_RETPOLINE_GENERIC;
924 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
928 spectre_v2_enabled = mode;
929 pr_info("%s\n", spectre_v2_strings[mode]);
932 * If spectre v2 protection has been enabled, unconditionally fill
933 * RSB during a context switch; this protects against two independent
936 * - RSB underflow (and switch to BTB) on Skylake+
937 * - SpectreRSB variant of spectre v2 on X86_BUG_SPECTRE_V2 CPUs
939 setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
940 pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n");
943 * Retpoline means the kernel is safe because it has no indirect
944 * branches. Enhanced IBRS protects firmware too, so, enable restricted
945 * speculation around firmware calls only when Enhanced IBRS isn't
948 * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because
949 * the user might select retpoline on the kernel command line and if
950 * the CPU supports Enhanced IBRS, kernel might un-intentionally not
951 * enable IBRS around firmware calls.
953 if (boot_cpu_has(X86_FEATURE_IBRS) && mode != SPECTRE_V2_IBRS_ENHANCED) {
954 setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
955 pr_info("Enabling Restricted Speculation for firmware calls\n");
958 /* Set up IBPB and STIBP depending on the general spectre V2 command */
959 spectre_v2_user_select_mitigation(cmd);
962 static void update_stibp_msr(void * __unused)
964 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
967 /* Update x86_spec_ctrl_base in case SMT state changed. */
968 static void update_stibp_strict(void)
970 u64 mask = x86_spec_ctrl_base & ~SPEC_CTRL_STIBP;
972 if (sched_smt_active())
973 mask |= SPEC_CTRL_STIBP;
975 if (mask == x86_spec_ctrl_base)
978 pr_info("Update user space SMT mitigation: STIBP %s\n",
979 mask & SPEC_CTRL_STIBP ? "always-on" : "off");
980 x86_spec_ctrl_base = mask;
981 on_each_cpu(update_stibp_msr, NULL, 1);
984 /* Update the static key controlling the evaluation of TIF_SPEC_IB */
985 static void update_indir_branch_cond(void)
987 if (sched_smt_active())
988 static_branch_enable(&switch_to_cond_stibp);
990 static_branch_disable(&switch_to_cond_stibp);
994 #define pr_fmt(fmt) fmt
996 /* Update the static key controlling the MDS CPU buffer clear in idle */
997 static void update_mds_branch_idle(void)
1000 * Enable the idle clearing if SMT is active on CPUs which are
1001 * affected only by MSBDS and not any other MDS variant.
1003 * The other variants cannot be mitigated when SMT is enabled, so
1004 * clearing the buffers on idle just to prevent the Store Buffer
1005 * repartitioning leak would be a window dressing exercise.
1007 if (!boot_cpu_has_bug(X86_BUG_MSBDS_ONLY))
1010 if (sched_smt_active())
1011 static_branch_enable(&mds_idle_clear);
1013 static_branch_disable(&mds_idle_clear);
1016 #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"
1017 #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"
1019 void cpu_bugs_smt_update(void)
1021 mutex_lock(&spec_ctrl_mutex);
1023 switch (spectre_v2_user_stibp) {
1024 case SPECTRE_V2_USER_NONE:
1026 case SPECTRE_V2_USER_STRICT:
1027 case SPECTRE_V2_USER_STRICT_PREFERRED:
1028 update_stibp_strict();
1030 case SPECTRE_V2_USER_PRCTL:
1031 case SPECTRE_V2_USER_SECCOMP:
1032 update_indir_branch_cond();
1036 switch (mds_mitigation) {
1037 case MDS_MITIGATION_FULL:
1038 case MDS_MITIGATION_VMWERV:
1039 if (sched_smt_active() && !boot_cpu_has(X86_BUG_MSBDS_ONLY))
1040 pr_warn_once(MDS_MSG_SMT);
1041 update_mds_branch_idle();
1043 case MDS_MITIGATION_OFF:
1047 switch (taa_mitigation) {
1048 case TAA_MITIGATION_VERW:
1049 case TAA_MITIGATION_UCODE_NEEDED:
1050 if (sched_smt_active())
1051 pr_warn_once(TAA_MSG_SMT);
1053 case TAA_MITIGATION_TSX_DISABLED:
1054 case TAA_MITIGATION_OFF:
1058 mutex_unlock(&spec_ctrl_mutex);
1062 #define pr_fmt(fmt) "Speculative Store Bypass: " fmt
1064 static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
1066 /* The kernel command line selection */
1067 enum ssb_mitigation_cmd {
1068 SPEC_STORE_BYPASS_CMD_NONE,
1069 SPEC_STORE_BYPASS_CMD_AUTO,
1070 SPEC_STORE_BYPASS_CMD_ON,
1071 SPEC_STORE_BYPASS_CMD_PRCTL,
1072 SPEC_STORE_BYPASS_CMD_SECCOMP,
1075 static const char * const ssb_strings[] = {
1076 [SPEC_STORE_BYPASS_NONE] = "Vulnerable",
1077 [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass disabled",
1078 [SPEC_STORE_BYPASS_PRCTL] = "Mitigation: Speculative Store Bypass disabled via prctl",
1079 [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
1082 static const struct {
1084 enum ssb_mitigation_cmd cmd;
1085 } ssb_mitigation_options[] __initconst = {
1086 { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */
1087 { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */
1088 { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */
1089 { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative Store Bypass via prctl */
1090 { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */
1093 static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
1095 enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO;
1099 if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable") ||
1100 cpu_mitigations_off()) {
1101 return SPEC_STORE_BYPASS_CMD_NONE;
1103 ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable",
1106 return SPEC_STORE_BYPASS_CMD_AUTO;
1108 for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) {
1109 if (!match_option(arg, ret, ssb_mitigation_options[i].option))
1112 cmd = ssb_mitigation_options[i].cmd;
1116 if (i >= ARRAY_SIZE(ssb_mitigation_options)) {
1117 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
1118 return SPEC_STORE_BYPASS_CMD_AUTO;
1125 static enum ssb_mitigation __init __ssb_select_mitigation(void)
1127 enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
1128 enum ssb_mitigation_cmd cmd;
1130 if (!boot_cpu_has(X86_FEATURE_SSBD))
1133 cmd = ssb_parse_cmdline();
1134 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) &&
1135 (cmd == SPEC_STORE_BYPASS_CMD_NONE ||
1136 cmd == SPEC_STORE_BYPASS_CMD_AUTO))
1140 case SPEC_STORE_BYPASS_CMD_AUTO:
1141 case SPEC_STORE_BYPASS_CMD_SECCOMP:
1143 * Choose prctl+seccomp as the default mode if seccomp is
1146 if (IS_ENABLED(CONFIG_SECCOMP))
1147 mode = SPEC_STORE_BYPASS_SECCOMP;
1149 mode = SPEC_STORE_BYPASS_PRCTL;
1151 case SPEC_STORE_BYPASS_CMD_ON:
1152 mode = SPEC_STORE_BYPASS_DISABLE;
1154 case SPEC_STORE_BYPASS_CMD_PRCTL:
1155 mode = SPEC_STORE_BYPASS_PRCTL;
1157 case SPEC_STORE_BYPASS_CMD_NONE:
1162 * If SSBD is controlled by the SPEC_CTRL MSR, then set the proper
1163 * bit in the mask to allow guests to use the mitigation even in the
1164 * case where the host does not enable it.
1166 if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
1167 static_cpu_has(X86_FEATURE_AMD_SSBD)) {
1168 x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
1172 * We have three CPU feature flags that are in play here:
1173 * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
1174 * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
1175 * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
1177 if (mode == SPEC_STORE_BYPASS_DISABLE) {
1178 setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
1180 * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
1181 * use a completely different MSR and bit dependent on family.
1183 if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
1184 !static_cpu_has(X86_FEATURE_AMD_SSBD)) {
1185 x86_amd_ssb_disable();
1187 x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
1188 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
1195 static void ssb_select_mitigation(void)
1197 ssb_mode = __ssb_select_mitigation();
1199 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1200 pr_info("%s\n", ssb_strings[ssb_mode]);
1204 #define pr_fmt(fmt) "Speculation prctl: " fmt
1206 static void task_update_spec_tif(struct task_struct *tsk)
1208 /* Force the update of the real TIF bits */
1209 set_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE);
1212 * Immediately update the speculation control MSRs for the current
1213 * task, but for a non-current task delay setting the CPU
1214 * mitigation until it is scheduled next.
1216 * This can only happen for SECCOMP mitigation. For PRCTL it's
1217 * always the current task.
1220 speculation_ctrl_update_current();
1223 static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
1225 if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
1226 ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
1230 case PR_SPEC_ENABLE:
1231 /* If speculation is force disabled, enable is not allowed */
1232 if (task_spec_ssb_force_disable(task))
1234 task_clear_spec_ssb_disable(task);
1235 task_clear_spec_ssb_noexec(task);
1236 task_update_spec_tif(task);
1238 case PR_SPEC_DISABLE:
1239 task_set_spec_ssb_disable(task);
1240 task_clear_spec_ssb_noexec(task);
1241 task_update_spec_tif(task);
1243 case PR_SPEC_FORCE_DISABLE:
1244 task_set_spec_ssb_disable(task);
1245 task_set_spec_ssb_force_disable(task);
1246 task_clear_spec_ssb_noexec(task);
1247 task_update_spec_tif(task);
1249 case PR_SPEC_DISABLE_NOEXEC:
1250 if (task_spec_ssb_force_disable(task))
1252 task_set_spec_ssb_disable(task);
1253 task_set_spec_ssb_noexec(task);
1254 task_update_spec_tif(task);
1262 static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)
1265 case PR_SPEC_ENABLE:
1266 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1267 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
1270 * Indirect branch speculation is always disabled in strict
1271 * mode. It can neither be enabled if it was force-disabled
1272 * by a previous prctl call.
1275 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_STRICT ||
1276 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
1277 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED ||
1278 task_spec_ib_force_disable(task))
1280 task_clear_spec_ib_disable(task);
1281 task_update_spec_tif(task);
1283 case PR_SPEC_DISABLE:
1284 case PR_SPEC_FORCE_DISABLE:
1286 * Indirect branch speculation is always allowed when
1287 * mitigation is force disabled.
1289 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1290 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
1292 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_STRICT ||
1293 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
1294 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED)
1296 task_set_spec_ib_disable(task);
1297 if (ctrl == PR_SPEC_FORCE_DISABLE)
1298 task_set_spec_ib_force_disable(task);
1299 task_update_spec_tif(task);
1307 int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
1311 case PR_SPEC_STORE_BYPASS:
1312 return ssb_prctl_set(task, ctrl);
1313 case PR_SPEC_INDIRECT_BRANCH:
1314 return ib_prctl_set(task, ctrl);
1320 #ifdef CONFIG_SECCOMP
1321 void arch_seccomp_spec_mitigate(struct task_struct *task)
1323 if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP)
1324 ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
1325 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
1326 spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP)
1327 ib_prctl_set(task, PR_SPEC_FORCE_DISABLE);
1331 static int ssb_prctl_get(struct task_struct *task)
1334 case SPEC_STORE_BYPASS_DISABLE:
1335 return PR_SPEC_DISABLE;
1336 case SPEC_STORE_BYPASS_SECCOMP:
1337 case SPEC_STORE_BYPASS_PRCTL:
1338 if (task_spec_ssb_force_disable(task))
1339 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
1340 if (task_spec_ssb_noexec(task))
1341 return PR_SPEC_PRCTL | PR_SPEC_DISABLE_NOEXEC;
1342 if (task_spec_ssb_disable(task))
1343 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
1344 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
1346 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1347 return PR_SPEC_ENABLE;
1348 return PR_SPEC_NOT_AFFECTED;
1352 static int ib_prctl_get(struct task_struct *task)
1354 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
1355 return PR_SPEC_NOT_AFFECTED;
1357 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1358 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
1359 return PR_SPEC_ENABLE;
1360 else if (spectre_v2_user_ibpb == SPECTRE_V2_USER_STRICT ||
1361 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
1362 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED)
1363 return PR_SPEC_DISABLE;
1364 else if (spectre_v2_user_ibpb == SPECTRE_V2_USER_PRCTL ||
1365 spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
1366 spectre_v2_user_stibp == SPECTRE_V2_USER_PRCTL ||
1367 spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP) {
1368 if (task_spec_ib_force_disable(task))
1369 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
1370 if (task_spec_ib_disable(task))
1371 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
1372 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
1374 return PR_SPEC_NOT_AFFECTED;
1377 int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
1380 case PR_SPEC_STORE_BYPASS:
1381 return ssb_prctl_get(task);
1382 case PR_SPEC_INDIRECT_BRANCH:
1383 return ib_prctl_get(task);
1389 void x86_spec_ctrl_setup_ap(void)
1391 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
1392 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
1394 if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
1395 x86_amd_ssb_disable();
1398 bool itlb_multihit_kvm_mitigation;
1399 EXPORT_SYMBOL_GPL(itlb_multihit_kvm_mitigation);
1402 #define pr_fmt(fmt) "L1TF: " fmt
1404 /* Default mitigation for L1TF-affected CPUs */
1405 enum l1tf_mitigations l1tf_mitigation __ro_after_init = L1TF_MITIGATION_FLUSH;
1406 #if IS_ENABLED(CONFIG_KVM_INTEL)
1407 EXPORT_SYMBOL_GPL(l1tf_mitigation);
1409 enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
1410 EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation);
1413 * These CPUs all support 44bits physical address space internally in the
1414 * cache but CPUID can report a smaller number of physical address bits.
1416 * The L1TF mitigation uses the top most address bit for the inversion of
1417 * non present PTEs. When the installed memory reaches into the top most
1418 * address bit due to memory holes, which has been observed on machines
1419 * which report 36bits physical address bits and have 32G RAM installed,
1420 * then the mitigation range check in l1tf_select_mitigation() triggers.
1421 * This is a false positive because the mitigation is still possible due to
1422 * the fact that the cache uses 44bit internally. Use the cache bits
1423 * instead of the reported physical bits and adjust them on the affected
1424 * machines to 44bit if the reported bits are less than 44.
1426 static void override_cache_bits(struct cpuinfo_x86 *c)
1431 switch (c->x86_model) {
1432 case INTEL_FAM6_NEHALEM:
1433 case INTEL_FAM6_WESTMERE:
1434 case INTEL_FAM6_SANDYBRIDGE:
1435 case INTEL_FAM6_IVYBRIDGE:
1436 case INTEL_FAM6_HASWELL:
1437 case INTEL_FAM6_HASWELL_L:
1438 case INTEL_FAM6_HASWELL_G:
1439 case INTEL_FAM6_BROADWELL:
1440 case INTEL_FAM6_BROADWELL_G:
1441 case INTEL_FAM6_SKYLAKE_L:
1442 case INTEL_FAM6_SKYLAKE:
1443 case INTEL_FAM6_KABYLAKE_L:
1444 case INTEL_FAM6_KABYLAKE:
1445 if (c->x86_cache_bits < 44)
1446 c->x86_cache_bits = 44;
1451 static void __init l1tf_select_mitigation(void)
1455 if (!boot_cpu_has_bug(X86_BUG_L1TF))
1458 if (cpu_mitigations_off())
1459 l1tf_mitigation = L1TF_MITIGATION_OFF;
1460 else if (cpu_mitigations_auto_nosmt())
1461 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
1463 override_cache_bits(&boot_cpu_data);
1465 switch (l1tf_mitigation) {
1466 case L1TF_MITIGATION_OFF:
1467 case L1TF_MITIGATION_FLUSH_NOWARN:
1468 case L1TF_MITIGATION_FLUSH:
1470 case L1TF_MITIGATION_FLUSH_NOSMT:
1471 case L1TF_MITIGATION_FULL:
1472 cpu_smt_disable(false);
1474 case L1TF_MITIGATION_FULL_FORCE:
1475 cpu_smt_disable(true);
1479 #if CONFIG_PGTABLE_LEVELS == 2
1480 pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
1484 half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT;
1485 if (l1tf_mitigation != L1TF_MITIGATION_OFF &&
1486 e820__mapped_any(half_pa, ULLONG_MAX - half_pa, E820_TYPE_RAM)) {
1487 pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
1488 pr_info("You may make it effective by booting the kernel with mem=%llu parameter.\n",
1490 pr_info("However, doing so will make a part of your RAM unusable.\n");
1491 pr_info("Reading https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html might help you decide.\n");
1495 setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV);
1498 static int __init l1tf_cmdline(char *str)
1500 if (!boot_cpu_has_bug(X86_BUG_L1TF))
1506 if (!strcmp(str, "off"))
1507 l1tf_mitigation = L1TF_MITIGATION_OFF;
1508 else if (!strcmp(str, "flush,nowarn"))
1509 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOWARN;
1510 else if (!strcmp(str, "flush"))
1511 l1tf_mitigation = L1TF_MITIGATION_FLUSH;
1512 else if (!strcmp(str, "flush,nosmt"))
1513 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
1514 else if (!strcmp(str, "full"))
1515 l1tf_mitigation = L1TF_MITIGATION_FULL;
1516 else if (!strcmp(str, "full,force"))
1517 l1tf_mitigation = L1TF_MITIGATION_FULL_FORCE;
1521 early_param("l1tf", l1tf_cmdline);
1524 #define pr_fmt(fmt) fmt
1528 #define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
1530 #if IS_ENABLED(CONFIG_KVM_INTEL)
1531 static const char * const l1tf_vmx_states[] = {
1532 [VMENTER_L1D_FLUSH_AUTO] = "auto",
1533 [VMENTER_L1D_FLUSH_NEVER] = "vulnerable",
1534 [VMENTER_L1D_FLUSH_COND] = "conditional cache flushes",
1535 [VMENTER_L1D_FLUSH_ALWAYS] = "cache flushes",
1536 [VMENTER_L1D_FLUSH_EPT_DISABLED] = "EPT disabled",
1537 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = "flush not necessary"
1540 static ssize_t l1tf_show_state(char *buf)
1542 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO)
1543 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
1545 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_EPT_DISABLED ||
1546 (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER &&
1547 sched_smt_active())) {
1548 return sprintf(buf, "%s; VMX: %s\n", L1TF_DEFAULT_MSG,
1549 l1tf_vmx_states[l1tf_vmx_mitigation]);
1552 return sprintf(buf, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG,
1553 l1tf_vmx_states[l1tf_vmx_mitigation],
1554 sched_smt_active() ? "vulnerable" : "disabled");
1557 static ssize_t itlb_multihit_show_state(char *buf)
1559 if (itlb_multihit_kvm_mitigation)
1560 return sprintf(buf, "KVM: Mitigation: Split huge pages\n");
1562 return sprintf(buf, "KVM: Vulnerable\n");
1565 static ssize_t l1tf_show_state(char *buf)
1567 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
1570 static ssize_t itlb_multihit_show_state(char *buf)
1572 return sprintf(buf, "Processor vulnerable\n");
1576 static ssize_t mds_show_state(char *buf)
1578 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
1579 return sprintf(buf, "%s; SMT Host state unknown\n",
1580 mds_strings[mds_mitigation]);
1583 if (boot_cpu_has(X86_BUG_MSBDS_ONLY)) {
1584 return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
1585 (mds_mitigation == MDS_MITIGATION_OFF ? "vulnerable" :
1586 sched_smt_active() ? "mitigated" : "disabled"));
1589 return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
1590 sched_smt_active() ? "vulnerable" : "disabled");
1593 static ssize_t tsx_async_abort_show_state(char *buf)
1595 if ((taa_mitigation == TAA_MITIGATION_TSX_DISABLED) ||
1596 (taa_mitigation == TAA_MITIGATION_OFF))
1597 return sprintf(buf, "%s\n", taa_strings[taa_mitigation]);
1599 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
1600 return sprintf(buf, "%s; SMT Host state unknown\n",
1601 taa_strings[taa_mitigation]);
1604 return sprintf(buf, "%s; SMT %s\n", taa_strings[taa_mitigation],
1605 sched_smt_active() ? "vulnerable" : "disabled");
1608 static char *stibp_state(void)
1610 if (spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
1613 switch (spectre_v2_user_stibp) {
1614 case SPECTRE_V2_USER_NONE:
1615 return ", STIBP: disabled";
1616 case SPECTRE_V2_USER_STRICT:
1617 return ", STIBP: forced";
1618 case SPECTRE_V2_USER_STRICT_PREFERRED:
1619 return ", STIBP: always-on";
1620 case SPECTRE_V2_USER_PRCTL:
1621 case SPECTRE_V2_USER_SECCOMP:
1622 if (static_key_enabled(&switch_to_cond_stibp))
1623 return ", STIBP: conditional";
1628 static char *ibpb_state(void)
1630 if (boot_cpu_has(X86_FEATURE_IBPB)) {
1631 if (static_key_enabled(&switch_mm_always_ibpb))
1632 return ", IBPB: always-on";
1633 if (static_key_enabled(&switch_mm_cond_ibpb))
1634 return ", IBPB: conditional";
1635 return ", IBPB: disabled";
1640 static ssize_t srbds_show_state(char *buf)
1642 return sprintf(buf, "%s\n", srbds_strings[srbds_mitigation]);
1645 static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
1646 char *buf, unsigned int bug)
1648 if (!boot_cpu_has_bug(bug))
1649 return sprintf(buf, "Not affected\n");
1652 case X86_BUG_CPU_MELTDOWN:
1653 if (boot_cpu_has(X86_FEATURE_PTI))
1654 return sprintf(buf, "Mitigation: PTI\n");
1656 if (hypervisor_is_type(X86_HYPER_XEN_PV))
1657 return sprintf(buf, "Unknown (XEN PV detected, hypervisor mitigation required)\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);