bugs.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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 <linux/sched/smt.h>
  18. #include <asm/spec-ctrl.h>
  19. #include <asm/cmdline.h>
  20. #include <asm/bugs.h>
  21. #include <asm/processor.h>
  22. #include <asm/processor-flags.h>
  23. #include <asm/fpu/internal.h>
  24. #include <asm/msr.h>
  25. #include <asm/vmx.h>
  26. #include <asm/paravirt.h>
  27. #include <asm/alternative.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/set_memory.h>
  30. #include <asm/intel-family.h>
  31. #include <asm/e820/api.h>
  32. #include <asm/hypervisor.h>
  33. static void __init spectre_v2_select_mitigation(void);
  34. static void __init ssb_select_mitigation(void);
  35. static void __init l1tf_select_mitigation(void);
  36. /* The base value of the SPEC_CTRL MSR that always has to be preserved. */
  37. u64 x86_spec_ctrl_base;
  38. EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
  39. static DEFINE_MUTEX(spec_ctrl_mutex);
  40. /*
  41. * The vendor and possibly platform specific bits which can be modified in
  42. * x86_spec_ctrl_base.
  43. */
  44. static u64 __ro_after_init x86_spec_ctrl_mask = SPEC_CTRL_IBRS;
  45. /*
  46. * AMD specific MSR info for Speculative Store Bypass control.
  47. * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu().
  48. */
  49. u64 __ro_after_init x86_amd_ls_cfg_base;
  50. u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask;
  51. /* Control conditional STIPB in switch_to() */
  52. DEFINE_STATIC_KEY_FALSE(switch_to_cond_stibp);
  53. /* Control conditional IBPB in switch_mm() */
  54. DEFINE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
  55. /* Control unconditional IBPB in switch_mm() */
  56. DEFINE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
  57. void __init check_bugs(void)
  58. {
  59. identify_boot_cpu();
  60. /*
  61. * identify_boot_cpu() initialized SMT support information, let the
  62. * core code know.
  63. */
  64. cpu_smt_check_topology_early();
  65. if (!IS_ENABLED(CONFIG_SMP)) {
  66. pr_info("CPU: ");
  67. print_cpu_info(&boot_cpu_data);
  68. }
  69. /*
  70. * Read the SPEC_CTRL MSR to account for reserved bits which may
  71. * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
  72. * init code as it is not enumerated and depends on the family.
  73. */
  74. if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
  75. rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
  76. /* Allow STIBP in MSR_SPEC_CTRL if supported */
  77. if (boot_cpu_has(X86_FEATURE_STIBP))
  78. x86_spec_ctrl_mask |= SPEC_CTRL_STIBP;
  79. /* Select the proper spectre mitigation before patching alternatives */
  80. spectre_v2_select_mitigation();
  81. /*
  82. * Select proper mitigation for any exposure to the Speculative Store
  83. * Bypass vulnerability.
  84. */
  85. ssb_select_mitigation();
  86. l1tf_select_mitigation();
  87. #ifdef CONFIG_X86_32
  88. /*
  89. * Check whether we are able to run this kernel safely on SMP.
  90. *
  91. * - i386 is no longer supported.
  92. * - In order to run on anything without a TSC, we need to be
  93. * compiled for a i486.
  94. */
  95. if (boot_cpu_data.x86 < 4)
  96. panic("Kernel requires i486+ for 'invlpg' and other features");
  97. init_utsname()->machine[1] =
  98. '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
  99. alternative_instructions();
  100. fpu__init_check_bugs();
  101. #else /* CONFIG_X86_64 */
  102. alternative_instructions();
  103. /*
  104. * Make sure the first 2MB area is not mapped by huge pages
  105. * There are typically fixed size MTRRs in there and overlapping
  106. * MTRRs into large pages causes slow downs.
  107. *
  108. * Right now we don't do that with gbpages because there seems
  109. * very little benefit for that case.
  110. */
  111. if (!direct_gbpages)
  112. set_memory_4k((unsigned long)__va(0), 1);
  113. #endif
  114. }
  115. void
  116. x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest)
  117. {
  118. u64 msrval, guestval, hostval = x86_spec_ctrl_base;
  119. struct thread_info *ti = current_thread_info();
  120. /* Is MSR_SPEC_CTRL implemented ? */
  121. if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) {
  122. /*
  123. * Restrict guest_spec_ctrl to supported values. Clear the
  124. * modifiable bits in the host base value and or the
  125. * modifiable bits from the guest value.
  126. */
  127. guestval = hostval & ~x86_spec_ctrl_mask;
  128. guestval |= guest_spec_ctrl & x86_spec_ctrl_mask;
  129. /* SSBD controlled in MSR_SPEC_CTRL */
  130. if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
  131. static_cpu_has(X86_FEATURE_AMD_SSBD))
  132. hostval |= ssbd_tif_to_spec_ctrl(ti->flags);
  133. /* Conditional STIBP enabled? */
  134. if (static_branch_unlikely(&switch_to_cond_stibp))
  135. hostval |= stibp_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. speculation_ctrl_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. #undef pr_fmt
  176. #define pr_fmt(fmt) "Spectre V2 : " fmt
  177. static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init =
  178. SPECTRE_V2_NONE;
  179. static enum spectre_v2_user_mitigation spectre_v2_user __ro_after_init =
  180. SPECTRE_V2_USER_NONE;
  181. #ifdef RETPOLINE
  182. static bool spectre_v2_bad_module;
  183. bool retpoline_module_ok(bool has_retpoline)
  184. {
  185. if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
  186. return true;
  187. pr_err("System may be vulnerable to spectre v2\n");
  188. spectre_v2_bad_module = true;
  189. return false;
  190. }
  191. static inline const char *spectre_v2_module_string(void)
  192. {
  193. return spectre_v2_bad_module ? " - vulnerable module loaded" : "";
  194. }
  195. #else
  196. static inline const char *spectre_v2_module_string(void) { return ""; }
  197. #endif
  198. static inline bool match_option(const char *arg, int arglen, const char *opt)
  199. {
  200. int len = strlen(opt);
  201. return len == arglen && !strncmp(arg, opt, len);
  202. }
  203. /* The kernel command line selection for spectre v2 */
  204. enum spectre_v2_mitigation_cmd {
  205. SPECTRE_V2_CMD_NONE,
  206. SPECTRE_V2_CMD_AUTO,
  207. SPECTRE_V2_CMD_FORCE,
  208. SPECTRE_V2_CMD_RETPOLINE,
  209. SPECTRE_V2_CMD_RETPOLINE_GENERIC,
  210. SPECTRE_V2_CMD_RETPOLINE_AMD,
  211. };
  212. enum spectre_v2_user_cmd {
  213. SPECTRE_V2_USER_CMD_NONE,
  214. SPECTRE_V2_USER_CMD_AUTO,
  215. SPECTRE_V2_USER_CMD_FORCE,
  216. SPECTRE_V2_USER_CMD_PRCTL,
  217. SPECTRE_V2_USER_CMD_PRCTL_IBPB,
  218. SPECTRE_V2_USER_CMD_SECCOMP,
  219. SPECTRE_V2_USER_CMD_SECCOMP_IBPB,
  220. };
  221. static const char * const spectre_v2_user_strings[] = {
  222. [SPECTRE_V2_USER_NONE] = "User space: Vulnerable",
  223. [SPECTRE_V2_USER_STRICT] = "User space: Mitigation: STIBP protection",
  224. [SPECTRE_V2_USER_PRCTL] = "User space: Mitigation: STIBP via prctl",
  225. [SPECTRE_V2_USER_SECCOMP] = "User space: Mitigation: STIBP via seccomp and prctl",
  226. };
  227. static const struct {
  228. const char *option;
  229. enum spectre_v2_user_cmd cmd;
  230. bool secure;
  231. } v2_user_options[] __initdata = {
  232. { "auto", SPECTRE_V2_USER_CMD_AUTO, false },
  233. { "off", SPECTRE_V2_USER_CMD_NONE, false },
  234. { "on", SPECTRE_V2_USER_CMD_FORCE, true },
  235. { "prctl", SPECTRE_V2_USER_CMD_PRCTL, false },
  236. { "prctl,ibpb", SPECTRE_V2_USER_CMD_PRCTL_IBPB, false },
  237. { "seccomp", SPECTRE_V2_USER_CMD_SECCOMP, false },
  238. { "seccomp,ibpb", SPECTRE_V2_USER_CMD_SECCOMP_IBPB, false },
  239. };
  240. static void __init spec_v2_user_print_cond(const char *reason, bool secure)
  241. {
  242. if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
  243. pr_info("spectre_v2_user=%s forced on command line.\n", reason);
  244. }
  245. static enum spectre_v2_user_cmd __init
  246. spectre_v2_parse_user_cmdline(enum spectre_v2_mitigation_cmd v2_cmd)
  247. {
  248. char arg[20];
  249. int ret, i;
  250. switch (v2_cmd) {
  251. case SPECTRE_V2_CMD_NONE:
  252. return SPECTRE_V2_USER_CMD_NONE;
  253. case SPECTRE_V2_CMD_FORCE:
  254. return SPECTRE_V2_USER_CMD_FORCE;
  255. default:
  256. break;
  257. }
  258. ret = cmdline_find_option(boot_command_line, "spectre_v2_user",
  259. arg, sizeof(arg));
  260. if (ret < 0)
  261. return SPECTRE_V2_USER_CMD_AUTO;
  262. for (i = 0; i < ARRAY_SIZE(v2_user_options); i++) {
  263. if (match_option(arg, ret, v2_user_options[i].option)) {
  264. spec_v2_user_print_cond(v2_user_options[i].option,
  265. v2_user_options[i].secure);
  266. return v2_user_options[i].cmd;
  267. }
  268. }
  269. pr_err("Unknown user space protection option (%s). Switching to AUTO select\n", arg);
  270. return SPECTRE_V2_USER_CMD_AUTO;
  271. }
  272. static void __init
  273. spectre_v2_user_select_mitigation(enum spectre_v2_mitigation_cmd v2_cmd)
  274. {
  275. enum spectre_v2_user_mitigation mode = SPECTRE_V2_USER_NONE;
  276. bool smt_possible = IS_ENABLED(CONFIG_SMP);
  277. enum spectre_v2_user_cmd cmd;
  278. if (!boot_cpu_has(X86_FEATURE_IBPB) && !boot_cpu_has(X86_FEATURE_STIBP))
  279. return;
  280. if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
  281. cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
  282. smt_possible = false;
  283. cmd = spectre_v2_parse_user_cmdline(v2_cmd);
  284. switch (cmd) {
  285. case SPECTRE_V2_USER_CMD_NONE:
  286. goto set_mode;
  287. case SPECTRE_V2_USER_CMD_FORCE:
  288. mode = SPECTRE_V2_USER_STRICT;
  289. break;
  290. case SPECTRE_V2_USER_CMD_PRCTL:
  291. case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
  292. mode = SPECTRE_V2_USER_PRCTL;
  293. break;
  294. case SPECTRE_V2_USER_CMD_AUTO:
  295. case SPECTRE_V2_USER_CMD_SECCOMP:
  296. case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
  297. if (IS_ENABLED(CONFIG_SECCOMP))
  298. mode = SPECTRE_V2_USER_SECCOMP;
  299. else
  300. mode = SPECTRE_V2_USER_PRCTL;
  301. break;
  302. }
  303. /* Initialize Indirect Branch Prediction Barrier */
  304. if (boot_cpu_has(X86_FEATURE_IBPB)) {
  305. setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
  306. switch (cmd) {
  307. case SPECTRE_V2_USER_CMD_FORCE:
  308. case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
  309. case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
  310. static_branch_enable(&switch_mm_always_ibpb);
  311. break;
  312. case SPECTRE_V2_USER_CMD_PRCTL:
  313. case SPECTRE_V2_USER_CMD_AUTO:
  314. case SPECTRE_V2_USER_CMD_SECCOMP:
  315. static_branch_enable(&switch_mm_cond_ibpb);
  316. break;
  317. default:
  318. break;
  319. }
  320. pr_info("mitigation: Enabling %s Indirect Branch Prediction Barrier\n",
  321. static_key_enabled(&switch_mm_always_ibpb) ?
  322. "always-on" : "conditional");
  323. }
  324. /* If enhanced IBRS is enabled no STIPB required */
  325. if (spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
  326. return;
  327. /*
  328. * If SMT is not possible or STIBP is not available clear the STIPB
  329. * mode.
  330. */
  331. if (!smt_possible || !boot_cpu_has(X86_FEATURE_STIBP))
  332. mode = SPECTRE_V2_USER_NONE;
  333. set_mode:
  334. spectre_v2_user = mode;
  335. /* Only print the STIBP mode when SMT possible */
  336. if (smt_possible)
  337. pr_info("%s\n", spectre_v2_user_strings[mode]);
  338. }
  339. static const char * const spectre_v2_strings[] = {
  340. [SPECTRE_V2_NONE] = "Vulnerable",
  341. [SPECTRE_V2_RETPOLINE_GENERIC] = "Mitigation: Full generic retpoline",
  342. [SPECTRE_V2_RETPOLINE_AMD] = "Mitigation: Full AMD retpoline",
  343. [SPECTRE_V2_IBRS_ENHANCED] = "Mitigation: Enhanced IBRS",
  344. };
  345. static const struct {
  346. const char *option;
  347. enum spectre_v2_mitigation_cmd cmd;
  348. bool secure;
  349. } mitigation_options[] __initdata = {
  350. { "off", SPECTRE_V2_CMD_NONE, false },
  351. { "on", SPECTRE_V2_CMD_FORCE, true },
  352. { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false },
  353. { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_AMD, false },
  354. { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
  355. { "auto", SPECTRE_V2_CMD_AUTO, false },
  356. };
  357. static void __init spec_v2_print_cond(const char *reason, bool secure)
  358. {
  359. if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
  360. pr_info("%s selected on command line.\n", reason);
  361. }
  362. static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
  363. {
  364. enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
  365. char arg[20];
  366. int ret, i;
  367. if (cmdline_find_option_bool(boot_command_line, "nospectre_v2"))
  368. return SPECTRE_V2_CMD_NONE;
  369. ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
  370. if (ret < 0)
  371. return SPECTRE_V2_CMD_AUTO;
  372. for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
  373. if (!match_option(arg, ret, mitigation_options[i].option))
  374. continue;
  375. cmd = mitigation_options[i].cmd;
  376. break;
  377. }
  378. if (i >= ARRAY_SIZE(mitigation_options)) {
  379. pr_err("unknown option (%s). Switching to AUTO select\n", arg);
  380. return SPECTRE_V2_CMD_AUTO;
  381. }
  382. if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
  383. cmd == SPECTRE_V2_CMD_RETPOLINE_AMD ||
  384. cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC) &&
  385. !IS_ENABLED(CONFIG_RETPOLINE)) {
  386. pr_err("%s selected but not compiled in. Switching to AUTO select\n", mitigation_options[i].option);
  387. return SPECTRE_V2_CMD_AUTO;
  388. }
  389. if (cmd == SPECTRE_V2_CMD_RETPOLINE_AMD &&
  390. boot_cpu_data.x86_vendor != X86_VENDOR_HYGON &&
  391. boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
  392. pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n");
  393. return SPECTRE_V2_CMD_AUTO;
  394. }
  395. spec_v2_print_cond(mitigation_options[i].option,
  396. mitigation_options[i].secure);
  397. return cmd;
  398. }
  399. static void __init spectre_v2_select_mitigation(void)
  400. {
  401. enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
  402. enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
  403. /*
  404. * If the CPU is not affected and the command line mode is NONE or AUTO
  405. * then nothing to do.
  406. */
  407. if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
  408. (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
  409. return;
  410. switch (cmd) {
  411. case SPECTRE_V2_CMD_NONE:
  412. return;
  413. case SPECTRE_V2_CMD_FORCE:
  414. case SPECTRE_V2_CMD_AUTO:
  415. if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
  416. mode = SPECTRE_V2_IBRS_ENHANCED;
  417. /* Force it so VMEXIT will restore correctly */
  418. x86_spec_ctrl_base |= SPEC_CTRL_IBRS;
  419. wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
  420. goto specv2_set_mode;
  421. }
  422. if (IS_ENABLED(CONFIG_RETPOLINE))
  423. goto retpoline_auto;
  424. break;
  425. case SPECTRE_V2_CMD_RETPOLINE_AMD:
  426. if (IS_ENABLED(CONFIG_RETPOLINE))
  427. goto retpoline_amd;
  428. break;
  429. case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
  430. if (IS_ENABLED(CONFIG_RETPOLINE))
  431. goto retpoline_generic;
  432. break;
  433. case SPECTRE_V2_CMD_RETPOLINE:
  434. if (IS_ENABLED(CONFIG_RETPOLINE))
  435. goto retpoline_auto;
  436. break;
  437. }
  438. pr_err("Spectre mitigation: kernel not compiled with retpoline; no mitigation available!");
  439. return;
  440. retpoline_auto:
  441. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
  442. boot_cpu_data.x86_vendor == X86_VENDOR_HYGON) {
  443. retpoline_amd:
  444. if (!boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
  445. pr_err("Spectre mitigation: LFENCE not serializing, switching to generic retpoline\n");
  446. goto retpoline_generic;
  447. }
  448. mode = SPECTRE_V2_RETPOLINE_AMD;
  449. setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD);
  450. setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
  451. } else {
  452. retpoline_generic:
  453. mode = SPECTRE_V2_RETPOLINE_GENERIC;
  454. setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
  455. }
  456. specv2_set_mode:
  457. spectre_v2_enabled = mode;
  458. pr_info("%s\n", spectre_v2_strings[mode]);
  459. /*
  460. * If spectre v2 protection has been enabled, unconditionally fill
  461. * RSB during a context switch; this protects against two independent
  462. * issues:
  463. *
  464. * - RSB underflow (and switch to BTB) on Skylake+
  465. * - SpectreRSB variant of spectre v2 on X86_BUG_SPECTRE_V2 CPUs
  466. */
  467. setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
  468. pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n");
  469. /*
  470. * Retpoline means the kernel is safe because it has no indirect
  471. * branches. Enhanced IBRS protects firmware too, so, enable restricted
  472. * speculation around firmware calls only when Enhanced IBRS isn't
  473. * supported.
  474. *
  475. * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because
  476. * the user might select retpoline on the kernel command line and if
  477. * the CPU supports Enhanced IBRS, kernel might un-intentionally not
  478. * enable IBRS around firmware calls.
  479. */
  480. if (boot_cpu_has(X86_FEATURE_IBRS) && mode != SPECTRE_V2_IBRS_ENHANCED) {
  481. setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
  482. pr_info("Enabling Restricted Speculation for firmware calls\n");
  483. }
  484. /* Set up IBPB and STIBP depending on the general spectre V2 command */
  485. spectre_v2_user_select_mitigation(cmd);
  486. /* Enable STIBP if appropriate */
  487. arch_smt_update();
  488. }
  489. static void update_stibp_msr(void * __unused)
  490. {
  491. wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
  492. }
  493. /* Update x86_spec_ctrl_base in case SMT state changed. */
  494. static void update_stibp_strict(void)
  495. {
  496. u64 mask = x86_spec_ctrl_base & ~SPEC_CTRL_STIBP;
  497. if (sched_smt_active())
  498. mask |= SPEC_CTRL_STIBP;
  499. if (mask == x86_spec_ctrl_base)
  500. return;
  501. pr_info("Update user space SMT mitigation: STIBP %s\n",
  502. mask & SPEC_CTRL_STIBP ? "always-on" : "off");
  503. x86_spec_ctrl_base = mask;
  504. on_each_cpu(update_stibp_msr, NULL, 1);
  505. }
  506. /* Update the static key controlling the evaluation of TIF_SPEC_IB */
  507. static void update_indir_branch_cond(void)
  508. {
  509. if (sched_smt_active())
  510. static_branch_enable(&switch_to_cond_stibp);
  511. else
  512. static_branch_disable(&switch_to_cond_stibp);
  513. }
  514. void arch_smt_update(void)
  515. {
  516. /* Enhanced IBRS implies STIBP. No update required. */
  517. if (spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
  518. return;
  519. mutex_lock(&spec_ctrl_mutex);
  520. switch (spectre_v2_user) {
  521. case SPECTRE_V2_USER_NONE:
  522. break;
  523. case SPECTRE_V2_USER_STRICT:
  524. update_stibp_strict();
  525. break;
  526. case SPECTRE_V2_USER_PRCTL:
  527. case SPECTRE_V2_USER_SECCOMP:
  528. update_indir_branch_cond();
  529. break;
  530. }
  531. mutex_unlock(&spec_ctrl_mutex);
  532. }
  533. #undef pr_fmt
  534. #define pr_fmt(fmt) "Speculative Store Bypass: " fmt
  535. static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
  536. /* The kernel command line selection */
  537. enum ssb_mitigation_cmd {
  538. SPEC_STORE_BYPASS_CMD_NONE,
  539. SPEC_STORE_BYPASS_CMD_AUTO,
  540. SPEC_STORE_BYPASS_CMD_ON,
  541. SPEC_STORE_BYPASS_CMD_PRCTL,
  542. SPEC_STORE_BYPASS_CMD_SECCOMP,
  543. };
  544. static const char * const ssb_strings[] = {
  545. [SPEC_STORE_BYPASS_NONE] = "Vulnerable",
  546. [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass disabled",
  547. [SPEC_STORE_BYPASS_PRCTL] = "Mitigation: Speculative Store Bypass disabled via prctl",
  548. [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
  549. };
  550. static const struct {
  551. const char *option;
  552. enum ssb_mitigation_cmd cmd;
  553. } ssb_mitigation_options[] __initdata = {
  554. { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */
  555. { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */
  556. { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */
  557. { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative Store Bypass via prctl */
  558. { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */
  559. };
  560. static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
  561. {
  562. enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO;
  563. char arg[20];
  564. int ret, i;
  565. if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable")) {
  566. return SPEC_STORE_BYPASS_CMD_NONE;
  567. } else {
  568. ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable",
  569. arg, sizeof(arg));
  570. if (ret < 0)
  571. return SPEC_STORE_BYPASS_CMD_AUTO;
  572. for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) {
  573. if (!match_option(arg, ret, ssb_mitigation_options[i].option))
  574. continue;
  575. cmd = ssb_mitigation_options[i].cmd;
  576. break;
  577. }
  578. if (i >= ARRAY_SIZE(ssb_mitigation_options)) {
  579. pr_err("unknown option (%s). Switching to AUTO select\n", arg);
  580. return SPEC_STORE_BYPASS_CMD_AUTO;
  581. }
  582. }
  583. return cmd;
  584. }
  585. static enum ssb_mitigation __init __ssb_select_mitigation(void)
  586. {
  587. enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
  588. enum ssb_mitigation_cmd cmd;
  589. if (!boot_cpu_has(X86_FEATURE_SSBD))
  590. return mode;
  591. cmd = ssb_parse_cmdline();
  592. if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) &&
  593. (cmd == SPEC_STORE_BYPASS_CMD_NONE ||
  594. cmd == SPEC_STORE_BYPASS_CMD_AUTO))
  595. return mode;
  596. switch (cmd) {
  597. case SPEC_STORE_BYPASS_CMD_AUTO:
  598. case SPEC_STORE_BYPASS_CMD_SECCOMP:
  599. /*
  600. * Choose prctl+seccomp as the default mode if seccomp is
  601. * enabled.
  602. */
  603. if (IS_ENABLED(CONFIG_SECCOMP))
  604. mode = SPEC_STORE_BYPASS_SECCOMP;
  605. else
  606. mode = SPEC_STORE_BYPASS_PRCTL;
  607. break;
  608. case SPEC_STORE_BYPASS_CMD_ON:
  609. mode = SPEC_STORE_BYPASS_DISABLE;
  610. break;
  611. case SPEC_STORE_BYPASS_CMD_PRCTL:
  612. mode = SPEC_STORE_BYPASS_PRCTL;
  613. break;
  614. case SPEC_STORE_BYPASS_CMD_NONE:
  615. break;
  616. }
  617. /*
  618. * We have three CPU feature flags that are in play here:
  619. * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
  620. * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
  621. * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
  622. */
  623. if (mode == SPEC_STORE_BYPASS_DISABLE) {
  624. setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
  625. /*
  626. * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
  627. * use a completely different MSR and bit dependent on family.
  628. */
  629. if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
  630. !static_cpu_has(X86_FEATURE_AMD_SSBD)) {
  631. x86_amd_ssb_disable();
  632. } else {
  633. x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
  634. x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
  635. wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
  636. }
  637. }
  638. return mode;
  639. }
  640. static void ssb_select_mitigation(void)
  641. {
  642. ssb_mode = __ssb_select_mitigation();
  643. if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
  644. pr_info("%s\n", ssb_strings[ssb_mode]);
  645. }
  646. #undef pr_fmt
  647. #define pr_fmt(fmt) "Speculation prctl: " fmt
  648. static void task_update_spec_tif(struct task_struct *tsk)
  649. {
  650. /* Force the update of the real TIF bits */
  651. set_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE);
  652. /*
  653. * Immediately update the speculation control MSRs for the current
  654. * task, but for a non-current task delay setting the CPU
  655. * mitigation until it is scheduled next.
  656. *
  657. * This can only happen for SECCOMP mitigation. For PRCTL it's
  658. * always the current task.
  659. */
  660. if (tsk == current)
  661. speculation_ctrl_update_current();
  662. }
  663. static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
  664. {
  665. if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
  666. ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
  667. return -ENXIO;
  668. switch (ctrl) {
  669. case PR_SPEC_ENABLE:
  670. /* If speculation is force disabled, enable is not allowed */
  671. if (task_spec_ssb_force_disable(task))
  672. return -EPERM;
  673. task_clear_spec_ssb_disable(task);
  674. task_update_spec_tif(task);
  675. break;
  676. case PR_SPEC_DISABLE:
  677. task_set_spec_ssb_disable(task);
  678. task_update_spec_tif(task);
  679. break;
  680. case PR_SPEC_FORCE_DISABLE:
  681. task_set_spec_ssb_disable(task);
  682. task_set_spec_ssb_force_disable(task);
  683. task_update_spec_tif(task);
  684. break;
  685. default:
  686. return -ERANGE;
  687. }
  688. return 0;
  689. }
  690. static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)
  691. {
  692. switch (ctrl) {
  693. case PR_SPEC_ENABLE:
  694. if (spectre_v2_user == SPECTRE_V2_USER_NONE)
  695. return 0;
  696. /*
  697. * Indirect branch speculation is always disabled in strict
  698. * mode.
  699. */
  700. if (spectre_v2_user == SPECTRE_V2_USER_STRICT)
  701. return -EPERM;
  702. task_clear_spec_ib_disable(task);
  703. task_update_spec_tif(task);
  704. break;
  705. case PR_SPEC_DISABLE:
  706. case PR_SPEC_FORCE_DISABLE:
  707. /*
  708. * Indirect branch speculation is always allowed when
  709. * mitigation is force disabled.
  710. */
  711. if (spectre_v2_user == SPECTRE_V2_USER_NONE)
  712. return -EPERM;
  713. if (spectre_v2_user == SPECTRE_V2_USER_STRICT)
  714. return 0;
  715. task_set_spec_ib_disable(task);
  716. if (ctrl == PR_SPEC_FORCE_DISABLE)
  717. task_set_spec_ib_force_disable(task);
  718. task_update_spec_tif(task);
  719. break;
  720. default:
  721. return -ERANGE;
  722. }
  723. return 0;
  724. }
  725. int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
  726. unsigned long ctrl)
  727. {
  728. switch (which) {
  729. case PR_SPEC_STORE_BYPASS:
  730. return ssb_prctl_set(task, ctrl);
  731. case PR_SPEC_INDIRECT_BRANCH:
  732. return ib_prctl_set(task, ctrl);
  733. default:
  734. return -ENODEV;
  735. }
  736. }
  737. #ifdef CONFIG_SECCOMP
  738. void arch_seccomp_spec_mitigate(struct task_struct *task)
  739. {
  740. if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP)
  741. ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
  742. if (spectre_v2_user == SPECTRE_V2_USER_SECCOMP)
  743. ib_prctl_set(task, PR_SPEC_FORCE_DISABLE);
  744. }
  745. #endif
  746. static int ssb_prctl_get(struct task_struct *task)
  747. {
  748. switch (ssb_mode) {
  749. case SPEC_STORE_BYPASS_DISABLE:
  750. return PR_SPEC_DISABLE;
  751. case SPEC_STORE_BYPASS_SECCOMP:
  752. case SPEC_STORE_BYPASS_PRCTL:
  753. if (task_spec_ssb_force_disable(task))
  754. return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
  755. if (task_spec_ssb_disable(task))
  756. return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
  757. return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
  758. default:
  759. if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
  760. return PR_SPEC_ENABLE;
  761. return PR_SPEC_NOT_AFFECTED;
  762. }
  763. }
  764. static int ib_prctl_get(struct task_struct *task)
  765. {
  766. if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
  767. return PR_SPEC_NOT_AFFECTED;
  768. switch (spectre_v2_user) {
  769. case SPECTRE_V2_USER_NONE:
  770. return PR_SPEC_ENABLE;
  771. case SPECTRE_V2_USER_PRCTL:
  772. case SPECTRE_V2_USER_SECCOMP:
  773. if (task_spec_ib_force_disable(task))
  774. return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
  775. if (task_spec_ib_disable(task))
  776. return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
  777. return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
  778. case SPECTRE_V2_USER_STRICT:
  779. return PR_SPEC_DISABLE;
  780. default:
  781. return PR_SPEC_NOT_AFFECTED;
  782. }
  783. }
  784. int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
  785. {
  786. switch (which) {
  787. case PR_SPEC_STORE_BYPASS:
  788. return ssb_prctl_get(task);
  789. case PR_SPEC_INDIRECT_BRANCH:
  790. return ib_prctl_get(task);
  791. default:
  792. return -ENODEV;
  793. }
  794. }
  795. void x86_spec_ctrl_setup_ap(void)
  796. {
  797. if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
  798. wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
  799. if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
  800. x86_amd_ssb_disable();
  801. }
  802. #undef pr_fmt
  803. #define pr_fmt(fmt) "L1TF: " fmt
  804. /* Default mitigation for L1TF-affected CPUs */
  805. enum l1tf_mitigations l1tf_mitigation __ro_after_init = L1TF_MITIGATION_FLUSH;
  806. #if IS_ENABLED(CONFIG_KVM_INTEL)
  807. EXPORT_SYMBOL_GPL(l1tf_mitigation);
  808. #endif
  809. enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
  810. EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation);
  811. /*
  812. * These CPUs all support 44bits physical address space internally in the
  813. * cache but CPUID can report a smaller number of physical address bits.
  814. *
  815. * The L1TF mitigation uses the top most address bit for the inversion of
  816. * non present PTEs. When the installed memory reaches into the top most
  817. * address bit due to memory holes, which has been observed on machines
  818. * which report 36bits physical address bits and have 32G RAM installed,
  819. * then the mitigation range check in l1tf_select_mitigation() triggers.
  820. * This is a false positive because the mitigation is still possible due to
  821. * the fact that the cache uses 44bit internally. Use the cache bits
  822. * instead of the reported physical bits and adjust them on the affected
  823. * machines to 44bit if the reported bits are less than 44.
  824. */
  825. static void override_cache_bits(struct cpuinfo_x86 *c)
  826. {
  827. if (c->x86 != 6)
  828. return;
  829. switch (c->x86_model) {
  830. case INTEL_FAM6_NEHALEM:
  831. case INTEL_FAM6_WESTMERE:
  832. case INTEL_FAM6_SANDYBRIDGE:
  833. case INTEL_FAM6_IVYBRIDGE:
  834. case INTEL_FAM6_HASWELL_CORE:
  835. case INTEL_FAM6_HASWELL_ULT:
  836. case INTEL_FAM6_HASWELL_GT3E:
  837. case INTEL_FAM6_BROADWELL_CORE:
  838. case INTEL_FAM6_BROADWELL_GT3E:
  839. case INTEL_FAM6_SKYLAKE_MOBILE:
  840. case INTEL_FAM6_SKYLAKE_DESKTOP:
  841. case INTEL_FAM6_KABYLAKE_MOBILE:
  842. case INTEL_FAM6_KABYLAKE_DESKTOP:
  843. if (c->x86_cache_bits < 44)
  844. c->x86_cache_bits = 44;
  845. break;
  846. }
  847. }
  848. static void __init l1tf_select_mitigation(void)
  849. {
  850. u64 half_pa;
  851. if (!boot_cpu_has_bug(X86_BUG_L1TF))
  852. return;
  853. override_cache_bits(&boot_cpu_data);
  854. switch (l1tf_mitigation) {
  855. case L1TF_MITIGATION_OFF:
  856. case L1TF_MITIGATION_FLUSH_NOWARN:
  857. case L1TF_MITIGATION_FLUSH:
  858. break;
  859. case L1TF_MITIGATION_FLUSH_NOSMT:
  860. case L1TF_MITIGATION_FULL:
  861. cpu_smt_disable(false);
  862. break;
  863. case L1TF_MITIGATION_FULL_FORCE:
  864. cpu_smt_disable(true);
  865. break;
  866. }
  867. #if CONFIG_PGTABLE_LEVELS == 2
  868. pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
  869. return;
  870. #endif
  871. half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT;
  872. if (e820__mapped_any(half_pa, ULLONG_MAX - half_pa, E820_TYPE_RAM)) {
  873. pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
  874. pr_info("You may make it effective by booting the kernel with mem=%llu parameter.\n",
  875. half_pa);
  876. pr_info("However, doing so will make a part of your RAM unusable.\n");
  877. pr_info("Reading https://www.kernel.org/doc/html/latest/admin-guide/l1tf.html might help you decide.\n");
  878. return;
  879. }
  880. setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV);
  881. }
  882. static int __init l1tf_cmdline(char *str)
  883. {
  884. if (!boot_cpu_has_bug(X86_BUG_L1TF))
  885. return 0;
  886. if (!str)
  887. return -EINVAL;
  888. if (!strcmp(str, "off"))
  889. l1tf_mitigation = L1TF_MITIGATION_OFF;
  890. else if (!strcmp(str, "flush,nowarn"))
  891. l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOWARN;
  892. else if (!strcmp(str, "flush"))
  893. l1tf_mitigation = L1TF_MITIGATION_FLUSH;
  894. else if (!strcmp(str, "flush,nosmt"))
  895. l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
  896. else if (!strcmp(str, "full"))
  897. l1tf_mitigation = L1TF_MITIGATION_FULL;
  898. else if (!strcmp(str, "full,force"))
  899. l1tf_mitigation = L1TF_MITIGATION_FULL_FORCE;
  900. return 0;
  901. }
  902. early_param("l1tf", l1tf_cmdline);
  903. #undef pr_fmt
  904. #ifdef CONFIG_SYSFS
  905. #define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
  906. #if IS_ENABLED(CONFIG_KVM_INTEL)
  907. static const char * const l1tf_vmx_states[] = {
  908. [VMENTER_L1D_FLUSH_AUTO] = "auto",
  909. [VMENTER_L1D_FLUSH_NEVER] = "vulnerable",
  910. [VMENTER_L1D_FLUSH_COND] = "conditional cache flushes",
  911. [VMENTER_L1D_FLUSH_ALWAYS] = "cache flushes",
  912. [VMENTER_L1D_FLUSH_EPT_DISABLED] = "EPT disabled",
  913. [VMENTER_L1D_FLUSH_NOT_REQUIRED] = "flush not necessary"
  914. };
  915. static ssize_t l1tf_show_state(char *buf)
  916. {
  917. if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO)
  918. return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
  919. if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_EPT_DISABLED ||
  920. (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER &&
  921. sched_smt_active())) {
  922. return sprintf(buf, "%s; VMX: %s\n", L1TF_DEFAULT_MSG,
  923. l1tf_vmx_states[l1tf_vmx_mitigation]);
  924. }
  925. return sprintf(buf, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG,
  926. l1tf_vmx_states[l1tf_vmx_mitigation],
  927. sched_smt_active() ? "vulnerable" : "disabled");
  928. }
  929. #else
  930. static ssize_t l1tf_show_state(char *buf)
  931. {
  932. return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
  933. }
  934. #endif
  935. static char *stibp_state(void)
  936. {
  937. if (spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
  938. return "";
  939. switch (spectre_v2_user) {
  940. case SPECTRE_V2_USER_NONE:
  941. return ", STIBP: disabled";
  942. case SPECTRE_V2_USER_STRICT:
  943. return ", STIBP: forced";
  944. case SPECTRE_V2_USER_PRCTL:
  945. case SPECTRE_V2_USER_SECCOMP:
  946. if (static_key_enabled(&switch_to_cond_stibp))
  947. return ", STIBP: conditional";
  948. }
  949. return "";
  950. }
  951. static char *ibpb_state(void)
  952. {
  953. if (boot_cpu_has(X86_FEATURE_IBPB)) {
  954. if (static_key_enabled(&switch_mm_always_ibpb))
  955. return ", IBPB: always-on";
  956. if (static_key_enabled(&switch_mm_cond_ibpb))
  957. return ", IBPB: conditional";
  958. return ", IBPB: disabled";
  959. }
  960. return "";
  961. }
  962. static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
  963. char *buf, unsigned int bug)
  964. {
  965. if (!boot_cpu_has_bug(bug))
  966. return sprintf(buf, "Not affected\n");
  967. switch (bug) {
  968. case X86_BUG_CPU_MELTDOWN:
  969. if (boot_cpu_has(X86_FEATURE_PTI))
  970. return sprintf(buf, "Mitigation: PTI\n");
  971. if (hypervisor_is_type(X86_HYPER_XEN_PV))
  972. return sprintf(buf, "Unknown (XEN PV detected, hypervisor mitigation required)\n");
  973. break;
  974. case X86_BUG_SPECTRE_V1:
  975. return sprintf(buf, "Mitigation: __user pointer sanitization\n");
  976. case X86_BUG_SPECTRE_V2:
  977. return sprintf(buf, "%s%s%s%s%s%s\n", spectre_v2_strings[spectre_v2_enabled],
  978. ibpb_state(),
  979. boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
  980. stibp_state(),
  981. boot_cpu_has(X86_FEATURE_RSB_CTXSW) ? ", RSB filling" : "",
  982. spectre_v2_module_string());
  983. case X86_BUG_SPEC_STORE_BYPASS:
  984. return sprintf(buf, "%s\n", ssb_strings[ssb_mode]);
  985. case X86_BUG_L1TF:
  986. if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV))
  987. return l1tf_show_state(buf);
  988. break;
  989. default:
  990. break;
  991. }
  992. return sprintf(buf, "Vulnerable\n");
  993. }
  994. ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
  995. {
  996. return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
  997. }
  998. ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
  999. {
  1000. return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
  1001. }
  1002. ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
  1003. {
  1004. return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
  1005. }
  1006. ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
  1007. {
  1008. return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS);
  1009. }
  1010. ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
  1011. {
  1012. return cpu_show_common(dev, attr, buf, X86_BUG_L1TF);
  1013. }
  1014. #endif