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>
34 #include <linux/bpf.h>
38 static void __init spectre_v1_select_mitigation(void);
39 static void __init spectre_v2_select_mitigation(void);
40 static void __init retbleed_select_mitigation(void);
41 static void __init spectre_v2_user_select_mitigation(void);
42 static void __init ssb_select_mitigation(void);
43 static void __init l1tf_select_mitigation(void);
44 static void __init mds_select_mitigation(void);
45 static void __init md_clear_update_mitigation(void);
46 static void __init md_clear_select_mitigation(void);
47 static void __init taa_select_mitigation(void);
48 static void __init mmio_select_mitigation(void);
49 static void __init srbds_select_mitigation(void);
51 /* The base value of the SPEC_CTRL MSR without task-specific bits set */
52 u64 x86_spec_ctrl_base;
53 EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
55 /* The current value of the SPEC_CTRL MSR with task-specific bits set */
56 DEFINE_PER_CPU(u64, x86_spec_ctrl_current);
57 EXPORT_SYMBOL_GPL(x86_spec_ctrl_current);
59 static DEFINE_MUTEX(spec_ctrl_mutex);
61 /* Update SPEC_CTRL MSR and its cached copy unconditionally */
62 static void update_spec_ctrl(u64 val)
64 this_cpu_write(x86_spec_ctrl_current, val);
65 wrmsrl(MSR_IA32_SPEC_CTRL, val);
69 * Keep track of the SPEC_CTRL MSR value for the current task, which may differ
70 * from x86_spec_ctrl_base due to STIBP/SSB in __speculation_ctrl_update().
72 void update_spec_ctrl_cond(u64 val)
74 if (this_cpu_read(x86_spec_ctrl_current) == val)
77 this_cpu_write(x86_spec_ctrl_current, val);
80 * When KERNEL_IBRS this MSR is written on return-to-user, unless
81 * forced the update can be delayed until that time.
83 if (!cpu_feature_enabled(X86_FEATURE_KERNEL_IBRS))
84 wrmsrl(MSR_IA32_SPEC_CTRL, val);
87 u64 spec_ctrl_current(void)
89 return this_cpu_read(x86_spec_ctrl_current);
91 EXPORT_SYMBOL_GPL(spec_ctrl_current);
94 * AMD specific MSR info for Speculative Store Bypass control.
95 * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu().
97 u64 __ro_after_init x86_amd_ls_cfg_base;
98 u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask;
100 /* Control conditional STIBP in switch_to() */
101 DEFINE_STATIC_KEY_FALSE(switch_to_cond_stibp);
102 /* Control conditional IBPB in switch_mm() */
103 DEFINE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
104 /* Control unconditional IBPB in switch_mm() */
105 DEFINE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
107 /* Control MDS CPU buffer clear before returning to user space */
108 DEFINE_STATIC_KEY_FALSE(mds_user_clear);
109 EXPORT_SYMBOL_GPL(mds_user_clear);
110 /* Control MDS CPU buffer clear before idling (halt, mwait) */
111 DEFINE_STATIC_KEY_FALSE(mds_idle_clear);
112 EXPORT_SYMBOL_GPL(mds_idle_clear);
114 /* Controls CPU Fill buffer clear before KVM guest MMIO accesses */
115 DEFINE_STATIC_KEY_FALSE(mmio_stale_data_clear);
116 EXPORT_SYMBOL_GPL(mmio_stale_data_clear);
118 void __init check_bugs(void)
123 * identify_boot_cpu() initialized SMT support information, let the
126 cpu_smt_check_topology();
128 if (!IS_ENABLED(CONFIG_SMP)) {
130 print_cpu_info(&boot_cpu_data);
134 * Read the SPEC_CTRL MSR to account for reserved bits which may
135 * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
136 * init code as it is not enumerated and depends on the family.
138 if (cpu_feature_enabled(X86_FEATURE_MSR_SPEC_CTRL)) {
139 rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
142 * Previously running kernel (kexec), may have some controls
143 * turned ON. Clear them and let the mitigations setup below
144 * rediscover them based on configuration.
146 x86_spec_ctrl_base &= ~SPEC_CTRL_MITIGATIONS_MASK;
149 /* Select the proper CPU mitigations before patching alternatives: */
150 spectre_v1_select_mitigation();
151 spectre_v2_select_mitigation();
153 * retbleed_select_mitigation() relies on the state set by
154 * spectre_v2_select_mitigation(); specifically it wants to know about
157 retbleed_select_mitigation();
159 * spectre_v2_user_select_mitigation() relies on the state set by
160 * retbleed_select_mitigation(); specifically the STIBP selection is
163 spectre_v2_user_select_mitigation();
164 ssb_select_mitigation();
165 l1tf_select_mitigation();
166 md_clear_select_mitigation();
167 srbds_select_mitigation();
173 * Check whether we are able to run this kernel safely on SMP.
175 * - i386 is no longer supported.
176 * - In order to run on anything without a TSC, we need to be
177 * compiled for a i486.
179 if (boot_cpu_data.x86 < 4)
180 panic("Kernel requires i486+ for 'invlpg' and other features");
182 init_utsname()->machine[1] =
183 '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
184 alternative_instructions();
186 fpu__init_check_bugs();
187 #else /* CONFIG_X86_64 */
188 alternative_instructions();
191 * Make sure the first 2MB area is not mapped by huge pages
192 * There are typically fixed size MTRRs in there and overlapping
193 * MTRRs into large pages causes slow downs.
195 * Right now we don't do that with gbpages because there seems
196 * very little benefit for that case.
199 set_memory_4k((unsigned long)__va(0), 1);
204 * NOTE: For VMX, this function is not called in the vmexit path.
205 * It uses vmx_spec_ctrl_restore_host() instead.
208 x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest)
210 u64 msrval, guestval = guest_spec_ctrl, hostval = spec_ctrl_current();
211 struct thread_info *ti = current_thread_info();
213 if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) {
214 if (hostval != guestval) {
215 msrval = setguest ? guestval : hostval;
216 wrmsrl(MSR_IA32_SPEC_CTRL, msrval);
221 * If SSBD is not handled in MSR_SPEC_CTRL on AMD, update
222 * MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported.
224 if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
225 !static_cpu_has(X86_FEATURE_VIRT_SSBD))
229 * If the host has SSBD mitigation enabled, force it in the host's
230 * virtual MSR value. If its not permanently enabled, evaluate
231 * current's TIF_SSBD thread flag.
233 if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE))
234 hostval = SPEC_CTRL_SSBD;
236 hostval = ssbd_tif_to_spec_ctrl(ti->flags);
238 /* Sanitize the guest value */
239 guestval = guest_virt_spec_ctrl & SPEC_CTRL_SSBD;
241 if (hostval != guestval) {
244 tif = setguest ? ssbd_spec_ctrl_to_tif(guestval) :
245 ssbd_spec_ctrl_to_tif(hostval);
247 speculation_ctrl_update(tif);
250 EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl);
252 static void x86_amd_ssb_disable(void)
254 u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_ssbd_mask;
256 if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
257 wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, SPEC_CTRL_SSBD);
258 else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
259 wrmsrl(MSR_AMD64_LS_CFG, msrval);
263 #define pr_fmt(fmt) "MDS: " fmt
265 /* Default mitigation for MDS-affected CPUs */
266 static enum mds_mitigations mds_mitigation __ro_after_init = MDS_MITIGATION_FULL;
267 static bool mds_nosmt __ro_after_init = false;
269 static const char * const mds_strings[] = {
270 [MDS_MITIGATION_OFF] = "Vulnerable",
271 [MDS_MITIGATION_FULL] = "Mitigation: Clear CPU buffers",
272 [MDS_MITIGATION_VMWERV] = "Vulnerable: Clear CPU buffers attempted, no microcode",
275 static void __init mds_select_mitigation(void)
277 if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off()) {
278 mds_mitigation = MDS_MITIGATION_OFF;
282 if (mds_mitigation == MDS_MITIGATION_FULL) {
283 if (!boot_cpu_has(X86_FEATURE_MD_CLEAR))
284 mds_mitigation = MDS_MITIGATION_VMWERV;
286 static_branch_enable(&mds_user_clear);
288 if (!boot_cpu_has(X86_BUG_MSBDS_ONLY) &&
289 (mds_nosmt || cpu_mitigations_auto_nosmt()))
290 cpu_smt_disable(false);
294 static int __init mds_cmdline(char *str)
296 if (!boot_cpu_has_bug(X86_BUG_MDS))
302 if (!strcmp(str, "off"))
303 mds_mitigation = MDS_MITIGATION_OFF;
304 else if (!strcmp(str, "full"))
305 mds_mitigation = MDS_MITIGATION_FULL;
306 else if (!strcmp(str, "full,nosmt")) {
307 mds_mitigation = MDS_MITIGATION_FULL;
313 early_param("mds", mds_cmdline);
316 #define pr_fmt(fmt) "TAA: " fmt
318 /* Default mitigation for TAA-affected CPUs */
319 static enum taa_mitigations taa_mitigation __ro_after_init = TAA_MITIGATION_VERW;
320 static bool taa_nosmt __ro_after_init;
322 static const char * const taa_strings[] = {
323 [TAA_MITIGATION_OFF] = "Vulnerable",
324 [TAA_MITIGATION_UCODE_NEEDED] = "Vulnerable: Clear CPU buffers attempted, no microcode",
325 [TAA_MITIGATION_VERW] = "Mitigation: Clear CPU buffers",
326 [TAA_MITIGATION_TSX_DISABLED] = "Mitigation: TSX disabled",
329 static void __init taa_select_mitigation(void)
333 if (!boot_cpu_has_bug(X86_BUG_TAA)) {
334 taa_mitigation = TAA_MITIGATION_OFF;
338 /* TSX previously disabled by tsx=off */
339 if (!boot_cpu_has(X86_FEATURE_RTM)) {
340 taa_mitigation = TAA_MITIGATION_TSX_DISABLED;
344 if (cpu_mitigations_off()) {
345 taa_mitigation = TAA_MITIGATION_OFF;
350 * TAA mitigation via VERW is turned off if both
351 * tsx_async_abort=off and mds=off are specified.
353 if (taa_mitigation == TAA_MITIGATION_OFF &&
354 mds_mitigation == MDS_MITIGATION_OFF)
357 if (boot_cpu_has(X86_FEATURE_MD_CLEAR))
358 taa_mitigation = TAA_MITIGATION_VERW;
360 taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
363 * VERW doesn't clear the CPU buffers when MD_CLEAR=1 and MDS_NO=1.
364 * A microcode update fixes this behavior to clear CPU buffers. It also
365 * adds support for MSR_IA32_TSX_CTRL which is enumerated by the
366 * ARCH_CAP_TSX_CTRL_MSR bit.
368 * On MDS_NO=1 CPUs if ARCH_CAP_TSX_CTRL_MSR is not set, microcode
369 * update is required.
371 ia32_cap = x86_read_arch_cap_msr();
372 if ( (ia32_cap & ARCH_CAP_MDS_NO) &&
373 !(ia32_cap & ARCH_CAP_TSX_CTRL_MSR))
374 taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
377 * TSX is enabled, select alternate mitigation for TAA which is
378 * the same as MDS. Enable MDS static branch to clear CPU buffers.
380 * For guests that can't determine whether the correct microcode is
381 * present on host, enable the mitigation for UCODE_NEEDED as well.
383 static_branch_enable(&mds_user_clear);
385 if (taa_nosmt || cpu_mitigations_auto_nosmt())
386 cpu_smt_disable(false);
389 static int __init tsx_async_abort_parse_cmdline(char *str)
391 if (!boot_cpu_has_bug(X86_BUG_TAA))
397 if (!strcmp(str, "off")) {
398 taa_mitigation = TAA_MITIGATION_OFF;
399 } else if (!strcmp(str, "full")) {
400 taa_mitigation = TAA_MITIGATION_VERW;
401 } else if (!strcmp(str, "full,nosmt")) {
402 taa_mitigation = TAA_MITIGATION_VERW;
408 early_param("tsx_async_abort", tsx_async_abort_parse_cmdline);
411 #define pr_fmt(fmt) "MMIO Stale Data: " fmt
413 enum mmio_mitigations {
415 MMIO_MITIGATION_UCODE_NEEDED,
416 MMIO_MITIGATION_VERW,
419 /* Default mitigation for Processor MMIO Stale Data vulnerabilities */
420 static enum mmio_mitigations mmio_mitigation __ro_after_init = MMIO_MITIGATION_VERW;
421 static bool mmio_nosmt __ro_after_init = false;
423 static const char * const mmio_strings[] = {
424 [MMIO_MITIGATION_OFF] = "Vulnerable",
425 [MMIO_MITIGATION_UCODE_NEEDED] = "Vulnerable: Clear CPU buffers attempted, no microcode",
426 [MMIO_MITIGATION_VERW] = "Mitigation: Clear CPU buffers",
429 static void __init mmio_select_mitigation(void)
433 if (!boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA) ||
434 boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN) ||
435 cpu_mitigations_off()) {
436 mmio_mitigation = MMIO_MITIGATION_OFF;
440 if (mmio_mitigation == MMIO_MITIGATION_OFF)
443 ia32_cap = x86_read_arch_cap_msr();
446 * Enable CPU buffer clear mitigation for host and VMM, if also affected
447 * by MDS or TAA. Otherwise, enable mitigation for VMM only.
449 if (boot_cpu_has_bug(X86_BUG_MDS) || (boot_cpu_has_bug(X86_BUG_TAA) &&
450 boot_cpu_has(X86_FEATURE_RTM)))
451 static_branch_enable(&mds_user_clear);
453 static_branch_enable(&mmio_stale_data_clear);
456 * If Processor-MMIO-Stale-Data bug is present and Fill Buffer data can
457 * be propagated to uncore buffers, clearing the Fill buffers on idle
458 * is required irrespective of SMT state.
460 if (!(ia32_cap & ARCH_CAP_FBSDP_NO))
461 static_branch_enable(&mds_idle_clear);
464 * Check if the system has the right microcode.
466 * CPU Fill buffer clear mitigation is enumerated by either an explicit
467 * FB_CLEAR or by the presence of both MD_CLEAR and L1D_FLUSH on MDS
470 if ((ia32_cap & ARCH_CAP_FB_CLEAR) ||
471 (boot_cpu_has(X86_FEATURE_MD_CLEAR) &&
472 boot_cpu_has(X86_FEATURE_FLUSH_L1D) &&
473 !(ia32_cap & ARCH_CAP_MDS_NO)))
474 mmio_mitigation = MMIO_MITIGATION_VERW;
476 mmio_mitigation = MMIO_MITIGATION_UCODE_NEEDED;
478 if (mmio_nosmt || cpu_mitigations_auto_nosmt())
479 cpu_smt_disable(false);
482 static int __init mmio_stale_data_parse_cmdline(char *str)
484 if (!boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA))
490 if (!strcmp(str, "off")) {
491 mmio_mitigation = MMIO_MITIGATION_OFF;
492 } else if (!strcmp(str, "full")) {
493 mmio_mitigation = MMIO_MITIGATION_VERW;
494 } else if (!strcmp(str, "full,nosmt")) {
495 mmio_mitigation = MMIO_MITIGATION_VERW;
501 early_param("mmio_stale_data", mmio_stale_data_parse_cmdline);
504 #define pr_fmt(fmt) "" fmt
506 static void __init md_clear_update_mitigation(void)
508 if (cpu_mitigations_off())
511 if (!static_key_enabled(&mds_user_clear))
515 * mds_user_clear is now enabled. Update MDS, TAA and MMIO Stale Data
516 * mitigation, if necessary.
518 if (mds_mitigation == MDS_MITIGATION_OFF &&
519 boot_cpu_has_bug(X86_BUG_MDS)) {
520 mds_mitigation = MDS_MITIGATION_FULL;
521 mds_select_mitigation();
523 if (taa_mitigation == TAA_MITIGATION_OFF &&
524 boot_cpu_has_bug(X86_BUG_TAA)) {
525 taa_mitigation = TAA_MITIGATION_VERW;
526 taa_select_mitigation();
528 if (mmio_mitigation == MMIO_MITIGATION_OFF &&
529 boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA)) {
530 mmio_mitigation = MMIO_MITIGATION_VERW;
531 mmio_select_mitigation();
534 if (boot_cpu_has_bug(X86_BUG_MDS))
535 pr_info("MDS: %s\n", mds_strings[mds_mitigation]);
536 if (boot_cpu_has_bug(X86_BUG_TAA))
537 pr_info("TAA: %s\n", taa_strings[taa_mitigation]);
538 if (boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA))
539 pr_info("MMIO Stale Data: %s\n", mmio_strings[mmio_mitigation]);
540 else if (boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN))
541 pr_info("MMIO Stale Data: Unknown: No mitigations\n");
544 static void __init md_clear_select_mitigation(void)
546 mds_select_mitigation();
547 taa_select_mitigation();
548 mmio_select_mitigation();
551 * As MDS, TAA and MMIO Stale Data mitigations are inter-related, update
552 * and print their mitigation after MDS, TAA and MMIO Stale Data
553 * mitigation selection is done.
555 md_clear_update_mitigation();
559 #define pr_fmt(fmt) "SRBDS: " fmt
561 enum srbds_mitigations {
562 SRBDS_MITIGATION_OFF,
563 SRBDS_MITIGATION_UCODE_NEEDED,
564 SRBDS_MITIGATION_FULL,
565 SRBDS_MITIGATION_TSX_OFF,
566 SRBDS_MITIGATION_HYPERVISOR,
569 static enum srbds_mitigations srbds_mitigation __ro_after_init = SRBDS_MITIGATION_FULL;
571 static const char * const srbds_strings[] = {
572 [SRBDS_MITIGATION_OFF] = "Vulnerable",
573 [SRBDS_MITIGATION_UCODE_NEEDED] = "Vulnerable: No microcode",
574 [SRBDS_MITIGATION_FULL] = "Mitigation: Microcode",
575 [SRBDS_MITIGATION_TSX_OFF] = "Mitigation: TSX disabled",
576 [SRBDS_MITIGATION_HYPERVISOR] = "Unknown: Dependent on hypervisor status",
579 static bool srbds_off;
581 void update_srbds_msr(void)
585 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
588 if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
591 if (srbds_mitigation == SRBDS_MITIGATION_UCODE_NEEDED)
594 rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
596 switch (srbds_mitigation) {
597 case SRBDS_MITIGATION_OFF:
598 case SRBDS_MITIGATION_TSX_OFF:
599 mcu_ctrl |= RNGDS_MITG_DIS;
601 case SRBDS_MITIGATION_FULL:
602 mcu_ctrl &= ~RNGDS_MITG_DIS;
608 wrmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
611 static void __init srbds_select_mitigation(void)
615 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
619 * Check to see if this is one of the MDS_NO systems supporting TSX that
620 * are only exposed to SRBDS when TSX is enabled or when CPU is affected
621 * by Processor MMIO Stale Data vulnerability.
623 ia32_cap = x86_read_arch_cap_msr();
624 if ((ia32_cap & ARCH_CAP_MDS_NO) && !boot_cpu_has(X86_FEATURE_RTM) &&
625 !boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA))
626 srbds_mitigation = SRBDS_MITIGATION_TSX_OFF;
627 else if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
628 srbds_mitigation = SRBDS_MITIGATION_HYPERVISOR;
629 else if (!boot_cpu_has(X86_FEATURE_SRBDS_CTRL))
630 srbds_mitigation = SRBDS_MITIGATION_UCODE_NEEDED;
631 else if (cpu_mitigations_off() || srbds_off)
632 srbds_mitigation = SRBDS_MITIGATION_OFF;
635 pr_info("%s\n", srbds_strings[srbds_mitigation]);
638 static int __init srbds_parse_cmdline(char *str)
643 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
646 srbds_off = !strcmp(str, "off");
649 early_param("srbds", srbds_parse_cmdline);
652 #define pr_fmt(fmt) "Spectre V1 : " fmt
654 enum spectre_v1_mitigation {
655 SPECTRE_V1_MITIGATION_NONE,
656 SPECTRE_V1_MITIGATION_AUTO,
659 static enum spectre_v1_mitigation spectre_v1_mitigation __ro_after_init =
660 SPECTRE_V1_MITIGATION_AUTO;
662 static const char * const spectre_v1_strings[] = {
663 [SPECTRE_V1_MITIGATION_NONE] = "Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers",
664 [SPECTRE_V1_MITIGATION_AUTO] = "Mitigation: usercopy/swapgs barriers and __user pointer sanitization",
668 * Does SMAP provide full mitigation against speculative kernel access to
671 static bool smap_works_speculatively(void)
673 if (!boot_cpu_has(X86_FEATURE_SMAP))
677 * On CPUs which are vulnerable to Meltdown, SMAP does not
678 * prevent speculative access to user data in the L1 cache.
679 * Consider SMAP to be non-functional as a mitigation on these
682 if (boot_cpu_has(X86_BUG_CPU_MELTDOWN))
688 static void __init spectre_v1_select_mitigation(void)
690 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1) || cpu_mitigations_off()) {
691 spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
695 if (spectre_v1_mitigation == SPECTRE_V1_MITIGATION_AUTO) {
697 * With Spectre v1, a user can speculatively control either
698 * path of a conditional swapgs with a user-controlled GS
699 * value. The mitigation is to add lfences to both code paths.
701 * If FSGSBASE is enabled, the user can put a kernel address in
702 * GS, in which case SMAP provides no protection.
704 * [ NOTE: Don't check for X86_FEATURE_FSGSBASE until the
705 * FSGSBASE enablement patches have been merged. ]
707 * If FSGSBASE is disabled, the user can only put a user space
708 * address in GS. That makes an attack harder, but still
709 * possible if there's no SMAP protection.
711 if (!smap_works_speculatively()) {
713 * Mitigation can be provided from SWAPGS itself or
714 * PTI as the CR3 write in the Meltdown mitigation
717 * If neither is there, mitigate with an LFENCE to
718 * stop speculation through swapgs.
720 if (boot_cpu_has_bug(X86_BUG_SWAPGS) &&
721 !boot_cpu_has(X86_FEATURE_PTI))
722 setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_USER);
725 * Enable lfences in the kernel entry (non-swapgs)
726 * paths, to prevent user entry from speculatively
729 setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_KERNEL);
733 pr_info("%s\n", spectre_v1_strings[spectre_v1_mitigation]);
736 static int __init nospectre_v1_cmdline(char *str)
738 spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
741 early_param("nospectre_v1", nospectre_v1_cmdline);
743 static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init =
747 #define pr_fmt(fmt) "RETBleed: " fmt
749 enum retbleed_mitigation {
750 RETBLEED_MITIGATION_NONE,
751 RETBLEED_MITIGATION_IBRS,
752 RETBLEED_MITIGATION_EIBRS,
755 enum retbleed_mitigation_cmd {
760 const char * const retbleed_strings[] = {
761 [RETBLEED_MITIGATION_NONE] = "Vulnerable",
762 [RETBLEED_MITIGATION_IBRS] = "Mitigation: IBRS",
763 [RETBLEED_MITIGATION_EIBRS] = "Mitigation: Enhanced IBRS",
766 static enum retbleed_mitigation retbleed_mitigation __ro_after_init =
767 RETBLEED_MITIGATION_NONE;
768 static enum retbleed_mitigation_cmd retbleed_cmd __ro_after_init =
771 static int __init retbleed_parse_cmdline(char *str)
776 if (!strcmp(str, "off"))
777 retbleed_cmd = RETBLEED_CMD_OFF;
778 else if (!strcmp(str, "auto"))
779 retbleed_cmd = RETBLEED_CMD_AUTO;
781 pr_err("Unknown retbleed option (%s). Defaulting to 'auto'\n", str);
785 early_param("retbleed", retbleed_parse_cmdline);
787 #define RETBLEED_UNTRAIN_MSG "WARNING: BTB untrained return thunk mitigation is only effective on AMD/Hygon!\n"
788 #define RETBLEED_COMPILER_MSG "WARNING: kernel not compiled with RETPOLINE or -mfunction-return capable compiler!\n"
789 #define RETBLEED_INTEL_MSG "WARNING: Spectre v2 mitigation leaves CPU vulnerable to RETBleed attacks, data leaks possible!\n"
791 static void __init retbleed_select_mitigation(void)
793 if (!boot_cpu_has_bug(X86_BUG_RETBLEED) || cpu_mitigations_off())
796 switch (retbleed_cmd) {
797 case RETBLEED_CMD_OFF:
800 case RETBLEED_CMD_AUTO:
803 * The Intel mitigation (IBRS) was already selected in
804 * spectre_v2_select_mitigation().
810 switch (retbleed_mitigation) {
816 * Let IBRS trump all on Intel without affecting the effects of the
817 * retbleed= cmdline option.
819 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
820 switch (spectre_v2_enabled) {
821 case SPECTRE_V2_IBRS:
822 retbleed_mitigation = RETBLEED_MITIGATION_IBRS;
824 case SPECTRE_V2_EIBRS:
825 case SPECTRE_V2_EIBRS_RETPOLINE:
826 case SPECTRE_V2_EIBRS_LFENCE:
827 retbleed_mitigation = RETBLEED_MITIGATION_EIBRS;
830 pr_err(RETBLEED_INTEL_MSG);
834 pr_info("%s\n", retbleed_strings[retbleed_mitigation]);
838 #define pr_fmt(fmt) "Spectre V2 : " fmt
840 static enum spectre_v2_user_mitigation spectre_v2_user_stibp __ro_after_init =
841 SPECTRE_V2_USER_NONE;
842 static enum spectre_v2_user_mitigation spectre_v2_user_ibpb __ro_after_init =
843 SPECTRE_V2_USER_NONE;
845 #ifdef CONFIG_RETPOLINE
846 static bool spectre_v2_bad_module;
848 bool retpoline_module_ok(bool has_retpoline)
850 if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
853 pr_err("System may be vulnerable to spectre v2\n");
854 spectre_v2_bad_module = true;
858 static inline const char *spectre_v2_module_string(void)
860 return spectre_v2_bad_module ? " - vulnerable module loaded" : "";
863 static inline const char *spectre_v2_module_string(void) { return ""; }
866 #define SPECTRE_V2_LFENCE_MSG "WARNING: LFENCE mitigation is not recommended for this CPU, data leaks possible!\n"
867 #define SPECTRE_V2_EIBRS_EBPF_MSG "WARNING: Unprivileged eBPF is enabled with eIBRS on, data leaks possible via Spectre v2 BHB attacks!\n"
868 #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"
869 #define SPECTRE_V2_IBRS_PERF_MSG "WARNING: IBRS mitigation selected on Enhanced IBRS CPU, this may cause unnecessary performance loss\n"
871 #ifdef CONFIG_BPF_SYSCALL
872 void unpriv_ebpf_notify(int new_state)
877 /* Unprivileged eBPF is enabled */
879 switch (spectre_v2_enabled) {
880 case SPECTRE_V2_EIBRS:
881 pr_err(SPECTRE_V2_EIBRS_EBPF_MSG);
883 case SPECTRE_V2_EIBRS_LFENCE:
884 if (sched_smt_active())
885 pr_err(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG);
893 static inline bool match_option(const char *arg, int arglen, const char *opt)
895 int len = strlen(opt);
897 return len == arglen && !strncmp(arg, opt, len);
900 /* The kernel command line selection for spectre v2 */
901 enum spectre_v2_mitigation_cmd {
904 SPECTRE_V2_CMD_FORCE,
905 SPECTRE_V2_CMD_RETPOLINE,
906 SPECTRE_V2_CMD_RETPOLINE_GENERIC,
907 SPECTRE_V2_CMD_RETPOLINE_LFENCE,
908 SPECTRE_V2_CMD_EIBRS,
909 SPECTRE_V2_CMD_EIBRS_RETPOLINE,
910 SPECTRE_V2_CMD_EIBRS_LFENCE,
914 enum spectre_v2_user_cmd {
915 SPECTRE_V2_USER_CMD_NONE,
916 SPECTRE_V2_USER_CMD_AUTO,
917 SPECTRE_V2_USER_CMD_FORCE,
918 SPECTRE_V2_USER_CMD_PRCTL,
919 SPECTRE_V2_USER_CMD_PRCTL_IBPB,
920 SPECTRE_V2_USER_CMD_SECCOMP,
921 SPECTRE_V2_USER_CMD_SECCOMP_IBPB,
924 static const char * const spectre_v2_user_strings[] = {
925 [SPECTRE_V2_USER_NONE] = "User space: Vulnerable",
926 [SPECTRE_V2_USER_STRICT] = "User space: Mitigation: STIBP protection",
927 [SPECTRE_V2_USER_STRICT_PREFERRED] = "User space: Mitigation: STIBP always-on protection",
928 [SPECTRE_V2_USER_PRCTL] = "User space: Mitigation: STIBP via prctl",
929 [SPECTRE_V2_USER_SECCOMP] = "User space: Mitigation: STIBP via seccomp and prctl",
932 static const struct {
934 enum spectre_v2_user_cmd cmd;
936 } v2_user_options[] __initconst = {
937 { "auto", SPECTRE_V2_USER_CMD_AUTO, false },
938 { "off", SPECTRE_V2_USER_CMD_NONE, false },
939 { "on", SPECTRE_V2_USER_CMD_FORCE, true },
940 { "prctl", SPECTRE_V2_USER_CMD_PRCTL, false },
941 { "prctl,ibpb", SPECTRE_V2_USER_CMD_PRCTL_IBPB, false },
942 { "seccomp", SPECTRE_V2_USER_CMD_SECCOMP, false },
943 { "seccomp,ibpb", SPECTRE_V2_USER_CMD_SECCOMP_IBPB, false },
946 static void __init spec_v2_user_print_cond(const char *reason, bool secure)
948 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
949 pr_info("spectre_v2_user=%s forced on command line.\n", reason);
952 static __ro_after_init enum spectre_v2_mitigation_cmd spectre_v2_cmd;
954 static enum spectre_v2_user_cmd __init
955 spectre_v2_parse_user_cmdline(void)
960 switch (spectre_v2_cmd) {
961 case SPECTRE_V2_CMD_NONE:
962 return SPECTRE_V2_USER_CMD_NONE;
963 case SPECTRE_V2_CMD_FORCE:
964 return SPECTRE_V2_USER_CMD_FORCE;
969 ret = cmdline_find_option(boot_command_line, "spectre_v2_user",
972 return SPECTRE_V2_USER_CMD_AUTO;
974 for (i = 0; i < ARRAY_SIZE(v2_user_options); i++) {
975 if (match_option(arg, ret, v2_user_options[i].option)) {
976 spec_v2_user_print_cond(v2_user_options[i].option,
977 v2_user_options[i].secure);
978 return v2_user_options[i].cmd;
982 pr_err("Unknown user space protection option (%s). Switching to AUTO select\n", arg);
983 return SPECTRE_V2_USER_CMD_AUTO;
986 static inline bool spectre_v2_in_eibrs_mode(enum spectre_v2_mitigation mode)
988 return mode == SPECTRE_V2_EIBRS ||
989 mode == SPECTRE_V2_EIBRS_RETPOLINE ||
990 mode == SPECTRE_V2_EIBRS_LFENCE;
993 static inline bool spectre_v2_in_ibrs_mode(enum spectre_v2_mitigation mode)
995 return spectre_v2_in_eibrs_mode(mode) || mode == SPECTRE_V2_IBRS;
999 spectre_v2_user_select_mitigation(void)
1001 enum spectre_v2_user_mitigation mode = SPECTRE_V2_USER_NONE;
1002 bool smt_possible = IS_ENABLED(CONFIG_SMP);
1003 enum spectre_v2_user_cmd cmd;
1005 if (!boot_cpu_has(X86_FEATURE_IBPB) && !boot_cpu_has(X86_FEATURE_STIBP))
1008 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
1009 cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
1010 smt_possible = false;
1012 cmd = spectre_v2_parse_user_cmdline();
1014 case SPECTRE_V2_USER_CMD_NONE:
1016 case SPECTRE_V2_USER_CMD_FORCE:
1017 mode = SPECTRE_V2_USER_STRICT;
1019 case SPECTRE_V2_USER_CMD_PRCTL:
1020 case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
1021 mode = SPECTRE_V2_USER_PRCTL;
1023 case SPECTRE_V2_USER_CMD_AUTO:
1024 case SPECTRE_V2_USER_CMD_SECCOMP:
1025 case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
1026 if (IS_ENABLED(CONFIG_SECCOMP))
1027 mode = SPECTRE_V2_USER_SECCOMP;
1029 mode = SPECTRE_V2_USER_PRCTL;
1033 /* Initialize Indirect Branch Prediction Barrier */
1034 if (boot_cpu_has(X86_FEATURE_IBPB)) {
1035 setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
1037 spectre_v2_user_ibpb = mode;
1039 case SPECTRE_V2_USER_CMD_FORCE:
1040 case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
1041 case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
1042 static_branch_enable(&switch_mm_always_ibpb);
1043 spectre_v2_user_ibpb = SPECTRE_V2_USER_STRICT;
1045 case SPECTRE_V2_USER_CMD_PRCTL:
1046 case SPECTRE_V2_USER_CMD_AUTO:
1047 case SPECTRE_V2_USER_CMD_SECCOMP:
1048 static_branch_enable(&switch_mm_cond_ibpb);
1054 pr_info("mitigation: Enabling %s Indirect Branch Prediction Barrier\n",
1055 static_key_enabled(&switch_mm_always_ibpb) ?
1056 "always-on" : "conditional");
1060 * If no STIBP, enhanced IBRS is enabled, or SMT impossible, STIBP
1063 * Enhanced IBRS also protects against cross-thread branch target
1064 * injection in user-mode as the IBRS bit remains always set which
1065 * implicitly enables cross-thread protections. However, in legacy IBRS
1066 * mode, the IBRS bit is set only on kernel entry and cleared on return
1067 * to userspace. This disables the implicit cross-thread protection,
1068 * so allow for STIBP to be selected in that case.
1070 if (!boot_cpu_has(X86_FEATURE_STIBP) ||
1072 spectre_v2_in_eibrs_mode(spectre_v2_enabled))
1076 * At this point, an STIBP mode other than "off" has been set.
1077 * If STIBP support is not being forced, check if STIBP always-on
1080 if (mode != SPECTRE_V2_USER_STRICT &&
1081 boot_cpu_has(X86_FEATURE_AMD_STIBP_ALWAYS_ON))
1082 mode = SPECTRE_V2_USER_STRICT_PREFERRED;
1084 spectre_v2_user_stibp = mode;
1087 pr_info("%s\n", spectre_v2_user_strings[mode]);
1090 static const char * const spectre_v2_strings[] = {
1091 [SPECTRE_V2_NONE] = "Vulnerable",
1092 [SPECTRE_V2_RETPOLINE] = "Mitigation: Retpolines",
1093 [SPECTRE_V2_LFENCE] = "Mitigation: LFENCE",
1094 [SPECTRE_V2_EIBRS] = "Mitigation: Enhanced IBRS",
1095 [SPECTRE_V2_EIBRS_LFENCE] = "Mitigation: Enhanced IBRS + LFENCE",
1096 [SPECTRE_V2_EIBRS_RETPOLINE] = "Mitigation: Enhanced IBRS + Retpolines",
1097 [SPECTRE_V2_IBRS] = "Mitigation: IBRS",
1100 static const struct {
1102 enum spectre_v2_mitigation_cmd cmd;
1104 } mitigation_options[] __initconst = {
1105 { "off", SPECTRE_V2_CMD_NONE, false },
1106 { "on", SPECTRE_V2_CMD_FORCE, true },
1107 { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false },
1108 { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_LFENCE, false },
1109 { "retpoline,lfence", SPECTRE_V2_CMD_RETPOLINE_LFENCE, false },
1110 { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
1111 { "eibrs", SPECTRE_V2_CMD_EIBRS, false },
1112 { "eibrs,lfence", SPECTRE_V2_CMD_EIBRS_LFENCE, false },
1113 { "eibrs,retpoline", SPECTRE_V2_CMD_EIBRS_RETPOLINE, false },
1114 { "auto", SPECTRE_V2_CMD_AUTO, false },
1115 { "ibrs", SPECTRE_V2_CMD_IBRS, false },
1118 static void __init spec_v2_print_cond(const char *reason, bool secure)
1120 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
1121 pr_info("%s selected on command line.\n", reason);
1124 static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
1126 enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
1130 if (cmdline_find_option_bool(boot_command_line, "nospectre_v2") ||
1131 cpu_mitigations_off())
1132 return SPECTRE_V2_CMD_NONE;
1134 ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
1136 return SPECTRE_V2_CMD_AUTO;
1138 for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
1139 if (!match_option(arg, ret, mitigation_options[i].option))
1141 cmd = mitigation_options[i].cmd;
1145 if (i >= ARRAY_SIZE(mitigation_options)) {
1146 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
1147 return SPECTRE_V2_CMD_AUTO;
1150 if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
1151 cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE ||
1152 cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC ||
1153 cmd == SPECTRE_V2_CMD_EIBRS_LFENCE ||
1154 cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) &&
1155 !IS_ENABLED(CONFIG_RETPOLINE)) {
1156 pr_err("%s selected but not compiled in. Switching to AUTO select\n",
1157 mitigation_options[i].option);
1158 return SPECTRE_V2_CMD_AUTO;
1161 if ((cmd == SPECTRE_V2_CMD_EIBRS ||
1162 cmd == SPECTRE_V2_CMD_EIBRS_LFENCE ||
1163 cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) &&
1164 !boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
1165 pr_err("%s selected but CPU doesn't have eIBRS. Switching to AUTO select\n",
1166 mitigation_options[i].option);
1167 return SPECTRE_V2_CMD_AUTO;
1170 if ((cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE ||
1171 cmd == SPECTRE_V2_CMD_EIBRS_LFENCE) &&
1172 !boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
1173 pr_err("%s selected, but CPU doesn't have a serializing LFENCE. Switching to AUTO select\n",
1174 mitigation_options[i].option);
1175 return SPECTRE_V2_CMD_AUTO;
1178 if (cmd == SPECTRE_V2_CMD_IBRS && boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
1179 pr_err("%s selected but not Intel CPU. Switching to AUTO select\n",
1180 mitigation_options[i].option);
1181 return SPECTRE_V2_CMD_AUTO;
1184 if (cmd == SPECTRE_V2_CMD_IBRS && !boot_cpu_has(X86_FEATURE_IBRS)) {
1185 pr_err("%s selected but CPU doesn't have IBRS. Switching to AUTO select\n",
1186 mitigation_options[i].option);
1187 return SPECTRE_V2_CMD_AUTO;
1190 if (cmd == SPECTRE_V2_CMD_IBRS && boot_cpu_has(X86_FEATURE_XENPV)) {
1191 pr_err("%s selected but running as XenPV guest. Switching to AUTO select\n",
1192 mitigation_options[i].option);
1193 return SPECTRE_V2_CMD_AUTO;
1196 spec_v2_print_cond(mitigation_options[i].option,
1197 mitigation_options[i].secure);
1201 static enum spectre_v2_mitigation __init spectre_v2_select_retpoline(void)
1203 if (!IS_ENABLED(CONFIG_RETPOLINE)) {
1204 pr_err("Kernel not compiled with retpoline; no mitigation available!");
1205 return SPECTRE_V2_NONE;
1208 return SPECTRE_V2_RETPOLINE;
1211 /* Disable in-kernel use of non-RSB RET predictors */
1212 static void __init spec_ctrl_disable_kernel_rrsba(void)
1216 if (!boot_cpu_has(X86_FEATURE_RRSBA_CTRL))
1219 ia32_cap = x86_read_arch_cap_msr();
1221 if (ia32_cap & ARCH_CAP_RRSBA) {
1222 x86_spec_ctrl_base |= SPEC_CTRL_RRSBA_DIS_S;
1223 update_spec_ctrl(x86_spec_ctrl_base);
1227 static void __init spectre_v2_determine_rsb_fill_type_at_vmexit(enum spectre_v2_mitigation mode)
1230 * Similar to context switches, there are two types of RSB attacks
1235 * 2) Poisoned RSB entry
1237 * When retpoline is enabled, both are mitigated by filling/clearing
1240 * When IBRS is enabled, while #1 would be mitigated by the IBRS branch
1241 * prediction isolation protections, RSB still needs to be cleared
1242 * because of #2. Note that SMEP provides no protection here, unlike
1243 * user-space-poisoned RSB entries.
1245 * eIBRS should protect against RSB poisoning, but if the EIBRS_PBRSB
1246 * bug is present then a LITE version of RSB protection is required,
1247 * just a single call needs to retire before a RET is executed.
1250 case SPECTRE_V2_NONE:
1253 case SPECTRE_V2_EIBRS_LFENCE:
1254 case SPECTRE_V2_EIBRS:
1255 if (boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB) &&
1256 (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)) {
1257 setup_force_cpu_cap(X86_FEATURE_RSB_VMEXIT_LITE);
1258 pr_info("Spectre v2 / PBRSB-eIBRS: Retire a single CALL on VMEXIT\n");
1262 case SPECTRE_V2_EIBRS_RETPOLINE:
1263 case SPECTRE_V2_RETPOLINE:
1264 case SPECTRE_V2_LFENCE:
1265 case SPECTRE_V2_IBRS:
1266 setup_force_cpu_cap(X86_FEATURE_RSB_VMEXIT);
1267 pr_info("Spectre v2 / SpectreRSB : Filling RSB on VMEXIT\n");
1271 pr_warn_once("Unknown Spectre v2 mode, disabling RSB mitigation at VM exit");
1275 static void __init spectre_v2_select_mitigation(void)
1277 enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
1278 enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
1281 * If the CPU is not affected and the command line mode is NONE or AUTO
1282 * then nothing to do.
1284 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
1285 (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
1289 case SPECTRE_V2_CMD_NONE:
1292 case SPECTRE_V2_CMD_FORCE:
1293 case SPECTRE_V2_CMD_AUTO:
1294 if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
1295 mode = SPECTRE_V2_EIBRS;
1299 if (boot_cpu_has_bug(X86_BUG_RETBLEED) &&
1300 retbleed_cmd != RETBLEED_CMD_OFF &&
1301 boot_cpu_has(X86_FEATURE_IBRS) &&
1302 boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
1303 mode = SPECTRE_V2_IBRS;
1307 mode = spectre_v2_select_retpoline();
1310 case SPECTRE_V2_CMD_RETPOLINE_LFENCE:
1311 pr_err(SPECTRE_V2_LFENCE_MSG);
1312 mode = SPECTRE_V2_LFENCE;
1315 case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
1316 mode = SPECTRE_V2_RETPOLINE;
1319 case SPECTRE_V2_CMD_RETPOLINE:
1320 mode = spectre_v2_select_retpoline();
1323 case SPECTRE_V2_CMD_IBRS:
1324 mode = SPECTRE_V2_IBRS;
1327 case SPECTRE_V2_CMD_EIBRS:
1328 mode = SPECTRE_V2_EIBRS;
1331 case SPECTRE_V2_CMD_EIBRS_LFENCE:
1332 mode = SPECTRE_V2_EIBRS_LFENCE;
1335 case SPECTRE_V2_CMD_EIBRS_RETPOLINE:
1336 mode = SPECTRE_V2_EIBRS_RETPOLINE;
1340 if (mode == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
1341 pr_err(SPECTRE_V2_EIBRS_EBPF_MSG);
1343 if (spectre_v2_in_ibrs_mode(mode)) {
1344 x86_spec_ctrl_base |= SPEC_CTRL_IBRS;
1345 update_spec_ctrl(x86_spec_ctrl_base);
1349 case SPECTRE_V2_NONE:
1350 case SPECTRE_V2_EIBRS:
1353 case SPECTRE_V2_IBRS:
1354 setup_force_cpu_cap(X86_FEATURE_KERNEL_IBRS);
1355 if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED))
1356 pr_warn(SPECTRE_V2_IBRS_PERF_MSG);
1359 case SPECTRE_V2_LFENCE:
1360 case SPECTRE_V2_EIBRS_LFENCE:
1361 setup_force_cpu_cap(X86_FEATURE_RETPOLINE_LFENCE);
1364 case SPECTRE_V2_RETPOLINE:
1365 case SPECTRE_V2_EIBRS_RETPOLINE:
1366 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
1371 * Disable alternate RSB predictions in kernel when indirect CALLs and
1372 * JMPs gets protection against BHI and Intramode-BTI, but RET
1373 * prediction from a non-RSB predictor is still a risk.
1375 if (mode == SPECTRE_V2_EIBRS_LFENCE ||
1376 mode == SPECTRE_V2_EIBRS_RETPOLINE ||
1377 mode == SPECTRE_V2_RETPOLINE)
1378 spec_ctrl_disable_kernel_rrsba();
1380 spectre_v2_enabled = mode;
1381 pr_info("%s\n", spectre_v2_strings[mode]);
1384 * If Spectre v2 protection has been enabled, fill the RSB during a
1385 * context switch. In general there are two types of RSB attacks
1386 * across context switches, for which the CALLs/RETs may be unbalanced.
1390 * Some Intel parts have "bottomless RSB". When the RSB is empty,
1391 * speculated return targets may come from the branch predictor,
1392 * which could have a user-poisoned BTB or BHB entry.
1394 * AMD has it even worse: *all* returns are speculated from the BTB,
1395 * regardless of the state of the RSB.
1397 * When IBRS or eIBRS is enabled, the "user -> kernel" attack
1398 * scenario is mitigated by the IBRS branch prediction isolation
1399 * properties, so the RSB buffer filling wouldn't be necessary to
1400 * protect against this type of attack.
1402 * The "user -> user" attack scenario is mitigated by RSB filling.
1404 * 2) Poisoned RSB entry
1406 * If the 'next' in-kernel return stack is shorter than 'prev',
1407 * 'next' could be tricked into speculating with a user-poisoned RSB
1410 * The "user -> kernel" attack scenario is mitigated by SMEP and
1413 * The "user -> user" scenario, also known as SpectreBHB, requires
1416 * So to mitigate all cases, unconditionally fill RSB on context
1419 * FIXME: Is this pointless for retbleed-affected AMD?
1421 setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
1422 pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n");
1424 spectre_v2_determine_rsb_fill_type_at_vmexit(mode);
1427 * Retpoline protects the kernel, but doesn't protect firmware. IBRS
1428 * and Enhanced IBRS protect firmware too, so enable IBRS around
1429 * firmware calls only when IBRS / Enhanced IBRS aren't otherwise
1432 * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because
1433 * the user might select retpoline on the kernel command line and if
1434 * the CPU supports Enhanced IBRS, kernel might un-intentionally not
1435 * enable IBRS around firmware calls.
1437 if (boot_cpu_has(X86_FEATURE_IBRS) && !spectre_v2_in_ibrs_mode(mode)) {
1438 setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
1439 pr_info("Enabling Restricted Speculation for firmware calls\n");
1442 /* Set up IBPB and STIBP depending on the general spectre V2 command */
1443 spectre_v2_cmd = cmd;
1446 static void update_stibp_msr(void * __unused)
1448 u64 val = spec_ctrl_current() | (x86_spec_ctrl_base & SPEC_CTRL_STIBP);
1449 update_spec_ctrl(val);
1452 /* Update x86_spec_ctrl_base in case SMT state changed. */
1453 static void update_stibp_strict(void)
1455 u64 mask = x86_spec_ctrl_base & ~SPEC_CTRL_STIBP;
1457 if (sched_smt_active())
1458 mask |= SPEC_CTRL_STIBP;
1460 if (mask == x86_spec_ctrl_base)
1463 pr_info("Update user space SMT mitigation: STIBP %s\n",
1464 mask & SPEC_CTRL_STIBP ? "always-on" : "off");
1465 x86_spec_ctrl_base = mask;
1466 on_each_cpu(update_stibp_msr, NULL, 1);
1469 /* Update the static key controlling the evaluation of TIF_SPEC_IB */
1470 static void update_indir_branch_cond(void)
1472 if (sched_smt_active())
1473 static_branch_enable(&switch_to_cond_stibp);
1475 static_branch_disable(&switch_to_cond_stibp);
1479 #define pr_fmt(fmt) fmt
1481 /* Update the static key controlling the MDS CPU buffer clear in idle */
1482 static void update_mds_branch_idle(void)
1484 u64 ia32_cap = x86_read_arch_cap_msr();
1487 * Enable the idle clearing if SMT is active on CPUs which are
1488 * affected only by MSBDS and not any other MDS variant.
1490 * The other variants cannot be mitigated when SMT is enabled, so
1491 * clearing the buffers on idle just to prevent the Store Buffer
1492 * repartitioning leak would be a window dressing exercise.
1494 if (!boot_cpu_has_bug(X86_BUG_MSBDS_ONLY))
1497 if (sched_smt_active()) {
1498 static_branch_enable(&mds_idle_clear);
1499 } else if (mmio_mitigation == MMIO_MITIGATION_OFF ||
1500 (ia32_cap & ARCH_CAP_FBSDP_NO)) {
1501 static_branch_disable(&mds_idle_clear);
1505 #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"
1506 #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"
1507 #define MMIO_MSG_SMT "MMIO Stale Data CPU bug present and SMT on, data leak possible. See https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/processor_mmio_stale_data.html for more details.\n"
1509 void arch_smt_update(void)
1511 mutex_lock(&spec_ctrl_mutex);
1513 if (sched_smt_active() && unprivileged_ebpf_enabled() &&
1514 spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
1515 pr_warn_once(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG);
1517 switch (spectre_v2_user_stibp) {
1518 case SPECTRE_V2_USER_NONE:
1520 case SPECTRE_V2_USER_STRICT:
1521 case SPECTRE_V2_USER_STRICT_PREFERRED:
1522 update_stibp_strict();
1524 case SPECTRE_V2_USER_PRCTL:
1525 case SPECTRE_V2_USER_SECCOMP:
1526 update_indir_branch_cond();
1530 switch (mds_mitigation) {
1531 case MDS_MITIGATION_FULL:
1532 case MDS_MITIGATION_VMWERV:
1533 if (sched_smt_active() && !boot_cpu_has(X86_BUG_MSBDS_ONLY))
1534 pr_warn_once(MDS_MSG_SMT);
1535 update_mds_branch_idle();
1537 case MDS_MITIGATION_OFF:
1541 switch (taa_mitigation) {
1542 case TAA_MITIGATION_VERW:
1543 case TAA_MITIGATION_UCODE_NEEDED:
1544 if (sched_smt_active())
1545 pr_warn_once(TAA_MSG_SMT);
1547 case TAA_MITIGATION_TSX_DISABLED:
1548 case TAA_MITIGATION_OFF:
1552 switch (mmio_mitigation) {
1553 case MMIO_MITIGATION_VERW:
1554 case MMIO_MITIGATION_UCODE_NEEDED:
1555 if (sched_smt_active())
1556 pr_warn_once(MMIO_MSG_SMT);
1558 case MMIO_MITIGATION_OFF:
1562 mutex_unlock(&spec_ctrl_mutex);
1566 #define pr_fmt(fmt) "Speculative Store Bypass: " fmt
1568 static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
1570 /* The kernel command line selection */
1571 enum ssb_mitigation_cmd {
1572 SPEC_STORE_BYPASS_CMD_NONE,
1573 SPEC_STORE_BYPASS_CMD_AUTO,
1574 SPEC_STORE_BYPASS_CMD_ON,
1575 SPEC_STORE_BYPASS_CMD_PRCTL,
1576 SPEC_STORE_BYPASS_CMD_SECCOMP,
1579 static const char * const ssb_strings[] = {
1580 [SPEC_STORE_BYPASS_NONE] = "Vulnerable",
1581 [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass disabled",
1582 [SPEC_STORE_BYPASS_PRCTL] = "Mitigation: Speculative Store Bypass disabled via prctl",
1583 [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
1586 static const struct {
1588 enum ssb_mitigation_cmd cmd;
1589 } ssb_mitigation_options[] __initconst = {
1590 { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */
1591 { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */
1592 { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */
1593 { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative Store Bypass via prctl */
1594 { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */
1597 static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
1599 enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO;
1603 if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable") ||
1604 cpu_mitigations_off()) {
1605 return SPEC_STORE_BYPASS_CMD_NONE;
1607 ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable",
1610 return SPEC_STORE_BYPASS_CMD_AUTO;
1612 for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) {
1613 if (!match_option(arg, ret, ssb_mitigation_options[i].option))
1616 cmd = ssb_mitigation_options[i].cmd;
1620 if (i >= ARRAY_SIZE(ssb_mitigation_options)) {
1621 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
1622 return SPEC_STORE_BYPASS_CMD_AUTO;
1629 static enum ssb_mitigation __init __ssb_select_mitigation(void)
1631 enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
1632 enum ssb_mitigation_cmd cmd;
1634 if (!boot_cpu_has(X86_FEATURE_SSBD))
1637 cmd = ssb_parse_cmdline();
1638 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) &&
1639 (cmd == SPEC_STORE_BYPASS_CMD_NONE ||
1640 cmd == SPEC_STORE_BYPASS_CMD_AUTO))
1644 case SPEC_STORE_BYPASS_CMD_AUTO:
1645 case SPEC_STORE_BYPASS_CMD_SECCOMP:
1647 * Choose prctl+seccomp as the default mode if seccomp is
1650 if (IS_ENABLED(CONFIG_SECCOMP))
1651 mode = SPEC_STORE_BYPASS_SECCOMP;
1653 mode = SPEC_STORE_BYPASS_PRCTL;
1655 case SPEC_STORE_BYPASS_CMD_ON:
1656 mode = SPEC_STORE_BYPASS_DISABLE;
1658 case SPEC_STORE_BYPASS_CMD_PRCTL:
1659 mode = SPEC_STORE_BYPASS_PRCTL;
1661 case SPEC_STORE_BYPASS_CMD_NONE:
1666 * We have three CPU feature flags that are in play here:
1667 * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
1668 * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
1669 * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
1671 if (mode == SPEC_STORE_BYPASS_DISABLE) {
1672 setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
1674 * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
1675 * use a completely different MSR and bit dependent on family.
1677 if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
1678 !static_cpu_has(X86_FEATURE_AMD_SSBD)) {
1679 x86_amd_ssb_disable();
1681 x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
1682 update_spec_ctrl(x86_spec_ctrl_base);
1689 static void ssb_select_mitigation(void)
1691 ssb_mode = __ssb_select_mitigation();
1693 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1694 pr_info("%s\n", ssb_strings[ssb_mode]);
1698 #define pr_fmt(fmt) "Speculation prctl: " fmt
1700 static void task_update_spec_tif(struct task_struct *tsk)
1702 /* Force the update of the real TIF bits */
1703 set_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE);
1706 * Immediately update the speculation control MSRs for the current
1707 * task, but for a non-current task delay setting the CPU
1708 * mitigation until it is scheduled next.
1710 * This can only happen for SECCOMP mitigation. For PRCTL it's
1711 * always the current task.
1714 speculation_ctrl_update_current();
1717 static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
1719 if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
1720 ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
1724 case PR_SPEC_ENABLE:
1725 /* If speculation is force disabled, enable is not allowed */
1726 if (task_spec_ssb_force_disable(task))
1728 task_clear_spec_ssb_disable(task);
1729 task_update_spec_tif(task);
1731 case PR_SPEC_DISABLE:
1732 task_set_spec_ssb_disable(task);
1733 task_update_spec_tif(task);
1735 case PR_SPEC_FORCE_DISABLE:
1736 task_set_spec_ssb_disable(task);
1737 task_set_spec_ssb_force_disable(task);
1738 task_update_spec_tif(task);
1746 static bool is_spec_ib_user_controlled(void)
1748 return spectre_v2_user_ibpb == SPECTRE_V2_USER_PRCTL ||
1749 spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
1750 spectre_v2_user_stibp == SPECTRE_V2_USER_PRCTL ||
1751 spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP;
1754 static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)
1757 case PR_SPEC_ENABLE:
1758 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1759 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
1762 * With strict mode for both IBPB and STIBP, the instruction
1763 * code paths avoid checking this task flag and instead,
1764 * unconditionally run the instruction. However, STIBP and IBPB
1765 * are independent and either can be set to conditionally
1766 * enabled regardless of the mode of the other.
1768 * If either is set to conditional, allow the task flag to be
1769 * updated, unless it was force-disabled by a previous prctl
1770 * call. Currently, this is possible on an AMD CPU which has the
1771 * feature X86_FEATURE_AMD_STIBP_ALWAYS_ON. In this case, if the
1772 * kernel is booted with 'spectre_v2_user=seccomp', then
1773 * spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP and
1774 * spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED.
1776 if (!is_spec_ib_user_controlled() ||
1777 task_spec_ib_force_disable(task))
1780 task_clear_spec_ib_disable(task);
1781 task_update_spec_tif(task);
1783 case PR_SPEC_DISABLE:
1784 case PR_SPEC_FORCE_DISABLE:
1786 * Indirect branch speculation is always allowed when
1787 * mitigation is force disabled.
1789 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1790 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
1793 if (!is_spec_ib_user_controlled())
1796 task_set_spec_ib_disable(task);
1797 if (ctrl == PR_SPEC_FORCE_DISABLE)
1798 task_set_spec_ib_force_disable(task);
1799 task_update_spec_tif(task);
1800 if (task == current)
1801 indirect_branch_prediction_barrier();
1809 int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
1813 case PR_SPEC_STORE_BYPASS:
1814 return ssb_prctl_set(task, ctrl);
1815 case PR_SPEC_INDIRECT_BRANCH:
1816 return ib_prctl_set(task, ctrl);
1822 #ifdef CONFIG_SECCOMP
1823 void arch_seccomp_spec_mitigate(struct task_struct *task)
1825 if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP)
1826 ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
1827 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
1828 spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP)
1829 ib_prctl_set(task, PR_SPEC_FORCE_DISABLE);
1833 static int ssb_prctl_get(struct task_struct *task)
1836 case SPEC_STORE_BYPASS_DISABLE:
1837 return PR_SPEC_DISABLE;
1838 case SPEC_STORE_BYPASS_SECCOMP:
1839 case SPEC_STORE_BYPASS_PRCTL:
1840 if (task_spec_ssb_force_disable(task))
1841 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
1842 if (task_spec_ssb_disable(task))
1843 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
1844 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
1846 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1847 return PR_SPEC_ENABLE;
1848 return PR_SPEC_NOT_AFFECTED;
1852 static int ib_prctl_get(struct task_struct *task)
1854 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
1855 return PR_SPEC_NOT_AFFECTED;
1857 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1858 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
1859 return PR_SPEC_ENABLE;
1860 else if (is_spec_ib_user_controlled()) {
1861 if (task_spec_ib_force_disable(task))
1862 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
1863 if (task_spec_ib_disable(task))
1864 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
1865 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
1866 } else if (spectre_v2_user_ibpb == SPECTRE_V2_USER_STRICT ||
1867 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
1868 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED)
1869 return PR_SPEC_DISABLE;
1871 return PR_SPEC_NOT_AFFECTED;
1874 int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
1877 case PR_SPEC_STORE_BYPASS:
1878 return ssb_prctl_get(task);
1879 case PR_SPEC_INDIRECT_BRANCH:
1880 return ib_prctl_get(task);
1886 void x86_spec_ctrl_setup_ap(void)
1888 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
1889 update_spec_ctrl(x86_spec_ctrl_base);
1891 if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
1892 x86_amd_ssb_disable();
1895 bool itlb_multihit_kvm_mitigation;
1896 EXPORT_SYMBOL_GPL(itlb_multihit_kvm_mitigation);
1899 #define pr_fmt(fmt) "L1TF: " fmt
1901 /* Default mitigation for L1TF-affected CPUs */
1902 enum l1tf_mitigations l1tf_mitigation __ro_after_init = L1TF_MITIGATION_FLUSH;
1903 #if IS_ENABLED(CONFIG_KVM_INTEL)
1904 EXPORT_SYMBOL_GPL(l1tf_mitigation);
1906 enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
1907 EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation);
1910 * These CPUs all support 44bits physical address space internally in the
1911 * cache but CPUID can report a smaller number of physical address bits.
1913 * The L1TF mitigation uses the top most address bit for the inversion of
1914 * non present PTEs. When the installed memory reaches into the top most
1915 * address bit due to memory holes, which has been observed on machines
1916 * which report 36bits physical address bits and have 32G RAM installed,
1917 * then the mitigation range check in l1tf_select_mitigation() triggers.
1918 * This is a false positive because the mitigation is still possible due to
1919 * the fact that the cache uses 44bit internally. Use the cache bits
1920 * instead of the reported physical bits and adjust them on the affected
1921 * machines to 44bit if the reported bits are less than 44.
1923 static void override_cache_bits(struct cpuinfo_x86 *c)
1928 switch (c->x86_model) {
1929 case INTEL_FAM6_NEHALEM:
1930 case INTEL_FAM6_WESTMERE:
1931 case INTEL_FAM6_SANDYBRIDGE:
1932 case INTEL_FAM6_IVYBRIDGE:
1933 case INTEL_FAM6_HASWELL_CORE:
1934 case INTEL_FAM6_HASWELL_ULT:
1935 case INTEL_FAM6_HASWELL_GT3E:
1936 case INTEL_FAM6_BROADWELL_CORE:
1937 case INTEL_FAM6_BROADWELL_GT3E:
1938 case INTEL_FAM6_SKYLAKE_MOBILE:
1939 case INTEL_FAM6_SKYLAKE_DESKTOP:
1940 case INTEL_FAM6_KABYLAKE_MOBILE:
1941 case INTEL_FAM6_KABYLAKE_DESKTOP:
1942 if (c->x86_cache_bits < 44)
1943 c->x86_cache_bits = 44;
1948 static void __init l1tf_select_mitigation(void)
1952 if (!boot_cpu_has_bug(X86_BUG_L1TF))
1955 if (cpu_mitigations_off())
1956 l1tf_mitigation = L1TF_MITIGATION_OFF;
1957 else if (cpu_mitigations_auto_nosmt())
1958 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
1960 override_cache_bits(&boot_cpu_data);
1962 switch (l1tf_mitigation) {
1963 case L1TF_MITIGATION_OFF:
1964 case L1TF_MITIGATION_FLUSH_NOWARN:
1965 case L1TF_MITIGATION_FLUSH:
1967 case L1TF_MITIGATION_FLUSH_NOSMT:
1968 case L1TF_MITIGATION_FULL:
1969 cpu_smt_disable(false);
1971 case L1TF_MITIGATION_FULL_FORCE:
1972 cpu_smt_disable(true);
1976 #if CONFIG_PGTABLE_LEVELS == 2
1977 pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
1981 half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT;
1982 if (l1tf_mitigation != L1TF_MITIGATION_OFF &&
1983 e820__mapped_any(half_pa, ULLONG_MAX - half_pa, E820_TYPE_RAM)) {
1984 pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
1985 pr_info("You may make it effective by booting the kernel with mem=%llu parameter.\n",
1987 pr_info("However, doing so will make a part of your RAM unusable.\n");
1988 pr_info("Reading https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html might help you decide.\n");
1992 setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV);
1995 static int __init l1tf_cmdline(char *str)
1997 if (!boot_cpu_has_bug(X86_BUG_L1TF))
2003 if (!strcmp(str, "off"))
2004 l1tf_mitigation = L1TF_MITIGATION_OFF;
2005 else if (!strcmp(str, "flush,nowarn"))
2006 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOWARN;
2007 else if (!strcmp(str, "flush"))
2008 l1tf_mitigation = L1TF_MITIGATION_FLUSH;
2009 else if (!strcmp(str, "flush,nosmt"))
2010 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
2011 else if (!strcmp(str, "full"))
2012 l1tf_mitigation = L1TF_MITIGATION_FULL;
2013 else if (!strcmp(str, "full,force"))
2014 l1tf_mitigation = L1TF_MITIGATION_FULL_FORCE;
2018 early_param("l1tf", l1tf_cmdline);
2021 #define pr_fmt(fmt) fmt
2025 #define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
2027 #if IS_ENABLED(CONFIG_KVM_INTEL)
2028 static const char * const l1tf_vmx_states[] = {
2029 [VMENTER_L1D_FLUSH_AUTO] = "auto",
2030 [VMENTER_L1D_FLUSH_NEVER] = "vulnerable",
2031 [VMENTER_L1D_FLUSH_COND] = "conditional cache flushes",
2032 [VMENTER_L1D_FLUSH_ALWAYS] = "cache flushes",
2033 [VMENTER_L1D_FLUSH_EPT_DISABLED] = "EPT disabled",
2034 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = "flush not necessary"
2037 static ssize_t l1tf_show_state(char *buf)
2039 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO)
2040 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
2042 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_EPT_DISABLED ||
2043 (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER &&
2044 sched_smt_active())) {
2045 return sprintf(buf, "%s; VMX: %s\n", L1TF_DEFAULT_MSG,
2046 l1tf_vmx_states[l1tf_vmx_mitigation]);
2049 return sprintf(buf, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG,
2050 l1tf_vmx_states[l1tf_vmx_mitigation],
2051 sched_smt_active() ? "vulnerable" : "disabled");
2054 static ssize_t itlb_multihit_show_state(char *buf)
2056 if (itlb_multihit_kvm_mitigation)
2057 return sprintf(buf, "KVM: Mitigation: Split huge pages\n");
2059 return sprintf(buf, "KVM: Vulnerable\n");
2062 static ssize_t l1tf_show_state(char *buf)
2064 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
2067 static ssize_t itlb_multihit_show_state(char *buf)
2069 return sprintf(buf, "Processor vulnerable\n");
2073 static ssize_t mds_show_state(char *buf)
2075 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
2076 return sprintf(buf, "%s; SMT Host state unknown\n",
2077 mds_strings[mds_mitigation]);
2080 if (boot_cpu_has(X86_BUG_MSBDS_ONLY)) {
2081 return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
2082 (mds_mitigation == MDS_MITIGATION_OFF ? "vulnerable" :
2083 sched_smt_active() ? "mitigated" : "disabled"));
2086 return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
2087 sched_smt_active() ? "vulnerable" : "disabled");
2090 static ssize_t tsx_async_abort_show_state(char *buf)
2092 if ((taa_mitigation == TAA_MITIGATION_TSX_DISABLED) ||
2093 (taa_mitigation == TAA_MITIGATION_OFF))
2094 return sprintf(buf, "%s\n", taa_strings[taa_mitigation]);
2096 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
2097 return sprintf(buf, "%s; SMT Host state unknown\n",
2098 taa_strings[taa_mitigation]);
2101 return sprintf(buf, "%s; SMT %s\n", taa_strings[taa_mitigation],
2102 sched_smt_active() ? "vulnerable" : "disabled");
2105 static ssize_t mmio_stale_data_show_state(char *buf)
2107 if (boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN))
2108 return sysfs_emit(buf, "Unknown: No mitigations\n");
2110 if (mmio_mitigation == MMIO_MITIGATION_OFF)
2111 return sysfs_emit(buf, "%s\n", mmio_strings[mmio_mitigation]);
2113 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
2114 return sysfs_emit(buf, "%s; SMT Host state unknown\n",
2115 mmio_strings[mmio_mitigation]);
2118 return sysfs_emit(buf, "%s; SMT %s\n", mmio_strings[mmio_mitigation],
2119 sched_smt_active() ? "vulnerable" : "disabled");
2122 static char *stibp_state(void)
2124 if (spectre_v2_in_eibrs_mode(spectre_v2_enabled))
2127 switch (spectre_v2_user_stibp) {
2128 case SPECTRE_V2_USER_NONE:
2129 return ", STIBP: disabled";
2130 case SPECTRE_V2_USER_STRICT:
2131 return ", STIBP: forced";
2132 case SPECTRE_V2_USER_STRICT_PREFERRED:
2133 return ", STIBP: always-on";
2134 case SPECTRE_V2_USER_PRCTL:
2135 case SPECTRE_V2_USER_SECCOMP:
2136 if (static_key_enabled(&switch_to_cond_stibp))
2137 return ", STIBP: conditional";
2142 static char *ibpb_state(void)
2144 if (boot_cpu_has(X86_FEATURE_IBPB)) {
2145 if (static_key_enabled(&switch_mm_always_ibpb))
2146 return ", IBPB: always-on";
2147 if (static_key_enabled(&switch_mm_cond_ibpb))
2148 return ", IBPB: conditional";
2149 return ", IBPB: disabled";
2154 static char *pbrsb_eibrs_state(void)
2156 if (boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB)) {
2157 if (boot_cpu_has(X86_FEATURE_RSB_VMEXIT_LITE) ||
2158 boot_cpu_has(X86_FEATURE_RSB_VMEXIT))
2159 return ", PBRSB-eIBRS: SW sequence";
2161 return ", PBRSB-eIBRS: Vulnerable";
2163 return ", PBRSB-eIBRS: Not affected";
2167 static ssize_t spectre_v2_show_state(char *buf)
2169 if (spectre_v2_enabled == SPECTRE_V2_LFENCE)
2170 return sprintf(buf, "Vulnerable: LFENCE\n");
2172 if (spectre_v2_enabled == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
2173 return sprintf(buf, "Vulnerable: eIBRS with unprivileged eBPF\n");
2175 if (sched_smt_active() && unprivileged_ebpf_enabled() &&
2176 spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
2177 return sprintf(buf, "Vulnerable: eIBRS+LFENCE with unprivileged eBPF and SMT\n");
2179 return sprintf(buf, "%s%s%s%s%s%s%s\n",
2180 spectre_v2_strings[spectre_v2_enabled],
2182 boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
2184 boot_cpu_has(X86_FEATURE_RSB_CTXSW) ? ", RSB filling" : "",
2185 pbrsb_eibrs_state(),
2186 spectre_v2_module_string());
2189 static ssize_t srbds_show_state(char *buf)
2191 return sprintf(buf, "%s\n", srbds_strings[srbds_mitigation]);
2194 static ssize_t retbleed_show_state(char *buf)
2196 return sprintf(buf, "%s\n", retbleed_strings[retbleed_mitigation]);
2199 static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
2200 char *buf, unsigned int bug)
2202 if (!boot_cpu_has_bug(bug))
2203 return sprintf(buf, "Not affected\n");
2206 case X86_BUG_CPU_MELTDOWN:
2207 if (boot_cpu_has(X86_FEATURE_PTI))
2208 return sprintf(buf, "Mitigation: PTI\n");
2210 if (hypervisor_is_type(X86_HYPER_XEN_PV))
2211 return sprintf(buf, "Unknown (XEN PV detected, hypervisor mitigation required)\n");
2215 case X86_BUG_SPECTRE_V1:
2216 return sprintf(buf, "%s\n", spectre_v1_strings[spectre_v1_mitigation]);
2218 case X86_BUG_SPECTRE_V2:
2219 return spectre_v2_show_state(buf);
2221 case X86_BUG_SPEC_STORE_BYPASS:
2222 return sprintf(buf, "%s\n", ssb_strings[ssb_mode]);
2225 if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV))
2226 return l1tf_show_state(buf);
2230 return mds_show_state(buf);
2233 return tsx_async_abort_show_state(buf);
2235 case X86_BUG_ITLB_MULTIHIT:
2236 return itlb_multihit_show_state(buf);
2239 return srbds_show_state(buf);
2241 case X86_BUG_MMIO_STALE_DATA:
2242 case X86_BUG_MMIO_UNKNOWN:
2243 return mmio_stale_data_show_state(buf);
2245 case X86_BUG_RETBLEED:
2246 return retbleed_show_state(buf);
2252 return sprintf(buf, "Vulnerable\n");
2255 ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
2257 return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
2260 ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
2262 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
2265 ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
2267 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
2270 ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
2272 return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS);
2275 ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
2277 return cpu_show_common(dev, attr, buf, X86_BUG_L1TF);
2280 ssize_t cpu_show_mds(struct device *dev, struct device_attribute *attr, char *buf)
2282 return cpu_show_common(dev, attr, buf, X86_BUG_MDS);
2285 ssize_t cpu_show_tsx_async_abort(struct device *dev, struct device_attribute *attr, char *buf)
2287 return cpu_show_common(dev, attr, buf, X86_BUG_TAA);
2290 ssize_t cpu_show_itlb_multihit(struct device *dev, struct device_attribute *attr, char *buf)
2292 return cpu_show_common(dev, attr, buf, X86_BUG_ITLB_MULTIHIT);
2295 ssize_t cpu_show_srbds(struct device *dev, struct device_attribute *attr, char *buf)
2297 return cpu_show_common(dev, attr, buf, X86_BUG_SRBDS);
2300 ssize_t cpu_show_mmio_stale_data(struct device *dev, struct device_attribute *attr, char *buf)
2302 if (boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN))
2303 return cpu_show_common(dev, attr, buf, X86_BUG_MMIO_UNKNOWN);
2305 return cpu_show_common(dev, attr, buf, X86_BUG_MMIO_STALE_DATA);
2308 ssize_t cpu_show_retbleed(struct device *dev, struct device_attribute *attr, char *buf)
2310 return cpu_show_common(dev, attr, buf, X86_BUG_RETBLEED);