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