nospec-branch.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_NOSPEC_BRANCH_H_
  3. #define _ASM_X86_NOSPEC_BRANCH_H_
  4. #include <linux/static_key.h>
  5. #include <linux/objtool.h>
  6. #include <linux/linkage.h>
  7. #include <asm/alternative.h>
  8. #include <asm/cpufeatures.h>
  9. #include <asm/msr-index.h>
  10. #include <asm/unwind_hints.h>
  11. #include <asm/percpu.h>
  12. #define RETPOLINE_THUNK_SIZE 32
  13. /*
  14. * Fill the CPU return stack buffer.
  15. *
  16. * Each entry in the RSB, if used for a speculative 'ret', contains an
  17. * infinite 'pause; lfence; jmp' loop to capture speculative execution.
  18. *
  19. * This is required in various cases for retpoline and IBRS-based
  20. * mitigations for the Spectre variant 2 vulnerability. Sometimes to
  21. * eliminate potentially bogus entries from the RSB, and sometimes
  22. * purely to ensure that it doesn't get empty, which on some CPUs would
  23. * allow predictions from other (unwanted!) sources to be used.
  24. *
  25. * We define a CPP macro such that it can be used from both .S files and
  26. * inline assembly. It's possible to do a .macro and then include that
  27. * from C via asm(".include <asm/nospec-branch.h>") but let's not go there.
  28. */
  29. #define RSB_CLEAR_LOOPS 32 /* To forcibly overwrite all entries */
  30. /*
  31. * Common helper for __FILL_RETURN_BUFFER and __FILL_ONE_RETURN.
  32. */
  33. #define __FILL_RETURN_SLOT \
  34. ANNOTATE_INTRA_FUNCTION_CALL; \
  35. call 772f; \
  36. int3; \
  37. 772:
  38. /*
  39. * Stuff the entire RSB.
  40. *
  41. * Google experimented with loop-unrolling and this turned out to be
  42. * the optimal version - two calls, each with their own speculation
  43. * trap should their return address end up getting used, in a loop.
  44. */
  45. #ifdef CONFIG_X86_64
  46. #define __FILL_RETURN_BUFFER(reg, nr) \
  47. mov $(nr/2), reg; \
  48. 771: \
  49. __FILL_RETURN_SLOT \
  50. __FILL_RETURN_SLOT \
  51. add $(BITS_PER_LONG/8) * 2, %_ASM_SP; \
  52. dec reg; \
  53. jnz 771b; \
  54. /* barrier for jnz misprediction */ \
  55. lfence;
  56. #else
  57. /*
  58. * i386 doesn't unconditionally have LFENCE, as such it can't
  59. * do a loop.
  60. */
  61. #define __FILL_RETURN_BUFFER(reg, nr) \
  62. .rept nr; \
  63. __FILL_RETURN_SLOT; \
  64. .endr; \
  65. add $(BITS_PER_LONG/8) * nr, %_ASM_SP;
  66. #endif
  67. /*
  68. * Stuff a single RSB slot.
  69. *
  70. * To mitigate Post-Barrier RSB speculation, one CALL instruction must be
  71. * forced to retire before letting a RET instruction execute.
  72. *
  73. * On PBRSB-vulnerable CPUs, it is not safe for a RET to be executed
  74. * before this point.
  75. */
  76. #define __FILL_ONE_RETURN \
  77. __FILL_RETURN_SLOT \
  78. add $(BITS_PER_LONG/8), %_ASM_SP; \
  79. lfence;
  80. #ifdef __ASSEMBLY__
  81. /*
  82. * This should be used immediately before an indirect jump/call. It tells
  83. * objtool the subsequent indirect jump/call is vouched safe for retpoline
  84. * builds.
  85. */
  86. .macro ANNOTATE_RETPOLINE_SAFE
  87. .Lannotate_\@:
  88. .pushsection .discard.retpoline_safe
  89. _ASM_PTR .Lannotate_\@
  90. .popsection
  91. .endm
  92. /*
  93. * (ab)use RETPOLINE_SAFE on RET to annotate away 'bare' RET instructions
  94. * vs RETBleed validation.
  95. */
  96. #define ANNOTATE_UNRET_SAFE ANNOTATE_RETPOLINE_SAFE
  97. /*
  98. * Abuse ANNOTATE_RETPOLINE_SAFE on a NOP to indicate UNRET_END, should
  99. * eventually turn into it's own annotation.
  100. */
  101. .macro ANNOTATE_UNRET_END
  102. #if (defined(CONFIG_CPU_UNRET_ENTRY) || defined(CONFIG_CPU_SRSO))
  103. ANNOTATE_RETPOLINE_SAFE
  104. nop
  105. #endif
  106. .endm
  107. /*
  108. * Equivalent to -mindirect-branch-cs-prefix; emit the 5 byte jmp/call
  109. * to the retpoline thunk with a CS prefix when the register requires
  110. * a RAX prefix byte to encode. Also see apply_retpolines().
  111. */
  112. .macro __CS_PREFIX reg:req
  113. .irp rs,r8,r9,r10,r11,r12,r13,r14,r15
  114. .ifc \reg,\rs
  115. .byte 0x2e
  116. .endif
  117. .endr
  118. .endm
  119. /*
  120. * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
  121. * indirect jmp/call which may be susceptible to the Spectre variant 2
  122. * attack.
  123. */
  124. .macro JMP_NOSPEC reg:req
  125. #ifdef CONFIG_RETPOLINE
  126. __CS_PREFIX \reg
  127. jmp __x86_indirect_thunk_\reg
  128. #else
  129. jmp *%\reg
  130. int3
  131. #endif
  132. .endm
  133. .macro CALL_NOSPEC reg:req
  134. #ifdef CONFIG_RETPOLINE
  135. __CS_PREFIX \reg
  136. call __x86_indirect_thunk_\reg
  137. #else
  138. call *%\reg
  139. #endif
  140. .endm
  141. /*
  142. * A simpler FILL_RETURN_BUFFER macro. Don't make people use the CPP
  143. * monstrosity above, manually.
  144. */
  145. .macro FILL_RETURN_BUFFER reg:req nr:req ftr:req ftr2=ALT_NOT(X86_FEATURE_ALWAYS)
  146. ALTERNATIVE_2 "jmp .Lskip_rsb_\@", \
  147. __stringify(__FILL_RETURN_BUFFER(\reg,\nr)), \ftr, \
  148. __stringify(__FILL_ONE_RETURN), \ftr2
  149. .Lskip_rsb_\@:
  150. .endm
  151. #ifdef CONFIG_CPU_UNRET_ENTRY
  152. #define CALL_UNTRAIN_RET "call entry_untrain_ret"
  153. #else
  154. #define CALL_UNTRAIN_RET ""
  155. #endif
  156. /*
  157. * Mitigate RETBleed for AMD/Hygon Zen uarch. Requires KERNEL CR3 because the
  158. * return thunk isn't mapped into the userspace tables (then again, AMD
  159. * typically has NO_MELTDOWN).
  160. *
  161. * While retbleed_untrain_ret() doesn't clobber anything but requires stack,
  162. * entry_ibpb() will clobber AX, CX, DX.
  163. *
  164. * As such, this must be placed after every *SWITCH_TO_KERNEL_CR3 at a point
  165. * where we have a stack but before any RET instruction.
  166. */
  167. .macro UNTRAIN_RET
  168. #if defined(CONFIG_CPU_UNRET_ENTRY) || defined(CONFIG_CPU_IBPB_ENTRY) || \
  169. defined(CONFIG_CPU_SRSO)
  170. ANNOTATE_UNRET_END
  171. ALTERNATIVE_2 "", \
  172. CALL_UNTRAIN_RET, X86_FEATURE_UNRET, \
  173. "call entry_ibpb", X86_FEATURE_ENTRY_IBPB
  174. #endif
  175. .endm
  176. #else /* __ASSEMBLY__ */
  177. #define ANNOTATE_RETPOLINE_SAFE \
  178. "999:\n\t" \
  179. ".pushsection .discard.retpoline_safe\n\t" \
  180. _ASM_PTR " 999b\n\t" \
  181. ".popsection\n\t"
  182. typedef u8 retpoline_thunk_t[RETPOLINE_THUNK_SIZE];
  183. extern retpoline_thunk_t __x86_indirect_thunk_array[];
  184. #ifdef CONFIG_RETHUNK
  185. extern void __x86_return_thunk(void);
  186. #else
  187. static inline void __x86_return_thunk(void) {}
  188. #endif
  189. extern void retbleed_return_thunk(void);
  190. extern void srso_return_thunk(void);
  191. extern void srso_alias_return_thunk(void);
  192. extern void retbleed_untrain_ret(void);
  193. extern void srso_untrain_ret(void);
  194. extern void srso_alias_untrain_ret(void);
  195. extern void entry_untrain_ret(void);
  196. extern void entry_ibpb(void);
  197. #ifdef CONFIG_RETPOLINE
  198. #define GEN(reg) \
  199. extern retpoline_thunk_t __x86_indirect_thunk_ ## reg;
  200. #include <asm/GEN-for-each-reg.h>
  201. #undef GEN
  202. #ifdef CONFIG_X86_64
  203. /*
  204. * Inline asm uses the %V modifier which is only in newer GCC
  205. * which is ensured when CONFIG_RETPOLINE is defined.
  206. */
  207. # define CALL_NOSPEC \
  208. ALTERNATIVE_2( \
  209. ANNOTATE_RETPOLINE_SAFE \
  210. "call *%[thunk_target]\n", \
  211. "call __x86_indirect_thunk_%V[thunk_target]\n", \
  212. X86_FEATURE_RETPOLINE, \
  213. "lfence;\n" \
  214. ANNOTATE_RETPOLINE_SAFE \
  215. "call *%[thunk_target]\n", \
  216. X86_FEATURE_RETPOLINE_LFENCE)
  217. # define THUNK_TARGET(addr) [thunk_target] "r" (addr)
  218. #else /* CONFIG_X86_32 */
  219. /*
  220. * For i386 we use the original ret-equivalent retpoline, because
  221. * otherwise we'll run out of registers. We don't care about CET
  222. * here, anyway.
  223. */
  224. # define CALL_NOSPEC \
  225. ALTERNATIVE_2( \
  226. ANNOTATE_RETPOLINE_SAFE \
  227. "call *%[thunk_target]\n", \
  228. " jmp 904f;\n" \
  229. " .align 16\n" \
  230. "901: call 903f;\n" \
  231. "902: pause;\n" \
  232. " lfence;\n" \
  233. " jmp 902b;\n" \
  234. " .align 16\n" \
  235. "903: lea 4(%%esp), %%esp;\n" \
  236. " pushl %[thunk_target];\n" \
  237. " ret;\n" \
  238. " .align 16\n" \
  239. "904: call 901b;\n", \
  240. X86_FEATURE_RETPOLINE, \
  241. "lfence;\n" \
  242. ANNOTATE_RETPOLINE_SAFE \
  243. "call *%[thunk_target]\n", \
  244. X86_FEATURE_RETPOLINE_LFENCE)
  245. # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
  246. #endif
  247. #else /* No retpoline for C / inline asm */
  248. # define CALL_NOSPEC "call *%[thunk_target]\n"
  249. # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
  250. #endif
  251. /* The Spectre V2 mitigation variants */
  252. enum spectre_v2_mitigation {
  253. SPECTRE_V2_NONE,
  254. SPECTRE_V2_RETPOLINE,
  255. SPECTRE_V2_LFENCE,
  256. SPECTRE_V2_EIBRS,
  257. SPECTRE_V2_EIBRS_RETPOLINE,
  258. SPECTRE_V2_EIBRS_LFENCE,
  259. SPECTRE_V2_IBRS,
  260. };
  261. /* The indirect branch speculation control variants */
  262. enum spectre_v2_user_mitigation {
  263. SPECTRE_V2_USER_NONE,
  264. SPECTRE_V2_USER_STRICT,
  265. SPECTRE_V2_USER_STRICT_PREFERRED,
  266. SPECTRE_V2_USER_PRCTL,
  267. SPECTRE_V2_USER_SECCOMP,
  268. };
  269. /* The Speculative Store Bypass disable variants */
  270. enum ssb_mitigation {
  271. SPEC_STORE_BYPASS_NONE,
  272. SPEC_STORE_BYPASS_DISABLE,
  273. SPEC_STORE_BYPASS_PRCTL,
  274. SPEC_STORE_BYPASS_SECCOMP,
  275. };
  276. extern char __indirect_thunk_start[];
  277. extern char __indirect_thunk_end[];
  278. static __always_inline
  279. void alternative_msr_write(unsigned int msr, u64 val, unsigned int feature)
  280. {
  281. asm volatile(ALTERNATIVE("", "wrmsr", %c[feature])
  282. : : "c" (msr),
  283. "a" ((u32)val),
  284. "d" ((u32)(val >> 32)),
  285. [feature] "i" (feature)
  286. : "memory");
  287. }
  288. extern u64 x86_pred_cmd;
  289. static inline void indirect_branch_prediction_barrier(void)
  290. {
  291. alternative_msr_write(MSR_IA32_PRED_CMD, x86_pred_cmd, X86_FEATURE_USE_IBPB);
  292. }
  293. /* The Intel SPEC CTRL MSR base value cache */
  294. extern u64 x86_spec_ctrl_base;
  295. DECLARE_PER_CPU(u64, x86_spec_ctrl_current);
  296. extern void update_spec_ctrl_cond(u64 val);
  297. extern u64 spec_ctrl_current(void);
  298. /*
  299. * With retpoline, we must use IBRS to restrict branch prediction
  300. * before calling into firmware.
  301. *
  302. * (Implemented as CPP macros due to header hell.)
  303. */
  304. #define firmware_restrict_branch_speculation_start() \
  305. do { \
  306. preempt_disable(); \
  307. alternative_msr_write(MSR_IA32_SPEC_CTRL, \
  308. spec_ctrl_current() | SPEC_CTRL_IBRS, \
  309. X86_FEATURE_USE_IBRS_FW); \
  310. alternative_msr_write(MSR_IA32_PRED_CMD, PRED_CMD_IBPB, \
  311. X86_FEATURE_USE_IBPB_FW); \
  312. } while (0)
  313. #define firmware_restrict_branch_speculation_end() \
  314. do { \
  315. alternative_msr_write(MSR_IA32_SPEC_CTRL, \
  316. spec_ctrl_current(), \
  317. X86_FEATURE_USE_IBRS_FW); \
  318. preempt_enable(); \
  319. } while (0)
  320. DECLARE_STATIC_KEY_FALSE(switch_to_cond_stibp);
  321. DECLARE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
  322. DECLARE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
  323. DECLARE_STATIC_KEY_FALSE(mds_user_clear);
  324. DECLARE_STATIC_KEY_FALSE(mds_idle_clear);
  325. DECLARE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);
  326. DECLARE_STATIC_KEY_FALSE(mmio_stale_data_clear);
  327. #include <asm/segment.h>
  328. /**
  329. * mds_clear_cpu_buffers - Mitigation for MDS and TAA vulnerability
  330. *
  331. * This uses the otherwise unused and obsolete VERW instruction in
  332. * combination with microcode which triggers a CPU buffer flush when the
  333. * instruction is executed.
  334. */
  335. static __always_inline void mds_clear_cpu_buffers(void)
  336. {
  337. static const u16 ds = __KERNEL_DS;
  338. /*
  339. * Has to be the memory-operand variant because only that
  340. * guarantees the CPU buffer flush functionality according to
  341. * documentation. The register-operand variant does not.
  342. * Works with any segment selector, but a valid writable
  343. * data segment is the fastest variant.
  344. *
  345. * "cc" clobber is required because VERW modifies ZF.
  346. */
  347. asm volatile("verw %[ds]" : : [ds] "m" (ds) : "cc");
  348. }
  349. /**
  350. * mds_user_clear_cpu_buffers - Mitigation for MDS and TAA vulnerability
  351. *
  352. * Clear CPU buffers if the corresponding static key is enabled
  353. */
  354. static __always_inline void mds_user_clear_cpu_buffers(void)
  355. {
  356. if (static_branch_likely(&mds_user_clear))
  357. mds_clear_cpu_buffers();
  358. }
  359. /**
  360. * mds_idle_clear_cpu_buffers - Mitigation for MDS vulnerability
  361. *
  362. * Clear CPU buffers if the corresponding static key is enabled
  363. */
  364. static inline void mds_idle_clear_cpu_buffers(void)
  365. {
  366. if (static_branch_likely(&mds_idle_clear))
  367. mds_clear_cpu_buffers();
  368. }
  369. #endif /* __ASSEMBLY__ */
  370. #endif /* _ASM_X86_NOSPEC_BRANCH_H_ */