nospec-branch.h 9.2 KB

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