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),
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>
18 #include <asm/spec-ctrl.h>
19 #include <asm/cmdline.h>
21 #include <asm/processor.h>
22 #include <asm/processor-flags.h>
23 #include <asm/fpu/internal.h>
26 #include <asm/paravirt.h>
27 #include <asm/alternative.h>
28 #include <asm/pgtable.h>
29 #include <asm/set_memory.h>
30 #include <asm/intel-family.h>
31 #include <asm/e820/api.h>
33 static void __init spectre_v2_select_mitigation(void);
34 static void __init ssb_select_mitigation(void);
35 static void __init l1tf_select_mitigation(void);
38 * Our boot-time value of the SPEC_CTRL MSR. We read it once so that any
39 * writes to SPEC_CTRL contain whatever reserved bits have been set.
41 u64 __ro_after_init x86_spec_ctrl_base;
42 EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
45 * The vendor and possibly platform specific bits which can be modified in
48 static u64 __ro_after_init x86_spec_ctrl_mask = SPEC_CTRL_IBRS;
51 * AMD specific MSR info for Speculative Store Bypass control.
52 * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu().
54 u64 __ro_after_init x86_amd_ls_cfg_base;
55 u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask;
57 void __init check_bugs(void)
62 * identify_boot_cpu() initialized SMT support information, let the
65 cpu_smt_check_topology();
67 if (!IS_ENABLED(CONFIG_SMP)) {
69 print_cpu_info(&boot_cpu_data);
73 * Read the SPEC_CTRL MSR to account for reserved bits which may
74 * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
75 * init code as it is not enumerated and depends on the family.
77 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
78 rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
80 /* Allow STIBP in MSR_SPEC_CTRL if supported */
81 if (boot_cpu_has(X86_FEATURE_STIBP))
82 x86_spec_ctrl_mask |= SPEC_CTRL_STIBP;
84 /* Select the proper spectre mitigation before patching alternatives */
85 spectre_v2_select_mitigation();
88 * Select proper mitigation for any exposure to the Speculative Store
89 * Bypass vulnerability.
91 ssb_select_mitigation();
93 l1tf_select_mitigation();
97 * Check whether we are able to run this kernel safely on SMP.
99 * - i386 is no longer supported.
100 * - In order to run on anything without a TSC, we need to be
101 * compiled for a i486.
103 if (boot_cpu_data.x86 < 4)
104 panic("Kernel requires i486+ for 'invlpg' and other features");
106 init_utsname()->machine[1] =
107 '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
108 alternative_instructions();
110 fpu__init_check_bugs();
111 #else /* CONFIG_X86_64 */
112 alternative_instructions();
115 * Make sure the first 2MB area is not mapped by huge pages
116 * There are typically fixed size MTRRs in there and overlapping
117 * MTRRs into large pages causes slow downs.
119 * Right now we don't do that with gbpages because there seems
120 * very little benefit for that case.
123 set_memory_4k((unsigned long)__va(0), 1);
127 /* The kernel command line selection */
128 enum spectre_v2_mitigation_cmd {
131 SPECTRE_V2_CMD_FORCE,
132 SPECTRE_V2_CMD_RETPOLINE,
133 SPECTRE_V2_CMD_RETPOLINE_GENERIC,
134 SPECTRE_V2_CMD_RETPOLINE_AMD,
137 static const char *spectre_v2_strings[] = {
138 [SPECTRE_V2_NONE] = "Vulnerable",
139 [SPECTRE_V2_RETPOLINE_MINIMAL] = "Vulnerable: Minimal generic ASM retpoline",
140 [SPECTRE_V2_RETPOLINE_MINIMAL_AMD] = "Vulnerable: Minimal AMD ASM retpoline",
141 [SPECTRE_V2_RETPOLINE_GENERIC] = "Mitigation: Full generic retpoline",
142 [SPECTRE_V2_RETPOLINE_AMD] = "Mitigation: Full AMD retpoline",
146 #define pr_fmt(fmt) "Spectre V2 : " fmt
148 static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init =
152 x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest)
154 u64 msrval, guestval, hostval = x86_spec_ctrl_base;
155 struct thread_info *ti = current_thread_info();
157 /* Is MSR_SPEC_CTRL implemented ? */
158 if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) {
160 * Restrict guest_spec_ctrl to supported values. Clear the
161 * modifiable bits in the host base value and or the
162 * modifiable bits from the guest value.
164 guestval = hostval & ~x86_spec_ctrl_mask;
165 guestval |= guest_spec_ctrl & x86_spec_ctrl_mask;
167 /* SSBD controlled in MSR_SPEC_CTRL */
168 if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
169 hostval |= ssbd_tif_to_spec_ctrl(ti->flags);
171 if (hostval != guestval) {
172 msrval = setguest ? guestval : hostval;
173 wrmsrl(MSR_IA32_SPEC_CTRL, msrval);
178 * If SSBD is not handled in MSR_SPEC_CTRL on AMD, update
179 * MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported.
181 if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
182 !static_cpu_has(X86_FEATURE_VIRT_SSBD))
186 * If the host has SSBD mitigation enabled, force it in the host's
187 * virtual MSR value. If its not permanently enabled, evaluate
188 * current's TIF_SSBD thread flag.
190 if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE))
191 hostval = SPEC_CTRL_SSBD;
193 hostval = ssbd_tif_to_spec_ctrl(ti->flags);
195 /* Sanitize the guest value */
196 guestval = guest_virt_spec_ctrl & SPEC_CTRL_SSBD;
198 if (hostval != guestval) {
201 tif = setguest ? ssbd_spec_ctrl_to_tif(guestval) :
202 ssbd_spec_ctrl_to_tif(hostval);
204 speculative_store_bypass_update(tif);
207 EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl);
209 static void x86_amd_ssb_disable(void)
211 u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_ssbd_mask;
213 if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
214 wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, SPEC_CTRL_SSBD);
215 else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
216 wrmsrl(MSR_AMD64_LS_CFG, msrval);
220 static bool spectre_v2_bad_module;
222 bool retpoline_module_ok(bool has_retpoline)
224 if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
227 pr_err("System may be vulnerable to spectre v2\n");
228 spectre_v2_bad_module = true;
232 static inline const char *spectre_v2_module_string(void)
234 return spectre_v2_bad_module ? " - vulnerable module loaded" : "";
237 static inline const char *spectre_v2_module_string(void) { return ""; }
240 static void __init spec2_print_if_insecure(const char *reason)
242 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
243 pr_info("%s selected on command line.\n", reason);
246 static void __init spec2_print_if_secure(const char *reason)
248 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
249 pr_info("%s selected on command line.\n", reason);
252 static inline bool retp_compiler(void)
254 return __is_defined(RETPOLINE);
257 static inline bool match_option(const char *arg, int arglen, const char *opt)
259 int len = strlen(opt);
261 return len == arglen && !strncmp(arg, opt, len);
264 static const struct {
266 enum spectre_v2_mitigation_cmd cmd;
268 } mitigation_options[] = {
269 { "off", SPECTRE_V2_CMD_NONE, false },
270 { "on", SPECTRE_V2_CMD_FORCE, true },
271 { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false },
272 { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_AMD, false },
273 { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
274 { "auto", SPECTRE_V2_CMD_AUTO, false },
277 static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
281 enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
283 if (cmdline_find_option_bool(boot_command_line, "nospectre_v2"))
284 return SPECTRE_V2_CMD_NONE;
286 ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
288 return SPECTRE_V2_CMD_AUTO;
290 for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
291 if (!match_option(arg, ret, mitigation_options[i].option))
293 cmd = mitigation_options[i].cmd;
297 if (i >= ARRAY_SIZE(mitigation_options)) {
298 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
299 return SPECTRE_V2_CMD_AUTO;
303 if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
304 cmd == SPECTRE_V2_CMD_RETPOLINE_AMD ||
305 cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC) &&
306 !IS_ENABLED(CONFIG_RETPOLINE)) {
307 pr_err("%s selected but not compiled in. Switching to AUTO select\n", mitigation_options[i].option);
308 return SPECTRE_V2_CMD_AUTO;
311 if (cmd == SPECTRE_V2_CMD_RETPOLINE_AMD &&
312 boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
313 pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n");
314 return SPECTRE_V2_CMD_AUTO;
317 if (mitigation_options[i].secure)
318 spec2_print_if_secure(mitigation_options[i].option);
320 spec2_print_if_insecure(mitigation_options[i].option);
325 /* Check for Skylake-like CPUs (for RSB handling) */
326 static bool __init is_skylake_era(void)
328 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
329 boot_cpu_data.x86 == 6) {
330 switch (boot_cpu_data.x86_model) {
331 case INTEL_FAM6_SKYLAKE_MOBILE:
332 case INTEL_FAM6_SKYLAKE_DESKTOP:
333 case INTEL_FAM6_SKYLAKE_X:
334 case INTEL_FAM6_KABYLAKE_MOBILE:
335 case INTEL_FAM6_KABYLAKE_DESKTOP:
342 static void __init spectre_v2_select_mitigation(void)
344 enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
345 enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
348 * If the CPU is not affected and the command line mode is NONE or AUTO
349 * then nothing to do.
351 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
352 (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
356 case SPECTRE_V2_CMD_NONE:
359 case SPECTRE_V2_CMD_FORCE:
360 case SPECTRE_V2_CMD_AUTO:
361 if (IS_ENABLED(CONFIG_RETPOLINE))
364 case SPECTRE_V2_CMD_RETPOLINE_AMD:
365 if (IS_ENABLED(CONFIG_RETPOLINE))
368 case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
369 if (IS_ENABLED(CONFIG_RETPOLINE))
370 goto retpoline_generic;
372 case SPECTRE_V2_CMD_RETPOLINE:
373 if (IS_ENABLED(CONFIG_RETPOLINE))
377 pr_err("Spectre mitigation: kernel not compiled with retpoline; no mitigation available!");
381 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
383 if (!boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
384 pr_err("Spectre mitigation: LFENCE not serializing, switching to generic retpoline\n");
385 goto retpoline_generic;
387 mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_AMD :
388 SPECTRE_V2_RETPOLINE_MINIMAL_AMD;
389 setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD);
390 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
393 mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_GENERIC :
394 SPECTRE_V2_RETPOLINE_MINIMAL;
395 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
398 spectre_v2_enabled = mode;
399 pr_info("%s\n", spectre_v2_strings[mode]);
402 * If neither SMEP nor PTI are available, there is a risk of
403 * hitting userspace addresses in the RSB after a context switch
404 * from a shallow call stack to a deeper one. To prevent this fill
405 * the entire RSB, even when using IBRS.
407 * Skylake era CPUs have a separate issue with *underflow* of the
408 * RSB, when they will predict 'ret' targets from the generic BTB.
409 * The proper mitigation for this is IBRS. If IBRS is not supported
410 * or deactivated in favour of retpolines the RSB fill on context
411 * switch is required.
413 if ((!boot_cpu_has(X86_FEATURE_PTI) &&
414 !boot_cpu_has(X86_FEATURE_SMEP)) || is_skylake_era()) {
415 setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
416 pr_info("Spectre v2 mitigation: Filling RSB on context switch\n");
419 /* Initialize Indirect Branch Prediction Barrier if supported */
420 if (boot_cpu_has(X86_FEATURE_IBPB)) {
421 setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
422 pr_info("Spectre v2 mitigation: Enabling Indirect Branch Prediction Barrier\n");
426 * Retpoline means the kernel is safe because it has no indirect
427 * branches. But firmware isn't, so use IBRS to protect that.
429 if (boot_cpu_has(X86_FEATURE_IBRS)) {
430 setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
431 pr_info("Enabling Restricted Speculation for firmware calls\n");
436 #define pr_fmt(fmt) "Speculative Store Bypass: " fmt
438 static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
440 /* The kernel command line selection */
441 enum ssb_mitigation_cmd {
442 SPEC_STORE_BYPASS_CMD_NONE,
443 SPEC_STORE_BYPASS_CMD_AUTO,
444 SPEC_STORE_BYPASS_CMD_ON,
445 SPEC_STORE_BYPASS_CMD_PRCTL,
446 SPEC_STORE_BYPASS_CMD_SECCOMP,
449 static const char *ssb_strings[] = {
450 [SPEC_STORE_BYPASS_NONE] = "Vulnerable",
451 [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass disabled",
452 [SPEC_STORE_BYPASS_PRCTL] = "Mitigation: Speculative Store Bypass disabled via prctl",
453 [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
456 static const struct {
458 enum ssb_mitigation_cmd cmd;
459 } ssb_mitigation_options[] = {
460 { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */
461 { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */
462 { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */
463 { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative Store Bypass via prctl */
464 { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */
467 static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
469 enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO;
473 if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable")) {
474 return SPEC_STORE_BYPASS_CMD_NONE;
476 ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable",
479 return SPEC_STORE_BYPASS_CMD_AUTO;
481 for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) {
482 if (!match_option(arg, ret, ssb_mitigation_options[i].option))
485 cmd = ssb_mitigation_options[i].cmd;
489 if (i >= ARRAY_SIZE(ssb_mitigation_options)) {
490 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
491 return SPEC_STORE_BYPASS_CMD_AUTO;
498 static enum ssb_mitigation __init __ssb_select_mitigation(void)
500 enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
501 enum ssb_mitigation_cmd cmd;
503 if (!boot_cpu_has(X86_FEATURE_SSBD))
506 cmd = ssb_parse_cmdline();
507 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) &&
508 (cmd == SPEC_STORE_BYPASS_CMD_NONE ||
509 cmd == SPEC_STORE_BYPASS_CMD_AUTO))
513 case SPEC_STORE_BYPASS_CMD_AUTO:
514 case SPEC_STORE_BYPASS_CMD_SECCOMP:
516 * Choose prctl+seccomp as the default mode if seccomp is
519 if (IS_ENABLED(CONFIG_SECCOMP))
520 mode = SPEC_STORE_BYPASS_SECCOMP;
522 mode = SPEC_STORE_BYPASS_PRCTL;
524 case SPEC_STORE_BYPASS_CMD_ON:
525 mode = SPEC_STORE_BYPASS_DISABLE;
527 case SPEC_STORE_BYPASS_CMD_PRCTL:
528 mode = SPEC_STORE_BYPASS_PRCTL;
530 case SPEC_STORE_BYPASS_CMD_NONE:
535 * We have three CPU feature flags that are in play here:
536 * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
537 * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
538 * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
540 if (mode == SPEC_STORE_BYPASS_DISABLE) {
541 setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
543 * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
544 * use a completely different MSR and bit dependent on family.
546 if (!static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
547 x86_amd_ssb_disable();
549 x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
550 x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
551 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
558 static void ssb_select_mitigation(void)
560 ssb_mode = __ssb_select_mitigation();
562 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
563 pr_info("%s\n", ssb_strings[ssb_mode]);
567 #define pr_fmt(fmt) "Speculation prctl: " fmt
569 static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
573 if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
574 ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
579 /* If speculation is force disabled, enable is not allowed */
580 if (task_spec_ssb_force_disable(task))
582 task_clear_spec_ssb_disable(task);
583 update = test_and_clear_tsk_thread_flag(task, TIF_SSBD);
585 case PR_SPEC_DISABLE:
586 task_set_spec_ssb_disable(task);
587 update = !test_and_set_tsk_thread_flag(task, TIF_SSBD);
589 case PR_SPEC_FORCE_DISABLE:
590 task_set_spec_ssb_disable(task);
591 task_set_spec_ssb_force_disable(task);
592 update = !test_and_set_tsk_thread_flag(task, TIF_SSBD);
599 * If being set on non-current task, delay setting the CPU
600 * mitigation until it is next scheduled.
602 if (task == current && update)
603 speculative_store_bypass_update_current();
608 int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
612 case PR_SPEC_STORE_BYPASS:
613 return ssb_prctl_set(task, ctrl);
619 #ifdef CONFIG_SECCOMP
620 void arch_seccomp_spec_mitigate(struct task_struct *task)
622 if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP)
623 ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
627 static int ssb_prctl_get(struct task_struct *task)
630 case SPEC_STORE_BYPASS_DISABLE:
631 return PR_SPEC_DISABLE;
632 case SPEC_STORE_BYPASS_SECCOMP:
633 case SPEC_STORE_BYPASS_PRCTL:
634 if (task_spec_ssb_force_disable(task))
635 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
636 if (task_spec_ssb_disable(task))
637 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
638 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
640 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
641 return PR_SPEC_ENABLE;
642 return PR_SPEC_NOT_AFFECTED;
646 int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
649 case PR_SPEC_STORE_BYPASS:
650 return ssb_prctl_get(task);
656 void x86_spec_ctrl_setup_ap(void)
658 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
659 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
661 if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
662 x86_amd_ssb_disable();
666 #define pr_fmt(fmt) "L1TF: " fmt
668 /* Default mitigation for L1TF-affected CPUs */
669 enum l1tf_mitigations l1tf_mitigation __ro_after_init = L1TF_MITIGATION_FLUSH;
670 #if IS_ENABLED(CONFIG_KVM_INTEL)
671 EXPORT_SYMBOL_GPL(l1tf_mitigation);
673 enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
674 EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation);
677 static void __init l1tf_select_mitigation(void)
681 if (!boot_cpu_has_bug(X86_BUG_L1TF))
684 switch (l1tf_mitigation) {
685 case L1TF_MITIGATION_OFF:
686 case L1TF_MITIGATION_FLUSH_NOWARN:
687 case L1TF_MITIGATION_FLUSH:
689 case L1TF_MITIGATION_FLUSH_NOSMT:
690 case L1TF_MITIGATION_FULL:
691 cpu_smt_disable(false);
693 case L1TF_MITIGATION_FULL_FORCE:
694 cpu_smt_disable(true);
698 #if CONFIG_PGTABLE_LEVELS == 2
699 pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
704 * This is extremely unlikely to happen because almost all
705 * systems have far more MAX_PA/2 than RAM can be fit into
708 half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT;
709 if (e820__mapped_any(half_pa, ULLONG_MAX - half_pa, E820_TYPE_RAM)) {
710 pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
714 setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV);
717 static int __init l1tf_cmdline(char *str)
719 if (!boot_cpu_has_bug(X86_BUG_L1TF))
725 if (!strcmp(str, "off"))
726 l1tf_mitigation = L1TF_MITIGATION_OFF;
727 else if (!strcmp(str, "flush,nowarn"))
728 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOWARN;
729 else if (!strcmp(str, "flush"))
730 l1tf_mitigation = L1TF_MITIGATION_FLUSH;
731 else if (!strcmp(str, "flush,nosmt"))
732 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
733 else if (!strcmp(str, "full"))
734 l1tf_mitigation = L1TF_MITIGATION_FULL;
735 else if (!strcmp(str, "full,force"))
736 l1tf_mitigation = L1TF_MITIGATION_FULL_FORCE;
740 early_param("l1tf", l1tf_cmdline);
746 #define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
748 #if IS_ENABLED(CONFIG_KVM_INTEL)
749 static const char *l1tf_vmx_states[] = {
750 [VMENTER_L1D_FLUSH_AUTO] = "auto",
751 [VMENTER_L1D_FLUSH_NEVER] = "vulnerable",
752 [VMENTER_L1D_FLUSH_COND] = "conditional cache flushes",
753 [VMENTER_L1D_FLUSH_ALWAYS] = "cache flushes",
754 [VMENTER_L1D_FLUSH_EPT_DISABLED] = "EPT disabled",
757 static ssize_t l1tf_show_state(char *buf)
759 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO)
760 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
762 return sprintf(buf, "%s; VMX: SMT %s, L1D %s\n", L1TF_DEFAULT_MSG,
763 cpu_smt_control == CPU_SMT_ENABLED ? "vulnerable" : "disabled",
764 l1tf_vmx_states[l1tf_vmx_mitigation]);
767 static ssize_t l1tf_show_state(char *buf)
769 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
773 static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
774 char *buf, unsigned int bug)
776 if (!boot_cpu_has_bug(bug))
777 return sprintf(buf, "Not affected\n");
780 case X86_BUG_CPU_MELTDOWN:
781 if (boot_cpu_has(X86_FEATURE_PTI))
782 return sprintf(buf, "Mitigation: PTI\n");
786 case X86_BUG_SPECTRE_V1:
787 return sprintf(buf, "Mitigation: __user pointer sanitization\n");
789 case X86_BUG_SPECTRE_V2:
790 return sprintf(buf, "%s%s%s%s\n", spectre_v2_strings[spectre_v2_enabled],
791 boot_cpu_has(X86_FEATURE_USE_IBPB) ? ", IBPB" : "",
792 boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
793 spectre_v2_module_string());
795 case X86_BUG_SPEC_STORE_BYPASS:
796 return sprintf(buf, "%s\n", ssb_strings[ssb_mode]);
799 if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV))
800 return l1tf_show_state(buf);
806 return sprintf(buf, "Vulnerable\n");
809 ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
811 return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
814 ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
816 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
819 ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
821 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
824 ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
826 return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS);
829 ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
831 return cpu_show_common(dev, attr, buf, X86_BUG_L1TF);