opcodes.h 8.1 KB

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