bugs.c 19 KB

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