blob: e7a1d7dad98285fb68fab954c302bef1ef1ffe47 [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{
David Blaikie50e4f9e2015-03-23 19:42:36 +0000313 return insn->prefixPresent[prefix] == 1 &&
314 insn->prefixLocations[prefix] == location;
Sean Callanan04cc3072009-12-19 02:59:52 +0000315}
316
317/*
318 * readPrefixes - Consumes all of an instruction's prefix bytes, and marks the
319 * instruction as having them. Also sets the instruction's default operand,
320 * address, and other relevant data sizes to report operands correctly.
321 *
322 * @param insn - The instruction whose prefixes are to be read.
323 * @return - 0 if the instruction could be read until the end of the prefix
324 * bytes, and no prefixes conflicted; nonzero otherwise.
325 */
326static int readPrefixes(struct InternalInstruction* insn) {
Richard Smith5d5061032014-04-20 22:15:37 +0000327 bool isPrefix = true;
328 bool prefixGroups[4] = { false };
Sean Callanan04cc3072009-12-19 02:59:52 +0000329 uint64_t prefixLocation;
Ted Kremenek3c4408c2011-01-23 17:05:06 +0000330 uint8_t byte = 0;
Richard Mitton79917a92013-08-30 21:32:42 +0000331 uint8_t nextByte;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000332
Richard Smith5d5061032014-04-20 22:15:37 +0000333 bool hasAdSize = false;
334 bool hasOpSize = false;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000335
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000336 dbgprintf(insn, "readPrefixes()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000337
Sean Callanan04cc3072009-12-19 02:59:52 +0000338 while (isPrefix) {
339 prefixLocation = insn->readerCursor;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000340
Richard Mitton576ee002013-08-30 21:19:48 +0000341 /* If we fail reading prefixes, just stop here and let the opcode reader deal with it */
Sean Callanan04cc3072009-12-19 02:59:52 +0000342 if (consumeByte(insn, &byte))
Richard Mitton576ee002013-08-30 21:19:48 +0000343 break;
Kevin Enderby014e1cd2012-03-09 17:52:49 +0000344
Benjamin Krameradfc73d2012-03-10 15:10:06 +0000345 /*
Dave Zarzycki07fabee2013-03-25 18:59:38 +0000346 * If the byte is a LOCK/REP/REPNE prefix and not a part of the opcode, then
347 * break and let it be disassembled as a normal "instruction".
Benjamin Krameradfc73d2012-03-10 15:10:06 +0000348 */
Richard Mitton576ee002013-08-30 21:19:48 +0000349 if (insn->readerCursor - 1 == insn->startLocation && byte == 0xf0)
350 break;
351
Dave Zarzycki07fabee2013-03-25 18:59:38 +0000352 if (insn->readerCursor - 1 == insn->startLocation
Richard Mitton576ee002013-08-30 21:19:48 +0000353 && (byte == 0xf2 || byte == 0xf3)
354 && !lookAtByte(insn, &nextByte))
355 {
Kevin Enderby35fd7922013-06-20 22:32:18 +0000356 /*
357 * If the byte is 0xf2 or 0xf3, and any of the following conditions are
358 * met:
359 * - it is followed by a LOCK (0xf0) prefix
360 * - it is followed by an xchg instruction
361 * then it should be disassembled as a xacquire/xrelease not repne/rep.
362 */
363 if ((byte == 0xf2 || byte == 0xf3) &&
364 ((nextByte == 0xf0) |
365 ((nextByte & 0xfe) == 0x86 || (nextByte & 0xf8) == 0x90)))
Richard Smith5d5061032014-04-20 22:15:37 +0000366 insn->xAcquireRelease = true;
Kevin Enderby35fd7922013-06-20 22:32:18 +0000367 /*
368 * Also if the byte is 0xf3, and the following condition is met:
369 * - it is followed by a "mov mem, reg" (opcode 0x88/0x89) or
370 * "mov mem, imm" (opcode 0xc6/0xc7) instructions.
371 * then it should be disassembled as an xrelease not rep.
372 */
373 if (byte == 0xf3 &&
374 (nextByte == 0x88 || nextByte == 0x89 ||
375 nextByte == 0xc6 || nextByte == 0xc7))
Richard Smith5d5061032014-04-20 22:15:37 +0000376 insn->xAcquireRelease = true;
Dave Zarzycki07fabee2013-03-25 18:59:38 +0000377 if (insn->mode == MODE_64BIT && (nextByte & 0xf0) == 0x40) {
378 if (consumeByte(insn, &nextByte))
379 return -1;
380 if (lookAtByte(insn, &nextByte))
381 return -1;
382 unconsumeByte(insn);
383 }
384 if (nextByte != 0x0f && nextByte != 0x90)
385 break;
386 }
387
Sean Callanan04cc3072009-12-19 02:59:52 +0000388 switch (byte) {
389 case 0xf0: /* LOCK */
390 case 0xf2: /* REPNE/REPNZ */
391 case 0xf3: /* REP or REPE/REPZ */
392 if (prefixGroups[0])
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000393 dbgprintf(insn, "Redundant Group 1 prefix");
Richard Smith5d5061032014-04-20 22:15:37 +0000394 prefixGroups[0] = true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000395 setPrefixPresent(insn, byte, prefixLocation);
396 break;
397 case 0x2e: /* CS segment override -OR- Branch not taken */
398 case 0x36: /* SS segment override -OR- Branch taken */
399 case 0x3e: /* DS segment override */
400 case 0x26: /* ES segment override */
401 case 0x64: /* FS segment override */
402 case 0x65: /* GS segment override */
403 switch (byte) {
404 case 0x2e:
405 insn->segmentOverride = SEG_OVERRIDE_CS;
406 break;
407 case 0x36:
408 insn->segmentOverride = SEG_OVERRIDE_SS;
409 break;
410 case 0x3e:
411 insn->segmentOverride = SEG_OVERRIDE_DS;
412 break;
413 case 0x26:
414 insn->segmentOverride = SEG_OVERRIDE_ES;
415 break;
416 case 0x64:
417 insn->segmentOverride = SEG_OVERRIDE_FS;
418 break;
419 case 0x65:
420 insn->segmentOverride = SEG_OVERRIDE_GS;
421 break;
422 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000423 debug("Unhandled override");
424 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +0000425 }
426 if (prefixGroups[1])
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000427 dbgprintf(insn, "Redundant Group 2 prefix");
Richard Smith5d5061032014-04-20 22:15:37 +0000428 prefixGroups[1] = true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000429 setPrefixPresent(insn, byte, prefixLocation);
430 break;
431 case 0x66: /* Operand-size override */
432 if (prefixGroups[2])
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000433 dbgprintf(insn, "Redundant Group 3 prefix");
Richard Smith5d5061032014-04-20 22:15:37 +0000434 prefixGroups[2] = true;
435 hasOpSize = true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000436 setPrefixPresent(insn, byte, prefixLocation);
437 break;
438 case 0x67: /* Address-size override */
439 if (prefixGroups[3])
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000440 dbgprintf(insn, "Redundant Group 4 prefix");
Richard Smith5d5061032014-04-20 22:15:37 +0000441 prefixGroups[3] = true;
442 hasAdSize = true;
Sean Callanan04cc3072009-12-19 02:59:52 +0000443 setPrefixPresent(insn, byte, prefixLocation);
444 break;
445 default: /* Not a prefix byte */
Richard Smith5d5061032014-04-20 22:15:37 +0000446 isPrefix = false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000447 break;
448 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000449
Sean Callanan04cc3072009-12-19 02:59:52 +0000450 if (isPrefix)
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000451 dbgprintf(insn, "Found prefix 0x%hhx", byte);
Sean Callanan04cc3072009-12-19 02:59:52 +0000452 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000453
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000454 insn->vectorExtensionType = TYPE_NO_VEX_XOP;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000455
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000456 if (byte == 0x62) {
457 uint8_t byte1, byte2;
458
459 if (consumeByte(insn, &byte1)) {
460 dbgprintf(insn, "Couldn't read second byte of EVEX prefix");
461 return -1;
462 }
463
464 if (lookAtByte(insn, &byte2)) {
465 dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
466 return -1;
467 }
468
469 if ((insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) &&
470 ((~byte1 & 0xc) == 0xc) && ((byte2 & 0x4) == 0x4)) {
471 insn->vectorExtensionType = TYPE_EVEX;
Craig Topper273515e2014-10-07 07:29:48 +0000472 } else {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000473 unconsumeByte(insn); /* unconsume byte1 */
474 unconsumeByte(insn); /* unconsume byte */
475 insn->necessaryPrefixLocation = insn->readerCursor - 2;
476 }
477
478 if (insn->vectorExtensionType == TYPE_EVEX) {
479 insn->vectorExtensionPrefix[0] = byte;
480 insn->vectorExtensionPrefix[1] = byte1;
481 if (consumeByte(insn, &insn->vectorExtensionPrefix[2])) {
482 dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
483 return -1;
484 }
485 if (consumeByte(insn, &insn->vectorExtensionPrefix[3])) {
486 dbgprintf(insn, "Couldn't read fourth byte of EVEX prefix");
487 return -1;
488 }
489
490 /* We simulate the REX prefix for simplicity's sake */
491 if (insn->mode == MODE_64BIT) {
492 insn->rexPrefix = 0x40
493 | (wFromEVEX3of4(insn->vectorExtensionPrefix[2]) << 3)
494 | (rFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 2)
495 | (xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 1)
496 | (bFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 0);
497 }
498
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000499 dbgprintf(insn, "Found EVEX prefix 0x%hhx 0x%hhx 0x%hhx 0x%hhx",
500 insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
501 insn->vectorExtensionPrefix[2], insn->vectorExtensionPrefix[3]);
502 }
Craig Topper273515e2014-10-07 07:29:48 +0000503 } else if (byte == 0xc4) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000504 uint8_t byte1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000505
Sean Callananc3fd5232011-03-15 01:23:15 +0000506 if (lookAtByte(insn, &byte1)) {
507 dbgprintf(insn, "Couldn't read second byte of VEX");
508 return -1;
509 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000510
Craig Topper45faba92011-09-26 05:12:43 +0000511 if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000512 insn->vectorExtensionType = TYPE_VEX_3B;
Sean Callananc3fd5232011-03-15 01:23:15 +0000513 insn->necessaryPrefixLocation = insn->readerCursor - 1;
Craig Topper273515e2014-10-07 07:29:48 +0000514 } else {
Sean Callanan04cc3072009-12-19 02:59:52 +0000515 unconsumeByte(insn);
516 insn->necessaryPrefixLocation = insn->readerCursor - 1;
517 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000518
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000519 if (insn->vectorExtensionType == TYPE_VEX_3B) {
520 insn->vectorExtensionPrefix[0] = byte;
521 consumeByte(insn, &insn->vectorExtensionPrefix[1]);
522 consumeByte(insn, &insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +0000523
524 /* We simulate the REX prefix for simplicity's sake */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000525
Craig Topper31854ba2011-10-03 07:51:09 +0000526 if (insn->mode == MODE_64BIT) {
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000527 insn->rexPrefix = 0x40
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000528 | (wFromVEX3of3(insn->vectorExtensionPrefix[2]) << 3)
529 | (rFromVEX2of3(insn->vectorExtensionPrefix[1]) << 2)
530 | (xFromVEX2of3(insn->vectorExtensionPrefix[1]) << 1)
531 | (bFromVEX2of3(insn->vectorExtensionPrefix[1]) << 0);
Craig Topper31854ba2011-10-03 07:51:09 +0000532 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000533
Craig Topper9e3e38a2013-10-03 05:17:48 +0000534 dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx 0x%hhx",
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000535 insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
536 insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +0000537 }
Craig Topper273515e2014-10-07 07:29:48 +0000538 } else if (byte == 0xc5) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000539 uint8_t byte1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000540
Sean Callananc3fd5232011-03-15 01:23:15 +0000541 if (lookAtByte(insn, &byte1)) {
542 dbgprintf(insn, "Couldn't read second byte of VEX");
543 return -1;
544 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000545
Craig Topper45faba92011-09-26 05:12:43 +0000546 if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000547 insn->vectorExtensionType = TYPE_VEX_2B;
Craig Topper273515e2014-10-07 07:29:48 +0000548 } else {
Sean Callananc3fd5232011-03-15 01:23:15 +0000549 unconsumeByte(insn);
550 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000551
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000552 if (insn->vectorExtensionType == TYPE_VEX_2B) {
553 insn->vectorExtensionPrefix[0] = byte;
554 consumeByte(insn, &insn->vectorExtensionPrefix[1]);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000555
Craig Topper31854ba2011-10-03 07:51:09 +0000556 if (insn->mode == MODE_64BIT) {
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000557 insn->rexPrefix = 0x40
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000558 | (rFromVEX2of2(insn->vectorExtensionPrefix[1]) << 2);
Craig Topper31854ba2011-10-03 07:51:09 +0000559 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000560
Craig Topper273515e2014-10-07 07:29:48 +0000561 switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000562 default:
563 break;
564 case VEX_PREFIX_66:
Richard Smith5d5061032014-04-20 22:15:37 +0000565 hasOpSize = true;
Sean Callananc3fd5232011-03-15 01:23:15 +0000566 break;
567 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000568
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000569 dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx",
570 insn->vectorExtensionPrefix[0],
571 insn->vectorExtensionPrefix[1]);
Craig Topper9e3e38a2013-10-03 05:17:48 +0000572 }
Craig Topper273515e2014-10-07 07:29:48 +0000573 } else if (byte == 0x8f) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000574 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;
Craig Topper273515e2014-10-07 07:29:48 +0000584 } else {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000585 unconsumeByte(insn);
586 insn->necessaryPrefixLocation = insn->readerCursor - 1;
587 }
588
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000589 if (insn->vectorExtensionType == TYPE_XOP) {
590 insn->vectorExtensionPrefix[0] = byte;
591 consumeByte(insn, &insn->vectorExtensionPrefix[1]);
592 consumeByte(insn, &insn->vectorExtensionPrefix[2]);
Craig Topper9e3e38a2013-10-03 05:17:48 +0000593
594 /* We simulate the REX prefix for simplicity's sake */
595
596 if (insn->mode == MODE_64BIT) {
597 insn->rexPrefix = 0x40
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000598 | (wFromXOP3of3(insn->vectorExtensionPrefix[2]) << 3)
599 | (rFromXOP2of3(insn->vectorExtensionPrefix[1]) << 2)
600 | (xFromXOP2of3(insn->vectorExtensionPrefix[1]) << 1)
601 | (bFromXOP2of3(insn->vectorExtensionPrefix[1]) << 0);
Craig Topper9e3e38a2013-10-03 05:17:48 +0000602 }
603
Craig Topper273515e2014-10-07 07:29:48 +0000604 switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000605 default:
606 break;
607 case VEX_PREFIX_66:
Richard Smith5d5061032014-04-20 22:15:37 +0000608 hasOpSize = true;
Craig Topper9e3e38a2013-10-03 05:17:48 +0000609 break;
610 }
611
612 dbgprintf(insn, "Found XOP prefix 0x%hhx 0x%hhx 0x%hhx",
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000613 insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
614 insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +0000615 }
Craig Topper273515e2014-10-07 07:29:48 +0000616 } else {
Sean Callananc3fd5232011-03-15 01:23:15 +0000617 if (insn->mode == MODE_64BIT) {
618 if ((byte & 0xf0) == 0x40) {
619 uint8_t opcodeByte;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000620
Sean Callananc3fd5232011-03-15 01:23:15 +0000621 if (lookAtByte(insn, &opcodeByte) || ((opcodeByte & 0xf0) == 0x40)) {
622 dbgprintf(insn, "Redundant REX prefix");
623 return -1;
624 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000625
Sean Callananc3fd5232011-03-15 01:23:15 +0000626 insn->rexPrefix = byte;
627 insn->necessaryPrefixLocation = insn->readerCursor - 2;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000628
Sean Callananc3fd5232011-03-15 01:23:15 +0000629 dbgprintf(insn, "Found REX prefix 0x%hhx", byte);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000630 } else {
Sean Callananc3fd5232011-03-15 01:23:15 +0000631 unconsumeByte(insn);
632 insn->necessaryPrefixLocation = insn->readerCursor - 1;
633 }
634 } else {
635 unconsumeByte(insn);
636 insn->necessaryPrefixLocation = insn->readerCursor - 1;
637 }
638 }
639
Sean Callanan04cc3072009-12-19 02:59:52 +0000640 if (insn->mode == MODE_16BIT) {
641 insn->registerSize = (hasOpSize ? 4 : 2);
642 insn->addressSize = (hasAdSize ? 4 : 2);
643 insn->displacementSize = (hasAdSize ? 4 : 2);
644 insn->immediateSize = (hasOpSize ? 4 : 2);
645 } else if (insn->mode == MODE_32BIT) {
646 insn->registerSize = (hasOpSize ? 2 : 4);
647 insn->addressSize = (hasAdSize ? 2 : 4);
648 insn->displacementSize = (hasAdSize ? 2 : 4);
Sean Callanan9f6c6222010-10-22 01:24:11 +0000649 insn->immediateSize = (hasOpSize ? 2 : 4);
Sean Callanan04cc3072009-12-19 02:59:52 +0000650 } else if (insn->mode == MODE_64BIT) {
651 if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
652 insn->registerSize = 8;
653 insn->addressSize = (hasAdSize ? 4 : 8);
654 insn->displacementSize = 4;
655 insn->immediateSize = 4;
656 } else if (insn->rexPrefix) {
657 insn->registerSize = (hasOpSize ? 2 : 4);
658 insn->addressSize = (hasAdSize ? 4 : 8);
659 insn->displacementSize = (hasOpSize ? 2 : 4);
660 insn->immediateSize = (hasOpSize ? 2 : 4);
661 } else {
662 insn->registerSize = (hasOpSize ? 2 : 4);
663 insn->addressSize = (hasAdSize ? 4 : 8);
664 insn->displacementSize = (hasOpSize ? 2 : 4);
665 insn->immediateSize = (hasOpSize ? 2 : 4);
666 }
667 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000668
Sean Callanan04cc3072009-12-19 02:59:52 +0000669 return 0;
670}
671
672/*
673 * readOpcode - Reads the opcode (excepting the ModR/M byte in the case of
674 * extended or escape opcodes).
675 *
676 * @param insn - The instruction whose opcode is to be read.
677 * @return - 0 if the opcode could be read successfully; nonzero otherwise.
678 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000679static int readOpcode(struct InternalInstruction* insn) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000680 /* Determine the length of the primary opcode */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000681
Sean Callanan04cc3072009-12-19 02:59:52 +0000682 uint8_t current;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000683
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000684 dbgprintf(insn, "readOpcode()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000685
Sean Callanan04cc3072009-12-19 02:59:52 +0000686 insn->opcodeType = ONEBYTE;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000687
Craig Topper273515e2014-10-07 07:29:48 +0000688 if (insn->vectorExtensionType == TYPE_EVEX) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000689 switch (mmFromEVEX2of4(insn->vectorExtensionPrefix[1])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000690 default:
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000691 dbgprintf(insn, "Unhandled mm field for instruction (0x%hhx)",
692 mmFromEVEX2of4(insn->vectorExtensionPrefix[1]));
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000693 return -1;
Sean Callananc3fd5232011-03-15 01:23:15 +0000694 case VEX_LOB_0F:
Sean Callananc3fd5232011-03-15 01:23:15 +0000695 insn->opcodeType = TWOBYTE;
696 return consumeByte(insn, &insn->opcode);
697 case VEX_LOB_0F38:
Sean Callananc3fd5232011-03-15 01:23:15 +0000698 insn->opcodeType = THREEBYTE_38;
699 return consumeByte(insn, &insn->opcode);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000700 case VEX_LOB_0F3A:
Sean Callananc3fd5232011-03-15 01:23:15 +0000701 insn->opcodeType = THREEBYTE_3A;
702 return consumeByte(insn, &insn->opcode);
703 }
Craig Topper273515e2014-10-07 07:29:48 +0000704 } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000705 switch (mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])) {
706 default:
707 dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
708 mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
709 return -1;
710 case VEX_LOB_0F:
711 insn->opcodeType = TWOBYTE;
712 return consumeByte(insn, &insn->opcode);
713 case VEX_LOB_0F38:
714 insn->opcodeType = THREEBYTE_38;
715 return consumeByte(insn, &insn->opcode);
716 case VEX_LOB_0F3A:
717 insn->opcodeType = THREEBYTE_3A;
718 return consumeByte(insn, &insn->opcode);
719 }
Craig Topper273515e2014-10-07 07:29:48 +0000720 } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000721 insn->opcodeType = TWOBYTE;
722 return consumeByte(insn, &insn->opcode);
Craig Topper273515e2014-10-07 07:29:48 +0000723 } else if (insn->vectorExtensionType == TYPE_XOP) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000724 switch (mmmmmFromXOP2of3(insn->vectorExtensionPrefix[1])) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000725 default:
726 dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000727 mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
Craig Topper9e3e38a2013-10-03 05:17:48 +0000728 return -1;
729 case XOP_MAP_SELECT_8:
730 insn->opcodeType = XOP8_MAP;
731 return consumeByte(insn, &insn->opcode);
732 case XOP_MAP_SELECT_9:
733 insn->opcodeType = XOP9_MAP;
734 return consumeByte(insn, &insn->opcode);
735 case XOP_MAP_SELECT_A:
736 insn->opcodeType = XOPA_MAP;
737 return consumeByte(insn, &insn->opcode);
738 }
739 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000740
Sean Callanan04cc3072009-12-19 02:59:52 +0000741 if (consumeByte(insn, &current))
742 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000743
Sean Callanan04cc3072009-12-19 02:59:52 +0000744 if (current == 0x0f) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000745 dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000746
Sean Callanan04cc3072009-12-19 02:59:52 +0000747 if (consumeByte(insn, &current))
748 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000749
Sean Callanan04cc3072009-12-19 02:59:52 +0000750 if (current == 0x38) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000751 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000752
Sean Callanan04cc3072009-12-19 02:59:52 +0000753 if (consumeByte(insn, &current))
754 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000755
Sean Callanan04cc3072009-12-19 02:59:52 +0000756 insn->opcodeType = THREEBYTE_38;
757 } else if (current == 0x3a) {
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_3A;
764 } else {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000765 dbgprintf(insn, "Didn't find a three-byte escape prefix");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000766
Sean Callanan04cc3072009-12-19 02:59:52 +0000767 insn->opcodeType = TWOBYTE;
768 }
769 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000770
Sean Callanan04cc3072009-12-19 02:59:52 +0000771 /*
772 * At this point we have consumed the full opcode.
773 * Anything we consume from here on must be unconsumed.
774 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000775
Sean Callanan04cc3072009-12-19 02:59:52 +0000776 insn->opcode = current;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000777
Sean Callanan04cc3072009-12-19 02:59:52 +0000778 return 0;
779}
780
781static int readModRM(struct InternalInstruction* insn);
782
783/*
784 * getIDWithAttrMask - Determines the ID of an instruction, consuming
785 * the ModR/M byte as appropriate for extended and escape opcodes,
786 * and using a supplied attribute mask.
787 *
788 * @param instructionID - A pointer whose target is filled in with the ID of the
789 * instruction.
790 * @param insn - The instruction whose ID is to be determined.
791 * @param attrMask - The attribute mask to search.
792 * @return - 0 if the ModR/M could be read when needed or was not
793 * needed; nonzero otherwise.
794 */
795static int getIDWithAttrMask(uint16_t* instructionID,
796 struct InternalInstruction* insn,
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000797 uint16_t attrMask) {
Richard Smith5d5061032014-04-20 22:15:37 +0000798 bool hasModRMExtension;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000799
Richard Smith89ee75d2014-04-20 21:07:34 +0000800 InstructionContext instructionClass = contextForAttrs(attrMask);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000801
Sean Callanan04cc3072009-12-19 02:59:52 +0000802 hasModRMExtension = modRMRequired(insn->opcodeType,
803 instructionClass,
804 insn->opcode);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000805
Sean Callanan04cc3072009-12-19 02:59:52 +0000806 if (hasModRMExtension) {
Rafael Espindola9f9a1062011-01-06 16:48:42 +0000807 if (readModRM(insn))
808 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000809
Sean Callanan04cc3072009-12-19 02:59:52 +0000810 *instructionID = decode(insn->opcodeType,
811 instructionClass,
812 insn->opcode,
813 insn->modRM);
814 } else {
815 *instructionID = decode(insn->opcodeType,
816 instructionClass,
817 insn->opcode,
818 0);
819 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000820
Sean Callanan04cc3072009-12-19 02:59:52 +0000821 return 0;
822}
823
824/*
825 * is16BitEquivalent - Determines whether two instruction names refer to
826 * equivalent instructions but one is 16-bit whereas the other is not.
827 *
828 * @param orig - The instruction that is not 16-bit
829 * @param equiv - The instruction that is 16-bit
830 */
Richard Smith5d5061032014-04-20 22:15:37 +0000831static bool is16BitEquivalent(const char* orig, const char* equiv) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000832 off_t i;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000833
Sean Callanan010b3732010-04-02 21:23:51 +0000834 for (i = 0;; i++) {
835 if (orig[i] == '\0' && equiv[i] == '\0')
Richard Smith5d5061032014-04-20 22:15:37 +0000836 return true;
Sean Callanan010b3732010-04-02 21:23:51 +0000837 if (orig[i] == '\0' || equiv[i] == '\0')
Richard Smith5d5061032014-04-20 22:15:37 +0000838 return false;
Sean Callanan010b3732010-04-02 21:23:51 +0000839 if (orig[i] != equiv[i]) {
840 if ((orig[i] == 'Q' || orig[i] == 'L') && equiv[i] == 'W')
Sean Callanan04cc3072009-12-19 02:59:52 +0000841 continue;
Sean Callanan010b3732010-04-02 21:23:51 +0000842 if ((orig[i] == '6' || orig[i] == '3') && equiv[i] == '1')
Sean Callanan04cc3072009-12-19 02:59:52 +0000843 continue;
Sean Callanan010b3732010-04-02 21:23:51 +0000844 if ((orig[i] == '4' || orig[i] == '2') && equiv[i] == '6')
Sean Callanan04cc3072009-12-19 02:59:52 +0000845 continue;
Richard Smith5d5061032014-04-20 22:15:37 +0000846 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000847 }
848 }
849}
850
851/*
Craig Topper0676b902014-10-07 07:29:50 +0000852 * is64Bit - Determines whether this instruction is a 64-bit instruction.
853 *
854 * @param name - The instruction that is not 16-bit
855 */
856static bool is64Bit(const char* name) {
857 off_t i;
858
859 for (i = 0;; ++i) {
860 if (name[i] == '\0')
861 return false;
862 if (name[i] == '6' && name[i+1] == '4')
863 return true;
864 }
865}
866
867/*
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000868 * getID - Determines the ID of an instruction, consuming the ModR/M byte as
869 * appropriate for extended and escape opcodes. Determines the attributes and
Sean Callanan04cc3072009-12-19 02:59:52 +0000870 * context for the instruction before doing so.
871 *
872 * @param insn - The instruction whose ID is to be determined.
873 * @return - 0 if the ModR/M could be read when needed or was not needed;
874 * nonzero otherwise.
875 */
Roman Divacky67923802012-09-05 21:17:34 +0000876static int getID(struct InternalInstruction* insn, const void *miiArg) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000877 uint16_t attrMask;
Sean Callanan04cc3072009-12-19 02:59:52 +0000878 uint16_t instructionID;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000879
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000880 dbgprintf(insn, "getID()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000881
Sean Callanan04cc3072009-12-19 02:59:52 +0000882 attrMask = ATTR_NONE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000883
Sean Callanan04cc3072009-12-19 02:59:52 +0000884 if (insn->mode == MODE_64BIT)
885 attrMask |= ATTR_64BIT;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000886
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000887 if (insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
888 attrMask |= (insn->vectorExtensionType == TYPE_EVEX) ? ATTR_EVEX : ATTR_VEX;
Sean Callananc3fd5232011-03-15 01:23:15 +0000889
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000890 if (insn->vectorExtensionType == TYPE_EVEX) {
891 switch (ppFromEVEX3of4(insn->vectorExtensionPrefix[2])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000892 case VEX_PREFIX_66:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000893 attrMask |= ATTR_OPSIZE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000894 break;
895 case VEX_PREFIX_F3:
896 attrMask |= ATTR_XS;
897 break;
898 case VEX_PREFIX_F2:
899 attrMask |= ATTR_XD;
900 break;
901 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000902
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000903 if (zFromEVEX4of4(insn->vectorExtensionPrefix[3]))
904 attrMask |= ATTR_EVEXKZ;
905 if (bFromEVEX4of4(insn->vectorExtensionPrefix[3]))
906 attrMask |= ATTR_EVEXB;
907 if (aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]))
908 attrMask |= ATTR_EVEXK;
909 if (lFromEVEX4of4(insn->vectorExtensionPrefix[3]))
910 attrMask |= ATTR_EVEXL;
911 if (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
912 attrMask |= ATTR_EVEXL2;
Craig Topper273515e2014-10-07 07:29:48 +0000913 } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000914 switch (ppFromVEX3of3(insn->vectorExtensionPrefix[2])) {
915 case VEX_PREFIX_66:
916 attrMask |= ATTR_OPSIZE;
917 break;
918 case VEX_PREFIX_F3:
919 attrMask |= ATTR_XS;
920 break;
921 case VEX_PREFIX_F2:
922 attrMask |= ATTR_XD;
923 break;
924 }
925
926 if (lFromVEX3of3(insn->vectorExtensionPrefix[2]))
Sean Callananc3fd5232011-03-15 01:23:15 +0000927 attrMask |= ATTR_VEXL;
Craig Topper273515e2014-10-07 07:29:48 +0000928 } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000929 switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000930 case VEX_PREFIX_66:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000931 attrMask |= ATTR_OPSIZE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000932 break;
933 case VEX_PREFIX_F3:
934 attrMask |= ATTR_XS;
935 break;
936 case VEX_PREFIX_F2:
937 attrMask |= ATTR_XD;
938 break;
939 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000940
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000941 if (lFromVEX2of2(insn->vectorExtensionPrefix[1]))
Craig Topper9e3e38a2013-10-03 05:17:48 +0000942 attrMask |= ATTR_VEXL;
Craig Topper273515e2014-10-07 07:29:48 +0000943 } else if (insn->vectorExtensionType == TYPE_XOP) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000944 switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000945 case VEX_PREFIX_66:
946 attrMask |= ATTR_OPSIZE;
947 break;
948 case VEX_PREFIX_F3:
949 attrMask |= ATTR_XS;
950 break;
951 case VEX_PREFIX_F2:
952 attrMask |= ATTR_XD;
953 break;
954 }
955
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000956 if (lFromXOP3of3(insn->vectorExtensionPrefix[2]))
Sean Callananc3fd5232011-03-15 01:23:15 +0000957 attrMask |= ATTR_VEXL;
Craig Topper273515e2014-10-07 07:29:48 +0000958 } else {
Sean Callananc3fd5232011-03-15 01:23:15 +0000959 return -1;
960 }
Craig Topper273515e2014-10-07 07:29:48 +0000961 } else {
David Woodhouse5cf4c672014-01-20 12:02:35 +0000962 if (insn->mode != MODE_16BIT && isPrefixAtLocation(insn, 0x66, insn->necessaryPrefixLocation))
Sean Callananc3fd5232011-03-15 01:23:15 +0000963 attrMask |= ATTR_OPSIZE;
Craig Topper6491c802012-02-27 01:54:29 +0000964 else if (isPrefixAtLocation(insn, 0x67, insn->necessaryPrefixLocation))
965 attrMask |= ATTR_ADSIZE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000966 else if (isPrefixAtLocation(insn, 0xf3, insn->necessaryPrefixLocation))
967 attrMask |= ATTR_XS;
968 else if (isPrefixAtLocation(insn, 0xf2, insn->necessaryPrefixLocation))
969 attrMask |= ATTR_XD;
Sean Callananc3fd5232011-03-15 01:23:15 +0000970 }
971
Craig Topperf18c8962011-10-04 06:30:42 +0000972 if (insn->rexPrefix & 0x08)
973 attrMask |= ATTR_REXW;
Craig Topperf01f1b52011-11-06 23:04:08 +0000974
David Woodhouse9c74fdb2014-01-20 12:02:48 +0000975 /*
976 * JCXZ/JECXZ need special handling for 16-bit mode because the meaning
977 * of the AdSize prefix is inverted w.r.t. 32-bit mode.
978 */
Craig Topper6e518772014-12-31 07:07:11 +0000979 if (insn->mode == MODE_16BIT && insn->opcodeType == ONEBYTE &&
980 insn->opcode == 0xE3)
981 attrMask ^= ATTR_ADSIZE;
David Woodhouse9c74fdb2014-01-20 12:02:48 +0000982
Vedant Kumarbf891b12015-08-26 16:20:29 +0000983 /*
984 * In 64-bit mode all f64 superscripted opcodes ignore opcode size prefix
985 * CALL/JMP/JCC instructions need to ignore 0x66 and consume 4 bytes
986 */
987
988 if (insn->mode == MODE_64BIT &&
989 isPrefixAtLocation(insn, 0x66, insn->necessaryPrefixLocation)) {
990 switch (insn->opcode) {
991 case 0xE8:
992 case 0xE9:
993 if (insn->opcodeType ==
994 ONEBYTE) { // breaks psubsb and other mmx instructions otherwise
995 attrMask ^= ATTR_OPSIZE;
996 insn->immediateSize = 4;
997 insn->displacementSize = 4;
998 }
999 break;
1000 case 0x82:
1001 case 0x83:
1002 case 0x84:
1003 case 0x85:
1004 case 0x86:
1005 case 0x87:
1006 case 0x88:
1007 case 0x89:
1008 case 0x8A:
1009 case 0x8B:
1010 case 0x8C:
1011 case 0x8D:
1012 case 0x8E:
1013 case 0x8F:
1014 if (insn->opcodeType ==
1015 TWOBYTE) { // breaks lea and three byte ops otherwise
1016 attrMask ^= ATTR_OPSIZE;
1017 insn->immediateSize = 4;
1018 insn->displacementSize = 4; // otherwise not sign extended
1019 }
1020 break;
1021 }
1022 }
1023
Craig Topper6e518772014-12-31 07:07:11 +00001024 if (getIDWithAttrMask(&instructionID, insn, attrMask))
1025 return -1;
David Woodhouse9c74fdb2014-01-20 12:02:48 +00001026
Sean Callanan04cc3072009-12-19 02:59:52 +00001027 /* The following clauses compensate for limitations of the tables. */
Craig Topperf01f1b52011-11-06 23:04:08 +00001028
Craig Topper0676b902014-10-07 07:29:50 +00001029 if (insn->mode != MODE_64BIT &&
1030 insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1031 /*
1032 * The tables can't distinquish between cases where the W-bit is used to
1033 * select register size and cases where its a required part of the opcode.
1034 */
1035 if ((insn->vectorExtensionType == TYPE_EVEX &&
1036 wFromEVEX3of4(insn->vectorExtensionPrefix[2])) ||
1037 (insn->vectorExtensionType == TYPE_VEX_3B &&
1038 wFromVEX3of3(insn->vectorExtensionPrefix[2])) ||
1039 (insn->vectorExtensionType == TYPE_XOP &&
1040 wFromXOP3of3(insn->vectorExtensionPrefix[2]))) {
1041
1042 uint16_t instructionIDWithREXW;
1043 if (getIDWithAttrMask(&instructionIDWithREXW,
1044 insn, attrMask | ATTR_REXW)) {
1045 insn->instructionID = instructionID;
1046 insn->spec = specifierForUID(instructionID);
1047 return 0;
1048 }
1049
1050 const char *SpecName = GetInstrName(instructionIDWithREXW, miiArg);
1051 // If not a 64-bit instruction. Switch the opcode.
1052 if (!is64Bit(SpecName)) {
1053 insn->instructionID = instructionIDWithREXW;
1054 insn->spec = specifierForUID(instructionIDWithREXW);
1055 return 0;
1056 }
1057 }
1058 }
1059
Craig Topper99bcab72014-12-31 07:07:31 +00001060 /*
1061 * Absolute moves need special handling.
1062 * -For 16-bit mode because the meaning of the AdSize and OpSize prefixes are
1063 * inverted w.r.t.
1064 * -For 32-bit mode we need to ensure the ADSIZE prefix is observed in
1065 * any position.
1066 */
1067 if (insn->opcodeType == ONEBYTE && ((insn->opcode & 0xFC) == 0xA0)) {
1068 /* Make sure we observed the prefixes in any position. */
1069 if (insn->prefixPresent[0x67])
1070 attrMask |= ATTR_ADSIZE;
1071 if (insn->prefixPresent[0x66])
1072 attrMask |= ATTR_OPSIZE;
1073
1074 /* In 16-bit, invert the attributes. */
1075 if (insn->mode == MODE_16BIT)
1076 attrMask ^= ATTR_ADSIZE | ATTR_OPSIZE;
1077
1078 if (getIDWithAttrMask(&instructionID, insn, attrMask))
1079 return -1;
1080
1081 insn->instructionID = instructionID;
1082 insn->spec = specifierForUID(instructionID);
1083 return 0;
1084 }
1085
David Woodhouse5cf4c672014-01-20 12:02:35 +00001086 if ((insn->mode == MODE_16BIT || insn->prefixPresent[0x66]) &&
1087 !(attrMask & ATTR_OPSIZE)) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001088 /*
1089 * The instruction tables make no distinction between instructions that
1090 * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a
1091 * particular spot (i.e., many MMX operations). In general we're
1092 * conservative, but in the specific case where OpSize is present but not
1093 * in the right place we check if there's a 16-bit operation.
1094 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001095
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +00001096 const struct InstructionSpecifier *spec;
Sean Callanan04cc3072009-12-19 02:59:52 +00001097 uint16_t instructionIDWithOpsize;
Benjamin Kramer915e3d92012-02-11 16:01:02 +00001098 const char *specName, *specWithOpSizeName;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001099
Sean Callanan04cc3072009-12-19 02:59:52 +00001100 spec = specifierForUID(instructionID);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001101
Sean Callanan04cc3072009-12-19 02:59:52 +00001102 if (getIDWithAttrMask(&instructionIDWithOpsize,
1103 insn,
1104 attrMask | ATTR_OPSIZE)) {
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001105 /*
Sean Callanan04cc3072009-12-19 02:59:52 +00001106 * ModRM required with OpSize but not present; give up and return version
1107 * without OpSize set
1108 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001109
Sean Callanan04cc3072009-12-19 02:59:52 +00001110 insn->instructionID = instructionID;
1111 insn->spec = spec;
1112 return 0;
1113 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001114
Richard Smith89ee75d2014-04-20 21:07:34 +00001115 specName = GetInstrName(instructionID, miiArg);
1116 specWithOpSizeName = GetInstrName(instructionIDWithOpsize, miiArg);
Benjamin Kramer478e8de2012-02-11 14:50:54 +00001117
David Woodhouse5cf4c672014-01-20 12:02:35 +00001118 if (is16BitEquivalent(specName, specWithOpSizeName) &&
1119 (insn->mode == MODE_16BIT) ^ insn->prefixPresent[0x66]) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001120 insn->instructionID = instructionIDWithOpsize;
Benjamin Kramer915e3d92012-02-11 16:01:02 +00001121 insn->spec = specifierForUID(instructionIDWithOpsize);
Sean Callanan04cc3072009-12-19 02:59:52 +00001122 } else {
1123 insn->instructionID = instructionID;
1124 insn->spec = spec;
1125 }
1126 return 0;
1127 }
Craig Topper21c33652011-10-02 16:56:09 +00001128
1129 if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 &&
1130 insn->rexPrefix & 0x01) {
1131 /*
1132 * NOOP shouldn't decode as NOOP if REX.b is set. Instead
1133 * it should decode as XCHG %r8, %eax.
1134 */
1135
1136 const struct InstructionSpecifier *spec;
1137 uint16_t instructionIDWithNewOpcode;
1138 const struct InstructionSpecifier *specWithNewOpcode;
1139
1140 spec = specifierForUID(instructionID);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001141
Craig Topperb58a9662011-10-05 03:29:32 +00001142 /* Borrow opcode from one of the other XCHGar opcodes */
Craig Topper21c33652011-10-02 16:56:09 +00001143 insn->opcode = 0x91;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001144
Craig Topper21c33652011-10-02 16:56:09 +00001145 if (getIDWithAttrMask(&instructionIDWithNewOpcode,
1146 insn,
1147 attrMask)) {
1148 insn->opcode = 0x90;
1149
1150 insn->instructionID = instructionID;
1151 insn->spec = spec;
1152 return 0;
1153 }
1154
1155 specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode);
1156
Craig Topperb58a9662011-10-05 03:29:32 +00001157 /* Change back */
Craig Topper21c33652011-10-02 16:56:09 +00001158 insn->opcode = 0x90;
1159
1160 insn->instructionID = instructionIDWithNewOpcode;
1161 insn->spec = specWithNewOpcode;
1162
1163 return 0;
1164 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001165
Sean Callanan04cc3072009-12-19 02:59:52 +00001166 insn->instructionID = instructionID;
1167 insn->spec = specifierForUID(insn->instructionID);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001168
Sean Callanan04cc3072009-12-19 02:59:52 +00001169 return 0;
1170}
1171
1172/*
1173 * readSIB - Consumes the SIB byte to determine addressing information for an
1174 * instruction.
1175 *
1176 * @param insn - The instruction whose SIB byte is to be read.
1177 * @return - 0 if the SIB byte was successfully read; nonzero otherwise.
1178 */
1179static int readSIB(struct InternalInstruction* insn) {
Richard Smith89ee75d2014-04-20 21:07:34 +00001180 SIBIndex sibIndexBase = SIB_INDEX_NONE;
1181 SIBBase sibBaseBase = SIB_BASE_NONE;
Sean Callanan04cc3072009-12-19 02:59:52 +00001182 uint8_t index, base;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001183
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001184 dbgprintf(insn, "readSIB()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001185
Sean Callanan04cc3072009-12-19 02:59:52 +00001186 if (insn->consumedSIB)
1187 return 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001188
Richard Smith5d5061032014-04-20 22:15:37 +00001189 insn->consumedSIB = true;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001190
Sean Callanan04cc3072009-12-19 02:59:52 +00001191 switch (insn->addressSize) {
1192 case 2:
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001193 dbgprintf(insn, "SIB-based addressing doesn't work in 16-bit mode");
Sean Callanan04cc3072009-12-19 02:59:52 +00001194 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001195 case 4:
1196 sibIndexBase = SIB_INDEX_EAX;
1197 sibBaseBase = SIB_BASE_EAX;
1198 break;
1199 case 8:
1200 sibIndexBase = SIB_INDEX_RAX;
1201 sibBaseBase = SIB_BASE_RAX;
1202 break;
1203 }
1204
1205 if (consumeByte(insn, &insn->sib))
1206 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001207
Sean Callanan04cc3072009-12-19 02:59:52 +00001208 index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
Douglas Katzmanfcda6f82015-06-24 22:04:55 +00001209
1210 // FIXME: The fifth bit (bit index 4) is only to be used for instructions
1211 // that understand VSIB indexing. ORing the bit in here is mildy dangerous
1212 // because performing math on an 'enum SIBIndex' can produce garbage.
1213 // Excluding the "none" value, it should cover 6 spaces of register names:
1214 // - 16 possibilities for 16-bit GPR starting at SIB_INDEX_BX_SI
1215 // - 16 possibilities for 32-bit GPR starting at SIB_INDEX_EAX
1216 // - 16 possibilities for 64-bit GPR starting at SIB_INDEX_RAX
1217 // - 32 possibilities for each of XMM, YMM, ZMM registers
1218 // When sibIndexBase gets assigned SIB_INDEX_RAX as it does in 64-bit mode,
1219 // summing in a fully decoded index between 0 and 31 can end up with a value
1220 // that looks like something in the low half of the XMM range.
1221 // translateRMMemory() tries to reverse the damage, with only partial success,
1222 // as evidenced by known bugs in "test/MC/Disassembler/X86/x86-64.txt"
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001223 if (insn->vectorExtensionType == TYPE_EVEX)
1224 index |= v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001225
Douglas Katzmanfcda6f82015-06-24 22:04:55 +00001226 if (index == 0x4) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001227 insn->sibIndex = SIB_INDEX_NONE;
Douglas Katzmanfcda6f82015-06-24 22:04:55 +00001228 } else {
Benjamin Kramer25bddae2011-02-27 18:13:53 +00001229 insn->sibIndex = (SIBIndex)(sibIndexBase + index);
Sean Callanan04cc3072009-12-19 02:59:52 +00001230 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001231
Douglas Katzmanfcda6f82015-06-24 22:04:55 +00001232 insn->sibScale = 1 << scaleFromSIB(insn->sib);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001233
Sean Callanan04cc3072009-12-19 02:59:52 +00001234 base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001235
Sean Callanan04cc3072009-12-19 02:59:52 +00001236 switch (base) {
1237 case 0x5:
Craig Topperfae5ac22014-02-17 10:03:43 +00001238 case 0xd:
Sean Callanan04cc3072009-12-19 02:59:52 +00001239 switch (modFromModRM(insn->modRM)) {
1240 case 0x0:
1241 insn->eaDisplacement = EA_DISP_32;
1242 insn->sibBase = SIB_BASE_NONE;
1243 break;
1244 case 0x1:
1245 insn->eaDisplacement = EA_DISP_8;
Craig Topperfae5ac22014-02-17 10:03:43 +00001246 insn->sibBase = (SIBBase)(sibBaseBase + base);
Sean Callanan04cc3072009-12-19 02:59:52 +00001247 break;
1248 case 0x2:
1249 insn->eaDisplacement = EA_DISP_32;
Craig Topperfae5ac22014-02-17 10:03:43 +00001250 insn->sibBase = (SIBBase)(sibBaseBase + base);
Sean Callanan04cc3072009-12-19 02:59:52 +00001251 break;
1252 case 0x3:
Sean Callanan010b3732010-04-02 21:23:51 +00001253 debug("Cannot have Mod = 0b11 and a SIB byte");
1254 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001255 }
1256 break;
1257 default:
Benjamin Kramer25bddae2011-02-27 18:13:53 +00001258 insn->sibBase = (SIBBase)(sibBaseBase + base);
Sean Callanan04cc3072009-12-19 02:59:52 +00001259 break;
1260 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001261
Sean Callanan04cc3072009-12-19 02:59:52 +00001262 return 0;
1263}
1264
1265/*
1266 * readDisplacement - Consumes the displacement of an instruction.
1267 *
1268 * @param insn - The instruction whose displacement is to be read.
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001269 * @return - 0 if the displacement byte was successfully read; nonzero
Sean Callanan04cc3072009-12-19 02:59:52 +00001270 * otherwise.
1271 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001272static int readDisplacement(struct InternalInstruction* insn) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001273 int8_t d8;
1274 int16_t d16;
1275 int32_t d32;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001276
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001277 dbgprintf(insn, "readDisplacement()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001278
Sean Callanan04cc3072009-12-19 02:59:52 +00001279 if (insn->consumedDisplacement)
1280 return 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001281
Richard Smith5d5061032014-04-20 22:15:37 +00001282 insn->consumedDisplacement = true;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +00001283 insn->displacementOffset = insn->readerCursor - insn->startLocation;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001284
Sean Callanan04cc3072009-12-19 02:59:52 +00001285 switch (insn->eaDisplacement) {
1286 case EA_DISP_NONE:
Richard Smith5d5061032014-04-20 22:15:37 +00001287 insn->consumedDisplacement = false;
Sean Callanan04cc3072009-12-19 02:59:52 +00001288 break;
1289 case EA_DISP_8:
1290 if (consumeInt8(insn, &d8))
1291 return -1;
1292 insn->displacement = d8;
1293 break;
1294 case EA_DISP_16:
1295 if (consumeInt16(insn, &d16))
1296 return -1;
1297 insn->displacement = d16;
1298 break;
1299 case EA_DISP_32:
1300 if (consumeInt32(insn, &d32))
1301 return -1;
1302 insn->displacement = d32;
1303 break;
1304 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001305
Richard Smith5d5061032014-04-20 22:15:37 +00001306 insn->consumedDisplacement = true;
Sean Callanan04cc3072009-12-19 02:59:52 +00001307 return 0;
1308}
1309
1310/*
1311 * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and
1312 * displacement) for an instruction and interprets it.
1313 *
1314 * @param insn - The instruction whose addressing information is to be read.
1315 * @return - 0 if the information was successfully read; nonzero otherwise.
1316 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001317static int readModRM(struct InternalInstruction* insn) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001318 uint8_t mod, rm, reg;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001319
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001320 dbgprintf(insn, "readModRM()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001321
Sean Callanan04cc3072009-12-19 02:59:52 +00001322 if (insn->consumedModRM)
1323 return 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001324
Rafael Espindola9f9a1062011-01-06 16:48:42 +00001325 if (consumeByte(insn, &insn->modRM))
1326 return -1;
Richard Smith5d5061032014-04-20 22:15:37 +00001327 insn->consumedModRM = true;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001328
Sean Callanan04cc3072009-12-19 02:59:52 +00001329 mod = modFromModRM(insn->modRM);
1330 rm = rmFromModRM(insn->modRM);
1331 reg = regFromModRM(insn->modRM);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001332
Sean Callanan04cc3072009-12-19 02:59:52 +00001333 /*
1334 * This goes by insn->registerSize to pick the correct register, which messes
1335 * up if we're using (say) XMM or 8-bit register operands. That gets fixed in
1336 * fixupReg().
1337 */
1338 switch (insn->registerSize) {
1339 case 2:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001340 insn->regBase = MODRM_REG_AX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001341 insn->eaRegBase = EA_REG_AX;
1342 break;
1343 case 4:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001344 insn->regBase = MODRM_REG_EAX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001345 insn->eaRegBase = EA_REG_EAX;
1346 break;
1347 case 8:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001348 insn->regBase = MODRM_REG_RAX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001349 insn->eaRegBase = EA_REG_RAX;
1350 break;
1351 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001352
Sean Callanan04cc3072009-12-19 02:59:52 +00001353 reg |= rFromREX(insn->rexPrefix) << 3;
1354 rm |= bFromREX(insn->rexPrefix) << 3;
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001355 if (insn->vectorExtensionType == TYPE_EVEX) {
1356 reg |= r2FromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1357 rm |= xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1358 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001359
Sean Callanan04cc3072009-12-19 02:59:52 +00001360 insn->reg = (Reg)(insn->regBase + reg);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001361
Sean Callanan04cc3072009-12-19 02:59:52 +00001362 switch (insn->addressSize) {
1363 case 2:
1364 insn->eaBaseBase = EA_BASE_BX_SI;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001365
Sean Callanan04cc3072009-12-19 02:59:52 +00001366 switch (mod) {
1367 case 0x0:
1368 if (rm == 0x6) {
1369 insn->eaBase = EA_BASE_NONE;
1370 insn->eaDisplacement = EA_DISP_16;
Sean Callanan010b3732010-04-02 21:23:51 +00001371 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001372 return -1;
1373 } else {
1374 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1375 insn->eaDisplacement = EA_DISP_NONE;
1376 }
1377 break;
1378 case 0x1:
1379 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1380 insn->eaDisplacement = EA_DISP_8;
Craig Topper399e39e2014-01-25 22:48:43 +00001381 insn->displacementSize = 1;
Sean Callanan010b3732010-04-02 21:23:51 +00001382 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001383 return -1;
1384 break;
1385 case 0x2:
1386 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1387 insn->eaDisplacement = EA_DISP_16;
Sean Callanan010b3732010-04-02 21:23:51 +00001388 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001389 return -1;
1390 break;
1391 case 0x3:
1392 insn->eaBase = (EABase)(insn->eaRegBase + rm);
Sean Callanan010b3732010-04-02 21:23:51 +00001393 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001394 return -1;
1395 break;
1396 }
1397 break;
1398 case 4:
1399 case 8:
1400 insn->eaBaseBase = (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001401
Sean Callanan04cc3072009-12-19 02:59:52 +00001402 switch (mod) {
1403 case 0x0:
1404 insn->eaDisplacement = EA_DISP_NONE; /* readSIB may override this */
Douglas Katzman6dc13972015-05-13 22:44:52 +00001405 // In determining whether RIP-relative mode is used (rm=5),
1406 // or whether a SIB byte is present (rm=4),
1407 // the extension bits (REX.b and EVEX.x) are ignored.
1408 switch (rm & 7) {
1409 case 0x4: // SIB byte is present
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001410 insn->eaBase = (insn->addressSize == 4 ?
Sean Callanan04cc3072009-12-19 02:59:52 +00001411 EA_BASE_sib : EA_BASE_sib64);
Craig Topper38afbfd2014-03-20 05:56:00 +00001412 if (readSIB(insn) || readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001413 return -1;
1414 break;
Douglas Katzman6dc13972015-05-13 22:44:52 +00001415 case 0x5: // RIP-relative
Sean Callanan04cc3072009-12-19 02:59:52 +00001416 insn->eaBase = EA_BASE_NONE;
1417 insn->eaDisplacement = EA_DISP_32;
Sean Callanan010b3732010-04-02 21:23:51 +00001418 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001419 return -1;
1420 break;
1421 default:
1422 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1423 break;
1424 }
1425 break;
1426 case 0x1:
Craig Topper399e39e2014-01-25 22:48:43 +00001427 insn->displacementSize = 1;
Alp Toker771f7652014-01-26 18:44:34 +00001428 /* FALLTHROUGH */
Sean Callanan04cc3072009-12-19 02:59:52 +00001429 case 0x2:
1430 insn->eaDisplacement = (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
Douglas Katzman6dc13972015-05-13 22:44:52 +00001431 switch (rm & 7) {
1432 case 0x4: // SIB byte is present
Sean Callanan04cc3072009-12-19 02:59:52 +00001433 insn->eaBase = EA_BASE_sib;
Craig Topper38afbfd2014-03-20 05:56:00 +00001434 if (readSIB(insn) || readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001435 return -1;
1436 break;
1437 default:
1438 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
Sean Callanan010b3732010-04-02 21:23:51 +00001439 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001440 return -1;
1441 break;
1442 }
1443 break;
1444 case 0x3:
1445 insn->eaDisplacement = EA_DISP_NONE;
1446 insn->eaBase = (EABase)(insn->eaRegBase + rm);
1447 break;
1448 }
1449 break;
1450 } /* switch (insn->addressSize) */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001451
Sean Callanan04cc3072009-12-19 02:59:52 +00001452 return 0;
1453}
1454
1455#define GENERIC_FIXUP_FUNC(name, base, prefix) \
1456 static uint8_t name(struct InternalInstruction *insn, \
1457 OperandType type, \
1458 uint8_t index, \
1459 uint8_t *valid) { \
1460 *valid = 1; \
1461 switch (type) { \
1462 default: \
Sean Callanan010b3732010-04-02 21:23:51 +00001463 debug("Unhandled register type"); \
1464 *valid = 0; \
1465 return 0; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001466 case TYPE_Rv: \
1467 return base + index; \
1468 case TYPE_R8: \
Sean Callanan010b3732010-04-02 21:23:51 +00001469 if (insn->rexPrefix && \
Sean Callanan04cc3072009-12-19 02:59:52 +00001470 index >= 4 && index <= 7) { \
1471 return prefix##_SPL + (index - 4); \
1472 } else { \
1473 return prefix##_AL + index; \
1474 } \
1475 case TYPE_R16: \
1476 return prefix##_AX + index; \
1477 case TYPE_R32: \
1478 return prefix##_EAX + index; \
1479 case TYPE_R64: \
1480 return prefix##_RAX + index; \
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001481 case TYPE_XMM512: \
1482 return prefix##_ZMM0 + index; \
Sean Callananc3fd5232011-03-15 01:23:15 +00001483 case TYPE_XMM256: \
1484 return prefix##_YMM0 + index; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001485 case TYPE_XMM128: \
1486 case TYPE_XMM64: \
1487 case TYPE_XMM32: \
1488 case TYPE_XMM: \
1489 return prefix##_XMM0 + index; \
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001490 case TYPE_VK1: \
1491 case TYPE_VK8: \
1492 case TYPE_VK16: \
Craig Topper9c26bcc2015-03-02 03:33:11 +00001493 if (index > 7) \
1494 *valid = 0; \
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001495 return prefix##_K0 + index; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001496 case TYPE_MM64: \
Craig Topperd5b39232014-12-26 18:19:44 +00001497 return prefix##_MM0 + (index & 0x7); \
Sean Callanan04cc3072009-12-19 02:59:52 +00001498 case TYPE_SEGMENTREG: \
Sean Callanan010b3732010-04-02 21:23:51 +00001499 if (index > 5) \
Sean Callanan04cc3072009-12-19 02:59:52 +00001500 *valid = 0; \
1501 return prefix##_ES + index; \
1502 case TYPE_DEBUGREG: \
Sean Callanan04cc3072009-12-19 02:59:52 +00001503 return prefix##_DR0 + index; \
Sean Callanane7e1cf92010-05-06 20:59:00 +00001504 case TYPE_CONTROLREG: \
Sean Callanane7e1cf92010-05-06 20:59:00 +00001505 return prefix##_CR0 + index; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001506 } \
1507 }
1508
1509/*
1510 * fixup*Value - Consults an operand type to determine the meaning of the
1511 * reg or R/M field. If the operand is an XMM operand, for example, an
1512 * operand would be XMM0 instead of AX, which readModRM() would otherwise
1513 * misinterpret it as.
1514 *
1515 * @param insn - The instruction containing the operand.
1516 * @param type - The operand type.
1517 * @param index - The existing value of the field as reported by readModRM().
1518 * @param valid - The address of a uint8_t. The target is set to 1 if the
1519 * field is valid for the register class; 0 if not.
Sean Callanan010b3732010-04-02 21:23:51 +00001520 * @return - The proper value.
Sean Callanan04cc3072009-12-19 02:59:52 +00001521 */
Sean Callanan2f9443f2009-12-22 02:07:42 +00001522GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase, MODRM_REG)
Sean Callanan04cc3072009-12-19 02:59:52 +00001523GENERIC_FIXUP_FUNC(fixupRMValue, insn->eaRegBase, EA_REG)
1524
1525/*
1526 * fixupReg - Consults an operand specifier to determine which of the
1527 * fixup*Value functions to use in correcting readModRM()'ss interpretation.
1528 *
1529 * @param insn - See fixup*Value().
1530 * @param op - The operand specifier.
1531 * @return - 0 if fixup was successful; -1 if the register returned was
1532 * invalid for its class.
1533 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001534static int fixupReg(struct InternalInstruction *insn,
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +00001535 const struct OperandSpecifier *op) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001536 uint8_t valid;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001537
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001538 dbgprintf(insn, "fixupReg()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001539
Sean Callanan04cc3072009-12-19 02:59:52 +00001540 switch ((OperandEncoding)op->encoding) {
1541 default:
Sean Callanan010b3732010-04-02 21:23:51 +00001542 debug("Expected a REG or R/M encoding in fixupReg");
1543 return -1;
Sean Callananc3fd5232011-03-15 01:23:15 +00001544 case ENCODING_VVVV:
1545 insn->vvvv = (Reg)fixupRegValue(insn,
1546 (OperandType)op->type,
1547 insn->vvvv,
1548 &valid);
1549 if (!valid)
1550 return -1;
1551 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001552 case ENCODING_REG:
1553 insn->reg = (Reg)fixupRegValue(insn,
1554 (OperandType)op->type,
1555 insn->reg - insn->regBase,
1556 &valid);
1557 if (!valid)
1558 return -1;
1559 break;
Adam Nemet5933c2f2014-07-17 17:04:56 +00001560 CASE_ENCODING_RM:
Sean Callanan04cc3072009-12-19 02:59:52 +00001561 if (insn->eaBase >= insn->eaRegBase) {
1562 insn->eaBase = (EABase)fixupRMValue(insn,
1563 (OperandType)op->type,
1564 insn->eaBase - insn->eaRegBase,
1565 &valid);
1566 if (!valid)
1567 return -1;
1568 }
1569 break;
1570 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001571
Sean Callanan04cc3072009-12-19 02:59:52 +00001572 return 0;
1573}
1574
1575/*
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001576 * readOpcodeRegister - Reads an operand from the opcode field of an
Sean Callanan04cc3072009-12-19 02:59:52 +00001577 * instruction and interprets it appropriately given the operand width.
1578 * Handles AddRegFrm instructions.
1579 *
Craig Topper91551182014-01-01 15:29:32 +00001580 * @param insn - the instruction whose opcode field is to be read.
Sean Callanan04cc3072009-12-19 02:59:52 +00001581 * @param size - The width (in bytes) of the register being specified.
1582 * 1 means AL and friends, 2 means AX, 4 means EAX, and 8 means
1583 * RAX.
Sean Callanan010b3732010-04-02 21:23:51 +00001584 * @return - 0 on success; nonzero otherwise.
Sean Callanan04cc3072009-12-19 02:59:52 +00001585 */
Sean Callanan010b3732010-04-02 21:23:51 +00001586static int readOpcodeRegister(struct InternalInstruction* insn, uint8_t size) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001587 dbgprintf(insn, "readOpcodeRegister()");
Sean Callanan04cc3072009-12-19 02:59:52 +00001588
Sean Callanan04cc3072009-12-19 02:59:52 +00001589 if (size == 0)
1590 size = insn->registerSize;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001591
Sean Callanan04cc3072009-12-19 02:59:52 +00001592 switch (size) {
1593 case 1:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001594 insn->opcodeRegister = (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001595 | (insn->opcode & 7)));
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001596 if (insn->rexPrefix &&
Sean Callanan010b3732010-04-02 21:23:51 +00001597 insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1598 insn->opcodeRegister < MODRM_REG_AL + 0x8) {
Sean Callanan2f9443f2009-12-22 02:07:42 +00001599 insn->opcodeRegister = (Reg)(MODRM_REG_SPL
1600 + (insn->opcodeRegister - MODRM_REG_AL - 4));
Sean Callanan04cc3072009-12-19 02:59:52 +00001601 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001602
Sean Callanan04cc3072009-12-19 02:59:52 +00001603 break;
1604 case 2:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001605 insn->opcodeRegister = (Reg)(MODRM_REG_AX
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001606 + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001607 | (insn->opcode & 7)));
Sean Callanan04cc3072009-12-19 02:59:52 +00001608 break;
1609 case 4:
Sean Callanan010b3732010-04-02 21:23:51 +00001610 insn->opcodeRegister = (Reg)(MODRM_REG_EAX
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001611 + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001612 | (insn->opcode & 7)));
Sean Callanan04cc3072009-12-19 02:59:52 +00001613 break;
1614 case 8:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001615 insn->opcodeRegister = (Reg)(MODRM_REG_RAX
1616 + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001617 | (insn->opcode & 7)));
Sean Callanan04cc3072009-12-19 02:59:52 +00001618 break;
1619 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001620
Sean Callanan010b3732010-04-02 21:23:51 +00001621 return 0;
Sean Callanan04cc3072009-12-19 02:59:52 +00001622}
1623
1624/*
1625 * readImmediate - Consumes an immediate operand from an instruction, given the
1626 * desired operand size.
1627 *
1628 * @param insn - The instruction whose operand is to be read.
1629 * @param size - The width (in bytes) of the operand.
1630 * @return - 0 if the immediate was successfully consumed; nonzero
1631 * otherwise.
1632 */
1633static int readImmediate(struct InternalInstruction* insn, uint8_t size) {
1634 uint8_t imm8;
1635 uint16_t imm16;
1636 uint32_t imm32;
1637 uint64_t imm64;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001638
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001639 dbgprintf(insn, "readImmediate()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001640
Sean Callanan010b3732010-04-02 21:23:51 +00001641 if (insn->numImmediatesConsumed == 2) {
1642 debug("Already consumed two immediates");
1643 return -1;
1644 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001645
Sean Callanan04cc3072009-12-19 02:59:52 +00001646 if (size == 0)
1647 size = insn->immediateSize;
1648 else
1649 insn->immediateSize = size;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +00001650 insn->immediateOffset = insn->readerCursor - insn->startLocation;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001651
Sean Callanan04cc3072009-12-19 02:59:52 +00001652 switch (size) {
1653 case 1:
1654 if (consumeByte(insn, &imm8))
1655 return -1;
1656 insn->immediates[insn->numImmediatesConsumed] = imm8;
1657 break;
1658 case 2:
1659 if (consumeUInt16(insn, &imm16))
1660 return -1;
1661 insn->immediates[insn->numImmediatesConsumed] = imm16;
1662 break;
1663 case 4:
1664 if (consumeUInt32(insn, &imm32))
1665 return -1;
1666 insn->immediates[insn->numImmediatesConsumed] = imm32;
1667 break;
1668 case 8:
1669 if (consumeUInt64(insn, &imm64))
1670 return -1;
1671 insn->immediates[insn->numImmediatesConsumed] = imm64;
1672 break;
1673 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001674
Sean Callanan04cc3072009-12-19 02:59:52 +00001675 insn->numImmediatesConsumed++;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001676
Sean Callanan04cc3072009-12-19 02:59:52 +00001677 return 0;
1678}
1679
1680/*
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001681 * readVVVV - Consumes vvvv from an instruction if it has a VEX prefix.
Sean Callananc3fd5232011-03-15 01:23:15 +00001682 *
1683 * @param insn - The instruction whose operand is to be read.
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001684 * @return - 0 if the vvvv was successfully consumed; nonzero
Sean Callananc3fd5232011-03-15 01:23:15 +00001685 * otherwise.
1686 */
1687static int readVVVV(struct InternalInstruction* insn) {
1688 dbgprintf(insn, "readVVVV()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001689
Richard Smith89ee75d2014-04-20 21:07:34 +00001690 int vvvv;
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001691 if (insn->vectorExtensionType == TYPE_EVEX)
Adam Nemet8ae70502014-06-24 01:42:32 +00001692 vvvv = (v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4 |
1693 vvvvFromEVEX3of4(insn->vectorExtensionPrefix[2]));
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001694 else if (insn->vectorExtensionType == TYPE_VEX_3B)
Richard Smith89ee75d2014-04-20 21:07:34 +00001695 vvvv = vvvvFromVEX3of3(insn->vectorExtensionPrefix[2]);
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001696 else if (insn->vectorExtensionType == TYPE_VEX_2B)
Richard Smith89ee75d2014-04-20 21:07:34 +00001697 vvvv = vvvvFromVEX2of2(insn->vectorExtensionPrefix[1]);
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001698 else if (insn->vectorExtensionType == TYPE_XOP)
Richard Smith89ee75d2014-04-20 21:07:34 +00001699 vvvv = vvvvFromXOP3of3(insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +00001700 else
1701 return -1;
1702
Craig Topper0d0be472011-10-03 08:14:29 +00001703 if (insn->mode != MODE_64BIT)
Richard Smith89ee75d2014-04-20 21:07:34 +00001704 vvvv &= 0x7;
Craig Topper0d0be472011-10-03 08:14:29 +00001705
Richard Smith89ee75d2014-04-20 21:07:34 +00001706 insn->vvvv = static_cast<Reg>(vvvv);
Sean Callananc3fd5232011-03-15 01:23:15 +00001707 return 0;
1708}
1709
1710/*
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001711 * readMaskRegister - Reads an mask register from the opcode field of an
1712 * instruction.
1713 *
1714 * @param insn - The instruction whose opcode field is to be read.
1715 * @return - 0 on success; nonzero otherwise.
1716 */
1717static int readMaskRegister(struct InternalInstruction* insn) {
1718 dbgprintf(insn, "readMaskRegister()");
1719
1720 if (insn->vectorExtensionType != TYPE_EVEX)
1721 return -1;
1722
Richard Smith89ee75d2014-04-20 21:07:34 +00001723 insn->writemask =
1724 static_cast<Reg>(aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]));
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001725 return 0;
1726}
1727
1728/*
Sean Callanan04cc3072009-12-19 02:59:52 +00001729 * readOperands - Consults the specifier for an instruction and consumes all
1730 * operands for that instruction, interpreting them as it goes.
1731 *
1732 * @param insn - The instruction whose operands are to be read and interpreted.
1733 * @return - 0 if all operands could be read; nonzero otherwise.
1734 */
1735static int readOperands(struct InternalInstruction* insn) {
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001736 int hasVVVV, needVVVV;
Craig Topper2ba766a2011-12-30 06:23:39 +00001737 int sawRegImm = 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001738
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001739 dbgprintf(insn, "readOperands()");
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001740
1741 /* If non-zero vvvv specified, need to make sure one of the operands
1742 uses it. */
1743 hasVVVV = !readVVVV(insn);
1744 needVVVV = hasVVVV && (insn->vvvv != 0);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001745
Patrik Hagglund31998382014-04-28 12:12:27 +00001746 for (const auto &Op : x86OperandSets[insn->spec->operands]) {
1747 switch (Op.encoding) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001748 case ENCODING_NONE:
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00001749 case ENCODING_SI:
David Woodhouseb33c2ef2014-01-22 15:08:21 +00001750 case ENCODING_DI:
Sean Callanan04cc3072009-12-19 02:59:52 +00001751 break;
1752 case ENCODING_REG:
Adam Nemet5933c2f2014-07-17 17:04:56 +00001753 CASE_ENCODING_RM:
Sean Callanan04cc3072009-12-19 02:59:52 +00001754 if (readModRM(insn))
1755 return -1;
Patrik Hagglund31998382014-04-28 12:12:27 +00001756 if (fixupReg(insn, &Op))
Sean Callanan04cc3072009-12-19 02:59:52 +00001757 return -1;
Adam Nemet5933c2f2014-07-17 17:04:56 +00001758 // Apply the AVX512 compressed displacement scaling factor.
1759 if (Op.encoding != ENCODING_REG && insn->eaDisplacement == EA_DISP_8)
1760 insn->displacement *= 1 << (Op.encoding - ENCODING_RM);
Sean Callanan04cc3072009-12-19 02:59:52 +00001761 break;
1762 case ENCODING_CB:
1763 case ENCODING_CW:
1764 case ENCODING_CD:
1765 case ENCODING_CP:
1766 case ENCODING_CO:
1767 case ENCODING_CT:
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001768 dbgprintf(insn, "We currently don't hande code-offset encodings");
Sean Callanan04cc3072009-12-19 02:59:52 +00001769 return -1;
1770 case ENCODING_IB:
Craig Topper2ba766a2011-12-30 06:23:39 +00001771 if (sawRegImm) {
Benjamin Kramer9c48f262012-01-04 22:06:45 +00001772 /* Saw a register immediate so don't read again and instead split the
1773 previous immediate. FIXME: This is a hack. */
Benjamin Kramer47aecca2012-01-01 17:55:36 +00001774 insn->immediates[insn->numImmediatesConsumed] =
1775 insn->immediates[insn->numImmediatesConsumed - 1] & 0xf;
1776 ++insn->numImmediatesConsumed;
Craig Topper2ba766a2011-12-30 06:23:39 +00001777 break;
1778 }
Sean Callanan04cc3072009-12-19 02:59:52 +00001779 if (readImmediate(insn, 1))
1780 return -1;
Patrik Hagglund31998382014-04-28 12:12:27 +00001781 if (Op.type == TYPE_XMM128 ||
1782 Op.type == TYPE_XMM256)
Craig Topper2ba766a2011-12-30 06:23:39 +00001783 sawRegImm = 1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001784 break;
1785 case ENCODING_IW:
1786 if (readImmediate(insn, 2))
1787 return -1;
1788 break;
1789 case ENCODING_ID:
1790 if (readImmediate(insn, 4))
1791 return -1;
1792 break;
1793 case ENCODING_IO:
1794 if (readImmediate(insn, 8))
1795 return -1;
1796 break;
1797 case ENCODING_Iv:
Sean Callanan010b3732010-04-02 21:23:51 +00001798 if (readImmediate(insn, insn->immediateSize))
1799 return -1;
Chris Lattnerd4758fc2010-04-16 21:15:15 +00001800 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001801 case ENCODING_Ia:
Sean Callanan010b3732010-04-02 21:23:51 +00001802 if (readImmediate(insn, insn->addressSize))
1803 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001804 break;
1805 case ENCODING_RB:
Sean Callanan010b3732010-04-02 21:23:51 +00001806 if (readOpcodeRegister(insn, 1))
1807 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001808 break;
1809 case ENCODING_RW:
Sean Callanan010b3732010-04-02 21:23:51 +00001810 if (readOpcodeRegister(insn, 2))
1811 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001812 break;
1813 case ENCODING_RD:
Sean Callanan010b3732010-04-02 21:23:51 +00001814 if (readOpcodeRegister(insn, 4))
1815 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001816 break;
1817 case ENCODING_RO:
Sean Callanan010b3732010-04-02 21:23:51 +00001818 if (readOpcodeRegister(insn, 8))
1819 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001820 break;
1821 case ENCODING_Rv:
Sean Callanan010b3732010-04-02 21:23:51 +00001822 if (readOpcodeRegister(insn, 0))
1823 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001824 break;
Craig Topper623b0d62014-01-01 14:22:37 +00001825 case ENCODING_FP:
Sean Callananc3fd5232011-03-15 01:23:15 +00001826 break;
1827 case ENCODING_VVVV:
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001828 needVVVV = 0; /* Mark that we have found a VVVV operand. */
1829 if (!hasVVVV)
Sean Callananc3fd5232011-03-15 01:23:15 +00001830 return -1;
Patrik Hagglund31998382014-04-28 12:12:27 +00001831 if (fixupReg(insn, &Op))
Sean Callananc3fd5232011-03-15 01:23:15 +00001832 return -1;
1833 break;
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001834 case ENCODING_WRITEMASK:
1835 if (readMaskRegister(insn))
1836 return -1;
1837 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001838 case ENCODING_DUP:
1839 break;
1840 default:
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001841 dbgprintf(insn, "Encountered an operand with an unknown encoding.");
Sean Callanan04cc3072009-12-19 02:59:52 +00001842 return -1;
1843 }
1844 }
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001845
1846 /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */
1847 if (needVVVV) return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001848
Sean Callanan04cc3072009-12-19 02:59:52 +00001849 return 0;
1850}
1851
1852/*
1853 * decodeInstruction - Reads and interprets a full instruction provided by the
1854 * user.
1855 *
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001856 * @param insn - A pointer to the instruction to be populated. Must be
Sean Callanan04cc3072009-12-19 02:59:52 +00001857 * pre-allocated.
1858 * @param reader - The function to be used to read the instruction's bytes.
1859 * @param readerArg - A generic argument to be passed to the reader to store
1860 * any internal state.
1861 * @param logger - If non-NULL, the function to be used to write log messages
1862 * and warnings.
1863 * @param loggerArg - A generic argument to be passed to the logger to store
1864 * any internal state.
1865 * @param startLoc - The address (in the reader's address space) of the first
1866 * byte in the instruction.
1867 * @param mode - The mode (real mode, IA-32e, or IA-32e in 64-bit mode) to
1868 * decode the instruction in.
1869 * @return - 0 if the instruction's memory could be read; nonzero if
1870 * not.
1871 */
Richard Smith89ee75d2014-04-20 21:07:34 +00001872int llvm::X86Disassembler::decodeInstruction(
1873 struct InternalInstruction *insn, byteReader_t reader,
1874 const void *readerArg, dlog_t logger, void *loggerArg, const void *miiArg,
1875 uint64_t startLoc, DisassemblerMode mode) {
Daniel Dunbarc745a622009-12-19 03:31:50 +00001876 memset(insn, 0, sizeof(struct InternalInstruction));
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001877
Sean Callanan04cc3072009-12-19 02:59:52 +00001878 insn->reader = reader;
1879 insn->readerArg = readerArg;
1880 insn->dlog = logger;
1881 insn->dlogArg = loggerArg;
1882 insn->startLocation = startLoc;
1883 insn->readerCursor = startLoc;
1884 insn->mode = mode;
1885 insn->numImmediatesConsumed = 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001886
Sean Callanan04cc3072009-12-19 02:59:52 +00001887 if (readPrefixes(insn) ||
1888 readOpcode(insn) ||
Benjamin Kramer478e8de2012-02-11 14:50:54 +00001889 getID(insn, miiArg) ||
Sean Callanan04cc3072009-12-19 02:59:52 +00001890 insn->instructionID == 0 ||
1891 readOperands(insn))
1892 return -1;
Craig Topperb8aec082012-08-01 07:39:18 +00001893
Patrik Hagglund31998382014-04-28 12:12:27 +00001894 insn->operands = x86OperandSets[insn->spec->operands];
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001895
Sean Callanan04cc3072009-12-19 02:59:52 +00001896 insn->length = insn->readerCursor - insn->startLocation;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001897
Benjamin Kramer4f672272010-03-18 12:18:36 +00001898 dbgprintf(insn, "Read from 0x%llx to 0x%llx: length %zu",
1899 startLoc, insn->readerCursor, insn->length);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001900
Sean Callanan04cc3072009-12-19 02:59:52 +00001901 if (insn->length > 15)
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001902 dbgprintf(insn, "Instruction exceeds 15-byte limit");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001903
Sean Callanan04cc3072009-12-19 02:59:52 +00001904 return 0;
1905}