1 #ifndef _ASM_X86_INAT_H
2 #define _ASM_X86_INAT_H
3 /*
4  * x86 instruction attributes
5  *
6  * Written by Masami Hiramatsu <mhiramat@redhat.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  *
22  */
23 #include <asm/inat_types.h>
24 
25 /*
26  * Internal bits. Don't use bitmasks directly, because these bits are
27  * unstable. You should use checking functions.
28  */
29 
30 #define INAT_OPCODE_TABLE_SIZE 256
31 #define INAT_GROUP_TABLE_SIZE 8
32 
33 /* Legacy last prefixes */
34 #define INAT_PFX_OPNDSZ	1	/* 0x66 */ /* LPFX1 */
35 #define INAT_PFX_REPE	2	/* 0xF3 */ /* LPFX2 */
36 #define INAT_PFX_REPNE	3	/* 0xF2 */ /* LPFX3 */
37 /* Other Legacy prefixes */
38 #define INAT_PFX_LOCK	4	/* 0xF0 */
39 #define INAT_PFX_CS	5	/* 0x2E */
40 #define INAT_PFX_DS	6	/* 0x3E */
41 #define INAT_PFX_ES	7	/* 0x26 */
42 #define INAT_PFX_FS	8	/* 0x64 */
43 #define INAT_PFX_GS	9	/* 0x65 */
44 #define INAT_PFX_SS	10	/* 0x36 */
45 #define INAT_PFX_ADDRSZ	11	/* 0x67 */
46 /* x86-64 REX prefix */
47 #define INAT_PFX_REX	12	/* 0x4X */
48 /* AVX VEX prefixes */
49 #define INAT_PFX_VEX2	13	/* 2-bytes VEX prefix */
50 #define INAT_PFX_VEX3	14	/* 3-bytes VEX prefix */
51 
52 #define INAT_LSTPFX_MAX	3
53 #define INAT_LGCPFX_MAX	11
54 
55 /* Immediate size */
56 #define INAT_IMM_BYTE		1
57 #define INAT_IMM_WORD		2
58 #define INAT_IMM_DWORD		3
59 #define INAT_IMM_QWORD		4
60 #define INAT_IMM_PTR		5
61 #define INAT_IMM_VWORD32	6
62 #define INAT_IMM_VWORD		7
63 
64 /* Legacy prefix */
65 #define INAT_PFX_OFFS	0
66 #define INAT_PFX_BITS	4
67 #define INAT_PFX_MAX    ((1 << INAT_PFX_BITS) - 1)
68 #define INAT_PFX_MASK	(INAT_PFX_MAX << INAT_PFX_OFFS)
69 /* Escape opcodes */
70 #define INAT_ESC_OFFS	(INAT_PFX_OFFS + INAT_PFX_BITS)
71 #define INAT_ESC_BITS	2
72 #define INAT_ESC_MAX	((1 << INAT_ESC_BITS) - 1)
73 #define INAT_ESC_MASK	(INAT_ESC_MAX << INAT_ESC_OFFS)
74 /* Group opcodes (1-16) */
75 #define INAT_GRP_OFFS	(INAT_ESC_OFFS + INAT_ESC_BITS)
76 #define INAT_GRP_BITS	5
77 #define INAT_GRP_MAX	((1 << INAT_GRP_BITS) - 1)
78 #define INAT_GRP_MASK	(INAT_GRP_MAX << INAT_GRP_OFFS)
79 /* Immediates */
80 #define INAT_IMM_OFFS	(INAT_GRP_OFFS + INAT_GRP_BITS)
81 #define INAT_IMM_BITS	3
82 #define INAT_IMM_MASK	(((1 << INAT_IMM_BITS) - 1) << INAT_IMM_OFFS)
83 /* Flags */
84 #define INAT_FLAG_OFFS	(INAT_IMM_OFFS + INAT_IMM_BITS)
85 #define INAT_MODRM	(1 << (INAT_FLAG_OFFS))
86 #define INAT_FORCE64	(1 << (INAT_FLAG_OFFS + 1))
87 #define INAT_SCNDIMM	(1 << (INAT_FLAG_OFFS + 2))
88 #define INAT_MOFFSET	(1 << (INAT_FLAG_OFFS + 3))
89 #define INAT_VARIANT	(1 << (INAT_FLAG_OFFS + 4))
90 #define INAT_VEXOK	(1 << (INAT_FLAG_OFFS + 5))
91 #define INAT_VEXONLY	(1 << (INAT_FLAG_OFFS + 6))
92 /* Attribute making macros for attribute tables */
93 #define INAT_MAKE_PREFIX(pfx)	(pfx << INAT_PFX_OFFS)
94 #define INAT_MAKE_ESCAPE(esc)	(esc << INAT_ESC_OFFS)
95 #define INAT_MAKE_GROUP(grp)	((grp << INAT_GRP_OFFS) | INAT_MODRM)
96 #define INAT_MAKE_IMM(imm)	(imm << INAT_IMM_OFFS)
97 
98 /* Attribute search APIs */
99 extern insn_attr_t inat_get_opcode_attribute(insn_byte_t opcode);
100 extern insn_attr_t inat_get_escape_attribute(insn_byte_t opcode,
101 					     insn_byte_t last_pfx,
102 					     insn_attr_t esc_attr);
103 extern insn_attr_t inat_get_group_attribute(insn_byte_t modrm,
104 					    insn_byte_t last_pfx,
105 					    insn_attr_t esc_attr);
106 extern insn_attr_t inat_get_avx_attribute(insn_byte_t opcode,
107 					  insn_byte_t vex_m,
108 					  insn_byte_t vex_pp);
109 
110 /* Attribute checking functions */
inat_is_legacy_prefix(insn_attr_t attr)111 static inline int inat_is_legacy_prefix(insn_attr_t attr)
112 {
113 	attr &= INAT_PFX_MASK;
114 	return attr && attr <= INAT_LGCPFX_MAX;
115 }
116 
inat_is_address_size_prefix(insn_attr_t attr)117 static inline int inat_is_address_size_prefix(insn_attr_t attr)
118 {
119 	return (attr & INAT_PFX_MASK) == INAT_PFX_ADDRSZ;
120 }
121 
inat_is_operand_size_prefix(insn_attr_t attr)122 static inline int inat_is_operand_size_prefix(insn_attr_t attr)
123 {
124 	return (attr & INAT_PFX_MASK) == INAT_PFX_OPNDSZ;
125 }
126 
inat_is_rex_prefix(insn_attr_t attr)127 static inline int inat_is_rex_prefix(insn_attr_t attr)
128 {
129 	return (attr & INAT_PFX_MASK) == INAT_PFX_REX;
130 }
131 
inat_last_prefix_id(insn_attr_t attr)132 static inline int inat_last_prefix_id(insn_attr_t attr)
133 {
134 	if ((attr & INAT_PFX_MASK) > INAT_LSTPFX_MAX)
135 		return 0;
136 	else
137 		return attr & INAT_PFX_MASK;
138 }
139 
inat_is_vex_prefix(insn_attr_t attr)140 static inline int inat_is_vex_prefix(insn_attr_t attr)
141 {
142 	attr &= INAT_PFX_MASK;
143 	return attr == INAT_PFX_VEX2 || attr == INAT_PFX_VEX3;
144 }
145 
inat_is_vex3_prefix(insn_attr_t attr)146 static inline int inat_is_vex3_prefix(insn_attr_t attr)
147 {
148 	return (attr & INAT_PFX_MASK) == INAT_PFX_VEX3;
149 }
150 
inat_is_escape(insn_attr_t attr)151 static inline int inat_is_escape(insn_attr_t attr)
152 {
153 	return attr & INAT_ESC_MASK;
154 }
155 
inat_escape_id(insn_attr_t attr)156 static inline int inat_escape_id(insn_attr_t attr)
157 {
158 	return (attr & INAT_ESC_MASK) >> INAT_ESC_OFFS;
159 }
160 
inat_is_group(insn_attr_t attr)161 static inline int inat_is_group(insn_attr_t attr)
162 {
163 	return attr & INAT_GRP_MASK;
164 }
165 
inat_group_id(insn_attr_t attr)166 static inline int inat_group_id(insn_attr_t attr)
167 {
168 	return (attr & INAT_GRP_MASK) >> INAT_GRP_OFFS;
169 }
170 
inat_group_common_attribute(insn_attr_t attr)171 static inline int inat_group_common_attribute(insn_attr_t attr)
172 {
173 	return attr & ~INAT_GRP_MASK;
174 }
175 
inat_has_immediate(insn_attr_t attr)176 static inline int inat_has_immediate(insn_attr_t attr)
177 {
178 	return attr & INAT_IMM_MASK;
179 }
180 
inat_immediate_size(insn_attr_t attr)181 static inline int inat_immediate_size(insn_attr_t attr)
182 {
183 	return (attr & INAT_IMM_MASK) >> INAT_IMM_OFFS;
184 }
185 
inat_has_modrm(insn_attr_t attr)186 static inline int inat_has_modrm(insn_attr_t attr)
187 {
188 	return attr & INAT_MODRM;
189 }
190 
inat_is_force64(insn_attr_t attr)191 static inline int inat_is_force64(insn_attr_t attr)
192 {
193 	return attr & INAT_FORCE64;
194 }
195 
inat_has_second_immediate(insn_attr_t attr)196 static inline int inat_has_second_immediate(insn_attr_t attr)
197 {
198 	return attr & INAT_SCNDIMM;
199 }
200 
inat_has_moffset(insn_attr_t attr)201 static inline int inat_has_moffset(insn_attr_t attr)
202 {
203 	return attr & INAT_MOFFSET;
204 }
205 
inat_has_variant(insn_attr_t attr)206 static inline int inat_has_variant(insn_attr_t attr)
207 {
208 	return attr & INAT_VARIANT;
209 }
210 
inat_accept_vex(insn_attr_t attr)211 static inline int inat_accept_vex(insn_attr_t attr)
212 {
213 	return attr & INAT_VEXOK;
214 }
215 
inat_must_vex(insn_attr_t attr)216 static inline int inat_must_vex(insn_attr_t attr)
217 {
218 	return attr & INAT_VEXONLY;
219 }
220 #endif
221