bugs.c 35 KB

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