xref: /openbmc/linux/arch/arm64/include/asm/insn.h (revision 617d2fbc)
1 /*
2  * Copyright (C) 2013 Huawei Ltd.
3  * Author: Jiang Liu <liuj97@gmail.com>
4  *
5  * Copyright (C) 2014 Zi Shen Lim <zlim.lnx@gmail.com>
6  *
7  * 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 
23 /* A64 instructions are always 32 bits. */
24 #define	AARCH64_INSN_SIZE		4
25 
26 #ifndef __ASSEMBLY__
27 /*
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  */
42 enum 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 
52 enum 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 
61 enum 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 
72 enum aarch64_insn_register_type {
73 	AARCH64_INSN_REGTYPE_RT,
74 };
75 
76 enum aarch64_insn_register {
77 	AARCH64_INSN_REG_0  = 0,
78 	AARCH64_INSN_REG_1  = 1,
79 	AARCH64_INSN_REG_2  = 2,
80 	AARCH64_INSN_REG_3  = 3,
81 	AARCH64_INSN_REG_4  = 4,
82 	AARCH64_INSN_REG_5  = 5,
83 	AARCH64_INSN_REG_6  = 6,
84 	AARCH64_INSN_REG_7  = 7,
85 	AARCH64_INSN_REG_8  = 8,
86 	AARCH64_INSN_REG_9  = 9,
87 	AARCH64_INSN_REG_10 = 10,
88 	AARCH64_INSN_REG_11 = 11,
89 	AARCH64_INSN_REG_12 = 12,
90 	AARCH64_INSN_REG_13 = 13,
91 	AARCH64_INSN_REG_14 = 14,
92 	AARCH64_INSN_REG_15 = 15,
93 	AARCH64_INSN_REG_16 = 16,
94 	AARCH64_INSN_REG_17 = 17,
95 	AARCH64_INSN_REG_18 = 18,
96 	AARCH64_INSN_REG_19 = 19,
97 	AARCH64_INSN_REG_20 = 20,
98 	AARCH64_INSN_REG_21 = 21,
99 	AARCH64_INSN_REG_22 = 22,
100 	AARCH64_INSN_REG_23 = 23,
101 	AARCH64_INSN_REG_24 = 24,
102 	AARCH64_INSN_REG_25 = 25,
103 	AARCH64_INSN_REG_26 = 26,
104 	AARCH64_INSN_REG_27 = 27,
105 	AARCH64_INSN_REG_28 = 28,
106 	AARCH64_INSN_REG_29 = 29,
107 	AARCH64_INSN_REG_FP = 29, /* Frame pointer */
108 	AARCH64_INSN_REG_30 = 30,
109 	AARCH64_INSN_REG_LR = 30, /* Link register */
110 	AARCH64_INSN_REG_ZR = 31, /* Zero: as source register */
111 	AARCH64_INSN_REG_SP = 31  /* Stack pointer: as load/store base reg */
112 };
113 
114 enum aarch64_insn_variant {
115 	AARCH64_INSN_VARIANT_32BIT,
116 	AARCH64_INSN_VARIANT_64BIT
117 };
118 
119 enum aarch64_insn_branch_type {
120 	AARCH64_INSN_BRANCH_NOLINK,
121 	AARCH64_INSN_BRANCH_LINK,
122 	AARCH64_INSN_BRANCH_COMP_ZERO,
123 	AARCH64_INSN_BRANCH_COMP_NONZERO,
124 };
125 
126 #define	__AARCH64_INSN_FUNCS(abbr, mask, val)	\
127 static __always_inline bool aarch64_insn_is_##abbr(u32 code) \
128 { return (code & (mask)) == (val); } \
129 static __always_inline u32 aarch64_insn_get_##abbr##_value(void) \
130 { return (val); }
131 
132 __AARCH64_INSN_FUNCS(b,		0xFC000000, 0x14000000)
133 __AARCH64_INSN_FUNCS(bl,	0xFC000000, 0x94000000)
134 __AARCH64_INSN_FUNCS(cbz,	0xFE000000, 0x34000000)
135 __AARCH64_INSN_FUNCS(cbnz,	0xFE000000, 0x35000000)
136 __AARCH64_INSN_FUNCS(svc,	0xFFE0001F, 0xD4000001)
137 __AARCH64_INSN_FUNCS(hvc,	0xFFE0001F, 0xD4000002)
138 __AARCH64_INSN_FUNCS(smc,	0xFFE0001F, 0xD4000003)
139 __AARCH64_INSN_FUNCS(brk,	0xFFE0001F, 0xD4200000)
140 __AARCH64_INSN_FUNCS(hint,	0xFFFFF01F, 0xD503201F)
141 
142 #undef	__AARCH64_INSN_FUNCS
143 
144 bool aarch64_insn_is_nop(u32 insn);
145 
146 int aarch64_insn_read(void *addr, u32 *insnp);
147 int aarch64_insn_write(void *addr, u32 insn);
148 enum aarch64_insn_encoding_class aarch64_get_insn_class(u32 insn);
149 u32 aarch64_insn_encode_immediate(enum aarch64_insn_imm_type type,
150 				  u32 insn, u64 imm);
151 u32 aarch64_insn_gen_branch_imm(unsigned long pc, unsigned long addr,
152 				enum aarch64_insn_branch_type type);
153 u32 aarch64_insn_gen_comp_branch_imm(unsigned long pc, unsigned long addr,
154 				     enum aarch64_insn_register reg,
155 				     enum aarch64_insn_variant variant,
156 				     enum aarch64_insn_branch_type type);
157 u32 aarch64_insn_gen_hint(enum aarch64_insn_hint_op op);
158 u32 aarch64_insn_gen_nop(void);
159 
160 bool aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn);
161 
162 int aarch64_insn_patch_text_nosync(void *addr, u32 insn);
163 int aarch64_insn_patch_text_sync(void *addrs[], u32 insns[], int cnt);
164 int aarch64_insn_patch_text(void *addrs[], u32 insns[], int cnt);
165 #endif /* __ASSEMBLY__ */
166 
167 #endif	/* __ASM_INSN_H */
168