2 * x86 instruction analysis
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * Copyright (C) IBM Corporation, 2002, 2004, 2009
21 #include <linux/string.h>
25 #define get_next(t, insn) \
26 ({t r; r = *(t*)insn->next_byte; insn->next_byte += sizeof(t); r; })
28 #define peek_next(t, insn) \
29 ({t r; r = *(t*)insn->next_byte; r; })
32 * insn_init() - initialize struct insn
33 * @insn: &struct insn to be initialized
34 * @kaddr: address (in kernel memory) of instruction (or copy thereof)
35 * @x86_64: !0 for 64-bit kernel or 64-bit app
37 void insn_init(struct insn *insn, const void *kaddr, int x86_64)
39 memset(insn, 0, sizeof(*insn));
41 insn->next_byte = kaddr;
42 insn->x86_64 = x86_64 ? 1 : 0;
51 * insn_get_prefixes - scan x86 instruction prefix bytes
52 * @insn: &struct insn containing instruction
54 * Populates the @insn->prefixes bitmap, and updates @insn->next_byte
55 * to point to the (first) opcode. No effect if @insn->prefixes.got
58 void insn_get_prefixes(struct insn *insn)
60 struct insn_field *prefixes = &insn->prefixes;
70 b = peek_next(insn_byte_t, insn);
71 attr = inat_get_opcode_attribute(b);
72 while (inat_is_prefix(attr)) {
73 /* Skip if same prefix */
74 for (i = 0; i < nb; i++)
75 if (prefixes->bytes[i] == b)
78 /* Invalid instruction */
80 prefixes->bytes[nb++] = b;
81 if (inat_is_address_size_prefix(attr)) {
82 /* address size switches 2/4 or 4/8 */
84 insn->addr_bytes ^= 12;
86 insn->addr_bytes ^= 6;
87 } else if (inat_is_operand_size_prefix(attr)) {
88 /* oprand size switches 2/4 */
89 insn->opnd_bytes ^= 6;
95 b = peek_next(insn_byte_t, insn);
96 attr = inat_get_opcode_attribute(b);
98 /* Set the last prefix */
99 if (lb && lb != insn->prefixes.bytes[3]) {
100 if (unlikely(insn->prefixes.bytes[3])) {
101 /* Swap the last prefix */
102 b = insn->prefixes.bytes[3];
103 for (i = 0; i < nb; i++)
104 if (prefixes->bytes[i] == lb)
105 prefixes->bytes[i] = b;
107 insn->prefixes.bytes[3] = lb;
111 b = peek_next(insn_byte_t, insn);
112 attr = inat_get_opcode_attribute(b);
113 if (inat_is_rex_prefix(attr)) {
114 insn->rex_prefix.value = b;
115 insn->rex_prefix.nbytes = 1;
118 /* REX.W overrides opnd_size */
119 insn->opnd_bytes = 8;
122 insn->rex_prefix.got = 1;
128 * insn_get_opcode - collect opcode(s)
129 * @insn: &struct insn containing instruction
131 * Populates @insn->opcode, updates @insn->next_byte to point past the
132 * opcode byte(s), and set @insn->attr (except for groups).
133 * If necessary, first collects any preceding (prefix) bytes.
134 * Sets @insn->opcode.value = opcode1. No effect if @insn->opcode.got
137 void insn_get_opcode(struct insn *insn)
139 struct insn_field *opcode = &insn->opcode;
143 if (!insn->prefixes.got)
144 insn_get_prefixes(insn);
146 /* Get first opcode */
147 op = get_next(insn_byte_t, insn);
148 opcode->bytes[0] = op;
150 insn->attr = inat_get_opcode_attribute(op);
151 while (inat_is_escape(insn->attr)) {
152 /* Get escaped opcode */
153 op = get_next(insn_byte_t, insn);
154 opcode->bytes[opcode->nbytes++] = op;
155 pfx = insn_last_prefix(insn);
156 insn->attr = inat_get_escape_attribute(op, pfx, insn->attr);
162 * insn_get_modrm - collect ModRM byte, if any
163 * @insn: &struct insn containing instruction
165 * Populates @insn->modrm and updates @insn->next_byte to point past the
166 * ModRM byte, if any. If necessary, first collects the preceding bytes
167 * (prefixes and opcode(s)). No effect if @insn->modrm.got is already 1.
169 void insn_get_modrm(struct insn *insn)
171 struct insn_field *modrm = &insn->modrm;
172 insn_byte_t pfx, mod;
175 if (!insn->opcode.got)
176 insn_get_opcode(insn);
178 if (inat_has_modrm(insn->attr)) {
179 mod = get_next(insn_byte_t, insn);
182 if (inat_is_group(insn->attr)) {
183 pfx = insn_last_prefix(insn);
184 insn->attr = inat_get_group_attribute(mod, pfx,
189 if (insn->x86_64 && inat_is_force64(insn->attr))
190 insn->opnd_bytes = 8;
196 * insn_rip_relative() - Does instruction use RIP-relative addressing mode?
197 * @insn: &struct insn containing instruction
199 * If necessary, first collects the instruction up to and including the
200 * ModRM byte. No effect if @insn->x86_64 is 0.
202 int insn_rip_relative(struct insn *insn)
204 struct insn_field *modrm = &insn->modrm;
209 insn_get_modrm(insn);
211 * For rip-relative instructions, the mod field (top 2 bits)
212 * is zero and the r/m field (bottom 3 bits) is 0x5.
214 return (modrm->nbytes && (modrm->value & 0xc7) == 0x5);
218 * insn_get_sib() - Get the SIB byte of instruction
219 * @insn: &struct insn containing instruction
221 * If necessary, first collects the instruction up to and including the
224 void insn_get_sib(struct insn *insn)
230 if (!insn->modrm.got)
231 insn_get_modrm(insn);
232 if (insn->modrm.nbytes) {
233 modrm = (insn_byte_t)insn->modrm.value;
234 if (insn->addr_bytes != 2 &&
235 X86_MODRM_MOD(modrm) != 3 && X86_MODRM_RM(modrm) == 4) {
236 insn->sib.value = get_next(insn_byte_t, insn);
237 insn->sib.nbytes = 1;
245 * insn_get_displacement() - Get the displacement of instruction
246 * @insn: &struct insn containing instruction
248 * If necessary, first collects the instruction up to and including the
250 * Displacement value is sign-expanded.
252 void insn_get_displacement(struct insn *insn)
254 insn_byte_t mod, rm, base;
256 if (insn->displacement.got)
260 if (insn->modrm.nbytes) {
262 * Interpreting the modrm byte:
263 * mod = 00 - no displacement fields (exceptions below)
264 * mod = 01 - 1-byte displacement field
265 * mod = 10 - displacement field is 4 bytes, or 2 bytes if
266 * address size = 2 (0x67 prefix in 32-bit mode)
267 * mod = 11 - no memory operand
269 * If address size = 2...
270 * mod = 00, r/m = 110 - displacement field is 2 bytes
272 * If address size != 2...
273 * mod != 11, r/m = 100 - SIB byte exists
274 * mod = 00, SIB base = 101 - displacement field is 4 bytes
275 * mod = 00, r/m = 101 - rip-relative addressing, displacement
278 mod = X86_MODRM_MOD(insn->modrm.value);
279 rm = X86_MODRM_RM(insn->modrm.value);
280 base = X86_SIB_BASE(insn->sib.value);
284 insn->displacement.value = get_next(char, insn);
285 insn->displacement.nbytes = 1;
286 } else if (insn->addr_bytes == 2) {
287 if ((mod == 0 && rm == 6) || mod == 2) {
288 insn->displacement.value =
289 get_next(short, insn);
290 insn->displacement.nbytes = 2;
293 if ((mod == 0 && rm == 5) || mod == 2 ||
294 (mod == 0 && base == 5)) {
295 insn->displacement.value = get_next(int, insn);
296 insn->displacement.nbytes = 4;
301 insn->displacement.got = 1;
304 /* Decode moffset16/32/64 */
305 static void __get_moffset(struct insn *insn)
307 switch (insn->addr_bytes) {
309 insn->moffset1.value = get_next(short, insn);
310 insn->moffset1.nbytes = 2;
313 insn->moffset1.value = get_next(int, insn);
314 insn->moffset1.nbytes = 4;
317 insn->moffset1.value = get_next(int, insn);
318 insn->moffset1.nbytes = 4;
319 insn->moffset2.value = get_next(int, insn);
320 insn->moffset2.nbytes = 4;
323 insn->moffset1.got = insn->moffset2.got = 1;
326 /* Decode imm v32(Iz) */
327 static void __get_immv32(struct insn *insn)
329 switch (insn->opnd_bytes) {
331 insn->immediate.value = get_next(short, insn);
332 insn->immediate.nbytes = 2;
336 insn->immediate.value = get_next(int, insn);
337 insn->immediate.nbytes = 4;
342 /* Decode imm v64(Iv/Ov) */
343 static void __get_immv(struct insn *insn)
345 switch (insn->opnd_bytes) {
347 insn->immediate1.value = get_next(short, insn);
348 insn->immediate1.nbytes = 2;
351 insn->immediate1.value = get_next(int, insn);
352 insn->immediate1.nbytes = 4;
355 insn->immediate1.value = get_next(int, insn);
356 insn->immediate1.nbytes = 4;
357 insn->immediate2.value = get_next(int, insn);
358 insn->immediate2.nbytes = 4;
361 insn->immediate1.got = insn->immediate2.got = 1;
364 /* Decode ptr16:16/32(Ap) */
365 static void __get_immptr(struct insn *insn)
367 switch (insn->opnd_bytes) {
369 insn->immediate1.value = get_next(short, insn);
370 insn->immediate1.nbytes = 2;
373 insn->immediate1.value = get_next(int, insn);
374 insn->immediate1.nbytes = 4;
377 /* ptr16:64 is not exist (no segment) */
380 insn->immediate2.value = get_next(unsigned short, insn);
381 insn->immediate2.nbytes = 2;
382 insn->immediate1.got = insn->immediate2.got = 1;
386 * insn_get_immediate() - Get the immediates of instruction
387 * @insn: &struct insn containing instruction
389 * If necessary, first collects the instruction up to and including the
390 * displacement bytes.
391 * Basically, most of immediates are sign-expanded. Unsigned-value can be
392 * get by bit masking with ((1 << (nbytes * 8)) - 1)
394 void insn_get_immediate(struct insn *insn)
396 if (insn->immediate.got)
398 if (!insn->displacement.got)
399 insn_get_displacement(insn);
401 if (inat_has_moffset(insn->attr)) {
406 if (!inat_has_immediate(insn->attr))
410 switch (inat_immediate_size(insn->attr)) {
412 insn->immediate.value = get_next(char, insn);
413 insn->immediate.nbytes = 1;
416 insn->immediate.value = get_next(short, insn);
417 insn->immediate.nbytes = 2;
420 insn->immediate.value = get_next(int, insn);
421 insn->immediate.nbytes = 4;
424 insn->immediate1.value = get_next(int, insn);
425 insn->immediate1.nbytes = 4;
426 insn->immediate2.value = get_next(int, insn);
427 insn->immediate2.nbytes = 4;
432 case INAT_IMM_VWORD32:
441 if (inat_has_second_immediate(insn->attr)) {
442 insn->immediate2.value = get_next(char, insn);
443 insn->immediate2.nbytes = 1;
446 insn->immediate.got = 1;
450 * insn_get_length() - Get the length of instruction
451 * @insn: &struct insn containing instruction
453 * If necessary, first collects the instruction up to and including the
456 void insn_get_length(struct insn *insn)
460 if (!insn->immediate.got)
461 insn_get_immediate(insn);
462 insn->length = (unsigned char)((unsigned long)insn->next_byte
463 - (unsigned long)insn->kaddr);