bugs.c 20 KB

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