opcodes.h 7.9 KB

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