blob: f57e417cab959ad17b286af035df81c6adb366ab [file] [log] [blame]
Leif Lindholm0c9030d2011-12-12 19:31:55 +01001/*
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
9#ifndef __ASM_ARM_OPCODES_H
10#define __ASM_ARM_OPCODES_H
11
12#ifndef __ASSEMBLY__
13extern asmlinkage unsigned int arm_check_condition(u32 opcode, u32 psr);
14#endif
15
16#define ARM_OPCODE_CONDTEST_FAIL 0
17#define ARM_OPCODE_CONDTEST_PASS 1
18#define ARM_OPCODE_CONDTEST_UNCOND 2
19
Dave Martinf5f51952012-02-01 10:42:22 +010020
21/*
Dave Martin0ce3de22012-09-03 13:49:23 +010022 * Assembler opcode byteswap helpers.
23 * These are only intended for use by this header: don't use them directly,
24 * because they will be suboptimal in most cases.
25 */
26#define ___asm_opcode_swab32(x) ( \
27 (((x) << 24) & 0xFF000000) \
28 | (((x) << 8) & 0x00FF0000) \
29 | (((x) >> 8) & 0x0000FF00) \
30 | (((x) >> 24) & 0x000000FF) \
31)
32#define ___asm_opcode_swab16(x) ( \
33 (((x) << 8) & 0xFF00) \
34 | (((x) >> 8) & 0x00FF) \
35)
36#define ___asm_opcode_swahb32(x) ( \
37 (((x) << 8) & 0xFF00FF00) \
38 | (((x) >> 8) & 0x00FF00FF) \
39)
40#define ___asm_opcode_swahw32(x) ( \
41 (((x) << 16) & 0xFFFF0000) \
42 | (((x) >> 16) & 0x0000FFFF) \
43)
44#define ___asm_opcode_identity32(x) ((x) & 0xFFFFFFFF)
45#define ___asm_opcode_identity16(x) ((x) & 0xFFFF)
46
47
48/*
Dave Martinf5f51952012-02-01 10:42:22 +010049 * Opcode byteswap helpers
50 *
51 * These macros help with converting instructions between a canonical integer
52 * format and in-memory representation, in an endianness-agnostic manner.
53 *
54 * __mem_to_opcode_*() convert from in-memory representation to canonical form.
55 * __opcode_to_mem_*() convert from canonical form to in-memory representation.
56 *
57 *
58 * Canonical instruction representation:
59 *
60 * ARM: 0xKKLLMMNN
61 * Thumb 16-bit: 0x0000KKLL, where KK < 0xE8
62 * Thumb 32-bit: 0xKKLLMMNN, where KK >= 0xE8
63 *
64 * There is no way to distinguish an ARM instruction in canonical representation
65 * from a Thumb instruction (just as these cannot be distinguished in memory).
66 * Where this distinction is important, it needs to be tracked separately.
67 *
68 * Note that values in the range 0x0000E800..0xE7FFFFFF intentionally do not
69 * represent any valid Thumb-2 instruction. For this range,
70 * __opcode_is_thumb32() and __opcode_is_thumb16() will both be false.
Dave Martin0ce3de22012-09-03 13:49:23 +010071 *
72 * The ___asm variants are intended only for use by this header, in situations
73 * involving inline assembler. For .S files, the normal __opcode_*() macros
74 * should do the right thing.
Dave Martinf5f51952012-02-01 10:42:22 +010075 */
Dave Martin0ce3de22012-09-03 13:49:23 +010076#ifdef __ASSEMBLY__
Dave Martinf5f51952012-02-01 10:42:22 +010077
Dave Martin0ce3de22012-09-03 13:49:23 +010078#define ___opcode_swab32(x) ___asm_opcode_swab32(x)
79#define ___opcode_swab16(x) ___asm_opcode_swab16(x)
80#define ___opcode_swahb32(x) ___asm_opcode_swahb32(x)
81#define ___opcode_swahw32(x) ___asm_opcode_swahw32(x)
82#define ___opcode_identity32(x) ___asm_opcode_identity32(x)
83#define ___opcode_identity16(x) ___asm_opcode_identity16(x)
84
85#else /* ! __ASSEMBLY__ */
Dave Martinf5f51952012-02-01 10:42:22 +010086
87#include <linux/types.h>
88#include <linux/swab.h>
89
Dave Martin0ce3de22012-09-03 13:49:23 +010090#define ___opcode_swab32(x) swab32(x)
91#define ___opcode_swab16(x) swab16(x)
92#define ___opcode_swahb32(x) swahb32(x)
93#define ___opcode_swahw32(x) swahw32(x)
94#define ___opcode_identity32(x) ((u32)(x))
95#define ___opcode_identity16(x) ((u16)(x))
96
97#endif /* ! __ASSEMBLY__ */
98
99
Dave Martinf5f51952012-02-01 10:42:22 +0100100#ifdef CONFIG_CPU_ENDIAN_BE8
Dave Martin57b9da32012-09-03 13:49:22 +0100101
Dave Martin0ce3de22012-09-03 13:49:23 +0100102#define __opcode_to_mem_arm(x) ___opcode_swab32(x)
103#define __opcode_to_mem_thumb16(x) ___opcode_swab16(x)
104#define __opcode_to_mem_thumb32(x) ___opcode_swahb32(x)
105#define ___asm_opcode_to_mem_arm(x) ___asm_opcode_swab32(x)
106#define ___asm_opcode_to_mem_thumb16(x) ___asm_opcode_swab16(x)
107#define ___asm_opcode_to_mem_thumb32(x) ___asm_opcode_swahb32(x)
Dave Martin57b9da32012-09-03 13:49:22 +0100108
109#else /* ! CONFIG_CPU_ENDIAN_BE8 */
110
Dave Martin0ce3de22012-09-03 13:49:23 +0100111#define __opcode_to_mem_arm(x) ___opcode_identity32(x)
112#define __opcode_to_mem_thumb16(x) ___opcode_identity16(x)
113#define ___asm_opcode_to_mem_arm(x) ___asm_opcode_identity32(x)
114#define ___asm_opcode_to_mem_thumb16(x) ___asm_opcode_identity16(x)
Dave Martin57b9da32012-09-03 13:49:22 +0100115#ifndef CONFIG_CPU_ENDIAN_BE32
116/*
117 * On BE32 systems, using 32-bit accesses to store Thumb instructions will not
118 * work in all cases, due to alignment constraints. For now, a correct
119 * version is not provided for BE32.
120 */
Dave Martin0ce3de22012-09-03 13:49:23 +0100121#define __opcode_to_mem_thumb32(x) ___opcode_swahw32(x)
122#define ___asm_opcode_to_mem_thumb32(x) ___asm_opcode_swahw32(x)
Dave Martinf5f51952012-02-01 10:42:22 +0100123#endif
124
Dave Martin57b9da32012-09-03 13:49:22 +0100125#endif /* ! CONFIG_CPU_ENDIAN_BE8 */
126
Dave Martinf5f51952012-02-01 10:42:22 +0100127#define __mem_to_opcode_arm(x) __opcode_to_mem_arm(x)
128#define __mem_to_opcode_thumb16(x) __opcode_to_mem_thumb16(x)
Dave Martin57b9da32012-09-03 13:49:22 +0100129#ifndef CONFIG_CPU_ENDIAN_BE32
Dave Martinf5f51952012-02-01 10:42:22 +0100130#define __mem_to_opcode_thumb32(x) __opcode_to_mem_thumb32(x)
Dave Martin57b9da32012-09-03 13:49:22 +0100131#endif
Dave Martinf5f51952012-02-01 10:42:22 +0100132
133/* Operations specific to Thumb opcodes */
134
135/* Instruction size checks: */
Dave Martin0ce3de22012-09-03 13:49:23 +0100136#define __opcode_is_thumb32(x) ( \
137 ((x) & 0xF8000000) == 0xE8000000 \
138 || ((x) & 0xF0000000) == 0xF0000000 \
139)
140#define __opcode_is_thumb16(x) ( \
141 ((x) & 0xFFFF0000) == 0 \
142 && !(((x) & 0xF800) == 0xE800 || ((x) & 0xF000) == 0xF000) \
143)
Dave Martinf5f51952012-02-01 10:42:22 +0100144
145/* Operations to construct or split 32-bit Thumb instructions: */
Dave Martin0ce3de22012-09-03 13:49:23 +0100146#define __opcode_thumb32_first(x) (___opcode_identity16((x) >> 16))
147#define __opcode_thumb32_second(x) (___opcode_identity16(x))
148#define __opcode_thumb32_compose(first, second) ( \
149 (___opcode_identity32(___opcode_identity16(first)) << 16) \
150 | ___opcode_identity32(___opcode_identity16(second)) \
151)
152#define ___asm_opcode_thumb32_first(x) (___asm_opcode_identity16((x) >> 16))
153#define ___asm_opcode_thumb32_second(x) (___asm_opcode_identity16(x))
154#define ___asm_opcode_thumb32_compose(first, second) ( \
155 (___asm_opcode_identity32(___asm_opcode_identity16(first)) << 16) \
156 | ___asm_opcode_identity32(___asm_opcode_identity16(second)) \
157)
Dave Martinf5f51952012-02-01 10:42:22 +0100158
Leif Lindholm0c9030d2011-12-12 19:31:55 +0100159#endif /* __ASM_ARM_OPCODES_H */