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