alternative.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ASM_ALTERNATIVE_H
  3. #define __ASM_ALTERNATIVE_H
  4. #include <asm/cpucaps.h>
  5. #include <asm/insn.h>
  6. #define ARM64_CB_PATCH ARM64_NCAPS
  7. #ifndef __ASSEMBLY__
  8. #include <linux/init.h>
  9. #include <linux/types.h>
  10. #include <linux/stddef.h>
  11. #include <linux/stringify.h>
  12. extern int alternatives_applied;
  13. struct alt_instr {
  14. s32 orig_offset; /* offset to original instruction */
  15. s32 alt_offset; /* offset to replacement instruction */
  16. u16 cpufeature; /* cpufeature bit set for replacement */
  17. u8 orig_len; /* size of original instruction(s) */
  18. u8 alt_len; /* size of new instruction(s), <= orig_len */
  19. };
  20. typedef void (*alternative_cb_t)(struct alt_instr *alt,
  21. __le32 *origptr, __le32 *updptr, int nr_inst);
  22. void __init apply_alternatives_all(void);
  23. #ifdef CONFIG_MODULES
  24. void apply_alternatives_module(void *start, size_t length);
  25. #else
  26. static inline void apply_alternatives_module(void *start, size_t length) { }
  27. #endif
  28. #define ALTINSTR_ENTRY(feature) \
  29. " .word 661b - .\n" /* label */ \
  30. " .word 663f - .\n" /* new instruction */ \
  31. " .hword " __stringify(feature) "\n" /* feature bit */ \
  32. " .byte 662b-661b\n" /* source len */ \
  33. " .byte 664f-663f\n" /* replacement len */
  34. #define ALTINSTR_ENTRY_CB(feature, cb) \
  35. " .word 661b - .\n" /* label */ \
  36. " .word " __stringify(cb) "- .\n" /* callback */ \
  37. " .hword " __stringify(feature) "\n" /* feature bit */ \
  38. " .byte 662b-661b\n" /* source len */ \
  39. " .byte 664f-663f\n" /* replacement len */
  40. /*
  41. * alternative assembly primitive:
  42. *
  43. * If any of these .org directive fail, it means that insn1 and insn2
  44. * don't have the same length. This used to be written as
  45. *
  46. * .if ((664b-663b) != (662b-661b))
  47. * .error "Alternatives instruction length mismatch"
  48. * .endif
  49. *
  50. * but most assemblers die if insn1 or insn2 have a .inst. This should
  51. * be fixed in a binutils release posterior to 2.25.51.0.2 (anything
  52. * containing commit 4e4d08cf7399b606 or c1baaddf8861).
  53. *
  54. * Alternatives with callbacks do not generate replacement instructions.
  55. */
  56. #define __ALTERNATIVE_CFG(oldinstr, newinstr, feature, cfg_enabled) \
  57. ".if "__stringify(cfg_enabled)" == 1\n" \
  58. "661:\n\t" \
  59. oldinstr "\n" \
  60. "662:\n" \
  61. ".pushsection .altinstructions,\"a\"\n" \
  62. ALTINSTR_ENTRY(feature) \
  63. ".popsection\n" \
  64. ".subsection 1\n" \
  65. "663:\n\t" \
  66. newinstr "\n" \
  67. "664:\n\t" \
  68. ".org . - (664b-663b) + (662b-661b)\n\t" \
  69. ".org . - (662b-661b) + (664b-663b)\n\t" \
  70. ".previous\n" \
  71. ".endif\n"
  72. #define __ALTERNATIVE_CFG_CB(oldinstr, feature, cfg_enabled, cb) \
  73. ".if "__stringify(cfg_enabled)" == 1\n" \
  74. "661:\n\t" \
  75. oldinstr "\n" \
  76. "662:\n" \
  77. ".pushsection .altinstructions,\"a\"\n" \
  78. ALTINSTR_ENTRY_CB(feature, cb) \
  79. ".popsection\n" \
  80. "663:\n\t" \
  81. "664:\n\t" \
  82. ".endif\n"
  83. #define _ALTERNATIVE_CFG(oldinstr, newinstr, feature, cfg, ...) \
  84. __ALTERNATIVE_CFG(oldinstr, newinstr, feature, IS_ENABLED(cfg))
  85. #define ALTERNATIVE_CB(oldinstr, cb) \
  86. __ALTERNATIVE_CFG_CB(oldinstr, ARM64_CB_PATCH, 1, cb)
  87. #else
  88. #include <asm/assembler.h>
  89. .macro altinstruction_entry orig_offset alt_offset feature orig_len alt_len
  90. .word \orig_offset - .
  91. .word \alt_offset - .
  92. .hword \feature
  93. .byte \orig_len
  94. .byte \alt_len
  95. .endm
  96. .macro alternative_insn insn1, insn2, cap, enable = 1
  97. .if \enable
  98. 661: \insn1
  99. 662: .pushsection .altinstructions, "a"
  100. altinstruction_entry 661b, 663f, \cap, 662b-661b, 664f-663f
  101. .popsection
  102. .subsection 1
  103. 663: \insn2
  104. 664: .org . - (664b-663b) + (662b-661b)
  105. .org . - (662b-661b) + (664b-663b)
  106. .previous
  107. .endif
  108. .endm
  109. /*
  110. * Alternative sequences
  111. *
  112. * The code for the case where the capability is not present will be
  113. * assembled and linked as normal. There are no restrictions on this
  114. * code.
  115. *
  116. * The code for the case where the capability is present will be
  117. * assembled into a special section to be used for dynamic patching.
  118. * Code for that case must:
  119. *
  120. * 1. Be exactly the same length (in bytes) as the default code
  121. * sequence.
  122. *
  123. * 2. Not contain a branch target that is used outside of the
  124. * alternative sequence it is defined in (branches into an
  125. * alternative sequence are not fixed up).
  126. */
  127. /*
  128. * Begin an alternative code sequence.
  129. */
  130. .macro alternative_if_not cap
  131. .set .Lasm_alt_mode, 0
  132. .pushsection .altinstructions, "a"
  133. altinstruction_entry 661f, 663f, \cap, 662f-661f, 664f-663f
  134. .popsection
  135. 661:
  136. .endm
  137. .macro alternative_if cap
  138. .set .Lasm_alt_mode, 1
  139. .pushsection .altinstructions, "a"
  140. altinstruction_entry 663f, 661f, \cap, 664f-663f, 662f-661f
  141. .popsection
  142. .subsection 1
  143. .align 2 /* So GAS knows label 661 is suitably aligned */
  144. 661:
  145. .endm
  146. .macro alternative_cb cb
  147. .set .Lasm_alt_mode, 0
  148. .pushsection .altinstructions, "a"
  149. altinstruction_entry 661f, \cb, ARM64_CB_PATCH, 662f-661f, 0
  150. .popsection
  151. 661:
  152. .endm
  153. /*
  154. * Provide the other half of the alternative code sequence.
  155. */
  156. .macro alternative_else
  157. 662:
  158. .if .Lasm_alt_mode==0
  159. .subsection 1
  160. .else
  161. .previous
  162. .endif
  163. 663:
  164. .endm
  165. /*
  166. * Complete an alternative code sequence.
  167. */
  168. .macro alternative_endif
  169. 664:
  170. .org . - (664b-663b) + (662b-661b)
  171. .org . - (662b-661b) + (664b-663b)
  172. .if .Lasm_alt_mode==0
  173. .previous
  174. .endif
  175. .endm
  176. /*
  177. * Callback-based alternative epilogue
  178. */
  179. .macro alternative_cb_end
  180. 662:
  181. .endm
  182. /*
  183. * Provides a trivial alternative or default sequence consisting solely
  184. * of NOPs. The number of NOPs is chosen automatically to match the
  185. * previous case.
  186. */
  187. .macro alternative_else_nop_endif
  188. alternative_else
  189. nops (662b-661b) / AARCH64_INSN_SIZE
  190. alternative_endif
  191. .endm
  192. #define _ALTERNATIVE_CFG(insn1, insn2, cap, cfg, ...) \
  193. alternative_insn insn1, insn2, cap, IS_ENABLED(cfg)
  194. .macro user_alt, label, oldinstr, newinstr, cond
  195. 9999: alternative_insn "\oldinstr", "\newinstr", \cond
  196. _asm_extable 9999b, \label
  197. .endm
  198. /*
  199. * Generate the assembly for UAO alternatives with exception table entries.
  200. * This is complicated as there is no post-increment or pair versions of the
  201. * unprivileged instructions, and USER() only works for single instructions.
  202. */
  203. #ifdef CONFIG_ARM64_UAO
  204. .macro uao_ldp l, reg1, reg2, addr, post_inc
  205. alternative_if_not ARM64_HAS_UAO
  206. 8888: ldp \reg1, \reg2, [\addr], \post_inc;
  207. 8889: nop;
  208. nop;
  209. alternative_else
  210. ldtr \reg1, [\addr];
  211. ldtr \reg2, [\addr, #8];
  212. add \addr, \addr, \post_inc;
  213. alternative_endif
  214. _asm_extable 8888b,\l;
  215. _asm_extable 8889b,\l;
  216. .endm
  217. .macro uao_stp l, reg1, reg2, addr, post_inc
  218. alternative_if_not ARM64_HAS_UAO
  219. 8888: stp \reg1, \reg2, [\addr], \post_inc;
  220. 8889: nop;
  221. nop;
  222. alternative_else
  223. sttr \reg1, [\addr];
  224. sttr \reg2, [\addr, #8];
  225. add \addr, \addr, \post_inc;
  226. alternative_endif
  227. _asm_extable 8888b,\l;
  228. _asm_extable 8889b,\l;
  229. .endm
  230. .macro uao_user_alternative l, inst, alt_inst, reg, addr, post_inc
  231. alternative_if_not ARM64_HAS_UAO
  232. 8888: \inst \reg, [\addr], \post_inc;
  233. nop;
  234. alternative_else
  235. \alt_inst \reg, [\addr];
  236. add \addr, \addr, \post_inc;
  237. alternative_endif
  238. _asm_extable 8888b,\l;
  239. .endm
  240. #else
  241. .macro uao_ldp l, reg1, reg2, addr, post_inc
  242. USER(\l, ldp \reg1, \reg2, [\addr], \post_inc)
  243. .endm
  244. .macro uao_stp l, reg1, reg2, addr, post_inc
  245. USER(\l, stp \reg1, \reg2, [\addr], \post_inc)
  246. .endm
  247. .macro uao_user_alternative l, inst, alt_inst, reg, addr, post_inc
  248. USER(\l, \inst \reg, [\addr], \post_inc)
  249. .endm
  250. #endif
  251. #endif /* __ASSEMBLY__ */
  252. /*
  253. * Usage: asm(ALTERNATIVE(oldinstr, newinstr, feature));
  254. *
  255. * Usage: asm(ALTERNATIVE(oldinstr, newinstr, feature, CONFIG_FOO));
  256. * N.B. If CONFIG_FOO is specified, but not selected, the whole block
  257. * will be omitted, including oldinstr.
  258. */
  259. #define ALTERNATIVE(oldinstr, newinstr, ...) \
  260. _ALTERNATIVE_CFG(oldinstr, newinstr, __VA_ARGS__, 1)
  261. #endif /* __ASM_ALTERNATIVE_H */