]>
Commit | Line | Data |
---|---|---|
1353ebb4 | 1 | /* |
1353ebb4 JF |
2 | * Copyright (C) 1994 Linus Torvalds |
3 | * | |
4 | * Cyrix stuff, June 1998 by: | |
5 | * - Rafael R. Reilova (moved everything from head.S), | |
6 | * <rreilova@ececs.uc.edu> | |
7 | * - Channing Corn (tests & fixes), | |
8 | * - Andrew D. Balsa (code cleanup). | |
9 | */ | |
10 | #include <linux/init.h> | |
11 | #include <linux/utsname.h> | |
6d283d72 | 12 | #include <linux/cpu.h> |
86cdbc59 | 13 | #include <linux/smp.h> |
1de873cb TG |
14 | #include <linux/nospec.h> |
15 | #include <linux/prctl.h> | |
687cc97a | 16 | |
20b509bf | 17 | #include <asm/spec-ctrl.h> |
687cc97a | 18 | #include <asm/cmdline.h> |
91eb1b79 | 19 | #include <asm/bugs.h> |
1353ebb4 | 20 | #include <asm/processor.h> |
7ebad705 | 21 | #include <asm/processor-flags.h> |
952f07ec | 22 | #include <asm/fpu/internal.h> |
1353ebb4 JF |
23 | #include <asm/msr.h> |
24 | #include <asm/paravirt.h> | |
25 | #include <asm/alternative.h> | |
62a67e12 | 26 | #include <asm/pgtable.h> |
d1163651 | 27 | #include <asm/set_memory.h> |
12aa317c | 28 | #include <asm/intel-family.h> |
1353ebb4 | 29 | |
687cc97a | 30 | static void __init spectre_v2_select_mitigation(void); |
ef68a13e | 31 | static void __init ssb_select_mitigation(void); |
687cc97a | 32 | |
3ef956dd KRW |
33 | /* |
34 | * Our boot-time value of the SPEC_CTRL MSR. We read it once so that any | |
35 | * writes to SPEC_CTRL contain whatever reserved bits have been set. | |
36 | */ | |
0b35aca2 | 37 | u64 __ro_after_init x86_spec_ctrl_base; |
3ef956dd | 38 | |
ca6704cf KRW |
39 | /* |
40 | * The vendor and possibly platform specific bits which can be modified in | |
41 | * x86_spec_ctrl_base. | |
42 | */ | |
43 | static u64 __ro_after_init x86_spec_ctrl_mask = ~SPEC_CTRL_IBRS; | |
44 | ||
7c60cee4 KRW |
45 | /* |
46 | * AMD specific MSR info for Speculative Store Bypass control. | |
0c0f6741 | 47 | * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu(). |
7c60cee4 KRW |
48 | */ |
49 | u64 __ro_after_init x86_amd_ls_cfg_base; | |
0c0f6741 | 50 | u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask; |
7c60cee4 | 51 | |
1353ebb4 JF |
52 | void __init check_bugs(void) |
53 | { | |
54 | identify_boot_cpu(); | |
55a36b65 | 55 | |
62a67e12 BP |
56 | if (!IS_ENABLED(CONFIG_SMP)) { |
57 | pr_info("CPU: "); | |
58 | print_cpu_info(&boot_cpu_data); | |
59 | } | |
60 | ||
3ef956dd KRW |
61 | /* |
62 | * Read the SPEC_CTRL MSR to account for reserved bits which may | |
7c60cee4 KRW |
63 | * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD |
64 | * init code as it is not enumerated and depends on the family. | |
3ef956dd | 65 | */ |
200e837f | 66 | if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) |
3ef956dd KRW |
67 | rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base); |
68 | ||
687cc97a DW |
69 | /* Select the proper spectre mitigation before patching alternatives */ |
70 | spectre_v2_select_mitigation(); | |
71 | ||
ef68a13e KRW |
72 | /* |
73 | * Select proper mitigation for any exposure to the Speculative Store | |
74 | * Bypass vulnerability. | |
75 | */ | |
76 | ssb_select_mitigation(); | |
77 | ||
62a67e12 | 78 | #ifdef CONFIG_X86_32 |
55a36b65 BP |
79 | /* |
80 | * Check whether we are able to run this kernel safely on SMP. | |
81 | * | |
82 | * - i386 is no longer supported. | |
83 | * - In order to run on anything without a TSC, we need to be | |
84 | * compiled for a i486. | |
85 | */ | |
86 | if (boot_cpu_data.x86 < 4) | |
87 | panic("Kernel requires i486+ for 'invlpg' and other features"); | |
88 | ||
bfe4bb15 MV |
89 | init_utsname()->machine[1] = |
90 | '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86); | |
1353ebb4 | 91 | alternative_instructions(); |
304bceda | 92 | |
4d164092 | 93 | fpu__init_check_bugs(); |
62a67e12 BP |
94 | #else /* CONFIG_X86_64 */ |
95 | alternative_instructions(); | |
96 | ||
97 | /* | |
98 | * Make sure the first 2MB area is not mapped by huge pages | |
99 | * There are typically fixed size MTRRs in there and overlapping | |
100 | * MTRRs into large pages causes slow downs. | |
101 | * | |
102 | * Right now we don't do that with gbpages because there seems | |
103 | * very little benefit for that case. | |
104 | */ | |
105 | if (!direct_gbpages) | |
106 | set_memory_4k((unsigned long)__va(0), 1); | |
107 | #endif | |
1353ebb4 | 108 | } |
6d283d72 | 109 | |
687cc97a DW |
110 | /* The kernel command line selection */ |
111 | enum spectre_v2_mitigation_cmd { | |
112 | SPECTRE_V2_CMD_NONE, | |
113 | SPECTRE_V2_CMD_AUTO, | |
114 | SPECTRE_V2_CMD_FORCE, | |
115 | SPECTRE_V2_CMD_RETPOLINE, | |
116 | SPECTRE_V2_CMD_RETPOLINE_GENERIC, | |
117 | SPECTRE_V2_CMD_RETPOLINE_AMD, | |
118 | }; | |
119 | ||
120 | static const char *spectre_v2_strings[] = { | |
121 | [SPECTRE_V2_NONE] = "Vulnerable", | |
122 | [SPECTRE_V2_RETPOLINE_MINIMAL] = "Vulnerable: Minimal generic ASM retpoline", | |
123 | [SPECTRE_V2_RETPOLINE_MINIMAL_AMD] = "Vulnerable: Minimal AMD ASM retpoline", | |
124 | [SPECTRE_V2_RETPOLINE_GENERIC] = "Mitigation: Full generic retpoline", | |
125 | [SPECTRE_V2_RETPOLINE_AMD] = "Mitigation: Full AMD retpoline", | |
126 | }; | |
127 | ||
128 | #undef pr_fmt | |
129 | #define pr_fmt(fmt) "Spectre V2 mitigation: " fmt | |
130 | ||
ed0cbc9e KC |
131 | static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init = |
132 | SPECTRE_V2_NONE; | |
687cc97a | 133 | |
3ef956dd KRW |
134 | void x86_spec_ctrl_set(u64 val) |
135 | { | |
ca6704cf | 136 | if (val & x86_spec_ctrl_mask) |
3ef956dd KRW |
137 | WARN_ONCE(1, "SPEC_CTRL MSR value 0x%16llx is unknown.\n", val); |
138 | else | |
139 | wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base | val); | |
140 | } | |
141 | EXPORT_SYMBOL_GPL(x86_spec_ctrl_set); | |
142 | ||
143 | u64 x86_spec_ctrl_get_default(void) | |
144 | { | |
0b35aca2 TG |
145 | u64 msrval = x86_spec_ctrl_base; |
146 | ||
200e837f | 147 | if (static_cpu_has(X86_FEATURE_SPEC_CTRL)) |
0c0f6741 | 148 | msrval |= ssbd_tif_to_spec_ctrl(current_thread_info()->flags); |
0b35aca2 | 149 | return msrval; |
3ef956dd KRW |
150 | } |
151 | EXPORT_SYMBOL_GPL(x86_spec_ctrl_get_default); | |
152 | ||
3a403df4 BP |
153 | void |
154 | x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest) | |
fe170612 | 155 | { |
3a403df4 BP |
156 | struct thread_info *ti = current_thread_info(); |
157 | u64 msr, host = x86_spec_ctrl_base; | |
0b35aca2 | 158 | |
200e837f | 159 | /* Is MSR_SPEC_CTRL implemented ? */ |
3a403df4 BP |
160 | if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) { |
161 | /* SSBD controlled in MSR_SPEC_CTRL */ | |
162 | if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD)) | |
163 | host |= ssbd_tif_to_spec_ctrl(ti->flags); | |
164 | ||
165 | if (host != guest_spec_ctrl) { | |
166 | msr = setguest ? guest_spec_ctrl : host; | |
167 | wrmsrl(MSR_IA32_SPEC_CTRL, msr); | |
168 | } | |
169 | } | |
fe170612 | 170 | } |
3a403df4 | 171 | EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl); |
fe170612 | 172 | |
0c0f6741 | 173 | static void x86_amd_ssb_disable(void) |
7c60cee4 | 174 | { |
0c0f6741 | 175 | u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_ssbd_mask; |
7c60cee4 | 176 | |
08e65b2a TL |
177 | if (boot_cpu_has(X86_FEATURE_VIRT_SSBD)) |
178 | wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, SPEC_CTRL_SSBD); | |
179 | else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD)) | |
7c60cee4 KRW |
180 | wrmsrl(MSR_AMD64_LS_CFG, msrval); |
181 | } | |
182 | ||
687cc97a DW |
183 | static void __init spec2_print_if_insecure(const char *reason) |
184 | { | |
185 | if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2)) | |
186 | pr_info("%s\n", reason); | |
187 | } | |
188 | ||
189 | static void __init spec2_print_if_secure(const char *reason) | |
190 | { | |
191 | if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2)) | |
192 | pr_info("%s\n", reason); | |
193 | } | |
194 | ||
195 | static inline bool retp_compiler(void) | |
196 | { | |
197 | return __is_defined(RETPOLINE); | |
198 | } | |
199 | ||
200 | static inline bool match_option(const char *arg, int arglen, const char *opt) | |
201 | { | |
202 | int len = strlen(opt); | |
203 | ||
204 | return len == arglen && !strncmp(arg, opt, len); | |
205 | } | |
206 | ||
207 | static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void) | |
208 | { | |
209 | char arg[20]; | |
210 | int ret; | |
211 | ||
212 | ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, | |
213 | sizeof(arg)); | |
214 | if (ret > 0) { | |
215 | if (match_option(arg, ret, "off")) { | |
216 | goto disable; | |
217 | } else if (match_option(arg, ret, "on")) { | |
218 | spec2_print_if_secure("force enabled on command line."); | |
219 | return SPECTRE_V2_CMD_FORCE; | |
220 | } else if (match_option(arg, ret, "retpoline")) { | |
221 | spec2_print_if_insecure("retpoline selected on command line."); | |
222 | return SPECTRE_V2_CMD_RETPOLINE; | |
223 | } else if (match_option(arg, ret, "retpoline,amd")) { | |
224 | if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) { | |
225 | pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n"); | |
226 | return SPECTRE_V2_CMD_AUTO; | |
227 | } | |
228 | spec2_print_if_insecure("AMD retpoline selected on command line."); | |
229 | return SPECTRE_V2_CMD_RETPOLINE_AMD; | |
230 | } else if (match_option(arg, ret, "retpoline,generic")) { | |
231 | spec2_print_if_insecure("generic retpoline selected on command line."); | |
232 | return SPECTRE_V2_CMD_RETPOLINE_GENERIC; | |
233 | } else if (match_option(arg, ret, "auto")) { | |
234 | return SPECTRE_V2_CMD_AUTO; | |
235 | } | |
236 | } | |
237 | ||
238 | if (!cmdline_find_option_bool(boot_command_line, "nospectre_v2")) | |
239 | return SPECTRE_V2_CMD_AUTO; | |
240 | disable: | |
241 | spec2_print_if_insecure("disabled on command line."); | |
242 | return SPECTRE_V2_CMD_NONE; | |
243 | } | |
244 | ||
12aa317c DW |
245 | /* Check for Skylake-like CPUs (for RSB handling) */ |
246 | static bool __init is_skylake_era(void) | |
247 | { | |
248 | if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL && | |
249 | boot_cpu_data.x86 == 6) { | |
250 | switch (boot_cpu_data.x86_model) { | |
251 | case INTEL_FAM6_SKYLAKE_MOBILE: | |
252 | case INTEL_FAM6_SKYLAKE_DESKTOP: | |
253 | case INTEL_FAM6_SKYLAKE_X: | |
254 | case INTEL_FAM6_KABYLAKE_MOBILE: | |
255 | case INTEL_FAM6_KABYLAKE_DESKTOP: | |
256 | return true; | |
257 | } | |
258 | } | |
259 | return false; | |
260 | } | |
261 | ||
687cc97a DW |
262 | static void __init spectre_v2_select_mitigation(void) |
263 | { | |
264 | enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline(); | |
265 | enum spectre_v2_mitigation mode = SPECTRE_V2_NONE; | |
266 | ||
267 | /* | |
268 | * If the CPU is not affected and the command line mode is NONE or AUTO | |
269 | * then nothing to do. | |
270 | */ | |
271 | if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) && | |
272 | (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO)) | |
273 | return; | |
274 | ||
275 | switch (cmd) { | |
276 | case SPECTRE_V2_CMD_NONE: | |
277 | return; | |
278 | ||
279 | case SPECTRE_V2_CMD_FORCE: | |
280 | /* FALLTRHU */ | |
281 | case SPECTRE_V2_CMD_AUTO: | |
282 | goto retpoline_auto; | |
283 | ||
284 | case SPECTRE_V2_CMD_RETPOLINE_AMD: | |
285 | if (IS_ENABLED(CONFIG_RETPOLINE)) | |
286 | goto retpoline_amd; | |
287 | break; | |
288 | case SPECTRE_V2_CMD_RETPOLINE_GENERIC: | |
289 | if (IS_ENABLED(CONFIG_RETPOLINE)) | |
290 | goto retpoline_generic; | |
291 | break; | |
292 | case SPECTRE_V2_CMD_RETPOLINE: | |
293 | if (IS_ENABLED(CONFIG_RETPOLINE)) | |
294 | goto retpoline_auto; | |
295 | break; | |
296 | } | |
297 | pr_err("kernel not compiled with retpoline; no mitigation available!"); | |
298 | return; | |
299 | ||
300 | retpoline_auto: | |
301 | if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) { | |
302 | retpoline_amd: | |
303 | if (!boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) { | |
304 | pr_err("LFENCE not serializing. Switching to generic retpoline\n"); | |
305 | goto retpoline_generic; | |
306 | } | |
307 | mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_AMD : | |
308 | SPECTRE_V2_RETPOLINE_MINIMAL_AMD; | |
309 | setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD); | |
310 | setup_force_cpu_cap(X86_FEATURE_RETPOLINE); | |
311 | } else { | |
312 | retpoline_generic: | |
313 | mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_GENERIC : | |
314 | SPECTRE_V2_RETPOLINE_MINIMAL; | |
315 | setup_force_cpu_cap(X86_FEATURE_RETPOLINE); | |
316 | } | |
317 | ||
318 | spectre_v2_enabled = mode; | |
319 | pr_info("%s\n", spectre_v2_strings[mode]); | |
12aa317c | 320 | |
86cdbc59 AW |
321 | pr_info("Speculation control IBPB %s IBRS %s", |
322 | ibpb_supported ? "supported" : "not-supported", | |
323 | ibrs_supported ? "supported" : "not-supported"); | |
324 | ||
325 | /* | |
326 | * If we have a full retpoline mode and then disable IBPB in kernel mode | |
327 | * we do not require both. | |
328 | */ | |
329 | if (mode == SPECTRE_V2_RETPOLINE_AMD || | |
330 | mode == SPECTRE_V2_RETPOLINE_GENERIC) | |
331 | { | |
332 | if (ibrs_supported) { | |
333 | pr_info("Retpoline compiled kernel. Defaulting IBRS to disabled"); | |
334 | set_ibrs_disabled(); | |
335 | if (!ibrs_inuse) | |
336 | sysctl_ibrs_enabled = 0; | |
337 | } | |
338 | } | |
339 | ||
12aa317c DW |
340 | /* |
341 | * If neither SMEP or KPTI are available, there is a risk of | |
342 | * hitting userspace addresses in the RSB after a context switch | |
343 | * from a shallow call stack to a deeper one. To prevent this fill | |
344 | * the entire RSB, even when using IBRS. | |
345 | * | |
346 | * Skylake era CPUs have a separate issue with *underflow* of the | |
347 | * RSB, when they will predict 'ret' targets from the generic BTB. | |
348 | * The proper mitigation for this is IBRS. If IBRS is not supported | |
349 | * or deactivated in favour of retpolines the RSB fill on context | |
350 | * switch is required. | |
351 | */ | |
352 | if ((!boot_cpu_has(X86_FEATURE_PTI) && | |
353 | !boot_cpu_has(X86_FEATURE_SMEP)) || is_skylake_era()) { | |
354 | setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW); | |
355 | pr_info("Filling RSB on context switch\n"); | |
356 | } | |
687cc97a DW |
357 | } |
358 | ||
ef68a13e KRW |
359 | #undef pr_fmt |
360 | #define pr_fmt(fmt) "Speculative Store Bypass: " fmt | |
361 | ||
ed0cbc9e | 362 | static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE; |
ef68a13e KRW |
363 | |
364 | /* The kernel command line selection */ | |
365 | enum ssb_mitigation_cmd { | |
366 | SPEC_STORE_BYPASS_CMD_NONE, | |
367 | SPEC_STORE_BYPASS_CMD_AUTO, | |
368 | SPEC_STORE_BYPASS_CMD_ON, | |
1de873cb | 369 | SPEC_STORE_BYPASS_CMD_PRCTL, |
4eae6d51 | 370 | SPEC_STORE_BYPASS_CMD_SECCOMP, |
ef68a13e KRW |
371 | }; |
372 | ||
373 | static const char *ssb_strings[] = { | |
374 | [SPEC_STORE_BYPASS_NONE] = "Vulnerable", | |
1de873cb | 375 | [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass disabled", |
4eae6d51 KC |
376 | [SPEC_STORE_BYPASS_PRCTL] = "Mitigation: Speculative Store Bypass disabled via prctl", |
377 | [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp", | |
ef68a13e KRW |
378 | }; |
379 | ||
380 | static const struct { | |
381 | const char *option; | |
382 | enum ssb_mitigation_cmd cmd; | |
383 | } ssb_mitigation_options[] = { | |
4eae6d51 KC |
384 | { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */ |
385 | { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */ | |
386 | { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */ | |
387 | { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative Store Bypass via prctl */ | |
388 | { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */ | |
ef68a13e KRW |
389 | }; |
390 | ||
391 | static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void) | |
392 | { | |
393 | enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO; | |
394 | char arg[20]; | |
395 | int ret, i; | |
396 | ||
397 | if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable")) { | |
398 | return SPEC_STORE_BYPASS_CMD_NONE; | |
399 | } else { | |
400 | ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable", | |
401 | arg, sizeof(arg)); | |
402 | if (ret < 0) | |
403 | return SPEC_STORE_BYPASS_CMD_AUTO; | |
404 | ||
405 | for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) { | |
406 | if (!match_option(arg, ret, ssb_mitigation_options[i].option)) | |
407 | continue; | |
408 | ||
409 | cmd = ssb_mitigation_options[i].cmd; | |
410 | break; | |
411 | } | |
412 | ||
413 | if (i >= ARRAY_SIZE(ssb_mitigation_options)) { | |
414 | pr_err("unknown option (%s). Switching to AUTO select\n", arg); | |
415 | return SPEC_STORE_BYPASS_CMD_AUTO; | |
416 | } | |
417 | } | |
418 | ||
419 | return cmd; | |
420 | } | |
421 | ||
7124b2d7 | 422 | static enum ssb_mitigation __init __ssb_select_mitigation(void) |
ef68a13e KRW |
423 | { |
424 | enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE; | |
425 | enum ssb_mitigation_cmd cmd; | |
426 | ||
0c0f6741 | 427 | if (!boot_cpu_has(X86_FEATURE_SSBD)) |
ef68a13e KRW |
428 | return mode; |
429 | ||
430 | cmd = ssb_parse_cmdline(); | |
431 | if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) && | |
432 | (cmd == SPEC_STORE_BYPASS_CMD_NONE || | |
433 | cmd == SPEC_STORE_BYPASS_CMD_AUTO)) | |
434 | return mode; | |
435 | ||
436 | switch (cmd) { | |
437 | case SPEC_STORE_BYPASS_CMD_AUTO: | |
4eae6d51 KC |
438 | case SPEC_STORE_BYPASS_CMD_SECCOMP: |
439 | /* | |
440 | * Choose prctl+seccomp as the default mode if seccomp is | |
441 | * enabled. | |
442 | */ | |
443 | if (IS_ENABLED(CONFIG_SECCOMP)) | |
444 | mode = SPEC_STORE_BYPASS_SECCOMP; | |
445 | else | |
446 | mode = SPEC_STORE_BYPASS_PRCTL; | |
1de873cb | 447 | break; |
ef68a13e KRW |
448 | case SPEC_STORE_BYPASS_CMD_ON: |
449 | mode = SPEC_STORE_BYPASS_DISABLE; | |
450 | break; | |
1de873cb TG |
451 | case SPEC_STORE_BYPASS_CMD_PRCTL: |
452 | mode = SPEC_STORE_BYPASS_PRCTL; | |
453 | break; | |
ef68a13e KRW |
454 | case SPEC_STORE_BYPASS_CMD_NONE: |
455 | break; | |
456 | } | |
457 | ||
2b83aba8 KRW |
458 | /* |
459 | * We have three CPU feature flags that are in play here: | |
460 | * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible. | |
0c0f6741 | 461 | * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass |
2b83aba8 KRW |
462 | * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation |
463 | */ | |
1de873cb | 464 | if (mode == SPEC_STORE_BYPASS_DISABLE) { |
ef68a13e | 465 | setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE); |
2b83aba8 KRW |
466 | /* |
467 | * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD uses | |
468 | * a completely different MSR and bit dependent on family. | |
469 | */ | |
470 | switch (boot_cpu_data.x86_vendor) { | |
471 | case X86_VENDOR_INTEL: | |
0c0f6741 KRW |
472 | x86_spec_ctrl_base |= SPEC_CTRL_SSBD; |
473 | x86_spec_ctrl_mask &= ~SPEC_CTRL_SSBD; | |
474 | x86_spec_ctrl_set(SPEC_CTRL_SSBD); | |
2b83aba8 KRW |
475 | break; |
476 | case X86_VENDOR_AMD: | |
0c0f6741 | 477 | x86_amd_ssb_disable(); |
2b83aba8 KRW |
478 | break; |
479 | } | |
480 | } | |
481 | ||
ef68a13e KRW |
482 | return mode; |
483 | } | |
484 | ||
d762db65 | 485 | static void ssb_select_mitigation(void) |
ef68a13e KRW |
486 | { |
487 | ssb_mode = __ssb_select_mitigation(); | |
488 | ||
489 | if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS)) | |
490 | pr_info("%s\n", ssb_strings[ssb_mode]); | |
491 | } | |
492 | ||
687cc97a | 493 | #undef pr_fmt |
4eae6d51 | 494 | #define pr_fmt(fmt) "Speculation prctl: " fmt |
687cc97a | 495 | |
bd90e222 | 496 | static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl) |
1de873cb | 497 | { |
e29928d4 | 498 | bool update; |
1de873cb | 499 | |
4eae6d51 KC |
500 | if (ssb_mode != SPEC_STORE_BYPASS_PRCTL && |
501 | ssb_mode != SPEC_STORE_BYPASS_SECCOMP) | |
1de873cb TG |
502 | return -ENXIO; |
503 | ||
e29928d4 TG |
504 | switch (ctrl) { |
505 | case PR_SPEC_ENABLE: | |
506 | /* If speculation is force disabled, enable is not allowed */ | |
507 | if (task_spec_ssb_force_disable(task)) | |
508 | return -EPERM; | |
509 | task_clear_spec_ssb_disable(task); | |
0c0f6741 | 510 | update = test_and_clear_tsk_thread_flag(task, TIF_SSBD); |
e29928d4 TG |
511 | break; |
512 | case PR_SPEC_DISABLE: | |
513 | task_set_spec_ssb_disable(task); | |
0c0f6741 | 514 | update = !test_and_set_tsk_thread_flag(task, TIF_SSBD); |
e29928d4 TG |
515 | break; |
516 | case PR_SPEC_FORCE_DISABLE: | |
517 | task_set_spec_ssb_disable(task); | |
518 | task_set_spec_ssb_force_disable(task); | |
0c0f6741 | 519 | update = !test_and_set_tsk_thread_flag(task, TIF_SSBD); |
e29928d4 TG |
520 | break; |
521 | default: | |
522 | return -ERANGE; | |
523 | } | |
1de873cb | 524 | |
bd90e222 KC |
525 | /* |
526 | * If being set on non-current task, delay setting the CPU | |
527 | * mitigation until it is next scheduled. | |
528 | */ | |
e29928d4 | 529 | if (task == current && update) |
559c7a59 | 530 | speculative_store_bypass_update_current(); |
1de873cb TG |
531 | |
532 | return 0; | |
533 | } | |
534 | ||
32e3c480 TG |
535 | int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which, |
536 | unsigned long ctrl) | |
537 | { | |
538 | switch (which) { | |
539 | case PR_SPEC_STORE_BYPASS: | |
540 | return ssb_prctl_set(task, ctrl); | |
541 | default: | |
542 | return -ENODEV; | |
543 | } | |
544 | } | |
545 | ||
546 | #ifdef CONFIG_SECCOMP | |
547 | void arch_seccomp_spec_mitigate(struct task_struct *task) | |
548 | { | |
4eae6d51 KC |
549 | if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP) |
550 | ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE); | |
32e3c480 TG |
551 | } |
552 | #endif | |
553 | ||
bd90e222 | 554 | static int ssb_prctl_get(struct task_struct *task) |
1de873cb TG |
555 | { |
556 | switch (ssb_mode) { | |
557 | case SPEC_STORE_BYPASS_DISABLE: | |
558 | return PR_SPEC_DISABLE; | |
4eae6d51 | 559 | case SPEC_STORE_BYPASS_SECCOMP: |
1de873cb | 560 | case SPEC_STORE_BYPASS_PRCTL: |
e29928d4 TG |
561 | if (task_spec_ssb_force_disable(task)) |
562 | return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE; | |
563 | if (task_spec_ssb_disable(task)) | |
1de873cb TG |
564 | return PR_SPEC_PRCTL | PR_SPEC_DISABLE; |
565 | return PR_SPEC_PRCTL | PR_SPEC_ENABLE; | |
566 | default: | |
567 | if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS)) | |
568 | return PR_SPEC_ENABLE; | |
569 | return PR_SPEC_NOT_AFFECTED; | |
570 | } | |
571 | } | |
572 | ||
bd90e222 | 573 | int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which) |
1de873cb TG |
574 | { |
575 | switch (which) { | |
576 | case PR_SPEC_STORE_BYPASS: | |
bd90e222 | 577 | return ssb_prctl_get(task); |
1de873cb TG |
578 | default: |
579 | return -ENODEV; | |
580 | } | |
581 | } | |
582 | ||
2b83aba8 KRW |
583 | void x86_spec_ctrl_setup_ap(void) |
584 | { | |
200e837f | 585 | if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) |
ca6704cf | 586 | x86_spec_ctrl_set(x86_spec_ctrl_base & ~x86_spec_ctrl_mask); |
7c60cee4 KRW |
587 | |
588 | if (ssb_mode == SPEC_STORE_BYPASS_DISABLE) | |
0c0f6741 | 589 | x86_amd_ssb_disable(); |
2b83aba8 KRW |
590 | } |
591 | ||
6d283d72 | 592 | #ifdef CONFIG_SYSFS |
d4c8cc47 | 593 | static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr, |
d762db65 | 594 | char *buf, unsigned int bug) |
6d283d72 | 595 | { |
8f04f8ba | 596 | if (!boot_cpu_has_bug(bug)) |
6d283d72 | 597 | return sprintf(buf, "Not affected\n"); |
8f04f8ba KRW |
598 | |
599 | switch (bug) { | |
600 | case X86_BUG_CPU_MELTDOWN: | |
601 | if (boot_cpu_has(X86_FEATURE_PTI)) | |
602 | return sprintf(buf, "Mitigation: PTI\n"); | |
603 | break; | |
604 | ||
605 | case X86_BUG_SPECTRE_V1: | |
606 | if (osb_is_enabled) | |
607 | return sprintf(buf, "Mitigation: OSB (observable speculation barrier, Intel v6)\n"); | |
608 | ||
609 | case X86_BUG_SPECTRE_V2: | |
610 | return sprintf(buf, "%s%s\n", spectre_v2_strings[spectre_v2_enabled], ibpb_inuse ? ", IBPB (Intel v4)" : ""); | |
611 | ||
ef68a13e KRW |
612 | case X86_BUG_SPEC_STORE_BYPASS: |
613 | return sprintf(buf, "%s\n", ssb_strings[ssb_mode]); | |
614 | ||
8f04f8ba KRW |
615 | default: |
616 | break; | |
617 | } | |
618 | ||
6d283d72 TG |
619 | return sprintf(buf, "Vulnerable\n"); |
620 | } | |
621 | ||
8f04f8ba KRW |
622 | ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, |
623 | char *buf) | |
6d283d72 | 624 | { |
8f04f8ba | 625 | return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN); |
6d283d72 TG |
626 | } |
627 | ||
8f04f8ba KRW |
628 | ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, |
629 | char *buf) | |
6d283d72 | 630 | { |
8f04f8ba KRW |
631 | return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1); |
632 | } | |
687cc97a | 633 | |
8f04f8ba KRW |
634 | ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, |
635 | char *buf) | |
636 | { | |
637 | return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2); | |
6d283d72 | 638 | } |
abe4b60b KRW |
639 | |
640 | ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf) | |
641 | { | |
642 | return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS); | |
643 | } | |
6d283d72 | 644 | #endif |