alternative-macros.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ASM_ALTERNATIVE_MACROS_H
  3. #define __ASM_ALTERNATIVE_MACROS_H
  4. #include <linux/const.h>
  5. #include <vdso/bits.h>
  6. #include <asm/cpucaps.h>
  7. #include <asm/insn-def.h>
  8. /*
  9. * Binutils 2.27.0 can't handle a 'UL' suffix on constants, so for the assembly
  10. * macros below we must use we must use `(1 << ARM64_CB_SHIFT)`.
  11. */
  12. #define ARM64_CB_SHIFT 15
  13. #define ARM64_CB_BIT BIT(ARM64_CB_SHIFT)
  14. #if ARM64_NCAPS >= ARM64_CB_BIT
  15. #error "cpucaps have overflown ARM64_CB_BIT"
  16. #endif
  17. #ifndef __ASSEMBLER__
  18. #include <linux/stringify.h>
  19. #define ALTINSTR_ENTRY(cpucap) \
  20. " .word 661b - .\n" /* label */ \
  21. " .word 663f - .\n" /* new instruction */ \
  22. " .hword " __stringify(cpucap) "\n" /* cpucap */ \
  23. " .byte 662b-661b\n" /* source len */ \
  24. " .byte 664f-663f\n" /* replacement len */
  25. #define ALTINSTR_ENTRY_CB(cpucap, cb) \
  26. " .word 661b - .\n" /* label */ \
  27. " .word " __stringify(cb) "- .\n" /* callback */ \
  28. " .hword " __stringify(cpucap) "\n" /* cpucap */ \
  29. " .byte 662b-661b\n" /* source len */ \
  30. " .byte 664f-663f\n" /* replacement len */
  31. /*
  32. * alternative assembly primitive:
  33. *
  34. * If any of these .org directive fail, it means that insn1 and insn2
  35. * don't have the same length. This used to be written as
  36. *
  37. * .if ((664b-663b) != (662b-661b))
  38. * .error "Alternatives instruction length mismatch"
  39. * .endif
  40. *
  41. * but most assemblers die if insn1 or insn2 have a .inst. This should
  42. * be fixed in a binutils release posterior to 2.25.51.0.2 (anything
  43. * containing commit 4e4d08cf7399b606 or c1baaddf8861).
  44. *
  45. * Alternatives with callbacks do not generate replacement instructions.
  46. */
  47. #define __ALTERNATIVE_CFG(oldinstr, newinstr, cpucap, cfg_enabled) \
  48. ".if "__stringify(cfg_enabled)" == 1\n" \
  49. "661:\n\t" \
  50. oldinstr "\n" \
  51. "662:\n" \
  52. ".pushsection .altinstructions,\"a\"\n" \
  53. ALTINSTR_ENTRY(cpucap) \
  54. ".popsection\n" \
  55. ".subsection 1\n" \
  56. "663:\n\t" \
  57. newinstr "\n" \
  58. "664:\n\t" \
  59. ".org . - (664b-663b) + (662b-661b)\n\t" \
  60. ".org . - (662b-661b) + (664b-663b)\n\t" \
  61. ".previous\n" \
  62. ".endif\n"
  63. #define __ALTERNATIVE_CFG_CB(oldinstr, cpucap, cfg_enabled, cb) \
  64. ".if "__stringify(cfg_enabled)" == 1\n" \
  65. "661:\n\t" \
  66. oldinstr "\n" \
  67. "662:\n" \
  68. ".pushsection .altinstructions,\"a\"\n" \
  69. ALTINSTR_ENTRY_CB(cpucap, cb) \
  70. ".popsection\n" \
  71. "663:\n\t" \
  72. "664:\n\t" \
  73. ".endif\n"
  74. #define _ALTERNATIVE_CFG(oldinstr, newinstr, cpucap, cfg, ...) \
  75. __ALTERNATIVE_CFG(oldinstr, newinstr, cpucap, IS_ENABLED(cfg))
  76. #define ALTERNATIVE_CB(oldinstr, cpucap, cb) \
  77. __ALTERNATIVE_CFG_CB(oldinstr, (1 << ARM64_CB_SHIFT) | (cpucap), 1, cb)
  78. #else
  79. #include <asm/assembler.h>
  80. .macro altinstruction_entry orig_offset alt_offset cpucap orig_len alt_len
  81. .word \orig_offset - .
  82. .word \alt_offset - .
  83. .hword (\cpucap)
  84. .byte \orig_len
  85. .byte \alt_len
  86. .endm
  87. .macro alternative_insn insn1, insn2, cap, enable = 1
  88. .if \enable
  89. 661: \insn1
  90. 662: .pushsection .altinstructions, "a"
  91. altinstruction_entry 661b, 663f, \cap, 662b-661b, 664f-663f
  92. .popsection
  93. .subsection 1
  94. 663: \insn2
  95. 664: .org . - (664b-663b) + (662b-661b)
  96. .org . - (662b-661b) + (664b-663b)
  97. .previous
  98. .endif
  99. .endm
  100. /*
  101. * Alternative sequences
  102. *
  103. * The code for the case where the capability is not present will be
  104. * assembled and linked as normal. There are no restrictions on this
  105. * code.
  106. *
  107. * The code for the case where the capability is present will be
  108. * assembled into a special section to be used for dynamic patching.
  109. * Code for that case must:
  110. *
  111. * 1. Be exactly the same length (in bytes) as the default code
  112. * sequence.
  113. *
  114. * 2. Not contain a branch target that is used outside of the
  115. * alternative sequence it is defined in (branches into an
  116. * alternative sequence are not fixed up).
  117. */
  118. /*
  119. * Begin an alternative code sequence.
  120. */
  121. .macro alternative_if_not cap
  122. .set .Lasm_alt_mode, 0
  123. .pushsection .altinstructions, "a"
  124. altinstruction_entry 661f, 663f, \cap, 662f-661f, 664f-663f
  125. .popsection
  126. 661:
  127. .endm
  128. .macro alternative_if cap
  129. .set .Lasm_alt_mode, 1
  130. .pushsection .altinstructions, "a"
  131. altinstruction_entry 663f, 661f, \cap, 664f-663f, 662f-661f
  132. .popsection
  133. .subsection 1
  134. .align 2 /* So GAS knows label 661 is suitably aligned */
  135. 661:
  136. .endm
  137. .macro alternative_cb cap, cb
  138. .set .Lasm_alt_mode, 0
  139. .pushsection .altinstructions, "a"
  140. altinstruction_entry 661f, \cb, (1 << ARM64_CB_SHIFT) | \cap, 662f-661f, 0
  141. .popsection
  142. 661:
  143. .endm
  144. /*
  145. * Provide the other half of the alternative code sequence.
  146. */
  147. .macro alternative_else
  148. 662:
  149. .if .Lasm_alt_mode==0
  150. .subsection 1
  151. .else
  152. .previous
  153. .endif
  154. 663:
  155. .endm
  156. /*
  157. * Complete an alternative code sequence.
  158. */
  159. .macro alternative_endif
  160. 664:
  161. .org . - (664b-663b) + (662b-661b)
  162. .org . - (662b-661b) + (664b-663b)
  163. .if .Lasm_alt_mode==0
  164. .previous
  165. .endif
  166. .endm
  167. /*
  168. * Callback-based alternative epilogue
  169. */
  170. .macro alternative_cb_end
  171. 662:
  172. .endm
  173. /*
  174. * Provides a trivial alternative or default sequence consisting solely
  175. * of NOPs. The number of NOPs is chosen automatically to match the
  176. * previous case.
  177. */
  178. .macro alternative_else_nop_endif
  179. alternative_else
  180. nops (662b-661b) / AARCH64_INSN_SIZE
  181. alternative_endif
  182. .endm
  183. #define _ALTERNATIVE_CFG(insn1, insn2, cap, cfg, ...) \
  184. alternative_insn insn1, insn2, cap, IS_ENABLED(cfg)
  185. #endif /* __ASSEMBLER__ */
  186. /*
  187. * Usage: asm(ALTERNATIVE(oldinstr, newinstr, cpucap));
  188. *
  189. * Usage: asm(ALTERNATIVE(oldinstr, newinstr, cpucap, CONFIG_FOO));
  190. * N.B. If CONFIG_FOO is specified, but not selected, the whole block
  191. * will be omitted, including oldinstr.
  192. */
  193. #define ALTERNATIVE(oldinstr, newinstr, ...) \
  194. _ALTERNATIVE_CFG(oldinstr, newinstr, __VA_ARGS__, 1)
  195. #ifndef __ASSEMBLER__
  196. #include <linux/types.h>
  197. static __always_inline bool
  198. alternative_has_cap_likely(const unsigned long cpucap)
  199. {
  200. if (!cpucap_is_possible(cpucap))
  201. return false;
  202. asm goto(
  203. #ifdef BUILD_VDSO
  204. ALTERNATIVE("b %l[l_no]", "nop", %[cpucap])
  205. #else
  206. ALTERNATIVE_CB("b %l[l_no]", %[cpucap], alt_cb_patch_nops)
  207. #endif
  208. :
  209. : [cpucap] "i" (cpucap)
  210. :
  211. : l_no);
  212. return true;
  213. l_no:
  214. return false;
  215. }
  216. static __always_inline bool
  217. alternative_has_cap_unlikely(const unsigned long cpucap)
  218. {
  219. if (!cpucap_is_possible(cpucap))
  220. return false;
  221. asm goto(
  222. ALTERNATIVE("nop", "b %l[l_yes]", %[cpucap])
  223. :
  224. : [cpucap] "i" (cpucap)
  225. :
  226. : l_yes);
  227. return false;
  228. l_yes:
  229. return true;
  230. }
  231. #endif /* __ASSEMBLER__ */
  232. #endif /* __ASM_ALTERNATIVE_MACROS_H */