blob: c2487d2aca258a35b2359f5b6f299297bdb1523b [file] [log] [blame]
Masami Hiramatsueb132962009-08-13 16:34:13 -04001#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
Masami Hiramatsu04d46c12009-10-27 16:42:11 -040033/* Legacy last prefixes */
Masami Hiramatsueb132962009-08-13 16:34:13 -040034#define INAT_PFX_OPNDSZ 1 /* 0x66 */ /* LPFX1 */
35#define INAT_PFX_REPNE 2 /* 0xF2 */ /* LPFX2 */
36#define INAT_PFX_REPE 3 /* 0xF3 */ /* LPFX3 */
Masami Hiramatsu04d46c12009-10-27 16:42:11 -040037/* Other Legacy prefixes */
Masami Hiramatsueb132962009-08-13 16:34:13 -040038#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 */
Masami Hiramatsu04d46c12009-10-27 16:42:11 -040046/* x86-64 REX prefix */
47#define INAT_PFX_REX 12 /* 0x4X */
Masami Hiramatsueb132962009-08-13 16:34:13 -040048
Masami Hiramatsu04d46c12009-10-27 16:42:11 -040049#define INAT_LSTPFX_MAX 3
50#define INAT_LGCPFX_MAX 11
Masami Hiramatsueb132962009-08-13 16:34:13 -040051
52/* Immediate size */
53#define INAT_IMM_BYTE 1
54#define INAT_IMM_WORD 2
55#define INAT_IMM_DWORD 3
56#define INAT_IMM_QWORD 4
57#define INAT_IMM_PTR 5
58#define INAT_IMM_VWORD32 6
59#define INAT_IMM_VWORD 7
60
61/* Legacy prefix */
62#define INAT_PFX_OFFS 0
63#define INAT_PFX_BITS 4
64#define INAT_PFX_MAX ((1 << INAT_PFX_BITS) - 1)
65#define INAT_PFX_MASK (INAT_PFX_MAX << INAT_PFX_OFFS)
66/* Escape opcodes */
67#define INAT_ESC_OFFS (INAT_PFX_OFFS + INAT_PFX_BITS)
68#define INAT_ESC_BITS 2
69#define INAT_ESC_MAX ((1 << INAT_ESC_BITS) - 1)
70#define INAT_ESC_MASK (INAT_ESC_MAX << INAT_ESC_OFFS)
71/* Group opcodes (1-16) */
72#define INAT_GRP_OFFS (INAT_ESC_OFFS + INAT_ESC_BITS)
73#define INAT_GRP_BITS 5
74#define INAT_GRP_MAX ((1 << INAT_GRP_BITS) - 1)
75#define INAT_GRP_MASK (INAT_GRP_MAX << INAT_GRP_OFFS)
76/* Immediates */
77#define INAT_IMM_OFFS (INAT_GRP_OFFS + INAT_GRP_BITS)
78#define INAT_IMM_BITS 3
79#define INAT_IMM_MASK (((1 << INAT_IMM_BITS) - 1) << INAT_IMM_OFFS)
80/* Flags */
81#define INAT_FLAG_OFFS (INAT_IMM_OFFS + INAT_IMM_BITS)
Masami Hiramatsu04d46c12009-10-27 16:42:11 -040082#define INAT_MODRM (1 << (INAT_FLAG_OFFS))
83#define INAT_FORCE64 (1 << (INAT_FLAG_OFFS + 1))
84#define INAT_SCNDIMM (1 << (INAT_FLAG_OFFS + 2))
85#define INAT_MOFFSET (1 << (INAT_FLAG_OFFS + 3))
86#define INAT_VARIANT (1 << (INAT_FLAG_OFFS + 4))
Masami Hiramatsueb132962009-08-13 16:34:13 -040087/* Attribute making macros for attribute tables */
88#define INAT_MAKE_PREFIX(pfx) (pfx << INAT_PFX_OFFS)
89#define INAT_MAKE_ESCAPE(esc) (esc << INAT_ESC_OFFS)
90#define INAT_MAKE_GROUP(grp) ((grp << INAT_GRP_OFFS) | INAT_MODRM)
91#define INAT_MAKE_IMM(imm) (imm << INAT_IMM_OFFS)
92
93/* Attribute search APIs */
94extern insn_attr_t inat_get_opcode_attribute(insn_byte_t opcode);
95extern insn_attr_t inat_get_escape_attribute(insn_byte_t opcode,
96 insn_byte_t last_pfx,
97 insn_attr_t esc_attr);
98extern insn_attr_t inat_get_group_attribute(insn_byte_t modrm,
99 insn_byte_t last_pfx,
100 insn_attr_t esc_attr);
101
102/* Attribute checking functions */
Masami Hiramatsu04d46c12009-10-27 16:42:11 -0400103static inline int inat_is_legacy_prefix(insn_attr_t attr)
Masami Hiramatsueb132962009-08-13 16:34:13 -0400104{
Masami Hiramatsu04d46c12009-10-27 16:42:11 -0400105 attr &= INAT_PFX_MASK;
106 return attr && attr <= INAT_LGCPFX_MAX;
Masami Hiramatsueb132962009-08-13 16:34:13 -0400107}
108
109static inline int inat_is_address_size_prefix(insn_attr_t attr)
110{
111 return (attr & INAT_PFX_MASK) == INAT_PFX_ADDRSZ;
112}
113
114static inline int inat_is_operand_size_prefix(insn_attr_t attr)
115{
116 return (attr & INAT_PFX_MASK) == INAT_PFX_OPNDSZ;
117}
118
Masami Hiramatsu04d46c12009-10-27 16:42:11 -0400119static inline int inat_is_rex_prefix(insn_attr_t attr)
120{
121 return (attr & INAT_PFX_MASK) == INAT_PFX_REX;
122}
123
Masami Hiramatsueb132962009-08-13 16:34:13 -0400124static inline int inat_last_prefix_id(insn_attr_t attr)
125{
Masami Hiramatsu04d46c12009-10-27 16:42:11 -0400126 if ((attr & INAT_PFX_MASK) > INAT_LSTPFX_MAX)
Masami Hiramatsueb132962009-08-13 16:34:13 -0400127 return 0;
128 else
129 return attr & INAT_PFX_MASK;
130}
131
132static inline int inat_is_escape(insn_attr_t attr)
133{
134 return attr & INAT_ESC_MASK;
135}
136
137static inline int inat_escape_id(insn_attr_t attr)
138{
139 return (attr & INAT_ESC_MASK) >> INAT_ESC_OFFS;
140}
141
142static inline int inat_is_group(insn_attr_t attr)
143{
144 return attr & INAT_GRP_MASK;
145}
146
147static inline int inat_group_id(insn_attr_t attr)
148{
149 return (attr & INAT_GRP_MASK) >> INAT_GRP_OFFS;
150}
151
152static inline int inat_group_common_attribute(insn_attr_t attr)
153{
154 return attr & ~INAT_GRP_MASK;
155}
156
157static inline int inat_has_immediate(insn_attr_t attr)
158{
159 return attr & INAT_IMM_MASK;
160}
161
162static inline int inat_immediate_size(insn_attr_t attr)
163{
164 return (attr & INAT_IMM_MASK) >> INAT_IMM_OFFS;
165}
166
Masami Hiramatsueb132962009-08-13 16:34:13 -0400167static inline int inat_has_modrm(insn_attr_t attr)
168{
169 return attr & INAT_MODRM;
170}
171
172static inline int inat_is_force64(insn_attr_t attr)
173{
174 return attr & INAT_FORCE64;
175}
176
177static inline int inat_has_second_immediate(insn_attr_t attr)
178{
179 return attr & INAT_SCNDIMM;
180}
181
182static inline int inat_has_moffset(insn_attr_t attr)
183{
184 return attr & INAT_MOFFSET;
185}
186
187static inline int inat_has_variant(insn_attr_t attr)
188{
189 return attr & INAT_VARIANT;
190}
191
192#endif