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>
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>
32 #include <asm/hypervisor.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);
39 * Our boot-time value of the SPEC_CTRL MSR. We read it once so that any
40 * writes to SPEC_CTRL contain whatever reserved bits have been set.
42 u64 __ro_after_init x86_spec_ctrl_base;
43 EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
46 * The vendor and possibly platform specific bits which can be modified in
49 static u64 __ro_after_init x86_spec_ctrl_mask = SPEC_CTRL_IBRS;
52 * AMD specific MSR info for Speculative Store Bypass control.
53 * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu().
55 u64 __ro_after_init x86_amd_ls_cfg_base;
56 u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask;
58 void __init check_bugs(void)
63 * identify_boot_cpu() initialized SMT support information, let the
66 cpu_smt_check_topology_early();
68 if (!IS_ENABLED(CONFIG_SMP)) {
70 print_cpu_info(&boot_cpu_data);
74 * Read the SPEC_CTRL MSR to account for reserved bits which may
75 * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
76 * init code as it is not enumerated and depends on the family.
78 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
79 rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
81 /* Allow STIBP in MSR_SPEC_CTRL if supported */
82 if (boot_cpu_has(X86_FEATURE_STIBP))
83 x86_spec_ctrl_mask |= SPEC_CTRL_STIBP;
85 /* Select the proper spectre mitigation before patching alternatives */
86 spectre_v2_select_mitigation();
89 * Select proper mitigation for any exposure to the Speculative Store
90 * Bypass vulnerability.
92 ssb_select_mitigation();
94 l1tf_select_mitigation();
98 * Check whether we are able to run this kernel safely on SMP.
100 * - i386 is no longer supported.
101 * - In order to run on anything without a TSC, we need to be
102 * compiled for a i486.
104 if (boot_cpu_data.x86 < 4)
105 panic("Kernel requires i486+ for 'invlpg' and other features");
107 init_utsname()->machine[1] =
108 '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
109 alternative_instructions();
111 fpu__init_check_bugs();
112 #else /* CONFIG_X86_64 */
113 alternative_instructions();
116 * Make sure the first 2MB area is not mapped by huge pages
117 * There are typically fixed size MTRRs in there and overlapping
118 * MTRRs into large pages causes slow downs.
120 * Right now we don't do that with gbpages because there seems
121 * very little benefit for that case.
124 set_memory_4k((unsigned long)__va(0), 1);
128 /* The kernel command line selection */
129 enum spectre_v2_mitigation_cmd {
132 SPECTRE_V2_CMD_FORCE,
133 SPECTRE_V2_CMD_RETPOLINE,
134 SPECTRE_V2_CMD_RETPOLINE_GENERIC,
135 SPECTRE_V2_CMD_RETPOLINE_AMD,
138 static const char *spectre_v2_strings[] = {
139 [SPECTRE_V2_NONE] = "Vulnerable",
140 [SPECTRE_V2_RETPOLINE_MINIMAL] = "Vulnerable: Minimal generic ASM retpoline",
141 [SPECTRE_V2_RETPOLINE_MINIMAL_AMD] = "Vulnerable: Minimal AMD ASM retpoline",
142 [SPECTRE_V2_RETPOLINE_GENERIC] = "Mitigation: Full generic retpoline",
143 [SPECTRE_V2_RETPOLINE_AMD] = "Mitigation: Full AMD retpoline",
144 [SPECTRE_V2_IBRS_ENHANCED] = "Mitigation: Enhanced IBRS",
148 #define pr_fmt(fmt) "Spectre V2 : " fmt
150 static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init =
154 x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest)
156 u64 msrval, guestval, hostval = x86_spec_ctrl_base;
157 struct thread_info *ti = current_thread_info();
159 /* Is MSR_SPEC_CTRL implemented ? */
160 if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) {
162 * Restrict guest_spec_ctrl to supported values. Clear the
163 * modifiable bits in the host base value and or the
164 * modifiable bits from the guest value.
166 guestval = hostval & ~x86_spec_ctrl_mask;
167 guestval |= guest_spec_ctrl & x86_spec_ctrl_mask;
169 /* SSBD controlled in MSR_SPEC_CTRL */
170 if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
171 static_cpu_has(X86_FEATURE_AMD_SSBD))
172 hostval |= ssbd_tif_to_spec_ctrl(ti->flags);
174 if (hostval != guestval) {
175 msrval = setguest ? guestval : hostval;
176 wrmsrl(MSR_IA32_SPEC_CTRL, msrval);
181 * If SSBD is not handled in MSR_SPEC_CTRL on AMD, update
182 * MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported.
184 if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
185 !static_cpu_has(X86_FEATURE_VIRT_SSBD))
189 * If the host has SSBD mitigation enabled, force it in the host's
190 * virtual MSR value. If its not permanently enabled, evaluate
191 * current's TIF_SSBD thread flag.
193 if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE))
194 hostval = SPEC_CTRL_SSBD;
196 hostval = ssbd_tif_to_spec_ctrl(ti->flags);
198 /* Sanitize the guest value */
199 guestval = guest_virt_spec_ctrl & SPEC_CTRL_SSBD;
201 if (hostval != guestval) {
204 tif = setguest ? ssbd_spec_ctrl_to_tif(guestval) :
205 ssbd_spec_ctrl_to_tif(hostval);
207 speculative_store_bypass_update(tif);
210 EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl);
212 static void x86_amd_ssb_disable(void)
214 u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_ssbd_mask;
216 if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
217 wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, SPEC_CTRL_SSBD);
218 else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
219 wrmsrl(MSR_AMD64_LS_CFG, msrval);
223 static bool spectre_v2_bad_module;
225 bool retpoline_module_ok(bool has_retpoline)
227 if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
230 pr_err("System may be vulnerable to spectre v2\n");
231 spectre_v2_bad_module = true;
235 static inline const char *spectre_v2_module_string(void)
237 return spectre_v2_bad_module ? " - vulnerable module loaded" : "";
240 static inline const char *spectre_v2_module_string(void) { return ""; }
243 static void __init spec2_print_if_insecure(const char *reason)
245 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
246 pr_info("%s selected on command line.\n", reason);
249 static void __init spec2_print_if_secure(const char *reason)
251 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
252 pr_info("%s selected on command line.\n", reason);
255 static inline bool retp_compiler(void)
257 return __is_defined(RETPOLINE);
260 static inline bool match_option(const char *arg, int arglen, const char *opt)
262 int len = strlen(opt);
264 return len == arglen && !strncmp(arg, opt, len);
267 static const struct {
269 enum spectre_v2_mitigation_cmd cmd;
271 } mitigation_options[] = {
272 { "off", SPECTRE_V2_CMD_NONE, false },
273 { "on", SPECTRE_V2_CMD_FORCE, true },
274 { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false },
275 { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_AMD, false },
276 { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
277 { "auto", SPECTRE_V2_CMD_AUTO, false },
280 static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
284 enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
286 if (cmdline_find_option_bool(boot_command_line, "nospectre_v2"))
287 return SPECTRE_V2_CMD_NONE;
289 ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
291 return SPECTRE_V2_CMD_AUTO;
293 for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
294 if (!match_option(arg, ret, mitigation_options[i].option))
296 cmd = mitigation_options[i].cmd;
300 if (i >= ARRAY_SIZE(mitigation_options)) {
301 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
302 return SPECTRE_V2_CMD_AUTO;
306 if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
307 cmd == SPECTRE_V2_CMD_RETPOLINE_AMD ||
308 cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC) &&
309 !IS_ENABLED(CONFIG_RETPOLINE)) {
310 pr_err("%s selected but not compiled in. Switching to AUTO select\n", mitigation_options[i].option);
311 return SPECTRE_V2_CMD_AUTO;
314 if (cmd == SPECTRE_V2_CMD_RETPOLINE_AMD &&
315 boot_cpu_data.x86_vendor != X86_VENDOR_HYGON &&
316 boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
317 pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n");
318 return SPECTRE_V2_CMD_AUTO;
321 if (mitigation_options[i].secure)
322 spec2_print_if_secure(mitigation_options[i].option);
324 spec2_print_if_insecure(mitigation_options[i].option);
329 static void __init spectre_v2_select_mitigation(void)
331 enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
332 enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
335 * If the CPU is not affected and the command line mode is NONE or AUTO
336 * then nothing to do.
338 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
339 (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
343 case SPECTRE_V2_CMD_NONE:
346 case SPECTRE_V2_CMD_FORCE:
347 case SPECTRE_V2_CMD_AUTO:
348 if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
349 mode = SPECTRE_V2_IBRS_ENHANCED;
350 /* Force it so VMEXIT will restore correctly */
351 x86_spec_ctrl_base |= SPEC_CTRL_IBRS;
352 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
353 goto specv2_set_mode;
355 if (IS_ENABLED(CONFIG_RETPOLINE))
358 case SPECTRE_V2_CMD_RETPOLINE_AMD:
359 if (IS_ENABLED(CONFIG_RETPOLINE))
362 case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
363 if (IS_ENABLED(CONFIG_RETPOLINE))
364 goto retpoline_generic;
366 case SPECTRE_V2_CMD_RETPOLINE:
367 if (IS_ENABLED(CONFIG_RETPOLINE))
371 pr_err("Spectre mitigation: kernel not compiled with retpoline; no mitigation available!");
375 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
376 boot_cpu_data.x86_vendor == X86_VENDOR_HYGON) {
378 if (!boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
379 pr_err("Spectre mitigation: LFENCE not serializing, switching to generic retpoline\n");
380 goto retpoline_generic;
382 mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_AMD :
383 SPECTRE_V2_RETPOLINE_MINIMAL_AMD;
384 setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD);
385 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
388 mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_GENERIC :
389 SPECTRE_V2_RETPOLINE_MINIMAL;
390 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
394 spectre_v2_enabled = mode;
395 pr_info("%s\n", spectre_v2_strings[mode]);
398 * If spectre v2 protection has been enabled, unconditionally fill
399 * RSB during a context switch; this protects against two independent
402 * - RSB underflow (and switch to BTB) on Skylake+
403 * - SpectreRSB variant of spectre v2 on X86_BUG_SPECTRE_V2 CPUs
405 setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
406 pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n");
408 /* Initialize Indirect Branch Prediction Barrier if supported */
409 if (boot_cpu_has(X86_FEATURE_IBPB)) {
410 setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
411 pr_info("Spectre v2 mitigation: Enabling Indirect Branch Prediction Barrier\n");
415 * Retpoline means the kernel is safe because it has no indirect
416 * branches. Enhanced IBRS protects firmware too, so, enable restricted
417 * speculation around firmware calls only when Enhanced IBRS isn't
420 * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because
421 * the user might select retpoline on the kernel command line and if
422 * the CPU supports Enhanced IBRS, kernel might un-intentionally not
423 * enable IBRS around firmware calls.
425 if (boot_cpu_has(X86_FEATURE_IBRS) && mode != SPECTRE_V2_IBRS_ENHANCED) {
426 setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
427 pr_info("Enabling Restricted Speculation for firmware calls\n");
432 #define pr_fmt(fmt) "Speculative Store Bypass: " fmt
434 static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
436 /* The kernel command line selection */
437 enum ssb_mitigation_cmd {
438 SPEC_STORE_BYPASS_CMD_NONE,
439 SPEC_STORE_BYPASS_CMD_AUTO,
440 SPEC_STORE_BYPASS_CMD_ON,
441 SPEC_STORE_BYPASS_CMD_PRCTL,
442 SPEC_STORE_BYPASS_CMD_SECCOMP,
445 static const char *ssb_strings[] = {
446 [SPEC_STORE_BYPASS_NONE] = "Vulnerable",
447 [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass disabled",
448 [SPEC_STORE_BYPASS_PRCTL] = "Mitigation: Speculative Store Bypass disabled via prctl",
449 [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
452 static const struct {
454 enum ssb_mitigation_cmd cmd;
455 } ssb_mitigation_options[] = {
456 { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */
457 { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */
458 { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */
459 { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative Store Bypass via prctl */
460 { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */
463 static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
465 enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO;
469 if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable")) {
470 return SPEC_STORE_BYPASS_CMD_NONE;
472 ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable",
475 return SPEC_STORE_BYPASS_CMD_AUTO;
477 for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) {
478 if (!match_option(arg, ret, ssb_mitigation_options[i].option))
481 cmd = ssb_mitigation_options[i].cmd;
485 if (i >= ARRAY_SIZE(ssb_mitigation_options)) {
486 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
487 return SPEC_STORE_BYPASS_CMD_AUTO;
494 static enum ssb_mitigation __init __ssb_select_mitigation(void)
496 enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
497 enum ssb_mitigation_cmd cmd;
499 if (!boot_cpu_has(X86_FEATURE_SSBD))
502 cmd = ssb_parse_cmdline();
503 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) &&
504 (cmd == SPEC_STORE_BYPASS_CMD_NONE ||
505 cmd == SPEC_STORE_BYPASS_CMD_AUTO))
509 case SPEC_STORE_BYPASS_CMD_AUTO:
510 case SPEC_STORE_BYPASS_CMD_SECCOMP:
512 * Choose prctl+seccomp as the default mode if seccomp is
515 if (IS_ENABLED(CONFIG_SECCOMP))
516 mode = SPEC_STORE_BYPASS_SECCOMP;
518 mode = SPEC_STORE_BYPASS_PRCTL;
520 case SPEC_STORE_BYPASS_CMD_ON:
521 mode = SPEC_STORE_BYPASS_DISABLE;
523 case SPEC_STORE_BYPASS_CMD_PRCTL:
524 mode = SPEC_STORE_BYPASS_PRCTL;
526 case SPEC_STORE_BYPASS_CMD_NONE:
531 * We have three CPU feature flags that are in play here:
532 * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
533 * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
534 * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
536 if (mode == SPEC_STORE_BYPASS_DISABLE) {
537 setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
539 * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
540 * use a completely different MSR and bit dependent on family.
542 if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
543 !static_cpu_has(X86_FEATURE_AMD_SSBD)) {
544 x86_amd_ssb_disable();
546 x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
547 x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
548 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
555 static void ssb_select_mitigation(void)
557 ssb_mode = __ssb_select_mitigation();
559 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
560 pr_info("%s\n", ssb_strings[ssb_mode]);
564 #define pr_fmt(fmt) "Speculation prctl: " fmt
566 static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
570 if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
571 ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
576 /* If speculation is force disabled, enable is not allowed */
577 if (task_spec_ssb_force_disable(task))
579 task_clear_spec_ssb_disable(task);
580 update = test_and_clear_tsk_thread_flag(task, TIF_SSBD);
582 case PR_SPEC_DISABLE:
583 task_set_spec_ssb_disable(task);
584 update = !test_and_set_tsk_thread_flag(task, TIF_SSBD);
586 case PR_SPEC_FORCE_DISABLE:
587 task_set_spec_ssb_disable(task);
588 task_set_spec_ssb_force_disable(task);
589 update = !test_and_set_tsk_thread_flag(task, TIF_SSBD);
596 * If being set on non-current task, delay setting the CPU
597 * mitigation until it is next scheduled.
599 if (task == current && update)
600 speculative_store_bypass_update_current();
605 int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
609 case PR_SPEC_STORE_BYPASS:
610 return ssb_prctl_set(task, ctrl);
616 #ifdef CONFIG_SECCOMP
617 void arch_seccomp_spec_mitigate(struct task_struct *task)
619 if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP)
620 ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
624 static int ssb_prctl_get(struct task_struct *task)
627 case SPEC_STORE_BYPASS_DISABLE:
628 return PR_SPEC_DISABLE;
629 case SPEC_STORE_BYPASS_SECCOMP:
630 case SPEC_STORE_BYPASS_PRCTL:
631 if (task_spec_ssb_force_disable(task))
632 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
633 if (task_spec_ssb_disable(task))
634 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
635 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
637 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
638 return PR_SPEC_ENABLE;
639 return PR_SPEC_NOT_AFFECTED;
643 int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
646 case PR_SPEC_STORE_BYPASS:
647 return ssb_prctl_get(task);
653 void x86_spec_ctrl_setup_ap(void)
655 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
656 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
658 if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
659 x86_amd_ssb_disable();
663 #define pr_fmt(fmt) "L1TF: " fmt
665 /* Default mitigation for L1TF-affected CPUs */
666 enum l1tf_mitigations l1tf_mitigation __ro_after_init = L1TF_MITIGATION_FLUSH;
667 #if IS_ENABLED(CONFIG_KVM_INTEL)
668 EXPORT_SYMBOL_GPL(l1tf_mitigation);
670 enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
671 EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation);
674 * These CPUs all support 44bits physical address space internally in the
675 * cache but CPUID can report a smaller number of physical address bits.
677 * The L1TF mitigation uses the top most address bit for the inversion of
678 * non present PTEs. When the installed memory reaches into the top most
679 * address bit due to memory holes, which has been observed on machines
680 * which report 36bits physical address bits and have 32G RAM installed,
681 * then the mitigation range check in l1tf_select_mitigation() triggers.
682 * This is a false positive because the mitigation is still possible due to
683 * the fact that the cache uses 44bit internally. Use the cache bits
684 * instead of the reported physical bits and adjust them on the affected
685 * machines to 44bit if the reported bits are less than 44.
687 static void override_cache_bits(struct cpuinfo_x86 *c)
692 switch (c->x86_model) {
693 case INTEL_FAM6_NEHALEM:
694 case INTEL_FAM6_WESTMERE:
695 case INTEL_FAM6_SANDYBRIDGE:
696 case INTEL_FAM6_IVYBRIDGE:
697 case INTEL_FAM6_HASWELL_CORE:
698 case INTEL_FAM6_HASWELL_ULT:
699 case INTEL_FAM6_HASWELL_GT3E:
700 case INTEL_FAM6_BROADWELL_CORE:
701 case INTEL_FAM6_BROADWELL_GT3E:
702 case INTEL_FAM6_SKYLAKE_MOBILE:
703 case INTEL_FAM6_SKYLAKE_DESKTOP:
704 case INTEL_FAM6_KABYLAKE_MOBILE:
705 case INTEL_FAM6_KABYLAKE_DESKTOP:
706 if (c->x86_cache_bits < 44)
707 c->x86_cache_bits = 44;
712 static void __init l1tf_select_mitigation(void)
716 if (!boot_cpu_has_bug(X86_BUG_L1TF))
719 override_cache_bits(&boot_cpu_data);
721 switch (l1tf_mitigation) {
722 case L1TF_MITIGATION_OFF:
723 case L1TF_MITIGATION_FLUSH_NOWARN:
724 case L1TF_MITIGATION_FLUSH:
726 case L1TF_MITIGATION_FLUSH_NOSMT:
727 case L1TF_MITIGATION_FULL:
728 cpu_smt_disable(false);
730 case L1TF_MITIGATION_FULL_FORCE:
731 cpu_smt_disable(true);
735 #if CONFIG_PGTABLE_LEVELS == 2
736 pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
740 half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT;
741 if (e820__mapped_any(half_pa, ULLONG_MAX - half_pa, E820_TYPE_RAM)) {
742 pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
743 pr_info("You may make it effective by booting the kernel with mem=%llu parameter.\n",
745 pr_info("However, doing so will make a part of your RAM unusable.\n");
746 pr_info("Reading https://www.kernel.org/doc/html/latest/admin-guide/l1tf.html might help you decide.\n");
750 setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV);
753 static int __init l1tf_cmdline(char *str)
755 if (!boot_cpu_has_bug(X86_BUG_L1TF))
761 if (!strcmp(str, "off"))
762 l1tf_mitigation = L1TF_MITIGATION_OFF;
763 else if (!strcmp(str, "flush,nowarn"))
764 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOWARN;
765 else if (!strcmp(str, "flush"))
766 l1tf_mitigation = L1TF_MITIGATION_FLUSH;
767 else if (!strcmp(str, "flush,nosmt"))
768 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
769 else if (!strcmp(str, "full"))
770 l1tf_mitigation = L1TF_MITIGATION_FULL;
771 else if (!strcmp(str, "full,force"))
772 l1tf_mitigation = L1TF_MITIGATION_FULL_FORCE;
776 early_param("l1tf", l1tf_cmdline);
782 #define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
784 #if IS_ENABLED(CONFIG_KVM_INTEL)
785 static const char *l1tf_vmx_states[] = {
786 [VMENTER_L1D_FLUSH_AUTO] = "auto",
787 [VMENTER_L1D_FLUSH_NEVER] = "vulnerable",
788 [VMENTER_L1D_FLUSH_COND] = "conditional cache flushes",
789 [VMENTER_L1D_FLUSH_ALWAYS] = "cache flushes",
790 [VMENTER_L1D_FLUSH_EPT_DISABLED] = "EPT disabled",
791 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = "flush not necessary"
794 static ssize_t l1tf_show_state(char *buf)
796 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO)
797 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
799 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_EPT_DISABLED ||
800 (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER &&
801 cpu_smt_control == CPU_SMT_ENABLED))
802 return sprintf(buf, "%s; VMX: %s\n", L1TF_DEFAULT_MSG,
803 l1tf_vmx_states[l1tf_vmx_mitigation]);
805 return sprintf(buf, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG,
806 l1tf_vmx_states[l1tf_vmx_mitigation],
807 cpu_smt_control == CPU_SMT_ENABLED ? "vulnerable" : "disabled");
810 static ssize_t l1tf_show_state(char *buf)
812 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
816 static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
817 char *buf, unsigned int bug)
819 if (!boot_cpu_has_bug(bug))
820 return sprintf(buf, "Not affected\n");
823 case X86_BUG_CPU_MELTDOWN:
824 if (boot_cpu_has(X86_FEATURE_PTI))
825 return sprintf(buf, "Mitigation: PTI\n");
827 if (hypervisor_is_type(X86_HYPER_XEN_PV))
828 return sprintf(buf, "Unknown (XEN PV detected, hypervisor mitigation required)\n");
832 case X86_BUG_SPECTRE_V1:
833 return sprintf(buf, "Mitigation: __user pointer sanitization\n");
835 case X86_BUG_SPECTRE_V2:
836 return sprintf(buf, "%s%s%s%s\n", spectre_v2_strings[spectre_v2_enabled],
837 boot_cpu_has(X86_FEATURE_USE_IBPB) ? ", IBPB" : "",
838 boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
839 spectre_v2_module_string());
841 case X86_BUG_SPEC_STORE_BYPASS:
842 return sprintf(buf, "%s\n", ssb_strings[ssb_mode]);
845 if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV))
846 return l1tf_show_state(buf);
852 return sprintf(buf, "Vulnerable\n");
855 ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
857 return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
860 ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
862 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
865 ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
867 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
870 ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
872 return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS);
875 ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
877 return cpu_show_common(dev, attr, buf, X86_BUG_L1TF);