opcodes.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * arch/arm/include/asm/opcodes.h
  4. */
  5. #ifndef __ASM_ARM_OPCODES_H
  6. #define __ASM_ARM_OPCODES_H
  7. #ifndef __ASSEMBLY__
  8. #include <linux/linkage.h>
  9. extern asmlinkage unsigned int arm_check_condition(u32 opcode, u32 psr);
  10. #endif
  11. #define ARM_OPCODE_CONDTEST_FAIL 0
  12. #define ARM_OPCODE_CONDTEST_PASS 1
  13. #define ARM_OPCODE_CONDTEST_UNCOND 2
  14. /*
  15. * Assembler opcode byteswap helpers.
  16. * These are only intended for use by this header: don't use them directly,
  17. * because they will be suboptimal in most cases.
  18. */
  19. #define ___asm_opcode_swab32(x) ( \
  20. (((x) << 24) & 0xFF000000) \
  21. | (((x) << 8) & 0x00FF0000) \
  22. | (((x) >> 8) & 0x0000FF00) \
  23. | (((x) >> 24) & 0x000000FF) \
  24. )
  25. #define ___asm_opcode_swab16(x) ( \
  26. (((x) << 8) & 0xFF00) \
  27. | (((x) >> 8) & 0x00FF) \
  28. )
  29. #define ___asm_opcode_swahb32(x) ( \
  30. (((x) << 8) & 0xFF00FF00) \
  31. | (((x) >> 8) & 0x00FF00FF) \
  32. )
  33. #define ___asm_opcode_swahw32(x) ( \
  34. (((x) << 16) & 0xFFFF0000) \
  35. | (((x) >> 16) & 0x0000FFFF) \
  36. )
  37. #define ___asm_opcode_identity32(x) ((x) & 0xFFFFFFFF)
  38. #define ___asm_opcode_identity16(x) ((x) & 0xFFFF)
  39. /*
  40. * Opcode byteswap helpers
  41. *
  42. * These macros help with converting instructions between a canonical integer
  43. * format and in-memory representation, in an endianness-agnostic manner.
  44. *
  45. * __mem_to_opcode_*() convert from in-memory representation to canonical form.
  46. * __opcode_to_mem_*() convert from canonical form to in-memory representation.
  47. *
  48. *
  49. * Canonical instruction representation:
  50. *
  51. * ARM: 0xKKLLMMNN
  52. * Thumb 16-bit: 0x0000KKLL, where KK < 0xE8
  53. * Thumb 32-bit: 0xKKLLMMNN, where KK >= 0xE8
  54. *
  55. * There is no way to distinguish an ARM instruction in canonical representation
  56. * from a Thumb instruction (just as these cannot be distinguished in memory).
  57. * Where this distinction is important, it needs to be tracked separately.
  58. *
  59. * Note that values in the range 0x0000E800..0xE7FFFFFF intentionally do not
  60. * represent any valid Thumb-2 instruction. For this range,
  61. * __opcode_is_thumb32() and __opcode_is_thumb16() will both be false.
  62. *
  63. * The ___asm variants are intended only for use by this header, in situations
  64. * involving inline assembler. For .S files, the normal __opcode_*() macros
  65. * should do the right thing.
  66. */
  67. #ifdef __ASSEMBLY__
  68. #define ___opcode_swab32(x) ___asm_opcode_swab32(x)
  69. #define ___opcode_swab16(x) ___asm_opcode_swab16(x)
  70. #define ___opcode_swahb32(x) ___asm_opcode_swahb32(x)
  71. #define ___opcode_swahw32(x) ___asm_opcode_swahw32(x)
  72. #define ___opcode_identity32(x) ___asm_opcode_identity32(x)
  73. #define ___opcode_identity16(x) ___asm_opcode_identity16(x)
  74. #else /* ! __ASSEMBLY__ */
  75. #include <linux/types.h>
  76. #include <linux/swab.h>
  77. #define ___opcode_swab32(x) swab32(x)
  78. #define ___opcode_swab16(x) swab16(x)
  79. #define ___opcode_swahb32(x) swahb32(x)
  80. #define ___opcode_swahw32(x) swahw32(x)
  81. #define ___opcode_identity32(x) ((u32)(x))
  82. #define ___opcode_identity16(x) ((u16)(x))
  83. #endif /* ! __ASSEMBLY__ */
  84. #ifdef CONFIG_CPU_ENDIAN_BE8
  85. #define __opcode_to_mem_arm(x) ___opcode_swab32(x)
  86. #define __opcode_to_mem_thumb16(x) ___opcode_swab16(x)
  87. #define __opcode_to_mem_thumb32(x) ___opcode_swahb32(x)
  88. #define ___asm_opcode_to_mem_arm(x) ___asm_opcode_swab32(x)
  89. #define ___asm_opcode_to_mem_thumb16(x) ___asm_opcode_swab16(x)
  90. #define ___asm_opcode_to_mem_thumb32(x) ___asm_opcode_swahb32(x)
  91. #else /* ! CONFIG_CPU_ENDIAN_BE8 */
  92. #define __opcode_to_mem_arm(x) ___opcode_identity32(x)
  93. #define __opcode_to_mem_thumb16(x) ___opcode_identity16(x)
  94. #define ___asm_opcode_to_mem_arm(x) ___asm_opcode_identity32(x)
  95. #define ___asm_opcode_to_mem_thumb16(x) ___asm_opcode_identity16(x)
  96. #ifdef CONFIG_CPU_ENDIAN_BE32
  97. #ifndef __ASSEMBLY__
  98. /*
  99. * On BE32 systems, using 32-bit accesses to store Thumb instructions will not
  100. * work in all cases, due to alignment constraints. For now, a correct
  101. * version is not provided for BE32, but the prototype needs to be there
  102. * to compile patch.c.
  103. */
  104. extern __u32 __opcode_to_mem_thumb32(__u32);
  105. #endif
  106. #else
  107. #define __opcode_to_mem_thumb32(x) ___opcode_swahw32(x)
  108. #define ___asm_opcode_to_mem_thumb32(x) ___asm_opcode_swahw32(x)
  109. #endif
  110. #endif /* ! CONFIG_CPU_ENDIAN_BE8 */
  111. #define __mem_to_opcode_arm(x) __opcode_to_mem_arm(x)
  112. #define __mem_to_opcode_thumb16(x) __opcode_to_mem_thumb16(x)
  113. #ifndef CONFIG_CPU_ENDIAN_BE32
  114. #define __mem_to_opcode_thumb32(x) __opcode_to_mem_thumb32(x)
  115. #endif
  116. /* Operations specific to Thumb opcodes */
  117. /* Instruction size checks: */
  118. #define __opcode_is_thumb32(x) ( \
  119. ((x) & 0xF8000000) == 0xE8000000 \
  120. || ((x) & 0xF0000000) == 0xF0000000 \
  121. )
  122. #define __opcode_is_thumb16(x) ( \
  123. ((x) & 0xFFFF0000) == 0 \
  124. && !(((x) & 0xF800) == 0xE800 || ((x) & 0xF000) == 0xF000) \
  125. )
  126. /* Operations to construct or split 32-bit Thumb instructions: */
  127. #define __opcode_thumb32_first(x) (___opcode_identity16((x) >> 16))
  128. #define __opcode_thumb32_second(x) (___opcode_identity16(x))
  129. #define __opcode_thumb32_compose(first, second) ( \
  130. (___opcode_identity32(___opcode_identity16(first)) << 16) \
  131. | ___opcode_identity32(___opcode_identity16(second)) \
  132. )
  133. #define ___asm_opcode_thumb32_first(x) (___asm_opcode_identity16((x) >> 16))
  134. #define ___asm_opcode_thumb32_second(x) (___asm_opcode_identity16(x))
  135. #define ___asm_opcode_thumb32_compose(first, second) ( \
  136. (___asm_opcode_identity32(___asm_opcode_identity16(first)) << 16) \
  137. | ___asm_opcode_identity32(___asm_opcode_identity16(second)) \
  138. )
  139. /*
  140. * Opcode injection helpers
  141. *
  142. * In rare cases it is necessary to assemble an opcode which the
  143. * assembler does not support directly, or which would normally be
  144. * rejected because of the CFLAGS or AFLAGS used to build the affected
  145. * file.
  146. *
  147. * Before using these macros, consider carefully whether it is feasible
  148. * instead to change the build flags for your file, or whether it really
  149. * makes sense to support old assembler versions when building that
  150. * particular kernel feature.
  151. *
  152. * The macros defined here should only be used where there is no viable
  153. * alternative.
  154. *
  155. *
  156. * __inst_arm(x): emit the specified ARM opcode
  157. * __inst_thumb16(x): emit the specified 16-bit Thumb opcode
  158. * __inst_thumb32(x): emit the specified 32-bit Thumb opcode
  159. *
  160. * __inst_arm_thumb16(arm, thumb): emit either the specified arm or
  161. * 16-bit Thumb opcode, depending on whether an ARM or Thumb-2
  162. * kernel is being built
  163. *
  164. * __inst_arm_thumb32(arm, thumb): emit either the specified arm or
  165. * 32-bit Thumb opcode, depending on whether an ARM or Thumb-2
  166. * kernel is being built
  167. *
  168. *
  169. * Note that using these macros directly is poor practice. Instead, you
  170. * should use them to define human-readable wrapper macros to encode the
  171. * instructions that you care about. In code which might run on ARMv7 or
  172. * above, you can usually use the __inst_arm_thumb{16,32} macros to
  173. * specify the ARM and Thumb alternatives at the same time. This ensures
  174. * that the correct opcode gets emitted depending on the instruction set
  175. * used for the kernel build.
  176. *
  177. * Look at opcodes-virt.h for an example of how to use these macros.
  178. */
  179. #include <linux/stringify.h>
  180. #define __inst_arm(x) ___inst_arm(___asm_opcode_to_mem_arm(x))
  181. #define __inst_thumb32(x) ___inst_thumb32( \
  182. ___asm_opcode_to_mem_thumb16(___asm_opcode_thumb32_first(x)), \
  183. ___asm_opcode_to_mem_thumb16(___asm_opcode_thumb32_second(x)) \
  184. )
  185. #define __inst_thumb16(x) ___inst_thumb16(___asm_opcode_to_mem_thumb16(x))
  186. #ifdef CONFIG_THUMB2_KERNEL
  187. #define __inst_arm_thumb16(arm_opcode, thumb_opcode) \
  188. __inst_thumb16(thumb_opcode)
  189. #define __inst_arm_thumb32(arm_opcode, thumb_opcode) \
  190. __inst_thumb32(thumb_opcode)
  191. #else
  192. #define __inst_arm_thumb16(arm_opcode, thumb_opcode) __inst_arm(arm_opcode)
  193. #define __inst_arm_thumb32(arm_opcode, thumb_opcode) __inst_arm(arm_opcode)
  194. #endif
  195. /* Helpers for the helpers. Don't use these directly. */
  196. #ifdef __ASSEMBLY__
  197. #define ___inst_arm(x) .long x
  198. #define ___inst_thumb16(x) .short x
  199. #define ___inst_thumb32(first, second) .short first, second
  200. #else
  201. #define ___inst_arm(x) ".long " __stringify(x) "\n\t"
  202. #define ___inst_thumb16(x) ".short " __stringify(x) "\n\t"
  203. #define ___inst_thumb32(first, second) \
  204. ".short " __stringify(first) ", " __stringify(second) "\n\t"
  205. #endif
  206. #endif /* __ASM_ARM_OPCODES_H */