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>
16 #include <asm/nospec-branch.h>
17 #include <asm/cmdline.h>
19 #include <asm/processor.h>
20 #include <asm/processor-flags.h>
21 #include <asm/fpu/internal.h>
23 #include <asm/paravirt.h>
24 #include <asm/alternative.h>
25 #include <asm/pgtable.h>
26 #include <asm/set_memory.h>
27 #include <asm/intel-family.h>
29 static void __init
spectre_v2_select_mitigation(void);
30 static void __init
ssb_select_mitigation(void);
33 * Our boot-time value of the SPEC_CTRL MSR. We read it once so that any
34 * writes to SPEC_CTRL contain whatever reserved bits have been set.
36 static u64 __ro_after_init x86_spec_ctrl_base
;
38 void __init
check_bugs(void)
42 if (!IS_ENABLED(CONFIG_SMP
)) {
44 print_cpu_info(&boot_cpu_data
);
48 * Read the SPEC_CTRL MSR to account for reserved bits which may
49 * have unknown values.
51 if (boot_cpu_has(X86_FEATURE_IBRS
))
52 rdmsrl(MSR_IA32_SPEC_CTRL
, x86_spec_ctrl_base
);
54 /* Select the proper spectre mitigation before patching alternatives */
55 spectre_v2_select_mitigation();
58 * Select proper mitigation for any exposure to the Speculative Store
59 * Bypass vulnerability.
61 ssb_select_mitigation();
65 * Check whether we are able to run this kernel safely on SMP.
67 * - i386 is no longer supported.
68 * - In order to run on anything without a TSC, we need to be
69 * compiled for a i486.
71 if (boot_cpu_data
.x86
< 4)
72 panic("Kernel requires i486+ for 'invlpg' and other features");
74 init_utsname()->machine
[1] =
75 '0' + (boot_cpu_data
.x86
> 6 ? 6 : boot_cpu_data
.x86
);
76 alternative_instructions();
78 fpu__init_check_bugs();
79 #else /* CONFIG_X86_64 */
80 alternative_instructions();
83 * Make sure the first 2MB area is not mapped by huge pages
84 * There are typically fixed size MTRRs in there and overlapping
85 * MTRRs into large pages causes slow downs.
87 * Right now we don't do that with gbpages because there seems
88 * very little benefit for that case.
91 set_memory_4k((unsigned long)__va(0), 1);
95 /* The kernel command line selection */
96 enum spectre_v2_mitigation_cmd
{
100 SPECTRE_V2_CMD_RETPOLINE
,
101 SPECTRE_V2_CMD_RETPOLINE_GENERIC
,
102 SPECTRE_V2_CMD_RETPOLINE_AMD
,
105 static const char *spectre_v2_strings
[] = {
106 [SPECTRE_V2_NONE
] = "Vulnerable",
107 [SPECTRE_V2_RETPOLINE_MINIMAL
] = "Vulnerable: Minimal generic ASM retpoline",
108 [SPECTRE_V2_RETPOLINE_MINIMAL_AMD
] = "Vulnerable: Minimal AMD ASM retpoline",
109 [SPECTRE_V2_RETPOLINE_GENERIC
] = "Mitigation: Full generic retpoline",
110 [SPECTRE_V2_RETPOLINE_AMD
] = "Mitigation: Full AMD retpoline",
114 #define pr_fmt(fmt) "Spectre V2 : " fmt
116 static enum spectre_v2_mitigation spectre_v2_enabled
= SPECTRE_V2_NONE
;
118 void x86_spec_ctrl_set(u64 val
)
120 if (val
& ~(SPEC_CTRL_IBRS
| SPEC_CTRL_RDS
))
121 WARN_ONCE(1, "SPEC_CTRL MSR value 0x%16llx is unknown.\n", val
);
123 wrmsrl(MSR_IA32_SPEC_CTRL
, x86_spec_ctrl_base
| val
);
125 EXPORT_SYMBOL_GPL(x86_spec_ctrl_set
);
127 u64
x86_spec_ctrl_get_default(void)
129 return x86_spec_ctrl_base
;
131 EXPORT_SYMBOL_GPL(x86_spec_ctrl_get_default
);
133 void x86_spec_ctrl_set_guest(u64 guest_spec_ctrl
)
135 if (!boot_cpu_has(X86_FEATURE_IBRS
))
137 if (x86_spec_ctrl_base
!= guest_spec_ctrl
)
138 wrmsrl(MSR_IA32_SPEC_CTRL
, guest_spec_ctrl
);
140 EXPORT_SYMBOL_GPL(x86_spec_ctrl_set_guest
);
142 void x86_spec_ctrl_restore_host(u64 guest_spec_ctrl
)
144 if (!boot_cpu_has(X86_FEATURE_IBRS
))
146 if (x86_spec_ctrl_base
!= guest_spec_ctrl
)
147 wrmsrl(MSR_IA32_SPEC_CTRL
, x86_spec_ctrl_base
);
149 EXPORT_SYMBOL_GPL(x86_spec_ctrl_restore_host
);
152 static bool spectre_v2_bad_module
;
154 bool retpoline_module_ok(bool has_retpoline
)
156 if (spectre_v2_enabled
== SPECTRE_V2_NONE
|| has_retpoline
)
159 pr_err("System may be vulnerable to spectre v2\n");
160 spectre_v2_bad_module
= true;
164 static inline const char *spectre_v2_module_string(void)
166 return spectre_v2_bad_module
? " - vulnerable module loaded" : "";
169 static inline const char *spectre_v2_module_string(void) { return ""; }
172 static void __init
spec2_print_if_insecure(const char *reason
)
174 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2
))
175 pr_info("%s selected on command line.\n", reason
);
178 static void __init
spec2_print_if_secure(const char *reason
)
180 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2
))
181 pr_info("%s selected on command line.\n", reason
);
184 static inline bool retp_compiler(void)
186 return __is_defined(RETPOLINE
);
189 static inline bool match_option(const char *arg
, int arglen
, const char *opt
)
191 int len
= strlen(opt
);
193 return len
== arglen
&& !strncmp(arg
, opt
, len
);
196 static const struct {
198 enum spectre_v2_mitigation_cmd cmd
;
200 } mitigation_options
[] = {
201 { "off", SPECTRE_V2_CMD_NONE
, false },
202 { "on", SPECTRE_V2_CMD_FORCE
, true },
203 { "retpoline", SPECTRE_V2_CMD_RETPOLINE
, false },
204 { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_AMD
, false },
205 { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC
, false },
206 { "auto", SPECTRE_V2_CMD_AUTO
, false },
209 static enum spectre_v2_mitigation_cmd __init
spectre_v2_parse_cmdline(void)
213 enum spectre_v2_mitigation_cmd cmd
= SPECTRE_V2_CMD_AUTO
;
215 if (cmdline_find_option_bool(boot_command_line
, "nospectre_v2"))
216 return SPECTRE_V2_CMD_NONE
;
218 ret
= cmdline_find_option(boot_command_line
, "spectre_v2", arg
, sizeof(arg
));
220 return SPECTRE_V2_CMD_AUTO
;
222 for (i
= 0; i
< ARRAY_SIZE(mitigation_options
); i
++) {
223 if (!match_option(arg
, ret
, mitigation_options
[i
].option
))
225 cmd
= mitigation_options
[i
].cmd
;
229 if (i
>= ARRAY_SIZE(mitigation_options
)) {
230 pr_err("unknown option (%s). Switching to AUTO select\n", arg
);
231 return SPECTRE_V2_CMD_AUTO
;
235 if ((cmd
== SPECTRE_V2_CMD_RETPOLINE
||
236 cmd
== SPECTRE_V2_CMD_RETPOLINE_AMD
||
237 cmd
== SPECTRE_V2_CMD_RETPOLINE_GENERIC
) &&
238 !IS_ENABLED(CONFIG_RETPOLINE
)) {
239 pr_err("%s selected but not compiled in. Switching to AUTO select\n", mitigation_options
[i
].option
);
240 return SPECTRE_V2_CMD_AUTO
;
243 if (cmd
== SPECTRE_V2_CMD_RETPOLINE_AMD
&&
244 boot_cpu_data
.x86_vendor
!= X86_VENDOR_AMD
) {
245 pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n");
246 return SPECTRE_V2_CMD_AUTO
;
249 if (mitigation_options
[i
].secure
)
250 spec2_print_if_secure(mitigation_options
[i
].option
);
252 spec2_print_if_insecure(mitigation_options
[i
].option
);
257 /* Check for Skylake-like CPUs (for RSB handling) */
258 static bool __init
is_skylake_era(void)
260 if (boot_cpu_data
.x86_vendor
== X86_VENDOR_INTEL
&&
261 boot_cpu_data
.x86
== 6) {
262 switch (boot_cpu_data
.x86_model
) {
263 case INTEL_FAM6_SKYLAKE_MOBILE
:
264 case INTEL_FAM6_SKYLAKE_DESKTOP
:
265 case INTEL_FAM6_SKYLAKE_X
:
266 case INTEL_FAM6_KABYLAKE_MOBILE
:
267 case INTEL_FAM6_KABYLAKE_DESKTOP
:
274 static void __init
spectre_v2_select_mitigation(void)
276 enum spectre_v2_mitigation_cmd cmd
= spectre_v2_parse_cmdline();
277 enum spectre_v2_mitigation mode
= SPECTRE_V2_NONE
;
280 * If the CPU is not affected and the command line mode is NONE or AUTO
281 * then nothing to do.
283 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2
) &&
284 (cmd
== SPECTRE_V2_CMD_NONE
|| cmd
== SPECTRE_V2_CMD_AUTO
))
288 case SPECTRE_V2_CMD_NONE
:
291 case SPECTRE_V2_CMD_FORCE
:
292 case SPECTRE_V2_CMD_AUTO
:
293 if (IS_ENABLED(CONFIG_RETPOLINE
))
296 case SPECTRE_V2_CMD_RETPOLINE_AMD
:
297 if (IS_ENABLED(CONFIG_RETPOLINE
))
300 case SPECTRE_V2_CMD_RETPOLINE_GENERIC
:
301 if (IS_ENABLED(CONFIG_RETPOLINE
))
302 goto retpoline_generic
;
304 case SPECTRE_V2_CMD_RETPOLINE
:
305 if (IS_ENABLED(CONFIG_RETPOLINE
))
309 pr_err("Spectre mitigation: kernel not compiled with retpoline; no mitigation available!");
313 if (boot_cpu_data
.x86_vendor
== X86_VENDOR_AMD
) {
315 if (!boot_cpu_has(X86_FEATURE_LFENCE_RDTSC
)) {
316 pr_err("Spectre mitigation: LFENCE not serializing, switching to generic retpoline\n");
317 goto retpoline_generic
;
319 mode
= retp_compiler() ? SPECTRE_V2_RETPOLINE_AMD
:
320 SPECTRE_V2_RETPOLINE_MINIMAL_AMD
;
321 setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD
);
322 setup_force_cpu_cap(X86_FEATURE_RETPOLINE
);
325 mode
= retp_compiler() ? SPECTRE_V2_RETPOLINE_GENERIC
:
326 SPECTRE_V2_RETPOLINE_MINIMAL
;
327 setup_force_cpu_cap(X86_FEATURE_RETPOLINE
);
330 spectre_v2_enabled
= mode
;
331 pr_info("%s\n", spectre_v2_strings
[mode
]);
334 * If neither SMEP nor PTI are available, there is a risk of
335 * hitting userspace addresses in the RSB after a context switch
336 * from a shallow call stack to a deeper one. To prevent this fill
337 * the entire RSB, even when using IBRS.
339 * Skylake era CPUs have a separate issue with *underflow* of the
340 * RSB, when they will predict 'ret' targets from the generic BTB.
341 * The proper mitigation for this is IBRS. If IBRS is not supported
342 * or deactivated in favour of retpolines the RSB fill on context
343 * switch is required.
345 if ((!boot_cpu_has(X86_FEATURE_PTI
) &&
346 !boot_cpu_has(X86_FEATURE_SMEP
)) || is_skylake_era()) {
347 setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW
);
348 pr_info("Spectre v2 mitigation: Filling RSB on context switch\n");
351 /* Initialize Indirect Branch Prediction Barrier if supported */
352 if (boot_cpu_has(X86_FEATURE_IBPB
)) {
353 setup_force_cpu_cap(X86_FEATURE_USE_IBPB
);
354 pr_info("Spectre v2 mitigation: Enabling Indirect Branch Prediction Barrier\n");
358 * Retpoline means the kernel is safe because it has no indirect
359 * branches. But firmware isn't, so use IBRS to protect that.
361 if (boot_cpu_has(X86_FEATURE_IBRS
)) {
362 setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW
);
363 pr_info("Enabling Restricted Speculation for firmware calls\n");
368 #define pr_fmt(fmt) "Speculative Store Bypass: " fmt
370 static enum ssb_mitigation ssb_mode
= SPEC_STORE_BYPASS_NONE
;
372 /* The kernel command line selection */
373 enum ssb_mitigation_cmd
{
374 SPEC_STORE_BYPASS_CMD_NONE
,
375 SPEC_STORE_BYPASS_CMD_AUTO
,
376 SPEC_STORE_BYPASS_CMD_ON
,
379 static const char *ssb_strings
[] = {
380 [SPEC_STORE_BYPASS_NONE
] = "Vulnerable",
381 [SPEC_STORE_BYPASS_DISABLE
] = "Mitigation: Speculative Store Bypass disabled"
384 static const struct {
386 enum ssb_mitigation_cmd cmd
;
387 } ssb_mitigation_options
[] = {
388 { "auto", SPEC_STORE_BYPASS_CMD_AUTO
}, /* Platform decides */
389 { "on", SPEC_STORE_BYPASS_CMD_ON
}, /* Disable Speculative Store Bypass */
390 { "off", SPEC_STORE_BYPASS_CMD_NONE
}, /* Don't touch Speculative Store Bypass */
393 static enum ssb_mitigation_cmd __init
ssb_parse_cmdline(void)
395 enum ssb_mitigation_cmd cmd
= SPEC_STORE_BYPASS_CMD_AUTO
;
399 if (cmdline_find_option_bool(boot_command_line
, "nospec_store_bypass_disable")) {
400 return SPEC_STORE_BYPASS_CMD_NONE
;
402 ret
= cmdline_find_option(boot_command_line
, "spec_store_bypass_disable",
405 return SPEC_STORE_BYPASS_CMD_AUTO
;
407 for (i
= 0; i
< ARRAY_SIZE(ssb_mitigation_options
); i
++) {
408 if (!match_option(arg
, ret
, ssb_mitigation_options
[i
].option
))
411 cmd
= ssb_mitigation_options
[i
].cmd
;
415 if (i
>= ARRAY_SIZE(ssb_mitigation_options
)) {
416 pr_err("unknown option (%s). Switching to AUTO select\n", arg
);
417 return SPEC_STORE_BYPASS_CMD_AUTO
;
424 static enum ssb_mitigation_cmd __init
__ssb_select_mitigation(void)
426 enum ssb_mitigation mode
= SPEC_STORE_BYPASS_NONE
;
427 enum ssb_mitigation_cmd cmd
;
429 if (!boot_cpu_has(X86_FEATURE_RDS
))
432 cmd
= ssb_parse_cmdline();
433 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS
) &&
434 (cmd
== SPEC_STORE_BYPASS_CMD_NONE
||
435 cmd
== SPEC_STORE_BYPASS_CMD_AUTO
))
439 case SPEC_STORE_BYPASS_CMD_AUTO
:
440 case SPEC_STORE_BYPASS_CMD_ON
:
441 mode
= SPEC_STORE_BYPASS_DISABLE
;
443 case SPEC_STORE_BYPASS_CMD_NONE
:
448 * We have three CPU feature flags that are in play here:
449 * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
450 * - X86_FEATURE_RDS - CPU is able to turn off speculative store bypass
451 * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
453 if (mode
!= SPEC_STORE_BYPASS_NONE
) {
454 setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE
);
456 * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD uses
457 * a completely different MSR and bit dependent on family.
459 switch (boot_cpu_data
.x86_vendor
) {
460 case X86_VENDOR_INTEL
:
461 x86_spec_ctrl_base
|= SPEC_CTRL_RDS
;
462 x86_spec_ctrl_set(SPEC_CTRL_RDS
);
472 static void ssb_select_mitigation()
474 ssb_mode
= __ssb_select_mitigation();
476 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS
))
477 pr_info("%s\n", ssb_strings
[ssb_mode
]);
482 void x86_spec_ctrl_setup_ap(void)
484 if (boot_cpu_has(X86_FEATURE_IBRS
))
485 x86_spec_ctrl_set(x86_spec_ctrl_base
& (SPEC_CTRL_IBRS
| SPEC_CTRL_RDS
));
490 ssize_t
cpu_show_common(struct device
*dev
, struct device_attribute
*attr
,
491 char *buf
, unsigned int bug
)
493 if (!boot_cpu_has_bug(bug
))
494 return sprintf(buf
, "Not affected\n");
497 case X86_BUG_CPU_MELTDOWN
:
498 if (boot_cpu_has(X86_FEATURE_PTI
))
499 return sprintf(buf
, "Mitigation: PTI\n");
503 case X86_BUG_SPECTRE_V1
:
504 return sprintf(buf
, "Mitigation: __user pointer sanitization\n");
506 case X86_BUG_SPECTRE_V2
:
507 return sprintf(buf
, "%s%s%s%s\n", spectre_v2_strings
[spectre_v2_enabled
],
508 boot_cpu_has(X86_FEATURE_USE_IBPB
) ? ", IBPB" : "",
509 boot_cpu_has(X86_FEATURE_USE_IBRS_FW
) ? ", IBRS_FW" : "",
510 spectre_v2_module_string());
512 case X86_BUG_SPEC_STORE_BYPASS
:
513 return sprintf(buf
, "%s\n", ssb_strings
[ssb_mode
]);
519 return sprintf(buf
, "Vulnerable\n");
522 ssize_t
cpu_show_meltdown(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
524 return cpu_show_common(dev
, attr
, buf
, X86_BUG_CPU_MELTDOWN
);
527 ssize_t
cpu_show_spectre_v1(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
529 return cpu_show_common(dev
, attr
, buf
, X86_BUG_SPECTRE_V1
);
532 ssize_t
cpu_show_spectre_v2(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
534 return cpu_show_common(dev
, attr
, buf
, X86_BUG_SPECTRE_V2
);
537 ssize_t
cpu_show_spec_store_bypass(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
539 return cpu_show_common(dev
, attr
, buf
, X86_BUG_SPEC_STORE_BYPASS
);