]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1353ebb4 | 2 | /* |
1353ebb4 JF |
3 | * Copyright (C) 1994 Linus Torvalds |
4 | * | |
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). | |
10 | */ | |
11 | #include <linux/init.h> | |
12 | #include <linux/utsname.h> | |
61dc0f55 | 13 | #include <linux/cpu.h> |
d280282b | 14 | #include <linux/module.h> |
574dcf89 TG |
15 | #include <linux/nospec.h> |
16 | #include <linux/prctl.h> | |
da285121 | 17 | |
d7a6a163 | 18 | #include <asm/spec-ctrl.h> |
da285121 | 19 | #include <asm/cmdline.h> |
91eb1b79 | 20 | #include <asm/bugs.h> |
1353ebb4 | 21 | #include <asm/processor.h> |
7ebad705 | 22 | #include <asm/processor-flags.h> |
952f07ec | 23 | #include <asm/fpu/internal.h> |
1353ebb4 | 24 | #include <asm/msr.h> |
b9cfedcd | 25 | #include <asm/vmx.h> |
1353ebb4 JF |
26 | #include <asm/paravirt.h> |
27 | #include <asm/alternative.h> | |
62a67e12 | 28 | #include <asm/pgtable.h> |
d1163651 | 29 | #include <asm/set_memory.h> |
c995efd5 | 30 | #include <asm/intel-family.h> |
05516ad8 | 31 | #include <asm/e820/api.h> |
1353ebb4 | 32 | |
da285121 | 33 | static void __init spectre_v2_select_mitigation(void); |
e63490c8 | 34 | static void __init ssb_select_mitigation(void); |
05516ad8 | 35 | static void __init l1tf_select_mitigation(void); |
da285121 | 36 | |
296b454a KRW |
37 | /* |
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. | |
40 | */ | |
5407b7f8 | 41 | u64 __ro_after_init x86_spec_ctrl_base; |
4ac9b1f9 | 42 | EXPORT_SYMBOL_GPL(x86_spec_ctrl_base); |
296b454a | 43 | |
d0c3bedd KRW |
44 | /* |
45 | * The vendor and possibly platform specific bits which can be modified in | |
46 | * x86_spec_ctrl_base. | |
47 | */ | |
e5f984ed | 48 | static u64 __ro_after_init x86_spec_ctrl_mask = SPEC_CTRL_IBRS; |
d0c3bedd | 49 | |
c37b94dd KRW |
50 | /* |
51 | * AMD specific MSR info for Speculative Store Bypass control. | |
8fe36c9d | 52 | * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu(). |
c37b94dd KRW |
53 | */ |
54 | u64 __ro_after_init x86_amd_ls_cfg_base; | |
8fe36c9d | 55 | u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask; |
c37b94dd | 56 | |
1353ebb4 JF |
57 | void __init check_bugs(void) |
58 | { | |
59 | identify_boot_cpu(); | |
55a36b65 | 60 | |
483ec3c6 TG |
61 | /* |
62 | * identify_boot_cpu() initialized SMT support information, let the | |
63 | * core code know. | |
64 | */ | |
ade31b9e | 65 | cpu_smt_check_topology_early(); |
483ec3c6 | 66 | |
62a67e12 BP |
67 | if (!IS_ENABLED(CONFIG_SMP)) { |
68 | pr_info("CPU: "); | |
69 | print_cpu_info(&boot_cpu_data); | |
70 | } | |
71 | ||
296b454a KRW |
72 | /* |
73 | * Read the SPEC_CTRL MSR to account for reserved bits which may | |
c37b94dd KRW |
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. | |
296b454a | 76 | */ |
50f9b919 | 77 | if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) |
296b454a KRW |
78 | rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base); |
79 | ||
e5f984ed TG |
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; | |
83 | ||
da285121 DW |
84 | /* Select the proper spectre mitigation before patching alternatives */ |
85 | spectre_v2_select_mitigation(); | |
86 | ||
e63490c8 KRW |
87 | /* |
88 | * Select proper mitigation for any exposure to the Speculative Store | |
89 | * Bypass vulnerability. | |
90 | */ | |
91 | ssb_select_mitigation(); | |
92 | ||
05516ad8 AK |
93 | l1tf_select_mitigation(); |
94 | ||
62a67e12 | 95 | #ifdef CONFIG_X86_32 |
55a36b65 BP |
96 | /* |
97 | * Check whether we are able to run this kernel safely on SMP. | |
98 | * | |
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. | |
102 | */ | |
103 | if (boot_cpu_data.x86 < 4) | |
104 | panic("Kernel requires i486+ for 'invlpg' and other features"); | |
105 | ||
bfe4bb15 MV |
106 | init_utsname()->machine[1] = |
107 | '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86); | |
1353ebb4 | 108 | alternative_instructions(); |
304bceda | 109 | |
4d164092 | 110 | fpu__init_check_bugs(); |
62a67e12 BP |
111 | #else /* CONFIG_X86_64 */ |
112 | alternative_instructions(); | |
113 | ||
114 | /* | |
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. | |
118 | * | |
119 | * Right now we don't do that with gbpages because there seems | |
120 | * very little benefit for that case. | |
121 | */ | |
122 | if (!direct_gbpages) | |
123 | set_memory_4k((unsigned long)__va(0), 1); | |
124 | #endif | |
1353ebb4 | 125 | } |
61dc0f55 | 126 | |
da285121 DW |
127 | /* The kernel command line selection */ |
128 | enum spectre_v2_mitigation_cmd { | |
129 | SPECTRE_V2_CMD_NONE, | |
130 | SPECTRE_V2_CMD_AUTO, | |
131 | SPECTRE_V2_CMD_FORCE, | |
132 | SPECTRE_V2_CMD_RETPOLINE, | |
133 | SPECTRE_V2_CMD_RETPOLINE_GENERIC, | |
134 | SPECTRE_V2_CMD_RETPOLINE_AMD, | |
135 | }; | |
136 | ||
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", | |
1044fde9 | 143 | [SPECTRE_V2_IBRS_ENHANCED] = "Mitigation: Enhanced IBRS", |
da285121 DW |
144 | }; |
145 | ||
146 | #undef pr_fmt | |
bbb5e08b | 147 | #define pr_fmt(fmt) "Spectre V2 : " fmt |
da285121 | 148 | |
b5e6d77d KC |
149 | static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init = |
150 | SPECTRE_V2_NONE; | |
d280282b | 151 | |
12376b62 BP |
152 | void |
153 | x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest) | |
19fff03f | 154 | { |
e5f984ed | 155 | u64 msrval, guestval, hostval = x86_spec_ctrl_base; |
12376b62 | 156 | struct thread_info *ti = current_thread_info(); |
5407b7f8 | 157 | |
50f9b919 | 158 | /* Is MSR_SPEC_CTRL implemented ? */ |
12376b62 | 159 | if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) { |
e5f984ed TG |
160 | /* |
161 | * Restrict guest_spec_ctrl to supported values. Clear the | |
162 | * modifiable bits in the host base value and or the | |
163 | * modifiable bits from the guest value. | |
164 | */ | |
165 | guestval = hostval & ~x86_spec_ctrl_mask; | |
166 | guestval |= guest_spec_ctrl & x86_spec_ctrl_mask; | |
167 | ||
12376b62 | 168 | /* SSBD controlled in MSR_SPEC_CTRL */ |
5005c716 TL |
169 | if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) || |
170 | static_cpu_has(X86_FEATURE_AMD_SSBD)) | |
e5f984ed | 171 | hostval |= ssbd_tif_to_spec_ctrl(ti->flags); |
12376b62 | 172 | |
e5f984ed TG |
173 | if (hostval != guestval) { |
174 | msrval = setguest ? guestval : hostval; | |
175 | wrmsrl(MSR_IA32_SPEC_CTRL, msrval); | |
12376b62 BP |
176 | } |
177 | } | |
1238ed31 TG |
178 | |
179 | /* | |
180 | * If SSBD is not handled in MSR_SPEC_CTRL on AMD, update | |
181 | * MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported. | |
182 | */ | |
183 | if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD) && | |
184 | !static_cpu_has(X86_FEATURE_VIRT_SSBD)) | |
185 | return; | |
186 | ||
187 | /* | |
188 | * If the host has SSBD mitigation enabled, force it in the host's | |
189 | * virtual MSR value. If its not permanently enabled, evaluate | |
190 | * current's TIF_SSBD thread flag. | |
191 | */ | |
192 | if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE)) | |
193 | hostval = SPEC_CTRL_SSBD; | |
194 | else | |
195 | hostval = ssbd_tif_to_spec_ctrl(ti->flags); | |
196 | ||
197 | /* Sanitize the guest value */ | |
198 | guestval = guest_virt_spec_ctrl & SPEC_CTRL_SSBD; | |
199 | ||
200 | if (hostval != guestval) { | |
201 | unsigned long tif; | |
202 | ||
203 | tif = setguest ? ssbd_spec_ctrl_to_tif(guestval) : | |
204 | ssbd_spec_ctrl_to_tif(hostval); | |
205 | ||
206 | speculative_store_bypass_update(tif); | |
207 | } | |
19fff03f | 208 | } |
12376b62 | 209 | EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl); |
19fff03f | 210 | |
8fe36c9d | 211 | static void x86_amd_ssb_disable(void) |
c37b94dd | 212 | { |
8fe36c9d | 213 | u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_ssbd_mask; |
c37b94dd | 214 | |
65e02bbd TL |
215 | if (boot_cpu_has(X86_FEATURE_VIRT_SSBD)) |
216 | wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, SPEC_CTRL_SSBD); | |
217 | else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD)) | |
c37b94dd KRW |
218 | wrmsrl(MSR_AMD64_LS_CFG, msrval); |
219 | } | |
220 | ||
d280282b | 221 | #ifdef RETPOLINE |
bb3c2578 TG |
222 | static bool spectre_v2_bad_module; |
223 | ||
d280282b AK |
224 | bool retpoline_module_ok(bool has_retpoline) |
225 | { | |
226 | if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline) | |
227 | return true; | |
228 | ||
c8b8e109 | 229 | pr_err("System may be vulnerable to spectre v2\n"); |
d280282b AK |
230 | spectre_v2_bad_module = true; |
231 | return false; | |
232 | } | |
bb3c2578 TG |
233 | |
234 | static inline const char *spectre_v2_module_string(void) | |
235 | { | |
236 | return spectre_v2_bad_module ? " - vulnerable module loaded" : ""; | |
237 | } | |
238 | #else | |
239 | static inline const char *spectre_v2_module_string(void) { return ""; } | |
d280282b | 240 | #endif |
da285121 DW |
241 | |
242 | static void __init spec2_print_if_insecure(const char *reason) | |
243 | { | |
244 | if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2)) | |
17c33e7c | 245 | pr_info("%s selected on command line.\n", reason); |
da285121 DW |
246 | } |
247 | ||
248 | static void __init spec2_print_if_secure(const char *reason) | |
249 | { | |
250 | if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2)) | |
17c33e7c | 251 | pr_info("%s selected on command line.\n", reason); |
da285121 DW |
252 | } |
253 | ||
254 | static inline bool retp_compiler(void) | |
255 | { | |
256 | return __is_defined(RETPOLINE); | |
257 | } | |
258 | ||
259 | static inline bool match_option(const char *arg, int arglen, const char *opt) | |
260 | { | |
261 | int len = strlen(opt); | |
262 | ||
263 | return len == arglen && !strncmp(arg, opt, len); | |
264 | } | |
265 | ||
17c33e7c KA |
266 | static const struct { |
267 | const char *option; | |
268 | enum spectre_v2_mitigation_cmd cmd; | |
269 | bool secure; | |
270 | } mitigation_options[] = { | |
271 | { "off", SPECTRE_V2_CMD_NONE, false }, | |
272 | { "on", SPECTRE_V2_CMD_FORCE, true }, | |
273 | { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false }, | |
274 | { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_AMD, false }, | |
275 | { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false }, | |
276 | { "auto", SPECTRE_V2_CMD_AUTO, false }, | |
277 | }; | |
278 | ||
da285121 DW |
279 | static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void) |
280 | { | |
281 | char arg[20]; | |
17c33e7c KA |
282 | int ret, i; |
283 | enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO; | |
284 | ||
285 | if (cmdline_find_option_bool(boot_command_line, "nospectre_v2")) | |
286 | return SPECTRE_V2_CMD_NONE; | |
287 | else { | |
713f1b95 | 288 | ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg)); |
17c33e7c KA |
289 | if (ret < 0) |
290 | return SPECTRE_V2_CMD_AUTO; | |
291 | ||
292 | for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) { | |
293 | if (!match_option(arg, ret, mitigation_options[i].option)) | |
294 | continue; | |
295 | cmd = mitigation_options[i].cmd; | |
296 | break; | |
297 | } | |
298 | ||
299 | if (i >= ARRAY_SIZE(mitigation_options)) { | |
ecad7915 | 300 | pr_err("unknown option (%s). Switching to AUTO select\n", arg); |
da285121 DW |
301 | return SPECTRE_V2_CMD_AUTO; |
302 | } | |
303 | } | |
304 | ||
17c33e7c KA |
305 | if ((cmd == SPECTRE_V2_CMD_RETPOLINE || |
306 | cmd == SPECTRE_V2_CMD_RETPOLINE_AMD || | |
307 | cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC) && | |
308 | !IS_ENABLED(CONFIG_RETPOLINE)) { | |
713f1b95 | 309 | pr_err("%s selected but not compiled in. Switching to AUTO select\n", mitigation_options[i].option); |
da285121 | 310 | return SPECTRE_V2_CMD_AUTO; |
17c33e7c KA |
311 | } |
312 | ||
313 | if (cmd == SPECTRE_V2_CMD_RETPOLINE_AMD && | |
314 | boot_cpu_data.x86_vendor != X86_VENDOR_AMD) { | |
315 | pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n"); | |
316 | return SPECTRE_V2_CMD_AUTO; | |
317 | } | |
318 | ||
319 | if (mitigation_options[i].secure) | |
320 | spec2_print_if_secure(mitigation_options[i].option); | |
321 | else | |
322 | spec2_print_if_insecure(mitigation_options[i].option); | |
323 | ||
324 | return cmd; | |
da285121 DW |
325 | } |
326 | ||
327 | static void __init spectre_v2_select_mitigation(void) | |
328 | { | |
329 | enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline(); | |
330 | enum spectre_v2_mitigation mode = SPECTRE_V2_NONE; | |
331 | ||
332 | /* | |
333 | * If the CPU is not affected and the command line mode is NONE or AUTO | |
334 | * then nothing to do. | |
335 | */ | |
336 | if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) && | |
337 | (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO)) | |
338 | return; | |
339 | ||
340 | switch (cmd) { | |
341 | case SPECTRE_V2_CMD_NONE: | |
342 | return; | |
343 | ||
344 | case SPECTRE_V2_CMD_FORCE: | |
da285121 | 345 | case SPECTRE_V2_CMD_AUTO: |
1044fde9 SP |
346 | if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) { |
347 | mode = SPECTRE_V2_IBRS_ENHANCED; | |
348 | /* Force it so VMEXIT will restore correctly */ | |
349 | x86_spec_ctrl_base |= SPEC_CTRL_IBRS; | |
350 | wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base); | |
351 | goto specv2_set_mode; | |
352 | } | |
d0f293e0 DL |
353 | if (IS_ENABLED(CONFIG_RETPOLINE)) |
354 | goto retpoline_auto; | |
355 | break; | |
da285121 DW |
356 | case SPECTRE_V2_CMD_RETPOLINE_AMD: |
357 | if (IS_ENABLED(CONFIG_RETPOLINE)) | |
358 | goto retpoline_amd; | |
359 | break; | |
360 | case SPECTRE_V2_CMD_RETPOLINE_GENERIC: | |
361 | if (IS_ENABLED(CONFIG_RETPOLINE)) | |
362 | goto retpoline_generic; | |
363 | break; | |
364 | case SPECTRE_V2_CMD_RETPOLINE: | |
365 | if (IS_ENABLED(CONFIG_RETPOLINE)) | |
366 | goto retpoline_auto; | |
367 | break; | |
368 | } | |
713f1b95 | 369 | pr_err("Spectre mitigation: kernel not compiled with retpoline; no mitigation available!"); |
da285121 DW |
370 | return; |
371 | ||
372 | retpoline_auto: | |
373 | if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) { | |
374 | retpoline_amd: | |
375 | if (!boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) { | |
713f1b95 | 376 | pr_err("Spectre mitigation: LFENCE not serializing, switching to generic retpoline\n"); |
da285121 DW |
377 | goto retpoline_generic; |
378 | } | |
379 | mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_AMD : | |
380 | SPECTRE_V2_RETPOLINE_MINIMAL_AMD; | |
381 | setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD); | |
382 | setup_force_cpu_cap(X86_FEATURE_RETPOLINE); | |
383 | } else { | |
384 | retpoline_generic: | |
385 | mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_GENERIC : | |
386 | SPECTRE_V2_RETPOLINE_MINIMAL; | |
387 | setup_force_cpu_cap(X86_FEATURE_RETPOLINE); | |
388 | } | |
389 | ||
1044fde9 | 390 | specv2_set_mode: |
da285121 DW |
391 | spectre_v2_enabled = mode; |
392 | pr_info("%s\n", spectre_v2_strings[mode]); | |
c995efd5 DW |
393 | |
394 | /* | |
14623a43 JK |
395 | * If spectre v2 protection has been enabled, unconditionally fill |
396 | * RSB during a context switch; this protects against two independent | |
397 | * issues: | |
c995efd5 | 398 | * |
14623a43 JK |
399 | * - RSB underflow (and switch to BTB) on Skylake+ |
400 | * - SpectreRSB variant of spectre v2 on X86_BUG_SPECTRE_V2 CPUs | |
c995efd5 | 401 | */ |
14623a43 JK |
402 | setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW); |
403 | pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n"); | |
bd12e896 DW |
404 | |
405 | /* Initialize Indirect Branch Prediction Barrier if supported */ | |
581abf91 DW |
406 | if (boot_cpu_has(X86_FEATURE_IBPB)) { |
407 | setup_force_cpu_cap(X86_FEATURE_USE_IBPB); | |
713f1b95 | 408 | pr_info("Spectre v2 mitigation: Enabling Indirect Branch Prediction Barrier\n"); |
bd12e896 | 409 | } |
390b99c3 DW |
410 | |
411 | /* | |
412 | * Retpoline means the kernel is safe because it has no indirect | |
1044fde9 SP |
413 | * branches. Enhanced IBRS protects firmware too, so, enable restricted |
414 | * speculation around firmware calls only when Enhanced IBRS isn't | |
415 | * supported. | |
416 | * | |
417 | * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because | |
418 | * the user might select retpoline on the kernel command line and if | |
419 | * the CPU supports Enhanced IBRS, kernel might un-intentionally not | |
420 | * enable IBRS around firmware calls. | |
390b99c3 | 421 | */ |
1044fde9 | 422 | if (boot_cpu_has(X86_FEATURE_IBRS) && mode != SPECTRE_V2_IBRS_ENHANCED) { |
390b99c3 DW |
423 | setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW); |
424 | pr_info("Enabling Restricted Speculation for firmware calls\n"); | |
425 | } | |
da285121 DW |
426 | } |
427 | ||
e63490c8 KRW |
428 | #undef pr_fmt |
429 | #define pr_fmt(fmt) "Speculative Store Bypass: " fmt | |
430 | ||
b5e6d77d | 431 | static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE; |
e63490c8 KRW |
432 | |
433 | /* The kernel command line selection */ | |
434 | enum ssb_mitigation_cmd { | |
435 | SPEC_STORE_BYPASS_CMD_NONE, | |
436 | SPEC_STORE_BYPASS_CMD_AUTO, | |
437 | SPEC_STORE_BYPASS_CMD_ON, | |
574dcf89 | 438 | SPEC_STORE_BYPASS_CMD_PRCTL, |
c7416003 | 439 | SPEC_STORE_BYPASS_CMD_SECCOMP, |
e63490c8 KRW |
440 | }; |
441 | ||
442 | static const char *ssb_strings[] = { | |
443 | [SPEC_STORE_BYPASS_NONE] = "Vulnerable", | |
574dcf89 | 444 | [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass disabled", |
c7416003 KC |
445 | [SPEC_STORE_BYPASS_PRCTL] = "Mitigation: Speculative Store Bypass disabled via prctl", |
446 | [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp", | |
e63490c8 KRW |
447 | }; |
448 | ||
449 | static const struct { | |
450 | const char *option; | |
451 | enum ssb_mitigation_cmd cmd; | |
452 | } ssb_mitigation_options[] = { | |
c7416003 KC |
453 | { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */ |
454 | { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */ | |
455 | { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */ | |
456 | { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative Store Bypass via prctl */ | |
457 | { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */ | |
e63490c8 KRW |
458 | }; |
459 | ||
460 | static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void) | |
461 | { | |
462 | enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO; | |
463 | char arg[20]; | |
464 | int ret, i; | |
465 | ||
466 | if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable")) { | |
467 | return SPEC_STORE_BYPASS_CMD_NONE; | |
468 | } else { | |
469 | ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable", | |
470 | arg, sizeof(arg)); | |
471 | if (ret < 0) | |
472 | return SPEC_STORE_BYPASS_CMD_AUTO; | |
473 | ||
474 | for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) { | |
475 | if (!match_option(arg, ret, ssb_mitigation_options[i].option)) | |
476 | continue; | |
477 | ||
478 | cmd = ssb_mitigation_options[i].cmd; | |
479 | break; | |
480 | } | |
481 | ||
482 | if (i >= ARRAY_SIZE(ssb_mitigation_options)) { | |
483 | pr_err("unknown option (%s). Switching to AUTO select\n", arg); | |
484 | return SPEC_STORE_BYPASS_CMD_AUTO; | |
485 | } | |
486 | } | |
487 | ||
488 | return cmd; | |
489 | } | |
490 | ||
dbe3009d | 491 | static enum ssb_mitigation __init __ssb_select_mitigation(void) |
e63490c8 KRW |
492 | { |
493 | enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE; | |
494 | enum ssb_mitigation_cmd cmd; | |
495 | ||
8fe36c9d | 496 | if (!boot_cpu_has(X86_FEATURE_SSBD)) |
e63490c8 KRW |
497 | return mode; |
498 | ||
499 | cmd = ssb_parse_cmdline(); | |
500 | if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) && | |
501 | (cmd == SPEC_STORE_BYPASS_CMD_NONE || | |
502 | cmd == SPEC_STORE_BYPASS_CMD_AUTO)) | |
503 | return mode; | |
504 | ||
505 | switch (cmd) { | |
506 | case SPEC_STORE_BYPASS_CMD_AUTO: | |
c7416003 KC |
507 | case SPEC_STORE_BYPASS_CMD_SECCOMP: |
508 | /* | |
509 | * Choose prctl+seccomp as the default mode if seccomp is | |
510 | * enabled. | |
511 | */ | |
512 | if (IS_ENABLED(CONFIG_SECCOMP)) | |
513 | mode = SPEC_STORE_BYPASS_SECCOMP; | |
514 | else | |
515 | mode = SPEC_STORE_BYPASS_PRCTL; | |
574dcf89 | 516 | break; |
e63490c8 KRW |
517 | case SPEC_STORE_BYPASS_CMD_ON: |
518 | mode = SPEC_STORE_BYPASS_DISABLE; | |
519 | break; | |
574dcf89 TG |
520 | case SPEC_STORE_BYPASS_CMD_PRCTL: |
521 | mode = SPEC_STORE_BYPASS_PRCTL; | |
522 | break; | |
e63490c8 KRW |
523 | case SPEC_STORE_BYPASS_CMD_NONE: |
524 | break; | |
525 | } | |
526 | ||
23b9eab9 KRW |
527 | /* |
528 | * We have three CPU feature flags that are in play here: | |
529 | * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible. | |
8fe36c9d | 530 | * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass |
23b9eab9 KRW |
531 | * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation |
532 | */ | |
574dcf89 | 533 | if (mode == SPEC_STORE_BYPASS_DISABLE) { |
e63490c8 | 534 | setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE); |
23b9eab9 | 535 | /* |
3b881627 KRW |
536 | * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may |
537 | * use a completely different MSR and bit dependent on family. | |
23b9eab9 | 538 | */ |
5005c716 TL |
539 | if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) && |
540 | !static_cpu_has(X86_FEATURE_AMD_SSBD)) { | |
733c54c9 | 541 | x86_amd_ssb_disable(); |
5005c716 | 542 | } else { |
8fe36c9d | 543 | x86_spec_ctrl_base |= SPEC_CTRL_SSBD; |
e5f984ed | 544 | x86_spec_ctrl_mask |= SPEC_CTRL_SSBD; |
208efa83 | 545 | wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base); |
23b9eab9 KRW |
546 | } |
547 | } | |
548 | ||
e63490c8 KRW |
549 | return mode; |
550 | } | |
551 | ||
043d480e | 552 | static void ssb_select_mitigation(void) |
e63490c8 KRW |
553 | { |
554 | ssb_mode = __ssb_select_mitigation(); | |
555 | ||
556 | if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS)) | |
557 | pr_info("%s\n", ssb_strings[ssb_mode]); | |
558 | } | |
559 | ||
da285121 | 560 | #undef pr_fmt |
c7416003 | 561 | #define pr_fmt(fmt) "Speculation prctl: " fmt |
da285121 | 562 | |
199bfed2 | 563 | static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl) |
574dcf89 | 564 | { |
733f4234 | 565 | bool update; |
574dcf89 | 566 | |
c7416003 KC |
567 | if (ssb_mode != SPEC_STORE_BYPASS_PRCTL && |
568 | ssb_mode != SPEC_STORE_BYPASS_SECCOMP) | |
574dcf89 TG |
569 | return -ENXIO; |
570 | ||
733f4234 TG |
571 | switch (ctrl) { |
572 | case PR_SPEC_ENABLE: | |
573 | /* If speculation is force disabled, enable is not allowed */ | |
574 | if (task_spec_ssb_force_disable(task)) | |
575 | return -EPERM; | |
576 | task_clear_spec_ssb_disable(task); | |
8fe36c9d | 577 | update = test_and_clear_tsk_thread_flag(task, TIF_SSBD); |
733f4234 TG |
578 | break; |
579 | case PR_SPEC_DISABLE: | |
580 | task_set_spec_ssb_disable(task); | |
8fe36c9d | 581 | update = !test_and_set_tsk_thread_flag(task, TIF_SSBD); |
733f4234 TG |
582 | break; |
583 | case PR_SPEC_FORCE_DISABLE: | |
584 | task_set_spec_ssb_disable(task); | |
585 | task_set_spec_ssb_force_disable(task); | |
8fe36c9d | 586 | update = !test_and_set_tsk_thread_flag(task, TIF_SSBD); |
733f4234 TG |
587 | break; |
588 | default: | |
589 | return -ERANGE; | |
590 | } | |
574dcf89 | 591 | |
199bfed2 KC |
592 | /* |
593 | * If being set on non-current task, delay setting the CPU | |
594 | * mitigation until it is next scheduled. | |
595 | */ | |
733f4234 | 596 | if (task == current && update) |
8a3ba093 | 597 | speculative_store_bypass_update_current(); |
574dcf89 TG |
598 | |
599 | return 0; | |
600 | } | |
601 | ||
5b38e244 TG |
602 | int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which, |
603 | unsigned long ctrl) | |
604 | { | |
605 | switch (which) { | |
606 | case PR_SPEC_STORE_BYPASS: | |
607 | return ssb_prctl_set(task, ctrl); | |
608 | default: | |
609 | return -ENODEV; | |
610 | } | |
611 | } | |
612 | ||
613 | #ifdef CONFIG_SECCOMP | |
614 | void arch_seccomp_spec_mitigate(struct task_struct *task) | |
615 | { | |
c7416003 KC |
616 | if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP) |
617 | ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE); | |
5b38e244 TG |
618 | } |
619 | #endif | |
620 | ||
199bfed2 | 621 | static int ssb_prctl_get(struct task_struct *task) |
574dcf89 TG |
622 | { |
623 | switch (ssb_mode) { | |
624 | case SPEC_STORE_BYPASS_DISABLE: | |
625 | return PR_SPEC_DISABLE; | |
c7416003 | 626 | case SPEC_STORE_BYPASS_SECCOMP: |
574dcf89 | 627 | case SPEC_STORE_BYPASS_PRCTL: |
733f4234 TG |
628 | if (task_spec_ssb_force_disable(task)) |
629 | return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE; | |
630 | if (task_spec_ssb_disable(task)) | |
574dcf89 TG |
631 | return PR_SPEC_PRCTL | PR_SPEC_DISABLE; |
632 | return PR_SPEC_PRCTL | PR_SPEC_ENABLE; | |
633 | default: | |
634 | if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS)) | |
635 | return PR_SPEC_ENABLE; | |
636 | return PR_SPEC_NOT_AFFECTED; | |
637 | } | |
638 | } | |
639 | ||
199bfed2 | 640 | int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which) |
574dcf89 TG |
641 | { |
642 | switch (which) { | |
643 | case PR_SPEC_STORE_BYPASS: | |
199bfed2 | 644 | return ssb_prctl_get(task); |
574dcf89 TG |
645 | default: |
646 | return -ENODEV; | |
647 | } | |
648 | } | |
649 | ||
23b9eab9 KRW |
650 | void x86_spec_ctrl_setup_ap(void) |
651 | { | |
50f9b919 | 652 | if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) |
208efa83 | 653 | wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base); |
c37b94dd KRW |
654 | |
655 | if (ssb_mode == SPEC_STORE_BYPASS_DISABLE) | |
8fe36c9d | 656 | x86_amd_ssb_disable(); |
23b9eab9 KRW |
657 | } |
658 | ||
73ca9f47 KRW |
659 | #undef pr_fmt |
660 | #define pr_fmt(fmt) "L1TF: " fmt | |
b9cfedcd | 661 | |
24fcb53c JK |
662 | /* Default mitigation for L1TF-affected CPUs */ |
663 | enum l1tf_mitigations l1tf_mitigation __ro_after_init = L1TF_MITIGATION_FLUSH; | |
b9cfedcd | 664 | #if IS_ENABLED(CONFIG_KVM_INTEL) |
24fcb53c JK |
665 | EXPORT_SYMBOL_GPL(l1tf_mitigation); |
666 | ||
522c7bed | 667 | enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO; |
b9cfedcd TG |
668 | EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation); |
669 | #endif | |
670 | ||
f1f016ed AK |
671 | /* |
672 | * These CPUs all support 44bits physical address space internally in the | |
673 | * cache but CPUID can report a smaller number of physical address bits. | |
674 | * | |
675 | * The L1TF mitigation uses the top most address bit for the inversion of | |
676 | * non present PTEs. When the installed memory reaches into the top most | |
677 | * address bit due to memory holes, which has been observed on machines | |
678 | * which report 36bits physical address bits and have 32G RAM installed, | |
679 | * then the mitigation range check in l1tf_select_mitigation() triggers. | |
680 | * This is a false positive because the mitigation is still possible due to | |
681 | * the fact that the cache uses 44bit internally. Use the cache bits | |
682 | * instead of the reported physical bits and adjust them on the affected | |
683 | * machines to 44bit if the reported bits are less than 44. | |
684 | */ | |
685 | static void override_cache_bits(struct cpuinfo_x86 *c) | |
686 | { | |
687 | if (c->x86 != 6) | |
688 | return; | |
689 | ||
690 | switch (c->x86_model) { | |
691 | case INTEL_FAM6_NEHALEM: | |
692 | case INTEL_FAM6_WESTMERE: | |
693 | case INTEL_FAM6_SANDYBRIDGE: | |
694 | case INTEL_FAM6_IVYBRIDGE: | |
695 | case INTEL_FAM6_HASWELL_CORE: | |
696 | case INTEL_FAM6_HASWELL_ULT: | |
697 | case INTEL_FAM6_HASWELL_GT3E: | |
698 | case INTEL_FAM6_BROADWELL_CORE: | |
699 | case INTEL_FAM6_BROADWELL_GT3E: | |
700 | case INTEL_FAM6_SKYLAKE_MOBILE: | |
701 | case INTEL_FAM6_SKYLAKE_DESKTOP: | |
702 | case INTEL_FAM6_KABYLAKE_MOBILE: | |
703 | case INTEL_FAM6_KABYLAKE_DESKTOP: | |
704 | if (c->x86_cache_bits < 44) | |
705 | c->x86_cache_bits = 44; | |
706 | break; | |
707 | } | |
708 | } | |
709 | ||
73ca9f47 KRW |
710 | static void __init l1tf_select_mitigation(void) |
711 | { | |
712 | u64 half_pa; | |
713 | ||
714 | if (!boot_cpu_has_bug(X86_BUG_L1TF)) | |
715 | return; | |
716 | ||
f1f016ed AK |
717 | override_cache_bits(&boot_cpu_data); |
718 | ||
24fcb53c JK |
719 | switch (l1tf_mitigation) { |
720 | case L1TF_MITIGATION_OFF: | |
721 | case L1TF_MITIGATION_FLUSH_NOWARN: | |
722 | case L1TF_MITIGATION_FLUSH: | |
723 | break; | |
724 | case L1TF_MITIGATION_FLUSH_NOSMT: | |
725 | case L1TF_MITIGATION_FULL: | |
726 | cpu_smt_disable(false); | |
727 | break; | |
728 | case L1TF_MITIGATION_FULL_FORCE: | |
729 | cpu_smt_disable(true); | |
730 | break; | |
731 | } | |
732 | ||
73ca9f47 KRW |
733 | #if CONFIG_PGTABLE_LEVELS == 2 |
734 | pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n"); | |
735 | return; | |
736 | #endif | |
737 | ||
73ca9f47 KRW |
738 | half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT; |
739 | if (e820__mapped_any(half_pa, ULLONG_MAX - half_pa, E820_TYPE_RAM)) { | |
740 | pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n"); | |
741 | return; | |
742 | } | |
743 | ||
744 | setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV); | |
745 | } | |
24fcb53c JK |
746 | |
747 | static int __init l1tf_cmdline(char *str) | |
748 | { | |
749 | if (!boot_cpu_has_bug(X86_BUG_L1TF)) | |
750 | return 0; | |
751 | ||
752 | if (!str) | |
753 | return -EINVAL; | |
754 | ||
755 | if (!strcmp(str, "off")) | |
756 | l1tf_mitigation = L1TF_MITIGATION_OFF; | |
757 | else if (!strcmp(str, "flush,nowarn")) | |
758 | l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOWARN; | |
759 | else if (!strcmp(str, "flush")) | |
760 | l1tf_mitigation = L1TF_MITIGATION_FLUSH; | |
761 | else if (!strcmp(str, "flush,nosmt")) | |
762 | l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT; | |
763 | else if (!strcmp(str, "full")) | |
764 | l1tf_mitigation = L1TF_MITIGATION_FULL; | |
765 | else if (!strcmp(str, "full,force")) | |
766 | l1tf_mitigation = L1TF_MITIGATION_FULL_FORCE; | |
767 | ||
768 | return 0; | |
769 | } | |
770 | early_param("l1tf", l1tf_cmdline); | |
771 | ||
73ca9f47 KRW |
772 | #undef pr_fmt |
773 | ||
61dc0f55 | 774 | #ifdef CONFIG_SYSFS |
d2b8fc2d | 775 | |
b9cfedcd TG |
776 | #define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion" |
777 | ||
778 | #if IS_ENABLED(CONFIG_KVM_INTEL) | |
779 | static const char *l1tf_vmx_states[] = { | |
1ead4979 TG |
780 | [VMENTER_L1D_FLUSH_AUTO] = "auto", |
781 | [VMENTER_L1D_FLUSH_NEVER] = "vulnerable", | |
782 | [VMENTER_L1D_FLUSH_COND] = "conditional cache flushes", | |
783 | [VMENTER_L1D_FLUSH_ALWAYS] = "cache flushes", | |
784 | [VMENTER_L1D_FLUSH_EPT_DISABLED] = "EPT disabled", | |
364a4311 | 785 | [VMENTER_L1D_FLUSH_NOT_REQUIRED] = "flush not necessary" |
b9cfedcd TG |
786 | }; |
787 | ||
788 | static ssize_t l1tf_show_state(char *buf) | |
789 | { | |
790 | if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) | |
791 | return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG); | |
792 | ||
3899f7b1 PB |
793 | if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_EPT_DISABLED || |
794 | (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER && | |
795 | cpu_smt_control == CPU_SMT_ENABLED)) | |
796 | return sprintf(buf, "%s; VMX: %s\n", L1TF_DEFAULT_MSG, | |
797 | l1tf_vmx_states[l1tf_vmx_mitigation]); | |
798 | ||
799 | return sprintf(buf, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG, | |
800 | l1tf_vmx_states[l1tf_vmx_mitigation], | |
801 | cpu_smt_control == CPU_SMT_ENABLED ? "vulnerable" : "disabled"); | |
b9cfedcd TG |
802 | } |
803 | #else | |
804 | static ssize_t l1tf_show_state(char *buf) | |
805 | { | |
806 | return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG); | |
807 | } | |
808 | #endif | |
809 | ||
ace051d5 | 810 | static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr, |
043d480e | 811 | char *buf, unsigned int bug) |
61dc0f55 | 812 | { |
d2b8fc2d | 813 | if (!boot_cpu_has_bug(bug)) |
61dc0f55 | 814 | return sprintf(buf, "Not affected\n"); |
d2b8fc2d KRW |
815 | |
816 | switch (bug) { | |
817 | case X86_BUG_CPU_MELTDOWN: | |
818 | if (boot_cpu_has(X86_FEATURE_PTI)) | |
819 | return sprintf(buf, "Mitigation: PTI\n"); | |
820 | ||
821 | break; | |
822 | ||
823 | case X86_BUG_SPECTRE_V1: | |
824 | return sprintf(buf, "Mitigation: __user pointer sanitization\n"); | |
825 | ||
826 | case X86_BUG_SPECTRE_V2: | |
827 | return sprintf(buf, "%s%s%s%s\n", spectre_v2_strings[spectre_v2_enabled], | |
828 | boot_cpu_has(X86_FEATURE_USE_IBPB) ? ", IBPB" : "", | |
829 | boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "", | |
830 | spectre_v2_module_string()); | |
831 | ||
e63490c8 KRW |
832 | case X86_BUG_SPEC_STORE_BYPASS: |
833 | return sprintf(buf, "%s\n", ssb_strings[ssb_mode]); | |
834 | ||
05516ad8 AK |
835 | case X86_BUG_L1TF: |
836 | if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV)) | |
b9cfedcd | 837 | return l1tf_show_state(buf); |
05516ad8 | 838 | break; |
d2b8fc2d KRW |
839 | default: |
840 | break; | |
841 | } | |
842 | ||
61dc0f55 TG |
843 | return sprintf(buf, "Vulnerable\n"); |
844 | } | |
845 | ||
d2b8fc2d KRW |
846 | ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf) |
847 | { | |
848 | return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN); | |
849 | } | |
850 | ||
713f1b95 | 851 | ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf) |
61dc0f55 | 852 | { |
d2b8fc2d | 853 | return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1); |
61dc0f55 TG |
854 | } |
855 | ||
713f1b95 | 856 | ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf) |
61dc0f55 | 857 | { |
d2b8fc2d | 858 | return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2); |
61dc0f55 | 859 | } |
d7de9182 KRW |
860 | |
861 | ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf) | |
862 | { | |
863 | return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS); | |
864 | } | |
05516ad8 AK |
865 | |
866 | ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf) | |
867 | { | |
868 | return cpu_show_common(dev, attr, buf, X86_BUG_L1TF); | |
869 | } | |
61dc0f55 | 870 | #endif |