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