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