• 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_eibrs_mode(enum spectre_v2_mitigation mode)1050 static inline bool spectre_v2_in_eibrs_mode(enum spectre_v2_mitigation mode)
1051 {
1052 	return mode == SPECTRE_V2_EIBRS ||
1053 	       mode == SPECTRE_V2_EIBRS_RETPOLINE ||
1054 	       mode == SPECTRE_V2_EIBRS_LFENCE;
1055 }
1056 
spectre_v2_in_ibrs_mode(enum spectre_v2_mitigation mode)1057 static inline bool spectre_v2_in_ibrs_mode(enum spectre_v2_mitigation mode)
1058 {
1059 	return spectre_v2_in_eibrs_mode(mode) || mode == SPECTRE_V2_IBRS;
1060 }
1061 
1062 static void __init
spectre_v2_user_select_mitigation(void)1063 spectre_v2_user_select_mitigation(void)
1064 {
1065 	enum spectre_v2_user_mitigation mode = SPECTRE_V2_USER_NONE;
1066 	bool smt_possible = IS_ENABLED(CONFIG_SMP);
1067 	enum spectre_v2_user_cmd cmd;
1068 
1069 	if (!boot_cpu_has(X86_FEATURE_IBPB) && !boot_cpu_has(X86_FEATURE_STIBP))
1070 		return;
1071 
1072 	if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
1073 	    cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
1074 		smt_possible = false;
1075 
1076 	cmd = spectre_v2_parse_user_cmdline();
1077 	switch (cmd) {
1078 	case SPECTRE_V2_USER_CMD_NONE:
1079 		goto set_mode;
1080 	case SPECTRE_V2_USER_CMD_FORCE:
1081 		mode = SPECTRE_V2_USER_STRICT;
1082 		break;
1083 	case SPECTRE_V2_USER_CMD_PRCTL:
1084 	case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
1085 		mode = SPECTRE_V2_USER_PRCTL;
1086 		break;
1087 	case SPECTRE_V2_USER_CMD_AUTO:
1088 	case SPECTRE_V2_USER_CMD_SECCOMP:
1089 	case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
1090 		if (IS_ENABLED(CONFIG_SECCOMP))
1091 			mode = SPECTRE_V2_USER_SECCOMP;
1092 		else
1093 			mode = SPECTRE_V2_USER_PRCTL;
1094 		break;
1095 	}
1096 
1097 	/* Initialize Indirect Branch Prediction Barrier */
1098 	if (boot_cpu_has(X86_FEATURE_IBPB)) {
1099 		setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
1100 
1101 		spectre_v2_user_ibpb = mode;
1102 		switch (cmd) {
1103 		case SPECTRE_V2_USER_CMD_FORCE:
1104 		case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
1105 		case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
1106 			static_branch_enable(&switch_mm_always_ibpb);
1107 			spectre_v2_user_ibpb = SPECTRE_V2_USER_STRICT;
1108 			break;
1109 		case SPECTRE_V2_USER_CMD_PRCTL:
1110 		case SPECTRE_V2_USER_CMD_AUTO:
1111 		case SPECTRE_V2_USER_CMD_SECCOMP:
1112 			static_branch_enable(&switch_mm_cond_ibpb);
1113 			break;
1114 		default:
1115 			break;
1116 		}
1117 
1118 		pr_info("mitigation: Enabling %s Indirect Branch Prediction Barrier\n",
1119 			static_key_enabled(&switch_mm_always_ibpb) ?
1120 			"always-on" : "conditional");
1121 	}
1122 
1123 	/*
1124 	 * If no STIBP, enhanced IBRS is enabled, or SMT impossible, STIBP
1125 	 * is not required.
1126 	 *
1127 	 * Enhanced IBRS also protects against cross-thread branch target
1128 	 * injection in user-mode as the IBRS bit remains always set which
1129 	 * implicitly enables cross-thread protections.  However, in legacy IBRS
1130 	 * mode, the IBRS bit is set only on kernel entry and cleared on return
1131 	 * to userspace. This disables the implicit cross-thread protection,
1132 	 * so allow for STIBP to be selected in that case.
1133 	 */
1134 	if (!boot_cpu_has(X86_FEATURE_STIBP) ||
1135 	    !smt_possible ||
1136 	    spectre_v2_in_eibrs_mode(spectre_v2_enabled))
1137 		return;
1138 
1139 	/*
1140 	 * At this point, an STIBP mode other than "off" has been set.
1141 	 * If STIBP support is not being forced, check if STIBP always-on
1142 	 * is preferred.
1143 	 */
1144 	if (mode != SPECTRE_V2_USER_STRICT &&
1145 	    boot_cpu_has(X86_FEATURE_AMD_STIBP_ALWAYS_ON))
1146 		mode = SPECTRE_V2_USER_STRICT_PREFERRED;
1147 
1148 	if (retbleed_mitigation == RETBLEED_MITIGATION_UNRET) {
1149 		if (mode != SPECTRE_V2_USER_STRICT &&
1150 		    mode != SPECTRE_V2_USER_STRICT_PREFERRED)
1151 			pr_info("Selecting STIBP always-on mode to complement retbleed mitigation\n");
1152 		mode = SPECTRE_V2_USER_STRICT_PREFERRED;
1153 	}
1154 
1155 	spectre_v2_user_stibp = mode;
1156 
1157 set_mode:
1158 	pr_info("%s\n", spectre_v2_user_strings[mode]);
1159 }
1160 
1161 static const char * const spectre_v2_strings[] = {
1162 	[SPECTRE_V2_NONE]			= "Vulnerable",
1163 	[SPECTRE_V2_RETPOLINE]			= "Mitigation: Retpolines",
1164 	[SPECTRE_V2_LFENCE]			= "Mitigation: LFENCE",
1165 	[SPECTRE_V2_EIBRS]			= "Mitigation: Enhanced IBRS",
1166 	[SPECTRE_V2_EIBRS_LFENCE]		= "Mitigation: Enhanced IBRS + LFENCE",
1167 	[SPECTRE_V2_EIBRS_RETPOLINE]		= "Mitigation: Enhanced IBRS + Retpolines",
1168 	[SPECTRE_V2_IBRS]			= "Mitigation: IBRS",
1169 };
1170 
1171 static const struct {
1172 	const char *option;
1173 	enum spectre_v2_mitigation_cmd cmd;
1174 	bool secure;
1175 } mitigation_options[] __initconst = {
1176 	{ "off",		SPECTRE_V2_CMD_NONE,		  false },
1177 	{ "on",			SPECTRE_V2_CMD_FORCE,		  true  },
1178 	{ "retpoline",		SPECTRE_V2_CMD_RETPOLINE,	  false },
1179 	{ "retpoline,amd",	SPECTRE_V2_CMD_RETPOLINE_LFENCE,  false },
1180 	{ "retpoline,lfence",	SPECTRE_V2_CMD_RETPOLINE_LFENCE,  false },
1181 	{ "retpoline,generic",	SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
1182 	{ "eibrs",		SPECTRE_V2_CMD_EIBRS,		  false },
1183 	{ "eibrs,lfence",	SPECTRE_V2_CMD_EIBRS_LFENCE,	  false },
1184 	{ "eibrs,retpoline",	SPECTRE_V2_CMD_EIBRS_RETPOLINE,	  false },
1185 	{ "auto",		SPECTRE_V2_CMD_AUTO,		  false },
1186 	{ "ibrs",		SPECTRE_V2_CMD_IBRS,              false },
1187 };
1188 
spec_v2_print_cond(const char * reason,bool secure)1189 static void __init spec_v2_print_cond(const char *reason, bool secure)
1190 {
1191 	if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
1192 		pr_info("%s selected on command line.\n", reason);
1193 }
1194 
spectre_v2_parse_cmdline(void)1195 static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
1196 {
1197 	enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
1198 	char arg[20];
1199 	int ret, i;
1200 
1201 	if (cmdline_find_option_bool(boot_command_line, "nospectre_v2") ||
1202 	    cpu_mitigations_off())
1203 		return SPECTRE_V2_CMD_NONE;
1204 
1205 	ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
1206 	if (ret < 0)
1207 		return SPECTRE_V2_CMD_AUTO;
1208 
1209 	for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
1210 		if (!match_option(arg, ret, mitigation_options[i].option))
1211 			continue;
1212 		cmd = mitigation_options[i].cmd;
1213 		break;
1214 	}
1215 
1216 	if (i >= ARRAY_SIZE(mitigation_options)) {
1217 		pr_err("unknown option (%s). Switching to AUTO select\n", arg);
1218 		return SPECTRE_V2_CMD_AUTO;
1219 	}
1220 
1221 	if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
1222 	     cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE ||
1223 	     cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC ||
1224 	     cmd == SPECTRE_V2_CMD_EIBRS_LFENCE ||
1225 	     cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) &&
1226 	    !IS_ENABLED(CONFIG_RETPOLINE)) {
1227 		pr_err("%s selected but not compiled in. Switching to AUTO select\n",
1228 		       mitigation_options[i].option);
1229 		return SPECTRE_V2_CMD_AUTO;
1230 	}
1231 
1232 	if ((cmd == SPECTRE_V2_CMD_EIBRS ||
1233 	     cmd == SPECTRE_V2_CMD_EIBRS_LFENCE ||
1234 	     cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) &&
1235 	    !boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
1236 		pr_err("%s selected but CPU doesn't have eIBRS. Switching to AUTO select\n",
1237 		       mitigation_options[i].option);
1238 		return SPECTRE_V2_CMD_AUTO;
1239 	}
1240 
1241 	if ((cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE ||
1242 	     cmd == SPECTRE_V2_CMD_EIBRS_LFENCE) &&
1243 	    !boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
1244 		pr_err("%s selected, but CPU doesn't have a serializing LFENCE. Switching to AUTO select\n",
1245 		       mitigation_options[i].option);
1246 		return SPECTRE_V2_CMD_AUTO;
1247 	}
1248 
1249 	if (cmd == SPECTRE_V2_CMD_IBRS && !IS_ENABLED(CONFIG_CPU_IBRS_ENTRY)) {
1250 		pr_err("%s selected but not compiled in. Switching to AUTO select\n",
1251 		       mitigation_options[i].option);
1252 		return SPECTRE_V2_CMD_AUTO;
1253 	}
1254 
1255 	if (cmd == SPECTRE_V2_CMD_IBRS && boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
1256 		pr_err("%s selected but not Intel CPU. Switching to AUTO select\n",
1257 		       mitigation_options[i].option);
1258 		return SPECTRE_V2_CMD_AUTO;
1259 	}
1260 
1261 	if (cmd == SPECTRE_V2_CMD_IBRS && !boot_cpu_has(X86_FEATURE_IBRS)) {
1262 		pr_err("%s selected but CPU doesn't have IBRS. Switching to AUTO select\n",
1263 		       mitigation_options[i].option);
1264 		return SPECTRE_V2_CMD_AUTO;
1265 	}
1266 
1267 	if (cmd == SPECTRE_V2_CMD_IBRS && boot_cpu_has(X86_FEATURE_XENPV)) {
1268 		pr_err("%s selected but running as XenPV guest. Switching to AUTO select\n",
1269 		       mitigation_options[i].option);
1270 		return SPECTRE_V2_CMD_AUTO;
1271 	}
1272 
1273 	spec_v2_print_cond(mitigation_options[i].option,
1274 			   mitigation_options[i].secure);
1275 	return cmd;
1276 }
1277 
spectre_v2_select_retpoline(void)1278 static enum spectre_v2_mitigation __init spectre_v2_select_retpoline(void)
1279 {
1280 	if (!IS_ENABLED(CONFIG_RETPOLINE)) {
1281 		pr_err("Kernel not compiled with retpoline; no mitigation available!");
1282 		return SPECTRE_V2_NONE;
1283 	}
1284 
1285 	return SPECTRE_V2_RETPOLINE;
1286 }
1287 
1288 /* Disable in-kernel use of non-RSB RET predictors */
spec_ctrl_disable_kernel_rrsba(void)1289 static void __init spec_ctrl_disable_kernel_rrsba(void)
1290 {
1291 	u64 ia32_cap;
1292 
1293 	if (!boot_cpu_has(X86_FEATURE_RRSBA_CTRL))
1294 		return;
1295 
1296 	ia32_cap = x86_read_arch_cap_msr();
1297 
1298 	if (ia32_cap & ARCH_CAP_RRSBA) {
1299 		x86_spec_ctrl_base |= SPEC_CTRL_RRSBA_DIS_S;
1300 		write_spec_ctrl_current(x86_spec_ctrl_base, true);
1301 	}
1302 }
1303 
spectre_v2_determine_rsb_fill_type_at_vmexit(enum spectre_v2_mitigation mode)1304 static void __init spectre_v2_determine_rsb_fill_type_at_vmexit(enum spectre_v2_mitigation mode)
1305 {
1306 	/*
1307 	 * Similar to context switches, there are two types of RSB attacks
1308 	 * after VM exit:
1309 	 *
1310 	 * 1) RSB underflow
1311 	 *
1312 	 * 2) Poisoned RSB entry
1313 	 *
1314 	 * When retpoline is enabled, both are mitigated by filling/clearing
1315 	 * the RSB.
1316 	 *
1317 	 * When IBRS is enabled, while #1 would be mitigated by the IBRS branch
1318 	 * prediction isolation protections, RSB still needs to be cleared
1319 	 * because of #2.  Note that SMEP provides no protection here, unlike
1320 	 * user-space-poisoned RSB entries.
1321 	 *
1322 	 * eIBRS should protect against RSB poisoning, but if the EIBRS_PBRSB
1323 	 * bug is present then a LITE version of RSB protection is required,
1324 	 * just a single call needs to retire before a RET is executed.
1325 	 */
1326 	switch (mode) {
1327 	case SPECTRE_V2_NONE:
1328 		return;
1329 
1330 	case SPECTRE_V2_EIBRS_LFENCE:
1331 	case SPECTRE_V2_EIBRS:
1332 		if (boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB)) {
1333 			setup_force_cpu_cap(X86_FEATURE_RSB_VMEXIT_LITE);
1334 			pr_info("Spectre v2 / PBRSB-eIBRS: Retire a single CALL on VMEXIT\n");
1335 		}
1336 		return;
1337 
1338 	case SPECTRE_V2_EIBRS_RETPOLINE:
1339 	case SPECTRE_V2_RETPOLINE:
1340 	case SPECTRE_V2_LFENCE:
1341 	case SPECTRE_V2_IBRS:
1342 		setup_force_cpu_cap(X86_FEATURE_RSB_VMEXIT);
1343 		pr_info("Spectre v2 / SpectreRSB : Filling RSB on VMEXIT\n");
1344 		return;
1345 	}
1346 
1347 	pr_warn_once("Unknown Spectre v2 mode, disabling RSB mitigation at VM exit");
1348 	dump_stack();
1349 }
1350 
spectre_v2_select_mitigation(void)1351 static void __init spectre_v2_select_mitigation(void)
1352 {
1353 	enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
1354 	enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
1355 
1356 	/*
1357 	 * If the CPU is not affected and the command line mode is NONE or AUTO
1358 	 * then nothing to do.
1359 	 */
1360 	if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
1361 	    (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
1362 		return;
1363 
1364 	switch (cmd) {
1365 	case SPECTRE_V2_CMD_NONE:
1366 		return;
1367 
1368 	case SPECTRE_V2_CMD_FORCE:
1369 	case SPECTRE_V2_CMD_AUTO:
1370 		if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
1371 			mode = SPECTRE_V2_EIBRS;
1372 			break;
1373 		}
1374 
1375 		if (IS_ENABLED(CONFIG_CPU_IBRS_ENTRY) &&
1376 		    boot_cpu_has_bug(X86_BUG_RETBLEED) &&
1377 		    retbleed_cmd != RETBLEED_CMD_OFF &&
1378 		    boot_cpu_has(X86_FEATURE_IBRS) &&
1379 		    boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
1380 			mode = SPECTRE_V2_IBRS;
1381 			break;
1382 		}
1383 
1384 		mode = spectre_v2_select_retpoline();
1385 		break;
1386 
1387 	case SPECTRE_V2_CMD_RETPOLINE_LFENCE:
1388 		pr_err(SPECTRE_V2_LFENCE_MSG);
1389 		mode = SPECTRE_V2_LFENCE;
1390 		break;
1391 
1392 	case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
1393 		mode = SPECTRE_V2_RETPOLINE;
1394 		break;
1395 
1396 	case SPECTRE_V2_CMD_RETPOLINE:
1397 		mode = spectre_v2_select_retpoline();
1398 		break;
1399 
1400 	case SPECTRE_V2_CMD_IBRS:
1401 		mode = SPECTRE_V2_IBRS;
1402 		break;
1403 
1404 	case SPECTRE_V2_CMD_EIBRS:
1405 		mode = SPECTRE_V2_EIBRS;
1406 		break;
1407 
1408 	case SPECTRE_V2_CMD_EIBRS_LFENCE:
1409 		mode = SPECTRE_V2_EIBRS_LFENCE;
1410 		break;
1411 
1412 	case SPECTRE_V2_CMD_EIBRS_RETPOLINE:
1413 		mode = SPECTRE_V2_EIBRS_RETPOLINE;
1414 		break;
1415 	}
1416 
1417 	if (mode == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
1418 		pr_err(SPECTRE_V2_EIBRS_EBPF_MSG);
1419 
1420 	if (spectre_v2_in_ibrs_mode(mode)) {
1421 		x86_spec_ctrl_base |= SPEC_CTRL_IBRS;
1422 		write_spec_ctrl_current(x86_spec_ctrl_base, true);
1423 	}
1424 
1425 	switch (mode) {
1426 	case SPECTRE_V2_NONE:
1427 	case SPECTRE_V2_EIBRS:
1428 		break;
1429 
1430 	case SPECTRE_V2_IBRS:
1431 		setup_force_cpu_cap(X86_FEATURE_KERNEL_IBRS);
1432 		break;
1433 
1434 	case SPECTRE_V2_LFENCE:
1435 	case SPECTRE_V2_EIBRS_LFENCE:
1436 		setup_force_cpu_cap(X86_FEATURE_RETPOLINE_LFENCE);
1437 		fallthrough;
1438 
1439 	case SPECTRE_V2_RETPOLINE:
1440 	case SPECTRE_V2_EIBRS_RETPOLINE:
1441 		setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
1442 		break;
1443 	}
1444 
1445 	/*
1446 	 * Disable alternate RSB predictions in kernel when indirect CALLs and
1447 	 * JMPs gets protection against BHI and Intramode-BTI, but RET
1448 	 * prediction from a non-RSB predictor is still a risk.
1449 	 */
1450 	if (mode == SPECTRE_V2_EIBRS_LFENCE ||
1451 	    mode == SPECTRE_V2_EIBRS_RETPOLINE ||
1452 	    mode == SPECTRE_V2_RETPOLINE)
1453 		spec_ctrl_disable_kernel_rrsba();
1454 
1455 	spectre_v2_enabled = mode;
1456 	pr_info("%s\n", spectre_v2_strings[mode]);
1457 
1458 	/*
1459 	 * If Spectre v2 protection has been enabled, fill the RSB during a
1460 	 * context switch.  In general there are two types of RSB attacks
1461 	 * across context switches, for which the CALLs/RETs may be unbalanced.
1462 	 *
1463 	 * 1) RSB underflow
1464 	 *
1465 	 *    Some Intel parts have "bottomless RSB".  When the RSB is empty,
1466 	 *    speculated return targets may come from the branch predictor,
1467 	 *    which could have a user-poisoned BTB or BHB entry.
1468 	 *
1469 	 *    AMD has it even worse: *all* returns are speculated from the BTB,
1470 	 *    regardless of the state of the RSB.
1471 	 *
1472 	 *    When IBRS or eIBRS is enabled, the "user -> kernel" attack
1473 	 *    scenario is mitigated by the IBRS branch prediction isolation
1474 	 *    properties, so the RSB buffer filling wouldn't be necessary to
1475 	 *    protect against this type of attack.
1476 	 *
1477 	 *    The "user -> user" attack scenario is mitigated by RSB filling.
1478 	 *
1479 	 * 2) Poisoned RSB entry
1480 	 *
1481 	 *    If the 'next' in-kernel return stack is shorter than 'prev',
1482 	 *    'next' could be tricked into speculating with a user-poisoned RSB
1483 	 *    entry.
1484 	 *
1485 	 *    The "user -> kernel" attack scenario is mitigated by SMEP and
1486 	 *    eIBRS.
1487 	 *
1488 	 *    The "user -> user" scenario, also known as SpectreBHB, requires
1489 	 *    RSB clearing.
1490 	 *
1491 	 * So to mitigate all cases, unconditionally fill RSB on context
1492 	 * switches.
1493 	 *
1494 	 * FIXME: Is this pointless for retbleed-affected AMD?
1495 	 */
1496 	setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
1497 	pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n");
1498 
1499 	spectre_v2_determine_rsb_fill_type_at_vmexit(mode);
1500 
1501 	/*
1502 	 * Retpoline protects the kernel, but doesn't protect firmware.  IBRS
1503 	 * and Enhanced IBRS protect firmware too, so enable IBRS around
1504 	 * firmware calls only when IBRS / Enhanced IBRS aren't otherwise
1505 	 * enabled.
1506 	 *
1507 	 * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because
1508 	 * the user might select retpoline on the kernel command line and if
1509 	 * the CPU supports Enhanced IBRS, kernel might un-intentionally not
1510 	 * enable IBRS around firmware calls.
1511 	 */
1512 	if (boot_cpu_has(X86_FEATURE_IBRS) && !spectre_v2_in_ibrs_mode(mode)) {
1513 		setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
1514 		pr_info("Enabling Restricted Speculation for firmware calls\n");
1515 	}
1516 
1517 	/* Set up IBPB and STIBP depending on the general spectre V2 command */
1518 	spectre_v2_cmd = cmd;
1519 }
1520 
update_stibp_msr(void * __unused)1521 static void update_stibp_msr(void * __unused)
1522 {
1523 	u64 val = spec_ctrl_current() | (x86_spec_ctrl_base & SPEC_CTRL_STIBP);
1524 	write_spec_ctrl_current(val, true);
1525 }
1526 
1527 /* Update x86_spec_ctrl_base in case SMT state changed. */
update_stibp_strict(void)1528 static void update_stibp_strict(void)
1529 {
1530 	u64 mask = x86_spec_ctrl_base & ~SPEC_CTRL_STIBP;
1531 
1532 	if (sched_smt_active())
1533 		mask |= SPEC_CTRL_STIBP;
1534 
1535 	if (mask == x86_spec_ctrl_base)
1536 		return;
1537 
1538 	pr_info("Update user space SMT mitigation: STIBP %s\n",
1539 		mask & SPEC_CTRL_STIBP ? "always-on" : "off");
1540 	x86_spec_ctrl_base = mask;
1541 	on_each_cpu(update_stibp_msr, NULL, 1);
1542 }
1543 
1544 /* Update the static key controlling the evaluation of TIF_SPEC_IB */
update_indir_branch_cond(void)1545 static void update_indir_branch_cond(void)
1546 {
1547 	if (sched_smt_active())
1548 		static_branch_enable(&switch_to_cond_stibp);
1549 	else
1550 		static_branch_disable(&switch_to_cond_stibp);
1551 }
1552 
1553 #undef pr_fmt
1554 #define pr_fmt(fmt) fmt
1555 
1556 /* Update the static key controlling the MDS CPU buffer clear in idle */
update_mds_branch_idle(void)1557 static void update_mds_branch_idle(void)
1558 {
1559 	u64 ia32_cap = x86_read_arch_cap_msr();
1560 
1561 	/*
1562 	 * Enable the idle clearing if SMT is active on CPUs which are
1563 	 * affected only by MSBDS and not any other MDS variant.
1564 	 *
1565 	 * The other variants cannot be mitigated when SMT is enabled, so
1566 	 * clearing the buffers on idle just to prevent the Store Buffer
1567 	 * repartitioning leak would be a window dressing exercise.
1568 	 */
1569 	if (!boot_cpu_has_bug(X86_BUG_MSBDS_ONLY))
1570 		return;
1571 
1572 	if (sched_smt_active()) {
1573 		static_branch_enable(&mds_idle_clear);
1574 	} else if (mmio_mitigation == MMIO_MITIGATION_OFF ||
1575 		   (ia32_cap & ARCH_CAP_FBSDP_NO)) {
1576 		static_branch_disable(&mds_idle_clear);
1577 	}
1578 }
1579 
1580 #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"
1581 #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"
1582 #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"
1583 
cpu_bugs_smt_update(void)1584 void cpu_bugs_smt_update(void)
1585 {
1586 	mutex_lock(&spec_ctrl_mutex);
1587 
1588 	if (sched_smt_active() && unprivileged_ebpf_enabled() &&
1589 	    spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
1590 		pr_warn_once(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG);
1591 
1592 	switch (spectre_v2_user_stibp) {
1593 	case SPECTRE_V2_USER_NONE:
1594 		break;
1595 	case SPECTRE_V2_USER_STRICT:
1596 	case SPECTRE_V2_USER_STRICT_PREFERRED:
1597 		update_stibp_strict();
1598 		break;
1599 	case SPECTRE_V2_USER_PRCTL:
1600 	case SPECTRE_V2_USER_SECCOMP:
1601 		update_indir_branch_cond();
1602 		break;
1603 	}
1604 
1605 	switch (mds_mitigation) {
1606 	case MDS_MITIGATION_FULL:
1607 	case MDS_MITIGATION_VMWERV:
1608 		if (sched_smt_active() && !boot_cpu_has(X86_BUG_MSBDS_ONLY))
1609 			pr_warn_once(MDS_MSG_SMT);
1610 		update_mds_branch_idle();
1611 		break;
1612 	case MDS_MITIGATION_OFF:
1613 		break;
1614 	}
1615 
1616 	switch (taa_mitigation) {
1617 	case TAA_MITIGATION_VERW:
1618 	case TAA_MITIGATION_UCODE_NEEDED:
1619 		if (sched_smt_active())
1620 			pr_warn_once(TAA_MSG_SMT);
1621 		break;
1622 	case TAA_MITIGATION_TSX_DISABLED:
1623 	case TAA_MITIGATION_OFF:
1624 		break;
1625 	}
1626 
1627 	switch (mmio_mitigation) {
1628 	case MMIO_MITIGATION_VERW:
1629 	case MMIO_MITIGATION_UCODE_NEEDED:
1630 		if (sched_smt_active())
1631 			pr_warn_once(MMIO_MSG_SMT);
1632 		break;
1633 	case MMIO_MITIGATION_OFF:
1634 		break;
1635 	}
1636 
1637 	mutex_unlock(&spec_ctrl_mutex);
1638 }
1639 
1640 #undef pr_fmt
1641 #define pr_fmt(fmt)	"Speculative Store Bypass: " fmt
1642 
1643 static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
1644 
1645 /* The kernel command line selection */
1646 enum ssb_mitigation_cmd {
1647 	SPEC_STORE_BYPASS_CMD_NONE,
1648 	SPEC_STORE_BYPASS_CMD_AUTO,
1649 	SPEC_STORE_BYPASS_CMD_ON,
1650 	SPEC_STORE_BYPASS_CMD_PRCTL,
1651 	SPEC_STORE_BYPASS_CMD_SECCOMP,
1652 };
1653 
1654 static const char * const ssb_strings[] = {
1655 	[SPEC_STORE_BYPASS_NONE]	= "Vulnerable",
1656 	[SPEC_STORE_BYPASS_DISABLE]	= "Mitigation: Speculative Store Bypass disabled",
1657 	[SPEC_STORE_BYPASS_PRCTL]	= "Mitigation: Speculative Store Bypass disabled via prctl",
1658 	[SPEC_STORE_BYPASS_SECCOMP]	= "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
1659 };
1660 
1661 static const struct {
1662 	const char *option;
1663 	enum ssb_mitigation_cmd cmd;
1664 } ssb_mitigation_options[]  __initconst = {
1665 	{ "auto",	SPEC_STORE_BYPASS_CMD_AUTO },    /* Platform decides */
1666 	{ "on",		SPEC_STORE_BYPASS_CMD_ON },      /* Disable Speculative Store Bypass */
1667 	{ "off",	SPEC_STORE_BYPASS_CMD_NONE },    /* Don't touch Speculative Store Bypass */
1668 	{ "prctl",	SPEC_STORE_BYPASS_CMD_PRCTL },   /* Disable Speculative Store Bypass via prctl */
1669 	{ "seccomp",	SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */
1670 };
1671 
ssb_parse_cmdline(void)1672 static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
1673 {
1674 	enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO;
1675 	char arg[20];
1676 	int ret, i;
1677 
1678 	if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable") ||
1679 	    cpu_mitigations_off()) {
1680 		return SPEC_STORE_BYPASS_CMD_NONE;
1681 	} else {
1682 		ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable",
1683 					  arg, sizeof(arg));
1684 		if (ret < 0)
1685 			return SPEC_STORE_BYPASS_CMD_AUTO;
1686 
1687 		for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) {
1688 			if (!match_option(arg, ret, ssb_mitigation_options[i].option))
1689 				continue;
1690 
1691 			cmd = ssb_mitigation_options[i].cmd;
1692 			break;
1693 		}
1694 
1695 		if (i >= ARRAY_SIZE(ssb_mitigation_options)) {
1696 			pr_err("unknown option (%s). Switching to AUTO select\n", arg);
1697 			return SPEC_STORE_BYPASS_CMD_AUTO;
1698 		}
1699 	}
1700 
1701 	return cmd;
1702 }
1703 
__ssb_select_mitigation(void)1704 static enum ssb_mitigation __init __ssb_select_mitigation(void)
1705 {
1706 	enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
1707 	enum ssb_mitigation_cmd cmd;
1708 
1709 	if (!boot_cpu_has(X86_FEATURE_SSBD))
1710 		return mode;
1711 
1712 	cmd = ssb_parse_cmdline();
1713 	if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) &&
1714 	    (cmd == SPEC_STORE_BYPASS_CMD_NONE ||
1715 	     cmd == SPEC_STORE_BYPASS_CMD_AUTO))
1716 		return mode;
1717 
1718 	switch (cmd) {
1719 	case SPEC_STORE_BYPASS_CMD_AUTO:
1720 	case SPEC_STORE_BYPASS_CMD_SECCOMP:
1721 		/*
1722 		 * Choose prctl+seccomp as the default mode if seccomp is
1723 		 * enabled.
1724 		 */
1725 		if (IS_ENABLED(CONFIG_SECCOMP))
1726 			mode = SPEC_STORE_BYPASS_SECCOMP;
1727 		else
1728 			mode = SPEC_STORE_BYPASS_PRCTL;
1729 		break;
1730 	case SPEC_STORE_BYPASS_CMD_ON:
1731 		mode = SPEC_STORE_BYPASS_DISABLE;
1732 		break;
1733 	case SPEC_STORE_BYPASS_CMD_PRCTL:
1734 		mode = SPEC_STORE_BYPASS_PRCTL;
1735 		break;
1736 	case SPEC_STORE_BYPASS_CMD_NONE:
1737 		break;
1738 	}
1739 
1740 	/*
1741 	 * We have three CPU feature flags that are in play here:
1742 	 *  - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
1743 	 *  - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
1744 	 *  - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
1745 	 */
1746 	if (mode == SPEC_STORE_BYPASS_DISABLE) {
1747 		setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
1748 		/*
1749 		 * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
1750 		 * use a completely different MSR and bit dependent on family.
1751 		 */
1752 		if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
1753 		    !static_cpu_has(X86_FEATURE_AMD_SSBD)) {
1754 			x86_amd_ssb_disable();
1755 		} else {
1756 			x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
1757 			write_spec_ctrl_current(x86_spec_ctrl_base, true);
1758 		}
1759 	}
1760 
1761 	return mode;
1762 }
1763 
ssb_select_mitigation(void)1764 static void ssb_select_mitigation(void)
1765 {
1766 	ssb_mode = __ssb_select_mitigation();
1767 
1768 	if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1769 		pr_info("%s\n", ssb_strings[ssb_mode]);
1770 }
1771 
1772 #undef pr_fmt
1773 #define pr_fmt(fmt)     "Speculation prctl: " fmt
1774 
task_update_spec_tif(struct task_struct * tsk)1775 static void task_update_spec_tif(struct task_struct *tsk)
1776 {
1777 	/* Force the update of the real TIF bits */
1778 	set_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE);
1779 
1780 	/*
1781 	 * Immediately update the speculation control MSRs for the current
1782 	 * task, but for a non-current task delay setting the CPU
1783 	 * mitigation until it is scheduled next.
1784 	 *
1785 	 * This can only happen for SECCOMP mitigation. For PRCTL it's
1786 	 * always the current task.
1787 	 */
1788 	if (tsk == current)
1789 		speculation_ctrl_update_current();
1790 }
1791 
ssb_prctl_set(struct task_struct * task,unsigned long ctrl)1792 static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
1793 {
1794 	if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
1795 	    ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
1796 		return -ENXIO;
1797 
1798 	switch (ctrl) {
1799 	case PR_SPEC_ENABLE:
1800 		/* If speculation is force disabled, enable is not allowed */
1801 		if (task_spec_ssb_force_disable(task))
1802 			return -EPERM;
1803 		task_clear_spec_ssb_disable(task);
1804 		task_clear_spec_ssb_noexec(task);
1805 		task_update_spec_tif(task);
1806 		break;
1807 	case PR_SPEC_DISABLE:
1808 		task_set_spec_ssb_disable(task);
1809 		task_clear_spec_ssb_noexec(task);
1810 		task_update_spec_tif(task);
1811 		break;
1812 	case PR_SPEC_FORCE_DISABLE:
1813 		task_set_spec_ssb_disable(task);
1814 		task_set_spec_ssb_force_disable(task);
1815 		task_clear_spec_ssb_noexec(task);
1816 		task_update_spec_tif(task);
1817 		break;
1818 	case PR_SPEC_DISABLE_NOEXEC:
1819 		if (task_spec_ssb_force_disable(task))
1820 			return -EPERM;
1821 		task_set_spec_ssb_disable(task);
1822 		task_set_spec_ssb_noexec(task);
1823 		task_update_spec_tif(task);
1824 		break;
1825 	default:
1826 		return -ERANGE;
1827 	}
1828 	return 0;
1829 }
1830 
is_spec_ib_user_controlled(void)1831 static bool is_spec_ib_user_controlled(void)
1832 {
1833 	return spectre_v2_user_ibpb == SPECTRE_V2_USER_PRCTL ||
1834 		spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
1835 		spectre_v2_user_stibp == SPECTRE_V2_USER_PRCTL ||
1836 		spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP;
1837 }
1838 
ib_prctl_set(struct task_struct * task,unsigned long ctrl)1839 static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)
1840 {
1841 	switch (ctrl) {
1842 	case PR_SPEC_ENABLE:
1843 		if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1844 		    spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
1845 			return 0;
1846 
1847 		/*
1848 		 * With strict mode for both IBPB and STIBP, the instruction
1849 		 * code paths avoid checking this task flag and instead,
1850 		 * unconditionally run the instruction. However, STIBP and IBPB
1851 		 * are independent and either can be set to conditionally
1852 		 * enabled regardless of the mode of the other.
1853 		 *
1854 		 * If either is set to conditional, allow the task flag to be
1855 		 * updated, unless it was force-disabled by a previous prctl
1856 		 * call. Currently, this is possible on an AMD CPU which has the
1857 		 * feature X86_FEATURE_AMD_STIBP_ALWAYS_ON. In this case, if the
1858 		 * kernel is booted with 'spectre_v2_user=seccomp', then
1859 		 * spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP and
1860 		 * spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED.
1861 		 */
1862 		if (!is_spec_ib_user_controlled() ||
1863 		    task_spec_ib_force_disable(task))
1864 			return -EPERM;
1865 
1866 		task_clear_spec_ib_disable(task);
1867 		task_update_spec_tif(task);
1868 		break;
1869 	case PR_SPEC_DISABLE:
1870 	case PR_SPEC_FORCE_DISABLE:
1871 		/*
1872 		 * Indirect branch speculation is always allowed when
1873 		 * mitigation is force disabled.
1874 		 */
1875 		if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1876 		    spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
1877 			return -EPERM;
1878 
1879 		if (!is_spec_ib_user_controlled())
1880 			return 0;
1881 
1882 		task_set_spec_ib_disable(task);
1883 		if (ctrl == PR_SPEC_FORCE_DISABLE)
1884 			task_set_spec_ib_force_disable(task);
1885 		task_update_spec_tif(task);
1886 		if (task == current)
1887 			indirect_branch_prediction_barrier();
1888 		break;
1889 	default:
1890 		return -ERANGE;
1891 	}
1892 	return 0;
1893 }
1894 
arch_prctl_spec_ctrl_set(struct task_struct * task,unsigned long which,unsigned long ctrl)1895 int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
1896 			     unsigned long ctrl)
1897 {
1898 	switch (which) {
1899 	case PR_SPEC_STORE_BYPASS:
1900 		return ssb_prctl_set(task, ctrl);
1901 	case PR_SPEC_INDIRECT_BRANCH:
1902 		return ib_prctl_set(task, ctrl);
1903 	default:
1904 		return -ENODEV;
1905 	}
1906 }
1907 
1908 #ifdef CONFIG_SECCOMP
arch_seccomp_spec_mitigate(struct task_struct * task)1909 void arch_seccomp_spec_mitigate(struct task_struct *task)
1910 {
1911 	if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP)
1912 		ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
1913 	if (spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
1914 	    spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP)
1915 		ib_prctl_set(task, PR_SPEC_FORCE_DISABLE);
1916 }
1917 #endif
1918 
ssb_prctl_get(struct task_struct * task)1919 static int ssb_prctl_get(struct task_struct *task)
1920 {
1921 	switch (ssb_mode) {
1922 	case SPEC_STORE_BYPASS_DISABLE:
1923 		return PR_SPEC_DISABLE;
1924 	case SPEC_STORE_BYPASS_SECCOMP:
1925 	case SPEC_STORE_BYPASS_PRCTL:
1926 		if (task_spec_ssb_force_disable(task))
1927 			return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
1928 		if (task_spec_ssb_noexec(task))
1929 			return PR_SPEC_PRCTL | PR_SPEC_DISABLE_NOEXEC;
1930 		if (task_spec_ssb_disable(task))
1931 			return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
1932 		return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
1933 	default:
1934 		if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1935 			return PR_SPEC_ENABLE;
1936 		return PR_SPEC_NOT_AFFECTED;
1937 	}
1938 }
1939 
ib_prctl_get(struct task_struct * task)1940 static int ib_prctl_get(struct task_struct *task)
1941 {
1942 	if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
1943 		return PR_SPEC_NOT_AFFECTED;
1944 
1945 	if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1946 	    spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
1947 		return PR_SPEC_ENABLE;
1948 	else if (is_spec_ib_user_controlled()) {
1949 		if (task_spec_ib_force_disable(task))
1950 			return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
1951 		if (task_spec_ib_disable(task))
1952 			return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
1953 		return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
1954 	} else if (spectre_v2_user_ibpb == SPECTRE_V2_USER_STRICT ||
1955 	    spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
1956 	    spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED)
1957 		return PR_SPEC_DISABLE;
1958 	else
1959 		return PR_SPEC_NOT_AFFECTED;
1960 }
1961 
arch_prctl_spec_ctrl_get(struct task_struct * task,unsigned long which)1962 int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
1963 {
1964 	switch (which) {
1965 	case PR_SPEC_STORE_BYPASS:
1966 		return ssb_prctl_get(task);
1967 	case PR_SPEC_INDIRECT_BRANCH:
1968 		return ib_prctl_get(task);
1969 	default:
1970 		return -ENODEV;
1971 	}
1972 }
1973 
x86_spec_ctrl_setup_ap(void)1974 void x86_spec_ctrl_setup_ap(void)
1975 {
1976 	if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
1977 		write_spec_ctrl_current(x86_spec_ctrl_base, true);
1978 
1979 	if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
1980 		x86_amd_ssb_disable();
1981 }
1982 
1983 bool itlb_multihit_kvm_mitigation;
1984 EXPORT_SYMBOL_GPL(itlb_multihit_kvm_mitigation);
1985 
1986 #undef pr_fmt
1987 #define pr_fmt(fmt)	"L1TF: " fmt
1988 
1989 /* Default mitigation for L1TF-affected CPUs */
1990 enum l1tf_mitigations l1tf_mitigation __ro_after_init = L1TF_MITIGATION_FLUSH;
1991 #if IS_ENABLED(CONFIG_KVM_INTEL)
1992 EXPORT_SYMBOL_GPL(l1tf_mitigation);
1993 #endif
1994 enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
1995 EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation);
1996 
1997 /*
1998  * These CPUs all support 44bits physical address space internally in the
1999  * cache but CPUID can report a smaller number of physical address bits.
2000  *
2001  * The L1TF mitigation uses the top most address bit for the inversion of
2002  * non present PTEs. When the installed memory reaches into the top most
2003  * address bit due to memory holes, which has been observed on machines
2004  * which report 36bits physical address bits and have 32G RAM installed,
2005  * then the mitigation range check in l1tf_select_mitigation() triggers.
2006  * This is a false positive because the mitigation is still possible due to
2007  * the fact that the cache uses 44bit internally. Use the cache bits
2008  * instead of the reported physical bits and adjust them on the affected
2009  * machines to 44bit if the reported bits are less than 44.
2010  */
override_cache_bits(struct cpuinfo_x86 * c)2011 static void override_cache_bits(struct cpuinfo_x86 *c)
2012 {
2013 	if (c->x86 != 6)
2014 		return;
2015 
2016 	switch (c->x86_model) {
2017 	case INTEL_FAM6_NEHALEM:
2018 	case INTEL_FAM6_WESTMERE:
2019 	case INTEL_FAM6_SANDYBRIDGE:
2020 	case INTEL_FAM6_IVYBRIDGE:
2021 	case INTEL_FAM6_HASWELL:
2022 	case INTEL_FAM6_HASWELL_L:
2023 	case INTEL_FAM6_HASWELL_G:
2024 	case INTEL_FAM6_BROADWELL:
2025 	case INTEL_FAM6_BROADWELL_G:
2026 	case INTEL_FAM6_SKYLAKE_L:
2027 	case INTEL_FAM6_SKYLAKE:
2028 	case INTEL_FAM6_KABYLAKE_L:
2029 	case INTEL_FAM6_KABYLAKE:
2030 		if (c->x86_cache_bits < 44)
2031 			c->x86_cache_bits = 44;
2032 		break;
2033 	}
2034 }
2035 
l1tf_select_mitigation(void)2036 static void __init l1tf_select_mitigation(void)
2037 {
2038 	u64 half_pa;
2039 
2040 	if (!boot_cpu_has_bug(X86_BUG_L1TF))
2041 		return;
2042 
2043 	if (cpu_mitigations_off())
2044 		l1tf_mitigation = L1TF_MITIGATION_OFF;
2045 	else if (cpu_mitigations_auto_nosmt())
2046 		l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
2047 
2048 	override_cache_bits(&boot_cpu_data);
2049 
2050 	switch (l1tf_mitigation) {
2051 	case L1TF_MITIGATION_OFF:
2052 	case L1TF_MITIGATION_FLUSH_NOWARN:
2053 	case L1TF_MITIGATION_FLUSH:
2054 		break;
2055 	case L1TF_MITIGATION_FLUSH_NOSMT:
2056 	case L1TF_MITIGATION_FULL:
2057 		cpu_smt_disable(false);
2058 		break;
2059 	case L1TF_MITIGATION_FULL_FORCE:
2060 		cpu_smt_disable(true);
2061 		break;
2062 	}
2063 
2064 #if CONFIG_PGTABLE_LEVELS == 2
2065 	pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
2066 	return;
2067 #endif
2068 
2069 	half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT;
2070 	if (l1tf_mitigation != L1TF_MITIGATION_OFF &&
2071 			e820__mapped_any(half_pa, ULLONG_MAX - half_pa, E820_TYPE_RAM)) {
2072 		pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
2073 		pr_info("You may make it effective by booting the kernel with mem=%llu parameter.\n",
2074 				half_pa);
2075 		pr_info("However, doing so will make a part of your RAM unusable.\n");
2076 		pr_info("Reading https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html might help you decide.\n");
2077 		return;
2078 	}
2079 
2080 	setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV);
2081 }
2082 
l1tf_cmdline(char * str)2083 static int __init l1tf_cmdline(char *str)
2084 {
2085 	if (!boot_cpu_has_bug(X86_BUG_L1TF))
2086 		return 0;
2087 
2088 	if (!str)
2089 		return -EINVAL;
2090 
2091 	if (!strcmp(str, "off"))
2092 		l1tf_mitigation = L1TF_MITIGATION_OFF;
2093 	else if (!strcmp(str, "flush,nowarn"))
2094 		l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOWARN;
2095 	else if (!strcmp(str, "flush"))
2096 		l1tf_mitigation = L1TF_MITIGATION_FLUSH;
2097 	else if (!strcmp(str, "flush,nosmt"))
2098 		l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
2099 	else if (!strcmp(str, "full"))
2100 		l1tf_mitigation = L1TF_MITIGATION_FULL;
2101 	else if (!strcmp(str, "full,force"))
2102 		l1tf_mitigation = L1TF_MITIGATION_FULL_FORCE;
2103 
2104 	return 0;
2105 }
2106 early_param("l1tf", l1tf_cmdline);
2107 
2108 #undef pr_fmt
2109 #define pr_fmt(fmt) fmt
2110 
2111 #ifdef CONFIG_SYSFS
2112 
2113 #define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
2114 
2115 #if IS_ENABLED(CONFIG_KVM_INTEL)
2116 static const char * const l1tf_vmx_states[] = {
2117 	[VMENTER_L1D_FLUSH_AUTO]		= "auto",
2118 	[VMENTER_L1D_FLUSH_NEVER]		= "vulnerable",
2119 	[VMENTER_L1D_FLUSH_COND]		= "conditional cache flushes",
2120 	[VMENTER_L1D_FLUSH_ALWAYS]		= "cache flushes",
2121 	[VMENTER_L1D_FLUSH_EPT_DISABLED]	= "EPT disabled",
2122 	[VMENTER_L1D_FLUSH_NOT_REQUIRED]	= "flush not necessary"
2123 };
2124 
l1tf_show_state(char * buf)2125 static ssize_t l1tf_show_state(char *buf)
2126 {
2127 	if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO)
2128 		return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
2129 
2130 	if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_EPT_DISABLED ||
2131 	    (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER &&
2132 	     sched_smt_active())) {
2133 		return sprintf(buf, "%s; VMX: %s\n", L1TF_DEFAULT_MSG,
2134 			       l1tf_vmx_states[l1tf_vmx_mitigation]);
2135 	}
2136 
2137 	return sprintf(buf, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG,
2138 		       l1tf_vmx_states[l1tf_vmx_mitigation],
2139 		       sched_smt_active() ? "vulnerable" : "disabled");
2140 }
2141 
itlb_multihit_show_state(char * buf)2142 static ssize_t itlb_multihit_show_state(char *buf)
2143 {
2144 	if (!boot_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
2145 	    !boot_cpu_has(X86_FEATURE_VMX))
2146 		return sprintf(buf, "KVM: Mitigation: VMX unsupported\n");
2147 	else if (!(cr4_read_shadow() & X86_CR4_VMXE))
2148 		return sprintf(buf, "KVM: Mitigation: VMX disabled\n");
2149 	else if (itlb_multihit_kvm_mitigation)
2150 		return sprintf(buf, "KVM: Mitigation: Split huge pages\n");
2151 	else
2152 		return sprintf(buf, "KVM: Vulnerable\n");
2153 }
2154 #else
l1tf_show_state(char * buf)2155 static ssize_t l1tf_show_state(char *buf)
2156 {
2157 	return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
2158 }
2159 
itlb_multihit_show_state(char * buf)2160 static ssize_t itlb_multihit_show_state(char *buf)
2161 {
2162 	return sprintf(buf, "Processor vulnerable\n");
2163 }
2164 #endif
2165 
mds_show_state(char * buf)2166 static ssize_t mds_show_state(char *buf)
2167 {
2168 	if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
2169 		return sprintf(buf, "%s; SMT Host state unknown\n",
2170 			       mds_strings[mds_mitigation]);
2171 	}
2172 
2173 	if (boot_cpu_has(X86_BUG_MSBDS_ONLY)) {
2174 		return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
2175 			       (mds_mitigation == MDS_MITIGATION_OFF ? "vulnerable" :
2176 			        sched_smt_active() ? "mitigated" : "disabled"));
2177 	}
2178 
2179 	return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
2180 		       sched_smt_active() ? "vulnerable" : "disabled");
2181 }
2182 
tsx_async_abort_show_state(char * buf)2183 static ssize_t tsx_async_abort_show_state(char *buf)
2184 {
2185 	if ((taa_mitigation == TAA_MITIGATION_TSX_DISABLED) ||
2186 	    (taa_mitigation == TAA_MITIGATION_OFF))
2187 		return sprintf(buf, "%s\n", taa_strings[taa_mitigation]);
2188 
2189 	if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
2190 		return sprintf(buf, "%s; SMT Host state unknown\n",
2191 			       taa_strings[taa_mitigation]);
2192 	}
2193 
2194 	return sprintf(buf, "%s; SMT %s\n", taa_strings[taa_mitigation],
2195 		       sched_smt_active() ? "vulnerable" : "disabled");
2196 }
2197 
mmio_stale_data_show_state(char * buf)2198 static ssize_t mmio_stale_data_show_state(char *buf)
2199 {
2200 	if (mmio_mitigation == MMIO_MITIGATION_OFF)
2201 		return sysfs_emit(buf, "%s\n", mmio_strings[mmio_mitigation]);
2202 
2203 	if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
2204 		return sysfs_emit(buf, "%s; SMT Host state unknown\n",
2205 				  mmio_strings[mmio_mitigation]);
2206 	}
2207 
2208 	return sysfs_emit(buf, "%s; SMT %s\n", mmio_strings[mmio_mitigation],
2209 			  sched_smt_active() ? "vulnerable" : "disabled");
2210 }
2211 
stibp_state(void)2212 static char *stibp_state(void)
2213 {
2214 	if (spectre_v2_in_eibrs_mode(spectre_v2_enabled))
2215 		return "";
2216 
2217 	switch (spectre_v2_user_stibp) {
2218 	case SPECTRE_V2_USER_NONE:
2219 		return ", STIBP: disabled";
2220 	case SPECTRE_V2_USER_STRICT:
2221 		return ", STIBP: forced";
2222 	case SPECTRE_V2_USER_STRICT_PREFERRED:
2223 		return ", STIBP: always-on";
2224 	case SPECTRE_V2_USER_PRCTL:
2225 	case SPECTRE_V2_USER_SECCOMP:
2226 		if (static_key_enabled(&switch_to_cond_stibp))
2227 			return ", STIBP: conditional";
2228 	}
2229 	return "";
2230 }
2231 
ibpb_state(void)2232 static char *ibpb_state(void)
2233 {
2234 	if (boot_cpu_has(X86_FEATURE_IBPB)) {
2235 		if (static_key_enabled(&switch_mm_always_ibpb))
2236 			return ", IBPB: always-on";
2237 		if (static_key_enabled(&switch_mm_cond_ibpb))
2238 			return ", IBPB: conditional";
2239 		return ", IBPB: disabled";
2240 	}
2241 	return "";
2242 }
2243 
pbrsb_eibrs_state(void)2244 static char *pbrsb_eibrs_state(void)
2245 {
2246 	if (boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB)) {
2247 		if (boot_cpu_has(X86_FEATURE_RSB_VMEXIT_LITE) ||
2248 		    boot_cpu_has(X86_FEATURE_RSB_VMEXIT))
2249 			return ", PBRSB-eIBRS: SW sequence";
2250 		else
2251 			return ", PBRSB-eIBRS: Vulnerable";
2252 	} else {
2253 		return ", PBRSB-eIBRS: Not affected";
2254 	}
2255 }
2256 
spectre_v2_show_state(char * buf)2257 static ssize_t spectre_v2_show_state(char *buf)
2258 {
2259 	if (spectre_v2_enabled == SPECTRE_V2_LFENCE)
2260 		return sprintf(buf, "Vulnerable: LFENCE\n");
2261 
2262 	if (spectre_v2_enabled == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
2263 		return sprintf(buf, "Vulnerable: eIBRS with unprivileged eBPF\n");
2264 
2265 	if (sched_smt_active() && unprivileged_ebpf_enabled() &&
2266 	    spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
2267 		return sprintf(buf, "Vulnerable: eIBRS+LFENCE with unprivileged eBPF and SMT\n");
2268 
2269 	return sprintf(buf, "%s%s%s%s%s%s%s\n",
2270 		       spectre_v2_strings[spectre_v2_enabled],
2271 		       ibpb_state(),
2272 		       boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
2273 		       stibp_state(),
2274 		       boot_cpu_has(X86_FEATURE_RSB_CTXSW) ? ", RSB filling" : "",
2275 		       pbrsb_eibrs_state(),
2276 		       spectre_v2_module_string());
2277 }
2278 
srbds_show_state(char * buf)2279 static ssize_t srbds_show_state(char *buf)
2280 {
2281 	return sprintf(buf, "%s\n", srbds_strings[srbds_mitigation]);
2282 }
2283 
retbleed_show_state(char * buf)2284 static ssize_t retbleed_show_state(char *buf)
2285 {
2286 	if (retbleed_mitigation == RETBLEED_MITIGATION_UNRET) {
2287 	    if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
2288 		boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)
2289 		    return sprintf(buf, "Vulnerable: untrained return thunk on non-Zen uarch\n");
2290 
2291 	    return sprintf(buf, "%s; SMT %s\n",
2292 			   retbleed_strings[retbleed_mitigation],
2293 			   !sched_smt_active() ? "disabled" :
2294 			   spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
2295 			   spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED ?
2296 			   "enabled with STIBP protection" : "vulnerable");
2297 	}
2298 
2299 	return sprintf(buf, "%s\n", retbleed_strings[retbleed_mitigation]);
2300 }
2301 
cpu_show_common(struct device * dev,struct device_attribute * attr,char * buf,unsigned int bug)2302 static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
2303 			       char *buf, unsigned int bug)
2304 {
2305 	if (!boot_cpu_has_bug(bug))
2306 		return sprintf(buf, "Not affected\n");
2307 
2308 	switch (bug) {
2309 	case X86_BUG_CPU_MELTDOWN:
2310 		if (boot_cpu_has(X86_FEATURE_PTI))
2311 			return sprintf(buf, "Mitigation: PTI\n");
2312 
2313 		if (hypervisor_is_type(X86_HYPER_XEN_PV))
2314 			return sprintf(buf, "Unknown (XEN PV detected, hypervisor mitigation required)\n");
2315 
2316 		break;
2317 
2318 	case X86_BUG_SPECTRE_V1:
2319 		return sprintf(buf, "%s\n", spectre_v1_strings[spectre_v1_mitigation]);
2320 
2321 	case X86_BUG_SPECTRE_V2:
2322 		return spectre_v2_show_state(buf);
2323 
2324 	case X86_BUG_SPEC_STORE_BYPASS:
2325 		return sprintf(buf, "%s\n", ssb_strings[ssb_mode]);
2326 
2327 	case X86_BUG_L1TF:
2328 		if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV))
2329 			return l1tf_show_state(buf);
2330 		break;
2331 
2332 	case X86_BUG_MDS:
2333 		return mds_show_state(buf);
2334 
2335 	case X86_BUG_TAA:
2336 		return tsx_async_abort_show_state(buf);
2337 
2338 	case X86_BUG_ITLB_MULTIHIT:
2339 		return itlb_multihit_show_state(buf);
2340 
2341 	case X86_BUG_SRBDS:
2342 		return srbds_show_state(buf);
2343 
2344 	case X86_BUG_MMIO_STALE_DATA:
2345 		return mmio_stale_data_show_state(buf);
2346 
2347 	case X86_BUG_RETBLEED:
2348 		return retbleed_show_state(buf);
2349 
2350 	default:
2351 		break;
2352 	}
2353 
2354 	return sprintf(buf, "Vulnerable\n");
2355 }
2356 
cpu_show_meltdown(struct device * dev,struct device_attribute * attr,char * buf)2357 ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
2358 {
2359 	return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
2360 }
2361 
cpu_show_spectre_v1(struct device * dev,struct device_attribute * attr,char * buf)2362 ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
2363 {
2364 	return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
2365 }
2366 
cpu_show_spectre_v2(struct device * dev,struct device_attribute * attr,char * buf)2367 ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
2368 {
2369 	return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
2370 }
2371 
cpu_show_spec_store_bypass(struct device * dev,struct device_attribute * attr,char * buf)2372 ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
2373 {
2374 	return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS);
2375 }
2376 
cpu_show_l1tf(struct device * dev,struct device_attribute * attr,char * buf)2377 ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
2378 {
2379 	return cpu_show_common(dev, attr, buf, X86_BUG_L1TF);
2380 }
2381 
cpu_show_mds(struct device * dev,struct device_attribute * attr,char * buf)2382 ssize_t cpu_show_mds(struct device *dev, struct device_attribute *attr, char *buf)
2383 {
2384 	return cpu_show_common(dev, attr, buf, X86_BUG_MDS);
2385 }
2386 
cpu_show_tsx_async_abort(struct device * dev,struct device_attribute * attr,char * buf)2387 ssize_t cpu_show_tsx_async_abort(struct device *dev, struct device_attribute *attr, char *buf)
2388 {
2389 	return cpu_show_common(dev, attr, buf, X86_BUG_TAA);
2390 }
2391 
cpu_show_itlb_multihit(struct device * dev,struct device_attribute * attr,char * buf)2392 ssize_t cpu_show_itlb_multihit(struct device *dev, struct device_attribute *attr, char *buf)
2393 {
2394 	return cpu_show_common(dev, attr, buf, X86_BUG_ITLB_MULTIHIT);
2395 }
2396 
cpu_show_srbds(struct device * dev,struct device_attribute * attr,char * buf)2397 ssize_t cpu_show_srbds(struct device *dev, struct device_attribute *attr, char *buf)
2398 {
2399 	return cpu_show_common(dev, attr, buf, X86_BUG_SRBDS);
2400 }
2401 
cpu_show_mmio_stale_data(struct device * dev,struct device_attribute * attr,char * buf)2402 ssize_t cpu_show_mmio_stale_data(struct device *dev, struct device_attribute *attr, char *buf)
2403 {
2404 	return cpu_show_common(dev, attr, buf, X86_BUG_MMIO_STALE_DATA);
2405 }
2406 
cpu_show_retbleed(struct device * dev,struct device_attribute * attr,char * buf)2407 ssize_t cpu_show_retbleed(struct device *dev, struct device_attribute *attr, char *buf)
2408 {
2409 	return cpu_show_common(dev, attr, buf, X86_BUG_RETBLEED);
2410 }
2411 #endif
2412