blob: 49e64032ad76aefa581274c85b369f3d54c1652b [file] [log] [blame]
Craig Topperabfe07e2014-10-07 07:29:46 +00001//===-- X86DisassemblerDecoder.cpp - Disassembler decoder -----------------===//
Richard Smith89ee75d2014-04-20 21:07:34 +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//===----------------------------------------------------------------------===//
Sean Callanan04cc3072009-12-19 02:59:52 +000015
Craig Topperabfe07e2014-10-07 07:29:46 +000016#include <cstdarg> /* for va_*() */
17#include <cstdio> /* for vsnprintf() */
18#include <cstdlib> /* for exit() */
19#include <cstring> /* for memset() */
Sean Callanan04cc3072009-12-19 02:59:52 +000020
21#include "X86DisassemblerDecoder.h"
22
Richard Smith89ee75d2014-04-20 21:07:34 +000023using namespace llvm::X86Disassembler;
24
Richard Smithac15f1c2014-04-20 21:52:16 +000025/// Specifies whether a ModR/M byte is needed and (if so) which
26/// instruction each possible value of the ModR/M byte corresponds to. Once
27/// this information is known, we have narrowed down to a single instruction.
28struct ModRMDecision {
29 uint8_t modrm_type;
30 uint16_t instructionIDs;
31};
32
33/// Specifies which set of ModR/M->instruction tables to look at
34/// given a particular opcode.
35struct OpcodeDecision {
36 ModRMDecision modRMDecisions[256];
37};
38
39/// Specifies which opcode->instruction tables to look at given
40/// a particular context (set of attributes). Since there are many possible
41/// contexts, the decoder first uses CONTEXTS_SYM to determine which context
42/// applies given a specific set of attributes. Hence there are only IC_max
43/// entries in this table, rather than 2^(ATTR_max).
44struct ContextDecision {
45 OpcodeDecision opcodeDecisions[IC_max];
46};
47
Sean Callanan04cc3072009-12-19 02:59:52 +000048#include "X86GenDisassemblerTables.inc"
49
Sean Callanan010b3732010-04-02 21:23:51 +000050#ifndef NDEBUG
Richard Smith89ee75d2014-04-20 21:07:34 +000051#define debug(s) do { Debug(__FILE__, __LINE__, s); } while (0)
Sean Callanan010b3732010-04-02 21:23:51 +000052#else
53#define debug(s) do { } while (0)
54#endif
55
Sean Callanan04cc3072009-12-19 02:59:52 +000056
57/*
58 * contextForAttrs - Client for the instruction context table. Takes a set of
59 * attributes and returns the appropriate decode context.
60 *
61 * @param attrMask - Attributes, from the enumeration attributeBits.
62 * @return - The InstructionContext to use when looking up an
63 * an instruction with these attributes.
64 */
Elena Demikhovsky371e3632013-12-25 11:40:51 +000065static InstructionContext contextForAttrs(uint16_t attrMask) {
Richard Smith89ee75d2014-04-20 21:07:34 +000066 return static_cast<InstructionContext>(CONTEXTS_SYM[attrMask]);
Sean Callanan04cc3072009-12-19 02:59:52 +000067}
68
69/*
70 * modRMRequired - Reads the appropriate instruction table to determine whether
71 * the ModR/M byte is required to decode a particular instruction.
72 *
73 * @param type - The opcode type (i.e., how many bytes it has).
74 * @param insnContext - The context for the instruction, as returned by
75 * contextForAttrs.
76 * @param opcode - The last byte of the instruction's opcode, not counting
77 * ModR/M extensions and escapes.
Richard Smith5d5061032014-04-20 22:15:37 +000078 * @return - true if the ModR/M byte is required, false otherwise.
Sean Callanan04cc3072009-12-19 02:59:52 +000079 */
Sean Callanan588785c2009-12-22 22:51:40 +000080static int modRMRequired(OpcodeType type,
Craig Topper21c33652011-10-02 16:56:09 +000081 InstructionContext insnContext,
Elena Demikhovsky371e3632013-12-25 11:40:51 +000082 uint16_t opcode) {
Craig Toppere73658d2014-04-28 04:05:08 +000083 const struct ContextDecision* decision = nullptr;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +000084
Sean Callanan04cc3072009-12-19 02:59:52 +000085 switch (type) {
86 case ONEBYTE:
87 decision = &ONEBYTE_SYM;
88 break;
89 case TWOBYTE:
90 decision = &TWOBYTE_SYM;
91 break;
92 case THREEBYTE_38:
93 decision = &THREEBYTE38_SYM;
94 break;
95 case THREEBYTE_3A:
96 decision = &THREEBYTE3A_SYM;
97 break;
Craig Topper9e3e38a2013-10-03 05:17:48 +000098 case XOP8_MAP:
99 decision = &XOP8_MAP_SYM;
100 break;
101 case XOP9_MAP:
102 decision = &XOP9_MAP_SYM;
103 break;
104 case XOPA_MAP:
105 decision = &XOPA_MAP_SYM;
106 break;
Sean Callanan04cc3072009-12-19 02:59:52 +0000107 }
Ahmed Charles636a3d62012-02-19 11:37:01 +0000108
Sean Callanan04cc3072009-12-19 02:59:52 +0000109 return decision->opcodeDecisions[insnContext].modRMDecisions[opcode].
110 modrm_type != MODRM_ONEENTRY;
Sean Callanan04cc3072009-12-19 02:59:52 +0000111}
112
113/*
114 * decode - Reads the appropriate instruction table to obtain the unique ID of
115 * an instruction.
116 *
117 * @param type - See modRMRequired().
118 * @param insnContext - See modRMRequired().
119 * @param opcode - See modRMRequired().
120 * @param modRM - The ModR/M byte if required, or any value if not.
Sean Callanan010b3732010-04-02 21:23:51 +0000121 * @return - The UID of the instruction, or 0 on failure.
Sean Callanan04cc3072009-12-19 02:59:52 +0000122 */
Sean Callanan588785c2009-12-22 22:51:40 +0000123static InstrUID decode(OpcodeType type,
Sean Callanan010b3732010-04-02 21:23:51 +0000124 InstructionContext insnContext,
125 uint8_t opcode,
126 uint8_t modRM) {
Craig Toppere73658d2014-04-28 04:05:08 +0000127 const struct ModRMDecision* dec = nullptr;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000128
Sean Callanan04cc3072009-12-19 02:59:52 +0000129 switch (type) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000130 case ONEBYTE:
131 dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
132 break;
133 case TWOBYTE:
134 dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
135 break;
136 case THREEBYTE_38:
137 dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
138 break;
139 case THREEBYTE_3A:
140 dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
141 break;
Craig Topper9e3e38a2013-10-03 05:17:48 +0000142 case XOP8_MAP:
143 dec = &XOP8_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
144 break;
145 case XOP9_MAP:
146 dec = &XOP9_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
147 break;
148 case XOPA_MAP:
149 dec = &XOPA_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
150 break;
Sean Callanan04cc3072009-12-19 02:59:52 +0000151 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000152
Sean Callanan04cc3072009-12-19 02:59:52 +0000153 switch (dec->modrm_type) {
154 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000155 debug("Corrupt table! Unknown modrm_type");
156 return 0;
Sean Callanan04cc3072009-12-19 02:59:52 +0000157 case MODRM_ONEENTRY:
Craig Topper487e7442012-02-09 07:45:30 +0000158 return modRMTable[dec->instructionIDs];
Sean Callanan04cc3072009-12-19 02:59:52 +0000159 case MODRM_SPLITRM:
160 if (modFromModRM(modRM) == 0x3)
Craig Topper487e7442012-02-09 07:45:30 +0000161 return modRMTable[dec->instructionIDs+1];
162 return modRMTable[dec->instructionIDs];
Craig Toppera0cd9702012-02-09 08:58:07 +0000163 case MODRM_SPLITREG:
164 if (modFromModRM(modRM) == 0x3)
165 return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)+8];
166 return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)];
Craig Topper963305b2012-09-13 05:45:42 +0000167 case MODRM_SPLITMISC:
168 if (modFromModRM(modRM) == 0x3)
169 return modRMTable[dec->instructionIDs+(modRM & 0x3f)+8];
170 return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)];
Sean Callanan04cc3072009-12-19 02:59:52 +0000171 case MODRM_FULL:
Craig Topper487e7442012-02-09 07:45:30 +0000172 return modRMTable[dec->instructionIDs+modRM];
Sean Callanan04cc3072009-12-19 02:59:52 +0000173 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000174}
175
176/*
177 * specifierForUID - Given a UID, returns the name and operand specification for
178 * that instruction.
179 *
180 * @param uid - The unique ID for the instruction. This should be returned by
181 * decode(); specifierForUID will not check bounds.
182 * @return - A pointer to the specification for that instruction.
183 */
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +0000184static const struct InstructionSpecifier *specifierForUID(InstrUID uid) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000185 return &INSTRUCTIONS_SYM[uid];
186}
187
188/*
189 * consumeByte - Uses the reader function provided by the user to consume one
190 * byte from the instruction's memory and advance the cursor.
191 *
192 * @param insn - The instruction with the reader function to use. The cursor
193 * for this instruction is advanced.
194 * @param byte - A pointer to a pre-allocated memory buffer to be populated
195 * with the data read.
196 * @return - 0 if the read was successful; nonzero otherwise.
197 */
Sean Callanan588785c2009-12-22 22:51:40 +0000198static int consumeByte(struct InternalInstruction* insn, uint8_t* byte) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000199 int ret = insn->reader(insn->readerArg, byte, insn->readerCursor);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000200
Sean Callanan04cc3072009-12-19 02:59:52 +0000201 if (!ret)
202 ++(insn->readerCursor);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000203
Sean Callanan04cc3072009-12-19 02:59:52 +0000204 return ret;
205}
206
207/*
208 * lookAtByte - Like consumeByte, but does not advance the cursor.
209 *
210 * @param insn - See consumeByte().
211 * @param byte - See consumeByte().
212 * @return - See consumeByte().
213 */
Sean Callanan588785c2009-12-22 22:51:40 +0000214static int lookAtByte(struct InternalInstruction* insn, uint8_t* byte) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000215 return insn->reader(insn->readerArg, byte, insn->readerCursor);
216}
217
Sean Callanan588785c2009-12-22 22:51:40 +0000218static void unconsumeByte(struct InternalInstruction* insn) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000219 insn->readerCursor--;
220}
221
Sean Callanan588785c2009-12-22 22:51:40 +0000222#define CONSUME_FUNC(name, type) \
223 static int name(struct InternalInstruction* insn, type* ptr) { \
224 type combined = 0; \
225 unsigned offset; \
226 for (offset = 0; offset < sizeof(type); ++offset) { \
227 uint8_t byte; \
228 int ret = insn->reader(insn->readerArg, \
229 &byte, \
230 insn->readerCursor + offset); \
231 if (ret) \
232 return ret; \
Richard Smith228e6d42012-08-24 23:29:28 +0000233 combined = combined | ((uint64_t)byte << (offset * 8)); \
Sean Callanan588785c2009-12-22 22:51:40 +0000234 } \
235 *ptr = combined; \
236 insn->readerCursor += sizeof(type); \
237 return 0; \
Sean Callanan04cc3072009-12-19 02:59:52 +0000238 }
239
240/*
241 * consume* - Use the reader function provided by the user to consume data
242 * values of various sizes from the instruction's memory and advance the
243 * cursor appropriately. These readers perform endian conversion.
244 *
245 * @param insn - See consumeByte().
246 * @param ptr - A pointer to a pre-allocated memory of appropriate size to
247 * be populated with the data read.
248 * @return - See consumeByte().
249 */
250CONSUME_FUNC(consumeInt8, int8_t)
251CONSUME_FUNC(consumeInt16, int16_t)
252CONSUME_FUNC(consumeInt32, int32_t)
253CONSUME_FUNC(consumeUInt16, uint16_t)
254CONSUME_FUNC(consumeUInt32, uint32_t)
255CONSUME_FUNC(consumeUInt64, uint64_t)
256
257/*
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000258 * dbgprintf - Uses the logging function provided by the user to log a single
Sean Callanan04cc3072009-12-19 02:59:52 +0000259 * message, typically without a carriage-return.
260 *
261 * @param insn - The instruction containing the logging function.
262 * @param format - See printf().
263 * @param ... - See printf().
264 */
Sean Callanan588785c2009-12-22 22:51:40 +0000265static void dbgprintf(struct InternalInstruction* insn,
266 const char* format,
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000267 ...) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000268 char buffer[256];
269 va_list ap;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000270
Sean Callanan04cc3072009-12-19 02:59:52 +0000271 if (!insn->dlog)
272 return;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000273
Sean Callanan04cc3072009-12-19 02:59:52 +0000274 va_start(ap, format);
275 (void)vsnprintf(buffer, sizeof(buffer), format, ap);
276 va_end(ap);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000277
Sean Callanan04cc3072009-12-19 02:59:52 +0000278 insn->dlog(insn->dlogArg, buffer);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000279
Sean Callanan04cc3072009-12-19 02:59:52 +0000280 return;
281}
282
283/*
284 * setPrefixPresent - Marks that a particular prefix is present at a particular
285 * location.
286 *
287 * @param insn - The instruction to be marked as having the prefix.
288 * @param prefix - The prefix that is present.
289 * @param location - The location where the prefix is located (in the address
290 * space of the instruction's reader).
291 */
Sean Callanan588785c2009-12-22 22:51:40 +0000292static void setPrefixPresent(struct InternalInstruction* insn,
Sean Callanan04cc3072009-12-19 02:59:52 +0000293 uint8_t prefix,
294 uint64_t location)
295{
296 insn->prefixPresent[prefix] = 1;
297 insn->prefixLocations[prefix] = location;
298}
299
300/*
301 * isPrefixAtLocation - Queries an instruction to determine whether a prefix is
302 * present at a given location.
303 *
304 * @param insn - The instruction to be queried.
305 * @param prefix - The prefix.
306 * @param location - The location to query.
307 * @return - Whether the prefix is at that location.
308 */
Richard Smith5d5061032014-04-20 22:15:37 +0000309static bool isPrefixAtLocation(struct InternalInstruction* insn,
Sean Callanan588785c2009-12-22 22:51:40 +0000310 uint8_t prefix,
311 uint64_t location)
Sean Callanan04cc3072009-12-19 02:59:52 +0000312{
313 if (insn->prefixPresent[prefix] == 1 &&
314 insn->prefixLocations[prefix] == location)
Richard Smith5d5061032014-04-20 22:15:37 +0000315 return true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000316 else
Richard Smith5d5061032014-04-20 22:15:37 +0000317 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000318}
319
320/*
321 * readPrefixes - Consumes all of an instruction's prefix bytes, and marks the
322 * instruction as having them. Also sets the instruction's default operand,
323 * address, and other relevant data sizes to report operands correctly.
324 *
325 * @param insn - The instruction whose prefixes are to be read.
326 * @return - 0 if the instruction could be read until the end of the prefix
327 * bytes, and no prefixes conflicted; nonzero otherwise.
328 */
329static int readPrefixes(struct InternalInstruction* insn) {
Richard Smith5d5061032014-04-20 22:15:37 +0000330 bool isPrefix = true;
331 bool prefixGroups[4] = { false };
Sean Callanan04cc3072009-12-19 02:59:52 +0000332 uint64_t prefixLocation;
Ted Kremenek3c4408c2011-01-23 17:05:06 +0000333 uint8_t byte = 0;
Richard Mitton79917a92013-08-30 21:32:42 +0000334 uint8_t nextByte;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000335
Richard Smith5d5061032014-04-20 22:15:37 +0000336 bool hasAdSize = false;
337 bool hasOpSize = false;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000338
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000339 dbgprintf(insn, "readPrefixes()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000340
Sean Callanan04cc3072009-12-19 02:59:52 +0000341 while (isPrefix) {
342 prefixLocation = insn->readerCursor;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000343
Richard Mitton576ee002013-08-30 21:19:48 +0000344 /* If we fail reading prefixes, just stop here and let the opcode reader deal with it */
Sean Callanan04cc3072009-12-19 02:59:52 +0000345 if (consumeByte(insn, &byte))
Richard Mitton576ee002013-08-30 21:19:48 +0000346 break;
Kevin Enderby014e1cd2012-03-09 17:52:49 +0000347
Benjamin Krameradfc73d2012-03-10 15:10:06 +0000348 /*
Dave Zarzycki07fabee2013-03-25 18:59:38 +0000349 * If the byte is a LOCK/REP/REPNE prefix and not a part of the opcode, then
350 * break and let it be disassembled as a normal "instruction".
Benjamin Krameradfc73d2012-03-10 15:10:06 +0000351 */
Richard Mitton576ee002013-08-30 21:19:48 +0000352 if (insn->readerCursor - 1 == insn->startLocation && byte == 0xf0)
353 break;
354
Dave Zarzycki07fabee2013-03-25 18:59:38 +0000355 if (insn->readerCursor - 1 == insn->startLocation
Richard Mitton576ee002013-08-30 21:19:48 +0000356 && (byte == 0xf2 || byte == 0xf3)
357 && !lookAtByte(insn, &nextByte))
358 {
Kevin Enderby35fd7922013-06-20 22:32:18 +0000359 /*
360 * If the byte is 0xf2 or 0xf3, and any of the following conditions are
361 * met:
362 * - it is followed by a LOCK (0xf0) prefix
363 * - it is followed by an xchg instruction
364 * then it should be disassembled as a xacquire/xrelease not repne/rep.
365 */
366 if ((byte == 0xf2 || byte == 0xf3) &&
367 ((nextByte == 0xf0) |
368 ((nextByte & 0xfe) == 0x86 || (nextByte & 0xf8) == 0x90)))
Richard Smith5d5061032014-04-20 22:15:37 +0000369 insn->xAcquireRelease = true;
Kevin Enderby35fd7922013-06-20 22:32:18 +0000370 /*
371 * Also if the byte is 0xf3, and the following condition is met:
372 * - it is followed by a "mov mem, reg" (opcode 0x88/0x89) or
373 * "mov mem, imm" (opcode 0xc6/0xc7) instructions.
374 * then it should be disassembled as an xrelease not rep.
375 */
376 if (byte == 0xf3 &&
377 (nextByte == 0x88 || nextByte == 0x89 ||
378 nextByte == 0xc6 || nextByte == 0xc7))
Richard Smith5d5061032014-04-20 22:15:37 +0000379 insn->xAcquireRelease = true;
Dave Zarzycki07fabee2013-03-25 18:59:38 +0000380 if (insn->mode == MODE_64BIT && (nextByte & 0xf0) == 0x40) {
381 if (consumeByte(insn, &nextByte))
382 return -1;
383 if (lookAtByte(insn, &nextByte))
384 return -1;
385 unconsumeByte(insn);
386 }
387 if (nextByte != 0x0f && nextByte != 0x90)
388 break;
389 }
390
Sean Callanan04cc3072009-12-19 02:59:52 +0000391 switch (byte) {
392 case 0xf0: /* LOCK */
393 case 0xf2: /* REPNE/REPNZ */
394 case 0xf3: /* REP or REPE/REPZ */
395 if (prefixGroups[0])
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000396 dbgprintf(insn, "Redundant Group 1 prefix");
Richard Smith5d5061032014-04-20 22:15:37 +0000397 prefixGroups[0] = true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000398 setPrefixPresent(insn, byte, prefixLocation);
399 break;
400 case 0x2e: /* CS segment override -OR- Branch not taken */
401 case 0x36: /* SS segment override -OR- Branch taken */
402 case 0x3e: /* DS segment override */
403 case 0x26: /* ES segment override */
404 case 0x64: /* FS segment override */
405 case 0x65: /* GS segment override */
406 switch (byte) {
407 case 0x2e:
408 insn->segmentOverride = SEG_OVERRIDE_CS;
409 break;
410 case 0x36:
411 insn->segmentOverride = SEG_OVERRIDE_SS;
412 break;
413 case 0x3e:
414 insn->segmentOverride = SEG_OVERRIDE_DS;
415 break;
416 case 0x26:
417 insn->segmentOverride = SEG_OVERRIDE_ES;
418 break;
419 case 0x64:
420 insn->segmentOverride = SEG_OVERRIDE_FS;
421 break;
422 case 0x65:
423 insn->segmentOverride = SEG_OVERRIDE_GS;
424 break;
425 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000426 debug("Unhandled override");
427 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +0000428 }
429 if (prefixGroups[1])
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000430 dbgprintf(insn, "Redundant Group 2 prefix");
Richard Smith5d5061032014-04-20 22:15:37 +0000431 prefixGroups[1] = true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000432 setPrefixPresent(insn, byte, prefixLocation);
433 break;
434 case 0x66: /* Operand-size override */
435 if (prefixGroups[2])
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000436 dbgprintf(insn, "Redundant Group 3 prefix");
Richard Smith5d5061032014-04-20 22:15:37 +0000437 prefixGroups[2] = true;
438 hasOpSize = true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000439 setPrefixPresent(insn, byte, prefixLocation);
440 break;
441 case 0x67: /* Address-size override */
442 if (prefixGroups[3])
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000443 dbgprintf(insn, "Redundant Group 4 prefix");
Richard Smith5d5061032014-04-20 22:15:37 +0000444 prefixGroups[3] = true;
445 hasAdSize = true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000446 setPrefixPresent(insn, byte, prefixLocation);
447 break;
448 default: /* Not a prefix byte */
Richard Smith5d5061032014-04-20 22:15:37 +0000449 isPrefix = false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000450 break;
451 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000452
Sean Callanan04cc3072009-12-19 02:59:52 +0000453 if (isPrefix)
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000454 dbgprintf(insn, "Found prefix 0x%hhx", byte);
Sean Callanan04cc3072009-12-19 02:59:52 +0000455 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000456
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000457 insn->vectorExtensionType = TYPE_NO_VEX_XOP;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000458
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000459 if (byte == 0x62) {
460 uint8_t byte1, byte2;
461
462 if (consumeByte(insn, &byte1)) {
463 dbgprintf(insn, "Couldn't read second byte of EVEX prefix");
464 return -1;
465 }
466
467 if (lookAtByte(insn, &byte2)) {
468 dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
469 return -1;
470 }
471
472 if ((insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) &&
473 ((~byte1 & 0xc) == 0xc) && ((byte2 & 0x4) == 0x4)) {
474 insn->vectorExtensionType = TYPE_EVEX;
Craig Topper273515e2014-10-07 07:29:48 +0000475 } else {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000476 unconsumeByte(insn); /* unconsume byte1 */
477 unconsumeByte(insn); /* unconsume byte */
478 insn->necessaryPrefixLocation = insn->readerCursor - 2;
479 }
480
481 if (insn->vectorExtensionType == TYPE_EVEX) {
482 insn->vectorExtensionPrefix[0] = byte;
483 insn->vectorExtensionPrefix[1] = byte1;
484 if (consumeByte(insn, &insn->vectorExtensionPrefix[2])) {
485 dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
486 return -1;
487 }
488 if (consumeByte(insn, &insn->vectorExtensionPrefix[3])) {
489 dbgprintf(insn, "Couldn't read fourth byte of EVEX prefix");
490 return -1;
491 }
492
493 /* We simulate the REX prefix for simplicity's sake */
494 if (insn->mode == MODE_64BIT) {
495 insn->rexPrefix = 0x40
496 | (wFromEVEX3of4(insn->vectorExtensionPrefix[2]) << 3)
497 | (rFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 2)
498 | (xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 1)
499 | (bFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 0);
500 }
501
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000502 dbgprintf(insn, "Found EVEX prefix 0x%hhx 0x%hhx 0x%hhx 0x%hhx",
503 insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
504 insn->vectorExtensionPrefix[2], insn->vectorExtensionPrefix[3]);
505 }
Craig Topper273515e2014-10-07 07:29:48 +0000506 } else if (byte == 0xc4) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000507 uint8_t byte1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000508
Sean Callananc3fd5232011-03-15 01:23:15 +0000509 if (lookAtByte(insn, &byte1)) {
510 dbgprintf(insn, "Couldn't read second byte of VEX");
511 return -1;
512 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000513
Craig Topper45faba92011-09-26 05:12:43 +0000514 if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000515 insn->vectorExtensionType = TYPE_VEX_3B;
Sean Callananc3fd5232011-03-15 01:23:15 +0000516 insn->necessaryPrefixLocation = insn->readerCursor - 1;
Craig Topper273515e2014-10-07 07:29:48 +0000517 } else {
Sean Callanan04cc3072009-12-19 02:59:52 +0000518 unconsumeByte(insn);
519 insn->necessaryPrefixLocation = insn->readerCursor - 1;
520 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000521
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000522 if (insn->vectorExtensionType == TYPE_VEX_3B) {
523 insn->vectorExtensionPrefix[0] = byte;
524 consumeByte(insn, &insn->vectorExtensionPrefix[1]);
525 consumeByte(insn, &insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +0000526
527 /* We simulate the REX prefix for simplicity's sake */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000528
Craig Topper31854ba2011-10-03 07:51:09 +0000529 if (insn->mode == MODE_64BIT) {
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000530 insn->rexPrefix = 0x40
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000531 | (wFromVEX3of3(insn->vectorExtensionPrefix[2]) << 3)
532 | (rFromVEX2of3(insn->vectorExtensionPrefix[1]) << 2)
533 | (xFromVEX2of3(insn->vectorExtensionPrefix[1]) << 1)
534 | (bFromVEX2of3(insn->vectorExtensionPrefix[1]) << 0);
Craig Topper31854ba2011-10-03 07:51:09 +0000535 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000536
Craig Topper9e3e38a2013-10-03 05:17:48 +0000537 dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx 0x%hhx",
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000538 insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
539 insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +0000540 }
Craig Topper273515e2014-10-07 07:29:48 +0000541 } else if (byte == 0xc5) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000542 uint8_t byte1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000543
Sean Callananc3fd5232011-03-15 01:23:15 +0000544 if (lookAtByte(insn, &byte1)) {
545 dbgprintf(insn, "Couldn't read second byte of VEX");
546 return -1;
547 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000548
Craig Topper45faba92011-09-26 05:12:43 +0000549 if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000550 insn->vectorExtensionType = TYPE_VEX_2B;
Craig Topper273515e2014-10-07 07:29:48 +0000551 } else {
Sean Callananc3fd5232011-03-15 01:23:15 +0000552 unconsumeByte(insn);
553 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000554
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000555 if (insn->vectorExtensionType == TYPE_VEX_2B) {
556 insn->vectorExtensionPrefix[0] = byte;
557 consumeByte(insn, &insn->vectorExtensionPrefix[1]);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000558
Craig Topper31854ba2011-10-03 07:51:09 +0000559 if (insn->mode == MODE_64BIT) {
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000560 insn->rexPrefix = 0x40
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000561 | (rFromVEX2of2(insn->vectorExtensionPrefix[1]) << 2);
Craig Topper31854ba2011-10-03 07:51:09 +0000562 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000563
Craig Topper273515e2014-10-07 07:29:48 +0000564 switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000565 default:
566 break;
567 case VEX_PREFIX_66:
Richard Smith5d5061032014-04-20 22:15:37 +0000568 hasOpSize = true;
Sean Callananc3fd5232011-03-15 01:23:15 +0000569 break;
570 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000571
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000572 dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx",
573 insn->vectorExtensionPrefix[0],
574 insn->vectorExtensionPrefix[1]);
Craig Topper9e3e38a2013-10-03 05:17:48 +0000575 }
Craig Topper273515e2014-10-07 07:29:48 +0000576 } else if (byte == 0x8f) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000577 uint8_t byte1;
578
579 if (lookAtByte(insn, &byte1)) {
580 dbgprintf(insn, "Couldn't read second byte of XOP");
581 return -1;
582 }
583
Craig Topper9eb88372013-10-03 06:29:59 +0000584 if ((byte1 & 0x38) != 0x0) { /* 0 in these 3 bits is a POP instruction. */
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000585 insn->vectorExtensionType = TYPE_XOP;
Craig Topper9e3e38a2013-10-03 05:17:48 +0000586 insn->necessaryPrefixLocation = insn->readerCursor - 1;
Craig Topper273515e2014-10-07 07:29:48 +0000587 } else {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000588 unconsumeByte(insn);
589 insn->necessaryPrefixLocation = insn->readerCursor - 1;
590 }
591
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000592 if (insn->vectorExtensionType == TYPE_XOP) {
593 insn->vectorExtensionPrefix[0] = byte;
594 consumeByte(insn, &insn->vectorExtensionPrefix[1]);
595 consumeByte(insn, &insn->vectorExtensionPrefix[2]);
Craig Topper9e3e38a2013-10-03 05:17:48 +0000596
597 /* We simulate the REX prefix for simplicity's sake */
598
599 if (insn->mode == MODE_64BIT) {
600 insn->rexPrefix = 0x40
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000601 | (wFromXOP3of3(insn->vectorExtensionPrefix[2]) << 3)
602 | (rFromXOP2of3(insn->vectorExtensionPrefix[1]) << 2)
603 | (xFromXOP2of3(insn->vectorExtensionPrefix[1]) << 1)
604 | (bFromXOP2of3(insn->vectorExtensionPrefix[1]) << 0);
Craig Topper9e3e38a2013-10-03 05:17:48 +0000605 }
606
Craig Topper273515e2014-10-07 07:29:48 +0000607 switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000608 default:
609 break;
610 case VEX_PREFIX_66:
Richard Smith5d5061032014-04-20 22:15:37 +0000611 hasOpSize = true;
Craig Topper9e3e38a2013-10-03 05:17:48 +0000612 break;
613 }
614
615 dbgprintf(insn, "Found XOP prefix 0x%hhx 0x%hhx 0x%hhx",
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000616 insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
617 insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +0000618 }
Craig Topper273515e2014-10-07 07:29:48 +0000619 } else {
Sean Callananc3fd5232011-03-15 01:23:15 +0000620 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
Craig Topper273515e2014-10-07 07:29:48 +0000691 if (insn->vectorExtensionType == TYPE_EVEX) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000692 switch (mmFromEVEX2of4(insn->vectorExtensionPrefix[1])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000693 default:
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000694 dbgprintf(insn, "Unhandled mm field for instruction (0x%hhx)",
695 mmFromEVEX2of4(insn->vectorExtensionPrefix[1]));
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000696 return -1;
Sean Callananc3fd5232011-03-15 01:23:15 +0000697 case VEX_LOB_0F:
Sean Callananc3fd5232011-03-15 01:23:15 +0000698 insn->opcodeType = TWOBYTE;
699 return consumeByte(insn, &insn->opcode);
700 case VEX_LOB_0F38:
Sean Callananc3fd5232011-03-15 01:23:15 +0000701 insn->opcodeType = THREEBYTE_38;
702 return consumeByte(insn, &insn->opcode);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000703 case VEX_LOB_0F3A:
Sean Callananc3fd5232011-03-15 01:23:15 +0000704 insn->opcodeType = THREEBYTE_3A;
705 return consumeByte(insn, &insn->opcode);
706 }
Craig Topper273515e2014-10-07 07:29:48 +0000707 } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000708 switch (mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])) {
709 default:
710 dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
711 mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
712 return -1;
713 case VEX_LOB_0F:
714 insn->opcodeType = TWOBYTE;
715 return consumeByte(insn, &insn->opcode);
716 case VEX_LOB_0F38:
717 insn->opcodeType = THREEBYTE_38;
718 return consumeByte(insn, &insn->opcode);
719 case VEX_LOB_0F3A:
720 insn->opcodeType = THREEBYTE_3A;
721 return consumeByte(insn, &insn->opcode);
722 }
Craig Topper273515e2014-10-07 07:29:48 +0000723 } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000724 insn->opcodeType = TWOBYTE;
725 return consumeByte(insn, &insn->opcode);
Craig Topper273515e2014-10-07 07:29:48 +0000726 } else if (insn->vectorExtensionType == TYPE_XOP) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000727 switch (mmmmmFromXOP2of3(insn->vectorExtensionPrefix[1])) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000728 default:
729 dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000730 mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
Craig Topper9e3e38a2013-10-03 05:17:48 +0000731 return -1;
732 case XOP_MAP_SELECT_8:
733 insn->opcodeType = XOP8_MAP;
734 return consumeByte(insn, &insn->opcode);
735 case XOP_MAP_SELECT_9:
736 insn->opcodeType = XOP9_MAP;
737 return consumeByte(insn, &insn->opcode);
738 case XOP_MAP_SELECT_A:
739 insn->opcodeType = XOPA_MAP;
740 return consumeByte(insn, &insn->opcode);
741 }
742 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000743
Sean Callanan04cc3072009-12-19 02:59:52 +0000744 if (consumeByte(insn, &current))
745 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000746
Sean Callanan04cc3072009-12-19 02:59:52 +0000747 if (current == 0x0f) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000748 dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000749
Sean Callanan04cc3072009-12-19 02:59:52 +0000750 if (consumeByte(insn, &current))
751 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000752
Sean Callanan04cc3072009-12-19 02:59:52 +0000753 if (current == 0x38) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000754 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000755
Sean Callanan04cc3072009-12-19 02:59:52 +0000756 if (consumeByte(insn, &current))
757 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000758
Sean Callanan04cc3072009-12-19 02:59:52 +0000759 insn->opcodeType = THREEBYTE_38;
760 } else if (current == 0x3a) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000761 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000762
Sean Callanan04cc3072009-12-19 02:59:52 +0000763 if (consumeByte(insn, &current))
764 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000765
Sean Callanan04cc3072009-12-19 02:59:52 +0000766 insn->opcodeType = THREEBYTE_3A;
767 } else {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000768 dbgprintf(insn, "Didn't find a three-byte escape prefix");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000769
Sean Callanan04cc3072009-12-19 02:59:52 +0000770 insn->opcodeType = TWOBYTE;
771 }
772 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000773
Sean Callanan04cc3072009-12-19 02:59:52 +0000774 /*
775 * At this point we have consumed the full opcode.
776 * Anything we consume from here on must be unconsumed.
777 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000778
Sean Callanan04cc3072009-12-19 02:59:52 +0000779 insn->opcode = current;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000780
Sean Callanan04cc3072009-12-19 02:59:52 +0000781 return 0;
782}
783
784static int readModRM(struct InternalInstruction* insn);
785
786/*
787 * getIDWithAttrMask - Determines the ID of an instruction, consuming
788 * the ModR/M byte as appropriate for extended and escape opcodes,
789 * and using a supplied attribute mask.
790 *
791 * @param instructionID - A pointer whose target is filled in with the ID of the
792 * instruction.
793 * @param insn - The instruction whose ID is to be determined.
794 * @param attrMask - The attribute mask to search.
795 * @return - 0 if the ModR/M could be read when needed or was not
796 * needed; nonzero otherwise.
797 */
798static int getIDWithAttrMask(uint16_t* instructionID,
799 struct InternalInstruction* insn,
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000800 uint16_t attrMask) {
Richard Smith5d5061032014-04-20 22:15:37 +0000801 bool hasModRMExtension;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000802
Richard Smith89ee75d2014-04-20 21:07:34 +0000803 InstructionContext instructionClass = contextForAttrs(attrMask);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000804
Sean Callanan04cc3072009-12-19 02:59:52 +0000805 hasModRMExtension = modRMRequired(insn->opcodeType,
806 instructionClass,
807 insn->opcode);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000808
Sean Callanan04cc3072009-12-19 02:59:52 +0000809 if (hasModRMExtension) {
Rafael Espindola9f9a1062011-01-06 16:48:42 +0000810 if (readModRM(insn))
811 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000812
Sean Callanan04cc3072009-12-19 02:59:52 +0000813 *instructionID = decode(insn->opcodeType,
814 instructionClass,
815 insn->opcode,
816 insn->modRM);
817 } else {
818 *instructionID = decode(insn->opcodeType,
819 instructionClass,
820 insn->opcode,
821 0);
822 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000823
Sean Callanan04cc3072009-12-19 02:59:52 +0000824 return 0;
825}
826
827/*
828 * is16BitEquivalent - Determines whether two instruction names refer to
829 * equivalent instructions but one is 16-bit whereas the other is not.
830 *
831 * @param orig - The instruction that is not 16-bit
832 * @param equiv - The instruction that is 16-bit
833 */
Richard Smith5d5061032014-04-20 22:15:37 +0000834static bool is16BitEquivalent(const char* orig, const char* equiv) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000835 off_t i;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000836
Sean Callanan010b3732010-04-02 21:23:51 +0000837 for (i = 0;; i++) {
838 if (orig[i] == '\0' && equiv[i] == '\0')
Richard Smith5d5061032014-04-20 22:15:37 +0000839 return true;
Sean Callanan010b3732010-04-02 21:23:51 +0000840 if (orig[i] == '\0' || equiv[i] == '\0')
Richard Smith5d5061032014-04-20 22:15:37 +0000841 return false;
Sean Callanan010b3732010-04-02 21:23:51 +0000842 if (orig[i] != equiv[i]) {
843 if ((orig[i] == 'Q' || orig[i] == 'L') && equiv[i] == 'W')
Sean Callanan04cc3072009-12-19 02:59:52 +0000844 continue;
Sean Callanan010b3732010-04-02 21:23:51 +0000845 if ((orig[i] == '6' || orig[i] == '3') && equiv[i] == '1')
Sean Callanan04cc3072009-12-19 02:59:52 +0000846 continue;
Sean Callanan010b3732010-04-02 21:23:51 +0000847 if ((orig[i] == '4' || orig[i] == '2') && equiv[i] == '6')
Sean Callanan04cc3072009-12-19 02:59:52 +0000848 continue;
Richard Smith5d5061032014-04-20 22:15:37 +0000849 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000850 }
851 }
852}
853
854/*
Craig Topper0676b902014-10-07 07:29:50 +0000855 * is64Bit - Determines whether this instruction is a 64-bit instruction.
856 *
857 * @param name - The instruction that is not 16-bit
858 */
859static bool is64Bit(const char* name) {
860 off_t i;
861
862 for (i = 0;; ++i) {
863 if (name[i] == '\0')
864 return false;
865 if (name[i] == '6' && name[i+1] == '4')
866 return true;
867 }
868}
869
870/*
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000871 * getID - Determines the ID of an instruction, consuming the ModR/M byte as
872 * appropriate for extended and escape opcodes. Determines the attributes and
Sean Callanan04cc3072009-12-19 02:59:52 +0000873 * context for the instruction before doing so.
874 *
875 * @param insn - The instruction whose ID is to be determined.
876 * @return - 0 if the ModR/M could be read when needed or was not needed;
877 * nonzero otherwise.
878 */
Roman Divacky67923802012-09-05 21:17:34 +0000879static int getID(struct InternalInstruction* insn, const void *miiArg) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000880 uint16_t attrMask;
Sean Callanan04cc3072009-12-19 02:59:52 +0000881 uint16_t instructionID;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000882
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000883 dbgprintf(insn, "getID()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000884
Sean Callanan04cc3072009-12-19 02:59:52 +0000885 attrMask = ATTR_NONE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000886
Sean Callanan04cc3072009-12-19 02:59:52 +0000887 if (insn->mode == MODE_64BIT)
888 attrMask |= ATTR_64BIT;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000889
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000890 if (insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
891 attrMask |= (insn->vectorExtensionType == TYPE_EVEX) ? ATTR_EVEX : ATTR_VEX;
Sean Callananc3fd5232011-03-15 01:23:15 +0000892
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000893 if (insn->vectorExtensionType == TYPE_EVEX) {
894 switch (ppFromEVEX3of4(insn->vectorExtensionPrefix[2])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000895 case VEX_PREFIX_66:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000896 attrMask |= ATTR_OPSIZE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000897 break;
898 case VEX_PREFIX_F3:
899 attrMask |= ATTR_XS;
900 break;
901 case VEX_PREFIX_F2:
902 attrMask |= ATTR_XD;
903 break;
904 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000905
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000906 if (zFromEVEX4of4(insn->vectorExtensionPrefix[3]))
907 attrMask |= ATTR_EVEXKZ;
908 if (bFromEVEX4of4(insn->vectorExtensionPrefix[3]))
909 attrMask |= ATTR_EVEXB;
910 if (aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]))
911 attrMask |= ATTR_EVEXK;
912 if (lFromEVEX4of4(insn->vectorExtensionPrefix[3]))
913 attrMask |= ATTR_EVEXL;
914 if (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
915 attrMask |= ATTR_EVEXL2;
Craig Topper273515e2014-10-07 07:29:48 +0000916 } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000917 switch (ppFromVEX3of3(insn->vectorExtensionPrefix[2])) {
918 case VEX_PREFIX_66:
919 attrMask |= ATTR_OPSIZE;
920 break;
921 case VEX_PREFIX_F3:
922 attrMask |= ATTR_XS;
923 break;
924 case VEX_PREFIX_F2:
925 attrMask |= ATTR_XD;
926 break;
927 }
928
929 if (lFromVEX3of3(insn->vectorExtensionPrefix[2]))
Sean Callananc3fd5232011-03-15 01:23:15 +0000930 attrMask |= ATTR_VEXL;
Craig Topper273515e2014-10-07 07:29:48 +0000931 } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000932 switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000933 case VEX_PREFIX_66:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000934 attrMask |= ATTR_OPSIZE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000935 break;
936 case VEX_PREFIX_F3:
937 attrMask |= ATTR_XS;
938 break;
939 case VEX_PREFIX_F2:
940 attrMask |= ATTR_XD;
941 break;
942 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000943
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000944 if (lFromVEX2of2(insn->vectorExtensionPrefix[1]))
Craig Topper9e3e38a2013-10-03 05:17:48 +0000945 attrMask |= ATTR_VEXL;
Craig Topper273515e2014-10-07 07:29:48 +0000946 } else if (insn->vectorExtensionType == TYPE_XOP) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000947 switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000948 case VEX_PREFIX_66:
949 attrMask |= ATTR_OPSIZE;
950 break;
951 case VEX_PREFIX_F3:
952 attrMask |= ATTR_XS;
953 break;
954 case VEX_PREFIX_F2:
955 attrMask |= ATTR_XD;
956 break;
957 }
958
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000959 if (lFromXOP3of3(insn->vectorExtensionPrefix[2]))
Sean Callananc3fd5232011-03-15 01:23:15 +0000960 attrMask |= ATTR_VEXL;
Craig Topper273515e2014-10-07 07:29:48 +0000961 } else {
Sean Callananc3fd5232011-03-15 01:23:15 +0000962 return -1;
963 }
Craig Topper273515e2014-10-07 07:29:48 +0000964 } else {
David Woodhouse5cf4c672014-01-20 12:02:35 +0000965 if (insn->mode != MODE_16BIT && isPrefixAtLocation(insn, 0x66, insn->necessaryPrefixLocation))
Sean Callananc3fd5232011-03-15 01:23:15 +0000966 attrMask |= ATTR_OPSIZE;
Craig Topper6491c802012-02-27 01:54:29 +0000967 else if (isPrefixAtLocation(insn, 0x67, insn->necessaryPrefixLocation))
968 attrMask |= ATTR_ADSIZE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000969 else if (isPrefixAtLocation(insn, 0xf3, insn->necessaryPrefixLocation))
970 attrMask |= ATTR_XS;
971 else if (isPrefixAtLocation(insn, 0xf2, insn->necessaryPrefixLocation))
972 attrMask |= ATTR_XD;
Sean Callananc3fd5232011-03-15 01:23:15 +0000973 }
974
Craig Topperf18c8962011-10-04 06:30:42 +0000975 if (insn->rexPrefix & 0x08)
976 attrMask |= ATTR_REXW;
Craig Topperf01f1b52011-11-06 23:04:08 +0000977
David Woodhouse9c74fdb2014-01-20 12:02:48 +0000978 /*
979 * JCXZ/JECXZ need special handling for 16-bit mode because the meaning
980 * of the AdSize prefix is inverted w.r.t. 32-bit mode.
981 */
Craig Topper6e518772014-12-31 07:07:11 +0000982 if (insn->mode == MODE_16BIT && insn->opcodeType == ONEBYTE &&
983 insn->opcode == 0xE3)
984 attrMask ^= ATTR_ADSIZE;
David Woodhouse9c74fdb2014-01-20 12:02:48 +0000985
Craig Topper6e518772014-12-31 07:07:11 +0000986 if (getIDWithAttrMask(&instructionID, insn, attrMask))
987 return -1;
David Woodhouse9c74fdb2014-01-20 12:02:48 +0000988
Sean Callanan04cc3072009-12-19 02:59:52 +0000989 /* The following clauses compensate for limitations of the tables. */
Craig Topperf01f1b52011-11-06 23:04:08 +0000990
Craig Topper0676b902014-10-07 07:29:50 +0000991 if (insn->mode != MODE_64BIT &&
992 insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
993 /*
994 * The tables can't distinquish between cases where the W-bit is used to
995 * select register size and cases where its a required part of the opcode.
996 */
997 if ((insn->vectorExtensionType == TYPE_EVEX &&
998 wFromEVEX3of4(insn->vectorExtensionPrefix[2])) ||
999 (insn->vectorExtensionType == TYPE_VEX_3B &&
1000 wFromVEX3of3(insn->vectorExtensionPrefix[2])) ||
1001 (insn->vectorExtensionType == TYPE_XOP &&
1002 wFromXOP3of3(insn->vectorExtensionPrefix[2]))) {
1003
1004 uint16_t instructionIDWithREXW;
1005 if (getIDWithAttrMask(&instructionIDWithREXW,
1006 insn, attrMask | ATTR_REXW)) {
1007 insn->instructionID = instructionID;
1008 insn->spec = specifierForUID(instructionID);
1009 return 0;
1010 }
1011
1012 const char *SpecName = GetInstrName(instructionIDWithREXW, miiArg);
1013 // If not a 64-bit instruction. Switch the opcode.
1014 if (!is64Bit(SpecName)) {
1015 insn->instructionID = instructionIDWithREXW;
1016 insn->spec = specifierForUID(instructionIDWithREXW);
1017 return 0;
1018 }
1019 }
1020 }
1021
Craig Topper99bcab72014-12-31 07:07:31 +00001022 /*
1023 * Absolute moves need special handling.
1024 * -For 16-bit mode because the meaning of the AdSize and OpSize prefixes are
1025 * inverted w.r.t.
1026 * -For 32-bit mode we need to ensure the ADSIZE prefix is observed in
1027 * any position.
1028 */
1029 if (insn->opcodeType == ONEBYTE && ((insn->opcode & 0xFC) == 0xA0)) {
1030 /* Make sure we observed the prefixes in any position. */
1031 if (insn->prefixPresent[0x67])
1032 attrMask |= ATTR_ADSIZE;
1033 if (insn->prefixPresent[0x66])
1034 attrMask |= ATTR_OPSIZE;
1035
1036 /* In 16-bit, invert the attributes. */
1037 if (insn->mode == MODE_16BIT)
1038 attrMask ^= ATTR_ADSIZE | ATTR_OPSIZE;
1039
1040 if (getIDWithAttrMask(&instructionID, insn, attrMask))
1041 return -1;
1042
1043 insn->instructionID = instructionID;
1044 insn->spec = specifierForUID(instructionID);
1045 return 0;
1046 }
1047
David Woodhouse5cf4c672014-01-20 12:02:35 +00001048 if ((insn->mode == MODE_16BIT || insn->prefixPresent[0x66]) &&
1049 !(attrMask & ATTR_OPSIZE)) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001050 /*
1051 * The instruction tables make no distinction between instructions that
1052 * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a
1053 * particular spot (i.e., many MMX operations). In general we're
1054 * conservative, but in the specific case where OpSize is present but not
1055 * in the right place we check if there's a 16-bit operation.
1056 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001057
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +00001058 const struct InstructionSpecifier *spec;
Sean Callanan04cc3072009-12-19 02:59:52 +00001059 uint16_t instructionIDWithOpsize;
Benjamin Kramer915e3d92012-02-11 16:01:02 +00001060 const char *specName, *specWithOpSizeName;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001061
Sean Callanan04cc3072009-12-19 02:59:52 +00001062 spec = specifierForUID(instructionID);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001063
Sean Callanan04cc3072009-12-19 02:59:52 +00001064 if (getIDWithAttrMask(&instructionIDWithOpsize,
1065 insn,
1066 attrMask | ATTR_OPSIZE)) {
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001067 /*
Sean Callanan04cc3072009-12-19 02:59:52 +00001068 * ModRM required with OpSize but not present; give up and return version
1069 * without OpSize set
1070 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001071
Sean Callanan04cc3072009-12-19 02:59:52 +00001072 insn->instructionID = instructionID;
1073 insn->spec = spec;
1074 return 0;
1075 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001076
Richard Smith89ee75d2014-04-20 21:07:34 +00001077 specName = GetInstrName(instructionID, miiArg);
1078 specWithOpSizeName = GetInstrName(instructionIDWithOpsize, miiArg);
Benjamin Kramer478e8de2012-02-11 14:50:54 +00001079
David Woodhouse5cf4c672014-01-20 12:02:35 +00001080 if (is16BitEquivalent(specName, specWithOpSizeName) &&
1081 (insn->mode == MODE_16BIT) ^ insn->prefixPresent[0x66]) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001082 insn->instructionID = instructionIDWithOpsize;
Benjamin Kramer915e3d92012-02-11 16:01:02 +00001083 insn->spec = specifierForUID(instructionIDWithOpsize);
Sean Callanan04cc3072009-12-19 02:59:52 +00001084 } else {
1085 insn->instructionID = instructionID;
1086 insn->spec = spec;
1087 }
1088 return 0;
1089 }
Craig Topper21c33652011-10-02 16:56:09 +00001090
1091 if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 &&
1092 insn->rexPrefix & 0x01) {
1093 /*
1094 * NOOP shouldn't decode as NOOP if REX.b is set. Instead
1095 * it should decode as XCHG %r8, %eax.
1096 */
1097
1098 const struct InstructionSpecifier *spec;
1099 uint16_t instructionIDWithNewOpcode;
1100 const struct InstructionSpecifier *specWithNewOpcode;
1101
1102 spec = specifierForUID(instructionID);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001103
Craig Topperb58a9662011-10-05 03:29:32 +00001104 /* Borrow opcode from one of the other XCHGar opcodes */
Craig Topper21c33652011-10-02 16:56:09 +00001105 insn->opcode = 0x91;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001106
Craig Topper21c33652011-10-02 16:56:09 +00001107 if (getIDWithAttrMask(&instructionIDWithNewOpcode,
1108 insn,
1109 attrMask)) {
1110 insn->opcode = 0x90;
1111
1112 insn->instructionID = instructionID;
1113 insn->spec = spec;
1114 return 0;
1115 }
1116
1117 specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode);
1118
Craig Topperb58a9662011-10-05 03:29:32 +00001119 /* Change back */
Craig Topper21c33652011-10-02 16:56:09 +00001120 insn->opcode = 0x90;
1121
1122 insn->instructionID = instructionIDWithNewOpcode;
1123 insn->spec = specWithNewOpcode;
1124
1125 return 0;
1126 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001127
Sean Callanan04cc3072009-12-19 02:59:52 +00001128 insn->instructionID = instructionID;
1129 insn->spec = specifierForUID(insn->instructionID);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001130
Sean Callanan04cc3072009-12-19 02:59:52 +00001131 return 0;
1132}
1133
1134/*
1135 * readSIB - Consumes the SIB byte to determine addressing information for an
1136 * instruction.
1137 *
1138 * @param insn - The instruction whose SIB byte is to be read.
1139 * @return - 0 if the SIB byte was successfully read; nonzero otherwise.
1140 */
1141static int readSIB(struct InternalInstruction* insn) {
Richard Smith89ee75d2014-04-20 21:07:34 +00001142 SIBIndex sibIndexBase = SIB_INDEX_NONE;
1143 SIBBase sibBaseBase = SIB_BASE_NONE;
Sean Callanan04cc3072009-12-19 02:59:52 +00001144 uint8_t index, base;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001145
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001146 dbgprintf(insn, "readSIB()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001147
Sean Callanan04cc3072009-12-19 02:59:52 +00001148 if (insn->consumedSIB)
1149 return 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001150
Richard Smith5d5061032014-04-20 22:15:37 +00001151 insn->consumedSIB = true;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001152
Sean Callanan04cc3072009-12-19 02:59:52 +00001153 switch (insn->addressSize) {
1154 case 2:
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001155 dbgprintf(insn, "SIB-based addressing doesn't work in 16-bit mode");
Sean Callanan04cc3072009-12-19 02:59:52 +00001156 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001157 case 4:
1158 sibIndexBase = SIB_INDEX_EAX;
1159 sibBaseBase = SIB_BASE_EAX;
1160 break;
1161 case 8:
1162 sibIndexBase = SIB_INDEX_RAX;
1163 sibBaseBase = SIB_BASE_RAX;
1164 break;
1165 }
1166
1167 if (consumeByte(insn, &insn->sib))
1168 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001169
Sean Callanan04cc3072009-12-19 02:59:52 +00001170 index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001171 if (insn->vectorExtensionType == TYPE_EVEX)
1172 index |= v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001173
Sean Callanan04cc3072009-12-19 02:59:52 +00001174 switch (index) {
1175 case 0x4:
1176 insn->sibIndex = SIB_INDEX_NONE;
1177 break;
1178 default:
Benjamin Kramer25bddae2011-02-27 18:13:53 +00001179 insn->sibIndex = (SIBIndex)(sibIndexBase + index);
Sean Callanan04cc3072009-12-19 02:59:52 +00001180 if (insn->sibIndex == SIB_INDEX_sib ||
1181 insn->sibIndex == SIB_INDEX_sib64)
1182 insn->sibIndex = SIB_INDEX_NONE;
1183 break;
1184 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001185
Sean Callanan04cc3072009-12-19 02:59:52 +00001186 switch (scaleFromSIB(insn->sib)) {
1187 case 0:
1188 insn->sibScale = 1;
1189 break;
1190 case 1:
1191 insn->sibScale = 2;
1192 break;
1193 case 2:
1194 insn->sibScale = 4;
1195 break;
1196 case 3:
1197 insn->sibScale = 8;
1198 break;
1199 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001200
Sean Callanan04cc3072009-12-19 02:59:52 +00001201 base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001202
Sean Callanan04cc3072009-12-19 02:59:52 +00001203 switch (base) {
1204 case 0x5:
Craig Topperfae5ac22014-02-17 10:03:43 +00001205 case 0xd:
Sean Callanan04cc3072009-12-19 02:59:52 +00001206 switch (modFromModRM(insn->modRM)) {
1207 case 0x0:
1208 insn->eaDisplacement = EA_DISP_32;
1209 insn->sibBase = SIB_BASE_NONE;
1210 break;
1211 case 0x1:
1212 insn->eaDisplacement = EA_DISP_8;
Craig Topperfae5ac22014-02-17 10:03:43 +00001213 insn->sibBase = (SIBBase)(sibBaseBase + base);
Sean Callanan04cc3072009-12-19 02:59:52 +00001214 break;
1215 case 0x2:
1216 insn->eaDisplacement = EA_DISP_32;
Craig Topperfae5ac22014-02-17 10:03:43 +00001217 insn->sibBase = (SIBBase)(sibBaseBase + base);
Sean Callanan04cc3072009-12-19 02:59:52 +00001218 break;
1219 case 0x3:
Sean Callanan010b3732010-04-02 21:23:51 +00001220 debug("Cannot have Mod = 0b11 and a SIB byte");
1221 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001222 }
1223 break;
1224 default:
Benjamin Kramer25bddae2011-02-27 18:13:53 +00001225 insn->sibBase = (SIBBase)(sibBaseBase + base);
Sean Callanan04cc3072009-12-19 02:59:52 +00001226 break;
1227 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001228
Sean Callanan04cc3072009-12-19 02:59:52 +00001229 return 0;
1230}
1231
1232/*
1233 * readDisplacement - Consumes the displacement of an instruction.
1234 *
1235 * @param insn - The instruction whose displacement is to be read.
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001236 * @return - 0 if the displacement byte was successfully read; nonzero
Sean Callanan04cc3072009-12-19 02:59:52 +00001237 * otherwise.
1238 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001239static int readDisplacement(struct InternalInstruction* insn) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001240 int8_t d8;
1241 int16_t d16;
1242 int32_t d32;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001243
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001244 dbgprintf(insn, "readDisplacement()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001245
Sean Callanan04cc3072009-12-19 02:59:52 +00001246 if (insn->consumedDisplacement)
1247 return 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001248
Richard Smith5d5061032014-04-20 22:15:37 +00001249 insn->consumedDisplacement = true;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +00001250 insn->displacementOffset = insn->readerCursor - insn->startLocation;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001251
Sean Callanan04cc3072009-12-19 02:59:52 +00001252 switch (insn->eaDisplacement) {
1253 case EA_DISP_NONE:
Richard Smith5d5061032014-04-20 22:15:37 +00001254 insn->consumedDisplacement = false;
Sean Callanan04cc3072009-12-19 02:59:52 +00001255 break;
1256 case EA_DISP_8:
1257 if (consumeInt8(insn, &d8))
1258 return -1;
1259 insn->displacement = d8;
1260 break;
1261 case EA_DISP_16:
1262 if (consumeInt16(insn, &d16))
1263 return -1;
1264 insn->displacement = d16;
1265 break;
1266 case EA_DISP_32:
1267 if (consumeInt32(insn, &d32))
1268 return -1;
1269 insn->displacement = d32;
1270 break;
1271 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001272
Richard Smith5d5061032014-04-20 22:15:37 +00001273 insn->consumedDisplacement = true;
Sean Callanan04cc3072009-12-19 02:59:52 +00001274 return 0;
1275}
1276
1277/*
1278 * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and
1279 * displacement) for an instruction and interprets it.
1280 *
1281 * @param insn - The instruction whose addressing information is to be read.
1282 * @return - 0 if the information was successfully read; nonzero otherwise.
1283 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001284static int readModRM(struct InternalInstruction* insn) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001285 uint8_t mod, rm, reg;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001286
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001287 dbgprintf(insn, "readModRM()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001288
Sean Callanan04cc3072009-12-19 02:59:52 +00001289 if (insn->consumedModRM)
1290 return 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001291
Rafael Espindola9f9a1062011-01-06 16:48:42 +00001292 if (consumeByte(insn, &insn->modRM))
1293 return -1;
Richard Smith5d5061032014-04-20 22:15:37 +00001294 insn->consumedModRM = true;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001295
Sean Callanan04cc3072009-12-19 02:59:52 +00001296 mod = modFromModRM(insn->modRM);
1297 rm = rmFromModRM(insn->modRM);
1298 reg = regFromModRM(insn->modRM);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001299
Sean Callanan04cc3072009-12-19 02:59:52 +00001300 /*
1301 * This goes by insn->registerSize to pick the correct register, which messes
1302 * up if we're using (say) XMM or 8-bit register operands. That gets fixed in
1303 * fixupReg().
1304 */
1305 switch (insn->registerSize) {
1306 case 2:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001307 insn->regBase = MODRM_REG_AX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001308 insn->eaRegBase = EA_REG_AX;
1309 break;
1310 case 4:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001311 insn->regBase = MODRM_REG_EAX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001312 insn->eaRegBase = EA_REG_EAX;
1313 break;
1314 case 8:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001315 insn->regBase = MODRM_REG_RAX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001316 insn->eaRegBase = EA_REG_RAX;
1317 break;
1318 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001319
Sean Callanan04cc3072009-12-19 02:59:52 +00001320 reg |= rFromREX(insn->rexPrefix) << 3;
1321 rm |= bFromREX(insn->rexPrefix) << 3;
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001322 if (insn->vectorExtensionType == TYPE_EVEX) {
1323 reg |= r2FromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1324 rm |= xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1325 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001326
Sean Callanan04cc3072009-12-19 02:59:52 +00001327 insn->reg = (Reg)(insn->regBase + reg);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001328
Sean Callanan04cc3072009-12-19 02:59:52 +00001329 switch (insn->addressSize) {
1330 case 2:
1331 insn->eaBaseBase = EA_BASE_BX_SI;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001332
Sean Callanan04cc3072009-12-19 02:59:52 +00001333 switch (mod) {
1334 case 0x0:
1335 if (rm == 0x6) {
1336 insn->eaBase = EA_BASE_NONE;
1337 insn->eaDisplacement = EA_DISP_16;
Sean Callanan010b3732010-04-02 21:23:51 +00001338 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001339 return -1;
1340 } else {
1341 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1342 insn->eaDisplacement = EA_DISP_NONE;
1343 }
1344 break;
1345 case 0x1:
1346 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1347 insn->eaDisplacement = EA_DISP_8;
Craig Topper399e39e2014-01-25 22:48:43 +00001348 insn->displacementSize = 1;
Sean Callanan010b3732010-04-02 21:23:51 +00001349 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001350 return -1;
1351 break;
1352 case 0x2:
1353 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1354 insn->eaDisplacement = EA_DISP_16;
Sean Callanan010b3732010-04-02 21:23:51 +00001355 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001356 return -1;
1357 break;
1358 case 0x3:
1359 insn->eaBase = (EABase)(insn->eaRegBase + rm);
Sean Callanan010b3732010-04-02 21:23:51 +00001360 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001361 return -1;
1362 break;
1363 }
1364 break;
1365 case 4:
1366 case 8:
1367 insn->eaBaseBase = (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001368
Sean Callanan04cc3072009-12-19 02:59:52 +00001369 switch (mod) {
1370 case 0x0:
1371 insn->eaDisplacement = EA_DISP_NONE; /* readSIB may override this */
1372 switch (rm) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001373 case 0x14:
Sean Callanan04cc3072009-12-19 02:59:52 +00001374 case 0x4:
1375 case 0xc: /* in case REXW.b is set */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001376 insn->eaBase = (insn->addressSize == 4 ?
Sean Callanan04cc3072009-12-19 02:59:52 +00001377 EA_BASE_sib : EA_BASE_sib64);
Craig Topper38afbfd2014-03-20 05:56:00 +00001378 if (readSIB(insn) || readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001379 return -1;
1380 break;
1381 case 0x5:
1382 insn->eaBase = EA_BASE_NONE;
1383 insn->eaDisplacement = EA_DISP_32;
Sean Callanan010b3732010-04-02 21:23:51 +00001384 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001385 return -1;
1386 break;
1387 default:
1388 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1389 break;
1390 }
1391 break;
1392 case 0x1:
Craig Topper399e39e2014-01-25 22:48:43 +00001393 insn->displacementSize = 1;
Alp Toker771f7652014-01-26 18:44:34 +00001394 /* FALLTHROUGH */
Sean Callanan04cc3072009-12-19 02:59:52 +00001395 case 0x2:
1396 insn->eaDisplacement = (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
1397 switch (rm) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001398 case 0x14:
Sean Callanan04cc3072009-12-19 02:59:52 +00001399 case 0x4:
1400 case 0xc: /* in case REXW.b is set */
1401 insn->eaBase = EA_BASE_sib;
Craig Topper38afbfd2014-03-20 05:56:00 +00001402 if (readSIB(insn) || readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001403 return -1;
1404 break;
1405 default:
1406 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
Sean Callanan010b3732010-04-02 21:23:51 +00001407 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001408 return -1;
1409 break;
1410 }
1411 break;
1412 case 0x3:
1413 insn->eaDisplacement = EA_DISP_NONE;
1414 insn->eaBase = (EABase)(insn->eaRegBase + rm);
1415 break;
1416 }
1417 break;
1418 } /* switch (insn->addressSize) */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001419
Sean Callanan04cc3072009-12-19 02:59:52 +00001420 return 0;
1421}
1422
1423#define GENERIC_FIXUP_FUNC(name, base, prefix) \
1424 static uint8_t name(struct InternalInstruction *insn, \
1425 OperandType type, \
1426 uint8_t index, \
1427 uint8_t *valid) { \
1428 *valid = 1; \
1429 switch (type) { \
1430 default: \
Sean Callanan010b3732010-04-02 21:23:51 +00001431 debug("Unhandled register type"); \
1432 *valid = 0; \
1433 return 0; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001434 case TYPE_Rv: \
1435 return base + index; \
1436 case TYPE_R8: \
Sean Callanan010b3732010-04-02 21:23:51 +00001437 if (insn->rexPrefix && \
Sean Callanan04cc3072009-12-19 02:59:52 +00001438 index >= 4 && index <= 7) { \
1439 return prefix##_SPL + (index - 4); \
1440 } else { \
1441 return prefix##_AL + index; \
1442 } \
1443 case TYPE_R16: \
1444 return prefix##_AX + index; \
1445 case TYPE_R32: \
1446 return prefix##_EAX + index; \
1447 case TYPE_R64: \
1448 return prefix##_RAX + index; \
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001449 case TYPE_XMM512: \
1450 return prefix##_ZMM0 + index; \
Sean Callananc3fd5232011-03-15 01:23:15 +00001451 case TYPE_XMM256: \
1452 return prefix##_YMM0 + index; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001453 case TYPE_XMM128: \
1454 case TYPE_XMM64: \
1455 case TYPE_XMM32: \
1456 case TYPE_XMM: \
1457 return prefix##_XMM0 + index; \
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001458 case TYPE_VK1: \
1459 case TYPE_VK8: \
1460 case TYPE_VK16: \
Craig Topper9c26bcc2015-03-02 03:33:11 +00001461 if (index > 7) \
1462 *valid = 0; \
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001463 return prefix##_K0 + index; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001464 case TYPE_MM64: \
Craig Topperd5b39232014-12-26 18:19:44 +00001465 return prefix##_MM0 + (index & 0x7); \
Sean Callanan04cc3072009-12-19 02:59:52 +00001466 case TYPE_SEGMENTREG: \
Sean Callanan010b3732010-04-02 21:23:51 +00001467 if (index > 5) \
Sean Callanan04cc3072009-12-19 02:59:52 +00001468 *valid = 0; \
1469 return prefix##_ES + index; \
1470 case TYPE_DEBUGREG: \
Sean Callanan04cc3072009-12-19 02:59:52 +00001471 return prefix##_DR0 + index; \
Sean Callanane7e1cf92010-05-06 20:59:00 +00001472 case TYPE_CONTROLREG: \
Sean Callanane7e1cf92010-05-06 20:59:00 +00001473 return prefix##_CR0 + index; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001474 } \
1475 }
1476
1477/*
1478 * fixup*Value - Consults an operand type to determine the meaning of the
1479 * reg or R/M field. If the operand is an XMM operand, for example, an
1480 * operand would be XMM0 instead of AX, which readModRM() would otherwise
1481 * misinterpret it as.
1482 *
1483 * @param insn - The instruction containing the operand.
1484 * @param type - The operand type.
1485 * @param index - The existing value of the field as reported by readModRM().
1486 * @param valid - The address of a uint8_t. The target is set to 1 if the
1487 * field is valid for the register class; 0 if not.
Sean Callanan010b3732010-04-02 21:23:51 +00001488 * @return - The proper value.
Sean Callanan04cc3072009-12-19 02:59:52 +00001489 */
Sean Callanan2f9443f2009-12-22 02:07:42 +00001490GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase, MODRM_REG)
Sean Callanan04cc3072009-12-19 02:59:52 +00001491GENERIC_FIXUP_FUNC(fixupRMValue, insn->eaRegBase, EA_REG)
1492
1493/*
1494 * fixupReg - Consults an operand specifier to determine which of the
1495 * fixup*Value functions to use in correcting readModRM()'ss interpretation.
1496 *
1497 * @param insn - See fixup*Value().
1498 * @param op - The operand specifier.
1499 * @return - 0 if fixup was successful; -1 if the register returned was
1500 * invalid for its class.
1501 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001502static int fixupReg(struct InternalInstruction *insn,
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +00001503 const struct OperandSpecifier *op) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001504 uint8_t valid;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001505
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001506 dbgprintf(insn, "fixupReg()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001507
Sean Callanan04cc3072009-12-19 02:59:52 +00001508 switch ((OperandEncoding)op->encoding) {
1509 default:
Sean Callanan010b3732010-04-02 21:23:51 +00001510 debug("Expected a REG or R/M encoding in fixupReg");
1511 return -1;
Sean Callananc3fd5232011-03-15 01:23:15 +00001512 case ENCODING_VVVV:
1513 insn->vvvv = (Reg)fixupRegValue(insn,
1514 (OperandType)op->type,
1515 insn->vvvv,
1516 &valid);
1517 if (!valid)
1518 return -1;
1519 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001520 case ENCODING_REG:
1521 insn->reg = (Reg)fixupRegValue(insn,
1522 (OperandType)op->type,
1523 insn->reg - insn->regBase,
1524 &valid);
1525 if (!valid)
1526 return -1;
1527 break;
Adam Nemet5933c2f2014-07-17 17:04:56 +00001528 CASE_ENCODING_RM:
Sean Callanan04cc3072009-12-19 02:59:52 +00001529 if (insn->eaBase >= insn->eaRegBase) {
1530 insn->eaBase = (EABase)fixupRMValue(insn,
1531 (OperandType)op->type,
1532 insn->eaBase - insn->eaRegBase,
1533 &valid);
1534 if (!valid)
1535 return -1;
1536 }
1537 break;
1538 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001539
Sean Callanan04cc3072009-12-19 02:59:52 +00001540 return 0;
1541}
1542
1543/*
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001544 * readOpcodeRegister - Reads an operand from the opcode field of an
Sean Callanan04cc3072009-12-19 02:59:52 +00001545 * instruction and interprets it appropriately given the operand width.
1546 * Handles AddRegFrm instructions.
1547 *
Craig Topper91551182014-01-01 15:29:32 +00001548 * @param insn - the instruction whose opcode field is to be read.
Sean Callanan04cc3072009-12-19 02:59:52 +00001549 * @param size - The width (in bytes) of the register being specified.
1550 * 1 means AL and friends, 2 means AX, 4 means EAX, and 8 means
1551 * RAX.
Sean Callanan010b3732010-04-02 21:23:51 +00001552 * @return - 0 on success; nonzero otherwise.
Sean Callanan04cc3072009-12-19 02:59:52 +00001553 */
Sean Callanan010b3732010-04-02 21:23:51 +00001554static int readOpcodeRegister(struct InternalInstruction* insn, uint8_t size) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001555 dbgprintf(insn, "readOpcodeRegister()");
Sean Callanan04cc3072009-12-19 02:59:52 +00001556
Sean Callanan04cc3072009-12-19 02:59:52 +00001557 if (size == 0)
1558 size = insn->registerSize;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001559
Sean Callanan04cc3072009-12-19 02:59:52 +00001560 switch (size) {
1561 case 1:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001562 insn->opcodeRegister = (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001563 | (insn->opcode & 7)));
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001564 if (insn->rexPrefix &&
Sean Callanan010b3732010-04-02 21:23:51 +00001565 insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1566 insn->opcodeRegister < MODRM_REG_AL + 0x8) {
Sean Callanan2f9443f2009-12-22 02:07:42 +00001567 insn->opcodeRegister = (Reg)(MODRM_REG_SPL
1568 + (insn->opcodeRegister - MODRM_REG_AL - 4));
Sean Callanan04cc3072009-12-19 02:59:52 +00001569 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001570
Sean Callanan04cc3072009-12-19 02:59:52 +00001571 break;
1572 case 2:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001573 insn->opcodeRegister = (Reg)(MODRM_REG_AX
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001574 + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001575 | (insn->opcode & 7)));
Sean Callanan04cc3072009-12-19 02:59:52 +00001576 break;
1577 case 4:
Sean Callanan010b3732010-04-02 21:23:51 +00001578 insn->opcodeRegister = (Reg)(MODRM_REG_EAX
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001579 + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001580 | (insn->opcode & 7)));
Sean Callanan04cc3072009-12-19 02:59:52 +00001581 break;
1582 case 8:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001583 insn->opcodeRegister = (Reg)(MODRM_REG_RAX
1584 + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001585 | (insn->opcode & 7)));
Sean Callanan04cc3072009-12-19 02:59:52 +00001586 break;
1587 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001588
Sean Callanan010b3732010-04-02 21:23:51 +00001589 return 0;
Sean Callanan04cc3072009-12-19 02:59:52 +00001590}
1591
1592/*
1593 * readImmediate - Consumes an immediate operand from an instruction, given the
1594 * desired operand size.
1595 *
1596 * @param insn - The instruction whose operand is to be read.
1597 * @param size - The width (in bytes) of the operand.
1598 * @return - 0 if the immediate was successfully consumed; nonzero
1599 * otherwise.
1600 */
1601static int readImmediate(struct InternalInstruction* insn, uint8_t size) {
1602 uint8_t imm8;
1603 uint16_t imm16;
1604 uint32_t imm32;
1605 uint64_t imm64;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001606
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001607 dbgprintf(insn, "readImmediate()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001608
Sean Callanan010b3732010-04-02 21:23:51 +00001609 if (insn->numImmediatesConsumed == 2) {
1610 debug("Already consumed two immediates");
1611 return -1;
1612 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001613
Sean Callanan04cc3072009-12-19 02:59:52 +00001614 if (size == 0)
1615 size = insn->immediateSize;
1616 else
1617 insn->immediateSize = size;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +00001618 insn->immediateOffset = insn->readerCursor - insn->startLocation;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001619
Sean Callanan04cc3072009-12-19 02:59:52 +00001620 switch (size) {
1621 case 1:
1622 if (consumeByte(insn, &imm8))
1623 return -1;
1624 insn->immediates[insn->numImmediatesConsumed] = imm8;
1625 break;
1626 case 2:
1627 if (consumeUInt16(insn, &imm16))
1628 return -1;
1629 insn->immediates[insn->numImmediatesConsumed] = imm16;
1630 break;
1631 case 4:
1632 if (consumeUInt32(insn, &imm32))
1633 return -1;
1634 insn->immediates[insn->numImmediatesConsumed] = imm32;
1635 break;
1636 case 8:
1637 if (consumeUInt64(insn, &imm64))
1638 return -1;
1639 insn->immediates[insn->numImmediatesConsumed] = imm64;
1640 break;
1641 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001642
Sean Callanan04cc3072009-12-19 02:59:52 +00001643 insn->numImmediatesConsumed++;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001644
Sean Callanan04cc3072009-12-19 02:59:52 +00001645 return 0;
1646}
1647
1648/*
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001649 * readVVVV - Consumes vvvv from an instruction if it has a VEX prefix.
Sean Callananc3fd5232011-03-15 01:23:15 +00001650 *
1651 * @param insn - The instruction whose operand is to be read.
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001652 * @return - 0 if the vvvv was successfully consumed; nonzero
Sean Callananc3fd5232011-03-15 01:23:15 +00001653 * otherwise.
1654 */
1655static int readVVVV(struct InternalInstruction* insn) {
1656 dbgprintf(insn, "readVVVV()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001657
Richard Smith89ee75d2014-04-20 21:07:34 +00001658 int vvvv;
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001659 if (insn->vectorExtensionType == TYPE_EVEX)
Adam Nemet8ae70502014-06-24 01:42:32 +00001660 vvvv = (v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4 |
1661 vvvvFromEVEX3of4(insn->vectorExtensionPrefix[2]));
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001662 else if (insn->vectorExtensionType == TYPE_VEX_3B)
Richard Smith89ee75d2014-04-20 21:07:34 +00001663 vvvv = vvvvFromVEX3of3(insn->vectorExtensionPrefix[2]);
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001664 else if (insn->vectorExtensionType == TYPE_VEX_2B)
Richard Smith89ee75d2014-04-20 21:07:34 +00001665 vvvv = vvvvFromVEX2of2(insn->vectorExtensionPrefix[1]);
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001666 else if (insn->vectorExtensionType == TYPE_XOP)
Richard Smith89ee75d2014-04-20 21:07:34 +00001667 vvvv = vvvvFromXOP3of3(insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +00001668 else
1669 return -1;
1670
Craig Topper0d0be472011-10-03 08:14:29 +00001671 if (insn->mode != MODE_64BIT)
Richard Smith89ee75d2014-04-20 21:07:34 +00001672 vvvv &= 0x7;
Craig Topper0d0be472011-10-03 08:14:29 +00001673
Richard Smith89ee75d2014-04-20 21:07:34 +00001674 insn->vvvv = static_cast<Reg>(vvvv);
Sean Callananc3fd5232011-03-15 01:23:15 +00001675 return 0;
1676}
1677
1678/*
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001679 * readMaskRegister - Reads an mask register from the opcode field of an
1680 * instruction.
1681 *
1682 * @param insn - The instruction whose opcode field is to be read.
1683 * @return - 0 on success; nonzero otherwise.
1684 */
1685static int readMaskRegister(struct InternalInstruction* insn) {
1686 dbgprintf(insn, "readMaskRegister()");
1687
1688 if (insn->vectorExtensionType != TYPE_EVEX)
1689 return -1;
1690
Richard Smith89ee75d2014-04-20 21:07:34 +00001691 insn->writemask =
1692 static_cast<Reg>(aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]));
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001693 return 0;
1694}
1695
1696/*
Sean Callanan04cc3072009-12-19 02:59:52 +00001697 * readOperands - Consults the specifier for an instruction and consumes all
1698 * operands for that instruction, interpreting them as it goes.
1699 *
1700 * @param insn - The instruction whose operands are to be read and interpreted.
1701 * @return - 0 if all operands could be read; nonzero otherwise.
1702 */
1703static int readOperands(struct InternalInstruction* insn) {
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001704 int hasVVVV, needVVVV;
Craig Topper2ba766a2011-12-30 06:23:39 +00001705 int sawRegImm = 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001706
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001707 dbgprintf(insn, "readOperands()");
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001708
1709 /* If non-zero vvvv specified, need to make sure one of the operands
1710 uses it. */
1711 hasVVVV = !readVVVV(insn);
1712 needVVVV = hasVVVV && (insn->vvvv != 0);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001713
Patrik Hagglund31998382014-04-28 12:12:27 +00001714 for (const auto &Op : x86OperandSets[insn->spec->operands]) {
1715 switch (Op.encoding) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001716 case ENCODING_NONE:
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00001717 case ENCODING_SI:
David Woodhouseb33c2ef2014-01-22 15:08:21 +00001718 case ENCODING_DI:
Sean Callanan04cc3072009-12-19 02:59:52 +00001719 break;
1720 case ENCODING_REG:
Adam Nemet5933c2f2014-07-17 17:04:56 +00001721 CASE_ENCODING_RM:
Sean Callanan04cc3072009-12-19 02:59:52 +00001722 if (readModRM(insn))
1723 return -1;
Patrik Hagglund31998382014-04-28 12:12:27 +00001724 if (fixupReg(insn, &Op))
Sean Callanan04cc3072009-12-19 02:59:52 +00001725 return -1;
Adam Nemet5933c2f2014-07-17 17:04:56 +00001726 // Apply the AVX512 compressed displacement scaling factor.
1727 if (Op.encoding != ENCODING_REG && insn->eaDisplacement == EA_DISP_8)
1728 insn->displacement *= 1 << (Op.encoding - ENCODING_RM);
Sean Callanan04cc3072009-12-19 02:59:52 +00001729 break;
1730 case ENCODING_CB:
1731 case ENCODING_CW:
1732 case ENCODING_CD:
1733 case ENCODING_CP:
1734 case ENCODING_CO:
1735 case ENCODING_CT:
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001736 dbgprintf(insn, "We currently don't hande code-offset encodings");
Sean Callanan04cc3072009-12-19 02:59:52 +00001737 return -1;
1738 case ENCODING_IB:
Craig Topper2ba766a2011-12-30 06:23:39 +00001739 if (sawRegImm) {
Benjamin Kramer9c48f262012-01-04 22:06:45 +00001740 /* Saw a register immediate so don't read again and instead split the
1741 previous immediate. FIXME: This is a hack. */
Benjamin Kramer47aecca2012-01-01 17:55:36 +00001742 insn->immediates[insn->numImmediatesConsumed] =
1743 insn->immediates[insn->numImmediatesConsumed - 1] & 0xf;
1744 ++insn->numImmediatesConsumed;
Craig Topper2ba766a2011-12-30 06:23:39 +00001745 break;
1746 }
Sean Callanan04cc3072009-12-19 02:59:52 +00001747 if (readImmediate(insn, 1))
1748 return -1;
Patrik Hagglund31998382014-04-28 12:12:27 +00001749 if (Op.type == TYPE_XMM128 ||
1750 Op.type == TYPE_XMM256)
Craig Topper2ba766a2011-12-30 06:23:39 +00001751 sawRegImm = 1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001752 break;
1753 case ENCODING_IW:
1754 if (readImmediate(insn, 2))
1755 return -1;
1756 break;
1757 case ENCODING_ID:
1758 if (readImmediate(insn, 4))
1759 return -1;
1760 break;
1761 case ENCODING_IO:
1762 if (readImmediate(insn, 8))
1763 return -1;
1764 break;
1765 case ENCODING_Iv:
Sean Callanan010b3732010-04-02 21:23:51 +00001766 if (readImmediate(insn, insn->immediateSize))
1767 return -1;
Chris Lattnerd4758fc2010-04-16 21:15:15 +00001768 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001769 case ENCODING_Ia:
Sean Callanan010b3732010-04-02 21:23:51 +00001770 if (readImmediate(insn, insn->addressSize))
1771 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001772 break;
1773 case ENCODING_RB:
Sean Callanan010b3732010-04-02 21:23:51 +00001774 if (readOpcodeRegister(insn, 1))
1775 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001776 break;
1777 case ENCODING_RW:
Sean Callanan010b3732010-04-02 21:23:51 +00001778 if (readOpcodeRegister(insn, 2))
1779 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001780 break;
1781 case ENCODING_RD:
Sean Callanan010b3732010-04-02 21:23:51 +00001782 if (readOpcodeRegister(insn, 4))
1783 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001784 break;
1785 case ENCODING_RO:
Sean Callanan010b3732010-04-02 21:23:51 +00001786 if (readOpcodeRegister(insn, 8))
1787 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001788 break;
1789 case ENCODING_Rv:
Sean Callanan010b3732010-04-02 21:23:51 +00001790 if (readOpcodeRegister(insn, 0))
1791 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001792 break;
Craig Topper623b0d62014-01-01 14:22:37 +00001793 case ENCODING_FP:
Sean Callananc3fd5232011-03-15 01:23:15 +00001794 break;
1795 case ENCODING_VVVV:
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001796 needVVVV = 0; /* Mark that we have found a VVVV operand. */
1797 if (!hasVVVV)
Sean Callananc3fd5232011-03-15 01:23:15 +00001798 return -1;
Patrik Hagglund31998382014-04-28 12:12:27 +00001799 if (fixupReg(insn, &Op))
Sean Callananc3fd5232011-03-15 01:23:15 +00001800 return -1;
1801 break;
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001802 case ENCODING_WRITEMASK:
1803 if (readMaskRegister(insn))
1804 return -1;
1805 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001806 case ENCODING_DUP:
1807 break;
1808 default:
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001809 dbgprintf(insn, "Encountered an operand with an unknown encoding.");
Sean Callanan04cc3072009-12-19 02:59:52 +00001810 return -1;
1811 }
1812 }
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001813
1814 /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */
1815 if (needVVVV) return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001816
Sean Callanan04cc3072009-12-19 02:59:52 +00001817 return 0;
1818}
1819
1820/*
1821 * decodeInstruction - Reads and interprets a full instruction provided by the
1822 * user.
1823 *
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001824 * @param insn - A pointer to the instruction to be populated. Must be
Sean Callanan04cc3072009-12-19 02:59:52 +00001825 * pre-allocated.
1826 * @param reader - The function to be used to read the instruction's bytes.
1827 * @param readerArg - A generic argument to be passed to the reader to store
1828 * any internal state.
1829 * @param logger - If non-NULL, the function to be used to write log messages
1830 * and warnings.
1831 * @param loggerArg - A generic argument to be passed to the logger to store
1832 * any internal state.
1833 * @param startLoc - The address (in the reader's address space) of the first
1834 * byte in the instruction.
1835 * @param mode - The mode (real mode, IA-32e, or IA-32e in 64-bit mode) to
1836 * decode the instruction in.
1837 * @return - 0 if the instruction's memory could be read; nonzero if
1838 * not.
1839 */
Richard Smith89ee75d2014-04-20 21:07:34 +00001840int llvm::X86Disassembler::decodeInstruction(
1841 struct InternalInstruction *insn, byteReader_t reader,
1842 const void *readerArg, dlog_t logger, void *loggerArg, const void *miiArg,
1843 uint64_t startLoc, DisassemblerMode mode) {
Daniel Dunbarc745a622009-12-19 03:31:50 +00001844 memset(insn, 0, sizeof(struct InternalInstruction));
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001845
Sean Callanan04cc3072009-12-19 02:59:52 +00001846 insn->reader = reader;
1847 insn->readerArg = readerArg;
1848 insn->dlog = logger;
1849 insn->dlogArg = loggerArg;
1850 insn->startLocation = startLoc;
1851 insn->readerCursor = startLoc;
1852 insn->mode = mode;
1853 insn->numImmediatesConsumed = 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001854
Sean Callanan04cc3072009-12-19 02:59:52 +00001855 if (readPrefixes(insn) ||
1856 readOpcode(insn) ||
Benjamin Kramer478e8de2012-02-11 14:50:54 +00001857 getID(insn, miiArg) ||
Sean Callanan04cc3072009-12-19 02:59:52 +00001858 insn->instructionID == 0 ||
1859 readOperands(insn))
1860 return -1;
Craig Topperb8aec082012-08-01 07:39:18 +00001861
Patrik Hagglund31998382014-04-28 12:12:27 +00001862 insn->operands = x86OperandSets[insn->spec->operands];
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001863
Sean Callanan04cc3072009-12-19 02:59:52 +00001864 insn->length = insn->readerCursor - insn->startLocation;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001865
Benjamin Kramer4f672272010-03-18 12:18:36 +00001866 dbgprintf(insn, "Read from 0x%llx to 0x%llx: length %zu",
1867 startLoc, insn->readerCursor, insn->length);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001868
Sean Callanan04cc3072009-12-19 02:59:52 +00001869 if (insn->length > 15)
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001870 dbgprintf(insn, "Instruction exceeds 15-byte limit");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001871
Sean Callanan04cc3072009-12-19 02:59:52 +00001872 return 0;
1873}