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);
31 void __init
check_bugs(void)
35 if (!IS_ENABLED(CONFIG_SMP
)) {
37 print_cpu_info(&boot_cpu_data
);
40 /* Select the proper spectre mitigation before patching alternatives */
41 spectre_v2_select_mitigation();
45 * Check whether we are able to run this kernel safely on SMP.
47 * - i386 is no longer supported.
48 * - In order to run on anything without a TSC, we need to be
49 * compiled for a i486.
51 if (boot_cpu_data
.x86
< 4)
52 panic("Kernel requires i486+ for 'invlpg' and other features");
54 init_utsname()->machine
[1] =
55 '0' + (boot_cpu_data
.x86
> 6 ? 6 : boot_cpu_data
.x86
);
56 alternative_instructions();
58 fpu__init_check_bugs();
59 #else /* CONFIG_X86_64 */
60 alternative_instructions();
63 * Make sure the first 2MB area is not mapped by huge pages
64 * There are typically fixed size MTRRs in there and overlapping
65 * MTRRs into large pages causes slow downs.
67 * Right now we don't do that with gbpages because there seems
68 * very little benefit for that case.
71 set_memory_4k((unsigned long)__va(0), 1);
75 /* The kernel command line selection */
76 enum spectre_v2_mitigation_cmd
{
80 SPECTRE_V2_CMD_RETPOLINE
,
81 SPECTRE_V2_CMD_RETPOLINE_GENERIC
,
82 SPECTRE_V2_CMD_RETPOLINE_AMD
,
85 static const char *spectre_v2_strings
[] = {
86 [SPECTRE_V2_NONE
] = "Vulnerable",
87 [SPECTRE_V2_RETPOLINE_MINIMAL
] = "Vulnerable: Minimal generic ASM retpoline",
88 [SPECTRE_V2_RETPOLINE_MINIMAL_AMD
] = "Vulnerable: Minimal AMD ASM retpoline",
89 [SPECTRE_V2_RETPOLINE_GENERIC
] = "Mitigation: Full generic retpoline",
90 [SPECTRE_V2_RETPOLINE_AMD
] = "Mitigation: Full AMD retpoline",
94 #define pr_fmt(fmt) "Spectre V2 : " fmt
96 static enum spectre_v2_mitigation spectre_v2_enabled
= SPECTRE_V2_NONE
;
99 static bool spectre_v2_bad_module
;
101 bool retpoline_module_ok(bool has_retpoline
)
103 if (spectre_v2_enabled
== SPECTRE_V2_NONE
|| has_retpoline
)
106 pr_err("System may be vunerable to spectre v2\n");
107 spectre_v2_bad_module
= true;
111 static inline const char *spectre_v2_module_string(void)
113 return spectre_v2_bad_module
? " - vulnerable module loaded" : "";
116 static inline const char *spectre_v2_module_string(void) { return ""; }
119 static void __init
spec2_print_if_insecure(const char *reason
)
121 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2
))
122 pr_info("%s\n", reason
);
125 static void __init
spec2_print_if_secure(const char *reason
)
127 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2
))
128 pr_info("%s\n", reason
);
131 static inline bool retp_compiler(void)
133 return __is_defined(RETPOLINE
);
136 static inline bool match_option(const char *arg
, int arglen
, const char *opt
)
138 int len
= strlen(opt
);
140 return len
== arglen
&& !strncmp(arg
, opt
, len
);
143 static enum spectre_v2_mitigation_cmd __init
spectre_v2_parse_cmdline(void)
148 ret
= cmdline_find_option(boot_command_line
, "spectre_v2", arg
,
151 if (match_option(arg
, ret
, "off")) {
153 } else if (match_option(arg
, ret
, "on")) {
154 spec2_print_if_secure("force enabled on command line.");
155 return SPECTRE_V2_CMD_FORCE
;
156 } else if (match_option(arg
, ret
, "retpoline")) {
157 spec2_print_if_insecure("retpoline selected on command line.");
158 return SPECTRE_V2_CMD_RETPOLINE
;
159 } else if (match_option(arg
, ret
, "retpoline,amd")) {
160 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_AMD
) {
161 pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n");
162 return SPECTRE_V2_CMD_AUTO
;
164 spec2_print_if_insecure("AMD retpoline selected on command line.");
165 return SPECTRE_V2_CMD_RETPOLINE_AMD
;
166 } else if (match_option(arg
, ret
, "retpoline,generic")) {
167 spec2_print_if_insecure("generic retpoline selected on command line.");
168 return SPECTRE_V2_CMD_RETPOLINE_GENERIC
;
169 } else if (match_option(arg
, ret
, "auto")) {
170 return SPECTRE_V2_CMD_AUTO
;
174 if (!cmdline_find_option_bool(boot_command_line
, "nospectre_v2"))
175 return SPECTRE_V2_CMD_AUTO
;
177 spec2_print_if_insecure("disabled on command line.");
178 return SPECTRE_V2_CMD_NONE
;
181 /* Check for Skylake-like CPUs (for RSB handling) */
182 static bool __init
is_skylake_era(void)
184 if (boot_cpu_data
.x86_vendor
== X86_VENDOR_INTEL
&&
185 boot_cpu_data
.x86
== 6) {
186 switch (boot_cpu_data
.x86_model
) {
187 case INTEL_FAM6_SKYLAKE_MOBILE
:
188 case INTEL_FAM6_SKYLAKE_DESKTOP
:
189 case INTEL_FAM6_SKYLAKE_X
:
190 case INTEL_FAM6_KABYLAKE_MOBILE
:
191 case INTEL_FAM6_KABYLAKE_DESKTOP
:
198 static void __init
spectre_v2_select_mitigation(void)
200 enum spectre_v2_mitigation_cmd cmd
= spectre_v2_parse_cmdline();
201 enum spectre_v2_mitigation mode
= SPECTRE_V2_NONE
;
204 * If the CPU is not affected and the command line mode is NONE or AUTO
205 * then nothing to do.
207 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2
) &&
208 (cmd
== SPECTRE_V2_CMD_NONE
|| cmd
== SPECTRE_V2_CMD_AUTO
))
212 case SPECTRE_V2_CMD_NONE
:
215 case SPECTRE_V2_CMD_FORCE
:
217 case SPECTRE_V2_CMD_AUTO
:
220 case SPECTRE_V2_CMD_RETPOLINE_AMD
:
221 if (IS_ENABLED(CONFIG_RETPOLINE
))
224 case SPECTRE_V2_CMD_RETPOLINE_GENERIC
:
225 if (IS_ENABLED(CONFIG_RETPOLINE
))
226 goto retpoline_generic
;
228 case SPECTRE_V2_CMD_RETPOLINE
:
229 if (IS_ENABLED(CONFIG_RETPOLINE
))
233 pr_err("kernel not compiled with retpoline; no mitigation available!");
237 if (boot_cpu_data
.x86_vendor
== X86_VENDOR_AMD
) {
239 if (!boot_cpu_has(X86_FEATURE_LFENCE_RDTSC
)) {
240 pr_err("LFENCE not serializing. Switching to generic retpoline\n");
241 goto retpoline_generic
;
243 mode
= retp_compiler() ? SPECTRE_V2_RETPOLINE_AMD
:
244 SPECTRE_V2_RETPOLINE_MINIMAL_AMD
;
245 setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD
);
246 setup_force_cpu_cap(X86_FEATURE_RETPOLINE
);
249 mode
= retp_compiler() ? SPECTRE_V2_RETPOLINE_GENERIC
:
250 SPECTRE_V2_RETPOLINE_MINIMAL
;
251 setup_force_cpu_cap(X86_FEATURE_RETPOLINE
);
254 spectre_v2_enabled
= mode
;
255 pr_info("%s\n", spectre_v2_strings
[mode
]);
258 * If neither SMEP or KPTI are available, there is a risk of
259 * hitting userspace addresses in the RSB after a context switch
260 * from a shallow call stack to a deeper one. To prevent this fill
261 * the entire RSB, even when using IBRS.
263 * Skylake era CPUs have a separate issue with *underflow* of the
264 * RSB, when they will predict 'ret' targets from the generic BTB.
265 * The proper mitigation for this is IBRS. If IBRS is not supported
266 * or deactivated in favour of retpolines the RSB fill on context
267 * switch is required.
269 if ((!boot_cpu_has(X86_FEATURE_PTI
) &&
270 !boot_cpu_has(X86_FEATURE_SMEP
)) || is_skylake_era()) {
271 setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW
);
272 pr_info("Filling RSB on context switch\n");
275 /* Initialize Indirect Branch Prediction Barrier if supported */
276 if (boot_cpu_has(X86_FEATURE_IBPB
)) {
277 setup_force_cpu_cap(X86_FEATURE_USE_IBPB
);
278 pr_info("Enabling Indirect Branch Prediction Barrier\n");
285 ssize_t
cpu_show_meltdown(struct device
*dev
,
286 struct device_attribute
*attr
, char *buf
)
288 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN
))
289 return sprintf(buf
, "Not affected\n");
290 if (boot_cpu_has(X86_FEATURE_PTI
))
291 return sprintf(buf
, "Mitigation: PTI\n");
292 return sprintf(buf
, "Vulnerable\n");
295 ssize_t
cpu_show_spectre_v1(struct device
*dev
,
296 struct device_attribute
*attr
, char *buf
)
298 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1
))
299 return sprintf(buf
, "Not affected\n");
300 return sprintf(buf
, "Vulnerable\n");
303 ssize_t
cpu_show_spectre_v2(struct device
*dev
,
304 struct device_attribute
*attr
, char *buf
)
306 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2
))
307 return sprintf(buf
, "Not affected\n");
309 return sprintf(buf
, "%s%s%s\n", spectre_v2_strings
[spectre_v2_enabled
],
310 boot_cpu_has(X86_FEATURE_USE_IBPB
) ? ", IBPB" : "",
311 spectre_v2_module_string());