bugs.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 <asm/nospec-branch.h>
  15. #include <asm/cmdline.h>
  16. #include <asm/bugs.h>
  17. #include <asm/processor.h>
  18. #include <asm/processor-flags.h>
  19. #include <asm/fpu/internal.h>
  20. #include <asm/msr.h>
  21. #include <asm/paravirt.h>
  22. #include <asm/alternative.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/set_memory.h>
  25. static void __init spectre_v2_select_mitigation(void);
  26. void __init check_bugs(void)
  27. {
  28. identify_boot_cpu();
  29. if (!IS_ENABLED(CONFIG_SMP)) {
  30. pr_info("CPU: ");
  31. print_cpu_info(&boot_cpu_data);
  32. }
  33. /* Select the proper spectre mitigation before patching alternatives */
  34. spectre_v2_select_mitigation();
  35. #ifdef CONFIG_X86_32
  36. /*
  37. * Check whether we are able to run this kernel safely on SMP.
  38. *
  39. * - i386 is no longer supported.
  40. * - In order to run on anything without a TSC, we need to be
  41. * compiled for a i486.
  42. */
  43. if (boot_cpu_data.x86 < 4)
  44. panic("Kernel requires i486+ for 'invlpg' and other features");
  45. init_utsname()->machine[1] =
  46. '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
  47. alternative_instructions();
  48. fpu__init_check_bugs();
  49. #else /* CONFIG_X86_64 */
  50. alternative_instructions();
  51. /*
  52. * Make sure the first 2MB area is not mapped by huge pages
  53. * There are typically fixed size MTRRs in there and overlapping
  54. * MTRRs into large pages causes slow downs.
  55. *
  56. * Right now we don't do that with gbpages because there seems
  57. * very little benefit for that case.
  58. */
  59. if (!direct_gbpages)
  60. set_memory_4k((unsigned long)__va(0), 1);
  61. #endif
  62. }
  63. /* The kernel command line selection */
  64. enum spectre_v2_mitigation_cmd {
  65. SPECTRE_V2_CMD_NONE,
  66. SPECTRE_V2_CMD_AUTO,
  67. SPECTRE_V2_CMD_FORCE,
  68. SPECTRE_V2_CMD_RETPOLINE,
  69. SPECTRE_V2_CMD_RETPOLINE_GENERIC,
  70. SPECTRE_V2_CMD_RETPOLINE_AMD,
  71. };
  72. static const char *spectre_v2_strings[] = {
  73. [SPECTRE_V2_NONE] = "Vulnerable",
  74. [SPECTRE_V2_RETPOLINE_MINIMAL] = "Vulnerable: Minimal generic ASM retpoline",
  75. [SPECTRE_V2_RETPOLINE_MINIMAL_AMD] = "Vulnerable: Minimal AMD ASM retpoline",
  76. [SPECTRE_V2_RETPOLINE_GENERIC] = "Mitigation: Full generic retpoline",
  77. [SPECTRE_V2_RETPOLINE_AMD] = "Mitigation: Full AMD retpoline",
  78. };
  79. #undef pr_fmt
  80. #define pr_fmt(fmt) "Spectre V2 mitigation: " fmt
  81. static enum spectre_v2_mitigation spectre_v2_enabled = SPECTRE_V2_NONE;
  82. static void __init spec2_print_if_insecure(const char *reason)
  83. {
  84. if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
  85. pr_info("%s\n", reason);
  86. }
  87. static void __init spec2_print_if_secure(const char *reason)
  88. {
  89. if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
  90. pr_info("%s\n", reason);
  91. }
  92. static inline bool retp_compiler(void)
  93. {
  94. return __is_defined(RETPOLINE);
  95. }
  96. static inline bool match_option(const char *arg, int arglen, const char *opt)
  97. {
  98. int len = strlen(opt);
  99. return len == arglen && !strncmp(arg, opt, len);
  100. }
  101. static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
  102. {
  103. char arg[20];
  104. int ret;
  105. ret = cmdline_find_option(boot_command_line, "spectre_v2", arg,
  106. sizeof(arg));
  107. if (ret > 0) {
  108. if (match_option(arg, ret, "off")) {
  109. goto disable;
  110. } else if (match_option(arg, ret, "on")) {
  111. spec2_print_if_secure("force enabled on command line.");
  112. return SPECTRE_V2_CMD_FORCE;
  113. } else if (match_option(arg, ret, "retpoline")) {
  114. spec2_print_if_insecure("retpoline selected on command line.");
  115. return SPECTRE_V2_CMD_RETPOLINE;
  116. } else if (match_option(arg, ret, "retpoline,amd")) {
  117. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
  118. pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n");
  119. return SPECTRE_V2_CMD_AUTO;
  120. }
  121. spec2_print_if_insecure("AMD retpoline selected on command line.");
  122. return SPECTRE_V2_CMD_RETPOLINE_AMD;
  123. } else if (match_option(arg, ret, "retpoline,generic")) {
  124. spec2_print_if_insecure("generic retpoline selected on command line.");
  125. return SPECTRE_V2_CMD_RETPOLINE_GENERIC;
  126. } else if (match_option(arg, ret, "auto")) {
  127. return SPECTRE_V2_CMD_AUTO;
  128. }
  129. }
  130. if (!cmdline_find_option_bool(boot_command_line, "nospectre_v2"))
  131. return SPECTRE_V2_CMD_AUTO;
  132. disable:
  133. spec2_print_if_insecure("disabled on command line.");
  134. return SPECTRE_V2_CMD_NONE;
  135. }
  136. static void __init spectre_v2_select_mitigation(void)
  137. {
  138. enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
  139. enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
  140. /*
  141. * If the CPU is not affected and the command line mode is NONE or AUTO
  142. * then nothing to do.
  143. */
  144. if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
  145. (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
  146. return;
  147. switch (cmd) {
  148. case SPECTRE_V2_CMD_NONE:
  149. return;
  150. case SPECTRE_V2_CMD_FORCE:
  151. /* FALLTRHU */
  152. case SPECTRE_V2_CMD_AUTO:
  153. goto retpoline_auto;
  154. case SPECTRE_V2_CMD_RETPOLINE_AMD:
  155. if (IS_ENABLED(CONFIG_RETPOLINE))
  156. goto retpoline_amd;
  157. break;
  158. case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
  159. if (IS_ENABLED(CONFIG_RETPOLINE))
  160. goto retpoline_generic;
  161. break;
  162. case SPECTRE_V2_CMD_RETPOLINE:
  163. if (IS_ENABLED(CONFIG_RETPOLINE))
  164. goto retpoline_auto;
  165. break;
  166. }
  167. pr_err("kernel not compiled with retpoline; no mitigation available!");
  168. return;
  169. retpoline_auto:
  170. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
  171. retpoline_amd:
  172. if (!boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
  173. pr_err("LFENCE not serializing. Switching to generic retpoline\n");
  174. goto retpoline_generic;
  175. }
  176. mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_AMD :
  177. SPECTRE_V2_RETPOLINE_MINIMAL_AMD;
  178. setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD);
  179. setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
  180. } else {
  181. retpoline_generic:
  182. mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_GENERIC :
  183. SPECTRE_V2_RETPOLINE_MINIMAL;
  184. setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
  185. }
  186. spectre_v2_enabled = mode;
  187. pr_info("%s\n", spectre_v2_strings[mode]);
  188. }
  189. #undef pr_fmt
  190. #ifdef CONFIG_SYSFS
  191. ssize_t cpu_show_meltdown(struct device *dev,
  192. struct device_attribute *attr, char *buf)
  193. {
  194. if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
  195. return sprintf(buf, "Not affected\n");
  196. if (boot_cpu_has(X86_FEATURE_PTI))
  197. return sprintf(buf, "Mitigation: PTI\n");
  198. return sprintf(buf, "Vulnerable\n");
  199. }
  200. ssize_t cpu_show_spectre_v1(struct device *dev,
  201. struct device_attribute *attr, char *buf)
  202. {
  203. if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1))
  204. return sprintf(buf, "Not affected\n");
  205. return sprintf(buf, "Vulnerable\n");
  206. }
  207. ssize_t cpu_show_spectre_v2(struct device *dev,
  208. struct device_attribute *attr, char *buf)
  209. {
  210. if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
  211. return sprintf(buf, "Not affected\n");
  212. return sprintf(buf, "%s\n", spectre_v2_strings[spectre_v2_enabled]);
  213. }
  214. #endif