]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/kernel/cpu/bugs.c
cpu/hotplug: Set CPU_SMT_NOT_SUPPORTED early
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / kernel / cpu / bugs.c
CommitLineData
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 33static void __init spectre_v2_select_mitigation(void);
e63490c8 34static void __init ssb_select_mitigation(void);
05516ad8 35static 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 41u64 __ro_after_init x86_spec_ctrl_base;
4ac9b1f9 42EXPORT_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 48static 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 */
54u64 __ro_after_init x86_amd_ls_cfg_base;
8fe36c9d 55u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask;
c37b94dd 56
1353ebb4
JF
57void __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 */
65 cpu_smt_check_topology();
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 */
128enum 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
137static 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",
143};
144
145#undef pr_fmt
bbb5e08b 146#define pr_fmt(fmt) "Spectre V2 : " fmt
da285121 147
b5e6d77d
KC
148static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init =
149 SPECTRE_V2_NONE;
d280282b 150
12376b62
BP
151void
152x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest)
19fff03f 153{
e5f984ed 154 u64 msrval, guestval, hostval = x86_spec_ctrl_base;
12376b62 155 struct thread_info *ti = current_thread_info();
5407b7f8 156
50f9b919 157 /* Is MSR_SPEC_CTRL implemented ? */
12376b62 158 if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) {
e5f984ed
TG
159 /*
160 * Restrict guest_spec_ctrl to supported values. Clear the
161 * modifiable bits in the host base value and or the
162 * modifiable bits from the guest value.
163 */
164 guestval = hostval & ~x86_spec_ctrl_mask;
165 guestval |= guest_spec_ctrl & x86_spec_ctrl_mask;
166
12376b62
BP
167 /* SSBD controlled in MSR_SPEC_CTRL */
168 if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
e5f984ed 169 hostval |= ssbd_tif_to_spec_ctrl(ti->flags);
12376b62 170
e5f984ed
TG
171 if (hostval != guestval) {
172 msrval = setguest ? guestval : hostval;
173 wrmsrl(MSR_IA32_SPEC_CTRL, msrval);
12376b62
BP
174 }
175 }
1238ed31
TG
176
177 /*
178 * If SSBD is not handled in MSR_SPEC_CTRL on AMD, update
179 * MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported.
180 */
181 if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
182 !static_cpu_has(X86_FEATURE_VIRT_SSBD))
183 return;
184
185 /*
186 * If the host has SSBD mitigation enabled, force it in the host's
187 * virtual MSR value. If its not permanently enabled, evaluate
188 * current's TIF_SSBD thread flag.
189 */
190 if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE))
191 hostval = SPEC_CTRL_SSBD;
192 else
193 hostval = ssbd_tif_to_spec_ctrl(ti->flags);
194
195 /* Sanitize the guest value */
196 guestval = guest_virt_spec_ctrl & SPEC_CTRL_SSBD;
197
198 if (hostval != guestval) {
199 unsigned long tif;
200
201 tif = setguest ? ssbd_spec_ctrl_to_tif(guestval) :
202 ssbd_spec_ctrl_to_tif(hostval);
203
204 speculative_store_bypass_update(tif);
205 }
19fff03f 206}
12376b62 207EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl);
19fff03f 208
8fe36c9d 209static void x86_amd_ssb_disable(void)
c37b94dd 210{
8fe36c9d 211 u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_ssbd_mask;
c37b94dd 212
65e02bbd
TL
213 if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
214 wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, SPEC_CTRL_SSBD);
215 else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
c37b94dd
KRW
216 wrmsrl(MSR_AMD64_LS_CFG, msrval);
217}
218
d280282b 219#ifdef RETPOLINE
bb3c2578
TG
220static bool spectre_v2_bad_module;
221
d280282b
AK
222bool retpoline_module_ok(bool has_retpoline)
223{
224 if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
225 return true;
226
c8b8e109 227 pr_err("System may be vulnerable to spectre v2\n");
d280282b
AK
228 spectre_v2_bad_module = true;
229 return false;
230}
bb3c2578
TG
231
232static inline const char *spectre_v2_module_string(void)
233{
234 return spectre_v2_bad_module ? " - vulnerable module loaded" : "";
235}
236#else
237static inline const char *spectre_v2_module_string(void) { return ""; }
d280282b 238#endif
da285121
DW
239
240static void __init spec2_print_if_insecure(const char *reason)
241{
242 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
17c33e7c 243 pr_info("%s selected on command line.\n", reason);
da285121
DW
244}
245
246static void __init spec2_print_if_secure(const char *reason)
247{
248 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
17c33e7c 249 pr_info("%s selected on command line.\n", reason);
da285121
DW
250}
251
252static inline bool retp_compiler(void)
253{
254 return __is_defined(RETPOLINE);
255}
256
257static inline bool match_option(const char *arg, int arglen, const char *opt)
258{
259 int len = strlen(opt);
260
261 return len == arglen && !strncmp(arg, opt, len);
262}
263
17c33e7c
KA
264static const struct {
265 const char *option;
266 enum spectre_v2_mitigation_cmd cmd;
267 bool secure;
268} mitigation_options[] = {
269 { "off", SPECTRE_V2_CMD_NONE, false },
270 { "on", SPECTRE_V2_CMD_FORCE, true },
271 { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false },
272 { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_AMD, false },
273 { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
274 { "auto", SPECTRE_V2_CMD_AUTO, false },
275};
276
da285121
DW
277static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
278{
279 char arg[20];
17c33e7c
KA
280 int ret, i;
281 enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
282
283 if (cmdline_find_option_bool(boot_command_line, "nospectre_v2"))
284 return SPECTRE_V2_CMD_NONE;
285 else {
713f1b95 286 ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
17c33e7c
KA
287 if (ret < 0)
288 return SPECTRE_V2_CMD_AUTO;
289
290 for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
291 if (!match_option(arg, ret, mitigation_options[i].option))
292 continue;
293 cmd = mitigation_options[i].cmd;
294 break;
295 }
296
297 if (i >= ARRAY_SIZE(mitigation_options)) {
ecad7915 298 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
da285121
DW
299 return SPECTRE_V2_CMD_AUTO;
300 }
301 }
302
17c33e7c
KA
303 if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
304 cmd == SPECTRE_V2_CMD_RETPOLINE_AMD ||
305 cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC) &&
306 !IS_ENABLED(CONFIG_RETPOLINE)) {
713f1b95 307 pr_err("%s selected but not compiled in. Switching to AUTO select\n", mitigation_options[i].option);
da285121 308 return SPECTRE_V2_CMD_AUTO;
17c33e7c
KA
309 }
310
311 if (cmd == SPECTRE_V2_CMD_RETPOLINE_AMD &&
312 boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
313 pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n");
314 return SPECTRE_V2_CMD_AUTO;
315 }
316
317 if (mitigation_options[i].secure)
318 spec2_print_if_secure(mitigation_options[i].option);
319 else
320 spec2_print_if_insecure(mitigation_options[i].option);
321
322 return cmd;
da285121
DW
323}
324
c995efd5
DW
325/* Check for Skylake-like CPUs (for RSB handling) */
326static bool __init is_skylake_era(void)
327{
328 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
329 boot_cpu_data.x86 == 6) {
330 switch (boot_cpu_data.x86_model) {
331 case INTEL_FAM6_SKYLAKE_MOBILE:
332 case INTEL_FAM6_SKYLAKE_DESKTOP:
333 case INTEL_FAM6_SKYLAKE_X:
334 case INTEL_FAM6_KABYLAKE_MOBILE:
335 case INTEL_FAM6_KABYLAKE_DESKTOP:
336 return true;
337 }
338 }
339 return false;
340}
341
da285121
DW
342static void __init spectre_v2_select_mitigation(void)
343{
344 enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
345 enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
346
347 /*
348 * If the CPU is not affected and the command line mode is NONE or AUTO
349 * then nothing to do.
350 */
351 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
352 (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
353 return;
354
355 switch (cmd) {
356 case SPECTRE_V2_CMD_NONE:
357 return;
358
359 case SPECTRE_V2_CMD_FORCE:
da285121 360 case SPECTRE_V2_CMD_AUTO:
d0f293e0
DL
361 if (IS_ENABLED(CONFIG_RETPOLINE))
362 goto retpoline_auto;
363 break;
da285121
DW
364 case SPECTRE_V2_CMD_RETPOLINE_AMD:
365 if (IS_ENABLED(CONFIG_RETPOLINE))
366 goto retpoline_amd;
367 break;
368 case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
369 if (IS_ENABLED(CONFIG_RETPOLINE))
370 goto retpoline_generic;
371 break;
372 case SPECTRE_V2_CMD_RETPOLINE:
373 if (IS_ENABLED(CONFIG_RETPOLINE))
374 goto retpoline_auto;
375 break;
376 }
713f1b95 377 pr_err("Spectre mitigation: kernel not compiled with retpoline; no mitigation available!");
da285121
DW
378 return;
379
380retpoline_auto:
381 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
382 retpoline_amd:
383 if (!boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
713f1b95 384 pr_err("Spectre mitigation: LFENCE not serializing, switching to generic retpoline\n");
da285121
DW
385 goto retpoline_generic;
386 }
387 mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_AMD :
388 SPECTRE_V2_RETPOLINE_MINIMAL_AMD;
389 setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD);
390 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
391 } else {
392 retpoline_generic:
393 mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_GENERIC :
394 SPECTRE_V2_RETPOLINE_MINIMAL;
395 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
396 }
397
398 spectre_v2_enabled = mode;
399 pr_info("%s\n", spectre_v2_strings[mode]);
c995efd5
DW
400
401 /*
713f1b95 402 * If neither SMEP nor PTI are available, there is a risk of
c995efd5
DW
403 * hitting userspace addresses in the RSB after a context switch
404 * from a shallow call stack to a deeper one. To prevent this fill
405 * the entire RSB, even when using IBRS.
406 *
407 * Skylake era CPUs have a separate issue with *underflow* of the
408 * RSB, when they will predict 'ret' targets from the generic BTB.
409 * The proper mitigation for this is IBRS. If IBRS is not supported
410 * or deactivated in favour of retpolines the RSB fill on context
411 * switch is required.
412 */
413 if ((!boot_cpu_has(X86_FEATURE_PTI) &&
414 !boot_cpu_has(X86_FEATURE_SMEP)) || is_skylake_era()) {
415 setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
713f1b95 416 pr_info("Spectre v2 mitigation: Filling RSB on context switch\n");
c995efd5 417 }
bd12e896
DW
418
419 /* Initialize Indirect Branch Prediction Barrier if supported */
581abf91
DW
420 if (boot_cpu_has(X86_FEATURE_IBPB)) {
421 setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
713f1b95 422 pr_info("Spectre v2 mitigation: Enabling Indirect Branch Prediction Barrier\n");
bd12e896 423 }
390b99c3
DW
424
425 /*
426 * Retpoline means the kernel is safe because it has no indirect
427 * branches. But firmware isn't, so use IBRS to protect that.
428 */
429 if (boot_cpu_has(X86_FEATURE_IBRS)) {
430 setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
431 pr_info("Enabling Restricted Speculation for firmware calls\n");
432 }
da285121
DW
433}
434
e63490c8
KRW
435#undef pr_fmt
436#define pr_fmt(fmt) "Speculative Store Bypass: " fmt
437
b5e6d77d 438static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
e63490c8
KRW
439
440/* The kernel command line selection */
441enum ssb_mitigation_cmd {
442 SPEC_STORE_BYPASS_CMD_NONE,
443 SPEC_STORE_BYPASS_CMD_AUTO,
444 SPEC_STORE_BYPASS_CMD_ON,
574dcf89 445 SPEC_STORE_BYPASS_CMD_PRCTL,
c7416003 446 SPEC_STORE_BYPASS_CMD_SECCOMP,
e63490c8
KRW
447};
448
449static const char *ssb_strings[] = {
450 [SPEC_STORE_BYPASS_NONE] = "Vulnerable",
574dcf89 451 [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass disabled",
c7416003
KC
452 [SPEC_STORE_BYPASS_PRCTL] = "Mitigation: Speculative Store Bypass disabled via prctl",
453 [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
e63490c8
KRW
454};
455
456static const struct {
457 const char *option;
458 enum ssb_mitigation_cmd cmd;
459} ssb_mitigation_options[] = {
c7416003
KC
460 { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */
461 { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */
462 { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */
463 { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative Store Bypass via prctl */
464 { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */
e63490c8
KRW
465};
466
467static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
468{
469 enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO;
470 char arg[20];
471 int ret, i;
472
473 if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable")) {
474 return SPEC_STORE_BYPASS_CMD_NONE;
475 } else {
476 ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable",
477 arg, sizeof(arg));
478 if (ret < 0)
479 return SPEC_STORE_BYPASS_CMD_AUTO;
480
481 for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) {
482 if (!match_option(arg, ret, ssb_mitigation_options[i].option))
483 continue;
484
485 cmd = ssb_mitigation_options[i].cmd;
486 break;
487 }
488
489 if (i >= ARRAY_SIZE(ssb_mitigation_options)) {
490 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
491 return SPEC_STORE_BYPASS_CMD_AUTO;
492 }
493 }
494
495 return cmd;
496}
497
dbe3009d 498static enum ssb_mitigation __init __ssb_select_mitigation(void)
e63490c8
KRW
499{
500 enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
501 enum ssb_mitigation_cmd cmd;
502
8fe36c9d 503 if (!boot_cpu_has(X86_FEATURE_SSBD))
e63490c8
KRW
504 return mode;
505
506 cmd = ssb_parse_cmdline();
507 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) &&
508 (cmd == SPEC_STORE_BYPASS_CMD_NONE ||
509 cmd == SPEC_STORE_BYPASS_CMD_AUTO))
510 return mode;
511
512 switch (cmd) {
513 case SPEC_STORE_BYPASS_CMD_AUTO:
c7416003
KC
514 case SPEC_STORE_BYPASS_CMD_SECCOMP:
515 /*
516 * Choose prctl+seccomp as the default mode if seccomp is
517 * enabled.
518 */
519 if (IS_ENABLED(CONFIG_SECCOMP))
520 mode = SPEC_STORE_BYPASS_SECCOMP;
521 else
522 mode = SPEC_STORE_BYPASS_PRCTL;
574dcf89 523 break;
e63490c8
KRW
524 case SPEC_STORE_BYPASS_CMD_ON:
525 mode = SPEC_STORE_BYPASS_DISABLE;
526 break;
574dcf89
TG
527 case SPEC_STORE_BYPASS_CMD_PRCTL:
528 mode = SPEC_STORE_BYPASS_PRCTL;
529 break;
e63490c8
KRW
530 case SPEC_STORE_BYPASS_CMD_NONE:
531 break;
532 }
533
23b9eab9
KRW
534 /*
535 * We have three CPU feature flags that are in play here:
536 * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
8fe36c9d 537 * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
23b9eab9
KRW
538 * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
539 */
574dcf89 540 if (mode == SPEC_STORE_BYPASS_DISABLE) {
e63490c8 541 setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
23b9eab9
KRW
542 /*
543 * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD uses
544 * a completely different MSR and bit dependent on family.
545 */
546 switch (boot_cpu_data.x86_vendor) {
547 case X86_VENDOR_INTEL:
8fe36c9d 548 x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
e5f984ed 549 x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
208efa83 550 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
23b9eab9
KRW
551 break;
552 case X86_VENDOR_AMD:
8fe36c9d 553 x86_amd_ssb_disable();
23b9eab9
KRW
554 break;
555 }
556 }
557
e63490c8
KRW
558 return mode;
559}
560
043d480e 561static void ssb_select_mitigation(void)
e63490c8
KRW
562{
563 ssb_mode = __ssb_select_mitigation();
564
565 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
566 pr_info("%s\n", ssb_strings[ssb_mode]);
567}
568
da285121 569#undef pr_fmt
c7416003 570#define pr_fmt(fmt) "Speculation prctl: " fmt
da285121 571
199bfed2 572static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
574dcf89 573{
733f4234 574 bool update;
574dcf89 575
c7416003
KC
576 if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
577 ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
574dcf89
TG
578 return -ENXIO;
579
733f4234
TG
580 switch (ctrl) {
581 case PR_SPEC_ENABLE:
582 /* If speculation is force disabled, enable is not allowed */
583 if (task_spec_ssb_force_disable(task))
584 return -EPERM;
585 task_clear_spec_ssb_disable(task);
8fe36c9d 586 update = test_and_clear_tsk_thread_flag(task, TIF_SSBD);
733f4234
TG
587 break;
588 case PR_SPEC_DISABLE:
589 task_set_spec_ssb_disable(task);
8fe36c9d 590 update = !test_and_set_tsk_thread_flag(task, TIF_SSBD);
733f4234
TG
591 break;
592 case PR_SPEC_FORCE_DISABLE:
593 task_set_spec_ssb_disable(task);
594 task_set_spec_ssb_force_disable(task);
8fe36c9d 595 update = !test_and_set_tsk_thread_flag(task, TIF_SSBD);
733f4234
TG
596 break;
597 default:
598 return -ERANGE;
599 }
574dcf89 600
199bfed2
KC
601 /*
602 * If being set on non-current task, delay setting the CPU
603 * mitigation until it is next scheduled.
604 */
733f4234 605 if (task == current && update)
8a3ba093 606 speculative_store_bypass_update_current();
574dcf89
TG
607
608 return 0;
609}
610
5b38e244
TG
611int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
612 unsigned long ctrl)
613{
614 switch (which) {
615 case PR_SPEC_STORE_BYPASS:
616 return ssb_prctl_set(task, ctrl);
617 default:
618 return -ENODEV;
619 }
620}
621
622#ifdef CONFIG_SECCOMP
623void arch_seccomp_spec_mitigate(struct task_struct *task)
624{
c7416003
KC
625 if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP)
626 ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
5b38e244
TG
627}
628#endif
629
199bfed2 630static int ssb_prctl_get(struct task_struct *task)
574dcf89
TG
631{
632 switch (ssb_mode) {
633 case SPEC_STORE_BYPASS_DISABLE:
634 return PR_SPEC_DISABLE;
c7416003 635 case SPEC_STORE_BYPASS_SECCOMP:
574dcf89 636 case SPEC_STORE_BYPASS_PRCTL:
733f4234
TG
637 if (task_spec_ssb_force_disable(task))
638 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
639 if (task_spec_ssb_disable(task))
574dcf89
TG
640 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
641 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
642 default:
643 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
644 return PR_SPEC_ENABLE;
645 return PR_SPEC_NOT_AFFECTED;
646 }
647}
648
199bfed2 649int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
574dcf89
TG
650{
651 switch (which) {
652 case PR_SPEC_STORE_BYPASS:
199bfed2 653 return ssb_prctl_get(task);
574dcf89
TG
654 default:
655 return -ENODEV;
656 }
657}
658
23b9eab9
KRW
659void x86_spec_ctrl_setup_ap(void)
660{
50f9b919 661 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
208efa83 662 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
c37b94dd
KRW
663
664 if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
8fe36c9d 665 x86_amd_ssb_disable();
23b9eab9
KRW
666}
667
73ca9f47
KRW
668#undef pr_fmt
669#define pr_fmt(fmt) "L1TF: " fmt
b9cfedcd
TG
670
671#if IS_ENABLED(CONFIG_KVM_INTEL)
522c7bed 672enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
b9cfedcd
TG
673EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation);
674#endif
675
73ca9f47
KRW
676static void __init l1tf_select_mitigation(void)
677{
678 u64 half_pa;
679
680 if (!boot_cpu_has_bug(X86_BUG_L1TF))
681 return;
682
683#if CONFIG_PGTABLE_LEVELS == 2
684 pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
685 return;
686#endif
687
688 /*
689 * This is extremely unlikely to happen because almost all
690 * systems have far more MAX_PA/2 than RAM can be fit into
691 * DIMM slots.
692 */
693 half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT;
694 if (e820__mapped_any(half_pa, ULLONG_MAX - half_pa, E820_TYPE_RAM)) {
695 pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
696 return;
697 }
698
699 setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV);
700}
701#undef pr_fmt
702
61dc0f55 703#ifdef CONFIG_SYSFS
d2b8fc2d 704
b9cfedcd
TG
705#define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
706
707#if IS_ENABLED(CONFIG_KVM_INTEL)
708static const char *l1tf_vmx_states[] = {
1ead4979
TG
709 [VMENTER_L1D_FLUSH_AUTO] = "auto",
710 [VMENTER_L1D_FLUSH_NEVER] = "vulnerable",
711 [VMENTER_L1D_FLUSH_COND] = "conditional cache flushes",
712 [VMENTER_L1D_FLUSH_ALWAYS] = "cache flushes",
713 [VMENTER_L1D_FLUSH_EPT_DISABLED] = "EPT disabled",
b9cfedcd
TG
714};
715
716static ssize_t l1tf_show_state(char *buf)
717{
718 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO)
719 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
720
721 return sprintf(buf, "%s; VMX: SMT %s, L1D %s\n", L1TF_DEFAULT_MSG,
722 cpu_smt_control == CPU_SMT_ENABLED ? "vulnerable" : "disabled",
723 l1tf_vmx_states[l1tf_vmx_mitigation]);
724}
725#else
726static ssize_t l1tf_show_state(char *buf)
727{
728 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
729}
730#endif
731
ace051d5 732static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
043d480e 733 char *buf, unsigned int bug)
61dc0f55 734{
d2b8fc2d 735 if (!boot_cpu_has_bug(bug))
61dc0f55 736 return sprintf(buf, "Not affected\n");
d2b8fc2d
KRW
737
738 switch (bug) {
739 case X86_BUG_CPU_MELTDOWN:
740 if (boot_cpu_has(X86_FEATURE_PTI))
741 return sprintf(buf, "Mitigation: PTI\n");
742
743 break;
744
745 case X86_BUG_SPECTRE_V1:
746 return sprintf(buf, "Mitigation: __user pointer sanitization\n");
747
748 case X86_BUG_SPECTRE_V2:
749 return sprintf(buf, "%s%s%s%s\n", spectre_v2_strings[spectre_v2_enabled],
750 boot_cpu_has(X86_FEATURE_USE_IBPB) ? ", IBPB" : "",
751 boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
752 spectre_v2_module_string());
753
e63490c8
KRW
754 case X86_BUG_SPEC_STORE_BYPASS:
755 return sprintf(buf, "%s\n", ssb_strings[ssb_mode]);
756
05516ad8
AK
757 case X86_BUG_L1TF:
758 if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV))
b9cfedcd 759 return l1tf_show_state(buf);
05516ad8 760 break;
d2b8fc2d
KRW
761 default:
762 break;
763 }
764
61dc0f55
TG
765 return sprintf(buf, "Vulnerable\n");
766}
767
d2b8fc2d
KRW
768ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
769{
770 return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
771}
772
713f1b95 773ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
61dc0f55 774{
d2b8fc2d 775 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
61dc0f55
TG
776}
777
713f1b95 778ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
61dc0f55 779{
d2b8fc2d 780 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
61dc0f55 781}
d7de9182
KRW
782
783ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
784{
785 return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS);
786}
05516ad8
AK
787
788ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
789{
790 return cpu_show_common(dev, attr, buf, X86_BUG_L1TF);
791}
61dc0f55 792#endif