2 * Copyright (C) 1994 Linus Torvalds
4 * Cyrix stuff, June 1998 by:
5 * - Rafael R. Reilova (moved everything from head.S),
6 * <rreilova@ececs.uc.edu>
7 * - Channing Corn (tests & fixes),
8 * - Andrew D. Balsa (code cleanup).
10 #include <linux/init.h>
11 #include <linux/utsname.h>
12 #include <linux/cpu.h>
13 #include <linux/smp.h>
15 #include <asm/nospec-branch.h>
16 #include <asm/cmdline.h>
18 #include <asm/processor.h>
19 #include <asm/processor-flags.h>
20 #include <asm/fpu/internal.h>
22 #include <asm/paravirt.h>
23 #include <asm/alternative.h>
24 #include <asm/pgtable.h>
25 #include <asm/set_memory.h>
26 #include <asm/intel-family.h>
28 static void __init
spectre_v2_select_mitigation(void);
31 * Our boot-time value of the SPEC_CTRL MSR. We read it once so that any
32 * writes to SPEC_CTRL contain whatever reserved bits have been set.
34 static u64 __ro_after_init x86_spec_ctrl_base
;
36 void __init
check_bugs(void)
40 if (!IS_ENABLED(CONFIG_SMP
)) {
42 print_cpu_info(&boot_cpu_data
);
46 * Read the SPEC_CTRL MSR to account for reserved bits which may
47 * have unknown values.
50 rdmsrl(MSR_IA32_SPEC_CTRL
, x86_spec_ctrl_base
);
52 /* Select the proper spectre mitigation before patching alternatives */
53 spectre_v2_select_mitigation();
57 * Check whether we are able to run this kernel safely on SMP.
59 * - i386 is no longer supported.
60 * - In order to run on anything without a TSC, we need to be
61 * compiled for a i486.
63 if (boot_cpu_data
.x86
< 4)
64 panic("Kernel requires i486+ for 'invlpg' and other features");
66 init_utsname()->machine
[1] =
67 '0' + (boot_cpu_data
.x86
> 6 ? 6 : boot_cpu_data
.x86
);
68 alternative_instructions();
70 fpu__init_check_bugs();
71 #else /* CONFIG_X86_64 */
72 alternative_instructions();
75 * Make sure the first 2MB area is not mapped by huge pages
76 * There are typically fixed size MTRRs in there and overlapping
77 * MTRRs into large pages causes slow downs.
79 * Right now we don't do that with gbpages because there seems
80 * very little benefit for that case.
83 set_memory_4k((unsigned long)__va(0), 1);
87 /* The kernel command line selection */
88 enum spectre_v2_mitigation_cmd
{
92 SPECTRE_V2_CMD_RETPOLINE
,
93 SPECTRE_V2_CMD_RETPOLINE_GENERIC
,
94 SPECTRE_V2_CMD_RETPOLINE_AMD
,
97 static const char *spectre_v2_strings
[] = {
98 [SPECTRE_V2_NONE
] = "Vulnerable",
99 [SPECTRE_V2_RETPOLINE_MINIMAL
] = "Vulnerable: Minimal generic ASM retpoline",
100 [SPECTRE_V2_RETPOLINE_MINIMAL_AMD
] = "Vulnerable: Minimal AMD ASM retpoline",
101 [SPECTRE_V2_RETPOLINE_GENERIC
] = "Mitigation: Full generic retpoline",
102 [SPECTRE_V2_RETPOLINE_AMD
] = "Mitigation: Full AMD retpoline",
106 #define pr_fmt(fmt) "Spectre V2 mitigation: " fmt
108 static enum spectre_v2_mitigation spectre_v2_enabled
= SPECTRE_V2_NONE
;
110 void x86_spec_ctrl_set(u64 val
)
112 if (val
& ~SPEC_CTRL_IBRS
)
113 WARN_ONCE(1, "SPEC_CTRL MSR value 0x%16llx is unknown.\n", val
);
115 wrmsrl(MSR_IA32_SPEC_CTRL
, x86_spec_ctrl_base
| val
);
117 EXPORT_SYMBOL_GPL(x86_spec_ctrl_set
);
119 u64
x86_spec_ctrl_get_default(void)
121 return x86_spec_ctrl_base
;
123 EXPORT_SYMBOL_GPL(x86_spec_ctrl_get_default
);
125 void x86_spec_ctrl_set_guest(u64 guest_spec_ctrl
)
129 if (x86_spec_ctrl_base
!= guest_spec_ctrl
)
130 wrmsrl(MSR_IA32_SPEC_CTRL
, guest_spec_ctrl
);
132 EXPORT_SYMBOL_GPL(x86_spec_ctrl_set_guest
);
134 void x86_spec_ctrl_restore_host(u64 guest_spec_ctrl
)
138 if (x86_spec_ctrl_base
!= guest_spec_ctrl
)
139 wrmsrl(MSR_IA32_SPEC_CTRL
, x86_spec_ctrl_base
);
141 EXPORT_SYMBOL_GPL(x86_spec_ctrl_restore_host
);
143 static void __init
spec2_print_if_insecure(const char *reason
)
145 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2
))
146 pr_info("%s\n", reason
);
149 static void __init
spec2_print_if_secure(const char *reason
)
151 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2
))
152 pr_info("%s\n", reason
);
155 static inline bool retp_compiler(void)
157 return __is_defined(RETPOLINE
);
160 static inline bool match_option(const char *arg
, int arglen
, const char *opt
)
162 int len
= strlen(opt
);
164 return len
== arglen
&& !strncmp(arg
, opt
, len
);
167 static enum spectre_v2_mitigation_cmd __init
spectre_v2_parse_cmdline(void)
172 ret
= cmdline_find_option(boot_command_line
, "spectre_v2", arg
,
175 if (match_option(arg
, ret
, "off")) {
177 } else if (match_option(arg
, ret
, "on")) {
178 spec2_print_if_secure("force enabled on command line.");
179 return SPECTRE_V2_CMD_FORCE
;
180 } else if (match_option(arg
, ret
, "retpoline")) {
181 spec2_print_if_insecure("retpoline selected on command line.");
182 return SPECTRE_V2_CMD_RETPOLINE
;
183 } else if (match_option(arg
, ret
, "retpoline,amd")) {
184 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_AMD
) {
185 pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n");
186 return SPECTRE_V2_CMD_AUTO
;
188 spec2_print_if_insecure("AMD retpoline selected on command line.");
189 return SPECTRE_V2_CMD_RETPOLINE_AMD
;
190 } else if (match_option(arg
, ret
, "retpoline,generic")) {
191 spec2_print_if_insecure("generic retpoline selected on command line.");
192 return SPECTRE_V2_CMD_RETPOLINE_GENERIC
;
193 } else if (match_option(arg
, ret
, "auto")) {
194 return SPECTRE_V2_CMD_AUTO
;
198 if (!cmdline_find_option_bool(boot_command_line
, "nospectre_v2"))
199 return SPECTRE_V2_CMD_AUTO
;
201 spec2_print_if_insecure("disabled on command line.");
202 return SPECTRE_V2_CMD_NONE
;
205 /* Check for Skylake-like CPUs (for RSB handling) */
206 static bool __init
is_skylake_era(void)
208 if (boot_cpu_data
.x86_vendor
== X86_VENDOR_INTEL
&&
209 boot_cpu_data
.x86
== 6) {
210 switch (boot_cpu_data
.x86_model
) {
211 case INTEL_FAM6_SKYLAKE_MOBILE
:
212 case INTEL_FAM6_SKYLAKE_DESKTOP
:
213 case INTEL_FAM6_SKYLAKE_X
:
214 case INTEL_FAM6_KABYLAKE_MOBILE
:
215 case INTEL_FAM6_KABYLAKE_DESKTOP
:
222 static void __init
spectre_v2_select_mitigation(void)
224 enum spectre_v2_mitigation_cmd cmd
= spectre_v2_parse_cmdline();
225 enum spectre_v2_mitigation mode
= SPECTRE_V2_NONE
;
228 * If the CPU is not affected and the command line mode is NONE or AUTO
229 * then nothing to do.
231 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2
) &&
232 (cmd
== SPECTRE_V2_CMD_NONE
|| cmd
== SPECTRE_V2_CMD_AUTO
))
236 case SPECTRE_V2_CMD_NONE
:
239 case SPECTRE_V2_CMD_FORCE
:
241 case SPECTRE_V2_CMD_AUTO
:
244 case SPECTRE_V2_CMD_RETPOLINE_AMD
:
245 if (IS_ENABLED(CONFIG_RETPOLINE
))
248 case SPECTRE_V2_CMD_RETPOLINE_GENERIC
:
249 if (IS_ENABLED(CONFIG_RETPOLINE
))
250 goto retpoline_generic
;
252 case SPECTRE_V2_CMD_RETPOLINE
:
253 if (IS_ENABLED(CONFIG_RETPOLINE
))
257 pr_err("kernel not compiled with retpoline; no mitigation available!");
261 if (boot_cpu_data
.x86_vendor
== X86_VENDOR_AMD
) {
263 if (!boot_cpu_has(X86_FEATURE_LFENCE_RDTSC
)) {
264 pr_err("LFENCE not serializing. Switching to generic retpoline\n");
265 goto retpoline_generic
;
267 mode
= retp_compiler() ? SPECTRE_V2_RETPOLINE_AMD
:
268 SPECTRE_V2_RETPOLINE_MINIMAL_AMD
;
269 setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD
);
270 setup_force_cpu_cap(X86_FEATURE_RETPOLINE
);
273 mode
= retp_compiler() ? SPECTRE_V2_RETPOLINE_GENERIC
:
274 SPECTRE_V2_RETPOLINE_MINIMAL
;
275 setup_force_cpu_cap(X86_FEATURE_RETPOLINE
);
278 spectre_v2_enabled
= mode
;
279 pr_info("%s\n", spectre_v2_strings
[mode
]);
281 pr_info("Speculation control IBPB %s IBRS %s",
282 ibpb_supported
? "supported" : "not-supported",
283 ibrs_supported
? "supported" : "not-supported");
286 * If we have a full retpoline mode and then disable IBPB in kernel mode
287 * we do not require both.
289 if (mode
== SPECTRE_V2_RETPOLINE_AMD
||
290 mode
== SPECTRE_V2_RETPOLINE_GENERIC
)
292 if (ibrs_supported
) {
293 pr_info("Retpoline compiled kernel. Defaulting IBRS to disabled");
296 sysctl_ibrs_enabled
= 0;
301 * If neither SMEP or KPTI are available, there is a risk of
302 * hitting userspace addresses in the RSB after a context switch
303 * from a shallow call stack to a deeper one. To prevent this fill
304 * the entire RSB, even when using IBRS.
306 * Skylake era CPUs have a separate issue with *underflow* of the
307 * RSB, when they will predict 'ret' targets from the generic BTB.
308 * The proper mitigation for this is IBRS. If IBRS is not supported
309 * or deactivated in favour of retpolines the RSB fill on context
310 * switch is required.
312 if ((!boot_cpu_has(X86_FEATURE_PTI
) &&
313 !boot_cpu_has(X86_FEATURE_SMEP
)) || is_skylake_era()) {
314 setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW
);
315 pr_info("Filling RSB on context switch\n");
322 ssize_t
cpu_show_common(struct device
*dev
, struct device_attribute
*attr
,
323 char *buf
, unsigned int bug
)
325 if (!boot_cpu_has_bug(bug
))
326 return sprintf(buf
, "Not affected\n");
329 case X86_BUG_CPU_MELTDOWN
:
330 if (boot_cpu_has(X86_FEATURE_PTI
))
331 return sprintf(buf
, "Mitigation: PTI\n");
334 case X86_BUG_SPECTRE_V1
:
336 return sprintf(buf
, "Mitigation: OSB (observable speculation barrier, Intel v6)\n");
338 case X86_BUG_SPECTRE_V2
:
339 return sprintf(buf
, "%s%s\n", spectre_v2_strings
[spectre_v2_enabled
], ibpb_inuse
? ", IBPB (Intel v4)" : "");
345 return sprintf(buf
, "Vulnerable\n");
348 ssize_t
cpu_show_meltdown(struct device
*dev
, struct device_attribute
*attr
,
351 return cpu_show_common(dev
, attr
, buf
, X86_BUG_CPU_MELTDOWN
);
354 ssize_t
cpu_show_spectre_v1(struct device
*dev
, struct device_attribute
*attr
,
357 return cpu_show_common(dev
, attr
, buf
, X86_BUG_SPECTRE_V1
);
360 ssize_t
cpu_show_spectre_v2(struct device
*dev
, struct device_attribute
*attr
,
363 return cpu_show_common(dev
, attr
, buf
, X86_BUG_SPECTRE_V2
);