blob: c15709d33dae3a13e51227b9cba4e2a52e8bc0f7 [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) {
Duncan Sands5b8a1db2012-02-05 14:20:11 +0000106 const struct ModRMDecision* dec = 0;
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:
Craig Topperce8f4c52012-02-09 07:45:30 +0000134 return modRMTable[dec->instructionIDs];
Sean Callanan8ed9f512009-12-19 02:59:52 +0000135 case MODRM_SPLITRM:
136 if (modFromModRM(modRM) == 0x3)
Craig Topperce8f4c52012-02-09 07:45:30 +0000137 return modRMTable[dec->instructionIDs+1];
138 return modRMTable[dec->instructionIDs];
Craig Topperf41ab772012-02-09 08:58:07 +0000139 case MODRM_SPLITREG:
140 if (modFromModRM(modRM) == 0x3)
141 return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)+8];
142 return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)];
Sean Callanan8ed9f512009-12-19 02:59:52 +0000143 case MODRM_FULL:
Craig Topperce8f4c52012-02-09 07:45:30 +0000144 return modRMTable[dec->instructionIDs+modRM];
Sean Callanan8ed9f512009-12-19 02:59:52 +0000145 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000146}
147
148/*
149 * specifierForUID - Given a UID, returns the name and operand specification for
150 * that instruction.
151 *
152 * @param uid - The unique ID for the instruction. This should be returned by
153 * decode(); specifierForUID will not check bounds.
154 * @return - A pointer to the specification for that instruction.
155 */
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000156static const struct InstructionSpecifier *specifierForUID(InstrUID uid) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000157 return &INSTRUCTIONS_SYM[uid];
158}
159
160/*
161 * consumeByte - Uses the reader function provided by the user to consume one
162 * byte from the instruction's memory and advance the cursor.
163 *
164 * @param insn - The instruction with the reader function to use. The cursor
165 * for this instruction is advanced.
166 * @param byte - A pointer to a pre-allocated memory buffer to be populated
167 * with the data read.
168 * @return - 0 if the read was successful; nonzero otherwise.
169 */
Sean Callanan542eabc2009-12-22 22:51:40 +0000170static int consumeByte(struct InternalInstruction* insn, uint8_t* byte) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000171 int ret = insn->reader(insn->readerArg, byte, insn->readerCursor);
172
173 if (!ret)
174 ++(insn->readerCursor);
175
176 return ret;
177}
178
179/*
180 * lookAtByte - Like consumeByte, but does not advance the cursor.
181 *
182 * @param insn - See consumeByte().
183 * @param byte - See consumeByte().
184 * @return - See consumeByte().
185 */
Sean Callanan542eabc2009-12-22 22:51:40 +0000186static int lookAtByte(struct InternalInstruction* insn, uint8_t* byte) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000187 return insn->reader(insn->readerArg, byte, insn->readerCursor);
188}
189
Sean Callanan542eabc2009-12-22 22:51:40 +0000190static void unconsumeByte(struct InternalInstruction* insn) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000191 insn->readerCursor--;
192}
193
Sean Callanan542eabc2009-12-22 22:51:40 +0000194#define CONSUME_FUNC(name, type) \
195 static int name(struct InternalInstruction* insn, type* ptr) { \
196 type combined = 0; \
197 unsigned offset; \
198 for (offset = 0; offset < sizeof(type); ++offset) { \
199 uint8_t byte; \
200 int ret = insn->reader(insn->readerArg, \
201 &byte, \
202 insn->readerCursor + offset); \
203 if (ret) \
204 return ret; \
205 combined = combined | ((type)byte << ((type)offset * 8)); \
206 } \
207 *ptr = combined; \
208 insn->readerCursor += sizeof(type); \
209 return 0; \
Sean Callanan8ed9f512009-12-19 02:59:52 +0000210 }
211
212/*
213 * consume* - Use the reader function provided by the user to consume data
214 * values of various sizes from the instruction's memory and advance the
215 * cursor appropriately. These readers perform endian conversion.
216 *
217 * @param insn - See consumeByte().
218 * @param ptr - A pointer to a pre-allocated memory of appropriate size to
219 * be populated with the data read.
220 * @return - See consumeByte().
221 */
222CONSUME_FUNC(consumeInt8, int8_t)
223CONSUME_FUNC(consumeInt16, int16_t)
224CONSUME_FUNC(consumeInt32, int32_t)
225CONSUME_FUNC(consumeUInt16, uint16_t)
226CONSUME_FUNC(consumeUInt32, uint32_t)
227CONSUME_FUNC(consumeUInt64, uint64_t)
228
229/*
Nuno Lopes392bbd92009-12-19 12:07:00 +0000230 * dbgprintf - Uses the logging function provided by the user to log a single
Sean Callanan8ed9f512009-12-19 02:59:52 +0000231 * message, typically without a carriage-return.
232 *
233 * @param insn - The instruction containing the logging function.
234 * @param format - See printf().
235 * @param ... - See printf().
236 */
Sean Callanan542eabc2009-12-22 22:51:40 +0000237static void dbgprintf(struct InternalInstruction* insn,
238 const char* format,
239 ...) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000240 char buffer[256];
241 va_list ap;
242
243 if (!insn->dlog)
244 return;
245
246 va_start(ap, format);
247 (void)vsnprintf(buffer, sizeof(buffer), format, ap);
248 va_end(ap);
249
250 insn->dlog(insn->dlogArg, buffer);
251
252 return;
253}
254
255/*
256 * setPrefixPresent - Marks that a particular prefix is present at a particular
257 * location.
258 *
259 * @param insn - The instruction to be marked as having the prefix.
260 * @param prefix - The prefix that is present.
261 * @param location - The location where the prefix is located (in the address
262 * space of the instruction's reader).
263 */
Sean Callanan542eabc2009-12-22 22:51:40 +0000264static void setPrefixPresent(struct InternalInstruction* insn,
Sean Callanan8ed9f512009-12-19 02:59:52 +0000265 uint8_t prefix,
266 uint64_t location)
267{
268 insn->prefixPresent[prefix] = 1;
269 insn->prefixLocations[prefix] = location;
270}
271
272/*
273 * isPrefixAtLocation - Queries an instruction to determine whether a prefix is
274 * present at a given location.
275 *
276 * @param insn - The instruction to be queried.
277 * @param prefix - The prefix.
278 * @param location - The location to query.
279 * @return - Whether the prefix is at that location.
280 */
Sean Callanan542eabc2009-12-22 22:51:40 +0000281static BOOL isPrefixAtLocation(struct InternalInstruction* insn,
282 uint8_t prefix,
283 uint64_t location)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000284{
285 if (insn->prefixPresent[prefix] == 1 &&
286 insn->prefixLocations[prefix] == location)
287 return TRUE;
288 else
289 return FALSE;
290}
291
292/*
293 * readPrefixes - Consumes all of an instruction's prefix bytes, and marks the
294 * instruction as having them. Also sets the instruction's default operand,
295 * address, and other relevant data sizes to report operands correctly.
296 *
297 * @param insn - The instruction whose prefixes are to be read.
298 * @return - 0 if the instruction could be read until the end of the prefix
299 * bytes, and no prefixes conflicted; nonzero otherwise.
300 */
301static int readPrefixes(struct InternalInstruction* insn) {
302 BOOL isPrefix = TRUE;
303 BOOL prefixGroups[4] = { FALSE };
304 uint64_t prefixLocation;
Ted Kremenek584520e2011-01-23 17:05:06 +0000305 uint8_t byte = 0;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000306
307 BOOL hasAdSize = FALSE;
308 BOOL hasOpSize = FALSE;
309
Nuno Lopes392bbd92009-12-19 12:07:00 +0000310 dbgprintf(insn, "readPrefixes()");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000311
312 while (isPrefix) {
313 prefixLocation = insn->readerCursor;
314
315 if (consumeByte(insn, &byte))
316 return -1;
317
318 switch (byte) {
319 case 0xf0: /* LOCK */
320 case 0xf2: /* REPNE/REPNZ */
321 case 0xf3: /* REP or REPE/REPZ */
322 if (prefixGroups[0])
Nuno Lopes392bbd92009-12-19 12:07:00 +0000323 dbgprintf(insn, "Redundant Group 1 prefix");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000324 prefixGroups[0] = TRUE;
325 setPrefixPresent(insn, byte, prefixLocation);
326 break;
327 case 0x2e: /* CS segment override -OR- Branch not taken */
328 case 0x36: /* SS segment override -OR- Branch taken */
329 case 0x3e: /* DS segment override */
330 case 0x26: /* ES segment override */
331 case 0x64: /* FS segment override */
332 case 0x65: /* GS segment override */
333 switch (byte) {
334 case 0x2e:
335 insn->segmentOverride = SEG_OVERRIDE_CS;
336 break;
337 case 0x36:
338 insn->segmentOverride = SEG_OVERRIDE_SS;
339 break;
340 case 0x3e:
341 insn->segmentOverride = SEG_OVERRIDE_DS;
342 break;
343 case 0x26:
344 insn->segmentOverride = SEG_OVERRIDE_ES;
345 break;
346 case 0x64:
347 insn->segmentOverride = SEG_OVERRIDE_FS;
348 break;
349 case 0x65:
350 insn->segmentOverride = SEG_OVERRIDE_GS;
351 break;
352 default:
Sean Callanana144c3f2010-04-02 21:23:51 +0000353 debug("Unhandled override");
354 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000355 }
356 if (prefixGroups[1])
Nuno Lopes392bbd92009-12-19 12:07:00 +0000357 dbgprintf(insn, "Redundant Group 2 prefix");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000358 prefixGroups[1] = TRUE;
359 setPrefixPresent(insn, byte, prefixLocation);
360 break;
361 case 0x66: /* Operand-size override */
362 if (prefixGroups[2])
Nuno Lopes392bbd92009-12-19 12:07:00 +0000363 dbgprintf(insn, "Redundant Group 3 prefix");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000364 prefixGroups[2] = TRUE;
365 hasOpSize = TRUE;
366 setPrefixPresent(insn, byte, prefixLocation);
367 break;
368 case 0x67: /* Address-size override */
369 if (prefixGroups[3])
Nuno Lopes392bbd92009-12-19 12:07:00 +0000370 dbgprintf(insn, "Redundant Group 4 prefix");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000371 prefixGroups[3] = TRUE;
372 hasAdSize = TRUE;
373 setPrefixPresent(insn, byte, prefixLocation);
374 break;
375 default: /* Not a prefix byte */
376 isPrefix = FALSE;
377 break;
378 }
379
380 if (isPrefix)
Nuno Lopes392bbd92009-12-19 12:07:00 +0000381 dbgprintf(insn, "Found prefix 0x%hhx", byte);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000382 }
Sean Callanana21e2ea2011-03-15 01:23:15 +0000383
384 insn->vexSize = 0;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000385
Sean Callanana21e2ea2011-03-15 01:23:15 +0000386 if (byte == 0xc4) {
387 uint8_t byte1;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000388
Sean Callanana21e2ea2011-03-15 01:23:15 +0000389 if (lookAtByte(insn, &byte1)) {
390 dbgprintf(insn, "Couldn't read second byte of VEX");
391 return -1;
392 }
393
Craig Topper100d86a2011-09-26 05:12:43 +0000394 if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) {
Sean Callanana21e2ea2011-03-15 01:23:15 +0000395 insn->vexSize = 3;
396 insn->necessaryPrefixLocation = insn->readerCursor - 1;
397 }
398 else {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000399 unconsumeByte(insn);
400 insn->necessaryPrefixLocation = insn->readerCursor - 1;
401 }
Sean Callanana21e2ea2011-03-15 01:23:15 +0000402
403 if (insn->vexSize == 3) {
404 insn->vexPrefix[0] = byte;
405 consumeByte(insn, &insn->vexPrefix[1]);
406 consumeByte(insn, &insn->vexPrefix[2]);
407
408 /* We simulate the REX prefix for simplicity's sake */
Craig Topper7b229762011-10-03 07:51:09 +0000409
410 if (insn->mode == MODE_64BIT) {
411 insn->rexPrefix = 0x40
412 | (wFromVEX3of3(insn->vexPrefix[2]) << 3)
413 | (rFromVEX2of3(insn->vexPrefix[1]) << 2)
414 | (xFromVEX2of3(insn->vexPrefix[1]) << 1)
415 | (bFromVEX2of3(insn->vexPrefix[1]) << 0);
416 }
Sean Callanana21e2ea2011-03-15 01:23:15 +0000417
418 switch (ppFromVEX3of3(insn->vexPrefix[2]))
419 {
420 default:
421 break;
422 case VEX_PREFIX_66:
423 hasOpSize = TRUE;
424 break;
425 }
426
427 dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx 0x%hhx", insn->vexPrefix[0], insn->vexPrefix[1], insn->vexPrefix[2]);
428 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000429 }
Sean Callanana21e2ea2011-03-15 01:23:15 +0000430 else if (byte == 0xc5) {
431 uint8_t byte1;
432
433 if (lookAtByte(insn, &byte1)) {
434 dbgprintf(insn, "Couldn't read second byte of VEX");
435 return -1;
436 }
437
Craig Topper100d86a2011-09-26 05:12:43 +0000438 if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) {
Sean Callanana21e2ea2011-03-15 01:23:15 +0000439 insn->vexSize = 2;
440 }
441 else {
442 unconsumeByte(insn);
443 }
444
445 if (insn->vexSize == 2) {
446 insn->vexPrefix[0] = byte;
447 consumeByte(insn, &insn->vexPrefix[1]);
448
Craig Topper7b229762011-10-03 07:51:09 +0000449 if (insn->mode == MODE_64BIT) {
450 insn->rexPrefix = 0x40
451 | (rFromVEX2of2(insn->vexPrefix[1]) << 2);
452 }
Sean Callanana21e2ea2011-03-15 01:23:15 +0000453
454 switch (ppFromVEX2of2(insn->vexPrefix[1]))
455 {
456 default:
457 break;
458 case VEX_PREFIX_66:
459 hasOpSize = TRUE;
460 break;
461 }
462
463 dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx", insn->vexPrefix[0], insn->vexPrefix[1]);
464 }
465 }
466 else {
467 if (insn->mode == MODE_64BIT) {
468 if ((byte & 0xf0) == 0x40) {
469 uint8_t opcodeByte;
470
471 if (lookAtByte(insn, &opcodeByte) || ((opcodeByte & 0xf0) == 0x40)) {
472 dbgprintf(insn, "Redundant REX prefix");
473 return -1;
474 }
475
476 insn->rexPrefix = byte;
477 insn->necessaryPrefixLocation = insn->readerCursor - 2;
478
479 dbgprintf(insn, "Found REX prefix 0x%hhx", byte);
480 } else {
481 unconsumeByte(insn);
482 insn->necessaryPrefixLocation = insn->readerCursor - 1;
483 }
484 } else {
485 unconsumeByte(insn);
486 insn->necessaryPrefixLocation = insn->readerCursor - 1;
487 }
488 }
489
Sean Callanan8ed9f512009-12-19 02:59:52 +0000490 if (insn->mode == MODE_16BIT) {
491 insn->registerSize = (hasOpSize ? 4 : 2);
492 insn->addressSize = (hasAdSize ? 4 : 2);
493 insn->displacementSize = (hasAdSize ? 4 : 2);
494 insn->immediateSize = (hasOpSize ? 4 : 2);
495 } else if (insn->mode == MODE_32BIT) {
496 insn->registerSize = (hasOpSize ? 2 : 4);
497 insn->addressSize = (hasAdSize ? 2 : 4);
498 insn->displacementSize = (hasAdSize ? 2 : 4);
Sean Callanan751752e2010-10-22 01:24:11 +0000499 insn->immediateSize = (hasOpSize ? 2 : 4);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000500 } else if (insn->mode == MODE_64BIT) {
501 if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
502 insn->registerSize = 8;
503 insn->addressSize = (hasAdSize ? 4 : 8);
504 insn->displacementSize = 4;
505 insn->immediateSize = 4;
506 } else if (insn->rexPrefix) {
507 insn->registerSize = (hasOpSize ? 2 : 4);
508 insn->addressSize = (hasAdSize ? 4 : 8);
509 insn->displacementSize = (hasOpSize ? 2 : 4);
510 insn->immediateSize = (hasOpSize ? 2 : 4);
511 } else {
512 insn->registerSize = (hasOpSize ? 2 : 4);
513 insn->addressSize = (hasAdSize ? 4 : 8);
514 insn->displacementSize = (hasOpSize ? 2 : 4);
515 insn->immediateSize = (hasOpSize ? 2 : 4);
516 }
517 }
518
519 return 0;
520}
521
522/*
523 * readOpcode - Reads the opcode (excepting the ModR/M byte in the case of
524 * extended or escape opcodes).
525 *
526 * @param insn - The instruction whose opcode is to be read.
527 * @return - 0 if the opcode could be read successfully; nonzero otherwise.
528 */
529static int readOpcode(struct InternalInstruction* insn) {
530 /* Determine the length of the primary opcode */
531
532 uint8_t current;
533
Nuno Lopes392bbd92009-12-19 12:07:00 +0000534 dbgprintf(insn, "readOpcode()");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000535
536 insn->opcodeType = ONEBYTE;
Sean Callanana21e2ea2011-03-15 01:23:15 +0000537
538 if (insn->vexSize == 3)
539 {
540 switch (mmmmmFromVEX2of3(insn->vexPrefix[1]))
541 {
542 default:
543 dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)", mmmmmFromVEX2of3(insn->vexPrefix[1]));
544 return -1;
545 case 0:
546 break;
547 case VEX_LOB_0F:
548 insn->twoByteEscape = 0x0f;
549 insn->opcodeType = TWOBYTE;
550 return consumeByte(insn, &insn->opcode);
551 case VEX_LOB_0F38:
552 insn->twoByteEscape = 0x0f;
553 insn->threeByteEscape = 0x38;
554 insn->opcodeType = THREEBYTE_38;
555 return consumeByte(insn, &insn->opcode);
556 case VEX_LOB_0F3A:
557 insn->twoByteEscape = 0x0f;
558 insn->threeByteEscape = 0x3a;
559 insn->opcodeType = THREEBYTE_3A;
560 return consumeByte(insn, &insn->opcode);
561 }
562 }
563 else if (insn->vexSize == 2)
564 {
565 insn->twoByteEscape = 0x0f;
566 insn->opcodeType = TWOBYTE;
567 return consumeByte(insn, &insn->opcode);
568 }
569
Sean Callanan8ed9f512009-12-19 02:59:52 +0000570 if (consumeByte(insn, &current))
571 return -1;
572
573 if (current == 0x0f) {
Nuno Lopes392bbd92009-12-19 12:07:00 +0000574 dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000575
576 insn->twoByteEscape = current;
577
578 if (consumeByte(insn, &current))
579 return -1;
580
581 if (current == 0x38) {
Nuno Lopes392bbd92009-12-19 12:07:00 +0000582 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000583
584 insn->threeByteEscape = current;
585
586 if (consumeByte(insn, &current))
587 return -1;
588
589 insn->opcodeType = THREEBYTE_38;
590 } else if (current == 0x3a) {
Nuno Lopes392bbd92009-12-19 12:07:00 +0000591 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000592
593 insn->threeByteEscape = current;
594
595 if (consumeByte(insn, &current))
596 return -1;
597
598 insn->opcodeType = THREEBYTE_3A;
Joerg Sonnenberger4a8ac8d2011-04-04 16:58:13 +0000599 } else if (current == 0xa6) {
600 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
601
602 insn->threeByteEscape = current;
603
604 if (consumeByte(insn, &current))
605 return -1;
606
607 insn->opcodeType = THREEBYTE_A6;
608 } else if (current == 0xa7) {
609 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
610
611 insn->threeByteEscape = current;
612
613 if (consumeByte(insn, &current))
614 return -1;
615
616 insn->opcodeType = THREEBYTE_A7;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000617 } else {
Nuno Lopes392bbd92009-12-19 12:07:00 +0000618 dbgprintf(insn, "Didn't find a three-byte escape prefix");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000619
620 insn->opcodeType = TWOBYTE;
621 }
622 }
623
624 /*
625 * At this point we have consumed the full opcode.
626 * Anything we consume from here on must be unconsumed.
627 */
628
629 insn->opcode = current;
630
631 return 0;
632}
633
634static int readModRM(struct InternalInstruction* insn);
635
636/*
637 * getIDWithAttrMask - Determines the ID of an instruction, consuming
638 * the ModR/M byte as appropriate for extended and escape opcodes,
639 * and using a supplied attribute mask.
640 *
641 * @param instructionID - A pointer whose target is filled in with the ID of the
642 * instruction.
643 * @param insn - The instruction whose ID is to be determined.
644 * @param attrMask - The attribute mask to search.
645 * @return - 0 if the ModR/M could be read when needed or was not
646 * needed; nonzero otherwise.
647 */
648static int getIDWithAttrMask(uint16_t* instructionID,
649 struct InternalInstruction* insn,
650 uint8_t attrMask) {
651 BOOL hasModRMExtension;
652
653 uint8_t instructionClass;
654
655 instructionClass = contextForAttrs(attrMask);
656
657 hasModRMExtension = modRMRequired(insn->opcodeType,
658 instructionClass,
659 insn->opcode);
660
661 if (hasModRMExtension) {
Rafael Espindola2f867a62011-01-06 16:48:42 +0000662 if (readModRM(insn))
663 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000664
665 *instructionID = decode(insn->opcodeType,
666 instructionClass,
667 insn->opcode,
668 insn->modRM);
669 } else {
670 *instructionID = decode(insn->opcodeType,
671 instructionClass,
672 insn->opcode,
673 0);
674 }
675
676 return 0;
677}
678
679/*
680 * is16BitEquivalent - Determines whether two instruction names refer to
681 * equivalent instructions but one is 16-bit whereas the other is not.
682 *
683 * @param orig - The instruction that is not 16-bit
684 * @param equiv - The instruction that is 16-bit
685 */
686static BOOL is16BitEquvalent(const char* orig, const char* equiv) {
687 off_t i;
688
Sean Callanana144c3f2010-04-02 21:23:51 +0000689 for (i = 0;; i++) {
690 if (orig[i] == '\0' && equiv[i] == '\0')
Sean Callanan8ed9f512009-12-19 02:59:52 +0000691 return TRUE;
Sean Callanana144c3f2010-04-02 21:23:51 +0000692 if (orig[i] == '\0' || equiv[i] == '\0')
Sean Callanan8ed9f512009-12-19 02:59:52 +0000693 return FALSE;
Sean Callanana144c3f2010-04-02 21:23:51 +0000694 if (orig[i] != equiv[i]) {
695 if ((orig[i] == 'Q' || orig[i] == 'L') && equiv[i] == 'W')
Sean Callanan8ed9f512009-12-19 02:59:52 +0000696 continue;
Sean Callanana144c3f2010-04-02 21:23:51 +0000697 if ((orig[i] == '6' || orig[i] == '3') && equiv[i] == '1')
Sean Callanan8ed9f512009-12-19 02:59:52 +0000698 continue;
Sean Callanana144c3f2010-04-02 21:23:51 +0000699 if ((orig[i] == '4' || orig[i] == '2') && equiv[i] == '6')
Sean Callanan8ed9f512009-12-19 02:59:52 +0000700 continue;
701 return FALSE;
702 }
703 }
704}
705
706/*
Sean Callanan8ed9f512009-12-19 02:59:52 +0000707 * getID - Determines the ID of an instruction, consuming the ModR/M byte as
708 * appropriate for extended and escape opcodes. Determines the attributes and
709 * context for the instruction before doing so.
710 *
711 * @param insn - The instruction whose ID is to be determined.
712 * @return - 0 if the ModR/M could be read when needed or was not needed;
713 * nonzero otherwise.
714 */
Benjamin Kramer953362c2012-02-11 14:50:54 +0000715static int getID(struct InternalInstruction* insn, void *miiArg) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000716 uint8_t attrMask;
717 uint16_t instructionID;
718
Nuno Lopes392bbd92009-12-19 12:07:00 +0000719 dbgprintf(insn, "getID()");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000720
721 attrMask = ATTR_NONE;
Sean Callanana21e2ea2011-03-15 01:23:15 +0000722
Sean Callanan8ed9f512009-12-19 02:59:52 +0000723 if (insn->mode == MODE_64BIT)
724 attrMask |= ATTR_64BIT;
Sean Callanana21e2ea2011-03-15 01:23:15 +0000725
726 if (insn->vexSize) {
727 attrMask |= ATTR_VEX;
728
729 if (insn->vexSize == 3) {
730 switch (ppFromVEX3of3(insn->vexPrefix[2])) {
731 case VEX_PREFIX_66:
732 attrMask |= ATTR_OPSIZE;
733 break;
734 case VEX_PREFIX_F3:
735 attrMask |= ATTR_XS;
736 break;
737 case VEX_PREFIX_F2:
738 attrMask |= ATTR_XD;
739 break;
740 }
741
Sean Callanana21e2ea2011-03-15 01:23:15 +0000742 if (lFromVEX3of3(insn->vexPrefix[2]))
743 attrMask |= ATTR_VEXL;
744 }
745 else if (insn->vexSize == 2) {
746 switch (ppFromVEX2of2(insn->vexPrefix[1])) {
747 case VEX_PREFIX_66:
748 attrMask |= ATTR_OPSIZE;
749 break;
750 case VEX_PREFIX_F3:
751 attrMask |= ATTR_XS;
752 break;
753 case VEX_PREFIX_F2:
754 attrMask |= ATTR_XD;
755 break;
756 }
757
758 if (lFromVEX2of2(insn->vexPrefix[1]))
759 attrMask |= ATTR_VEXL;
760 }
761 else {
762 return -1;
763 }
764 }
765 else {
Sean Callanana21e2ea2011-03-15 01:23:15 +0000766 if (isPrefixAtLocation(insn, 0x66, insn->necessaryPrefixLocation))
767 attrMask |= ATTR_OPSIZE;
768 else if (isPrefixAtLocation(insn, 0xf3, insn->necessaryPrefixLocation))
769 attrMask |= ATTR_XS;
770 else if (isPrefixAtLocation(insn, 0xf2, insn->necessaryPrefixLocation))
771 attrMask |= ATTR_XD;
Sean Callanana21e2ea2011-03-15 01:23:15 +0000772 }
773
Craig Topper6744a172011-10-04 06:30:42 +0000774 if (insn->rexPrefix & 0x08)
775 attrMask |= ATTR_REXW;
Craig Topperc8eb8802011-11-06 23:04:08 +0000776
Sean Callanana144c3f2010-04-02 21:23:51 +0000777 if (getIDWithAttrMask(&instructionID, insn, attrMask))
Sean Callanan8ed9f512009-12-19 02:59:52 +0000778 return -1;
Craig Topperc8eb8802011-11-06 23:04:08 +0000779
Sean Callanan8ed9f512009-12-19 02:59:52 +0000780 /* The following clauses compensate for limitations of the tables. */
Craig Topperc8eb8802011-11-06 23:04:08 +0000781
782 if ((attrMask & ATTR_VEXL) && (attrMask & ATTR_REXW) &&
783 !(attrMask & ATTR_OPSIZE)) {
Craig Topper6744a172011-10-04 06:30:42 +0000784 /*
785 * Some VEX instructions ignore the L-bit, but use the W-bit. Normally L-bit
786 * has precedence since there are no L-bit with W-bit entries in the tables.
787 * So if the L-bit isn't significant we should use the W-bit instead.
Craig Topperc8eb8802011-11-06 23:04:08 +0000788 * We only need to do this if the instruction doesn't specify OpSize since
789 * there is a VEX_L_W_OPSIZE table.
Craig Topper6744a172011-10-04 06:30:42 +0000790 */
791
792 const struct InstructionSpecifier *spec;
793 uint16_t instructionIDWithWBit;
794 const struct InstructionSpecifier *specWithWBit;
795
796 spec = specifierForUID(instructionID);
797
798 if (getIDWithAttrMask(&instructionIDWithWBit,
799 insn,
800 (attrMask & (~ATTR_VEXL)) | ATTR_REXW)) {
801 insn->instructionID = instructionID;
802 insn->spec = spec;
803 return 0;
804 }
805
806 specWithWBit = specifierForUID(instructionIDWithWBit);
807
808 if (instructionID != instructionIDWithWBit) {
809 insn->instructionID = instructionIDWithWBit;
810 insn->spec = specWithWBit;
811 } else {
812 insn->instructionID = instructionID;
813 insn->spec = spec;
814 }
815 return 0;
816 }
817
Sean Callanan8ed9f512009-12-19 02:59:52 +0000818 if (insn->prefixPresent[0x66] && !(attrMask & ATTR_OPSIZE)) {
819 /*
820 * The instruction tables make no distinction between instructions that
821 * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a
822 * particular spot (i.e., many MMX operations). In general we're
823 * conservative, but in the specific case where OpSize is present but not
824 * in the right place we check if there's a 16-bit operation.
825 */
826
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000827 const struct InstructionSpecifier *spec;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000828 uint16_t instructionIDWithOpsize;
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000829 const struct InstructionSpecifier *specWithOpsize;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000830
831 spec = specifierForUID(instructionID);
832
833 if (getIDWithAttrMask(&instructionIDWithOpsize,
834 insn,
835 attrMask | ATTR_OPSIZE)) {
836 /*
837 * ModRM required with OpSize but not present; give up and return version
838 * without OpSize set
839 */
840
841 insn->instructionID = instructionID;
842 insn->spec = spec;
843 return 0;
844 }
845
846 specWithOpsize = specifierForUID(instructionIDWithOpsize);
Benjamin Kramer953362c2012-02-11 14:50:54 +0000847
848 const char *specName = x86DisassemblerGetInstrName(instructionID, miiArg);
849 const char *specWithOpSizeSizeName =
850 x86DisassemblerGetInstrName(instructionIDWithOpsize, miiArg);
851
852 if (is16BitEquvalent(specName, specWithOpSizeSizeName)) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000853 insn->instructionID = instructionIDWithOpsize;
854 insn->spec = specWithOpsize;
855 } else {
856 insn->instructionID = instructionID;
857 insn->spec = spec;
858 }
859 return 0;
860 }
Craig Topper146c6d72011-10-02 16:56:09 +0000861
862 if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 &&
863 insn->rexPrefix & 0x01) {
864 /*
865 * NOOP shouldn't decode as NOOP if REX.b is set. Instead
866 * it should decode as XCHG %r8, %eax.
867 */
868
869 const struct InstructionSpecifier *spec;
870 uint16_t instructionIDWithNewOpcode;
871 const struct InstructionSpecifier *specWithNewOpcode;
872
873 spec = specifierForUID(instructionID);
874
Craig Topper41e59c72011-10-05 03:29:32 +0000875 /* Borrow opcode from one of the other XCHGar opcodes */
Craig Topper146c6d72011-10-02 16:56:09 +0000876 insn->opcode = 0x91;
877
878 if (getIDWithAttrMask(&instructionIDWithNewOpcode,
879 insn,
880 attrMask)) {
881 insn->opcode = 0x90;
882
883 insn->instructionID = instructionID;
884 insn->spec = spec;
885 return 0;
886 }
887
888 specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode);
889
Craig Topper41e59c72011-10-05 03:29:32 +0000890 /* Change back */
Craig Topper146c6d72011-10-02 16:56:09 +0000891 insn->opcode = 0x90;
892
893 insn->instructionID = instructionIDWithNewOpcode;
894 insn->spec = specWithNewOpcode;
895
896 return 0;
897 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000898
899 insn->instructionID = instructionID;
900 insn->spec = specifierForUID(insn->instructionID);
901
902 return 0;
903}
904
905/*
906 * readSIB - Consumes the SIB byte to determine addressing information for an
907 * instruction.
908 *
909 * @param insn - The instruction whose SIB byte is to be read.
910 * @return - 0 if the SIB byte was successfully read; nonzero otherwise.
911 */
912static int readSIB(struct InternalInstruction* insn) {
Daniel Dunbarbaf2e352009-12-22 01:41:37 +0000913 SIBIndex sibIndexBase = 0;
914 SIBBase sibBaseBase = 0;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000915 uint8_t index, base;
916
Nuno Lopes392bbd92009-12-19 12:07:00 +0000917 dbgprintf(insn, "readSIB()");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000918
919 if (insn->consumedSIB)
920 return 0;
921
922 insn->consumedSIB = TRUE;
923
924 switch (insn->addressSize) {
925 case 2:
Nuno Lopes392bbd92009-12-19 12:07:00 +0000926 dbgprintf(insn, "SIB-based addressing doesn't work in 16-bit mode");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000927 return -1;
928 break;
929 case 4:
930 sibIndexBase = SIB_INDEX_EAX;
931 sibBaseBase = SIB_BASE_EAX;
932 break;
933 case 8:
934 sibIndexBase = SIB_INDEX_RAX;
935 sibBaseBase = SIB_BASE_RAX;
936 break;
937 }
938
939 if (consumeByte(insn, &insn->sib))
940 return -1;
941
942 index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
943
944 switch (index) {
945 case 0x4:
946 insn->sibIndex = SIB_INDEX_NONE;
947 break;
948 default:
Benjamin Kramer9e9bb082011-02-27 18:13:53 +0000949 insn->sibIndex = (SIBIndex)(sibIndexBase + index);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000950 if (insn->sibIndex == SIB_INDEX_sib ||
951 insn->sibIndex == SIB_INDEX_sib64)
952 insn->sibIndex = SIB_INDEX_NONE;
953 break;
954 }
955
956 switch (scaleFromSIB(insn->sib)) {
957 case 0:
958 insn->sibScale = 1;
959 break;
960 case 1:
961 insn->sibScale = 2;
962 break;
963 case 2:
964 insn->sibScale = 4;
965 break;
966 case 3:
967 insn->sibScale = 8;
968 break;
969 }
970
971 base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
972
973 switch (base) {
974 case 0x5:
975 switch (modFromModRM(insn->modRM)) {
976 case 0x0:
977 insn->eaDisplacement = EA_DISP_32;
978 insn->sibBase = SIB_BASE_NONE;
979 break;
980 case 0x1:
981 insn->eaDisplacement = EA_DISP_8;
982 insn->sibBase = (insn->addressSize == 4 ?
983 SIB_BASE_EBP : SIB_BASE_RBP);
984 break;
985 case 0x2:
986 insn->eaDisplacement = EA_DISP_32;
987 insn->sibBase = (insn->addressSize == 4 ?
988 SIB_BASE_EBP : SIB_BASE_RBP);
989 break;
990 case 0x3:
Sean Callanana144c3f2010-04-02 21:23:51 +0000991 debug("Cannot have Mod = 0b11 and a SIB byte");
992 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000993 }
994 break;
995 default:
Benjamin Kramer9e9bb082011-02-27 18:13:53 +0000996 insn->sibBase = (SIBBase)(sibBaseBase + base);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000997 break;
998 }
999
1000 return 0;
1001}
1002
1003/*
1004 * readDisplacement - Consumes the displacement of an instruction.
1005 *
1006 * @param insn - The instruction whose displacement is to be read.
1007 * @return - 0 if the displacement byte was successfully read; nonzero
1008 * otherwise.
1009 */
1010static int readDisplacement(struct InternalInstruction* insn) {
1011 int8_t d8;
1012 int16_t d16;
1013 int32_t d32;
1014
Nuno Lopes392bbd92009-12-19 12:07:00 +00001015 dbgprintf(insn, "readDisplacement()");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001016
1017 if (insn->consumedDisplacement)
1018 return 0;
1019
1020 insn->consumedDisplacement = TRUE;
1021
1022 switch (insn->eaDisplacement) {
1023 case EA_DISP_NONE:
1024 insn->consumedDisplacement = FALSE;
1025 break;
1026 case EA_DISP_8:
1027 if (consumeInt8(insn, &d8))
1028 return -1;
1029 insn->displacement = d8;
1030 break;
1031 case EA_DISP_16:
1032 if (consumeInt16(insn, &d16))
1033 return -1;
1034 insn->displacement = d16;
1035 break;
1036 case EA_DISP_32:
1037 if (consumeInt32(insn, &d32))
1038 return -1;
1039 insn->displacement = d32;
1040 break;
1041 }
1042
1043 insn->consumedDisplacement = TRUE;
1044 return 0;
1045}
1046
1047/*
1048 * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and
1049 * displacement) for an instruction and interprets it.
1050 *
1051 * @param insn - The instruction whose addressing information is to be read.
1052 * @return - 0 if the information was successfully read; nonzero otherwise.
1053 */
1054static int readModRM(struct InternalInstruction* insn) {
1055 uint8_t mod, rm, reg;
1056
Nuno Lopes392bbd92009-12-19 12:07:00 +00001057 dbgprintf(insn, "readModRM()");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001058
1059 if (insn->consumedModRM)
1060 return 0;
1061
Rafael Espindola2f867a62011-01-06 16:48:42 +00001062 if (consumeByte(insn, &insn->modRM))
1063 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001064 insn->consumedModRM = TRUE;
1065
1066 mod = modFromModRM(insn->modRM);
1067 rm = rmFromModRM(insn->modRM);
1068 reg = regFromModRM(insn->modRM);
1069
1070 /*
1071 * This goes by insn->registerSize to pick the correct register, which messes
1072 * up if we're using (say) XMM or 8-bit register operands. That gets fixed in
1073 * fixupReg().
1074 */
1075 switch (insn->registerSize) {
1076 case 2:
Sean Callanan06b766d2009-12-22 02:07:42 +00001077 insn->regBase = MODRM_REG_AX;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001078 insn->eaRegBase = EA_REG_AX;
1079 break;
1080 case 4:
Sean Callanan06b766d2009-12-22 02:07:42 +00001081 insn->regBase = MODRM_REG_EAX;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001082 insn->eaRegBase = EA_REG_EAX;
1083 break;
1084 case 8:
Sean Callanan06b766d2009-12-22 02:07:42 +00001085 insn->regBase = MODRM_REG_RAX;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001086 insn->eaRegBase = EA_REG_RAX;
1087 break;
1088 }
1089
1090 reg |= rFromREX(insn->rexPrefix) << 3;
1091 rm |= bFromREX(insn->rexPrefix) << 3;
1092
1093 insn->reg = (Reg)(insn->regBase + reg);
1094
1095 switch (insn->addressSize) {
1096 case 2:
1097 insn->eaBaseBase = EA_BASE_BX_SI;
1098
1099 switch (mod) {
1100 case 0x0:
1101 if (rm == 0x6) {
1102 insn->eaBase = EA_BASE_NONE;
1103 insn->eaDisplacement = EA_DISP_16;
Sean Callanana144c3f2010-04-02 21:23:51 +00001104 if (readDisplacement(insn))
Sean Callanan8ed9f512009-12-19 02:59:52 +00001105 return -1;
1106 } else {
1107 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1108 insn->eaDisplacement = EA_DISP_NONE;
1109 }
1110 break;
1111 case 0x1:
1112 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1113 insn->eaDisplacement = EA_DISP_8;
Sean Callanana144c3f2010-04-02 21:23:51 +00001114 if (readDisplacement(insn))
Sean Callanan8ed9f512009-12-19 02:59:52 +00001115 return -1;
1116 break;
1117 case 0x2:
1118 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1119 insn->eaDisplacement = EA_DISP_16;
Sean Callanana144c3f2010-04-02 21:23:51 +00001120 if (readDisplacement(insn))
Sean Callanan8ed9f512009-12-19 02:59:52 +00001121 return -1;
1122 break;
1123 case 0x3:
1124 insn->eaBase = (EABase)(insn->eaRegBase + rm);
Sean Callanana144c3f2010-04-02 21:23:51 +00001125 if (readDisplacement(insn))
Sean Callanan8ed9f512009-12-19 02:59:52 +00001126 return -1;
1127 break;
1128 }
1129 break;
1130 case 4:
1131 case 8:
1132 insn->eaBaseBase = (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
1133
1134 switch (mod) {
1135 case 0x0:
1136 insn->eaDisplacement = EA_DISP_NONE; /* readSIB may override this */
1137 switch (rm) {
1138 case 0x4:
1139 case 0xc: /* in case REXW.b is set */
1140 insn->eaBase = (insn->addressSize == 4 ?
1141 EA_BASE_sib : EA_BASE_sib64);
1142 readSIB(insn);
Sean Callanana144c3f2010-04-02 21:23:51 +00001143 if (readDisplacement(insn))
Sean Callanan8ed9f512009-12-19 02:59:52 +00001144 return -1;
1145 break;
1146 case 0x5:
1147 insn->eaBase = EA_BASE_NONE;
1148 insn->eaDisplacement = EA_DISP_32;
Sean Callanana144c3f2010-04-02 21:23:51 +00001149 if (readDisplacement(insn))
Sean Callanan8ed9f512009-12-19 02:59:52 +00001150 return -1;
1151 break;
1152 default:
1153 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1154 break;
1155 }
1156 break;
1157 case 0x1:
1158 case 0x2:
1159 insn->eaDisplacement = (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
1160 switch (rm) {
1161 case 0x4:
1162 case 0xc: /* in case REXW.b is set */
1163 insn->eaBase = EA_BASE_sib;
1164 readSIB(insn);
Sean Callanana144c3f2010-04-02 21:23:51 +00001165 if (readDisplacement(insn))
Sean Callanan8ed9f512009-12-19 02:59:52 +00001166 return -1;
1167 break;
1168 default:
1169 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
Sean Callanana144c3f2010-04-02 21:23:51 +00001170 if (readDisplacement(insn))
Sean Callanan8ed9f512009-12-19 02:59:52 +00001171 return -1;
1172 break;
1173 }
1174 break;
1175 case 0x3:
1176 insn->eaDisplacement = EA_DISP_NONE;
1177 insn->eaBase = (EABase)(insn->eaRegBase + rm);
1178 break;
1179 }
1180 break;
1181 } /* switch (insn->addressSize) */
1182
1183 return 0;
1184}
1185
1186#define GENERIC_FIXUP_FUNC(name, base, prefix) \
1187 static uint8_t name(struct InternalInstruction *insn, \
1188 OperandType type, \
1189 uint8_t index, \
1190 uint8_t *valid) { \
1191 *valid = 1; \
1192 switch (type) { \
1193 default: \
Sean Callanana144c3f2010-04-02 21:23:51 +00001194 debug("Unhandled register type"); \
1195 *valid = 0; \
1196 return 0; \
Sean Callanan8ed9f512009-12-19 02:59:52 +00001197 case TYPE_Rv: \
1198 return base + index; \
1199 case TYPE_R8: \
Sean Callanana144c3f2010-04-02 21:23:51 +00001200 if (insn->rexPrefix && \
Sean Callanan8ed9f512009-12-19 02:59:52 +00001201 index >= 4 && index <= 7) { \
1202 return prefix##_SPL + (index - 4); \
1203 } else { \
1204 return prefix##_AL + index; \
1205 } \
1206 case TYPE_R16: \
1207 return prefix##_AX + index; \
1208 case TYPE_R32: \
1209 return prefix##_EAX + index; \
1210 case TYPE_R64: \
1211 return prefix##_RAX + index; \
Sean Callanana21e2ea2011-03-15 01:23:15 +00001212 case TYPE_XMM256: \
1213 return prefix##_YMM0 + index; \
Sean Callanan8ed9f512009-12-19 02:59:52 +00001214 case TYPE_XMM128: \
1215 case TYPE_XMM64: \
1216 case TYPE_XMM32: \
1217 case TYPE_XMM: \
1218 return prefix##_XMM0 + index; \
1219 case TYPE_MM64: \
1220 case TYPE_MM32: \
1221 case TYPE_MM: \
Sean Callanana144c3f2010-04-02 21:23:51 +00001222 if (index > 7) \
Sean Callanan8ed9f512009-12-19 02:59:52 +00001223 *valid = 0; \
1224 return prefix##_MM0 + index; \
1225 case TYPE_SEGMENTREG: \
Sean Callanana144c3f2010-04-02 21:23:51 +00001226 if (index > 5) \
Sean Callanan8ed9f512009-12-19 02:59:52 +00001227 *valid = 0; \
1228 return prefix##_ES + index; \
1229 case TYPE_DEBUGREG: \
Sean Callanana144c3f2010-04-02 21:23:51 +00001230 if (index > 7) \
Sean Callanan8ed9f512009-12-19 02:59:52 +00001231 *valid = 0; \
1232 return prefix##_DR0 + index; \
Sean Callanan1a8b7892010-05-06 20:59:00 +00001233 case TYPE_CONTROLREG: \
Sean Callanana144c3f2010-04-02 21:23:51 +00001234 if (index > 8) \
Sean Callanan8ed9f512009-12-19 02:59:52 +00001235 *valid = 0; \
Sean Callanan1a8b7892010-05-06 20:59:00 +00001236 return prefix##_CR0 + index; \
Sean Callanan8ed9f512009-12-19 02:59:52 +00001237 } \
1238 }
1239
1240/*
1241 * fixup*Value - Consults an operand type to determine the meaning of the
1242 * reg or R/M field. If the operand is an XMM operand, for example, an
1243 * operand would be XMM0 instead of AX, which readModRM() would otherwise
1244 * misinterpret it as.
1245 *
1246 * @param insn - The instruction containing the operand.
1247 * @param type - The operand type.
1248 * @param index - The existing value of the field as reported by readModRM().
1249 * @param valid - The address of a uint8_t. The target is set to 1 if the
1250 * field is valid for the register class; 0 if not.
Sean Callanana144c3f2010-04-02 21:23:51 +00001251 * @return - The proper value.
Sean Callanan8ed9f512009-12-19 02:59:52 +00001252 */
Sean Callanan06b766d2009-12-22 02:07:42 +00001253GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase, MODRM_REG)
Sean Callanan8ed9f512009-12-19 02:59:52 +00001254GENERIC_FIXUP_FUNC(fixupRMValue, insn->eaRegBase, EA_REG)
1255
1256/*
1257 * fixupReg - Consults an operand specifier to determine which of the
1258 * fixup*Value functions to use in correcting readModRM()'ss interpretation.
1259 *
1260 * @param insn - See fixup*Value().
1261 * @param op - The operand specifier.
1262 * @return - 0 if fixup was successful; -1 if the register returned was
1263 * invalid for its class.
1264 */
1265static int fixupReg(struct InternalInstruction *insn,
Benjamin Kramer4d1dca92010-10-23 09:10:44 +00001266 const struct OperandSpecifier *op) {
Sean Callanan8ed9f512009-12-19 02:59:52 +00001267 uint8_t valid;
1268
Nuno Lopes392bbd92009-12-19 12:07:00 +00001269 dbgprintf(insn, "fixupReg()");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001270
1271 switch ((OperandEncoding)op->encoding) {
1272 default:
Sean Callanana144c3f2010-04-02 21:23:51 +00001273 debug("Expected a REG or R/M encoding in fixupReg");
1274 return -1;
Sean Callanana21e2ea2011-03-15 01:23:15 +00001275 case ENCODING_VVVV:
1276 insn->vvvv = (Reg)fixupRegValue(insn,
1277 (OperandType)op->type,
1278 insn->vvvv,
1279 &valid);
1280 if (!valid)
1281 return -1;
1282 break;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001283 case ENCODING_REG:
1284 insn->reg = (Reg)fixupRegValue(insn,
1285 (OperandType)op->type,
1286 insn->reg - insn->regBase,
1287 &valid);
1288 if (!valid)
1289 return -1;
1290 break;
1291 case ENCODING_RM:
1292 if (insn->eaBase >= insn->eaRegBase) {
1293 insn->eaBase = (EABase)fixupRMValue(insn,
1294 (OperandType)op->type,
1295 insn->eaBase - insn->eaRegBase,
1296 &valid);
1297 if (!valid)
1298 return -1;
1299 }
1300 break;
1301 }
1302
1303 return 0;
1304}
1305
1306/*
1307 * readOpcodeModifier - Reads an operand from the opcode field of an
1308 * instruction. Handles AddRegFrm instructions.
1309 *
1310 * @param insn - The instruction whose opcode field is to be read.
1311 * @param inModRM - Indicates that the opcode field is to be read from the
1312 * ModR/M extension; useful for escape opcodes
Sean Callanana144c3f2010-04-02 21:23:51 +00001313 * @return - 0 on success; nonzero otherwise.
Sean Callanan8ed9f512009-12-19 02:59:52 +00001314 */
Sean Callanana144c3f2010-04-02 21:23:51 +00001315static int readOpcodeModifier(struct InternalInstruction* insn) {
Nuno Lopes392bbd92009-12-19 12:07:00 +00001316 dbgprintf(insn, "readOpcodeModifier()");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001317
1318 if (insn->consumedOpcodeModifier)
Sean Callanana144c3f2010-04-02 21:23:51 +00001319 return 0;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001320
1321 insn->consumedOpcodeModifier = TRUE;
1322
Sean Callanana144c3f2010-04-02 21:23:51 +00001323 switch (insn->spec->modifierType) {
Sean Callanan8ed9f512009-12-19 02:59:52 +00001324 default:
Sean Callanana144c3f2010-04-02 21:23:51 +00001325 debug("Unknown modifier type.");
1326 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001327 case MODIFIER_NONE:
Sean Callanana144c3f2010-04-02 21:23:51 +00001328 debug("No modifier but an operand expects one.");
1329 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001330 case MODIFIER_OPCODE:
1331 insn->opcodeModifier = insn->opcode - insn->spec->modifierBase;
Sean Callanana144c3f2010-04-02 21:23:51 +00001332 return 0;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001333 case MODIFIER_MODRM:
1334 insn->opcodeModifier = insn->modRM - insn->spec->modifierBase;
Sean Callanana144c3f2010-04-02 21:23:51 +00001335 return 0;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001336 }
1337}
1338
1339/*
1340 * readOpcodeRegister - Reads an operand from the opcode field of an
1341 * instruction and interprets it appropriately given the operand width.
1342 * Handles AddRegFrm instructions.
1343 *
1344 * @param insn - See readOpcodeModifier().
1345 * @param size - The width (in bytes) of the register being specified.
1346 * 1 means AL and friends, 2 means AX, 4 means EAX, and 8 means
1347 * RAX.
Sean Callanana144c3f2010-04-02 21:23:51 +00001348 * @return - 0 on success; nonzero otherwise.
Sean Callanan8ed9f512009-12-19 02:59:52 +00001349 */
Sean Callanana144c3f2010-04-02 21:23:51 +00001350static int readOpcodeRegister(struct InternalInstruction* insn, uint8_t size) {
Nuno Lopes392bbd92009-12-19 12:07:00 +00001351 dbgprintf(insn, "readOpcodeRegister()");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001352
Sean Callanana144c3f2010-04-02 21:23:51 +00001353 if (readOpcodeModifier(insn))
1354 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001355
1356 if (size == 0)
1357 size = insn->registerSize;
1358
1359 switch (size) {
1360 case 1:
Sean Callanan06b766d2009-12-22 02:07:42 +00001361 insn->opcodeRegister = (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3)
1362 | insn->opcodeModifier));
Sean Callanana144c3f2010-04-02 21:23:51 +00001363 if (insn->rexPrefix &&
1364 insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1365 insn->opcodeRegister < MODRM_REG_AL + 0x8) {
Sean Callanan06b766d2009-12-22 02:07:42 +00001366 insn->opcodeRegister = (Reg)(MODRM_REG_SPL
1367 + (insn->opcodeRegister - MODRM_REG_AL - 4));
Sean Callanan8ed9f512009-12-19 02:59:52 +00001368 }
1369
1370 break;
1371 case 2:
Sean Callanan06b766d2009-12-22 02:07:42 +00001372 insn->opcodeRegister = (Reg)(MODRM_REG_AX
1373 + ((bFromREX(insn->rexPrefix) << 3)
1374 | insn->opcodeModifier));
Sean Callanan8ed9f512009-12-19 02:59:52 +00001375 break;
1376 case 4:
Sean Callanana144c3f2010-04-02 21:23:51 +00001377 insn->opcodeRegister = (Reg)(MODRM_REG_EAX
Sean Callanan06b766d2009-12-22 02:07:42 +00001378 + ((bFromREX(insn->rexPrefix) << 3)
1379 | insn->opcodeModifier));
Sean Callanan8ed9f512009-12-19 02:59:52 +00001380 break;
1381 case 8:
Sean Callanan06b766d2009-12-22 02:07:42 +00001382 insn->opcodeRegister = (Reg)(MODRM_REG_RAX
1383 + ((bFromREX(insn->rexPrefix) << 3)
1384 | insn->opcodeModifier));
Sean Callanan8ed9f512009-12-19 02:59:52 +00001385 break;
1386 }
Sean Callanana144c3f2010-04-02 21:23:51 +00001387
1388 return 0;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001389}
1390
1391/*
1392 * readImmediate - Consumes an immediate operand from an instruction, given the
1393 * desired operand size.
1394 *
1395 * @param insn - The instruction whose operand is to be read.
1396 * @param size - The width (in bytes) of the operand.
1397 * @return - 0 if the immediate was successfully consumed; nonzero
1398 * otherwise.
1399 */
1400static int readImmediate(struct InternalInstruction* insn, uint8_t size) {
1401 uint8_t imm8;
1402 uint16_t imm16;
1403 uint32_t imm32;
1404 uint64_t imm64;
1405
Nuno Lopes392bbd92009-12-19 12:07:00 +00001406 dbgprintf(insn, "readImmediate()");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001407
Sean Callanana144c3f2010-04-02 21:23:51 +00001408 if (insn->numImmediatesConsumed == 2) {
1409 debug("Already consumed two immediates");
1410 return -1;
1411 }
Sean Callanan8ed9f512009-12-19 02:59:52 +00001412
1413 if (size == 0)
1414 size = insn->immediateSize;
1415 else
1416 insn->immediateSize = size;
1417
1418 switch (size) {
1419 case 1:
1420 if (consumeByte(insn, &imm8))
1421 return -1;
1422 insn->immediates[insn->numImmediatesConsumed] = imm8;
1423 break;
1424 case 2:
1425 if (consumeUInt16(insn, &imm16))
1426 return -1;
1427 insn->immediates[insn->numImmediatesConsumed] = imm16;
1428 break;
1429 case 4:
1430 if (consumeUInt32(insn, &imm32))
1431 return -1;
1432 insn->immediates[insn->numImmediatesConsumed] = imm32;
1433 break;
1434 case 8:
1435 if (consumeUInt64(insn, &imm64))
1436 return -1;
1437 insn->immediates[insn->numImmediatesConsumed] = imm64;
1438 break;
1439 }
1440
1441 insn->numImmediatesConsumed++;
1442
1443 return 0;
1444}
1445
1446/*
Craig Topper4bbeb182011-09-13 07:37:44 +00001447 * readVVVV - Consumes vvvv from an instruction if it has a VEX prefix.
Sean Callanana21e2ea2011-03-15 01:23:15 +00001448 *
1449 * @param insn - The instruction whose operand is to be read.
Craig Topper4bbeb182011-09-13 07:37:44 +00001450 * @return - 0 if the vvvv was successfully consumed; nonzero
Sean Callanana21e2ea2011-03-15 01:23:15 +00001451 * otherwise.
1452 */
1453static int readVVVV(struct InternalInstruction* insn) {
1454 dbgprintf(insn, "readVVVV()");
1455
1456 if (insn->vexSize == 3)
1457 insn->vvvv = vvvvFromVEX3of3(insn->vexPrefix[2]);
1458 else if (insn->vexSize == 2)
1459 insn->vvvv = vvvvFromVEX2of2(insn->vexPrefix[1]);
1460 else
1461 return -1;
1462
Craig Topper04c5be92011-10-03 08:14:29 +00001463 if (insn->mode != MODE_64BIT)
1464 insn->vvvv &= 0x7;
1465
Sean Callanana21e2ea2011-03-15 01:23:15 +00001466 return 0;
1467}
1468
1469/*
Sean Callanan8ed9f512009-12-19 02:59:52 +00001470 * readOperands - Consults the specifier for an instruction and consumes all
1471 * operands for that instruction, interpreting them as it goes.
1472 *
1473 * @param insn - The instruction whose operands are to be read and interpreted.
1474 * @return - 0 if all operands could be read; nonzero otherwise.
1475 */
1476static int readOperands(struct InternalInstruction* insn) {
1477 int index;
Craig Topper4bbeb182011-09-13 07:37:44 +00001478 int hasVVVV, needVVVV;
Craig Topper06f554d2011-12-30 06:23:39 +00001479 int sawRegImm = 0;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001480
Nuno Lopes392bbd92009-12-19 12:07:00 +00001481 dbgprintf(insn, "readOperands()");
Craig Topper4bbeb182011-09-13 07:37:44 +00001482
1483 /* If non-zero vvvv specified, need to make sure one of the operands
1484 uses it. */
1485 hasVVVV = !readVVVV(insn);
1486 needVVVV = hasVVVV && (insn->vvvv != 0);
Sean Callanan8ed9f512009-12-19 02:59:52 +00001487
1488 for (index = 0; index < X86_MAX_OPERANDS; ++index) {
1489 switch (insn->spec->operands[index].encoding) {
1490 case ENCODING_NONE:
1491 break;
1492 case ENCODING_REG:
1493 case ENCODING_RM:
1494 if (readModRM(insn))
1495 return -1;
1496 if (fixupReg(insn, &insn->spec->operands[index]))
1497 return -1;
1498 break;
1499 case ENCODING_CB:
1500 case ENCODING_CW:
1501 case ENCODING_CD:
1502 case ENCODING_CP:
1503 case ENCODING_CO:
1504 case ENCODING_CT:
Nuno Lopes392bbd92009-12-19 12:07:00 +00001505 dbgprintf(insn, "We currently don't hande code-offset encodings");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001506 return -1;
1507 case ENCODING_IB:
Craig Topper06f554d2011-12-30 06:23:39 +00001508 if (sawRegImm) {
Benjamin Kramera5f89422012-01-04 22:06:45 +00001509 /* Saw a register immediate so don't read again and instead split the
1510 previous immediate. FIXME: This is a hack. */
Benjamin Kramer89435742012-01-01 17:55:36 +00001511 insn->immediates[insn->numImmediatesConsumed] =
1512 insn->immediates[insn->numImmediatesConsumed - 1] & 0xf;
1513 ++insn->numImmediatesConsumed;
Craig Topper06f554d2011-12-30 06:23:39 +00001514 break;
1515 }
Sean Callanan8ed9f512009-12-19 02:59:52 +00001516 if (readImmediate(insn, 1))
1517 return -1;
Sean Callanan5edca812010-04-07 21:42:19 +00001518 if (insn->spec->operands[index].type == TYPE_IMM3 &&
1519 insn->immediates[insn->numImmediatesConsumed - 1] > 7)
1520 return -1;
Craig Topper06f554d2011-12-30 06:23:39 +00001521 if (insn->spec->operands[index].type == TYPE_XMM128 ||
1522 insn->spec->operands[index].type == TYPE_XMM256)
1523 sawRegImm = 1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001524 break;
1525 case ENCODING_IW:
1526 if (readImmediate(insn, 2))
1527 return -1;
1528 break;
1529 case ENCODING_ID:
1530 if (readImmediate(insn, 4))
1531 return -1;
1532 break;
1533 case ENCODING_IO:
1534 if (readImmediate(insn, 8))
1535 return -1;
1536 break;
1537 case ENCODING_Iv:
Sean Callanana144c3f2010-04-02 21:23:51 +00001538 if (readImmediate(insn, insn->immediateSize))
1539 return -1;
Chris Lattneraef1fea2010-04-16 21:15:15 +00001540 break;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001541 case ENCODING_Ia:
Sean Callanana144c3f2010-04-02 21:23:51 +00001542 if (readImmediate(insn, insn->addressSize))
1543 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001544 break;
1545 case ENCODING_RB:
Sean Callanana144c3f2010-04-02 21:23:51 +00001546 if (readOpcodeRegister(insn, 1))
1547 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001548 break;
1549 case ENCODING_RW:
Sean Callanana144c3f2010-04-02 21:23:51 +00001550 if (readOpcodeRegister(insn, 2))
1551 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001552 break;
1553 case ENCODING_RD:
Sean Callanana144c3f2010-04-02 21:23:51 +00001554 if (readOpcodeRegister(insn, 4))
1555 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001556 break;
1557 case ENCODING_RO:
Sean Callanana144c3f2010-04-02 21:23:51 +00001558 if (readOpcodeRegister(insn, 8))
1559 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001560 break;
1561 case ENCODING_Rv:
Sean Callanana144c3f2010-04-02 21:23:51 +00001562 if (readOpcodeRegister(insn, 0))
1563 return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001564 break;
1565 case ENCODING_I:
Sean Callanana144c3f2010-04-02 21:23:51 +00001566 if (readOpcodeModifier(insn))
1567 return -1;
Sean Callanana21e2ea2011-03-15 01:23:15 +00001568 break;
1569 case ENCODING_VVVV:
Craig Topper4bbeb182011-09-13 07:37:44 +00001570 needVVVV = 0; /* Mark that we have found a VVVV operand. */
1571 if (!hasVVVV)
Sean Callanana21e2ea2011-03-15 01:23:15 +00001572 return -1;
1573 if (fixupReg(insn, &insn->spec->operands[index]))
1574 return -1;
1575 break;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001576 case ENCODING_DUP:
1577 break;
1578 default:
Nuno Lopes392bbd92009-12-19 12:07:00 +00001579 dbgprintf(insn, "Encountered an operand with an unknown encoding.");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001580 return -1;
1581 }
1582 }
Craig Topper4bbeb182011-09-13 07:37:44 +00001583
1584 /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */
1585 if (needVVVV) return -1;
Sean Callanan8ed9f512009-12-19 02:59:52 +00001586
1587 return 0;
1588}
1589
1590/*
1591 * decodeInstruction - Reads and interprets a full instruction provided by the
1592 * user.
1593 *
1594 * @param insn - A pointer to the instruction to be populated. Must be
1595 * pre-allocated.
1596 * @param reader - The function to be used to read the instruction's bytes.
1597 * @param readerArg - A generic argument to be passed to the reader to store
1598 * any internal state.
1599 * @param logger - If non-NULL, the function to be used to write log messages
1600 * and warnings.
1601 * @param loggerArg - A generic argument to be passed to the logger to store
1602 * any internal state.
1603 * @param startLoc - The address (in the reader's address space) of the first
1604 * byte in the instruction.
1605 * @param mode - The mode (real mode, IA-32e, or IA-32e in 64-bit mode) to
1606 * decode the instruction in.
1607 * @return - 0 if the instruction's memory could be read; nonzero if
1608 * not.
1609 */
1610int decodeInstruction(struct InternalInstruction* insn,
1611 byteReader_t reader,
1612 void* readerArg,
1613 dlog_t logger,
1614 void* loggerArg,
Benjamin Kramer953362c2012-02-11 14:50:54 +00001615 void* miiArg,
Sean Callanan8ed9f512009-12-19 02:59:52 +00001616 uint64_t startLoc,
1617 DisassemblerMode mode) {
Daniel Dunbar71f842d2009-12-19 03:31:50 +00001618 memset(insn, 0, sizeof(struct InternalInstruction));
Sean Callanan8ed9f512009-12-19 02:59:52 +00001619
1620 insn->reader = reader;
1621 insn->readerArg = readerArg;
1622 insn->dlog = logger;
1623 insn->dlogArg = loggerArg;
1624 insn->startLocation = startLoc;
1625 insn->readerCursor = startLoc;
1626 insn->mode = mode;
1627 insn->numImmediatesConsumed = 0;
1628
1629 if (readPrefixes(insn) ||
1630 readOpcode(insn) ||
Benjamin Kramer953362c2012-02-11 14:50:54 +00001631 getID(insn, miiArg) ||
Sean Callanan8ed9f512009-12-19 02:59:52 +00001632 insn->instructionID == 0 ||
1633 readOperands(insn))
1634 return -1;
1635
1636 insn->length = insn->readerCursor - insn->startLocation;
1637
Benjamin Kramer7c97ed72010-03-18 12:18:36 +00001638 dbgprintf(insn, "Read from 0x%llx to 0x%llx: length %zu",
1639 startLoc, insn->readerCursor, insn->length);
Sean Callanan8ed9f512009-12-19 02:59:52 +00001640
1641 if (insn->length > 15)
Nuno Lopes392bbd92009-12-19 12:07:00 +00001642 dbgprintf(insn, "Instruction exceeds 15-byte limit");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001643
1644 return 0;
1645}