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