bugs.c 19 KB

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