xref: /linux/tools/arch/x86/include/asm/inat.h (revision 3f41368fbfe1b3d5922d317fe1a0a0cab6846802)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _ASM_X86_INAT_H
3 #define _ASM_X86_INAT_H
4 /*
5  * x86 instruction attributes
6  *
7  * Written by Masami Hiramatsu <mhiramat@redhat.com>
8  */
9 #include "inat_types.h" /* __ignore_sync_check__ */
10 
11 /*
12  * Internal bits. Don't use bitmasks directly, because these bits are
13  * unstable. You should use checking functions.
14  */
15 
16 #define INAT_OPCODE_TABLE_SIZE 256
17 #define INAT_GROUP_TABLE_SIZE 8
18 
19 /* Legacy last prefixes */
20 #define INAT_PFX_OPNDSZ	1	/* 0x66 */ /* LPFX1 */
21 #define INAT_PFX_REPE	2	/* 0xF3 */ /* LPFX2 */
22 #define INAT_PFX_REPNE	3	/* 0xF2 */ /* LPFX3 */
23 /* Other Legacy prefixes */
24 #define INAT_PFX_LOCK	4	/* 0xF0 */
25 #define INAT_PFX_CS	5	/* 0x2E */
26 #define INAT_PFX_DS	6	/* 0x3E */
27 #define INAT_PFX_ES	7	/* 0x26 */
28 #define INAT_PFX_FS	8	/* 0x64 */
29 #define INAT_PFX_GS	9	/* 0x65 */
30 #define INAT_PFX_SS	10	/* 0x36 */
31 #define INAT_PFX_ADDRSZ	11	/* 0x67 */
32 /* x86-64 REX prefix */
33 #define INAT_PFX_REX	12	/* 0x4X */
34 /* AVX VEX prefixes */
35 #define INAT_PFX_VEX2	13	/* 2-bytes VEX prefix */
36 #define INAT_PFX_VEX3	14	/* 3-bytes VEX prefix */
37 #define INAT_PFX_EVEX	15	/* EVEX prefix */
38 /* x86-64 REX2 prefix */
39 #define INAT_PFX_REX2	16	/* 0xD5 */
40 
41 #define INAT_LSTPFX_MAX	3
42 #define INAT_LGCPFX_MAX	11
43 
44 /* Immediate size */
45 #define INAT_IMM_BYTE		1
46 #define INAT_IMM_WORD		2
47 #define INAT_IMM_DWORD		3
48 #define INAT_IMM_QWORD		4
49 #define INAT_IMM_PTR		5
50 #define INAT_IMM_VWORD32	6
51 #define INAT_IMM_VWORD		7
52 
53 /* Legacy prefix */
54 #define INAT_PFX_OFFS	0
55 #define INAT_PFX_BITS	5
56 #define INAT_PFX_MAX    ((1 << INAT_PFX_BITS) - 1)
57 #define INAT_PFX_MASK	(INAT_PFX_MAX << INAT_PFX_OFFS)
58 /* Escape opcodes */
59 #define INAT_ESC_OFFS	(INAT_PFX_OFFS + INAT_PFX_BITS)
60 #define INAT_ESC_BITS	2
61 #define INAT_ESC_MAX	((1 << INAT_ESC_BITS) - 1)
62 #define INAT_ESC_MASK	(INAT_ESC_MAX << INAT_ESC_OFFS)
63 /* Group opcodes (1-16) */
64 #define INAT_GRP_OFFS	(INAT_ESC_OFFS + INAT_ESC_BITS)
65 #define INAT_GRP_BITS	5
66 #define INAT_GRP_MAX	((1 << INAT_GRP_BITS) - 1)
67 #define INAT_GRP_MASK	(INAT_GRP_MAX << INAT_GRP_OFFS)
68 /* Immediates */
69 #define INAT_IMM_OFFS	(INAT_GRP_OFFS + INAT_GRP_BITS)
70 #define INAT_IMM_BITS	3
71 #define INAT_IMM_MASK	(((1 << INAT_IMM_BITS) - 1) << INAT_IMM_OFFS)
72 /* Flags */
73 #define INAT_FLAG_OFFS	(INAT_IMM_OFFS + INAT_IMM_BITS)
74 #define INAT_MODRM	(1 << (INAT_FLAG_OFFS))
75 #define INAT_FORCE64	(1 << (INAT_FLAG_OFFS + 1))
76 #define INAT_SCNDIMM	(1 << (INAT_FLAG_OFFS + 2))
77 #define INAT_MOFFSET	(1 << (INAT_FLAG_OFFS + 3))
78 #define INAT_VARIANT	(1 << (INAT_FLAG_OFFS + 4))
79 #define INAT_VEXOK	(1 << (INAT_FLAG_OFFS + 5))
80 #define INAT_VEXONLY	(1 << (INAT_FLAG_OFFS + 6))
81 #define INAT_EVEXONLY	(1 << (INAT_FLAG_OFFS + 7))
82 #define INAT_NO_REX2	(1 << (INAT_FLAG_OFFS + 8))
83 #define INAT_REX2_VARIANT	(1 << (INAT_FLAG_OFFS + 9))
84 #define INAT_EVEX_SCALABLE	(1 << (INAT_FLAG_OFFS + 10))
85 /* Attribute making macros for attribute tables */
86 #define INAT_MAKE_PREFIX(pfx)	(pfx << INAT_PFX_OFFS)
87 #define INAT_MAKE_ESCAPE(esc)	(esc << INAT_ESC_OFFS)
88 #define INAT_MAKE_GROUP(grp)	((grp << INAT_GRP_OFFS) | INAT_MODRM)
89 #define INAT_MAKE_IMM(imm)	(imm << INAT_IMM_OFFS)
90 
91 /* Identifiers for segment registers */
92 #define INAT_SEG_REG_IGNORE	0
93 #define INAT_SEG_REG_DEFAULT	1
94 #define INAT_SEG_REG_CS		2
95 #define INAT_SEG_REG_SS		3
96 #define INAT_SEG_REG_DS		4
97 #define INAT_SEG_REG_ES		5
98 #define INAT_SEG_REG_FS		6
99 #define INAT_SEG_REG_GS		7
100 
101 /* Attribute search APIs */
102 extern insn_attr_t inat_get_opcode_attribute(insn_byte_t opcode);
103 extern int inat_get_last_prefix_id(insn_byte_t last_pfx);
104 extern insn_attr_t inat_get_escape_attribute(insn_byte_t opcode,
105 					     int lpfx_id,
106 					     insn_attr_t esc_attr);
107 extern insn_attr_t inat_get_group_attribute(insn_byte_t modrm,
108 					    int lpfx_id,
109 					    insn_attr_t esc_attr);
110 extern insn_attr_t inat_get_avx_attribute(insn_byte_t opcode,
111 					  insn_byte_t vex_m,
112 					  insn_byte_t vex_pp);
113 
114 /* Attribute checking functions */
115 static inline int inat_is_legacy_prefix(insn_attr_t attr)
116 {
117 	attr &= INAT_PFX_MASK;
118 	return attr && attr <= INAT_LGCPFX_MAX;
119 }
120 
121 static inline int inat_is_address_size_prefix(insn_attr_t attr)
122 {
123 	return (attr & INAT_PFX_MASK) == INAT_PFX_ADDRSZ;
124 }
125 
126 static inline int inat_is_operand_size_prefix(insn_attr_t attr)
127 {
128 	return (attr & INAT_PFX_MASK) == INAT_PFX_OPNDSZ;
129 }
130 
131 static inline int inat_is_rex_prefix(insn_attr_t attr)
132 {
133 	return (attr & INAT_PFX_MASK) == INAT_PFX_REX;
134 }
135 
136 static inline int inat_is_rex2_prefix(insn_attr_t attr)
137 {
138 	return (attr & INAT_PFX_MASK) == INAT_PFX_REX2;
139 }
140 
141 static inline int inat_last_prefix_id(insn_attr_t attr)
142 {
143 	if ((attr & INAT_PFX_MASK) > INAT_LSTPFX_MAX)
144 		return 0;
145 	else
146 		return attr & INAT_PFX_MASK;
147 }
148 
149 static inline int inat_is_vex_prefix(insn_attr_t attr)
150 {
151 	attr &= INAT_PFX_MASK;
152 	return attr == INAT_PFX_VEX2 || attr == INAT_PFX_VEX3 ||
153 	       attr == INAT_PFX_EVEX;
154 }
155 
156 static inline int inat_is_evex_prefix(insn_attr_t attr)
157 {
158 	return (attr & INAT_PFX_MASK) == INAT_PFX_EVEX;
159 }
160 
161 static inline int inat_is_vex3_prefix(insn_attr_t attr)
162 {
163 	return (attr & INAT_PFX_MASK) == INAT_PFX_VEX3;
164 }
165 
166 static inline int inat_is_escape(insn_attr_t attr)
167 {
168 	return attr & INAT_ESC_MASK;
169 }
170 
171 static inline int inat_escape_id(insn_attr_t attr)
172 {
173 	return (attr & INAT_ESC_MASK) >> INAT_ESC_OFFS;
174 }
175 
176 static inline int inat_is_group(insn_attr_t attr)
177 {
178 	return attr & INAT_GRP_MASK;
179 }
180 
181 static inline int inat_group_id(insn_attr_t attr)
182 {
183 	return (attr & INAT_GRP_MASK) >> INAT_GRP_OFFS;
184 }
185 
186 static inline int inat_group_common_attribute(insn_attr_t attr)
187 {
188 	return attr & ~INAT_GRP_MASK;
189 }
190 
191 static inline int inat_has_immediate(insn_attr_t attr)
192 {
193 	return attr & INAT_IMM_MASK;
194 }
195 
196 static inline int inat_immediate_size(insn_attr_t attr)
197 {
198 	return (attr & INAT_IMM_MASK) >> INAT_IMM_OFFS;
199 }
200 
201 static inline int inat_has_modrm(insn_attr_t attr)
202 {
203 	return attr & INAT_MODRM;
204 }
205 
206 static inline int inat_is_force64(insn_attr_t attr)
207 {
208 	return attr & INAT_FORCE64;
209 }
210 
211 static inline int inat_has_second_immediate(insn_attr_t attr)
212 {
213 	return attr & INAT_SCNDIMM;
214 }
215 
216 static inline int inat_has_moffset(insn_attr_t attr)
217 {
218 	return attr & INAT_MOFFSET;
219 }
220 
221 static inline int inat_has_variant(insn_attr_t attr)
222 {
223 	return attr & INAT_VARIANT;
224 }
225 
226 static inline int inat_accept_vex(insn_attr_t attr)
227 {
228 	return attr & INAT_VEXOK;
229 }
230 
231 static inline int inat_must_vex(insn_attr_t attr)
232 {
233 	return attr & (INAT_VEXONLY | INAT_EVEXONLY);
234 }
235 
236 static inline int inat_must_evex(insn_attr_t attr)
237 {
238 	return attr & INAT_EVEXONLY;
239 }
240 
241 static inline int inat_evex_scalable(insn_attr_t attr)
242 {
243 	return attr & INAT_EVEX_SCALABLE;
244 }
245 #endif
246