bugs.c 20 KB

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