bugs.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 <asm/nospec-branch.h>
  16. #include <asm/cmdline.h>
  17. #include <asm/bugs.h>
  18. #include <asm/processor.h>
  19. #include <asm/processor-flags.h>
  20. #include <asm/fpu/internal.h>
  21. #include <asm/msr.h>
  22. #include <asm/paravirt.h>
  23. #include <asm/alternative.h>
  24. #include <asm/pgtable.h>
  25. #include <asm/set_memory.h>
  26. #include <asm/intel-family.h>
  27. static void __init spectre_v2_select_mitigation(void);
  28. void __init check_bugs(void)
  29. {
  30. identify_boot_cpu();
  31. if (!IS_ENABLED(CONFIG_SMP)) {
  32. pr_info("CPU: ");
  33. print_cpu_info(&boot_cpu_data);
  34. }
  35. /* Select the proper spectre mitigation before patching alternatives */
  36. spectre_v2_select_mitigation();
  37. #ifdef CONFIG_X86_32
  38. /*
  39. * Check whether we are able to run this kernel safely on SMP.
  40. *
  41. * - i386 is no longer supported.
  42. * - In order to run on anything without a TSC, we need to be
  43. * compiled for a i486.
  44. */
  45. if (boot_cpu_data.x86 < 4)
  46. panic("Kernel requires i486+ for 'invlpg' and other features");
  47. init_utsname()->machine[1] =
  48. '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
  49. alternative_instructions();
  50. fpu__init_check_bugs();
  51. #else /* CONFIG_X86_64 */
  52. alternative_instructions();
  53. /*
  54. * Make sure the first 2MB area is not mapped by huge pages
  55. * There are typically fixed size MTRRs in there and overlapping
  56. * MTRRs into large pages causes slow downs.
  57. *
  58. * Right now we don't do that with gbpages because there seems
  59. * very little benefit for that case.
  60. */
  61. if (!direct_gbpages)
  62. set_memory_4k((unsigned long)__va(0), 1);
  63. #endif
  64. }
  65. /* The kernel command line selection */
  66. enum spectre_v2_mitigation_cmd {
  67. SPECTRE_V2_CMD_NONE,
  68. SPECTRE_V2_CMD_AUTO,
  69. SPECTRE_V2_CMD_FORCE,
  70. SPECTRE_V2_CMD_RETPOLINE,
  71. SPECTRE_V2_CMD_RETPOLINE_GENERIC,
  72. SPECTRE_V2_CMD_RETPOLINE_AMD,
  73. };
  74. static const char *spectre_v2_strings[] = {
  75. [SPECTRE_V2_NONE] = "Vulnerable",
  76. [SPECTRE_V2_RETPOLINE_MINIMAL] = "Vulnerable: Minimal generic ASM retpoline",
  77. [SPECTRE_V2_RETPOLINE_MINIMAL_AMD] = "Vulnerable: Minimal AMD ASM retpoline",
  78. [SPECTRE_V2_RETPOLINE_GENERIC] = "Mitigation: Full generic retpoline",
  79. [SPECTRE_V2_RETPOLINE_AMD] = "Mitigation: Full AMD retpoline",
  80. };
  81. #undef pr_fmt
  82. #define pr_fmt(fmt) "Spectre V2 : " fmt
  83. static enum spectre_v2_mitigation spectre_v2_enabled = SPECTRE_V2_NONE;
  84. #ifdef RETPOLINE
  85. static bool spectre_v2_bad_module;
  86. bool retpoline_module_ok(bool has_retpoline)
  87. {
  88. if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
  89. return true;
  90. pr_err("System may be vulnerable to spectre v2\n");
  91. spectre_v2_bad_module = true;
  92. return false;
  93. }
  94. static inline const char *spectre_v2_module_string(void)
  95. {
  96. return spectre_v2_bad_module ? " - vulnerable module loaded" : "";
  97. }
  98. #else
  99. static inline const char *spectre_v2_module_string(void) { return ""; }
  100. #endif
  101. static void __init spec2_print_if_insecure(const char *reason)
  102. {
  103. if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
  104. pr_info("%s selected on command line.\n", reason);
  105. }
  106. static void __init spec2_print_if_secure(const char *reason)
  107. {
  108. if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
  109. pr_info("%s selected on command line.\n", reason);
  110. }
  111. static inline bool retp_compiler(void)
  112. {
  113. return __is_defined(RETPOLINE);
  114. }
  115. static inline bool match_option(const char *arg, int arglen, const char *opt)
  116. {
  117. int len = strlen(opt);
  118. return len == arglen && !strncmp(arg, opt, len);
  119. }
  120. static const struct {
  121. const char *option;
  122. enum spectre_v2_mitigation_cmd cmd;
  123. bool secure;
  124. } mitigation_options[] = {
  125. { "off", SPECTRE_V2_CMD_NONE, false },
  126. { "on", SPECTRE_V2_CMD_FORCE, true },
  127. { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false },
  128. { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_AMD, false },
  129. { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
  130. { "auto", SPECTRE_V2_CMD_AUTO, false },
  131. };
  132. static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
  133. {
  134. char arg[20];
  135. int ret, i;
  136. enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
  137. if (cmdline_find_option_bool(boot_command_line, "nospectre_v2"))
  138. return SPECTRE_V2_CMD_NONE;
  139. else {
  140. ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
  141. if (ret < 0)
  142. return SPECTRE_V2_CMD_AUTO;
  143. for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
  144. if (!match_option(arg, ret, mitigation_options[i].option))
  145. continue;
  146. cmd = mitigation_options[i].cmd;
  147. break;
  148. }
  149. if (i >= ARRAY_SIZE(mitigation_options)) {
  150. pr_err("unknown option (%s). Switching to AUTO select\n", arg);
  151. return SPECTRE_V2_CMD_AUTO;
  152. }
  153. }
  154. if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
  155. cmd == SPECTRE_V2_CMD_RETPOLINE_AMD ||
  156. cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC) &&
  157. !IS_ENABLED(CONFIG_RETPOLINE)) {
  158. pr_err("%s selected but not compiled in. Switching to AUTO select\n", mitigation_options[i].option);
  159. return SPECTRE_V2_CMD_AUTO;
  160. }
  161. if (cmd == SPECTRE_V2_CMD_RETPOLINE_AMD &&
  162. boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
  163. pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n");
  164. return SPECTRE_V2_CMD_AUTO;
  165. }
  166. if (mitigation_options[i].secure)
  167. spec2_print_if_secure(mitigation_options[i].option);
  168. else
  169. spec2_print_if_insecure(mitigation_options[i].option);
  170. return cmd;
  171. }
  172. /* Check for Skylake-like CPUs (for RSB handling) */
  173. static bool __init is_skylake_era(void)
  174. {
  175. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  176. boot_cpu_data.x86 == 6) {
  177. switch (boot_cpu_data.x86_model) {
  178. case INTEL_FAM6_SKYLAKE_MOBILE:
  179. case INTEL_FAM6_SKYLAKE_DESKTOP:
  180. case INTEL_FAM6_SKYLAKE_X:
  181. case INTEL_FAM6_KABYLAKE_MOBILE:
  182. case INTEL_FAM6_KABYLAKE_DESKTOP:
  183. return true;
  184. }
  185. }
  186. return false;
  187. }
  188. static void __init spectre_v2_select_mitigation(void)
  189. {
  190. enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
  191. enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
  192. /*
  193. * If the CPU is not affected and the command line mode is NONE or AUTO
  194. * then nothing to do.
  195. */
  196. if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
  197. (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
  198. return;
  199. switch (cmd) {
  200. case SPECTRE_V2_CMD_NONE:
  201. return;
  202. case SPECTRE_V2_CMD_FORCE:
  203. case SPECTRE_V2_CMD_AUTO:
  204. if (IS_ENABLED(CONFIG_RETPOLINE))
  205. goto retpoline_auto;
  206. break;
  207. case SPECTRE_V2_CMD_RETPOLINE_AMD:
  208. if (IS_ENABLED(CONFIG_RETPOLINE))
  209. goto retpoline_amd;
  210. break;
  211. case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
  212. if (IS_ENABLED(CONFIG_RETPOLINE))
  213. goto retpoline_generic;
  214. break;
  215. case SPECTRE_V2_CMD_RETPOLINE:
  216. if (IS_ENABLED(CONFIG_RETPOLINE))
  217. goto retpoline_auto;
  218. break;
  219. }
  220. pr_err("Spectre mitigation: kernel not compiled with retpoline; no mitigation available!");
  221. return;
  222. retpoline_auto:
  223. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
  224. retpoline_amd:
  225. if (!boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
  226. pr_err("Spectre mitigation: LFENCE not serializing, switching to generic retpoline\n");
  227. goto retpoline_generic;
  228. }
  229. mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_AMD :
  230. SPECTRE_V2_RETPOLINE_MINIMAL_AMD;
  231. setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD);
  232. setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
  233. } else {
  234. retpoline_generic:
  235. mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_GENERIC :
  236. SPECTRE_V2_RETPOLINE_MINIMAL;
  237. setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
  238. }
  239. spectre_v2_enabled = mode;
  240. pr_info("%s\n", spectre_v2_strings[mode]);
  241. /*
  242. * If neither SMEP nor PTI are available, there is a risk of
  243. * hitting userspace addresses in the RSB after a context switch
  244. * from a shallow call stack to a deeper one. To prevent this fill
  245. * the entire RSB, even when using IBRS.
  246. *
  247. * Skylake era CPUs have a separate issue with *underflow* of the
  248. * RSB, when they will predict 'ret' targets from the generic BTB.
  249. * The proper mitigation for this is IBRS. If IBRS is not supported
  250. * or deactivated in favour of retpolines the RSB fill on context
  251. * switch is required.
  252. */
  253. if ((!boot_cpu_has(X86_FEATURE_PTI) &&
  254. !boot_cpu_has(X86_FEATURE_SMEP)) || is_skylake_era()) {
  255. setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
  256. pr_info("Spectre v2 mitigation: Filling RSB on context switch\n");
  257. }
  258. /* Initialize Indirect Branch Prediction Barrier if supported */
  259. if (boot_cpu_has(X86_FEATURE_IBPB)) {
  260. setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
  261. pr_info("Spectre v2 mitigation: Enabling Indirect Branch Prediction Barrier\n");
  262. }
  263. /*
  264. * Retpoline means the kernel is safe because it has no indirect
  265. * branches. But firmware isn't, so use IBRS to protect that.
  266. */
  267. if (boot_cpu_has(X86_FEATURE_IBRS)) {
  268. setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
  269. pr_info("Enabling Restricted Speculation for firmware calls\n");
  270. }
  271. }
  272. #undef pr_fmt
  273. #ifdef CONFIG_SYSFS
  274. ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
  275. {
  276. if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
  277. return sprintf(buf, "Not affected\n");
  278. if (boot_cpu_has(X86_FEATURE_PTI))
  279. return sprintf(buf, "Mitigation: PTI\n");
  280. return sprintf(buf, "Vulnerable\n");
  281. }
  282. ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
  283. {
  284. if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1))
  285. return sprintf(buf, "Not affected\n");
  286. return sprintf(buf, "Mitigation: __user pointer sanitization\n");
  287. }
  288. ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
  289. {
  290. if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
  291. return sprintf(buf, "Not affected\n");
  292. return sprintf(buf, "%s%s%s%s\n", spectre_v2_strings[spectre_v2_enabled],
  293. boot_cpu_has(X86_FEATURE_USE_IBPB) ? ", IBPB" : "",
  294. boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
  295. spectre_v2_module_string());
  296. }
  297. #endif