alternative-macros.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 BUILD_FIPS140_KO
  18. #ifndef __ASSEMBLY__
  19. #include <linux/stringify.h>
  20. #define ALTINSTR_ENTRY(feature) \
  21. " .word 661b - .\n" /* label */ \
  22. " .word 663f - .\n" /* new instruction */ \
  23. " .hword " __stringify(feature) "\n" /* feature bit */ \
  24. " .byte 662b-661b\n" /* source len */ \
  25. " .byte 664f-663f\n" /* replacement len */
  26. #define ALTINSTR_ENTRY_CB(feature, cb) \
  27. " .word 661b - .\n" /* label */ \
  28. " .word " __stringify(cb) "- .\n" /* callback */ \
  29. " .hword " __stringify(feature) "\n" /* feature bit */ \
  30. " .byte 662b-661b\n" /* source len */ \
  31. " .byte 664f-663f\n" /* replacement len */
  32. /*
  33. * alternative assembly primitive:
  34. *
  35. * If any of these .org directive fail, it means that insn1 and insn2
  36. * don't have the same length. This used to be written as
  37. *
  38. * .if ((664b-663b) != (662b-661b))
  39. * .error "Alternatives instruction length mismatch"
  40. * .endif
  41. *
  42. * but most assemblers die if insn1 or insn2 have a .inst. This should
  43. * be fixed in a binutils release posterior to 2.25.51.0.2 (anything
  44. * containing commit 4e4d08cf7399b606 or c1baaddf8861).
  45. *
  46. * Alternatives with callbacks do not generate replacement instructions.
  47. */
  48. #define __ALTERNATIVE_CFG(oldinstr, newinstr, feature, cfg_enabled) \
  49. ".if "__stringify(cfg_enabled)" == 1\n" \
  50. "661:\n\t" \
  51. oldinstr "\n" \
  52. "662:\n" \
  53. ".pushsection .altinstructions,\"a\"\n" \
  54. ALTINSTR_ENTRY(feature) \
  55. ".popsection\n" \
  56. ".subsection 1\n" \
  57. "663:\n\t" \
  58. newinstr "\n" \
  59. "664:\n\t" \
  60. ".org . - (664b-663b) + (662b-661b)\n\t" \
  61. ".org . - (662b-661b) + (664b-663b)\n\t" \
  62. ".previous\n" \
  63. ".endif\n"
  64. #define __ALTERNATIVE_CFG_CB(oldinstr, feature, cfg_enabled, cb) \
  65. ".if "__stringify(cfg_enabled)" == 1\n" \
  66. "661:\n\t" \
  67. oldinstr "\n" \
  68. "662:\n" \
  69. ".pushsection .altinstructions,\"a\"\n" \
  70. ALTINSTR_ENTRY_CB(feature, cb) \
  71. ".popsection\n" \
  72. "663:\n\t" \
  73. "664:\n\t" \
  74. ".endif\n"
  75. #define _ALTERNATIVE_CFG(oldinstr, newinstr, feature, cfg, ...) \
  76. __ALTERNATIVE_CFG(oldinstr, newinstr, feature, IS_ENABLED(cfg))
  77. #define ALTERNATIVE_CB(oldinstr, feature, cb) \
  78. __ALTERNATIVE_CFG_CB(oldinstr, (1 << ARM64_CB_SHIFT) | (feature), 1, cb)
  79. #else
  80. #include <asm/assembler.h>
  81. .macro altinstruction_entry orig_offset alt_offset feature orig_len alt_len
  82. .word \orig_offset - .
  83. .word \alt_offset - .
  84. .hword (\feature)
  85. .byte \orig_len
  86. .byte \alt_len
  87. .endm
  88. .macro alternative_insn insn1, insn2, cap, enable = 1
  89. .if \enable
  90. 661: \insn1
  91. 662: .pushsection .altinstructions, "a"
  92. altinstruction_entry 661b, 663f, \cap, 662b-661b, 664f-663f
  93. .popsection
  94. .subsection 1
  95. 663: \insn2
  96. 664: .org . - (664b-663b) + (662b-661b)
  97. .org . - (662b-661b) + (664b-663b)
  98. .previous
  99. .endif
  100. .endm
  101. /*
  102. * Alternative sequences
  103. *
  104. * The code for the case where the capability is not present will be
  105. * assembled and linked as normal. There are no restrictions on this
  106. * code.
  107. *
  108. * The code for the case where the capability is present will be
  109. * assembled into a special section to be used for dynamic patching.
  110. * Code for that case must:
  111. *
  112. * 1. Be exactly the same length (in bytes) as the default code
  113. * sequence.
  114. *
  115. * 2. Not contain a branch target that is used outside of the
  116. * alternative sequence it is defined in (branches into an
  117. * alternative sequence are not fixed up).
  118. */
  119. /*
  120. * Begin an alternative code sequence.
  121. */
  122. .macro alternative_if_not cap
  123. .set .Lasm_alt_mode, 0
  124. .pushsection .altinstructions, "a"
  125. altinstruction_entry 661f, 663f, \cap, 662f-661f, 664f-663f
  126. .popsection
  127. 661:
  128. .endm
  129. .macro alternative_if cap
  130. .set .Lasm_alt_mode, 1
  131. .pushsection .altinstructions, "a"
  132. altinstruction_entry 663f, 661f, \cap, 664f-663f, 662f-661f
  133. .popsection
  134. .subsection 1
  135. .align 2 /* So GAS knows label 661 is suitably aligned */
  136. 661:
  137. .endm
  138. .macro alternative_cb cap, cb
  139. .set .Lasm_alt_mode, 0
  140. .pushsection .altinstructions, "a"
  141. altinstruction_entry 661f, \cb, (1 << ARM64_CB_SHIFT) | \cap, 662f-661f, 0
  142. .popsection
  143. 661:
  144. .endm
  145. /*
  146. * Provide the other half of the alternative code sequence.
  147. */
  148. .macro alternative_else
  149. 662:
  150. .if .Lasm_alt_mode==0
  151. .subsection 1
  152. .else
  153. .previous
  154. .endif
  155. 663:
  156. .endm
  157. /*
  158. * Complete an alternative code sequence.
  159. */
  160. .macro alternative_endif
  161. 664:
  162. .org . - (664b-663b) + (662b-661b)
  163. .org . - (662b-661b) + (664b-663b)
  164. .if .Lasm_alt_mode==0
  165. .previous
  166. .endif
  167. .endm
  168. /*
  169. * Callback-based alternative epilogue
  170. */
  171. .macro alternative_cb_end
  172. 662:
  173. .endm
  174. /*
  175. * Provides a trivial alternative or default sequence consisting solely
  176. * of NOPs. The number of NOPs is chosen automatically to match the
  177. * previous case.
  178. */
  179. .macro alternative_else_nop_endif
  180. alternative_else
  181. nops (662b-661b) / AARCH64_INSN_SIZE
  182. alternative_endif
  183. .endm
  184. #define _ALTERNATIVE_CFG(insn1, insn2, cap, cfg, ...) \
  185. alternative_insn insn1, insn2, cap, IS_ENABLED(cfg)
  186. #endif /* __ASSEMBLY__ */
  187. /*
  188. * Usage: asm(ALTERNATIVE(oldinstr, newinstr, feature));
  189. *
  190. * Usage: asm(ALTERNATIVE(oldinstr, newinstr, feature, CONFIG_FOO));
  191. * N.B. If CONFIG_FOO is specified, but not selected, the whole block
  192. * will be omitted, including oldinstr.
  193. */
  194. #define ALTERNATIVE(oldinstr, newinstr, ...) \
  195. _ALTERNATIVE_CFG(oldinstr, newinstr, __VA_ARGS__, 1)
  196. #ifndef __ASSEMBLY__
  197. #include <linux/types.h>
  198. static __always_inline bool
  199. alternative_has_feature_likely(unsigned long feature)
  200. {
  201. compiletime_assert(feature < ARM64_NCAPS,
  202. "feature must be < ARM64_NCAPS");
  203. asm_volatile_goto(
  204. ALTERNATIVE_CB("b %l[l_no]", %[feature], alt_cb_patch_nops)
  205. :
  206. : [feature] "i" (feature)
  207. :
  208. : l_no);
  209. return true;
  210. l_no:
  211. return false;
  212. }
  213. static __always_inline bool
  214. alternative_has_feature_unlikely(unsigned long feature)
  215. {
  216. compiletime_assert(feature < ARM64_NCAPS,
  217. "feature must be < ARM64_NCAPS");
  218. asm_volatile_goto(
  219. ALTERNATIVE("nop", "b %l[l_yes]", %[feature])
  220. :
  221. : [feature] "i" (feature)
  222. :
  223. : l_yes);
  224. return false;
  225. l_yes:
  226. return true;
  227. }
  228. #endif /* __ASSEMBLY__ */
  229. #else
  230. /*
  231. * The FIPS140 module does not support alternatives patching, as this
  232. * invalidates the HMAC digest of the .text section. However, some alternatives
  233. * are known to be irrelevant so we can tolerate them in the FIPS140 module, as
  234. * they will never be applied in the first place in the use cases that the
  235. * FIPS140 module targets (Android running on a production phone). Any other
  236. * uses of alternatives should be avoided, as it is not safe in the general
  237. * case to simply use the default sequence in one place (the fips module) and
  238. * the alternative sequence everywhere else.
  239. *
  240. * Below is an allowlist of features that we can ignore, by simply taking the
  241. * safe default instruction sequence. Note that this implies that the FIPS140
  242. * module is not compatible with VHE, or with pseudo-NMI support.
  243. */
  244. #define __ALT_ARM64_HAS_LDAPR 0,
  245. #define __ALT_ARM64_HAS_VIRT_HOST_EXTN 0,
  246. #define __ALT_ARM64_HAS_IRQ_PRIO_MASKING 0,
  247. #define ALTERNATIVE(oldinstr, newinstr, feature, ...) \
  248. _ALTERNATIVE(oldinstr, __ALT_ ## feature, #feature)
  249. #define _ALTERNATIVE(oldinstr, feature, feature_str) \
  250. __take_second_arg(feature oldinstr, \
  251. ".err Feature " feature_str " not supported in fips140 module")
  252. #ifndef __ASSEMBLY__
  253. #include <linux/types.h>
  254. static __always_inline bool
  255. alternative_has_feature_likely(unsigned long feature)
  256. {
  257. return feature == ARM64_HAS_LDAPR ||
  258. feature == ARM64_HAS_VIRT_HOST_EXTN ||
  259. feature == ARM64_HAS_IRQ_PRIO_MASKING;
  260. }
  261. #define alternative_has_feature_unlikely alternative_has_feature_likely
  262. #endif /* !__ASSEMBLY__ */
  263. #endif /* BUILD_FIPS140_KO */
  264. #endif /* __ASM_ALTERNATIVE_MACROS_H */