]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/kernel/cpu/bugs.c
s390/speculation: Support 'mitigations=' cmdline option
[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>
fc8944e3 17#include <linux/sched/smt.h>
da285121 18
d7a6a163 19#include <asm/spec-ctrl.h>
da285121 20#include <asm/cmdline.h>
91eb1b79 21#include <asm/bugs.h>
1353ebb4 22#include <asm/processor.h>
7ebad705 23#include <asm/processor-flags.h>
952f07ec 24#include <asm/fpu/internal.h>
1353ebb4 25#include <asm/msr.h>
b9cfedcd 26#include <asm/vmx.h>
1353ebb4
JF
27#include <asm/paravirt.h>
28#include <asm/alternative.h>
62a67e12 29#include <asm/pgtable.h>
d1163651 30#include <asm/set_memory.h>
c995efd5 31#include <asm/intel-family.h>
05516ad8 32#include <asm/e820/api.h>
1122cc78 33#include <asm/hypervisor.h>
1353ebb4 34
da285121 35static void __init spectre_v2_select_mitigation(void);
e63490c8 36static void __init ssb_select_mitigation(void);
05516ad8 37static void __init l1tf_select_mitigation(void);
fc5ee888 38static void __init mds_select_mitigation(void);
da285121 39
7011f443
JK
40/* The base value of the SPEC_CTRL MSR that always has to be preserved. */
41u64 x86_spec_ctrl_base;
4ac9b1f9 42EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
7011f443 43static DEFINE_MUTEX(spec_ctrl_mutex);
296b454a 44
d0c3bedd
KRW
45/*
46 * The vendor and possibly platform specific bits which can be modified in
47 * x86_spec_ctrl_base.
48 */
e5f984ed 49static u64 __ro_after_init x86_spec_ctrl_mask = SPEC_CTRL_IBRS;
d0c3bedd 50
c37b94dd
KRW
51/*
52 * AMD specific MSR info for Speculative Store Bypass control.
8fe36c9d 53 * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu().
c37b94dd
KRW
54 */
55u64 __ro_after_init x86_amd_ls_cfg_base;
8fe36c9d 56u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask;
c37b94dd 57
8edb388f 58/* Control conditional STIBP in switch_to() */
6cd930f0 59DEFINE_STATIC_KEY_FALSE(switch_to_cond_stibp);
58b2d4c8
TG
60/* Control conditional IBPB in switch_mm() */
61DEFINE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
62/* Control unconditional IBPB in switch_mm() */
63DEFINE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
6cd930f0 64
5ab15133
TG
65/* Control MDS CPU buffer clear before returning to user space */
66DEFINE_STATIC_KEY_FALSE(mds_user_clear);
abf9852b 67EXPORT_SYMBOL_GPL(mds_user_clear);
f3eb8f09
TG
68/* Control MDS CPU buffer clear before idling (halt, mwait) */
69DEFINE_STATIC_KEY_FALSE(mds_idle_clear);
70EXPORT_SYMBOL_GPL(mds_idle_clear);
5ab15133 71
1353ebb4
JF
72void __init check_bugs(void)
73{
74 identify_boot_cpu();
55a36b65 75
483ec3c6
TG
76 /*
77 * identify_boot_cpu() initialized SMT support information, let the
78 * core code know.
79 */
ade31b9e 80 cpu_smt_check_topology_early();
483ec3c6 81
62a67e12
BP
82 if (!IS_ENABLED(CONFIG_SMP)) {
83 pr_info("CPU: ");
84 print_cpu_info(&boot_cpu_data);
85 }
86
296b454a
KRW
87 /*
88 * Read the SPEC_CTRL MSR to account for reserved bits which may
c37b94dd
KRW
89 * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
90 * init code as it is not enumerated and depends on the family.
296b454a 91 */
50f9b919 92 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
296b454a
KRW
93 rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
94
e5f984ed
TG
95 /* Allow STIBP in MSR_SPEC_CTRL if supported */
96 if (boot_cpu_has(X86_FEATURE_STIBP))
97 x86_spec_ctrl_mask |= SPEC_CTRL_STIBP;
98
da285121
DW
99 /* Select the proper spectre mitigation before patching alternatives */
100 spectre_v2_select_mitigation();
101
e63490c8
KRW
102 /*
103 * Select proper mitigation for any exposure to the Speculative Store
104 * Bypass vulnerability.
105 */
106 ssb_select_mitigation();
107
05516ad8
AK
108 l1tf_select_mitigation();
109
fc5ee888
TG
110 mds_select_mitigation();
111
15ff73d9
JP
112 arch_smt_update();
113
62a67e12 114#ifdef CONFIG_X86_32
55a36b65
BP
115 /*
116 * Check whether we are able to run this kernel safely on SMP.
117 *
118 * - i386 is no longer supported.
119 * - In order to run on anything without a TSC, we need to be
120 * compiled for a i486.
121 */
122 if (boot_cpu_data.x86 < 4)
123 panic("Kernel requires i486+ for 'invlpg' and other features");
124
bfe4bb15
MV
125 init_utsname()->machine[1] =
126 '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
1353ebb4 127 alternative_instructions();
304bceda 128
4d164092 129 fpu__init_check_bugs();
62a67e12
BP
130#else /* CONFIG_X86_64 */
131 alternative_instructions();
132
133 /*
134 * Make sure the first 2MB area is not mapped by huge pages
135 * There are typically fixed size MTRRs in there and overlapping
136 * MTRRs into large pages causes slow downs.
137 *
138 * Right now we don't do that with gbpages because there seems
139 * very little benefit for that case.
140 */
141 if (!direct_gbpages)
142 set_memory_4k((unsigned long)__va(0), 1);
143#endif
1353ebb4 144}
61dc0f55 145
12376b62
BP
146void
147x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest)
19fff03f 148{
e5f984ed 149 u64 msrval, guestval, hostval = x86_spec_ctrl_base;
12376b62 150 struct thread_info *ti = current_thread_info();
5407b7f8 151
50f9b919 152 /* Is MSR_SPEC_CTRL implemented ? */
12376b62 153 if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) {
e5f984ed
TG
154 /*
155 * Restrict guest_spec_ctrl to supported values. Clear the
156 * modifiable bits in the host base value and or the
157 * modifiable bits from the guest value.
158 */
159 guestval = hostval & ~x86_spec_ctrl_mask;
160 guestval |= guest_spec_ctrl & x86_spec_ctrl_mask;
161
12376b62 162 /* SSBD controlled in MSR_SPEC_CTRL */
5005c716
TL
163 if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
164 static_cpu_has(X86_FEATURE_AMD_SSBD))
e5f984ed 165 hostval |= ssbd_tif_to_spec_ctrl(ti->flags);
12376b62 166
e0b04783
TC
167 /* Conditional STIBP enabled? */
168 if (static_branch_unlikely(&switch_to_cond_stibp))
169 hostval |= stibp_tif_to_spec_ctrl(ti->flags);
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
8fce7184 204 speculation_ctrl_update(tif);
1238ed31 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
fc5ee888
TG
219#undef pr_fmt
220#define pr_fmt(fmt) "MDS: " fmt
221
64adee4f 222/* Default mitigation for MDS-affected CPUs */
fc5ee888 223static enum mds_mitigations mds_mitigation __ro_after_init = MDS_MITIGATION_FULL;
e2f3c337 224static bool mds_nosmt __ro_after_init = false;
fc5ee888
TG
225
226static const char * const mds_strings[] = {
227 [MDS_MITIGATION_OFF] = "Vulnerable",
ebf1e8cb
TG
228 [MDS_MITIGATION_FULL] = "Mitigation: Clear CPU buffers",
229 [MDS_MITIGATION_VMWERV] = "Vulnerable: Clear CPU buffers attempted, no microcode",
fc5ee888
TG
230};
231
232static void mds_select_mitigation(void)
233{
234 if (!boot_cpu_has_bug(X86_BUG_MDS)) {
235 mds_mitigation = MDS_MITIGATION_OFF;
236 return;
237 }
238
239 if (mds_mitigation == MDS_MITIGATION_FULL) {
ebf1e8cb
TG
240 if (!boot_cpu_has(X86_FEATURE_MD_CLEAR))
241 mds_mitigation = MDS_MITIGATION_VMWERV;
e2f3c337 242
ebf1e8cb 243 static_branch_enable(&mds_user_clear);
e2f3c337
JP
244
245 if (mds_nosmt && !boot_cpu_has(X86_BUG_MSBDS_ONLY))
246 cpu_smt_disable(false);
fc5ee888 247 }
e2f3c337 248
fc5ee888
TG
249 pr_info("%s\n", mds_strings[mds_mitigation]);
250}
251
252static int __init mds_cmdline(char *str)
253{
254 if (!boot_cpu_has_bug(X86_BUG_MDS))
255 return 0;
256
257 if (!str)
258 return -EINVAL;
259
260 if (!strcmp(str, "off"))
261 mds_mitigation = MDS_MITIGATION_OFF;
262 else if (!strcmp(str, "full"))
263 mds_mitigation = MDS_MITIGATION_FULL;
e2f3c337
JP
264 else if (!strcmp(str, "full,nosmt")) {
265 mds_mitigation = MDS_MITIGATION_FULL;
266 mds_nosmt = true;
267 }
fc5ee888
TG
268
269 return 0;
270}
271early_param("mds", mds_cmdline);
272
aef0bebc
TG
273#undef pr_fmt
274#define pr_fmt(fmt) "Spectre V2 : " fmt
275
276static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init =
277 SPECTRE_V2_NONE;
278
6cd930f0
TG
279static enum spectre_v2_user_mitigation spectre_v2_user __ro_after_init =
280 SPECTRE_V2_USER_NONE;
281
e9a556ad 282#ifdef CONFIG_RETPOLINE
bb3c2578
TG
283static bool spectre_v2_bad_module;
284
d280282b
AK
285bool retpoline_module_ok(bool has_retpoline)
286{
287 if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
288 return true;
289
c8b8e109 290 pr_err("System may be vulnerable to spectre v2\n");
d280282b
AK
291 spectre_v2_bad_module = true;
292 return false;
293}
bb3c2578
TG
294
295static inline const char *spectre_v2_module_string(void)
296{
297 return spectre_v2_bad_module ? " - vulnerable module loaded" : "";
298}
299#else
300static inline const char *spectre_v2_module_string(void) { return ""; }
d280282b 301#endif
da285121 302
da285121
DW
303static inline bool match_option(const char *arg, int arglen, const char *opt)
304{
305 int len = strlen(opt);
306
307 return len == arglen && !strncmp(arg, opt, len);
308}
309
aef0bebc
TG
310/* The kernel command line selection for spectre v2 */
311enum spectre_v2_mitigation_cmd {
312 SPECTRE_V2_CMD_NONE,
313 SPECTRE_V2_CMD_AUTO,
314 SPECTRE_V2_CMD_FORCE,
315 SPECTRE_V2_CMD_RETPOLINE,
316 SPECTRE_V2_CMD_RETPOLINE_GENERIC,
317 SPECTRE_V2_CMD_RETPOLINE_AMD,
318};
319
6cd930f0
TG
320enum spectre_v2_user_cmd {
321 SPECTRE_V2_USER_CMD_NONE,
322 SPECTRE_V2_USER_CMD_AUTO,
323 SPECTRE_V2_USER_CMD_FORCE,
2cdf6a58 324 SPECTRE_V2_USER_CMD_PRCTL,
fd1b7023 325 SPECTRE_V2_USER_CMD_PRCTL_IBPB,
1c3cf627 326 SPECTRE_V2_USER_CMD_SECCOMP,
fd1b7023 327 SPECTRE_V2_USER_CMD_SECCOMP_IBPB,
6cd930f0
TG
328};
329
330static const char * const spectre_v2_user_strings[] = {
fbed8eca
TL
331 [SPECTRE_V2_USER_NONE] = "User space: Vulnerable",
332 [SPECTRE_V2_USER_STRICT] = "User space: Mitigation: STIBP protection",
333 [SPECTRE_V2_USER_STRICT_PREFERRED] = "User space: Mitigation: STIBP always-on protection",
334 [SPECTRE_V2_USER_PRCTL] = "User space: Mitigation: STIBP via prctl",
335 [SPECTRE_V2_USER_SECCOMP] = "User space: Mitigation: STIBP via seccomp and prctl",
6cd930f0
TG
336};
337
338static const struct {
339 const char *option;
340 enum spectre_v2_user_cmd cmd;
341 bool secure;
342} v2_user_options[] __initdata = {
fd1b7023
TG
343 { "auto", SPECTRE_V2_USER_CMD_AUTO, false },
344 { "off", SPECTRE_V2_USER_CMD_NONE, false },
345 { "on", SPECTRE_V2_USER_CMD_FORCE, true },
346 { "prctl", SPECTRE_V2_USER_CMD_PRCTL, false },
347 { "prctl,ibpb", SPECTRE_V2_USER_CMD_PRCTL_IBPB, false },
348 { "seccomp", SPECTRE_V2_USER_CMD_SECCOMP, false },
349 { "seccomp,ibpb", SPECTRE_V2_USER_CMD_SECCOMP_IBPB, false },
6cd930f0
TG
350};
351
352static void __init spec_v2_user_print_cond(const char *reason, bool secure)
353{
354 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
355 pr_info("spectre_v2_user=%s forced on command line.\n", reason);
356}
357
358static enum spectre_v2_user_cmd __init
359spectre_v2_parse_user_cmdline(enum spectre_v2_mitigation_cmd v2_cmd)
360{
361 char arg[20];
362 int ret, i;
363
364 switch (v2_cmd) {
365 case SPECTRE_V2_CMD_NONE:
366 return SPECTRE_V2_USER_CMD_NONE;
367 case SPECTRE_V2_CMD_FORCE:
368 return SPECTRE_V2_USER_CMD_FORCE;
369 default:
370 break;
371 }
372
373 ret = cmdline_find_option(boot_command_line, "spectre_v2_user",
374 arg, sizeof(arg));
375 if (ret < 0)
376 return SPECTRE_V2_USER_CMD_AUTO;
377
378 for (i = 0; i < ARRAY_SIZE(v2_user_options); i++) {
379 if (match_option(arg, ret, v2_user_options[i].option)) {
380 spec_v2_user_print_cond(v2_user_options[i].option,
381 v2_user_options[i].secure);
382 return v2_user_options[i].cmd;
383 }
384 }
385
386 pr_err("Unknown user space protection option (%s). Switching to AUTO select\n", arg);
387 return SPECTRE_V2_USER_CMD_AUTO;
388}
389
390static void __init
391spectre_v2_user_select_mitigation(enum spectre_v2_mitigation_cmd v2_cmd)
392{
393 enum spectre_v2_user_mitigation mode = SPECTRE_V2_USER_NONE;
394 bool smt_possible = IS_ENABLED(CONFIG_SMP);
fd1b7023 395 enum spectre_v2_user_cmd cmd;
6cd930f0
TG
396
397 if (!boot_cpu_has(X86_FEATURE_IBPB) && !boot_cpu_has(X86_FEATURE_STIBP))
398 return;
399
400 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
401 cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
402 smt_possible = false;
403
fd1b7023
TG
404 cmd = spectre_v2_parse_user_cmdline(v2_cmd);
405 switch (cmd) {
6cd930f0
TG
406 case SPECTRE_V2_USER_CMD_NONE:
407 goto set_mode;
408 case SPECTRE_V2_USER_CMD_FORCE:
409 mode = SPECTRE_V2_USER_STRICT;
410 break;
2cdf6a58 411 case SPECTRE_V2_USER_CMD_PRCTL:
fd1b7023 412 case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
2cdf6a58
TG
413 mode = SPECTRE_V2_USER_PRCTL;
414 break;
1c3cf627
TG
415 case SPECTRE_V2_USER_CMD_AUTO:
416 case SPECTRE_V2_USER_CMD_SECCOMP:
fd1b7023 417 case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
1c3cf627
TG
418 if (IS_ENABLED(CONFIG_SECCOMP))
419 mode = SPECTRE_V2_USER_SECCOMP;
420 else
421 mode = SPECTRE_V2_USER_PRCTL;
422 break;
6cd930f0
TG
423 }
424
fbed8eca
TL
425 /*
426 * At this point, an STIBP mode other than "off" has been set.
427 * If STIBP support is not being forced, check if STIBP always-on
428 * is preferred.
429 */
430 if (mode != SPECTRE_V2_USER_STRICT &&
431 boot_cpu_has(X86_FEATURE_AMD_STIBP_ALWAYS_ON))
432 mode = SPECTRE_V2_USER_STRICT_PREFERRED;
433
6cd930f0
TG
434 /* Initialize Indirect Branch Prediction Barrier */
435 if (boot_cpu_has(X86_FEATURE_IBPB)) {
436 setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
58b2d4c8 437
fd1b7023
TG
438 switch (cmd) {
439 case SPECTRE_V2_USER_CMD_FORCE:
440 case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
441 case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
58b2d4c8
TG
442 static_branch_enable(&switch_mm_always_ibpb);
443 break;
fd1b7023
TG
444 case SPECTRE_V2_USER_CMD_PRCTL:
445 case SPECTRE_V2_USER_CMD_AUTO:
446 case SPECTRE_V2_USER_CMD_SECCOMP:
2cdf6a58
TG
447 static_branch_enable(&switch_mm_cond_ibpb);
448 break;
58b2d4c8
TG
449 default:
450 break;
451 }
452
453 pr_info("mitigation: Enabling %s Indirect Branch Prediction Barrier\n",
fd1b7023
TG
454 static_key_enabled(&switch_mm_always_ibpb) ?
455 "always-on" : "conditional");
6cd930f0
TG
456 }
457
8edb388f 458 /* If enhanced IBRS is enabled no STIBP required */
6cd930f0
TG
459 if (spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
460 return;
461
2cdf6a58 462 /*
8edb388f 463 * If SMT is not possible or STIBP is not available clear the STIBP
2cdf6a58
TG
464 * mode.
465 */
466 if (!smt_possible || !boot_cpu_has(X86_FEATURE_STIBP))
467 mode = SPECTRE_V2_USER_NONE;
6cd930f0
TG
468set_mode:
469 spectre_v2_user = mode;
470 /* Only print the STIBP mode when SMT possible */
471 if (smt_possible)
472 pr_info("%s\n", spectre_v2_user_strings[mode]);
473}
474
0eb5928b 475static const char * const spectre_v2_strings[] = {
aef0bebc
TG
476 [SPECTRE_V2_NONE] = "Vulnerable",
477 [SPECTRE_V2_RETPOLINE_GENERIC] = "Mitigation: Full generic retpoline",
478 [SPECTRE_V2_RETPOLINE_AMD] = "Mitigation: Full AMD retpoline",
479 [SPECTRE_V2_IBRS_ENHANCED] = "Mitigation: Enhanced IBRS",
480};
481
17c33e7c
KA
482static const struct {
483 const char *option;
484 enum spectre_v2_mitigation_cmd cmd;
485 bool secure;
5a76cb46 486} mitigation_options[] __initdata = {
aef0bebc
TG
487 { "off", SPECTRE_V2_CMD_NONE, false },
488 { "on", SPECTRE_V2_CMD_FORCE, true },
489 { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false },
490 { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_AMD, false },
491 { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
492 { "auto", SPECTRE_V2_CMD_AUTO, false },
17c33e7c
KA
493};
494
10640103 495static void __init spec_v2_print_cond(const char *reason, bool secure)
aef0bebc 496{
10640103 497 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
aef0bebc
TG
498 pr_info("%s selected on command line.\n", reason);
499}
500
da285121
DW
501static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
502{
aef0bebc 503 enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
da285121 504 char arg[20];
17c33e7c 505 int ret, i;
17c33e7c 506
2a56e210
JP
507 if (cmdline_find_option_bool(boot_command_line, "nospectre_v2") ||
508 cpu_mitigations_off())
17c33e7c 509 return SPECTRE_V2_CMD_NONE;
17c33e7c 510
4e8e955a
TC
511 ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
512 if (ret < 0)
513 return SPECTRE_V2_CMD_AUTO;
514
515 for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
516 if (!match_option(arg, ret, mitigation_options[i].option))
517 continue;
518 cmd = mitigation_options[i].cmd;
519 break;
520 }
521
522 if (i >= ARRAY_SIZE(mitigation_options)) {
523 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
524 return SPECTRE_V2_CMD_AUTO;
da285121
DW
525 }
526
17c33e7c
KA
527 if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
528 cmd == SPECTRE_V2_CMD_RETPOLINE_AMD ||
529 cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC) &&
530 !IS_ENABLED(CONFIG_RETPOLINE)) {
713f1b95 531 pr_err("%s selected but not compiled in. Switching to AUTO select\n", mitigation_options[i].option);
da285121 532 return SPECTRE_V2_CMD_AUTO;
17c33e7c
KA
533 }
534
535 if (cmd == SPECTRE_V2_CMD_RETPOLINE_AMD &&
536 boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
537 pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n");
538 return SPECTRE_V2_CMD_AUTO;
539 }
540
10640103
TG
541 spec_v2_print_cond(mitigation_options[i].option,
542 mitigation_options[i].secure);
17c33e7c 543 return cmd;
da285121
DW
544}
545
546static void __init spectre_v2_select_mitigation(void)
547{
548 enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
549 enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
550
551 /*
552 * If the CPU is not affected and the command line mode is NONE or AUTO
553 * then nothing to do.
554 */
555 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
556 (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
557 return;
558
559 switch (cmd) {
560 case SPECTRE_V2_CMD_NONE:
561 return;
562
563 case SPECTRE_V2_CMD_FORCE:
da285121 564 case SPECTRE_V2_CMD_AUTO:
1044fde9
SP
565 if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
566 mode = SPECTRE_V2_IBRS_ENHANCED;
567 /* Force it so VMEXIT will restore correctly */
568 x86_spec_ctrl_base |= SPEC_CTRL_IBRS;
569 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
570 goto specv2_set_mode;
571 }
d0f293e0
DL
572 if (IS_ENABLED(CONFIG_RETPOLINE))
573 goto retpoline_auto;
574 break;
da285121
DW
575 case SPECTRE_V2_CMD_RETPOLINE_AMD:
576 if (IS_ENABLED(CONFIG_RETPOLINE))
577 goto retpoline_amd;
578 break;
579 case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
580 if (IS_ENABLED(CONFIG_RETPOLINE))
581 goto retpoline_generic;
582 break;
583 case SPECTRE_V2_CMD_RETPOLINE:
584 if (IS_ENABLED(CONFIG_RETPOLINE))
585 goto retpoline_auto;
586 break;
587 }
713f1b95 588 pr_err("Spectre mitigation: kernel not compiled with retpoline; no mitigation available!");
da285121
DW
589 return;
590
591retpoline_auto:
592 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
593 retpoline_amd:
594 if (!boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
713f1b95 595 pr_err("Spectre mitigation: LFENCE not serializing, switching to generic retpoline\n");
da285121
DW
596 goto retpoline_generic;
597 }
51dd01b5 598 mode = SPECTRE_V2_RETPOLINE_AMD;
da285121
DW
599 setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD);
600 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
601 } else {
602 retpoline_generic:
51dd01b5 603 mode = SPECTRE_V2_RETPOLINE_GENERIC;
da285121
DW
604 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
605 }
606
1044fde9 607specv2_set_mode:
da285121
DW
608 spectre_v2_enabled = mode;
609 pr_info("%s\n", spectre_v2_strings[mode]);
c995efd5
DW
610
611 /*
14623a43
JK
612 * If spectre v2 protection has been enabled, unconditionally fill
613 * RSB during a context switch; this protects against two independent
614 * issues:
c995efd5 615 *
14623a43
JK
616 * - RSB underflow (and switch to BTB) on Skylake+
617 * - SpectreRSB variant of spectre v2 on X86_BUG_SPECTRE_V2 CPUs
c995efd5 618 */
14623a43
JK
619 setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
620 pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n");
bd12e896 621
390b99c3
DW
622 /*
623 * Retpoline means the kernel is safe because it has no indirect
1044fde9
SP
624 * branches. Enhanced IBRS protects firmware too, so, enable restricted
625 * speculation around firmware calls only when Enhanced IBRS isn't
626 * supported.
627 *
628 * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because
629 * the user might select retpoline on the kernel command line and if
630 * the CPU supports Enhanced IBRS, kernel might un-intentionally not
631 * enable IBRS around firmware calls.
390b99c3 632 */
1044fde9 633 if (boot_cpu_has(X86_FEATURE_IBRS) && mode != SPECTRE_V2_IBRS_ENHANCED) {
390b99c3
DW
634 setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
635 pr_info("Enabling Restricted Speculation for firmware calls\n");
636 }
7011f443 637
6cd930f0
TG
638 /* Set up IBPB and STIBP depending on the general spectre V2 command */
639 spectre_v2_user_select_mitigation(cmd);
da285121
DW
640}
641
984d266f 642static void update_stibp_msr(void * __unused)
aef0bebc 643{
984d266f 644 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
aef0bebc
TG
645}
646
984d266f
TG
647/* Update x86_spec_ctrl_base in case SMT state changed. */
648static void update_stibp_strict(void)
aef0bebc 649{
984d266f
TG
650 u64 mask = x86_spec_ctrl_base & ~SPEC_CTRL_STIBP;
651
652 if (sched_smt_active())
653 mask |= SPEC_CTRL_STIBP;
654
655 if (mask == x86_spec_ctrl_base)
656 return;
657
658 pr_info("Update user space SMT mitigation: STIBP %s\n",
659 mask & SPEC_CTRL_STIBP ? "always-on" : "off");
660 x86_spec_ctrl_base = mask;
661 on_each_cpu(update_stibp_msr, NULL, 1);
aef0bebc
TG
662}
663
2cdf6a58
TG
664/* Update the static key controlling the evaluation of TIF_SPEC_IB */
665static void update_indir_branch_cond(void)
666{
667 if (sched_smt_active())
668 static_branch_enable(&switch_to_cond_stibp);
669 else
670 static_branch_disable(&switch_to_cond_stibp);
671}
672
0923e77e
JP
673#undef pr_fmt
674#define pr_fmt(fmt) fmt
675
fc5ee888
TG
676/* Update the static key controlling the MDS CPU buffer clear in idle */
677static void update_mds_branch_idle(void)
678{
679 /*
680 * Enable the idle clearing on CPUs which are affected only by
681 * MDBDS and not any other MDS variant. The other variants cannot
682 * be mitigated when SMT is enabled, so clearing the buffers on
683 * idle would be a window dressing exercise.
684 */
685 if (!boot_cpu_has(X86_BUG_MSBDS_ONLY))
686 return;
687
688 if (sched_smt_active())
689 static_branch_enable(&mds_idle_clear);
690 else
691 static_branch_disable(&mds_idle_clear);
692}
693
0923e77e
JP
694#define MDS_MSG_SMT "MDS CPU bug present and SMT on, data leak possible. See https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/mds.html for more details.\n"
695
aef0bebc
TG
696void arch_smt_update(void)
697{
984d266f
TG
698 /* Enhanced IBRS implies STIBP. No update required. */
699 if (spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
aef0bebc
TG
700 return;
701
702 mutex_lock(&spec_ctrl_mutex);
703
984d266f
TG
704 switch (spectre_v2_user) {
705 case SPECTRE_V2_USER_NONE:
706 break;
707 case SPECTRE_V2_USER_STRICT:
fbed8eca 708 case SPECTRE_V2_USER_STRICT_PREFERRED:
984d266f
TG
709 update_stibp_strict();
710 break;
ac40ad3b 711 case SPECTRE_V2_USER_PRCTL:
1c3cf627 712 case SPECTRE_V2_USER_SECCOMP:
2cdf6a58 713 update_indir_branch_cond();
ac40ad3b 714 break;
aef0bebc 715 }
984d266f 716
ebf1e8cb
TG
717 switch(mds_mitigation) {
718 case MDS_MITIGATION_FULL:
719 case MDS_MITIGATION_VMWERV:
0923e77e
JP
720 if (sched_smt_active() && !boot_cpu_has(X86_BUG_MSBDS_ONLY))
721 pr_warn_once(MDS_MSG_SMT);
fc5ee888 722 update_mds_branch_idle();
ebf1e8cb
TG
723 break;
724 case MDS_MITIGATION_OFF:
725 break;
726 }
fc5ee888 727
aef0bebc
TG
728 mutex_unlock(&spec_ctrl_mutex);
729}
730
e63490c8
KRW
731#undef pr_fmt
732#define pr_fmt(fmt) "Speculative Store Bypass: " fmt
733
b5e6d77d 734static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
e63490c8
KRW
735
736/* The kernel command line selection */
737enum ssb_mitigation_cmd {
738 SPEC_STORE_BYPASS_CMD_NONE,
739 SPEC_STORE_BYPASS_CMD_AUTO,
740 SPEC_STORE_BYPASS_CMD_ON,
574dcf89 741 SPEC_STORE_BYPASS_CMD_PRCTL,
c7416003 742 SPEC_STORE_BYPASS_CMD_SECCOMP,
e63490c8
KRW
743};
744
0eb5928b 745static const char * const ssb_strings[] = {
e63490c8 746 [SPEC_STORE_BYPASS_NONE] = "Vulnerable",
574dcf89 747 [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass disabled",
c7416003
KC
748 [SPEC_STORE_BYPASS_PRCTL] = "Mitigation: Speculative Store Bypass disabled via prctl",
749 [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
e63490c8
KRW
750};
751
752static const struct {
753 const char *option;
754 enum ssb_mitigation_cmd cmd;
5a76cb46 755} ssb_mitigation_options[] __initdata = {
c7416003
KC
756 { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */
757 { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */
758 { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */
759 { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative Store Bypass via prctl */
760 { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */
e63490c8
KRW
761};
762
763static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
764{
765 enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO;
766 char arg[20];
767 int ret, i;
768
2a56e210
JP
769 if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable") ||
770 cpu_mitigations_off()) {
e63490c8
KRW
771 return SPEC_STORE_BYPASS_CMD_NONE;
772 } else {
773 ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable",
774 arg, sizeof(arg));
775 if (ret < 0)
776 return SPEC_STORE_BYPASS_CMD_AUTO;
777
778 for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) {
779 if (!match_option(arg, ret, ssb_mitigation_options[i].option))
780 continue;
781
782 cmd = ssb_mitigation_options[i].cmd;
783 break;
784 }
785
786 if (i >= ARRAY_SIZE(ssb_mitigation_options)) {
787 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
788 return SPEC_STORE_BYPASS_CMD_AUTO;
789 }
790 }
791
792 return cmd;
793}
794
dbe3009d 795static enum ssb_mitigation __init __ssb_select_mitigation(void)
e63490c8
KRW
796{
797 enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
798 enum ssb_mitigation_cmd cmd;
799
8fe36c9d 800 if (!boot_cpu_has(X86_FEATURE_SSBD))
e63490c8
KRW
801 return mode;
802
803 cmd = ssb_parse_cmdline();
804 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) &&
805 (cmd == SPEC_STORE_BYPASS_CMD_NONE ||
806 cmd == SPEC_STORE_BYPASS_CMD_AUTO))
807 return mode;
808
809 switch (cmd) {
810 case SPEC_STORE_BYPASS_CMD_AUTO:
c7416003
KC
811 case SPEC_STORE_BYPASS_CMD_SECCOMP:
812 /*
813 * Choose prctl+seccomp as the default mode if seccomp is
814 * enabled.
815 */
816 if (IS_ENABLED(CONFIG_SECCOMP))
817 mode = SPEC_STORE_BYPASS_SECCOMP;
818 else
819 mode = SPEC_STORE_BYPASS_PRCTL;
574dcf89 820 break;
e63490c8
KRW
821 case SPEC_STORE_BYPASS_CMD_ON:
822 mode = SPEC_STORE_BYPASS_DISABLE;
823 break;
574dcf89
TG
824 case SPEC_STORE_BYPASS_CMD_PRCTL:
825 mode = SPEC_STORE_BYPASS_PRCTL;
826 break;
e63490c8
KRW
827 case SPEC_STORE_BYPASS_CMD_NONE:
828 break;
829 }
830
23b9eab9
KRW
831 /*
832 * We have three CPU feature flags that are in play here:
833 * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
8fe36c9d 834 * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
23b9eab9
KRW
835 * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
836 */
574dcf89 837 if (mode == SPEC_STORE_BYPASS_DISABLE) {
e63490c8 838 setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
23b9eab9 839 /*
3b881627
KRW
840 * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
841 * use a completely different MSR and bit dependent on family.
23b9eab9 842 */
5005c716
TL
843 if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
844 !static_cpu_has(X86_FEATURE_AMD_SSBD)) {
733c54c9 845 x86_amd_ssb_disable();
5005c716 846 } else {
8fe36c9d 847 x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
e5f984ed 848 x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
208efa83 849 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
23b9eab9
KRW
850 }
851 }
852
e63490c8
KRW
853 return mode;
854}
855
043d480e 856static void ssb_select_mitigation(void)
e63490c8
KRW
857{
858 ssb_mode = __ssb_select_mitigation();
859
860 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
861 pr_info("%s\n", ssb_strings[ssb_mode]);
862}
863
da285121 864#undef pr_fmt
c7416003 865#define pr_fmt(fmt) "Speculation prctl: " fmt
da285121 866
801d6893 867static void task_update_spec_tif(struct task_struct *tsk)
574dcf89 868{
801d6893
TG
869 /* Force the update of the real TIF bits */
870 set_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE);
d4595948
TG
871
872 /*
873 * Immediately update the speculation control MSRs for the current
874 * task, but for a non-current task delay setting the CPU
875 * mitigation until it is scheduled next.
876 *
877 * This can only happen for SECCOMP mitigation. For PRCTL it's
878 * always the current task.
879 */
801d6893 880 if (tsk == current)
d4595948
TG
881 speculation_ctrl_update_current();
882}
883
884static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
885{
c7416003
KC
886 if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
887 ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
574dcf89
TG
888 return -ENXIO;
889
733f4234
TG
890 switch (ctrl) {
891 case PR_SPEC_ENABLE:
892 /* If speculation is force disabled, enable is not allowed */
893 if (task_spec_ssb_force_disable(task))
894 return -EPERM;
895 task_clear_spec_ssb_disable(task);
801d6893 896 task_update_spec_tif(task);
733f4234
TG
897 break;
898 case PR_SPEC_DISABLE:
899 task_set_spec_ssb_disable(task);
801d6893 900 task_update_spec_tif(task);
733f4234
TG
901 break;
902 case PR_SPEC_FORCE_DISABLE:
903 task_set_spec_ssb_disable(task);
904 task_set_spec_ssb_force_disable(task);
801d6893 905 task_update_spec_tif(task);
733f4234
TG
906 break;
907 default:
908 return -ERANGE;
909 }
574dcf89
TG
910 return 0;
911}
912
ac40ad3b
TG
913static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)
914{
915 switch (ctrl) {
916 case PR_SPEC_ENABLE:
917 if (spectre_v2_user == SPECTRE_V2_USER_NONE)
918 return 0;
919 /*
920 * Indirect branch speculation is always disabled in strict
921 * mode.
922 */
fbed8eca
TL
923 if (spectre_v2_user == SPECTRE_V2_USER_STRICT ||
924 spectre_v2_user == SPECTRE_V2_USER_STRICT_PREFERRED)
ac40ad3b
TG
925 return -EPERM;
926 task_clear_spec_ib_disable(task);
927 task_update_spec_tif(task);
928 break;
929 case PR_SPEC_DISABLE:
930 case PR_SPEC_FORCE_DISABLE:
931 /*
932 * Indirect branch speculation is always allowed when
933 * mitigation is force disabled.
934 */
935 if (spectre_v2_user == SPECTRE_V2_USER_NONE)
936 return -EPERM;
fbed8eca
TL
937 if (spectre_v2_user == SPECTRE_V2_USER_STRICT ||
938 spectre_v2_user == SPECTRE_V2_USER_STRICT_PREFERRED)
ac40ad3b
TG
939 return 0;
940 task_set_spec_ib_disable(task);
941 if (ctrl == PR_SPEC_FORCE_DISABLE)
942 task_set_spec_ib_force_disable(task);
943 task_update_spec_tif(task);
944 break;
945 default:
946 return -ERANGE;
947 }
948 return 0;
949}
950
5b38e244
TG
951int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
952 unsigned long ctrl)
953{
954 switch (which) {
955 case PR_SPEC_STORE_BYPASS:
956 return ssb_prctl_set(task, ctrl);
ac40ad3b
TG
957 case PR_SPEC_INDIRECT_BRANCH:
958 return ib_prctl_set(task, ctrl);
5b38e244
TG
959 default:
960 return -ENODEV;
961 }
962}
963
964#ifdef CONFIG_SECCOMP
965void arch_seccomp_spec_mitigate(struct task_struct *task)
966{
c7416003
KC
967 if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP)
968 ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
1c3cf627
TG
969 if (spectre_v2_user == SPECTRE_V2_USER_SECCOMP)
970 ib_prctl_set(task, PR_SPEC_FORCE_DISABLE);
5b38e244
TG
971}
972#endif
973
199bfed2 974static int ssb_prctl_get(struct task_struct *task)
574dcf89
TG
975{
976 switch (ssb_mode) {
977 case SPEC_STORE_BYPASS_DISABLE:
978 return PR_SPEC_DISABLE;
c7416003 979 case SPEC_STORE_BYPASS_SECCOMP:
574dcf89 980 case SPEC_STORE_BYPASS_PRCTL:
733f4234
TG
981 if (task_spec_ssb_force_disable(task))
982 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
983 if (task_spec_ssb_disable(task))
574dcf89
TG
984 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
985 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
986 default:
987 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
988 return PR_SPEC_ENABLE;
989 return PR_SPEC_NOT_AFFECTED;
990 }
991}
992
ac40ad3b
TG
993static int ib_prctl_get(struct task_struct *task)
994{
995 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
996 return PR_SPEC_NOT_AFFECTED;
997
998 switch (spectre_v2_user) {
999 case SPECTRE_V2_USER_NONE:
1000 return PR_SPEC_ENABLE;
1001 case SPECTRE_V2_USER_PRCTL:
1c3cf627 1002 case SPECTRE_V2_USER_SECCOMP:
ac40ad3b
TG
1003 if (task_spec_ib_force_disable(task))
1004 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
1005 if (task_spec_ib_disable(task))
1006 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
1007 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
1008 case SPECTRE_V2_USER_STRICT:
fbed8eca 1009 case SPECTRE_V2_USER_STRICT_PREFERRED:
ac40ad3b
TG
1010 return PR_SPEC_DISABLE;
1011 default:
1012 return PR_SPEC_NOT_AFFECTED;
1013 }
1014}
1015
199bfed2 1016int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
574dcf89
TG
1017{
1018 switch (which) {
1019 case PR_SPEC_STORE_BYPASS:
199bfed2 1020 return ssb_prctl_get(task);
ac40ad3b
TG
1021 case PR_SPEC_INDIRECT_BRANCH:
1022 return ib_prctl_get(task);
574dcf89
TG
1023 default:
1024 return -ENODEV;
1025 }
1026}
1027
23b9eab9
KRW
1028void x86_spec_ctrl_setup_ap(void)
1029{
50f9b919 1030 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
208efa83 1031 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
c37b94dd
KRW
1032
1033 if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
8fe36c9d 1034 x86_amd_ssb_disable();
23b9eab9
KRW
1035}
1036
73ca9f47
KRW
1037#undef pr_fmt
1038#define pr_fmt(fmt) "L1TF: " fmt
b9cfedcd 1039
24fcb53c
JK
1040/* Default mitigation for L1TF-affected CPUs */
1041enum l1tf_mitigations l1tf_mitigation __ro_after_init = L1TF_MITIGATION_FLUSH;
b9cfedcd 1042#if IS_ENABLED(CONFIG_KVM_INTEL)
24fcb53c
JK
1043EXPORT_SYMBOL_GPL(l1tf_mitigation);
1044
522c7bed 1045enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
b9cfedcd
TG
1046EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation);
1047#endif
1048
f1f016ed
AK
1049/*
1050 * These CPUs all support 44bits physical address space internally in the
1051 * cache but CPUID can report a smaller number of physical address bits.
1052 *
1053 * The L1TF mitigation uses the top most address bit for the inversion of
1054 * non present PTEs. When the installed memory reaches into the top most
1055 * address bit due to memory holes, which has been observed on machines
1056 * which report 36bits physical address bits and have 32G RAM installed,
1057 * then the mitigation range check in l1tf_select_mitigation() triggers.
1058 * This is a false positive because the mitigation is still possible due to
1059 * the fact that the cache uses 44bit internally. Use the cache bits
1060 * instead of the reported physical bits and adjust them on the affected
1061 * machines to 44bit if the reported bits are less than 44.
1062 */
1063static void override_cache_bits(struct cpuinfo_x86 *c)
1064{
1065 if (c->x86 != 6)
1066 return;
1067
1068 switch (c->x86_model) {
1069 case INTEL_FAM6_NEHALEM:
1070 case INTEL_FAM6_WESTMERE:
1071 case INTEL_FAM6_SANDYBRIDGE:
1072 case INTEL_FAM6_IVYBRIDGE:
1073 case INTEL_FAM6_HASWELL_CORE:
1074 case INTEL_FAM6_HASWELL_ULT:
1075 case INTEL_FAM6_HASWELL_GT3E:
1076 case INTEL_FAM6_BROADWELL_CORE:
1077 case INTEL_FAM6_BROADWELL_GT3E:
1078 case INTEL_FAM6_SKYLAKE_MOBILE:
1079 case INTEL_FAM6_SKYLAKE_DESKTOP:
1080 case INTEL_FAM6_KABYLAKE_MOBILE:
1081 case INTEL_FAM6_KABYLAKE_DESKTOP:
1082 if (c->x86_cache_bits < 44)
1083 c->x86_cache_bits = 44;
1084 break;
1085 }
1086}
1087
73ca9f47
KRW
1088static void __init l1tf_select_mitigation(void)
1089{
1090 u64 half_pa;
1091
1092 if (!boot_cpu_has_bug(X86_BUG_L1TF))
1093 return;
1094
2a56e210
JP
1095 if (cpu_mitigations_off())
1096 l1tf_mitigation = L1TF_MITIGATION_OFF;
1097 else if (cpu_mitigations_auto_nosmt())
1098 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
1099
f1f016ed
AK
1100 override_cache_bits(&boot_cpu_data);
1101
24fcb53c
JK
1102 switch (l1tf_mitigation) {
1103 case L1TF_MITIGATION_OFF:
1104 case L1TF_MITIGATION_FLUSH_NOWARN:
1105 case L1TF_MITIGATION_FLUSH:
1106 break;
1107 case L1TF_MITIGATION_FLUSH_NOSMT:
1108 case L1TF_MITIGATION_FULL:
1109 cpu_smt_disable(false);
1110 break;
1111 case L1TF_MITIGATION_FULL_FORCE:
1112 cpu_smt_disable(true);
1113 break;
1114 }
1115
73ca9f47
KRW
1116#if CONFIG_PGTABLE_LEVELS == 2
1117 pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
1118 return;
1119#endif
1120
73ca9f47
KRW
1121 half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT;
1122 if (e820__mapped_any(half_pa, ULLONG_MAX - half_pa, E820_TYPE_RAM)) {
1123 pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
f80e341a
VB
1124 pr_info("You may make it effective by booting the kernel with mem=%llu parameter.\n",
1125 half_pa);
1126 pr_info("However, doing so will make a part of your RAM unusable.\n");
1127 pr_info("Reading https://www.kernel.org/doc/html/latest/admin-guide/l1tf.html might help you decide.\n");
73ca9f47
KRW
1128 return;
1129 }
1130
1131 setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV);
1132}
24fcb53c
JK
1133
1134static int __init l1tf_cmdline(char *str)
1135{
1136 if (!boot_cpu_has_bug(X86_BUG_L1TF))
1137 return 0;
1138
1139 if (!str)
1140 return -EINVAL;
1141
1142 if (!strcmp(str, "off"))
1143 l1tf_mitigation = L1TF_MITIGATION_OFF;
1144 else if (!strcmp(str, "flush,nowarn"))
1145 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOWARN;
1146 else if (!strcmp(str, "flush"))
1147 l1tf_mitigation = L1TF_MITIGATION_FLUSH;
1148 else if (!strcmp(str, "flush,nosmt"))
1149 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
1150 else if (!strcmp(str, "full"))
1151 l1tf_mitigation = L1TF_MITIGATION_FULL;
1152 else if (!strcmp(str, "full,force"))
1153 l1tf_mitigation = L1TF_MITIGATION_FULL_FORCE;
1154
1155 return 0;
1156}
1157early_param("l1tf", l1tf_cmdline);
1158
73ca9f47 1159#undef pr_fmt
0923e77e 1160#define pr_fmt(fmt) fmt
73ca9f47 1161
61dc0f55 1162#ifdef CONFIG_SYSFS
d2b8fc2d 1163
b9cfedcd
TG
1164#define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
1165
1166#if IS_ENABLED(CONFIG_KVM_INTEL)
0eb5928b 1167static const char * const l1tf_vmx_states[] = {
1ead4979
TG
1168 [VMENTER_L1D_FLUSH_AUTO] = "auto",
1169 [VMENTER_L1D_FLUSH_NEVER] = "vulnerable",
1170 [VMENTER_L1D_FLUSH_COND] = "conditional cache flushes",
1171 [VMENTER_L1D_FLUSH_ALWAYS] = "cache flushes",
1172 [VMENTER_L1D_FLUSH_EPT_DISABLED] = "EPT disabled",
364a4311 1173 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = "flush not necessary"
b9cfedcd
TG
1174};
1175
1176static ssize_t l1tf_show_state(char *buf)
1177{
1178 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO)
1179 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
1180
3899f7b1
PB
1181 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_EPT_DISABLED ||
1182 (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER &&
8bc59ba0 1183 sched_smt_active())) {
3899f7b1
PB
1184 return sprintf(buf, "%s; VMX: %s\n", L1TF_DEFAULT_MSG,
1185 l1tf_vmx_states[l1tf_vmx_mitigation]);
8bc59ba0 1186 }
3899f7b1
PB
1187
1188 return sprintf(buf, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG,
1189 l1tf_vmx_states[l1tf_vmx_mitigation],
8bc59ba0 1190 sched_smt_active() ? "vulnerable" : "disabled");
b9cfedcd
TG
1191}
1192#else
1193static ssize_t l1tf_show_state(char *buf)
1194{
1195 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
1196}
1197#endif
1198
1122cc78
TG
1199static ssize_t mds_show_state(char *buf)
1200{
1201 if (!hypervisor_is_type(X86_HYPER_NATIVE)) {
1202 return sprintf(buf, "%s; SMT Host state unknown\n",
1203 mds_strings[mds_mitigation]);
1204 }
1205
1206 if (boot_cpu_has(X86_BUG_MSBDS_ONLY)) {
1207 return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
5c89a99c
KRW
1208 (mds_mitigation == MDS_MITIGATION_OFF ? "vulnerable" :
1209 sched_smt_active() ? "mitigated" : "disabled"));
1122cc78
TG
1210 }
1211
1212 return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
1213 sched_smt_active() ? "vulnerable" : "disabled");
1214}
1215
2daa53e8
TC
1216static char *stibp_state(void)
1217{
4a8d91c8
TC
1218 if (spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
1219 return "";
1220
6cd930f0
TG
1221 switch (spectre_v2_user) {
1222 case SPECTRE_V2_USER_NONE:
1223 return ", STIBP: disabled";
1224 case SPECTRE_V2_USER_STRICT:
1225 return ", STIBP: forced";
fbed8eca
TL
1226 case SPECTRE_V2_USER_STRICT_PREFERRED:
1227 return ", STIBP: always-on";
ac40ad3b 1228 case SPECTRE_V2_USER_PRCTL:
1c3cf627 1229 case SPECTRE_V2_USER_SECCOMP:
2cdf6a58
TG
1230 if (static_key_enabled(&switch_to_cond_stibp))
1231 return ", STIBP: conditional";
6cd930f0
TG
1232 }
1233 return "";
2daa53e8
TC
1234}
1235
1236static char *ibpb_state(void)
1237{
58b2d4c8 1238 if (boot_cpu_has(X86_FEATURE_IBPB)) {
2cdf6a58 1239 if (static_key_enabled(&switch_mm_always_ibpb))
58b2d4c8 1240 return ", IBPB: always-on";
2cdf6a58
TG
1241 if (static_key_enabled(&switch_mm_cond_ibpb))
1242 return ", IBPB: conditional";
1243 return ", IBPB: disabled";
58b2d4c8
TG
1244 }
1245 return "";
2daa53e8
TC
1246}
1247
ace051d5 1248static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
043d480e 1249 char *buf, unsigned int bug)
61dc0f55 1250{
d2b8fc2d 1251 if (!boot_cpu_has_bug(bug))
61dc0f55 1252 return sprintf(buf, "Not affected\n");
d2b8fc2d
KRW
1253
1254 switch (bug) {
1255 case X86_BUG_CPU_MELTDOWN:
1256 if (boot_cpu_has(X86_FEATURE_PTI))
1257 return sprintf(buf, "Mitigation: PTI\n");
1258
1259 break;
1260
1261 case X86_BUG_SPECTRE_V1:
1262 return sprintf(buf, "Mitigation: __user pointer sanitization\n");
1263
1264 case X86_BUG_SPECTRE_V2:
0b96b80d 1265 return sprintf(buf, "%s%s%s%s%s%s\n", spectre_v2_strings[spectre_v2_enabled],
2daa53e8 1266 ibpb_state(),
d2b8fc2d 1267 boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
2daa53e8 1268 stibp_state(),
5e2fafd5 1269 boot_cpu_has(X86_FEATURE_RSB_CTXSW) ? ", RSB filling" : "",
d2b8fc2d
KRW
1270 spectre_v2_module_string());
1271
e63490c8
KRW
1272 case X86_BUG_SPEC_STORE_BYPASS:
1273 return sprintf(buf, "%s\n", ssb_strings[ssb_mode]);
1274
05516ad8
AK
1275 case X86_BUG_L1TF:
1276 if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV))
b9cfedcd 1277 return l1tf_show_state(buf);
05516ad8 1278 break;
1122cc78
TG
1279
1280 case X86_BUG_MDS:
1281 return mds_show_state(buf);
1282
d2b8fc2d
KRW
1283 default:
1284 break;
1285 }
1286
61dc0f55
TG
1287 return sprintf(buf, "Vulnerable\n");
1288}
1289
d2b8fc2d
KRW
1290ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
1291{
1292 return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
1293}
1294
713f1b95 1295ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
61dc0f55 1296{
d2b8fc2d 1297 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
61dc0f55
TG
1298}
1299
713f1b95 1300ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
61dc0f55 1301{
d2b8fc2d 1302 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
61dc0f55 1303}
d7de9182
KRW
1304
1305ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
1306{
1307 return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS);
1308}
05516ad8
AK
1309
1310ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
1311{
1312 return cpu_show_common(dev, attr, buf, X86_BUG_L1TF);
1313}
1122cc78
TG
1314
1315ssize_t cpu_show_mds(struct device *dev, struct device_attribute *attr, char *buf)
1316{
1317 return cpu_show_common(dev, attr, buf, X86_BUG_MDS);
1318}
61dc0f55 1319#endif