opcodes.h 8.0 KB

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