nospec-branch.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 <asm/alternative.h>
  6. #include <asm/alternative-asm.h>
  7. #include <asm/cpufeatures.h>
  8. #include <asm/msr-index.h>
  9. /*
  10. * Fill the CPU return stack buffer.
  11. *
  12. * Each entry in the RSB, if used for a speculative 'ret', contains an
  13. * infinite 'pause; lfence; jmp' loop to capture speculative execution.
  14. *
  15. * This is required in various cases for retpoline and IBRS-based
  16. * mitigations for the Spectre variant 2 vulnerability. Sometimes to
  17. * eliminate potentially bogus entries from the RSB, and sometimes
  18. * purely to ensure that it doesn't get empty, which on some CPUs would
  19. * allow predictions from other (unwanted!) sources to be used.
  20. *
  21. * We define a CPP macro such that it can be used from both .S files and
  22. * inline assembly. It's possible to do a .macro and then include that
  23. * from C via asm(".include <asm/nospec-branch.h>") but let's not go there.
  24. */
  25. #define RSB_CLEAR_LOOPS 32 /* To forcibly overwrite all entries */
  26. #define RSB_FILL_LOOPS 16 /* To avoid underflow */
  27. /*
  28. * Google experimented with loop-unrolling and this turned out to be
  29. * the optimal version — two calls, each with their own speculation
  30. * trap should their return address end up getting used, in a loop.
  31. */
  32. #define __FILL_RETURN_BUFFER(reg, nr, sp) \
  33. mov $(nr/2), reg; \
  34. 771: \
  35. call 772f; \
  36. 773: /* speculation trap */ \
  37. pause; \
  38. lfence; \
  39. jmp 773b; \
  40. 772: \
  41. call 774f; \
  42. 775: /* speculation trap */ \
  43. pause; \
  44. lfence; \
  45. jmp 775b; \
  46. 774: \
  47. dec reg; \
  48. jnz 771b; \
  49. add $(BITS_PER_LONG/8) * nr, sp;
  50. #ifdef __ASSEMBLY__
  51. /*
  52. * This should be used immediately before a retpoline alternative. It tells
  53. * objtool where the retpolines are so that it can make sense of the control
  54. * flow by just reading the original instruction(s) and ignoring the
  55. * alternatives.
  56. */
  57. .macro ANNOTATE_NOSPEC_ALTERNATIVE
  58. .Lannotate_\@:
  59. .pushsection .discard.nospec
  60. .long .Lannotate_\@ - .
  61. .popsection
  62. .endm
  63. /*
  64. * This should be used immediately before an indirect jump/call. It tells
  65. * objtool the subsequent indirect jump/call is vouched safe for retpoline
  66. * builds.
  67. */
  68. .macro ANNOTATE_RETPOLINE_SAFE
  69. .Lannotate_\@:
  70. .pushsection .discard.retpoline_safe
  71. _ASM_PTR .Lannotate_\@
  72. .popsection
  73. .endm
  74. /*
  75. * These are the bare retpoline primitives for indirect jmp and call.
  76. * Do not use these directly; they only exist to make the ALTERNATIVE
  77. * invocation below less ugly.
  78. */
  79. .macro RETPOLINE_JMP reg:req
  80. call .Ldo_rop_\@
  81. .Lspec_trap_\@:
  82. pause
  83. lfence
  84. jmp .Lspec_trap_\@
  85. .Ldo_rop_\@:
  86. mov \reg, (%_ASM_SP)
  87. ret
  88. .endm
  89. /*
  90. * This is a wrapper around RETPOLINE_JMP so the called function in reg
  91. * returns to the instruction after the macro.
  92. */
  93. .macro RETPOLINE_CALL reg:req
  94. jmp .Ldo_call_\@
  95. .Ldo_retpoline_jmp_\@:
  96. RETPOLINE_JMP \reg
  97. .Ldo_call_\@:
  98. call .Ldo_retpoline_jmp_\@
  99. .endm
  100. /*
  101. * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
  102. * indirect jmp/call which may be susceptible to the Spectre variant 2
  103. * attack.
  104. */
  105. .macro JMP_NOSPEC reg:req
  106. #ifdef CONFIG_RETPOLINE
  107. ANNOTATE_NOSPEC_ALTERNATIVE
  108. ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; jmp *\reg), \
  109. __stringify(RETPOLINE_JMP \reg), X86_FEATURE_RETPOLINE, \
  110. __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; jmp *\reg), X86_FEATURE_RETPOLINE_AMD
  111. #else
  112. jmp *\reg
  113. #endif
  114. .endm
  115. .macro CALL_NOSPEC reg:req
  116. #ifdef CONFIG_RETPOLINE
  117. ANNOTATE_NOSPEC_ALTERNATIVE
  118. ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; call *\reg), \
  119. __stringify(RETPOLINE_CALL \reg), X86_FEATURE_RETPOLINE,\
  120. __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; call *\reg), X86_FEATURE_RETPOLINE_AMD
  121. #else
  122. call *\reg
  123. #endif
  124. .endm
  125. /*
  126. * A simpler FILL_RETURN_BUFFER macro. Don't make people use the CPP
  127. * monstrosity above, manually.
  128. */
  129. .macro FILL_RETURN_BUFFER reg:req nr:req ftr:req
  130. #ifdef CONFIG_RETPOLINE
  131. ANNOTATE_NOSPEC_ALTERNATIVE
  132. ALTERNATIVE "jmp .Lskip_rsb_\@", \
  133. __stringify(__FILL_RETURN_BUFFER(\reg,\nr,%_ASM_SP)) \
  134. \ftr
  135. .Lskip_rsb_\@:
  136. #endif
  137. .endm
  138. #else /* __ASSEMBLY__ */
  139. #define ANNOTATE_NOSPEC_ALTERNATIVE \
  140. "999:\n\t" \
  141. ".pushsection .discard.nospec\n\t" \
  142. ".long 999b - .\n\t" \
  143. ".popsection\n\t"
  144. #define ANNOTATE_RETPOLINE_SAFE \
  145. "999:\n\t" \
  146. ".pushsection .discard.retpoline_safe\n\t" \
  147. _ASM_PTR " 999b\n\t" \
  148. ".popsection\n\t"
  149. #ifdef CONFIG_RETPOLINE
  150. #ifdef CONFIG_X86_64
  151. /*
  152. * Inline asm uses the %V modifier which is only in newer GCC
  153. * which is ensured when CONFIG_RETPOLINE is defined.
  154. */
  155. # define CALL_NOSPEC \
  156. ANNOTATE_NOSPEC_ALTERNATIVE \
  157. ALTERNATIVE_2( \
  158. ANNOTATE_RETPOLINE_SAFE \
  159. "call *%[thunk_target]\n", \
  160. "call __x86_indirect_thunk_%V[thunk_target]\n", \
  161. X86_FEATURE_RETPOLINE, \
  162. "lfence;\n" \
  163. ANNOTATE_RETPOLINE_SAFE \
  164. "call *%[thunk_target]\n", \
  165. X86_FEATURE_RETPOLINE_AMD)
  166. # define THUNK_TARGET(addr) [thunk_target] "r" (addr)
  167. #else /* CONFIG_X86_32 */
  168. /*
  169. * For i386 we use the original ret-equivalent retpoline, because
  170. * otherwise we'll run out of registers. We don't care about CET
  171. * here, anyway.
  172. */
  173. # define CALL_NOSPEC \
  174. ANNOTATE_NOSPEC_ALTERNATIVE \
  175. ALTERNATIVE_2( \
  176. ANNOTATE_RETPOLINE_SAFE \
  177. "call *%[thunk_target]\n", \
  178. " jmp 904f;\n" \
  179. " .align 16\n" \
  180. "901: call 903f;\n" \
  181. "902: pause;\n" \
  182. " lfence;\n" \
  183. " jmp 902b;\n" \
  184. " .align 16\n" \
  185. "903: addl $4, %%esp;\n" \
  186. " pushl %[thunk_target];\n" \
  187. " ret;\n" \
  188. " .align 16\n" \
  189. "904: call 901b;\n", \
  190. X86_FEATURE_RETPOLINE, \
  191. "lfence;\n" \
  192. ANNOTATE_RETPOLINE_SAFE \
  193. "call *%[thunk_target]\n", \
  194. X86_FEATURE_RETPOLINE_AMD)
  195. # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
  196. #endif
  197. #else /* No retpoline for C / inline asm */
  198. # define CALL_NOSPEC "call *%[thunk_target]\n"
  199. # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
  200. #endif
  201. /* The Spectre V2 mitigation variants */
  202. enum spectre_v2_mitigation {
  203. SPECTRE_V2_NONE,
  204. SPECTRE_V2_RETPOLINE_GENERIC,
  205. SPECTRE_V2_RETPOLINE_AMD,
  206. SPECTRE_V2_IBRS_ENHANCED,
  207. };
  208. /* The indirect branch speculation control variants */
  209. enum spectre_v2_user_mitigation {
  210. SPECTRE_V2_USER_NONE,
  211. SPECTRE_V2_USER_STRICT,
  212. SPECTRE_V2_USER_PRCTL,
  213. SPECTRE_V2_USER_SECCOMP,
  214. };
  215. /* The Speculative Store Bypass disable variants */
  216. enum ssb_mitigation {
  217. SPEC_STORE_BYPASS_NONE,
  218. SPEC_STORE_BYPASS_DISABLE,
  219. SPEC_STORE_BYPASS_PRCTL,
  220. SPEC_STORE_BYPASS_SECCOMP,
  221. };
  222. extern char __indirect_thunk_start[];
  223. extern char __indirect_thunk_end[];
  224. /*
  225. * On VMEXIT we must ensure that no RSB predictions learned in the guest
  226. * can be followed in the host, by overwriting the RSB completely. Both
  227. * retpoline and IBRS mitigations for Spectre v2 need this; only on future
  228. * CPUs with IBRS_ALL *might* it be avoided.
  229. */
  230. static inline void vmexit_fill_RSB(void)
  231. {
  232. #ifdef CONFIG_RETPOLINE
  233. unsigned long loops;
  234. asm volatile (ANNOTATE_NOSPEC_ALTERNATIVE
  235. ALTERNATIVE("jmp 910f",
  236. __stringify(__FILL_RETURN_BUFFER(%0, RSB_CLEAR_LOOPS, %1)),
  237. X86_FEATURE_RETPOLINE)
  238. "910:"
  239. : "=r" (loops), ASM_CALL_CONSTRAINT
  240. : : "memory" );
  241. #endif
  242. }
  243. static __always_inline
  244. void alternative_msr_write(unsigned int msr, u64 val, unsigned int feature)
  245. {
  246. asm volatile(ALTERNATIVE("", "wrmsr", %c[feature])
  247. : : "c" (msr),
  248. "a" ((u32)val),
  249. "d" ((u32)(val >> 32)),
  250. [feature] "i" (feature)
  251. : "memory");
  252. }
  253. static inline void indirect_branch_prediction_barrier(void)
  254. {
  255. u64 val = PRED_CMD_IBPB;
  256. alternative_msr_write(MSR_IA32_PRED_CMD, val, X86_FEATURE_USE_IBPB);
  257. }
  258. /* The Intel SPEC CTRL MSR base value cache */
  259. extern u64 x86_spec_ctrl_base;
  260. /*
  261. * With retpoline, we must use IBRS to restrict branch prediction
  262. * before calling into firmware.
  263. *
  264. * (Implemented as CPP macros due to header hell.)
  265. */
  266. #define firmware_restrict_branch_speculation_start() \
  267. do { \
  268. u64 val = x86_spec_ctrl_base | SPEC_CTRL_IBRS; \
  269. \
  270. preempt_disable(); \
  271. alternative_msr_write(MSR_IA32_SPEC_CTRL, val, \
  272. X86_FEATURE_USE_IBRS_FW); \
  273. } while (0)
  274. #define firmware_restrict_branch_speculation_end() \
  275. do { \
  276. u64 val = x86_spec_ctrl_base; \
  277. \
  278. alternative_msr_write(MSR_IA32_SPEC_CTRL, val, \
  279. X86_FEATURE_USE_IBRS_FW); \
  280. preempt_enable(); \
  281. } while (0)
  282. DECLARE_STATIC_KEY_FALSE(switch_to_cond_stibp);
  283. DECLARE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
  284. DECLARE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
  285. DECLARE_STATIC_KEY_FALSE(mds_user_clear);
  286. #include <asm/segment.h>
  287. /**
  288. * mds_clear_cpu_buffers - Mitigation for MDS vulnerability
  289. *
  290. * This uses the otherwise unused and obsolete VERW instruction in
  291. * combination with microcode which triggers a CPU buffer flush when the
  292. * instruction is executed.
  293. */
  294. static inline void mds_clear_cpu_buffers(void)
  295. {
  296. static const u16 ds = __KERNEL_DS;
  297. /*
  298. * Has to be the memory-operand variant because only that
  299. * guarantees the CPU buffer flush functionality according to
  300. * documentation. The register-operand variant does not.
  301. * Works with any segment selector, but a valid writable
  302. * data segment is the fastest variant.
  303. *
  304. * "cc" clobber is required because VERW modifies ZF.
  305. */
  306. asm volatile("verw %[ds]" : : [ds] "m" (ds) : "cc");
  307. }
  308. /**
  309. * mds_user_clear_cpu_buffers - Mitigation for MDS vulnerability
  310. *
  311. * Clear CPU buffers if the corresponding static key is enabled
  312. */
  313. static inline void mds_user_clear_cpu_buffers(void)
  314. {
  315. if (static_branch_likely(&mds_user_clear))
  316. mds_clear_cpu_buffers();
  317. }
  318. #endif /* __ASSEMBLY__ */
  319. /*
  320. * Below is used in the eBPF JIT compiler and emits the byte sequence
  321. * for the following assembly:
  322. *
  323. * With retpolines configured:
  324. *
  325. * callq do_rop
  326. * spec_trap:
  327. * pause
  328. * lfence
  329. * jmp spec_trap
  330. * do_rop:
  331. * mov %rax,(%rsp) for x86_64
  332. * mov %edx,(%esp) for x86_32
  333. * retq
  334. *
  335. * Without retpolines configured:
  336. *
  337. * jmp *%rax for x86_64
  338. * jmp *%edx for x86_32
  339. */
  340. #ifdef CONFIG_RETPOLINE
  341. # ifdef CONFIG_X86_64
  342. # define RETPOLINE_RAX_BPF_JIT_SIZE 17
  343. # define RETPOLINE_RAX_BPF_JIT() \
  344. do { \
  345. EMIT1_off32(0xE8, 7); /* callq do_rop */ \
  346. /* spec_trap: */ \
  347. EMIT2(0xF3, 0x90); /* pause */ \
  348. EMIT3(0x0F, 0xAE, 0xE8); /* lfence */ \
  349. EMIT2(0xEB, 0xF9); /* jmp spec_trap */ \
  350. /* do_rop: */ \
  351. EMIT4(0x48, 0x89, 0x04, 0x24); /* mov %rax,(%rsp) */ \
  352. EMIT1(0xC3); /* retq */ \
  353. } while (0)
  354. # else /* !CONFIG_X86_64 */
  355. # define RETPOLINE_EDX_BPF_JIT() \
  356. do { \
  357. EMIT1_off32(0xE8, 7); /* call do_rop */ \
  358. /* spec_trap: */ \
  359. EMIT2(0xF3, 0x90); /* pause */ \
  360. EMIT3(0x0F, 0xAE, 0xE8); /* lfence */ \
  361. EMIT2(0xEB, 0xF9); /* jmp spec_trap */ \
  362. /* do_rop: */ \
  363. EMIT3(0x89, 0x14, 0x24); /* mov %edx,(%esp) */ \
  364. EMIT1(0xC3); /* ret */ \
  365. } while (0)
  366. # endif
  367. #else /* !CONFIG_RETPOLINE */
  368. # ifdef CONFIG_X86_64
  369. # define RETPOLINE_RAX_BPF_JIT_SIZE 2
  370. # define RETPOLINE_RAX_BPF_JIT() \
  371. EMIT2(0xFF, 0xE0); /* jmp *%rax */
  372. # else /* !CONFIG_X86_64 */
  373. # define RETPOLINE_EDX_BPF_JIT() \
  374. EMIT2(0xFF, 0xE2) /* jmp *%edx */
  375. # endif
  376. #endif
  377. #endif /* _ASM_X86_NOSPEC_BRANCH_H_ */