• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Copyright (C) 1994  Linus Torvalds
4  *
5  *  Cyrix stuff, June 1998 by:
6  *	- Rafael R. Reilova (moved everything from head.S),
7  *        <rreilova@ececs.uc.edu>
8  *	- Channing Corn (tests & fixes),
9  *	- Andrew D. Balsa (code cleanup).
10  */
11 #include <linux/init.h>
12 #include <linux/utsname.h>
13 #include <linux/cpu.h>
14 #include <linux/module.h>
15 #include <linux/nospec.h>
16 #include <linux/prctl.h>
17 #include <linux/sched/smt.h>
18 #include <linux/pgtable.h>
19 #include <linux/bpf.h>
20 
21 #include <asm/spec-ctrl.h>
22 #include <asm/cmdline.h>
23 #include <asm/bugs.h>
24 #include <asm/processor.h>
25 #include <asm/processor-flags.h>
26 #include <asm/fpu/internal.h>
27 #include <asm/msr.h>
28 #include <asm/vmx.h>
29 #include <asm/paravirt.h>
30 #include <asm/alternative.h>
31 #include <asm/set_memory.h>
32 #include <asm/intel-family.h>
33 #include <asm/e820/api.h>
34 #include <asm/hypervisor.h>
35 #include <asm/tlbflush.h>
36 
37 #include "cpu.h"
38 
39 static void __init spectre_v1_select_mitigation(void);
40 static void __init spectre_v2_select_mitigation(void);
41 static void __init retbleed_select_mitigation(void);
42 static void __init spectre_v2_user_select_mitigation(void);
43 static void __init ssb_select_mitigation(void);
44 static void __init l1tf_select_mitigation(void);
45 static void __init mds_select_mitigation(void);
46 static void __init md_clear_update_mitigation(void);
47 static void __init md_clear_select_mitigation(void);
48 static void __init taa_select_mitigation(void);
49 static void __init mmio_select_mitigation(void);
50 static void __init srbds_select_mitigation(void);
51 
52 /* The base value of the SPEC_CTRL MSR without task-specific bits set */
53 u64 x86_spec_ctrl_base;
54 EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
55 
56 /* The current value of the SPEC_CTRL MSR with task-specific bits set */
57 DEFINE_PER_CPU(u64, x86_spec_ctrl_current);
58 EXPORT_SYMBOL_GPL(x86_spec_ctrl_current);
59 
60 static DEFINE_MUTEX(spec_ctrl_mutex);
61 
62 /*
63  * Keep track of the SPEC_CTRL MSR value for the current task, which may differ
64  * from x86_spec_ctrl_base due to STIBP/SSB in __speculation_ctrl_update().
65  */
write_spec_ctrl_current(u64 val,bool force)66 void write_spec_ctrl_current(u64 val, bool force)
67 {
68 	if (this_cpu_read(x86_spec_ctrl_current) == val)
69 		return;
70 
71 	this_cpu_write(x86_spec_ctrl_current, val);
72 
73 	/*
74 	 * When KERNEL_IBRS this MSR is written on return-to-user, unless
75 	 * forced the update can be delayed until that time.
76 	 */
77 	if (force || !cpu_feature_enabled(X86_FEATURE_KERNEL_IBRS))
78 		wrmsrl(MSR_IA32_SPEC_CTRL, val);
79 }
80 
spec_ctrl_current(void)81 u64 spec_ctrl_current(void)
82 {
83 	return this_cpu_read(x86_spec_ctrl_current);
84 }
85 EXPORT_SYMBOL_GPL(spec_ctrl_current);
86 
87 /*
88  * AMD specific MSR info for Speculative Store Bypass control.
89  * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu().
90  */
91 u64 __ro_after_init x86_amd_ls_cfg_base;
92 u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask;
93 
94 /* Control conditional STIBP in switch_to() */
95 DEFINE_STATIC_KEY_FALSE(switch_to_cond_stibp);
96 /* Control conditional IBPB in switch_mm() */
97 DEFINE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
98 /* Control unconditional IBPB in switch_mm() */
99 DEFINE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
100 
101 /* Control MDS CPU buffer clear before returning to user space */
102 DEFINE_STATIC_KEY_FALSE(mds_user_clear);
103 EXPORT_SYMBOL_GPL(mds_user_clear);
104 /* Control MDS CPU buffer clear before idling (halt, mwait) */
105 DEFINE_STATIC_KEY_FALSE(mds_idle_clear);
106 EXPORT_SYMBOL_GPL(mds_idle_clear);
107 
108 /* Controls CPU Fill buffer clear before KVM guest MMIO accesses */
109 DEFINE_STATIC_KEY_FALSE(mmio_stale_data_clear);
110 EXPORT_SYMBOL_GPL(mmio_stale_data_clear);
111 
check_bugs(void)112 void __init check_bugs(void)
113 {
114 	identify_boot_cpu();
115 
116 	/*
117 	 * identify_boot_cpu() initialized SMT support information, let the
118 	 * core code know.
119 	 */
120 	cpu_smt_check_topology();
121 
122 	if (!IS_ENABLED(CONFIG_SMP)) {
123 		pr_info("CPU: ");
124 		print_cpu_info(&boot_cpu_data);
125 	}
126 
127 	/*
128 	 * Read the SPEC_CTRL MSR to account for reserved bits which may
129 	 * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
130 	 * init code as it is not enumerated and depends on the family.
131 	 */
132 	if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
133 		rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
134 
135 	/* Select the proper CPU mitigations before patching alternatives: */
136 	spectre_v1_select_mitigation();
137 	spectre_v2_select_mitigation();
138 	/*
139 	 * retbleed_select_mitigation() relies on the state set by
140 	 * spectre_v2_select_mitigation(); specifically it wants to know about
141 	 * spectre_v2=ibrs.
142 	 */
143 	retbleed_select_mitigation();
144 	/*
145 	 * spectre_v2_user_select_mitigation() relies on the state set by
146 	 * retbleed_select_mitigation(); specifically the STIBP selection is
147 	 * forced for UNRET.
148 	 */
149 	spectre_v2_user_select_mitigation();
150 	ssb_select_mitigation();
151 	l1tf_select_mitigation();
152 	md_clear_select_mitigation();
153 	srbds_select_mitigation();
154 
155 	arch_smt_update();
156 
157 #ifdef CONFIG_X86_32
158 	/*
159 	 * Check whether we are able to run this kernel safely on SMP.
160 	 *
161 	 * - i386 is no longer supported.
162 	 * - In order to run on anything without a TSC, we need to be
163 	 *   compiled for a i486.
164 	 */
165 	if (boot_cpu_data.x86 < 4)
166 		panic("Kernel requires i486+ for 'invlpg' and other features");
167 
168 	init_utsname()->machine[1] =
169 		'0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
170 	alternative_instructions();
171 
172 	fpu__init_check_bugs();
173 #else /* CONFIG_X86_64 */
174 	alternative_instructions();
175 
176 	/*
177 	 * Make sure the first 2MB area is not mapped by huge pages
178 	 * There are typically fixed size MTRRs in there and overlapping
179 	 * MTRRs into large pages causes slow downs.
180 	 *
181 	 * Right now we don't do that with gbpages because there seems
182 	 * very little benefit for that case.
183 	 */
184 	if (!direct_gbpages)
185 		set_memory_4k((unsigned long)__va(0), 1);
186 #endif
187 }
188 
189 /*
190  * NOTE: For VMX, this function is not called in the vmexit path.
191  * It uses vmx_spec_ctrl_restore_host() instead.
192  */
193 void
x86_virt_spec_ctrl(u64 guest_spec_ctrl,u64 guest_virt_spec_ctrl,bool setguest)194 x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest)
195 {
196 	u64 msrval, guestval = guest_spec_ctrl, hostval = spec_ctrl_current();
197 	struct thread_info *ti = current_thread_info();
198 
199 	if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) {
200 		if (hostval != guestval) {
201 			msrval = setguest ? guestval : hostval;
202 			wrmsrl(MSR_IA32_SPEC_CTRL, msrval);
203 		}
204 	}
205 
206 	/*
207 	 * If SSBD is not handled in MSR_SPEC_CTRL on AMD, update
208 	 * MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported.
209 	 */
210 	if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
211 	    !static_cpu_has(X86_FEATURE_VIRT_SSBD))
212 		return;
213 
214 	/*
215 	 * If the host has SSBD mitigation enabled, force it in the host's
216 	 * virtual MSR value. If its not permanently enabled, evaluate
217 	 * current's TIF_SSBD thread flag.
218 	 */
219 	if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE))
220 		hostval = SPEC_CTRL_SSBD;
221 	else
222 		hostval = ssbd_tif_to_spec_ctrl(ti->flags);
223 
224 	/* Sanitize the guest value */
225 	guestval = guest_virt_spec_ctrl & SPEC_CTRL_SSBD;
226 
227 	if (hostval != guestval) {
228 		unsigned long tif;
229 
230 		tif = setguest ? ssbd_spec_ctrl_to_tif(guestval) :
231 				 ssbd_spec_ctrl_to_tif(hostval);
232 
233 		speculation_ctrl_update(tif);
234 	}
235 }
236 EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl);
237 
x86_amd_ssb_disable(void)238 static void x86_amd_ssb_disable(void)
239 {
240 	u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_ssbd_mask;
241 
242 	if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
243 		wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, SPEC_CTRL_SSBD);
244 	else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
245 		wrmsrl(MSR_AMD64_LS_CFG, msrval);
246 }
247 
248 #undef pr_fmt
249 #define pr_fmt(fmt)	"MDS: " fmt
250 
251 /* Default mitigation for MDS-affected CPUs */
252 static enum mds_mitigations mds_mitigation __ro_after_init = MDS_MITIGATION_FULL;
253 static bool mds_nosmt __ro_after_init = false;
254 
255 static const char * const mds_strings[] = {
256 	[MDS_MITIGATION_OFF]	= "Vulnerable",
257 	[MDS_MITIGATION_FULL]	= "Mitigation: Clear CPU buffers",
258 	[MDS_MITIGATION_VMWERV]	= "Vulnerable: Clear CPU buffers attempted, no microcode",
259 };
260 
mds_select_mitigation(void)261 static void __init mds_select_mitigation(void)
262 {
263 	if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off()) {
264 		mds_mitigation = MDS_MITIGATION_OFF;
265 		return;
266 	}
267 
268 	if (mds_mitigation == MDS_MITIGATION_FULL) {
269 		if (!boot_cpu_has(X86_FEATURE_MD_CLEAR))
270 			mds_mitigation = MDS_MITIGATION_VMWERV;
271 
272 		static_branch_enable(&mds_user_clear);
273 
274 		if (!boot_cpu_has(X86_BUG_MSBDS_ONLY) &&
275 		    (mds_nosmt || cpu_mitigations_auto_nosmt()))
276 			cpu_smt_disable(false);
277 	}
278 }
279 
mds_cmdline(char * str)280 static int __init mds_cmdline(char *str)
281 {
282 	if (!boot_cpu_has_bug(X86_BUG_MDS))
283 		return 0;
284 
285 	if (!str)
286 		return -EINVAL;
287 
288 	if (!strcmp(str, "off"))
289 		mds_mitigation = MDS_MITIGATION_OFF;
290 	else if (!strcmp(str, "full"))
291 		mds_mitigation = MDS_MITIGATION_FULL;
292 	else if (!strcmp(str, "full,nosmt")) {
293 		mds_mitigation = MDS_MITIGATION_FULL;
294 		mds_nosmt = true;
295 	}
296 
297 	return 0;
298 }
299 early_param("mds", mds_cmdline);
300 
301 #undef pr_fmt
302 #define pr_fmt(fmt)	"TAA: " fmt
303 
304 enum taa_mitigations {
305 	TAA_MITIGATION_OFF,
306 	TAA_MITIGATION_UCODE_NEEDED,
307 	TAA_MITIGATION_VERW,
308 	TAA_MITIGATION_TSX_DISABLED,
309 };
310 
311 /* Default mitigation for TAA-affected CPUs */
312 static enum taa_mitigations taa_mitigation __ro_after_init = TAA_MITIGATION_VERW;
313 static bool taa_nosmt __ro_after_init;
314 
315 static const char * const taa_strings[] = {
316 	[TAA_MITIGATION_OFF]		= "Vulnerable",
317 	[TAA_MITIGATION_UCODE_NEEDED]	= "Vulnerable: Clear CPU buffers attempted, no microcode",
318 	[TAA_MITIGATION_VERW]		= "Mitigation: Clear CPU buffers",
319 	[TAA_MITIGATION_TSX_DISABLED]	= "Mitigation: TSX disabled",
320 };
321 
taa_select_mitigation(void)322 static void __init taa_select_mitigation(void)
323 {
324 	u64 ia32_cap;
325 
326 	if (!boot_cpu_has_bug(X86_BUG_TAA)) {
327 		taa_mitigation = TAA_MITIGATION_OFF;
328 		return;
329 	}
330 
331 	/* TSX previously disabled by tsx=off */
332 	if (!boot_cpu_has(X86_FEATURE_RTM)) {
333 		taa_mitigation = TAA_MITIGATION_TSX_DISABLED;
334 		return;
335 	}
336 
337 	if (cpu_mitigations_off()) {
338 		taa_mitigation = TAA_MITIGATION_OFF;
339 		return;
340 	}
341 
342 	/*
343 	 * TAA mitigation via VERW is turned off if both
344 	 * tsx_async_abort=off and mds=off are specified.
345 	 */
346 	if (taa_mitigation == TAA_MITIGATION_OFF &&
347 	    mds_mitigation == MDS_MITIGATION_OFF)
348 		return;
349 
350 	if (boot_cpu_has(X86_FEATURE_MD_CLEAR))
351 		taa_mitigation = TAA_MITIGATION_VERW;
352 	else
353 		taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
354 
355 	/*
356 	 * VERW doesn't clear the CPU buffers when MD_CLEAR=1 and MDS_NO=1.
357 	 * A microcode update fixes this behavior to clear CPU buffers. It also
358 	 * adds support for MSR_IA32_TSX_CTRL which is enumerated by the
359 	 * ARCH_CAP_TSX_CTRL_MSR bit.
360 	 *
361 	 * On MDS_NO=1 CPUs if ARCH_CAP_TSX_CTRL_MSR is not set, microcode
362 	 * update is required.
363 	 */
364 	ia32_cap = x86_read_arch_cap_msr();
365 	if ( (ia32_cap & ARCH_CAP_MDS_NO) &&
366 	    !(ia32_cap & ARCH_CAP_TSX_CTRL_MSR))
367 		taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
368 
369 	/*
370 	 * TSX is enabled, select alternate mitigation for TAA which is
371 	 * the same as MDS. Enable MDS static branch to clear CPU buffers.
372 	 *
373 	 * For guests that can't determine whether the correct microcode is
374 	 * present on host, enable the mitigation for UCODE_NEEDED as well.
375 	 */
376 	static_branch_enable(&mds_user_clear);
377 
378 	if (taa_nosmt || cpu_mitigations_auto_nosmt())
379 		cpu_smt_disable(false);
380 }
381 
tsx_async_abort_parse_cmdline(char * str)382 static int __init tsx_async_abort_parse_cmdline(char *str)
383 {
384 	if (!boot_cpu_has_bug(X86_BUG_TAA))
385 		return 0;
386 
387 	if (!str)
388 		return -EINVAL;
389 
390 	if (!strcmp(str, "off")) {
391 		taa_mitigation = TAA_MITIGATION_OFF;
392 	} else if (!strcmp(str, "full")) {
393 		taa_mitigation = TAA_MITIGATION_VERW;
394 	} else if (!strcmp(str, "full,nosmt")) {
395 		taa_mitigation = TAA_MITIGATION_VERW;
396 		taa_nosmt = true;
397 	}
398 
399 	return 0;
400 }
401 early_param("tsx_async_abort", tsx_async_abort_parse_cmdline);
402 
403 #undef pr_fmt
404 #define pr_fmt(fmt)	"MMIO Stale Data: " fmt
405 
406 enum mmio_mitigations {
407 	MMIO_MITIGATION_OFF,
408 	MMIO_MITIGATION_UCODE_NEEDED,
409 	MMIO_MITIGATION_VERW,
410 };
411 
412 /* Default mitigation for Processor MMIO Stale Data vulnerabilities */
413 static enum mmio_mitigations mmio_mitigation __ro_after_init = MMIO_MITIGATION_VERW;
414 static bool mmio_nosmt __ro_after_init = false;
415 
416 static const char * const mmio_strings[] = {
417 	[MMIO_MITIGATION_OFF]		= "Vulnerable",
418 	[MMIO_MITIGATION_UCODE_NEEDED]	= "Vulnerable: Clear CPU buffers attempted, no microcode",
419 	[MMIO_MITIGATION_VERW]		= "Mitigation: Clear CPU buffers",
420 };
421 
mmio_select_mitigation(void)422 static void __init mmio_select_mitigation(void)
423 {
424 	u64 ia32_cap;
425 
426 	if (!boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA) ||
427 	    cpu_mitigations_off()) {
428 		mmio_mitigation = MMIO_MITIGATION_OFF;
429 		return;
430 	}
431 
432 	if (mmio_mitigation == MMIO_MITIGATION_OFF)
433 		return;
434 
435 	ia32_cap = x86_read_arch_cap_msr();
436 
437 	/*
438 	 * Enable CPU buffer clear mitigation for host and VMM, if also affected
439 	 * by MDS or TAA. Otherwise, enable mitigation for VMM only.
440 	 */
441 	if (boot_cpu_has_bug(X86_BUG_MDS) || (boot_cpu_has_bug(X86_BUG_TAA) &&
442 					      boot_cpu_has(X86_FEATURE_RTM)))
443 		static_branch_enable(&mds_user_clear);
444 	else
445 		static_branch_enable(&mmio_stale_data_clear);
446 
447 	/*
448 	 * If Processor-MMIO-Stale-Data bug is present and Fill Buffer data can
449 	 * be propagated to uncore buffers, clearing the Fill buffers on idle
450 	 * is required irrespective of SMT state.
451 	 */
452 	if (!(ia32_cap & ARCH_CAP_FBSDP_NO))
453 		static_branch_enable(&mds_idle_clear);
454 
455 	/*
456 	 * Check if the system has the right microcode.
457 	 *
458 	 * CPU Fill buffer clear mitigation is enumerated by either an explicit
459 	 * FB_CLEAR or by the presence of both MD_CLEAR and L1D_FLUSH on MDS
460 	 * affected systems.
461 	 */
462 	if ((ia32_cap & ARCH_CAP_FB_CLEAR) ||
463 	    (boot_cpu_has(X86_FEATURE_MD_CLEAR) &&
464 	     boot_cpu_has(X86_FEATURE_FLUSH_L1D) &&
465 	     !(ia32_cap & ARCH_CAP_MDS_NO)))
466 		mmio_mitigation = MMIO_MITIGATION_VERW;
467 	else
468 		mmio_mitigation = MMIO_MITIGATION_UCODE_NEEDED;
469 
470 	if (mmio_nosmt || cpu_mitigations_auto_nosmt())
471 		cpu_smt_disable(false);
472 }
473 
mmio_stale_data_parse_cmdline(char * str)474 static int __init mmio_stale_data_parse_cmdline(char *str)
475 {
476 	if (!boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA))
477 		return 0;
478 
479 	if (!str)
480 		return -EINVAL;
481 
482 	if (!strcmp(str, "off")) {
483 		mmio_mitigation = MMIO_MITIGATION_OFF;
484 	} else if (!strcmp(str, "full")) {
485 		mmio_mitigation = MMIO_MITIGATION_VERW;
486 	} else if (!strcmp(str, "full,nosmt")) {
487 		mmio_mitigation = MMIO_MITIGATION_VERW;
488 		mmio_nosmt = true;
489 	}
490 
491 	return 0;
492 }
493 early_param("mmio_stale_data", mmio_stale_data_parse_cmdline);
494 
495 #undef pr_fmt
496 #define pr_fmt(fmt)     "" fmt
497 
md_clear_update_mitigation(void)498 static void __init md_clear_update_mitigation(void)
499 {
500 	if (cpu_mitigations_off())
501 		return;
502 
503 	if (!static_key_enabled(&mds_user_clear))
504 		goto out;
505 
506 	/*
507 	 * mds_user_clear is now enabled. Update MDS, TAA and MMIO Stale Data
508 	 * mitigation, if necessary.
509 	 */
510 	if (mds_mitigation == MDS_MITIGATION_OFF &&
511 	    boot_cpu_has_bug(X86_BUG_MDS)) {
512 		mds_mitigation = MDS_MITIGATION_FULL;
513 		mds_select_mitigation();
514 	}
515 	if (taa_mitigation == TAA_MITIGATION_OFF &&
516 	    boot_cpu_has_bug(X86_BUG_TAA)) {
517 		taa_mitigation = TAA_MITIGATION_VERW;
518 		taa_select_mitigation();
519 	}
520 	if (mmio_mitigation == MMIO_MITIGATION_OFF &&
521 	    boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA)) {
522 		mmio_mitigation = MMIO_MITIGATION_VERW;
523 		mmio_select_mitigation();
524 	}
525 out:
526 	if (boot_cpu_has_bug(X86_BUG_MDS))
527 		pr_info("MDS: %s\n", mds_strings[mds_mitigation]);
528 	if (boot_cpu_has_bug(X86_BUG_TAA))
529 		pr_info("TAA: %s\n", taa_strings[taa_mitigation]);
530 	if (boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA))
531 		pr_info("MMIO Stale Data: %s\n", mmio_strings[mmio_mitigation]);
532 }
533 
md_clear_select_mitigation(void)534 static void __init md_clear_select_mitigation(void)
535 {
536 	mds_select_mitigation();
537 	taa_select_mitigation();
538 	mmio_select_mitigation();
539 
540 	/*
541 	 * As MDS, TAA and MMIO Stale Data mitigations are inter-related, update
542 	 * and print their mitigation after MDS, TAA and MMIO Stale Data
543 	 * mitigation selection is done.
544 	 */
545 	md_clear_update_mitigation();
546 }
547 
548 #undef pr_fmt
549 #define pr_fmt(fmt)	"SRBDS: " fmt
550 
551 enum srbds_mitigations {
552 	SRBDS_MITIGATION_OFF,
553 	SRBDS_MITIGATION_UCODE_NEEDED,
554 	SRBDS_MITIGATION_FULL,
555 	SRBDS_MITIGATION_TSX_OFF,
556 	SRBDS_MITIGATION_HYPERVISOR,
557 };
558 
559 static enum srbds_mitigations srbds_mitigation __ro_after_init = SRBDS_MITIGATION_FULL;
560 
561 static const char * const srbds_strings[] = {
562 	[SRBDS_MITIGATION_OFF]		= "Vulnerable",
563 	[SRBDS_MITIGATION_UCODE_NEEDED]	= "Vulnerable: No microcode",
564 	[SRBDS_MITIGATION_FULL]		= "Mitigation: Microcode",
565 	[SRBDS_MITIGATION_TSX_OFF]	= "Mitigation: TSX disabled",
566 	[SRBDS_MITIGATION_HYPERVISOR]	= "Unknown: Dependent on hypervisor status",
567 };
568 
569 static bool srbds_off;
570 
update_srbds_msr(void)571 void update_srbds_msr(void)
572 {
573 	u64 mcu_ctrl;
574 
575 	if (!boot_cpu_has_bug(X86_BUG_SRBDS))
576 		return;
577 
578 	if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
579 		return;
580 
581 	if (srbds_mitigation == SRBDS_MITIGATION_UCODE_NEEDED)
582 		return;
583 
584 	rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
585 
586 	switch (srbds_mitigation) {
587 	case SRBDS_MITIGATION_OFF:
588 	case SRBDS_MITIGATION_TSX_OFF:
589 		mcu_ctrl |= RNGDS_MITG_DIS;
590 		break;
591 	case SRBDS_MITIGATION_FULL:
592 		mcu_ctrl &= ~RNGDS_MITG_DIS;
593 		break;
594 	default:
595 		break;
596 	}
597 
598 	wrmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
599 }
600 
srbds_select_mitigation(void)601 static void __init srbds_select_mitigation(void)
602 {
603 	u64 ia32_cap;
604 
605 	if (!boot_cpu_has_bug(X86_BUG_SRBDS))
606 		return;
607 
608 	/*
609 	 * Check to see if this is one of the MDS_NO systems supporting TSX that
610 	 * are only exposed to SRBDS when TSX is enabled or when CPU is affected
611 	 * by Processor MMIO Stale Data vulnerability.
612 	 */
613 	ia32_cap = x86_read_arch_cap_msr();
614 	if ((ia32_cap & ARCH_CAP_MDS_NO) && !boot_cpu_has(X86_FEATURE_RTM) &&
615 	    !boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA))
616 		srbds_mitigation = SRBDS_MITIGATION_TSX_OFF;
617 	else if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
618 		srbds_mitigation = SRBDS_MITIGATION_HYPERVISOR;
619 	else if (!boot_cpu_has(X86_FEATURE_SRBDS_CTRL))
620 		srbds_mitigation = SRBDS_MITIGATION_UCODE_NEEDED;
621 	else if (cpu_mitigations_off() || srbds_off)
622 		srbds_mitigation = SRBDS_MITIGATION_OFF;
623 
624 	update_srbds_msr();
625 	pr_info("%s\n", srbds_strings[srbds_mitigation]);
626 }
627 
srbds_parse_cmdline(char * str)628 static int __init srbds_parse_cmdline(char *str)
629 {
630 	if (!str)
631 		return -EINVAL;
632 
633 	if (!boot_cpu_has_bug(X86_BUG_SRBDS))
634 		return 0;
635 
636 	srbds_off = !strcmp(str, "off");
637 	return 0;
638 }
639 early_param("srbds", srbds_parse_cmdline);
640 
641 #undef pr_fmt
642 #define pr_fmt(fmt)     "Spectre V1 : " fmt
643 
644 enum spectre_v1_mitigation {
645 	SPECTRE_V1_MITIGATION_NONE,
646 	SPECTRE_V1_MITIGATION_AUTO,
647 };
648 
649 static enum spectre_v1_mitigation spectre_v1_mitigation __ro_after_init =
650 	SPECTRE_V1_MITIGATION_AUTO;
651 
652 static const char * const spectre_v1_strings[] = {
653 	[SPECTRE_V1_MITIGATION_NONE] = "Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers",
654 	[SPECTRE_V1_MITIGATION_AUTO] = "Mitigation: usercopy/swapgs barriers and __user pointer sanitization",
655 };
656 
657 /*
658  * Does SMAP provide full mitigation against speculative kernel access to
659  * userspace?
660  */
smap_works_speculatively(void)661 static bool smap_works_speculatively(void)
662 {
663 	if (!boot_cpu_has(X86_FEATURE_SMAP))
664 		return false;
665 
666 	/*
667 	 * On CPUs which are vulnerable to Meltdown, SMAP does not
668 	 * prevent speculative access to user data in the L1 cache.
669 	 * Consider SMAP to be non-functional as a mitigation on these
670 	 * CPUs.
671 	 */
672 	if (boot_cpu_has(X86_BUG_CPU_MELTDOWN))
673 		return false;
674 
675 	return true;
676 }
677 
spectre_v1_select_mitigation(void)678 static void __init spectre_v1_select_mitigation(void)
679 {
680 	if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1) || cpu_mitigations_off()) {
681 		spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
682 		return;
683 	}
684 
685 	if (spectre_v1_mitigation == SPECTRE_V1_MITIGATION_AUTO) {
686 		/*
687 		 * With Spectre v1, a user can speculatively control either
688 		 * path of a conditional swapgs with a user-controlled GS
689 		 * value.  The mitigation is to add lfences to both code paths.
690 		 *
691 		 * If FSGSBASE is enabled, the user can put a kernel address in
692 		 * GS, in which case SMAP provides no protection.
693 		 *
694 		 * If FSGSBASE is disabled, the user can only put a user space
695 		 * address in GS.  That makes an attack harder, but still
696 		 * possible if there's no SMAP protection.
697 		 */
698 		if (boot_cpu_has(X86_FEATURE_FSGSBASE) ||
699 		    !smap_works_speculatively()) {
700 			/*
701 			 * Mitigation can be provided from SWAPGS itself or
702 			 * PTI as the CR3 write in the Meltdown mitigation
703 			 * is serializing.
704 			 *
705 			 * If neither is there, mitigate with an LFENCE to
706 			 * stop speculation through swapgs.
707 			 */
708 			if (boot_cpu_has_bug(X86_BUG_SWAPGS) &&
709 			    !boot_cpu_has(X86_FEATURE_PTI))
710 				setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_USER);
711 
712 			/*
713 			 * Enable lfences in the kernel entry (non-swapgs)
714 			 * paths, to prevent user entry from speculatively
715 			 * skipping swapgs.
716 			 */
717 			setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_KERNEL);
718 		}
719 	}
720 
721 	pr_info("%s\n", spectre_v1_strings[spectre_v1_mitigation]);
722 }
723 
nospectre_v1_cmdline(char * str)724 static int __init nospectre_v1_cmdline(char *str)
725 {
726 	spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
727 	return 0;
728 }
729 early_param("nospectre_v1", nospectre_v1_cmdline);
730 
731 static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init =
732 	SPECTRE_V2_NONE;
733 
734 #undef pr_fmt
735 #define pr_fmt(fmt)     "RETBleed: " fmt
736 
737 enum retbleed_mitigation {
738 	RETBLEED_MITIGATION_NONE,
739 	RETBLEED_MITIGATION_UNRET,
740 	RETBLEED_MITIGATION_IBPB,
741 	RETBLEED_MITIGATION_IBRS,
742 	RETBLEED_MITIGATION_EIBRS,
743 };
744 
745 enum retbleed_mitigation_cmd {
746 	RETBLEED_CMD_OFF,
747 	RETBLEED_CMD_AUTO,
748 	RETBLEED_CMD_UNRET,
749 	RETBLEED_CMD_IBPB,
750 };
751 
752 const char * const retbleed_strings[] = {
753 	[RETBLEED_MITIGATION_NONE]	= "Vulnerable",
754 	[RETBLEED_MITIGATION_UNRET]	= "Mitigation: untrained return thunk",
755 	[RETBLEED_MITIGATION_IBPB]	= "Mitigation: IBPB",
756 	[RETBLEED_MITIGATION_IBRS]	= "Mitigation: IBRS",
757 	[RETBLEED_MITIGATION_EIBRS]	= "Mitigation: Enhanced IBRS",
758 };
759 
760 static enum retbleed_mitigation retbleed_mitigation __ro_after_init =
761 	RETBLEED_MITIGATION_NONE;
762 static enum retbleed_mitigation_cmd retbleed_cmd __ro_after_init =
763 	RETBLEED_CMD_AUTO;
764 
765 static int __ro_after_init retbleed_nosmt = false;
766 
retbleed_parse_cmdline(char * str)767 static int __init retbleed_parse_cmdline(char *str)
768 {
769 	if (!str)
770 		return -EINVAL;
771 
772 	while (str) {
773 		char *next = strchr(str, ',');
774 		if (next) {
775 			*next = 0;
776 			next++;
777 		}
778 
779 		if (!strcmp(str, "off")) {
780 			retbleed_cmd = RETBLEED_CMD_OFF;
781 		} else if (!strcmp(str, "auto")) {
782 			retbleed_cmd = RETBLEED_CMD_AUTO;
783 		} else if (!strcmp(str, "unret")) {
784 			retbleed_cmd = RETBLEED_CMD_UNRET;
785 		} else if (!strcmp(str, "ibpb")) {
786 			retbleed_cmd = RETBLEED_CMD_IBPB;
787 		} else if (!strcmp(str, "nosmt")) {
788 			retbleed_nosmt = true;
789 		} else {
790 			pr_err("Ignoring unknown retbleed option (%s).", str);
791 		}
792 
793 		str = next;
794 	}
795 
796 	return 0;
797 }
798 early_param("retbleed", retbleed_parse_cmdline);
799 
800 #define RETBLEED_UNTRAIN_MSG "WARNING: BTB untrained return thunk mitigation is only effective on AMD/Hygon!\n"
801 #define RETBLEED_INTEL_MSG "WARNING: Spectre v2 mitigation leaves CPU vulnerable to RETBleed attacks, data leaks possible!\n"
802 
retbleed_select_mitigation(void)803 static void __init retbleed_select_mitigation(void)
804 {
805 	bool mitigate_smt = false;
806 
807 	if (!boot_cpu_has_bug(X86_BUG_RETBLEED) || cpu_mitigations_off())
808 		return;
809 
810 	switch (retbleed_cmd) {
811 	case RETBLEED_CMD_OFF:
812 		return;
813 
814 	case RETBLEED_CMD_UNRET:
815 		if (IS_ENABLED(CONFIG_CPU_UNRET_ENTRY)) {
816 			retbleed_mitigation = RETBLEED_MITIGATION_UNRET;
817 		} else {
818 			pr_err("WARNING: kernel not compiled with CPU_UNRET_ENTRY.\n");
819 			goto do_cmd_auto;
820 		}
821 		break;
822 
823 	case RETBLEED_CMD_IBPB:
824 		if (!boot_cpu_has(X86_FEATURE_IBPB)) {
825 			pr_err("WARNING: CPU does not support IBPB.\n");
826 			goto do_cmd_auto;
827 		} else if (IS_ENABLED(CONFIG_CPU_IBPB_ENTRY)) {
828 			retbleed_mitigation = RETBLEED_MITIGATION_IBPB;
829 		} else {
830 			pr_err("WARNING: kernel not compiled with CPU_IBPB_ENTRY.\n");
831 			goto do_cmd_auto;
832 		}
833 		break;
834 
835 do_cmd_auto:
836 	case RETBLEED_CMD_AUTO:
837 	default:
838 		if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
839 		    boot_cpu_data.x86_vendor == X86_VENDOR_HYGON) {
840 			if (IS_ENABLED(CONFIG_CPU_UNRET_ENTRY))
841 				retbleed_mitigation = RETBLEED_MITIGATION_UNRET;
842 			else if (IS_ENABLED(CONFIG_CPU_IBPB_ENTRY) && boot_cpu_has(X86_FEATURE_IBPB))
843 				retbleed_mitigation = RETBLEED_MITIGATION_IBPB;
844 		}
845 
846 		/*
847 		 * The Intel mitigation (IBRS or eIBRS) was already selected in
848 		 * spectre_v2_select_mitigation().  'retbleed_mitigation' will
849 		 * be set accordingly below.
850 		 */
851 
852 		break;
853 	}
854 
855 	switch (retbleed_mitigation) {
856 	case RETBLEED_MITIGATION_UNRET:
857 		setup_force_cpu_cap(X86_FEATURE_RETHUNK);
858 		setup_force_cpu_cap(X86_FEATURE_UNRET);
859 
860 		if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
861 		    boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)
862 			pr_err(RETBLEED_UNTRAIN_MSG);
863 
864 		mitigate_smt = true;
865 		break;
866 
867 	case RETBLEED_MITIGATION_IBPB:
868 		setup_force_cpu_cap(X86_FEATURE_ENTRY_IBPB);
869 		mitigate_smt = true;
870 		break;
871 
872 	default:
873 		break;
874 	}
875 
876 	if (mitigate_smt && !boot_cpu_has(X86_FEATURE_STIBP) &&
877 	    (retbleed_nosmt || cpu_mitigations_auto_nosmt()))
878 		cpu_smt_disable(false);
879 
880 	/*
881 	 * Let IBRS trump all on Intel without affecting the effects of the
882 	 * retbleed= cmdline option.
883 	 */
884 	if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
885 		switch (spectre_v2_enabled) {
886 		case SPECTRE_V2_IBRS:
887 			retbleed_mitigation = RETBLEED_MITIGATION_IBRS;
888 			break;
889 		case SPECTRE_V2_EIBRS:
890 		case SPECTRE_V2_EIBRS_RETPOLINE:
891 		case SPECTRE_V2_EIBRS_LFENCE:
892 			retbleed_mitigation = RETBLEED_MITIGATION_EIBRS;
893 			break;
894 		default:
895 			pr_err(RETBLEED_INTEL_MSG);
896 		}
897 	}
898 
899 	pr_info("%s\n", retbleed_strings[retbleed_mitigation]);
900 }
901 
902 #undef pr_fmt
903 #define pr_fmt(fmt)     "Spectre V2 : " fmt
904 
905 static enum spectre_v2_user_mitigation spectre_v2_user_stibp __ro_after_init =
906 	SPECTRE_V2_USER_NONE;
907 static enum spectre_v2_user_mitigation spectre_v2_user_ibpb __ro_after_init =
908 	SPECTRE_V2_USER_NONE;
909 
910 #ifdef CONFIG_RETPOLINE
911 static bool spectre_v2_bad_module;
912 
retpoline_module_ok(bool has_retpoline)913 bool retpoline_module_ok(bool has_retpoline)
914 {
915 	if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
916 		return true;
917 
918 	pr_err("System may be vulnerable to spectre v2\n");
919 	spectre_v2_bad_module = true;
920 	return false;
921 }
922 
spectre_v2_module_string(void)923 static inline const char *spectre_v2_module_string(void)
924 {
925 	return spectre_v2_bad_module ? " - vulnerable module loaded" : "";
926 }
927 #else
spectre_v2_module_string(void)928 static inline const char *spectre_v2_module_string(void) { return ""; }
929 #endif
930 
931 #define SPECTRE_V2_LFENCE_MSG "WARNING: LFENCE mitigation is not recommended for this CPU, data leaks possible!\n"
932 #define SPECTRE_V2_EIBRS_EBPF_MSG "WARNING: Unprivileged eBPF is enabled with eIBRS on, data leaks possible via Spectre v2 BHB attacks!\n"
933 #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"
934 
935 #ifdef CONFIG_BPF_SYSCALL
unpriv_ebpf_notify(int new_state)936 void unpriv_ebpf_notify(int new_state)
937 {
938 	if (new_state)
939 		return;
940 
941 	/* Unprivileged eBPF is enabled */
942 
943 	switch (spectre_v2_enabled) {
944 	case SPECTRE_V2_EIBRS:
945 		pr_err(SPECTRE_V2_EIBRS_EBPF_MSG);
946 		break;
947 	case SPECTRE_V2_EIBRS_LFENCE:
948 		if (sched_smt_active())
949 			pr_err(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG);
950 		break;
951 	default:
952 		break;
953 	}
954 }
955 #endif
956 
match_option(const char * arg,int arglen,const char * opt)957 static inline bool match_option(const char *arg, int arglen, const char *opt)
958 {
959 	int len = strlen(opt);
960 
961 	return len == arglen && !strncmp(arg, opt, len);
962 }
963 
964 /* The kernel command line selection for spectre v2 */
965 enum spectre_v2_mitigation_cmd {
966 	SPECTRE_V2_CMD_NONE,
967 	SPECTRE_V2_CMD_AUTO,
968 	SPECTRE_V2_CMD_FORCE,
969 	SPECTRE_V2_CMD_RETPOLINE,
970 	SPECTRE_V2_CMD_RETPOLINE_GENERIC,
971 	SPECTRE_V2_CMD_RETPOLINE_LFENCE,
972 	SPECTRE_V2_CMD_EIBRS,
973 	SPECTRE_V2_CMD_EIBRS_RETPOLINE,
974 	SPECTRE_V2_CMD_EIBRS_LFENCE,
975 	SPECTRE_V2_CMD_IBRS,
976 };
977 
978 enum spectre_v2_user_cmd {
979 	SPECTRE_V2_USER_CMD_NONE,
980 	SPECTRE_V2_USER_CMD_AUTO,
981 	SPECTRE_V2_USER_CMD_FORCE,
982 	SPECTRE_V2_USER_CMD_PRCTL,
983 	SPECTRE_V2_USER_CMD_PRCTL_IBPB,
984 	SPECTRE_V2_USER_CMD_SECCOMP,
985 	SPECTRE_V2_USER_CMD_SECCOMP_IBPB,
986 };
987 
988 static const char * const spectre_v2_user_strings[] = {
989 	[SPECTRE_V2_USER_NONE]			= "User space: Vulnerable",
990 	[SPECTRE_V2_USER_STRICT]		= "User space: Mitigation: STIBP protection",
991 	[SPECTRE_V2_USER_STRICT_PREFERRED]	= "User space: Mitigation: STIBP always-on protection",
992 	[SPECTRE_V2_USER_PRCTL]			= "User space: Mitigation: STIBP via prctl",
993 	[SPECTRE_V2_USER_SECCOMP]		= "User space: Mitigation: STIBP via seccomp and prctl",
994 };
995 
996 static const struct {
997 	const char			*option;
998 	enum spectre_v2_user_cmd	cmd;
999 	bool				secure;
1000 } v2_user_options[] __initconst = {
1001 	{ "auto",		SPECTRE_V2_USER_CMD_AUTO,		false },
1002 	{ "off",		SPECTRE_V2_USER_CMD_NONE,		false },
1003 	{ "on",			SPECTRE_V2_USER_CMD_FORCE,		true  },
1004 	{ "prctl",		SPECTRE_V2_USER_CMD_PRCTL,		false },
1005 	{ "prctl,ibpb",		SPECTRE_V2_USER_CMD_PRCTL_IBPB,		false },
1006 	{ "seccomp",		SPECTRE_V2_USER_CMD_SECCOMP,		false },
1007 	{ "seccomp,ibpb",	SPECTRE_V2_USER_CMD_SECCOMP_IBPB,	false },
1008 };
1009 
spec_v2_user_print_cond(const char * reason,bool secure)1010 static void __init spec_v2_user_print_cond(const char *reason, bool secure)
1011 {
1012 	if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
1013 		pr_info("spectre_v2_user=%s forced on command line.\n", reason);
1014 }
1015 
1016 static __ro_after_init enum spectre_v2_mitigation_cmd spectre_v2_cmd;
1017 
1018 static enum spectre_v2_user_cmd __init
spectre_v2_parse_user_cmdline(void)1019 spectre_v2_parse_user_cmdline(void)
1020 {
1021 	char arg[20];
1022 	int ret, i;
1023 
1024 	switch (spectre_v2_cmd) {
1025 	case SPECTRE_V2_CMD_NONE:
1026 		return SPECTRE_V2_USER_CMD_NONE;
1027 	case SPECTRE_V2_CMD_FORCE:
1028 		return SPECTRE_V2_USER_CMD_FORCE;
1029 	default:
1030 		break;
1031 	}
1032 
1033 	ret = cmdline_find_option(boot_command_line, "spectre_v2_user",
1034 				  arg, sizeof(arg));
1035 	if (ret < 0)
1036 		return SPECTRE_V2_USER_CMD_AUTO;
1037 
1038 	for (i = 0; i < ARRAY_SIZE(v2_user_options); i++) {
1039 		if (match_option(arg, ret, v2_user_options[i].option)) {
1040 			spec_v2_user_print_cond(v2_user_options[i].option,
1041 						v2_user_options[i].secure);
1042 			return v2_user_options[i].cmd;
1043 		}
1044 	}
1045 
1046 	pr_err("Unknown user space protection option (%s). Switching to AUTO select\n", arg);
1047 	return SPECTRE_V2_USER_CMD_AUTO;
1048 }
1049 
spectre_v2_in_ibrs_mode(enum spectre_v2_mitigation mode)1050 static inline bool spectre_v2_in_ibrs_mode(enum spectre_v2_mitigation mode)
1051 {
1052 	return mode == SPECTRE_V2_IBRS ||
1053 	       mode == SPECTRE_V2_EIBRS ||
1054 	       mode == SPECTRE_V2_EIBRS_RETPOLINE ||
1055 	       mode == SPECTRE_V2_EIBRS_LFENCE;
1056 }
1057 
1058 static void __init
spectre_v2_user_select_mitigation(void)1059 spectre_v2_user_select_mitigation(void)
1060 {
1061 	enum spectre_v2_user_mitigation mode = SPECTRE_V2_USER_NONE;
1062 	bool smt_possible = IS_ENABLED(CONFIG_SMP);
1063 	enum spectre_v2_user_cmd cmd;
1064 
1065 	if (!boot_cpu_has(X86_FEATURE_IBPB) && !boot_cpu_has(X86_FEATURE_STIBP))
1066 		return;
1067 
1068 	if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
1069 	    cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
1070 		smt_possible = false;
1071 
1072 	cmd = spectre_v2_parse_user_cmdline();
1073 	switch (cmd) {
1074 	case SPECTRE_V2_USER_CMD_NONE:
1075 		goto set_mode;
1076 	case SPECTRE_V2_USER_CMD_FORCE:
1077 		mode = SPECTRE_V2_USER_STRICT;
1078 		break;
1079 	case SPECTRE_V2_USER_CMD_PRCTL:
1080 	case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
1081 		mode = SPECTRE_V2_USER_PRCTL;
1082 		break;
1083 	case SPECTRE_V2_USER_CMD_AUTO:
1084 	case SPECTRE_V2_USER_CMD_SECCOMP:
1085 	case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
1086 		if (IS_ENABLED(CONFIG_SECCOMP))
1087 			mode = SPECTRE_V2_USER_SECCOMP;
1088 		else
1089 			mode = SPECTRE_V2_USER_PRCTL;
1090 		break;
1091 	}
1092 
1093 	/* Initialize Indirect Branch Prediction Barrier */
1094 	if (boot_cpu_has(X86_FEATURE_IBPB)) {
1095 		setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
1096 
1097 		spectre_v2_user_ibpb = mode;
1098 		switch (cmd) {
1099 		case SPECTRE_V2_USER_CMD_FORCE:
1100 		case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
1101 		case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
1102 			static_branch_enable(&switch_mm_always_ibpb);
1103 			spectre_v2_user_ibpb = SPECTRE_V2_USER_STRICT;
1104 			break;
1105 		case SPECTRE_V2_USER_CMD_PRCTL:
1106 		case SPECTRE_V2_USER_CMD_AUTO:
1107 		case SPECTRE_V2_USER_CMD_SECCOMP:
1108 			static_branch_enable(&switch_mm_cond_ibpb);
1109 			break;
1110 		default:
1111 			break;
1112 		}
1113 
1114 		pr_info("mitigation: Enabling %s Indirect Branch Prediction Barrier\n",
1115 			static_key_enabled(&switch_mm_always_ibpb) ?
1116 			"always-on" : "conditional");
1117 	}
1118 
1119 	/*
1120 	 * If no STIBP, IBRS or enhanced IBRS is enabled, or SMT impossible,
1121 	 * STIBP is not required.
1122 	 */
1123 	if (!boot_cpu_has(X86_FEATURE_STIBP) ||
1124 	    !smt_possible ||
1125 	    spectre_v2_in_ibrs_mode(spectre_v2_enabled))
1126 		return;
1127 
1128 	/*
1129 	 * At this point, an STIBP mode other than "off" has been set.
1130 	 * If STIBP support is not being forced, check if STIBP always-on
1131 	 * is preferred.
1132 	 */
1133 	if (mode != SPECTRE_V2_USER_STRICT &&
1134 	    boot_cpu_has(X86_FEATURE_AMD_STIBP_ALWAYS_ON))
1135 		mode = SPECTRE_V2_USER_STRICT_PREFERRED;
1136 
1137 	if (retbleed_mitigation == RETBLEED_MITIGATION_UNRET) {
1138 		if (mode != SPECTRE_V2_USER_STRICT &&
1139 		    mode != SPECTRE_V2_USER_STRICT_PREFERRED)
1140 			pr_info("Selecting STIBP always-on mode to complement retbleed mitigation\n");
1141 		mode = SPECTRE_V2_USER_STRICT_PREFERRED;
1142 	}
1143 
1144 	spectre_v2_user_stibp = mode;
1145 
1146 set_mode:
1147 	pr_info("%s\n", spectre_v2_user_strings[mode]);
1148 }
1149 
1150 static const char * const spectre_v2_strings[] = {
1151 	[SPECTRE_V2_NONE]			= "Vulnerable",
1152 	[SPECTRE_V2_RETPOLINE]			= "Mitigation: Retpolines",
1153 	[SPECTRE_V2_LFENCE]			= "Mitigation: LFENCE",
1154 	[SPECTRE_V2_EIBRS]			= "Mitigation: Enhanced IBRS",
1155 	[SPECTRE_V2_EIBRS_LFENCE]		= "Mitigation: Enhanced IBRS + LFENCE",
1156 	[SPECTRE_V2_EIBRS_RETPOLINE]		= "Mitigation: Enhanced IBRS + Retpolines",
1157 	[SPECTRE_V2_IBRS]			= "Mitigation: IBRS",
1158 };
1159 
1160 static const struct {
1161 	const char *option;
1162 	enum spectre_v2_mitigation_cmd cmd;
1163 	bool secure;
1164 } mitigation_options[] __initconst = {
1165 	{ "off",		SPECTRE_V2_CMD_NONE,		  false },
1166 	{ "on",			SPECTRE_V2_CMD_FORCE,		  true  },
1167 	{ "retpoline",		SPECTRE_V2_CMD_RETPOLINE,	  false },
1168 	{ "retpoline,amd",	SPECTRE_V2_CMD_RETPOLINE_LFENCE,  false },
1169 	{ "retpoline,lfence",	SPECTRE_V2_CMD_RETPOLINE_LFENCE,  false },
1170 	{ "retpoline,generic",	SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
1171 	{ "eibrs",		SPECTRE_V2_CMD_EIBRS,		  false },
1172 	{ "eibrs,lfence",	SPECTRE_V2_CMD_EIBRS_LFENCE,	  false },
1173 	{ "eibrs,retpoline",	SPECTRE_V2_CMD_EIBRS_RETPOLINE,	  false },
1174 	{ "auto",		SPECTRE_V2_CMD_AUTO,		  false },
1175 	{ "ibrs",		SPECTRE_V2_CMD_IBRS,              false },
1176 };
1177 
spec_v2_print_cond(const char * reason,bool secure)1178 static void __init spec_v2_print_cond(const char *reason, bool secure)
1179 {
1180 	if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
1181 		pr_info("%s selected on command line.\n", reason);
1182 }
1183 
spectre_v2_parse_cmdline(void)1184 static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
1185 {
1186 	enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
1187 	char arg[20];
1188 	int ret, i;
1189 
1190 	if (cmdline_find_option_bool(boot_command_line, "nospectre_v2") ||
1191 	    cpu_mitigations_off())
1192 		return SPECTRE_V2_CMD_NONE;
1193 
1194 	ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
1195 	if (ret < 0)
1196 		return SPECTRE_V2_CMD_AUTO;
1197 
1198 	for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
1199 		if (!match_option(arg, ret, mitigation_options[i].option))
1200 			continue;
1201 		cmd = mitigation_options[i].cmd;
1202 		break;
1203 	}
1204 
1205 	if (i >= ARRAY_SIZE(mitigation_options)) {
1206 		pr_err("unknown option (%s). Switching to AUTO select\n", arg);
1207 		return SPECTRE_V2_CMD_AUTO;
1208 	}
1209 
1210 	if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
1211 	     cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE ||
1212 	     cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC ||
1213 	     cmd == SPECTRE_V2_CMD_EIBRS_LFENCE ||
1214 	     cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) &&
1215 	    !IS_ENABLED(CONFIG_RETPOLINE)) {
1216 		pr_err("%s selected but not compiled in. Switching to AUTO select\n",
1217 		       mitigation_options[i].option);
1218 		return SPECTRE_V2_CMD_AUTO;
1219 	}
1220 
1221 	if ((cmd == SPECTRE_V2_CMD_EIBRS ||
1222 	     cmd == SPECTRE_V2_CMD_EIBRS_LFENCE ||
1223 	     cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) &&
1224 	    !boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
1225 		pr_err("%s selected but CPU doesn't have eIBRS. Switching to AUTO select\n",
1226 		       mitigation_options[i].option);
1227 		return SPECTRE_V2_CMD_AUTO;
1228 	}
1229 
1230 	if ((cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE ||
1231 	     cmd == SPECTRE_V2_CMD_EIBRS_LFENCE) &&
1232 	    !boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
1233 		pr_err("%s selected, but CPU doesn't have a serializing LFENCE. Switching to AUTO select\n",
1234 		       mitigation_options[i].option);
1235 		return SPECTRE_V2_CMD_AUTO;
1236 	}
1237 
1238 	if (cmd == SPECTRE_V2_CMD_IBRS && !IS_ENABLED(CONFIG_CPU_IBRS_ENTRY)) {
1239 		pr_err("%s selected but not compiled in. Switching to AUTO select\n",
1240 		       mitigation_options[i].option);
1241 		return SPECTRE_V2_CMD_AUTO;
1242 	}
1243 
1244 	if (cmd == SPECTRE_V2_CMD_IBRS && boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
1245 		pr_err("%s selected but not Intel CPU. Switching to AUTO select\n",
1246 		       mitigation_options[i].option);
1247 		return SPECTRE_V2_CMD_AUTO;
1248 	}
1249 
1250 	if (cmd == SPECTRE_V2_CMD_IBRS && !boot_cpu_has(X86_FEATURE_IBRS)) {
1251 		pr_err("%s selected but CPU doesn't have IBRS. Switching to AUTO select\n",
1252 		       mitigation_options[i].option);
1253 		return SPECTRE_V2_CMD_AUTO;
1254 	}
1255 
1256 	if (cmd == SPECTRE_V2_CMD_IBRS && boot_cpu_has(X86_FEATURE_XENPV)) {
1257 		pr_err("%s selected but running as XenPV guest. Switching to AUTO select\n",
1258 		       mitigation_options[i].option);
1259 		return SPECTRE_V2_CMD_AUTO;
1260 	}
1261 
1262 	spec_v2_print_cond(mitigation_options[i].option,
1263 			   mitigation_options[i].secure);
1264 	return cmd;
1265 }
1266 
spectre_v2_select_retpoline(void)1267 static enum spectre_v2_mitigation __init spectre_v2_select_retpoline(void)
1268 {
1269 	if (!IS_ENABLED(CONFIG_RETPOLINE)) {
1270 		pr_err("Kernel not compiled with retpoline; no mitigation available!");
1271 		return SPECTRE_V2_NONE;
1272 	}
1273 
1274 	return SPECTRE_V2_RETPOLINE;
1275 }
1276 
1277 /* Disable in-kernel use of non-RSB RET predictors */
spec_ctrl_disable_kernel_rrsba(void)1278 static void __init spec_ctrl_disable_kernel_rrsba(void)
1279 {
1280 	u64 ia32_cap;
1281 
1282 	if (!boot_cpu_has(X86_FEATURE_RRSBA_CTRL))
1283 		return;
1284 
1285 	ia32_cap = x86_read_arch_cap_msr();
1286 
1287 	if (ia32_cap & ARCH_CAP_RRSBA) {
1288 		x86_spec_ctrl_base |= SPEC_CTRL_RRSBA_DIS_S;
1289 		write_spec_ctrl_current(x86_spec_ctrl_base, true);
1290 	}
1291 }
1292 
spectre_v2_determine_rsb_fill_type_at_vmexit(enum spectre_v2_mitigation mode)1293 static void __init spectre_v2_determine_rsb_fill_type_at_vmexit(enum spectre_v2_mitigation mode)
1294 {
1295 	/*
1296 	 * Similar to context switches, there are two types of RSB attacks
1297 	 * after VM exit:
1298 	 *
1299 	 * 1) RSB underflow
1300 	 *
1301 	 * 2) Poisoned RSB entry
1302 	 *
1303 	 * When retpoline is enabled, both are mitigated by filling/clearing
1304 	 * the RSB.
1305 	 *
1306 	 * When IBRS is enabled, while #1 would be mitigated by the IBRS branch
1307 	 * prediction isolation protections, RSB still needs to be cleared
1308 	 * because of #2.  Note that SMEP provides no protection here, unlike
1309 	 * user-space-poisoned RSB entries.
1310 	 *
1311 	 * eIBRS should protect against RSB poisoning, but if the EIBRS_PBRSB
1312 	 * bug is present then a LITE version of RSB protection is required,
1313 	 * just a single call needs to retire before a RET is executed.
1314 	 */
1315 	switch (mode) {
1316 	case SPECTRE_V2_NONE:
1317 		return;
1318 
1319 	case SPECTRE_V2_EIBRS_LFENCE:
1320 	case SPECTRE_V2_EIBRS:
1321 		if (boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB)) {
1322 			setup_force_cpu_cap(X86_FEATURE_RSB_VMEXIT_LITE);
1323 			pr_info("Spectre v2 / PBRSB-eIBRS: Retire a single CALL on VMEXIT\n");
1324 		}
1325 		return;
1326 
1327 	case SPECTRE_V2_EIBRS_RETPOLINE:
1328 	case SPECTRE_V2_RETPOLINE:
1329 	case SPECTRE_V2_LFENCE:
1330 	case SPECTRE_V2_IBRS:
1331 		setup_force_cpu_cap(X86_FEATURE_RSB_VMEXIT);
1332 		pr_info("Spectre v2 / SpectreRSB : Filling RSB on VMEXIT\n");
1333 		return;
1334 	}
1335 
1336 	pr_warn_once("Unknown Spectre v2 mode, disabling RSB mitigation at VM exit");
1337 	dump_stack();
1338 }
1339 
spectre_v2_select_mitigation(void)1340 static void __init spectre_v2_select_mitigation(void)
1341 {
1342 	enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
1343 	enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
1344 
1345 	/*
1346 	 * If the CPU is not affected and the command line mode is NONE or AUTO
1347 	 * then nothing to do.
1348 	 */
1349 	if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
1350 	    (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
1351 		return;
1352 
1353 	switch (cmd) {
1354 	case SPECTRE_V2_CMD_NONE:
1355 		return;
1356 
1357 	case SPECTRE_V2_CMD_FORCE:
1358 	case SPECTRE_V2_CMD_AUTO:
1359 		if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
1360 			mode = SPECTRE_V2_EIBRS;
1361 			break;
1362 		}
1363 
1364 		if (IS_ENABLED(CONFIG_CPU_IBRS_ENTRY) &&
1365 		    boot_cpu_has_bug(X86_BUG_RETBLEED) &&
1366 		    retbleed_cmd != RETBLEED_CMD_OFF &&
1367 		    boot_cpu_has(X86_FEATURE_IBRS) &&
1368 		    boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
1369 			mode = SPECTRE_V2_IBRS;
1370 			break;
1371 		}
1372 
1373 		mode = spectre_v2_select_retpoline();
1374 		break;
1375 
1376 	case SPECTRE_V2_CMD_RETPOLINE_LFENCE:
1377 		pr_err(SPECTRE_V2_LFENCE_MSG);
1378 		mode = SPECTRE_V2_LFENCE;
1379 		break;
1380 
1381 	case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
1382 		mode = SPECTRE_V2_RETPOLINE;
1383 		break;
1384 
1385 	case SPECTRE_V2_CMD_RETPOLINE:
1386 		mode = spectre_v2_select_retpoline();
1387 		break;
1388 
1389 	case SPECTRE_V2_CMD_IBRS:
1390 		mode = SPECTRE_V2_IBRS;
1391 		break;
1392 
1393 	case SPECTRE_V2_CMD_EIBRS:
1394 		mode = SPECTRE_V2_EIBRS;
1395 		break;
1396 
1397 	case SPECTRE_V2_CMD_EIBRS_LFENCE:
1398 		mode = SPECTRE_V2_EIBRS_LFENCE;
1399 		break;
1400 
1401 	case SPECTRE_V2_CMD_EIBRS_RETPOLINE:
1402 		mode = SPECTRE_V2_EIBRS_RETPOLINE;
1403 		break;
1404 	}
1405 
1406 	if (mode == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
1407 		pr_err(SPECTRE_V2_EIBRS_EBPF_MSG);
1408 
1409 	if (spectre_v2_in_ibrs_mode(mode)) {
1410 		x86_spec_ctrl_base |= SPEC_CTRL_IBRS;
1411 		write_spec_ctrl_current(x86_spec_ctrl_base, true);
1412 	}
1413 
1414 	switch (mode) {
1415 	case SPECTRE_V2_NONE:
1416 	case SPECTRE_V2_EIBRS:
1417 		break;
1418 
1419 	case SPECTRE_V2_IBRS:
1420 		setup_force_cpu_cap(X86_FEATURE_KERNEL_IBRS);
1421 		break;
1422 
1423 	case SPECTRE_V2_LFENCE:
1424 	case SPECTRE_V2_EIBRS_LFENCE:
1425 		setup_force_cpu_cap(X86_FEATURE_RETPOLINE_LFENCE);
1426 		fallthrough;
1427 
1428 	case SPECTRE_V2_RETPOLINE:
1429 	case SPECTRE_V2_EIBRS_RETPOLINE:
1430 		setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
1431 		break;
1432 	}
1433 
1434 	/*
1435 	 * Disable alternate RSB predictions in kernel when indirect CALLs and
1436 	 * JMPs gets protection against BHI and Intramode-BTI, but RET
1437 	 * prediction from a non-RSB predictor is still a risk.
1438 	 */
1439 	if (mode == SPECTRE_V2_EIBRS_LFENCE ||
1440 	    mode == SPECTRE_V2_EIBRS_RETPOLINE ||
1441 	    mode == SPECTRE_V2_RETPOLINE)
1442 		spec_ctrl_disable_kernel_rrsba();
1443 
1444 	spectre_v2_enabled = mode;
1445 	pr_info("%s\n", spectre_v2_strings[mode]);
1446 
1447 	/*
1448 	 * If Spectre v2 protection has been enabled, fill the RSB during a
1449 	 * context switch.  In general there are two types of RSB attacks
1450 	 * across context switches, for which the CALLs/RETs may be unbalanced.
1451 	 *
1452 	 * 1) RSB underflow
1453 	 *
1454 	 *    Some Intel parts have "bottomless RSB".  When the RSB is empty,
1455 	 *    speculated return targets may come from the branch predictor,
1456 	 *    which could have a user-poisoned BTB or BHB entry.
1457 	 *
1458 	 *    AMD has it even worse: *all* returns are speculated from the BTB,
1459 	 *    regardless of the state of the RSB.
1460 	 *
1461 	 *    When IBRS or eIBRS is enabled, the "user -> kernel" attack
1462 	 *    scenario is mitigated by the IBRS branch prediction isolation
1463 	 *    properties, so the RSB buffer filling wouldn't be necessary to
1464 	 *    protect against this type of attack.
1465 	 *
1466 	 *    The "user -> user" attack scenario is mitigated by RSB filling.
1467 	 *
1468 	 * 2) Poisoned RSB entry
1469 	 *
1470 	 *    If the 'next' in-kernel return stack is shorter than 'prev',
1471 	 *    'next' could be tricked into speculating with a user-poisoned RSB
1472 	 *    entry.
1473 	 *
1474 	 *    The "user -> kernel" attack scenario is mitigated by SMEP and
1475 	 *    eIBRS.
1476 	 *
1477 	 *    The "user -> user" scenario, also known as SpectreBHB, requires
1478 	 *    RSB clearing.
1479 	 *
1480 	 * So to mitigate all cases, unconditionally fill RSB on context
1481 	 * switches.
1482 	 *
1483 	 * FIXME: Is this pointless for retbleed-affected AMD?
1484 	 */
1485 	setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
1486 	pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n");
1487 
1488 	spectre_v2_determine_rsb_fill_type_at_vmexit(mode);
1489 
1490 	/*
1491 	 * Retpoline protects the kernel, but doesn't protect firmware.  IBRS
1492 	 * and Enhanced IBRS protect firmware too, so enable IBRS around
1493 	 * firmware calls only when IBRS / Enhanced IBRS aren't otherwise
1494 	 * enabled.
1495 	 *
1496 	 * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because
1497 	 * the user might select retpoline on the kernel command line and if
1498 	 * the CPU supports Enhanced IBRS, kernel might un-intentionally not
1499 	 * enable IBRS around firmware calls.
1500 	 */
1501 	if (boot_cpu_has(X86_FEATURE_IBRS) && !spectre_v2_in_ibrs_mode(mode)) {
1502 		setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
1503 		pr_info("Enabling Restricted Speculation for firmware calls\n");
1504 	}
1505 
1506 	/* Set up IBPB and STIBP depending on the general spectre V2 command */
1507 	spectre_v2_cmd = cmd;
1508 }
1509 
update_stibp_msr(void * __unused)1510 static void update_stibp_msr(void * __unused)
1511 {
1512 	u64 val = spec_ctrl_current() | (x86_spec_ctrl_base & SPEC_CTRL_STIBP);
1513 	write_spec_ctrl_current(val, true);
1514 }
1515 
1516 /* Update x86_spec_ctrl_base in case SMT state changed. */
update_stibp_strict(void)1517 static void update_stibp_strict(void)
1518 {
1519 	u64 mask = x86_spec_ctrl_base & ~SPEC_CTRL_STIBP;
1520 
1521 	if (sched_smt_active())
1522 		mask |= SPEC_CTRL_STIBP;
1523 
1524 	if (mask == x86_spec_ctrl_base)
1525 		return;
1526 
1527 	pr_info("Update user space SMT mitigation: STIBP %s\n",
1528 		mask & SPEC_CTRL_STIBP ? "always-on" : "off");
1529 	x86_spec_ctrl_base = mask;
1530 	on_each_cpu(update_stibp_msr, NULL, 1);
1531 }
1532 
1533 /* Update the static key controlling the evaluation of TIF_SPEC_IB */
update_indir_branch_cond(void)1534 static void update_indir_branch_cond(void)
1535 {
1536 	if (sched_smt_active())
1537 		static_branch_enable(&switch_to_cond_stibp);
1538 	else
1539 		static_branch_disable(&switch_to_cond_stibp);
1540 }
1541 
1542 #undef pr_fmt
1543 #define pr_fmt(fmt) fmt
1544 
1545 /* Update the static key controlling the MDS CPU buffer clear in idle */
update_mds_branch_idle(void)1546 static void update_mds_branch_idle(void)
1547 {
1548 	u64 ia32_cap = x86_read_arch_cap_msr();
1549 
1550 	/*
1551 	 * Enable the idle clearing if SMT is active on CPUs which are
1552 	 * affected only by MSBDS and not any other MDS variant.
1553 	 *
1554 	 * The other variants cannot be mitigated when SMT is enabled, so
1555 	 * clearing the buffers on idle just to prevent the Store Buffer
1556 	 * repartitioning leak would be a window dressing exercise.
1557 	 */
1558 	if (!boot_cpu_has_bug(X86_BUG_MSBDS_ONLY))
1559 		return;
1560 
1561 	if (sched_smt_active()) {
1562 		static_branch_enable(&mds_idle_clear);
1563 	} else if (mmio_mitigation == MMIO_MITIGATION_OFF ||
1564 		   (ia32_cap & ARCH_CAP_FBSDP_NO)) {
1565 		static_branch_disable(&mds_idle_clear);
1566 	}
1567 }
1568 
1569 #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"
1570 #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"
1571 #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"
1572 
cpu_bugs_smt_update(void)1573 void cpu_bugs_smt_update(void)
1574 {
1575 	mutex_lock(&spec_ctrl_mutex);
1576 
1577 	if (sched_smt_active() && unprivileged_ebpf_enabled() &&
1578 	    spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
1579 		pr_warn_once(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG);
1580 
1581 	switch (spectre_v2_user_stibp) {
1582 	case SPECTRE_V2_USER_NONE:
1583 		break;
1584 	case SPECTRE_V2_USER_STRICT:
1585 	case SPECTRE_V2_USER_STRICT_PREFERRED:
1586 		update_stibp_strict();
1587 		break;
1588 	case SPECTRE_V2_USER_PRCTL:
1589 	case SPECTRE_V2_USER_SECCOMP:
1590 		update_indir_branch_cond();
1591 		break;
1592 	}
1593 
1594 	switch (mds_mitigation) {
1595 	case MDS_MITIGATION_FULL:
1596 	case MDS_MITIGATION_VMWERV:
1597 		if (sched_smt_active() && !boot_cpu_has(X86_BUG_MSBDS_ONLY))
1598 			pr_warn_once(MDS_MSG_SMT);
1599 		update_mds_branch_idle();
1600 		break;
1601 	case MDS_MITIGATION_OFF:
1602 		break;
1603 	}
1604 
1605 	switch (taa_mitigation) {
1606 	case TAA_MITIGATION_VERW:
1607 	case TAA_MITIGATION_UCODE_NEEDED:
1608 		if (sched_smt_active())
1609 			pr_warn_once(TAA_MSG_SMT);
1610 		break;
1611 	case TAA_MITIGATION_TSX_DISABLED:
1612 	case TAA_MITIGATION_OFF:
1613 		break;
1614 	}
1615 
1616 	switch (mmio_mitigation) {
1617 	case MMIO_MITIGATION_VERW:
1618 	case MMIO_MITIGATION_UCODE_NEEDED:
1619 		if (sched_smt_active())
1620 			pr_warn_once(MMIO_MSG_SMT);
1621 		break;
1622 	case MMIO_MITIGATION_OFF:
1623 		break;
1624 	}
1625 
1626 	mutex_unlock(&spec_ctrl_mutex);
1627 }
1628 
1629 #undef pr_fmt
1630 #define pr_fmt(fmt)	"Speculative Store Bypass: " fmt
1631 
1632 static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
1633 
1634 /* The kernel command line selection */
1635 enum ssb_mitigation_cmd {
1636 	SPEC_STORE_BYPASS_CMD_NONE,
1637 	SPEC_STORE_BYPASS_CMD_AUTO,
1638 	SPEC_STORE_BYPASS_CMD_ON,
1639 	SPEC_STORE_BYPASS_CMD_PRCTL,
1640 	SPEC_STORE_BYPASS_CMD_SECCOMP,
1641 };
1642 
1643 static const char * const ssb_strings[] = {
1644 	[SPEC_STORE_BYPASS_NONE]	= "Vulnerable",
1645 	[SPEC_STORE_BYPASS_DISABLE]	= "Mitigation: Speculative Store Bypass disabled",
1646 	[SPEC_STORE_BYPASS_PRCTL]	= "Mitigation: Speculative Store Bypass disabled via prctl",
1647 	[SPEC_STORE_BYPASS_SECCOMP]	= "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
1648 };
1649 
1650 static const struct {
1651 	const char *option;
1652 	enum ssb_mitigation_cmd cmd;
1653 } ssb_mitigation_options[]  __initconst = {
1654 	{ "auto",	SPEC_STORE_BYPASS_CMD_AUTO },    /* Platform decides */
1655 	{ "on",		SPEC_STORE_BYPASS_CMD_ON },      /* Disable Speculative Store Bypass */
1656 	{ "off",	SPEC_STORE_BYPASS_CMD_NONE },    /* Don't touch Speculative Store Bypass */
1657 	{ "prctl",	SPEC_STORE_BYPASS_CMD_PRCTL },   /* Disable Speculative Store Bypass via prctl */
1658 	{ "seccomp",	SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */
1659 };
1660 
ssb_parse_cmdline(void)1661 static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
1662 {
1663 	enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO;
1664 	char arg[20];
1665 	int ret, i;
1666 
1667 	if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable") ||
1668 	    cpu_mitigations_off()) {
1669 		return SPEC_STORE_BYPASS_CMD_NONE;
1670 	} else {
1671 		ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable",
1672 					  arg, sizeof(arg));
1673 		if (ret < 0)
1674 			return SPEC_STORE_BYPASS_CMD_AUTO;
1675 
1676 		for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) {
1677 			if (!match_option(arg, ret, ssb_mitigation_options[i].option))
1678 				continue;
1679 
1680 			cmd = ssb_mitigation_options[i].cmd;
1681 			break;
1682 		}
1683 
1684 		if (i >= ARRAY_SIZE(ssb_mitigation_options)) {
1685 			pr_err("unknown option (%s). Switching to AUTO select\n", arg);
1686 			return SPEC_STORE_BYPASS_CMD_AUTO;
1687 		}
1688 	}
1689 
1690 	return cmd;
1691 }
1692 
__ssb_select_mitigation(void)1693 static enum ssb_mitigation __init __ssb_select_mitigation(void)
1694 {
1695 	enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
1696 	enum ssb_mitigation_cmd cmd;
1697 
1698 	if (!boot_cpu_has(X86_FEATURE_SSBD))
1699 		return mode;
1700 
1701 	cmd = ssb_parse_cmdline();
1702 	if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) &&
1703 	    (cmd == SPEC_STORE_BYPASS_CMD_NONE ||
1704 	     cmd == SPEC_STORE_BYPASS_CMD_AUTO))
1705 		return mode;
1706 
1707 	switch (cmd) {
1708 	case SPEC_STORE_BYPASS_CMD_AUTO:
1709 	case SPEC_STORE_BYPASS_CMD_SECCOMP:
1710 		/*
1711 		 * Choose prctl+seccomp as the default mode if seccomp is
1712 		 * enabled.
1713 		 */
1714 		if (IS_ENABLED(CONFIG_SECCOMP))
1715 			mode = SPEC_STORE_BYPASS_SECCOMP;
1716 		else
1717 			mode = SPEC_STORE_BYPASS_PRCTL;
1718 		break;
1719 	case SPEC_STORE_BYPASS_CMD_ON:
1720 		mode = SPEC_STORE_BYPASS_DISABLE;
1721 		break;
1722 	case SPEC_STORE_BYPASS_CMD_PRCTL:
1723 		mode = SPEC_STORE_BYPASS_PRCTL;
1724 		break;
1725 	case SPEC_STORE_BYPASS_CMD_NONE:
1726 		break;
1727 	}
1728 
1729 	/*
1730 	 * We have three CPU feature flags that are in play here:
1731 	 *  - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
1732 	 *  - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
1733 	 *  - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
1734 	 */
1735 	if (mode == SPEC_STORE_BYPASS_DISABLE) {
1736 		setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
1737 		/*
1738 		 * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
1739 		 * use a completely different MSR and bit dependent on family.
1740 		 */
1741 		if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
1742 		    !static_cpu_has(X86_FEATURE_AMD_SSBD)) {
1743 			x86_amd_ssb_disable();
1744 		} else {
1745 			x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
1746 			write_spec_ctrl_current(x86_spec_ctrl_base, true);
1747 		}
1748 	}
1749 
1750 	return mode;
1751 }
1752 
ssb_select_mitigation(void)1753 static void ssb_select_mitigation(void)
1754 {
1755 	ssb_mode = __ssb_select_mitigation();
1756 
1757 	if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1758 		pr_info("%s\n", ssb_strings[ssb_mode]);
1759 }
1760 
1761 #undef pr_fmt
1762 #define pr_fmt(fmt)     "Speculation prctl: " fmt
1763 
task_update_spec_tif(struct task_struct * tsk)1764 static void task_update_spec_tif(struct task_struct *tsk)
1765 {
1766 	/* Force the update of the real TIF bits */
1767 	set_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE);
1768 
1769 	/*
1770 	 * Immediately update the speculation control MSRs for the current
1771 	 * task, but for a non-current task delay setting the CPU
1772 	 * mitigation until it is scheduled next.
1773 	 *
1774 	 * This can only happen for SECCOMP mitigation. For PRCTL it's
1775 	 * always the current task.
1776 	 */
1777 	if (tsk == current)
1778 		speculation_ctrl_update_current();
1779 }
1780 
ssb_prctl_set(struct task_struct * task,unsigned long ctrl)1781 static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
1782 {
1783 	if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
1784 	    ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
1785 		return -ENXIO;
1786 
1787 	switch (ctrl) {
1788 	case PR_SPEC_ENABLE:
1789 		/* If speculation is force disabled, enable is not allowed */
1790 		if (task_spec_ssb_force_disable(task))
1791 			return -EPERM;
1792 		task_clear_spec_ssb_disable(task);
1793 		task_clear_spec_ssb_noexec(task);
1794 		task_update_spec_tif(task);
1795 		break;
1796 	case PR_SPEC_DISABLE:
1797 		task_set_spec_ssb_disable(task);
1798 		task_clear_spec_ssb_noexec(task);
1799 		task_update_spec_tif(task);
1800 		break;
1801 	case PR_SPEC_FORCE_DISABLE:
1802 		task_set_spec_ssb_disable(task);
1803 		task_set_spec_ssb_force_disable(task);
1804 		task_clear_spec_ssb_noexec(task);
1805 		task_update_spec_tif(task);
1806 		break;
1807 	case PR_SPEC_DISABLE_NOEXEC:
1808 		if (task_spec_ssb_force_disable(task))
1809 			return -EPERM;
1810 		task_set_spec_ssb_disable(task);
1811 		task_set_spec_ssb_noexec(task);
1812 		task_update_spec_tif(task);
1813 		break;
1814 	default:
1815 		return -ERANGE;
1816 	}
1817 	return 0;
1818 }
1819 
is_spec_ib_user_controlled(void)1820 static bool is_spec_ib_user_controlled(void)
1821 {
1822 	return spectre_v2_user_ibpb == SPECTRE_V2_USER_PRCTL ||
1823 		spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
1824 		spectre_v2_user_stibp == SPECTRE_V2_USER_PRCTL ||
1825 		spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP;
1826 }
1827 
ib_prctl_set(struct task_struct * task,unsigned long ctrl)1828 static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)
1829 {
1830 	switch (ctrl) {
1831 	case PR_SPEC_ENABLE:
1832 		if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1833 		    spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
1834 			return 0;
1835 
1836 		/*
1837 		 * With strict mode for both IBPB and STIBP, the instruction
1838 		 * code paths avoid checking this task flag and instead,
1839 		 * unconditionally run the instruction. However, STIBP and IBPB
1840 		 * are independent and either can be set to conditionally
1841 		 * enabled regardless of the mode of the other.
1842 		 *
1843 		 * If either is set to conditional, allow the task flag to be
1844 		 * updated, unless it was force-disabled by a previous prctl
1845 		 * call. Currently, this is possible on an AMD CPU which has the
1846 		 * feature X86_FEATURE_AMD_STIBP_ALWAYS_ON. In this case, if the
1847 		 * kernel is booted with 'spectre_v2_user=seccomp', then
1848 		 * spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP and
1849 		 * spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED.
1850 		 */
1851 		if (!is_spec_ib_user_controlled() ||
1852 		    task_spec_ib_force_disable(task))
1853 			return -EPERM;
1854 
1855 		task_clear_spec_ib_disable(task);
1856 		task_update_spec_tif(task);
1857 		break;
1858 	case PR_SPEC_DISABLE:
1859 	case PR_SPEC_FORCE_DISABLE:
1860 		/*
1861 		 * Indirect branch speculation is always allowed when
1862 		 * mitigation is force disabled.
1863 		 */
1864 		if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1865 		    spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
1866 			return -EPERM;
1867 
1868 		if (!is_spec_ib_user_controlled())
1869 			return 0;
1870 
1871 		task_set_spec_ib_disable(task);
1872 		if (ctrl == PR_SPEC_FORCE_DISABLE)
1873 			task_set_spec_ib_force_disable(task);
1874 		task_update_spec_tif(task);
1875 		if (task == current)
1876 			indirect_branch_prediction_barrier();
1877 		break;
1878 	default:
1879 		return -ERANGE;
1880 	}
1881 	return 0;
1882 }
1883 
arch_prctl_spec_ctrl_set(struct task_struct * task,unsigned long which,unsigned long ctrl)1884 int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
1885 			     unsigned long ctrl)
1886 {
1887 	switch (which) {
1888 	case PR_SPEC_STORE_BYPASS:
1889 		return ssb_prctl_set(task, ctrl);
1890 	case PR_SPEC_INDIRECT_BRANCH:
1891 		return ib_prctl_set(task, ctrl);
1892 	default:
1893 		return -ENODEV;
1894 	}
1895 }
1896 
1897 #ifdef CONFIG_SECCOMP
arch_seccomp_spec_mitigate(struct task_struct * task)1898 void arch_seccomp_spec_mitigate(struct task_struct *task)
1899 {
1900 	if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP)
1901 		ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
1902 	if (spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
1903 	    spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP)
1904 		ib_prctl_set(task, PR_SPEC_FORCE_DISABLE);
1905 }
1906 #endif
1907 
ssb_prctl_get(struct task_struct * task)1908 static int ssb_prctl_get(struct task_struct *task)
1909 {
1910 	switch (ssb_mode) {
1911 	case SPEC_STORE_BYPASS_DISABLE:
1912 		return PR_SPEC_DISABLE;
1913 	case SPEC_STORE_BYPASS_SECCOMP:
1914 	case SPEC_STORE_BYPASS_PRCTL:
1915 		if (task_spec_ssb_force_disable(task))
1916 			return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
1917 		if (task_spec_ssb_noexec(task))
1918 			return PR_SPEC_PRCTL | PR_SPEC_DISABLE_NOEXEC;
1919 		if (task_spec_ssb_disable(task))
1920 			return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
1921 		return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
1922 	default:
1923 		if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1924 			return PR_SPEC_ENABLE;
1925 		return PR_SPEC_NOT_AFFECTED;
1926 	}
1927 }
1928 
ib_prctl_get(struct task_struct * task)1929 static int ib_prctl_get(struct task_struct *task)
1930 {
1931 	if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
1932 		return PR_SPEC_NOT_AFFECTED;
1933 
1934 	if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1935 	    spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
1936 		return PR_SPEC_ENABLE;
1937 	else if (is_spec_ib_user_controlled()) {
1938 		if (task_spec_ib_force_disable(task))
1939 			return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
1940 		if (task_spec_ib_disable(task))
1941 			return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
1942 		return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
1943 	} else if (spectre_v2_user_ibpb == SPECTRE_V2_USER_STRICT ||
1944 	    spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
1945 	    spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED)
1946 		return PR_SPEC_DISABLE;
1947 	else
1948 		return PR_SPEC_NOT_AFFECTED;
1949 }
1950 
arch_prctl_spec_ctrl_get(struct task_struct * task,unsigned long which)1951 int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
1952 {
1953 	switch (which) {
1954 	case PR_SPEC_STORE_BYPASS:
1955 		return ssb_prctl_get(task);
1956 	case PR_SPEC_INDIRECT_BRANCH:
1957 		return ib_prctl_get(task);
1958 	default:
1959 		return -ENODEV;
1960 	}
1961 }
1962 
x86_spec_ctrl_setup_ap(void)1963 void x86_spec_ctrl_setup_ap(void)
1964 {
1965 	if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
1966 		write_spec_ctrl_current(x86_spec_ctrl_base, true);
1967 
1968 	if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
1969 		x86_amd_ssb_disable();
1970 }
1971 
1972 bool itlb_multihit_kvm_mitigation;
1973 EXPORT_SYMBOL_GPL(itlb_multihit_kvm_mitigation);
1974 
1975 #undef pr_fmt
1976 #define pr_fmt(fmt)	"L1TF: " fmt
1977 
1978 /* Default mitigation for L1TF-affected CPUs */
1979 enum l1tf_mitigations l1tf_mitigation __ro_after_init = L1TF_MITIGATION_FLUSH;
1980 #if IS_ENABLED(CONFIG_KVM_INTEL)
1981 EXPORT_SYMBOL_GPL(l1tf_mitigation);
1982 #endif
1983 enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
1984 EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation);
1985 
1986 /*
1987  * These CPUs all support 44bits physical address space internally in the
1988  * cache but CPUID can report a smaller number of physical address bits.
1989  *
1990  * The L1TF mitigation uses the top most address bit for the inversion of
1991  * non present PTEs. When the installed memory reaches into the top most
1992  * address bit due to memory holes, which has been observed on machines
1993  * which report 36bits physical address bits and have 32G RAM installed,
1994  * then the mitigation range check in l1tf_select_mitigation() triggers.
1995  * This is a false positive because the mitigation is still possible due to
1996  * the fact that the cache uses 44bit internally. Use the cache bits
1997  * instead of the reported physical bits and adjust them on the affected
1998  * machines to 44bit if the reported bits are less than 44.
1999  */
override_cache_bits(struct cpuinfo_x86 * c)2000 static void override_cache_bits(struct cpuinfo_x86 *c)
2001 {
2002 	if (c->x86 != 6)
2003 		return;
2004 
2005 	switch (c->x86_model) {
2006 	case INTEL_FAM6_NEHALEM:
2007 	case INTEL_FAM6_WESTMERE:
2008 	case INTEL_FAM6_SANDYBRIDGE:
2009 	case INTEL_FAM6_IVYBRIDGE:
2010 	case INTEL_FAM6_HASWELL:
2011 	case INTEL_FAM6_HASWELL_L:
2012 	case INTEL_FAM6_HASWELL_G:
2013 	case INTEL_FAM6_BROADWELL:
2014 	case INTEL_FAM6_BROADWELL_G:
2015 	case INTEL_FAM6_SKYLAKE_L:
2016 	case INTEL_FAM6_SKYLAKE:
2017 	case INTEL_FAM6_KABYLAKE_L:
2018 	case INTEL_FAM6_KABYLAKE:
2019 		if (c->x86_cache_bits < 44)
2020 			c->x86_cache_bits = 44;
2021 		break;
2022 	}
2023 }
2024 
l1tf_select_mitigation(void)2025 static void __init l1tf_select_mitigation(void)
2026 {
2027 	u64 half_pa;
2028 
2029 	if (!boot_cpu_has_bug(X86_BUG_L1TF))
2030 		return;
2031 
2032 	if (cpu_mitigations_off())
2033 		l1tf_mitigation = L1TF_MITIGATION_OFF;
2034 	else if (cpu_mitigations_auto_nosmt())
2035 		l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
2036 
2037 	override_cache_bits(&boot_cpu_data);
2038 
2039 	switch (l1tf_mitigation) {
2040 	case L1TF_MITIGATION_OFF:
2041 	case L1TF_MITIGATION_FLUSH_NOWARN:
2042 	case L1TF_MITIGATION_FLUSH:
2043 		break;
2044 	case L1TF_MITIGATION_FLUSH_NOSMT:
2045 	case L1TF_MITIGATION_FULL:
2046 		cpu_smt_disable(false);
2047 		break;
2048 	case L1TF_MITIGATION_FULL_FORCE:
2049 		cpu_smt_disable(true);
2050 		break;
2051 	}
2052 
2053 #if CONFIG_PGTABLE_LEVELS == 2
2054 	pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
2055 	return;
2056 #endif
2057 
2058 	half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT;
2059 	if (l1tf_mitigation != L1TF_MITIGATION_OFF &&
2060 			e820__mapped_any(half_pa, ULLONG_MAX - half_pa, E820_TYPE_RAM)) {
2061 		pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
2062 		pr_info("You may make it effective by booting the kernel with mem=%llu parameter.\n",
2063 				half_pa);
2064 		pr_info("However, doing so will make a part of your RAM unusable.\n");
2065 		pr_info("Reading https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html might help you decide.\n");
2066 		return;
2067 	}
2068 
2069 	setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV);
2070 }
2071 
l1tf_cmdline(char * str)2072 static int __init l1tf_cmdline(char *str)
2073 {
2074 	if (!boot_cpu_has_bug(X86_BUG_L1TF))
2075 		return 0;
2076 
2077 	if (!str)
2078 		return -EINVAL;
2079 
2080 	if (!strcmp(str, "off"))
2081 		l1tf_mitigation = L1TF_MITIGATION_OFF;
2082 	else if (!strcmp(str, "flush,nowarn"))
2083 		l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOWARN;
2084 	else if (!strcmp(str, "flush"))
2085 		l1tf_mitigation = L1TF_MITIGATION_FLUSH;
2086 	else if (!strcmp(str, "flush,nosmt"))
2087 		l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
2088 	else if (!strcmp(str, "full"))
2089 		l1tf_mitigation = L1TF_MITIGATION_FULL;
2090 	else if (!strcmp(str, "full,force"))
2091 		l1tf_mitigation = L1TF_MITIGATION_FULL_FORCE;
2092 
2093 	return 0;
2094 }
2095 early_param("l1tf", l1tf_cmdline);
2096 
2097 #undef pr_fmt
2098 #define pr_fmt(fmt) fmt
2099 
2100 #ifdef CONFIG_SYSFS
2101 
2102 #define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
2103 
2104 #if IS_ENABLED(CONFIG_KVM_INTEL)
2105 static const char * const l1tf_vmx_states[] = {
2106 	[VMENTER_L1D_FLUSH_AUTO]		= "auto",
2107 	[VMENTER_L1D_FLUSH_NEVER]		= "vulnerable",
2108 	[VMENTER_L1D_FLUSH_COND]		= "conditional cache flushes",
2109 	[VMENTER_L1D_FLUSH_ALWAYS]		= "cache flushes",
2110 	[VMENTER_L1D_FLUSH_EPT_DISABLED]	= "EPT disabled",
2111 	[VMENTER_L1D_FLUSH_NOT_REQUIRED]	= "flush not necessary"
2112 };
2113 
l1tf_show_state(char * buf)2114 static ssize_t l1tf_show_state(char *buf)
2115 {
2116 	if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO)
2117 		return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
2118 
2119 	if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_EPT_DISABLED ||
2120 	    (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER &&
2121 	     sched_smt_active())) {
2122 		return sprintf(buf, "%s; VMX: %s\n", L1TF_DEFAULT_MSG,
2123 			       l1tf_vmx_states[l1tf_vmx_mitigation]);
2124 	}
2125 
2126 	return sprintf(buf, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG,
2127 		       l1tf_vmx_states[l1tf_vmx_mitigation],
2128 		       sched_smt_active() ? "vulnerable" : "disabled");
2129 }
2130 
itlb_multihit_show_state(char * buf)2131 static ssize_t itlb_multihit_show_state(char *buf)
2132 {
2133 	if (!boot_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
2134 	    !boot_cpu_has(X86_FEATURE_VMX))
2135 		return sprintf(buf, "KVM: Mitigation: VMX unsupported\n");
2136 	else if (!(cr4_read_shadow() & X86_CR4_VMXE))
2137 		return sprintf(buf, "KVM: Mitigation: VMX disabled\n");
2138 	else if (itlb_multihit_kvm_mitigation)
2139 		return sprintf(buf, "KVM: Mitigation: Split huge pages\n");
2140 	else
2141 		return sprintf(buf, "KVM: Vulnerable\n");
2142 }
2143 #else
l1tf_show_state(char * buf)2144 static ssize_t l1tf_show_state(char *buf)
2145 {
2146 	return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
2147 }
2148 
itlb_multihit_show_state(char * buf)2149 static ssize_t itlb_multihit_show_state(char *buf)
2150 {
2151 	return sprintf(buf, "Processor vulnerable\n");
2152 }
2153 #endif
2154 
mds_show_state(char * buf)2155 static ssize_t mds_show_state(char *buf)
2156 {
2157 	if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
2158 		return sprintf(buf, "%s; SMT Host state unknown\n",
2159 			       mds_strings[mds_mitigation]);
2160 	}
2161 
2162 	if (boot_cpu_has(X86_BUG_MSBDS_ONLY)) {
2163 		return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
2164 			       (mds_mitigation == MDS_MITIGATION_OFF ? "vulnerable" :
2165 			        sched_smt_active() ? "mitigated" : "disabled"));
2166 	}
2167 
2168 	return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
2169 		       sched_smt_active() ? "vulnerable" : "disabled");
2170 }
2171 
tsx_async_abort_show_state(char * buf)2172 static ssize_t tsx_async_abort_show_state(char *buf)
2173 {
2174 	if ((taa_mitigation == TAA_MITIGATION_TSX_DISABLED) ||
2175 	    (taa_mitigation == TAA_MITIGATION_OFF))
2176 		return sprintf(buf, "%s\n", taa_strings[taa_mitigation]);
2177 
2178 	if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
2179 		return sprintf(buf, "%s; SMT Host state unknown\n",
2180 			       taa_strings[taa_mitigation]);
2181 	}
2182 
2183 	return sprintf(buf, "%s; SMT %s\n", taa_strings[taa_mitigation],
2184 		       sched_smt_active() ? "vulnerable" : "disabled");
2185 }
2186 
mmio_stale_data_show_state(char * buf)2187 static ssize_t mmio_stale_data_show_state(char *buf)
2188 {
2189 	if (mmio_mitigation == MMIO_MITIGATION_OFF)
2190 		return sysfs_emit(buf, "%s\n", mmio_strings[mmio_mitigation]);
2191 
2192 	if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
2193 		return sysfs_emit(buf, "%s; SMT Host state unknown\n",
2194 				  mmio_strings[mmio_mitigation]);
2195 	}
2196 
2197 	return sysfs_emit(buf, "%s; SMT %s\n", mmio_strings[mmio_mitigation],
2198 			  sched_smt_active() ? "vulnerable" : "disabled");
2199 }
2200 
stibp_state(void)2201 static char *stibp_state(void)
2202 {
2203 	if (spectre_v2_in_ibrs_mode(spectre_v2_enabled))
2204 		return "";
2205 
2206 	switch (spectre_v2_user_stibp) {
2207 	case SPECTRE_V2_USER_NONE:
2208 		return ", STIBP: disabled";
2209 	case SPECTRE_V2_USER_STRICT:
2210 		return ", STIBP: forced";
2211 	case SPECTRE_V2_USER_STRICT_PREFERRED:
2212 		return ", STIBP: always-on";
2213 	case SPECTRE_V2_USER_PRCTL:
2214 	case SPECTRE_V2_USER_SECCOMP:
2215 		if (static_key_enabled(&switch_to_cond_stibp))
2216 			return ", STIBP: conditional";
2217 	}
2218 	return "";
2219 }
2220 
ibpb_state(void)2221 static char *ibpb_state(void)
2222 {
2223 	if (boot_cpu_has(X86_FEATURE_IBPB)) {
2224 		if (static_key_enabled(&switch_mm_always_ibpb))
2225 			return ", IBPB: always-on";
2226 		if (static_key_enabled(&switch_mm_cond_ibpb))
2227 			return ", IBPB: conditional";
2228 		return ", IBPB: disabled";
2229 	}
2230 	return "";
2231 }
2232 
pbrsb_eibrs_state(void)2233 static char *pbrsb_eibrs_state(void)
2234 {
2235 	if (boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB)) {
2236 		if (boot_cpu_has(X86_FEATURE_RSB_VMEXIT_LITE) ||
2237 		    boot_cpu_has(X86_FEATURE_RSB_VMEXIT))
2238 			return ", PBRSB-eIBRS: SW sequence";
2239 		else
2240 			return ", PBRSB-eIBRS: Vulnerable";
2241 	} else {
2242 		return ", PBRSB-eIBRS: Not affected";
2243 	}
2244 }
2245 
spectre_v2_show_state(char * buf)2246 static ssize_t spectre_v2_show_state(char *buf)
2247 {
2248 	if (spectre_v2_enabled == SPECTRE_V2_LFENCE)
2249 		return sprintf(buf, "Vulnerable: LFENCE\n");
2250 
2251 	if (spectre_v2_enabled == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
2252 		return sprintf(buf, "Vulnerable: eIBRS with unprivileged eBPF\n");
2253 
2254 	if (sched_smt_active() && unprivileged_ebpf_enabled() &&
2255 	    spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
2256 		return sprintf(buf, "Vulnerable: eIBRS+LFENCE with unprivileged eBPF and SMT\n");
2257 
2258 	return sprintf(buf, "%s%s%s%s%s%s%s\n",
2259 		       spectre_v2_strings[spectre_v2_enabled],
2260 		       ibpb_state(),
2261 		       boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
2262 		       stibp_state(),
2263 		       boot_cpu_has(X86_FEATURE_RSB_CTXSW) ? ", RSB filling" : "",
2264 		       pbrsb_eibrs_state(),
2265 		       spectre_v2_module_string());
2266 }
2267 
srbds_show_state(char * buf)2268 static ssize_t srbds_show_state(char *buf)
2269 {
2270 	return sprintf(buf, "%s\n", srbds_strings[srbds_mitigation]);
2271 }
2272 
retbleed_show_state(char * buf)2273 static ssize_t retbleed_show_state(char *buf)
2274 {
2275 	if (retbleed_mitigation == RETBLEED_MITIGATION_UNRET) {
2276 	    if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
2277 		boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)
2278 		    return sprintf(buf, "Vulnerable: untrained return thunk on non-Zen uarch\n");
2279 
2280 	    return sprintf(buf, "%s; SMT %s\n",
2281 			   retbleed_strings[retbleed_mitigation],
2282 			   !sched_smt_active() ? "disabled" :
2283 			   spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
2284 			   spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED ?
2285 			   "enabled with STIBP protection" : "vulnerable");
2286 	}
2287 
2288 	return sprintf(buf, "%s\n", retbleed_strings[retbleed_mitigation]);
2289 }
2290 
cpu_show_common(struct device * dev,struct device_attribute * attr,char * buf,unsigned int bug)2291 static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
2292 			       char *buf, unsigned int bug)
2293 {
2294 	if (!boot_cpu_has_bug(bug))
2295 		return sprintf(buf, "Not affected\n");
2296 
2297 	switch (bug) {
2298 	case X86_BUG_CPU_MELTDOWN:
2299 		if (boot_cpu_has(X86_FEATURE_PTI))
2300 			return sprintf(buf, "Mitigation: PTI\n");
2301 
2302 		if (hypervisor_is_type(X86_HYPER_XEN_PV))
2303 			return sprintf(buf, "Unknown (XEN PV detected, hypervisor mitigation required)\n");
2304 
2305 		break;
2306 
2307 	case X86_BUG_SPECTRE_V1:
2308 		return sprintf(buf, "%s\n", spectre_v1_strings[spectre_v1_mitigation]);
2309 
2310 	case X86_BUG_SPECTRE_V2:
2311 		return spectre_v2_show_state(buf);
2312 
2313 	case X86_BUG_SPEC_STORE_BYPASS:
2314 		return sprintf(buf, "%s\n", ssb_strings[ssb_mode]);
2315 
2316 	case X86_BUG_L1TF:
2317 		if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV))
2318 			return l1tf_show_state(buf);
2319 		break;
2320 
2321 	case X86_BUG_MDS:
2322 		return mds_show_state(buf);
2323 
2324 	case X86_BUG_TAA:
2325 		return tsx_async_abort_show_state(buf);
2326 
2327 	case X86_BUG_ITLB_MULTIHIT:
2328 		return itlb_multihit_show_state(buf);
2329 
2330 	case X86_BUG_SRBDS:
2331 		return srbds_show_state(buf);
2332 
2333 	case X86_BUG_MMIO_STALE_DATA:
2334 		return mmio_stale_data_show_state(buf);
2335 
2336 	case X86_BUG_RETBLEED:
2337 		return retbleed_show_state(buf);
2338 
2339 	default:
2340 		break;
2341 	}
2342 
2343 	return sprintf(buf, "Vulnerable\n");
2344 }
2345 
cpu_show_meltdown(struct device * dev,struct device_attribute * attr,char * buf)2346 ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
2347 {
2348 	return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
2349 }
2350 
cpu_show_spectre_v1(struct device * dev,struct device_attribute * attr,char * buf)2351 ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
2352 {
2353 	return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
2354 }
2355 
cpu_show_spectre_v2(struct device * dev,struct device_attribute * attr,char * buf)2356 ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
2357 {
2358 	return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
2359 }
2360 
cpu_show_spec_store_bypass(struct device * dev,struct device_attribute * attr,char * buf)2361 ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
2362 {
2363 	return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS);
2364 }
2365 
cpu_show_l1tf(struct device * dev,struct device_attribute * attr,char * buf)2366 ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
2367 {
2368 	return cpu_show_common(dev, attr, buf, X86_BUG_L1TF);
2369 }
2370 
cpu_show_mds(struct device * dev,struct device_attribute * attr,char * buf)2371 ssize_t cpu_show_mds(struct device *dev, struct device_attribute *attr, char *buf)
2372 {
2373 	return cpu_show_common(dev, attr, buf, X86_BUG_MDS);
2374 }
2375 
cpu_show_tsx_async_abort(struct device * dev,struct device_attribute * attr,char * buf)2376 ssize_t cpu_show_tsx_async_abort(struct device *dev, struct device_attribute *attr, char *buf)
2377 {
2378 	return cpu_show_common(dev, attr, buf, X86_BUG_TAA);
2379 }
2380 
cpu_show_itlb_multihit(struct device * dev,struct device_attribute * attr,char * buf)2381 ssize_t cpu_show_itlb_multihit(struct device *dev, struct device_attribute *attr, char *buf)
2382 {
2383 	return cpu_show_common(dev, attr, buf, X86_BUG_ITLB_MULTIHIT);
2384 }
2385 
cpu_show_srbds(struct device * dev,struct device_attribute * attr,char * buf)2386 ssize_t cpu_show_srbds(struct device *dev, struct device_attribute *attr, char *buf)
2387 {
2388 	return cpu_show_common(dev, attr, buf, X86_BUG_SRBDS);
2389 }
2390 
cpu_show_mmio_stale_data(struct device * dev,struct device_attribute * attr,char * buf)2391 ssize_t cpu_show_mmio_stale_data(struct device *dev, struct device_attribute *attr, char *buf)
2392 {
2393 	return cpu_show_common(dev, attr, buf, X86_BUG_MMIO_STALE_DATA);
2394 }
2395 
cpu_show_retbleed(struct device * dev,struct device_attribute * attr,char * buf)2396 ssize_t cpu_show_retbleed(struct device *dev, struct device_attribute *attr, char *buf)
2397 {
2398 	return cpu_show_common(dev, attr, buf, X86_BUG_RETBLEED);
2399 }
2400 #endif
2401