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