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