blob: a2b5e63fd129a059cbe702de16298cd4f6eb2739 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001/*===-- X86DisassemblerDecoder.c - Disassembler decoder ------------*- C -*-===*
Sean Callanan04cc3072009-12-19 02:59:52 +00002 *
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 Callanan04cc3072009-12-19 02:59:52 +000016#include <stdarg.h> /* for va_*() */
17#include <stdio.h> /* for vsnprintf() */
18#include <stdlib.h> /* for exit() */
Daniel Dunbarc745a622009-12-19 03:31:50 +000019#include <string.h> /* for memset() */
Sean Callanan04cc3072009-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 Callanan010b3732010-04-02 21:23:51 +000028#ifndef NDEBUG
29#define debug(s) do { x86DisassemblerDebug(__FILE__, __LINE__, s); } while (0)
30#else
31#define debug(s) do { } while (0)
32#endif
33
Sean Callanan04cc3072009-12-19 02:59:52 +000034
35/*
36 * contextForAttrs - Client for the instruction context table. Takes a set of
37 * attributes and returns the appropriate decode context.
38 *
39 * @param attrMask - Attributes, from the enumeration attributeBits.
40 * @return - The InstructionContext to use when looking up an
41 * an instruction with these attributes.
42 */
Elena Demikhovsky371e3632013-12-25 11:40:51 +000043static InstructionContext contextForAttrs(uint16_t attrMask) {
Sean Callanan04cc3072009-12-19 02:59:52 +000044 return CONTEXTS_SYM[attrMask];
45}
46
47/*
48 * modRMRequired - Reads the appropriate instruction table to determine whether
49 * the ModR/M byte is required to decode a particular instruction.
50 *
51 * @param type - The opcode type (i.e., how many bytes it has).
52 * @param insnContext - The context for the instruction, as returned by
53 * contextForAttrs.
54 * @param opcode - The last byte of the instruction's opcode, not counting
55 * ModR/M extensions and escapes.
56 * @return - TRUE if the ModR/M byte is required, FALSE otherwise.
57 */
Sean Callanan588785c2009-12-22 22:51:40 +000058static int modRMRequired(OpcodeType type,
Craig Topper21c33652011-10-02 16:56:09 +000059 InstructionContext insnContext,
Elena Demikhovsky371e3632013-12-25 11:40:51 +000060 uint16_t opcode) {
Daniel Dunbar8b532de2009-12-22 01:41:37 +000061 const struct ContextDecision* decision = 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +000062
Sean Callanan04cc3072009-12-19 02:59:52 +000063 switch (type) {
64 case ONEBYTE:
65 decision = &ONEBYTE_SYM;
66 break;
67 case TWOBYTE:
68 decision = &TWOBYTE_SYM;
69 break;
70 case THREEBYTE_38:
71 decision = &THREEBYTE38_SYM;
72 break;
73 case THREEBYTE_3A:
74 decision = &THREEBYTE3A_SYM;
75 break;
Joerg Sonnenbergerfc4789d2011-04-04 16:58:13 +000076 case THREEBYTE_A6:
77 decision = &THREEBYTEA6_SYM;
78 break;
79 case THREEBYTE_A7:
80 decision = &THREEBYTEA7_SYM;
81 break;
Craig Topper9e3e38a2013-10-03 05:17:48 +000082 case XOP8_MAP:
83 decision = &XOP8_MAP_SYM;
84 break;
85 case XOP9_MAP:
86 decision = &XOP9_MAP_SYM;
87 break;
88 case XOPA_MAP:
89 decision = &XOPA_MAP_SYM;
90 break;
Sean Callanan04cc3072009-12-19 02:59:52 +000091 }
Ahmed Charles636a3d62012-02-19 11:37:01 +000092
Sean Callanan04cc3072009-12-19 02:59:52 +000093 return decision->opcodeDecisions[insnContext].modRMDecisions[opcode].
94 modrm_type != MODRM_ONEENTRY;
Sean Callanan04cc3072009-12-19 02:59:52 +000095}
96
97/*
98 * decode - Reads the appropriate instruction table to obtain the unique ID of
99 * an instruction.
100 *
101 * @param type - See modRMRequired().
102 * @param insnContext - See modRMRequired().
103 * @param opcode - See modRMRequired().
104 * @param modRM - The ModR/M byte if required, or any value if not.
Sean Callanan010b3732010-04-02 21:23:51 +0000105 * @return - The UID of the instruction, or 0 on failure.
Sean Callanan04cc3072009-12-19 02:59:52 +0000106 */
Sean Callanan588785c2009-12-22 22:51:40 +0000107static InstrUID decode(OpcodeType type,
Sean Callanan010b3732010-04-02 21:23:51 +0000108 InstructionContext insnContext,
109 uint8_t opcode,
110 uint8_t modRM) {
Duncan Sandsae22c602012-02-05 14:20:11 +0000111 const struct ModRMDecision* dec = 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000112
Sean Callanan04cc3072009-12-19 02:59:52 +0000113 switch (type) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000114 case ONEBYTE:
115 dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
116 break;
117 case TWOBYTE:
118 dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
119 break;
120 case THREEBYTE_38:
121 dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
122 break;
123 case THREEBYTE_3A:
124 dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
125 break;
Joerg Sonnenbergerfc4789d2011-04-04 16:58:13 +0000126 case THREEBYTE_A6:
127 dec = &THREEBYTEA6_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
128 break;
129 case THREEBYTE_A7:
130 dec = &THREEBYTEA7_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
131 break;
Craig Topper9e3e38a2013-10-03 05:17:48 +0000132 case XOP8_MAP:
133 dec = &XOP8_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
134 break;
135 case XOP9_MAP:
136 dec = &XOP9_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
137 break;
138 case XOPA_MAP:
139 dec = &XOPA_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
140 break;
Sean Callanan04cc3072009-12-19 02:59:52 +0000141 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000142
Sean Callanan04cc3072009-12-19 02:59:52 +0000143 switch (dec->modrm_type) {
144 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000145 debug("Corrupt table! Unknown modrm_type");
146 return 0;
Sean Callanan04cc3072009-12-19 02:59:52 +0000147 case MODRM_ONEENTRY:
Craig Topper487e7442012-02-09 07:45:30 +0000148 return modRMTable[dec->instructionIDs];
Sean Callanan04cc3072009-12-19 02:59:52 +0000149 case MODRM_SPLITRM:
150 if (modFromModRM(modRM) == 0x3)
Craig Topper487e7442012-02-09 07:45:30 +0000151 return modRMTable[dec->instructionIDs+1];
152 return modRMTable[dec->instructionIDs];
Craig Toppera0cd9702012-02-09 08:58:07 +0000153 case MODRM_SPLITREG:
154 if (modFromModRM(modRM) == 0x3)
155 return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)+8];
156 return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)];
Craig Topper963305b2012-09-13 05:45:42 +0000157 case MODRM_SPLITMISC:
158 if (modFromModRM(modRM) == 0x3)
159 return modRMTable[dec->instructionIDs+(modRM & 0x3f)+8];
160 return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)];
Sean Callanan04cc3072009-12-19 02:59:52 +0000161 case MODRM_FULL:
Craig Topper487e7442012-02-09 07:45:30 +0000162 return modRMTable[dec->instructionIDs+modRM];
Sean Callanan04cc3072009-12-19 02:59:52 +0000163 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000164}
165
166/*
167 * specifierForUID - Given a UID, returns the name and operand specification for
168 * that instruction.
169 *
170 * @param uid - The unique ID for the instruction. This should be returned by
171 * decode(); specifierForUID will not check bounds.
172 * @return - A pointer to the specification for that instruction.
173 */
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +0000174static const struct InstructionSpecifier *specifierForUID(InstrUID uid) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000175 return &INSTRUCTIONS_SYM[uid];
176}
177
178/*
179 * consumeByte - Uses the reader function provided by the user to consume one
180 * byte from the instruction's memory and advance the cursor.
181 *
182 * @param insn - The instruction with the reader function to use. The cursor
183 * for this instruction is advanced.
184 * @param byte - A pointer to a pre-allocated memory buffer to be populated
185 * with the data read.
186 * @return - 0 if the read was successful; nonzero otherwise.
187 */
Sean Callanan588785c2009-12-22 22:51:40 +0000188static int consumeByte(struct InternalInstruction* insn, uint8_t* byte) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000189 int ret = insn->reader(insn->readerArg, byte, insn->readerCursor);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000190
Sean Callanan04cc3072009-12-19 02:59:52 +0000191 if (!ret)
192 ++(insn->readerCursor);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000193
Sean Callanan04cc3072009-12-19 02:59:52 +0000194 return ret;
195}
196
197/*
198 * lookAtByte - Like consumeByte, but does not advance the cursor.
199 *
200 * @param insn - See consumeByte().
201 * @param byte - See consumeByte().
202 * @return - See consumeByte().
203 */
Sean Callanan588785c2009-12-22 22:51:40 +0000204static int lookAtByte(struct InternalInstruction* insn, uint8_t* byte) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000205 return insn->reader(insn->readerArg, byte, insn->readerCursor);
206}
207
Sean Callanan588785c2009-12-22 22:51:40 +0000208static void unconsumeByte(struct InternalInstruction* insn) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000209 insn->readerCursor--;
210}
211
Sean Callanan588785c2009-12-22 22:51:40 +0000212#define CONSUME_FUNC(name, type) \
213 static int name(struct InternalInstruction* insn, type* ptr) { \
214 type combined = 0; \
215 unsigned offset; \
216 for (offset = 0; offset < sizeof(type); ++offset) { \
217 uint8_t byte; \
218 int ret = insn->reader(insn->readerArg, \
219 &byte, \
220 insn->readerCursor + offset); \
221 if (ret) \
222 return ret; \
Richard Smith228e6d42012-08-24 23:29:28 +0000223 combined = combined | ((uint64_t)byte << (offset * 8)); \
Sean Callanan588785c2009-12-22 22:51:40 +0000224 } \
225 *ptr = combined; \
226 insn->readerCursor += sizeof(type); \
227 return 0; \
Sean Callanan04cc3072009-12-19 02:59:52 +0000228 }
229
230/*
231 * consume* - Use the reader function provided by the user to consume data
232 * values of various sizes from the instruction's memory and advance the
233 * cursor appropriately. These readers perform endian conversion.
234 *
235 * @param insn - See consumeByte().
236 * @param ptr - A pointer to a pre-allocated memory of appropriate size to
237 * be populated with the data read.
238 * @return - See consumeByte().
239 */
240CONSUME_FUNC(consumeInt8, int8_t)
241CONSUME_FUNC(consumeInt16, int16_t)
242CONSUME_FUNC(consumeInt32, int32_t)
243CONSUME_FUNC(consumeUInt16, uint16_t)
244CONSUME_FUNC(consumeUInt32, uint32_t)
245CONSUME_FUNC(consumeUInt64, uint64_t)
246
247/*
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000248 * dbgprintf - Uses the logging function provided by the user to log a single
Sean Callanan04cc3072009-12-19 02:59:52 +0000249 * message, typically without a carriage-return.
250 *
251 * @param insn - The instruction containing the logging function.
252 * @param format - See printf().
253 * @param ... - See printf().
254 */
Sean Callanan588785c2009-12-22 22:51:40 +0000255static void dbgprintf(struct InternalInstruction* insn,
256 const char* format,
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000257 ...) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000258 char buffer[256];
259 va_list ap;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000260
Sean Callanan04cc3072009-12-19 02:59:52 +0000261 if (!insn->dlog)
262 return;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000263
Sean Callanan04cc3072009-12-19 02:59:52 +0000264 va_start(ap, format);
265 (void)vsnprintf(buffer, sizeof(buffer), format, ap);
266 va_end(ap);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000267
Sean Callanan04cc3072009-12-19 02:59:52 +0000268 insn->dlog(insn->dlogArg, buffer);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000269
Sean Callanan04cc3072009-12-19 02:59:52 +0000270 return;
271}
272
273/*
274 * setPrefixPresent - Marks that a particular prefix is present at a particular
275 * location.
276 *
277 * @param insn - The instruction to be marked as having the prefix.
278 * @param prefix - The prefix that is present.
279 * @param location - The location where the prefix is located (in the address
280 * space of the instruction's reader).
281 */
Sean Callanan588785c2009-12-22 22:51:40 +0000282static void setPrefixPresent(struct InternalInstruction* insn,
Sean Callanan04cc3072009-12-19 02:59:52 +0000283 uint8_t prefix,
284 uint64_t location)
285{
286 insn->prefixPresent[prefix] = 1;
287 insn->prefixLocations[prefix] = location;
288}
289
290/*
291 * isPrefixAtLocation - Queries an instruction to determine whether a prefix is
292 * present at a given location.
293 *
294 * @param insn - The instruction to be queried.
295 * @param prefix - The prefix.
296 * @param location - The location to query.
297 * @return - Whether the prefix is at that location.
298 */
Sean Callanan588785c2009-12-22 22:51:40 +0000299static BOOL isPrefixAtLocation(struct InternalInstruction* insn,
300 uint8_t prefix,
301 uint64_t location)
Sean Callanan04cc3072009-12-19 02:59:52 +0000302{
303 if (insn->prefixPresent[prefix] == 1 &&
304 insn->prefixLocations[prefix] == location)
305 return TRUE;
306 else
307 return FALSE;
308}
309
310/*
311 * readPrefixes - Consumes all of an instruction's prefix bytes, and marks the
312 * instruction as having them. Also sets the instruction's default operand,
313 * address, and other relevant data sizes to report operands correctly.
314 *
315 * @param insn - The instruction whose prefixes are to be read.
316 * @return - 0 if the instruction could be read until the end of the prefix
317 * bytes, and no prefixes conflicted; nonzero otherwise.
318 */
319static int readPrefixes(struct InternalInstruction* insn) {
320 BOOL isPrefix = TRUE;
321 BOOL prefixGroups[4] = { FALSE };
322 uint64_t prefixLocation;
Ted Kremenek3c4408c2011-01-23 17:05:06 +0000323 uint8_t byte = 0;
Richard Mitton79917a92013-08-30 21:32:42 +0000324 uint8_t nextByte;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000325
Sean Callanan04cc3072009-12-19 02:59:52 +0000326 BOOL hasAdSize = FALSE;
327 BOOL hasOpSize = FALSE;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000328
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000329 dbgprintf(insn, "readPrefixes()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000330
Sean Callanan04cc3072009-12-19 02:59:52 +0000331 while (isPrefix) {
332 prefixLocation = insn->readerCursor;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000333
Richard Mitton576ee002013-08-30 21:19:48 +0000334 /* If we fail reading prefixes, just stop here and let the opcode reader deal with it */
Sean Callanan04cc3072009-12-19 02:59:52 +0000335 if (consumeByte(insn, &byte))
Richard Mitton576ee002013-08-30 21:19:48 +0000336 break;
Kevin Enderby014e1cd2012-03-09 17:52:49 +0000337
Benjamin Krameradfc73d2012-03-10 15:10:06 +0000338 /*
Dave Zarzycki07fabee2013-03-25 18:59:38 +0000339 * If the byte is a LOCK/REP/REPNE prefix and not a part of the opcode, then
340 * break and let it be disassembled as a normal "instruction".
Benjamin Krameradfc73d2012-03-10 15:10:06 +0000341 */
Richard Mitton576ee002013-08-30 21:19:48 +0000342 if (insn->readerCursor - 1 == insn->startLocation && byte == 0xf0)
343 break;
344
Dave Zarzycki07fabee2013-03-25 18:59:38 +0000345 if (insn->readerCursor - 1 == insn->startLocation
Richard Mitton576ee002013-08-30 21:19:48 +0000346 && (byte == 0xf2 || byte == 0xf3)
347 && !lookAtByte(insn, &nextByte))
348 {
Kevin Enderby35fd7922013-06-20 22:32:18 +0000349 /*
350 * If the byte is 0xf2 or 0xf3, and any of the following conditions are
351 * met:
352 * - it is followed by a LOCK (0xf0) prefix
353 * - it is followed by an xchg instruction
354 * then it should be disassembled as a xacquire/xrelease not repne/rep.
355 */
356 if ((byte == 0xf2 || byte == 0xf3) &&
357 ((nextByte == 0xf0) |
358 ((nextByte & 0xfe) == 0x86 || (nextByte & 0xf8) == 0x90)))
359 insn->xAcquireRelease = TRUE;
360 /*
361 * Also if the byte is 0xf3, and the following condition is met:
362 * - it is followed by a "mov mem, reg" (opcode 0x88/0x89) or
363 * "mov mem, imm" (opcode 0xc6/0xc7) instructions.
364 * then it should be disassembled as an xrelease not rep.
365 */
366 if (byte == 0xf3 &&
367 (nextByte == 0x88 || nextByte == 0x89 ||
368 nextByte == 0xc6 || nextByte == 0xc7))
369 insn->xAcquireRelease = TRUE;
Dave Zarzycki07fabee2013-03-25 18:59:38 +0000370 if (insn->mode == MODE_64BIT && (nextByte & 0xf0) == 0x40) {
371 if (consumeByte(insn, &nextByte))
372 return -1;
373 if (lookAtByte(insn, &nextByte))
374 return -1;
375 unconsumeByte(insn);
376 }
377 if (nextByte != 0x0f && nextByte != 0x90)
378 break;
379 }
380
Sean Callanan04cc3072009-12-19 02:59:52 +0000381 switch (byte) {
382 case 0xf0: /* LOCK */
383 case 0xf2: /* REPNE/REPNZ */
384 case 0xf3: /* REP or REPE/REPZ */
385 if (prefixGroups[0])
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000386 dbgprintf(insn, "Redundant Group 1 prefix");
Sean Callanan04cc3072009-12-19 02:59:52 +0000387 prefixGroups[0] = TRUE;
388 setPrefixPresent(insn, byte, prefixLocation);
389 break;
390 case 0x2e: /* CS segment override -OR- Branch not taken */
391 case 0x36: /* SS segment override -OR- Branch taken */
392 case 0x3e: /* DS segment override */
393 case 0x26: /* ES segment override */
394 case 0x64: /* FS segment override */
395 case 0x65: /* GS segment override */
396 switch (byte) {
397 case 0x2e:
398 insn->segmentOverride = SEG_OVERRIDE_CS;
399 break;
400 case 0x36:
401 insn->segmentOverride = SEG_OVERRIDE_SS;
402 break;
403 case 0x3e:
404 insn->segmentOverride = SEG_OVERRIDE_DS;
405 break;
406 case 0x26:
407 insn->segmentOverride = SEG_OVERRIDE_ES;
408 break;
409 case 0x64:
410 insn->segmentOverride = SEG_OVERRIDE_FS;
411 break;
412 case 0x65:
413 insn->segmentOverride = SEG_OVERRIDE_GS;
414 break;
415 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000416 debug("Unhandled override");
417 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +0000418 }
419 if (prefixGroups[1])
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000420 dbgprintf(insn, "Redundant Group 2 prefix");
Sean Callanan04cc3072009-12-19 02:59:52 +0000421 prefixGroups[1] = TRUE;
422 setPrefixPresent(insn, byte, prefixLocation);
423 break;
424 case 0x66: /* Operand-size override */
425 if (prefixGroups[2])
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000426 dbgprintf(insn, "Redundant Group 3 prefix");
Sean Callanan04cc3072009-12-19 02:59:52 +0000427 prefixGroups[2] = TRUE;
428 hasOpSize = TRUE;
429 setPrefixPresent(insn, byte, prefixLocation);
430 break;
431 case 0x67: /* Address-size override */
432 if (prefixGroups[3])
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000433 dbgprintf(insn, "Redundant Group 4 prefix");
Sean Callanan04cc3072009-12-19 02:59:52 +0000434 prefixGroups[3] = TRUE;
435 hasAdSize = TRUE;
436 setPrefixPresent(insn, byte, prefixLocation);
437 break;
438 default: /* Not a prefix byte */
439 isPrefix = FALSE;
440 break;
441 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000442
Sean Callanan04cc3072009-12-19 02:59:52 +0000443 if (isPrefix)
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000444 dbgprintf(insn, "Found prefix 0x%hhx", byte);
Sean Callanan04cc3072009-12-19 02:59:52 +0000445 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000446
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000447 insn->vectorExtensionType = TYPE_NO_VEX_XOP;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000448
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000449 if (byte == 0x62) {
450 uint8_t byte1, byte2;
451
452 if (consumeByte(insn, &byte1)) {
453 dbgprintf(insn, "Couldn't read second byte of EVEX prefix");
454 return -1;
455 }
456
457 if (lookAtByte(insn, &byte2)) {
458 dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
459 return -1;
460 }
461
462 if ((insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) &&
463 ((~byte1 & 0xc) == 0xc) && ((byte2 & 0x4) == 0x4)) {
464 insn->vectorExtensionType = TYPE_EVEX;
465 }
466 else {
467 unconsumeByte(insn); /* unconsume byte1 */
468 unconsumeByte(insn); /* unconsume byte */
469 insn->necessaryPrefixLocation = insn->readerCursor - 2;
470 }
471
472 if (insn->vectorExtensionType == TYPE_EVEX) {
473 insn->vectorExtensionPrefix[0] = byte;
474 insn->vectorExtensionPrefix[1] = byte1;
475 if (consumeByte(insn, &insn->vectorExtensionPrefix[2])) {
476 dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
477 return -1;
478 }
479 if (consumeByte(insn, &insn->vectorExtensionPrefix[3])) {
480 dbgprintf(insn, "Couldn't read fourth byte of EVEX prefix");
481 return -1;
482 }
483
484 /* We simulate the REX prefix for simplicity's sake */
485 if (insn->mode == MODE_64BIT) {
486 insn->rexPrefix = 0x40
487 | (wFromEVEX3of4(insn->vectorExtensionPrefix[2]) << 3)
488 | (rFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 2)
489 | (xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 1)
490 | (bFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 0);
491 }
492
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000493 dbgprintf(insn, "Found EVEX prefix 0x%hhx 0x%hhx 0x%hhx 0x%hhx",
494 insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
495 insn->vectorExtensionPrefix[2], insn->vectorExtensionPrefix[3]);
496 }
497 }
498 else if (byte == 0xc4) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000499 uint8_t byte1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000500
Sean Callananc3fd5232011-03-15 01:23:15 +0000501 if (lookAtByte(insn, &byte1)) {
502 dbgprintf(insn, "Couldn't read second byte of VEX");
503 return -1;
504 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000505
Craig Topper45faba92011-09-26 05:12:43 +0000506 if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000507 insn->vectorExtensionType = TYPE_VEX_3B;
Sean Callananc3fd5232011-03-15 01:23:15 +0000508 insn->necessaryPrefixLocation = insn->readerCursor - 1;
509 }
510 else {
Sean Callanan04cc3072009-12-19 02:59:52 +0000511 unconsumeByte(insn);
512 insn->necessaryPrefixLocation = insn->readerCursor - 1;
513 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000514
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000515 if (insn->vectorExtensionType == TYPE_VEX_3B) {
516 insn->vectorExtensionPrefix[0] = byte;
517 consumeByte(insn, &insn->vectorExtensionPrefix[1]);
518 consumeByte(insn, &insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +0000519
520 /* We simulate the REX prefix for simplicity's sake */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000521
Craig Topper31854ba2011-10-03 07:51:09 +0000522 if (insn->mode == MODE_64BIT) {
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000523 insn->rexPrefix = 0x40
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000524 | (wFromVEX3of3(insn->vectorExtensionPrefix[2]) << 3)
525 | (rFromVEX2of3(insn->vectorExtensionPrefix[1]) << 2)
526 | (xFromVEX2of3(insn->vectorExtensionPrefix[1]) << 1)
527 | (bFromVEX2of3(insn->vectorExtensionPrefix[1]) << 0);
Craig Topper31854ba2011-10-03 07:51:09 +0000528 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000529
Craig Topper9e3e38a2013-10-03 05:17:48 +0000530 dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx 0x%hhx",
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000531 insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
532 insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +0000533 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000534 }
Sean Callananc3fd5232011-03-15 01:23:15 +0000535 else if (byte == 0xc5) {
536 uint8_t byte1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000537
Sean Callananc3fd5232011-03-15 01:23:15 +0000538 if (lookAtByte(insn, &byte1)) {
539 dbgprintf(insn, "Couldn't read second byte of VEX");
540 return -1;
541 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000542
Craig Topper45faba92011-09-26 05:12:43 +0000543 if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000544 insn->vectorExtensionType = TYPE_VEX_2B;
Sean Callananc3fd5232011-03-15 01:23:15 +0000545 }
546 else {
547 unconsumeByte(insn);
548 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000549
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000550 if (insn->vectorExtensionType == TYPE_VEX_2B) {
551 insn->vectorExtensionPrefix[0] = byte;
552 consumeByte(insn, &insn->vectorExtensionPrefix[1]);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000553
Craig Topper31854ba2011-10-03 07:51:09 +0000554 if (insn->mode == MODE_64BIT) {
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000555 insn->rexPrefix = 0x40
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000556 | (rFromVEX2of2(insn->vectorExtensionPrefix[1]) << 2);
Craig Topper31854ba2011-10-03 07:51:09 +0000557 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000558
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000559 switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1]))
Sean Callananc3fd5232011-03-15 01:23:15 +0000560 {
561 default:
562 break;
563 case VEX_PREFIX_66:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000564 hasOpSize = TRUE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000565 break;
566 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000567
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000568 dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx",
569 insn->vectorExtensionPrefix[0],
570 insn->vectorExtensionPrefix[1]);
Craig Topper9e3e38a2013-10-03 05:17:48 +0000571 }
572 }
573 else if (byte == 0x8f) {
574 uint8_t byte1;
575
576 if (lookAtByte(insn, &byte1)) {
577 dbgprintf(insn, "Couldn't read second byte of XOP");
578 return -1;
579 }
580
Craig Topper9eb88372013-10-03 06:29:59 +0000581 if ((byte1 & 0x38) != 0x0) { /* 0 in these 3 bits is a POP instruction. */
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000582 insn->vectorExtensionType = TYPE_XOP;
Craig Topper9e3e38a2013-10-03 05:17:48 +0000583 insn->necessaryPrefixLocation = insn->readerCursor - 1;
584 }
585 else {
586 unconsumeByte(insn);
587 insn->necessaryPrefixLocation = insn->readerCursor - 1;
588 }
589
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000590 if (insn->vectorExtensionType == TYPE_XOP) {
591 insn->vectorExtensionPrefix[0] = byte;
592 consumeByte(insn, &insn->vectorExtensionPrefix[1]);
593 consumeByte(insn, &insn->vectorExtensionPrefix[2]);
Craig Topper9e3e38a2013-10-03 05:17:48 +0000594
595 /* We simulate the REX prefix for simplicity's sake */
596
597 if (insn->mode == MODE_64BIT) {
598 insn->rexPrefix = 0x40
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000599 | (wFromXOP3of3(insn->vectorExtensionPrefix[2]) << 3)
600 | (rFromXOP2of3(insn->vectorExtensionPrefix[1]) << 2)
601 | (xFromXOP2of3(insn->vectorExtensionPrefix[1]) << 1)
602 | (bFromXOP2of3(insn->vectorExtensionPrefix[1]) << 0);
Craig Topper9e3e38a2013-10-03 05:17:48 +0000603 }
604
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000605 switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2]))
Craig Topper9e3e38a2013-10-03 05:17:48 +0000606 {
607 default:
608 break;
609 case VEX_PREFIX_66:
610 hasOpSize = TRUE;
611 break;
612 }
613
614 dbgprintf(insn, "Found XOP prefix 0x%hhx 0x%hhx 0x%hhx",
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000615 insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
616 insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +0000617 }
618 }
619 else {
620 if (insn->mode == MODE_64BIT) {
621 if ((byte & 0xf0) == 0x40) {
622 uint8_t opcodeByte;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000623
Sean Callananc3fd5232011-03-15 01:23:15 +0000624 if (lookAtByte(insn, &opcodeByte) || ((opcodeByte & 0xf0) == 0x40)) {
625 dbgprintf(insn, "Redundant REX prefix");
626 return -1;
627 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000628
Sean Callananc3fd5232011-03-15 01:23:15 +0000629 insn->rexPrefix = byte;
630 insn->necessaryPrefixLocation = insn->readerCursor - 2;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000631
Sean Callananc3fd5232011-03-15 01:23:15 +0000632 dbgprintf(insn, "Found REX prefix 0x%hhx", byte);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000633 } else {
Sean Callananc3fd5232011-03-15 01:23:15 +0000634 unconsumeByte(insn);
635 insn->necessaryPrefixLocation = insn->readerCursor - 1;
636 }
637 } else {
638 unconsumeByte(insn);
639 insn->necessaryPrefixLocation = insn->readerCursor - 1;
640 }
641 }
642
Sean Callanan04cc3072009-12-19 02:59:52 +0000643 if (insn->mode == MODE_16BIT) {
644 insn->registerSize = (hasOpSize ? 4 : 2);
645 insn->addressSize = (hasAdSize ? 4 : 2);
646 insn->displacementSize = (hasAdSize ? 4 : 2);
647 insn->immediateSize = (hasOpSize ? 4 : 2);
648 } else if (insn->mode == MODE_32BIT) {
649 insn->registerSize = (hasOpSize ? 2 : 4);
650 insn->addressSize = (hasAdSize ? 2 : 4);
651 insn->displacementSize = (hasAdSize ? 2 : 4);
Sean Callanan9f6c6222010-10-22 01:24:11 +0000652 insn->immediateSize = (hasOpSize ? 2 : 4);
Sean Callanan04cc3072009-12-19 02:59:52 +0000653 } else if (insn->mode == MODE_64BIT) {
654 if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
655 insn->registerSize = 8;
656 insn->addressSize = (hasAdSize ? 4 : 8);
657 insn->displacementSize = 4;
658 insn->immediateSize = 4;
659 } else if (insn->rexPrefix) {
660 insn->registerSize = (hasOpSize ? 2 : 4);
661 insn->addressSize = (hasAdSize ? 4 : 8);
662 insn->displacementSize = (hasOpSize ? 2 : 4);
663 insn->immediateSize = (hasOpSize ? 2 : 4);
664 } else {
665 insn->registerSize = (hasOpSize ? 2 : 4);
666 insn->addressSize = (hasAdSize ? 4 : 8);
667 insn->displacementSize = (hasOpSize ? 2 : 4);
668 insn->immediateSize = (hasOpSize ? 2 : 4);
669 }
670 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000671
Sean Callanan04cc3072009-12-19 02:59:52 +0000672 return 0;
673}
674
675/*
676 * readOpcode - Reads the opcode (excepting the ModR/M byte in the case of
677 * extended or escape opcodes).
678 *
679 * @param insn - The instruction whose opcode is to be read.
680 * @return - 0 if the opcode could be read successfully; nonzero otherwise.
681 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000682static int readOpcode(struct InternalInstruction* insn) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000683 /* Determine the length of the primary opcode */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000684
Sean Callanan04cc3072009-12-19 02:59:52 +0000685 uint8_t current;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000686
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000687 dbgprintf(insn, "readOpcode()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000688
Sean Callanan04cc3072009-12-19 02:59:52 +0000689 insn->opcodeType = ONEBYTE;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000690
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000691 if (insn->vectorExtensionType == TYPE_EVEX)
Sean Callananc3fd5232011-03-15 01:23:15 +0000692 {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000693 switch (mmFromEVEX2of4(insn->vectorExtensionPrefix[1])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000694 default:
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000695 dbgprintf(insn, "Unhandled mm field for instruction (0x%hhx)",
696 mmFromEVEX2of4(insn->vectorExtensionPrefix[1]));
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000697 return -1;
Sean Callananc3fd5232011-03-15 01:23:15 +0000698 case VEX_LOB_0F:
Sean Callananc3fd5232011-03-15 01:23:15 +0000699 insn->opcodeType = TWOBYTE;
700 return consumeByte(insn, &insn->opcode);
701 case VEX_LOB_0F38:
Sean Callananc3fd5232011-03-15 01:23:15 +0000702 insn->opcodeType = THREEBYTE_38;
703 return consumeByte(insn, &insn->opcode);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000704 case VEX_LOB_0F3A:
Sean Callananc3fd5232011-03-15 01:23:15 +0000705 insn->opcodeType = THREEBYTE_3A;
706 return consumeByte(insn, &insn->opcode);
707 }
708 }
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000709 else if (insn->vectorExtensionType == TYPE_VEX_3B) {
710 switch (mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])) {
711 default:
712 dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
713 mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
714 return -1;
715 case VEX_LOB_0F:
716 insn->opcodeType = TWOBYTE;
717 return consumeByte(insn, &insn->opcode);
718 case VEX_LOB_0F38:
719 insn->opcodeType = THREEBYTE_38;
720 return consumeByte(insn, &insn->opcode);
721 case VEX_LOB_0F3A:
722 insn->opcodeType = THREEBYTE_3A;
723 return consumeByte(insn, &insn->opcode);
724 }
725 }
726 else if (insn->vectorExtensionType == TYPE_VEX_2B) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000727 insn->opcodeType = TWOBYTE;
728 return consumeByte(insn, &insn->opcode);
729 }
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000730 else if (insn->vectorExtensionType == TYPE_XOP) {
731 switch (mmmmmFromXOP2of3(insn->vectorExtensionPrefix[1])) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000732 default:
733 dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000734 mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
Craig Topper9e3e38a2013-10-03 05:17:48 +0000735 return -1;
736 case XOP_MAP_SELECT_8:
737 insn->opcodeType = XOP8_MAP;
738 return consumeByte(insn, &insn->opcode);
739 case XOP_MAP_SELECT_9:
740 insn->opcodeType = XOP9_MAP;
741 return consumeByte(insn, &insn->opcode);
742 case XOP_MAP_SELECT_A:
743 insn->opcodeType = XOPA_MAP;
744 return consumeByte(insn, &insn->opcode);
745 }
746 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000747
Sean Callanan04cc3072009-12-19 02:59:52 +0000748 if (consumeByte(insn, &current))
749 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000750
Sean Callanan04cc3072009-12-19 02:59:52 +0000751 if (current == 0x0f) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000752 dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000753
Sean Callanan04cc3072009-12-19 02:59:52 +0000754 if (consumeByte(insn, &current))
755 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000756
Sean Callanan04cc3072009-12-19 02:59:52 +0000757 if (current == 0x38) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000758 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000759
Sean Callanan04cc3072009-12-19 02:59:52 +0000760 if (consumeByte(insn, &current))
761 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000762
Sean Callanan04cc3072009-12-19 02:59:52 +0000763 insn->opcodeType = THREEBYTE_38;
764 } else if (current == 0x3a) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000765 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000766
Sean Callanan04cc3072009-12-19 02:59:52 +0000767 if (consumeByte(insn, &current))
768 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000769
Sean Callanan04cc3072009-12-19 02:59:52 +0000770 insn->opcodeType = THREEBYTE_3A;
Joerg Sonnenbergerfc4789d2011-04-04 16:58:13 +0000771 } else if (current == 0xa6) {
772 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000773
Joerg Sonnenbergerfc4789d2011-04-04 16:58:13 +0000774 if (consumeByte(insn, &current))
775 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000776
Joerg Sonnenbergerfc4789d2011-04-04 16:58:13 +0000777 insn->opcodeType = THREEBYTE_A6;
778 } else if (current == 0xa7) {
779 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000780
Joerg Sonnenbergerfc4789d2011-04-04 16:58:13 +0000781 if (consumeByte(insn, &current))
782 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000783
Joerg Sonnenbergerfc4789d2011-04-04 16:58:13 +0000784 insn->opcodeType = THREEBYTE_A7;
Sean Callanan04cc3072009-12-19 02:59:52 +0000785 } else {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000786 dbgprintf(insn, "Didn't find a three-byte escape prefix");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000787
Sean Callanan04cc3072009-12-19 02:59:52 +0000788 insn->opcodeType = TWOBYTE;
789 }
790 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000791
Sean Callanan04cc3072009-12-19 02:59:52 +0000792 /*
793 * At this point we have consumed the full opcode.
794 * Anything we consume from here on must be unconsumed.
795 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000796
Sean Callanan04cc3072009-12-19 02:59:52 +0000797 insn->opcode = current;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000798
Sean Callanan04cc3072009-12-19 02:59:52 +0000799 return 0;
800}
801
802static int readModRM(struct InternalInstruction* insn);
803
804/*
805 * getIDWithAttrMask - Determines the ID of an instruction, consuming
806 * the ModR/M byte as appropriate for extended and escape opcodes,
807 * and using a supplied attribute mask.
808 *
809 * @param instructionID - A pointer whose target is filled in with the ID of the
810 * instruction.
811 * @param insn - The instruction whose ID is to be determined.
812 * @param attrMask - The attribute mask to search.
813 * @return - 0 if the ModR/M could be read when needed or was not
814 * needed; nonzero otherwise.
815 */
816static int getIDWithAttrMask(uint16_t* instructionID,
817 struct InternalInstruction* insn,
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000818 uint16_t attrMask) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000819 BOOL hasModRMExtension;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000820
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000821 uint16_t instructionClass;
Sean Callanan04cc3072009-12-19 02:59:52 +0000822
823 instructionClass = contextForAttrs(attrMask);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000824
Sean Callanan04cc3072009-12-19 02:59:52 +0000825 hasModRMExtension = modRMRequired(insn->opcodeType,
826 instructionClass,
827 insn->opcode);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000828
Sean Callanan04cc3072009-12-19 02:59:52 +0000829 if (hasModRMExtension) {
Rafael Espindola9f9a1062011-01-06 16:48:42 +0000830 if (readModRM(insn))
831 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000832
Sean Callanan04cc3072009-12-19 02:59:52 +0000833 *instructionID = decode(insn->opcodeType,
834 instructionClass,
835 insn->opcode,
836 insn->modRM);
837 } else {
838 *instructionID = decode(insn->opcodeType,
839 instructionClass,
840 insn->opcode,
841 0);
842 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000843
Sean Callanan04cc3072009-12-19 02:59:52 +0000844 return 0;
845}
846
847/*
848 * is16BitEquivalent - Determines whether two instruction names refer to
849 * equivalent instructions but one is 16-bit whereas the other is not.
850 *
851 * @param orig - The instruction that is not 16-bit
852 * @param equiv - The instruction that is 16-bit
853 */
Joerg Sonnenberger2b86e482012-10-29 17:56:15 +0000854static BOOL is16BitEquivalent(const char* orig, const char* equiv) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000855 off_t i;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000856
Sean Callanan010b3732010-04-02 21:23:51 +0000857 for (i = 0;; i++) {
858 if (orig[i] == '\0' && equiv[i] == '\0')
Sean Callanan04cc3072009-12-19 02:59:52 +0000859 return TRUE;
Sean Callanan010b3732010-04-02 21:23:51 +0000860 if (orig[i] == '\0' || equiv[i] == '\0')
Sean Callanan04cc3072009-12-19 02:59:52 +0000861 return FALSE;
Sean Callanan010b3732010-04-02 21:23:51 +0000862 if (orig[i] != equiv[i]) {
863 if ((orig[i] == 'Q' || orig[i] == 'L') && equiv[i] == 'W')
Sean Callanan04cc3072009-12-19 02:59:52 +0000864 continue;
Sean Callanan010b3732010-04-02 21:23:51 +0000865 if ((orig[i] == '6' || orig[i] == '3') && equiv[i] == '1')
Sean Callanan04cc3072009-12-19 02:59:52 +0000866 continue;
Sean Callanan010b3732010-04-02 21:23:51 +0000867 if ((orig[i] == '4' || orig[i] == '2') && equiv[i] == '6')
Sean Callanan04cc3072009-12-19 02:59:52 +0000868 continue;
869 return FALSE;
870 }
871 }
872}
873
874/*
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000875 * getID - Determines the ID of an instruction, consuming the ModR/M byte as
876 * appropriate for extended and escape opcodes. Determines the attributes and
Sean Callanan04cc3072009-12-19 02:59:52 +0000877 * context for the instruction before doing so.
878 *
879 * @param insn - The instruction whose ID is to be determined.
880 * @return - 0 if the ModR/M could be read when needed or was not needed;
881 * nonzero otherwise.
882 */
Roman Divacky67923802012-09-05 21:17:34 +0000883static int getID(struct InternalInstruction* insn, const void *miiArg) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000884 uint16_t attrMask;
Sean Callanan04cc3072009-12-19 02:59:52 +0000885 uint16_t instructionID;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000886
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000887 dbgprintf(insn, "getID()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000888
Sean Callanan04cc3072009-12-19 02:59:52 +0000889 attrMask = ATTR_NONE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000890
Sean Callanan04cc3072009-12-19 02:59:52 +0000891 if (insn->mode == MODE_64BIT)
892 attrMask |= ATTR_64BIT;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000893
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000894 if (insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
895 attrMask |= (insn->vectorExtensionType == TYPE_EVEX) ? ATTR_EVEX : ATTR_VEX;
Sean Callananc3fd5232011-03-15 01:23:15 +0000896
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000897 if (insn->vectorExtensionType == TYPE_EVEX) {
898 switch (ppFromEVEX3of4(insn->vectorExtensionPrefix[2])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000899 case VEX_PREFIX_66:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000900 attrMask |= ATTR_OPSIZE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000901 break;
902 case VEX_PREFIX_F3:
903 attrMask |= ATTR_XS;
904 break;
905 case VEX_PREFIX_F2:
906 attrMask |= ATTR_XD;
907 break;
908 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000909
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000910 if (zFromEVEX4of4(insn->vectorExtensionPrefix[3]))
911 attrMask |= ATTR_EVEXKZ;
912 if (bFromEVEX4of4(insn->vectorExtensionPrefix[3]))
913 attrMask |= ATTR_EVEXB;
914 if (aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]))
915 attrMask |= ATTR_EVEXK;
916 if (lFromEVEX4of4(insn->vectorExtensionPrefix[3]))
917 attrMask |= ATTR_EVEXL;
918 if (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
919 attrMask |= ATTR_EVEXL2;
920 }
921 else if (insn->vectorExtensionType == TYPE_VEX_3B) {
922 switch (ppFromVEX3of3(insn->vectorExtensionPrefix[2])) {
923 case VEX_PREFIX_66:
924 attrMask |= ATTR_OPSIZE;
925 break;
926 case VEX_PREFIX_F3:
927 attrMask |= ATTR_XS;
928 break;
929 case VEX_PREFIX_F2:
930 attrMask |= ATTR_XD;
931 break;
932 }
933
934 if (lFromVEX3of3(insn->vectorExtensionPrefix[2]))
Sean Callananc3fd5232011-03-15 01:23:15 +0000935 attrMask |= ATTR_VEXL;
936 }
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000937 else if (insn->vectorExtensionType == TYPE_VEX_2B) {
938 switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000939 case VEX_PREFIX_66:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000940 attrMask |= ATTR_OPSIZE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000941 break;
942 case VEX_PREFIX_F3:
943 attrMask |= ATTR_XS;
944 break;
945 case VEX_PREFIX_F2:
946 attrMask |= ATTR_XD;
947 break;
948 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000949
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000950 if (lFromVEX2of2(insn->vectorExtensionPrefix[1]))
Craig Topper9e3e38a2013-10-03 05:17:48 +0000951 attrMask |= ATTR_VEXL;
952 }
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000953 else if (insn->vectorExtensionType == TYPE_XOP) {
954 switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000955 case VEX_PREFIX_66:
956 attrMask |= ATTR_OPSIZE;
957 break;
958 case VEX_PREFIX_F3:
959 attrMask |= ATTR_XS;
960 break;
961 case VEX_PREFIX_F2:
962 attrMask |= ATTR_XD;
963 break;
964 }
965
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000966 if (lFromXOP3of3(insn->vectorExtensionPrefix[2]))
Sean Callananc3fd5232011-03-15 01:23:15 +0000967 attrMask |= ATTR_VEXL;
968 }
969 else {
970 return -1;
971 }
972 }
973 else {
David Woodhouse5cf4c672014-01-20 12:02:35 +0000974 if (insn->mode != MODE_16BIT && isPrefixAtLocation(insn, 0x66, insn->necessaryPrefixLocation))
Sean Callananc3fd5232011-03-15 01:23:15 +0000975 attrMask |= ATTR_OPSIZE;
Craig Topper6491c802012-02-27 01:54:29 +0000976 else if (isPrefixAtLocation(insn, 0x67, insn->necessaryPrefixLocation))
977 attrMask |= ATTR_ADSIZE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000978 else if (isPrefixAtLocation(insn, 0xf3, insn->necessaryPrefixLocation))
979 attrMask |= ATTR_XS;
980 else if (isPrefixAtLocation(insn, 0xf2, insn->necessaryPrefixLocation))
981 attrMask |= ATTR_XD;
Sean Callananc3fd5232011-03-15 01:23:15 +0000982 }
983
Craig Topperf18c8962011-10-04 06:30:42 +0000984 if (insn->rexPrefix & 0x08)
985 attrMask |= ATTR_REXW;
Craig Topperf01f1b52011-11-06 23:04:08 +0000986
Sean Callanan010b3732010-04-02 21:23:51 +0000987 if (getIDWithAttrMask(&instructionID, insn, attrMask))
Sean Callanan04cc3072009-12-19 02:59:52 +0000988 return -1;
Craig Topperf01f1b52011-11-06 23:04:08 +0000989
David Woodhouse9c74fdb2014-01-20 12:02:48 +0000990 /*
991 * JCXZ/JECXZ need special handling for 16-bit mode because the meaning
992 * of the AdSize prefix is inverted w.r.t. 32-bit mode.
993 */
994 if (insn->mode == MODE_16BIT && insn->opcode == 0xE3) {
995 const struct InstructionSpecifier *spec;
996 spec = specifierForUID(instructionID);
997
998 /*
999 * Check for Ii8PCRel instructions. We could alternatively do a
1000 * string-compare on the names, but this is probably cheaper.
1001 */
1002 if (x86OperandSets[spec->operands][0].type == TYPE_REL8) {
1003 attrMask ^= ATTR_ADSIZE;
1004 if (getIDWithAttrMask(&instructionID, insn, attrMask))
1005 return -1;
1006 }
1007 }
1008
Sean Callanan04cc3072009-12-19 02:59:52 +00001009 /* The following clauses compensate for limitations of the tables. */
Craig Topperf01f1b52011-11-06 23:04:08 +00001010
David Woodhouse5cf4c672014-01-20 12:02:35 +00001011 if ((insn->mode == MODE_16BIT || insn->prefixPresent[0x66]) &&
1012 !(attrMask & ATTR_OPSIZE)) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001013 /*
1014 * The instruction tables make no distinction between instructions that
1015 * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a
1016 * particular spot (i.e., many MMX operations). In general we're
1017 * conservative, but in the specific case where OpSize is present but not
1018 * in the right place we check if there's a 16-bit operation.
1019 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001020
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +00001021 const struct InstructionSpecifier *spec;
Sean Callanan04cc3072009-12-19 02:59:52 +00001022 uint16_t instructionIDWithOpsize;
Benjamin Kramer915e3d92012-02-11 16:01:02 +00001023 const char *specName, *specWithOpSizeName;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001024
Sean Callanan04cc3072009-12-19 02:59:52 +00001025 spec = specifierForUID(instructionID);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001026
Sean Callanan04cc3072009-12-19 02:59:52 +00001027 if (getIDWithAttrMask(&instructionIDWithOpsize,
1028 insn,
1029 attrMask | ATTR_OPSIZE)) {
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001030 /*
Sean Callanan04cc3072009-12-19 02:59:52 +00001031 * ModRM required with OpSize but not present; give up and return version
1032 * without OpSize set
1033 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001034
Sean Callanan04cc3072009-12-19 02:59:52 +00001035 insn->instructionID = instructionID;
1036 insn->spec = spec;
1037 return 0;
1038 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001039
Benjamin Kramer915e3d92012-02-11 16:01:02 +00001040 specName = x86DisassemblerGetInstrName(instructionID, miiArg);
1041 specWithOpSizeName =
Benjamin Kramer478e8de2012-02-11 14:50:54 +00001042 x86DisassemblerGetInstrName(instructionIDWithOpsize, miiArg);
1043
David Woodhouse5cf4c672014-01-20 12:02:35 +00001044 if (is16BitEquivalent(specName, specWithOpSizeName) &&
1045 (insn->mode == MODE_16BIT) ^ insn->prefixPresent[0x66]) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001046 insn->instructionID = instructionIDWithOpsize;
Benjamin Kramer915e3d92012-02-11 16:01:02 +00001047 insn->spec = specifierForUID(instructionIDWithOpsize);
Sean Callanan04cc3072009-12-19 02:59:52 +00001048 } else {
1049 insn->instructionID = instructionID;
1050 insn->spec = spec;
1051 }
1052 return 0;
1053 }
Craig Topper21c33652011-10-02 16:56:09 +00001054
1055 if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 &&
1056 insn->rexPrefix & 0x01) {
1057 /*
1058 * NOOP shouldn't decode as NOOP if REX.b is set. Instead
1059 * it should decode as XCHG %r8, %eax.
1060 */
1061
1062 const struct InstructionSpecifier *spec;
1063 uint16_t instructionIDWithNewOpcode;
1064 const struct InstructionSpecifier *specWithNewOpcode;
1065
1066 spec = specifierForUID(instructionID);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001067
Craig Topperb58a9662011-10-05 03:29:32 +00001068 /* Borrow opcode from one of the other XCHGar opcodes */
Craig Topper21c33652011-10-02 16:56:09 +00001069 insn->opcode = 0x91;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001070
Craig Topper21c33652011-10-02 16:56:09 +00001071 if (getIDWithAttrMask(&instructionIDWithNewOpcode,
1072 insn,
1073 attrMask)) {
1074 insn->opcode = 0x90;
1075
1076 insn->instructionID = instructionID;
1077 insn->spec = spec;
1078 return 0;
1079 }
1080
1081 specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode);
1082
Craig Topperb58a9662011-10-05 03:29:32 +00001083 /* Change back */
Craig Topper21c33652011-10-02 16:56:09 +00001084 insn->opcode = 0x90;
1085
1086 insn->instructionID = instructionIDWithNewOpcode;
1087 insn->spec = specWithNewOpcode;
1088
1089 return 0;
1090 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001091
Sean Callanan04cc3072009-12-19 02:59:52 +00001092 insn->instructionID = instructionID;
1093 insn->spec = specifierForUID(insn->instructionID);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001094
Sean Callanan04cc3072009-12-19 02:59:52 +00001095 return 0;
1096}
1097
1098/*
1099 * readSIB - Consumes the SIB byte to determine addressing information for an
1100 * instruction.
1101 *
1102 * @param insn - The instruction whose SIB byte is to be read.
1103 * @return - 0 if the SIB byte was successfully read; nonzero otherwise.
1104 */
1105static int readSIB(struct InternalInstruction* insn) {
Daniel Dunbar8b532de2009-12-22 01:41:37 +00001106 SIBIndex sibIndexBase = 0;
1107 SIBBase sibBaseBase = 0;
Sean Callanan04cc3072009-12-19 02:59:52 +00001108 uint8_t index, base;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001109
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001110 dbgprintf(insn, "readSIB()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001111
Sean Callanan04cc3072009-12-19 02:59:52 +00001112 if (insn->consumedSIB)
1113 return 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001114
Sean Callanan04cc3072009-12-19 02:59:52 +00001115 insn->consumedSIB = TRUE;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001116
Sean Callanan04cc3072009-12-19 02:59:52 +00001117 switch (insn->addressSize) {
1118 case 2:
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001119 dbgprintf(insn, "SIB-based addressing doesn't work in 16-bit mode");
Sean Callanan04cc3072009-12-19 02:59:52 +00001120 return -1;
1121 break;
1122 case 4:
1123 sibIndexBase = SIB_INDEX_EAX;
1124 sibBaseBase = SIB_BASE_EAX;
1125 break;
1126 case 8:
1127 sibIndexBase = SIB_INDEX_RAX;
1128 sibBaseBase = SIB_BASE_RAX;
1129 break;
1130 }
1131
1132 if (consumeByte(insn, &insn->sib))
1133 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001134
Sean Callanan04cc3072009-12-19 02:59:52 +00001135 index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001136 if (insn->vectorExtensionType == TYPE_EVEX)
1137 index |= v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001138
Sean Callanan04cc3072009-12-19 02:59:52 +00001139 switch (index) {
1140 case 0x4:
1141 insn->sibIndex = SIB_INDEX_NONE;
1142 break;
1143 default:
Benjamin Kramer25bddae2011-02-27 18:13:53 +00001144 insn->sibIndex = (SIBIndex)(sibIndexBase + index);
Sean Callanan04cc3072009-12-19 02:59:52 +00001145 if (insn->sibIndex == SIB_INDEX_sib ||
1146 insn->sibIndex == SIB_INDEX_sib64)
1147 insn->sibIndex = SIB_INDEX_NONE;
1148 break;
1149 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001150
Sean Callanan04cc3072009-12-19 02:59:52 +00001151 switch (scaleFromSIB(insn->sib)) {
1152 case 0:
1153 insn->sibScale = 1;
1154 break;
1155 case 1:
1156 insn->sibScale = 2;
1157 break;
1158 case 2:
1159 insn->sibScale = 4;
1160 break;
1161 case 3:
1162 insn->sibScale = 8;
1163 break;
1164 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001165
Sean Callanan04cc3072009-12-19 02:59:52 +00001166 base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001167
Sean Callanan04cc3072009-12-19 02:59:52 +00001168 switch (base) {
1169 case 0x5:
Craig Topperfae5ac22014-02-17 10:03:43 +00001170 case 0xd:
Sean Callanan04cc3072009-12-19 02:59:52 +00001171 switch (modFromModRM(insn->modRM)) {
1172 case 0x0:
1173 insn->eaDisplacement = EA_DISP_32;
1174 insn->sibBase = SIB_BASE_NONE;
1175 break;
1176 case 0x1:
1177 insn->eaDisplacement = EA_DISP_8;
Craig Topperfae5ac22014-02-17 10:03:43 +00001178 insn->sibBase = (SIBBase)(sibBaseBase + base);
Sean Callanan04cc3072009-12-19 02:59:52 +00001179 break;
1180 case 0x2:
1181 insn->eaDisplacement = EA_DISP_32;
Craig Topperfae5ac22014-02-17 10:03:43 +00001182 insn->sibBase = (SIBBase)(sibBaseBase + base);
Sean Callanan04cc3072009-12-19 02:59:52 +00001183 break;
1184 case 0x3:
Sean Callanan010b3732010-04-02 21:23:51 +00001185 debug("Cannot have Mod = 0b11 and a SIB byte");
1186 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001187 }
1188 break;
1189 default:
Benjamin Kramer25bddae2011-02-27 18:13:53 +00001190 insn->sibBase = (SIBBase)(sibBaseBase + base);
Sean Callanan04cc3072009-12-19 02:59:52 +00001191 break;
1192 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001193
Sean Callanan04cc3072009-12-19 02:59:52 +00001194 return 0;
1195}
1196
1197/*
1198 * readDisplacement - Consumes the displacement of an instruction.
1199 *
1200 * @param insn - The instruction whose displacement is to be read.
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001201 * @return - 0 if the displacement byte was successfully read; nonzero
Sean Callanan04cc3072009-12-19 02:59:52 +00001202 * otherwise.
1203 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001204static int readDisplacement(struct InternalInstruction* insn) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001205 int8_t d8;
1206 int16_t d16;
1207 int32_t d32;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001208
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001209 dbgprintf(insn, "readDisplacement()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001210
Sean Callanan04cc3072009-12-19 02:59:52 +00001211 if (insn->consumedDisplacement)
1212 return 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001213
Sean Callanan04cc3072009-12-19 02:59:52 +00001214 insn->consumedDisplacement = TRUE;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +00001215 insn->displacementOffset = insn->readerCursor - insn->startLocation;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001216
Sean Callanan04cc3072009-12-19 02:59:52 +00001217 switch (insn->eaDisplacement) {
1218 case EA_DISP_NONE:
1219 insn->consumedDisplacement = FALSE;
1220 break;
1221 case EA_DISP_8:
1222 if (consumeInt8(insn, &d8))
1223 return -1;
1224 insn->displacement = d8;
1225 break;
1226 case EA_DISP_16:
1227 if (consumeInt16(insn, &d16))
1228 return -1;
1229 insn->displacement = d16;
1230 break;
1231 case EA_DISP_32:
1232 if (consumeInt32(insn, &d32))
1233 return -1;
1234 insn->displacement = d32;
1235 break;
1236 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001237
Sean Callanan04cc3072009-12-19 02:59:52 +00001238 insn->consumedDisplacement = TRUE;
1239 return 0;
1240}
1241
1242/*
1243 * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and
1244 * displacement) for an instruction and interprets it.
1245 *
1246 * @param insn - The instruction whose addressing information is to be read.
1247 * @return - 0 if the information was successfully read; nonzero otherwise.
1248 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001249static int readModRM(struct InternalInstruction* insn) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001250 uint8_t mod, rm, reg;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001251
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001252 dbgprintf(insn, "readModRM()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001253
Sean Callanan04cc3072009-12-19 02:59:52 +00001254 if (insn->consumedModRM)
1255 return 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001256
Rafael Espindola9f9a1062011-01-06 16:48:42 +00001257 if (consumeByte(insn, &insn->modRM))
1258 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001259 insn->consumedModRM = TRUE;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001260
Sean Callanan04cc3072009-12-19 02:59:52 +00001261 mod = modFromModRM(insn->modRM);
1262 rm = rmFromModRM(insn->modRM);
1263 reg = regFromModRM(insn->modRM);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001264
Sean Callanan04cc3072009-12-19 02:59:52 +00001265 /*
1266 * This goes by insn->registerSize to pick the correct register, which messes
1267 * up if we're using (say) XMM or 8-bit register operands. That gets fixed in
1268 * fixupReg().
1269 */
1270 switch (insn->registerSize) {
1271 case 2:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001272 insn->regBase = MODRM_REG_AX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001273 insn->eaRegBase = EA_REG_AX;
1274 break;
1275 case 4:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001276 insn->regBase = MODRM_REG_EAX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001277 insn->eaRegBase = EA_REG_EAX;
1278 break;
1279 case 8:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001280 insn->regBase = MODRM_REG_RAX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001281 insn->eaRegBase = EA_REG_RAX;
1282 break;
1283 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001284
Sean Callanan04cc3072009-12-19 02:59:52 +00001285 reg |= rFromREX(insn->rexPrefix) << 3;
1286 rm |= bFromREX(insn->rexPrefix) << 3;
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001287 if (insn->vectorExtensionType == TYPE_EVEX) {
1288 reg |= r2FromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1289 rm |= xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1290 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001291
Sean Callanan04cc3072009-12-19 02:59:52 +00001292 insn->reg = (Reg)(insn->regBase + reg);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001293
Sean Callanan04cc3072009-12-19 02:59:52 +00001294 switch (insn->addressSize) {
1295 case 2:
1296 insn->eaBaseBase = EA_BASE_BX_SI;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001297
Sean Callanan04cc3072009-12-19 02:59:52 +00001298 switch (mod) {
1299 case 0x0:
1300 if (rm == 0x6) {
1301 insn->eaBase = EA_BASE_NONE;
1302 insn->eaDisplacement = EA_DISP_16;
Sean Callanan010b3732010-04-02 21:23:51 +00001303 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001304 return -1;
1305 } else {
1306 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1307 insn->eaDisplacement = EA_DISP_NONE;
1308 }
1309 break;
1310 case 0x1:
1311 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1312 insn->eaDisplacement = EA_DISP_8;
Craig Topper399e39e2014-01-25 22:48:43 +00001313 insn->displacementSize = 1;
Sean Callanan010b3732010-04-02 21:23:51 +00001314 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001315 return -1;
1316 break;
1317 case 0x2:
1318 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1319 insn->eaDisplacement = EA_DISP_16;
Sean Callanan010b3732010-04-02 21:23:51 +00001320 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001321 return -1;
1322 break;
1323 case 0x3:
1324 insn->eaBase = (EABase)(insn->eaRegBase + rm);
Sean Callanan010b3732010-04-02 21:23:51 +00001325 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001326 return -1;
1327 break;
1328 }
1329 break;
1330 case 4:
1331 case 8:
1332 insn->eaBaseBase = (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001333
Sean Callanan04cc3072009-12-19 02:59:52 +00001334 switch (mod) {
1335 case 0x0:
1336 insn->eaDisplacement = EA_DISP_NONE; /* readSIB may override this */
1337 switch (rm) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001338 case 0x14:
Sean Callanan04cc3072009-12-19 02:59:52 +00001339 case 0x4:
1340 case 0xc: /* in case REXW.b is set */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001341 insn->eaBase = (insn->addressSize == 4 ?
Sean Callanan04cc3072009-12-19 02:59:52 +00001342 EA_BASE_sib : EA_BASE_sib64);
1343 readSIB(insn);
Sean Callanan010b3732010-04-02 21:23:51 +00001344 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001345 return -1;
1346 break;
1347 case 0x5:
1348 insn->eaBase = EA_BASE_NONE;
1349 insn->eaDisplacement = EA_DISP_32;
Sean Callanan010b3732010-04-02 21:23:51 +00001350 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001351 return -1;
1352 break;
1353 default:
1354 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1355 break;
1356 }
1357 break;
1358 case 0x1:
Craig Topper399e39e2014-01-25 22:48:43 +00001359 insn->displacementSize = 1;
Alp Toker771f7652014-01-26 18:44:34 +00001360 /* FALLTHROUGH */
Sean Callanan04cc3072009-12-19 02:59:52 +00001361 case 0x2:
1362 insn->eaDisplacement = (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
1363 switch (rm) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001364 case 0x14:
Sean Callanan04cc3072009-12-19 02:59:52 +00001365 case 0x4:
1366 case 0xc: /* in case REXW.b is set */
1367 insn->eaBase = EA_BASE_sib;
1368 readSIB(insn);
Sean Callanan010b3732010-04-02 21:23:51 +00001369 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001370 return -1;
1371 break;
1372 default:
1373 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
Sean Callanan010b3732010-04-02 21:23:51 +00001374 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001375 return -1;
1376 break;
1377 }
1378 break;
1379 case 0x3:
1380 insn->eaDisplacement = EA_DISP_NONE;
1381 insn->eaBase = (EABase)(insn->eaRegBase + rm);
1382 break;
1383 }
1384 break;
1385 } /* switch (insn->addressSize) */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001386
Sean Callanan04cc3072009-12-19 02:59:52 +00001387 return 0;
1388}
1389
1390#define GENERIC_FIXUP_FUNC(name, base, prefix) \
1391 static uint8_t name(struct InternalInstruction *insn, \
1392 OperandType type, \
1393 uint8_t index, \
1394 uint8_t *valid) { \
1395 *valid = 1; \
1396 switch (type) { \
1397 default: \
Sean Callanan010b3732010-04-02 21:23:51 +00001398 debug("Unhandled register type"); \
1399 *valid = 0; \
1400 return 0; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001401 case TYPE_Rv: \
1402 return base + index; \
1403 case TYPE_R8: \
Sean Callanan010b3732010-04-02 21:23:51 +00001404 if (insn->rexPrefix && \
Sean Callanan04cc3072009-12-19 02:59:52 +00001405 index >= 4 && index <= 7) { \
1406 return prefix##_SPL + (index - 4); \
1407 } else { \
1408 return prefix##_AL + index; \
1409 } \
1410 case TYPE_R16: \
1411 return prefix##_AX + index; \
1412 case TYPE_R32: \
1413 return prefix##_EAX + index; \
1414 case TYPE_R64: \
1415 return prefix##_RAX + index; \
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001416 case TYPE_XMM512: \
1417 return prefix##_ZMM0 + index; \
Sean Callananc3fd5232011-03-15 01:23:15 +00001418 case TYPE_XMM256: \
1419 return prefix##_YMM0 + index; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001420 case TYPE_XMM128: \
1421 case TYPE_XMM64: \
1422 case TYPE_XMM32: \
1423 case TYPE_XMM: \
1424 return prefix##_XMM0 + index; \
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001425 case TYPE_VK1: \
1426 case TYPE_VK8: \
1427 case TYPE_VK16: \
1428 return prefix##_K0 + index; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001429 case TYPE_MM64: \
1430 case TYPE_MM32: \
1431 case TYPE_MM: \
Sean Callanan010b3732010-04-02 21:23:51 +00001432 if (index > 7) \
Sean Callanan04cc3072009-12-19 02:59:52 +00001433 *valid = 0; \
1434 return prefix##_MM0 + index; \
1435 case TYPE_SEGMENTREG: \
Sean Callanan010b3732010-04-02 21:23:51 +00001436 if (index > 5) \
Sean Callanan04cc3072009-12-19 02:59:52 +00001437 *valid = 0; \
1438 return prefix##_ES + index; \
1439 case TYPE_DEBUGREG: \
Sean Callanan010b3732010-04-02 21:23:51 +00001440 if (index > 7) \
Sean Callanan04cc3072009-12-19 02:59:52 +00001441 *valid = 0; \
1442 return prefix##_DR0 + index; \
Sean Callanane7e1cf92010-05-06 20:59:00 +00001443 case TYPE_CONTROLREG: \
Sean Callanan010b3732010-04-02 21:23:51 +00001444 if (index > 8) \
Sean Callanan04cc3072009-12-19 02:59:52 +00001445 *valid = 0; \
Sean Callanane7e1cf92010-05-06 20:59:00 +00001446 return prefix##_CR0 + index; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001447 } \
1448 }
1449
1450/*
1451 * fixup*Value - Consults an operand type to determine the meaning of the
1452 * reg or R/M field. If the operand is an XMM operand, for example, an
1453 * operand would be XMM0 instead of AX, which readModRM() would otherwise
1454 * misinterpret it as.
1455 *
1456 * @param insn - The instruction containing the operand.
1457 * @param type - The operand type.
1458 * @param index - The existing value of the field as reported by readModRM().
1459 * @param valid - The address of a uint8_t. The target is set to 1 if the
1460 * field is valid for the register class; 0 if not.
Sean Callanan010b3732010-04-02 21:23:51 +00001461 * @return - The proper value.
Sean Callanan04cc3072009-12-19 02:59:52 +00001462 */
Sean Callanan2f9443f2009-12-22 02:07:42 +00001463GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase, MODRM_REG)
Sean Callanan04cc3072009-12-19 02:59:52 +00001464GENERIC_FIXUP_FUNC(fixupRMValue, insn->eaRegBase, EA_REG)
1465
1466/*
1467 * fixupReg - Consults an operand specifier to determine which of the
1468 * fixup*Value functions to use in correcting readModRM()'ss interpretation.
1469 *
1470 * @param insn - See fixup*Value().
1471 * @param op - The operand specifier.
1472 * @return - 0 if fixup was successful; -1 if the register returned was
1473 * invalid for its class.
1474 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001475static int fixupReg(struct InternalInstruction *insn,
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +00001476 const struct OperandSpecifier *op) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001477 uint8_t valid;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001478
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001479 dbgprintf(insn, "fixupReg()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001480
Sean Callanan04cc3072009-12-19 02:59:52 +00001481 switch ((OperandEncoding)op->encoding) {
1482 default:
Sean Callanan010b3732010-04-02 21:23:51 +00001483 debug("Expected a REG or R/M encoding in fixupReg");
1484 return -1;
Sean Callananc3fd5232011-03-15 01:23:15 +00001485 case ENCODING_VVVV:
1486 insn->vvvv = (Reg)fixupRegValue(insn,
1487 (OperandType)op->type,
1488 insn->vvvv,
1489 &valid);
1490 if (!valid)
1491 return -1;
1492 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001493 case ENCODING_REG:
1494 insn->reg = (Reg)fixupRegValue(insn,
1495 (OperandType)op->type,
1496 insn->reg - insn->regBase,
1497 &valid);
1498 if (!valid)
1499 return -1;
1500 break;
1501 case ENCODING_RM:
1502 if (insn->eaBase >= insn->eaRegBase) {
1503 insn->eaBase = (EABase)fixupRMValue(insn,
1504 (OperandType)op->type,
1505 insn->eaBase - insn->eaRegBase,
1506 &valid);
1507 if (!valid)
1508 return -1;
1509 }
1510 break;
1511 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001512
Sean Callanan04cc3072009-12-19 02:59:52 +00001513 return 0;
1514}
1515
1516/*
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001517 * readOpcodeRegister - Reads an operand from the opcode field of an
Sean Callanan04cc3072009-12-19 02:59:52 +00001518 * instruction and interprets it appropriately given the operand width.
1519 * Handles AddRegFrm instructions.
1520 *
Craig Topper91551182014-01-01 15:29:32 +00001521 * @param insn - the instruction whose opcode field is to be read.
Sean Callanan04cc3072009-12-19 02:59:52 +00001522 * @param size - The width (in bytes) of the register being specified.
1523 * 1 means AL and friends, 2 means AX, 4 means EAX, and 8 means
1524 * RAX.
Sean Callanan010b3732010-04-02 21:23:51 +00001525 * @return - 0 on success; nonzero otherwise.
Sean Callanan04cc3072009-12-19 02:59:52 +00001526 */
Sean Callanan010b3732010-04-02 21:23:51 +00001527static int readOpcodeRegister(struct InternalInstruction* insn, uint8_t size) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001528 dbgprintf(insn, "readOpcodeRegister()");
Sean Callanan04cc3072009-12-19 02:59:52 +00001529
Sean Callanan04cc3072009-12-19 02:59:52 +00001530 if (size == 0)
1531 size = insn->registerSize;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001532
Sean Callanan04cc3072009-12-19 02:59:52 +00001533 switch (size) {
1534 case 1:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001535 insn->opcodeRegister = (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001536 | (insn->opcode & 7)));
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001537 if (insn->rexPrefix &&
Sean Callanan010b3732010-04-02 21:23:51 +00001538 insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1539 insn->opcodeRegister < MODRM_REG_AL + 0x8) {
Sean Callanan2f9443f2009-12-22 02:07:42 +00001540 insn->opcodeRegister = (Reg)(MODRM_REG_SPL
1541 + (insn->opcodeRegister - MODRM_REG_AL - 4));
Sean Callanan04cc3072009-12-19 02:59:52 +00001542 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001543
Sean Callanan04cc3072009-12-19 02:59:52 +00001544 break;
1545 case 2:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001546 insn->opcodeRegister = (Reg)(MODRM_REG_AX
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001547 + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001548 | (insn->opcode & 7)));
Sean Callanan04cc3072009-12-19 02:59:52 +00001549 break;
1550 case 4:
Sean Callanan010b3732010-04-02 21:23:51 +00001551 insn->opcodeRegister = (Reg)(MODRM_REG_EAX
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001552 + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001553 | (insn->opcode & 7)));
Sean Callanan04cc3072009-12-19 02:59:52 +00001554 break;
1555 case 8:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001556 insn->opcodeRegister = (Reg)(MODRM_REG_RAX
1557 + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001558 | (insn->opcode & 7)));
Sean Callanan04cc3072009-12-19 02:59:52 +00001559 break;
1560 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001561
Sean Callanan010b3732010-04-02 21:23:51 +00001562 return 0;
Sean Callanan04cc3072009-12-19 02:59:52 +00001563}
1564
1565/*
1566 * readImmediate - Consumes an immediate operand from an instruction, given the
1567 * desired operand size.
1568 *
1569 * @param insn - The instruction whose operand is to be read.
1570 * @param size - The width (in bytes) of the operand.
1571 * @return - 0 if the immediate was successfully consumed; nonzero
1572 * otherwise.
1573 */
1574static int readImmediate(struct InternalInstruction* insn, uint8_t size) {
1575 uint8_t imm8;
1576 uint16_t imm16;
1577 uint32_t imm32;
1578 uint64_t imm64;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001579
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001580 dbgprintf(insn, "readImmediate()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001581
Sean Callanan010b3732010-04-02 21:23:51 +00001582 if (insn->numImmediatesConsumed == 2) {
1583 debug("Already consumed two immediates");
1584 return -1;
1585 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001586
Sean Callanan04cc3072009-12-19 02:59:52 +00001587 if (size == 0)
1588 size = insn->immediateSize;
1589 else
1590 insn->immediateSize = size;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +00001591 insn->immediateOffset = insn->readerCursor - insn->startLocation;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001592
Sean Callanan04cc3072009-12-19 02:59:52 +00001593 switch (size) {
1594 case 1:
1595 if (consumeByte(insn, &imm8))
1596 return -1;
1597 insn->immediates[insn->numImmediatesConsumed] = imm8;
1598 break;
1599 case 2:
1600 if (consumeUInt16(insn, &imm16))
1601 return -1;
1602 insn->immediates[insn->numImmediatesConsumed] = imm16;
1603 break;
1604 case 4:
1605 if (consumeUInt32(insn, &imm32))
1606 return -1;
1607 insn->immediates[insn->numImmediatesConsumed] = imm32;
1608 break;
1609 case 8:
1610 if (consumeUInt64(insn, &imm64))
1611 return -1;
1612 insn->immediates[insn->numImmediatesConsumed] = imm64;
1613 break;
1614 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001615
Sean Callanan04cc3072009-12-19 02:59:52 +00001616 insn->numImmediatesConsumed++;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001617
Sean Callanan04cc3072009-12-19 02:59:52 +00001618 return 0;
1619}
1620
1621/*
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001622 * readVVVV - Consumes vvvv from an instruction if it has a VEX prefix.
Sean Callananc3fd5232011-03-15 01:23:15 +00001623 *
1624 * @param insn - The instruction whose operand is to be read.
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001625 * @return - 0 if the vvvv was successfully consumed; nonzero
Sean Callananc3fd5232011-03-15 01:23:15 +00001626 * otherwise.
1627 */
1628static int readVVVV(struct InternalInstruction* insn) {
1629 dbgprintf(insn, "readVVVV()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001630
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001631 if (insn->vectorExtensionType == TYPE_EVEX)
1632 insn->vvvv = vvvvFromEVEX3of4(insn->vectorExtensionPrefix[2]);
1633 else if (insn->vectorExtensionType == TYPE_VEX_3B)
1634 insn->vvvv = vvvvFromVEX3of3(insn->vectorExtensionPrefix[2]);
1635 else if (insn->vectorExtensionType == TYPE_VEX_2B)
1636 insn->vvvv = vvvvFromVEX2of2(insn->vectorExtensionPrefix[1]);
1637 else if (insn->vectorExtensionType == TYPE_XOP)
1638 insn->vvvv = vvvvFromXOP3of3(insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +00001639 else
1640 return -1;
1641
Craig Topper0d0be472011-10-03 08:14:29 +00001642 if (insn->mode != MODE_64BIT)
1643 insn->vvvv &= 0x7;
1644
Sean Callananc3fd5232011-03-15 01:23:15 +00001645 return 0;
1646}
1647
1648/*
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001649 * readMaskRegister - Reads an mask register from the opcode field of an
1650 * instruction.
1651 *
1652 * @param insn - The instruction whose opcode field is to be read.
1653 * @return - 0 on success; nonzero otherwise.
1654 */
1655static int readMaskRegister(struct InternalInstruction* insn) {
1656 dbgprintf(insn, "readMaskRegister()");
1657
1658 if (insn->vectorExtensionType != TYPE_EVEX)
1659 return -1;
1660
1661 insn->writemask = aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]);
1662 return 0;
1663}
1664
1665/*
Sean Callanan04cc3072009-12-19 02:59:52 +00001666 * readOperands - Consults the specifier for an instruction and consumes all
1667 * operands for that instruction, interpreting them as it goes.
1668 *
1669 * @param insn - The instruction whose operands are to be read and interpreted.
1670 * @return - 0 if all operands could be read; nonzero otherwise.
1671 */
1672static int readOperands(struct InternalInstruction* insn) {
1673 int index;
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001674 int hasVVVV, needVVVV;
Craig Topper2ba766a2011-12-30 06:23:39 +00001675 int sawRegImm = 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001676
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001677 dbgprintf(insn, "readOperands()");
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001678
1679 /* If non-zero vvvv specified, need to make sure one of the operands
1680 uses it. */
1681 hasVVVV = !readVVVV(insn);
1682 needVVVV = hasVVVV && (insn->vvvv != 0);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001683
Sean Callanan04cc3072009-12-19 02:59:52 +00001684 for (index = 0; index < X86_MAX_OPERANDS; ++index) {
Craig Topperb8aec082012-08-01 07:39:18 +00001685 switch (x86OperandSets[insn->spec->operands][index].encoding) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001686 case ENCODING_NONE:
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00001687 case ENCODING_SI:
David Woodhouseb33c2ef2014-01-22 15:08:21 +00001688 case ENCODING_DI:
Sean Callanan04cc3072009-12-19 02:59:52 +00001689 break;
1690 case ENCODING_REG:
1691 case ENCODING_RM:
1692 if (readModRM(insn))
1693 return -1;
Craig Topperb8aec082012-08-01 07:39:18 +00001694 if (fixupReg(insn, &x86OperandSets[insn->spec->operands][index]))
Sean Callanan04cc3072009-12-19 02:59:52 +00001695 return -1;
1696 break;
1697 case ENCODING_CB:
1698 case ENCODING_CW:
1699 case ENCODING_CD:
1700 case ENCODING_CP:
1701 case ENCODING_CO:
1702 case ENCODING_CT:
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001703 dbgprintf(insn, "We currently don't hande code-offset encodings");
Sean Callanan04cc3072009-12-19 02:59:52 +00001704 return -1;
1705 case ENCODING_IB:
Craig Topper2ba766a2011-12-30 06:23:39 +00001706 if (sawRegImm) {
Benjamin Kramer9c48f262012-01-04 22:06:45 +00001707 /* Saw a register immediate so don't read again and instead split the
1708 previous immediate. FIXME: This is a hack. */
Benjamin Kramer47aecca2012-01-01 17:55:36 +00001709 insn->immediates[insn->numImmediatesConsumed] =
1710 insn->immediates[insn->numImmediatesConsumed - 1] & 0xf;
1711 ++insn->numImmediatesConsumed;
Craig Topper2ba766a2011-12-30 06:23:39 +00001712 break;
1713 }
Sean Callanan04cc3072009-12-19 02:59:52 +00001714 if (readImmediate(insn, 1))
1715 return -1;
Craig Topperb8aec082012-08-01 07:39:18 +00001716 if (x86OperandSets[insn->spec->operands][index].type == TYPE_IMM3 &&
Sean Callanan1efe6612010-04-07 21:42:19 +00001717 insn->immediates[insn->numImmediatesConsumed - 1] > 7)
1718 return -1;
Craig Topperb8aec082012-08-01 07:39:18 +00001719 if (x86OperandSets[insn->spec->operands][index].type == TYPE_IMM5 &&
Craig Topper7629d632012-04-03 05:20:24 +00001720 insn->immediates[insn->numImmediatesConsumed - 1] > 31)
1721 return -1;
Craig Topperb8aec082012-08-01 07:39:18 +00001722 if (x86OperandSets[insn->spec->operands][index].type == TYPE_XMM128 ||
1723 x86OperandSets[insn->spec->operands][index].type == TYPE_XMM256)
Craig Topper2ba766a2011-12-30 06:23:39 +00001724 sawRegImm = 1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001725 break;
1726 case ENCODING_IW:
1727 if (readImmediate(insn, 2))
1728 return -1;
1729 break;
1730 case ENCODING_ID:
1731 if (readImmediate(insn, 4))
1732 return -1;
1733 break;
1734 case ENCODING_IO:
1735 if (readImmediate(insn, 8))
1736 return -1;
1737 break;
1738 case ENCODING_Iv:
Sean Callanan010b3732010-04-02 21:23:51 +00001739 if (readImmediate(insn, insn->immediateSize))
1740 return -1;
Chris Lattnerd4758fc2010-04-16 21:15:15 +00001741 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001742 case ENCODING_Ia:
Sean Callanan010b3732010-04-02 21:23:51 +00001743 if (readImmediate(insn, insn->addressSize))
1744 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001745 break;
1746 case ENCODING_RB:
Sean Callanan010b3732010-04-02 21:23:51 +00001747 if (readOpcodeRegister(insn, 1))
1748 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001749 break;
1750 case ENCODING_RW:
Sean Callanan010b3732010-04-02 21:23:51 +00001751 if (readOpcodeRegister(insn, 2))
1752 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001753 break;
1754 case ENCODING_RD:
Sean Callanan010b3732010-04-02 21:23:51 +00001755 if (readOpcodeRegister(insn, 4))
1756 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001757 break;
1758 case ENCODING_RO:
Sean Callanan010b3732010-04-02 21:23:51 +00001759 if (readOpcodeRegister(insn, 8))
1760 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001761 break;
1762 case ENCODING_Rv:
Sean Callanan010b3732010-04-02 21:23:51 +00001763 if (readOpcodeRegister(insn, 0))
1764 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001765 break;
Craig Topper623b0d62014-01-01 14:22:37 +00001766 case ENCODING_FP:
Sean Callananc3fd5232011-03-15 01:23:15 +00001767 break;
1768 case ENCODING_VVVV:
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001769 needVVVV = 0; /* Mark that we have found a VVVV operand. */
1770 if (!hasVVVV)
Sean Callananc3fd5232011-03-15 01:23:15 +00001771 return -1;
Craig Topperb8aec082012-08-01 07:39:18 +00001772 if (fixupReg(insn, &x86OperandSets[insn->spec->operands][index]))
Sean Callananc3fd5232011-03-15 01:23:15 +00001773 return -1;
1774 break;
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001775 case ENCODING_WRITEMASK:
1776 if (readMaskRegister(insn))
1777 return -1;
1778 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001779 case ENCODING_DUP:
1780 break;
1781 default:
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001782 dbgprintf(insn, "Encountered an operand with an unknown encoding.");
Sean Callanan04cc3072009-12-19 02:59:52 +00001783 return -1;
1784 }
1785 }
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001786
1787 /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */
1788 if (needVVVV) return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001789
Sean Callanan04cc3072009-12-19 02:59:52 +00001790 return 0;
1791}
1792
1793/*
1794 * decodeInstruction - Reads and interprets a full instruction provided by the
1795 * user.
1796 *
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001797 * @param insn - A pointer to the instruction to be populated. Must be
Sean Callanan04cc3072009-12-19 02:59:52 +00001798 * pre-allocated.
1799 * @param reader - The function to be used to read the instruction's bytes.
1800 * @param readerArg - A generic argument to be passed to the reader to store
1801 * any internal state.
1802 * @param logger - If non-NULL, the function to be used to write log messages
1803 * and warnings.
1804 * @param loggerArg - A generic argument to be passed to the logger to store
1805 * any internal state.
1806 * @param startLoc - The address (in the reader's address space) of the first
1807 * byte in the instruction.
1808 * @param mode - The mode (real mode, IA-32e, or IA-32e in 64-bit mode) to
1809 * decode the instruction in.
1810 * @return - 0 if the instruction's memory could be read; nonzero if
1811 * not.
1812 */
1813int decodeInstruction(struct InternalInstruction* insn,
1814 byteReader_t reader,
Roman Divacky67923802012-09-05 21:17:34 +00001815 const void* readerArg,
Sean Callanan04cc3072009-12-19 02:59:52 +00001816 dlog_t logger,
1817 void* loggerArg,
Roman Divacky67923802012-09-05 21:17:34 +00001818 const void* miiArg,
Sean Callanan04cc3072009-12-19 02:59:52 +00001819 uint64_t startLoc,
1820 DisassemblerMode mode) {
Daniel Dunbarc745a622009-12-19 03:31:50 +00001821 memset(insn, 0, sizeof(struct InternalInstruction));
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001822
Sean Callanan04cc3072009-12-19 02:59:52 +00001823 insn->reader = reader;
1824 insn->readerArg = readerArg;
1825 insn->dlog = logger;
1826 insn->dlogArg = loggerArg;
1827 insn->startLocation = startLoc;
1828 insn->readerCursor = startLoc;
1829 insn->mode = mode;
1830 insn->numImmediatesConsumed = 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001831
Sean Callanan04cc3072009-12-19 02:59:52 +00001832 if (readPrefixes(insn) ||
1833 readOpcode(insn) ||
Benjamin Kramer478e8de2012-02-11 14:50:54 +00001834 getID(insn, miiArg) ||
Sean Callanan04cc3072009-12-19 02:59:52 +00001835 insn->instructionID == 0 ||
1836 readOperands(insn))
1837 return -1;
Craig Topperb8aec082012-08-01 07:39:18 +00001838
1839 insn->operands = &x86OperandSets[insn->spec->operands][0];
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001840
Sean Callanan04cc3072009-12-19 02:59:52 +00001841 insn->length = insn->readerCursor - insn->startLocation;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001842
Benjamin Kramer4f672272010-03-18 12:18:36 +00001843 dbgprintf(insn, "Read from 0x%llx to 0x%llx: length %zu",
1844 startLoc, insn->readerCursor, insn->length);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001845
Sean Callanan04cc3072009-12-19 02:59:52 +00001846 if (insn->length > 15)
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001847 dbgprintf(insn, "Instruction exceeds 15-byte limit");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001848
Sean Callanan04cc3072009-12-19 02:59:52 +00001849 return 0;
1850}