opcodes.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #endif /* __ASM_ARM_OPCODES_H */