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>
18 #include <linux/pgtable.h>
19 #include <linux/bpf.h>
21 #include <asm/spec-ctrl.h>
22 #include <asm/cmdline.h>
24 #include <asm/processor.h>
25 #include <asm/processor-flags.h>
26 #include <asm/fpu/api.h>
29 #include <asm/paravirt.h>
30 #include <asm/alternative.h>
31 #include <asm/set_memory.h>
32 #include <asm/intel-family.h>
33 #include <asm/e820/api.h>
34 #include <asm/hypervisor.h>
35 #include <asm/tlbflush.h>
39 static void __init spectre_v1_select_mitigation(void);
40 static void __init spectre_v2_select_mitigation(void);
41 static void __init ssb_select_mitigation(void);
42 static void __init l1tf_select_mitigation(void);
43 static void __init mds_select_mitigation(void);
44 static void __init mds_print_mitigation(void);
45 static void __init taa_select_mitigation(void);
46 static void __init srbds_select_mitigation(void);
47 static void __init l1d_flush_select_mitigation(void);
49 /* The base value of the SPEC_CTRL MSR that always has to be preserved. */
50 u64 x86_spec_ctrl_base;
51 EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
52 static DEFINE_MUTEX(spec_ctrl_mutex);
55 * The vendor and possibly platform specific bits which can be modified in
58 static u64 __ro_after_init x86_spec_ctrl_mask = SPEC_CTRL_IBRS;
61 * AMD specific MSR info for Speculative Store Bypass control.
62 * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu().
64 u64 __ro_after_init x86_amd_ls_cfg_base;
65 u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask;
67 /* Control conditional STIBP in switch_to() */
68 DEFINE_STATIC_KEY_FALSE(switch_to_cond_stibp);
69 /* Control conditional IBPB in switch_mm() */
70 DEFINE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
71 /* Control unconditional IBPB in switch_mm() */
72 DEFINE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
74 /* Control MDS CPU buffer clear before returning to user space */
75 DEFINE_STATIC_KEY_FALSE(mds_user_clear);
76 EXPORT_SYMBOL_GPL(mds_user_clear);
77 /* Control MDS CPU buffer clear before idling (halt, mwait) */
78 DEFINE_STATIC_KEY_FALSE(mds_idle_clear);
79 EXPORT_SYMBOL_GPL(mds_idle_clear);
82 * Controls whether l1d flush based mitigations are enabled,
83 * based on hw features and admin setting via boot parameter
86 DEFINE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);
88 void __init check_bugs(void)
93 * identify_boot_cpu() initialized SMT support information, let the
96 cpu_smt_check_topology();
98 if (!IS_ENABLED(CONFIG_SMP)) {
100 print_cpu_info(&boot_cpu_data);
104 * Read the SPEC_CTRL MSR to account for reserved bits which may
105 * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
106 * init code as it is not enumerated and depends on the family.
108 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
109 rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
111 /* Allow STIBP in MSR_SPEC_CTRL if supported */
112 if (boot_cpu_has(X86_FEATURE_STIBP))
113 x86_spec_ctrl_mask |= SPEC_CTRL_STIBP;
115 /* Select the proper CPU mitigations before patching alternatives: */
116 spectre_v1_select_mitigation();
117 spectre_v2_select_mitigation();
118 ssb_select_mitigation();
119 l1tf_select_mitigation();
120 mds_select_mitigation();
121 taa_select_mitigation();
122 srbds_select_mitigation();
123 l1d_flush_select_mitigation();
126 * As MDS and TAA mitigations are inter-related, print MDS
127 * mitigation until after TAA mitigation selection is done.
129 mds_print_mitigation();
135 * Check whether we are able to run this kernel safely on SMP.
137 * - i386 is no longer supported.
138 * - In order to run on anything without a TSC, we need to be
139 * compiled for a i486.
141 if (boot_cpu_data.x86 < 4)
142 panic("Kernel requires i486+ for 'invlpg' and other features");
144 init_utsname()->machine[1] =
145 '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
146 alternative_instructions();
148 fpu__init_check_bugs();
149 #else /* CONFIG_X86_64 */
150 alternative_instructions();
153 * Make sure the first 2MB area is not mapped by huge pages
154 * There are typically fixed size MTRRs in there and overlapping
155 * MTRRs into large pages causes slow downs.
157 * Right now we don't do that with gbpages because there seems
158 * very little benefit for that case.
161 set_memory_4k((unsigned long)__va(0), 1);
166 x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest)
168 u64 msrval, guestval, hostval = x86_spec_ctrl_base;
169 struct thread_info *ti = current_thread_info();
171 /* Is MSR_SPEC_CTRL implemented ? */
172 if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) {
174 * Restrict guest_spec_ctrl to supported values. Clear the
175 * modifiable bits in the host base value and or the
176 * modifiable bits from the guest value.
178 guestval = hostval & ~x86_spec_ctrl_mask;
179 guestval |= guest_spec_ctrl & x86_spec_ctrl_mask;
181 /* SSBD controlled in MSR_SPEC_CTRL */
182 if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
183 static_cpu_has(X86_FEATURE_AMD_SSBD))
184 hostval |= ssbd_tif_to_spec_ctrl(ti->flags);
186 /* Conditional STIBP enabled? */
187 if (static_branch_unlikely(&switch_to_cond_stibp))
188 hostval |= stibp_tif_to_spec_ctrl(ti->flags);
190 if (hostval != guestval) {
191 msrval = setguest ? guestval : hostval;
192 wrmsrl(MSR_IA32_SPEC_CTRL, msrval);
197 * If SSBD is not handled in MSR_SPEC_CTRL on AMD, update
198 * MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported.
200 if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
201 !static_cpu_has(X86_FEATURE_VIRT_SSBD))
205 * If the host has SSBD mitigation enabled, force it in the host's
206 * virtual MSR value. If its not permanently enabled, evaluate
207 * current's TIF_SSBD thread flag.
209 if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE))
210 hostval = SPEC_CTRL_SSBD;
212 hostval = ssbd_tif_to_spec_ctrl(ti->flags);
214 /* Sanitize the guest value */
215 guestval = guest_virt_spec_ctrl & SPEC_CTRL_SSBD;
217 if (hostval != guestval) {
220 tif = setguest ? ssbd_spec_ctrl_to_tif(guestval) :
221 ssbd_spec_ctrl_to_tif(hostval);
223 speculation_ctrl_update(tif);
226 EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl);
228 static void x86_amd_ssb_disable(void)
230 u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_ssbd_mask;
232 if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
233 wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, SPEC_CTRL_SSBD);
234 else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
235 wrmsrl(MSR_AMD64_LS_CFG, msrval);
239 #define pr_fmt(fmt) "MDS: " fmt
241 /* Default mitigation for MDS-affected CPUs */
242 static enum mds_mitigations mds_mitigation __ro_after_init = MDS_MITIGATION_FULL;
243 static bool mds_nosmt __ro_after_init = false;
245 static const char * const mds_strings[] = {
246 [MDS_MITIGATION_OFF] = "Vulnerable",
247 [MDS_MITIGATION_FULL] = "Mitigation: Clear CPU buffers",
248 [MDS_MITIGATION_VMWERV] = "Vulnerable: Clear CPU buffers attempted, no microcode",
251 static void __init mds_select_mitigation(void)
253 if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off()) {
254 mds_mitigation = MDS_MITIGATION_OFF;
258 if (mds_mitigation == MDS_MITIGATION_FULL) {
259 if (!boot_cpu_has(X86_FEATURE_MD_CLEAR))
260 mds_mitigation = MDS_MITIGATION_VMWERV;
262 static_branch_enable(&mds_user_clear);
264 if (!boot_cpu_has(X86_BUG_MSBDS_ONLY) &&
265 (mds_nosmt || cpu_mitigations_auto_nosmt()))
266 cpu_smt_disable(false);
270 static void __init mds_print_mitigation(void)
272 if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off())
275 pr_info("%s\n", mds_strings[mds_mitigation]);
278 static int __init mds_cmdline(char *str)
280 if (!boot_cpu_has_bug(X86_BUG_MDS))
286 if (!strcmp(str, "off"))
287 mds_mitigation = MDS_MITIGATION_OFF;
288 else if (!strcmp(str, "full"))
289 mds_mitigation = MDS_MITIGATION_FULL;
290 else if (!strcmp(str, "full,nosmt")) {
291 mds_mitigation = MDS_MITIGATION_FULL;
297 early_param("mds", mds_cmdline);
300 #define pr_fmt(fmt) "TAA: " fmt
302 enum taa_mitigations {
304 TAA_MITIGATION_UCODE_NEEDED,
306 TAA_MITIGATION_TSX_DISABLED,
309 /* Default mitigation for TAA-affected CPUs */
310 static enum taa_mitigations taa_mitigation __ro_after_init = TAA_MITIGATION_VERW;
311 static bool taa_nosmt __ro_after_init;
313 static const char * const taa_strings[] = {
314 [TAA_MITIGATION_OFF] = "Vulnerable",
315 [TAA_MITIGATION_UCODE_NEEDED] = "Vulnerable: Clear CPU buffers attempted, no microcode",
316 [TAA_MITIGATION_VERW] = "Mitigation: Clear CPU buffers",
317 [TAA_MITIGATION_TSX_DISABLED] = "Mitigation: TSX disabled",
320 static void __init taa_select_mitigation(void)
324 if (!boot_cpu_has_bug(X86_BUG_TAA)) {
325 taa_mitigation = TAA_MITIGATION_OFF;
329 /* TSX previously disabled by tsx=off */
330 if (!boot_cpu_has(X86_FEATURE_RTM)) {
331 taa_mitigation = TAA_MITIGATION_TSX_DISABLED;
335 if (cpu_mitigations_off()) {
336 taa_mitigation = TAA_MITIGATION_OFF;
341 * TAA mitigation via VERW is turned off if both
342 * tsx_async_abort=off and mds=off are specified.
344 if (taa_mitigation == TAA_MITIGATION_OFF &&
345 mds_mitigation == MDS_MITIGATION_OFF)
348 if (boot_cpu_has(X86_FEATURE_MD_CLEAR))
349 taa_mitigation = TAA_MITIGATION_VERW;
351 taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
354 * VERW doesn't clear the CPU buffers when MD_CLEAR=1 and MDS_NO=1.
355 * A microcode update fixes this behavior to clear CPU buffers. It also
356 * adds support for MSR_IA32_TSX_CTRL which is enumerated by the
357 * ARCH_CAP_TSX_CTRL_MSR bit.
359 * On MDS_NO=1 CPUs if ARCH_CAP_TSX_CTRL_MSR is not set, microcode
360 * update is required.
362 ia32_cap = x86_read_arch_cap_msr();
363 if ( (ia32_cap & ARCH_CAP_MDS_NO) &&
364 !(ia32_cap & ARCH_CAP_TSX_CTRL_MSR))
365 taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
368 * TSX is enabled, select alternate mitigation for TAA which is
369 * the same as MDS. Enable MDS static branch to clear CPU buffers.
371 * For guests that can't determine whether the correct microcode is
372 * present on host, enable the mitigation for UCODE_NEEDED as well.
374 static_branch_enable(&mds_user_clear);
376 if (taa_nosmt || cpu_mitigations_auto_nosmt())
377 cpu_smt_disable(false);
380 * Update MDS mitigation, if necessary, as the mds_user_clear is
381 * now enabled for TAA mitigation.
383 if (mds_mitigation == MDS_MITIGATION_OFF &&
384 boot_cpu_has_bug(X86_BUG_MDS)) {
385 mds_mitigation = MDS_MITIGATION_FULL;
386 mds_select_mitigation();
389 pr_info("%s\n", taa_strings[taa_mitigation]);
392 static int __init tsx_async_abort_parse_cmdline(char *str)
394 if (!boot_cpu_has_bug(X86_BUG_TAA))
400 if (!strcmp(str, "off")) {
401 taa_mitigation = TAA_MITIGATION_OFF;
402 } else if (!strcmp(str, "full")) {
403 taa_mitigation = TAA_MITIGATION_VERW;
404 } else if (!strcmp(str, "full,nosmt")) {
405 taa_mitigation = TAA_MITIGATION_VERW;
411 early_param("tsx_async_abort", tsx_async_abort_parse_cmdline);
414 #define pr_fmt(fmt) "SRBDS: " fmt
416 enum srbds_mitigations {
417 SRBDS_MITIGATION_OFF,
418 SRBDS_MITIGATION_UCODE_NEEDED,
419 SRBDS_MITIGATION_FULL,
420 SRBDS_MITIGATION_TSX_OFF,
421 SRBDS_MITIGATION_HYPERVISOR,
424 static enum srbds_mitigations srbds_mitigation __ro_after_init = SRBDS_MITIGATION_FULL;
426 static const char * const srbds_strings[] = {
427 [SRBDS_MITIGATION_OFF] = "Vulnerable",
428 [SRBDS_MITIGATION_UCODE_NEEDED] = "Vulnerable: No microcode",
429 [SRBDS_MITIGATION_FULL] = "Mitigation: Microcode",
430 [SRBDS_MITIGATION_TSX_OFF] = "Mitigation: TSX disabled",
431 [SRBDS_MITIGATION_HYPERVISOR] = "Unknown: Dependent on hypervisor status",
434 static bool srbds_off;
436 void update_srbds_msr(void)
440 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
443 if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
446 if (srbds_mitigation == SRBDS_MITIGATION_UCODE_NEEDED)
450 * A MDS_NO CPU for which SRBDS mitigation is not needed due to TSX
451 * being disabled and it hasn't received the SRBDS MSR microcode.
453 if (!boot_cpu_has(X86_FEATURE_SRBDS_CTRL))
456 rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
458 switch (srbds_mitigation) {
459 case SRBDS_MITIGATION_OFF:
460 case SRBDS_MITIGATION_TSX_OFF:
461 mcu_ctrl |= RNGDS_MITG_DIS;
463 case SRBDS_MITIGATION_FULL:
464 mcu_ctrl &= ~RNGDS_MITG_DIS;
470 wrmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
473 static void __init srbds_select_mitigation(void)
477 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
481 * Check to see if this is one of the MDS_NO systems supporting
482 * TSX that are only exposed to SRBDS when TSX is enabled.
484 ia32_cap = x86_read_arch_cap_msr();
485 if ((ia32_cap & ARCH_CAP_MDS_NO) && !boot_cpu_has(X86_FEATURE_RTM))
486 srbds_mitigation = SRBDS_MITIGATION_TSX_OFF;
487 else if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
488 srbds_mitigation = SRBDS_MITIGATION_HYPERVISOR;
489 else if (!boot_cpu_has(X86_FEATURE_SRBDS_CTRL))
490 srbds_mitigation = SRBDS_MITIGATION_UCODE_NEEDED;
491 else if (cpu_mitigations_off() || srbds_off)
492 srbds_mitigation = SRBDS_MITIGATION_OFF;
495 pr_info("%s\n", srbds_strings[srbds_mitigation]);
498 static int __init srbds_parse_cmdline(char *str)
503 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
506 srbds_off = !strcmp(str, "off");
509 early_param("srbds", srbds_parse_cmdline);
512 #define pr_fmt(fmt) "L1D Flush : " fmt
514 enum l1d_flush_mitigations {
519 static enum l1d_flush_mitigations l1d_flush_mitigation __initdata = L1D_FLUSH_OFF;
521 static void __init l1d_flush_select_mitigation(void)
523 if (!l1d_flush_mitigation || !boot_cpu_has(X86_FEATURE_FLUSH_L1D))
526 static_branch_enable(&switch_mm_cond_l1d_flush);
527 pr_info("Conditional flush on switch_mm() enabled\n");
530 static int __init l1d_flush_parse_cmdline(char *str)
532 if (!strcmp(str, "on"))
533 l1d_flush_mitigation = L1D_FLUSH_ON;
537 early_param("l1d_flush", l1d_flush_parse_cmdline);
540 #define pr_fmt(fmt) "Spectre V1 : " fmt
542 enum spectre_v1_mitigation {
543 SPECTRE_V1_MITIGATION_NONE,
544 SPECTRE_V1_MITIGATION_AUTO,
547 static enum spectre_v1_mitigation spectre_v1_mitigation __ro_after_init =
548 SPECTRE_V1_MITIGATION_AUTO;
550 static const char * const spectre_v1_strings[] = {
551 [SPECTRE_V1_MITIGATION_NONE] = "Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers",
552 [SPECTRE_V1_MITIGATION_AUTO] = "Mitigation: usercopy/swapgs barriers and __user pointer sanitization",
556 * Does SMAP provide full mitigation against speculative kernel access to
559 static bool smap_works_speculatively(void)
561 if (!boot_cpu_has(X86_FEATURE_SMAP))
565 * On CPUs which are vulnerable to Meltdown, SMAP does not
566 * prevent speculative access to user data in the L1 cache.
567 * Consider SMAP to be non-functional as a mitigation on these
570 if (boot_cpu_has(X86_BUG_CPU_MELTDOWN))
576 static void __init spectre_v1_select_mitigation(void)
578 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1) || cpu_mitigations_off()) {
579 spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
583 if (spectre_v1_mitigation == SPECTRE_V1_MITIGATION_AUTO) {
585 * With Spectre v1, a user can speculatively control either
586 * path of a conditional swapgs with a user-controlled GS
587 * value. The mitigation is to add lfences to both code paths.
589 * If FSGSBASE is enabled, the user can put a kernel address in
590 * GS, in which case SMAP provides no protection.
592 * If FSGSBASE is disabled, the user can only put a user space
593 * address in GS. That makes an attack harder, but still
594 * possible if there's no SMAP protection.
596 if (boot_cpu_has(X86_FEATURE_FSGSBASE) ||
597 !smap_works_speculatively()) {
599 * Mitigation can be provided from SWAPGS itself or
600 * PTI as the CR3 write in the Meltdown mitigation
603 * If neither is there, mitigate with an LFENCE to
604 * stop speculation through swapgs.
606 if (boot_cpu_has_bug(X86_BUG_SWAPGS) &&
607 !boot_cpu_has(X86_FEATURE_PTI))
608 setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_USER);
611 * Enable lfences in the kernel entry (non-swapgs)
612 * paths, to prevent user entry from speculatively
615 setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_KERNEL);
619 pr_info("%s\n", spectre_v1_strings[spectre_v1_mitigation]);
622 static int __init nospectre_v1_cmdline(char *str)
624 spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
627 early_param("nospectre_v1", nospectre_v1_cmdline);
630 #define pr_fmt(fmt) "Spectre V2 : " fmt
632 static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init =
635 static enum spectre_v2_user_mitigation spectre_v2_user_stibp __ro_after_init =
636 SPECTRE_V2_USER_NONE;
637 static enum spectre_v2_user_mitigation spectre_v2_user_ibpb __ro_after_init =
638 SPECTRE_V2_USER_NONE;
640 #ifdef CONFIG_RETPOLINE
641 static bool spectre_v2_bad_module;
643 bool retpoline_module_ok(bool has_retpoline)
645 if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
648 pr_err("System may be vulnerable to spectre v2\n");
649 spectre_v2_bad_module = true;
653 static inline const char *spectre_v2_module_string(void)
655 return spectre_v2_bad_module ? " - vulnerable module loaded" : "";
658 static inline const char *spectre_v2_module_string(void) { return ""; }
661 #define SPECTRE_V2_LFENCE_MSG "WARNING: LFENCE mitigation is not recommended for this CPU, data leaks possible!\n"
662 #define SPECTRE_V2_EIBRS_EBPF_MSG "WARNING: Unprivileged eBPF is enabled with eIBRS on, data leaks possible via Spectre v2 BHB attacks!\n"
663 #define SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG "WARNING: Unprivileged eBPF is enabled with eIBRS+LFENCE mitigation and SMT, data leaks possible via Spectre v2 BHB attacks!\n"
665 #ifdef CONFIG_BPF_SYSCALL
666 void unpriv_ebpf_notify(int new_state)
671 /* Unprivileged eBPF is enabled */
673 switch (spectre_v2_enabled) {
674 case SPECTRE_V2_EIBRS:
675 pr_err(SPECTRE_V2_EIBRS_EBPF_MSG);
677 case SPECTRE_V2_EIBRS_LFENCE:
678 if (sched_smt_active())
679 pr_err(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG);
687 static inline bool match_option(const char *arg, int arglen, const char *opt)
689 int len = strlen(opt);
691 return len == arglen && !strncmp(arg, opt, len);
694 /* The kernel command line selection for spectre v2 */
695 enum spectre_v2_mitigation_cmd {
698 SPECTRE_V2_CMD_FORCE,
699 SPECTRE_V2_CMD_RETPOLINE,
700 SPECTRE_V2_CMD_RETPOLINE_GENERIC,
701 SPECTRE_V2_CMD_RETPOLINE_LFENCE,
702 SPECTRE_V2_CMD_EIBRS,
703 SPECTRE_V2_CMD_EIBRS_RETPOLINE,
704 SPECTRE_V2_CMD_EIBRS_LFENCE,
707 enum spectre_v2_user_cmd {
708 SPECTRE_V2_USER_CMD_NONE,
709 SPECTRE_V2_USER_CMD_AUTO,
710 SPECTRE_V2_USER_CMD_FORCE,
711 SPECTRE_V2_USER_CMD_PRCTL,
712 SPECTRE_V2_USER_CMD_PRCTL_IBPB,
713 SPECTRE_V2_USER_CMD_SECCOMP,
714 SPECTRE_V2_USER_CMD_SECCOMP_IBPB,
717 static const char * const spectre_v2_user_strings[] = {
718 [SPECTRE_V2_USER_NONE] = "User space: Vulnerable",
719 [SPECTRE_V2_USER_STRICT] = "User space: Mitigation: STIBP protection",
720 [SPECTRE_V2_USER_STRICT_PREFERRED] = "User space: Mitigation: STIBP always-on protection",
721 [SPECTRE_V2_USER_PRCTL] = "User space: Mitigation: STIBP via prctl",
722 [SPECTRE_V2_USER_SECCOMP] = "User space: Mitigation: STIBP via seccomp and prctl",
725 static const struct {
727 enum spectre_v2_user_cmd cmd;
729 } v2_user_options[] __initconst = {
730 { "auto", SPECTRE_V2_USER_CMD_AUTO, false },
731 { "off", SPECTRE_V2_USER_CMD_NONE, false },
732 { "on", SPECTRE_V2_USER_CMD_FORCE, true },
733 { "prctl", SPECTRE_V2_USER_CMD_PRCTL, false },
734 { "prctl,ibpb", SPECTRE_V2_USER_CMD_PRCTL_IBPB, false },
735 { "seccomp", SPECTRE_V2_USER_CMD_SECCOMP, false },
736 { "seccomp,ibpb", SPECTRE_V2_USER_CMD_SECCOMP_IBPB, false },
739 static void __init spec_v2_user_print_cond(const char *reason, bool secure)
741 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
742 pr_info("spectre_v2_user=%s forced on command line.\n", reason);
745 static enum spectre_v2_user_cmd __init
746 spectre_v2_parse_user_cmdline(enum spectre_v2_mitigation_cmd v2_cmd)
752 case SPECTRE_V2_CMD_NONE:
753 return SPECTRE_V2_USER_CMD_NONE;
754 case SPECTRE_V2_CMD_FORCE:
755 return SPECTRE_V2_USER_CMD_FORCE;
760 ret = cmdline_find_option(boot_command_line, "spectre_v2_user",
763 return SPECTRE_V2_USER_CMD_AUTO;
765 for (i = 0; i < ARRAY_SIZE(v2_user_options); i++) {
766 if (match_option(arg, ret, v2_user_options[i].option)) {
767 spec_v2_user_print_cond(v2_user_options[i].option,
768 v2_user_options[i].secure);
769 return v2_user_options[i].cmd;
773 pr_err("Unknown user space protection option (%s). Switching to AUTO select\n", arg);
774 return SPECTRE_V2_USER_CMD_AUTO;
777 static inline bool spectre_v2_in_eibrs_mode(enum spectre_v2_mitigation mode)
779 return (mode == SPECTRE_V2_EIBRS ||
780 mode == SPECTRE_V2_EIBRS_RETPOLINE ||
781 mode == SPECTRE_V2_EIBRS_LFENCE);
785 spectre_v2_user_select_mitigation(enum spectre_v2_mitigation_cmd v2_cmd)
787 enum spectre_v2_user_mitigation mode = SPECTRE_V2_USER_NONE;
788 bool smt_possible = IS_ENABLED(CONFIG_SMP);
789 enum spectre_v2_user_cmd cmd;
791 if (!boot_cpu_has(X86_FEATURE_IBPB) && !boot_cpu_has(X86_FEATURE_STIBP))
794 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
795 cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
796 smt_possible = false;
798 cmd = spectre_v2_parse_user_cmdline(v2_cmd);
800 case SPECTRE_V2_USER_CMD_NONE:
802 case SPECTRE_V2_USER_CMD_FORCE:
803 mode = SPECTRE_V2_USER_STRICT;
805 case SPECTRE_V2_USER_CMD_AUTO:
806 case SPECTRE_V2_USER_CMD_PRCTL:
807 case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
808 mode = SPECTRE_V2_USER_PRCTL;
810 case SPECTRE_V2_USER_CMD_SECCOMP:
811 case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
812 if (IS_ENABLED(CONFIG_SECCOMP))
813 mode = SPECTRE_V2_USER_SECCOMP;
815 mode = SPECTRE_V2_USER_PRCTL;
819 /* Initialize Indirect Branch Prediction Barrier */
820 if (boot_cpu_has(X86_FEATURE_IBPB)) {
821 setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
823 spectre_v2_user_ibpb = mode;
825 case SPECTRE_V2_USER_CMD_FORCE:
826 case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
827 case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
828 static_branch_enable(&switch_mm_always_ibpb);
829 spectre_v2_user_ibpb = SPECTRE_V2_USER_STRICT;
831 case SPECTRE_V2_USER_CMD_PRCTL:
832 case SPECTRE_V2_USER_CMD_AUTO:
833 case SPECTRE_V2_USER_CMD_SECCOMP:
834 static_branch_enable(&switch_mm_cond_ibpb);
840 pr_info("mitigation: Enabling %s Indirect Branch Prediction Barrier\n",
841 static_key_enabled(&switch_mm_always_ibpb) ?
842 "always-on" : "conditional");
846 * If no STIBP, enhanced IBRS is enabled or SMT impossible, STIBP is not
849 if (!boot_cpu_has(X86_FEATURE_STIBP) ||
851 spectre_v2_in_eibrs_mode(spectre_v2_enabled))
855 * At this point, an STIBP mode other than "off" has been set.
856 * If STIBP support is not being forced, check if STIBP always-on
859 if (mode != SPECTRE_V2_USER_STRICT &&
860 boot_cpu_has(X86_FEATURE_AMD_STIBP_ALWAYS_ON))
861 mode = SPECTRE_V2_USER_STRICT_PREFERRED;
863 spectre_v2_user_stibp = mode;
866 pr_info("%s\n", spectre_v2_user_strings[mode]);
869 static const char * const spectre_v2_strings[] = {
870 [SPECTRE_V2_NONE] = "Vulnerable",
871 [SPECTRE_V2_RETPOLINE] = "Mitigation: Retpolines",
872 [SPECTRE_V2_LFENCE] = "Mitigation: LFENCE",
873 [SPECTRE_V2_EIBRS] = "Mitigation: Enhanced IBRS",
874 [SPECTRE_V2_EIBRS_LFENCE] = "Mitigation: Enhanced IBRS + LFENCE",
875 [SPECTRE_V2_EIBRS_RETPOLINE] = "Mitigation: Enhanced IBRS + Retpolines",
878 static const struct {
880 enum spectre_v2_mitigation_cmd cmd;
882 } mitigation_options[] __initconst = {
883 { "off", SPECTRE_V2_CMD_NONE, false },
884 { "on", SPECTRE_V2_CMD_FORCE, true },
885 { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false },
886 { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_LFENCE, false },
887 { "retpoline,lfence", SPECTRE_V2_CMD_RETPOLINE_LFENCE, false },
888 { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
889 { "eibrs", SPECTRE_V2_CMD_EIBRS, false },
890 { "eibrs,lfence", SPECTRE_V2_CMD_EIBRS_LFENCE, false },
891 { "eibrs,retpoline", SPECTRE_V2_CMD_EIBRS_RETPOLINE, false },
892 { "auto", SPECTRE_V2_CMD_AUTO, false },
895 static void __init spec_v2_print_cond(const char *reason, bool secure)
897 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
898 pr_info("%s selected on command line.\n", reason);
901 static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
903 enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
907 if (cmdline_find_option_bool(boot_command_line, "nospectre_v2") ||
908 cpu_mitigations_off())
909 return SPECTRE_V2_CMD_NONE;
911 ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
913 return SPECTRE_V2_CMD_AUTO;
915 for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
916 if (!match_option(arg, ret, mitigation_options[i].option))
918 cmd = mitigation_options[i].cmd;
922 if (i >= ARRAY_SIZE(mitigation_options)) {
923 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
924 return SPECTRE_V2_CMD_AUTO;
927 if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
928 cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE ||
929 cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC ||
930 cmd == SPECTRE_V2_CMD_EIBRS_LFENCE ||
931 cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) &&
932 !IS_ENABLED(CONFIG_RETPOLINE)) {
933 pr_err("%s selected but not compiled in. Switching to AUTO select\n",
934 mitigation_options[i].option);
935 return SPECTRE_V2_CMD_AUTO;
938 if ((cmd == SPECTRE_V2_CMD_EIBRS ||
939 cmd == SPECTRE_V2_CMD_EIBRS_LFENCE ||
940 cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) &&
941 !boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
942 pr_err("%s selected but CPU doesn't have eIBRS. Switching to AUTO select\n",
943 mitigation_options[i].option);
944 return SPECTRE_V2_CMD_AUTO;
947 if ((cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE ||
948 cmd == SPECTRE_V2_CMD_EIBRS_LFENCE) &&
949 !boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
950 pr_err("%s selected, but CPU doesn't have a serializing LFENCE. Switching to AUTO select\n",
951 mitigation_options[i].option);
952 return SPECTRE_V2_CMD_AUTO;
955 spec_v2_print_cond(mitigation_options[i].option,
956 mitigation_options[i].secure);
960 static enum spectre_v2_mitigation __init spectre_v2_select_retpoline(void)
962 if (!IS_ENABLED(CONFIG_RETPOLINE)) {
963 pr_err("Kernel not compiled with retpoline; no mitigation available!");
964 return SPECTRE_V2_NONE;
967 return SPECTRE_V2_RETPOLINE;
970 static void __init spectre_v2_select_mitigation(void)
972 enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
973 enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
976 * If the CPU is not affected and the command line mode is NONE or AUTO
977 * then nothing to do.
979 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
980 (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
984 case SPECTRE_V2_CMD_NONE:
987 case SPECTRE_V2_CMD_FORCE:
988 case SPECTRE_V2_CMD_AUTO:
989 if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
990 mode = SPECTRE_V2_EIBRS;
994 mode = spectre_v2_select_retpoline();
997 case SPECTRE_V2_CMD_RETPOLINE_LFENCE:
998 pr_err(SPECTRE_V2_LFENCE_MSG);
999 mode = SPECTRE_V2_LFENCE;
1002 case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
1003 mode = SPECTRE_V2_RETPOLINE;
1006 case SPECTRE_V2_CMD_RETPOLINE:
1007 mode = spectre_v2_select_retpoline();
1010 case SPECTRE_V2_CMD_EIBRS:
1011 mode = SPECTRE_V2_EIBRS;
1014 case SPECTRE_V2_CMD_EIBRS_LFENCE:
1015 mode = SPECTRE_V2_EIBRS_LFENCE;
1018 case SPECTRE_V2_CMD_EIBRS_RETPOLINE:
1019 mode = SPECTRE_V2_EIBRS_RETPOLINE;
1023 if (mode == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
1024 pr_err(SPECTRE_V2_EIBRS_EBPF_MSG);
1026 if (spectre_v2_in_eibrs_mode(mode)) {
1027 /* Force it so VMEXIT will restore correctly */
1028 x86_spec_ctrl_base |= SPEC_CTRL_IBRS;
1029 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
1033 case SPECTRE_V2_NONE:
1034 case SPECTRE_V2_EIBRS:
1037 case SPECTRE_V2_LFENCE:
1038 case SPECTRE_V2_EIBRS_LFENCE:
1039 setup_force_cpu_cap(X86_FEATURE_RETPOLINE_LFENCE);
1042 case SPECTRE_V2_RETPOLINE:
1043 case SPECTRE_V2_EIBRS_RETPOLINE:
1044 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
1048 spectre_v2_enabled = mode;
1049 pr_info("%s\n", spectre_v2_strings[mode]);
1052 * If spectre v2 protection has been enabled, unconditionally fill
1053 * RSB during a context switch; this protects against two independent
1056 * - RSB underflow (and switch to BTB) on Skylake+
1057 * - SpectreRSB variant of spectre v2 on X86_BUG_SPECTRE_V2 CPUs
1059 setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
1060 pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n");
1063 * Retpoline means the kernel is safe because it has no indirect
1064 * branches. Enhanced IBRS protects firmware too, so, enable restricted
1065 * speculation around firmware calls only when Enhanced IBRS isn't
1068 * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because
1069 * the user might select retpoline on the kernel command line and if
1070 * the CPU supports Enhanced IBRS, kernel might un-intentionally not
1071 * enable IBRS around firmware calls.
1073 if (boot_cpu_has(X86_FEATURE_IBRS) && !spectre_v2_in_eibrs_mode(mode)) {
1074 setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
1075 pr_info("Enabling Restricted Speculation for firmware calls\n");
1078 /* Set up IBPB and STIBP depending on the general spectre V2 command */
1079 spectre_v2_user_select_mitigation(cmd);
1082 static void update_stibp_msr(void * __unused)
1084 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
1087 /* Update x86_spec_ctrl_base in case SMT state changed. */
1088 static void update_stibp_strict(void)
1090 u64 mask = x86_spec_ctrl_base & ~SPEC_CTRL_STIBP;
1092 if (sched_smt_active())
1093 mask |= SPEC_CTRL_STIBP;
1095 if (mask == x86_spec_ctrl_base)
1098 pr_info("Update user space SMT mitigation: STIBP %s\n",
1099 mask & SPEC_CTRL_STIBP ? "always-on" : "off");
1100 x86_spec_ctrl_base = mask;
1101 on_each_cpu(update_stibp_msr, NULL, 1);
1104 /* Update the static key controlling the evaluation of TIF_SPEC_IB */
1105 static void update_indir_branch_cond(void)
1107 if (sched_smt_active())
1108 static_branch_enable(&switch_to_cond_stibp);
1110 static_branch_disable(&switch_to_cond_stibp);
1114 #define pr_fmt(fmt) fmt
1116 /* Update the static key controlling the MDS CPU buffer clear in idle */
1117 static void update_mds_branch_idle(void)
1120 * Enable the idle clearing if SMT is active on CPUs which are
1121 * affected only by MSBDS and not any other MDS variant.
1123 * The other variants cannot be mitigated when SMT is enabled, so
1124 * clearing the buffers on idle just to prevent the Store Buffer
1125 * repartitioning leak would be a window dressing exercise.
1127 if (!boot_cpu_has_bug(X86_BUG_MSBDS_ONLY))
1130 if (sched_smt_active())
1131 static_branch_enable(&mds_idle_clear);
1133 static_branch_disable(&mds_idle_clear);
1136 #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"
1137 #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"
1139 void cpu_bugs_smt_update(void)
1141 mutex_lock(&spec_ctrl_mutex);
1143 if (sched_smt_active() && unprivileged_ebpf_enabled() &&
1144 spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
1145 pr_warn_once(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG);
1147 switch (spectre_v2_user_stibp) {
1148 case SPECTRE_V2_USER_NONE:
1150 case SPECTRE_V2_USER_STRICT:
1151 case SPECTRE_V2_USER_STRICT_PREFERRED:
1152 update_stibp_strict();
1154 case SPECTRE_V2_USER_PRCTL:
1155 case SPECTRE_V2_USER_SECCOMP:
1156 update_indir_branch_cond();
1160 switch (mds_mitigation) {
1161 case MDS_MITIGATION_FULL:
1162 case MDS_MITIGATION_VMWERV:
1163 if (sched_smt_active() && !boot_cpu_has(X86_BUG_MSBDS_ONLY))
1164 pr_warn_once(MDS_MSG_SMT);
1165 update_mds_branch_idle();
1167 case MDS_MITIGATION_OFF:
1171 switch (taa_mitigation) {
1172 case TAA_MITIGATION_VERW:
1173 case TAA_MITIGATION_UCODE_NEEDED:
1174 if (sched_smt_active())
1175 pr_warn_once(TAA_MSG_SMT);
1177 case TAA_MITIGATION_TSX_DISABLED:
1178 case TAA_MITIGATION_OFF:
1182 mutex_unlock(&spec_ctrl_mutex);
1186 #define pr_fmt(fmt) "Speculative Store Bypass: " fmt
1188 static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
1190 /* The kernel command line selection */
1191 enum ssb_mitigation_cmd {
1192 SPEC_STORE_BYPASS_CMD_NONE,
1193 SPEC_STORE_BYPASS_CMD_AUTO,
1194 SPEC_STORE_BYPASS_CMD_ON,
1195 SPEC_STORE_BYPASS_CMD_PRCTL,
1196 SPEC_STORE_BYPASS_CMD_SECCOMP,
1199 static const char * const ssb_strings[] = {
1200 [SPEC_STORE_BYPASS_NONE] = "Vulnerable",
1201 [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass disabled",
1202 [SPEC_STORE_BYPASS_PRCTL] = "Mitigation: Speculative Store Bypass disabled via prctl",
1203 [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
1206 static const struct {
1208 enum ssb_mitigation_cmd cmd;
1209 } ssb_mitigation_options[] __initconst = {
1210 { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */
1211 { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */
1212 { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */
1213 { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative Store Bypass via prctl */
1214 { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */
1217 static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
1219 enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO;
1223 if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable") ||
1224 cpu_mitigations_off()) {
1225 return SPEC_STORE_BYPASS_CMD_NONE;
1227 ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable",
1230 return SPEC_STORE_BYPASS_CMD_AUTO;
1232 for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) {
1233 if (!match_option(arg, ret, ssb_mitigation_options[i].option))
1236 cmd = ssb_mitigation_options[i].cmd;
1240 if (i >= ARRAY_SIZE(ssb_mitigation_options)) {
1241 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
1242 return SPEC_STORE_BYPASS_CMD_AUTO;
1249 static enum ssb_mitigation __init __ssb_select_mitigation(void)
1251 enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
1252 enum ssb_mitigation_cmd cmd;
1254 if (!boot_cpu_has(X86_FEATURE_SSBD))
1257 cmd = ssb_parse_cmdline();
1258 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) &&
1259 (cmd == SPEC_STORE_BYPASS_CMD_NONE ||
1260 cmd == SPEC_STORE_BYPASS_CMD_AUTO))
1264 case SPEC_STORE_BYPASS_CMD_SECCOMP:
1266 * Choose prctl+seccomp as the default mode if seccomp is
1269 if (IS_ENABLED(CONFIG_SECCOMP))
1270 mode = SPEC_STORE_BYPASS_SECCOMP;
1272 mode = SPEC_STORE_BYPASS_PRCTL;
1274 case SPEC_STORE_BYPASS_CMD_ON:
1275 mode = SPEC_STORE_BYPASS_DISABLE;
1277 case SPEC_STORE_BYPASS_CMD_AUTO:
1278 case SPEC_STORE_BYPASS_CMD_PRCTL:
1279 mode = SPEC_STORE_BYPASS_PRCTL;
1281 case SPEC_STORE_BYPASS_CMD_NONE:
1286 * If SSBD is controlled by the SPEC_CTRL MSR, then set the proper
1287 * bit in the mask to allow guests to use the mitigation even in the
1288 * case where the host does not enable it.
1290 if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
1291 static_cpu_has(X86_FEATURE_AMD_SSBD)) {
1292 x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
1296 * We have three CPU feature flags that are in play here:
1297 * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
1298 * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
1299 * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
1301 if (mode == SPEC_STORE_BYPASS_DISABLE) {
1302 setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
1304 * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
1305 * use a completely different MSR and bit dependent on family.
1307 if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
1308 !static_cpu_has(X86_FEATURE_AMD_SSBD)) {
1309 x86_amd_ssb_disable();
1311 x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
1312 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
1319 static void ssb_select_mitigation(void)
1321 ssb_mode = __ssb_select_mitigation();
1323 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1324 pr_info("%s\n", ssb_strings[ssb_mode]);
1328 #define pr_fmt(fmt) "Speculation prctl: " fmt
1330 static void task_update_spec_tif(struct task_struct *tsk)
1332 /* Force the update of the real TIF bits */
1333 set_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE);
1336 * Immediately update the speculation control MSRs for the current
1337 * task, but for a non-current task delay setting the CPU
1338 * mitigation until it is scheduled next.
1340 * This can only happen for SECCOMP mitigation. For PRCTL it's
1341 * always the current task.
1344 speculation_ctrl_update_current();
1347 static int l1d_flush_prctl_set(struct task_struct *task, unsigned long ctrl)
1350 if (!static_branch_unlikely(&switch_mm_cond_l1d_flush))
1354 case PR_SPEC_ENABLE:
1355 set_ti_thread_flag(&task->thread_info, TIF_SPEC_L1D_FLUSH);
1357 case PR_SPEC_DISABLE:
1358 clear_ti_thread_flag(&task->thread_info, TIF_SPEC_L1D_FLUSH);
1365 static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
1367 if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
1368 ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
1372 case PR_SPEC_ENABLE:
1373 /* If speculation is force disabled, enable is not allowed */
1374 if (task_spec_ssb_force_disable(task))
1376 task_clear_spec_ssb_disable(task);
1377 task_clear_spec_ssb_noexec(task);
1378 task_update_spec_tif(task);
1380 case PR_SPEC_DISABLE:
1381 task_set_spec_ssb_disable(task);
1382 task_clear_spec_ssb_noexec(task);
1383 task_update_spec_tif(task);
1385 case PR_SPEC_FORCE_DISABLE:
1386 task_set_spec_ssb_disable(task);
1387 task_set_spec_ssb_force_disable(task);
1388 task_clear_spec_ssb_noexec(task);
1389 task_update_spec_tif(task);
1391 case PR_SPEC_DISABLE_NOEXEC:
1392 if (task_spec_ssb_force_disable(task))
1394 task_set_spec_ssb_disable(task);
1395 task_set_spec_ssb_noexec(task);
1396 task_update_spec_tif(task);
1404 static bool is_spec_ib_user_controlled(void)
1406 return spectre_v2_user_ibpb == SPECTRE_V2_USER_PRCTL ||
1407 spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
1408 spectre_v2_user_stibp == SPECTRE_V2_USER_PRCTL ||
1409 spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP;
1412 static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)
1415 case PR_SPEC_ENABLE:
1416 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1417 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
1421 * With strict mode for both IBPB and STIBP, the instruction
1422 * code paths avoid checking this task flag and instead,
1423 * unconditionally run the instruction. However, STIBP and IBPB
1424 * are independent and either can be set to conditionally
1425 * enabled regardless of the mode of the other.
1427 * If either is set to conditional, allow the task flag to be
1428 * updated, unless it was force-disabled by a previous prctl
1429 * call. Currently, this is possible on an AMD CPU which has the
1430 * feature X86_FEATURE_AMD_STIBP_ALWAYS_ON. In this case, if the
1431 * kernel is booted with 'spectre_v2_user=seccomp', then
1432 * spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP and
1433 * spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED.
1435 if (!is_spec_ib_user_controlled() ||
1436 task_spec_ib_force_disable(task))
1439 task_clear_spec_ib_disable(task);
1440 task_update_spec_tif(task);
1442 case PR_SPEC_DISABLE:
1443 case PR_SPEC_FORCE_DISABLE:
1445 * Indirect branch speculation is always allowed when
1446 * mitigation is force disabled.
1448 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1449 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
1452 if (!is_spec_ib_user_controlled())
1455 task_set_spec_ib_disable(task);
1456 if (ctrl == PR_SPEC_FORCE_DISABLE)
1457 task_set_spec_ib_force_disable(task);
1458 task_update_spec_tif(task);
1466 int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
1470 case PR_SPEC_STORE_BYPASS:
1471 return ssb_prctl_set(task, ctrl);
1472 case PR_SPEC_INDIRECT_BRANCH:
1473 return ib_prctl_set(task, ctrl);
1474 case PR_SPEC_L1D_FLUSH:
1475 return l1d_flush_prctl_set(task, ctrl);
1481 #ifdef CONFIG_SECCOMP
1482 void arch_seccomp_spec_mitigate(struct task_struct *task)
1484 if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP)
1485 ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
1486 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
1487 spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP)
1488 ib_prctl_set(task, PR_SPEC_FORCE_DISABLE);
1492 static int l1d_flush_prctl_get(struct task_struct *task)
1494 if (!static_branch_unlikely(&switch_mm_cond_l1d_flush))
1495 return PR_SPEC_FORCE_DISABLE;
1497 if (test_ti_thread_flag(&task->thread_info, TIF_SPEC_L1D_FLUSH))
1498 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
1500 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
1503 static int ssb_prctl_get(struct task_struct *task)
1506 case SPEC_STORE_BYPASS_DISABLE:
1507 return PR_SPEC_DISABLE;
1508 case SPEC_STORE_BYPASS_SECCOMP:
1509 case SPEC_STORE_BYPASS_PRCTL:
1510 if (task_spec_ssb_force_disable(task))
1511 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
1512 if (task_spec_ssb_noexec(task))
1513 return PR_SPEC_PRCTL | PR_SPEC_DISABLE_NOEXEC;
1514 if (task_spec_ssb_disable(task))
1515 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
1516 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
1518 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1519 return PR_SPEC_ENABLE;
1520 return PR_SPEC_NOT_AFFECTED;
1524 static int ib_prctl_get(struct task_struct *task)
1526 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
1527 return PR_SPEC_NOT_AFFECTED;
1529 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1530 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
1531 return PR_SPEC_ENABLE;
1532 else if (is_spec_ib_user_controlled()) {
1533 if (task_spec_ib_force_disable(task))
1534 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
1535 if (task_spec_ib_disable(task))
1536 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
1537 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
1538 } else if (spectre_v2_user_ibpb == SPECTRE_V2_USER_STRICT ||
1539 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
1540 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED)
1541 return PR_SPEC_DISABLE;
1543 return PR_SPEC_NOT_AFFECTED;
1546 int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
1549 case PR_SPEC_STORE_BYPASS:
1550 return ssb_prctl_get(task);
1551 case PR_SPEC_INDIRECT_BRANCH:
1552 return ib_prctl_get(task);
1553 case PR_SPEC_L1D_FLUSH:
1554 return l1d_flush_prctl_get(task);
1560 void x86_spec_ctrl_setup_ap(void)
1562 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
1563 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
1565 if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
1566 x86_amd_ssb_disable();
1569 bool itlb_multihit_kvm_mitigation;
1570 EXPORT_SYMBOL_GPL(itlb_multihit_kvm_mitigation);
1573 #define pr_fmt(fmt) "L1TF: " fmt
1575 /* Default mitigation for L1TF-affected CPUs */
1576 enum l1tf_mitigations l1tf_mitigation __ro_after_init = L1TF_MITIGATION_FLUSH;
1577 #if IS_ENABLED(CONFIG_KVM_INTEL)
1578 EXPORT_SYMBOL_GPL(l1tf_mitigation);
1580 enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
1581 EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation);
1584 * These CPUs all support 44bits physical address space internally in the
1585 * cache but CPUID can report a smaller number of physical address bits.
1587 * The L1TF mitigation uses the top most address bit for the inversion of
1588 * non present PTEs. When the installed memory reaches into the top most
1589 * address bit due to memory holes, which has been observed on machines
1590 * which report 36bits physical address bits and have 32G RAM installed,
1591 * then the mitigation range check in l1tf_select_mitigation() triggers.
1592 * This is a false positive because the mitigation is still possible due to
1593 * the fact that the cache uses 44bit internally. Use the cache bits
1594 * instead of the reported physical bits and adjust them on the affected
1595 * machines to 44bit if the reported bits are less than 44.
1597 static void override_cache_bits(struct cpuinfo_x86 *c)
1602 switch (c->x86_model) {
1603 case INTEL_FAM6_NEHALEM:
1604 case INTEL_FAM6_WESTMERE:
1605 case INTEL_FAM6_SANDYBRIDGE:
1606 case INTEL_FAM6_IVYBRIDGE:
1607 case INTEL_FAM6_HASWELL:
1608 case INTEL_FAM6_HASWELL_L:
1609 case INTEL_FAM6_HASWELL_G:
1610 case INTEL_FAM6_BROADWELL:
1611 case INTEL_FAM6_BROADWELL_G:
1612 case INTEL_FAM6_SKYLAKE_L:
1613 case INTEL_FAM6_SKYLAKE:
1614 case INTEL_FAM6_KABYLAKE_L:
1615 case INTEL_FAM6_KABYLAKE:
1616 if (c->x86_cache_bits < 44)
1617 c->x86_cache_bits = 44;
1622 static void __init l1tf_select_mitigation(void)
1626 if (!boot_cpu_has_bug(X86_BUG_L1TF))
1629 if (cpu_mitigations_off())
1630 l1tf_mitigation = L1TF_MITIGATION_OFF;
1631 else if (cpu_mitigations_auto_nosmt())
1632 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
1634 override_cache_bits(&boot_cpu_data);
1636 switch (l1tf_mitigation) {
1637 case L1TF_MITIGATION_OFF:
1638 case L1TF_MITIGATION_FLUSH_NOWARN:
1639 case L1TF_MITIGATION_FLUSH:
1641 case L1TF_MITIGATION_FLUSH_NOSMT:
1642 case L1TF_MITIGATION_FULL:
1643 cpu_smt_disable(false);
1645 case L1TF_MITIGATION_FULL_FORCE:
1646 cpu_smt_disable(true);
1650 #if CONFIG_PGTABLE_LEVELS == 2
1651 pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
1655 half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT;
1656 if (l1tf_mitigation != L1TF_MITIGATION_OFF &&
1657 e820__mapped_any(half_pa, ULLONG_MAX - half_pa, E820_TYPE_RAM)) {
1658 pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
1659 pr_info("You may make it effective by booting the kernel with mem=%llu parameter.\n",
1661 pr_info("However, doing so will make a part of your RAM unusable.\n");
1662 pr_info("Reading https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html might help you decide.\n");
1666 setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV);
1669 static int __init l1tf_cmdline(char *str)
1671 if (!boot_cpu_has_bug(X86_BUG_L1TF))
1677 if (!strcmp(str, "off"))
1678 l1tf_mitigation = L1TF_MITIGATION_OFF;
1679 else if (!strcmp(str, "flush,nowarn"))
1680 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOWARN;
1681 else if (!strcmp(str, "flush"))
1682 l1tf_mitigation = L1TF_MITIGATION_FLUSH;
1683 else if (!strcmp(str, "flush,nosmt"))
1684 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
1685 else if (!strcmp(str, "full"))
1686 l1tf_mitigation = L1TF_MITIGATION_FULL;
1687 else if (!strcmp(str, "full,force"))
1688 l1tf_mitigation = L1TF_MITIGATION_FULL_FORCE;
1692 early_param("l1tf", l1tf_cmdline);
1695 #define pr_fmt(fmt) fmt
1699 #define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
1701 #if IS_ENABLED(CONFIG_KVM_INTEL)
1702 static const char * const l1tf_vmx_states[] = {
1703 [VMENTER_L1D_FLUSH_AUTO] = "auto",
1704 [VMENTER_L1D_FLUSH_NEVER] = "vulnerable",
1705 [VMENTER_L1D_FLUSH_COND] = "conditional cache flushes",
1706 [VMENTER_L1D_FLUSH_ALWAYS] = "cache flushes",
1707 [VMENTER_L1D_FLUSH_EPT_DISABLED] = "EPT disabled",
1708 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = "flush not necessary"
1711 static ssize_t l1tf_show_state(char *buf)
1713 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO)
1714 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
1716 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_EPT_DISABLED ||
1717 (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER &&
1718 sched_smt_active())) {
1719 return sprintf(buf, "%s; VMX: %s\n", L1TF_DEFAULT_MSG,
1720 l1tf_vmx_states[l1tf_vmx_mitigation]);
1723 return sprintf(buf, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG,
1724 l1tf_vmx_states[l1tf_vmx_mitigation],
1725 sched_smt_active() ? "vulnerable" : "disabled");
1728 static ssize_t itlb_multihit_show_state(char *buf)
1730 if (!boot_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
1731 !boot_cpu_has(X86_FEATURE_VMX))
1732 return sprintf(buf, "KVM: Mitigation: VMX unsupported\n");
1733 else if (!(cr4_read_shadow() & X86_CR4_VMXE))
1734 return sprintf(buf, "KVM: Mitigation: VMX disabled\n");
1735 else if (itlb_multihit_kvm_mitigation)
1736 return sprintf(buf, "KVM: Mitigation: Split huge pages\n");
1738 return sprintf(buf, "KVM: Vulnerable\n");
1741 static ssize_t l1tf_show_state(char *buf)
1743 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
1746 static ssize_t itlb_multihit_show_state(char *buf)
1748 return sprintf(buf, "Processor vulnerable\n");
1752 static ssize_t mds_show_state(char *buf)
1754 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
1755 return sprintf(buf, "%s; SMT Host state unknown\n",
1756 mds_strings[mds_mitigation]);
1759 if (boot_cpu_has(X86_BUG_MSBDS_ONLY)) {
1760 return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
1761 (mds_mitigation == MDS_MITIGATION_OFF ? "vulnerable" :
1762 sched_smt_active() ? "mitigated" : "disabled"));
1765 return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
1766 sched_smt_active() ? "vulnerable" : "disabled");
1769 static ssize_t tsx_async_abort_show_state(char *buf)
1771 if ((taa_mitigation == TAA_MITIGATION_TSX_DISABLED) ||
1772 (taa_mitigation == TAA_MITIGATION_OFF))
1773 return sprintf(buf, "%s\n", taa_strings[taa_mitigation]);
1775 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
1776 return sprintf(buf, "%s; SMT Host state unknown\n",
1777 taa_strings[taa_mitigation]);
1780 return sprintf(buf, "%s; SMT %s\n", taa_strings[taa_mitigation],
1781 sched_smt_active() ? "vulnerable" : "disabled");
1784 static char *stibp_state(void)
1786 if (spectre_v2_in_eibrs_mode(spectre_v2_enabled))
1789 switch (spectre_v2_user_stibp) {
1790 case SPECTRE_V2_USER_NONE:
1791 return ", STIBP: disabled";
1792 case SPECTRE_V2_USER_STRICT:
1793 return ", STIBP: forced";
1794 case SPECTRE_V2_USER_STRICT_PREFERRED:
1795 return ", STIBP: always-on";
1796 case SPECTRE_V2_USER_PRCTL:
1797 case SPECTRE_V2_USER_SECCOMP:
1798 if (static_key_enabled(&switch_to_cond_stibp))
1799 return ", STIBP: conditional";
1804 static char *ibpb_state(void)
1806 if (boot_cpu_has(X86_FEATURE_IBPB)) {
1807 if (static_key_enabled(&switch_mm_always_ibpb))
1808 return ", IBPB: always-on";
1809 if (static_key_enabled(&switch_mm_cond_ibpb))
1810 return ", IBPB: conditional";
1811 return ", IBPB: disabled";
1816 static ssize_t spectre_v2_show_state(char *buf)
1818 if (spectre_v2_enabled == SPECTRE_V2_LFENCE)
1819 return sprintf(buf, "Vulnerable: LFENCE\n");
1821 if (spectre_v2_enabled == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
1822 return sprintf(buf, "Vulnerable: eIBRS with unprivileged eBPF\n");
1824 if (sched_smt_active() && unprivileged_ebpf_enabled() &&
1825 spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
1826 return sprintf(buf, "Vulnerable: eIBRS+LFENCE with unprivileged eBPF and SMT\n");
1828 return sprintf(buf, "%s%s%s%s%s%s\n",
1829 spectre_v2_strings[spectre_v2_enabled],
1831 boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
1833 boot_cpu_has(X86_FEATURE_RSB_CTXSW) ? ", RSB filling" : "",
1834 spectre_v2_module_string());
1837 static ssize_t srbds_show_state(char *buf)
1839 return sprintf(buf, "%s\n", srbds_strings[srbds_mitigation]);
1842 static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
1843 char *buf, unsigned int bug)
1845 if (!boot_cpu_has_bug(bug))
1846 return sprintf(buf, "Not affected\n");
1849 case X86_BUG_CPU_MELTDOWN:
1850 if (boot_cpu_has(X86_FEATURE_PTI))
1851 return sprintf(buf, "Mitigation: PTI\n");
1853 if (hypervisor_is_type(X86_HYPER_XEN_PV))
1854 return sprintf(buf, "Unknown (XEN PV detected, hypervisor mitigation required)\n");
1858 case X86_BUG_SPECTRE_V1:
1859 return sprintf(buf, "%s\n", spectre_v1_strings[spectre_v1_mitigation]);
1861 case X86_BUG_SPECTRE_V2:
1862 return spectre_v2_show_state(buf);
1864 case X86_BUG_SPEC_STORE_BYPASS:
1865 return sprintf(buf, "%s\n", ssb_strings[ssb_mode]);
1868 if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV))
1869 return l1tf_show_state(buf);
1873 return mds_show_state(buf);
1876 return tsx_async_abort_show_state(buf);
1878 case X86_BUG_ITLB_MULTIHIT:
1879 return itlb_multihit_show_state(buf);
1882 return srbds_show_state(buf);
1888 return sprintf(buf, "Vulnerable\n");
1891 ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
1893 return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
1896 ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
1898 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
1901 ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
1903 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
1906 ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
1908 return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS);
1911 ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
1913 return cpu_show_common(dev, attr, buf, X86_BUG_L1TF);
1916 ssize_t cpu_show_mds(struct device *dev, struct device_attribute *attr, char *buf)
1918 return cpu_show_common(dev, attr, buf, X86_BUG_MDS);
1921 ssize_t cpu_show_tsx_async_abort(struct device *dev, struct device_attribute *attr, char *buf)
1923 return cpu_show_common(dev, attr, buf, X86_BUG_TAA);
1926 ssize_t cpu_show_itlb_multihit(struct device *dev, struct device_attribute *attr, char *buf)
1928 return cpu_show_common(dev, attr, buf, X86_BUG_ITLB_MULTIHIT);
1931 ssize_t cpu_show_srbds(struct device *dev, struct device_attribute *attr, char *buf)
1933 return cpu_show_common(dev, attr, buf, X86_BUG_SRBDS);