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
retbleed_select_mitigation(void);
41 static void __init
spectre_v2_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);
50 static void __init
l1d_flush_select_mitigation(void);
52 /* The base value of the SPEC_CTRL MSR without task-specific bits set */
53 u64 x86_spec_ctrl_base
;
54 EXPORT_SYMBOL_GPL(x86_spec_ctrl_base
);
56 /* The current value of the SPEC_CTRL MSR with task-specific bits set */
57 DEFINE_PER_CPU(u64
, x86_spec_ctrl_current
);
58 EXPORT_SYMBOL_GPL(x86_spec_ctrl_current
);
60 static DEFINE_MUTEX(spec_ctrl_mutex
);
63 * Keep track of the SPEC_CTRL MSR value for the current task, which may differ
64 * from x86_spec_ctrl_base due to STIBP/SSB in __speculation_ctrl_update().
66 void write_spec_ctrl_current(u64 val
, bool force
)
68 if (this_cpu_read(x86_spec_ctrl_current
) == val
)
71 this_cpu_write(x86_spec_ctrl_current
, val
);
74 * When KERNEL_IBRS this MSR is written on return-to-user, unless
75 * forced the update can be delayed until that time.
77 if (force
|| !cpu_feature_enabled(X86_FEATURE_KERNEL_IBRS
))
78 wrmsrl(MSR_IA32_SPEC_CTRL
, val
);
82 * The vendor and possibly platform specific bits which can be modified in
85 static u64 __ro_after_init x86_spec_ctrl_mask
= SPEC_CTRL_IBRS
;
88 * AMD specific MSR info for Speculative Store Bypass control.
89 * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu().
91 u64 __ro_after_init x86_amd_ls_cfg_base
;
92 u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask
;
94 /* Control conditional STIBP in switch_to() */
95 DEFINE_STATIC_KEY_FALSE(switch_to_cond_stibp
);
96 /* Control conditional IBPB in switch_mm() */
97 DEFINE_STATIC_KEY_FALSE(switch_mm_cond_ibpb
);
98 /* Control unconditional IBPB in switch_mm() */
99 DEFINE_STATIC_KEY_FALSE(switch_mm_always_ibpb
);
101 /* Control MDS CPU buffer clear before returning to user space */
102 DEFINE_STATIC_KEY_FALSE(mds_user_clear
);
103 EXPORT_SYMBOL_GPL(mds_user_clear
);
104 /* Control MDS CPU buffer clear before idling (halt, mwait) */
105 DEFINE_STATIC_KEY_FALSE(mds_idle_clear
);
106 EXPORT_SYMBOL_GPL(mds_idle_clear
);
109 * Controls whether l1d flush based mitigations are enabled,
110 * based on hw features and admin setting via boot parameter
113 DEFINE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush
);
115 /* Controls CPU Fill buffer clear before KVM guest MMIO accesses */
116 DEFINE_STATIC_KEY_FALSE(mmio_stale_data_clear
);
117 EXPORT_SYMBOL_GPL(mmio_stale_data_clear
);
119 void __init
check_bugs(void)
124 * identify_boot_cpu() initialized SMT support information, let the
127 cpu_smt_check_topology();
129 if (!IS_ENABLED(CONFIG_SMP
)) {
131 print_cpu_info(&boot_cpu_data
);
135 * Read the SPEC_CTRL MSR to account for reserved bits which may
136 * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
137 * init code as it is not enumerated and depends on the family.
139 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL
))
140 rdmsrl(MSR_IA32_SPEC_CTRL
, x86_spec_ctrl_base
);
142 /* Allow STIBP in MSR_SPEC_CTRL if supported */
143 if (boot_cpu_has(X86_FEATURE_STIBP
))
144 x86_spec_ctrl_mask
|= SPEC_CTRL_STIBP
;
146 /* Select the proper CPU mitigations before patching alternatives: */
147 spectre_v1_select_mitigation();
148 retbleed_select_mitigation();
150 * spectre_v2_select_mitigation() relies on the state set by
151 * retbleed_select_mitigation(); specifically the STIBP selection is
154 spectre_v2_select_mitigation();
155 ssb_select_mitigation();
156 l1tf_select_mitigation();
157 md_clear_select_mitigation();
158 srbds_select_mitigation();
159 l1d_flush_select_mitigation();
165 * Check whether we are able to run this kernel safely on SMP.
167 * - i386 is no longer supported.
168 * - In order to run on anything without a TSC, we need to be
169 * compiled for a i486.
171 if (boot_cpu_data
.x86
< 4)
172 panic("Kernel requires i486+ for 'invlpg' and other features");
174 init_utsname()->machine
[1] =
175 '0' + (boot_cpu_data
.x86
> 6 ? 6 : boot_cpu_data
.x86
);
176 alternative_instructions();
178 fpu__init_check_bugs();
179 #else /* CONFIG_X86_64 */
180 alternative_instructions();
183 * Make sure the first 2MB area is not mapped by huge pages
184 * There are typically fixed size MTRRs in there and overlapping
185 * MTRRs into large pages causes slow downs.
187 * Right now we don't do that with gbpages because there seems
188 * very little benefit for that case.
191 set_memory_4k((unsigned long)__va(0), 1);
196 x86_virt_spec_ctrl(u64 guest_spec_ctrl
, u64 guest_virt_spec_ctrl
, bool setguest
)
198 u64 msrval
, guestval
, hostval
= x86_spec_ctrl_base
;
199 struct thread_info
*ti
= current_thread_info();
201 /* Is MSR_SPEC_CTRL implemented ? */
202 if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL
)) {
204 * Restrict guest_spec_ctrl to supported values. Clear the
205 * modifiable bits in the host base value and or the
206 * modifiable bits from the guest value.
208 guestval
= hostval
& ~x86_spec_ctrl_mask
;
209 guestval
|= guest_spec_ctrl
& x86_spec_ctrl_mask
;
211 /* SSBD controlled in MSR_SPEC_CTRL */
212 if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD
) ||
213 static_cpu_has(X86_FEATURE_AMD_SSBD
))
214 hostval
|= ssbd_tif_to_spec_ctrl(ti
->flags
);
216 /* Conditional STIBP enabled? */
217 if (static_branch_unlikely(&switch_to_cond_stibp
))
218 hostval
|= stibp_tif_to_spec_ctrl(ti
->flags
);
220 if (hostval
!= guestval
) {
221 msrval
= setguest
? guestval
: hostval
;
222 wrmsrl(MSR_IA32_SPEC_CTRL
, msrval
);
227 * If SSBD is not handled in MSR_SPEC_CTRL on AMD, update
228 * MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported.
230 if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD
) &&
231 !static_cpu_has(X86_FEATURE_VIRT_SSBD
))
235 * If the host has SSBD mitigation enabled, force it in the host's
236 * virtual MSR value. If its not permanently enabled, evaluate
237 * current's TIF_SSBD thread flag.
239 if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE
))
240 hostval
= SPEC_CTRL_SSBD
;
242 hostval
= ssbd_tif_to_spec_ctrl(ti
->flags
);
244 /* Sanitize the guest value */
245 guestval
= guest_virt_spec_ctrl
& SPEC_CTRL_SSBD
;
247 if (hostval
!= guestval
) {
250 tif
= setguest
? ssbd_spec_ctrl_to_tif(guestval
) :
251 ssbd_spec_ctrl_to_tif(hostval
);
253 speculation_ctrl_update(tif
);
256 EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl
);
258 static void x86_amd_ssb_disable(void)
260 u64 msrval
= x86_amd_ls_cfg_base
| x86_amd_ls_cfg_ssbd_mask
;
262 if (boot_cpu_has(X86_FEATURE_VIRT_SSBD
))
263 wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL
, SPEC_CTRL_SSBD
);
264 else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD
))
265 wrmsrl(MSR_AMD64_LS_CFG
, msrval
);
269 #define pr_fmt(fmt) "MDS: " fmt
271 /* Default mitigation for MDS-affected CPUs */
272 static enum mds_mitigations mds_mitigation __ro_after_init
= MDS_MITIGATION_FULL
;
273 static bool mds_nosmt __ro_after_init
= false;
275 static const char * const mds_strings
[] = {
276 [MDS_MITIGATION_OFF
] = "Vulnerable",
277 [MDS_MITIGATION_FULL
] = "Mitigation: Clear CPU buffers",
278 [MDS_MITIGATION_VMWERV
] = "Vulnerable: Clear CPU buffers attempted, no microcode",
281 static void __init
mds_select_mitigation(void)
283 if (!boot_cpu_has_bug(X86_BUG_MDS
) || cpu_mitigations_off()) {
284 mds_mitigation
= MDS_MITIGATION_OFF
;
288 if (mds_mitigation
== MDS_MITIGATION_FULL
) {
289 if (!boot_cpu_has(X86_FEATURE_MD_CLEAR
))
290 mds_mitigation
= MDS_MITIGATION_VMWERV
;
292 static_branch_enable(&mds_user_clear
);
294 if (!boot_cpu_has(X86_BUG_MSBDS_ONLY
) &&
295 (mds_nosmt
|| cpu_mitigations_auto_nosmt()))
296 cpu_smt_disable(false);
300 static int __init
mds_cmdline(char *str
)
302 if (!boot_cpu_has_bug(X86_BUG_MDS
))
308 if (!strcmp(str
, "off"))
309 mds_mitigation
= MDS_MITIGATION_OFF
;
310 else if (!strcmp(str
, "full"))
311 mds_mitigation
= MDS_MITIGATION_FULL
;
312 else if (!strcmp(str
, "full,nosmt")) {
313 mds_mitigation
= MDS_MITIGATION_FULL
;
319 early_param("mds", mds_cmdline
);
322 #define pr_fmt(fmt) "TAA: " fmt
324 enum taa_mitigations
{
326 TAA_MITIGATION_UCODE_NEEDED
,
328 TAA_MITIGATION_TSX_DISABLED
,
331 /* Default mitigation for TAA-affected CPUs */
332 static enum taa_mitigations taa_mitigation __ro_after_init
= TAA_MITIGATION_VERW
;
333 static bool taa_nosmt __ro_after_init
;
335 static const char * const taa_strings
[] = {
336 [TAA_MITIGATION_OFF
] = "Vulnerable",
337 [TAA_MITIGATION_UCODE_NEEDED
] = "Vulnerable: Clear CPU buffers attempted, no microcode",
338 [TAA_MITIGATION_VERW
] = "Mitigation: Clear CPU buffers",
339 [TAA_MITIGATION_TSX_DISABLED
] = "Mitigation: TSX disabled",
342 static void __init
taa_select_mitigation(void)
346 if (!boot_cpu_has_bug(X86_BUG_TAA
)) {
347 taa_mitigation
= TAA_MITIGATION_OFF
;
351 /* TSX previously disabled by tsx=off */
352 if (!boot_cpu_has(X86_FEATURE_RTM
)) {
353 taa_mitigation
= TAA_MITIGATION_TSX_DISABLED
;
357 if (cpu_mitigations_off()) {
358 taa_mitigation
= TAA_MITIGATION_OFF
;
363 * TAA mitigation via VERW is turned off if both
364 * tsx_async_abort=off and mds=off are specified.
366 if (taa_mitigation
== TAA_MITIGATION_OFF
&&
367 mds_mitigation
== MDS_MITIGATION_OFF
)
370 if (boot_cpu_has(X86_FEATURE_MD_CLEAR
))
371 taa_mitigation
= TAA_MITIGATION_VERW
;
373 taa_mitigation
= TAA_MITIGATION_UCODE_NEEDED
;
376 * VERW doesn't clear the CPU buffers when MD_CLEAR=1 and MDS_NO=1.
377 * A microcode update fixes this behavior to clear CPU buffers. It also
378 * adds support for MSR_IA32_TSX_CTRL which is enumerated by the
379 * ARCH_CAP_TSX_CTRL_MSR bit.
381 * On MDS_NO=1 CPUs if ARCH_CAP_TSX_CTRL_MSR is not set, microcode
382 * update is required.
384 ia32_cap
= x86_read_arch_cap_msr();
385 if ( (ia32_cap
& ARCH_CAP_MDS_NO
) &&
386 !(ia32_cap
& ARCH_CAP_TSX_CTRL_MSR
))
387 taa_mitigation
= TAA_MITIGATION_UCODE_NEEDED
;
390 * TSX is enabled, select alternate mitigation for TAA which is
391 * the same as MDS. Enable MDS static branch to clear CPU buffers.
393 * For guests that can't determine whether the correct microcode is
394 * present on host, enable the mitigation for UCODE_NEEDED as well.
396 static_branch_enable(&mds_user_clear
);
398 if (taa_nosmt
|| cpu_mitigations_auto_nosmt())
399 cpu_smt_disable(false);
402 static int __init
tsx_async_abort_parse_cmdline(char *str
)
404 if (!boot_cpu_has_bug(X86_BUG_TAA
))
410 if (!strcmp(str
, "off")) {
411 taa_mitigation
= TAA_MITIGATION_OFF
;
412 } else if (!strcmp(str
, "full")) {
413 taa_mitigation
= TAA_MITIGATION_VERW
;
414 } else if (!strcmp(str
, "full,nosmt")) {
415 taa_mitigation
= TAA_MITIGATION_VERW
;
421 early_param("tsx_async_abort", tsx_async_abort_parse_cmdline
);
424 #define pr_fmt(fmt) "MMIO Stale Data: " fmt
426 enum mmio_mitigations
{
428 MMIO_MITIGATION_UCODE_NEEDED
,
429 MMIO_MITIGATION_VERW
,
432 /* Default mitigation for Processor MMIO Stale Data vulnerabilities */
433 static enum mmio_mitigations mmio_mitigation __ro_after_init
= MMIO_MITIGATION_VERW
;
434 static bool mmio_nosmt __ro_after_init
= false;
436 static const char * const mmio_strings
[] = {
437 [MMIO_MITIGATION_OFF
] = "Vulnerable",
438 [MMIO_MITIGATION_UCODE_NEEDED
] = "Vulnerable: Clear CPU buffers attempted, no microcode",
439 [MMIO_MITIGATION_VERW
] = "Mitigation: Clear CPU buffers",
442 static void __init
mmio_select_mitigation(void)
446 if (!boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA
) ||
447 cpu_mitigations_off()) {
448 mmio_mitigation
= MMIO_MITIGATION_OFF
;
452 if (mmio_mitigation
== MMIO_MITIGATION_OFF
)
455 ia32_cap
= x86_read_arch_cap_msr();
458 * Enable CPU buffer clear mitigation for host and VMM, if also affected
459 * by MDS or TAA. Otherwise, enable mitigation for VMM only.
461 if (boot_cpu_has_bug(X86_BUG_MDS
) || (boot_cpu_has_bug(X86_BUG_TAA
) &&
462 boot_cpu_has(X86_FEATURE_RTM
)))
463 static_branch_enable(&mds_user_clear
);
465 static_branch_enable(&mmio_stale_data_clear
);
468 * If Processor-MMIO-Stale-Data bug is present and Fill Buffer data can
469 * be propagated to uncore buffers, clearing the Fill buffers on idle
470 * is required irrespective of SMT state.
472 if (!(ia32_cap
& ARCH_CAP_FBSDP_NO
))
473 static_branch_enable(&mds_idle_clear
);
476 * Check if the system has the right microcode.
478 * CPU Fill buffer clear mitigation is enumerated by either an explicit
479 * FB_CLEAR or by the presence of both MD_CLEAR and L1D_FLUSH on MDS
482 if ((ia32_cap
& ARCH_CAP_FB_CLEAR
) ||
483 (boot_cpu_has(X86_FEATURE_MD_CLEAR
) &&
484 boot_cpu_has(X86_FEATURE_FLUSH_L1D
) &&
485 !(ia32_cap
& ARCH_CAP_MDS_NO
)))
486 mmio_mitigation
= MMIO_MITIGATION_VERW
;
488 mmio_mitigation
= MMIO_MITIGATION_UCODE_NEEDED
;
490 if (mmio_nosmt
|| cpu_mitigations_auto_nosmt())
491 cpu_smt_disable(false);
494 static int __init
mmio_stale_data_parse_cmdline(char *str
)
496 if (!boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA
))
502 if (!strcmp(str
, "off")) {
503 mmio_mitigation
= MMIO_MITIGATION_OFF
;
504 } else if (!strcmp(str
, "full")) {
505 mmio_mitigation
= MMIO_MITIGATION_VERW
;
506 } else if (!strcmp(str
, "full,nosmt")) {
507 mmio_mitigation
= MMIO_MITIGATION_VERW
;
513 early_param("mmio_stale_data", mmio_stale_data_parse_cmdline
);
516 #define pr_fmt(fmt) "" fmt
518 static void __init
md_clear_update_mitigation(void)
520 if (cpu_mitigations_off())
523 if (!static_key_enabled(&mds_user_clear
))
527 * mds_user_clear is now enabled. Update MDS, TAA and MMIO Stale Data
528 * mitigation, if necessary.
530 if (mds_mitigation
== MDS_MITIGATION_OFF
&&
531 boot_cpu_has_bug(X86_BUG_MDS
)) {
532 mds_mitigation
= MDS_MITIGATION_FULL
;
533 mds_select_mitigation();
535 if (taa_mitigation
== TAA_MITIGATION_OFF
&&
536 boot_cpu_has_bug(X86_BUG_TAA
)) {
537 taa_mitigation
= TAA_MITIGATION_VERW
;
538 taa_select_mitigation();
540 if (mmio_mitigation
== MMIO_MITIGATION_OFF
&&
541 boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA
)) {
542 mmio_mitigation
= MMIO_MITIGATION_VERW
;
543 mmio_select_mitigation();
546 if (boot_cpu_has_bug(X86_BUG_MDS
))
547 pr_info("MDS: %s\n", mds_strings
[mds_mitigation
]);
548 if (boot_cpu_has_bug(X86_BUG_TAA
))
549 pr_info("TAA: %s\n", taa_strings
[taa_mitigation
]);
550 if (boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA
))
551 pr_info("MMIO Stale Data: %s\n", mmio_strings
[mmio_mitigation
]);
554 static void __init
md_clear_select_mitigation(void)
556 mds_select_mitigation();
557 taa_select_mitigation();
558 mmio_select_mitigation();
561 * As MDS, TAA and MMIO Stale Data mitigations are inter-related, update
562 * and print their mitigation after MDS, TAA and MMIO Stale Data
563 * mitigation selection is done.
565 md_clear_update_mitigation();
569 #define pr_fmt(fmt) "SRBDS: " fmt
571 enum srbds_mitigations
{
572 SRBDS_MITIGATION_OFF
,
573 SRBDS_MITIGATION_UCODE_NEEDED
,
574 SRBDS_MITIGATION_FULL
,
575 SRBDS_MITIGATION_TSX_OFF
,
576 SRBDS_MITIGATION_HYPERVISOR
,
579 static enum srbds_mitigations srbds_mitigation __ro_after_init
= SRBDS_MITIGATION_FULL
;
581 static const char * const srbds_strings
[] = {
582 [SRBDS_MITIGATION_OFF
] = "Vulnerable",
583 [SRBDS_MITIGATION_UCODE_NEEDED
] = "Vulnerable: No microcode",
584 [SRBDS_MITIGATION_FULL
] = "Mitigation: Microcode",
585 [SRBDS_MITIGATION_TSX_OFF
] = "Mitigation: TSX disabled",
586 [SRBDS_MITIGATION_HYPERVISOR
] = "Unknown: Dependent on hypervisor status",
589 static bool srbds_off
;
591 void update_srbds_msr(void)
595 if (!boot_cpu_has_bug(X86_BUG_SRBDS
))
598 if (boot_cpu_has(X86_FEATURE_HYPERVISOR
))
601 if (srbds_mitigation
== SRBDS_MITIGATION_UCODE_NEEDED
)
604 rdmsrl(MSR_IA32_MCU_OPT_CTRL
, mcu_ctrl
);
606 switch (srbds_mitigation
) {
607 case SRBDS_MITIGATION_OFF
:
608 case SRBDS_MITIGATION_TSX_OFF
:
609 mcu_ctrl
|= RNGDS_MITG_DIS
;
611 case SRBDS_MITIGATION_FULL
:
612 mcu_ctrl
&= ~RNGDS_MITG_DIS
;
618 wrmsrl(MSR_IA32_MCU_OPT_CTRL
, mcu_ctrl
);
621 static void __init
srbds_select_mitigation(void)
625 if (!boot_cpu_has_bug(X86_BUG_SRBDS
))
629 * Check to see if this is one of the MDS_NO systems supporting TSX that
630 * are only exposed to SRBDS when TSX is enabled or when CPU is affected
631 * by Processor MMIO Stale Data vulnerability.
633 ia32_cap
= x86_read_arch_cap_msr();
634 if ((ia32_cap
& ARCH_CAP_MDS_NO
) && !boot_cpu_has(X86_FEATURE_RTM
) &&
635 !boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA
))
636 srbds_mitigation
= SRBDS_MITIGATION_TSX_OFF
;
637 else if (boot_cpu_has(X86_FEATURE_HYPERVISOR
))
638 srbds_mitigation
= SRBDS_MITIGATION_HYPERVISOR
;
639 else if (!boot_cpu_has(X86_FEATURE_SRBDS_CTRL
))
640 srbds_mitigation
= SRBDS_MITIGATION_UCODE_NEEDED
;
641 else if (cpu_mitigations_off() || srbds_off
)
642 srbds_mitigation
= SRBDS_MITIGATION_OFF
;
645 pr_info("%s\n", srbds_strings
[srbds_mitigation
]);
648 static int __init
srbds_parse_cmdline(char *str
)
653 if (!boot_cpu_has_bug(X86_BUG_SRBDS
))
656 srbds_off
= !strcmp(str
, "off");
659 early_param("srbds", srbds_parse_cmdline
);
662 #define pr_fmt(fmt) "L1D Flush : " fmt
664 enum l1d_flush_mitigations
{
669 static enum l1d_flush_mitigations l1d_flush_mitigation __initdata
= L1D_FLUSH_OFF
;
671 static void __init
l1d_flush_select_mitigation(void)
673 if (!l1d_flush_mitigation
|| !boot_cpu_has(X86_FEATURE_FLUSH_L1D
))
676 static_branch_enable(&switch_mm_cond_l1d_flush
);
677 pr_info("Conditional flush on switch_mm() enabled\n");
680 static int __init
l1d_flush_parse_cmdline(char *str
)
682 if (!strcmp(str
, "on"))
683 l1d_flush_mitigation
= L1D_FLUSH_ON
;
687 early_param("l1d_flush", l1d_flush_parse_cmdline
);
690 #define pr_fmt(fmt) "Spectre V1 : " fmt
692 enum spectre_v1_mitigation
{
693 SPECTRE_V1_MITIGATION_NONE
,
694 SPECTRE_V1_MITIGATION_AUTO
,
697 static enum spectre_v1_mitigation spectre_v1_mitigation __ro_after_init
=
698 SPECTRE_V1_MITIGATION_AUTO
;
700 static const char * const spectre_v1_strings
[] = {
701 [SPECTRE_V1_MITIGATION_NONE
] = "Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers",
702 [SPECTRE_V1_MITIGATION_AUTO
] = "Mitigation: usercopy/swapgs barriers and __user pointer sanitization",
706 * Does SMAP provide full mitigation against speculative kernel access to
709 static bool smap_works_speculatively(void)
711 if (!boot_cpu_has(X86_FEATURE_SMAP
))
715 * On CPUs which are vulnerable to Meltdown, SMAP does not
716 * prevent speculative access to user data in the L1 cache.
717 * Consider SMAP to be non-functional as a mitigation on these
720 if (boot_cpu_has(X86_BUG_CPU_MELTDOWN
))
726 static void __init
spectre_v1_select_mitigation(void)
728 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1
) || cpu_mitigations_off()) {
729 spectre_v1_mitigation
= SPECTRE_V1_MITIGATION_NONE
;
733 if (spectre_v1_mitigation
== SPECTRE_V1_MITIGATION_AUTO
) {
735 * With Spectre v1, a user can speculatively control either
736 * path of a conditional swapgs with a user-controlled GS
737 * value. The mitigation is to add lfences to both code paths.
739 * If FSGSBASE is enabled, the user can put a kernel address in
740 * GS, in which case SMAP provides no protection.
742 * If FSGSBASE is disabled, the user can only put a user space
743 * address in GS. That makes an attack harder, but still
744 * possible if there's no SMAP protection.
746 if (boot_cpu_has(X86_FEATURE_FSGSBASE
) ||
747 !smap_works_speculatively()) {
749 * Mitigation can be provided from SWAPGS itself or
750 * PTI as the CR3 write in the Meltdown mitigation
753 * If neither is there, mitigate with an LFENCE to
754 * stop speculation through swapgs.
756 if (boot_cpu_has_bug(X86_BUG_SWAPGS
) &&
757 !boot_cpu_has(X86_FEATURE_PTI
))
758 setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_USER
);
761 * Enable lfences in the kernel entry (non-swapgs)
762 * paths, to prevent user entry from speculatively
765 setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_KERNEL
);
769 pr_info("%s\n", spectre_v1_strings
[spectre_v1_mitigation
]);
772 static int __init
nospectre_v1_cmdline(char *str
)
774 spectre_v1_mitigation
= SPECTRE_V1_MITIGATION_NONE
;
777 early_param("nospectre_v1", nospectre_v1_cmdline
);
780 #define pr_fmt(fmt) "RETBleed: " fmt
782 enum retbleed_mitigation
{
783 RETBLEED_MITIGATION_NONE
,
784 RETBLEED_MITIGATION_UNRET
,
787 enum retbleed_mitigation_cmd
{
793 const char * const retbleed_strings
[] = {
794 [RETBLEED_MITIGATION_NONE
] = "Vulnerable",
795 [RETBLEED_MITIGATION_UNRET
] = "Mitigation: untrained return thunk",
798 static enum retbleed_mitigation retbleed_mitigation __ro_after_init
=
799 RETBLEED_MITIGATION_NONE
;
800 static enum retbleed_mitigation_cmd retbleed_cmd __ro_after_init
=
803 static int __ro_after_init retbleed_nosmt
= false;
805 static int __init
retbleed_parse_cmdline(char *str
)
811 char *next
= strchr(str
, ',');
817 if (!strcmp(str
, "off")) {
818 retbleed_cmd
= RETBLEED_CMD_OFF
;
819 } else if (!strcmp(str
, "auto")) {
820 retbleed_cmd
= RETBLEED_CMD_AUTO
;
821 } else if (!strcmp(str
, "unret")) {
822 retbleed_cmd
= RETBLEED_CMD_UNRET
;
823 } else if (!strcmp(str
, "nosmt")) {
824 retbleed_nosmt
= true;
826 pr_err("Ignoring unknown retbleed option (%s).", str
);
834 early_param("retbleed", retbleed_parse_cmdline
);
836 #define RETBLEED_UNTRAIN_MSG "WARNING: BTB untrained return thunk mitigation is only effective on AMD/Hygon!\n"
837 #define RETBLEED_COMPILER_MSG "WARNING: kernel not compiled with RETPOLINE or -mfunction-return capable compiler!\n"
839 static void __init
retbleed_select_mitigation(void)
841 if (!boot_cpu_has_bug(X86_BUG_RETBLEED
) || cpu_mitigations_off())
844 switch (retbleed_cmd
) {
845 case RETBLEED_CMD_OFF
:
848 case RETBLEED_CMD_UNRET
:
849 retbleed_mitigation
= RETBLEED_MITIGATION_UNRET
;
852 case RETBLEED_CMD_AUTO
:
854 if (!boot_cpu_has_bug(X86_BUG_RETBLEED
))
857 if (boot_cpu_data
.x86_vendor
== X86_VENDOR_AMD
||
858 boot_cpu_data
.x86_vendor
== X86_VENDOR_HYGON
)
859 retbleed_mitigation
= RETBLEED_MITIGATION_UNRET
;
863 switch (retbleed_mitigation
) {
864 case RETBLEED_MITIGATION_UNRET
:
866 if (!IS_ENABLED(CONFIG_RETPOLINE
) ||
867 !IS_ENABLED(CONFIG_CC_HAS_RETURN_THUNK
)) {
868 pr_err(RETBLEED_COMPILER_MSG
);
869 retbleed_mitigation
= RETBLEED_MITIGATION_NONE
;
873 setup_force_cpu_cap(X86_FEATURE_RETHUNK
);
874 setup_force_cpu_cap(X86_FEATURE_UNRET
);
876 if (!boot_cpu_has(X86_FEATURE_STIBP
) &&
877 (retbleed_nosmt
|| cpu_mitigations_auto_nosmt()))
878 cpu_smt_disable(false);
880 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_AMD
&&
881 boot_cpu_data
.x86_vendor
!= X86_VENDOR_HYGON
)
882 pr_err(RETBLEED_UNTRAIN_MSG
);
889 pr_info("%s\n", retbleed_strings
[retbleed_mitigation
]);
893 #define pr_fmt(fmt) "Spectre V2 : " fmt
895 static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init
=
898 static enum spectre_v2_user_mitigation spectre_v2_user_stibp __ro_after_init
=
899 SPECTRE_V2_USER_NONE
;
900 static enum spectre_v2_user_mitigation spectre_v2_user_ibpb __ro_after_init
=
901 SPECTRE_V2_USER_NONE
;
903 #ifdef CONFIG_RETPOLINE
904 static bool spectre_v2_bad_module
;
906 bool retpoline_module_ok(bool has_retpoline
)
908 if (spectre_v2_enabled
== SPECTRE_V2_NONE
|| has_retpoline
)
911 pr_err("System may be vulnerable to spectre v2\n");
912 spectre_v2_bad_module
= true;
916 static inline const char *spectre_v2_module_string(void)
918 return spectre_v2_bad_module
? " - vulnerable module loaded" : "";
921 static inline const char *spectre_v2_module_string(void) { return ""; }
924 #define SPECTRE_V2_LFENCE_MSG "WARNING: LFENCE mitigation is not recommended for this CPU, data leaks possible!\n"
925 #define SPECTRE_V2_EIBRS_EBPF_MSG "WARNING: Unprivileged eBPF is enabled with eIBRS on, data leaks possible via Spectre v2 BHB attacks!\n"
926 #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"
928 #ifdef CONFIG_BPF_SYSCALL
929 void unpriv_ebpf_notify(int new_state
)
934 /* Unprivileged eBPF is enabled */
936 switch (spectre_v2_enabled
) {
937 case SPECTRE_V2_EIBRS
:
938 pr_err(SPECTRE_V2_EIBRS_EBPF_MSG
);
940 case SPECTRE_V2_EIBRS_LFENCE
:
941 if (sched_smt_active())
942 pr_err(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG
);
950 static inline bool match_option(const char *arg
, int arglen
, const char *opt
)
952 int len
= strlen(opt
);
954 return len
== arglen
&& !strncmp(arg
, opt
, len
);
957 /* The kernel command line selection for spectre v2 */
958 enum spectre_v2_mitigation_cmd
{
961 SPECTRE_V2_CMD_FORCE
,
962 SPECTRE_V2_CMD_RETPOLINE
,
963 SPECTRE_V2_CMD_RETPOLINE_GENERIC
,
964 SPECTRE_V2_CMD_RETPOLINE_LFENCE
,
965 SPECTRE_V2_CMD_EIBRS
,
966 SPECTRE_V2_CMD_EIBRS_RETPOLINE
,
967 SPECTRE_V2_CMD_EIBRS_LFENCE
,
970 enum spectre_v2_user_cmd
{
971 SPECTRE_V2_USER_CMD_NONE
,
972 SPECTRE_V2_USER_CMD_AUTO
,
973 SPECTRE_V2_USER_CMD_FORCE
,
974 SPECTRE_V2_USER_CMD_PRCTL
,
975 SPECTRE_V2_USER_CMD_PRCTL_IBPB
,
976 SPECTRE_V2_USER_CMD_SECCOMP
,
977 SPECTRE_V2_USER_CMD_SECCOMP_IBPB
,
980 static const char * const spectre_v2_user_strings
[] = {
981 [SPECTRE_V2_USER_NONE
] = "User space: Vulnerable",
982 [SPECTRE_V2_USER_STRICT
] = "User space: Mitigation: STIBP protection",
983 [SPECTRE_V2_USER_STRICT_PREFERRED
] = "User space: Mitigation: STIBP always-on protection",
984 [SPECTRE_V2_USER_PRCTL
] = "User space: Mitigation: STIBP via prctl",
985 [SPECTRE_V2_USER_SECCOMP
] = "User space: Mitigation: STIBP via seccomp and prctl",
988 static const struct {
990 enum spectre_v2_user_cmd cmd
;
992 } v2_user_options
[] __initconst
= {
993 { "auto", SPECTRE_V2_USER_CMD_AUTO
, false },
994 { "off", SPECTRE_V2_USER_CMD_NONE
, false },
995 { "on", SPECTRE_V2_USER_CMD_FORCE
, true },
996 { "prctl", SPECTRE_V2_USER_CMD_PRCTL
, false },
997 { "prctl,ibpb", SPECTRE_V2_USER_CMD_PRCTL_IBPB
, false },
998 { "seccomp", SPECTRE_V2_USER_CMD_SECCOMP
, false },
999 { "seccomp,ibpb", SPECTRE_V2_USER_CMD_SECCOMP_IBPB
, false },
1002 static void __init
spec_v2_user_print_cond(const char *reason
, bool secure
)
1004 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2
) != secure
)
1005 pr_info("spectre_v2_user=%s forced on command line.\n", reason
);
1008 static enum spectre_v2_user_cmd __init
1009 spectre_v2_parse_user_cmdline(enum spectre_v2_mitigation_cmd v2_cmd
)
1015 case SPECTRE_V2_CMD_NONE
:
1016 return SPECTRE_V2_USER_CMD_NONE
;
1017 case SPECTRE_V2_CMD_FORCE
:
1018 return SPECTRE_V2_USER_CMD_FORCE
;
1023 ret
= cmdline_find_option(boot_command_line
, "spectre_v2_user",
1026 return SPECTRE_V2_USER_CMD_AUTO
;
1028 for (i
= 0; i
< ARRAY_SIZE(v2_user_options
); i
++) {
1029 if (match_option(arg
, ret
, v2_user_options
[i
].option
)) {
1030 spec_v2_user_print_cond(v2_user_options
[i
].option
,
1031 v2_user_options
[i
].secure
);
1032 return v2_user_options
[i
].cmd
;
1036 pr_err("Unknown user space protection option (%s). Switching to AUTO select\n", arg
);
1037 return SPECTRE_V2_USER_CMD_AUTO
;
1040 static inline bool spectre_v2_in_eibrs_mode(enum spectre_v2_mitigation mode
)
1042 return (mode
== SPECTRE_V2_EIBRS
||
1043 mode
== SPECTRE_V2_EIBRS_RETPOLINE
||
1044 mode
== SPECTRE_V2_EIBRS_LFENCE
);
1048 spectre_v2_user_select_mitigation(enum spectre_v2_mitigation_cmd v2_cmd
)
1050 enum spectre_v2_user_mitigation mode
= SPECTRE_V2_USER_NONE
;
1051 bool smt_possible
= IS_ENABLED(CONFIG_SMP
);
1052 enum spectre_v2_user_cmd cmd
;
1054 if (!boot_cpu_has(X86_FEATURE_IBPB
) && !boot_cpu_has(X86_FEATURE_STIBP
))
1057 if (cpu_smt_control
== CPU_SMT_FORCE_DISABLED
||
1058 cpu_smt_control
== CPU_SMT_NOT_SUPPORTED
)
1059 smt_possible
= false;
1061 cmd
= spectre_v2_parse_user_cmdline(v2_cmd
);
1063 case SPECTRE_V2_USER_CMD_NONE
:
1065 case SPECTRE_V2_USER_CMD_FORCE
:
1066 mode
= SPECTRE_V2_USER_STRICT
;
1068 case SPECTRE_V2_USER_CMD_PRCTL
:
1069 case SPECTRE_V2_USER_CMD_PRCTL_IBPB
:
1070 mode
= SPECTRE_V2_USER_PRCTL
;
1072 case SPECTRE_V2_USER_CMD_AUTO
:
1073 case SPECTRE_V2_USER_CMD_SECCOMP
:
1074 case SPECTRE_V2_USER_CMD_SECCOMP_IBPB
:
1075 if (IS_ENABLED(CONFIG_SECCOMP
))
1076 mode
= SPECTRE_V2_USER_SECCOMP
;
1078 mode
= SPECTRE_V2_USER_PRCTL
;
1082 /* Initialize Indirect Branch Prediction Barrier */
1083 if (boot_cpu_has(X86_FEATURE_IBPB
)) {
1084 setup_force_cpu_cap(X86_FEATURE_USE_IBPB
);
1086 spectre_v2_user_ibpb
= mode
;
1088 case SPECTRE_V2_USER_CMD_FORCE
:
1089 case SPECTRE_V2_USER_CMD_PRCTL_IBPB
:
1090 case SPECTRE_V2_USER_CMD_SECCOMP_IBPB
:
1091 static_branch_enable(&switch_mm_always_ibpb
);
1092 spectre_v2_user_ibpb
= SPECTRE_V2_USER_STRICT
;
1094 case SPECTRE_V2_USER_CMD_PRCTL
:
1095 case SPECTRE_V2_USER_CMD_AUTO
:
1096 case SPECTRE_V2_USER_CMD_SECCOMP
:
1097 static_branch_enable(&switch_mm_cond_ibpb
);
1103 pr_info("mitigation: Enabling %s Indirect Branch Prediction Barrier\n",
1104 static_key_enabled(&switch_mm_always_ibpb
) ?
1105 "always-on" : "conditional");
1109 * If no STIBP, enhanced IBRS is enabled or SMT impossible, STIBP is not
1112 if (!boot_cpu_has(X86_FEATURE_STIBP
) ||
1114 spectre_v2_in_eibrs_mode(spectre_v2_enabled
))
1118 * At this point, an STIBP mode other than "off" has been set.
1119 * If STIBP support is not being forced, check if STIBP always-on
1122 if (mode
!= SPECTRE_V2_USER_STRICT
&&
1123 boot_cpu_has(X86_FEATURE_AMD_STIBP_ALWAYS_ON
))
1124 mode
= SPECTRE_V2_USER_STRICT_PREFERRED
;
1126 if (retbleed_mitigation
== RETBLEED_MITIGATION_UNRET
) {
1127 if (mode
!= SPECTRE_V2_USER_STRICT
&&
1128 mode
!= SPECTRE_V2_USER_STRICT_PREFERRED
)
1129 pr_info("Selecting STIBP always-on mode to complement retbleed mitigation'\n");
1130 mode
= SPECTRE_V2_USER_STRICT_PREFERRED
;
1133 spectre_v2_user_stibp
= mode
;
1136 pr_info("%s\n", spectre_v2_user_strings
[mode
]);
1139 static const char * const spectre_v2_strings
[] = {
1140 [SPECTRE_V2_NONE
] = "Vulnerable",
1141 [SPECTRE_V2_RETPOLINE
] = "Mitigation: Retpolines",
1142 [SPECTRE_V2_LFENCE
] = "Mitigation: LFENCE",
1143 [SPECTRE_V2_EIBRS
] = "Mitigation: Enhanced IBRS",
1144 [SPECTRE_V2_EIBRS_LFENCE
] = "Mitigation: Enhanced IBRS + LFENCE",
1145 [SPECTRE_V2_EIBRS_RETPOLINE
] = "Mitigation: Enhanced IBRS + Retpolines",
1148 static const struct {
1150 enum spectre_v2_mitigation_cmd cmd
;
1152 } mitigation_options
[] __initconst
= {
1153 { "off", SPECTRE_V2_CMD_NONE
, false },
1154 { "on", SPECTRE_V2_CMD_FORCE
, true },
1155 { "retpoline", SPECTRE_V2_CMD_RETPOLINE
, false },
1156 { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_LFENCE
, false },
1157 { "retpoline,lfence", SPECTRE_V2_CMD_RETPOLINE_LFENCE
, false },
1158 { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC
, false },
1159 { "eibrs", SPECTRE_V2_CMD_EIBRS
, false },
1160 { "eibrs,lfence", SPECTRE_V2_CMD_EIBRS_LFENCE
, false },
1161 { "eibrs,retpoline", SPECTRE_V2_CMD_EIBRS_RETPOLINE
, false },
1162 { "auto", SPECTRE_V2_CMD_AUTO
, false },
1165 static void __init
spec_v2_print_cond(const char *reason
, bool secure
)
1167 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2
) != secure
)
1168 pr_info("%s selected on command line.\n", reason
);
1171 static enum spectre_v2_mitigation_cmd __init
spectre_v2_parse_cmdline(void)
1173 enum spectre_v2_mitigation_cmd cmd
= SPECTRE_V2_CMD_AUTO
;
1177 if (cmdline_find_option_bool(boot_command_line
, "nospectre_v2") ||
1178 cpu_mitigations_off())
1179 return SPECTRE_V2_CMD_NONE
;
1181 ret
= cmdline_find_option(boot_command_line
, "spectre_v2", arg
, sizeof(arg
));
1183 return SPECTRE_V2_CMD_AUTO
;
1185 for (i
= 0; i
< ARRAY_SIZE(mitigation_options
); i
++) {
1186 if (!match_option(arg
, ret
, mitigation_options
[i
].option
))
1188 cmd
= mitigation_options
[i
].cmd
;
1192 if (i
>= ARRAY_SIZE(mitigation_options
)) {
1193 pr_err("unknown option (%s). Switching to AUTO select\n", arg
);
1194 return SPECTRE_V2_CMD_AUTO
;
1197 if ((cmd
== SPECTRE_V2_CMD_RETPOLINE
||
1198 cmd
== SPECTRE_V2_CMD_RETPOLINE_LFENCE
||
1199 cmd
== SPECTRE_V2_CMD_RETPOLINE_GENERIC
||
1200 cmd
== SPECTRE_V2_CMD_EIBRS_LFENCE
||
1201 cmd
== SPECTRE_V2_CMD_EIBRS_RETPOLINE
) &&
1202 !IS_ENABLED(CONFIG_RETPOLINE
)) {
1203 pr_err("%s selected but not compiled in. Switching to AUTO select\n",
1204 mitigation_options
[i
].option
);
1205 return SPECTRE_V2_CMD_AUTO
;
1208 if ((cmd
== SPECTRE_V2_CMD_EIBRS
||
1209 cmd
== SPECTRE_V2_CMD_EIBRS_LFENCE
||
1210 cmd
== SPECTRE_V2_CMD_EIBRS_RETPOLINE
) &&
1211 !boot_cpu_has(X86_FEATURE_IBRS_ENHANCED
)) {
1212 pr_err("%s selected but CPU doesn't have eIBRS. Switching to AUTO select\n",
1213 mitigation_options
[i
].option
);
1214 return SPECTRE_V2_CMD_AUTO
;
1217 if ((cmd
== SPECTRE_V2_CMD_RETPOLINE_LFENCE
||
1218 cmd
== SPECTRE_V2_CMD_EIBRS_LFENCE
) &&
1219 !boot_cpu_has(X86_FEATURE_LFENCE_RDTSC
)) {
1220 pr_err("%s selected, but CPU doesn't have a serializing LFENCE. Switching to AUTO select\n",
1221 mitigation_options
[i
].option
);
1222 return SPECTRE_V2_CMD_AUTO
;
1225 spec_v2_print_cond(mitigation_options
[i
].option
,
1226 mitigation_options
[i
].secure
);
1230 static enum spectre_v2_mitigation __init
spectre_v2_select_retpoline(void)
1232 if (!IS_ENABLED(CONFIG_RETPOLINE
)) {
1233 pr_err("Kernel not compiled with retpoline; no mitigation available!");
1234 return SPECTRE_V2_NONE
;
1237 return SPECTRE_V2_RETPOLINE
;
1240 static void __init
spectre_v2_select_mitigation(void)
1242 enum spectre_v2_mitigation_cmd cmd
= spectre_v2_parse_cmdline();
1243 enum spectre_v2_mitigation mode
= SPECTRE_V2_NONE
;
1246 * If the CPU is not affected and the command line mode is NONE or AUTO
1247 * then nothing to do.
1249 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2
) &&
1250 (cmd
== SPECTRE_V2_CMD_NONE
|| cmd
== SPECTRE_V2_CMD_AUTO
))
1254 case SPECTRE_V2_CMD_NONE
:
1257 case SPECTRE_V2_CMD_FORCE
:
1258 case SPECTRE_V2_CMD_AUTO
:
1259 if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED
)) {
1260 mode
= SPECTRE_V2_EIBRS
;
1264 mode
= spectre_v2_select_retpoline();
1267 case SPECTRE_V2_CMD_RETPOLINE_LFENCE
:
1268 pr_err(SPECTRE_V2_LFENCE_MSG
);
1269 mode
= SPECTRE_V2_LFENCE
;
1272 case SPECTRE_V2_CMD_RETPOLINE_GENERIC
:
1273 mode
= SPECTRE_V2_RETPOLINE
;
1276 case SPECTRE_V2_CMD_RETPOLINE
:
1277 mode
= spectre_v2_select_retpoline();
1280 case SPECTRE_V2_CMD_EIBRS
:
1281 mode
= SPECTRE_V2_EIBRS
;
1284 case SPECTRE_V2_CMD_EIBRS_LFENCE
:
1285 mode
= SPECTRE_V2_EIBRS_LFENCE
;
1288 case SPECTRE_V2_CMD_EIBRS_RETPOLINE
:
1289 mode
= SPECTRE_V2_EIBRS_RETPOLINE
;
1293 if (mode
== SPECTRE_V2_EIBRS
&& unprivileged_ebpf_enabled())
1294 pr_err(SPECTRE_V2_EIBRS_EBPF_MSG
);
1296 if (spectre_v2_in_eibrs_mode(mode
)) {
1297 /* Force it so VMEXIT will restore correctly */
1298 x86_spec_ctrl_base
|= SPEC_CTRL_IBRS
;
1299 write_spec_ctrl_current(x86_spec_ctrl_base
, true);
1303 case SPECTRE_V2_NONE
:
1304 case SPECTRE_V2_EIBRS
:
1307 case SPECTRE_V2_LFENCE
:
1308 case SPECTRE_V2_EIBRS_LFENCE
:
1309 setup_force_cpu_cap(X86_FEATURE_RETPOLINE_LFENCE
);
1312 case SPECTRE_V2_RETPOLINE
:
1313 case SPECTRE_V2_EIBRS_RETPOLINE
:
1314 setup_force_cpu_cap(X86_FEATURE_RETPOLINE
);
1318 spectre_v2_enabled
= mode
;
1319 pr_info("%s\n", spectre_v2_strings
[mode
]);
1322 * If spectre v2 protection has been enabled, unconditionally fill
1323 * RSB during a context switch; this protects against two independent
1326 * - RSB underflow (and switch to BTB) on Skylake+
1327 * - SpectreRSB variant of spectre v2 on X86_BUG_SPECTRE_V2 CPUs
1329 setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW
);
1330 pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n");
1333 * Retpoline means the kernel is safe because it has no indirect
1334 * branches. Enhanced IBRS protects firmware too, so, enable restricted
1335 * speculation around firmware calls only when Enhanced IBRS isn't
1338 * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because
1339 * the user might select retpoline on the kernel command line and if
1340 * the CPU supports Enhanced IBRS, kernel might un-intentionally not
1341 * enable IBRS around firmware calls.
1343 if (boot_cpu_has(X86_FEATURE_IBRS
) && !spectre_v2_in_eibrs_mode(mode
)) {
1344 setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW
);
1345 pr_info("Enabling Restricted Speculation for firmware calls\n");
1348 /* Set up IBPB and STIBP depending on the general spectre V2 command */
1349 spectre_v2_user_select_mitigation(cmd
);
1352 static void update_stibp_msr(void * __unused
)
1354 write_spec_ctrl_current(x86_spec_ctrl_base
, true);
1357 /* Update x86_spec_ctrl_base in case SMT state changed. */
1358 static void update_stibp_strict(void)
1360 u64 mask
= x86_spec_ctrl_base
& ~SPEC_CTRL_STIBP
;
1362 if (sched_smt_active())
1363 mask
|= SPEC_CTRL_STIBP
;
1365 if (mask
== x86_spec_ctrl_base
)
1368 pr_info("Update user space SMT mitigation: STIBP %s\n",
1369 mask
& SPEC_CTRL_STIBP
? "always-on" : "off");
1370 x86_spec_ctrl_base
= mask
;
1371 on_each_cpu(update_stibp_msr
, NULL
, 1);
1374 /* Update the static key controlling the evaluation of TIF_SPEC_IB */
1375 static void update_indir_branch_cond(void)
1377 if (sched_smt_active())
1378 static_branch_enable(&switch_to_cond_stibp
);
1380 static_branch_disable(&switch_to_cond_stibp
);
1384 #define pr_fmt(fmt) fmt
1386 /* Update the static key controlling the MDS CPU buffer clear in idle */
1387 static void update_mds_branch_idle(void)
1389 u64 ia32_cap
= x86_read_arch_cap_msr();
1392 * Enable the idle clearing if SMT is active on CPUs which are
1393 * affected only by MSBDS and not any other MDS variant.
1395 * The other variants cannot be mitigated when SMT is enabled, so
1396 * clearing the buffers on idle just to prevent the Store Buffer
1397 * repartitioning leak would be a window dressing exercise.
1399 if (!boot_cpu_has_bug(X86_BUG_MSBDS_ONLY
))
1402 if (sched_smt_active()) {
1403 static_branch_enable(&mds_idle_clear
);
1404 } else if (mmio_mitigation
== MMIO_MITIGATION_OFF
||
1405 (ia32_cap
& ARCH_CAP_FBSDP_NO
)) {
1406 static_branch_disable(&mds_idle_clear
);
1410 #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"
1411 #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"
1412 #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"
1414 void cpu_bugs_smt_update(void)
1416 mutex_lock(&spec_ctrl_mutex
);
1418 if (sched_smt_active() && unprivileged_ebpf_enabled() &&
1419 spectre_v2_enabled
== SPECTRE_V2_EIBRS_LFENCE
)
1420 pr_warn_once(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG
);
1422 switch (spectre_v2_user_stibp
) {
1423 case SPECTRE_V2_USER_NONE
:
1425 case SPECTRE_V2_USER_STRICT
:
1426 case SPECTRE_V2_USER_STRICT_PREFERRED
:
1427 update_stibp_strict();
1429 case SPECTRE_V2_USER_PRCTL
:
1430 case SPECTRE_V2_USER_SECCOMP
:
1431 update_indir_branch_cond();
1435 switch (mds_mitigation
) {
1436 case MDS_MITIGATION_FULL
:
1437 case MDS_MITIGATION_VMWERV
:
1438 if (sched_smt_active() && !boot_cpu_has(X86_BUG_MSBDS_ONLY
))
1439 pr_warn_once(MDS_MSG_SMT
);
1440 update_mds_branch_idle();
1442 case MDS_MITIGATION_OFF
:
1446 switch (taa_mitigation
) {
1447 case TAA_MITIGATION_VERW
:
1448 case TAA_MITIGATION_UCODE_NEEDED
:
1449 if (sched_smt_active())
1450 pr_warn_once(TAA_MSG_SMT
);
1452 case TAA_MITIGATION_TSX_DISABLED
:
1453 case TAA_MITIGATION_OFF
:
1457 switch (mmio_mitigation
) {
1458 case MMIO_MITIGATION_VERW
:
1459 case MMIO_MITIGATION_UCODE_NEEDED
:
1460 if (sched_smt_active())
1461 pr_warn_once(MMIO_MSG_SMT
);
1463 case MMIO_MITIGATION_OFF
:
1467 mutex_unlock(&spec_ctrl_mutex
);
1471 #define pr_fmt(fmt) "Speculative Store Bypass: " fmt
1473 static enum ssb_mitigation ssb_mode __ro_after_init
= SPEC_STORE_BYPASS_NONE
;
1475 /* The kernel command line selection */
1476 enum ssb_mitigation_cmd
{
1477 SPEC_STORE_BYPASS_CMD_NONE
,
1478 SPEC_STORE_BYPASS_CMD_AUTO
,
1479 SPEC_STORE_BYPASS_CMD_ON
,
1480 SPEC_STORE_BYPASS_CMD_PRCTL
,
1481 SPEC_STORE_BYPASS_CMD_SECCOMP
,
1484 static const char * const ssb_strings
[] = {
1485 [SPEC_STORE_BYPASS_NONE
] = "Vulnerable",
1486 [SPEC_STORE_BYPASS_DISABLE
] = "Mitigation: Speculative Store Bypass disabled",
1487 [SPEC_STORE_BYPASS_PRCTL
] = "Mitigation: Speculative Store Bypass disabled via prctl",
1488 [SPEC_STORE_BYPASS_SECCOMP
] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
1491 static const struct {
1493 enum ssb_mitigation_cmd cmd
;
1494 } ssb_mitigation_options
[] __initconst
= {
1495 { "auto", SPEC_STORE_BYPASS_CMD_AUTO
}, /* Platform decides */
1496 { "on", SPEC_STORE_BYPASS_CMD_ON
}, /* Disable Speculative Store Bypass */
1497 { "off", SPEC_STORE_BYPASS_CMD_NONE
}, /* Don't touch Speculative Store Bypass */
1498 { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL
}, /* Disable Speculative Store Bypass via prctl */
1499 { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP
}, /* Disable Speculative Store Bypass via prctl and seccomp */
1502 static enum ssb_mitigation_cmd __init
ssb_parse_cmdline(void)
1504 enum ssb_mitigation_cmd cmd
= SPEC_STORE_BYPASS_CMD_AUTO
;
1508 if (cmdline_find_option_bool(boot_command_line
, "nospec_store_bypass_disable") ||
1509 cpu_mitigations_off()) {
1510 return SPEC_STORE_BYPASS_CMD_NONE
;
1512 ret
= cmdline_find_option(boot_command_line
, "spec_store_bypass_disable",
1515 return SPEC_STORE_BYPASS_CMD_AUTO
;
1517 for (i
= 0; i
< ARRAY_SIZE(ssb_mitigation_options
); i
++) {
1518 if (!match_option(arg
, ret
, ssb_mitigation_options
[i
].option
))
1521 cmd
= ssb_mitigation_options
[i
].cmd
;
1525 if (i
>= ARRAY_SIZE(ssb_mitigation_options
)) {
1526 pr_err("unknown option (%s). Switching to AUTO select\n", arg
);
1527 return SPEC_STORE_BYPASS_CMD_AUTO
;
1534 static enum ssb_mitigation __init
__ssb_select_mitigation(void)
1536 enum ssb_mitigation mode
= SPEC_STORE_BYPASS_NONE
;
1537 enum ssb_mitigation_cmd cmd
;
1539 if (!boot_cpu_has(X86_FEATURE_SSBD
))
1542 cmd
= ssb_parse_cmdline();
1543 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS
) &&
1544 (cmd
== SPEC_STORE_BYPASS_CMD_NONE
||
1545 cmd
== SPEC_STORE_BYPASS_CMD_AUTO
))
1549 case SPEC_STORE_BYPASS_CMD_AUTO
:
1550 case SPEC_STORE_BYPASS_CMD_SECCOMP
:
1552 * Choose prctl+seccomp as the default mode if seccomp is
1555 if (IS_ENABLED(CONFIG_SECCOMP
))
1556 mode
= SPEC_STORE_BYPASS_SECCOMP
;
1558 mode
= SPEC_STORE_BYPASS_PRCTL
;
1560 case SPEC_STORE_BYPASS_CMD_ON
:
1561 mode
= SPEC_STORE_BYPASS_DISABLE
;
1563 case SPEC_STORE_BYPASS_CMD_PRCTL
:
1564 mode
= SPEC_STORE_BYPASS_PRCTL
;
1566 case SPEC_STORE_BYPASS_CMD_NONE
:
1571 * If SSBD is controlled by the SPEC_CTRL MSR, then set the proper
1572 * bit in the mask to allow guests to use the mitigation even in the
1573 * case where the host does not enable it.
1575 if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD
) ||
1576 static_cpu_has(X86_FEATURE_AMD_SSBD
)) {
1577 x86_spec_ctrl_mask
|= SPEC_CTRL_SSBD
;
1581 * We have three CPU feature flags that are in play here:
1582 * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
1583 * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
1584 * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
1586 if (mode
== SPEC_STORE_BYPASS_DISABLE
) {
1587 setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE
);
1589 * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
1590 * use a completely different MSR and bit dependent on family.
1592 if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD
) &&
1593 !static_cpu_has(X86_FEATURE_AMD_SSBD
)) {
1594 x86_amd_ssb_disable();
1596 x86_spec_ctrl_base
|= SPEC_CTRL_SSBD
;
1597 write_spec_ctrl_current(x86_spec_ctrl_base
, true);
1604 static void ssb_select_mitigation(void)
1606 ssb_mode
= __ssb_select_mitigation();
1608 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS
))
1609 pr_info("%s\n", ssb_strings
[ssb_mode
]);
1613 #define pr_fmt(fmt) "Speculation prctl: " fmt
1615 static void task_update_spec_tif(struct task_struct
*tsk
)
1617 /* Force the update of the real TIF bits */
1618 set_tsk_thread_flag(tsk
, TIF_SPEC_FORCE_UPDATE
);
1621 * Immediately update the speculation control MSRs for the current
1622 * task, but for a non-current task delay setting the CPU
1623 * mitigation until it is scheduled next.
1625 * This can only happen for SECCOMP mitigation. For PRCTL it's
1626 * always the current task.
1629 speculation_ctrl_update_current();
1632 static int l1d_flush_prctl_set(struct task_struct
*task
, unsigned long ctrl
)
1635 if (!static_branch_unlikely(&switch_mm_cond_l1d_flush
))
1639 case PR_SPEC_ENABLE
:
1640 set_ti_thread_flag(&task
->thread_info
, TIF_SPEC_L1D_FLUSH
);
1642 case PR_SPEC_DISABLE
:
1643 clear_ti_thread_flag(&task
->thread_info
, TIF_SPEC_L1D_FLUSH
);
1650 static int ssb_prctl_set(struct task_struct
*task
, unsigned long ctrl
)
1652 if (ssb_mode
!= SPEC_STORE_BYPASS_PRCTL
&&
1653 ssb_mode
!= SPEC_STORE_BYPASS_SECCOMP
)
1657 case PR_SPEC_ENABLE
:
1658 /* If speculation is force disabled, enable is not allowed */
1659 if (task_spec_ssb_force_disable(task
))
1661 task_clear_spec_ssb_disable(task
);
1662 task_clear_spec_ssb_noexec(task
);
1663 task_update_spec_tif(task
);
1665 case PR_SPEC_DISABLE
:
1666 task_set_spec_ssb_disable(task
);
1667 task_clear_spec_ssb_noexec(task
);
1668 task_update_spec_tif(task
);
1670 case PR_SPEC_FORCE_DISABLE
:
1671 task_set_spec_ssb_disable(task
);
1672 task_set_spec_ssb_force_disable(task
);
1673 task_clear_spec_ssb_noexec(task
);
1674 task_update_spec_tif(task
);
1676 case PR_SPEC_DISABLE_NOEXEC
:
1677 if (task_spec_ssb_force_disable(task
))
1679 task_set_spec_ssb_disable(task
);
1680 task_set_spec_ssb_noexec(task
);
1681 task_update_spec_tif(task
);
1689 static bool is_spec_ib_user_controlled(void)
1691 return spectre_v2_user_ibpb
== SPECTRE_V2_USER_PRCTL
||
1692 spectre_v2_user_ibpb
== SPECTRE_V2_USER_SECCOMP
||
1693 spectre_v2_user_stibp
== SPECTRE_V2_USER_PRCTL
||
1694 spectre_v2_user_stibp
== SPECTRE_V2_USER_SECCOMP
;
1697 static int ib_prctl_set(struct task_struct
*task
, unsigned long ctrl
)
1700 case PR_SPEC_ENABLE
:
1701 if (spectre_v2_user_ibpb
== SPECTRE_V2_USER_NONE
&&
1702 spectre_v2_user_stibp
== SPECTRE_V2_USER_NONE
)
1706 * With strict mode for both IBPB and STIBP, the instruction
1707 * code paths avoid checking this task flag and instead,
1708 * unconditionally run the instruction. However, STIBP and IBPB
1709 * are independent and either can be set to conditionally
1710 * enabled regardless of the mode of the other.
1712 * If either is set to conditional, allow the task flag to be
1713 * updated, unless it was force-disabled by a previous prctl
1714 * call. Currently, this is possible on an AMD CPU which has the
1715 * feature X86_FEATURE_AMD_STIBP_ALWAYS_ON. In this case, if the
1716 * kernel is booted with 'spectre_v2_user=seccomp', then
1717 * spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP and
1718 * spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED.
1720 if (!is_spec_ib_user_controlled() ||
1721 task_spec_ib_force_disable(task
))
1724 task_clear_spec_ib_disable(task
);
1725 task_update_spec_tif(task
);
1727 case PR_SPEC_DISABLE
:
1728 case PR_SPEC_FORCE_DISABLE
:
1730 * Indirect branch speculation is always allowed when
1731 * mitigation is force disabled.
1733 if (spectre_v2_user_ibpb
== SPECTRE_V2_USER_NONE
&&
1734 spectre_v2_user_stibp
== SPECTRE_V2_USER_NONE
)
1737 if (!is_spec_ib_user_controlled())
1740 task_set_spec_ib_disable(task
);
1741 if (ctrl
== PR_SPEC_FORCE_DISABLE
)
1742 task_set_spec_ib_force_disable(task
);
1743 task_update_spec_tif(task
);
1751 int arch_prctl_spec_ctrl_set(struct task_struct
*task
, unsigned long which
,
1755 case PR_SPEC_STORE_BYPASS
:
1756 return ssb_prctl_set(task
, ctrl
);
1757 case PR_SPEC_INDIRECT_BRANCH
:
1758 return ib_prctl_set(task
, ctrl
);
1759 case PR_SPEC_L1D_FLUSH
:
1760 return l1d_flush_prctl_set(task
, ctrl
);
1766 #ifdef CONFIG_SECCOMP
1767 void arch_seccomp_spec_mitigate(struct task_struct
*task
)
1769 if (ssb_mode
== SPEC_STORE_BYPASS_SECCOMP
)
1770 ssb_prctl_set(task
, PR_SPEC_FORCE_DISABLE
);
1771 if (spectre_v2_user_ibpb
== SPECTRE_V2_USER_SECCOMP
||
1772 spectre_v2_user_stibp
== SPECTRE_V2_USER_SECCOMP
)
1773 ib_prctl_set(task
, PR_SPEC_FORCE_DISABLE
);
1777 static int l1d_flush_prctl_get(struct task_struct
*task
)
1779 if (!static_branch_unlikely(&switch_mm_cond_l1d_flush
))
1780 return PR_SPEC_FORCE_DISABLE
;
1782 if (test_ti_thread_flag(&task
->thread_info
, TIF_SPEC_L1D_FLUSH
))
1783 return PR_SPEC_PRCTL
| PR_SPEC_ENABLE
;
1785 return PR_SPEC_PRCTL
| PR_SPEC_DISABLE
;
1788 static int ssb_prctl_get(struct task_struct
*task
)
1791 case SPEC_STORE_BYPASS_DISABLE
:
1792 return PR_SPEC_DISABLE
;
1793 case SPEC_STORE_BYPASS_SECCOMP
:
1794 case SPEC_STORE_BYPASS_PRCTL
:
1795 if (task_spec_ssb_force_disable(task
))
1796 return PR_SPEC_PRCTL
| PR_SPEC_FORCE_DISABLE
;
1797 if (task_spec_ssb_noexec(task
))
1798 return PR_SPEC_PRCTL
| PR_SPEC_DISABLE_NOEXEC
;
1799 if (task_spec_ssb_disable(task
))
1800 return PR_SPEC_PRCTL
| PR_SPEC_DISABLE
;
1801 return PR_SPEC_PRCTL
| PR_SPEC_ENABLE
;
1803 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS
))
1804 return PR_SPEC_ENABLE
;
1805 return PR_SPEC_NOT_AFFECTED
;
1809 static int ib_prctl_get(struct task_struct
*task
)
1811 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2
))
1812 return PR_SPEC_NOT_AFFECTED
;
1814 if (spectre_v2_user_ibpb
== SPECTRE_V2_USER_NONE
&&
1815 spectre_v2_user_stibp
== SPECTRE_V2_USER_NONE
)
1816 return PR_SPEC_ENABLE
;
1817 else if (is_spec_ib_user_controlled()) {
1818 if (task_spec_ib_force_disable(task
))
1819 return PR_SPEC_PRCTL
| PR_SPEC_FORCE_DISABLE
;
1820 if (task_spec_ib_disable(task
))
1821 return PR_SPEC_PRCTL
| PR_SPEC_DISABLE
;
1822 return PR_SPEC_PRCTL
| PR_SPEC_ENABLE
;
1823 } else if (spectre_v2_user_ibpb
== SPECTRE_V2_USER_STRICT
||
1824 spectre_v2_user_stibp
== SPECTRE_V2_USER_STRICT
||
1825 spectre_v2_user_stibp
== SPECTRE_V2_USER_STRICT_PREFERRED
)
1826 return PR_SPEC_DISABLE
;
1828 return PR_SPEC_NOT_AFFECTED
;
1831 int arch_prctl_spec_ctrl_get(struct task_struct
*task
, unsigned long which
)
1834 case PR_SPEC_STORE_BYPASS
:
1835 return ssb_prctl_get(task
);
1836 case PR_SPEC_INDIRECT_BRANCH
:
1837 return ib_prctl_get(task
);
1838 case PR_SPEC_L1D_FLUSH
:
1839 return l1d_flush_prctl_get(task
);
1845 void x86_spec_ctrl_setup_ap(void)
1847 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL
))
1848 write_spec_ctrl_current(x86_spec_ctrl_base
, true);
1850 if (ssb_mode
== SPEC_STORE_BYPASS_DISABLE
)
1851 x86_amd_ssb_disable();
1854 bool itlb_multihit_kvm_mitigation
;
1855 EXPORT_SYMBOL_GPL(itlb_multihit_kvm_mitigation
);
1858 #define pr_fmt(fmt) "L1TF: " fmt
1860 /* Default mitigation for L1TF-affected CPUs */
1861 enum l1tf_mitigations l1tf_mitigation __ro_after_init
= L1TF_MITIGATION_FLUSH
;
1862 #if IS_ENABLED(CONFIG_KVM_INTEL)
1863 EXPORT_SYMBOL_GPL(l1tf_mitigation
);
1865 enum vmx_l1d_flush_state l1tf_vmx_mitigation
= VMENTER_L1D_FLUSH_AUTO
;
1866 EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation
);
1869 * These CPUs all support 44bits physical address space internally in the
1870 * cache but CPUID can report a smaller number of physical address bits.
1872 * The L1TF mitigation uses the top most address bit for the inversion of
1873 * non present PTEs. When the installed memory reaches into the top most
1874 * address bit due to memory holes, which has been observed on machines
1875 * which report 36bits physical address bits and have 32G RAM installed,
1876 * then the mitigation range check in l1tf_select_mitigation() triggers.
1877 * This is a false positive because the mitigation is still possible due to
1878 * the fact that the cache uses 44bit internally. Use the cache bits
1879 * instead of the reported physical bits and adjust them on the affected
1880 * machines to 44bit if the reported bits are less than 44.
1882 static void override_cache_bits(struct cpuinfo_x86
*c
)
1887 switch (c
->x86_model
) {
1888 case INTEL_FAM6_NEHALEM
:
1889 case INTEL_FAM6_WESTMERE
:
1890 case INTEL_FAM6_SANDYBRIDGE
:
1891 case INTEL_FAM6_IVYBRIDGE
:
1892 case INTEL_FAM6_HASWELL
:
1893 case INTEL_FAM6_HASWELL_L
:
1894 case INTEL_FAM6_HASWELL_G
:
1895 case INTEL_FAM6_BROADWELL
:
1896 case INTEL_FAM6_BROADWELL_G
:
1897 case INTEL_FAM6_SKYLAKE_L
:
1898 case INTEL_FAM6_SKYLAKE
:
1899 case INTEL_FAM6_KABYLAKE_L
:
1900 case INTEL_FAM6_KABYLAKE
:
1901 if (c
->x86_cache_bits
< 44)
1902 c
->x86_cache_bits
= 44;
1907 static void __init
l1tf_select_mitigation(void)
1911 if (!boot_cpu_has_bug(X86_BUG_L1TF
))
1914 if (cpu_mitigations_off())
1915 l1tf_mitigation
= L1TF_MITIGATION_OFF
;
1916 else if (cpu_mitigations_auto_nosmt())
1917 l1tf_mitigation
= L1TF_MITIGATION_FLUSH_NOSMT
;
1919 override_cache_bits(&boot_cpu_data
);
1921 switch (l1tf_mitigation
) {
1922 case L1TF_MITIGATION_OFF
:
1923 case L1TF_MITIGATION_FLUSH_NOWARN
:
1924 case L1TF_MITIGATION_FLUSH
:
1926 case L1TF_MITIGATION_FLUSH_NOSMT
:
1927 case L1TF_MITIGATION_FULL
:
1928 cpu_smt_disable(false);
1930 case L1TF_MITIGATION_FULL_FORCE
:
1931 cpu_smt_disable(true);
1935 #if CONFIG_PGTABLE_LEVELS == 2
1936 pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
1940 half_pa
= (u64
)l1tf_pfn_limit() << PAGE_SHIFT
;
1941 if (l1tf_mitigation
!= L1TF_MITIGATION_OFF
&&
1942 e820__mapped_any(half_pa
, ULLONG_MAX
- half_pa
, E820_TYPE_RAM
)) {
1943 pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
1944 pr_info("You may make it effective by booting the kernel with mem=%llu parameter.\n",
1946 pr_info("However, doing so will make a part of your RAM unusable.\n");
1947 pr_info("Reading https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html might help you decide.\n");
1951 setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV
);
1954 static int __init
l1tf_cmdline(char *str
)
1956 if (!boot_cpu_has_bug(X86_BUG_L1TF
))
1962 if (!strcmp(str
, "off"))
1963 l1tf_mitigation
= L1TF_MITIGATION_OFF
;
1964 else if (!strcmp(str
, "flush,nowarn"))
1965 l1tf_mitigation
= L1TF_MITIGATION_FLUSH_NOWARN
;
1966 else if (!strcmp(str
, "flush"))
1967 l1tf_mitigation
= L1TF_MITIGATION_FLUSH
;
1968 else if (!strcmp(str
, "flush,nosmt"))
1969 l1tf_mitigation
= L1TF_MITIGATION_FLUSH_NOSMT
;
1970 else if (!strcmp(str
, "full"))
1971 l1tf_mitigation
= L1TF_MITIGATION_FULL
;
1972 else if (!strcmp(str
, "full,force"))
1973 l1tf_mitigation
= L1TF_MITIGATION_FULL_FORCE
;
1977 early_param("l1tf", l1tf_cmdline
);
1980 #define pr_fmt(fmt) fmt
1984 #define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
1986 #if IS_ENABLED(CONFIG_KVM_INTEL)
1987 static const char * const l1tf_vmx_states
[] = {
1988 [VMENTER_L1D_FLUSH_AUTO
] = "auto",
1989 [VMENTER_L1D_FLUSH_NEVER
] = "vulnerable",
1990 [VMENTER_L1D_FLUSH_COND
] = "conditional cache flushes",
1991 [VMENTER_L1D_FLUSH_ALWAYS
] = "cache flushes",
1992 [VMENTER_L1D_FLUSH_EPT_DISABLED
] = "EPT disabled",
1993 [VMENTER_L1D_FLUSH_NOT_REQUIRED
] = "flush not necessary"
1996 static ssize_t
l1tf_show_state(char *buf
)
1998 if (l1tf_vmx_mitigation
== VMENTER_L1D_FLUSH_AUTO
)
1999 return sprintf(buf
, "%s\n", L1TF_DEFAULT_MSG
);
2001 if (l1tf_vmx_mitigation
== VMENTER_L1D_FLUSH_EPT_DISABLED
||
2002 (l1tf_vmx_mitigation
== VMENTER_L1D_FLUSH_NEVER
&&
2003 sched_smt_active())) {
2004 return sprintf(buf
, "%s; VMX: %s\n", L1TF_DEFAULT_MSG
,
2005 l1tf_vmx_states
[l1tf_vmx_mitigation
]);
2008 return sprintf(buf
, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG
,
2009 l1tf_vmx_states
[l1tf_vmx_mitigation
],
2010 sched_smt_active() ? "vulnerable" : "disabled");
2013 static ssize_t
itlb_multihit_show_state(char *buf
)
2015 if (!boot_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL
) ||
2016 !boot_cpu_has(X86_FEATURE_VMX
))
2017 return sprintf(buf
, "KVM: Mitigation: VMX unsupported\n");
2018 else if (!(cr4_read_shadow() & X86_CR4_VMXE
))
2019 return sprintf(buf
, "KVM: Mitigation: VMX disabled\n");
2020 else if (itlb_multihit_kvm_mitigation
)
2021 return sprintf(buf
, "KVM: Mitigation: Split huge pages\n");
2023 return sprintf(buf
, "KVM: Vulnerable\n");
2026 static ssize_t
l1tf_show_state(char *buf
)
2028 return sprintf(buf
, "%s\n", L1TF_DEFAULT_MSG
);
2031 static ssize_t
itlb_multihit_show_state(char *buf
)
2033 return sprintf(buf
, "Processor vulnerable\n");
2037 static ssize_t
mds_show_state(char *buf
)
2039 if (boot_cpu_has(X86_FEATURE_HYPERVISOR
)) {
2040 return sprintf(buf
, "%s; SMT Host state unknown\n",
2041 mds_strings
[mds_mitigation
]);
2044 if (boot_cpu_has(X86_BUG_MSBDS_ONLY
)) {
2045 return sprintf(buf
, "%s; SMT %s\n", mds_strings
[mds_mitigation
],
2046 (mds_mitigation
== MDS_MITIGATION_OFF
? "vulnerable" :
2047 sched_smt_active() ? "mitigated" : "disabled"));
2050 return sprintf(buf
, "%s; SMT %s\n", mds_strings
[mds_mitigation
],
2051 sched_smt_active() ? "vulnerable" : "disabled");
2054 static ssize_t
tsx_async_abort_show_state(char *buf
)
2056 if ((taa_mitigation
== TAA_MITIGATION_TSX_DISABLED
) ||
2057 (taa_mitigation
== TAA_MITIGATION_OFF
))
2058 return sprintf(buf
, "%s\n", taa_strings
[taa_mitigation
]);
2060 if (boot_cpu_has(X86_FEATURE_HYPERVISOR
)) {
2061 return sprintf(buf
, "%s; SMT Host state unknown\n",
2062 taa_strings
[taa_mitigation
]);
2065 return sprintf(buf
, "%s; SMT %s\n", taa_strings
[taa_mitigation
],
2066 sched_smt_active() ? "vulnerable" : "disabled");
2069 static ssize_t
mmio_stale_data_show_state(char *buf
)
2071 if (mmio_mitigation
== MMIO_MITIGATION_OFF
)
2072 return sysfs_emit(buf
, "%s\n", mmio_strings
[mmio_mitigation
]);
2074 if (boot_cpu_has(X86_FEATURE_HYPERVISOR
)) {
2075 return sysfs_emit(buf
, "%s; SMT Host state unknown\n",
2076 mmio_strings
[mmio_mitigation
]);
2079 return sysfs_emit(buf
, "%s; SMT %s\n", mmio_strings
[mmio_mitigation
],
2080 sched_smt_active() ? "vulnerable" : "disabled");
2083 static char *stibp_state(void)
2085 if (spectre_v2_in_eibrs_mode(spectre_v2_enabled
))
2088 switch (spectre_v2_user_stibp
) {
2089 case SPECTRE_V2_USER_NONE
:
2090 return ", STIBP: disabled";
2091 case SPECTRE_V2_USER_STRICT
:
2092 return ", STIBP: forced";
2093 case SPECTRE_V2_USER_STRICT_PREFERRED
:
2094 return ", STIBP: always-on";
2095 case SPECTRE_V2_USER_PRCTL
:
2096 case SPECTRE_V2_USER_SECCOMP
:
2097 if (static_key_enabled(&switch_to_cond_stibp
))
2098 return ", STIBP: conditional";
2103 static char *ibpb_state(void)
2105 if (boot_cpu_has(X86_FEATURE_IBPB
)) {
2106 if (static_key_enabled(&switch_mm_always_ibpb
))
2107 return ", IBPB: always-on";
2108 if (static_key_enabled(&switch_mm_cond_ibpb
))
2109 return ", IBPB: conditional";
2110 return ", IBPB: disabled";
2115 static ssize_t
spectre_v2_show_state(char *buf
)
2117 if (spectre_v2_enabled
== SPECTRE_V2_LFENCE
)
2118 return sprintf(buf
, "Vulnerable: LFENCE\n");
2120 if (spectre_v2_enabled
== SPECTRE_V2_EIBRS
&& unprivileged_ebpf_enabled())
2121 return sprintf(buf
, "Vulnerable: eIBRS with unprivileged eBPF\n");
2123 if (sched_smt_active() && unprivileged_ebpf_enabled() &&
2124 spectre_v2_enabled
== SPECTRE_V2_EIBRS_LFENCE
)
2125 return sprintf(buf
, "Vulnerable: eIBRS+LFENCE with unprivileged eBPF and SMT\n");
2127 return sprintf(buf
, "%s%s%s%s%s%s\n",
2128 spectre_v2_strings
[spectre_v2_enabled
],
2130 boot_cpu_has(X86_FEATURE_USE_IBRS_FW
) ? ", IBRS_FW" : "",
2132 boot_cpu_has(X86_FEATURE_RSB_CTXSW
) ? ", RSB filling" : "",
2133 spectre_v2_module_string());
2136 static ssize_t
srbds_show_state(char *buf
)
2138 return sprintf(buf
, "%s\n", srbds_strings
[srbds_mitigation
]);
2141 static ssize_t
retbleed_show_state(char *buf
)
2143 if (retbleed_mitigation
== RETBLEED_MITIGATION_UNRET
) {
2144 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_AMD
&&
2145 boot_cpu_data
.x86_vendor
!= X86_VENDOR_HYGON
)
2146 return sprintf(buf
, "Vulnerable: untrained return thunk on non-Zen uarch\n");
2148 return sprintf(buf
, "%s; SMT %s\n",
2149 retbleed_strings
[retbleed_mitigation
],
2150 !sched_smt_active() ? "disabled" :
2151 spectre_v2_user_stibp
== SPECTRE_V2_USER_STRICT
||
2152 spectre_v2_user_stibp
== SPECTRE_V2_USER_STRICT_PREFERRED
?
2153 "enabled with STIBP protection" : "vulnerable");
2156 return sprintf(buf
, "%s\n", retbleed_strings
[retbleed_mitigation
]);
2159 static ssize_t
cpu_show_common(struct device
*dev
, struct device_attribute
*attr
,
2160 char *buf
, unsigned int bug
)
2162 if (!boot_cpu_has_bug(bug
))
2163 return sprintf(buf
, "Not affected\n");
2166 case X86_BUG_CPU_MELTDOWN
:
2167 if (boot_cpu_has(X86_FEATURE_PTI
))
2168 return sprintf(buf
, "Mitigation: PTI\n");
2170 if (hypervisor_is_type(X86_HYPER_XEN_PV
))
2171 return sprintf(buf
, "Unknown (XEN PV detected, hypervisor mitigation required)\n");
2175 case X86_BUG_SPECTRE_V1
:
2176 return sprintf(buf
, "%s\n", spectre_v1_strings
[spectre_v1_mitigation
]);
2178 case X86_BUG_SPECTRE_V2
:
2179 return spectre_v2_show_state(buf
);
2181 case X86_BUG_SPEC_STORE_BYPASS
:
2182 return sprintf(buf
, "%s\n", ssb_strings
[ssb_mode
]);
2185 if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV
))
2186 return l1tf_show_state(buf
);
2190 return mds_show_state(buf
);
2193 return tsx_async_abort_show_state(buf
);
2195 case X86_BUG_ITLB_MULTIHIT
:
2196 return itlb_multihit_show_state(buf
);
2199 return srbds_show_state(buf
);
2201 case X86_BUG_MMIO_STALE_DATA
:
2202 return mmio_stale_data_show_state(buf
);
2204 case X86_BUG_RETBLEED
:
2205 return retbleed_show_state(buf
);
2211 return sprintf(buf
, "Vulnerable\n");
2214 ssize_t
cpu_show_meltdown(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
2216 return cpu_show_common(dev
, attr
, buf
, X86_BUG_CPU_MELTDOWN
);
2219 ssize_t
cpu_show_spectre_v1(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
2221 return cpu_show_common(dev
, attr
, buf
, X86_BUG_SPECTRE_V1
);
2224 ssize_t
cpu_show_spectre_v2(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
2226 return cpu_show_common(dev
, attr
, buf
, X86_BUG_SPECTRE_V2
);
2229 ssize_t
cpu_show_spec_store_bypass(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
2231 return cpu_show_common(dev
, attr
, buf
, X86_BUG_SPEC_STORE_BYPASS
);
2234 ssize_t
cpu_show_l1tf(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
2236 return cpu_show_common(dev
, attr
, buf
, X86_BUG_L1TF
);
2239 ssize_t
cpu_show_mds(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
2241 return cpu_show_common(dev
, attr
, buf
, X86_BUG_MDS
);
2244 ssize_t
cpu_show_tsx_async_abort(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
2246 return cpu_show_common(dev
, attr
, buf
, X86_BUG_TAA
);
2249 ssize_t
cpu_show_itlb_multihit(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
2251 return cpu_show_common(dev
, attr
, buf
, X86_BUG_ITLB_MULTIHIT
);
2254 ssize_t
cpu_show_srbds(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
2256 return cpu_show_common(dev
, attr
, buf
, X86_BUG_SRBDS
);
2259 ssize_t
cpu_show_mmio_stale_data(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
2261 return cpu_show_common(dev
, attr
, buf
, X86_BUG_MMIO_STALE_DATA
);
2264 ssize_t
cpu_show_retbleed(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
2266 return cpu_show_common(dev
, attr
, buf
, X86_BUG_RETBLEED
);