1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 1994 Linus Torvalds
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).
11 #include <linux/init.h>
12 #include <linux/utsname.h>
13 #include <linux/cpu.h>
15 #include <asm/nospec-branch.h>
16 #include <asm/cmdline.h>
18 #include <asm/processor.h>
19 #include <asm/processor-flags.h>
20 #include <asm/fpu/internal.h>
22 #include <asm/paravirt.h>
23 #include <asm/alternative.h>
24 #include <asm/pgtable.h>
25 #include <asm/set_memory.h>
27 static void __init spectre_v2_select_mitigation(void);
29 void __init check_bugs(void)
33 if (!IS_ENABLED(CONFIG_SMP)) {
35 print_cpu_info(&boot_cpu_data);
38 /* Select the proper spectre mitigation before patching alternatives */
39 spectre_v2_select_mitigation();
43 * Check whether we are able to run this kernel safely on SMP.
45 * - i386 is no longer supported.
46 * - In order to run on anything without a TSC, we need to be
47 * compiled for a i486.
49 if (boot_cpu_data.x86 < 4)
50 panic("Kernel requires i486+ for 'invlpg' and other features");
52 init_utsname()->machine[1] =
53 '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
54 alternative_instructions();
56 fpu__init_check_bugs();
57 #else /* CONFIG_X86_64 */
58 alternative_instructions();
61 * Make sure the first 2MB area is not mapped by huge pages
62 * There are typically fixed size MTRRs in there and overlapping
63 * MTRRs into large pages causes slow downs.
65 * Right now we don't do that with gbpages because there seems
66 * very little benefit for that case.
69 set_memory_4k((unsigned long)__va(0), 1);
73 /* The kernel command line selection */
74 enum spectre_v2_mitigation_cmd {
78 SPECTRE_V2_CMD_RETPOLINE,
79 SPECTRE_V2_CMD_RETPOLINE_GENERIC,
80 SPECTRE_V2_CMD_RETPOLINE_AMD,
83 static const char *spectre_v2_strings[] = {
84 [SPECTRE_V2_NONE] = "Vulnerable",
85 [SPECTRE_V2_RETPOLINE_MINIMAL] = "Vulnerable: Minimal generic ASM retpoline",
86 [SPECTRE_V2_RETPOLINE_MINIMAL_AMD] = "Vulnerable: Minimal AMD ASM retpoline",
87 [SPECTRE_V2_RETPOLINE_GENERIC] = "Mitigation: Full generic retpoline",
88 [SPECTRE_V2_RETPOLINE_AMD] = "Mitigation: Full AMD retpoline",
92 #define pr_fmt(fmt) "Spectre V2 mitigation: " fmt
94 static enum spectre_v2_mitigation spectre_v2_enabled = SPECTRE_V2_NONE;
96 static void __init spec2_print_if_insecure(const char *reason)
98 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
99 pr_info("%s\n", reason);
102 static void __init spec2_print_if_secure(const char *reason)
104 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
105 pr_info("%s\n", reason);
108 static inline bool retp_compiler(void)
110 return __is_defined(RETPOLINE);
113 static inline bool match_option(const char *arg, int arglen, const char *opt)
115 int len = strlen(opt);
117 return len == arglen && !strncmp(arg, opt, len);
120 static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
125 ret = cmdline_find_option(boot_command_line, "spectre_v2", arg,
128 if (match_option(arg, ret, "off")) {
130 } else if (match_option(arg, ret, "on")) {
131 spec2_print_if_secure("force enabled on command line.");
132 return SPECTRE_V2_CMD_FORCE;
133 } else if (match_option(arg, ret, "retpoline")) {
134 spec2_print_if_insecure("retpoline selected on command line.");
135 return SPECTRE_V2_CMD_RETPOLINE;
136 } else if (match_option(arg, ret, "retpoline,amd")) {
137 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
138 pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n");
139 return SPECTRE_V2_CMD_AUTO;
141 spec2_print_if_insecure("AMD retpoline selected on command line.");
142 return SPECTRE_V2_CMD_RETPOLINE_AMD;
143 } else if (match_option(arg, ret, "retpoline,generic")) {
144 spec2_print_if_insecure("generic retpoline selected on command line.");
145 return SPECTRE_V2_CMD_RETPOLINE_GENERIC;
146 } else if (match_option(arg, ret, "auto")) {
147 return SPECTRE_V2_CMD_AUTO;
151 if (!cmdline_find_option_bool(boot_command_line, "nospectre_v2"))
152 return SPECTRE_V2_CMD_AUTO;
154 spec2_print_if_insecure("disabled on command line.");
155 return SPECTRE_V2_CMD_NONE;
158 static void __init spectre_v2_select_mitigation(void)
160 enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
161 enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
164 * If the CPU is not affected and the command line mode is NONE or AUTO
165 * then nothing to do.
167 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
168 (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
172 case SPECTRE_V2_CMD_NONE:
175 case SPECTRE_V2_CMD_FORCE:
177 case SPECTRE_V2_CMD_AUTO:
180 case SPECTRE_V2_CMD_RETPOLINE_AMD:
181 if (IS_ENABLED(CONFIG_RETPOLINE))
184 case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
185 if (IS_ENABLED(CONFIG_RETPOLINE))
186 goto retpoline_generic;
188 case SPECTRE_V2_CMD_RETPOLINE:
189 if (IS_ENABLED(CONFIG_RETPOLINE))
193 pr_err("kernel not compiled with retpoline; no mitigation available!");
197 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
199 if (!boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
200 pr_err("LFENCE not serializing. Switching to generic retpoline\n");
201 goto retpoline_generic;
203 mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_AMD :
204 SPECTRE_V2_RETPOLINE_MINIMAL_AMD;
205 setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD);
206 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
209 mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_GENERIC :
210 SPECTRE_V2_RETPOLINE_MINIMAL;
211 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
214 spectre_v2_enabled = mode;
215 pr_info("%s\n", spectre_v2_strings[mode]);
221 ssize_t cpu_show_meltdown(struct device *dev,
222 struct device_attribute *attr, char *buf)
224 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
225 return sprintf(buf, "Not affected\n");
226 if (boot_cpu_has(X86_FEATURE_PTI))
227 return sprintf(buf, "Mitigation: PTI\n");
228 return sprintf(buf, "Vulnerable\n");
231 ssize_t cpu_show_spectre_v1(struct device *dev,
232 struct device_attribute *attr, char *buf)
234 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1))
235 return sprintf(buf, "Not affected\n");
236 return sprintf(buf, "Vulnerable\n");
239 ssize_t cpu_show_spectre_v2(struct device *dev,
240 struct device_attribute *attr, char *buf)
242 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
243 return sprintf(buf, "Not affected\n");
245 return sprintf(buf, "%s\n", spectre_v2_strings[spectre_v2_enabled]);