blob: 5080962058b0431b1e76ce8faac56ec234ea562f [file] [log] [blame]
Jiang Liub11a64a2014-01-07 22:17:08 +08001/*
2 * Copyright (C) 2013 Huawei Ltd.
3 * Author: Jiang Liu <liuj97@gmail.com>
4 *
Zi Shen Lim617d2fb2014-08-27 05:15:17 +01005 * Copyright (C) 2014 Zi Shen Lim <zlim.lnx@gmail.com>
6 *
Jiang Liub11a64a2014-01-07 22:17:08 +08007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19#ifndef __ASM_INSN_H
20#define __ASM_INSN_H
21#include <linux/types.h>
22
Jiang Liuae164802014-01-07 22:17:09 +080023/* A64 instructions are always 32 bits. */
24#define AARCH64_INSN_SIZE 4
25
AKASHI Takahiro26e9b832014-04-30 10:54:30 +010026#ifndef __ASSEMBLY__
Jiang Liub11a64a2014-01-07 22:17:08 +080027/*
28 * ARM Architecture Reference Manual for ARMv8 Profile-A, Issue A.a
29 * Section C3.1 "A64 instruction index by encoding":
30 * AArch64 main encoding table
31 * Bit position
32 * 28 27 26 25 Encoding Group
33 * 0 0 - - Unallocated
34 * 1 0 0 - Data processing, immediate
35 * 1 0 1 - Branch, exception generation and system instructions
36 * - 1 - 0 Loads and stores
37 * - 1 0 1 Data processing - register
38 * 0 1 1 1 Data processing - SIMD and floating point
39 * 1 1 1 1 Data processing - SIMD and floating point
40 * "-" means "don't care"
41 */
42enum aarch64_insn_encoding_class {
43 AARCH64_INSN_CLS_UNKNOWN, /* UNALLOCATED */
44 AARCH64_INSN_CLS_DP_IMM, /* Data processing - immediate */
45 AARCH64_INSN_CLS_DP_REG, /* Data processing - register */
46 AARCH64_INSN_CLS_DP_FPSIMD, /* Data processing - SIMD and FP */
47 AARCH64_INSN_CLS_LDST, /* Loads and stores */
48 AARCH64_INSN_CLS_BR_SYS, /* Branch, exception generation and
49 * system instructions */
50};
51
52enum aarch64_insn_hint_op {
53 AARCH64_INSN_HINT_NOP = 0x0 << 5,
54 AARCH64_INSN_HINT_YIELD = 0x1 << 5,
55 AARCH64_INSN_HINT_WFE = 0x2 << 5,
56 AARCH64_INSN_HINT_WFI = 0x3 << 5,
57 AARCH64_INSN_HINT_SEV = 0x4 << 5,
58 AARCH64_INSN_HINT_SEVL = 0x5 << 5,
59};
60
Jiang Liuc84fced2014-01-07 22:17:10 +080061enum aarch64_insn_imm_type {
62 AARCH64_INSN_IMM_ADR,
63 AARCH64_INSN_IMM_26,
64 AARCH64_INSN_IMM_19,
65 AARCH64_INSN_IMM_16,
66 AARCH64_INSN_IMM_14,
67 AARCH64_INSN_IMM_12,
68 AARCH64_INSN_IMM_9,
69 AARCH64_INSN_IMM_MAX
70};
71
Zi Shen Lim617d2fb2014-08-27 05:15:17 +010072enum aarch64_insn_register_type {
73 AARCH64_INSN_REGTYPE_RT,
Zi Shen Limc0cafba2014-08-27 05:15:18 +010074 AARCH64_INSN_REGTYPE_RN,
Zi Shen Lim617d2fb2014-08-27 05:15:17 +010075};
76
77enum aarch64_insn_register {
78 AARCH64_INSN_REG_0 = 0,
79 AARCH64_INSN_REG_1 = 1,
80 AARCH64_INSN_REG_2 = 2,
81 AARCH64_INSN_REG_3 = 3,
82 AARCH64_INSN_REG_4 = 4,
83 AARCH64_INSN_REG_5 = 5,
84 AARCH64_INSN_REG_6 = 6,
85 AARCH64_INSN_REG_7 = 7,
86 AARCH64_INSN_REG_8 = 8,
87 AARCH64_INSN_REG_9 = 9,
88 AARCH64_INSN_REG_10 = 10,
89 AARCH64_INSN_REG_11 = 11,
90 AARCH64_INSN_REG_12 = 12,
91 AARCH64_INSN_REG_13 = 13,
92 AARCH64_INSN_REG_14 = 14,
93 AARCH64_INSN_REG_15 = 15,
94 AARCH64_INSN_REG_16 = 16,
95 AARCH64_INSN_REG_17 = 17,
96 AARCH64_INSN_REG_18 = 18,
97 AARCH64_INSN_REG_19 = 19,
98 AARCH64_INSN_REG_20 = 20,
99 AARCH64_INSN_REG_21 = 21,
100 AARCH64_INSN_REG_22 = 22,
101 AARCH64_INSN_REG_23 = 23,
102 AARCH64_INSN_REG_24 = 24,
103 AARCH64_INSN_REG_25 = 25,
104 AARCH64_INSN_REG_26 = 26,
105 AARCH64_INSN_REG_27 = 27,
106 AARCH64_INSN_REG_28 = 28,
107 AARCH64_INSN_REG_29 = 29,
108 AARCH64_INSN_REG_FP = 29, /* Frame pointer */
109 AARCH64_INSN_REG_30 = 30,
110 AARCH64_INSN_REG_LR = 30, /* Link register */
111 AARCH64_INSN_REG_ZR = 31, /* Zero: as source register */
112 AARCH64_INSN_REG_SP = 31 /* Stack pointer: as load/store base reg */
113};
114
115enum aarch64_insn_variant {
116 AARCH64_INSN_VARIANT_32BIT,
117 AARCH64_INSN_VARIANT_64BIT
118};
119
Jiang Liu5c5bf252014-01-07 22:17:11 +0800120enum aarch64_insn_branch_type {
121 AARCH64_INSN_BRANCH_NOLINK,
122 AARCH64_INSN_BRANCH_LINK,
Zi Shen Limc0cafba2014-08-27 05:15:18 +0100123 AARCH64_INSN_BRANCH_RETURN,
Zi Shen Lim617d2fb2014-08-27 05:15:17 +0100124 AARCH64_INSN_BRANCH_COMP_ZERO,
125 AARCH64_INSN_BRANCH_COMP_NONZERO,
Jiang Liu5c5bf252014-01-07 22:17:11 +0800126};
127
Jiang Liub11a64a2014-01-07 22:17:08 +0800128#define __AARCH64_INSN_FUNCS(abbr, mask, val) \
129static __always_inline bool aarch64_insn_is_##abbr(u32 code) \
130{ return (code & (mask)) == (val); } \
131static __always_inline u32 aarch64_insn_get_##abbr##_value(void) \
132{ return (val); }
133
134__AARCH64_INSN_FUNCS(b, 0xFC000000, 0x14000000)
135__AARCH64_INSN_FUNCS(bl, 0xFC000000, 0x94000000)
Zi Shen Lim617d2fb2014-08-27 05:15:17 +0100136__AARCH64_INSN_FUNCS(cbz, 0xFE000000, 0x34000000)
137__AARCH64_INSN_FUNCS(cbnz, 0xFE000000, 0x35000000)
Jiang Liub11a64a2014-01-07 22:17:08 +0800138__AARCH64_INSN_FUNCS(svc, 0xFFE0001F, 0xD4000001)
139__AARCH64_INSN_FUNCS(hvc, 0xFFE0001F, 0xD4000002)
140__AARCH64_INSN_FUNCS(smc, 0xFFE0001F, 0xD4000003)
141__AARCH64_INSN_FUNCS(brk, 0xFFE0001F, 0xD4200000)
142__AARCH64_INSN_FUNCS(hint, 0xFFFFF01F, 0xD503201F)
Zi Shen Limc0cafba2014-08-27 05:15:18 +0100143__AARCH64_INSN_FUNCS(br, 0xFFFFFC1F, 0xD61F0000)
144__AARCH64_INSN_FUNCS(blr, 0xFFFFFC1F, 0xD63F0000)
145__AARCH64_INSN_FUNCS(ret, 0xFFFFFC1F, 0xD65F0000)
Jiang Liub11a64a2014-01-07 22:17:08 +0800146
147#undef __AARCH64_INSN_FUNCS
148
149bool aarch64_insn_is_nop(u32 insn);
150
Jiang Liuae164802014-01-07 22:17:09 +0800151int aarch64_insn_read(void *addr, u32 *insnp);
152int aarch64_insn_write(void *addr, u32 insn);
Jiang Liub11a64a2014-01-07 22:17:08 +0800153enum aarch64_insn_encoding_class aarch64_get_insn_class(u32 insn);
Jiang Liuc84fced2014-01-07 22:17:10 +0800154u32 aarch64_insn_encode_immediate(enum aarch64_insn_imm_type type,
155 u32 insn, u64 imm);
Jiang Liu5c5bf252014-01-07 22:17:11 +0800156u32 aarch64_insn_gen_branch_imm(unsigned long pc, unsigned long addr,
157 enum aarch64_insn_branch_type type);
Zi Shen Lim617d2fb2014-08-27 05:15:17 +0100158u32 aarch64_insn_gen_comp_branch_imm(unsigned long pc, unsigned long addr,
159 enum aarch64_insn_register reg,
160 enum aarch64_insn_variant variant,
161 enum aarch64_insn_branch_type type);
Jiang Liu5c5bf252014-01-07 22:17:11 +0800162u32 aarch64_insn_gen_hint(enum aarch64_insn_hint_op op);
163u32 aarch64_insn_gen_nop(void);
Zi Shen Limc0cafba2014-08-27 05:15:18 +0100164u32 aarch64_insn_gen_branch_reg(enum aarch64_insn_register reg,
165 enum aarch64_insn_branch_type type);
Jiang Liu5c5bf252014-01-07 22:17:11 +0800166
Jiang Liub11a64a2014-01-07 22:17:08 +0800167bool aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn);
168
Jiang Liuae164802014-01-07 22:17:09 +0800169int aarch64_insn_patch_text_nosync(void *addr, u32 insn);
170int aarch64_insn_patch_text_sync(void *addrs[], u32 insns[], int cnt);
171int aarch64_insn_patch_text(void *addrs[], u32 insns[], int cnt);
AKASHI Takahiro26e9b832014-04-30 10:54:30 +0100172#endif /* __ASSEMBLY__ */
Jiang Liuae164802014-01-07 22:17:09 +0800173
Jiang Liub11a64a2014-01-07 22:17:08 +0800174#endif /* __ASM_INSN_H */