blob: bb24d331317dc004f77162be4a9160e7cfc4e473 [file] [log] [blame]
Sean Callanan8ed9f512009-12-19 02:59:52 +00001/*===- X86DisassemblerDecoder.c - Disassembler decoder -------------*- C -*-==*
2 *
3 * The LLVM Compiler Infrastructure
4 *
5 * This file is distributed under the University of Illinois Open Source
6 * License. See LICENSE.TXT for details.
7 *
8 *===----------------------------------------------------------------------===*
9 *
10 * This file is part of the X86 Disassembler.
11 * It contains the implementation of the instruction decoder.
12 * Documentation for the disassembler can be found in X86Disassembler.h.
13 *
14 *===----------------------------------------------------------------------===*/
15
Sean Callanan8ed9f512009-12-19 02:59:52 +000016#include <stdarg.h> /* for va_*() */
17#include <stdio.h> /* for vsnprintf() */
18#include <stdlib.h> /* for exit() */
Daniel Dunbar71f842d2009-12-19 03:31:50 +000019#include <string.h> /* for memset() */
Sean Callanan8ed9f512009-12-19 02:59:52 +000020
21#include "X86DisassemblerDecoder.h"
22
23#include "X86GenDisassemblerTables.inc"
24
25#define TRUE 1
26#define FALSE 0
27
Sean Callanana144c3f2010-04-02 21:23:51 +000028typedef int8_t bool;
29
Sean Callanana144c3f2010-04-02 21:23:51 +000030#ifndef NDEBUG
31#define debug(s) do { x86DisassemblerDebug(__FILE__, __LINE__, s); } while (0)
32#else
33#define debug(s) do { } while (0)
34#endif
35
Sean Callanan8ed9f512009-12-19 02:59:52 +000036
37/*
38 * contextForAttrs - Client for the instruction context table. Takes a set of
39 * attributes and returns the appropriate decode context.
40 *
41 * @param attrMask - Attributes, from the enumeration attributeBits.
42 * @return - The InstructionContext to use when looking up an
43 * an instruction with these attributes.
44 */
Sean Callanan542eabc2009-12-22 22:51:40 +000045static InstructionContext contextForAttrs(uint8_t attrMask) {
Sean Callanan8ed9f512009-12-19 02:59:52 +000046 return CONTEXTS_SYM[attrMask];
47}
48
49/*
50 * modRMRequired - Reads the appropriate instruction table to determine whether
51 * the ModR/M byte is required to decode a particular instruction.
52 *
53 * @param type - The opcode type (i.e., how many bytes it has).
54 * @param insnContext - The context for the instruction, as returned by
55 * contextForAttrs.
56 * @param opcode - The last byte of the instruction's opcode, not counting
57 * ModR/M extensions and escapes.
58 * @return - TRUE if the ModR/M byte is required, FALSE otherwise.
59 */
Sean Callanan542eabc2009-12-22 22:51:40 +000060static int modRMRequired(OpcodeType type,
Craig Topper146c6d72011-10-02 16:56:09 +000061 InstructionContext insnContext,
62 uint8_t opcode) {
Daniel Dunbarbaf2e352009-12-22 01:41:37 +000063 const struct ContextDecision* decision = 0;
Sean Callanan8ed9f512009-12-19 02:59:52 +000064
65 switch (type) {
66 case ONEBYTE:
67 decision = &ONEBYTE_SYM;
68 break;
69 case TWOBYTE:
70 decision = &TWOBYTE_SYM;
71 break;
72 case THREEBYTE_38:
73 decision = &THREEBYTE38_SYM;
74 break;
75 case THREEBYTE_3A:
76 decision = &THREEBYTE3A_SYM;
77 break;
Joerg Sonnenberger4a8ac8d2011-04-04 16:58:13 +000078 case THREEBYTE_A6:
79 decision = &THREEBYTEA6_SYM;
80 break;
81 case THREEBYTE_A7:
82 decision = &THREEBYTEA7_SYM;
83 break;
Sean Callanan8ed9f512009-12-19 02:59:52 +000084 }
85
86 return decision->opcodeDecisions[insnContext].modRMDecisions[opcode].
87 modrm_type != MODRM_ONEENTRY;
88
Sean Callanan8ed9f512009-12-19 02:59:52 +000089 return 0;
90}
91
92/*
93 * decode - Reads the appropriate instruction table to obtain the unique ID of
94 * an instruction.
95 *
96 * @param type - See modRMRequired().
97 * @param insnContext - See modRMRequired().
98 * @param opcode - See modRMRequired().
99 * @param modRM - The ModR/M byte if required, or any value if not.
Sean Callanana144c3f2010-04-02 21:23:51 +0000100 * @return - The UID of the instruction, or 0 on failure.
Sean Callanan8ed9f512009-12-19 02:59:52 +0000101 */
Sean Callanan542eabc2009-12-22 22:51:40 +0000102static InstrUID decode(OpcodeType type,
Sean Callanana144c3f2010-04-02 21:23:51 +0000103 InstructionContext insnContext,
104 uint8_t opcode,
105 uint8_t modRM) {
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000106 const struct ModRMDecision* dec;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000107
108 switch (type) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000109 case ONEBYTE:
110 dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
111 break;
112 case TWOBYTE:
113 dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
114 break;
115 case THREEBYTE_38:
116 dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
117 break;
118 case THREEBYTE_3A:
119 dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
120 break;
Joerg Sonnenberger4a8ac8d2011-04-04 16:58:13 +0000121 case THREEBYTE_A6:
122 dec = &THREEBYTEA6_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
123 break;
124 case THREEBYTE_A7:
125 dec = &THREEBYTEA7_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
126 break;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000127 }
128
129 switch (dec->modrm_type) {
130 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000131 debug("Corrupt table! Unknown modrm_type");
132 return 0;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000133 case MODRM_ONEENTRY:
134 return dec->instructionIDs[0];
135 case MODRM_SPLITRM:
136 if (modFromModRM(modRM) == 0x3)
137 return dec->instructionIDs[1];
138 else
139 return dec->instructionIDs[0];
140 case MODRM_FULL:
141 return dec->instructionIDs[modRM];
142 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000143}
144
145/*
146 * specifierForUID - Given a UID, returns the name and operand specification for
147 * that instruction.
148 *
149 * @param uid - The unique ID for the instruction. This should be returned by
150 * decode(); specifierForUID will not check bounds.
151 * @return - A pointer to the specification for that instruction.
152 */
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000153static const struct InstructionSpecifier *specifierForUID(InstrUID uid) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000154 return &INSTRUCTIONS_SYM[uid];
155}
156
157/*
158 * consumeByte - Uses the reader function provided by the user to consume one
159 * byte from the instruction's memory and advance the cursor.
160 *
161 * @param insn - The instruction with the reader function to use. The cursor
162 * for this instruction is advanced.
163 * @param byte - A pointer to a pre-allocated memory buffer to be populated
164 * with the data read.
165 * @return - 0 if the read was successful; nonzero otherwise.
166 */
Sean Callanan542eabc2009-12-22 22:51:40 +0000167static int consumeByte(struct InternalInstruction* insn, uint8_t* byte) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000168 int ret = insn->reader(insn->readerArg, byte, insn->readerCursor);
169
170 if (!ret)
171 ++(insn->readerCursor);
172
173 return ret;
174}
175
176/*
177 * lookAtByte - Like consumeByte, but does not advance the cursor.
178 *
179 * @param insn - See consumeByte().
180 * @param byte - See consumeByte().
181 * @return - See consumeByte().
182 */
Sean Callanan542eabc2009-12-22 22:51:40 +0000183static int lookAtByte(struct InternalInstruction* insn, uint8_t* byte) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000184 return insn->reader(insn->readerArg, byte, insn->readerCursor);
185}
186
Sean Callanan542eabc2009-12-22 22:51:40 +0000187static void unconsumeByte(struct InternalInstruction* insn) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000188 insn->readerCursor--;
189}
190
Sean Callanan542eabc2009-12-22 22:51:40 +0000191#define CONSUME_FUNC(name, type) \
192 static int name(struct InternalInstruction* insn, type* ptr) { \
193 type combined = 0; \
194 unsigned offset; \
195 for (offset = 0; offset < sizeof(type); ++offset) { \
196 uint8_t byte; \
197 int ret = insn->reader(insn->readerArg, \
198 &byte, \
199 insn->readerCursor + offset); \
200 if (ret) \
201 return ret; \
202 combined = combined | ((type)byte << ((type)offset * 8)); \
203 } \
204 *ptr = combined; \
205 insn->readerCursor += sizeof(type); \
206 return 0; \
Sean Callanan8ed9f512009-12-19 02:59:52 +0000207 }
208
209/*
210 * consume* - Use the reader function provided by the user to consume data
211 * values of various sizes from the instruction's memory and advance the
212 * cursor appropriately. These readers perform endian conversion.
213 *
214 * @param insn - See consumeByte().
215 * @param ptr - A pointer to a pre-allocated memory of appropriate size to
216 * be populated with the data read.
217 * @return - See consumeByte().
218 */
219CONSUME_FUNC(consumeInt8, int8_t)
220CONSUME_FUNC(consumeInt16, int16_t)
221CONSUME_FUNC(consumeInt32, int32_t)
222CONSUME_FUNC(consumeUInt16, uint16_t)
223CONSUME_FUNC(consumeUInt32, uint32_t)
224CONSUME_FUNC(consumeUInt64, uint64_t)
225
226/*
Nuno Lopes392bbd92009-12-19 12:07:00 +0000227 * dbgprintf - Uses the logging function provided by the user to log a single
Sean Callanan8ed9f512009-12-19 02:59:52 +0000228 * message, typically without a carriage-return.
229 *
230 * @param insn - The instruction containing the logging function.
231 * @param format - See printf().
232 * @param ... - See printf().
233 */
Sean Callanan542eabc2009-12-22 22:51:40 +0000234static void dbgprintf(struct InternalInstruction* insn,
235 const char* format,
236 ...) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000237 char buffer[256];
238 va_list ap;
239
240 if (!insn->dlog)
241 return;
242
243 va_start(ap, format);
244 (void)vsnprintf(buffer, sizeof(buffer), format, ap);
245 va_end(ap);
246
247 insn->dlog(insn->dlogArg, buffer);
248
249 return;
250}
251
252/*
253 * setPrefixPresent - Marks that a particular prefix is present at a particular
254 * location.
255 *
256 * @param insn - The instruction to be marked as having the prefix.
257 * @param prefix - The prefix that is present.
258 * @param location - The location where the prefix is located (in the address
259 * space of the instruction's reader).
260 */
Sean Callanan542eabc2009-12-22 22:51:40 +0000261static void setPrefixPresent(struct InternalInstruction* insn,
Sean Callanan8ed9f512009-12-19 02:59:52 +0000262 uint8_t prefix,
263 uint64_t location)
264{
265 insn->prefixPresent[prefix] = 1;
266 insn->prefixLocations[prefix] = location;
267}
268
269/*
270 * isPrefixAtLocation - Queries an instruction to determine whether a prefix is
271 * present at a given location.
272 *
273 * @param insn - The instruction to be queried.
274 * @param prefix - The prefix.
275 * @param location - The location to query.
276 * @return - Whether the prefix is at that location.
277 */
Sean Callanan542eabc2009-12-22 22:51:40 +0000278static BOOL isPrefixAtLocation(struct InternalInstruction* insn,
279 uint8_t prefix,
280 uint64_t location)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000281{
282 if (insn->prefixPresent[prefix] == 1 &&
283 insn->prefixLocations[prefix] == location)
284 return TRUE;
285 else
286 return FALSE;
287}
288
289/*
290 * readPrefixes - Consumes all of an instruction's prefix bytes, and marks the
291 * instruction as having them. Also sets the instruction's default operand,
292 * address, and other relevant data sizes to report operands correctly.
293 *
294 * @param insn - The instruction whose prefixes are to be read.
295 * @return - 0 if the instruction could be read until the end of the prefix
296 * bytes, and no prefixes conflicted; nonzero otherwise.
297 */
298static int readPrefixes(struct InternalInstruction* insn) {
299 BOOL isPrefix = TRUE;
300 BOOL prefixGroups[4] = { FALSE };
301 uint64_t prefixLocation;
Ted Kremenek584520e2011-01-23 17:05:06 +0000302 uint8_t byte = 0;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000303
304 BOOL hasAdSize = FALSE;
305 BOOL hasOpSize = FALSE;
306
Nuno Lopes392bbd92009-12-19 12:07:00 +0000307 dbgprintf(insn, "readPrefixes()");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000308
309 while (isPrefix) {
310 prefixLocation = insn->readerCursor;
311
312 if (consumeByte(insn, &byte))
313 return -1;
314
315 switch (byte) {
316 case 0xf0: /* LOCK */
317 case 0xf2: /* REPNE/REPNZ */
318 case 0xf3: /* REP or REPE/REPZ */
319 if (prefixGroups[0])
Nuno Lopes392bbd92009-12-19 12:07:00 +0000320 dbgprintf(insn, "Redundant Group 1 prefix");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000321 prefixGroups[0] = TRUE;
322 setPrefixPresent(insn, byte, prefixLocation);
323 break;
324 case 0x2e: /* CS segment override -OR- Branch not taken */
325 case 0x36: /* SS segment override -OR- Branch taken */
326 case 0x3e: /* DS segment override */
327 case 0x26: /* ES segment override */
328 case 0x64: /* FS segment override */
329 case 0x65: /* GS segment override */
330 switch (byte) {
331 case 0x2e:
332 insn->segmentOverride = SEG_OVERRIDE_CS;
333 break;
334 case 0x36:
335 insn->segmentOverride = SEG_OVERRIDE_SS;
336 break;
337 case 0x3e:
338 insn->segmentOverride = SEG_OVERRIDE_DS;
339 break;
340 case 0x26:
341 insn->segmentOverride = SEG_OVERRIDE_ES;
342 break;
343 case 0x64:
344 insn->segmentOverride = SEG_OVERRIDE_FS;
345 break;
346 case 0x65:
347 insn->segmentOverride = SEG_OVERRIDE_GS;
348 break;
349 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000350 debug("Unhandled override");
351 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000352 }
353 if (prefixGroups[1])
Nuno Lopes392bbd92009-12-19 12:07:00 +0000354 dbgprintf(insn, "Redundant Group 2 prefix");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000355 prefixGroups[1] = TRUE;
356 setPrefixPresent(insn, byte, prefixLocation);
357 break;
358 case 0x66: /* Operand-size override */
359 if (prefixGroups[2])
Nuno Lopes392bbd92009-12-19 12:07:00 +0000360 dbgprintf(insn, "Redundant Group 3 prefix");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000361 prefixGroups[2] = TRUE;
362 hasOpSize = TRUE;
363 setPrefixPresent(insn, byte, prefixLocation);
364 break;
365 case 0x67: /* Address-size override */
366 if (prefixGroups[3])
Nuno Lopes392bbd92009-12-19 12:07:00 +0000367 dbgprintf(insn, "Redundant Group 4 prefix");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000368 prefixGroups[3] = TRUE;
369 hasAdSize = TRUE;
370 setPrefixPresent(insn, byte, prefixLocation);
371 break;
372 default: /* Not a prefix byte */
373 isPrefix = FALSE;
374 break;
375 }
376
377 if (isPrefix)
Nuno Lopes392bbd92009-12-19 12:07:00 +0000378 dbgprintf(insn, "Found prefix 0x%hhx", byte);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000379 }
Sean Callanana21e2ea2011-03-15 01:23:15 +0000380
381 insn->vexSize = 0;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000382
Sean Callanana21e2ea2011-03-15 01:23:15 +0000383 if (byte == 0xc4) {
384 uint8_t byte1;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000385
Sean Callanana21e2ea2011-03-15 01:23:15 +0000386 if (lookAtByte(insn, &byte1)) {
387 dbgprintf(insn, "Couldn't read second byte of VEX");
388 return -1;
389 }
390
Craig Topper100d86a2011-09-26 05:12:43 +0000391 if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) {
Sean Callanana21e2ea2011-03-15 01:23:15 +0000392 insn->vexSize = 3;
393 insn->necessaryPrefixLocation = insn->readerCursor - 1;
394 }
395 else {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000396 unconsumeByte(insn);
397 insn->necessaryPrefixLocation = insn->readerCursor - 1;
398 }
Sean Callanana21e2ea2011-03-15 01:23:15 +0000399
400 if (insn->vexSize == 3) {
401 insn->vexPrefix[0] = byte;
402 consumeByte(insn, &insn->vexPrefix[1]);
403 consumeByte(insn, &insn->vexPrefix[2]);
404
405 /* We simulate the REX prefix for simplicity's sake */
Craig Topper7b229762011-10-03 07:51:09 +0000406
407 if (insn->mode == MODE_64BIT) {
408 insn->rexPrefix = 0x40
409 | (wFromVEX3of3(insn->vexPrefix[2]) << 3)
410 | (rFromVEX2of3(insn->vexPrefix[1]) << 2)
411 | (xFromVEX2of3(insn->vexPrefix[1]) << 1)
412 | (bFromVEX2of3(insn->vexPrefix[1]) << 0);
413 }
Sean Callanana21e2ea2011-03-15 01:23:15 +0000414
415 switch (ppFromVEX3of3(insn->vexPrefix[2]))
416 {
417 default:
418 break;
419 case VEX_PREFIX_66:
420 hasOpSize = TRUE;
421 break;
422 }
423
424 dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx 0x%hhx", insn->vexPrefix[0], insn->vexPrefix[1], insn->vexPrefix[2]);
425 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000426 }
Sean Callanana21e2ea2011-03-15 01:23:15 +0000427 else if (byte == 0xc5) {
428 uint8_t byte1;
429
430 if (lookAtByte(insn, &byte1)) {
431 dbgprintf(insn, "Couldn't read second byte of VEX");
432 return -1;
433 }
434
Craig Topper100d86a2011-09-26 05:12:43 +0000435 if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) {
Sean Callanana21e2ea2011-03-15 01:23:15 +0000436 insn->vexSize = 2;
437 }
438 else {
439 unconsumeByte(insn);
440 }
441
442 if (insn->vexSize == 2) {
443 insn->vexPrefix[0] = byte;
444 consumeByte(insn, &insn->vexPrefix[1]);
445
Craig Topper7b229762011-10-03 07:51:09 +0000446 if (insn->mode == MODE_64BIT) {
447 insn->rexPrefix = 0x40
448 | (rFromVEX2of2(insn->vexPrefix[1]) << 2);
449 }
Sean Callanana21e2ea2011-03-15 01:23:15 +0000450
451 switch (ppFromVEX2of2(insn->vexPrefix[1]))
452 {
453 default:
454 break;
455 case VEX_PREFIX_66:
456 hasOpSize = TRUE;
457 break;
458 }
459
460 dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx", insn->vexPrefix[0], insn->vexPrefix[1]);
461 }
462 }
463 else {
464 if (insn->mode == MODE_64BIT) {
465 if ((byte & 0xf0) == 0x40) {
466 uint8_t opcodeByte;
467
468 if (lookAtByte(insn, &opcodeByte) || ((opcodeByte & 0xf0) == 0x40)) {
469 dbgprintf(insn, "Redundant REX prefix");
470 return -1;
471 }
472
473 insn->rexPrefix = byte;
474 insn->necessaryPrefixLocation = insn->readerCursor - 2;
475
476 dbgprintf(insn, "Found REX prefix 0x%hhx", byte);
477 } else {
478 unconsumeByte(insn);
479 insn->necessaryPrefixLocation = insn->readerCursor - 1;
480 }
481 } else {
482 unconsumeByte(insn);
483 insn->necessaryPrefixLocation = insn->readerCursor - 1;
484 }
485 }
486
Sean Callanan8ed9f512009-12-19 02:59:52 +0000487 if (insn->mode == MODE_16BIT) {
488 insn->registerSize = (hasOpSize ? 4 : 2);
489 insn->addressSize = (hasAdSize ? 4 : 2);
490 insn->displacementSize = (hasAdSize ? 4 : 2);
491 insn->immediateSize = (hasOpSize ? 4 : 2);
492 } else if (insn->mode == MODE_32BIT) {
493 insn->registerSize = (hasOpSize ? 2 : 4);
494 insn->addressSize = (hasAdSize ? 2 : 4);
495 insn->displacementSize = (hasAdSize ? 2 : 4);
Sean Callanan751752e2010-10-22 01:24:11 +0000496 insn->immediateSize = (hasOpSize ? 2 : 4);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000497 } else if (insn->mode == MODE_64BIT) {
498 if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
499 insn->registerSize = 8;
500 insn->addressSize = (hasAdSize ? 4 : 8);
501 insn->displacementSize = 4;
502 insn->immediateSize = 4;
503 } else if (insn->rexPrefix) {
504 insn->registerSize = (hasOpSize ? 2 : 4);
505 insn->addressSize = (hasAdSize ? 4 : 8);
506 insn->displacementSize = (hasOpSize ? 2 : 4);
507 insn->immediateSize = (hasOpSize ? 2 : 4);
508 } else {
509 insn->registerSize = (hasOpSize ? 2 : 4);
510 insn->addressSize = (hasAdSize ? 4 : 8);
511 insn->displacementSize = (hasOpSize ? 2 : 4);
512 insn->immediateSize = (hasOpSize ? 2 : 4);
513 }
514 }
515
516 return 0;
517}
518
519/*
520 * readOpcode - Reads the opcode (excepting the ModR/M byte in the case of
521 * extended or escape opcodes).
522 *
523 * @param insn - The instruction whose opcode is to be read.
524 * @return - 0 if the opcode could be read successfully; nonzero otherwise.
525 */
526static int readOpcode(struct InternalInstruction* insn) {
527 /* Determine the length of the primary opcode */
528
529 uint8_t current;
530
Nuno Lopes392bbd92009-12-19 12:07:00 +0000531 dbgprintf(insn, "readOpcode()");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000532
533 insn->opcodeType = ONEBYTE;
Sean Callanana21e2ea2011-03-15 01:23:15 +0000534
535 if (insn->vexSize == 3)
536 {
537 switch (mmmmmFromVEX2of3(insn->vexPrefix[1]))
538 {
539 default:
540 dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)", mmmmmFromVEX2of3(insn->vexPrefix[1]));
541 return -1;
542 case 0:
543 break;
544 case VEX_LOB_0F:
545 insn->twoByteEscape = 0x0f;
546 insn->opcodeType = TWOBYTE;
547 return consumeByte(insn, &insn->opcode);
548 case VEX_LOB_0F38:
549 insn->twoByteEscape = 0x0f;
550 insn->threeByteEscape = 0x38;
551 insn->opcodeType = THREEBYTE_38;
552 return consumeByte(insn, &insn->opcode);
553 case VEX_LOB_0F3A:
554 insn->twoByteEscape = 0x0f;
555 insn->threeByteEscape = 0x3a;
556 insn->opcodeType = THREEBYTE_3A;
557 return consumeByte(insn, &insn->opcode);
558 }
559 }
560 else if (insn->vexSize == 2)
561 {
562 insn->twoByteEscape = 0x0f;
563 insn->opcodeType = TWOBYTE;
564 return consumeByte(insn, &insn->opcode);
565 }
566
Sean Callanan8ed9f512009-12-19 02:59:52 +0000567 if (consumeByte(insn, &current))
568 return -1;
569
570 if (current == 0x0f) {
Nuno Lopes392bbd92009-12-19 12:07:00 +0000571 dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000572
573 insn->twoByteEscape = current;
574
575 if (consumeByte(insn, &current))
576 return -1;
577
578 if (current == 0x38) {
Nuno Lopes392bbd92009-12-19 12:07:00 +0000579 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000580
581 insn->threeByteEscape = current;
582
583 if (consumeByte(insn, &current))
584 return -1;
585
586 insn->opcodeType = THREEBYTE_38;
587 } else if (current == 0x3a) {
Nuno Lopes392bbd92009-12-19 12:07:00 +0000588 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000589
590 insn->threeByteEscape = current;
591
592 if (consumeByte(insn, &current))
593 return -1;
594
595 insn->opcodeType = THREEBYTE_3A;
Joerg Sonnenberger4a8ac8d2011-04-04 16:58:13 +0000596 } else if (current == 0xa6) {
597 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
598
599 insn->threeByteEscape = current;
600
601 if (consumeByte(insn, &current))
602 return -1;
603
604 insn->opcodeType = THREEBYTE_A6;
605 } else if (current == 0xa7) {
606 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
607
608 insn->threeByteEscape = current;
609
610 if (consumeByte(insn, &current))
611 return -1;
612
613 insn->opcodeType = THREEBYTE_A7;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000614 } else {
Nuno Lopes392bbd92009-12-19 12:07:00 +0000615 dbgprintf(insn, "Didn't find a three-byte escape prefix");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000616
617 insn->opcodeType = TWOBYTE;
618 }
619 }
620
621 /*
622 * At this point we have consumed the full opcode.
623 * Anything we consume from here on must be unconsumed.
624 */
625
626 insn->opcode = current;
627
628 return 0;
629}
630
631static int readModRM(struct InternalInstruction* insn);
632
633/*
634 * getIDWithAttrMask - Determines the ID of an instruction, consuming
635 * the ModR/M byte as appropriate for extended and escape opcodes,
636 * and using a supplied attribute mask.
637 *
638 * @param instructionID - A pointer whose target is filled in with the ID of the
639 * instruction.
640 * @param insn - The instruction whose ID is to be determined.
641 * @param attrMask - The attribute mask to search.
642 * @return - 0 if the ModR/M could be read when needed or was not
643 * needed; nonzero otherwise.
644 */
645static int getIDWithAttrMask(uint16_t* instructionID,
646 struct InternalInstruction* insn,
647 uint8_t attrMask) {
648 BOOL hasModRMExtension;
649
650 uint8_t instructionClass;
651
652 instructionClass = contextForAttrs(attrMask);
653
654 hasModRMExtension = modRMRequired(insn->opcodeType,
655 instructionClass,
656 insn->opcode);
657
658 if (hasModRMExtension) {
Rafael Espindola2f867a62011-01-06 16:48:42 +0000659 if (readModRM(insn))
660 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000661
662 *instructionID = decode(insn->opcodeType,
663 instructionClass,
664 insn->opcode,
665 insn->modRM);
666 } else {
667 *instructionID = decode(insn->opcodeType,
668 instructionClass,
669 insn->opcode,
670 0);
671 }
672
673 return 0;
674}
675
676/*
677 * is16BitEquivalent - Determines whether two instruction names refer to
678 * equivalent instructions but one is 16-bit whereas the other is not.
679 *
680 * @param orig - The instruction that is not 16-bit
681 * @param equiv - The instruction that is 16-bit
682 */
683static BOOL is16BitEquvalent(const char* orig, const char* equiv) {
684 off_t i;
685
Sean Callanana144c3f2010-04-02 21:23:51 +0000686 for (i = 0;; i++) {
687 if (orig[i] == '\0' && equiv[i] == '\0')
Sean Callanan8ed9f512009-12-19 02:59:52 +0000688 return TRUE;
Sean Callanana144c3f2010-04-02 21:23:51 +0000689 if (orig[i] == '\0' || equiv[i] == '\0')
Sean Callanan8ed9f512009-12-19 02:59:52 +0000690 return FALSE;
Sean Callanana144c3f2010-04-02 21:23:51 +0000691 if (orig[i] != equiv[i]) {
692 if ((orig[i] == 'Q' || orig[i] == 'L') && equiv[i] == 'W')
Sean Callanan8ed9f512009-12-19 02:59:52 +0000693 continue;
Sean Callanana144c3f2010-04-02 21:23:51 +0000694 if ((orig[i] == '6' || orig[i] == '3') && equiv[i] == '1')
Sean Callanan8ed9f512009-12-19 02:59:52 +0000695 continue;
Sean Callanana144c3f2010-04-02 21:23:51 +0000696 if ((orig[i] == '4' || orig[i] == '2') && equiv[i] == '6')
Sean Callanan8ed9f512009-12-19 02:59:52 +0000697 continue;
698 return FALSE;
699 }
700 }
701}
702
703/*
Sean Callanan8ed9f512009-12-19 02:59:52 +0000704 * getID - Determines the ID of an instruction, consuming the ModR/M byte as
705 * appropriate for extended and escape opcodes. Determines the attributes and
706 * context for the instruction before doing so.
707 *
708 * @param insn - The instruction whose ID is to be determined.
709 * @return - 0 if the ModR/M could be read when needed or was not needed;
710 * nonzero otherwise.
711 */
712static int getID(struct InternalInstruction* insn) {
713 uint8_t attrMask;
714 uint16_t instructionID;
715
Nuno Lopes392bbd92009-12-19 12:07:00 +0000716 dbgprintf(insn, "getID()");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000717
718 attrMask = ATTR_NONE;
Sean Callanana21e2ea2011-03-15 01:23:15 +0000719
Sean Callanan8ed9f512009-12-19 02:59:52 +0000720 if (insn->mode == MODE_64BIT)
721 attrMask |= ATTR_64BIT;
Sean Callanana21e2ea2011-03-15 01:23:15 +0000722
723 if (insn->vexSize) {
724 attrMask |= ATTR_VEX;
725
726 if (insn->vexSize == 3) {
727 switch (ppFromVEX3of3(insn->vexPrefix[2])) {
728 case VEX_PREFIX_66:
729 attrMask |= ATTR_OPSIZE;
730 break;
731 case VEX_PREFIX_F3:
732 attrMask |= ATTR_XS;
733 break;
734 case VEX_PREFIX_F2:
735 attrMask |= ATTR_XD;
736 break;
737 }
738
Sean Callanana21e2ea2011-03-15 01:23:15 +0000739 if (lFromVEX3of3(insn->vexPrefix[2]))
740 attrMask |= ATTR_VEXL;
741 }
742 else if (insn->vexSize == 2) {
743 switch (ppFromVEX2of2(insn->vexPrefix[1])) {
744 case VEX_PREFIX_66:
745 attrMask |= ATTR_OPSIZE;
746 break;
747 case VEX_PREFIX_F3:
748 attrMask |= ATTR_XS;
749 break;
750 case VEX_PREFIX_F2:
751 attrMask |= ATTR_XD;
752 break;
753 }
754
755 if (lFromVEX2of2(insn->vexPrefix[1]))
756 attrMask |= ATTR_VEXL;
757 }
758 else {
759 return -1;
760 }
761 }
762 else {
Sean Callanana21e2ea2011-03-15 01:23:15 +0000763 if (isPrefixAtLocation(insn, 0x66, insn->necessaryPrefixLocation))
764 attrMask |= ATTR_OPSIZE;
765 else if (isPrefixAtLocation(insn, 0xf3, insn->necessaryPrefixLocation))
766 attrMask |= ATTR_XS;
767 else if (isPrefixAtLocation(insn, 0xf2, insn->necessaryPrefixLocation))
768 attrMask |= ATTR_XD;
Sean Callanana21e2ea2011-03-15 01:23:15 +0000769 }
770
Craig Topper6744a172011-10-04 06:30:42 +0000771 if (insn->rexPrefix & 0x08)
772 attrMask |= ATTR_REXW;
Craig Topperc8eb8802011-11-06 23:04:08 +0000773
Sean Callanana144c3f2010-04-02 21:23:51 +0000774 if (getIDWithAttrMask(&instructionID, insn, attrMask))
Sean Callanan8ed9f512009-12-19 02:59:52 +0000775 return -1;
Craig Topperc8eb8802011-11-06 23:04:08 +0000776
Sean Callanan8ed9f512009-12-19 02:59:52 +0000777 /* The following clauses compensate for limitations of the tables. */
Craig Topperc8eb8802011-11-06 23:04:08 +0000778
779 if ((attrMask & ATTR_VEXL) && (attrMask & ATTR_REXW) &&
780 !(attrMask & ATTR_OPSIZE)) {
Craig Topper6744a172011-10-04 06:30:42 +0000781 /*
782 * Some VEX instructions ignore the L-bit, but use the W-bit. Normally L-bit
783 * has precedence since there are no L-bit with W-bit entries in the tables.
784 * So if the L-bit isn't significant we should use the W-bit instead.
Craig Topperc8eb8802011-11-06 23:04:08 +0000785 * We only need to do this if the instruction doesn't specify OpSize since
786 * there is a VEX_L_W_OPSIZE table.
Craig Topper6744a172011-10-04 06:30:42 +0000787 */
788
789 const struct InstructionSpecifier *spec;
790 uint16_t instructionIDWithWBit;
791 const struct InstructionSpecifier *specWithWBit;
792
793 spec = specifierForUID(instructionID);
794
795 if (getIDWithAttrMask(&instructionIDWithWBit,
796 insn,
797 (attrMask & (~ATTR_VEXL)) | ATTR_REXW)) {
798 insn->instructionID = instructionID;
799 insn->spec = spec;
800 return 0;
801 }
802
803 specWithWBit = specifierForUID(instructionIDWithWBit);
804
805 if (instructionID != instructionIDWithWBit) {
806 insn->instructionID = instructionIDWithWBit;
807 insn->spec = specWithWBit;
808 } else {
809 insn->instructionID = instructionID;
810 insn->spec = spec;
811 }
812 return 0;
813 }
814
Sean Callanan8ed9f512009-12-19 02:59:52 +0000815 if (insn->prefixPresent[0x66] && !(attrMask & ATTR_OPSIZE)) {
816 /*
817 * The instruction tables make no distinction between instructions that
818 * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a
819 * particular spot (i.e., many MMX operations). In general we're
820 * conservative, but in the specific case where OpSize is present but not
821 * in the right place we check if there's a 16-bit operation.
822 */
823
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000824 const struct InstructionSpecifier *spec;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000825 uint16_t instructionIDWithOpsize;
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000826 const struct InstructionSpecifier *specWithOpsize;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000827
828 spec = specifierForUID(instructionID);
829
830 if (getIDWithAttrMask(&instructionIDWithOpsize,
831 insn,
832 attrMask | ATTR_OPSIZE)) {
833 /*
834 * ModRM required with OpSize but not present; give up and return version
835 * without OpSize set
836 */
837
838 insn->instructionID = instructionID;
839 insn->spec = spec;
840 return 0;
841 }
842
843 specWithOpsize = specifierForUID(instructionIDWithOpsize);
844
845 if (is16BitEquvalent(spec->name, specWithOpsize->name)) {
846 insn->instructionID = instructionIDWithOpsize;
847 insn->spec = specWithOpsize;
848 } else {
849 insn->instructionID = instructionID;
850 insn->spec = spec;
851 }
852 return 0;
853 }
Craig Topper146c6d72011-10-02 16:56:09 +0000854
855 if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 &&
856 insn->rexPrefix & 0x01) {
857 /*
858 * NOOP shouldn't decode as NOOP if REX.b is set. Instead
859 * it should decode as XCHG %r8, %eax.
860 */
861
862 const struct InstructionSpecifier *spec;
863 uint16_t instructionIDWithNewOpcode;
864 const struct InstructionSpecifier *specWithNewOpcode;
865
866 spec = specifierForUID(instructionID);
867
Craig Topper41e59c72011-10-05 03:29:32 +0000868 /* Borrow opcode from one of the other XCHGar opcodes */
Craig Topper146c6d72011-10-02 16:56:09 +0000869 insn->opcode = 0x91;
870
871 if (getIDWithAttrMask(&instructionIDWithNewOpcode,
872 insn,
873 attrMask)) {
874 insn->opcode = 0x90;
875
876 insn->instructionID = instructionID;
877 insn->spec = spec;
878 return 0;
879 }
880
881 specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode);
882
Craig Topper41e59c72011-10-05 03:29:32 +0000883 /* Change back */
Craig Topper146c6d72011-10-02 16:56:09 +0000884 insn->opcode = 0x90;
885
886 insn->instructionID = instructionIDWithNewOpcode;
887 insn->spec = specWithNewOpcode;
888
889 return 0;
890 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000891
892 insn->instructionID = instructionID;
893 insn->spec = specifierForUID(insn->instructionID);
894
895 return 0;
896}
897
898/*
899 * readSIB - Consumes the SIB byte to determine addressing information for an
900 * instruction.
901 *
902 * @param insn - The instruction whose SIB byte is to be read.
903 * @return - 0 if the SIB byte was successfully read; nonzero otherwise.
904 */
905static int readSIB(struct InternalInstruction* insn) {
Daniel Dunbarbaf2e352009-12-22 01:41:37 +0000906 SIBIndex sibIndexBase = 0;
907 SIBBase sibBaseBase = 0;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000908 uint8_t index, base;
909
Nuno Lopes392bbd92009-12-19 12:07:00 +0000910 dbgprintf(insn, "readSIB()");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000911
912 if (insn->consumedSIB)
913 return 0;
914
915 insn->consumedSIB = TRUE;
916
917 switch (insn->addressSize) {
918 case 2:
Nuno Lopes392bbd92009-12-19 12:07:00 +0000919 dbgprintf(insn, "SIB-based addressing doesn't work in 16-bit mode");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000920 return -1;
921 break;
922 case 4:
923 sibIndexBase = SIB_INDEX_EAX;
924 sibBaseBase = SIB_BASE_EAX;
925 break;
926 case 8:
927 sibIndexBase = SIB_INDEX_RAX;
928 sibBaseBase = SIB_BASE_RAX;
929 break;
930 }
931
932 if (consumeByte(insn, &insn->sib))
933 return -1;
934
935 index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
936
937 switch (index) {
938 case 0x4:
939 insn->sibIndex = SIB_INDEX_NONE;
940 break;
941 default:
Benjamin Kramer9e9bb082011-02-27 18:13:53 +0000942 insn->sibIndex = (SIBIndex)(sibIndexBase + index);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000943 if (insn->sibIndex == SIB_INDEX_sib ||
944 insn->sibIndex == SIB_INDEX_sib64)
945 insn->sibIndex = SIB_INDEX_NONE;
946 break;
947 }
948
949 switch (scaleFromSIB(insn->sib)) {
950 case 0:
951 insn->sibScale = 1;
952 break;
953 case 1:
954 insn->sibScale = 2;
955 break;
956 case 2:
957 insn->sibScale = 4;
958 break;
959 case 3:
960 insn->sibScale = 8;
961 break;
962 }
963
964 base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
965
966 switch (base) {
967 case 0x5:
968 switch (modFromModRM(insn->modRM)) {
969 case 0x0:
970 insn->eaDisplacement = EA_DISP_32;
971 insn->sibBase = SIB_BASE_NONE;
972 break;
973 case 0x1:
974 insn->eaDisplacement = EA_DISP_8;
975 insn->sibBase = (insn->addressSize == 4 ?
976 SIB_BASE_EBP : SIB_BASE_RBP);
977 break;
978 case 0x2:
979 insn->eaDisplacement = EA_DISP_32;
980 insn->sibBase = (insn->addressSize == 4 ?
981 SIB_BASE_EBP : SIB_BASE_RBP);
982 break;
983 case 0x3:
Sean Callanana144c3f2010-04-02 21:23:51 +0000984 debug("Cannot have Mod = 0b11 and a SIB byte");
985 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000986 }
987 break;
988 default:
Benjamin Kramer9e9bb082011-02-27 18:13:53 +0000989 insn->sibBase = (SIBBase)(sibBaseBase + base);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000990 break;
991 }
992
993 return 0;
994}
995
996/*
997 * readDisplacement - Consumes the displacement of an instruction.
998 *
999 * @param insn - The instruction whose displacement is to be read.
1000 * @return - 0 if the displacement byte was successfully read; nonzero
1001 * otherwise.
1002 */
1003static int readDisplacement(struct InternalInstruction* insn) {
1004 int8_t d8;
1005 int16_t d16;
1006 int32_t d32;
1007
Nuno Lopes392bbd92009-12-19 12:07:00 +00001008 dbgprintf(insn, "readDisplacement()");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001009
1010 if (insn->consumedDisplacement)
1011 return 0;
1012
1013 insn->consumedDisplacement = TRUE;
1014
1015 switch (insn->eaDisplacement) {
1016 case EA_DISP_NONE:
1017 insn->consumedDisplacement = FALSE;
1018 break;
1019 case EA_DISP_8:
1020 if (consumeInt8(insn, &d8))
1021 return -1;
1022 insn->displacement = d8;
1023 break;
1024 case EA_DISP_16:
1025 if (consumeInt16(insn, &d16))
1026 return -1;
1027 insn->displacement = d16;
1028 break;
1029 case EA_DISP_32:
1030 if (consumeInt32(insn, &d32))
1031 return -1;
1032 insn->displacement = d32;
1033 break;
1034 }
1035
1036 insn->consumedDisplacement = TRUE;
1037 return 0;
1038}
1039
1040/*
1041 * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and
1042 * displacement) for an instruction and interprets it.
1043 *
1044 * @param insn - The instruction whose addressing information is to be read.
1045 * @return - 0 if the information was successfully read; nonzero otherwise.
1046 */
1047static int readModRM(struct InternalInstruction* insn) {
1048 uint8_t mod, rm, reg;
1049
Nuno Lopes392bbd92009-12-19 12:07:00 +00001050 dbgprintf(insn, "readModRM()");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001051
1052 if (insn->consumedModRM)
1053 return 0;
1054
Rafael Espindola2f867a62011-01-06 16:48:42 +00001055 if (consumeByte(insn, &insn->modRM))
1056 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001057 insn->consumedModRM = TRUE;
1058
1059 mod = modFromModRM(insn->modRM);
1060 rm = rmFromModRM(insn->modRM);
1061 reg = regFromModRM(insn->modRM);
1062
1063 /*
1064 * This goes by insn->registerSize to pick the correct register, which messes
1065 * up if we're using (say) XMM or 8-bit register operands. That gets fixed in
1066 * fixupReg().
1067 */
1068 switch (insn->registerSize) {
1069 case 2:
Sean Callanan06b766d2009-12-22 02:07:42 +00001070 insn->regBase = MODRM_REG_AX;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001071 insn->eaRegBase = EA_REG_AX;
1072 break;
1073 case 4:
Sean Callanan06b766d2009-12-22 02:07:42 +00001074 insn->regBase = MODRM_REG_EAX;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001075 insn->eaRegBase = EA_REG_EAX;
1076 break;
1077 case 8:
Sean Callanan06b766d2009-12-22 02:07:42 +00001078 insn->regBase = MODRM_REG_RAX;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001079 insn->eaRegBase = EA_REG_RAX;
1080 break;
1081 }
1082
1083 reg |= rFromREX(insn->rexPrefix) << 3;
1084 rm |= bFromREX(insn->rexPrefix) << 3;
1085
1086 insn->reg = (Reg)(insn->regBase + reg);
1087
1088 switch (insn->addressSize) {
1089 case 2:
1090 insn->eaBaseBase = EA_BASE_BX_SI;
1091
1092 switch (mod) {
1093 case 0x0:
1094 if (rm == 0x6) {
1095 insn->eaBase = EA_BASE_NONE;
1096 insn->eaDisplacement = EA_DISP_16;
Sean Callanana144c3f2010-04-02 21:23:51 +00001097 if (readDisplacement(insn))
Sean Callanan8ed9f512009-12-19 02:59:52 +00001098 return -1;
1099 } else {
1100 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1101 insn->eaDisplacement = EA_DISP_NONE;
1102 }
1103 break;
1104 case 0x1:
1105 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1106 insn->eaDisplacement = EA_DISP_8;
Sean Callanana144c3f2010-04-02 21:23:51 +00001107 if (readDisplacement(insn))
Sean Callanan8ed9f512009-12-19 02:59:52 +00001108 return -1;
1109 break;
1110 case 0x2:
1111 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1112 insn->eaDisplacement = EA_DISP_16;
Sean Callanana144c3f2010-04-02 21:23:51 +00001113 if (readDisplacement(insn))
Sean Callanan8ed9f512009-12-19 02:59:52 +00001114 return -1;
1115 break;
1116 case 0x3:
1117 insn->eaBase = (EABase)(insn->eaRegBase + rm);
Sean Callanana144c3f2010-04-02 21:23:51 +00001118 if (readDisplacement(insn))
Sean Callanan8ed9f512009-12-19 02:59:52 +00001119 return -1;
1120 break;
1121 }
1122 break;
1123 case 4:
1124 case 8:
1125 insn->eaBaseBase = (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
1126
1127 switch (mod) {
1128 case 0x0:
1129 insn->eaDisplacement = EA_DISP_NONE; /* readSIB may override this */
1130 switch (rm) {
1131 case 0x4:
1132 case 0xc: /* in case REXW.b is set */
1133 insn->eaBase = (insn->addressSize == 4 ?
1134 EA_BASE_sib : EA_BASE_sib64);
1135 readSIB(insn);
Sean Callanana144c3f2010-04-02 21:23:51 +00001136 if (readDisplacement(insn))
Sean Callanan8ed9f512009-12-19 02:59:52 +00001137 return -1;
1138 break;
1139 case 0x5:
1140 insn->eaBase = EA_BASE_NONE;
1141 insn->eaDisplacement = EA_DISP_32;
Sean Callanana144c3f2010-04-02 21:23:51 +00001142 if (readDisplacement(insn))
Sean Callanan8ed9f512009-12-19 02:59:52 +00001143 return -1;
1144 break;
1145 default:
1146 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1147 break;
1148 }
1149 break;
1150 case 0x1:
1151 case 0x2:
1152 insn->eaDisplacement = (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
1153 switch (rm) {
1154 case 0x4:
1155 case 0xc: /* in case REXW.b is set */
1156 insn->eaBase = EA_BASE_sib;
1157 readSIB(insn);
Sean Callanana144c3f2010-04-02 21:23:51 +00001158 if (readDisplacement(insn))
Sean Callanan8ed9f512009-12-19 02:59:52 +00001159 return -1;
1160 break;
1161 default:
1162 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
Sean Callanana144c3f2010-04-02 21:23:51 +00001163 if (readDisplacement(insn))
Sean Callanan8ed9f512009-12-19 02:59:52 +00001164 return -1;
1165 break;
1166 }
1167 break;
1168 case 0x3:
1169 insn->eaDisplacement = EA_DISP_NONE;
1170 insn->eaBase = (EABase)(insn->eaRegBase + rm);
1171 break;
1172 }
1173 break;
1174 } /* switch (insn->addressSize) */
1175
1176 return 0;
1177}
1178
1179#define GENERIC_FIXUP_FUNC(name, base, prefix) \
1180 static uint8_t name(struct InternalInstruction *insn, \
1181 OperandType type, \
1182 uint8_t index, \
1183 uint8_t *valid) { \
1184 *valid = 1; \
1185 switch (type) { \
1186 default: \
Sean Callanana144c3f2010-04-02 21:23:51 +00001187 debug("Unhandled register type"); \
1188 *valid = 0; \
1189 return 0; \
Sean Callanan8ed9f512009-12-19 02:59:52 +00001190 case TYPE_Rv: \
1191 return base + index; \
1192 case TYPE_R8: \
Sean Callanana144c3f2010-04-02 21:23:51 +00001193 if (insn->rexPrefix && \
Sean Callanan8ed9f512009-12-19 02:59:52 +00001194 index >= 4 && index <= 7) { \
1195 return prefix##_SPL + (index - 4); \
1196 } else { \
1197 return prefix##_AL + index; \
1198 } \
1199 case TYPE_R16: \
1200 return prefix##_AX + index; \
1201 case TYPE_R32: \
1202 return prefix##_EAX + index; \
1203 case TYPE_R64: \
1204 return prefix##_RAX + index; \
Sean Callanana21e2ea2011-03-15 01:23:15 +00001205 case TYPE_XMM256: \
1206 return prefix##_YMM0 + index; \
Sean Callanan8ed9f512009-12-19 02:59:52 +00001207 case TYPE_XMM128: \
1208 case TYPE_XMM64: \
1209 case TYPE_XMM32: \
1210 case TYPE_XMM: \
1211 return prefix##_XMM0 + index; \
1212 case TYPE_MM64: \
1213 case TYPE_MM32: \
1214 case TYPE_MM: \
Sean Callanana144c3f2010-04-02 21:23:51 +00001215 if (index > 7) \
Sean Callanan8ed9f512009-12-19 02:59:52 +00001216 *valid = 0; \
1217 return prefix##_MM0 + index; \
1218 case TYPE_SEGMENTREG: \
Sean Callanana144c3f2010-04-02 21:23:51 +00001219 if (index > 5) \
Sean Callanan8ed9f512009-12-19 02:59:52 +00001220 *valid = 0; \
1221 return prefix##_ES + index; \
1222 case TYPE_DEBUGREG: \
Sean Callanana144c3f2010-04-02 21:23:51 +00001223 if (index > 7) \
Sean Callanan8ed9f512009-12-19 02:59:52 +00001224 *valid = 0; \
1225 return prefix##_DR0 + index; \
Sean Callanan1a8b7892010-05-06 20:59:00 +00001226 case TYPE_CONTROLREG: \
Sean Callanana144c3f2010-04-02 21:23:51 +00001227 if (index > 8) \
Sean Callanan8ed9f512009-12-19 02:59:52 +00001228 *valid = 0; \
Sean Callanan1a8b7892010-05-06 20:59:00 +00001229 return prefix##_CR0 + index; \
Sean Callanan8ed9f512009-12-19 02:59:52 +00001230 } \
1231 }
1232
1233/*
1234 * fixup*Value - Consults an operand type to determine the meaning of the
1235 * reg or R/M field. If the operand is an XMM operand, for example, an
1236 * operand would be XMM0 instead of AX, which readModRM() would otherwise
1237 * misinterpret it as.
1238 *
1239 * @param insn - The instruction containing the operand.
1240 * @param type - The operand type.
1241 * @param index - The existing value of the field as reported by readModRM().
1242 * @param valid - The address of a uint8_t. The target is set to 1 if the
1243 * field is valid for the register class; 0 if not.
Sean Callanana144c3f2010-04-02 21:23:51 +00001244 * @return - The proper value.
Sean Callanan8ed9f512009-12-19 02:59:52 +00001245 */
Sean Callanan06b766d2009-12-22 02:07:42 +00001246GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase, MODRM_REG)
Sean Callanan8ed9f512009-12-19 02:59:52 +00001247GENERIC_FIXUP_FUNC(fixupRMValue, insn->eaRegBase, EA_REG)
1248
1249/*
1250 * fixupReg - Consults an operand specifier to determine which of the
1251 * fixup*Value functions to use in correcting readModRM()'ss interpretation.
1252 *
1253 * @param insn - See fixup*Value().
1254 * @param op - The operand specifier.
1255 * @return - 0 if fixup was successful; -1 if the register returned was
1256 * invalid for its class.
1257 */
1258static int fixupReg(struct InternalInstruction *insn,
Benjamin Kramer4d1dca92010-10-23 09:10:44 +00001259 const struct OperandSpecifier *op) {
Sean Callanan8ed9f512009-12-19 02:59:52 +00001260 uint8_t valid;
1261
Nuno Lopes392bbd92009-12-19 12:07:00 +00001262 dbgprintf(insn, "fixupReg()");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001263
1264 switch ((OperandEncoding)op->encoding) {
1265 default:
Sean Callanana144c3f2010-04-02 21:23:51 +00001266 debug("Expected a REG or R/M encoding in fixupReg");
1267 return -1;
Sean Callanana21e2ea2011-03-15 01:23:15 +00001268 case ENCODING_VVVV:
1269 insn->vvvv = (Reg)fixupRegValue(insn,
1270 (OperandType)op->type,
1271 insn->vvvv,
1272 &valid);
1273 if (!valid)
1274 return -1;
1275 break;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001276 case ENCODING_REG:
1277 insn->reg = (Reg)fixupRegValue(insn,
1278 (OperandType)op->type,
1279 insn->reg - insn->regBase,
1280 &valid);
1281 if (!valid)
1282 return -1;
1283 break;
1284 case ENCODING_RM:
1285 if (insn->eaBase >= insn->eaRegBase) {
1286 insn->eaBase = (EABase)fixupRMValue(insn,
1287 (OperandType)op->type,
1288 insn->eaBase - insn->eaRegBase,
1289 &valid);
1290 if (!valid)
1291 return -1;
1292 }
1293 break;
1294 }
1295
1296 return 0;
1297}
1298
1299/*
1300 * readOpcodeModifier - Reads an operand from the opcode field of an
1301 * instruction. Handles AddRegFrm instructions.
1302 *
1303 * @param insn - The instruction whose opcode field is to be read.
1304 * @param inModRM - Indicates that the opcode field is to be read from the
1305 * ModR/M extension; useful for escape opcodes
Sean Callanana144c3f2010-04-02 21:23:51 +00001306 * @return - 0 on success; nonzero otherwise.
Sean Callanan8ed9f512009-12-19 02:59:52 +00001307 */
Sean Callanana144c3f2010-04-02 21:23:51 +00001308static int readOpcodeModifier(struct InternalInstruction* insn) {
Nuno Lopes392bbd92009-12-19 12:07:00 +00001309 dbgprintf(insn, "readOpcodeModifier()");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001310
1311 if (insn->consumedOpcodeModifier)
Sean Callanana144c3f2010-04-02 21:23:51 +00001312 return 0;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001313
1314 insn->consumedOpcodeModifier = TRUE;
1315
Sean Callanana144c3f2010-04-02 21:23:51 +00001316 switch (insn->spec->modifierType) {
Sean Callanan8ed9f512009-12-19 02:59:52 +00001317 default:
Sean Callanana144c3f2010-04-02 21:23:51 +00001318 debug("Unknown modifier type.");
1319 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001320 case MODIFIER_NONE:
Sean Callanana144c3f2010-04-02 21:23:51 +00001321 debug("No modifier but an operand expects one.");
1322 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001323 case MODIFIER_OPCODE:
1324 insn->opcodeModifier = insn->opcode - insn->spec->modifierBase;
Sean Callanana144c3f2010-04-02 21:23:51 +00001325 return 0;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001326 case MODIFIER_MODRM:
1327 insn->opcodeModifier = insn->modRM - insn->spec->modifierBase;
Sean Callanana144c3f2010-04-02 21:23:51 +00001328 return 0;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001329 }
1330}
1331
1332/*
1333 * readOpcodeRegister - Reads an operand from the opcode field of an
1334 * instruction and interprets it appropriately given the operand width.
1335 * Handles AddRegFrm instructions.
1336 *
1337 * @param insn - See readOpcodeModifier().
1338 * @param size - The width (in bytes) of the register being specified.
1339 * 1 means AL and friends, 2 means AX, 4 means EAX, and 8 means
1340 * RAX.
Sean Callanana144c3f2010-04-02 21:23:51 +00001341 * @return - 0 on success; nonzero otherwise.
Sean Callanan8ed9f512009-12-19 02:59:52 +00001342 */
Sean Callanana144c3f2010-04-02 21:23:51 +00001343static int readOpcodeRegister(struct InternalInstruction* insn, uint8_t size) {
Nuno Lopes392bbd92009-12-19 12:07:00 +00001344 dbgprintf(insn, "readOpcodeRegister()");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001345
Sean Callanana144c3f2010-04-02 21:23:51 +00001346 if (readOpcodeModifier(insn))
1347 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001348
1349 if (size == 0)
1350 size = insn->registerSize;
1351
1352 switch (size) {
1353 case 1:
Sean Callanan06b766d2009-12-22 02:07:42 +00001354 insn->opcodeRegister = (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3)
1355 | insn->opcodeModifier));
Sean Callanana144c3f2010-04-02 21:23:51 +00001356 if (insn->rexPrefix &&
1357 insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1358 insn->opcodeRegister < MODRM_REG_AL + 0x8) {
Sean Callanan06b766d2009-12-22 02:07:42 +00001359 insn->opcodeRegister = (Reg)(MODRM_REG_SPL
1360 + (insn->opcodeRegister - MODRM_REG_AL - 4));
Sean Callanan8ed9f512009-12-19 02:59:52 +00001361 }
1362
1363 break;
1364 case 2:
Sean Callanan06b766d2009-12-22 02:07:42 +00001365 insn->opcodeRegister = (Reg)(MODRM_REG_AX
1366 + ((bFromREX(insn->rexPrefix) << 3)
1367 | insn->opcodeModifier));
Sean Callanan8ed9f512009-12-19 02:59:52 +00001368 break;
1369 case 4:
Sean Callanana144c3f2010-04-02 21:23:51 +00001370 insn->opcodeRegister = (Reg)(MODRM_REG_EAX
Sean Callanan06b766d2009-12-22 02:07:42 +00001371 + ((bFromREX(insn->rexPrefix) << 3)
1372 | insn->opcodeModifier));
Sean Callanan8ed9f512009-12-19 02:59:52 +00001373 break;
1374 case 8:
Sean Callanan06b766d2009-12-22 02:07:42 +00001375 insn->opcodeRegister = (Reg)(MODRM_REG_RAX
1376 + ((bFromREX(insn->rexPrefix) << 3)
1377 | insn->opcodeModifier));
Sean Callanan8ed9f512009-12-19 02:59:52 +00001378 break;
1379 }
Sean Callanana144c3f2010-04-02 21:23:51 +00001380
1381 return 0;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001382}
1383
1384/*
1385 * readImmediate - Consumes an immediate operand from an instruction, given the
1386 * desired operand size.
1387 *
1388 * @param insn - The instruction whose operand is to be read.
1389 * @param size - The width (in bytes) of the operand.
1390 * @return - 0 if the immediate was successfully consumed; nonzero
1391 * otherwise.
1392 */
1393static int readImmediate(struct InternalInstruction* insn, uint8_t size) {
1394 uint8_t imm8;
1395 uint16_t imm16;
1396 uint32_t imm32;
1397 uint64_t imm64;
1398
Nuno Lopes392bbd92009-12-19 12:07:00 +00001399 dbgprintf(insn, "readImmediate()");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001400
Sean Callanana144c3f2010-04-02 21:23:51 +00001401 if (insn->numImmediatesConsumed == 2) {
1402 debug("Already consumed two immediates");
1403 return -1;
1404 }
Sean Callanan8ed9f512009-12-19 02:59:52 +00001405
1406 if (size == 0)
1407 size = insn->immediateSize;
1408 else
1409 insn->immediateSize = size;
1410
1411 switch (size) {
1412 case 1:
1413 if (consumeByte(insn, &imm8))
1414 return -1;
1415 insn->immediates[insn->numImmediatesConsumed] = imm8;
1416 break;
1417 case 2:
1418 if (consumeUInt16(insn, &imm16))
1419 return -1;
1420 insn->immediates[insn->numImmediatesConsumed] = imm16;
1421 break;
1422 case 4:
1423 if (consumeUInt32(insn, &imm32))
1424 return -1;
1425 insn->immediates[insn->numImmediatesConsumed] = imm32;
1426 break;
1427 case 8:
1428 if (consumeUInt64(insn, &imm64))
1429 return -1;
1430 insn->immediates[insn->numImmediatesConsumed] = imm64;
1431 break;
1432 }
1433
1434 insn->numImmediatesConsumed++;
1435
1436 return 0;
1437}
1438
1439/*
Craig Topper4bbeb182011-09-13 07:37:44 +00001440 * readVVVV - Consumes vvvv from an instruction if it has a VEX prefix.
Sean Callanana21e2ea2011-03-15 01:23:15 +00001441 *
1442 * @param insn - The instruction whose operand is to be read.
Craig Topper4bbeb182011-09-13 07:37:44 +00001443 * @return - 0 if the vvvv was successfully consumed; nonzero
Sean Callanana21e2ea2011-03-15 01:23:15 +00001444 * otherwise.
1445 */
1446static int readVVVV(struct InternalInstruction* insn) {
1447 dbgprintf(insn, "readVVVV()");
1448
1449 if (insn->vexSize == 3)
1450 insn->vvvv = vvvvFromVEX3of3(insn->vexPrefix[2]);
1451 else if (insn->vexSize == 2)
1452 insn->vvvv = vvvvFromVEX2of2(insn->vexPrefix[1]);
1453 else
1454 return -1;
1455
Craig Topper04c5be92011-10-03 08:14:29 +00001456 if (insn->mode != MODE_64BIT)
1457 insn->vvvv &= 0x7;
1458
Sean Callanana21e2ea2011-03-15 01:23:15 +00001459 return 0;
1460}
1461
1462/*
Sean Callanan8ed9f512009-12-19 02:59:52 +00001463 * readOperands - Consults the specifier for an instruction and consumes all
1464 * operands for that instruction, interpreting them as it goes.
1465 *
1466 * @param insn - The instruction whose operands are to be read and interpreted.
1467 * @return - 0 if all operands could be read; nonzero otherwise.
1468 */
1469static int readOperands(struct InternalInstruction* insn) {
1470 int index;
Craig Topper4bbeb182011-09-13 07:37:44 +00001471 int hasVVVV, needVVVV;
Craig Topper06f554d2011-12-30 06:23:39 +00001472 int sawRegImm = 0;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001473
Nuno Lopes392bbd92009-12-19 12:07:00 +00001474 dbgprintf(insn, "readOperands()");
Craig Topper4bbeb182011-09-13 07:37:44 +00001475
1476 /* If non-zero vvvv specified, need to make sure one of the operands
1477 uses it. */
1478 hasVVVV = !readVVVV(insn);
1479 needVVVV = hasVVVV && (insn->vvvv != 0);
Sean Callanan8ed9f512009-12-19 02:59:52 +00001480
1481 for (index = 0; index < X86_MAX_OPERANDS; ++index) {
1482 switch (insn->spec->operands[index].encoding) {
1483 case ENCODING_NONE:
1484 break;
1485 case ENCODING_REG:
1486 case ENCODING_RM:
1487 if (readModRM(insn))
1488 return -1;
1489 if (fixupReg(insn, &insn->spec->operands[index]))
1490 return -1;
1491 break;
1492 case ENCODING_CB:
1493 case ENCODING_CW:
1494 case ENCODING_CD:
1495 case ENCODING_CP:
1496 case ENCODING_CO:
1497 case ENCODING_CT:
Nuno Lopes392bbd92009-12-19 12:07:00 +00001498 dbgprintf(insn, "We currently don't hande code-offset encodings");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001499 return -1;
1500 case ENCODING_IB:
Craig Topper06f554d2011-12-30 06:23:39 +00001501 if (sawRegImm) {
Benjamin Kramera5f89422012-01-04 22:06:45 +00001502 /* Saw a register immediate so don't read again and instead split the
1503 previous immediate. FIXME: This is a hack. */
Benjamin Kramer89435742012-01-01 17:55:36 +00001504 insn->immediates[insn->numImmediatesConsumed] =
1505 insn->immediates[insn->numImmediatesConsumed - 1] & 0xf;
1506 ++insn->numImmediatesConsumed;
Craig Topper06f554d2011-12-30 06:23:39 +00001507 break;
1508 }
Sean Callanan8ed9f512009-12-19 02:59:52 +00001509 if (readImmediate(insn, 1))
1510 return -1;
Sean Callanan5edca812010-04-07 21:42:19 +00001511 if (insn->spec->operands[index].type == TYPE_IMM3 &&
1512 insn->immediates[insn->numImmediatesConsumed - 1] > 7)
1513 return -1;
Craig Topper06f554d2011-12-30 06:23:39 +00001514 if (insn->spec->operands[index].type == TYPE_XMM128 ||
1515 insn->spec->operands[index].type == TYPE_XMM256)
1516 sawRegImm = 1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001517 break;
1518 case ENCODING_IW:
1519 if (readImmediate(insn, 2))
1520 return -1;
1521 break;
1522 case ENCODING_ID:
1523 if (readImmediate(insn, 4))
1524 return -1;
1525 break;
1526 case ENCODING_IO:
1527 if (readImmediate(insn, 8))
1528 return -1;
1529 break;
1530 case ENCODING_Iv:
Sean Callanana144c3f2010-04-02 21:23:51 +00001531 if (readImmediate(insn, insn->immediateSize))
1532 return -1;
Chris Lattneraef1fea2010-04-16 21:15:15 +00001533 break;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001534 case ENCODING_Ia:
Sean Callanana144c3f2010-04-02 21:23:51 +00001535 if (readImmediate(insn, insn->addressSize))
1536 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001537 break;
1538 case ENCODING_RB:
Sean Callanana144c3f2010-04-02 21:23:51 +00001539 if (readOpcodeRegister(insn, 1))
1540 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001541 break;
1542 case ENCODING_RW:
Sean Callanana144c3f2010-04-02 21:23:51 +00001543 if (readOpcodeRegister(insn, 2))
1544 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001545 break;
1546 case ENCODING_RD:
Sean Callanana144c3f2010-04-02 21:23:51 +00001547 if (readOpcodeRegister(insn, 4))
1548 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001549 break;
1550 case ENCODING_RO:
Sean Callanana144c3f2010-04-02 21:23:51 +00001551 if (readOpcodeRegister(insn, 8))
1552 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001553 break;
1554 case ENCODING_Rv:
Sean Callanana144c3f2010-04-02 21:23:51 +00001555 if (readOpcodeRegister(insn, 0))
1556 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001557 break;
1558 case ENCODING_I:
Sean Callanana144c3f2010-04-02 21:23:51 +00001559 if (readOpcodeModifier(insn))
1560 return -1;
Sean Callanana21e2ea2011-03-15 01:23:15 +00001561 break;
1562 case ENCODING_VVVV:
Craig Topper4bbeb182011-09-13 07:37:44 +00001563 needVVVV = 0; /* Mark that we have found a VVVV operand. */
1564 if (!hasVVVV)
Sean Callanana21e2ea2011-03-15 01:23:15 +00001565 return -1;
1566 if (fixupReg(insn, &insn->spec->operands[index]))
1567 return -1;
1568 break;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001569 case ENCODING_DUP:
1570 break;
1571 default:
Nuno Lopes392bbd92009-12-19 12:07:00 +00001572 dbgprintf(insn, "Encountered an operand with an unknown encoding.");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001573 return -1;
1574 }
1575 }
Craig Topper4bbeb182011-09-13 07:37:44 +00001576
1577 /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */
1578 if (needVVVV) return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001579
1580 return 0;
1581}
1582
1583/*
1584 * decodeInstruction - Reads and interprets a full instruction provided by the
1585 * user.
1586 *
1587 * @param insn - A pointer to the instruction to be populated. Must be
1588 * pre-allocated.
1589 * @param reader - The function to be used to read the instruction's bytes.
1590 * @param readerArg - A generic argument to be passed to the reader to store
1591 * any internal state.
1592 * @param logger - If non-NULL, the function to be used to write log messages
1593 * and warnings.
1594 * @param loggerArg - A generic argument to be passed to the logger to store
1595 * any internal state.
1596 * @param startLoc - The address (in the reader's address space) of the first
1597 * byte in the instruction.
1598 * @param mode - The mode (real mode, IA-32e, or IA-32e in 64-bit mode) to
1599 * decode the instruction in.
1600 * @return - 0 if the instruction's memory could be read; nonzero if
1601 * not.
1602 */
1603int decodeInstruction(struct InternalInstruction* insn,
1604 byteReader_t reader,
1605 void* readerArg,
1606 dlog_t logger,
1607 void* loggerArg,
1608 uint64_t startLoc,
1609 DisassemblerMode mode) {
Daniel Dunbar71f842d2009-12-19 03:31:50 +00001610 memset(insn, 0, sizeof(struct InternalInstruction));
Sean Callanan8ed9f512009-12-19 02:59:52 +00001611
1612 insn->reader = reader;
1613 insn->readerArg = readerArg;
1614 insn->dlog = logger;
1615 insn->dlogArg = loggerArg;
1616 insn->startLocation = startLoc;
1617 insn->readerCursor = startLoc;
1618 insn->mode = mode;
1619 insn->numImmediatesConsumed = 0;
1620
1621 if (readPrefixes(insn) ||
1622 readOpcode(insn) ||
1623 getID(insn) ||
1624 insn->instructionID == 0 ||
1625 readOperands(insn))
1626 return -1;
1627
1628 insn->length = insn->readerCursor - insn->startLocation;
1629
Benjamin Kramer7c97ed72010-03-18 12:18:36 +00001630 dbgprintf(insn, "Read from 0x%llx to 0x%llx: length %zu",
1631 startLoc, insn->readerCursor, insn->length);
Sean Callanan8ed9f512009-12-19 02:59:52 +00001632
1633 if (insn->length > 15)
Nuno Lopes392bbd92009-12-19 12:07:00 +00001634 dbgprintf(insn, "Instruction exceeds 15-byte limit");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001635
1636 return 0;
1637}