xref: /openbmc/linux/arch/x86/kernel/cpu/bugs.c (revision 4d75f5c664195b970e1cd2fd25b65b5eff257a0a)
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 	enum spectre_v2_user_cmd mode;
1446 	char arg[20];
1447 	int ret, i;
1448 
1449 	mode = IS_ENABLED(CONFIG_MITIGATION_SPECTRE_V2) ?
1450 		SPECTRE_V2_USER_CMD_AUTO : SPECTRE_V2_USER_CMD_NONE;
1451 
1452 	switch (spectre_v2_cmd) {
1453 	case SPECTRE_V2_CMD_NONE:
1454 		return SPECTRE_V2_USER_CMD_NONE;
1455 	case SPECTRE_V2_CMD_FORCE:
1456 		return SPECTRE_V2_USER_CMD_FORCE;
1457 	default:
1458 		break;
1459 	}
1460 
1461 	ret = cmdline_find_option(boot_command_line, "spectre_v2_user",
1462 				  arg, sizeof(arg));
1463 	if (ret < 0)
1464 		return mode;
1465 
1466 	for (i = 0; i < ARRAY_SIZE(v2_user_options); i++) {
1467 		if (match_option(arg, ret, v2_user_options[i].option)) {
1468 			spec_v2_user_print_cond(v2_user_options[i].option,
1469 						v2_user_options[i].secure);
1470 			return v2_user_options[i].cmd;
1471 		}
1472 	}
1473 
1474 	pr_err("Unknown user space protection option (%s). Switching to default\n", arg);
1475 	return mode;
1476 }
1477 
spectre_v2_in_ibrs_mode(enum spectre_v2_mitigation mode)1478 static inline bool spectre_v2_in_ibrs_mode(enum spectre_v2_mitigation mode)
1479 {
1480 	return spectre_v2_in_eibrs_mode(mode) || mode == SPECTRE_V2_IBRS;
1481 }
1482 
1483 static void __init
spectre_v2_user_select_mitigation(void)1484 spectre_v2_user_select_mitigation(void)
1485 {
1486 	enum spectre_v2_user_mitigation mode = SPECTRE_V2_USER_NONE;
1487 	bool smt_possible = IS_ENABLED(CONFIG_SMP);
1488 	enum spectre_v2_user_cmd cmd;
1489 
1490 	if (!boot_cpu_has(X86_FEATURE_IBPB) && !boot_cpu_has(X86_FEATURE_STIBP))
1491 		return;
1492 
1493 	if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
1494 	    cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
1495 		smt_possible = false;
1496 
1497 	cmd = spectre_v2_parse_user_cmdline();
1498 	switch (cmd) {
1499 	case SPECTRE_V2_USER_CMD_NONE:
1500 		goto set_mode;
1501 	case SPECTRE_V2_USER_CMD_FORCE:
1502 		mode = SPECTRE_V2_USER_STRICT;
1503 		break;
1504 	case SPECTRE_V2_USER_CMD_AUTO:
1505 	case SPECTRE_V2_USER_CMD_PRCTL:
1506 	case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
1507 		mode = SPECTRE_V2_USER_PRCTL;
1508 		break;
1509 	case SPECTRE_V2_USER_CMD_SECCOMP:
1510 	case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
1511 		if (IS_ENABLED(CONFIG_SECCOMP))
1512 			mode = SPECTRE_V2_USER_SECCOMP;
1513 		else
1514 			mode = SPECTRE_V2_USER_PRCTL;
1515 		break;
1516 	}
1517 
1518 	/* Initialize Indirect Branch Prediction Barrier */
1519 	if (boot_cpu_has(X86_FEATURE_IBPB)) {
1520 		setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
1521 
1522 		spectre_v2_user_ibpb = mode;
1523 		switch (cmd) {
1524 		case SPECTRE_V2_USER_CMD_FORCE:
1525 		case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
1526 		case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
1527 			static_branch_enable(&switch_mm_always_ibpb);
1528 			spectre_v2_user_ibpb = SPECTRE_V2_USER_STRICT;
1529 			break;
1530 		case SPECTRE_V2_USER_CMD_PRCTL:
1531 		case SPECTRE_V2_USER_CMD_AUTO:
1532 		case SPECTRE_V2_USER_CMD_SECCOMP:
1533 			static_branch_enable(&switch_mm_cond_ibpb);
1534 			break;
1535 		default:
1536 			break;
1537 		}
1538 
1539 		pr_info("mitigation: Enabling %s Indirect Branch Prediction Barrier\n",
1540 			static_key_enabled(&switch_mm_always_ibpb) ?
1541 			"always-on" : "conditional");
1542 	}
1543 
1544 	/*
1545 	 * If no STIBP, Intel enhanced IBRS is enabled, or SMT impossible, STIBP
1546 	 * is not required.
1547 	 *
1548 	 * Intel's Enhanced IBRS also protects against cross-thread branch target
1549 	 * injection in user-mode as the IBRS bit remains always set which
1550 	 * implicitly enables cross-thread protections.  However, in legacy IBRS
1551 	 * mode, the IBRS bit is set only on kernel entry and cleared on return
1552 	 * to userspace.  AMD Automatic IBRS also does not protect userspace.
1553 	 * These modes therefore disable the implicit cross-thread protection,
1554 	 * so allow for STIBP to be selected in those cases.
1555 	 */
1556 	if (!boot_cpu_has(X86_FEATURE_STIBP) ||
1557 	    !smt_possible ||
1558 	    (spectre_v2_in_eibrs_mode(spectre_v2_enabled) &&
1559 	     !boot_cpu_has(X86_FEATURE_AUTOIBRS)))
1560 		return;
1561 
1562 	/*
1563 	 * At this point, an STIBP mode other than "off" has been set.
1564 	 * If STIBP support is not being forced, check if STIBP always-on
1565 	 * is preferred.
1566 	 */
1567 	if (mode != SPECTRE_V2_USER_STRICT &&
1568 	    boot_cpu_has(X86_FEATURE_AMD_STIBP_ALWAYS_ON))
1569 		mode = SPECTRE_V2_USER_STRICT_PREFERRED;
1570 
1571 	if (retbleed_mitigation == RETBLEED_MITIGATION_UNRET ||
1572 	    retbleed_mitigation == RETBLEED_MITIGATION_IBPB) {
1573 		if (mode != SPECTRE_V2_USER_STRICT &&
1574 		    mode != SPECTRE_V2_USER_STRICT_PREFERRED)
1575 			pr_info("Selecting STIBP always-on mode to complement retbleed mitigation\n");
1576 		mode = SPECTRE_V2_USER_STRICT_PREFERRED;
1577 	}
1578 
1579 	spectre_v2_user_stibp = mode;
1580 
1581 set_mode:
1582 	pr_info("%s\n", spectre_v2_user_strings[mode]);
1583 }
1584 
1585 static const char * const spectre_v2_strings[] = {
1586 	[SPECTRE_V2_NONE]			= "Vulnerable",
1587 	[SPECTRE_V2_RETPOLINE]			= "Mitigation: Retpolines",
1588 	[SPECTRE_V2_LFENCE]			= "Mitigation: LFENCE",
1589 	[SPECTRE_V2_EIBRS]			= "Mitigation: Enhanced / Automatic IBRS",
1590 	[SPECTRE_V2_EIBRS_LFENCE]		= "Mitigation: Enhanced / Automatic IBRS + LFENCE",
1591 	[SPECTRE_V2_EIBRS_RETPOLINE]		= "Mitigation: Enhanced / Automatic IBRS + Retpolines",
1592 	[SPECTRE_V2_IBRS]			= "Mitigation: IBRS",
1593 };
1594 
1595 static const struct {
1596 	const char *option;
1597 	enum spectre_v2_mitigation_cmd cmd;
1598 	bool secure;
1599 } mitigation_options[] __initconst = {
1600 	{ "off",		SPECTRE_V2_CMD_NONE,		  false },
1601 	{ "on",			SPECTRE_V2_CMD_FORCE,		  true  },
1602 	{ "retpoline",		SPECTRE_V2_CMD_RETPOLINE,	  false },
1603 	{ "retpoline,amd",	SPECTRE_V2_CMD_RETPOLINE_LFENCE,  false },
1604 	{ "retpoline,lfence",	SPECTRE_V2_CMD_RETPOLINE_LFENCE,  false },
1605 	{ "retpoline,generic",	SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
1606 	{ "eibrs",		SPECTRE_V2_CMD_EIBRS,		  false },
1607 	{ "eibrs,lfence",	SPECTRE_V2_CMD_EIBRS_LFENCE,	  false },
1608 	{ "eibrs,retpoline",	SPECTRE_V2_CMD_EIBRS_RETPOLINE,	  false },
1609 	{ "auto",		SPECTRE_V2_CMD_AUTO,		  false },
1610 	{ "ibrs",		SPECTRE_V2_CMD_IBRS,              false },
1611 };
1612 
spec_v2_print_cond(const char * reason,bool secure)1613 static void __init spec_v2_print_cond(const char *reason, bool secure)
1614 {
1615 	if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
1616 		pr_info("%s selected on command line.\n", reason);
1617 }
1618 
spectre_v2_parse_cmdline(void)1619 static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
1620 {
1621 	enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
1622 	char arg[20];
1623 	int ret, i;
1624 
1625 	if (cmdline_find_option_bool(boot_command_line, "nospectre_v2") ||
1626 	    cpu_mitigations_off())
1627 		return SPECTRE_V2_CMD_NONE;
1628 
1629 	ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
1630 	if (ret < 0)
1631 		return SPECTRE_V2_CMD_AUTO;
1632 
1633 	for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
1634 		if (!match_option(arg, ret, mitigation_options[i].option))
1635 			continue;
1636 		cmd = mitigation_options[i].cmd;
1637 		break;
1638 	}
1639 
1640 	if (i >= ARRAY_SIZE(mitigation_options)) {
1641 		pr_err("unknown option (%s). Switching to AUTO select\n", arg);
1642 		return SPECTRE_V2_CMD_AUTO;
1643 	}
1644 
1645 	if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
1646 	     cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE ||
1647 	     cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC ||
1648 	     cmd == SPECTRE_V2_CMD_EIBRS_LFENCE ||
1649 	     cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) &&
1650 	    !IS_ENABLED(CONFIG_RETPOLINE)) {
1651 		pr_err("%s selected but not compiled in. Switching to AUTO select\n",
1652 		       mitigation_options[i].option);
1653 		return SPECTRE_V2_CMD_AUTO;
1654 	}
1655 
1656 	if ((cmd == SPECTRE_V2_CMD_EIBRS ||
1657 	     cmd == SPECTRE_V2_CMD_EIBRS_LFENCE ||
1658 	     cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) &&
1659 	    !boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
1660 		pr_err("%s selected but CPU doesn't have Enhanced or Automatic IBRS. Switching to AUTO select\n",
1661 		       mitigation_options[i].option);
1662 		return SPECTRE_V2_CMD_AUTO;
1663 	}
1664 
1665 	if ((cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE ||
1666 	     cmd == SPECTRE_V2_CMD_EIBRS_LFENCE) &&
1667 	    !boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
1668 		pr_err("%s selected, but CPU doesn't have a serializing LFENCE. Switching to AUTO select\n",
1669 		       mitigation_options[i].option);
1670 		return SPECTRE_V2_CMD_AUTO;
1671 	}
1672 
1673 	if (cmd == SPECTRE_V2_CMD_IBRS && !IS_ENABLED(CONFIG_CPU_IBRS_ENTRY)) {
1674 		pr_err("%s selected but not compiled in. Switching to AUTO select\n",
1675 		       mitigation_options[i].option);
1676 		return SPECTRE_V2_CMD_AUTO;
1677 	}
1678 
1679 	if (cmd == SPECTRE_V2_CMD_IBRS && boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
1680 		pr_err("%s selected but not Intel CPU. Switching to AUTO select\n",
1681 		       mitigation_options[i].option);
1682 		return SPECTRE_V2_CMD_AUTO;
1683 	}
1684 
1685 	if (cmd == SPECTRE_V2_CMD_IBRS && !boot_cpu_has(X86_FEATURE_IBRS)) {
1686 		pr_err("%s selected but CPU doesn't have IBRS. Switching to AUTO select\n",
1687 		       mitigation_options[i].option);
1688 		return SPECTRE_V2_CMD_AUTO;
1689 	}
1690 
1691 	if (cmd == SPECTRE_V2_CMD_IBRS && cpu_feature_enabled(X86_FEATURE_XENPV)) {
1692 		pr_err("%s selected but running as XenPV guest. Switching to AUTO select\n",
1693 		       mitigation_options[i].option);
1694 		return SPECTRE_V2_CMD_AUTO;
1695 	}
1696 
1697 	spec_v2_print_cond(mitigation_options[i].option,
1698 			   mitigation_options[i].secure);
1699 	return cmd;
1700 }
1701 
spectre_v2_select_retpoline(void)1702 static enum spectre_v2_mitigation __init spectre_v2_select_retpoline(void)
1703 {
1704 	if (!IS_ENABLED(CONFIG_RETPOLINE)) {
1705 		pr_err("Kernel not compiled with retpoline; no mitigation available!");
1706 		return SPECTRE_V2_NONE;
1707 	}
1708 
1709 	return SPECTRE_V2_RETPOLINE;
1710 }
1711 
1712 static bool __ro_after_init rrsba_disabled;
1713 
1714 /* Disable in-kernel use of non-RSB RET predictors */
spec_ctrl_disable_kernel_rrsba(void)1715 static void __init spec_ctrl_disable_kernel_rrsba(void)
1716 {
1717 	if (rrsba_disabled)
1718 		return;
1719 
1720 	if (!(x86_arch_cap_msr & ARCH_CAP_RRSBA)) {
1721 		rrsba_disabled = true;
1722 		return;
1723 	}
1724 
1725 	if (!boot_cpu_has(X86_FEATURE_RRSBA_CTRL))
1726 		return;
1727 
1728 	x86_spec_ctrl_base |= SPEC_CTRL_RRSBA_DIS_S;
1729 	update_spec_ctrl(x86_spec_ctrl_base);
1730 	rrsba_disabled = true;
1731 }
1732 
spectre_v2_select_rsb_mitigation(enum spectre_v2_mitigation mode)1733 static void __init spectre_v2_select_rsb_mitigation(enum spectre_v2_mitigation mode)
1734 {
1735 	/*
1736 	 * Similar to context switches, there are two types of RSB attacks
1737 	 * after VM exit:
1738 	 *
1739 	 * 1) RSB underflow
1740 	 *
1741 	 * 2) Poisoned RSB entry
1742 	 *
1743 	 * When retpoline is enabled, both are mitigated by filling/clearing
1744 	 * the RSB.
1745 	 *
1746 	 * When IBRS is enabled, while #1 would be mitigated by the IBRS branch
1747 	 * prediction isolation protections, RSB still needs to be cleared
1748 	 * because of #2.  Note that SMEP provides no protection here, unlike
1749 	 * user-space-poisoned RSB entries.
1750 	 *
1751 	 * eIBRS should protect against RSB poisoning, but if the EIBRS_PBRSB
1752 	 * bug is present then a LITE version of RSB protection is required,
1753 	 * just a single call needs to retire before a RET is executed.
1754 	 */
1755 	switch (mode) {
1756 	case SPECTRE_V2_NONE:
1757 		break;
1758 
1759 	case SPECTRE_V2_EIBRS:
1760 	case SPECTRE_V2_EIBRS_LFENCE:
1761 	case SPECTRE_V2_EIBRS_RETPOLINE:
1762 		if (boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB)) {
1763 			pr_info("Spectre v2 / PBRSB-eIBRS: Retire a single CALL on VMEXIT\n");
1764 			setup_force_cpu_cap(X86_FEATURE_RSB_VMEXIT_LITE);
1765 		}
1766 		break;
1767 
1768 	case SPECTRE_V2_RETPOLINE:
1769 	case SPECTRE_V2_LFENCE:
1770 	case SPECTRE_V2_IBRS:
1771 		pr_info("Spectre v2 / SpectreRSB: Filling RSB on context switch and VMEXIT\n");
1772 		setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
1773 		setup_force_cpu_cap(X86_FEATURE_RSB_VMEXIT);
1774 		break;
1775 
1776 	default:
1777 		pr_warn_once("Unknown Spectre v2 mode, disabling RSB mitigation\n");
1778 		dump_stack();
1779 		break;
1780 	}
1781 }
1782 
1783 /*
1784  * Set BHI_DIS_S to prevent indirect branches in kernel to be influenced by
1785  * branch history in userspace. Not needed if BHI_NO is set.
1786  */
spec_ctrl_bhi_dis(void)1787 static bool __init spec_ctrl_bhi_dis(void)
1788 {
1789 	if (!boot_cpu_has(X86_FEATURE_BHI_CTRL))
1790 		return false;
1791 
1792 	x86_spec_ctrl_base |= SPEC_CTRL_BHI_DIS_S;
1793 	update_spec_ctrl(x86_spec_ctrl_base);
1794 	setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_HW);
1795 
1796 	return true;
1797 }
1798 
1799 enum bhi_mitigations {
1800 	BHI_MITIGATION_OFF,
1801 	BHI_MITIGATION_ON,
1802 };
1803 
1804 static enum bhi_mitigations bhi_mitigation __ro_after_init =
1805 	IS_ENABLED(CONFIG_MITIGATION_SPECTRE_BHI) ? BHI_MITIGATION_ON : BHI_MITIGATION_OFF;
1806 
spectre_bhi_parse_cmdline(char * str)1807 static int __init spectre_bhi_parse_cmdline(char *str)
1808 {
1809 	if (!str)
1810 		return -EINVAL;
1811 
1812 	if (!strcmp(str, "off"))
1813 		bhi_mitigation = BHI_MITIGATION_OFF;
1814 	else if (!strcmp(str, "on"))
1815 		bhi_mitigation = BHI_MITIGATION_ON;
1816 	else
1817 		pr_err("Ignoring unknown spectre_bhi option (%s)", str);
1818 
1819 	return 0;
1820 }
1821 early_param("spectre_bhi", spectre_bhi_parse_cmdline);
1822 
bhi_select_mitigation(void)1823 static void __init bhi_select_mitigation(void)
1824 {
1825 	if (bhi_mitigation == BHI_MITIGATION_OFF)
1826 		return;
1827 
1828 	/* Retpoline mitigates against BHI unless the CPU has RRSBA behavior */
1829 	if (boot_cpu_has(X86_FEATURE_RETPOLINE) &&
1830 	    !boot_cpu_has(X86_FEATURE_RETPOLINE_LFENCE)) {
1831 		spec_ctrl_disable_kernel_rrsba();
1832 		if (rrsba_disabled)
1833 			return;
1834 	}
1835 
1836 	if (!IS_ENABLED(CONFIG_X86_64))
1837 		return;
1838 
1839 	/* Mitigate in hardware if supported */
1840 	if (spec_ctrl_bhi_dis())
1841 		return;
1842 
1843 	/* Mitigate KVM by default */
1844 	setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_LOOP_ON_VMEXIT);
1845 	pr_info("Spectre BHI mitigation: SW BHB clearing on vm exit\n");
1846 
1847 	/* Mitigate syscalls when the mitigation is forced =on */
1848 	setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_LOOP);
1849 	pr_info("Spectre BHI mitigation: SW BHB clearing on syscall\n");
1850 }
1851 
spectre_v2_select_mitigation(void)1852 static void __init spectre_v2_select_mitigation(void)
1853 {
1854 	enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
1855 	enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
1856 
1857 	/*
1858 	 * If the CPU is not affected and the command line mode is NONE or AUTO
1859 	 * then nothing to do.
1860 	 */
1861 	if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
1862 	    (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
1863 		return;
1864 
1865 	switch (cmd) {
1866 	case SPECTRE_V2_CMD_NONE:
1867 		return;
1868 
1869 	case SPECTRE_V2_CMD_FORCE:
1870 	case SPECTRE_V2_CMD_AUTO:
1871 		if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
1872 			mode = SPECTRE_V2_EIBRS;
1873 			break;
1874 		}
1875 
1876 		if (IS_ENABLED(CONFIG_CPU_IBRS_ENTRY) &&
1877 		    boot_cpu_has_bug(X86_BUG_RETBLEED) &&
1878 		    retbleed_cmd != RETBLEED_CMD_OFF &&
1879 		    retbleed_cmd != RETBLEED_CMD_STUFF &&
1880 		    boot_cpu_has(X86_FEATURE_IBRS) &&
1881 		    boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
1882 			mode = SPECTRE_V2_IBRS;
1883 			break;
1884 		}
1885 
1886 		mode = spectre_v2_select_retpoline();
1887 		break;
1888 
1889 	case SPECTRE_V2_CMD_RETPOLINE_LFENCE:
1890 		pr_err(SPECTRE_V2_LFENCE_MSG);
1891 		mode = SPECTRE_V2_LFENCE;
1892 		break;
1893 
1894 	case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
1895 		mode = SPECTRE_V2_RETPOLINE;
1896 		break;
1897 
1898 	case SPECTRE_V2_CMD_RETPOLINE:
1899 		mode = spectre_v2_select_retpoline();
1900 		break;
1901 
1902 	case SPECTRE_V2_CMD_IBRS:
1903 		mode = SPECTRE_V2_IBRS;
1904 		break;
1905 
1906 	case SPECTRE_V2_CMD_EIBRS:
1907 		mode = SPECTRE_V2_EIBRS;
1908 		break;
1909 
1910 	case SPECTRE_V2_CMD_EIBRS_LFENCE:
1911 		mode = SPECTRE_V2_EIBRS_LFENCE;
1912 		break;
1913 
1914 	case SPECTRE_V2_CMD_EIBRS_RETPOLINE:
1915 		mode = SPECTRE_V2_EIBRS_RETPOLINE;
1916 		break;
1917 	}
1918 
1919 	if (mode == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
1920 		pr_err(SPECTRE_V2_EIBRS_EBPF_MSG);
1921 
1922 	if (spectre_v2_in_ibrs_mode(mode)) {
1923 		if (boot_cpu_has(X86_FEATURE_AUTOIBRS)) {
1924 			msr_set_bit(MSR_EFER, _EFER_AUTOIBRS);
1925 		} else {
1926 			x86_spec_ctrl_base |= SPEC_CTRL_IBRS;
1927 			update_spec_ctrl(x86_spec_ctrl_base);
1928 		}
1929 	}
1930 
1931 	switch (mode) {
1932 	case SPECTRE_V2_NONE:
1933 	case SPECTRE_V2_EIBRS:
1934 		break;
1935 
1936 	case SPECTRE_V2_IBRS:
1937 		setup_force_cpu_cap(X86_FEATURE_KERNEL_IBRS);
1938 		if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED))
1939 			pr_warn(SPECTRE_V2_IBRS_PERF_MSG);
1940 		break;
1941 
1942 	case SPECTRE_V2_LFENCE:
1943 	case SPECTRE_V2_EIBRS_LFENCE:
1944 		setup_force_cpu_cap(X86_FEATURE_RETPOLINE_LFENCE);
1945 		fallthrough;
1946 
1947 	case SPECTRE_V2_RETPOLINE:
1948 	case SPECTRE_V2_EIBRS_RETPOLINE:
1949 		setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
1950 		break;
1951 	}
1952 
1953 	/*
1954 	 * Disable alternate RSB predictions in kernel when indirect CALLs and
1955 	 * JMPs gets protection against BHI and Intramode-BTI, but RET
1956 	 * prediction from a non-RSB predictor is still a risk.
1957 	 */
1958 	if (mode == SPECTRE_V2_EIBRS_LFENCE ||
1959 	    mode == SPECTRE_V2_EIBRS_RETPOLINE ||
1960 	    mode == SPECTRE_V2_RETPOLINE)
1961 		spec_ctrl_disable_kernel_rrsba();
1962 
1963 	if (boot_cpu_has(X86_BUG_BHI))
1964 		bhi_select_mitigation();
1965 
1966 	spectre_v2_enabled = mode;
1967 	pr_info("%s\n", spectre_v2_strings[mode]);
1968 
1969 	/*
1970 	 * If Spectre v2 protection has been enabled, fill the RSB during a
1971 	 * context switch.  In general there are two types of RSB attacks
1972 	 * across context switches, for which the CALLs/RETs may be unbalanced.
1973 	 *
1974 	 * 1) RSB underflow
1975 	 *
1976 	 *    Some Intel parts have "bottomless RSB".  When the RSB is empty,
1977 	 *    speculated return targets may come from the branch predictor,
1978 	 *    which could have a user-poisoned BTB or BHB entry.
1979 	 *
1980 	 *    AMD has it even worse: *all* returns are speculated from the BTB,
1981 	 *    regardless of the state of the RSB.
1982 	 *
1983 	 *    When IBRS or eIBRS is enabled, the "user -> kernel" attack
1984 	 *    scenario is mitigated by the IBRS branch prediction isolation
1985 	 *    properties, so the RSB buffer filling wouldn't be necessary to
1986 	 *    protect against this type of attack.
1987 	 *
1988 	 *    The "user -> user" attack scenario is mitigated by RSB filling.
1989 	 *
1990 	 * 2) Poisoned RSB entry
1991 	 *
1992 	 *    If the 'next' in-kernel return stack is shorter than 'prev',
1993 	 *    'next' could be tricked into speculating with a user-poisoned RSB
1994 	 *    entry.
1995 	 *
1996 	 *    The "user -> kernel" attack scenario is mitigated by SMEP and
1997 	 *    eIBRS.
1998 	 *
1999 	 *    The "user -> user" scenario, also known as SpectreBHB, requires
2000 	 *    RSB clearing.
2001 	 *
2002 	 * So to mitigate all cases, unconditionally fill RSB on context
2003 	 * switches.
2004 	 *
2005 	 * FIXME: Is this pointless for retbleed-affected AMD?
2006 	 */
2007 	spectre_v2_select_rsb_mitigation(mode);
2008 
2009 	/*
2010 	 * Retpoline protects the kernel, but doesn't protect firmware.  IBRS
2011 	 * and Enhanced IBRS protect firmware too, so enable IBRS around
2012 	 * firmware calls only when IBRS / Enhanced / Automatic IBRS aren't
2013 	 * otherwise enabled.
2014 	 *
2015 	 * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because
2016 	 * the user might select retpoline on the kernel command line and if
2017 	 * the CPU supports Enhanced IBRS, kernel might un-intentionally not
2018 	 * enable IBRS around firmware calls.
2019 	 */
2020 	if (boot_cpu_has_bug(X86_BUG_RETBLEED) &&
2021 	    boot_cpu_has(X86_FEATURE_IBPB) &&
2022 	    (boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
2023 	     boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)) {
2024 
2025 		if (retbleed_cmd != RETBLEED_CMD_IBPB) {
2026 			setup_force_cpu_cap(X86_FEATURE_USE_IBPB_FW);
2027 			pr_info("Enabling Speculation Barrier for firmware calls\n");
2028 		}
2029 
2030 	} else if (boot_cpu_has(X86_FEATURE_IBRS) && !spectre_v2_in_ibrs_mode(mode)) {
2031 		setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
2032 		pr_info("Enabling Restricted Speculation for firmware calls\n");
2033 	}
2034 
2035 	/* Set up IBPB and STIBP depending on the general spectre V2 command */
2036 	spectre_v2_cmd = cmd;
2037 }
2038 
update_stibp_msr(void * __unused)2039 static void update_stibp_msr(void * __unused)
2040 {
2041 	u64 val = spec_ctrl_current() | (x86_spec_ctrl_base & SPEC_CTRL_STIBP);
2042 	update_spec_ctrl(val);
2043 }
2044 
2045 /* Update x86_spec_ctrl_base in case SMT state changed. */
update_stibp_strict(void)2046 static void update_stibp_strict(void)
2047 {
2048 	u64 mask = x86_spec_ctrl_base & ~SPEC_CTRL_STIBP;
2049 
2050 	if (sched_smt_active())
2051 		mask |= SPEC_CTRL_STIBP;
2052 
2053 	if (mask == x86_spec_ctrl_base)
2054 		return;
2055 
2056 	pr_info("Update user space SMT mitigation: STIBP %s\n",
2057 		mask & SPEC_CTRL_STIBP ? "always-on" : "off");
2058 	x86_spec_ctrl_base = mask;
2059 	on_each_cpu(update_stibp_msr, NULL, 1);
2060 }
2061 
2062 /* Update the static key controlling the evaluation of TIF_SPEC_IB */
update_indir_branch_cond(void)2063 static void update_indir_branch_cond(void)
2064 {
2065 	if (sched_smt_active())
2066 		static_branch_enable(&switch_to_cond_stibp);
2067 	else
2068 		static_branch_disable(&switch_to_cond_stibp);
2069 }
2070 
2071 #undef pr_fmt
2072 #define pr_fmt(fmt) fmt
2073 
2074 /* Update the static key controlling the MDS CPU buffer clear in idle */
update_mds_branch_idle(void)2075 static void update_mds_branch_idle(void)
2076 {
2077 	/*
2078 	 * Enable the idle clearing if SMT is active on CPUs which are
2079 	 * affected only by MSBDS and not any other MDS variant.
2080 	 *
2081 	 * The other variants cannot be mitigated when SMT is enabled, so
2082 	 * clearing the buffers on idle just to prevent the Store Buffer
2083 	 * repartitioning leak would be a window dressing exercise.
2084 	 */
2085 	if (!boot_cpu_has_bug(X86_BUG_MSBDS_ONLY))
2086 		return;
2087 
2088 	if (sched_smt_active()) {
2089 		static_branch_enable(&mds_idle_clear);
2090 	} else if (mmio_mitigation == MMIO_MITIGATION_OFF ||
2091 		   (x86_arch_cap_msr & ARCH_CAP_FBSDP_NO)) {
2092 		static_branch_disable(&mds_idle_clear);
2093 	}
2094 }
2095 
2096 #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"
2097 #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"
2098 #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"
2099 
cpu_bugs_smt_update(void)2100 void cpu_bugs_smt_update(void)
2101 {
2102 	mutex_lock(&spec_ctrl_mutex);
2103 
2104 	if (sched_smt_active() && unprivileged_ebpf_enabled() &&
2105 	    spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
2106 		pr_warn_once(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG);
2107 
2108 	switch (spectre_v2_user_stibp) {
2109 	case SPECTRE_V2_USER_NONE:
2110 		break;
2111 	case SPECTRE_V2_USER_STRICT:
2112 	case SPECTRE_V2_USER_STRICT_PREFERRED:
2113 		update_stibp_strict();
2114 		break;
2115 	case SPECTRE_V2_USER_PRCTL:
2116 	case SPECTRE_V2_USER_SECCOMP:
2117 		update_indir_branch_cond();
2118 		break;
2119 	}
2120 
2121 	switch (mds_mitigation) {
2122 	case MDS_MITIGATION_FULL:
2123 	case MDS_MITIGATION_VMWERV:
2124 		if (sched_smt_active() && !boot_cpu_has(X86_BUG_MSBDS_ONLY))
2125 			pr_warn_once(MDS_MSG_SMT);
2126 		update_mds_branch_idle();
2127 		break;
2128 	case MDS_MITIGATION_OFF:
2129 		break;
2130 	}
2131 
2132 	switch (taa_mitigation) {
2133 	case TAA_MITIGATION_VERW:
2134 	case TAA_MITIGATION_UCODE_NEEDED:
2135 		if (sched_smt_active())
2136 			pr_warn_once(TAA_MSG_SMT);
2137 		break;
2138 	case TAA_MITIGATION_TSX_DISABLED:
2139 	case TAA_MITIGATION_OFF:
2140 		break;
2141 	}
2142 
2143 	switch (mmio_mitigation) {
2144 	case MMIO_MITIGATION_VERW:
2145 	case MMIO_MITIGATION_UCODE_NEEDED:
2146 		if (sched_smt_active())
2147 			pr_warn_once(MMIO_MSG_SMT);
2148 		break;
2149 	case MMIO_MITIGATION_OFF:
2150 		break;
2151 	}
2152 
2153 	mutex_unlock(&spec_ctrl_mutex);
2154 }
2155 
2156 #undef pr_fmt
2157 #define pr_fmt(fmt)	"Speculative Store Bypass: " fmt
2158 
2159 static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
2160 
2161 /* The kernel command line selection */
2162 enum ssb_mitigation_cmd {
2163 	SPEC_STORE_BYPASS_CMD_NONE,
2164 	SPEC_STORE_BYPASS_CMD_AUTO,
2165 	SPEC_STORE_BYPASS_CMD_ON,
2166 	SPEC_STORE_BYPASS_CMD_PRCTL,
2167 	SPEC_STORE_BYPASS_CMD_SECCOMP,
2168 };
2169 
2170 static const char * const ssb_strings[] = {
2171 	[SPEC_STORE_BYPASS_NONE]	= "Vulnerable",
2172 	[SPEC_STORE_BYPASS_DISABLE]	= "Mitigation: Speculative Store Bypass disabled",
2173 	[SPEC_STORE_BYPASS_PRCTL]	= "Mitigation: Speculative Store Bypass disabled via prctl",
2174 	[SPEC_STORE_BYPASS_SECCOMP]	= "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
2175 };
2176 
2177 static const struct {
2178 	const char *option;
2179 	enum ssb_mitigation_cmd cmd;
2180 } ssb_mitigation_options[]  __initconst = {
2181 	{ "auto",	SPEC_STORE_BYPASS_CMD_AUTO },    /* Platform decides */
2182 	{ "on",		SPEC_STORE_BYPASS_CMD_ON },      /* Disable Speculative Store Bypass */
2183 	{ "off",	SPEC_STORE_BYPASS_CMD_NONE },    /* Don't touch Speculative Store Bypass */
2184 	{ "prctl",	SPEC_STORE_BYPASS_CMD_PRCTL },   /* Disable Speculative Store Bypass via prctl */
2185 	{ "seccomp",	SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */
2186 };
2187 
ssb_parse_cmdline(void)2188 static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
2189 {
2190 	enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO;
2191 	char arg[20];
2192 	int ret, i;
2193 
2194 	if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable") ||
2195 	    cpu_mitigations_off()) {
2196 		return SPEC_STORE_BYPASS_CMD_NONE;
2197 	} else {
2198 		ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable",
2199 					  arg, sizeof(arg));
2200 		if (ret < 0)
2201 			return SPEC_STORE_BYPASS_CMD_AUTO;
2202 
2203 		for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) {
2204 			if (!match_option(arg, ret, ssb_mitigation_options[i].option))
2205 				continue;
2206 
2207 			cmd = ssb_mitigation_options[i].cmd;
2208 			break;
2209 		}
2210 
2211 		if (i >= ARRAY_SIZE(ssb_mitigation_options)) {
2212 			pr_err("unknown option (%s). Switching to AUTO select\n", arg);
2213 			return SPEC_STORE_BYPASS_CMD_AUTO;
2214 		}
2215 	}
2216 
2217 	return cmd;
2218 }
2219 
__ssb_select_mitigation(void)2220 static enum ssb_mitigation __init __ssb_select_mitigation(void)
2221 {
2222 	enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
2223 	enum ssb_mitigation_cmd cmd;
2224 
2225 	if (!boot_cpu_has(X86_FEATURE_SSBD))
2226 		return mode;
2227 
2228 	cmd = ssb_parse_cmdline();
2229 	if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) &&
2230 	    (cmd == SPEC_STORE_BYPASS_CMD_NONE ||
2231 	     cmd == SPEC_STORE_BYPASS_CMD_AUTO))
2232 		return mode;
2233 
2234 	switch (cmd) {
2235 	case SPEC_STORE_BYPASS_CMD_SECCOMP:
2236 		/*
2237 		 * Choose prctl+seccomp as the default mode if seccomp is
2238 		 * enabled.
2239 		 */
2240 		if (IS_ENABLED(CONFIG_SECCOMP))
2241 			mode = SPEC_STORE_BYPASS_SECCOMP;
2242 		else
2243 			mode = SPEC_STORE_BYPASS_PRCTL;
2244 		break;
2245 	case SPEC_STORE_BYPASS_CMD_ON:
2246 		mode = SPEC_STORE_BYPASS_DISABLE;
2247 		break;
2248 	case SPEC_STORE_BYPASS_CMD_AUTO:
2249 	case SPEC_STORE_BYPASS_CMD_PRCTL:
2250 		mode = SPEC_STORE_BYPASS_PRCTL;
2251 		break;
2252 	case SPEC_STORE_BYPASS_CMD_NONE:
2253 		break;
2254 	}
2255 
2256 	/*
2257 	 * We have three CPU feature flags that are in play here:
2258 	 *  - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
2259 	 *  - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
2260 	 *  - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
2261 	 */
2262 	if (mode == SPEC_STORE_BYPASS_DISABLE) {
2263 		setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
2264 		/*
2265 		 * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
2266 		 * use a completely different MSR and bit dependent on family.
2267 		 */
2268 		if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
2269 		    !static_cpu_has(X86_FEATURE_AMD_SSBD)) {
2270 			x86_amd_ssb_disable();
2271 		} else {
2272 			x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
2273 			update_spec_ctrl(x86_spec_ctrl_base);
2274 		}
2275 	}
2276 
2277 	return mode;
2278 }
2279 
ssb_select_mitigation(void)2280 static void ssb_select_mitigation(void)
2281 {
2282 	ssb_mode = __ssb_select_mitigation();
2283 
2284 	if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
2285 		pr_info("%s\n", ssb_strings[ssb_mode]);
2286 }
2287 
2288 #undef pr_fmt
2289 #define pr_fmt(fmt)     "Speculation prctl: " fmt
2290 
task_update_spec_tif(struct task_struct * tsk)2291 static void task_update_spec_tif(struct task_struct *tsk)
2292 {
2293 	/* Force the update of the real TIF bits */
2294 	set_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE);
2295 
2296 	/*
2297 	 * Immediately update the speculation control MSRs for the current
2298 	 * task, but for a non-current task delay setting the CPU
2299 	 * mitigation until it is scheduled next.
2300 	 *
2301 	 * This can only happen for SECCOMP mitigation. For PRCTL it's
2302 	 * always the current task.
2303 	 */
2304 	if (tsk == current)
2305 		speculation_ctrl_update_current();
2306 }
2307 
l1d_flush_prctl_set(struct task_struct * task,unsigned long ctrl)2308 static int l1d_flush_prctl_set(struct task_struct *task, unsigned long ctrl)
2309 {
2310 
2311 	if (!static_branch_unlikely(&switch_mm_cond_l1d_flush))
2312 		return -EPERM;
2313 
2314 	switch (ctrl) {
2315 	case PR_SPEC_ENABLE:
2316 		set_ti_thread_flag(&task->thread_info, TIF_SPEC_L1D_FLUSH);
2317 		return 0;
2318 	case PR_SPEC_DISABLE:
2319 		clear_ti_thread_flag(&task->thread_info, TIF_SPEC_L1D_FLUSH);
2320 		return 0;
2321 	default:
2322 		return -ERANGE;
2323 	}
2324 }
2325 
ssb_prctl_set(struct task_struct * task,unsigned long ctrl)2326 static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
2327 {
2328 	if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
2329 	    ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
2330 		return -ENXIO;
2331 
2332 	switch (ctrl) {
2333 	case PR_SPEC_ENABLE:
2334 		/* If speculation is force disabled, enable is not allowed */
2335 		if (task_spec_ssb_force_disable(task))
2336 			return -EPERM;
2337 		task_clear_spec_ssb_disable(task);
2338 		task_clear_spec_ssb_noexec(task);
2339 		task_update_spec_tif(task);
2340 		break;
2341 	case PR_SPEC_DISABLE:
2342 		task_set_spec_ssb_disable(task);
2343 		task_clear_spec_ssb_noexec(task);
2344 		task_update_spec_tif(task);
2345 		break;
2346 	case PR_SPEC_FORCE_DISABLE:
2347 		task_set_spec_ssb_disable(task);
2348 		task_set_spec_ssb_force_disable(task);
2349 		task_clear_spec_ssb_noexec(task);
2350 		task_update_spec_tif(task);
2351 		break;
2352 	case PR_SPEC_DISABLE_NOEXEC:
2353 		if (task_spec_ssb_force_disable(task))
2354 			return -EPERM;
2355 		task_set_spec_ssb_disable(task);
2356 		task_set_spec_ssb_noexec(task);
2357 		task_update_spec_tif(task);
2358 		break;
2359 	default:
2360 		return -ERANGE;
2361 	}
2362 	return 0;
2363 }
2364 
is_spec_ib_user_controlled(void)2365 static bool is_spec_ib_user_controlled(void)
2366 {
2367 	return spectre_v2_user_ibpb == SPECTRE_V2_USER_PRCTL ||
2368 		spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
2369 		spectre_v2_user_stibp == SPECTRE_V2_USER_PRCTL ||
2370 		spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP;
2371 }
2372 
ib_prctl_set(struct task_struct * task,unsigned long ctrl)2373 static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)
2374 {
2375 	switch (ctrl) {
2376 	case PR_SPEC_ENABLE:
2377 		if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
2378 		    spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
2379 			return 0;
2380 
2381 		/*
2382 		 * With strict mode for both IBPB and STIBP, the instruction
2383 		 * code paths avoid checking this task flag and instead,
2384 		 * unconditionally run the instruction. However, STIBP and IBPB
2385 		 * are independent and either can be set to conditionally
2386 		 * enabled regardless of the mode of the other.
2387 		 *
2388 		 * If either is set to conditional, allow the task flag to be
2389 		 * updated, unless it was force-disabled by a previous prctl
2390 		 * call. Currently, this is possible on an AMD CPU which has the
2391 		 * feature X86_FEATURE_AMD_STIBP_ALWAYS_ON. In this case, if the
2392 		 * kernel is booted with 'spectre_v2_user=seccomp', then
2393 		 * spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP and
2394 		 * spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED.
2395 		 */
2396 		if (!is_spec_ib_user_controlled() ||
2397 		    task_spec_ib_force_disable(task))
2398 			return -EPERM;
2399 
2400 		task_clear_spec_ib_disable(task);
2401 		task_update_spec_tif(task);
2402 		break;
2403 	case PR_SPEC_DISABLE:
2404 	case PR_SPEC_FORCE_DISABLE:
2405 		/*
2406 		 * Indirect branch speculation is always allowed when
2407 		 * mitigation is force disabled.
2408 		 */
2409 		if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
2410 		    spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
2411 			return -EPERM;
2412 
2413 		if (!is_spec_ib_user_controlled())
2414 			return 0;
2415 
2416 		task_set_spec_ib_disable(task);
2417 		if (ctrl == PR_SPEC_FORCE_DISABLE)
2418 			task_set_spec_ib_force_disable(task);
2419 		task_update_spec_tif(task);
2420 		if (task == current)
2421 			indirect_branch_prediction_barrier();
2422 		break;
2423 	default:
2424 		return -ERANGE;
2425 	}
2426 	return 0;
2427 }
2428 
arch_prctl_spec_ctrl_set(struct task_struct * task,unsigned long which,unsigned long ctrl)2429 int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
2430 			     unsigned long ctrl)
2431 {
2432 	switch (which) {
2433 	case PR_SPEC_STORE_BYPASS:
2434 		return ssb_prctl_set(task, ctrl);
2435 	case PR_SPEC_INDIRECT_BRANCH:
2436 		return ib_prctl_set(task, ctrl);
2437 	case PR_SPEC_L1D_FLUSH:
2438 		return l1d_flush_prctl_set(task, ctrl);
2439 	default:
2440 		return -ENODEV;
2441 	}
2442 }
2443 
2444 #ifdef CONFIG_SECCOMP
arch_seccomp_spec_mitigate(struct task_struct * task)2445 void arch_seccomp_spec_mitigate(struct task_struct *task)
2446 {
2447 	if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP)
2448 		ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
2449 	if (spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
2450 	    spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP)
2451 		ib_prctl_set(task, PR_SPEC_FORCE_DISABLE);
2452 }
2453 #endif
2454 
l1d_flush_prctl_get(struct task_struct * task)2455 static int l1d_flush_prctl_get(struct task_struct *task)
2456 {
2457 	if (!static_branch_unlikely(&switch_mm_cond_l1d_flush))
2458 		return PR_SPEC_FORCE_DISABLE;
2459 
2460 	if (test_ti_thread_flag(&task->thread_info, TIF_SPEC_L1D_FLUSH))
2461 		return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
2462 	else
2463 		return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
2464 }
2465 
ssb_prctl_get(struct task_struct * task)2466 static int ssb_prctl_get(struct task_struct *task)
2467 {
2468 	switch (ssb_mode) {
2469 	case SPEC_STORE_BYPASS_DISABLE:
2470 		return PR_SPEC_DISABLE;
2471 	case SPEC_STORE_BYPASS_SECCOMP:
2472 	case SPEC_STORE_BYPASS_PRCTL:
2473 		if (task_spec_ssb_force_disable(task))
2474 			return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
2475 		if (task_spec_ssb_noexec(task))
2476 			return PR_SPEC_PRCTL | PR_SPEC_DISABLE_NOEXEC;
2477 		if (task_spec_ssb_disable(task))
2478 			return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
2479 		return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
2480 	default:
2481 		if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
2482 			return PR_SPEC_ENABLE;
2483 		return PR_SPEC_NOT_AFFECTED;
2484 	}
2485 }
2486 
ib_prctl_get(struct task_struct * task)2487 static int ib_prctl_get(struct task_struct *task)
2488 {
2489 	if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
2490 		return PR_SPEC_NOT_AFFECTED;
2491 
2492 	if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
2493 	    spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
2494 		return PR_SPEC_ENABLE;
2495 	else if (is_spec_ib_user_controlled()) {
2496 		if (task_spec_ib_force_disable(task))
2497 			return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
2498 		if (task_spec_ib_disable(task))
2499 			return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
2500 		return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
2501 	} else if (spectre_v2_user_ibpb == SPECTRE_V2_USER_STRICT ||
2502 	    spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
2503 	    spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED)
2504 		return PR_SPEC_DISABLE;
2505 	else
2506 		return PR_SPEC_NOT_AFFECTED;
2507 }
2508 
arch_prctl_spec_ctrl_get(struct task_struct * task,unsigned long which)2509 int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
2510 {
2511 	switch (which) {
2512 	case PR_SPEC_STORE_BYPASS:
2513 		return ssb_prctl_get(task);
2514 	case PR_SPEC_INDIRECT_BRANCH:
2515 		return ib_prctl_get(task);
2516 	case PR_SPEC_L1D_FLUSH:
2517 		return l1d_flush_prctl_get(task);
2518 	default:
2519 		return -ENODEV;
2520 	}
2521 }
2522 
x86_spec_ctrl_setup_ap(void)2523 void x86_spec_ctrl_setup_ap(void)
2524 {
2525 	if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
2526 		update_spec_ctrl(x86_spec_ctrl_base);
2527 
2528 	if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
2529 		x86_amd_ssb_disable();
2530 }
2531 
2532 bool itlb_multihit_kvm_mitigation;
2533 EXPORT_SYMBOL_GPL(itlb_multihit_kvm_mitigation);
2534 
2535 #undef pr_fmt
2536 #define pr_fmt(fmt)	"L1TF: " fmt
2537 
2538 /* Default mitigation for L1TF-affected CPUs */
2539 enum l1tf_mitigations l1tf_mitigation __ro_after_init = L1TF_MITIGATION_FLUSH;
2540 #if IS_ENABLED(CONFIG_KVM_INTEL)
2541 EXPORT_SYMBOL_GPL(l1tf_mitigation);
2542 #endif
2543 enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
2544 EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation);
2545 
2546 /*
2547  * These CPUs all support 44bits physical address space internally in the
2548  * cache but CPUID can report a smaller number of physical address bits.
2549  *
2550  * The L1TF mitigation uses the top most address bit for the inversion of
2551  * non present PTEs. When the installed memory reaches into the top most
2552  * address bit due to memory holes, which has been observed on machines
2553  * which report 36bits physical address bits and have 32G RAM installed,
2554  * then the mitigation range check in l1tf_select_mitigation() triggers.
2555  * This is a false positive because the mitigation is still possible due to
2556  * the fact that the cache uses 44bit internally. Use the cache bits
2557  * instead of the reported physical bits and adjust them on the affected
2558  * machines to 44bit if the reported bits are less than 44.
2559  */
override_cache_bits(struct cpuinfo_x86 * c)2560 static void override_cache_bits(struct cpuinfo_x86 *c)
2561 {
2562 	if (c->x86 != 6)
2563 		return;
2564 
2565 	switch (c->x86_model) {
2566 	case INTEL_FAM6_NEHALEM:
2567 	case INTEL_FAM6_WESTMERE:
2568 	case INTEL_FAM6_SANDYBRIDGE:
2569 	case INTEL_FAM6_IVYBRIDGE:
2570 	case INTEL_FAM6_HASWELL:
2571 	case INTEL_FAM6_HASWELL_L:
2572 	case INTEL_FAM6_HASWELL_G:
2573 	case INTEL_FAM6_BROADWELL:
2574 	case INTEL_FAM6_BROADWELL_G:
2575 	case INTEL_FAM6_SKYLAKE_L:
2576 	case INTEL_FAM6_SKYLAKE:
2577 	case INTEL_FAM6_KABYLAKE_L:
2578 	case INTEL_FAM6_KABYLAKE:
2579 		if (c->x86_cache_bits < 44)
2580 			c->x86_cache_bits = 44;
2581 		break;
2582 	}
2583 }
2584 
l1tf_select_mitigation(void)2585 static void __init l1tf_select_mitigation(void)
2586 {
2587 	u64 half_pa;
2588 
2589 	if (!boot_cpu_has_bug(X86_BUG_L1TF))
2590 		return;
2591 
2592 	if (cpu_mitigations_off())
2593 		l1tf_mitigation = L1TF_MITIGATION_OFF;
2594 	else if (cpu_mitigations_auto_nosmt())
2595 		l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
2596 
2597 	override_cache_bits(&boot_cpu_data);
2598 
2599 	switch (l1tf_mitigation) {
2600 	case L1TF_MITIGATION_OFF:
2601 	case L1TF_MITIGATION_FLUSH_NOWARN:
2602 	case L1TF_MITIGATION_FLUSH:
2603 		break;
2604 	case L1TF_MITIGATION_FLUSH_NOSMT:
2605 	case L1TF_MITIGATION_FULL:
2606 		cpu_smt_disable(false);
2607 		break;
2608 	case L1TF_MITIGATION_FULL_FORCE:
2609 		cpu_smt_disable(true);
2610 		break;
2611 	}
2612 
2613 #if CONFIG_PGTABLE_LEVELS == 2
2614 	pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
2615 	return;
2616 #endif
2617 
2618 	half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT;
2619 	if (l1tf_mitigation != L1TF_MITIGATION_OFF &&
2620 			e820__mapped_any(half_pa, ULLONG_MAX - half_pa, E820_TYPE_RAM)) {
2621 		pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
2622 		pr_info("You may make it effective by booting the kernel with mem=%llu parameter.\n",
2623 				half_pa);
2624 		pr_info("However, doing so will make a part of your RAM unusable.\n");
2625 		pr_info("Reading https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html might help you decide.\n");
2626 		return;
2627 	}
2628 
2629 	setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV);
2630 }
2631 
l1tf_cmdline(char * str)2632 static int __init l1tf_cmdline(char *str)
2633 {
2634 	if (!boot_cpu_has_bug(X86_BUG_L1TF))
2635 		return 0;
2636 
2637 	if (!str)
2638 		return -EINVAL;
2639 
2640 	if (!strcmp(str, "off"))
2641 		l1tf_mitigation = L1TF_MITIGATION_OFF;
2642 	else if (!strcmp(str, "flush,nowarn"))
2643 		l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOWARN;
2644 	else if (!strcmp(str, "flush"))
2645 		l1tf_mitigation = L1TF_MITIGATION_FLUSH;
2646 	else if (!strcmp(str, "flush,nosmt"))
2647 		l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
2648 	else if (!strcmp(str, "full"))
2649 		l1tf_mitigation = L1TF_MITIGATION_FULL;
2650 	else if (!strcmp(str, "full,force"))
2651 		l1tf_mitigation = L1TF_MITIGATION_FULL_FORCE;
2652 
2653 	return 0;
2654 }
2655 early_param("l1tf", l1tf_cmdline);
2656 
2657 #undef pr_fmt
2658 #define pr_fmt(fmt)	"Speculative Return Stack Overflow: " fmt
2659 
2660 enum srso_mitigation {
2661 	SRSO_MITIGATION_NONE,
2662 	SRSO_MITIGATION_UCODE_NEEDED,
2663 	SRSO_MITIGATION_SAFE_RET_UCODE_NEEDED,
2664 	SRSO_MITIGATION_MICROCODE,
2665 	SRSO_MITIGATION_SAFE_RET,
2666 	SRSO_MITIGATION_IBPB,
2667 	SRSO_MITIGATION_IBPB_ON_VMEXIT,
2668 };
2669 
2670 enum srso_mitigation_cmd {
2671 	SRSO_CMD_OFF,
2672 	SRSO_CMD_MICROCODE,
2673 	SRSO_CMD_SAFE_RET,
2674 	SRSO_CMD_IBPB,
2675 	SRSO_CMD_IBPB_ON_VMEXIT,
2676 };
2677 
2678 static const char * const srso_strings[] = {
2679 	[SRSO_MITIGATION_NONE]			= "Vulnerable",
2680 	[SRSO_MITIGATION_UCODE_NEEDED]		= "Vulnerable: No microcode",
2681 	[SRSO_MITIGATION_SAFE_RET_UCODE_NEEDED]	= "Vulnerable: Safe RET, no microcode",
2682 	[SRSO_MITIGATION_MICROCODE]		= "Vulnerable: Microcode, no safe RET",
2683 	[SRSO_MITIGATION_SAFE_RET]		= "Mitigation: Safe RET",
2684 	[SRSO_MITIGATION_IBPB]			= "Mitigation: IBPB",
2685 	[SRSO_MITIGATION_IBPB_ON_VMEXIT]	= "Mitigation: IBPB on VMEXIT only"
2686 };
2687 
2688 static enum srso_mitigation srso_mitigation __ro_after_init = SRSO_MITIGATION_NONE;
2689 static enum srso_mitigation_cmd srso_cmd __ro_after_init = SRSO_CMD_SAFE_RET;
2690 
srso_parse_cmdline(char * str)2691 static int __init srso_parse_cmdline(char *str)
2692 {
2693 	if (!str)
2694 		return -EINVAL;
2695 
2696 	if (!strcmp(str, "off"))
2697 		srso_cmd = SRSO_CMD_OFF;
2698 	else if (!strcmp(str, "microcode"))
2699 		srso_cmd = SRSO_CMD_MICROCODE;
2700 	else if (!strcmp(str, "safe-ret"))
2701 		srso_cmd = SRSO_CMD_SAFE_RET;
2702 	else if (!strcmp(str, "ibpb"))
2703 		srso_cmd = SRSO_CMD_IBPB;
2704 	else if (!strcmp(str, "ibpb-vmexit"))
2705 		srso_cmd = SRSO_CMD_IBPB_ON_VMEXIT;
2706 	else
2707 		pr_err("Ignoring unknown SRSO option (%s).", str);
2708 
2709 	return 0;
2710 }
2711 early_param("spec_rstack_overflow", srso_parse_cmdline);
2712 
2713 #define SRSO_NOTICE "WARNING: See https://kernel.org/doc/html/latest/admin-guide/hw-vuln/srso.html for mitigation options."
2714 
srso_select_mitigation(void)2715 static void __init srso_select_mitigation(void)
2716 {
2717 	bool has_microcode = boot_cpu_has(X86_FEATURE_IBPB_BRTYPE);
2718 
2719 	if (!boot_cpu_has_bug(X86_BUG_SRSO) || cpu_mitigations_off())
2720 		goto pred_cmd;
2721 
2722 	if (has_microcode) {
2723 		/*
2724 		 * Zen1/2 with SMT off aren't vulnerable after the right
2725 		 * IBPB microcode has been applied.
2726 		 */
2727 		if (boot_cpu_data.x86 < 0x19 && !cpu_smt_possible()) {
2728 			setup_force_cpu_cap(X86_FEATURE_SRSO_NO);
2729 			return;
2730 		}
2731 
2732 		if (retbleed_mitigation == RETBLEED_MITIGATION_IBPB) {
2733 			srso_mitigation = SRSO_MITIGATION_IBPB;
2734 			goto out;
2735 		}
2736 	} else {
2737 		pr_warn("IBPB-extending microcode not applied!\n");
2738 		pr_warn(SRSO_NOTICE);
2739 
2740 		/* may be overwritten by SRSO_CMD_SAFE_RET below */
2741 		srso_mitigation = SRSO_MITIGATION_UCODE_NEEDED;
2742 	}
2743 
2744 	switch (srso_cmd) {
2745 	case SRSO_CMD_OFF:
2746 		goto pred_cmd;
2747 
2748 	case SRSO_CMD_MICROCODE:
2749 		if (has_microcode) {
2750 			srso_mitigation = SRSO_MITIGATION_MICROCODE;
2751 			pr_warn(SRSO_NOTICE);
2752 		}
2753 		break;
2754 
2755 	case SRSO_CMD_SAFE_RET:
2756 		if (IS_ENABLED(CONFIG_CPU_SRSO)) {
2757 			/*
2758 			 * Enable the return thunk for generated code
2759 			 * like ftrace, static_call, etc.
2760 			 */
2761 			setup_force_cpu_cap(X86_FEATURE_RETHUNK);
2762 			setup_force_cpu_cap(X86_FEATURE_UNRET);
2763 
2764 			if (boot_cpu_data.x86 == 0x19) {
2765 				setup_force_cpu_cap(X86_FEATURE_SRSO_ALIAS);
2766 				set_return_thunk(srso_alias_return_thunk);
2767 			} else {
2768 				setup_force_cpu_cap(X86_FEATURE_SRSO);
2769 				set_return_thunk(srso_return_thunk);
2770 			}
2771 			if (has_microcode)
2772 				srso_mitigation = SRSO_MITIGATION_SAFE_RET;
2773 			else
2774 				srso_mitigation = SRSO_MITIGATION_SAFE_RET_UCODE_NEEDED;
2775 		} else {
2776 			pr_err("WARNING: kernel not compiled with CPU_SRSO.\n");
2777 			goto pred_cmd;
2778 		}
2779 		break;
2780 
2781 	case SRSO_CMD_IBPB:
2782 		if (IS_ENABLED(CONFIG_CPU_IBPB_ENTRY)) {
2783 			if (has_microcode) {
2784 				setup_force_cpu_cap(X86_FEATURE_ENTRY_IBPB);
2785 				setup_force_cpu_cap(X86_FEATURE_IBPB_ON_VMEXIT);
2786 				srso_mitigation = SRSO_MITIGATION_IBPB;
2787 
2788 				/*
2789 				 * IBPB on entry already obviates the need for
2790 				 * software-based untraining so clear those in case some
2791 				 * other mitigation like Retbleed has selected them.
2792 				 */
2793 				setup_clear_cpu_cap(X86_FEATURE_UNRET);
2794 				setup_clear_cpu_cap(X86_FEATURE_RETHUNK);
2795 
2796 				/*
2797 				 * There is no need for RSB filling: entry_ibpb() ensures
2798 				 * all predictions, including the RSB, are invalidated,
2799 				 * regardless of IBPB implementation.
2800 				 */
2801 				setup_clear_cpu_cap(X86_FEATURE_RSB_VMEXIT);
2802 			}
2803 		} else {
2804 			pr_err("WARNING: kernel not compiled with CPU_IBPB_ENTRY.\n");
2805 			goto pred_cmd;
2806 		}
2807 		break;
2808 
2809 	case SRSO_CMD_IBPB_ON_VMEXIT:
2810 		if (IS_ENABLED(CONFIG_CPU_IBPB_ENTRY)) {
2811 			if (has_microcode) {
2812 				setup_force_cpu_cap(X86_FEATURE_IBPB_ON_VMEXIT);
2813 				srso_mitigation = SRSO_MITIGATION_IBPB_ON_VMEXIT;
2814 
2815 				/*
2816 				 * There is no need for RSB filling: entry_ibpb() ensures
2817 				 * all predictions, including the RSB, are invalidated,
2818 				 * regardless of IBPB implementation.
2819 				 */
2820 				setup_clear_cpu_cap(X86_FEATURE_RSB_VMEXIT);
2821 			}
2822 		} else {
2823 			pr_err("WARNING: kernel not compiled with CPU_IBPB_ENTRY.\n");
2824 			goto pred_cmd;
2825 		}
2826 		break;
2827 
2828 	default:
2829 		break;
2830 	}
2831 
2832 out:
2833 	pr_info("%s\n", srso_strings[srso_mitigation]);
2834 
2835 pred_cmd:
2836 	if ((!boot_cpu_has_bug(X86_BUG_SRSO) || srso_cmd == SRSO_CMD_OFF) &&
2837 	     boot_cpu_has(X86_FEATURE_SBPB))
2838 		x86_pred_cmd = PRED_CMD_SBPB;
2839 }
2840 
2841 #undef pr_fmt
2842 #define pr_fmt(fmt) fmt
2843 
2844 #ifdef CONFIG_SYSFS
2845 
2846 #define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
2847 
2848 #if IS_ENABLED(CONFIG_KVM_INTEL)
2849 static const char * const l1tf_vmx_states[] = {
2850 	[VMENTER_L1D_FLUSH_AUTO]		= "auto",
2851 	[VMENTER_L1D_FLUSH_NEVER]		= "vulnerable",
2852 	[VMENTER_L1D_FLUSH_COND]		= "conditional cache flushes",
2853 	[VMENTER_L1D_FLUSH_ALWAYS]		= "cache flushes",
2854 	[VMENTER_L1D_FLUSH_EPT_DISABLED]	= "EPT disabled",
2855 	[VMENTER_L1D_FLUSH_NOT_REQUIRED]	= "flush not necessary"
2856 };
2857 
l1tf_show_state(char * buf)2858 static ssize_t l1tf_show_state(char *buf)
2859 {
2860 	if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO)
2861 		return sysfs_emit(buf, "%s\n", L1TF_DEFAULT_MSG);
2862 
2863 	if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_EPT_DISABLED ||
2864 	    (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER &&
2865 	     sched_smt_active())) {
2866 		return sysfs_emit(buf, "%s; VMX: %s\n", L1TF_DEFAULT_MSG,
2867 				  l1tf_vmx_states[l1tf_vmx_mitigation]);
2868 	}
2869 
2870 	return sysfs_emit(buf, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG,
2871 			  l1tf_vmx_states[l1tf_vmx_mitigation],
2872 			  sched_smt_active() ? "vulnerable" : "disabled");
2873 }
2874 
itlb_multihit_show_state(char * buf)2875 static ssize_t itlb_multihit_show_state(char *buf)
2876 {
2877 	if (!boot_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
2878 	    !boot_cpu_has(X86_FEATURE_VMX))
2879 		return sysfs_emit(buf, "KVM: Mitigation: VMX unsupported\n");
2880 	else if (!(cr4_read_shadow() & X86_CR4_VMXE))
2881 		return sysfs_emit(buf, "KVM: Mitigation: VMX disabled\n");
2882 	else if (itlb_multihit_kvm_mitigation)
2883 		return sysfs_emit(buf, "KVM: Mitigation: Split huge pages\n");
2884 	else
2885 		return sysfs_emit(buf, "KVM: Vulnerable\n");
2886 }
2887 #else
l1tf_show_state(char * buf)2888 static ssize_t l1tf_show_state(char *buf)
2889 {
2890 	return sysfs_emit(buf, "%s\n", L1TF_DEFAULT_MSG);
2891 }
2892 
itlb_multihit_show_state(char * buf)2893 static ssize_t itlb_multihit_show_state(char *buf)
2894 {
2895 	return sysfs_emit(buf, "Processor vulnerable\n");
2896 }
2897 #endif
2898 
mds_show_state(char * buf)2899 static ssize_t mds_show_state(char *buf)
2900 {
2901 	if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
2902 		return sysfs_emit(buf, "%s; SMT Host state unknown\n",
2903 				  mds_strings[mds_mitigation]);
2904 	}
2905 
2906 	if (boot_cpu_has(X86_BUG_MSBDS_ONLY)) {
2907 		return sysfs_emit(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
2908 				  (mds_mitigation == MDS_MITIGATION_OFF ? "vulnerable" :
2909 				   sched_smt_active() ? "mitigated" : "disabled"));
2910 	}
2911 
2912 	return sysfs_emit(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
2913 			  sched_smt_active() ? "vulnerable" : "disabled");
2914 }
2915 
tsx_async_abort_show_state(char * buf)2916 static ssize_t tsx_async_abort_show_state(char *buf)
2917 {
2918 	if ((taa_mitigation == TAA_MITIGATION_TSX_DISABLED) ||
2919 	    (taa_mitigation == TAA_MITIGATION_OFF))
2920 		return sysfs_emit(buf, "%s\n", taa_strings[taa_mitigation]);
2921 
2922 	if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
2923 		return sysfs_emit(buf, "%s; SMT Host state unknown\n",
2924 				  taa_strings[taa_mitigation]);
2925 	}
2926 
2927 	return sysfs_emit(buf, "%s; SMT %s\n", taa_strings[taa_mitigation],
2928 			  sched_smt_active() ? "vulnerable" : "disabled");
2929 }
2930 
mmio_stale_data_show_state(char * buf)2931 static ssize_t mmio_stale_data_show_state(char *buf)
2932 {
2933 	if (boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN))
2934 		return sysfs_emit(buf, "Unknown: No mitigations\n");
2935 
2936 	if (mmio_mitigation == MMIO_MITIGATION_OFF)
2937 		return sysfs_emit(buf, "%s\n", mmio_strings[mmio_mitigation]);
2938 
2939 	if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
2940 		return sysfs_emit(buf, "%s; SMT Host state unknown\n",
2941 				  mmio_strings[mmio_mitigation]);
2942 	}
2943 
2944 	return sysfs_emit(buf, "%s; SMT %s\n", mmio_strings[mmio_mitigation],
2945 			  sched_smt_active() ? "vulnerable" : "disabled");
2946 }
2947 
rfds_show_state(char * buf)2948 static ssize_t rfds_show_state(char *buf)
2949 {
2950 	return sysfs_emit(buf, "%s\n", rfds_strings[rfds_mitigation]);
2951 }
2952 
its_show_state(char * buf)2953 static ssize_t its_show_state(char *buf)
2954 {
2955 	return sysfs_emit(buf, "%s\n", its_strings[its_mitigation]);
2956 }
2957 
stibp_state(void)2958 static char *stibp_state(void)
2959 {
2960 	if (spectre_v2_in_eibrs_mode(spectre_v2_enabled) &&
2961 	    !boot_cpu_has(X86_FEATURE_AUTOIBRS))
2962 		return "";
2963 
2964 	switch (spectre_v2_user_stibp) {
2965 	case SPECTRE_V2_USER_NONE:
2966 		return "; STIBP: disabled";
2967 	case SPECTRE_V2_USER_STRICT:
2968 		return "; STIBP: forced";
2969 	case SPECTRE_V2_USER_STRICT_PREFERRED:
2970 		return "; STIBP: always-on";
2971 	case SPECTRE_V2_USER_PRCTL:
2972 	case SPECTRE_V2_USER_SECCOMP:
2973 		if (static_key_enabled(&switch_to_cond_stibp))
2974 			return "; STIBP: conditional";
2975 	}
2976 	return "";
2977 }
2978 
ibpb_state(void)2979 static char *ibpb_state(void)
2980 {
2981 	if (boot_cpu_has(X86_FEATURE_IBPB)) {
2982 		if (static_key_enabled(&switch_mm_always_ibpb))
2983 			return "; IBPB: always-on";
2984 		if (static_key_enabled(&switch_mm_cond_ibpb))
2985 			return "; IBPB: conditional";
2986 		return "; IBPB: disabled";
2987 	}
2988 	return "";
2989 }
2990 
pbrsb_eibrs_state(void)2991 static char *pbrsb_eibrs_state(void)
2992 {
2993 	if (boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB)) {
2994 		if (boot_cpu_has(X86_FEATURE_RSB_VMEXIT_LITE) ||
2995 		    boot_cpu_has(X86_FEATURE_RSB_VMEXIT))
2996 			return "; PBRSB-eIBRS: SW sequence";
2997 		else
2998 			return "; PBRSB-eIBRS: Vulnerable";
2999 	} else {
3000 		return "; PBRSB-eIBRS: Not affected";
3001 	}
3002 }
3003 
spectre_bhi_state(void)3004 static const char *spectre_bhi_state(void)
3005 {
3006 	if (!boot_cpu_has_bug(X86_BUG_BHI))
3007 		return "; BHI: Not affected";
3008 	else if (boot_cpu_has(X86_FEATURE_CLEAR_BHB_HW))
3009 		return "; BHI: BHI_DIS_S";
3010 	else if (boot_cpu_has(X86_FEATURE_CLEAR_BHB_LOOP))
3011 		return "; BHI: SW loop, KVM: SW loop";
3012 	else if (boot_cpu_has(X86_FEATURE_RETPOLINE) &&
3013 		 !boot_cpu_has(X86_FEATURE_RETPOLINE_LFENCE) &&
3014 		 rrsba_disabled)
3015 		return "; BHI: Retpoline";
3016 	else if (boot_cpu_has(X86_FEATURE_CLEAR_BHB_LOOP_ON_VMEXIT))
3017 		return "; BHI: Vulnerable, KVM: SW loop";
3018 
3019 	return "; BHI: Vulnerable";
3020 }
3021 
spectre_v2_show_state(char * buf)3022 static ssize_t spectre_v2_show_state(char *buf)
3023 {
3024 	if (spectre_v2_enabled == SPECTRE_V2_LFENCE)
3025 		return sysfs_emit(buf, "Vulnerable: LFENCE\n");
3026 
3027 	if (spectre_v2_enabled == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
3028 		return sysfs_emit(buf, "Vulnerable: eIBRS with unprivileged eBPF\n");
3029 
3030 	if (sched_smt_active() && unprivileged_ebpf_enabled() &&
3031 	    spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
3032 		return sysfs_emit(buf, "Vulnerable: eIBRS+LFENCE with unprivileged eBPF and SMT\n");
3033 
3034 	return sysfs_emit(buf, "%s%s%s%s%s%s%s%s\n",
3035 			  spectre_v2_strings[spectre_v2_enabled],
3036 			  ibpb_state(),
3037 			  boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? "; IBRS_FW" : "",
3038 			  stibp_state(),
3039 			  boot_cpu_has(X86_FEATURE_RSB_CTXSW) ? "; RSB filling" : "",
3040 			  pbrsb_eibrs_state(),
3041 			  spectre_bhi_state(),
3042 			  /* this should always be at the end */
3043 			  spectre_v2_module_string());
3044 }
3045 
srbds_show_state(char * buf)3046 static ssize_t srbds_show_state(char *buf)
3047 {
3048 	return sysfs_emit(buf, "%s\n", srbds_strings[srbds_mitigation]);
3049 }
3050 
retbleed_show_state(char * buf)3051 static ssize_t retbleed_show_state(char *buf)
3052 {
3053 	if (retbleed_mitigation == RETBLEED_MITIGATION_UNRET ||
3054 	    retbleed_mitigation == RETBLEED_MITIGATION_IBPB) {
3055 		if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
3056 		    boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)
3057 			return sysfs_emit(buf, "Vulnerable: untrained return thunk / IBPB on non-AMD based uarch\n");
3058 
3059 		return sysfs_emit(buf, "%s; SMT %s\n", retbleed_strings[retbleed_mitigation],
3060 				  !sched_smt_active() ? "disabled" :
3061 				  spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
3062 				  spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED ?
3063 				  "enabled with STIBP protection" : "vulnerable");
3064 	}
3065 
3066 	return sysfs_emit(buf, "%s\n", retbleed_strings[retbleed_mitigation]);
3067 }
3068 
srso_show_state(char * buf)3069 static ssize_t srso_show_state(char *buf)
3070 {
3071 	if (boot_cpu_has(X86_FEATURE_SRSO_NO))
3072 		return sysfs_emit(buf, "Mitigation: SMT disabled\n");
3073 
3074 	return sysfs_emit(buf, "%s\n", srso_strings[srso_mitigation]);
3075 }
3076 
gds_show_state(char * buf)3077 static ssize_t gds_show_state(char *buf)
3078 {
3079 	return sysfs_emit(buf, "%s\n", gds_strings[gds_mitigation]);
3080 }
3081 
cpu_show_common(struct device * dev,struct device_attribute * attr,char * buf,unsigned int bug)3082 static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
3083 			       char *buf, unsigned int bug)
3084 {
3085 	if (!boot_cpu_has_bug(bug))
3086 		return sysfs_emit(buf, "Not affected\n");
3087 
3088 	switch (bug) {
3089 	case X86_BUG_CPU_MELTDOWN:
3090 		if (boot_cpu_has(X86_FEATURE_PTI))
3091 			return sysfs_emit(buf, "Mitigation: PTI\n");
3092 
3093 		if (hypervisor_is_type(X86_HYPER_XEN_PV))
3094 			return sysfs_emit(buf, "Unknown (XEN PV detected, hypervisor mitigation required)\n");
3095 
3096 		break;
3097 
3098 	case X86_BUG_SPECTRE_V1:
3099 		return sysfs_emit(buf, "%s\n", spectre_v1_strings[spectre_v1_mitigation]);
3100 
3101 	case X86_BUG_SPECTRE_V2:
3102 		return spectre_v2_show_state(buf);
3103 
3104 	case X86_BUG_SPEC_STORE_BYPASS:
3105 		return sysfs_emit(buf, "%s\n", ssb_strings[ssb_mode]);
3106 
3107 	case X86_BUG_L1TF:
3108 		if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV))
3109 			return l1tf_show_state(buf);
3110 		break;
3111 
3112 	case X86_BUG_MDS:
3113 		return mds_show_state(buf);
3114 
3115 	case X86_BUG_TAA:
3116 		return tsx_async_abort_show_state(buf);
3117 
3118 	case X86_BUG_ITLB_MULTIHIT:
3119 		return itlb_multihit_show_state(buf);
3120 
3121 	case X86_BUG_SRBDS:
3122 		return srbds_show_state(buf);
3123 
3124 	case X86_BUG_MMIO_STALE_DATA:
3125 	case X86_BUG_MMIO_UNKNOWN:
3126 		return mmio_stale_data_show_state(buf);
3127 
3128 	case X86_BUG_RETBLEED:
3129 		return retbleed_show_state(buf);
3130 
3131 	case X86_BUG_SRSO:
3132 		return srso_show_state(buf);
3133 
3134 	case X86_BUG_GDS:
3135 		return gds_show_state(buf);
3136 
3137 	case X86_BUG_RFDS:
3138 		return rfds_show_state(buf);
3139 
3140 	case X86_BUG_ITS:
3141 		return its_show_state(buf);
3142 
3143 	default:
3144 		break;
3145 	}
3146 
3147 	return sysfs_emit(buf, "Vulnerable\n");
3148 }
3149 
cpu_show_meltdown(struct device * dev,struct device_attribute * attr,char * buf)3150 ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
3151 {
3152 	return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
3153 }
3154 
cpu_show_spectre_v1(struct device * dev,struct device_attribute * attr,char * buf)3155 ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
3156 {
3157 	return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
3158 }
3159 
cpu_show_spectre_v2(struct device * dev,struct device_attribute * attr,char * buf)3160 ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
3161 {
3162 	return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
3163 }
3164 
cpu_show_spec_store_bypass(struct device * dev,struct device_attribute * attr,char * buf)3165 ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
3166 {
3167 	return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS);
3168 }
3169 
cpu_show_l1tf(struct device * dev,struct device_attribute * attr,char * buf)3170 ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
3171 {
3172 	return cpu_show_common(dev, attr, buf, X86_BUG_L1TF);
3173 }
3174 
cpu_show_mds(struct device * dev,struct device_attribute * attr,char * buf)3175 ssize_t cpu_show_mds(struct device *dev, struct device_attribute *attr, char *buf)
3176 {
3177 	return cpu_show_common(dev, attr, buf, X86_BUG_MDS);
3178 }
3179 
cpu_show_tsx_async_abort(struct device * dev,struct device_attribute * attr,char * buf)3180 ssize_t cpu_show_tsx_async_abort(struct device *dev, struct device_attribute *attr, char *buf)
3181 {
3182 	return cpu_show_common(dev, attr, buf, X86_BUG_TAA);
3183 }
3184 
cpu_show_itlb_multihit(struct device * dev,struct device_attribute * attr,char * buf)3185 ssize_t cpu_show_itlb_multihit(struct device *dev, struct device_attribute *attr, char *buf)
3186 {
3187 	return cpu_show_common(dev, attr, buf, X86_BUG_ITLB_MULTIHIT);
3188 }
3189 
cpu_show_srbds(struct device * dev,struct device_attribute * attr,char * buf)3190 ssize_t cpu_show_srbds(struct device *dev, struct device_attribute *attr, char *buf)
3191 {
3192 	return cpu_show_common(dev, attr, buf, X86_BUG_SRBDS);
3193 }
3194 
cpu_show_mmio_stale_data(struct device * dev,struct device_attribute * attr,char * buf)3195 ssize_t cpu_show_mmio_stale_data(struct device *dev, struct device_attribute *attr, char *buf)
3196 {
3197 	if (boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN))
3198 		return cpu_show_common(dev, attr, buf, X86_BUG_MMIO_UNKNOWN);
3199 	else
3200 		return cpu_show_common(dev, attr, buf, X86_BUG_MMIO_STALE_DATA);
3201 }
3202 
cpu_show_retbleed(struct device * dev,struct device_attribute * attr,char * buf)3203 ssize_t cpu_show_retbleed(struct device *dev, struct device_attribute *attr, char *buf)
3204 {
3205 	return cpu_show_common(dev, attr, buf, X86_BUG_RETBLEED);
3206 }
3207 
cpu_show_spec_rstack_overflow(struct device * dev,struct device_attribute * attr,char * buf)3208 ssize_t cpu_show_spec_rstack_overflow(struct device *dev, struct device_attribute *attr, char *buf)
3209 {
3210 	return cpu_show_common(dev, attr, buf, X86_BUG_SRSO);
3211 }
3212 
cpu_show_gds(struct device * dev,struct device_attribute * attr,char * buf)3213 ssize_t cpu_show_gds(struct device *dev, struct device_attribute *attr, char *buf)
3214 {
3215 	return cpu_show_common(dev, attr, buf, X86_BUG_GDS);
3216 }
3217 
cpu_show_reg_file_data_sampling(struct device * dev,struct device_attribute * attr,char * buf)3218 ssize_t cpu_show_reg_file_data_sampling(struct device *dev, struct device_attribute *attr, char *buf)
3219 {
3220 	return cpu_show_common(dev, attr, buf, X86_BUG_RFDS);
3221 }
3222 
cpu_show_indirect_target_selection(struct device * dev,struct device_attribute * attr,char * buf)3223 ssize_t cpu_show_indirect_target_selection(struct device *dev, struct device_attribute *attr, char *buf)
3224 {
3225 	return cpu_show_common(dev, attr, buf, X86_BUG_ITS);
3226 }
3227 #endif
3228