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>
34 static void __init
spectre_v2_select_mitigation(void);
35 static void __init
ssb_select_mitigation(void);
36 static void __init
l1tf_select_mitigation(void);
38 /* The base value of the SPEC_CTRL MSR that always has to be preserved. */
39 u64 x86_spec_ctrl_base
;
40 EXPORT_SYMBOL_GPL(x86_spec_ctrl_base
);
41 static DEFINE_MUTEX(spec_ctrl_mutex
);
44 * The vendor and possibly platform specific bits which can be modified in
47 static u64 __ro_after_init x86_spec_ctrl_mask
= SPEC_CTRL_IBRS
;
50 * AMD specific MSR info for Speculative Store Bypass control.
51 * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu().
53 u64 __ro_after_init x86_amd_ls_cfg_base
;
54 u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask
;
56 /* Control conditional STIPB in switch_to() */
57 DEFINE_STATIC_KEY_FALSE(switch_to_cond_stibp
);
59 void __init
check_bugs(void)
64 * identify_boot_cpu() initialized SMT support information, let the
67 cpu_smt_check_topology_early();
69 if (!IS_ENABLED(CONFIG_SMP
)) {
71 print_cpu_info(&boot_cpu_data
);
75 * Read the SPEC_CTRL MSR to account for reserved bits which may
76 * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
77 * init code as it is not enumerated and depends on the family.
79 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL
))
80 rdmsrl(MSR_IA32_SPEC_CTRL
, x86_spec_ctrl_base
);
82 /* Allow STIBP in MSR_SPEC_CTRL if supported */
83 if (boot_cpu_has(X86_FEATURE_STIBP
))
84 x86_spec_ctrl_mask
|= SPEC_CTRL_STIBP
;
86 /* Select the proper spectre mitigation before patching alternatives */
87 spectre_v2_select_mitigation();
90 * Select proper mitigation for any exposure to the Speculative Store
91 * Bypass vulnerability.
93 ssb_select_mitigation();
95 l1tf_select_mitigation();
99 * Check whether we are able to run this kernel safely on SMP.
101 * - i386 is no longer supported.
102 * - In order to run on anything without a TSC, we need to be
103 * compiled for a i486.
105 if (boot_cpu_data
.x86
< 4)
106 panic("Kernel requires i486+ for 'invlpg' and other features");
108 init_utsname()->machine
[1] =
109 '0' + (boot_cpu_data
.x86
> 6 ? 6 : boot_cpu_data
.x86
);
110 alternative_instructions();
112 fpu__init_check_bugs();
113 #else /* CONFIG_X86_64 */
114 alternative_instructions();
117 * Make sure the first 2MB area is not mapped by huge pages
118 * There are typically fixed size MTRRs in there and overlapping
119 * MTRRs into large pages causes slow downs.
121 * Right now we don't do that with gbpages because there seems
122 * very little benefit for that case.
125 set_memory_4k((unsigned long)__va(0), 1);
130 x86_virt_spec_ctrl(u64 guest_spec_ctrl
, u64 guest_virt_spec_ctrl
, bool setguest
)
132 u64 msrval
, guestval
, hostval
= x86_spec_ctrl_base
;
133 struct thread_info
*ti
= current_thread_info();
135 /* Is MSR_SPEC_CTRL implemented ? */
136 if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL
)) {
138 * Restrict guest_spec_ctrl to supported values. Clear the
139 * modifiable bits in the host base value and or the
140 * modifiable bits from the guest value.
142 guestval
= hostval
& ~x86_spec_ctrl_mask
;
143 guestval
|= guest_spec_ctrl
& x86_spec_ctrl_mask
;
145 /* SSBD controlled in MSR_SPEC_CTRL */
146 if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD
) ||
147 static_cpu_has(X86_FEATURE_AMD_SSBD
))
148 hostval
|= ssbd_tif_to_spec_ctrl(ti
->flags
);
150 if (hostval
!= guestval
) {
151 msrval
= setguest
? guestval
: hostval
;
152 wrmsrl(MSR_IA32_SPEC_CTRL
, msrval
);
157 * If SSBD is not handled in MSR_SPEC_CTRL on AMD, update
158 * MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported.
160 if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD
) &&
161 !static_cpu_has(X86_FEATURE_VIRT_SSBD
))
165 * If the host has SSBD mitigation enabled, force it in the host's
166 * virtual MSR value. If its not permanently enabled, evaluate
167 * current's TIF_SSBD thread flag.
169 if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE
))
170 hostval
= SPEC_CTRL_SSBD
;
172 hostval
= ssbd_tif_to_spec_ctrl(ti
->flags
);
174 /* Sanitize the guest value */
175 guestval
= guest_virt_spec_ctrl
& SPEC_CTRL_SSBD
;
177 if (hostval
!= guestval
) {
180 tif
= setguest
? ssbd_spec_ctrl_to_tif(guestval
) :
181 ssbd_spec_ctrl_to_tif(hostval
);
183 speculation_ctrl_update(tif
);
186 EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl
);
188 static void x86_amd_ssb_disable(void)
190 u64 msrval
= x86_amd_ls_cfg_base
| x86_amd_ls_cfg_ssbd_mask
;
192 if (boot_cpu_has(X86_FEATURE_VIRT_SSBD
))
193 wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL
, SPEC_CTRL_SSBD
);
194 else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD
))
195 wrmsrl(MSR_AMD64_LS_CFG
, msrval
);
199 #define pr_fmt(fmt) "Spectre V2 : " fmt
201 static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init
=
204 static enum spectre_v2_user_mitigation spectre_v2_user __ro_after_init
=
205 SPECTRE_V2_USER_NONE
;
208 static bool spectre_v2_bad_module
;
210 bool retpoline_module_ok(bool has_retpoline
)
212 if (spectre_v2_enabled
== SPECTRE_V2_NONE
|| has_retpoline
)
215 pr_err("System may be vulnerable to spectre v2\n");
216 spectre_v2_bad_module
= true;
220 static inline const char *spectre_v2_module_string(void)
222 return spectre_v2_bad_module
? " - vulnerable module loaded" : "";
225 static inline const char *spectre_v2_module_string(void) { return ""; }
228 static inline bool match_option(const char *arg
, int arglen
, const char *opt
)
230 int len
= strlen(opt
);
232 return len
== arglen
&& !strncmp(arg
, opt
, len
);
235 /* The kernel command line selection for spectre v2 */
236 enum spectre_v2_mitigation_cmd
{
239 SPECTRE_V2_CMD_FORCE
,
240 SPECTRE_V2_CMD_RETPOLINE
,
241 SPECTRE_V2_CMD_RETPOLINE_GENERIC
,
242 SPECTRE_V2_CMD_RETPOLINE_AMD
,
245 enum spectre_v2_user_cmd
{
246 SPECTRE_V2_USER_CMD_NONE
,
247 SPECTRE_V2_USER_CMD_AUTO
,
248 SPECTRE_V2_USER_CMD_FORCE
,
251 static const char * const spectre_v2_user_strings
[] = {
252 [SPECTRE_V2_USER_NONE
] = "User space: Vulnerable",
253 [SPECTRE_V2_USER_STRICT
] = "User space: Mitigation: STIBP protection",
256 static const struct {
258 enum spectre_v2_user_cmd cmd
;
260 } v2_user_options
[] __initdata
= {
261 { "auto", SPECTRE_V2_USER_CMD_AUTO
, false },
262 { "off", SPECTRE_V2_USER_CMD_NONE
, false },
263 { "on", SPECTRE_V2_USER_CMD_FORCE
, true },
266 static void __init
spec_v2_user_print_cond(const char *reason
, bool secure
)
268 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2
) != secure
)
269 pr_info("spectre_v2_user=%s forced on command line.\n", reason
);
272 static enum spectre_v2_user_cmd __init
273 spectre_v2_parse_user_cmdline(enum spectre_v2_mitigation_cmd v2_cmd
)
279 case SPECTRE_V2_CMD_NONE
:
280 return SPECTRE_V2_USER_CMD_NONE
;
281 case SPECTRE_V2_CMD_FORCE
:
282 return SPECTRE_V2_USER_CMD_FORCE
;
287 ret
= cmdline_find_option(boot_command_line
, "spectre_v2_user",
290 return SPECTRE_V2_USER_CMD_AUTO
;
292 for (i
= 0; i
< ARRAY_SIZE(v2_user_options
); i
++) {
293 if (match_option(arg
, ret
, v2_user_options
[i
].option
)) {
294 spec_v2_user_print_cond(v2_user_options
[i
].option
,
295 v2_user_options
[i
].secure
);
296 return v2_user_options
[i
].cmd
;
300 pr_err("Unknown user space protection option (%s). Switching to AUTO select\n", arg
);
301 return SPECTRE_V2_USER_CMD_AUTO
;
305 spectre_v2_user_select_mitigation(enum spectre_v2_mitigation_cmd v2_cmd
)
307 enum spectre_v2_user_mitigation mode
= SPECTRE_V2_USER_NONE
;
308 bool smt_possible
= IS_ENABLED(CONFIG_SMP
);
310 if (!boot_cpu_has(X86_FEATURE_IBPB
) && !boot_cpu_has(X86_FEATURE_STIBP
))
313 if (cpu_smt_control
== CPU_SMT_FORCE_DISABLED
||
314 cpu_smt_control
== CPU_SMT_NOT_SUPPORTED
)
315 smt_possible
= false;
317 switch (spectre_v2_parse_user_cmdline(v2_cmd
)) {
318 case SPECTRE_V2_USER_CMD_AUTO
:
319 case SPECTRE_V2_USER_CMD_NONE
:
321 case SPECTRE_V2_USER_CMD_FORCE
:
322 mode
= SPECTRE_V2_USER_STRICT
;
326 /* Initialize Indirect Branch Prediction Barrier */
327 if (boot_cpu_has(X86_FEATURE_IBPB
)) {
328 setup_force_cpu_cap(X86_FEATURE_USE_IBPB
);
329 pr_info("Spectre v2 mitigation: Enabling Indirect Branch Prediction Barrier\n");
332 /* If enhanced IBRS is enabled no STIPB required */
333 if (spectre_v2_enabled
== SPECTRE_V2_IBRS_ENHANCED
)
337 spectre_v2_user
= mode
;
338 /* Only print the STIBP mode when SMT possible */
340 pr_info("%s\n", spectre_v2_user_strings
[mode
]);
343 static const char * const spectre_v2_strings
[] = {
344 [SPECTRE_V2_NONE
] = "Vulnerable",
345 [SPECTRE_V2_RETPOLINE_GENERIC
] = "Mitigation: Full generic retpoline",
346 [SPECTRE_V2_RETPOLINE_AMD
] = "Mitigation: Full AMD retpoline",
347 [SPECTRE_V2_IBRS_ENHANCED
] = "Mitigation: Enhanced IBRS",
350 static const struct {
352 enum spectre_v2_mitigation_cmd cmd
;
354 } mitigation_options
[] __initdata
= {
355 { "off", SPECTRE_V2_CMD_NONE
, false },
356 { "on", SPECTRE_V2_CMD_FORCE
, true },
357 { "retpoline", SPECTRE_V2_CMD_RETPOLINE
, false },
358 { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_AMD
, false },
359 { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC
, false },
360 { "auto", SPECTRE_V2_CMD_AUTO
, false },
363 static void __init
spec_v2_print_cond(const char *reason
, bool secure
)
365 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2
) != secure
)
366 pr_info("%s selected on command line.\n", reason
);
369 static enum spectre_v2_mitigation_cmd __init
spectre_v2_parse_cmdline(void)
371 enum spectre_v2_mitigation_cmd cmd
= SPECTRE_V2_CMD_AUTO
;
375 if (cmdline_find_option_bool(boot_command_line
, "nospectre_v2"))
376 return SPECTRE_V2_CMD_NONE
;
378 ret
= cmdline_find_option(boot_command_line
, "spectre_v2", arg
, sizeof(arg
));
380 return SPECTRE_V2_CMD_AUTO
;
382 for (i
= 0; i
< ARRAY_SIZE(mitigation_options
); i
++) {
383 if (!match_option(arg
, ret
, mitigation_options
[i
].option
))
385 cmd
= mitigation_options
[i
].cmd
;
389 if (i
>= ARRAY_SIZE(mitigation_options
)) {
390 pr_err("unknown option (%s). Switching to AUTO select\n", arg
);
391 return SPECTRE_V2_CMD_AUTO
;
394 if ((cmd
== SPECTRE_V2_CMD_RETPOLINE
||
395 cmd
== SPECTRE_V2_CMD_RETPOLINE_AMD
||
396 cmd
== SPECTRE_V2_CMD_RETPOLINE_GENERIC
) &&
397 !IS_ENABLED(CONFIG_RETPOLINE
)) {
398 pr_err("%s selected but not compiled in. Switching to AUTO select\n", mitigation_options
[i
].option
);
399 return SPECTRE_V2_CMD_AUTO
;
402 if (cmd
== SPECTRE_V2_CMD_RETPOLINE_AMD
&&
403 boot_cpu_data
.x86_vendor
!= X86_VENDOR_AMD
) {
404 pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n");
405 return SPECTRE_V2_CMD_AUTO
;
408 spec_v2_print_cond(mitigation_options
[i
].option
,
409 mitigation_options
[i
].secure
);
413 static void __init
spectre_v2_select_mitigation(void)
415 enum spectre_v2_mitigation_cmd cmd
= spectre_v2_parse_cmdline();
416 enum spectre_v2_mitigation mode
= SPECTRE_V2_NONE
;
419 * If the CPU is not affected and the command line mode is NONE or AUTO
420 * then nothing to do.
422 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2
) &&
423 (cmd
== SPECTRE_V2_CMD_NONE
|| cmd
== SPECTRE_V2_CMD_AUTO
))
427 case SPECTRE_V2_CMD_NONE
:
430 case SPECTRE_V2_CMD_FORCE
:
431 case SPECTRE_V2_CMD_AUTO
:
432 if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED
)) {
433 mode
= SPECTRE_V2_IBRS_ENHANCED
;
434 /* Force it so VMEXIT will restore correctly */
435 x86_spec_ctrl_base
|= SPEC_CTRL_IBRS
;
436 wrmsrl(MSR_IA32_SPEC_CTRL
, x86_spec_ctrl_base
);
437 goto specv2_set_mode
;
439 if (IS_ENABLED(CONFIG_RETPOLINE
))
442 case SPECTRE_V2_CMD_RETPOLINE_AMD
:
443 if (IS_ENABLED(CONFIG_RETPOLINE
))
446 case SPECTRE_V2_CMD_RETPOLINE_GENERIC
:
447 if (IS_ENABLED(CONFIG_RETPOLINE
))
448 goto retpoline_generic
;
450 case SPECTRE_V2_CMD_RETPOLINE
:
451 if (IS_ENABLED(CONFIG_RETPOLINE
))
455 pr_err("Spectre mitigation: kernel not compiled with retpoline; no mitigation available!");
459 if (boot_cpu_data
.x86_vendor
== X86_VENDOR_AMD
) {
461 if (!boot_cpu_has(X86_FEATURE_LFENCE_RDTSC
)) {
462 pr_err("Spectre mitigation: LFENCE not serializing, switching to generic retpoline\n");
463 goto retpoline_generic
;
465 mode
= SPECTRE_V2_RETPOLINE_AMD
;
466 setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD
);
467 setup_force_cpu_cap(X86_FEATURE_RETPOLINE
);
470 mode
= SPECTRE_V2_RETPOLINE_GENERIC
;
471 setup_force_cpu_cap(X86_FEATURE_RETPOLINE
);
475 spectre_v2_enabled
= mode
;
476 pr_info("%s\n", spectre_v2_strings
[mode
]);
479 * If spectre v2 protection has been enabled, unconditionally fill
480 * RSB during a context switch; this protects against two independent
483 * - RSB underflow (and switch to BTB) on Skylake+
484 * - SpectreRSB variant of spectre v2 on X86_BUG_SPECTRE_V2 CPUs
486 setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW
);
487 pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n");
490 * Retpoline means the kernel is safe because it has no indirect
491 * branches. Enhanced IBRS protects firmware too, so, enable restricted
492 * speculation around firmware calls only when Enhanced IBRS isn't
495 * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because
496 * the user might select retpoline on the kernel command line and if
497 * the CPU supports Enhanced IBRS, kernel might un-intentionally not
498 * enable IBRS around firmware calls.
500 if (boot_cpu_has(X86_FEATURE_IBRS
) && mode
!= SPECTRE_V2_IBRS_ENHANCED
) {
501 setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW
);
502 pr_info("Enabling Restricted Speculation for firmware calls\n");
505 /* Set up IBPB and STIBP depending on the general spectre V2 command */
506 spectre_v2_user_select_mitigation(cmd
);
508 /* Enable STIBP if appropriate */
512 static bool stibp_needed(void)
514 /* Enhanced IBRS makes using STIBP unnecessary. */
515 if (spectre_v2_enabled
== SPECTRE_V2_IBRS_ENHANCED
)
518 /* Check for strict user mitigation mode */
519 return spectre_v2_user
== SPECTRE_V2_USER_STRICT
;
522 static void update_stibp_msr(void *info
)
524 wrmsrl(MSR_IA32_SPEC_CTRL
, x86_spec_ctrl_base
);
527 void arch_smt_update(void)
534 mutex_lock(&spec_ctrl_mutex
);
536 mask
= x86_spec_ctrl_base
& ~SPEC_CTRL_STIBP
;
537 if (sched_smt_active())
538 mask
|= SPEC_CTRL_STIBP
;
540 if (mask
!= x86_spec_ctrl_base
) {
541 pr_info("Spectre v2 cross-process SMT mitigation: %s STIBP\n",
542 mask
& SPEC_CTRL_STIBP
? "Enabling" : "Disabling");
543 x86_spec_ctrl_base
= mask
;
544 on_each_cpu(update_stibp_msr
, NULL
, 1);
546 mutex_unlock(&spec_ctrl_mutex
);
550 #define pr_fmt(fmt) "Speculative Store Bypass: " fmt
552 static enum ssb_mitigation ssb_mode __ro_after_init
= SPEC_STORE_BYPASS_NONE
;
554 /* The kernel command line selection */
555 enum ssb_mitigation_cmd
{
556 SPEC_STORE_BYPASS_CMD_NONE
,
557 SPEC_STORE_BYPASS_CMD_AUTO
,
558 SPEC_STORE_BYPASS_CMD_ON
,
559 SPEC_STORE_BYPASS_CMD_PRCTL
,
560 SPEC_STORE_BYPASS_CMD_SECCOMP
,
563 static const char * const ssb_strings
[] = {
564 [SPEC_STORE_BYPASS_NONE
] = "Vulnerable",
565 [SPEC_STORE_BYPASS_DISABLE
] = "Mitigation: Speculative Store Bypass disabled",
566 [SPEC_STORE_BYPASS_PRCTL
] = "Mitigation: Speculative Store Bypass disabled via prctl",
567 [SPEC_STORE_BYPASS_SECCOMP
] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
570 static const struct {
572 enum ssb_mitigation_cmd cmd
;
573 } ssb_mitigation_options
[] __initdata
= {
574 { "auto", SPEC_STORE_BYPASS_CMD_AUTO
}, /* Platform decides */
575 { "on", SPEC_STORE_BYPASS_CMD_ON
}, /* Disable Speculative Store Bypass */
576 { "off", SPEC_STORE_BYPASS_CMD_NONE
}, /* Don't touch Speculative Store Bypass */
577 { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL
}, /* Disable Speculative Store Bypass via prctl */
578 { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP
}, /* Disable Speculative Store Bypass via prctl and seccomp */
581 static enum ssb_mitigation_cmd __init
ssb_parse_cmdline(void)
583 enum ssb_mitigation_cmd cmd
= SPEC_STORE_BYPASS_CMD_AUTO
;
587 if (cmdline_find_option_bool(boot_command_line
, "nospec_store_bypass_disable")) {
588 return SPEC_STORE_BYPASS_CMD_NONE
;
590 ret
= cmdline_find_option(boot_command_line
, "spec_store_bypass_disable",
593 return SPEC_STORE_BYPASS_CMD_AUTO
;
595 for (i
= 0; i
< ARRAY_SIZE(ssb_mitigation_options
); i
++) {
596 if (!match_option(arg
, ret
, ssb_mitigation_options
[i
].option
))
599 cmd
= ssb_mitigation_options
[i
].cmd
;
603 if (i
>= ARRAY_SIZE(ssb_mitigation_options
)) {
604 pr_err("unknown option (%s). Switching to AUTO select\n", arg
);
605 return SPEC_STORE_BYPASS_CMD_AUTO
;
612 static enum ssb_mitigation __init
__ssb_select_mitigation(void)
614 enum ssb_mitigation mode
= SPEC_STORE_BYPASS_NONE
;
615 enum ssb_mitigation_cmd cmd
;
617 if (!boot_cpu_has(X86_FEATURE_SSBD
))
620 cmd
= ssb_parse_cmdline();
621 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS
) &&
622 (cmd
== SPEC_STORE_BYPASS_CMD_NONE
||
623 cmd
== SPEC_STORE_BYPASS_CMD_AUTO
))
627 case SPEC_STORE_BYPASS_CMD_AUTO
:
628 case SPEC_STORE_BYPASS_CMD_SECCOMP
:
630 * Choose prctl+seccomp as the default mode if seccomp is
633 if (IS_ENABLED(CONFIG_SECCOMP
))
634 mode
= SPEC_STORE_BYPASS_SECCOMP
;
636 mode
= SPEC_STORE_BYPASS_PRCTL
;
638 case SPEC_STORE_BYPASS_CMD_ON
:
639 mode
= SPEC_STORE_BYPASS_DISABLE
;
641 case SPEC_STORE_BYPASS_CMD_PRCTL
:
642 mode
= SPEC_STORE_BYPASS_PRCTL
;
644 case SPEC_STORE_BYPASS_CMD_NONE
:
649 * We have three CPU feature flags that are in play here:
650 * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
651 * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
652 * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
654 if (mode
== SPEC_STORE_BYPASS_DISABLE
) {
655 setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE
);
657 * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
658 * use a completely different MSR and bit dependent on family.
660 if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD
) &&
661 !static_cpu_has(X86_FEATURE_AMD_SSBD
)) {
662 x86_amd_ssb_disable();
664 x86_spec_ctrl_base
|= SPEC_CTRL_SSBD
;
665 x86_spec_ctrl_mask
|= SPEC_CTRL_SSBD
;
666 wrmsrl(MSR_IA32_SPEC_CTRL
, x86_spec_ctrl_base
);
673 static void ssb_select_mitigation(void)
675 ssb_mode
= __ssb_select_mitigation();
677 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS
))
678 pr_info("%s\n", ssb_strings
[ssb_mode
]);
682 #define pr_fmt(fmt) "Speculation prctl: " fmt
684 static int ssb_prctl_set(struct task_struct
*task
, unsigned long ctrl
)
688 if (ssb_mode
!= SPEC_STORE_BYPASS_PRCTL
&&
689 ssb_mode
!= SPEC_STORE_BYPASS_SECCOMP
)
694 /* If speculation is force disabled, enable is not allowed */
695 if (task_spec_ssb_force_disable(task
))
697 task_clear_spec_ssb_disable(task
);
698 update
= test_and_clear_tsk_thread_flag(task
, TIF_SSBD
);
700 case PR_SPEC_DISABLE
:
701 task_set_spec_ssb_disable(task
);
702 update
= !test_and_set_tsk_thread_flag(task
, TIF_SSBD
);
704 case PR_SPEC_FORCE_DISABLE
:
705 task_set_spec_ssb_disable(task
);
706 task_set_spec_ssb_force_disable(task
);
707 update
= !test_and_set_tsk_thread_flag(task
, TIF_SSBD
);
714 * If being set on non-current task, delay setting the CPU
715 * mitigation until it is next scheduled.
717 if (task
== current
&& update
)
718 speculation_ctrl_update_current();
723 int arch_prctl_spec_ctrl_set(struct task_struct
*task
, unsigned long which
,
727 case PR_SPEC_STORE_BYPASS
:
728 return ssb_prctl_set(task
, ctrl
);
734 #ifdef CONFIG_SECCOMP
735 void arch_seccomp_spec_mitigate(struct task_struct
*task
)
737 if (ssb_mode
== SPEC_STORE_BYPASS_SECCOMP
)
738 ssb_prctl_set(task
, PR_SPEC_FORCE_DISABLE
);
742 static int ssb_prctl_get(struct task_struct
*task
)
745 case SPEC_STORE_BYPASS_DISABLE
:
746 return PR_SPEC_DISABLE
;
747 case SPEC_STORE_BYPASS_SECCOMP
:
748 case SPEC_STORE_BYPASS_PRCTL
:
749 if (task_spec_ssb_force_disable(task
))
750 return PR_SPEC_PRCTL
| PR_SPEC_FORCE_DISABLE
;
751 if (task_spec_ssb_disable(task
))
752 return PR_SPEC_PRCTL
| PR_SPEC_DISABLE
;
753 return PR_SPEC_PRCTL
| PR_SPEC_ENABLE
;
755 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS
))
756 return PR_SPEC_ENABLE
;
757 return PR_SPEC_NOT_AFFECTED
;
761 int arch_prctl_spec_ctrl_get(struct task_struct
*task
, unsigned long which
)
764 case PR_SPEC_STORE_BYPASS
:
765 return ssb_prctl_get(task
);
771 void x86_spec_ctrl_setup_ap(void)
773 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL
))
774 wrmsrl(MSR_IA32_SPEC_CTRL
, x86_spec_ctrl_base
);
776 if (ssb_mode
== SPEC_STORE_BYPASS_DISABLE
)
777 x86_amd_ssb_disable();
781 #define pr_fmt(fmt) "L1TF: " fmt
783 /* Default mitigation for L1TF-affected CPUs */
784 enum l1tf_mitigations l1tf_mitigation __ro_after_init
= L1TF_MITIGATION_FLUSH
;
785 #if IS_ENABLED(CONFIG_KVM_INTEL)
786 EXPORT_SYMBOL_GPL(l1tf_mitigation
);
788 enum vmx_l1d_flush_state l1tf_vmx_mitigation
= VMENTER_L1D_FLUSH_AUTO
;
789 EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation
);
793 * These CPUs all support 44bits physical address space internally in the
794 * cache but CPUID can report a smaller number of physical address bits.
796 * The L1TF mitigation uses the top most address bit for the inversion of
797 * non present PTEs. When the installed memory reaches into the top most
798 * address bit due to memory holes, which has been observed on machines
799 * which report 36bits physical address bits and have 32G RAM installed,
800 * then the mitigation range check in l1tf_select_mitigation() triggers.
801 * This is a false positive because the mitigation is still possible due to
802 * the fact that the cache uses 44bit internally. Use the cache bits
803 * instead of the reported physical bits and adjust them on the affected
804 * machines to 44bit if the reported bits are less than 44.
806 static void override_cache_bits(struct cpuinfo_x86
*c
)
811 switch (c
->x86_model
) {
812 case INTEL_FAM6_NEHALEM
:
813 case INTEL_FAM6_WESTMERE
:
814 case INTEL_FAM6_SANDYBRIDGE
:
815 case INTEL_FAM6_IVYBRIDGE
:
816 case INTEL_FAM6_HASWELL_CORE
:
817 case INTEL_FAM6_HASWELL_ULT
:
818 case INTEL_FAM6_HASWELL_GT3E
:
819 case INTEL_FAM6_BROADWELL_CORE
:
820 case INTEL_FAM6_BROADWELL_GT3E
:
821 case INTEL_FAM6_SKYLAKE_MOBILE
:
822 case INTEL_FAM6_SKYLAKE_DESKTOP
:
823 case INTEL_FAM6_KABYLAKE_MOBILE
:
824 case INTEL_FAM6_KABYLAKE_DESKTOP
:
825 if (c
->x86_cache_bits
< 44)
826 c
->x86_cache_bits
= 44;
831 static void __init
l1tf_select_mitigation(void)
835 if (!boot_cpu_has_bug(X86_BUG_L1TF
))
838 override_cache_bits(&boot_cpu_data
);
840 switch (l1tf_mitigation
) {
841 case L1TF_MITIGATION_OFF
:
842 case L1TF_MITIGATION_FLUSH_NOWARN
:
843 case L1TF_MITIGATION_FLUSH
:
845 case L1TF_MITIGATION_FLUSH_NOSMT
:
846 case L1TF_MITIGATION_FULL
:
847 cpu_smt_disable(false);
849 case L1TF_MITIGATION_FULL_FORCE
:
850 cpu_smt_disable(true);
854 #if CONFIG_PGTABLE_LEVELS == 2
855 pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
859 half_pa
= (u64
)l1tf_pfn_limit() << PAGE_SHIFT
;
860 if (e820__mapped_any(half_pa
, ULLONG_MAX
- half_pa
, E820_TYPE_RAM
)) {
861 pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
865 setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV
);
868 static int __init
l1tf_cmdline(char *str
)
870 if (!boot_cpu_has_bug(X86_BUG_L1TF
))
876 if (!strcmp(str
, "off"))
877 l1tf_mitigation
= L1TF_MITIGATION_OFF
;
878 else if (!strcmp(str
, "flush,nowarn"))
879 l1tf_mitigation
= L1TF_MITIGATION_FLUSH_NOWARN
;
880 else if (!strcmp(str
, "flush"))
881 l1tf_mitigation
= L1TF_MITIGATION_FLUSH
;
882 else if (!strcmp(str
, "flush,nosmt"))
883 l1tf_mitigation
= L1TF_MITIGATION_FLUSH_NOSMT
;
884 else if (!strcmp(str
, "full"))
885 l1tf_mitigation
= L1TF_MITIGATION_FULL
;
886 else if (!strcmp(str
, "full,force"))
887 l1tf_mitigation
= L1TF_MITIGATION_FULL_FORCE
;
891 early_param("l1tf", l1tf_cmdline
);
897 #define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
899 #if IS_ENABLED(CONFIG_KVM_INTEL)
900 static const char * const l1tf_vmx_states
[] = {
901 [VMENTER_L1D_FLUSH_AUTO
] = "auto",
902 [VMENTER_L1D_FLUSH_NEVER
] = "vulnerable",
903 [VMENTER_L1D_FLUSH_COND
] = "conditional cache flushes",
904 [VMENTER_L1D_FLUSH_ALWAYS
] = "cache flushes",
905 [VMENTER_L1D_FLUSH_EPT_DISABLED
] = "EPT disabled",
906 [VMENTER_L1D_FLUSH_NOT_REQUIRED
] = "flush not necessary"
909 static ssize_t
l1tf_show_state(char *buf
)
911 if (l1tf_vmx_mitigation
== VMENTER_L1D_FLUSH_AUTO
)
912 return sprintf(buf
, "%s\n", L1TF_DEFAULT_MSG
);
914 if (l1tf_vmx_mitigation
== VMENTER_L1D_FLUSH_EPT_DISABLED
||
915 (l1tf_vmx_mitigation
== VMENTER_L1D_FLUSH_NEVER
&&
916 sched_smt_active())) {
917 return sprintf(buf
, "%s; VMX: %s\n", L1TF_DEFAULT_MSG
,
918 l1tf_vmx_states
[l1tf_vmx_mitigation
]);
921 return sprintf(buf
, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG
,
922 l1tf_vmx_states
[l1tf_vmx_mitigation
],
923 sched_smt_active() ? "vulnerable" : "disabled");
926 static ssize_t
l1tf_show_state(char *buf
)
928 return sprintf(buf
, "%s\n", L1TF_DEFAULT_MSG
);
932 static char *stibp_state(void)
934 if (spectre_v2_enabled
== SPECTRE_V2_IBRS_ENHANCED
)
937 switch (spectre_v2_user
) {
938 case SPECTRE_V2_USER_NONE
:
939 return ", STIBP: disabled";
940 case SPECTRE_V2_USER_STRICT
:
941 return ", STIBP: forced";
946 static char *ibpb_state(void)
948 if (boot_cpu_has(X86_FEATURE_USE_IBPB
))
954 static ssize_t
cpu_show_common(struct device
*dev
, struct device_attribute
*attr
,
955 char *buf
, unsigned int bug
)
957 if (!boot_cpu_has_bug(bug
))
958 return sprintf(buf
, "Not affected\n");
961 case X86_BUG_CPU_MELTDOWN
:
962 if (boot_cpu_has(X86_FEATURE_PTI
))
963 return sprintf(buf
, "Mitigation: PTI\n");
967 case X86_BUG_SPECTRE_V1
:
968 return sprintf(buf
, "Mitigation: __user pointer sanitization\n");
970 case X86_BUG_SPECTRE_V2
:
971 return sprintf(buf
, "%s%s%s%s%s%s\n", spectre_v2_strings
[spectre_v2_enabled
],
973 boot_cpu_has(X86_FEATURE_USE_IBRS_FW
) ? ", IBRS_FW" : "",
975 boot_cpu_has(X86_FEATURE_RSB_CTXSW
) ? ", RSB filling" : "",
976 spectre_v2_module_string());
978 case X86_BUG_SPEC_STORE_BYPASS
:
979 return sprintf(buf
, "%s\n", ssb_strings
[ssb_mode
]);
982 if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV
))
983 return l1tf_show_state(buf
);
989 return sprintf(buf
, "Vulnerable\n");
992 ssize_t
cpu_show_meltdown(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
994 return cpu_show_common(dev
, attr
, buf
, X86_BUG_CPU_MELTDOWN
);
997 ssize_t
cpu_show_spectre_v1(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
999 return cpu_show_common(dev
, attr
, buf
, X86_BUG_SPECTRE_V1
);
1002 ssize_t
cpu_show_spectre_v2(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1004 return cpu_show_common(dev
, attr
, buf
, X86_BUG_SPECTRE_V2
);
1007 ssize_t
cpu_show_spec_store_bypass(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1009 return cpu_show_common(dev
, attr
, buf
, X86_BUG_SPEC_STORE_BYPASS
);
1012 ssize_t
cpu_show_l1tf(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1014 return cpu_show_common(dev
, attr
, buf
, X86_BUG_L1TF
);