]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - arch/x86/kernel/cpu/bugs.c
x86/mm: Prepare for opt-in based L1D flush in switch_mm()
[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>
da285121 19
28a27752 20#include <asm/spec-ctrl.h>
da285121 21#include <asm/cmdline.h>
91eb1b79 22#include <asm/bugs.h>
1353ebb4 23#include <asm/processor.h>
7ebad705 24#include <asm/processor-flags.h>
952f07ec 25#include <asm/fpu/internal.h>
1353ebb4 26#include <asm/msr.h>
72c6d2db 27#include <asm/vmx.h>
1353ebb4
JF
28#include <asm/paravirt.h>
29#include <asm/alternative.h>
d1163651 30#include <asm/set_memory.h>
c995efd5 31#include <asm/intel-family.h>
17dbca11 32#include <asm/e820/api.h>
6cb2b08f 33#include <asm/hypervisor.h>
f29dfa53 34#include <asm/tlbflush.h>
1353ebb4 35
ad3bc25a
BP
36#include "cpu.h"
37
a2059825 38static void __init spectre_v1_select_mitigation(void);
da285121 39static void __init spectre_v2_select_mitigation(void);
24f7fc83 40static void __init ssb_select_mitigation(void);
17dbca11 41static void __init l1tf_select_mitigation(void);
bc124170 42static void __init mds_select_mitigation(void);
cd5a2aa8 43static void __init mds_print_mitigation(void);
1b42f017 44static void __init taa_select_mitigation(void);
7e5b3c26 45static void __init srbds_select_mitigation(void);
b5f06f64 46static void __init l1d_flush_select_mitigation(void);
da285121 47
53c613fe
JK
48/* The base value of the SPEC_CTRL MSR that always has to be preserved. */
49u64 x86_spec_ctrl_base;
fa8ac498 50EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
53c613fe 51static DEFINE_MUTEX(spec_ctrl_mutex);
1b86883c 52
1115a859
KRW
53/*
54 * The vendor and possibly platform specific bits which can be modified in
55 * x86_spec_ctrl_base.
56 */
be6fcb54 57static u64 __ro_after_init x86_spec_ctrl_mask = SPEC_CTRL_IBRS;
1115a859 58
764f3c21
KRW
59/*
60 * AMD specific MSR info for Speculative Store Bypass control.
9f65fb29 61 * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu().
764f3c21
KRW
62 */
63u64 __ro_after_init x86_amd_ls_cfg_base;
9f65fb29 64u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask;
764f3c21 65
aa77bfb3 66/* Control conditional STIBP in switch_to() */
fa1202ef 67DEFINE_STATIC_KEY_FALSE(switch_to_cond_stibp);
4c71a2b6
TG
68/* Control conditional IBPB in switch_mm() */
69DEFINE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
70/* Control unconditional IBPB in switch_mm() */
71DEFINE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
fa1202ef 72
04dcbdb8
TG
73/* Control MDS CPU buffer clear before returning to user space */
74DEFINE_STATIC_KEY_FALSE(mds_user_clear);
650b68a0 75EXPORT_SYMBOL_GPL(mds_user_clear);
07f07f55
TG
76/* Control MDS CPU buffer clear before idling (halt, mwait) */
77DEFINE_STATIC_KEY_FALSE(mds_idle_clear);
78EXPORT_SYMBOL_GPL(mds_idle_clear);
04dcbdb8 79
b5f06f64
BS
80/*
81 * Controls whether l1d flush based mitigations are enabled,
82 * based on hw features and admin setting via boot parameter
83 * defaults to false
84 */
85DEFINE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);
86
1353ebb4
JF
87void __init check_bugs(void)
88{
89 identify_boot_cpu();
55a36b65 90
fee0aede
TG
91 /*
92 * identify_boot_cpu() initialized SMT support information, let the
93 * core code know.
94 */
b284909a 95 cpu_smt_check_topology();
fee0aede 96
62a67e12
BP
97 if (!IS_ENABLED(CONFIG_SMP)) {
98 pr_info("CPU: ");
99 print_cpu_info(&boot_cpu_data);
100 }
101
1b86883c
KRW
102 /*
103 * Read the SPEC_CTRL MSR to account for reserved bits which may
764f3c21
KRW
104 * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
105 * init code as it is not enumerated and depends on the family.
1b86883c 106 */
7eb8956a 107 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
1b86883c
KRW
108 rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
109
be6fcb54
TG
110 /* Allow STIBP in MSR_SPEC_CTRL if supported */
111 if (boot_cpu_has(X86_FEATURE_STIBP))
112 x86_spec_ctrl_mask |= SPEC_CTRL_STIBP;
113
a2059825
JP
114 /* Select the proper CPU mitigations before patching alternatives: */
115 spectre_v1_select_mitigation();
da285121 116 spectre_v2_select_mitigation();
24f7fc83 117 ssb_select_mitigation();
17dbca11 118 l1tf_select_mitigation();
bc124170 119 mds_select_mitigation();
1b42f017 120 taa_select_mitigation();
7e5b3c26 121 srbds_select_mitigation();
b5f06f64 122 l1d_flush_select_mitigation();
bc124170 123
cd5a2aa8
WL
124 /*
125 * As MDS and TAA mitigations are inter-related, print MDS
126 * mitigation until after TAA mitigation selection is done.
127 */
128 mds_print_mitigation();
129
7c3658b2
JP
130 arch_smt_update();
131
62a67e12 132#ifdef CONFIG_X86_32
55a36b65
BP
133 /*
134 * Check whether we are able to run this kernel safely on SMP.
135 *
136 * - i386 is no longer supported.
137 * - In order to run on anything without a TSC, we need to be
138 * compiled for a i486.
139 */
140 if (boot_cpu_data.x86 < 4)
141 panic("Kernel requires i486+ for 'invlpg' and other features");
142
bfe4bb15
MV
143 init_utsname()->machine[1] =
144 '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
1353ebb4 145 alternative_instructions();
304bceda 146
4d164092 147 fpu__init_check_bugs();
62a67e12
BP
148#else /* CONFIG_X86_64 */
149 alternative_instructions();
150
151 /*
152 * Make sure the first 2MB area is not mapped by huge pages
153 * There are typically fixed size MTRRs in there and overlapping
154 * MTRRs into large pages causes slow downs.
155 *
156 * Right now we don't do that with gbpages because there seems
157 * very little benefit for that case.
158 */
159 if (!direct_gbpages)
160 set_memory_4k((unsigned long)__va(0), 1);
161#endif
1353ebb4 162}
61dc0f55 163
cc69b349
BP
164void
165x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest)
5cf68754 166{
be6fcb54 167 u64 msrval, guestval, hostval = x86_spec_ctrl_base;
cc69b349 168 struct thread_info *ti = current_thread_info();
885f82bf 169
7eb8956a 170 /* Is MSR_SPEC_CTRL implemented ? */
cc69b349 171 if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) {
be6fcb54
TG
172 /*
173 * Restrict guest_spec_ctrl to supported values. Clear the
174 * modifiable bits in the host base value and or the
175 * modifiable bits from the guest value.
176 */
177 guestval = hostval & ~x86_spec_ctrl_mask;
178 guestval |= guest_spec_ctrl & x86_spec_ctrl_mask;
179
cc69b349 180 /* SSBD controlled in MSR_SPEC_CTRL */
612bc3b3
TL
181 if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
182 static_cpu_has(X86_FEATURE_AMD_SSBD))
be6fcb54 183 hostval |= ssbd_tif_to_spec_ctrl(ti->flags);
cc69b349 184
5bfbe3ad
TC
185 /* Conditional STIBP enabled? */
186 if (static_branch_unlikely(&switch_to_cond_stibp))
187 hostval |= stibp_tif_to_spec_ctrl(ti->flags);
188
be6fcb54
TG
189 if (hostval != guestval) {
190 msrval = setguest ? guestval : hostval;
191 wrmsrl(MSR_IA32_SPEC_CTRL, msrval);
cc69b349
BP
192 }
193 }
47c61b39
TG
194
195 /*
196 * If SSBD is not handled in MSR_SPEC_CTRL on AMD, update
197 * MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported.
198 */
199 if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
200 !static_cpu_has(X86_FEATURE_VIRT_SSBD))
201 return;
202
203 /*
204 * If the host has SSBD mitigation enabled, force it in the host's
205 * virtual MSR value. If its not permanently enabled, evaluate
206 * current's TIF_SSBD thread flag.
207 */
208 if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE))
209 hostval = SPEC_CTRL_SSBD;
210 else
211 hostval = ssbd_tif_to_spec_ctrl(ti->flags);
212
213 /* Sanitize the guest value */
214 guestval = guest_virt_spec_ctrl & SPEC_CTRL_SSBD;
215
216 if (hostval != guestval) {
217 unsigned long tif;
218
219 tif = setguest ? ssbd_spec_ctrl_to_tif(guestval) :
220 ssbd_spec_ctrl_to_tif(hostval);
221
26c4d75b 222 speculation_ctrl_update(tif);
47c61b39 223 }
5cf68754 224}
cc69b349 225EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl);
5cf68754 226
9f65fb29 227static void x86_amd_ssb_disable(void)
764f3c21 228{
9f65fb29 229 u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_ssbd_mask;
764f3c21 230
11fb0683
TL
231 if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
232 wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, SPEC_CTRL_SSBD);
233 else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
764f3c21
KRW
234 wrmsrl(MSR_AMD64_LS_CFG, msrval);
235}
236
bc124170
TG
237#undef pr_fmt
238#define pr_fmt(fmt) "MDS: " fmt
239
cae5ec34 240/* Default mitigation for MDS-affected CPUs */
bc124170 241static enum mds_mitigations mds_mitigation __ro_after_init = MDS_MITIGATION_FULL;
d71eb0ce 242static bool mds_nosmt __ro_after_init = false;
bc124170
TG
243
244static const char * const mds_strings[] = {
245 [MDS_MITIGATION_OFF] = "Vulnerable",
22dd8365
TG
246 [MDS_MITIGATION_FULL] = "Mitigation: Clear CPU buffers",
247 [MDS_MITIGATION_VMWERV] = "Vulnerable: Clear CPU buffers attempted, no microcode",
bc124170
TG
248};
249
250static void __init mds_select_mitigation(void)
251{
5c14068f 252 if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off()) {
bc124170
TG
253 mds_mitigation = MDS_MITIGATION_OFF;
254 return;
255 }
256
257 if (mds_mitigation == MDS_MITIGATION_FULL) {
22dd8365
TG
258 if (!boot_cpu_has(X86_FEATURE_MD_CLEAR))
259 mds_mitigation = MDS_MITIGATION_VMWERV;
d71eb0ce 260
22dd8365 261 static_branch_enable(&mds_user_clear);
d71eb0ce 262
5c14068f
JP
263 if (!boot_cpu_has(X86_BUG_MSBDS_ONLY) &&
264 (mds_nosmt || cpu_mitigations_auto_nosmt()))
d71eb0ce 265 cpu_smt_disable(false);
bc124170 266 }
cd5a2aa8
WL
267}
268
269static void __init mds_print_mitigation(void)
270{
271 if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off())
272 return;
d71eb0ce 273
bc124170
TG
274 pr_info("%s\n", mds_strings[mds_mitigation]);
275}
276
277static int __init mds_cmdline(char *str)
278{
279 if (!boot_cpu_has_bug(X86_BUG_MDS))
280 return 0;
281
282 if (!str)
283 return -EINVAL;
284
285 if (!strcmp(str, "off"))
286 mds_mitigation = MDS_MITIGATION_OFF;
287 else if (!strcmp(str, "full"))
288 mds_mitigation = MDS_MITIGATION_FULL;
d71eb0ce
JP
289 else if (!strcmp(str, "full,nosmt")) {
290 mds_mitigation = MDS_MITIGATION_FULL;
291 mds_nosmt = true;
292 }
bc124170
TG
293
294 return 0;
295}
296early_param("mds", mds_cmdline);
297
1b42f017
PG
298#undef pr_fmt
299#define pr_fmt(fmt) "TAA: " fmt
300
72c2ce98
BP
301enum taa_mitigations {
302 TAA_MITIGATION_OFF,
303 TAA_MITIGATION_UCODE_NEEDED,
304 TAA_MITIGATION_VERW,
305 TAA_MITIGATION_TSX_DISABLED,
306};
307
1b42f017
PG
308/* Default mitigation for TAA-affected CPUs */
309static enum taa_mitigations taa_mitigation __ro_after_init = TAA_MITIGATION_VERW;
310static bool taa_nosmt __ro_after_init;
311
312static const char * const taa_strings[] = {
313 [TAA_MITIGATION_OFF] = "Vulnerable",
314 [TAA_MITIGATION_UCODE_NEEDED] = "Vulnerable: Clear CPU buffers attempted, no microcode",
315 [TAA_MITIGATION_VERW] = "Mitigation: Clear CPU buffers",
316 [TAA_MITIGATION_TSX_DISABLED] = "Mitigation: TSX disabled",
317};
318
319static void __init taa_select_mitigation(void)
320{
321 u64 ia32_cap;
322
323 if (!boot_cpu_has_bug(X86_BUG_TAA)) {
324 taa_mitigation = TAA_MITIGATION_OFF;
325 return;
326 }
327
328 /* TSX previously disabled by tsx=off */
329 if (!boot_cpu_has(X86_FEATURE_RTM)) {
330 taa_mitigation = TAA_MITIGATION_TSX_DISABLED;
331 goto out;
332 }
333
334 if (cpu_mitigations_off()) {
335 taa_mitigation = TAA_MITIGATION_OFF;
336 return;
337 }
338
64870ed1
WL
339 /*
340 * TAA mitigation via VERW is turned off if both
341 * tsx_async_abort=off and mds=off are specified.
342 */
343 if (taa_mitigation == TAA_MITIGATION_OFF &&
344 mds_mitigation == MDS_MITIGATION_OFF)
1b42f017
PG
345 goto out;
346
347 if (boot_cpu_has(X86_FEATURE_MD_CLEAR))
348 taa_mitigation = TAA_MITIGATION_VERW;
349 else
350 taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
351
352 /*
353 * VERW doesn't clear the CPU buffers when MD_CLEAR=1 and MDS_NO=1.
354 * A microcode update fixes this behavior to clear CPU buffers. It also
355 * adds support for MSR_IA32_TSX_CTRL which is enumerated by the
356 * ARCH_CAP_TSX_CTRL_MSR bit.
357 *
358 * On MDS_NO=1 CPUs if ARCH_CAP_TSX_CTRL_MSR is not set, microcode
359 * update is required.
360 */
361 ia32_cap = x86_read_arch_cap_msr();
362 if ( (ia32_cap & ARCH_CAP_MDS_NO) &&
363 !(ia32_cap & ARCH_CAP_TSX_CTRL_MSR))
364 taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
365
366 /*
367 * TSX is enabled, select alternate mitigation for TAA which is
368 * the same as MDS. Enable MDS static branch to clear CPU buffers.
369 *
370 * For guests that can't determine whether the correct microcode is
371 * present on host, enable the mitigation for UCODE_NEEDED as well.
372 */
373 static_branch_enable(&mds_user_clear);
374
375 if (taa_nosmt || cpu_mitigations_auto_nosmt())
376 cpu_smt_disable(false);
377
64870ed1
WL
378 /*
379 * Update MDS mitigation, if necessary, as the mds_user_clear is
380 * now enabled for TAA mitigation.
381 */
382 if (mds_mitigation == MDS_MITIGATION_OFF &&
383 boot_cpu_has_bug(X86_BUG_MDS)) {
384 mds_mitigation = MDS_MITIGATION_FULL;
385 mds_select_mitigation();
386 }
1b42f017
PG
387out:
388 pr_info("%s\n", taa_strings[taa_mitigation]);
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
7e5b3c26
MG
412#undef pr_fmt
413#define pr_fmt(fmt) "SRBDS: " fmt
414
415enum srbds_mitigations {
416 SRBDS_MITIGATION_OFF,
417 SRBDS_MITIGATION_UCODE_NEEDED,
418 SRBDS_MITIGATION_FULL,
419 SRBDS_MITIGATION_TSX_OFF,
420 SRBDS_MITIGATION_HYPERVISOR,
421};
422
423static enum srbds_mitigations srbds_mitigation __ro_after_init = SRBDS_MITIGATION_FULL;
424
425static const char * const srbds_strings[] = {
426 [SRBDS_MITIGATION_OFF] = "Vulnerable",
427 [SRBDS_MITIGATION_UCODE_NEEDED] = "Vulnerable: No microcode",
428 [SRBDS_MITIGATION_FULL] = "Mitigation: Microcode",
429 [SRBDS_MITIGATION_TSX_OFF] = "Mitigation: TSX disabled",
430 [SRBDS_MITIGATION_HYPERVISOR] = "Unknown: Dependent on hypervisor status",
431};
432
433static bool srbds_off;
434
435void update_srbds_msr(void)
436{
437 u64 mcu_ctrl;
438
439 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
440 return;
441
442 if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
443 return;
444
445 if (srbds_mitigation == SRBDS_MITIGATION_UCODE_NEEDED)
446 return;
447
448 rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
449
450 switch (srbds_mitigation) {
451 case SRBDS_MITIGATION_OFF:
452 case SRBDS_MITIGATION_TSX_OFF:
453 mcu_ctrl |= RNGDS_MITG_DIS;
454 break;
455 case SRBDS_MITIGATION_FULL:
456 mcu_ctrl &= ~RNGDS_MITG_DIS;
457 break;
458 default:
459 break;
460 }
461
462 wrmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
463}
464
465static void __init srbds_select_mitigation(void)
466{
467 u64 ia32_cap;
468
469 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
470 return;
471
472 /*
473 * Check to see if this is one of the MDS_NO systems supporting
474 * TSX that are only exposed to SRBDS when TSX is enabled.
475 */
476 ia32_cap = x86_read_arch_cap_msr();
477 if ((ia32_cap & ARCH_CAP_MDS_NO) && !boot_cpu_has(X86_FEATURE_RTM))
478 srbds_mitigation = SRBDS_MITIGATION_TSX_OFF;
479 else if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
480 srbds_mitigation = SRBDS_MITIGATION_HYPERVISOR;
481 else if (!boot_cpu_has(X86_FEATURE_SRBDS_CTRL))
482 srbds_mitigation = SRBDS_MITIGATION_UCODE_NEEDED;
483 else if (cpu_mitigations_off() || srbds_off)
484 srbds_mitigation = SRBDS_MITIGATION_OFF;
485
486 update_srbds_msr();
487 pr_info("%s\n", srbds_strings[srbds_mitigation]);
488}
489
490static int __init srbds_parse_cmdline(char *str)
491{
492 if (!str)
493 return -EINVAL;
494
495 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
496 return 0;
497
498 srbds_off = !strcmp(str, "off");
499 return 0;
500}
501early_param("srbds", srbds_parse_cmdline);
502
b5f06f64
BS
503#undef pr_fmt
504#define pr_fmt(fmt) "L1D Flush : " fmt
505
506enum l1d_flush_mitigations {
507 L1D_FLUSH_OFF = 0,
508 L1D_FLUSH_ON,
509};
510
511static enum l1d_flush_mitigations l1d_flush_mitigation __initdata = L1D_FLUSH_OFF;
512
513static void __init l1d_flush_select_mitigation(void)
514{
515 if (!l1d_flush_mitigation || !boot_cpu_has(X86_FEATURE_FLUSH_L1D))
516 return;
517
518 static_branch_enable(&switch_mm_cond_l1d_flush);
519 pr_info("Conditional flush on switch_mm() enabled\n");
520}
521
522static int __init l1d_flush_parse_cmdline(char *str)
523{
524 if (!strcmp(str, "on"))
525 l1d_flush_mitigation = L1D_FLUSH_ON;
526
527 return 0;
528}
529early_param("l1d_flush", l1d_flush_parse_cmdline);
530
a2059825
JP
531#undef pr_fmt
532#define pr_fmt(fmt) "Spectre V1 : " fmt
533
534enum spectre_v1_mitigation {
535 SPECTRE_V1_MITIGATION_NONE,
536 SPECTRE_V1_MITIGATION_AUTO,
537};
538
539static enum spectre_v1_mitigation spectre_v1_mitigation __ro_after_init =
540 SPECTRE_V1_MITIGATION_AUTO;
541
542static const char * const spectre_v1_strings[] = {
543 [SPECTRE_V1_MITIGATION_NONE] = "Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers",
544 [SPECTRE_V1_MITIGATION_AUTO] = "Mitigation: usercopy/swapgs barriers and __user pointer sanitization",
545};
546
a2059825
JP
547/*
548 * Does SMAP provide full mitigation against speculative kernel access to
549 * userspace?
550 */
551static bool smap_works_speculatively(void)
552{
553 if (!boot_cpu_has(X86_FEATURE_SMAP))
554 return false;
555
556 /*
557 * On CPUs which are vulnerable to Meltdown, SMAP does not
558 * prevent speculative access to user data in the L1 cache.
559 * Consider SMAP to be non-functional as a mitigation on these
560 * CPUs.
561 */
562 if (boot_cpu_has(X86_BUG_CPU_MELTDOWN))
563 return false;
564
565 return true;
566}
567
568static void __init spectre_v1_select_mitigation(void)
569{
570 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1) || cpu_mitigations_off()) {
571 spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
572 return;
573 }
574
575 if (spectre_v1_mitigation == SPECTRE_V1_MITIGATION_AUTO) {
576 /*
577 * With Spectre v1, a user can speculatively control either
578 * path of a conditional swapgs with a user-controlled GS
579 * value. The mitigation is to add lfences to both code paths.
580 *
581 * If FSGSBASE is enabled, the user can put a kernel address in
582 * GS, in which case SMAP provides no protection.
583 *
a2059825
JP
584 * If FSGSBASE is disabled, the user can only put a user space
585 * address in GS. That makes an attack harder, but still
586 * possible if there's no SMAP protection.
587 */
978e1342
TL
588 if (boot_cpu_has(X86_FEATURE_FSGSBASE) ||
589 !smap_works_speculatively()) {
a2059825
JP
590 /*
591 * Mitigation can be provided from SWAPGS itself or
592 * PTI as the CR3 write in the Meltdown mitigation
593 * is serializing.
594 *
f36cf386
TG
595 * If neither is there, mitigate with an LFENCE to
596 * stop speculation through swapgs.
a2059825 597 */
f36cf386
TG
598 if (boot_cpu_has_bug(X86_BUG_SWAPGS) &&
599 !boot_cpu_has(X86_FEATURE_PTI))
a2059825
JP
600 setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_USER);
601
602 /*
603 * Enable lfences in the kernel entry (non-swapgs)
604 * paths, to prevent user entry from speculatively
605 * skipping swapgs.
606 */
607 setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_KERNEL);
608 }
609 }
610
611 pr_info("%s\n", spectre_v1_strings[spectre_v1_mitigation]);
612}
613
614static int __init nospectre_v1_cmdline(char *str)
615{
616 spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
617 return 0;
618}
619early_param("nospectre_v1", nospectre_v1_cmdline);
620
15d6b7aa
TG
621#undef pr_fmt
622#define pr_fmt(fmt) "Spectre V2 : " fmt
623
624static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init =
625 SPECTRE_V2_NONE;
626
21998a35
AS
627static enum spectre_v2_user_mitigation spectre_v2_user_stibp __ro_after_init =
628 SPECTRE_V2_USER_NONE;
629static enum spectre_v2_user_mitigation spectre_v2_user_ibpb __ro_after_init =
fa1202ef
TG
630 SPECTRE_V2_USER_NONE;
631
e4f35891 632#ifdef CONFIG_RETPOLINE
e383095c
TG
633static bool spectre_v2_bad_module;
634
caf7501a
AK
635bool retpoline_module_ok(bool has_retpoline)
636{
637 if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
638 return true;
639
e698dcdf 640 pr_err("System may be vulnerable to spectre v2\n");
caf7501a
AK
641 spectre_v2_bad_module = true;
642 return false;
643}
e383095c
TG
644
645static inline const char *spectre_v2_module_string(void)
646{
647 return spectre_v2_bad_module ? " - vulnerable module loaded" : "";
648}
649#else
650static inline const char *spectre_v2_module_string(void) { return ""; }
caf7501a 651#endif
da285121 652
da285121
DW
653static inline bool match_option(const char *arg, int arglen, const char *opt)
654{
655 int len = strlen(opt);
656
657 return len == arglen && !strncmp(arg, opt, len);
658}
659
15d6b7aa
TG
660/* The kernel command line selection for spectre v2 */
661enum spectre_v2_mitigation_cmd {
662 SPECTRE_V2_CMD_NONE,
663 SPECTRE_V2_CMD_AUTO,
664 SPECTRE_V2_CMD_FORCE,
665 SPECTRE_V2_CMD_RETPOLINE,
666 SPECTRE_V2_CMD_RETPOLINE_GENERIC,
667 SPECTRE_V2_CMD_RETPOLINE_AMD,
668};
669
fa1202ef
TG
670enum spectre_v2_user_cmd {
671 SPECTRE_V2_USER_CMD_NONE,
672 SPECTRE_V2_USER_CMD_AUTO,
673 SPECTRE_V2_USER_CMD_FORCE,
7cc765a6 674 SPECTRE_V2_USER_CMD_PRCTL,
55a97402 675 SPECTRE_V2_USER_CMD_PRCTL_IBPB,
6b3e64c2 676 SPECTRE_V2_USER_CMD_SECCOMP,
55a97402 677 SPECTRE_V2_USER_CMD_SECCOMP_IBPB,
fa1202ef
TG
678};
679
680static const char * const spectre_v2_user_strings[] = {
20c3a2c3
TL
681 [SPECTRE_V2_USER_NONE] = "User space: Vulnerable",
682 [SPECTRE_V2_USER_STRICT] = "User space: Mitigation: STIBP protection",
683 [SPECTRE_V2_USER_STRICT_PREFERRED] = "User space: Mitigation: STIBP always-on protection",
684 [SPECTRE_V2_USER_PRCTL] = "User space: Mitigation: STIBP via prctl",
685 [SPECTRE_V2_USER_SECCOMP] = "User space: Mitigation: STIBP via seccomp and prctl",
fa1202ef
TG
686};
687
688static const struct {
689 const char *option;
690 enum spectre_v2_user_cmd cmd;
691 bool secure;
1de7edbb 692} v2_user_options[] __initconst = {
55a97402
TG
693 { "auto", SPECTRE_V2_USER_CMD_AUTO, false },
694 { "off", SPECTRE_V2_USER_CMD_NONE, false },
695 { "on", SPECTRE_V2_USER_CMD_FORCE, true },
696 { "prctl", SPECTRE_V2_USER_CMD_PRCTL, false },
697 { "prctl,ibpb", SPECTRE_V2_USER_CMD_PRCTL_IBPB, false },
698 { "seccomp", SPECTRE_V2_USER_CMD_SECCOMP, false },
699 { "seccomp,ibpb", SPECTRE_V2_USER_CMD_SECCOMP_IBPB, false },
fa1202ef
TG
700};
701
702static void __init spec_v2_user_print_cond(const char *reason, bool secure)
703{
704 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
705 pr_info("spectre_v2_user=%s forced on command line.\n", reason);
706}
707
708static enum spectre_v2_user_cmd __init
709spectre_v2_parse_user_cmdline(enum spectre_v2_mitigation_cmd v2_cmd)
710{
711 char arg[20];
712 int ret, i;
713
714 switch (v2_cmd) {
715 case SPECTRE_V2_CMD_NONE:
716 return SPECTRE_V2_USER_CMD_NONE;
717 case SPECTRE_V2_CMD_FORCE:
718 return SPECTRE_V2_USER_CMD_FORCE;
719 default:
720 break;
721 }
722
723 ret = cmdline_find_option(boot_command_line, "spectre_v2_user",
724 arg, sizeof(arg));
725 if (ret < 0)
726 return SPECTRE_V2_USER_CMD_AUTO;
727
728 for (i = 0; i < ARRAY_SIZE(v2_user_options); i++) {
729 if (match_option(arg, ret, v2_user_options[i].option)) {
730 spec_v2_user_print_cond(v2_user_options[i].option,
731 v2_user_options[i].secure);
732 return v2_user_options[i].cmd;
733 }
734 }
735
736 pr_err("Unknown user space protection option (%s). Switching to AUTO select\n", arg);
737 return SPECTRE_V2_USER_CMD_AUTO;
738}
739
740static void __init
741spectre_v2_user_select_mitigation(enum spectre_v2_mitigation_cmd v2_cmd)
742{
743 enum spectre_v2_user_mitigation mode = SPECTRE_V2_USER_NONE;
744 bool smt_possible = IS_ENABLED(CONFIG_SMP);
55a97402 745 enum spectre_v2_user_cmd cmd;
fa1202ef
TG
746
747 if (!boot_cpu_has(X86_FEATURE_IBPB) && !boot_cpu_has(X86_FEATURE_STIBP))
748 return;
749
750 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
751 cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
752 smt_possible = false;
753
55a97402
TG
754 cmd = spectre_v2_parse_user_cmdline(v2_cmd);
755 switch (cmd) {
fa1202ef
TG
756 case SPECTRE_V2_USER_CMD_NONE:
757 goto set_mode;
758 case SPECTRE_V2_USER_CMD_FORCE:
759 mode = SPECTRE_V2_USER_STRICT;
760 break;
7cc765a6 761 case SPECTRE_V2_USER_CMD_PRCTL:
55a97402 762 case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
7cc765a6
TG
763 mode = SPECTRE_V2_USER_PRCTL;
764 break;
6b3e64c2
TG
765 case SPECTRE_V2_USER_CMD_AUTO:
766 case SPECTRE_V2_USER_CMD_SECCOMP:
55a97402 767 case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
6b3e64c2
TG
768 if (IS_ENABLED(CONFIG_SECCOMP))
769 mode = SPECTRE_V2_USER_SECCOMP;
770 else
771 mode = SPECTRE_V2_USER_PRCTL;
772 break;
fa1202ef
TG
773 }
774
775 /* Initialize Indirect Branch Prediction Barrier */
776 if (boot_cpu_has(X86_FEATURE_IBPB)) {
777 setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
4c71a2b6 778
33fc379d 779 spectre_v2_user_ibpb = mode;
55a97402
TG
780 switch (cmd) {
781 case SPECTRE_V2_USER_CMD_FORCE:
782 case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
783 case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
4c71a2b6 784 static_branch_enable(&switch_mm_always_ibpb);
33fc379d 785 spectre_v2_user_ibpb = SPECTRE_V2_USER_STRICT;
4c71a2b6 786 break;
55a97402
TG
787 case SPECTRE_V2_USER_CMD_PRCTL:
788 case SPECTRE_V2_USER_CMD_AUTO:
789 case SPECTRE_V2_USER_CMD_SECCOMP:
7cc765a6
TG
790 static_branch_enable(&switch_mm_cond_ibpb);
791 break;
4c71a2b6
TG
792 default:
793 break;
794 }
795
796 pr_info("mitigation: Enabling %s Indirect Branch Prediction Barrier\n",
55a97402
TG
797 static_key_enabled(&switch_mm_always_ibpb) ?
798 "always-on" : "conditional");
fa1202ef
TG
799 }
800
21998a35 801 /*
a5ce9f2b 802 * If no STIBP, enhanced IBRS is enabled or SMT impossible, STIBP is not
21998a35
AS
803 * required.
804 */
a5ce9f2b
BP
805 if (!boot_cpu_has(X86_FEATURE_STIBP) ||
806 !smt_possible ||
807 spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
fa1202ef
TG
808 return;
809
7cc765a6 810 /*
21998a35
AS
811 * At this point, an STIBP mode other than "off" has been set.
812 * If STIBP support is not being forced, check if STIBP always-on
813 * is preferred.
7cc765a6 814 */
21998a35
AS
815 if (mode != SPECTRE_V2_USER_STRICT &&
816 boot_cpu_has(X86_FEATURE_AMD_STIBP_ALWAYS_ON))
817 mode = SPECTRE_V2_USER_STRICT_PREFERRED;
818
21998a35
AS
819 spectre_v2_user_stibp = mode;
820
fa1202ef 821set_mode:
21998a35 822 pr_info("%s\n", spectre_v2_user_strings[mode]);
fa1202ef
TG
823}
824
8770709f 825static const char * const spectre_v2_strings[] = {
15d6b7aa
TG
826 [SPECTRE_V2_NONE] = "Vulnerable",
827 [SPECTRE_V2_RETPOLINE_GENERIC] = "Mitigation: Full generic retpoline",
828 [SPECTRE_V2_RETPOLINE_AMD] = "Mitigation: Full AMD retpoline",
829 [SPECTRE_V2_IBRS_ENHANCED] = "Mitigation: Enhanced IBRS",
830};
831
9005c683
KA
832static const struct {
833 const char *option;
834 enum spectre_v2_mitigation_cmd cmd;
835 bool secure;
1de7edbb 836} mitigation_options[] __initconst = {
15d6b7aa
TG
837 { "off", SPECTRE_V2_CMD_NONE, false },
838 { "on", SPECTRE_V2_CMD_FORCE, true },
839 { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false },
840 { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_AMD, false },
841 { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
842 { "auto", SPECTRE_V2_CMD_AUTO, false },
9005c683
KA
843};
844
495d470e 845static void __init spec_v2_print_cond(const char *reason, bool secure)
15d6b7aa 846{
495d470e 847 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
15d6b7aa
TG
848 pr_info("%s selected on command line.\n", reason);
849}
850
da285121
DW
851static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
852{
15d6b7aa 853 enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
da285121 854 char arg[20];
9005c683 855 int ret, i;
9005c683 856
d68be4c4
JP
857 if (cmdline_find_option_bool(boot_command_line, "nospectre_v2") ||
858 cpu_mitigations_off())
9005c683 859 return SPECTRE_V2_CMD_NONE;
9005c683 860
24848509
TC
861 ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
862 if (ret < 0)
863 return SPECTRE_V2_CMD_AUTO;
864
865 for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
866 if (!match_option(arg, ret, mitigation_options[i].option))
867 continue;
868 cmd = mitigation_options[i].cmd;
869 break;
870 }
871
872 if (i >= ARRAY_SIZE(mitigation_options)) {
873 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
874 return SPECTRE_V2_CMD_AUTO;
da285121
DW
875 }
876
9005c683
KA
877 if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
878 cmd == SPECTRE_V2_CMD_RETPOLINE_AMD ||
879 cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC) &&
880 !IS_ENABLED(CONFIG_RETPOLINE)) {
21e433bd 881 pr_err("%s selected but not compiled in. Switching to AUTO select\n", mitigation_options[i].option);
da285121 882 return SPECTRE_V2_CMD_AUTO;
9005c683
KA
883 }
884
885 if (cmd == SPECTRE_V2_CMD_RETPOLINE_AMD &&
1a576b23 886 boot_cpu_data.x86_vendor != X86_VENDOR_HYGON &&
9005c683
KA
887 boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
888 pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n");
889 return SPECTRE_V2_CMD_AUTO;
890 }
891
495d470e
TG
892 spec_v2_print_cond(mitigation_options[i].option,
893 mitigation_options[i].secure);
9005c683 894 return cmd;
da285121
DW
895}
896
897static void __init spectre_v2_select_mitigation(void)
898{
899 enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
900 enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
901
902 /*
903 * If the CPU is not affected and the command line mode is NONE or AUTO
904 * then nothing to do.
905 */
906 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
907 (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
908 return;
909
910 switch (cmd) {
911 case SPECTRE_V2_CMD_NONE:
912 return;
913
914 case SPECTRE_V2_CMD_FORCE:
da285121 915 case SPECTRE_V2_CMD_AUTO:
706d5168
SP
916 if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
917 mode = SPECTRE_V2_IBRS_ENHANCED;
918 /* Force it so VMEXIT will restore correctly */
919 x86_spec_ctrl_base |= SPEC_CTRL_IBRS;
920 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
921 goto specv2_set_mode;
922 }
9471eee9
DL
923 if (IS_ENABLED(CONFIG_RETPOLINE))
924 goto retpoline_auto;
925 break;
da285121
DW
926 case SPECTRE_V2_CMD_RETPOLINE_AMD:
927 if (IS_ENABLED(CONFIG_RETPOLINE))
928 goto retpoline_amd;
929 break;
930 case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
931 if (IS_ENABLED(CONFIG_RETPOLINE))
932 goto retpoline_generic;
933 break;
934 case SPECTRE_V2_CMD_RETPOLINE:
935 if (IS_ENABLED(CONFIG_RETPOLINE))
936 goto retpoline_auto;
937 break;
938 }
21e433bd 939 pr_err("Spectre mitigation: kernel not compiled with retpoline; no mitigation available!");
da285121
DW
940 return;
941
942retpoline_auto:
1a576b23
PW
943 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
944 boot_cpu_data.x86_vendor == X86_VENDOR_HYGON) {
da285121
DW
945 retpoline_amd:
946 if (!boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
21e433bd 947 pr_err("Spectre mitigation: LFENCE not serializing, switching to generic retpoline\n");
da285121
DW
948 goto retpoline_generic;
949 }
ef014aae 950 mode = SPECTRE_V2_RETPOLINE_AMD;
da285121
DW
951 setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD);
952 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
953 } else {
954 retpoline_generic:
ef014aae 955 mode = SPECTRE_V2_RETPOLINE_GENERIC;
da285121
DW
956 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
957 }
958
706d5168 959specv2_set_mode:
da285121
DW
960 spectre_v2_enabled = mode;
961 pr_info("%s\n", spectre_v2_strings[mode]);
c995efd5
DW
962
963 /*
fdf82a78
JK
964 * If spectre v2 protection has been enabled, unconditionally fill
965 * RSB during a context switch; this protects against two independent
966 * issues:
c995efd5 967 *
fdf82a78
JK
968 * - RSB underflow (and switch to BTB) on Skylake+
969 * - SpectreRSB variant of spectre v2 on X86_BUG_SPECTRE_V2 CPUs
c995efd5 970 */
fdf82a78
JK
971 setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
972 pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n");
20ffa1ca 973
dd84441a
DW
974 /*
975 * Retpoline means the kernel is safe because it has no indirect
706d5168
SP
976 * branches. Enhanced IBRS protects firmware too, so, enable restricted
977 * speculation around firmware calls only when Enhanced IBRS isn't
978 * supported.
979 *
980 * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because
981 * the user might select retpoline on the kernel command line and if
982 * the CPU supports Enhanced IBRS, kernel might un-intentionally not
983 * enable IBRS around firmware calls.
dd84441a 984 */
706d5168 985 if (boot_cpu_has(X86_FEATURE_IBRS) && mode != SPECTRE_V2_IBRS_ENHANCED) {
dd84441a
DW
986 setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
987 pr_info("Enabling Restricted Speculation for firmware calls\n");
988 }
53c613fe 989
fa1202ef
TG
990 /* Set up IBPB and STIBP depending on the general spectre V2 command */
991 spectre_v2_user_select_mitigation(cmd);
da285121
DW
992}
993
6893a959 994static void update_stibp_msr(void * __unused)
15d6b7aa 995{
6893a959 996 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
15d6b7aa
TG
997}
998
6893a959
TG
999/* Update x86_spec_ctrl_base in case SMT state changed. */
1000static void update_stibp_strict(void)
15d6b7aa 1001{
6893a959
TG
1002 u64 mask = x86_spec_ctrl_base & ~SPEC_CTRL_STIBP;
1003
1004 if (sched_smt_active())
1005 mask |= SPEC_CTRL_STIBP;
1006
1007 if (mask == x86_spec_ctrl_base)
1008 return;
1009
1010 pr_info("Update user space SMT mitigation: STIBP %s\n",
1011 mask & SPEC_CTRL_STIBP ? "always-on" : "off");
1012 x86_spec_ctrl_base = mask;
1013 on_each_cpu(update_stibp_msr, NULL, 1);
15d6b7aa
TG
1014}
1015
7cc765a6
TG
1016/* Update the static key controlling the evaluation of TIF_SPEC_IB */
1017static void update_indir_branch_cond(void)
1018{
1019 if (sched_smt_active())
1020 static_branch_enable(&switch_to_cond_stibp);
1021 else
1022 static_branch_disable(&switch_to_cond_stibp);
1023}
1024
39226ef0
JP
1025#undef pr_fmt
1026#define pr_fmt(fmt) fmt
1027
bc124170
TG
1028/* Update the static key controlling the MDS CPU buffer clear in idle */
1029static void update_mds_branch_idle(void)
1030{
1031 /*
1032 * Enable the idle clearing if SMT is active on CPUs which are
1033 * affected only by MSBDS and not any other MDS variant.
1034 *
1035 * The other variants cannot be mitigated when SMT is enabled, so
1036 * clearing the buffers on idle just to prevent the Store Buffer
1037 * repartitioning leak would be a window dressing exercise.
1038 */
1039 if (!boot_cpu_has_bug(X86_BUG_MSBDS_ONLY))
1040 return;
1041
1042 if (sched_smt_active())
1043 static_branch_enable(&mds_idle_clear);
1044 else
1045 static_branch_disable(&mds_idle_clear);
1046}
1047
39226ef0 1048#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 1049#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"
39226ef0 1050
9c92374b 1051void cpu_bugs_smt_update(void)
15d6b7aa 1052{
15d6b7aa
TG
1053 mutex_lock(&spec_ctrl_mutex);
1054
21998a35 1055 switch (spectre_v2_user_stibp) {
6893a959
TG
1056 case SPECTRE_V2_USER_NONE:
1057 break;
1058 case SPECTRE_V2_USER_STRICT:
20c3a2c3 1059 case SPECTRE_V2_USER_STRICT_PREFERRED:
6893a959
TG
1060 update_stibp_strict();
1061 break;
9137bb27 1062 case SPECTRE_V2_USER_PRCTL:
6b3e64c2 1063 case SPECTRE_V2_USER_SECCOMP:
7cc765a6 1064 update_indir_branch_cond();
9137bb27 1065 break;
15d6b7aa 1066 }
6893a959 1067
22dd8365
TG
1068 switch (mds_mitigation) {
1069 case MDS_MITIGATION_FULL:
1070 case MDS_MITIGATION_VMWERV:
39226ef0
JP
1071 if (sched_smt_active() && !boot_cpu_has(X86_BUG_MSBDS_ONLY))
1072 pr_warn_once(MDS_MSG_SMT);
bc124170 1073 update_mds_branch_idle();
22dd8365
TG
1074 break;
1075 case MDS_MITIGATION_OFF:
1076 break;
1077 }
bc124170 1078
1b42f017
PG
1079 switch (taa_mitigation) {
1080 case TAA_MITIGATION_VERW:
1081 case TAA_MITIGATION_UCODE_NEEDED:
1082 if (sched_smt_active())
1083 pr_warn_once(TAA_MSG_SMT);
1084 break;
1085 case TAA_MITIGATION_TSX_DISABLED:
1086 case TAA_MITIGATION_OFF:
1087 break;
1088 }
1089
15d6b7aa
TG
1090 mutex_unlock(&spec_ctrl_mutex);
1091}
1092
24f7fc83
KRW
1093#undef pr_fmt
1094#define pr_fmt(fmt) "Speculative Store Bypass: " fmt
1095
f9544b2b 1096static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
24f7fc83
KRW
1097
1098/* The kernel command line selection */
1099enum ssb_mitigation_cmd {
1100 SPEC_STORE_BYPASS_CMD_NONE,
1101 SPEC_STORE_BYPASS_CMD_AUTO,
1102 SPEC_STORE_BYPASS_CMD_ON,
a73ec77e 1103 SPEC_STORE_BYPASS_CMD_PRCTL,
f21b53b2 1104 SPEC_STORE_BYPASS_CMD_SECCOMP,
24f7fc83
KRW
1105};
1106
8770709f 1107static const char * const ssb_strings[] = {
24f7fc83 1108 [SPEC_STORE_BYPASS_NONE] = "Vulnerable",
a73ec77e 1109 [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass disabled",
f21b53b2
KC
1110 [SPEC_STORE_BYPASS_PRCTL] = "Mitigation: Speculative Store Bypass disabled via prctl",
1111 [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
24f7fc83
KRW
1112};
1113
1114static const struct {
1115 const char *option;
1116 enum ssb_mitigation_cmd cmd;
1de7edbb 1117} ssb_mitigation_options[] __initconst = {
f21b53b2
KC
1118 { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */
1119 { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */
1120 { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */
1121 { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative Store Bypass via prctl */
1122 { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */
24f7fc83
KRW
1123};
1124
1125static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
1126{
1127 enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO;
1128 char arg[20];
1129 int ret, i;
1130
d68be4c4
JP
1131 if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable") ||
1132 cpu_mitigations_off()) {
24f7fc83
KRW
1133 return SPEC_STORE_BYPASS_CMD_NONE;
1134 } else {
1135 ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable",
1136 arg, sizeof(arg));
1137 if (ret < 0)
1138 return SPEC_STORE_BYPASS_CMD_AUTO;
1139
1140 for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) {
1141 if (!match_option(arg, ret, ssb_mitigation_options[i].option))
1142 continue;
1143
1144 cmd = ssb_mitigation_options[i].cmd;
1145 break;
1146 }
1147
1148 if (i >= ARRAY_SIZE(ssb_mitigation_options)) {
1149 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
1150 return SPEC_STORE_BYPASS_CMD_AUTO;
1151 }
1152 }
1153
1154 return cmd;
1155}
1156
d66d8ff3 1157static enum ssb_mitigation __init __ssb_select_mitigation(void)
24f7fc83
KRW
1158{
1159 enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
1160 enum ssb_mitigation_cmd cmd;
1161
9f65fb29 1162 if (!boot_cpu_has(X86_FEATURE_SSBD))
24f7fc83
KRW
1163 return mode;
1164
1165 cmd = ssb_parse_cmdline();
1166 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) &&
1167 (cmd == SPEC_STORE_BYPASS_CMD_NONE ||
1168 cmd == SPEC_STORE_BYPASS_CMD_AUTO))
1169 return mode;
1170
1171 switch (cmd) {
1172 case SPEC_STORE_BYPASS_CMD_AUTO:
f21b53b2
KC
1173 case SPEC_STORE_BYPASS_CMD_SECCOMP:
1174 /*
1175 * Choose prctl+seccomp as the default mode if seccomp is
1176 * enabled.
1177 */
1178 if (IS_ENABLED(CONFIG_SECCOMP))
1179 mode = SPEC_STORE_BYPASS_SECCOMP;
1180 else
1181 mode = SPEC_STORE_BYPASS_PRCTL;
a73ec77e 1182 break;
24f7fc83
KRW
1183 case SPEC_STORE_BYPASS_CMD_ON:
1184 mode = SPEC_STORE_BYPASS_DISABLE;
1185 break;
a73ec77e
TG
1186 case SPEC_STORE_BYPASS_CMD_PRCTL:
1187 mode = SPEC_STORE_BYPASS_PRCTL;
1188 break;
24f7fc83
KRW
1189 case SPEC_STORE_BYPASS_CMD_NONE:
1190 break;
1191 }
1192
c1f7fec1
AJ
1193 /*
1194 * If SSBD is controlled by the SPEC_CTRL MSR, then set the proper
1195 * bit in the mask to allow guests to use the mitigation even in the
1196 * case where the host does not enable it.
1197 */
1198 if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
1199 static_cpu_has(X86_FEATURE_AMD_SSBD)) {
1200 x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
1201 }
1202
77243971
KRW
1203 /*
1204 * We have three CPU feature flags that are in play here:
1205 * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
9f65fb29 1206 * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
77243971
KRW
1207 * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
1208 */
a73ec77e 1209 if (mode == SPEC_STORE_BYPASS_DISABLE) {
24f7fc83 1210 setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
77243971 1211 /*
6ac2f49e
KRW
1212 * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
1213 * use a completely different MSR and bit dependent on family.
77243971 1214 */
612bc3b3
TL
1215 if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
1216 !static_cpu_has(X86_FEATURE_AMD_SSBD)) {
108fab4b 1217 x86_amd_ssb_disable();
612bc3b3 1218 } else {
9f65fb29 1219 x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
4b59bdb5 1220 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
77243971
KRW
1221 }
1222 }
1223
24f7fc83
KRW
1224 return mode;
1225}
1226
ffed645e 1227static void ssb_select_mitigation(void)
24f7fc83
KRW
1228{
1229 ssb_mode = __ssb_select_mitigation();
1230
1231 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1232 pr_info("%s\n", ssb_strings[ssb_mode]);
1233}
1234
da285121 1235#undef pr_fmt
f21b53b2 1236#define pr_fmt(fmt) "Speculation prctl: " fmt
da285121 1237
6d991ba5 1238static void task_update_spec_tif(struct task_struct *tsk)
a73ec77e 1239{
6d991ba5
TG
1240 /* Force the update of the real TIF bits */
1241 set_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE);
e6da8bb6
TG
1242
1243 /*
1244 * Immediately update the speculation control MSRs for the current
1245 * task, but for a non-current task delay setting the CPU
1246 * mitigation until it is scheduled next.
1247 *
1248 * This can only happen for SECCOMP mitigation. For PRCTL it's
1249 * always the current task.
1250 */
6d991ba5 1251 if (tsk == current)
e6da8bb6
TG
1252 speculation_ctrl_update_current();
1253}
1254
1255static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
1256{
f21b53b2
KC
1257 if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
1258 ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
a73ec77e
TG
1259 return -ENXIO;
1260
356e4bff
TG
1261 switch (ctrl) {
1262 case PR_SPEC_ENABLE:
1263 /* If speculation is force disabled, enable is not allowed */
1264 if (task_spec_ssb_force_disable(task))
1265 return -EPERM;
1266 task_clear_spec_ssb_disable(task);
71368af9 1267 task_clear_spec_ssb_noexec(task);
6d991ba5 1268 task_update_spec_tif(task);
356e4bff
TG
1269 break;
1270 case PR_SPEC_DISABLE:
1271 task_set_spec_ssb_disable(task);
71368af9 1272 task_clear_spec_ssb_noexec(task);
6d991ba5 1273 task_update_spec_tif(task);
356e4bff
TG
1274 break;
1275 case PR_SPEC_FORCE_DISABLE:
1276 task_set_spec_ssb_disable(task);
1277 task_set_spec_ssb_force_disable(task);
71368af9
WL
1278 task_clear_spec_ssb_noexec(task);
1279 task_update_spec_tif(task);
1280 break;
1281 case PR_SPEC_DISABLE_NOEXEC:
1282 if (task_spec_ssb_force_disable(task))
1283 return -EPERM;
1284 task_set_spec_ssb_disable(task);
1285 task_set_spec_ssb_noexec(task);
6d991ba5 1286 task_update_spec_tif(task);
356e4bff
TG
1287 break;
1288 default:
1289 return -ERANGE;
1290 }
a73ec77e
TG
1291 return 0;
1292}
1293
1978b3a5
AM
1294static bool is_spec_ib_user_controlled(void)
1295{
1296 return spectre_v2_user_ibpb == SPECTRE_V2_USER_PRCTL ||
1297 spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
1298 spectre_v2_user_stibp == SPECTRE_V2_USER_PRCTL ||
1299 spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP;
1300}
1301
9137bb27
TG
1302static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)
1303{
1304 switch (ctrl) {
1305 case PR_SPEC_ENABLE:
21998a35
AS
1306 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1307 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
9137bb27 1308 return 0;
1978b3a5 1309
9137bb27 1310 /*
1978b3a5
AM
1311 * With strict mode for both IBPB and STIBP, the instruction
1312 * code paths avoid checking this task flag and instead,
1313 * unconditionally run the instruction. However, STIBP and IBPB
1314 * are independent and either can be set to conditionally
1315 * enabled regardless of the mode of the other.
1316 *
1317 * If either is set to conditional, allow the task flag to be
1318 * updated, unless it was force-disabled by a previous prctl
1319 * call. Currently, this is possible on an AMD CPU which has the
1320 * feature X86_FEATURE_AMD_STIBP_ALWAYS_ON. In this case, if the
1321 * kernel is booted with 'spectre_v2_user=seccomp', then
1322 * spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP and
1323 * spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED.
9137bb27 1324 */
1978b3a5 1325 if (!is_spec_ib_user_controlled() ||
4d8df8cb 1326 task_spec_ib_force_disable(task))
9137bb27 1327 return -EPERM;
1978b3a5 1328
9137bb27
TG
1329 task_clear_spec_ib_disable(task);
1330 task_update_spec_tif(task);
1331 break;
1332 case PR_SPEC_DISABLE:
1333 case PR_SPEC_FORCE_DISABLE:
1334 /*
1335 * Indirect branch speculation is always allowed when
1336 * mitigation is force disabled.
1337 */
21998a35
AS
1338 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1339 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
9137bb27 1340 return -EPERM;
1978b3a5
AM
1341
1342 if (!is_spec_ib_user_controlled())
9137bb27 1343 return 0;
1978b3a5 1344
9137bb27
TG
1345 task_set_spec_ib_disable(task);
1346 if (ctrl == PR_SPEC_FORCE_DISABLE)
1347 task_set_spec_ib_force_disable(task);
1348 task_update_spec_tif(task);
1349 break;
1350 default:
1351 return -ERANGE;
1352 }
1353 return 0;
1354}
1355
8bf37d8c
TG
1356int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
1357 unsigned long ctrl)
1358{
1359 switch (which) {
1360 case PR_SPEC_STORE_BYPASS:
1361 return ssb_prctl_set(task, ctrl);
9137bb27
TG
1362 case PR_SPEC_INDIRECT_BRANCH:
1363 return ib_prctl_set(task, ctrl);
8bf37d8c
TG
1364 default:
1365 return -ENODEV;
1366 }
1367}
1368
1369#ifdef CONFIG_SECCOMP
1370void arch_seccomp_spec_mitigate(struct task_struct *task)
1371{
f21b53b2
KC
1372 if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP)
1373 ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
21998a35
AS
1374 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
1375 spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP)
6b3e64c2 1376 ib_prctl_set(task, PR_SPEC_FORCE_DISABLE);
8bf37d8c
TG
1377}
1378#endif
1379
7bbf1373 1380static int ssb_prctl_get(struct task_struct *task)
a73ec77e
TG
1381{
1382 switch (ssb_mode) {
1383 case SPEC_STORE_BYPASS_DISABLE:
1384 return PR_SPEC_DISABLE;
f21b53b2 1385 case SPEC_STORE_BYPASS_SECCOMP:
a73ec77e 1386 case SPEC_STORE_BYPASS_PRCTL:
356e4bff
TG
1387 if (task_spec_ssb_force_disable(task))
1388 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
71368af9
WL
1389 if (task_spec_ssb_noexec(task))
1390 return PR_SPEC_PRCTL | PR_SPEC_DISABLE_NOEXEC;
356e4bff 1391 if (task_spec_ssb_disable(task))
a73ec77e
TG
1392 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
1393 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
1394 default:
1395 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1396 return PR_SPEC_ENABLE;
1397 return PR_SPEC_NOT_AFFECTED;
1398 }
1399}
1400
9137bb27
TG
1401static int ib_prctl_get(struct task_struct *task)
1402{
1403 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
1404 return PR_SPEC_NOT_AFFECTED;
1405
21998a35
AS
1406 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1407 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
9137bb27 1408 return PR_SPEC_ENABLE;
1978b3a5 1409 else if (is_spec_ib_user_controlled()) {
9137bb27
TG
1410 if (task_spec_ib_force_disable(task))
1411 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
1412 if (task_spec_ib_disable(task))
1413 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
1414 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
1978b3a5
AM
1415 } else if (spectre_v2_user_ibpb == SPECTRE_V2_USER_STRICT ||
1416 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
1417 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED)
1418 return PR_SPEC_DISABLE;
1419 else
9137bb27 1420 return PR_SPEC_NOT_AFFECTED;
9137bb27
TG
1421}
1422
7bbf1373 1423int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
a73ec77e
TG
1424{
1425 switch (which) {
1426 case PR_SPEC_STORE_BYPASS:
7bbf1373 1427 return ssb_prctl_get(task);
9137bb27
TG
1428 case PR_SPEC_INDIRECT_BRANCH:
1429 return ib_prctl_get(task);
a73ec77e
TG
1430 default:
1431 return -ENODEV;
1432 }
1433}
1434
77243971
KRW
1435void x86_spec_ctrl_setup_ap(void)
1436{
7eb8956a 1437 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
4b59bdb5 1438 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
764f3c21
KRW
1439
1440 if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
9f65fb29 1441 x86_amd_ssb_disable();
77243971
KRW
1442}
1443
b8e8c830
PB
1444bool itlb_multihit_kvm_mitigation;
1445EXPORT_SYMBOL_GPL(itlb_multihit_kvm_mitigation);
1446
56563f53
KRW
1447#undef pr_fmt
1448#define pr_fmt(fmt) "L1TF: " fmt
72c6d2db 1449
d90a7a0e
JK
1450/* Default mitigation for L1TF-affected CPUs */
1451enum l1tf_mitigations l1tf_mitigation __ro_after_init = L1TF_MITIGATION_FLUSH;
72c6d2db 1452#if IS_ENABLED(CONFIG_KVM_INTEL)
d90a7a0e 1453EXPORT_SYMBOL_GPL(l1tf_mitigation);
1eb46908 1454#endif
895ae47f 1455enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
72c6d2db 1456EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation);
72c6d2db 1457
cc51e542
AK
1458/*
1459 * These CPUs all support 44bits physical address space internally in the
1460 * cache but CPUID can report a smaller number of physical address bits.
1461 *
1462 * The L1TF mitigation uses the top most address bit for the inversion of
1463 * non present PTEs. When the installed memory reaches into the top most
1464 * address bit due to memory holes, which has been observed on machines
1465 * which report 36bits physical address bits and have 32G RAM installed,
1466 * then the mitigation range check in l1tf_select_mitigation() triggers.
1467 * This is a false positive because the mitigation is still possible due to
1468 * the fact that the cache uses 44bit internally. Use the cache bits
1469 * instead of the reported physical bits and adjust them on the affected
1470 * machines to 44bit if the reported bits are less than 44.
1471 */
1472static void override_cache_bits(struct cpuinfo_x86 *c)
1473{
1474 if (c->x86 != 6)
1475 return;
1476
1477 switch (c->x86_model) {
1478 case INTEL_FAM6_NEHALEM:
1479 case INTEL_FAM6_WESTMERE:
1480 case INTEL_FAM6_SANDYBRIDGE:
1481 case INTEL_FAM6_IVYBRIDGE:
c66f78a6 1482 case INTEL_FAM6_HASWELL:
af239c44 1483 case INTEL_FAM6_HASWELL_L:
5e741407 1484 case INTEL_FAM6_HASWELL_G:
c66f78a6 1485 case INTEL_FAM6_BROADWELL:
5e741407 1486 case INTEL_FAM6_BROADWELL_G:
af239c44 1487 case INTEL_FAM6_SKYLAKE_L:
c66f78a6 1488 case INTEL_FAM6_SKYLAKE:
af239c44 1489 case INTEL_FAM6_KABYLAKE_L:
c66f78a6 1490 case INTEL_FAM6_KABYLAKE:
cc51e542
AK
1491 if (c->x86_cache_bits < 44)
1492 c->x86_cache_bits = 44;
1493 break;
1494 }
1495}
1496
56563f53
KRW
1497static void __init l1tf_select_mitigation(void)
1498{
1499 u64 half_pa;
1500
1501 if (!boot_cpu_has_bug(X86_BUG_L1TF))
1502 return;
1503
d68be4c4
JP
1504 if (cpu_mitigations_off())
1505 l1tf_mitigation = L1TF_MITIGATION_OFF;
1506 else if (cpu_mitigations_auto_nosmt())
1507 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
1508
cc51e542
AK
1509 override_cache_bits(&boot_cpu_data);
1510
d90a7a0e
JK
1511 switch (l1tf_mitigation) {
1512 case L1TF_MITIGATION_OFF:
1513 case L1TF_MITIGATION_FLUSH_NOWARN:
1514 case L1TF_MITIGATION_FLUSH:
1515 break;
1516 case L1TF_MITIGATION_FLUSH_NOSMT:
1517 case L1TF_MITIGATION_FULL:
1518 cpu_smt_disable(false);
1519 break;
1520 case L1TF_MITIGATION_FULL_FORCE:
1521 cpu_smt_disable(true);
1522 break;
1523 }
1524
56563f53
KRW
1525#if CONFIG_PGTABLE_LEVELS == 2
1526 pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
1527 return;
1528#endif
1529
56563f53 1530 half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT;
5b5e4d62
MH
1531 if (l1tf_mitigation != L1TF_MITIGATION_OFF &&
1532 e820__mapped_any(half_pa, ULLONG_MAX - half_pa, E820_TYPE_RAM)) {
56563f53 1533 pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
6a012288
VB
1534 pr_info("You may make it effective by booting the kernel with mem=%llu parameter.\n",
1535 half_pa);
1536 pr_info("However, doing so will make a part of your RAM unusable.\n");
65fd4cb6 1537 pr_info("Reading https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html might help you decide.\n");
56563f53
KRW
1538 return;
1539 }
1540
1541 setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV);
1542}
d90a7a0e
JK
1543
1544static int __init l1tf_cmdline(char *str)
1545{
1546 if (!boot_cpu_has_bug(X86_BUG_L1TF))
1547 return 0;
1548
1549 if (!str)
1550 return -EINVAL;
1551
1552 if (!strcmp(str, "off"))
1553 l1tf_mitigation = L1TF_MITIGATION_OFF;
1554 else if (!strcmp(str, "flush,nowarn"))
1555 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOWARN;
1556 else if (!strcmp(str, "flush"))
1557 l1tf_mitigation = L1TF_MITIGATION_FLUSH;
1558 else if (!strcmp(str, "flush,nosmt"))
1559 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
1560 else if (!strcmp(str, "full"))
1561 l1tf_mitigation = L1TF_MITIGATION_FULL;
1562 else if (!strcmp(str, "full,force"))
1563 l1tf_mitigation = L1TF_MITIGATION_FULL_FORCE;
1564
1565 return 0;
1566}
1567early_param("l1tf", l1tf_cmdline);
1568
56563f53 1569#undef pr_fmt
39226ef0 1570#define pr_fmt(fmt) fmt
56563f53 1571
61dc0f55 1572#ifdef CONFIG_SYSFS
d1059518 1573
72c6d2db
TG
1574#define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
1575
1576#if IS_ENABLED(CONFIG_KVM_INTEL)
8770709f 1577static const char * const l1tf_vmx_states[] = {
a7b9020b
TG
1578 [VMENTER_L1D_FLUSH_AUTO] = "auto",
1579 [VMENTER_L1D_FLUSH_NEVER] = "vulnerable",
1580 [VMENTER_L1D_FLUSH_COND] = "conditional cache flushes",
1581 [VMENTER_L1D_FLUSH_ALWAYS] = "cache flushes",
1582 [VMENTER_L1D_FLUSH_EPT_DISABLED] = "EPT disabled",
8e0b2b91 1583 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = "flush not necessary"
72c6d2db
TG
1584};
1585
1586static ssize_t l1tf_show_state(char *buf)
1587{
1588 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO)
1589 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
1590
ea156d19
PB
1591 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_EPT_DISABLED ||
1592 (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER &&
130d6f94 1593 sched_smt_active())) {
ea156d19
PB
1594 return sprintf(buf, "%s; VMX: %s\n", L1TF_DEFAULT_MSG,
1595 l1tf_vmx_states[l1tf_vmx_mitigation]);
130d6f94 1596 }
ea156d19
PB
1597
1598 return sprintf(buf, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG,
1599 l1tf_vmx_states[l1tf_vmx_mitigation],
130d6f94 1600 sched_smt_active() ? "vulnerable" : "disabled");
72c6d2db 1601}
b8e8c830
PB
1602
1603static ssize_t itlb_multihit_show_state(char *buf)
1604{
f29dfa53
PG
1605 if (!boot_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
1606 !boot_cpu_has(X86_FEATURE_VMX))
1607 return sprintf(buf, "KVM: Mitigation: VMX unsupported\n");
1608 else if (!(cr4_read_shadow() & X86_CR4_VMXE))
1609 return sprintf(buf, "KVM: Mitigation: VMX disabled\n");
1610 else if (itlb_multihit_kvm_mitigation)
b8e8c830
PB
1611 return sprintf(buf, "KVM: Mitigation: Split huge pages\n");
1612 else
1613 return sprintf(buf, "KVM: Vulnerable\n");
1614}
72c6d2db
TG
1615#else
1616static ssize_t l1tf_show_state(char *buf)
1617{
1618 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
1619}
72c6d2db 1620
db4d30fb
VT
1621static ssize_t itlb_multihit_show_state(char *buf)
1622{
1623 return sprintf(buf, "Processor vulnerable\n");
1624}
b8e8c830 1625#endif
db4d30fb 1626
8a4b06d3
TG
1627static ssize_t mds_show_state(char *buf)
1628{
517c3ba0 1629 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
8a4b06d3
TG
1630 return sprintf(buf, "%s; SMT Host state unknown\n",
1631 mds_strings[mds_mitigation]);
1632 }
1633
1634 if (boot_cpu_has(X86_BUG_MSBDS_ONLY)) {
1635 return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
e2c3c947
KRW
1636 (mds_mitigation == MDS_MITIGATION_OFF ? "vulnerable" :
1637 sched_smt_active() ? "mitigated" : "disabled"));
8a4b06d3
TG
1638 }
1639
1640 return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
1641 sched_smt_active() ? "vulnerable" : "disabled");
1642}
1643
6608b45a
PG
1644static ssize_t tsx_async_abort_show_state(char *buf)
1645{
1646 if ((taa_mitigation == TAA_MITIGATION_TSX_DISABLED) ||
1647 (taa_mitigation == TAA_MITIGATION_OFF))
1648 return sprintf(buf, "%s\n", taa_strings[taa_mitigation]);
1649
1650 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
1651 return sprintf(buf, "%s; SMT Host state unknown\n",
1652 taa_strings[taa_mitigation]);
1653 }
1654
1655 return sprintf(buf, "%s; SMT %s\n", taa_strings[taa_mitigation],
1656 sched_smt_active() ? "vulnerable" : "disabled");
1657}
1658
a8f76ae4
TC
1659static char *stibp_state(void)
1660{
34bce7c9
TC
1661 if (spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
1662 return "";
1663
21998a35 1664 switch (spectre_v2_user_stibp) {
fa1202ef
TG
1665 case SPECTRE_V2_USER_NONE:
1666 return ", STIBP: disabled";
1667 case SPECTRE_V2_USER_STRICT:
1668 return ", STIBP: forced";
20c3a2c3
TL
1669 case SPECTRE_V2_USER_STRICT_PREFERRED:
1670 return ", STIBP: always-on";
9137bb27 1671 case SPECTRE_V2_USER_PRCTL:
6b3e64c2 1672 case SPECTRE_V2_USER_SECCOMP:
7cc765a6
TG
1673 if (static_key_enabled(&switch_to_cond_stibp))
1674 return ", STIBP: conditional";
fa1202ef
TG
1675 }
1676 return "";
a8f76ae4
TC
1677}
1678
1679static char *ibpb_state(void)
1680{
4c71a2b6 1681 if (boot_cpu_has(X86_FEATURE_IBPB)) {
7cc765a6 1682 if (static_key_enabled(&switch_mm_always_ibpb))
4c71a2b6 1683 return ", IBPB: always-on";
7cc765a6
TG
1684 if (static_key_enabled(&switch_mm_cond_ibpb))
1685 return ", IBPB: conditional";
1686 return ", IBPB: disabled";
4c71a2b6
TG
1687 }
1688 return "";
a8f76ae4
TC
1689}
1690
7e5b3c26
MG
1691static ssize_t srbds_show_state(char *buf)
1692{
1693 return sprintf(buf, "%s\n", srbds_strings[srbds_mitigation]);
1694}
1695
7bb4d366 1696static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
ffed645e 1697 char *buf, unsigned int bug)
61dc0f55 1698{
d1059518 1699 if (!boot_cpu_has_bug(bug))
61dc0f55 1700 return sprintf(buf, "Not affected\n");
d1059518
KRW
1701
1702 switch (bug) {
1703 case X86_BUG_CPU_MELTDOWN:
1704 if (boot_cpu_has(X86_FEATURE_PTI))
1705 return sprintf(buf, "Mitigation: PTI\n");
1706
6cb2b08f
JK
1707 if (hypervisor_is_type(X86_HYPER_XEN_PV))
1708 return sprintf(buf, "Unknown (XEN PV detected, hypervisor mitigation required)\n");
1709
d1059518
KRW
1710 break;
1711
1712 case X86_BUG_SPECTRE_V1:
a2059825 1713 return sprintf(buf, "%s\n", spectre_v1_strings[spectre_v1_mitigation]);
d1059518
KRW
1714
1715 case X86_BUG_SPECTRE_V2:
b86bda04 1716 return sprintf(buf, "%s%s%s%s%s%s\n", spectre_v2_strings[spectre_v2_enabled],
a8f76ae4 1717 ibpb_state(),
d1059518 1718 boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
a8f76ae4 1719 stibp_state(),
bb4b3b77 1720 boot_cpu_has(X86_FEATURE_RSB_CTXSW) ? ", RSB filling" : "",
d1059518
KRW
1721 spectre_v2_module_string());
1722
24f7fc83
KRW
1723 case X86_BUG_SPEC_STORE_BYPASS:
1724 return sprintf(buf, "%s\n", ssb_strings[ssb_mode]);
1725
17dbca11
AK
1726 case X86_BUG_L1TF:
1727 if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV))
72c6d2db 1728 return l1tf_show_state(buf);
17dbca11 1729 break;
8a4b06d3
TG
1730
1731 case X86_BUG_MDS:
1732 return mds_show_state(buf);
1733
6608b45a
PG
1734 case X86_BUG_TAA:
1735 return tsx_async_abort_show_state(buf);
1736
db4d30fb
VT
1737 case X86_BUG_ITLB_MULTIHIT:
1738 return itlb_multihit_show_state(buf);
1739
7e5b3c26
MG
1740 case X86_BUG_SRBDS:
1741 return srbds_show_state(buf);
1742
d1059518
KRW
1743 default:
1744 break;
1745 }
1746
61dc0f55
TG
1747 return sprintf(buf, "Vulnerable\n");
1748}
1749
d1059518
KRW
1750ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
1751{
1752 return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
1753}
1754
21e433bd 1755ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
61dc0f55 1756{
d1059518 1757 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
61dc0f55
TG
1758}
1759
21e433bd 1760ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
61dc0f55 1761{
d1059518 1762 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
61dc0f55 1763}
c456442c
KRW
1764
1765ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
1766{
1767 return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS);
1768}
17dbca11
AK
1769
1770ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
1771{
1772 return cpu_show_common(dev, attr, buf, X86_BUG_L1TF);
1773}
8a4b06d3
TG
1774
1775ssize_t cpu_show_mds(struct device *dev, struct device_attribute *attr, char *buf)
1776{
1777 return cpu_show_common(dev, attr, buf, X86_BUG_MDS);
1778}
6608b45a
PG
1779
1780ssize_t cpu_show_tsx_async_abort(struct device *dev, struct device_attribute *attr, char *buf)
1781{
1782 return cpu_show_common(dev, attr, buf, X86_BUG_TAA);
1783}
db4d30fb
VT
1784
1785ssize_t cpu_show_itlb_multihit(struct device *dev, struct device_attribute *attr, char *buf)
1786{
1787 return cpu_show_common(dev, attr, buf, X86_BUG_ITLB_MULTIHIT);
1788}
7e5b3c26
MG
1789
1790ssize_t cpu_show_srbds(struct device *dev, struct device_attribute *attr, char *buf)
1791{
1792 return cpu_show_common(dev, attr, buf, X86_BUG_SRBDS);
1793}
61dc0f55 1794#endif