]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/x86/kernel/cpu/bugs.c
x86/speculation: Split out TIF update
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / kernel / cpu / bugs.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 1994 Linus Torvalds
4 *
5 * Cyrix stuff, June 1998 by:
6 * - Rafael R. Reilova (moved everything from head.S),
7 * <rreilova@ececs.uc.edu>
8 * - Channing Corn (tests & fixes),
9 * - Andrew D. Balsa (code cleanup).
10 */
11 #include <linux/init.h>
12 #include <linux/utsname.h>
13 #include <linux/cpu.h>
14 #include <linux/module.h>
15 #include <linux/nospec.h>
16 #include <linux/prctl.h>
17 #include <linux/sched/smt.h>
18
19 #include <asm/spec-ctrl.h>
20 #include <asm/cmdline.h>
21 #include <asm/bugs.h>
22 #include <asm/processor.h>
23 #include <asm/processor-flags.h>
24 #include <asm/fpu/internal.h>
25 #include <asm/msr.h>
26 #include <asm/vmx.h>
27 #include <asm/paravirt.h>
28 #include <asm/alternative.h>
29 #include <asm/pgtable.h>
30 #include <asm/set_memory.h>
31 #include <asm/intel-family.h>
32 #include <asm/e820/api.h>
33
34 static void __init spectre_v2_select_mitigation(void);
35 static void __init ssb_select_mitigation(void);
36 static void __init l1tf_select_mitigation(void);
37
38 /* The base value of the SPEC_CTRL MSR that always has to be preserved. */
39 u64 x86_spec_ctrl_base;
40 EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
41 static DEFINE_MUTEX(spec_ctrl_mutex);
42
43 /*
44 * The vendor and possibly platform specific bits which can be modified in
45 * x86_spec_ctrl_base.
46 */
47 static u64 __ro_after_init x86_spec_ctrl_mask = SPEC_CTRL_IBRS;
48
49 /*
50 * AMD specific MSR info for Speculative Store Bypass control.
51 * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu().
52 */
53 u64 __ro_after_init x86_amd_ls_cfg_base;
54 u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask;
55
56 /* Control conditional STIPB in switch_to() */
57 DEFINE_STATIC_KEY_FALSE(switch_to_cond_stibp);
58 /* Control conditional IBPB in switch_mm() */
59 DEFINE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
60 /* Control unconditional IBPB in switch_mm() */
61 DEFINE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
62
63 void __init check_bugs(void)
64 {
65 identify_boot_cpu();
66
67 /*
68 * identify_boot_cpu() initialized SMT support information, let the
69 * core code know.
70 */
71 cpu_smt_check_topology_early();
72
73 if (!IS_ENABLED(CONFIG_SMP)) {
74 pr_info("CPU: ");
75 print_cpu_info(&boot_cpu_data);
76 }
77
78 /*
79 * Read the SPEC_CTRL MSR to account for reserved bits which may
80 * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
81 * init code as it is not enumerated and depends on the family.
82 */
83 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
84 rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
85
86 /* Allow STIBP in MSR_SPEC_CTRL if supported */
87 if (boot_cpu_has(X86_FEATURE_STIBP))
88 x86_spec_ctrl_mask |= SPEC_CTRL_STIBP;
89
90 /* Select the proper spectre mitigation before patching alternatives */
91 spectre_v2_select_mitigation();
92
93 /*
94 * Select proper mitigation for any exposure to the Speculative Store
95 * Bypass vulnerability.
96 */
97 ssb_select_mitigation();
98
99 l1tf_select_mitigation();
100
101 #ifdef CONFIG_X86_32
102 /*
103 * Check whether we are able to run this kernel safely on SMP.
104 *
105 * - i386 is no longer supported.
106 * - In order to run on anything without a TSC, we need to be
107 * compiled for a i486.
108 */
109 if (boot_cpu_data.x86 < 4)
110 panic("Kernel requires i486+ for 'invlpg' and other features");
111
112 init_utsname()->machine[1] =
113 '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
114 alternative_instructions();
115
116 fpu__init_check_bugs();
117 #else /* CONFIG_X86_64 */
118 alternative_instructions();
119
120 /*
121 * Make sure the first 2MB area is not mapped by huge pages
122 * There are typically fixed size MTRRs in there and overlapping
123 * MTRRs into large pages causes slow downs.
124 *
125 * Right now we don't do that with gbpages because there seems
126 * very little benefit for that case.
127 */
128 if (!direct_gbpages)
129 set_memory_4k((unsigned long)__va(0), 1);
130 #endif
131 }
132
133 void
134 x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest)
135 {
136 u64 msrval, guestval, hostval = x86_spec_ctrl_base;
137 struct thread_info *ti = current_thread_info();
138
139 /* Is MSR_SPEC_CTRL implemented ? */
140 if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) {
141 /*
142 * Restrict guest_spec_ctrl to supported values. Clear the
143 * modifiable bits in the host base value and or the
144 * modifiable bits from the guest value.
145 */
146 guestval = hostval & ~x86_spec_ctrl_mask;
147 guestval |= guest_spec_ctrl & x86_spec_ctrl_mask;
148
149 /* SSBD controlled in MSR_SPEC_CTRL */
150 if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
151 static_cpu_has(X86_FEATURE_AMD_SSBD))
152 hostval |= ssbd_tif_to_spec_ctrl(ti->flags);
153
154 /* Conditional STIBP enabled? */
155 if (static_branch_unlikely(&switch_to_cond_stibp))
156 hostval |= stibp_tif_to_spec_ctrl(ti->flags);
157
158 if (hostval != guestval) {
159 msrval = setguest ? guestval : hostval;
160 wrmsrl(MSR_IA32_SPEC_CTRL, msrval);
161 }
162 }
163
164 /*
165 * If SSBD is not handled in MSR_SPEC_CTRL on AMD, update
166 * MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported.
167 */
168 if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
169 !static_cpu_has(X86_FEATURE_VIRT_SSBD))
170 return;
171
172 /*
173 * If the host has SSBD mitigation enabled, force it in the host's
174 * virtual MSR value. If its not permanently enabled, evaluate
175 * current's TIF_SSBD thread flag.
176 */
177 if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE))
178 hostval = SPEC_CTRL_SSBD;
179 else
180 hostval = ssbd_tif_to_spec_ctrl(ti->flags);
181
182 /* Sanitize the guest value */
183 guestval = guest_virt_spec_ctrl & SPEC_CTRL_SSBD;
184
185 if (hostval != guestval) {
186 unsigned long tif;
187
188 tif = setguest ? ssbd_spec_ctrl_to_tif(guestval) :
189 ssbd_spec_ctrl_to_tif(hostval);
190
191 speculation_ctrl_update(tif);
192 }
193 }
194 EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl);
195
196 static void x86_amd_ssb_disable(void)
197 {
198 u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_ssbd_mask;
199
200 if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
201 wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, SPEC_CTRL_SSBD);
202 else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
203 wrmsrl(MSR_AMD64_LS_CFG, msrval);
204 }
205
206 #undef pr_fmt
207 #define pr_fmt(fmt) "Spectre V2 : " fmt
208
209 static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init =
210 SPECTRE_V2_NONE;
211
212 static enum spectre_v2_user_mitigation spectre_v2_user __ro_after_init =
213 SPECTRE_V2_USER_NONE;
214
215 #ifdef RETPOLINE
216 static bool spectre_v2_bad_module;
217
218 bool retpoline_module_ok(bool has_retpoline)
219 {
220 if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
221 return true;
222
223 pr_err("System may be vulnerable to spectre v2\n");
224 spectre_v2_bad_module = true;
225 return false;
226 }
227
228 static inline const char *spectre_v2_module_string(void)
229 {
230 return spectre_v2_bad_module ? " - vulnerable module loaded" : "";
231 }
232 #else
233 static inline const char *spectre_v2_module_string(void) { return ""; }
234 #endif
235
236 static inline bool match_option(const char *arg, int arglen, const char *opt)
237 {
238 int len = strlen(opt);
239
240 return len == arglen && !strncmp(arg, opt, len);
241 }
242
243 /* The kernel command line selection for spectre v2 */
244 enum spectre_v2_mitigation_cmd {
245 SPECTRE_V2_CMD_NONE,
246 SPECTRE_V2_CMD_AUTO,
247 SPECTRE_V2_CMD_FORCE,
248 SPECTRE_V2_CMD_RETPOLINE,
249 SPECTRE_V2_CMD_RETPOLINE_GENERIC,
250 SPECTRE_V2_CMD_RETPOLINE_AMD,
251 };
252
253 enum spectre_v2_user_cmd {
254 SPECTRE_V2_USER_CMD_NONE,
255 SPECTRE_V2_USER_CMD_AUTO,
256 SPECTRE_V2_USER_CMD_FORCE,
257 };
258
259 static const char * const spectre_v2_user_strings[] = {
260 [SPECTRE_V2_USER_NONE] = "User space: Vulnerable",
261 [SPECTRE_V2_USER_STRICT] = "User space: Mitigation: STIBP protection",
262 };
263
264 static const struct {
265 const char *option;
266 enum spectre_v2_user_cmd cmd;
267 bool secure;
268 } v2_user_options[] __initdata = {
269 { "auto", SPECTRE_V2_USER_CMD_AUTO, false },
270 { "off", SPECTRE_V2_USER_CMD_NONE, false },
271 { "on", SPECTRE_V2_USER_CMD_FORCE, true },
272 };
273
274 static void __init spec_v2_user_print_cond(const char *reason, bool secure)
275 {
276 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
277 pr_info("spectre_v2_user=%s forced on command line.\n", reason);
278 }
279
280 static enum spectre_v2_user_cmd __init
281 spectre_v2_parse_user_cmdline(enum spectre_v2_mitigation_cmd v2_cmd)
282 {
283 char arg[20];
284 int ret, i;
285
286 switch (v2_cmd) {
287 case SPECTRE_V2_CMD_NONE:
288 return SPECTRE_V2_USER_CMD_NONE;
289 case SPECTRE_V2_CMD_FORCE:
290 return SPECTRE_V2_USER_CMD_FORCE;
291 default:
292 break;
293 }
294
295 ret = cmdline_find_option(boot_command_line, "spectre_v2_user",
296 arg, sizeof(arg));
297 if (ret < 0)
298 return SPECTRE_V2_USER_CMD_AUTO;
299
300 for (i = 0; i < ARRAY_SIZE(v2_user_options); i++) {
301 if (match_option(arg, ret, v2_user_options[i].option)) {
302 spec_v2_user_print_cond(v2_user_options[i].option,
303 v2_user_options[i].secure);
304 return v2_user_options[i].cmd;
305 }
306 }
307
308 pr_err("Unknown user space protection option (%s). Switching to AUTO select\n", arg);
309 return SPECTRE_V2_USER_CMD_AUTO;
310 }
311
312 static void __init
313 spectre_v2_user_select_mitigation(enum spectre_v2_mitigation_cmd v2_cmd)
314 {
315 enum spectre_v2_user_mitigation mode = SPECTRE_V2_USER_NONE;
316 bool smt_possible = IS_ENABLED(CONFIG_SMP);
317
318 if (!boot_cpu_has(X86_FEATURE_IBPB) && !boot_cpu_has(X86_FEATURE_STIBP))
319 return;
320
321 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
322 cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
323 smt_possible = false;
324
325 switch (spectre_v2_parse_user_cmdline(v2_cmd)) {
326 case SPECTRE_V2_USER_CMD_AUTO:
327 case SPECTRE_V2_USER_CMD_NONE:
328 goto set_mode;
329 case SPECTRE_V2_USER_CMD_FORCE:
330 mode = SPECTRE_V2_USER_STRICT;
331 break;
332 }
333
334 /* Initialize Indirect Branch Prediction Barrier */
335 if (boot_cpu_has(X86_FEATURE_IBPB)) {
336 setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
337
338 switch (mode) {
339 case SPECTRE_V2_USER_STRICT:
340 static_branch_enable(&switch_mm_always_ibpb);
341 break;
342 default:
343 break;
344 }
345
346 pr_info("mitigation: Enabling %s Indirect Branch Prediction Barrier\n",
347 mode == SPECTRE_V2_USER_STRICT ? "always-on" : "conditional");
348 }
349
350 /* If enhanced IBRS is enabled no STIPB required */
351 if (spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
352 return;
353
354 set_mode:
355 spectre_v2_user = mode;
356 /* Only print the STIBP mode when SMT possible */
357 if (smt_possible)
358 pr_info("%s\n", spectre_v2_user_strings[mode]);
359 }
360
361 static const char * const spectre_v2_strings[] = {
362 [SPECTRE_V2_NONE] = "Vulnerable",
363 [SPECTRE_V2_RETPOLINE_GENERIC] = "Mitigation: Full generic retpoline",
364 [SPECTRE_V2_RETPOLINE_AMD] = "Mitigation: Full AMD retpoline",
365 [SPECTRE_V2_IBRS_ENHANCED] = "Mitigation: Enhanced IBRS",
366 };
367
368 static const struct {
369 const char *option;
370 enum spectre_v2_mitigation_cmd cmd;
371 bool secure;
372 } mitigation_options[] __initdata = {
373 { "off", SPECTRE_V2_CMD_NONE, false },
374 { "on", SPECTRE_V2_CMD_FORCE, true },
375 { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false },
376 { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_AMD, false },
377 { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
378 { "auto", SPECTRE_V2_CMD_AUTO, false },
379 };
380
381 static void __init spec_v2_print_cond(const char *reason, bool secure)
382 {
383 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
384 pr_info("%s selected on command line.\n", reason);
385 }
386
387 static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
388 {
389 enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
390 char arg[20];
391 int ret, i;
392
393 if (cmdline_find_option_bool(boot_command_line, "nospectre_v2"))
394 return SPECTRE_V2_CMD_NONE;
395
396 ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
397 if (ret < 0)
398 return SPECTRE_V2_CMD_AUTO;
399
400 for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
401 if (!match_option(arg, ret, mitigation_options[i].option))
402 continue;
403 cmd = mitigation_options[i].cmd;
404 break;
405 }
406
407 if (i >= ARRAY_SIZE(mitigation_options)) {
408 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
409 return SPECTRE_V2_CMD_AUTO;
410 }
411
412 if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
413 cmd == SPECTRE_V2_CMD_RETPOLINE_AMD ||
414 cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC) &&
415 !IS_ENABLED(CONFIG_RETPOLINE)) {
416 pr_err("%s selected but not compiled in. Switching to AUTO select\n", mitigation_options[i].option);
417 return SPECTRE_V2_CMD_AUTO;
418 }
419
420 if (cmd == SPECTRE_V2_CMD_RETPOLINE_AMD &&
421 boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
422 pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n");
423 return SPECTRE_V2_CMD_AUTO;
424 }
425
426 spec_v2_print_cond(mitigation_options[i].option,
427 mitigation_options[i].secure);
428 return cmd;
429 }
430
431 static void __init spectre_v2_select_mitigation(void)
432 {
433 enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
434 enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
435
436 /*
437 * If the CPU is not affected and the command line mode is NONE or AUTO
438 * then nothing to do.
439 */
440 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
441 (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
442 return;
443
444 switch (cmd) {
445 case SPECTRE_V2_CMD_NONE:
446 return;
447
448 case SPECTRE_V2_CMD_FORCE:
449 case SPECTRE_V2_CMD_AUTO:
450 if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
451 mode = SPECTRE_V2_IBRS_ENHANCED;
452 /* Force it so VMEXIT will restore correctly */
453 x86_spec_ctrl_base |= SPEC_CTRL_IBRS;
454 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
455 goto specv2_set_mode;
456 }
457 if (IS_ENABLED(CONFIG_RETPOLINE))
458 goto retpoline_auto;
459 break;
460 case SPECTRE_V2_CMD_RETPOLINE_AMD:
461 if (IS_ENABLED(CONFIG_RETPOLINE))
462 goto retpoline_amd;
463 break;
464 case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
465 if (IS_ENABLED(CONFIG_RETPOLINE))
466 goto retpoline_generic;
467 break;
468 case SPECTRE_V2_CMD_RETPOLINE:
469 if (IS_ENABLED(CONFIG_RETPOLINE))
470 goto retpoline_auto;
471 break;
472 }
473 pr_err("Spectre mitigation: kernel not compiled with retpoline; no mitigation available!");
474 return;
475
476 retpoline_auto:
477 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
478 retpoline_amd:
479 if (!boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
480 pr_err("Spectre mitigation: LFENCE not serializing, switching to generic retpoline\n");
481 goto retpoline_generic;
482 }
483 mode = SPECTRE_V2_RETPOLINE_AMD;
484 setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD);
485 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
486 } else {
487 retpoline_generic:
488 mode = SPECTRE_V2_RETPOLINE_GENERIC;
489 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
490 }
491
492 specv2_set_mode:
493 spectre_v2_enabled = mode;
494 pr_info("%s\n", spectre_v2_strings[mode]);
495
496 /*
497 * If spectre v2 protection has been enabled, unconditionally fill
498 * RSB during a context switch; this protects against two independent
499 * issues:
500 *
501 * - RSB underflow (and switch to BTB) on Skylake+
502 * - SpectreRSB variant of spectre v2 on X86_BUG_SPECTRE_V2 CPUs
503 */
504 setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
505 pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n");
506
507 /*
508 * Retpoline means the kernel is safe because it has no indirect
509 * branches. Enhanced IBRS protects firmware too, so, enable restricted
510 * speculation around firmware calls only when Enhanced IBRS isn't
511 * supported.
512 *
513 * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because
514 * the user might select retpoline on the kernel command line and if
515 * the CPU supports Enhanced IBRS, kernel might un-intentionally not
516 * enable IBRS around firmware calls.
517 */
518 if (boot_cpu_has(X86_FEATURE_IBRS) && mode != SPECTRE_V2_IBRS_ENHANCED) {
519 setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
520 pr_info("Enabling Restricted Speculation for firmware calls\n");
521 }
522
523 /* Set up IBPB and STIBP depending on the general spectre V2 command */
524 spectre_v2_user_select_mitigation(cmd);
525
526 /* Enable STIBP if appropriate */
527 arch_smt_update();
528 }
529
530 static bool stibp_needed(void)
531 {
532 /* Enhanced IBRS makes using STIBP unnecessary. */
533 if (spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
534 return false;
535
536 /* Check for strict user mitigation mode */
537 return spectre_v2_user == SPECTRE_V2_USER_STRICT;
538 }
539
540 static void update_stibp_msr(void *info)
541 {
542 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
543 }
544
545 void arch_smt_update(void)
546 {
547 u64 mask;
548
549 if (!stibp_needed())
550 return;
551
552 mutex_lock(&spec_ctrl_mutex);
553
554 mask = x86_spec_ctrl_base & ~SPEC_CTRL_STIBP;
555 if (sched_smt_active())
556 mask |= SPEC_CTRL_STIBP;
557
558 if (mask != x86_spec_ctrl_base) {
559 pr_info("Spectre v2 cross-process SMT mitigation: %s STIBP\n",
560 mask & SPEC_CTRL_STIBP ? "Enabling" : "Disabling");
561 x86_spec_ctrl_base = mask;
562 on_each_cpu(update_stibp_msr, NULL, 1);
563 }
564 mutex_unlock(&spec_ctrl_mutex);
565 }
566
567 #undef pr_fmt
568 #define pr_fmt(fmt) "Speculative Store Bypass: " fmt
569
570 static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
571
572 /* The kernel command line selection */
573 enum ssb_mitigation_cmd {
574 SPEC_STORE_BYPASS_CMD_NONE,
575 SPEC_STORE_BYPASS_CMD_AUTO,
576 SPEC_STORE_BYPASS_CMD_ON,
577 SPEC_STORE_BYPASS_CMD_PRCTL,
578 SPEC_STORE_BYPASS_CMD_SECCOMP,
579 };
580
581 static const char * const ssb_strings[] = {
582 [SPEC_STORE_BYPASS_NONE] = "Vulnerable",
583 [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass disabled",
584 [SPEC_STORE_BYPASS_PRCTL] = "Mitigation: Speculative Store Bypass disabled via prctl",
585 [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
586 };
587
588 static const struct {
589 const char *option;
590 enum ssb_mitigation_cmd cmd;
591 } ssb_mitigation_options[] __initdata = {
592 { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */
593 { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */
594 { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */
595 { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative Store Bypass via prctl */
596 { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */
597 };
598
599 static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
600 {
601 enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO;
602 char arg[20];
603 int ret, i;
604
605 if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable")) {
606 return SPEC_STORE_BYPASS_CMD_NONE;
607 } else {
608 ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable",
609 arg, sizeof(arg));
610 if (ret < 0)
611 return SPEC_STORE_BYPASS_CMD_AUTO;
612
613 for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) {
614 if (!match_option(arg, ret, ssb_mitigation_options[i].option))
615 continue;
616
617 cmd = ssb_mitigation_options[i].cmd;
618 break;
619 }
620
621 if (i >= ARRAY_SIZE(ssb_mitigation_options)) {
622 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
623 return SPEC_STORE_BYPASS_CMD_AUTO;
624 }
625 }
626
627 return cmd;
628 }
629
630 static enum ssb_mitigation __init __ssb_select_mitigation(void)
631 {
632 enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
633 enum ssb_mitigation_cmd cmd;
634
635 if (!boot_cpu_has(X86_FEATURE_SSBD))
636 return mode;
637
638 cmd = ssb_parse_cmdline();
639 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) &&
640 (cmd == SPEC_STORE_BYPASS_CMD_NONE ||
641 cmd == SPEC_STORE_BYPASS_CMD_AUTO))
642 return mode;
643
644 switch (cmd) {
645 case SPEC_STORE_BYPASS_CMD_AUTO:
646 case SPEC_STORE_BYPASS_CMD_SECCOMP:
647 /*
648 * Choose prctl+seccomp as the default mode if seccomp is
649 * enabled.
650 */
651 if (IS_ENABLED(CONFIG_SECCOMP))
652 mode = SPEC_STORE_BYPASS_SECCOMP;
653 else
654 mode = SPEC_STORE_BYPASS_PRCTL;
655 break;
656 case SPEC_STORE_BYPASS_CMD_ON:
657 mode = SPEC_STORE_BYPASS_DISABLE;
658 break;
659 case SPEC_STORE_BYPASS_CMD_PRCTL:
660 mode = SPEC_STORE_BYPASS_PRCTL;
661 break;
662 case SPEC_STORE_BYPASS_CMD_NONE:
663 break;
664 }
665
666 /*
667 * We have three CPU feature flags that are in play here:
668 * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
669 * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
670 * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
671 */
672 if (mode == SPEC_STORE_BYPASS_DISABLE) {
673 setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
674 /*
675 * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
676 * use a completely different MSR and bit dependent on family.
677 */
678 if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
679 !static_cpu_has(X86_FEATURE_AMD_SSBD)) {
680 x86_amd_ssb_disable();
681 } else {
682 x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
683 x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
684 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
685 }
686 }
687
688 return mode;
689 }
690
691 static void ssb_select_mitigation(void)
692 {
693 ssb_mode = __ssb_select_mitigation();
694
695 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
696 pr_info("%s\n", ssb_strings[ssb_mode]);
697 }
698
699 #undef pr_fmt
700 #define pr_fmt(fmt) "Speculation prctl: " fmt
701
702 static void task_update_spec_tif(struct task_struct *tsk, int tifbit, bool on)
703 {
704 bool update;
705
706 if (on)
707 update = !test_and_set_tsk_thread_flag(tsk, tifbit);
708 else
709 update = test_and_clear_tsk_thread_flag(tsk, tifbit);
710
711 /*
712 * Immediately update the speculation control MSRs for the current
713 * task, but for a non-current task delay setting the CPU
714 * mitigation until it is scheduled next.
715 *
716 * This can only happen for SECCOMP mitigation. For PRCTL it's
717 * always the current task.
718 */
719 if (tsk == current && update)
720 speculation_ctrl_update_current();
721 }
722
723 static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
724 {
725 if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
726 ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
727 return -ENXIO;
728
729 switch (ctrl) {
730 case PR_SPEC_ENABLE:
731 /* If speculation is force disabled, enable is not allowed */
732 if (task_spec_ssb_force_disable(task))
733 return -EPERM;
734 task_clear_spec_ssb_disable(task);
735 task_update_spec_tif(task, TIF_SSBD, false);
736 break;
737 case PR_SPEC_DISABLE:
738 task_set_spec_ssb_disable(task);
739 task_update_spec_tif(task, TIF_SSBD, true);
740 break;
741 case PR_SPEC_FORCE_DISABLE:
742 task_set_spec_ssb_disable(task);
743 task_set_spec_ssb_force_disable(task);
744 task_update_spec_tif(task, TIF_SSBD, true);
745 break;
746 default:
747 return -ERANGE;
748 }
749 return 0;
750 }
751
752 int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
753 unsigned long ctrl)
754 {
755 switch (which) {
756 case PR_SPEC_STORE_BYPASS:
757 return ssb_prctl_set(task, ctrl);
758 default:
759 return -ENODEV;
760 }
761 }
762
763 #ifdef CONFIG_SECCOMP
764 void arch_seccomp_spec_mitigate(struct task_struct *task)
765 {
766 if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP)
767 ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
768 }
769 #endif
770
771 static int ssb_prctl_get(struct task_struct *task)
772 {
773 switch (ssb_mode) {
774 case SPEC_STORE_BYPASS_DISABLE:
775 return PR_SPEC_DISABLE;
776 case SPEC_STORE_BYPASS_SECCOMP:
777 case SPEC_STORE_BYPASS_PRCTL:
778 if (task_spec_ssb_force_disable(task))
779 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
780 if (task_spec_ssb_disable(task))
781 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
782 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
783 default:
784 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
785 return PR_SPEC_ENABLE;
786 return PR_SPEC_NOT_AFFECTED;
787 }
788 }
789
790 int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
791 {
792 switch (which) {
793 case PR_SPEC_STORE_BYPASS:
794 return ssb_prctl_get(task);
795 default:
796 return -ENODEV;
797 }
798 }
799
800 void x86_spec_ctrl_setup_ap(void)
801 {
802 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
803 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
804
805 if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
806 x86_amd_ssb_disable();
807 }
808
809 #undef pr_fmt
810 #define pr_fmt(fmt) "L1TF: " fmt
811
812 /* Default mitigation for L1TF-affected CPUs */
813 enum l1tf_mitigations l1tf_mitigation __ro_after_init = L1TF_MITIGATION_FLUSH;
814 #if IS_ENABLED(CONFIG_KVM_INTEL)
815 EXPORT_SYMBOL_GPL(l1tf_mitigation);
816
817 enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
818 EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation);
819 #endif
820
821 /*
822 * These CPUs all support 44bits physical address space internally in the
823 * cache but CPUID can report a smaller number of physical address bits.
824 *
825 * The L1TF mitigation uses the top most address bit for the inversion of
826 * non present PTEs. When the installed memory reaches into the top most
827 * address bit due to memory holes, which has been observed on machines
828 * which report 36bits physical address bits and have 32G RAM installed,
829 * then the mitigation range check in l1tf_select_mitigation() triggers.
830 * This is a false positive because the mitigation is still possible due to
831 * the fact that the cache uses 44bit internally. Use the cache bits
832 * instead of the reported physical bits and adjust them on the affected
833 * machines to 44bit if the reported bits are less than 44.
834 */
835 static void override_cache_bits(struct cpuinfo_x86 *c)
836 {
837 if (c->x86 != 6)
838 return;
839
840 switch (c->x86_model) {
841 case INTEL_FAM6_NEHALEM:
842 case INTEL_FAM6_WESTMERE:
843 case INTEL_FAM6_SANDYBRIDGE:
844 case INTEL_FAM6_IVYBRIDGE:
845 case INTEL_FAM6_HASWELL_CORE:
846 case INTEL_FAM6_HASWELL_ULT:
847 case INTEL_FAM6_HASWELL_GT3E:
848 case INTEL_FAM6_BROADWELL_CORE:
849 case INTEL_FAM6_BROADWELL_GT3E:
850 case INTEL_FAM6_SKYLAKE_MOBILE:
851 case INTEL_FAM6_SKYLAKE_DESKTOP:
852 case INTEL_FAM6_KABYLAKE_MOBILE:
853 case INTEL_FAM6_KABYLAKE_DESKTOP:
854 if (c->x86_cache_bits < 44)
855 c->x86_cache_bits = 44;
856 break;
857 }
858 }
859
860 static void __init l1tf_select_mitigation(void)
861 {
862 u64 half_pa;
863
864 if (!boot_cpu_has_bug(X86_BUG_L1TF))
865 return;
866
867 override_cache_bits(&boot_cpu_data);
868
869 switch (l1tf_mitigation) {
870 case L1TF_MITIGATION_OFF:
871 case L1TF_MITIGATION_FLUSH_NOWARN:
872 case L1TF_MITIGATION_FLUSH:
873 break;
874 case L1TF_MITIGATION_FLUSH_NOSMT:
875 case L1TF_MITIGATION_FULL:
876 cpu_smt_disable(false);
877 break;
878 case L1TF_MITIGATION_FULL_FORCE:
879 cpu_smt_disable(true);
880 break;
881 }
882
883 #if CONFIG_PGTABLE_LEVELS == 2
884 pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
885 return;
886 #endif
887
888 half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT;
889 if (e820__mapped_any(half_pa, ULLONG_MAX - half_pa, E820_TYPE_RAM)) {
890 pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
891 return;
892 }
893
894 setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV);
895 }
896
897 static int __init l1tf_cmdline(char *str)
898 {
899 if (!boot_cpu_has_bug(X86_BUG_L1TF))
900 return 0;
901
902 if (!str)
903 return -EINVAL;
904
905 if (!strcmp(str, "off"))
906 l1tf_mitigation = L1TF_MITIGATION_OFF;
907 else if (!strcmp(str, "flush,nowarn"))
908 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOWARN;
909 else if (!strcmp(str, "flush"))
910 l1tf_mitigation = L1TF_MITIGATION_FLUSH;
911 else if (!strcmp(str, "flush,nosmt"))
912 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
913 else if (!strcmp(str, "full"))
914 l1tf_mitigation = L1TF_MITIGATION_FULL;
915 else if (!strcmp(str, "full,force"))
916 l1tf_mitigation = L1TF_MITIGATION_FULL_FORCE;
917
918 return 0;
919 }
920 early_param("l1tf", l1tf_cmdline);
921
922 #undef pr_fmt
923
924 #ifdef CONFIG_SYSFS
925
926 #define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
927
928 #if IS_ENABLED(CONFIG_KVM_INTEL)
929 static const char * const l1tf_vmx_states[] = {
930 [VMENTER_L1D_FLUSH_AUTO] = "auto",
931 [VMENTER_L1D_FLUSH_NEVER] = "vulnerable",
932 [VMENTER_L1D_FLUSH_COND] = "conditional cache flushes",
933 [VMENTER_L1D_FLUSH_ALWAYS] = "cache flushes",
934 [VMENTER_L1D_FLUSH_EPT_DISABLED] = "EPT disabled",
935 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = "flush not necessary"
936 };
937
938 static ssize_t l1tf_show_state(char *buf)
939 {
940 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO)
941 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
942
943 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_EPT_DISABLED ||
944 (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER &&
945 sched_smt_active())) {
946 return sprintf(buf, "%s; VMX: %s\n", L1TF_DEFAULT_MSG,
947 l1tf_vmx_states[l1tf_vmx_mitigation]);
948 }
949
950 return sprintf(buf, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG,
951 l1tf_vmx_states[l1tf_vmx_mitigation],
952 sched_smt_active() ? "vulnerable" : "disabled");
953 }
954 #else
955 static ssize_t l1tf_show_state(char *buf)
956 {
957 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
958 }
959 #endif
960
961 static char *stibp_state(void)
962 {
963 if (spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
964 return "";
965
966 switch (spectre_v2_user) {
967 case SPECTRE_V2_USER_NONE:
968 return ", STIBP: disabled";
969 case SPECTRE_V2_USER_STRICT:
970 return ", STIBP: forced";
971 }
972 return "";
973 }
974
975 static char *ibpb_state(void)
976 {
977 if (boot_cpu_has(X86_FEATURE_IBPB)) {
978 switch (spectre_v2_user) {
979 case SPECTRE_V2_USER_NONE:
980 return ", IBPB: disabled";
981 case SPECTRE_V2_USER_STRICT:
982 return ", IBPB: always-on";
983 }
984 }
985 return "";
986 }
987
988 static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
989 char *buf, unsigned int bug)
990 {
991 if (!boot_cpu_has_bug(bug))
992 return sprintf(buf, "Not affected\n");
993
994 switch (bug) {
995 case X86_BUG_CPU_MELTDOWN:
996 if (boot_cpu_has(X86_FEATURE_PTI))
997 return sprintf(buf, "Mitigation: PTI\n");
998
999 break;
1000
1001 case X86_BUG_SPECTRE_V1:
1002 return sprintf(buf, "Mitigation: __user pointer sanitization\n");
1003
1004 case X86_BUG_SPECTRE_V2:
1005 return sprintf(buf, "%s%s%s%s%s%s\n", spectre_v2_strings[spectre_v2_enabled],
1006 ibpb_state(),
1007 boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
1008 stibp_state(),
1009 boot_cpu_has(X86_FEATURE_RSB_CTXSW) ? ", RSB filling" : "",
1010 spectre_v2_module_string());
1011
1012 case X86_BUG_SPEC_STORE_BYPASS:
1013 return sprintf(buf, "%s\n", ssb_strings[ssb_mode]);
1014
1015 case X86_BUG_L1TF:
1016 if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV))
1017 return l1tf_show_state(buf);
1018 break;
1019 default:
1020 break;
1021 }
1022
1023 return sprintf(buf, "Vulnerable\n");
1024 }
1025
1026 ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
1027 {
1028 return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
1029 }
1030
1031 ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
1032 {
1033 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
1034 }
1035
1036 ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
1037 {
1038 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
1039 }
1040
1041 ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
1042 {
1043 return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS);
1044 }
1045
1046 ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
1047 {
1048 return cpu_show_common(dev, attr, buf, X86_BUG_L1TF);
1049 }
1050 #endif