1#ifndef _ASM_X86_INSN_H
2#define _ASM_X86_INSN_H
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "inat.h"
25
26struct insn_field {
27 union {
28 insn_value_t value;
29 insn_byte_t bytes[4];
30 };
31
32 unsigned char got;
33 unsigned char nbytes;
34};
35
36struct insn {
37 struct insn_field prefixes;
38
39
40
41 struct insn_field rex_prefix;
42 struct insn_field vex_prefix;
43 struct insn_field opcode;
44
45
46
47
48 struct insn_field modrm;
49 struct insn_field sib;
50 struct insn_field displacement;
51 union {
52 struct insn_field immediate;
53 struct insn_field moffset1;
54 struct insn_field immediate1;
55 };
56 union {
57 struct insn_field moffset2;
58 struct insn_field immediate2;
59 };
60
61 insn_attr_t attr;
62 unsigned char opnd_bytes;
63 unsigned char addr_bytes;
64 unsigned char length;
65 unsigned char x86_64;
66
67 const insn_byte_t *kaddr;
68 const insn_byte_t *end_kaddr;
69 const insn_byte_t *next_byte;
70};
71
72#define MAX_INSN_SIZE 15
73
74#define X86_MODRM_MOD(modrm) (((modrm) & 0xc0) >> 6)
75#define X86_MODRM_REG(modrm) (((modrm) & 0x38) >> 3)
76#define X86_MODRM_RM(modrm) ((modrm) & 0x07)
77
78#define X86_SIB_SCALE(sib) (((sib) & 0xc0) >> 6)
79#define X86_SIB_INDEX(sib) (((sib) & 0x38) >> 3)
80#define X86_SIB_BASE(sib) ((sib) & 0x07)
81
82#define X86_REX_W(rex) ((rex) & 8)
83#define X86_REX_R(rex) ((rex) & 4)
84#define X86_REX_X(rex) ((rex) & 2)
85#define X86_REX_B(rex) ((rex) & 1)
86
87
88#define X86_VEX_W(vex) ((vex) & 0x80)
89#define X86_VEX_R(vex) ((vex) & 0x80)
90#define X86_VEX_X(vex) ((vex) & 0x40)
91#define X86_VEX_B(vex) ((vex) & 0x20)
92#define X86_VEX_L(vex) ((vex) & 0x04)
93
94#define X86_EVEX_M(vex) ((vex) & 0x03)
95#define X86_VEX3_M(vex) ((vex) & 0x1f)
96#define X86_VEX2_M 1
97#define X86_VEX_V(vex) (((vex) & 0x78) >> 3)
98#define X86_VEX_P(vex) ((vex) & 0x03)
99#define X86_VEX_M_MAX 0x1f
100
101extern void insn_init(struct insn *insn, const void *kaddr, int buf_len, int x86_64);
102extern void insn_get_prefixes(struct insn *insn);
103extern void insn_get_opcode(struct insn *insn);
104extern void insn_get_modrm(struct insn *insn);
105extern void insn_get_sib(struct insn *insn);
106extern void insn_get_displacement(struct insn *insn);
107extern void insn_get_immediate(struct insn *insn);
108extern void insn_get_length(struct insn *insn);
109
110
111static inline void insn_get_attribute(struct insn *insn)
112{
113 insn_get_modrm(insn);
114}
115
116
117extern int insn_rip_relative(struct insn *insn);
118
119
120static inline void kernel_insn_init(struct insn *insn,
121 const void *kaddr, int buf_len)
122{
123#ifdef CONFIG_X86_64
124 insn_init(insn, kaddr, buf_len, 1);
125#else
126 insn_init(insn, kaddr, buf_len, 0);
127#endif
128}
129
130static inline int insn_is_avx(struct insn *insn)
131{
132 if (!insn->prefixes.got)
133 insn_get_prefixes(insn);
134 return (insn->vex_prefix.value != 0);
135}
136
137static inline int insn_is_evex(struct insn *insn)
138{
139 if (!insn->prefixes.got)
140 insn_get_prefixes(insn);
141 return (insn->vex_prefix.nbytes == 4);
142}
143
144
145static inline int insn_complete(struct insn *insn)
146{
147 return insn->opcode.got && insn->modrm.got && insn->sib.got &&
148 insn->displacement.got && insn->immediate.got;
149}
150
151static inline insn_byte_t insn_vex_m_bits(struct insn *insn)
152{
153 if (insn->vex_prefix.nbytes == 2)
154 return X86_VEX2_M;
155 else if (insn->vex_prefix.nbytes == 3)
156 return X86_VEX3_M(insn->vex_prefix.bytes[1]);
157 else
158 return X86_EVEX_M(insn->vex_prefix.bytes[1]);
159}
160
161static inline insn_byte_t insn_vex_p_bits(struct insn *insn)
162{
163 if (insn->vex_prefix.nbytes == 2)
164 return X86_VEX_P(insn->vex_prefix.bytes[1]);
165 else
166 return X86_VEX_P(insn->vex_prefix.bytes[2]);
167}
168
169
170static inline int insn_last_prefix_id(struct insn *insn)
171{
172 if (insn_is_avx(insn))
173 return insn_vex_p_bits(insn);
174
175 if (insn->prefixes.bytes[3])
176 return inat_get_last_prefix_id(insn->prefixes.bytes[3]);
177
178 return 0;
179}
180
181
182static inline int insn_offset_rex_prefix(struct insn *insn)
183{
184 return insn->prefixes.nbytes;
185}
186static inline int insn_offset_vex_prefix(struct insn *insn)
187{
188 return insn_offset_rex_prefix(insn) + insn->rex_prefix.nbytes;
189}
190static inline int insn_offset_opcode(struct insn *insn)
191{
192 return insn_offset_vex_prefix(insn) + insn->vex_prefix.nbytes;
193}
194static inline int insn_offset_modrm(struct insn *insn)
195{
196 return insn_offset_opcode(insn) + insn->opcode.nbytes;
197}
198static inline int insn_offset_sib(struct insn *insn)
199{
200 return insn_offset_modrm(insn) + insn->modrm.nbytes;
201}
202static inline int insn_offset_displacement(struct insn *insn)
203{
204 return insn_offset_sib(insn) + insn->sib.nbytes;
205}
206static inline int insn_offset_immediate(struct insn *insn)
207{
208 return insn_offset_displacement(insn) + insn->displacement.nbytes;
209}
210
211#endif
212