opcodes.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* SPDX-License-Identifier: GPL-2.0 */
  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. #ifndef CONFIG_CPU_ENDIAN_BE32
  97. /*
  98. * On BE32 systems, using 32-bit accesses to store Thumb instructions will not
  99. * work in all cases, due to alignment constraints. For now, a correct
  100. * version is not provided for BE32.
  101. */
  102. #define __opcode_to_mem_thumb32(x) ___opcode_swahw32(x)
  103. #define ___asm_opcode_to_mem_thumb32(x) ___asm_opcode_swahw32(x)
  104. #endif
  105. #endif /* ! CONFIG_CPU_ENDIAN_BE8 */
  106. #define __mem_to_opcode_arm(x) __opcode_to_mem_arm(x)
  107. #define __mem_to_opcode_thumb16(x) __opcode_to_mem_thumb16(x)
  108. #ifndef CONFIG_CPU_ENDIAN_BE32
  109. #define __mem_to_opcode_thumb32(x) __opcode_to_mem_thumb32(x)
  110. #endif
  111. /* Operations specific to Thumb opcodes */
  112. /* Instruction size checks: */
  113. #define __opcode_is_thumb32(x) ( \
  114. ((x) & 0xF8000000) == 0xE8000000 \
  115. || ((x) & 0xF0000000) == 0xF0000000 \
  116. )
  117. #define __opcode_is_thumb16(x) ( \
  118. ((x) & 0xFFFF0000) == 0 \
  119. && !(((x) & 0xF800) == 0xE800 || ((x) & 0xF000) == 0xF000) \
  120. )
  121. /* Operations to construct or split 32-bit Thumb instructions: */
  122. #define __opcode_thumb32_first(x) (___opcode_identity16((x) >> 16))
  123. #define __opcode_thumb32_second(x) (___opcode_identity16(x))
  124. #define __opcode_thumb32_compose(first, second) ( \
  125. (___opcode_identity32(___opcode_identity16(first)) << 16) \
  126. | ___opcode_identity32(___opcode_identity16(second)) \
  127. )
  128. #define ___asm_opcode_thumb32_first(x) (___asm_opcode_identity16((x) >> 16))
  129. #define ___asm_opcode_thumb32_second(x) (___asm_opcode_identity16(x))
  130. #define ___asm_opcode_thumb32_compose(first, second) ( \
  131. (___asm_opcode_identity32(___asm_opcode_identity16(first)) << 16) \
  132. | ___asm_opcode_identity32(___asm_opcode_identity16(second)) \
  133. )
  134. /*
  135. * Opcode injection helpers
  136. *
  137. * In rare cases it is necessary to assemble an opcode which the
  138. * assembler does not support directly, or which would normally be
  139. * rejected because of the CFLAGS or AFLAGS used to build the affected
  140. * file.
  141. *
  142. * Before using these macros, consider carefully whether it is feasible
  143. * instead to change the build flags for your file, or whether it really
  144. * makes sense to support old assembler versions when building that
  145. * particular kernel feature.
  146. *
  147. * The macros defined here should only be used where there is no viable
  148. * alternative.
  149. *
  150. *
  151. * __inst_arm(x): emit the specified ARM opcode
  152. * __inst_thumb16(x): emit the specified 16-bit Thumb opcode
  153. * __inst_thumb32(x): emit the specified 32-bit Thumb opcode
  154. *
  155. * __inst_arm_thumb16(arm, thumb): emit either the specified arm or
  156. * 16-bit Thumb opcode, depending on whether an ARM or Thumb-2
  157. * kernel is being built
  158. *
  159. * __inst_arm_thumb32(arm, thumb): emit either the specified arm or
  160. * 32-bit Thumb opcode, depending on whether an ARM or Thumb-2
  161. * kernel is being built
  162. *
  163. *
  164. * Note that using these macros directly is poor practice. Instead, you
  165. * should use them to define human-readable wrapper macros to encode the
  166. * instructions that you care about. In code which might run on ARMv7 or
  167. * above, you can usually use the __inst_arm_thumb{16,32} macros to
  168. * specify the ARM and Thumb alternatives at the same time. This ensures
  169. * that the correct opcode gets emitted depending on the instruction set
  170. * used for the kernel build.
  171. *
  172. * Look at opcodes-virt.h for an example of how to use these macros.
  173. */
  174. #include <linux/stringify.h>
  175. #define __inst_arm(x) ___inst_arm(___asm_opcode_to_mem_arm(x))
  176. #define __inst_thumb32(x) ___inst_thumb32( \
  177. ___asm_opcode_to_mem_thumb16(___asm_opcode_thumb32_first(x)), \
  178. ___asm_opcode_to_mem_thumb16(___asm_opcode_thumb32_second(x)) \
  179. )
  180. #define __inst_thumb16(x) ___inst_thumb16(___asm_opcode_to_mem_thumb16(x))
  181. #ifdef CONFIG_THUMB2_KERNEL
  182. #define __inst_arm_thumb16(arm_opcode, thumb_opcode) \
  183. __inst_thumb16(thumb_opcode)
  184. #define __inst_arm_thumb32(arm_opcode, thumb_opcode) \
  185. __inst_thumb32(thumb_opcode)
  186. #else
  187. #define __inst_arm_thumb16(arm_opcode, thumb_opcode) __inst_arm(arm_opcode)
  188. #define __inst_arm_thumb32(arm_opcode, thumb_opcode) __inst_arm(arm_opcode)
  189. #endif
  190. /* Helpers for the helpers. Don't use these directly. */
  191. #ifdef __ASSEMBLY__
  192. #define ___inst_arm(x) .long x
  193. #define ___inst_thumb16(x) .short x
  194. #define ___inst_thumb32(first, second) .short first, second
  195. #else
  196. #define ___inst_arm(x) ".long " __stringify(x) "\n\t"
  197. #define ___inst_thumb16(x) ".short " __stringify(x) "\n\t"
  198. #define ___inst_thumb32(first, second) \
  199. ".short " __stringify(first) ", " __stringify(second) "\n\t"
  200. #endif
  201. #endif /* __ASM_ARM_OPCODES_H */