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/cpu.h>
13 #include <linux/module.h>
14 #include <linux/nospec.h>
15 #include <linux/prctl.h>
16 #include <linux/sched/smt.h>
17 #include <linux/pgtable.h>
18 #include <linux/bpf.h>
19
20 #include <asm/spec-ctrl.h>
21 #include <asm/cmdline.h>
22 #include <asm/bugs.h>
23 #include <asm/processor.h>
24 #include <asm/processor-flags.h>
25 #include <asm/fpu/api.h>
26 #include <asm/msr.h>
27 #include <asm/vmx.h>
28 #include <asm/paravirt.h>
29 #include <asm/intel-family.h>
30 #include <asm/e820/api.h>
31 #include <asm/hypervisor.h>
32 #include <asm/tlbflush.h>
33 #include <asm/cpu.h>
34
35 #include "cpu.h"
36
37 static void __init spectre_v1_select_mitigation(void);
38 static void __init spectre_v2_select_mitigation(void);
39 static void __init retbleed_select_mitigation(void);
40 static void __init spectre_v2_user_select_mitigation(void);
41 static void __init ssb_select_mitigation(void);
42 static void __init l1tf_select_mitigation(void);
43 static void __init mds_select_mitigation(void);
44 static void __init md_clear_update_mitigation(void);
45 static void __init md_clear_select_mitigation(void);
46 static void __init taa_select_mitigation(void);
47 static void __init mmio_select_mitigation(void);
48 static void __init srbds_select_mitigation(void);
49 static void __init l1d_flush_select_mitigation(void);
50 static void __init srso_select_mitigation(void);
51 static void __init gds_select_mitigation(void);
52 static void __init its_select_mitigation(void);
53
54 /* The base value of the SPEC_CTRL MSR without task-specific bits set */
55 u64 x86_spec_ctrl_base;
56 EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
57
58 /* The current value of the SPEC_CTRL MSR with task-specific bits set */
59 DEFINE_PER_CPU(u64, x86_spec_ctrl_current);
60 EXPORT_SYMBOL_GPL(x86_spec_ctrl_current);
61
62 u64 x86_pred_cmd __ro_after_init = PRED_CMD_IBPB;
63 EXPORT_SYMBOL_GPL(x86_pred_cmd);
64
65 static u64 __ro_after_init x86_arch_cap_msr;
66
67 static DEFINE_MUTEX(spec_ctrl_mutex);
68
69 void (*x86_return_thunk)(void) __ro_after_init = __x86_return_thunk;
70
set_return_thunk(void * thunk)71 static void __init set_return_thunk(void *thunk)
72 {
73 if (x86_return_thunk != __x86_return_thunk)
74 pr_warn("x86/bugs: return thunk changed\n");
75
76 x86_return_thunk = thunk;
77 }
78
79 /* Update SPEC_CTRL MSR and its cached copy unconditionally */
update_spec_ctrl(u64 val)80 static void update_spec_ctrl(u64 val)
81 {
82 this_cpu_write(x86_spec_ctrl_current, val);
83 wrmsrl(MSR_IA32_SPEC_CTRL, val);
84 }
85
86 /*
87 * Keep track of the SPEC_CTRL MSR value for the current task, which may differ
88 * from x86_spec_ctrl_base due to STIBP/SSB in __speculation_ctrl_update().
89 */
update_spec_ctrl_cond(u64 val)90 void update_spec_ctrl_cond(u64 val)
91 {
92 if (this_cpu_read(x86_spec_ctrl_current) == val)
93 return;
94
95 this_cpu_write(x86_spec_ctrl_current, val);
96
97 /*
98 * When KERNEL_IBRS this MSR is written on return-to-user, unless
99 * forced the update can be delayed until that time.
100 */
101 if (!cpu_feature_enabled(X86_FEATURE_KERNEL_IBRS))
102 wrmsrl(MSR_IA32_SPEC_CTRL, val);
103 }
104
spec_ctrl_current(void)105 noinstr u64 spec_ctrl_current(void)
106 {
107 return this_cpu_read(x86_spec_ctrl_current);
108 }
109 EXPORT_SYMBOL_GPL(spec_ctrl_current);
110
111 /*
112 * AMD specific MSR info for Speculative Store Bypass control.
113 * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu().
114 */
115 u64 __ro_after_init x86_amd_ls_cfg_base;
116 u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask;
117
118 /* Control conditional STIBP in switch_to() */
119 DEFINE_STATIC_KEY_FALSE(switch_to_cond_stibp);
120 /* Control conditional IBPB in switch_mm() */
121 DEFINE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
122 /* Control unconditional IBPB in switch_mm() */
123 DEFINE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
124
125 /* Control MDS CPU buffer clear before idling (halt, mwait) */
126 DEFINE_STATIC_KEY_FALSE(mds_idle_clear);
127 EXPORT_SYMBOL_GPL(mds_idle_clear);
128
129 /*
130 * Controls whether l1d flush based mitigations are enabled,
131 * based on hw features and admin setting via boot parameter
132 * defaults to false
133 */
134 DEFINE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);
135
136 /* Controls CPU Fill buffer clear before KVM guest MMIO accesses */
137 DEFINE_STATIC_KEY_FALSE(mmio_stale_data_clear);
138 EXPORT_SYMBOL_GPL(mmio_stale_data_clear);
139
cpu_select_mitigations(void)140 void __init cpu_select_mitigations(void)
141 {
142 /*
143 * Read the SPEC_CTRL MSR to account for reserved bits which may
144 * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
145 * init code as it is not enumerated and depends on the family.
146 */
147 if (cpu_feature_enabled(X86_FEATURE_MSR_SPEC_CTRL)) {
148 rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
149
150 /*
151 * Previously running kernel (kexec), may have some controls
152 * turned ON. Clear them and let the mitigations setup below
153 * rediscover them based on configuration.
154 */
155 x86_spec_ctrl_base &= ~SPEC_CTRL_MITIGATIONS_MASK;
156 }
157
158 x86_arch_cap_msr = x86_read_arch_cap_msr();
159
160 /* Select the proper CPU mitigations before patching alternatives: */
161 spectre_v1_select_mitigation();
162 spectre_v2_select_mitigation();
163 /*
164 * retbleed_select_mitigation() relies on the state set by
165 * spectre_v2_select_mitigation(); specifically it wants to know about
166 * spectre_v2=ibrs.
167 */
168 retbleed_select_mitigation();
169 /*
170 * spectre_v2_user_select_mitigation() relies on the state set by
171 * retbleed_select_mitigation(); specifically the STIBP selection is
172 * forced for UNRET or IBPB.
173 */
174 spectre_v2_user_select_mitigation();
175 ssb_select_mitigation();
176 l1tf_select_mitigation();
177 md_clear_select_mitigation();
178 srbds_select_mitigation();
179 l1d_flush_select_mitigation();
180
181 /*
182 * srso_select_mitigation() depends and must run after
183 * retbleed_select_mitigation().
184 */
185 srso_select_mitigation();
186 gds_select_mitigation();
187 its_select_mitigation();
188 }
189
190 /*
191 * NOTE: This function is *only* called for SVM, since Intel uses
192 * MSR_IA32_SPEC_CTRL for SSBD.
193 */
194 void
x86_virt_spec_ctrl(u64 guest_virt_spec_ctrl,bool setguest)195 x86_virt_spec_ctrl(u64 guest_virt_spec_ctrl, bool setguest)
196 {
197 u64 guestval, hostval;
198 struct thread_info *ti = current_thread_info();
199
200 /*
201 * If SSBD is not handled in MSR_SPEC_CTRL on AMD, update
202 * MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported.
203 */
204 if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
205 !static_cpu_has(X86_FEATURE_VIRT_SSBD))
206 return;
207
208 /*
209 * If the host has SSBD mitigation enabled, force it in the host's
210 * virtual MSR value. If its not permanently enabled, evaluate
211 * current's TIF_SSBD thread flag.
212 */
213 if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE))
214 hostval = SPEC_CTRL_SSBD;
215 else
216 hostval = ssbd_tif_to_spec_ctrl(ti->flags);
217
218 /* Sanitize the guest value */
219 guestval = guest_virt_spec_ctrl & SPEC_CTRL_SSBD;
220
221 if (hostval != guestval) {
222 unsigned long tif;
223
224 tif = setguest ? ssbd_spec_ctrl_to_tif(guestval) :
225 ssbd_spec_ctrl_to_tif(hostval);
226
227 speculation_ctrl_update(tif);
228 }
229 }
230 EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl);
231
x86_amd_ssb_disable(void)232 static void x86_amd_ssb_disable(void)
233 {
234 u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_ssbd_mask;
235
236 if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
237 wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, SPEC_CTRL_SSBD);
238 else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
239 wrmsrl(MSR_AMD64_LS_CFG, msrval);
240 }
241
242 #undef pr_fmt
243 #define pr_fmt(fmt) "MDS: " fmt
244
245 /* Default mitigation for MDS-affected CPUs */
246 static enum mds_mitigations mds_mitigation __ro_after_init = MDS_MITIGATION_FULL;
247 static bool mds_nosmt __ro_after_init = false;
248
249 static const char * const mds_strings[] = {
250 [MDS_MITIGATION_OFF] = "Vulnerable",
251 [MDS_MITIGATION_FULL] = "Mitigation: Clear CPU buffers",
252 [MDS_MITIGATION_VMWERV] = "Vulnerable: Clear CPU buffers attempted, no microcode",
253 };
254
mds_select_mitigation(void)255 static void __init mds_select_mitigation(void)
256 {
257 if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off()) {
258 mds_mitigation = MDS_MITIGATION_OFF;
259 return;
260 }
261
262 if (mds_mitigation == MDS_MITIGATION_FULL) {
263 if (!boot_cpu_has(X86_FEATURE_MD_CLEAR))
264 mds_mitigation = MDS_MITIGATION_VMWERV;
265
266 setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
267
268 if (!boot_cpu_has(X86_BUG_MSBDS_ONLY) &&
269 (mds_nosmt || cpu_mitigations_auto_nosmt()))
270 cpu_smt_disable(false);
271 }
272 }
273
mds_cmdline(char * str)274 static int __init mds_cmdline(char *str)
275 {
276 if (!boot_cpu_has_bug(X86_BUG_MDS))
277 return 0;
278
279 if (!str)
280 return -EINVAL;
281
282 if (!strcmp(str, "off"))
283 mds_mitigation = MDS_MITIGATION_OFF;
284 else if (!strcmp(str, "full"))
285 mds_mitigation = MDS_MITIGATION_FULL;
286 else if (!strcmp(str, "full,nosmt")) {
287 mds_mitigation = MDS_MITIGATION_FULL;
288 mds_nosmt = true;
289 }
290
291 return 0;
292 }
293 early_param("mds", mds_cmdline);
294
295 #undef pr_fmt
296 #define pr_fmt(fmt) "TAA: " fmt
297
298 enum taa_mitigations {
299 TAA_MITIGATION_OFF,
300 TAA_MITIGATION_UCODE_NEEDED,
301 TAA_MITIGATION_VERW,
302 TAA_MITIGATION_TSX_DISABLED,
303 };
304
305 /* Default mitigation for TAA-affected CPUs */
306 static enum taa_mitigations taa_mitigation __ro_after_init = TAA_MITIGATION_VERW;
307 static bool taa_nosmt __ro_after_init;
308
309 static const char * const taa_strings[] = {
310 [TAA_MITIGATION_OFF] = "Vulnerable",
311 [TAA_MITIGATION_UCODE_NEEDED] = "Vulnerable: Clear CPU buffers attempted, no microcode",
312 [TAA_MITIGATION_VERW] = "Mitigation: Clear CPU buffers",
313 [TAA_MITIGATION_TSX_DISABLED] = "Mitigation: TSX disabled",
314 };
315
taa_select_mitigation(void)316 static void __init taa_select_mitigation(void)
317 {
318 if (!boot_cpu_has_bug(X86_BUG_TAA)) {
319 taa_mitigation = TAA_MITIGATION_OFF;
320 return;
321 }
322
323 /* TSX previously disabled by tsx=off */
324 if (!boot_cpu_has(X86_FEATURE_RTM)) {
325 taa_mitigation = TAA_MITIGATION_TSX_DISABLED;
326 return;
327 }
328
329 if (cpu_mitigations_off()) {
330 taa_mitigation = TAA_MITIGATION_OFF;
331 return;
332 }
333
334 /*
335 * TAA mitigation via VERW is turned off if both
336 * tsx_async_abort=off and mds=off are specified.
337 */
338 if (taa_mitigation == TAA_MITIGATION_OFF &&
339 mds_mitigation == MDS_MITIGATION_OFF)
340 return;
341
342 if (boot_cpu_has(X86_FEATURE_MD_CLEAR))
343 taa_mitigation = TAA_MITIGATION_VERW;
344 else
345 taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
346
347 /*
348 * VERW doesn't clear the CPU buffers when MD_CLEAR=1 and MDS_NO=1.
349 * A microcode update fixes this behavior to clear CPU buffers. It also
350 * adds support for MSR_IA32_TSX_CTRL which is enumerated by the
351 * ARCH_CAP_TSX_CTRL_MSR bit.
352 *
353 * On MDS_NO=1 CPUs if ARCH_CAP_TSX_CTRL_MSR is not set, microcode
354 * update is required.
355 */
356 if ( (x86_arch_cap_msr & ARCH_CAP_MDS_NO) &&
357 !(x86_arch_cap_msr & ARCH_CAP_TSX_CTRL_MSR))
358 taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
359
360 /*
361 * TSX is enabled, select alternate mitigation for TAA which is
362 * the same as MDS. Enable MDS static branch to clear CPU buffers.
363 *
364 * For guests that can't determine whether the correct microcode is
365 * present on host, enable the mitigation for UCODE_NEEDED as well.
366 */
367 setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
368
369 if (taa_nosmt || cpu_mitigations_auto_nosmt())
370 cpu_smt_disable(false);
371 }
372
tsx_async_abort_parse_cmdline(char * str)373 static int __init tsx_async_abort_parse_cmdline(char *str)
374 {
375 if (!boot_cpu_has_bug(X86_BUG_TAA))
376 return 0;
377
378 if (!str)
379 return -EINVAL;
380
381 if (!strcmp(str, "off")) {
382 taa_mitigation = TAA_MITIGATION_OFF;
383 } else if (!strcmp(str, "full")) {
384 taa_mitigation = TAA_MITIGATION_VERW;
385 } else if (!strcmp(str, "full,nosmt")) {
386 taa_mitigation = TAA_MITIGATION_VERW;
387 taa_nosmt = true;
388 }
389
390 return 0;
391 }
392 early_param("tsx_async_abort", tsx_async_abort_parse_cmdline);
393
394 #undef pr_fmt
395 #define pr_fmt(fmt) "MMIO Stale Data: " fmt
396
397 enum mmio_mitigations {
398 MMIO_MITIGATION_OFF,
399 MMIO_MITIGATION_UCODE_NEEDED,
400 MMIO_MITIGATION_VERW,
401 };
402
403 /* Default mitigation for Processor MMIO Stale Data vulnerabilities */
404 static enum mmio_mitigations mmio_mitigation __ro_after_init = MMIO_MITIGATION_VERW;
405 static bool mmio_nosmt __ro_after_init = false;
406
407 static const char * const mmio_strings[] = {
408 [MMIO_MITIGATION_OFF] = "Vulnerable",
409 [MMIO_MITIGATION_UCODE_NEEDED] = "Vulnerable: Clear CPU buffers attempted, no microcode",
410 [MMIO_MITIGATION_VERW] = "Mitigation: Clear CPU buffers",
411 };
412
mmio_select_mitigation(void)413 static void __init mmio_select_mitigation(void)
414 {
415 if (!boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA) ||
416 boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN) ||
417 cpu_mitigations_off()) {
418 mmio_mitigation = MMIO_MITIGATION_OFF;
419 return;
420 }
421
422 if (mmio_mitigation == MMIO_MITIGATION_OFF)
423 return;
424
425 /*
426 * Enable CPU buffer clear mitigation for host and VMM, if also affected
427 * by MDS or TAA. Otherwise, enable mitigation for VMM only.
428 */
429 if (boot_cpu_has_bug(X86_BUG_MDS) || (boot_cpu_has_bug(X86_BUG_TAA) &&
430 boot_cpu_has(X86_FEATURE_RTM)))
431 setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
432
433 /*
434 * X86_FEATURE_CLEAR_CPU_BUF could be enabled by other VERW based
435 * mitigations, disable KVM-only mitigation in that case.
436 */
437 if (boot_cpu_has(X86_FEATURE_CLEAR_CPU_BUF))
438 static_branch_disable(&mmio_stale_data_clear);
439 else
440 static_branch_enable(&mmio_stale_data_clear);
441
442 /*
443 * If Processor-MMIO-Stale-Data bug is present and Fill Buffer data can
444 * be propagated to uncore buffers, clearing the Fill buffers on idle
445 * is required irrespective of SMT state.
446 */
447 if (!(x86_arch_cap_msr & ARCH_CAP_FBSDP_NO))
448 static_branch_enable(&mds_idle_clear);
449
450 /*
451 * Check if the system has the right microcode.
452 *
453 * CPU Fill buffer clear mitigation is enumerated by either an explicit
454 * FB_CLEAR or by the presence of both MD_CLEAR and L1D_FLUSH on MDS
455 * affected systems.
456 */
457 if ((x86_arch_cap_msr & ARCH_CAP_FB_CLEAR) ||
458 (boot_cpu_has(X86_FEATURE_MD_CLEAR) &&
459 boot_cpu_has(X86_FEATURE_FLUSH_L1D) &&
460 !(x86_arch_cap_msr & ARCH_CAP_MDS_NO)))
461 mmio_mitigation = MMIO_MITIGATION_VERW;
462 else
463 mmio_mitigation = MMIO_MITIGATION_UCODE_NEEDED;
464
465 if (mmio_nosmt || cpu_mitigations_auto_nosmt())
466 cpu_smt_disable(false);
467 }
468
mmio_stale_data_parse_cmdline(char * str)469 static int __init mmio_stale_data_parse_cmdline(char *str)
470 {
471 if (!boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA))
472 return 0;
473
474 if (!str)
475 return -EINVAL;
476
477 if (!strcmp(str, "off")) {
478 mmio_mitigation = MMIO_MITIGATION_OFF;
479 } else if (!strcmp(str, "full")) {
480 mmio_mitigation = MMIO_MITIGATION_VERW;
481 } else if (!strcmp(str, "full,nosmt")) {
482 mmio_mitigation = MMIO_MITIGATION_VERW;
483 mmio_nosmt = true;
484 }
485
486 return 0;
487 }
488 early_param("mmio_stale_data", mmio_stale_data_parse_cmdline);
489
490 #undef pr_fmt
491 #define pr_fmt(fmt) "Register File Data Sampling: " fmt
492
493 enum rfds_mitigations {
494 RFDS_MITIGATION_OFF,
495 RFDS_MITIGATION_VERW,
496 RFDS_MITIGATION_UCODE_NEEDED,
497 };
498
499 /* Default mitigation for Register File Data Sampling */
500 static enum rfds_mitigations rfds_mitigation __ro_after_init =
501 IS_ENABLED(CONFIG_MITIGATION_RFDS) ? RFDS_MITIGATION_VERW : RFDS_MITIGATION_OFF;
502
503 static const char * const rfds_strings[] = {
504 [RFDS_MITIGATION_OFF] = "Vulnerable",
505 [RFDS_MITIGATION_VERW] = "Mitigation: Clear Register File",
506 [RFDS_MITIGATION_UCODE_NEEDED] = "Vulnerable: No microcode",
507 };
508
rfds_select_mitigation(void)509 static void __init rfds_select_mitigation(void)
510 {
511 if (!boot_cpu_has_bug(X86_BUG_RFDS) || cpu_mitigations_off()) {
512 rfds_mitigation = RFDS_MITIGATION_OFF;
513 return;
514 }
515 if (rfds_mitigation == RFDS_MITIGATION_OFF)
516 return;
517
518 if (x86_arch_cap_msr & ARCH_CAP_RFDS_CLEAR)
519 setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
520 else
521 rfds_mitigation = RFDS_MITIGATION_UCODE_NEEDED;
522 }
523
rfds_parse_cmdline(char * str)524 static __init int rfds_parse_cmdline(char *str)
525 {
526 if (!str)
527 return -EINVAL;
528
529 if (!boot_cpu_has_bug(X86_BUG_RFDS))
530 return 0;
531
532 if (!strcmp(str, "off"))
533 rfds_mitigation = RFDS_MITIGATION_OFF;
534 else if (!strcmp(str, "on"))
535 rfds_mitigation = RFDS_MITIGATION_VERW;
536
537 return 0;
538 }
539 early_param("reg_file_data_sampling", rfds_parse_cmdline);
540
541 #undef pr_fmt
542 #define pr_fmt(fmt) "" fmt
543
md_clear_update_mitigation(void)544 static void __init md_clear_update_mitigation(void)
545 {
546 if (cpu_mitigations_off())
547 return;
548
549 if (!boot_cpu_has(X86_FEATURE_CLEAR_CPU_BUF))
550 goto out;
551
552 /*
553 * X86_FEATURE_CLEAR_CPU_BUF is now enabled. Update MDS, TAA and MMIO
554 * Stale Data mitigation, if necessary.
555 */
556 if (mds_mitigation == MDS_MITIGATION_OFF &&
557 boot_cpu_has_bug(X86_BUG_MDS)) {
558 mds_mitigation = MDS_MITIGATION_FULL;
559 mds_select_mitigation();
560 }
561 if (taa_mitigation == TAA_MITIGATION_OFF &&
562 boot_cpu_has_bug(X86_BUG_TAA)) {
563 taa_mitigation = TAA_MITIGATION_VERW;
564 taa_select_mitigation();
565 }
566 /*
567 * MMIO_MITIGATION_OFF is not checked here so that mmio_stale_data_clear
568 * gets updated correctly as per X86_FEATURE_CLEAR_CPU_BUF state.
569 */
570 if (boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA)) {
571 mmio_mitigation = MMIO_MITIGATION_VERW;
572 mmio_select_mitigation();
573 }
574 if (rfds_mitigation == RFDS_MITIGATION_OFF &&
575 boot_cpu_has_bug(X86_BUG_RFDS)) {
576 rfds_mitigation = RFDS_MITIGATION_VERW;
577 rfds_select_mitigation();
578 }
579 out:
580 if (boot_cpu_has_bug(X86_BUG_MDS))
581 pr_info("MDS: %s\n", mds_strings[mds_mitigation]);
582 if (boot_cpu_has_bug(X86_BUG_TAA))
583 pr_info("TAA: %s\n", taa_strings[taa_mitigation]);
584 if (boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA))
585 pr_info("MMIO Stale Data: %s\n", mmio_strings[mmio_mitigation]);
586 else if (boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN))
587 pr_info("MMIO Stale Data: Unknown: No mitigations\n");
588 if (boot_cpu_has_bug(X86_BUG_RFDS))
589 pr_info("Register File Data Sampling: %s\n", rfds_strings[rfds_mitigation]);
590 }
591
md_clear_select_mitigation(void)592 static void __init md_clear_select_mitigation(void)
593 {
594 mds_select_mitigation();
595 taa_select_mitigation();
596 mmio_select_mitigation();
597 rfds_select_mitigation();
598
599 /*
600 * As these mitigations are inter-related and rely on VERW instruction
601 * to clear the microarchitural buffers, update and print their status
602 * after mitigation selection is done for each of these vulnerabilities.
603 */
604 md_clear_update_mitigation();
605 }
606
607 #undef pr_fmt
608 #define pr_fmt(fmt) "SRBDS: " fmt
609
610 enum srbds_mitigations {
611 SRBDS_MITIGATION_OFF,
612 SRBDS_MITIGATION_UCODE_NEEDED,
613 SRBDS_MITIGATION_FULL,
614 SRBDS_MITIGATION_TSX_OFF,
615 SRBDS_MITIGATION_HYPERVISOR,
616 };
617
618 static enum srbds_mitigations srbds_mitigation __ro_after_init = SRBDS_MITIGATION_FULL;
619
620 static const char * const srbds_strings[] = {
621 [SRBDS_MITIGATION_OFF] = "Vulnerable",
622 [SRBDS_MITIGATION_UCODE_NEEDED] = "Vulnerable: No microcode",
623 [SRBDS_MITIGATION_FULL] = "Mitigation: Microcode",
624 [SRBDS_MITIGATION_TSX_OFF] = "Mitigation: TSX disabled",
625 [SRBDS_MITIGATION_HYPERVISOR] = "Unknown: Dependent on hypervisor status",
626 };
627
628 static bool srbds_off;
629
update_srbds_msr(void)630 void update_srbds_msr(void)
631 {
632 u64 mcu_ctrl;
633
634 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
635 return;
636
637 if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
638 return;
639
640 if (srbds_mitigation == SRBDS_MITIGATION_UCODE_NEEDED)
641 return;
642
643 /*
644 * A MDS_NO CPU for which SRBDS mitigation is not needed due to TSX
645 * being disabled and it hasn't received the SRBDS MSR microcode.
646 */
647 if (!boot_cpu_has(X86_FEATURE_SRBDS_CTRL))
648 return;
649
650 rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
651
652 switch (srbds_mitigation) {
653 case SRBDS_MITIGATION_OFF:
654 case SRBDS_MITIGATION_TSX_OFF:
655 mcu_ctrl |= RNGDS_MITG_DIS;
656 break;
657 case SRBDS_MITIGATION_FULL:
658 mcu_ctrl &= ~RNGDS_MITG_DIS;
659 break;
660 default:
661 break;
662 }
663
664 wrmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
665 }
666
srbds_select_mitigation(void)667 static void __init srbds_select_mitigation(void)
668 {
669 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
670 return;
671
672 /*
673 * Check to see if this is one of the MDS_NO systems supporting TSX that
674 * are only exposed to SRBDS when TSX is enabled or when CPU is affected
675 * by Processor MMIO Stale Data vulnerability.
676 */
677 if ((x86_arch_cap_msr & ARCH_CAP_MDS_NO) && !boot_cpu_has(X86_FEATURE_RTM) &&
678 !boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA))
679 srbds_mitigation = SRBDS_MITIGATION_TSX_OFF;
680 else if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
681 srbds_mitigation = SRBDS_MITIGATION_HYPERVISOR;
682 else if (!boot_cpu_has(X86_FEATURE_SRBDS_CTRL))
683 srbds_mitigation = SRBDS_MITIGATION_UCODE_NEEDED;
684 else if (cpu_mitigations_off() || srbds_off)
685 srbds_mitigation = SRBDS_MITIGATION_OFF;
686
687 update_srbds_msr();
688 pr_info("%s\n", srbds_strings[srbds_mitigation]);
689 }
690
srbds_parse_cmdline(char * str)691 static int __init srbds_parse_cmdline(char *str)
692 {
693 if (!str)
694 return -EINVAL;
695
696 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
697 return 0;
698
699 srbds_off = !strcmp(str, "off");
700 return 0;
701 }
702 early_param("srbds", srbds_parse_cmdline);
703
704 #undef pr_fmt
705 #define pr_fmt(fmt) "L1D Flush : " fmt
706
707 enum l1d_flush_mitigations {
708 L1D_FLUSH_OFF = 0,
709 L1D_FLUSH_ON,
710 };
711
712 static enum l1d_flush_mitigations l1d_flush_mitigation __initdata = L1D_FLUSH_OFF;
713
l1d_flush_select_mitigation(void)714 static void __init l1d_flush_select_mitigation(void)
715 {
716 if (!l1d_flush_mitigation || !boot_cpu_has(X86_FEATURE_FLUSH_L1D))
717 return;
718
719 static_branch_enable(&switch_mm_cond_l1d_flush);
720 pr_info("Conditional flush on switch_mm() enabled\n");
721 }
722
l1d_flush_parse_cmdline(char * str)723 static int __init l1d_flush_parse_cmdline(char *str)
724 {
725 if (!strcmp(str, "on"))
726 l1d_flush_mitigation = L1D_FLUSH_ON;
727
728 return 0;
729 }
730 early_param("l1d_flush", l1d_flush_parse_cmdline);
731
732 #undef pr_fmt
733 #define pr_fmt(fmt) "GDS: " fmt
734
735 enum gds_mitigations {
736 GDS_MITIGATION_OFF,
737 GDS_MITIGATION_UCODE_NEEDED,
738 GDS_MITIGATION_FORCE,
739 GDS_MITIGATION_FULL,
740 GDS_MITIGATION_FULL_LOCKED,
741 GDS_MITIGATION_HYPERVISOR,
742 };
743
744 #if IS_ENABLED(CONFIG_GDS_FORCE_MITIGATION)
745 static enum gds_mitigations gds_mitigation __ro_after_init = GDS_MITIGATION_FORCE;
746 #else
747 static enum gds_mitigations gds_mitigation __ro_after_init = GDS_MITIGATION_FULL;
748 #endif
749
750 static const char * const gds_strings[] = {
751 [GDS_MITIGATION_OFF] = "Vulnerable",
752 [GDS_MITIGATION_UCODE_NEEDED] = "Vulnerable: No microcode",
753 [GDS_MITIGATION_FORCE] = "Mitigation: AVX disabled, no microcode",
754 [GDS_MITIGATION_FULL] = "Mitigation: Microcode",
755 [GDS_MITIGATION_FULL_LOCKED] = "Mitigation: Microcode (locked)",
756 [GDS_MITIGATION_HYPERVISOR] = "Unknown: Dependent on hypervisor status",
757 };
758
gds_ucode_mitigated(void)759 bool gds_ucode_mitigated(void)
760 {
761 return (gds_mitigation == GDS_MITIGATION_FULL ||
762 gds_mitigation == GDS_MITIGATION_FULL_LOCKED);
763 }
764 EXPORT_SYMBOL_GPL(gds_ucode_mitigated);
765
update_gds_msr(void)766 void update_gds_msr(void)
767 {
768 u64 mcu_ctrl_after;
769 u64 mcu_ctrl;
770
771 switch (gds_mitigation) {
772 case GDS_MITIGATION_OFF:
773 rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
774 mcu_ctrl |= GDS_MITG_DIS;
775 break;
776 case GDS_MITIGATION_FULL_LOCKED:
777 /*
778 * The LOCKED state comes from the boot CPU. APs might not have
779 * the same state. Make sure the mitigation is enabled on all
780 * CPUs.
781 */
782 case GDS_MITIGATION_FULL:
783 rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
784 mcu_ctrl &= ~GDS_MITG_DIS;
785 break;
786 case GDS_MITIGATION_FORCE:
787 case GDS_MITIGATION_UCODE_NEEDED:
788 case GDS_MITIGATION_HYPERVISOR:
789 return;
790 };
791
792 wrmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
793
794 /*
795 * Check to make sure that the WRMSR value was not ignored. Writes to
796 * GDS_MITG_DIS will be ignored if this processor is locked but the boot
797 * processor was not.
798 */
799 rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl_after);
800 WARN_ON_ONCE(mcu_ctrl != mcu_ctrl_after);
801 }
802
gds_select_mitigation(void)803 static void __init gds_select_mitigation(void)
804 {
805 u64 mcu_ctrl;
806
807 if (!boot_cpu_has_bug(X86_BUG_GDS))
808 return;
809
810 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
811 gds_mitigation = GDS_MITIGATION_HYPERVISOR;
812 goto out;
813 }
814
815 if (cpu_mitigations_off())
816 gds_mitigation = GDS_MITIGATION_OFF;
817 /* Will verify below that mitigation _can_ be disabled */
818
819 /* No microcode */
820 if (!(x86_arch_cap_msr & ARCH_CAP_GDS_CTRL)) {
821 if (gds_mitigation == GDS_MITIGATION_FORCE) {
822 /*
823 * This only needs to be done on the boot CPU so do it
824 * here rather than in update_gds_msr()
825 */
826 setup_clear_cpu_cap(X86_FEATURE_AVX);
827 pr_warn("Microcode update needed! Disabling AVX as mitigation.\n");
828 } else {
829 gds_mitigation = GDS_MITIGATION_UCODE_NEEDED;
830 }
831 goto out;
832 }
833
834 /* Microcode has mitigation, use it */
835 if (gds_mitigation == GDS_MITIGATION_FORCE)
836 gds_mitigation = GDS_MITIGATION_FULL;
837
838 rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
839 if (mcu_ctrl & GDS_MITG_LOCKED) {
840 if (gds_mitigation == GDS_MITIGATION_OFF)
841 pr_warn("Mitigation locked. Disable failed.\n");
842
843 /*
844 * The mitigation is selected from the boot CPU. All other CPUs
845 * _should_ have the same state. If the boot CPU isn't locked
846 * but others are then update_gds_msr() will WARN() of the state
847 * mismatch. If the boot CPU is locked update_gds_msr() will
848 * ensure the other CPUs have the mitigation enabled.
849 */
850 gds_mitigation = GDS_MITIGATION_FULL_LOCKED;
851 }
852
853 update_gds_msr();
854 out:
855 pr_info("%s\n", gds_strings[gds_mitigation]);
856 }
857
gds_parse_cmdline(char * str)858 static int __init gds_parse_cmdline(char *str)
859 {
860 if (!str)
861 return -EINVAL;
862
863 if (!boot_cpu_has_bug(X86_BUG_GDS))
864 return 0;
865
866 if (!strcmp(str, "off"))
867 gds_mitigation = GDS_MITIGATION_OFF;
868 else if (!strcmp(str, "force"))
869 gds_mitigation = GDS_MITIGATION_FORCE;
870
871 return 0;
872 }
873 early_param("gather_data_sampling", gds_parse_cmdline);
874
875 #undef pr_fmt
876 #define pr_fmt(fmt) "Spectre V1 : " fmt
877
878 enum spectre_v1_mitigation {
879 SPECTRE_V1_MITIGATION_NONE,
880 SPECTRE_V1_MITIGATION_AUTO,
881 };
882
883 static enum spectre_v1_mitigation spectre_v1_mitigation __ro_after_init =
884 SPECTRE_V1_MITIGATION_AUTO;
885
886 static const char * const spectre_v1_strings[] = {
887 [SPECTRE_V1_MITIGATION_NONE] = "Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers",
888 [SPECTRE_V1_MITIGATION_AUTO] = "Mitigation: usercopy/swapgs barriers and __user pointer sanitization",
889 };
890
891 /*
892 * Does SMAP provide full mitigation against speculative kernel access to
893 * userspace?
894 */
smap_works_speculatively(void)895 static bool smap_works_speculatively(void)
896 {
897 if (!boot_cpu_has(X86_FEATURE_SMAP))
898 return false;
899
900 /*
901 * On CPUs which are vulnerable to Meltdown, SMAP does not
902 * prevent speculative access to user data in the L1 cache.
903 * Consider SMAP to be non-functional as a mitigation on these
904 * CPUs.
905 */
906 if (boot_cpu_has(X86_BUG_CPU_MELTDOWN))
907 return false;
908
909 return true;
910 }
911
spectre_v1_select_mitigation(void)912 static void __init spectre_v1_select_mitigation(void)
913 {
914 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1) || cpu_mitigations_off()) {
915 spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
916 return;
917 }
918
919 if (spectre_v1_mitigation == SPECTRE_V1_MITIGATION_AUTO) {
920 /*
921 * With Spectre v1, a user can speculatively control either
922 * path of a conditional swapgs with a user-controlled GS
923 * value. The mitigation is to add lfences to both code paths.
924 *
925 * If FSGSBASE is enabled, the user can put a kernel address in
926 * GS, in which case SMAP provides no protection.
927 *
928 * If FSGSBASE is disabled, the user can only put a user space
929 * address in GS. That makes an attack harder, but still
930 * possible if there's no SMAP protection.
931 */
932 if (boot_cpu_has(X86_FEATURE_FSGSBASE) ||
933 !smap_works_speculatively()) {
934 /*
935 * Mitigation can be provided from SWAPGS itself or
936 * PTI as the CR3 write in the Meltdown mitigation
937 * is serializing.
938 *
939 * If neither is there, mitigate with an LFENCE to
940 * stop speculation through swapgs.
941 */
942 if (boot_cpu_has_bug(X86_BUG_SWAPGS) &&
943 !boot_cpu_has(X86_FEATURE_PTI))
944 setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_USER);
945
946 /*
947 * Enable lfences in the kernel entry (non-swapgs)
948 * paths, to prevent user entry from speculatively
949 * skipping swapgs.
950 */
951 setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_KERNEL);
952 }
953 }
954
955 pr_info("%s\n", spectre_v1_strings[spectre_v1_mitigation]);
956 }
957
nospectre_v1_cmdline(char * str)958 static int __init nospectre_v1_cmdline(char *str)
959 {
960 spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
961 return 0;
962 }
963 early_param("nospectre_v1", nospectre_v1_cmdline);
964
965 enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init = SPECTRE_V2_NONE;
966
967 #undef pr_fmt
968 #define pr_fmt(fmt) "RETBleed: " fmt
969
970 enum retbleed_mitigation {
971 RETBLEED_MITIGATION_NONE,
972 RETBLEED_MITIGATION_UNRET,
973 RETBLEED_MITIGATION_IBPB,
974 RETBLEED_MITIGATION_IBRS,
975 RETBLEED_MITIGATION_EIBRS,
976 RETBLEED_MITIGATION_STUFF,
977 };
978
979 enum retbleed_mitigation_cmd {
980 RETBLEED_CMD_OFF,
981 RETBLEED_CMD_AUTO,
982 RETBLEED_CMD_UNRET,
983 RETBLEED_CMD_IBPB,
984 RETBLEED_CMD_STUFF,
985 };
986
987 static const char * const retbleed_strings[] = {
988 [RETBLEED_MITIGATION_NONE] = "Vulnerable",
989 [RETBLEED_MITIGATION_UNRET] = "Mitigation: untrained return thunk",
990 [RETBLEED_MITIGATION_IBPB] = "Mitigation: IBPB",
991 [RETBLEED_MITIGATION_IBRS] = "Mitigation: IBRS",
992 [RETBLEED_MITIGATION_EIBRS] = "Mitigation: Enhanced IBRS",
993 [RETBLEED_MITIGATION_STUFF] = "Mitigation: Stuffing",
994 };
995
996 static enum retbleed_mitigation retbleed_mitigation __ro_after_init =
997 RETBLEED_MITIGATION_NONE;
998 static enum retbleed_mitigation_cmd retbleed_cmd __ro_after_init =
999 RETBLEED_CMD_AUTO;
1000
1001 static int __ro_after_init retbleed_nosmt = false;
1002
retbleed_parse_cmdline(char * str)1003 static int __init retbleed_parse_cmdline(char *str)
1004 {
1005 if (!str)
1006 return -EINVAL;
1007
1008 while (str) {
1009 char *next = strchr(str, ',');
1010 if (next) {
1011 *next = 0;
1012 next++;
1013 }
1014
1015 if (!strcmp(str, "off")) {
1016 retbleed_cmd = RETBLEED_CMD_OFF;
1017 } else if (!strcmp(str, "auto")) {
1018 retbleed_cmd = RETBLEED_CMD_AUTO;
1019 } else if (!strcmp(str, "unret")) {
1020 retbleed_cmd = RETBLEED_CMD_UNRET;
1021 } else if (!strcmp(str, "ibpb")) {
1022 retbleed_cmd = RETBLEED_CMD_IBPB;
1023 } else if (!strcmp(str, "stuff")) {
1024 retbleed_cmd = RETBLEED_CMD_STUFF;
1025 } else if (!strcmp(str, "nosmt")) {
1026 retbleed_nosmt = true;
1027 } else if (!strcmp(str, "force")) {
1028 setup_force_cpu_bug(X86_BUG_RETBLEED);
1029 } else {
1030 pr_err("Ignoring unknown retbleed option (%s).", str);
1031 }
1032
1033 str = next;
1034 }
1035
1036 return 0;
1037 }
1038 early_param("retbleed", retbleed_parse_cmdline);
1039
1040 #define RETBLEED_UNTRAIN_MSG "WARNING: BTB untrained return thunk mitigation is only effective on AMD/Hygon!\n"
1041 #define RETBLEED_INTEL_MSG "WARNING: Spectre v2 mitigation leaves CPU vulnerable to RETBleed attacks, data leaks possible!\n"
1042
retbleed_select_mitigation(void)1043 static void __init retbleed_select_mitigation(void)
1044 {
1045 bool mitigate_smt = false;
1046
1047 if (!boot_cpu_has_bug(X86_BUG_RETBLEED) || cpu_mitigations_off())
1048 return;
1049
1050 switch (retbleed_cmd) {
1051 case RETBLEED_CMD_OFF:
1052 return;
1053
1054 case RETBLEED_CMD_UNRET:
1055 if (IS_ENABLED(CONFIG_CPU_UNRET_ENTRY)) {
1056 retbleed_mitigation = RETBLEED_MITIGATION_UNRET;
1057 } else {
1058 pr_err("WARNING: kernel not compiled with CPU_UNRET_ENTRY.\n");
1059 goto do_cmd_auto;
1060 }
1061 break;
1062
1063 case RETBLEED_CMD_IBPB:
1064 if (!boot_cpu_has(X86_FEATURE_IBPB)) {
1065 pr_err("WARNING: CPU does not support IBPB.\n");
1066 goto do_cmd_auto;
1067 } else if (IS_ENABLED(CONFIG_CPU_IBPB_ENTRY)) {
1068 retbleed_mitigation = RETBLEED_MITIGATION_IBPB;
1069 } else {
1070 pr_err("WARNING: kernel not compiled with CPU_IBPB_ENTRY.\n");
1071 goto do_cmd_auto;
1072 }
1073 break;
1074
1075 case RETBLEED_CMD_STUFF:
1076 if (IS_ENABLED(CONFIG_CALL_DEPTH_TRACKING) &&
1077 spectre_v2_enabled == SPECTRE_V2_RETPOLINE) {
1078 retbleed_mitigation = RETBLEED_MITIGATION_STUFF;
1079
1080 } else {
1081 if (IS_ENABLED(CONFIG_CALL_DEPTH_TRACKING))
1082 pr_err("WARNING: retbleed=stuff depends on spectre_v2=retpoline\n");
1083 else
1084 pr_err("WARNING: kernel not compiled with CALL_DEPTH_TRACKING.\n");
1085
1086 goto do_cmd_auto;
1087 }
1088 break;
1089
1090 do_cmd_auto:
1091 case RETBLEED_CMD_AUTO:
1092 default:
1093 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
1094 boot_cpu_data.x86_vendor == X86_VENDOR_HYGON) {
1095 if (IS_ENABLED(CONFIG_CPU_UNRET_ENTRY))
1096 retbleed_mitigation = RETBLEED_MITIGATION_UNRET;
1097 else if (IS_ENABLED(CONFIG_CPU_IBPB_ENTRY) && boot_cpu_has(X86_FEATURE_IBPB))
1098 retbleed_mitigation = RETBLEED_MITIGATION_IBPB;
1099 }
1100
1101 /*
1102 * The Intel mitigation (IBRS or eIBRS) was already selected in
1103 * spectre_v2_select_mitigation(). 'retbleed_mitigation' will
1104 * be set accordingly below.
1105 */
1106
1107 break;
1108 }
1109
1110 switch (retbleed_mitigation) {
1111 case RETBLEED_MITIGATION_UNRET:
1112 setup_force_cpu_cap(X86_FEATURE_RETHUNK);
1113 setup_force_cpu_cap(X86_FEATURE_UNRET);
1114
1115 set_return_thunk(retbleed_return_thunk);
1116
1117 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
1118 boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)
1119 pr_err(RETBLEED_UNTRAIN_MSG);
1120
1121 mitigate_smt = true;
1122 break;
1123
1124 case RETBLEED_MITIGATION_IBPB:
1125 setup_force_cpu_cap(X86_FEATURE_ENTRY_IBPB);
1126 setup_force_cpu_cap(X86_FEATURE_IBPB_ON_VMEXIT);
1127 mitigate_smt = true;
1128
1129 /*
1130 * IBPB on entry already obviates the need for
1131 * software-based untraining so clear those in case some
1132 * other mitigation like SRSO has selected them.
1133 */
1134 setup_clear_cpu_cap(X86_FEATURE_UNRET);
1135 setup_clear_cpu_cap(X86_FEATURE_RETHUNK);
1136
1137 /*
1138 * There is no need for RSB filling: entry_ibpb() ensures
1139 * all predictions, including the RSB, are invalidated,
1140 * regardless of IBPB implementation.
1141 */
1142 setup_clear_cpu_cap(X86_FEATURE_RSB_VMEXIT);
1143
1144 break;
1145
1146 case RETBLEED_MITIGATION_STUFF:
1147 setup_force_cpu_cap(X86_FEATURE_RETHUNK);
1148 setup_force_cpu_cap(X86_FEATURE_CALL_DEPTH);
1149 #ifdef CONFIG_CALL_DEPTH_TRACKING
1150 set_return_thunk(&__x86_return_skl);
1151 #endif
1152 break;
1153
1154 default:
1155 break;
1156 }
1157
1158 if (mitigate_smt && !boot_cpu_has(X86_FEATURE_STIBP) &&
1159 (retbleed_nosmt || cpu_mitigations_auto_nosmt()))
1160 cpu_smt_disable(false);
1161
1162 /*
1163 * Let IBRS trump all on Intel without affecting the effects of the
1164 * retbleed= cmdline option except for call depth based stuffing
1165 */
1166 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
1167 switch (spectre_v2_enabled) {
1168 case SPECTRE_V2_IBRS:
1169 retbleed_mitigation = RETBLEED_MITIGATION_IBRS;
1170 break;
1171 case SPECTRE_V2_EIBRS:
1172 case SPECTRE_V2_EIBRS_RETPOLINE:
1173 case SPECTRE_V2_EIBRS_LFENCE:
1174 retbleed_mitigation = RETBLEED_MITIGATION_EIBRS;
1175 break;
1176 default:
1177 if (retbleed_mitigation != RETBLEED_MITIGATION_STUFF)
1178 pr_err(RETBLEED_INTEL_MSG);
1179 }
1180 }
1181
1182 pr_info("%s\n", retbleed_strings[retbleed_mitigation]);
1183 }
1184
1185 #undef pr_fmt
1186 #define pr_fmt(fmt) "ITS: " fmt
1187
1188 enum its_mitigation_cmd {
1189 ITS_CMD_OFF,
1190 ITS_CMD_ON,
1191 ITS_CMD_VMEXIT,
1192 ITS_CMD_RSB_STUFF,
1193 };
1194
1195 enum its_mitigation {
1196 ITS_MITIGATION_OFF,
1197 ITS_MITIGATION_VMEXIT_ONLY,
1198 ITS_MITIGATION_ALIGNED_THUNKS,
1199 ITS_MITIGATION_RETPOLINE_STUFF,
1200 };
1201
1202 static const char * const its_strings[] = {
1203 [ITS_MITIGATION_OFF] = "Vulnerable",
1204 [ITS_MITIGATION_VMEXIT_ONLY] = "Mitigation: Vulnerable, KVM: Not affected",
1205 [ITS_MITIGATION_ALIGNED_THUNKS] = "Mitigation: Aligned branch/return thunks",
1206 [ITS_MITIGATION_RETPOLINE_STUFF] = "Mitigation: Retpolines, Stuffing RSB",
1207 };
1208
1209 static enum its_mitigation its_mitigation __ro_after_init = ITS_MITIGATION_ALIGNED_THUNKS;
1210
1211 static enum its_mitigation_cmd its_cmd __ro_after_init =
1212 IS_ENABLED(CONFIG_MITIGATION_ITS) ? ITS_CMD_ON : ITS_CMD_OFF;
1213
its_parse_cmdline(char * str)1214 static int __init its_parse_cmdline(char *str)
1215 {
1216 if (!str)
1217 return -EINVAL;
1218
1219 if (!IS_ENABLED(CONFIG_MITIGATION_ITS)) {
1220 pr_err("Mitigation disabled at compile time, ignoring option (%s)", str);
1221 return 0;
1222 }
1223
1224 if (!strcmp(str, "off")) {
1225 its_cmd = ITS_CMD_OFF;
1226 } else if (!strcmp(str, "on")) {
1227 its_cmd = ITS_CMD_ON;
1228 } else if (!strcmp(str, "force")) {
1229 its_cmd = ITS_CMD_ON;
1230 setup_force_cpu_bug(X86_BUG_ITS);
1231 } else if (!strcmp(str, "vmexit")) {
1232 its_cmd = ITS_CMD_VMEXIT;
1233 } else if (!strcmp(str, "stuff")) {
1234 its_cmd = ITS_CMD_RSB_STUFF;
1235 } else {
1236 pr_err("Ignoring unknown indirect_target_selection option (%s).", str);
1237 }
1238
1239 return 0;
1240 }
1241 early_param("indirect_target_selection", its_parse_cmdline);
1242
its_select_mitigation(void)1243 static void __init its_select_mitigation(void)
1244 {
1245 enum its_mitigation_cmd cmd = its_cmd;
1246
1247 if (!boot_cpu_has_bug(X86_BUG_ITS) || cpu_mitigations_off()) {
1248 its_mitigation = ITS_MITIGATION_OFF;
1249 return;
1250 }
1251
1252 /* Retpoline+CDT mitigates ITS, bail out */
1253 if (boot_cpu_has(X86_FEATURE_RETPOLINE) &&
1254 boot_cpu_has(X86_FEATURE_CALL_DEPTH)) {
1255 its_mitigation = ITS_MITIGATION_RETPOLINE_STUFF;
1256 goto out;
1257 }
1258
1259 /* Exit early to avoid irrelevant warnings */
1260 if (cmd == ITS_CMD_OFF) {
1261 its_mitigation = ITS_MITIGATION_OFF;
1262 goto out;
1263 }
1264 if (spectre_v2_enabled == SPECTRE_V2_NONE) {
1265 pr_err("WARNING: Spectre-v2 mitigation is off, disabling ITS\n");
1266 its_mitigation = ITS_MITIGATION_OFF;
1267 goto out;
1268 }
1269 if (!IS_ENABLED(CONFIG_RETPOLINE) || !IS_ENABLED(CONFIG_RETHUNK)) {
1270 pr_err("WARNING: ITS mitigation depends on retpoline and rethunk support\n");
1271 its_mitigation = ITS_MITIGATION_OFF;
1272 goto out;
1273 }
1274 if (IS_ENABLED(CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_64B)) {
1275 pr_err("WARNING: ITS mitigation is not compatible with CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_64B\n");
1276 its_mitigation = ITS_MITIGATION_OFF;
1277 goto out;
1278 }
1279 if (boot_cpu_has(X86_FEATURE_RETPOLINE_LFENCE)) {
1280 pr_err("WARNING: ITS mitigation is not compatible with lfence mitigation\n");
1281 its_mitigation = ITS_MITIGATION_OFF;
1282 goto out;
1283 }
1284
1285 if (cmd == ITS_CMD_RSB_STUFF &&
1286 (!boot_cpu_has(X86_FEATURE_RETPOLINE) || !IS_ENABLED(CONFIG_CALL_DEPTH_TRACKING))) {
1287 pr_err("RSB stuff mitigation not supported, using default\n");
1288 cmd = ITS_CMD_ON;
1289 }
1290
1291 switch (cmd) {
1292 case ITS_CMD_OFF:
1293 its_mitigation = ITS_MITIGATION_OFF;
1294 break;
1295 case ITS_CMD_VMEXIT:
1296 if (boot_cpu_has_bug(X86_BUG_ITS_NATIVE_ONLY)) {
1297 its_mitigation = ITS_MITIGATION_VMEXIT_ONLY;
1298 goto out;
1299 }
1300 fallthrough;
1301 case ITS_CMD_ON:
1302 its_mitigation = ITS_MITIGATION_ALIGNED_THUNKS;
1303 if (!boot_cpu_has(X86_FEATURE_RETPOLINE))
1304 setup_force_cpu_cap(X86_FEATURE_INDIRECT_THUNK_ITS);
1305 setup_force_cpu_cap(X86_FEATURE_RETHUNK);
1306 set_return_thunk(its_return_thunk);
1307 break;
1308 case ITS_CMD_RSB_STUFF:
1309 its_mitigation = ITS_MITIGATION_RETPOLINE_STUFF;
1310 setup_force_cpu_cap(X86_FEATURE_RETHUNK);
1311 setup_force_cpu_cap(X86_FEATURE_CALL_DEPTH);
1312 #ifdef CONFIG_CALL_DEPTH_TRACKING
1313 set_return_thunk(&__x86_return_skl);
1314 #endif
1315 if (retbleed_mitigation == RETBLEED_MITIGATION_NONE) {
1316 retbleed_mitigation = RETBLEED_MITIGATION_STUFF;
1317 pr_info("Retbleed mitigation updated to stuffing\n");
1318 }
1319 break;
1320 }
1321 out:
1322 pr_info("%s\n", its_strings[its_mitigation]);
1323 }
1324
1325 #undef pr_fmt
1326 #define pr_fmt(fmt) "Spectre V2 : " fmt
1327
1328 static enum spectre_v2_user_mitigation spectre_v2_user_stibp __ro_after_init =
1329 SPECTRE_V2_USER_NONE;
1330 static enum spectre_v2_user_mitigation spectre_v2_user_ibpb __ro_after_init =
1331 SPECTRE_V2_USER_NONE;
1332
1333 #ifdef CONFIG_RETPOLINE
1334 static bool spectre_v2_bad_module;
1335
retpoline_module_ok(bool has_retpoline)1336 bool retpoline_module_ok(bool has_retpoline)
1337 {
1338 if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
1339 return true;
1340
1341 pr_err("System may be vulnerable to spectre v2\n");
1342 spectre_v2_bad_module = true;
1343 return false;
1344 }
1345
spectre_v2_module_string(void)1346 static inline const char *spectre_v2_module_string(void)
1347 {
1348 return spectre_v2_bad_module ? " - vulnerable module loaded" : "";
1349 }
1350 #else
spectre_v2_module_string(void)1351 static inline const char *spectre_v2_module_string(void) { return ""; }
1352 #endif
1353
1354 #define SPECTRE_V2_LFENCE_MSG "WARNING: LFENCE mitigation is not recommended for this CPU, data leaks possible!\n"
1355 #define SPECTRE_V2_EIBRS_EBPF_MSG "WARNING: Unprivileged eBPF is enabled with eIBRS on, data leaks possible via Spectre v2 BHB attacks!\n"
1356 #define SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG "WARNING: Unprivileged eBPF is enabled with eIBRS+LFENCE mitigation and SMT, data leaks possible via Spectre v2 BHB attacks!\n"
1357 #define SPECTRE_V2_IBRS_PERF_MSG "WARNING: IBRS mitigation selected on Enhanced IBRS CPU, this may cause unnecessary performance loss\n"
1358
1359 #ifdef CONFIG_BPF_SYSCALL
unpriv_ebpf_notify(int new_state)1360 void unpriv_ebpf_notify(int new_state)
1361 {
1362 if (new_state)
1363 return;
1364
1365 /* Unprivileged eBPF is enabled */
1366
1367 switch (spectre_v2_enabled) {
1368 case SPECTRE_V2_EIBRS:
1369 pr_err(SPECTRE_V2_EIBRS_EBPF_MSG);
1370 break;
1371 case SPECTRE_V2_EIBRS_LFENCE:
1372 if (sched_smt_active())
1373 pr_err(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG);
1374 break;
1375 default:
1376 break;
1377 }
1378 }
1379 #endif
1380
match_option(const char * arg,int arglen,const char * opt)1381 static inline bool match_option(const char *arg, int arglen, const char *opt)
1382 {
1383 int len = strlen(opt);
1384
1385 return len == arglen && !strncmp(arg, opt, len);
1386 }
1387
1388 /* The kernel command line selection for spectre v2 */
1389 enum spectre_v2_mitigation_cmd {
1390 SPECTRE_V2_CMD_NONE,
1391 SPECTRE_V2_CMD_AUTO,
1392 SPECTRE_V2_CMD_FORCE,
1393 SPECTRE_V2_CMD_RETPOLINE,
1394 SPECTRE_V2_CMD_RETPOLINE_GENERIC,
1395 SPECTRE_V2_CMD_RETPOLINE_LFENCE,
1396 SPECTRE_V2_CMD_EIBRS,
1397 SPECTRE_V2_CMD_EIBRS_RETPOLINE,
1398 SPECTRE_V2_CMD_EIBRS_LFENCE,
1399 SPECTRE_V2_CMD_IBRS,
1400 };
1401
1402 enum spectre_v2_user_cmd {
1403 SPECTRE_V2_USER_CMD_NONE,
1404 SPECTRE_V2_USER_CMD_AUTO,
1405 SPECTRE_V2_USER_CMD_FORCE,
1406 SPECTRE_V2_USER_CMD_PRCTL,
1407 SPECTRE_V2_USER_CMD_PRCTL_IBPB,
1408 SPECTRE_V2_USER_CMD_SECCOMP,
1409 SPECTRE_V2_USER_CMD_SECCOMP_IBPB,
1410 };
1411
1412 static const char * const spectre_v2_user_strings[] = {
1413 [SPECTRE_V2_USER_NONE] = "User space: Vulnerable",
1414 [SPECTRE_V2_USER_STRICT] = "User space: Mitigation: STIBP protection",
1415 [SPECTRE_V2_USER_STRICT_PREFERRED] = "User space: Mitigation: STIBP always-on protection",
1416 [SPECTRE_V2_USER_PRCTL] = "User space: Mitigation: STIBP via prctl",
1417 [SPECTRE_V2_USER_SECCOMP] = "User space: Mitigation: STIBP via seccomp and prctl",
1418 };
1419
1420 static const struct {
1421 const char *option;
1422 enum spectre_v2_user_cmd cmd;
1423 bool secure;
1424 } v2_user_options[] __initconst = {
1425 { "auto", SPECTRE_V2_USER_CMD_AUTO, false },
1426 { "off", SPECTRE_V2_USER_CMD_NONE, false },
1427 { "on", SPECTRE_V2_USER_CMD_FORCE, true },
1428 { "prctl", SPECTRE_V2_USER_CMD_PRCTL, false },
1429 { "prctl,ibpb", SPECTRE_V2_USER_CMD_PRCTL_IBPB, false },
1430 { "seccomp", SPECTRE_V2_USER_CMD_SECCOMP, false },
1431 { "seccomp,ibpb", SPECTRE_V2_USER_CMD_SECCOMP_IBPB, false },
1432 };
1433
spec_v2_user_print_cond(const char * reason,bool secure)1434 static void __init spec_v2_user_print_cond(const char *reason, bool secure)
1435 {
1436 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
1437 pr_info("spectre_v2_user=%s forced on command line.\n", reason);
1438 }
1439
1440 static __ro_after_init enum spectre_v2_mitigation_cmd spectre_v2_cmd;
1441
1442 static enum spectre_v2_user_cmd __init
spectre_v2_parse_user_cmdline(void)1443 spectre_v2_parse_user_cmdline(void)
1444 {
1445 char arg[20];
1446 int ret, i;
1447
1448 switch (spectre_v2_cmd) {
1449 case SPECTRE_V2_CMD_NONE:
1450 return SPECTRE_V2_USER_CMD_NONE;
1451 case SPECTRE_V2_CMD_FORCE:
1452 return SPECTRE_V2_USER_CMD_FORCE;
1453 default:
1454 break;
1455 }
1456
1457 ret = cmdline_find_option(boot_command_line, "spectre_v2_user",
1458 arg, sizeof(arg));
1459 if (ret < 0)
1460 return SPECTRE_V2_USER_CMD_AUTO;
1461
1462 for (i = 0; i < ARRAY_SIZE(v2_user_options); i++) {
1463 if (match_option(arg, ret, v2_user_options[i].option)) {
1464 spec_v2_user_print_cond(v2_user_options[i].option,
1465 v2_user_options[i].secure);
1466 return v2_user_options[i].cmd;
1467 }
1468 }
1469
1470 pr_err("Unknown user space protection option (%s). Switching to AUTO select\n", arg);
1471 return SPECTRE_V2_USER_CMD_AUTO;
1472 }
1473
spectre_v2_in_ibrs_mode(enum spectre_v2_mitigation mode)1474 static inline bool spectre_v2_in_ibrs_mode(enum spectre_v2_mitigation mode)
1475 {
1476 return spectre_v2_in_eibrs_mode(mode) || mode == SPECTRE_V2_IBRS;
1477 }
1478
1479 static void __init
spectre_v2_user_select_mitigation(void)1480 spectre_v2_user_select_mitigation(void)
1481 {
1482 enum spectre_v2_user_mitigation mode = SPECTRE_V2_USER_NONE;
1483 bool smt_possible = IS_ENABLED(CONFIG_SMP);
1484 enum spectre_v2_user_cmd cmd;
1485
1486 if (!boot_cpu_has(X86_FEATURE_IBPB) && !boot_cpu_has(X86_FEATURE_STIBP))
1487 return;
1488
1489 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
1490 cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
1491 smt_possible = false;
1492
1493 cmd = spectre_v2_parse_user_cmdline();
1494 switch (cmd) {
1495 case SPECTRE_V2_USER_CMD_NONE:
1496 goto set_mode;
1497 case SPECTRE_V2_USER_CMD_FORCE:
1498 mode = SPECTRE_V2_USER_STRICT;
1499 break;
1500 case SPECTRE_V2_USER_CMD_AUTO:
1501 case SPECTRE_V2_USER_CMD_PRCTL:
1502 case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
1503 mode = SPECTRE_V2_USER_PRCTL;
1504 break;
1505 case SPECTRE_V2_USER_CMD_SECCOMP:
1506 case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
1507 if (IS_ENABLED(CONFIG_SECCOMP))
1508 mode = SPECTRE_V2_USER_SECCOMP;
1509 else
1510 mode = SPECTRE_V2_USER_PRCTL;
1511 break;
1512 }
1513
1514 /* Initialize Indirect Branch Prediction Barrier */
1515 if (boot_cpu_has(X86_FEATURE_IBPB)) {
1516 setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
1517
1518 spectre_v2_user_ibpb = mode;
1519 switch (cmd) {
1520 case SPECTRE_V2_USER_CMD_FORCE:
1521 case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
1522 case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
1523 static_branch_enable(&switch_mm_always_ibpb);
1524 spectre_v2_user_ibpb = SPECTRE_V2_USER_STRICT;
1525 break;
1526 case SPECTRE_V2_USER_CMD_PRCTL:
1527 case SPECTRE_V2_USER_CMD_AUTO:
1528 case SPECTRE_V2_USER_CMD_SECCOMP:
1529 static_branch_enable(&switch_mm_cond_ibpb);
1530 break;
1531 default:
1532 break;
1533 }
1534
1535 pr_info("mitigation: Enabling %s Indirect Branch Prediction Barrier\n",
1536 static_key_enabled(&switch_mm_always_ibpb) ?
1537 "always-on" : "conditional");
1538 }
1539
1540 /*
1541 * If no STIBP, Intel enhanced IBRS is enabled, or SMT impossible, STIBP
1542 * is not required.
1543 *
1544 * Intel's Enhanced IBRS also protects against cross-thread branch target
1545 * injection in user-mode as the IBRS bit remains always set which
1546 * implicitly enables cross-thread protections. However, in legacy IBRS
1547 * mode, the IBRS bit is set only on kernel entry and cleared on return
1548 * to userspace. AMD Automatic IBRS also does not protect userspace.
1549 * These modes therefore disable the implicit cross-thread protection,
1550 * so allow for STIBP to be selected in those cases.
1551 */
1552 if (!boot_cpu_has(X86_FEATURE_STIBP) ||
1553 !smt_possible ||
1554 (spectre_v2_in_eibrs_mode(spectre_v2_enabled) &&
1555 !boot_cpu_has(X86_FEATURE_AUTOIBRS)))
1556 return;
1557
1558 /*
1559 * At this point, an STIBP mode other than "off" has been set.
1560 * If STIBP support is not being forced, check if STIBP always-on
1561 * is preferred.
1562 */
1563 if (mode != SPECTRE_V2_USER_STRICT &&
1564 boot_cpu_has(X86_FEATURE_AMD_STIBP_ALWAYS_ON))
1565 mode = SPECTRE_V2_USER_STRICT_PREFERRED;
1566
1567 if (retbleed_mitigation == RETBLEED_MITIGATION_UNRET ||
1568 retbleed_mitigation == RETBLEED_MITIGATION_IBPB) {
1569 if (mode != SPECTRE_V2_USER_STRICT &&
1570 mode != SPECTRE_V2_USER_STRICT_PREFERRED)
1571 pr_info("Selecting STIBP always-on mode to complement retbleed mitigation\n");
1572 mode = SPECTRE_V2_USER_STRICT_PREFERRED;
1573 }
1574
1575 spectre_v2_user_stibp = mode;
1576
1577 set_mode:
1578 pr_info("%s\n", spectre_v2_user_strings[mode]);
1579 }
1580
1581 static const char * const spectre_v2_strings[] = {
1582 [SPECTRE_V2_NONE] = "Vulnerable",
1583 [SPECTRE_V2_RETPOLINE] = "Mitigation: Retpolines",
1584 [SPECTRE_V2_LFENCE] = "Mitigation: LFENCE",
1585 [SPECTRE_V2_EIBRS] = "Mitigation: Enhanced / Automatic IBRS",
1586 [SPECTRE_V2_EIBRS_LFENCE] = "Mitigation: Enhanced / Automatic IBRS + LFENCE",
1587 [SPECTRE_V2_EIBRS_RETPOLINE] = "Mitigation: Enhanced / Automatic IBRS + Retpolines",
1588 [SPECTRE_V2_IBRS] = "Mitigation: IBRS",
1589 };
1590
1591 static const struct {
1592 const char *option;
1593 enum spectre_v2_mitigation_cmd cmd;
1594 bool secure;
1595 } mitigation_options[] __initconst = {
1596 { "off", SPECTRE_V2_CMD_NONE, false },
1597 { "on", SPECTRE_V2_CMD_FORCE, true },
1598 { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false },
1599 { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_LFENCE, false },
1600 { "retpoline,lfence", SPECTRE_V2_CMD_RETPOLINE_LFENCE, false },
1601 { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
1602 { "eibrs", SPECTRE_V2_CMD_EIBRS, false },
1603 { "eibrs,lfence", SPECTRE_V2_CMD_EIBRS_LFENCE, false },
1604 { "eibrs,retpoline", SPECTRE_V2_CMD_EIBRS_RETPOLINE, false },
1605 { "auto", SPECTRE_V2_CMD_AUTO, false },
1606 { "ibrs", SPECTRE_V2_CMD_IBRS, false },
1607 };
1608
spec_v2_print_cond(const char * reason,bool secure)1609 static void __init spec_v2_print_cond(const char *reason, bool secure)
1610 {
1611 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
1612 pr_info("%s selected on command line.\n", reason);
1613 }
1614
spectre_v2_parse_cmdline(void)1615 static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
1616 {
1617 enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
1618 char arg[20];
1619 int ret, i;
1620
1621 if (cmdline_find_option_bool(boot_command_line, "nospectre_v2") ||
1622 cpu_mitigations_off())
1623 return SPECTRE_V2_CMD_NONE;
1624
1625 ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
1626 if (ret < 0)
1627 return SPECTRE_V2_CMD_AUTO;
1628
1629 for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
1630 if (!match_option(arg, ret, mitigation_options[i].option))
1631 continue;
1632 cmd = mitigation_options[i].cmd;
1633 break;
1634 }
1635
1636 if (i >= ARRAY_SIZE(mitigation_options)) {
1637 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
1638 return SPECTRE_V2_CMD_AUTO;
1639 }
1640
1641 if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
1642 cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE ||
1643 cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC ||
1644 cmd == SPECTRE_V2_CMD_EIBRS_LFENCE ||
1645 cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) &&
1646 !IS_ENABLED(CONFIG_RETPOLINE)) {
1647 pr_err("%s selected but not compiled in. Switching to AUTO select\n",
1648 mitigation_options[i].option);
1649 return SPECTRE_V2_CMD_AUTO;
1650 }
1651
1652 if ((cmd == SPECTRE_V2_CMD_EIBRS ||
1653 cmd == SPECTRE_V2_CMD_EIBRS_LFENCE ||
1654 cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) &&
1655 !boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
1656 pr_err("%s selected but CPU doesn't have Enhanced or Automatic IBRS. Switching to AUTO select\n",
1657 mitigation_options[i].option);
1658 return SPECTRE_V2_CMD_AUTO;
1659 }
1660
1661 if ((cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE ||
1662 cmd == SPECTRE_V2_CMD_EIBRS_LFENCE) &&
1663 !boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
1664 pr_err("%s selected, but CPU doesn't have a serializing LFENCE. Switching to AUTO select\n",
1665 mitigation_options[i].option);
1666 return SPECTRE_V2_CMD_AUTO;
1667 }
1668
1669 if (cmd == SPECTRE_V2_CMD_IBRS && !IS_ENABLED(CONFIG_CPU_IBRS_ENTRY)) {
1670 pr_err("%s selected but not compiled in. Switching to AUTO select\n",
1671 mitigation_options[i].option);
1672 return SPECTRE_V2_CMD_AUTO;
1673 }
1674
1675 if (cmd == SPECTRE_V2_CMD_IBRS && boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
1676 pr_err("%s selected but not Intel CPU. Switching to AUTO select\n",
1677 mitigation_options[i].option);
1678 return SPECTRE_V2_CMD_AUTO;
1679 }
1680
1681 if (cmd == SPECTRE_V2_CMD_IBRS && !boot_cpu_has(X86_FEATURE_IBRS)) {
1682 pr_err("%s selected but CPU doesn't have IBRS. Switching to AUTO select\n",
1683 mitigation_options[i].option);
1684 return SPECTRE_V2_CMD_AUTO;
1685 }
1686
1687 if (cmd == SPECTRE_V2_CMD_IBRS && cpu_feature_enabled(X86_FEATURE_XENPV)) {
1688 pr_err("%s selected but running as XenPV guest. Switching to AUTO select\n",
1689 mitigation_options[i].option);
1690 return SPECTRE_V2_CMD_AUTO;
1691 }
1692
1693 spec_v2_print_cond(mitigation_options[i].option,
1694 mitigation_options[i].secure);
1695 return cmd;
1696 }
1697
spectre_v2_select_retpoline(void)1698 static enum spectre_v2_mitigation __init spectre_v2_select_retpoline(void)
1699 {
1700 if (!IS_ENABLED(CONFIG_RETPOLINE)) {
1701 pr_err("Kernel not compiled with retpoline; no mitigation available!");
1702 return SPECTRE_V2_NONE;
1703 }
1704
1705 return SPECTRE_V2_RETPOLINE;
1706 }
1707
1708 static bool __ro_after_init rrsba_disabled;
1709
1710 /* Disable in-kernel use of non-RSB RET predictors */
spec_ctrl_disable_kernel_rrsba(void)1711 static void __init spec_ctrl_disable_kernel_rrsba(void)
1712 {
1713 if (rrsba_disabled)
1714 return;
1715
1716 if (!(x86_arch_cap_msr & ARCH_CAP_RRSBA)) {
1717 rrsba_disabled = true;
1718 return;
1719 }
1720
1721 if (!boot_cpu_has(X86_FEATURE_RRSBA_CTRL))
1722 return;
1723
1724 x86_spec_ctrl_base |= SPEC_CTRL_RRSBA_DIS_S;
1725 update_spec_ctrl(x86_spec_ctrl_base);
1726 rrsba_disabled = true;
1727 }
1728
spectre_v2_select_rsb_mitigation(enum spectre_v2_mitigation mode)1729 static void __init spectre_v2_select_rsb_mitigation(enum spectre_v2_mitigation mode)
1730 {
1731 /*
1732 * Similar to context switches, there are two types of RSB attacks
1733 * after VM exit:
1734 *
1735 * 1) RSB underflow
1736 *
1737 * 2) Poisoned RSB entry
1738 *
1739 * When retpoline is enabled, both are mitigated by filling/clearing
1740 * the RSB.
1741 *
1742 * When IBRS is enabled, while #1 would be mitigated by the IBRS branch
1743 * prediction isolation protections, RSB still needs to be cleared
1744 * because of #2. Note that SMEP provides no protection here, unlike
1745 * user-space-poisoned RSB entries.
1746 *
1747 * eIBRS should protect against RSB poisoning, but if the EIBRS_PBRSB
1748 * bug is present then a LITE version of RSB protection is required,
1749 * just a single call needs to retire before a RET is executed.
1750 */
1751 switch (mode) {
1752 case SPECTRE_V2_NONE:
1753 break;
1754
1755 case SPECTRE_V2_EIBRS:
1756 case SPECTRE_V2_EIBRS_LFENCE:
1757 case SPECTRE_V2_EIBRS_RETPOLINE:
1758 if (boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB)) {
1759 pr_info("Spectre v2 / PBRSB-eIBRS: Retire a single CALL on VMEXIT\n");
1760 setup_force_cpu_cap(X86_FEATURE_RSB_VMEXIT_LITE);
1761 }
1762 break;
1763
1764 case SPECTRE_V2_RETPOLINE:
1765 case SPECTRE_V2_LFENCE:
1766 case SPECTRE_V2_IBRS:
1767 pr_info("Spectre v2 / SpectreRSB: Filling RSB on context switch and VMEXIT\n");
1768 setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
1769 setup_force_cpu_cap(X86_FEATURE_RSB_VMEXIT);
1770 break;
1771
1772 default:
1773 pr_warn_once("Unknown Spectre v2 mode, disabling RSB mitigation\n");
1774 dump_stack();
1775 break;
1776 }
1777 }
1778
1779 /*
1780 * Set BHI_DIS_S to prevent indirect branches in kernel to be influenced by
1781 * branch history in userspace. Not needed if BHI_NO is set.
1782 */
spec_ctrl_bhi_dis(void)1783 static bool __init spec_ctrl_bhi_dis(void)
1784 {
1785 if (!boot_cpu_has(X86_FEATURE_BHI_CTRL))
1786 return false;
1787
1788 x86_spec_ctrl_base |= SPEC_CTRL_BHI_DIS_S;
1789 update_spec_ctrl(x86_spec_ctrl_base);
1790 setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_HW);
1791
1792 return true;
1793 }
1794
1795 enum bhi_mitigations {
1796 BHI_MITIGATION_OFF,
1797 BHI_MITIGATION_ON,
1798 };
1799
1800 static enum bhi_mitigations bhi_mitigation __ro_after_init =
1801 IS_ENABLED(CONFIG_MITIGATION_SPECTRE_BHI) ? BHI_MITIGATION_ON : BHI_MITIGATION_OFF;
1802
spectre_bhi_parse_cmdline(char * str)1803 static int __init spectre_bhi_parse_cmdline(char *str)
1804 {
1805 if (!str)
1806 return -EINVAL;
1807
1808 if (!strcmp(str, "off"))
1809 bhi_mitigation = BHI_MITIGATION_OFF;
1810 else if (!strcmp(str, "on"))
1811 bhi_mitigation = BHI_MITIGATION_ON;
1812 else
1813 pr_err("Ignoring unknown spectre_bhi option (%s)", str);
1814
1815 return 0;
1816 }
1817 early_param("spectre_bhi", spectre_bhi_parse_cmdline);
1818
bhi_select_mitigation(void)1819 static void __init bhi_select_mitigation(void)
1820 {
1821 if (bhi_mitigation == BHI_MITIGATION_OFF)
1822 return;
1823
1824 /* Retpoline mitigates against BHI unless the CPU has RRSBA behavior */
1825 if (boot_cpu_has(X86_FEATURE_RETPOLINE) &&
1826 !boot_cpu_has(X86_FEATURE_RETPOLINE_LFENCE)) {
1827 spec_ctrl_disable_kernel_rrsba();
1828 if (rrsba_disabled)
1829 return;
1830 }
1831
1832 if (!IS_ENABLED(CONFIG_X86_64))
1833 return;
1834
1835 /* Mitigate in hardware if supported */
1836 if (spec_ctrl_bhi_dis())
1837 return;
1838
1839 /* Mitigate KVM by default */
1840 setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_LOOP_ON_VMEXIT);
1841 pr_info("Spectre BHI mitigation: SW BHB clearing on vm exit\n");
1842
1843 /* Mitigate syscalls when the mitigation is forced =on */
1844 setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_LOOP);
1845 pr_info("Spectre BHI mitigation: SW BHB clearing on syscall\n");
1846 }
1847
spectre_v2_select_mitigation(void)1848 static void __init spectre_v2_select_mitigation(void)
1849 {
1850 enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
1851 enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
1852
1853 /*
1854 * If the CPU is not affected and the command line mode is NONE or AUTO
1855 * then nothing to do.
1856 */
1857 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
1858 (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
1859 return;
1860
1861 switch (cmd) {
1862 case SPECTRE_V2_CMD_NONE:
1863 return;
1864
1865 case SPECTRE_V2_CMD_FORCE:
1866 case SPECTRE_V2_CMD_AUTO:
1867 if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
1868 mode = SPECTRE_V2_EIBRS;
1869 break;
1870 }
1871
1872 if (IS_ENABLED(CONFIG_CPU_IBRS_ENTRY) &&
1873 boot_cpu_has_bug(X86_BUG_RETBLEED) &&
1874 retbleed_cmd != RETBLEED_CMD_OFF &&
1875 retbleed_cmd != RETBLEED_CMD_STUFF &&
1876 boot_cpu_has(X86_FEATURE_IBRS) &&
1877 boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
1878 mode = SPECTRE_V2_IBRS;
1879 break;
1880 }
1881
1882 mode = spectre_v2_select_retpoline();
1883 break;
1884
1885 case SPECTRE_V2_CMD_RETPOLINE_LFENCE:
1886 pr_err(SPECTRE_V2_LFENCE_MSG);
1887 mode = SPECTRE_V2_LFENCE;
1888 break;
1889
1890 case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
1891 mode = SPECTRE_V2_RETPOLINE;
1892 break;
1893
1894 case SPECTRE_V2_CMD_RETPOLINE:
1895 mode = spectre_v2_select_retpoline();
1896 break;
1897
1898 case SPECTRE_V2_CMD_IBRS:
1899 mode = SPECTRE_V2_IBRS;
1900 break;
1901
1902 case SPECTRE_V2_CMD_EIBRS:
1903 mode = SPECTRE_V2_EIBRS;
1904 break;
1905
1906 case SPECTRE_V2_CMD_EIBRS_LFENCE:
1907 mode = SPECTRE_V2_EIBRS_LFENCE;
1908 break;
1909
1910 case SPECTRE_V2_CMD_EIBRS_RETPOLINE:
1911 mode = SPECTRE_V2_EIBRS_RETPOLINE;
1912 break;
1913 }
1914
1915 if (mode == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
1916 pr_err(SPECTRE_V2_EIBRS_EBPF_MSG);
1917
1918 if (spectre_v2_in_ibrs_mode(mode)) {
1919 if (boot_cpu_has(X86_FEATURE_AUTOIBRS)) {
1920 msr_set_bit(MSR_EFER, _EFER_AUTOIBRS);
1921 } else {
1922 x86_spec_ctrl_base |= SPEC_CTRL_IBRS;
1923 update_spec_ctrl(x86_spec_ctrl_base);
1924 }
1925 }
1926
1927 switch (mode) {
1928 case SPECTRE_V2_NONE:
1929 case SPECTRE_V2_EIBRS:
1930 break;
1931
1932 case SPECTRE_V2_IBRS:
1933 setup_force_cpu_cap(X86_FEATURE_KERNEL_IBRS);
1934 if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED))
1935 pr_warn(SPECTRE_V2_IBRS_PERF_MSG);
1936 break;
1937
1938 case SPECTRE_V2_LFENCE:
1939 case SPECTRE_V2_EIBRS_LFENCE:
1940 setup_force_cpu_cap(X86_FEATURE_RETPOLINE_LFENCE);
1941 fallthrough;
1942
1943 case SPECTRE_V2_RETPOLINE:
1944 case SPECTRE_V2_EIBRS_RETPOLINE:
1945 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
1946 break;
1947 }
1948
1949 /*
1950 * Disable alternate RSB predictions in kernel when indirect CALLs and
1951 * JMPs gets protection against BHI and Intramode-BTI, but RET
1952 * prediction from a non-RSB predictor is still a risk.
1953 */
1954 if (mode == SPECTRE_V2_EIBRS_LFENCE ||
1955 mode == SPECTRE_V2_EIBRS_RETPOLINE ||
1956 mode == SPECTRE_V2_RETPOLINE)
1957 spec_ctrl_disable_kernel_rrsba();
1958
1959 if (boot_cpu_has(X86_BUG_BHI))
1960 bhi_select_mitigation();
1961
1962 spectre_v2_enabled = mode;
1963 pr_info("%s\n", spectre_v2_strings[mode]);
1964
1965 /*
1966 * If Spectre v2 protection has been enabled, fill the RSB during a
1967 * context switch. In general there are two types of RSB attacks
1968 * across context switches, for which the CALLs/RETs may be unbalanced.
1969 *
1970 * 1) RSB underflow
1971 *
1972 * Some Intel parts have "bottomless RSB". When the RSB is empty,
1973 * speculated return targets may come from the branch predictor,
1974 * which could have a user-poisoned BTB or BHB entry.
1975 *
1976 * AMD has it even worse: *all* returns are speculated from the BTB,
1977 * regardless of the state of the RSB.
1978 *
1979 * When IBRS or eIBRS is enabled, the "user -> kernel" attack
1980 * scenario is mitigated by the IBRS branch prediction isolation
1981 * properties, so the RSB buffer filling wouldn't be necessary to
1982 * protect against this type of attack.
1983 *
1984 * The "user -> user" attack scenario is mitigated by RSB filling.
1985 *
1986 * 2) Poisoned RSB entry
1987 *
1988 * If the 'next' in-kernel return stack is shorter than 'prev',
1989 * 'next' could be tricked into speculating with a user-poisoned RSB
1990 * entry.
1991 *
1992 * The "user -> kernel" attack scenario is mitigated by SMEP and
1993 * eIBRS.
1994 *
1995 * The "user -> user" scenario, also known as SpectreBHB, requires
1996 * RSB clearing.
1997 *
1998 * So to mitigate all cases, unconditionally fill RSB on context
1999 * switches.
2000 *
2001 * FIXME: Is this pointless for retbleed-affected AMD?
2002 */
2003 spectre_v2_select_rsb_mitigation(mode);
2004
2005 /*
2006 * Retpoline protects the kernel, but doesn't protect firmware. IBRS
2007 * and Enhanced IBRS protect firmware too, so enable IBRS around
2008 * firmware calls only when IBRS / Enhanced / Automatic IBRS aren't
2009 * otherwise enabled.
2010 *
2011 * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because
2012 * the user might select retpoline on the kernel command line and if
2013 * the CPU supports Enhanced IBRS, kernel might un-intentionally not
2014 * enable IBRS around firmware calls.
2015 */
2016 if (boot_cpu_has_bug(X86_BUG_RETBLEED) &&
2017 boot_cpu_has(X86_FEATURE_IBPB) &&
2018 (boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
2019 boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)) {
2020
2021 if (retbleed_cmd != RETBLEED_CMD_IBPB) {
2022 setup_force_cpu_cap(X86_FEATURE_USE_IBPB_FW);
2023 pr_info("Enabling Speculation Barrier for firmware calls\n");
2024 }
2025
2026 } else if (boot_cpu_has(X86_FEATURE_IBRS) && !spectre_v2_in_ibrs_mode(mode)) {
2027 setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
2028 pr_info("Enabling Restricted Speculation for firmware calls\n");
2029 }
2030
2031 /* Set up IBPB and STIBP depending on the general spectre V2 command */
2032 spectre_v2_cmd = cmd;
2033 }
2034
update_stibp_msr(void * __unused)2035 static void update_stibp_msr(void * __unused)
2036 {
2037 u64 val = spec_ctrl_current() | (x86_spec_ctrl_base & SPEC_CTRL_STIBP);
2038 update_spec_ctrl(val);
2039 }
2040
2041 /* Update x86_spec_ctrl_base in case SMT state changed. */
update_stibp_strict(void)2042 static void update_stibp_strict(void)
2043 {
2044 u64 mask = x86_spec_ctrl_base & ~SPEC_CTRL_STIBP;
2045
2046 if (sched_smt_active())
2047 mask |= SPEC_CTRL_STIBP;
2048
2049 if (mask == x86_spec_ctrl_base)
2050 return;
2051
2052 pr_info("Update user space SMT mitigation: STIBP %s\n",
2053 mask & SPEC_CTRL_STIBP ? "always-on" : "off");
2054 x86_spec_ctrl_base = mask;
2055 on_each_cpu(update_stibp_msr, NULL, 1);
2056 }
2057
2058 /* Update the static key controlling the evaluation of TIF_SPEC_IB */
update_indir_branch_cond(void)2059 static void update_indir_branch_cond(void)
2060 {
2061 if (sched_smt_active())
2062 static_branch_enable(&switch_to_cond_stibp);
2063 else
2064 static_branch_disable(&switch_to_cond_stibp);
2065 }
2066
2067 #undef pr_fmt
2068 #define pr_fmt(fmt) fmt
2069
2070 /* Update the static key controlling the MDS CPU buffer clear in idle */
update_mds_branch_idle(void)2071 static void update_mds_branch_idle(void)
2072 {
2073 /*
2074 * Enable the idle clearing if SMT is active on CPUs which are
2075 * affected only by MSBDS and not any other MDS variant.
2076 *
2077 * The other variants cannot be mitigated when SMT is enabled, so
2078 * clearing the buffers on idle just to prevent the Store Buffer
2079 * repartitioning leak would be a window dressing exercise.
2080 */
2081 if (!boot_cpu_has_bug(X86_BUG_MSBDS_ONLY))
2082 return;
2083
2084 if (sched_smt_active()) {
2085 static_branch_enable(&mds_idle_clear);
2086 } else if (mmio_mitigation == MMIO_MITIGATION_OFF ||
2087 (x86_arch_cap_msr & ARCH_CAP_FBSDP_NO)) {
2088 static_branch_disable(&mds_idle_clear);
2089 }
2090 }
2091
2092 #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"
2093 #define TAA_MSG_SMT "TAA CPU bug present and SMT on, data leak possible. See https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/tsx_async_abort.html for more details.\n"
2094 #define MMIO_MSG_SMT "MMIO Stale Data CPU bug present and SMT on, data leak possible. See https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/processor_mmio_stale_data.html for more details.\n"
2095
cpu_bugs_smt_update(void)2096 void cpu_bugs_smt_update(void)
2097 {
2098 mutex_lock(&spec_ctrl_mutex);
2099
2100 if (sched_smt_active() && unprivileged_ebpf_enabled() &&
2101 spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
2102 pr_warn_once(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG);
2103
2104 switch (spectre_v2_user_stibp) {
2105 case SPECTRE_V2_USER_NONE:
2106 break;
2107 case SPECTRE_V2_USER_STRICT:
2108 case SPECTRE_V2_USER_STRICT_PREFERRED:
2109 update_stibp_strict();
2110 break;
2111 case SPECTRE_V2_USER_PRCTL:
2112 case SPECTRE_V2_USER_SECCOMP:
2113 update_indir_branch_cond();
2114 break;
2115 }
2116
2117 switch (mds_mitigation) {
2118 case MDS_MITIGATION_FULL:
2119 case MDS_MITIGATION_VMWERV:
2120 if (sched_smt_active() && !boot_cpu_has(X86_BUG_MSBDS_ONLY))
2121 pr_warn_once(MDS_MSG_SMT);
2122 update_mds_branch_idle();
2123 break;
2124 case MDS_MITIGATION_OFF:
2125 break;
2126 }
2127
2128 switch (taa_mitigation) {
2129 case TAA_MITIGATION_VERW:
2130 case TAA_MITIGATION_UCODE_NEEDED:
2131 if (sched_smt_active())
2132 pr_warn_once(TAA_MSG_SMT);
2133 break;
2134 case TAA_MITIGATION_TSX_DISABLED:
2135 case TAA_MITIGATION_OFF:
2136 break;
2137 }
2138
2139 switch (mmio_mitigation) {
2140 case MMIO_MITIGATION_VERW:
2141 case MMIO_MITIGATION_UCODE_NEEDED:
2142 if (sched_smt_active())
2143 pr_warn_once(MMIO_MSG_SMT);
2144 break;
2145 case MMIO_MITIGATION_OFF:
2146 break;
2147 }
2148
2149 mutex_unlock(&spec_ctrl_mutex);
2150 }
2151
2152 #undef pr_fmt
2153 #define pr_fmt(fmt) "Speculative Store Bypass: " fmt
2154
2155 static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
2156
2157 /* The kernel command line selection */
2158 enum ssb_mitigation_cmd {
2159 SPEC_STORE_BYPASS_CMD_NONE,
2160 SPEC_STORE_BYPASS_CMD_AUTO,
2161 SPEC_STORE_BYPASS_CMD_ON,
2162 SPEC_STORE_BYPASS_CMD_PRCTL,
2163 SPEC_STORE_BYPASS_CMD_SECCOMP,
2164 };
2165
2166 static const char * const ssb_strings[] = {
2167 [SPEC_STORE_BYPASS_NONE] = "Vulnerable",
2168 [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass disabled",
2169 [SPEC_STORE_BYPASS_PRCTL] = "Mitigation: Speculative Store Bypass disabled via prctl",
2170 [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
2171 };
2172
2173 static const struct {
2174 const char *option;
2175 enum ssb_mitigation_cmd cmd;
2176 } ssb_mitigation_options[] __initconst = {
2177 { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */
2178 { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */
2179 { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */
2180 { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative Store Bypass via prctl */
2181 { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */
2182 };
2183
ssb_parse_cmdline(void)2184 static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
2185 {
2186 enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO;
2187 char arg[20];
2188 int ret, i;
2189
2190 if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable") ||
2191 cpu_mitigations_off()) {
2192 return SPEC_STORE_BYPASS_CMD_NONE;
2193 } else {
2194 ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable",
2195 arg, sizeof(arg));
2196 if (ret < 0)
2197 return SPEC_STORE_BYPASS_CMD_AUTO;
2198
2199 for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) {
2200 if (!match_option(arg, ret, ssb_mitigation_options[i].option))
2201 continue;
2202
2203 cmd = ssb_mitigation_options[i].cmd;
2204 break;
2205 }
2206
2207 if (i >= ARRAY_SIZE(ssb_mitigation_options)) {
2208 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
2209 return SPEC_STORE_BYPASS_CMD_AUTO;
2210 }
2211 }
2212
2213 return cmd;
2214 }
2215
__ssb_select_mitigation(void)2216 static enum ssb_mitigation __init __ssb_select_mitigation(void)
2217 {
2218 enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
2219 enum ssb_mitigation_cmd cmd;
2220
2221 if (!boot_cpu_has(X86_FEATURE_SSBD))
2222 return mode;
2223
2224 cmd = ssb_parse_cmdline();
2225 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) &&
2226 (cmd == SPEC_STORE_BYPASS_CMD_NONE ||
2227 cmd == SPEC_STORE_BYPASS_CMD_AUTO))
2228 return mode;
2229
2230 switch (cmd) {
2231 case SPEC_STORE_BYPASS_CMD_SECCOMP:
2232 /*
2233 * Choose prctl+seccomp as the default mode if seccomp is
2234 * enabled.
2235 */
2236 if (IS_ENABLED(CONFIG_SECCOMP))
2237 mode = SPEC_STORE_BYPASS_SECCOMP;
2238 else
2239 mode = SPEC_STORE_BYPASS_PRCTL;
2240 break;
2241 case SPEC_STORE_BYPASS_CMD_ON:
2242 mode = SPEC_STORE_BYPASS_DISABLE;
2243 break;
2244 case SPEC_STORE_BYPASS_CMD_AUTO:
2245 case SPEC_STORE_BYPASS_CMD_PRCTL:
2246 mode = SPEC_STORE_BYPASS_PRCTL;
2247 break;
2248 case SPEC_STORE_BYPASS_CMD_NONE:
2249 break;
2250 }
2251
2252 /*
2253 * We have three CPU feature flags that are in play here:
2254 * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
2255 * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
2256 * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
2257 */
2258 if (mode == SPEC_STORE_BYPASS_DISABLE) {
2259 setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
2260 /*
2261 * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
2262 * use a completely different MSR and bit dependent on family.
2263 */
2264 if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
2265 !static_cpu_has(X86_FEATURE_AMD_SSBD)) {
2266 x86_amd_ssb_disable();
2267 } else {
2268 x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
2269 update_spec_ctrl(x86_spec_ctrl_base);
2270 }
2271 }
2272
2273 return mode;
2274 }
2275
ssb_select_mitigation(void)2276 static void ssb_select_mitigation(void)
2277 {
2278 ssb_mode = __ssb_select_mitigation();
2279
2280 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
2281 pr_info("%s\n", ssb_strings[ssb_mode]);
2282 }
2283
2284 #undef pr_fmt
2285 #define pr_fmt(fmt) "Speculation prctl: " fmt
2286
task_update_spec_tif(struct task_struct * tsk)2287 static void task_update_spec_tif(struct task_struct *tsk)
2288 {
2289 /* Force the update of the real TIF bits */
2290 set_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE);
2291
2292 /*
2293 * Immediately update the speculation control MSRs for the current
2294 * task, but for a non-current task delay setting the CPU
2295 * mitigation until it is scheduled next.
2296 *
2297 * This can only happen for SECCOMP mitigation. For PRCTL it's
2298 * always the current task.
2299 */
2300 if (tsk == current)
2301 speculation_ctrl_update_current();
2302 }
2303
l1d_flush_prctl_set(struct task_struct * task,unsigned long ctrl)2304 static int l1d_flush_prctl_set(struct task_struct *task, unsigned long ctrl)
2305 {
2306
2307 if (!static_branch_unlikely(&switch_mm_cond_l1d_flush))
2308 return -EPERM;
2309
2310 switch (ctrl) {
2311 case PR_SPEC_ENABLE:
2312 set_ti_thread_flag(&task->thread_info, TIF_SPEC_L1D_FLUSH);
2313 return 0;
2314 case PR_SPEC_DISABLE:
2315 clear_ti_thread_flag(&task->thread_info, TIF_SPEC_L1D_FLUSH);
2316 return 0;
2317 default:
2318 return -ERANGE;
2319 }
2320 }
2321
ssb_prctl_set(struct task_struct * task,unsigned long ctrl)2322 static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
2323 {
2324 if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
2325 ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
2326 return -ENXIO;
2327
2328 switch (ctrl) {
2329 case PR_SPEC_ENABLE:
2330 /* If speculation is force disabled, enable is not allowed */
2331 if (task_spec_ssb_force_disable(task))
2332 return -EPERM;
2333 task_clear_spec_ssb_disable(task);
2334 task_clear_spec_ssb_noexec(task);
2335 task_update_spec_tif(task);
2336 break;
2337 case PR_SPEC_DISABLE:
2338 task_set_spec_ssb_disable(task);
2339 task_clear_spec_ssb_noexec(task);
2340 task_update_spec_tif(task);
2341 break;
2342 case PR_SPEC_FORCE_DISABLE:
2343 task_set_spec_ssb_disable(task);
2344 task_set_spec_ssb_force_disable(task);
2345 task_clear_spec_ssb_noexec(task);
2346 task_update_spec_tif(task);
2347 break;
2348 case PR_SPEC_DISABLE_NOEXEC:
2349 if (task_spec_ssb_force_disable(task))
2350 return -EPERM;
2351 task_set_spec_ssb_disable(task);
2352 task_set_spec_ssb_noexec(task);
2353 task_update_spec_tif(task);
2354 break;
2355 default:
2356 return -ERANGE;
2357 }
2358 return 0;
2359 }
2360
is_spec_ib_user_controlled(void)2361 static bool is_spec_ib_user_controlled(void)
2362 {
2363 return spectre_v2_user_ibpb == SPECTRE_V2_USER_PRCTL ||
2364 spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
2365 spectre_v2_user_stibp == SPECTRE_V2_USER_PRCTL ||
2366 spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP;
2367 }
2368
ib_prctl_set(struct task_struct * task,unsigned long ctrl)2369 static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)
2370 {
2371 switch (ctrl) {
2372 case PR_SPEC_ENABLE:
2373 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
2374 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
2375 return 0;
2376
2377 /*
2378 * With strict mode for both IBPB and STIBP, the instruction
2379 * code paths avoid checking this task flag and instead,
2380 * unconditionally run the instruction. However, STIBP and IBPB
2381 * are independent and either can be set to conditionally
2382 * enabled regardless of the mode of the other.
2383 *
2384 * If either is set to conditional, allow the task flag to be
2385 * updated, unless it was force-disabled by a previous prctl
2386 * call. Currently, this is possible on an AMD CPU which has the
2387 * feature X86_FEATURE_AMD_STIBP_ALWAYS_ON. In this case, if the
2388 * kernel is booted with 'spectre_v2_user=seccomp', then
2389 * spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP and
2390 * spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED.
2391 */
2392 if (!is_spec_ib_user_controlled() ||
2393 task_spec_ib_force_disable(task))
2394 return -EPERM;
2395
2396 task_clear_spec_ib_disable(task);
2397 task_update_spec_tif(task);
2398 break;
2399 case PR_SPEC_DISABLE:
2400 case PR_SPEC_FORCE_DISABLE:
2401 /*
2402 * Indirect branch speculation is always allowed when
2403 * mitigation is force disabled.
2404 */
2405 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
2406 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
2407 return -EPERM;
2408
2409 if (!is_spec_ib_user_controlled())
2410 return 0;
2411
2412 task_set_spec_ib_disable(task);
2413 if (ctrl == PR_SPEC_FORCE_DISABLE)
2414 task_set_spec_ib_force_disable(task);
2415 task_update_spec_tif(task);
2416 if (task == current)
2417 indirect_branch_prediction_barrier();
2418 break;
2419 default:
2420 return -ERANGE;
2421 }
2422 return 0;
2423 }
2424
arch_prctl_spec_ctrl_set(struct task_struct * task,unsigned long which,unsigned long ctrl)2425 int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
2426 unsigned long ctrl)
2427 {
2428 switch (which) {
2429 case PR_SPEC_STORE_BYPASS:
2430 return ssb_prctl_set(task, ctrl);
2431 case PR_SPEC_INDIRECT_BRANCH:
2432 return ib_prctl_set(task, ctrl);
2433 case PR_SPEC_L1D_FLUSH:
2434 return l1d_flush_prctl_set(task, ctrl);
2435 default:
2436 return -ENODEV;
2437 }
2438 }
2439
2440 #ifdef CONFIG_SECCOMP
arch_seccomp_spec_mitigate(struct task_struct * task)2441 void arch_seccomp_spec_mitigate(struct task_struct *task)
2442 {
2443 if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP)
2444 ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
2445 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
2446 spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP)
2447 ib_prctl_set(task, PR_SPEC_FORCE_DISABLE);
2448 }
2449 #endif
2450
l1d_flush_prctl_get(struct task_struct * task)2451 static int l1d_flush_prctl_get(struct task_struct *task)
2452 {
2453 if (!static_branch_unlikely(&switch_mm_cond_l1d_flush))
2454 return PR_SPEC_FORCE_DISABLE;
2455
2456 if (test_ti_thread_flag(&task->thread_info, TIF_SPEC_L1D_FLUSH))
2457 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
2458 else
2459 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
2460 }
2461
ssb_prctl_get(struct task_struct * task)2462 static int ssb_prctl_get(struct task_struct *task)
2463 {
2464 switch (ssb_mode) {
2465 case SPEC_STORE_BYPASS_DISABLE:
2466 return PR_SPEC_DISABLE;
2467 case SPEC_STORE_BYPASS_SECCOMP:
2468 case SPEC_STORE_BYPASS_PRCTL:
2469 if (task_spec_ssb_force_disable(task))
2470 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
2471 if (task_spec_ssb_noexec(task))
2472 return PR_SPEC_PRCTL | PR_SPEC_DISABLE_NOEXEC;
2473 if (task_spec_ssb_disable(task))
2474 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
2475 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
2476 default:
2477 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
2478 return PR_SPEC_ENABLE;
2479 return PR_SPEC_NOT_AFFECTED;
2480 }
2481 }
2482
ib_prctl_get(struct task_struct * task)2483 static int ib_prctl_get(struct task_struct *task)
2484 {
2485 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
2486 return PR_SPEC_NOT_AFFECTED;
2487
2488 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
2489 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
2490 return PR_SPEC_ENABLE;
2491 else if (is_spec_ib_user_controlled()) {
2492 if (task_spec_ib_force_disable(task))
2493 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
2494 if (task_spec_ib_disable(task))
2495 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
2496 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
2497 } else if (spectre_v2_user_ibpb == SPECTRE_V2_USER_STRICT ||
2498 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
2499 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED)
2500 return PR_SPEC_DISABLE;
2501 else
2502 return PR_SPEC_NOT_AFFECTED;
2503 }
2504
arch_prctl_spec_ctrl_get(struct task_struct * task,unsigned long which)2505 int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
2506 {
2507 switch (which) {
2508 case PR_SPEC_STORE_BYPASS:
2509 return ssb_prctl_get(task);
2510 case PR_SPEC_INDIRECT_BRANCH:
2511 return ib_prctl_get(task);
2512 case PR_SPEC_L1D_FLUSH:
2513 return l1d_flush_prctl_get(task);
2514 default:
2515 return -ENODEV;
2516 }
2517 }
2518
x86_spec_ctrl_setup_ap(void)2519 void x86_spec_ctrl_setup_ap(void)
2520 {
2521 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
2522 update_spec_ctrl(x86_spec_ctrl_base);
2523
2524 if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
2525 x86_amd_ssb_disable();
2526 }
2527
2528 bool itlb_multihit_kvm_mitigation;
2529 EXPORT_SYMBOL_GPL(itlb_multihit_kvm_mitigation);
2530
2531 #undef pr_fmt
2532 #define pr_fmt(fmt) "L1TF: " fmt
2533
2534 /* Default mitigation for L1TF-affected CPUs */
2535 enum l1tf_mitigations l1tf_mitigation __ro_after_init = L1TF_MITIGATION_FLUSH;
2536 #if IS_ENABLED(CONFIG_KVM_INTEL)
2537 EXPORT_SYMBOL_GPL(l1tf_mitigation);
2538 #endif
2539 enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
2540 EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation);
2541
2542 /*
2543 * These CPUs all support 44bits physical address space internally in the
2544 * cache but CPUID can report a smaller number of physical address bits.
2545 *
2546 * The L1TF mitigation uses the top most address bit for the inversion of
2547 * non present PTEs. When the installed memory reaches into the top most
2548 * address bit due to memory holes, which has been observed on machines
2549 * which report 36bits physical address bits and have 32G RAM installed,
2550 * then the mitigation range check in l1tf_select_mitigation() triggers.
2551 * This is a false positive because the mitigation is still possible due to
2552 * the fact that the cache uses 44bit internally. Use the cache bits
2553 * instead of the reported physical bits and adjust them on the affected
2554 * machines to 44bit if the reported bits are less than 44.
2555 */
override_cache_bits(struct cpuinfo_x86 * c)2556 static void override_cache_bits(struct cpuinfo_x86 *c)
2557 {
2558 if (c->x86 != 6)
2559 return;
2560
2561 switch (c->x86_model) {
2562 case INTEL_FAM6_NEHALEM:
2563 case INTEL_FAM6_WESTMERE:
2564 case INTEL_FAM6_SANDYBRIDGE:
2565 case INTEL_FAM6_IVYBRIDGE:
2566 case INTEL_FAM6_HASWELL:
2567 case INTEL_FAM6_HASWELL_L:
2568 case INTEL_FAM6_HASWELL_G:
2569 case INTEL_FAM6_BROADWELL:
2570 case INTEL_FAM6_BROADWELL_G:
2571 case INTEL_FAM6_SKYLAKE_L:
2572 case INTEL_FAM6_SKYLAKE:
2573 case INTEL_FAM6_KABYLAKE_L:
2574 case INTEL_FAM6_KABYLAKE:
2575 if (c->x86_cache_bits < 44)
2576 c->x86_cache_bits = 44;
2577 break;
2578 }
2579 }
2580
l1tf_select_mitigation(void)2581 static void __init l1tf_select_mitigation(void)
2582 {
2583 u64 half_pa;
2584
2585 if (!boot_cpu_has_bug(X86_BUG_L1TF))
2586 return;
2587
2588 if (cpu_mitigations_off())
2589 l1tf_mitigation = L1TF_MITIGATION_OFF;
2590 else if (cpu_mitigations_auto_nosmt())
2591 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
2592
2593 override_cache_bits(&boot_cpu_data);
2594
2595 switch (l1tf_mitigation) {
2596 case L1TF_MITIGATION_OFF:
2597 case L1TF_MITIGATION_FLUSH_NOWARN:
2598 case L1TF_MITIGATION_FLUSH:
2599 break;
2600 case L1TF_MITIGATION_FLUSH_NOSMT:
2601 case L1TF_MITIGATION_FULL:
2602 cpu_smt_disable(false);
2603 break;
2604 case L1TF_MITIGATION_FULL_FORCE:
2605 cpu_smt_disable(true);
2606 break;
2607 }
2608
2609 #if CONFIG_PGTABLE_LEVELS == 2
2610 pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
2611 return;
2612 #endif
2613
2614 half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT;
2615 if (l1tf_mitigation != L1TF_MITIGATION_OFF &&
2616 e820__mapped_any(half_pa, ULLONG_MAX - half_pa, E820_TYPE_RAM)) {
2617 pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
2618 pr_info("You may make it effective by booting the kernel with mem=%llu parameter.\n",
2619 half_pa);
2620 pr_info("However, doing so will make a part of your RAM unusable.\n");
2621 pr_info("Reading https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html might help you decide.\n");
2622 return;
2623 }
2624
2625 setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV);
2626 }
2627
l1tf_cmdline(char * str)2628 static int __init l1tf_cmdline(char *str)
2629 {
2630 if (!boot_cpu_has_bug(X86_BUG_L1TF))
2631 return 0;
2632
2633 if (!str)
2634 return -EINVAL;
2635
2636 if (!strcmp(str, "off"))
2637 l1tf_mitigation = L1TF_MITIGATION_OFF;
2638 else if (!strcmp(str, "flush,nowarn"))
2639 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOWARN;
2640 else if (!strcmp(str, "flush"))
2641 l1tf_mitigation = L1TF_MITIGATION_FLUSH;
2642 else if (!strcmp(str, "flush,nosmt"))
2643 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
2644 else if (!strcmp(str, "full"))
2645 l1tf_mitigation = L1TF_MITIGATION_FULL;
2646 else if (!strcmp(str, "full,force"))
2647 l1tf_mitigation = L1TF_MITIGATION_FULL_FORCE;
2648
2649 return 0;
2650 }
2651 early_param("l1tf", l1tf_cmdline);
2652
2653 #undef pr_fmt
2654 #define pr_fmt(fmt) "Speculative Return Stack Overflow: " fmt
2655
2656 enum srso_mitigation {
2657 SRSO_MITIGATION_NONE,
2658 SRSO_MITIGATION_UCODE_NEEDED,
2659 SRSO_MITIGATION_SAFE_RET_UCODE_NEEDED,
2660 SRSO_MITIGATION_MICROCODE,
2661 SRSO_MITIGATION_SAFE_RET,
2662 SRSO_MITIGATION_IBPB,
2663 SRSO_MITIGATION_IBPB_ON_VMEXIT,
2664 };
2665
2666 enum srso_mitigation_cmd {
2667 SRSO_CMD_OFF,
2668 SRSO_CMD_MICROCODE,
2669 SRSO_CMD_SAFE_RET,
2670 SRSO_CMD_IBPB,
2671 SRSO_CMD_IBPB_ON_VMEXIT,
2672 };
2673
2674 static const char * const srso_strings[] = {
2675 [SRSO_MITIGATION_NONE] = "Vulnerable",
2676 [SRSO_MITIGATION_UCODE_NEEDED] = "Vulnerable: No microcode",
2677 [SRSO_MITIGATION_SAFE_RET_UCODE_NEEDED] = "Vulnerable: Safe RET, no microcode",
2678 [SRSO_MITIGATION_MICROCODE] = "Vulnerable: Microcode, no safe RET",
2679 [SRSO_MITIGATION_SAFE_RET] = "Mitigation: Safe RET",
2680 [SRSO_MITIGATION_IBPB] = "Mitigation: IBPB",
2681 [SRSO_MITIGATION_IBPB_ON_VMEXIT] = "Mitigation: IBPB on VMEXIT only"
2682 };
2683
2684 static enum srso_mitigation srso_mitigation __ro_after_init = SRSO_MITIGATION_NONE;
2685 static enum srso_mitigation_cmd srso_cmd __ro_after_init = SRSO_CMD_SAFE_RET;
2686
srso_parse_cmdline(char * str)2687 static int __init srso_parse_cmdline(char *str)
2688 {
2689 if (!str)
2690 return -EINVAL;
2691
2692 if (!strcmp(str, "off"))
2693 srso_cmd = SRSO_CMD_OFF;
2694 else if (!strcmp(str, "microcode"))
2695 srso_cmd = SRSO_CMD_MICROCODE;
2696 else if (!strcmp(str, "safe-ret"))
2697 srso_cmd = SRSO_CMD_SAFE_RET;
2698 else if (!strcmp(str, "ibpb"))
2699 srso_cmd = SRSO_CMD_IBPB;
2700 else if (!strcmp(str, "ibpb-vmexit"))
2701 srso_cmd = SRSO_CMD_IBPB_ON_VMEXIT;
2702 else
2703 pr_err("Ignoring unknown SRSO option (%s).", str);
2704
2705 return 0;
2706 }
2707 early_param("spec_rstack_overflow", srso_parse_cmdline);
2708
2709 #define SRSO_NOTICE "WARNING: See https://kernel.org/doc/html/latest/admin-guide/hw-vuln/srso.html for mitigation options."
2710
srso_select_mitigation(void)2711 static void __init srso_select_mitigation(void)
2712 {
2713 bool has_microcode = boot_cpu_has(X86_FEATURE_IBPB_BRTYPE);
2714
2715 if (!boot_cpu_has_bug(X86_BUG_SRSO) || cpu_mitigations_off())
2716 goto pred_cmd;
2717
2718 if (has_microcode) {
2719 /*
2720 * Zen1/2 with SMT off aren't vulnerable after the right
2721 * IBPB microcode has been applied.
2722 */
2723 if (boot_cpu_data.x86 < 0x19 && !cpu_smt_possible()) {
2724 setup_force_cpu_cap(X86_FEATURE_SRSO_NO);
2725 return;
2726 }
2727
2728 if (retbleed_mitigation == RETBLEED_MITIGATION_IBPB) {
2729 srso_mitigation = SRSO_MITIGATION_IBPB;
2730 goto out;
2731 }
2732 } else {
2733 pr_warn("IBPB-extending microcode not applied!\n");
2734 pr_warn(SRSO_NOTICE);
2735
2736 /* may be overwritten by SRSO_CMD_SAFE_RET below */
2737 srso_mitigation = SRSO_MITIGATION_UCODE_NEEDED;
2738 }
2739
2740 switch (srso_cmd) {
2741 case SRSO_CMD_OFF:
2742 goto pred_cmd;
2743
2744 case SRSO_CMD_MICROCODE:
2745 if (has_microcode) {
2746 srso_mitigation = SRSO_MITIGATION_MICROCODE;
2747 pr_warn(SRSO_NOTICE);
2748 }
2749 break;
2750
2751 case SRSO_CMD_SAFE_RET:
2752 if (IS_ENABLED(CONFIG_CPU_SRSO)) {
2753 /*
2754 * Enable the return thunk for generated code
2755 * like ftrace, static_call, etc.
2756 */
2757 setup_force_cpu_cap(X86_FEATURE_RETHUNK);
2758 setup_force_cpu_cap(X86_FEATURE_UNRET);
2759
2760 if (boot_cpu_data.x86 == 0x19) {
2761 setup_force_cpu_cap(X86_FEATURE_SRSO_ALIAS);
2762 set_return_thunk(srso_alias_return_thunk);
2763 } else {
2764 setup_force_cpu_cap(X86_FEATURE_SRSO);
2765 set_return_thunk(srso_return_thunk);
2766 }
2767 if (has_microcode)
2768 srso_mitigation = SRSO_MITIGATION_SAFE_RET;
2769 else
2770 srso_mitigation = SRSO_MITIGATION_SAFE_RET_UCODE_NEEDED;
2771 } else {
2772 pr_err("WARNING: kernel not compiled with CPU_SRSO.\n");
2773 goto pred_cmd;
2774 }
2775 break;
2776
2777 case SRSO_CMD_IBPB:
2778 if (IS_ENABLED(CONFIG_CPU_IBPB_ENTRY)) {
2779 if (has_microcode) {
2780 setup_force_cpu_cap(X86_FEATURE_ENTRY_IBPB);
2781 setup_force_cpu_cap(X86_FEATURE_IBPB_ON_VMEXIT);
2782 srso_mitigation = SRSO_MITIGATION_IBPB;
2783
2784 /*
2785 * IBPB on entry already obviates the need for
2786 * software-based untraining so clear those in case some
2787 * other mitigation like Retbleed has selected them.
2788 */
2789 setup_clear_cpu_cap(X86_FEATURE_UNRET);
2790 setup_clear_cpu_cap(X86_FEATURE_RETHUNK);
2791
2792 /*
2793 * There is no need for RSB filling: entry_ibpb() ensures
2794 * all predictions, including the RSB, are invalidated,
2795 * regardless of IBPB implementation.
2796 */
2797 setup_clear_cpu_cap(X86_FEATURE_RSB_VMEXIT);
2798 }
2799 } else {
2800 pr_err("WARNING: kernel not compiled with CPU_IBPB_ENTRY.\n");
2801 goto pred_cmd;
2802 }
2803 break;
2804
2805 case SRSO_CMD_IBPB_ON_VMEXIT:
2806 if (IS_ENABLED(CONFIG_CPU_IBPB_ENTRY)) {
2807 if (has_microcode) {
2808 setup_force_cpu_cap(X86_FEATURE_IBPB_ON_VMEXIT);
2809 srso_mitigation = SRSO_MITIGATION_IBPB_ON_VMEXIT;
2810
2811 /*
2812 * There is no need for RSB filling: entry_ibpb() ensures
2813 * all predictions, including the RSB, are invalidated,
2814 * regardless of IBPB implementation.
2815 */
2816 setup_clear_cpu_cap(X86_FEATURE_RSB_VMEXIT);
2817 }
2818 } else {
2819 pr_err("WARNING: kernel not compiled with CPU_IBPB_ENTRY.\n");
2820 goto pred_cmd;
2821 }
2822 break;
2823
2824 default:
2825 break;
2826 }
2827
2828 out:
2829 pr_info("%s\n", srso_strings[srso_mitigation]);
2830
2831 pred_cmd:
2832 if ((!boot_cpu_has_bug(X86_BUG_SRSO) || srso_cmd == SRSO_CMD_OFF) &&
2833 boot_cpu_has(X86_FEATURE_SBPB))
2834 x86_pred_cmd = PRED_CMD_SBPB;
2835 }
2836
2837 #undef pr_fmt
2838 #define pr_fmt(fmt) fmt
2839
2840 #ifdef CONFIG_SYSFS
2841
2842 #define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
2843
2844 #if IS_ENABLED(CONFIG_KVM_INTEL)
2845 static const char * const l1tf_vmx_states[] = {
2846 [VMENTER_L1D_FLUSH_AUTO] = "auto",
2847 [VMENTER_L1D_FLUSH_NEVER] = "vulnerable",
2848 [VMENTER_L1D_FLUSH_COND] = "conditional cache flushes",
2849 [VMENTER_L1D_FLUSH_ALWAYS] = "cache flushes",
2850 [VMENTER_L1D_FLUSH_EPT_DISABLED] = "EPT disabled",
2851 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = "flush not necessary"
2852 };
2853
l1tf_show_state(char * buf)2854 static ssize_t l1tf_show_state(char *buf)
2855 {
2856 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO)
2857 return sysfs_emit(buf, "%s\n", L1TF_DEFAULT_MSG);
2858
2859 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_EPT_DISABLED ||
2860 (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER &&
2861 sched_smt_active())) {
2862 return sysfs_emit(buf, "%s; VMX: %s\n", L1TF_DEFAULT_MSG,
2863 l1tf_vmx_states[l1tf_vmx_mitigation]);
2864 }
2865
2866 return sysfs_emit(buf, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG,
2867 l1tf_vmx_states[l1tf_vmx_mitigation],
2868 sched_smt_active() ? "vulnerable" : "disabled");
2869 }
2870
itlb_multihit_show_state(char * buf)2871 static ssize_t itlb_multihit_show_state(char *buf)
2872 {
2873 if (!boot_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
2874 !boot_cpu_has(X86_FEATURE_VMX))
2875 return sysfs_emit(buf, "KVM: Mitigation: VMX unsupported\n");
2876 else if (!(cr4_read_shadow() & X86_CR4_VMXE))
2877 return sysfs_emit(buf, "KVM: Mitigation: VMX disabled\n");
2878 else if (itlb_multihit_kvm_mitigation)
2879 return sysfs_emit(buf, "KVM: Mitigation: Split huge pages\n");
2880 else
2881 return sysfs_emit(buf, "KVM: Vulnerable\n");
2882 }
2883 #else
l1tf_show_state(char * buf)2884 static ssize_t l1tf_show_state(char *buf)
2885 {
2886 return sysfs_emit(buf, "%s\n", L1TF_DEFAULT_MSG);
2887 }
2888
itlb_multihit_show_state(char * buf)2889 static ssize_t itlb_multihit_show_state(char *buf)
2890 {
2891 return sysfs_emit(buf, "Processor vulnerable\n");
2892 }
2893 #endif
2894
mds_show_state(char * buf)2895 static ssize_t mds_show_state(char *buf)
2896 {
2897 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
2898 return sysfs_emit(buf, "%s; SMT Host state unknown\n",
2899 mds_strings[mds_mitigation]);
2900 }
2901
2902 if (boot_cpu_has(X86_BUG_MSBDS_ONLY)) {
2903 return sysfs_emit(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
2904 (mds_mitigation == MDS_MITIGATION_OFF ? "vulnerable" :
2905 sched_smt_active() ? "mitigated" : "disabled"));
2906 }
2907
2908 return sysfs_emit(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
2909 sched_smt_active() ? "vulnerable" : "disabled");
2910 }
2911
tsx_async_abort_show_state(char * buf)2912 static ssize_t tsx_async_abort_show_state(char *buf)
2913 {
2914 if ((taa_mitigation == TAA_MITIGATION_TSX_DISABLED) ||
2915 (taa_mitigation == TAA_MITIGATION_OFF))
2916 return sysfs_emit(buf, "%s\n", taa_strings[taa_mitigation]);
2917
2918 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
2919 return sysfs_emit(buf, "%s; SMT Host state unknown\n",
2920 taa_strings[taa_mitigation]);
2921 }
2922
2923 return sysfs_emit(buf, "%s; SMT %s\n", taa_strings[taa_mitigation],
2924 sched_smt_active() ? "vulnerable" : "disabled");
2925 }
2926
mmio_stale_data_show_state(char * buf)2927 static ssize_t mmio_stale_data_show_state(char *buf)
2928 {
2929 if (boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN))
2930 return sysfs_emit(buf, "Unknown: No mitigations\n");
2931
2932 if (mmio_mitigation == MMIO_MITIGATION_OFF)
2933 return sysfs_emit(buf, "%s\n", mmio_strings[mmio_mitigation]);
2934
2935 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
2936 return sysfs_emit(buf, "%s; SMT Host state unknown\n",
2937 mmio_strings[mmio_mitigation]);
2938 }
2939
2940 return sysfs_emit(buf, "%s; SMT %s\n", mmio_strings[mmio_mitigation],
2941 sched_smt_active() ? "vulnerable" : "disabled");
2942 }
2943
rfds_show_state(char * buf)2944 static ssize_t rfds_show_state(char *buf)
2945 {
2946 return sysfs_emit(buf, "%s\n", rfds_strings[rfds_mitigation]);
2947 }
2948
its_show_state(char * buf)2949 static ssize_t its_show_state(char *buf)
2950 {
2951 return sysfs_emit(buf, "%s\n", its_strings[its_mitigation]);
2952 }
2953
stibp_state(void)2954 static char *stibp_state(void)
2955 {
2956 if (spectre_v2_in_eibrs_mode(spectre_v2_enabled) &&
2957 !boot_cpu_has(X86_FEATURE_AUTOIBRS))
2958 return "";
2959
2960 switch (spectre_v2_user_stibp) {
2961 case SPECTRE_V2_USER_NONE:
2962 return "; STIBP: disabled";
2963 case SPECTRE_V2_USER_STRICT:
2964 return "; STIBP: forced";
2965 case SPECTRE_V2_USER_STRICT_PREFERRED:
2966 return "; STIBP: always-on";
2967 case SPECTRE_V2_USER_PRCTL:
2968 case SPECTRE_V2_USER_SECCOMP:
2969 if (static_key_enabled(&switch_to_cond_stibp))
2970 return "; STIBP: conditional";
2971 }
2972 return "";
2973 }
2974
ibpb_state(void)2975 static char *ibpb_state(void)
2976 {
2977 if (boot_cpu_has(X86_FEATURE_IBPB)) {
2978 if (static_key_enabled(&switch_mm_always_ibpb))
2979 return "; IBPB: always-on";
2980 if (static_key_enabled(&switch_mm_cond_ibpb))
2981 return "; IBPB: conditional";
2982 return "; IBPB: disabled";
2983 }
2984 return "";
2985 }
2986
pbrsb_eibrs_state(void)2987 static char *pbrsb_eibrs_state(void)
2988 {
2989 if (boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB)) {
2990 if (boot_cpu_has(X86_FEATURE_RSB_VMEXIT_LITE) ||
2991 boot_cpu_has(X86_FEATURE_RSB_VMEXIT))
2992 return "; PBRSB-eIBRS: SW sequence";
2993 else
2994 return "; PBRSB-eIBRS: Vulnerable";
2995 } else {
2996 return "; PBRSB-eIBRS: Not affected";
2997 }
2998 }
2999
spectre_bhi_state(void)3000 static const char *spectre_bhi_state(void)
3001 {
3002 if (!boot_cpu_has_bug(X86_BUG_BHI))
3003 return "; BHI: Not affected";
3004 else if (boot_cpu_has(X86_FEATURE_CLEAR_BHB_HW))
3005 return "; BHI: BHI_DIS_S";
3006 else if (boot_cpu_has(X86_FEATURE_CLEAR_BHB_LOOP))
3007 return "; BHI: SW loop, KVM: SW loop";
3008 else if (boot_cpu_has(X86_FEATURE_RETPOLINE) &&
3009 !boot_cpu_has(X86_FEATURE_RETPOLINE_LFENCE) &&
3010 rrsba_disabled)
3011 return "; BHI: Retpoline";
3012 else if (boot_cpu_has(X86_FEATURE_CLEAR_BHB_LOOP_ON_VMEXIT))
3013 return "; BHI: Vulnerable, KVM: SW loop";
3014
3015 return "; BHI: Vulnerable";
3016 }
3017
spectre_v2_show_state(char * buf)3018 static ssize_t spectre_v2_show_state(char *buf)
3019 {
3020 if (spectre_v2_enabled == SPECTRE_V2_LFENCE)
3021 return sysfs_emit(buf, "Vulnerable: LFENCE\n");
3022
3023 if (spectre_v2_enabled == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
3024 return sysfs_emit(buf, "Vulnerable: eIBRS with unprivileged eBPF\n");
3025
3026 if (sched_smt_active() && unprivileged_ebpf_enabled() &&
3027 spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
3028 return sysfs_emit(buf, "Vulnerable: eIBRS+LFENCE with unprivileged eBPF and SMT\n");
3029
3030 return sysfs_emit(buf, "%s%s%s%s%s%s%s%s\n",
3031 spectre_v2_strings[spectre_v2_enabled],
3032 ibpb_state(),
3033 boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? "; IBRS_FW" : "",
3034 stibp_state(),
3035 boot_cpu_has(X86_FEATURE_RSB_CTXSW) ? "; RSB filling" : "",
3036 pbrsb_eibrs_state(),
3037 spectre_bhi_state(),
3038 /* this should always be at the end */
3039 spectre_v2_module_string());
3040 }
3041
srbds_show_state(char * buf)3042 static ssize_t srbds_show_state(char *buf)
3043 {
3044 return sysfs_emit(buf, "%s\n", srbds_strings[srbds_mitigation]);
3045 }
3046
retbleed_show_state(char * buf)3047 static ssize_t retbleed_show_state(char *buf)
3048 {
3049 if (retbleed_mitigation == RETBLEED_MITIGATION_UNRET ||
3050 retbleed_mitigation == RETBLEED_MITIGATION_IBPB) {
3051 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
3052 boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)
3053 return sysfs_emit(buf, "Vulnerable: untrained return thunk / IBPB on non-AMD based uarch\n");
3054
3055 return sysfs_emit(buf, "%s; SMT %s\n", retbleed_strings[retbleed_mitigation],
3056 !sched_smt_active() ? "disabled" :
3057 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
3058 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED ?
3059 "enabled with STIBP protection" : "vulnerable");
3060 }
3061
3062 return sysfs_emit(buf, "%s\n", retbleed_strings[retbleed_mitigation]);
3063 }
3064
srso_show_state(char * buf)3065 static ssize_t srso_show_state(char *buf)
3066 {
3067 if (boot_cpu_has(X86_FEATURE_SRSO_NO))
3068 return sysfs_emit(buf, "Mitigation: SMT disabled\n");
3069
3070 return sysfs_emit(buf, "%s\n", srso_strings[srso_mitigation]);
3071 }
3072
gds_show_state(char * buf)3073 static ssize_t gds_show_state(char *buf)
3074 {
3075 return sysfs_emit(buf, "%s\n", gds_strings[gds_mitigation]);
3076 }
3077
cpu_show_common(struct device * dev,struct device_attribute * attr,char * buf,unsigned int bug)3078 static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
3079 char *buf, unsigned int bug)
3080 {
3081 if (!boot_cpu_has_bug(bug))
3082 return sysfs_emit(buf, "Not affected\n");
3083
3084 switch (bug) {
3085 case X86_BUG_CPU_MELTDOWN:
3086 if (boot_cpu_has(X86_FEATURE_PTI))
3087 return sysfs_emit(buf, "Mitigation: PTI\n");
3088
3089 if (hypervisor_is_type(X86_HYPER_XEN_PV))
3090 return sysfs_emit(buf, "Unknown (XEN PV detected, hypervisor mitigation required)\n");
3091
3092 break;
3093
3094 case X86_BUG_SPECTRE_V1:
3095 return sysfs_emit(buf, "%s\n", spectre_v1_strings[spectre_v1_mitigation]);
3096
3097 case X86_BUG_SPECTRE_V2:
3098 return spectre_v2_show_state(buf);
3099
3100 case X86_BUG_SPEC_STORE_BYPASS:
3101 return sysfs_emit(buf, "%s\n", ssb_strings[ssb_mode]);
3102
3103 case X86_BUG_L1TF:
3104 if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV))
3105 return l1tf_show_state(buf);
3106 break;
3107
3108 case X86_BUG_MDS:
3109 return mds_show_state(buf);
3110
3111 case X86_BUG_TAA:
3112 return tsx_async_abort_show_state(buf);
3113
3114 case X86_BUG_ITLB_MULTIHIT:
3115 return itlb_multihit_show_state(buf);
3116
3117 case X86_BUG_SRBDS:
3118 return srbds_show_state(buf);
3119
3120 case X86_BUG_MMIO_STALE_DATA:
3121 case X86_BUG_MMIO_UNKNOWN:
3122 return mmio_stale_data_show_state(buf);
3123
3124 case X86_BUG_RETBLEED:
3125 return retbleed_show_state(buf);
3126
3127 case X86_BUG_SRSO:
3128 return srso_show_state(buf);
3129
3130 case X86_BUG_GDS:
3131 return gds_show_state(buf);
3132
3133 case X86_BUG_RFDS:
3134 return rfds_show_state(buf);
3135
3136 case X86_BUG_ITS:
3137 return its_show_state(buf);
3138
3139 default:
3140 break;
3141 }
3142
3143 return sysfs_emit(buf, "Vulnerable\n");
3144 }
3145
cpu_show_meltdown(struct device * dev,struct device_attribute * attr,char * buf)3146 ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
3147 {
3148 return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
3149 }
3150
cpu_show_spectre_v1(struct device * dev,struct device_attribute * attr,char * buf)3151 ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
3152 {
3153 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
3154 }
3155
cpu_show_spectre_v2(struct device * dev,struct device_attribute * attr,char * buf)3156 ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
3157 {
3158 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
3159 }
3160
cpu_show_spec_store_bypass(struct device * dev,struct device_attribute * attr,char * buf)3161 ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
3162 {
3163 return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS);
3164 }
3165
cpu_show_l1tf(struct device * dev,struct device_attribute * attr,char * buf)3166 ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
3167 {
3168 return cpu_show_common(dev, attr, buf, X86_BUG_L1TF);
3169 }
3170
cpu_show_mds(struct device * dev,struct device_attribute * attr,char * buf)3171 ssize_t cpu_show_mds(struct device *dev, struct device_attribute *attr, char *buf)
3172 {
3173 return cpu_show_common(dev, attr, buf, X86_BUG_MDS);
3174 }
3175
cpu_show_tsx_async_abort(struct device * dev,struct device_attribute * attr,char * buf)3176 ssize_t cpu_show_tsx_async_abort(struct device *dev, struct device_attribute *attr, char *buf)
3177 {
3178 return cpu_show_common(dev, attr, buf, X86_BUG_TAA);
3179 }
3180
cpu_show_itlb_multihit(struct device * dev,struct device_attribute * attr,char * buf)3181 ssize_t cpu_show_itlb_multihit(struct device *dev, struct device_attribute *attr, char *buf)
3182 {
3183 return cpu_show_common(dev, attr, buf, X86_BUG_ITLB_MULTIHIT);
3184 }
3185
cpu_show_srbds(struct device * dev,struct device_attribute * attr,char * buf)3186 ssize_t cpu_show_srbds(struct device *dev, struct device_attribute *attr, char *buf)
3187 {
3188 return cpu_show_common(dev, attr, buf, X86_BUG_SRBDS);
3189 }
3190
cpu_show_mmio_stale_data(struct device * dev,struct device_attribute * attr,char * buf)3191 ssize_t cpu_show_mmio_stale_data(struct device *dev, struct device_attribute *attr, char *buf)
3192 {
3193 if (boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN))
3194 return cpu_show_common(dev, attr, buf, X86_BUG_MMIO_UNKNOWN);
3195 else
3196 return cpu_show_common(dev, attr, buf, X86_BUG_MMIO_STALE_DATA);
3197 }
3198
cpu_show_retbleed(struct device * dev,struct device_attribute * attr,char * buf)3199 ssize_t cpu_show_retbleed(struct device *dev, struct device_attribute *attr, char *buf)
3200 {
3201 return cpu_show_common(dev, attr, buf, X86_BUG_RETBLEED);
3202 }
3203
cpu_show_spec_rstack_overflow(struct device * dev,struct device_attribute * attr,char * buf)3204 ssize_t cpu_show_spec_rstack_overflow(struct device *dev, struct device_attribute *attr, char *buf)
3205 {
3206 return cpu_show_common(dev, attr, buf, X86_BUG_SRSO);
3207 }
3208
cpu_show_gds(struct device * dev,struct device_attribute * attr,char * buf)3209 ssize_t cpu_show_gds(struct device *dev, struct device_attribute *attr, char *buf)
3210 {
3211 return cpu_show_common(dev, attr, buf, X86_BUG_GDS);
3212 }
3213
cpu_show_reg_file_data_sampling(struct device * dev,struct device_attribute * attr,char * buf)3214 ssize_t cpu_show_reg_file_data_sampling(struct device *dev, struct device_attribute *attr, char *buf)
3215 {
3216 return cpu_show_common(dev, attr, buf, X86_BUG_RFDS);
3217 }
3218
cpu_show_indirect_target_selection(struct device * dev,struct device_attribute * attr,char * buf)3219 ssize_t cpu_show_indirect_target_selection(struct device *dev, struct device_attribute *attr, char *buf)
3220 {
3221 return cpu_show_common(dev, attr, buf, X86_BUG_ITS);
3222 }
3223 #endif
3224