alternative.h 6.5 KB

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