xref: /openbmc/linux/arch/x86/include/asm/inat.h (revision b8d312aa)
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 <asm/inat_types.h>
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 
39 #define INAT_LSTPFX_MAX	3
40 #define INAT_LGCPFX_MAX	11
41 
42 /* Immediate size */
43 #define INAT_IMM_BYTE		1
44 #define INAT_IMM_WORD		2
45 #define INAT_IMM_DWORD		3
46 #define INAT_IMM_QWORD		4
47 #define INAT_IMM_PTR		5
48 #define INAT_IMM_VWORD32	6
49 #define INAT_IMM_VWORD		7
50 
51 /* Legacy prefix */
52 #define INAT_PFX_OFFS	0
53 #define INAT_PFX_BITS	4
54 #define INAT_PFX_MAX    ((1 << INAT_PFX_BITS) - 1)
55 #define INAT_PFX_MASK	(INAT_PFX_MAX << INAT_PFX_OFFS)
56 /* Escape opcodes */
57 #define INAT_ESC_OFFS	(INAT_PFX_OFFS + INAT_PFX_BITS)
58 #define INAT_ESC_BITS	2
59 #define INAT_ESC_MAX	((1 << INAT_ESC_BITS) - 1)
60 #define INAT_ESC_MASK	(INAT_ESC_MAX << INAT_ESC_OFFS)
61 /* Group opcodes (1-16) */
62 #define INAT_GRP_OFFS	(INAT_ESC_OFFS + INAT_ESC_BITS)
63 #define INAT_GRP_BITS	5
64 #define INAT_GRP_MAX	((1 << INAT_GRP_BITS) - 1)
65 #define INAT_GRP_MASK	(INAT_GRP_MAX << INAT_GRP_OFFS)
66 /* Immediates */
67 #define INAT_IMM_OFFS	(INAT_GRP_OFFS + INAT_GRP_BITS)
68 #define INAT_IMM_BITS	3
69 #define INAT_IMM_MASK	(((1 << INAT_IMM_BITS) - 1) << INAT_IMM_OFFS)
70 /* Flags */
71 #define INAT_FLAG_OFFS	(INAT_IMM_OFFS + INAT_IMM_BITS)
72 #define INAT_MODRM	(1 << (INAT_FLAG_OFFS))
73 #define INAT_FORCE64	(1 << (INAT_FLAG_OFFS + 1))
74 #define INAT_SCNDIMM	(1 << (INAT_FLAG_OFFS + 2))
75 #define INAT_MOFFSET	(1 << (INAT_FLAG_OFFS + 3))
76 #define INAT_VARIANT	(1 << (INAT_FLAG_OFFS + 4))
77 #define INAT_VEXOK	(1 << (INAT_FLAG_OFFS + 5))
78 #define INAT_VEXONLY	(1 << (INAT_FLAG_OFFS + 6))
79 #define INAT_EVEXONLY	(1 << (INAT_FLAG_OFFS + 7))
80 /* Attribute making macros for attribute tables */
81 #define INAT_MAKE_PREFIX(pfx)	(pfx << INAT_PFX_OFFS)
82 #define INAT_MAKE_ESCAPE(esc)	(esc << INAT_ESC_OFFS)
83 #define INAT_MAKE_GROUP(grp)	((grp << INAT_GRP_OFFS) | INAT_MODRM)
84 #define INAT_MAKE_IMM(imm)	(imm << INAT_IMM_OFFS)
85 
86 /* Identifiers for segment registers */
87 #define INAT_SEG_REG_IGNORE	0
88 #define INAT_SEG_REG_DEFAULT	1
89 #define INAT_SEG_REG_CS		2
90 #define INAT_SEG_REG_SS		3
91 #define INAT_SEG_REG_DS		4
92 #define INAT_SEG_REG_ES		5
93 #define INAT_SEG_REG_FS		6
94 #define INAT_SEG_REG_GS		7
95 
96 /* Attribute search APIs */
97 extern insn_attr_t inat_get_opcode_attribute(insn_byte_t opcode);
98 extern int inat_get_last_prefix_id(insn_byte_t last_pfx);
99 extern insn_attr_t inat_get_escape_attribute(insn_byte_t opcode,
100 					     int lpfx_id,
101 					     insn_attr_t esc_attr);
102 extern insn_attr_t inat_get_group_attribute(insn_byte_t modrm,
103 					    int lpfx_id,
104 					    insn_attr_t esc_attr);
105 extern insn_attr_t inat_get_avx_attribute(insn_byte_t opcode,
106 					  insn_byte_t vex_m,
107 					  insn_byte_t vex_pp);
108 
109 /* Attribute checking functions */
110 static inline int inat_is_legacy_prefix(insn_attr_t attr)
111 {
112 	attr &= INAT_PFX_MASK;
113 	return attr && attr <= INAT_LGCPFX_MAX;
114 }
115 
116 static inline int inat_is_address_size_prefix(insn_attr_t attr)
117 {
118 	return (attr & INAT_PFX_MASK) == INAT_PFX_ADDRSZ;
119 }
120 
121 static inline int inat_is_operand_size_prefix(insn_attr_t attr)
122 {
123 	return (attr & INAT_PFX_MASK) == INAT_PFX_OPNDSZ;
124 }
125 
126 static inline int inat_is_rex_prefix(insn_attr_t attr)
127 {
128 	return (attr & INAT_PFX_MASK) == INAT_PFX_REX;
129 }
130 
131 static inline int inat_last_prefix_id(insn_attr_t attr)
132 {
133 	if ((attr & INAT_PFX_MASK) > INAT_LSTPFX_MAX)
134 		return 0;
135 	else
136 		return attr & INAT_PFX_MASK;
137 }
138 
139 static inline int inat_is_vex_prefix(insn_attr_t attr)
140 {
141 	attr &= INAT_PFX_MASK;
142 	return attr == INAT_PFX_VEX2 || attr == INAT_PFX_VEX3 ||
143 	       attr == INAT_PFX_EVEX;
144 }
145 
146 static inline int inat_is_evex_prefix(insn_attr_t attr)
147 {
148 	return (attr & INAT_PFX_MASK) == INAT_PFX_EVEX;
149 }
150 
151 static inline int inat_is_vex3_prefix(insn_attr_t attr)
152 {
153 	return (attr & INAT_PFX_MASK) == INAT_PFX_VEX3;
154 }
155 
156 static inline int inat_is_escape(insn_attr_t attr)
157 {
158 	return attr & INAT_ESC_MASK;
159 }
160 
161 static inline int inat_escape_id(insn_attr_t attr)
162 {
163 	return (attr & INAT_ESC_MASK) >> INAT_ESC_OFFS;
164 }
165 
166 static inline int inat_is_group(insn_attr_t attr)
167 {
168 	return attr & INAT_GRP_MASK;
169 }
170 
171 static inline int inat_group_id(insn_attr_t attr)
172 {
173 	return (attr & INAT_GRP_MASK) >> INAT_GRP_OFFS;
174 }
175 
176 static inline int inat_group_common_attribute(insn_attr_t attr)
177 {
178 	return attr & ~INAT_GRP_MASK;
179 }
180 
181 static inline int inat_has_immediate(insn_attr_t attr)
182 {
183 	return attr & INAT_IMM_MASK;
184 }
185 
186 static inline int inat_immediate_size(insn_attr_t attr)
187 {
188 	return (attr & INAT_IMM_MASK) >> INAT_IMM_OFFS;
189 }
190 
191 static inline int inat_has_modrm(insn_attr_t attr)
192 {
193 	return attr & INAT_MODRM;
194 }
195 
196 static inline int inat_is_force64(insn_attr_t attr)
197 {
198 	return attr & INAT_FORCE64;
199 }
200 
201 static inline int inat_has_second_immediate(insn_attr_t attr)
202 {
203 	return attr & INAT_SCNDIMM;
204 }
205 
206 static inline int inat_has_moffset(insn_attr_t attr)
207 {
208 	return attr & INAT_MOFFSET;
209 }
210 
211 static inline int inat_has_variant(insn_attr_t attr)
212 {
213 	return attr & INAT_VARIANT;
214 }
215 
216 static inline int inat_accept_vex(insn_attr_t attr)
217 {
218 	return attr & INAT_VEXOK;
219 }
220 
221 static inline int inat_must_vex(insn_attr_t attr)
222 {
223 	return attr & (INAT_VEXONLY | INAT_EVEXONLY);
224 }
225 
226 static inline int inat_must_evex(insn_attr_t attr)
227 {
228 	return attr & INAT_EVEXONLY;
229 }
230 #endif
231