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