blob: 80acff22f7bc060caac3e849b22c64764f5fac26 [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
Chandler Carruth6bda14b2017-06-06 11:49:48 +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 * contextForAttrs - Client for the instruction context table. Takes a set of
58 * attributes and returns the appropriate decode context.
59 *
60 * @param attrMask - Attributes, from the enumeration attributeBits.
61 * @return - The InstructionContext to use when looking up an
62 * an instruction with these attributes.
63 */
Elena Demikhovsky371e3632013-12-25 11:40:51 +000064static InstructionContext contextForAttrs(uint16_t attrMask) {
Richard Smith89ee75d2014-04-20 21:07:34 +000065 return static_cast<InstructionContext>(CONTEXTS_SYM[attrMask]);
Sean Callanan04cc3072009-12-19 02:59:52 +000066}
67
68/*
69 * modRMRequired - Reads the appropriate instruction table to determine whether
70 * the ModR/M byte is required to decode a particular instruction.
71 *
72 * @param type - The opcode type (i.e., how many bytes it has).
73 * @param insnContext - The context for the instruction, as returned by
74 * contextForAttrs.
75 * @param opcode - The last byte of the instruction's opcode, not counting
76 * ModR/M extensions and escapes.
Richard Smith5d5061032014-04-20 22:15:37 +000077 * @return - true if the ModR/M byte is required, false otherwise.
Sean Callanan04cc3072009-12-19 02:59:52 +000078 */
Sean Callanan588785c2009-12-22 22:51:40 +000079static int modRMRequired(OpcodeType type,
Craig Topper21c33652011-10-02 16:56:09 +000080 InstructionContext insnContext,
Elena Demikhovsky371e3632013-12-25 11:40:51 +000081 uint16_t opcode) {
Craig Toppere73658d2014-04-28 04:05:08 +000082 const struct ContextDecision* decision = nullptr;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +000083
Sean Callanan04cc3072009-12-19 02:59:52 +000084 switch (type) {
85 case ONEBYTE:
86 decision = &ONEBYTE_SYM;
87 break;
88 case TWOBYTE:
89 decision = &TWOBYTE_SYM;
90 break;
91 case THREEBYTE_38:
92 decision = &THREEBYTE38_SYM;
93 break;
94 case THREEBYTE_3A:
95 decision = &THREEBYTE3A_SYM;
96 break;
Craig Topper9e3e38a2013-10-03 05:17:48 +000097 case XOP8_MAP:
98 decision = &XOP8_MAP_SYM;
99 break;
100 case XOP9_MAP:
101 decision = &XOP9_MAP_SYM;
102 break;
103 case XOPA_MAP:
104 decision = &XOPA_MAP_SYM;
105 break;
Sean Callanan04cc3072009-12-19 02:59:52 +0000106 }
Ahmed Charles636a3d62012-02-19 11:37:01 +0000107
Sean Callanan04cc3072009-12-19 02:59:52 +0000108 return decision->opcodeDecisions[insnContext].modRMDecisions[opcode].
109 modrm_type != MODRM_ONEENTRY;
Sean Callanan04cc3072009-12-19 02:59:52 +0000110}
111
112/*
113 * decode - Reads the appropriate instruction table to obtain the unique ID of
114 * an instruction.
115 *
116 * @param type - See modRMRequired().
117 * @param insnContext - See modRMRequired().
118 * @param opcode - See modRMRequired().
119 * @param modRM - The ModR/M byte if required, or any value if not.
Sean Callanan010b3732010-04-02 21:23:51 +0000120 * @return - The UID of the instruction, or 0 on failure.
Sean Callanan04cc3072009-12-19 02:59:52 +0000121 */
Sean Callanan588785c2009-12-22 22:51:40 +0000122static InstrUID decode(OpcodeType type,
Sean Callanan010b3732010-04-02 21:23:51 +0000123 InstructionContext insnContext,
124 uint8_t opcode,
125 uint8_t modRM) {
Craig Toppere73658d2014-04-28 04:05:08 +0000126 const struct ModRMDecision* dec = nullptr;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000127
Sean Callanan04cc3072009-12-19 02:59:52 +0000128 switch (type) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000129 case ONEBYTE:
130 dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
131 break;
132 case TWOBYTE:
133 dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
134 break;
135 case THREEBYTE_38:
136 dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
137 break;
138 case THREEBYTE_3A:
139 dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
140 break;
Craig Topper9e3e38a2013-10-03 05:17:48 +0000141 case XOP8_MAP:
142 dec = &XOP8_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
143 break;
144 case XOP9_MAP:
145 dec = &XOP9_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
146 break;
147 case XOPA_MAP:
148 dec = &XOPA_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
149 break;
Sean Callanan04cc3072009-12-19 02:59:52 +0000150 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000151
Sean Callanan04cc3072009-12-19 02:59:52 +0000152 switch (dec->modrm_type) {
153 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000154 debug("Corrupt table! Unknown modrm_type");
155 return 0;
Sean Callanan04cc3072009-12-19 02:59:52 +0000156 case MODRM_ONEENTRY:
Craig Topper487e7442012-02-09 07:45:30 +0000157 return modRMTable[dec->instructionIDs];
Sean Callanan04cc3072009-12-19 02:59:52 +0000158 case MODRM_SPLITRM:
159 if (modFromModRM(modRM) == 0x3)
Craig Topper487e7442012-02-09 07:45:30 +0000160 return modRMTable[dec->instructionIDs+1];
161 return modRMTable[dec->instructionIDs];
Craig Toppera0cd9702012-02-09 08:58:07 +0000162 case MODRM_SPLITREG:
163 if (modFromModRM(modRM) == 0x3)
164 return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)+8];
165 return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)];
Craig Topper963305b2012-09-13 05:45:42 +0000166 case MODRM_SPLITMISC:
167 if (modFromModRM(modRM) == 0x3)
168 return modRMTable[dec->instructionIDs+(modRM & 0x3f)+8];
169 return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)];
Sean Callanan04cc3072009-12-19 02:59:52 +0000170 case MODRM_FULL:
Craig Topper487e7442012-02-09 07:45:30 +0000171 return modRMTable[dec->instructionIDs+modRM];
Sean Callanan04cc3072009-12-19 02:59:52 +0000172 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000173}
174
175/*
176 * specifierForUID - Given a UID, returns the name and operand specification for
177 * that instruction.
178 *
179 * @param uid - The unique ID for the instruction. This should be returned by
180 * decode(); specifierForUID will not check bounds.
181 * @return - A pointer to the specification for that instruction.
182 */
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +0000183static const struct InstructionSpecifier *specifierForUID(InstrUID uid) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000184 return &INSTRUCTIONS_SYM[uid];
185}
186
187/*
188 * consumeByte - Uses the reader function provided by the user to consume one
189 * byte from the instruction's memory and advance the cursor.
190 *
191 * @param insn - The instruction with the reader function to use. The cursor
192 * for this instruction is advanced.
193 * @param byte - A pointer to a pre-allocated memory buffer to be populated
194 * with the data read.
195 * @return - 0 if the read was successful; nonzero otherwise.
196 */
Sean Callanan588785c2009-12-22 22:51:40 +0000197static int consumeByte(struct InternalInstruction* insn, uint8_t* byte) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000198 int ret = insn->reader(insn->readerArg, byte, insn->readerCursor);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000199
Sean Callanan04cc3072009-12-19 02:59:52 +0000200 if (!ret)
201 ++(insn->readerCursor);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000202
Sean Callanan04cc3072009-12-19 02:59:52 +0000203 return ret;
204}
205
206/*
207 * lookAtByte - Like consumeByte, but does not advance the cursor.
208 *
209 * @param insn - See consumeByte().
210 * @param byte - See consumeByte().
211 * @return - See consumeByte().
212 */
Sean Callanan588785c2009-12-22 22:51:40 +0000213static int lookAtByte(struct InternalInstruction* insn, uint8_t* byte) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000214 return insn->reader(insn->readerArg, byte, insn->readerCursor);
215}
216
Sean Callanan588785c2009-12-22 22:51:40 +0000217static void unconsumeByte(struct InternalInstruction* insn) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000218 insn->readerCursor--;
219}
220
Sean Callanan588785c2009-12-22 22:51:40 +0000221#define CONSUME_FUNC(name, type) \
222 static int name(struct InternalInstruction* insn, type* ptr) { \
223 type combined = 0; \
224 unsigned offset; \
225 for (offset = 0; offset < sizeof(type); ++offset) { \
226 uint8_t byte; \
227 int ret = insn->reader(insn->readerArg, \
228 &byte, \
229 insn->readerCursor + offset); \
230 if (ret) \
231 return ret; \
Richard Smith228e6d42012-08-24 23:29:28 +0000232 combined = combined | ((uint64_t)byte << (offset * 8)); \
Sean Callanan588785c2009-12-22 22:51:40 +0000233 } \
234 *ptr = combined; \
235 insn->readerCursor += sizeof(type); \
236 return 0; \
Sean Callanan04cc3072009-12-19 02:59:52 +0000237 }
238
239/*
240 * consume* - Use the reader function provided by the user to consume data
241 * values of various sizes from the instruction's memory and advance the
242 * cursor appropriately. These readers perform endian conversion.
243 *
244 * @param insn - See consumeByte().
245 * @param ptr - A pointer to a pre-allocated memory of appropriate size to
246 * be populated with the data read.
247 * @return - See consumeByte().
248 */
249CONSUME_FUNC(consumeInt8, int8_t)
250CONSUME_FUNC(consumeInt16, int16_t)
251CONSUME_FUNC(consumeInt32, int32_t)
252CONSUME_FUNC(consumeUInt16, uint16_t)
253CONSUME_FUNC(consumeUInt32, uint32_t)
254CONSUME_FUNC(consumeUInt64, uint64_t)
255
256/*
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000257 * dbgprintf - Uses the logging function provided by the user to log a single
Sean Callanan04cc3072009-12-19 02:59:52 +0000258 * message, typically without a carriage-return.
259 *
260 * @param insn - The instruction containing the logging function.
261 * @param format - See printf().
262 * @param ... - See printf().
263 */
Sean Callanan588785c2009-12-22 22:51:40 +0000264static void dbgprintf(struct InternalInstruction* insn,
265 const char* format,
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000266 ...) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000267 char buffer[256];
268 va_list ap;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000269
Sean Callanan04cc3072009-12-19 02:59:52 +0000270 if (!insn->dlog)
271 return;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000272
Sean Callanan04cc3072009-12-19 02:59:52 +0000273 va_start(ap, format);
274 (void)vsnprintf(buffer, sizeof(buffer), format, ap);
275 va_end(ap);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000276
Sean Callanan04cc3072009-12-19 02:59:52 +0000277 insn->dlog(insn->dlogArg, buffer);
Sean Callanan04cc3072009-12-19 02:59:52 +0000278}
279
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000280static bool isREX(struct InternalInstruction *insn, uint8_t prefix) {
281 if (insn->mode == MODE_64BIT)
282 return prefix >= 0x40 && prefix <= 0x4f;
283 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000284}
285
286/*
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000287 * setPrefixPresent - Marks that a particular prefix is present as mandatory
Sean Callanan04cc3072009-12-19 02:59:52 +0000288 *
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000289 * @param insn - The instruction to be marked as having the prefix.
290 * @param prefix - The prefix that is present.
Sean Callanan04cc3072009-12-19 02:59:52 +0000291 */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000292static void setPrefixPresent(struct InternalInstruction *insn, uint8_t prefix) {
293 uint8_t nextByte;
294 switch (prefix) {
295 case 0xf2:
296 case 0xf3:
297 if (lookAtByte(insn, &nextByte))
298 break;
299 // TODO:
300 // 1. There could be several 0x66
301 // 2. if (nextByte == 0x66) and nextNextByte != 0x0f then
302 // it's not mandatory prefix
303 // 3. if (nextByte >= 0x40 && nextByte <= 0x4f) it's REX and we need
304 // 0x0f exactly after it to be mandatory prefix
305 if (isREX(insn, nextByte) || nextByte == 0x0f || nextByte == 0x66)
306 // The last of 0xf2 /0xf3 is mandatory prefix
307 insn->mandatoryPrefix = prefix;
308 insn->repeatPrefix = prefix;
309 break;
310 case 0x66:
311 if (lookAtByte(insn, &nextByte))
312 break;
313 // 0x66 can't overwrite existing mandatory prefix and should be ignored
314 if (!insn->mandatoryPrefix && (nextByte == 0x0f || isREX(insn, nextByte)))
315 insn->mandatoryPrefix = prefix;
316 break;
317 }
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;
Ted Kremenek3c4408c2011-01-23 17:05:06 +0000331 uint8_t byte = 0;
Richard Mitton79917a92013-08-30 21:32:42 +0000332 uint8_t nextByte;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000333
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000334 dbgprintf(insn, "readPrefixes()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000335
Sean Callanan04cc3072009-12-19 02:59:52 +0000336 while (isPrefix) {
Richard Mitton576ee002013-08-30 21:19:48 +0000337 /* If we fail reading prefixes, just stop here and let the opcode reader deal with it */
Sean Callanan04cc3072009-12-19 02:59:52 +0000338 if (consumeByte(insn, &byte))
Richard Mitton576ee002013-08-30 21:19:48 +0000339 break;
Kevin Enderby014e1cd2012-03-09 17:52:49 +0000340
Benjamin Krameradfc73d2012-03-10 15:10:06 +0000341 /*
Dave Zarzycki07fabee2013-03-25 18:59:38 +0000342 * If the byte is a LOCK/REP/REPNE prefix and not a part of the opcode, then
343 * break and let it be disassembled as a normal "instruction".
Benjamin Krameradfc73d2012-03-10 15:10:06 +0000344 */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000345 if (insn->readerCursor - 1 == insn->startLocation && byte == 0xf0) // LOCK
Richard Mitton576ee002013-08-30 21:19:48 +0000346 break;
347
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000348 if ((byte == 0xf2 || byte == 0xf3) && !lookAtByte(insn, &nextByte)) {
Kevin Enderby35fd7922013-06-20 22:32:18 +0000349 /*
350 * If the byte is 0xf2 or 0xf3, and any of the following conditions are
351 * met:
352 * - it is followed by a LOCK (0xf0) prefix
353 * - it is followed by an xchg instruction
354 * then it should be disassembled as a xacquire/xrelease not repne/rep.
355 */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000356 if (((nextByte == 0xf0) ||
357 ((nextByte & 0xfe) == 0x86 || (nextByte & 0xf8) == 0x90))) {
Richard Smith5d5061032014-04-20 22:15:37 +0000358 insn->xAcquireRelease = true;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000359 if (!(byte == 0xf3 && nextByte == 0x90)) // PAUSE instruction support
360 break;
361 }
Kevin Enderby35fd7922013-06-20 22:32:18 +0000362 /*
363 * Also if the byte is 0xf3, and the following condition is met:
364 * - it is followed by a "mov mem, reg" (opcode 0x88/0x89) or
365 * "mov mem, imm" (opcode 0xc6/0xc7) instructions.
366 * then it should be disassembled as an xrelease not rep.
367 */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000368 if (byte == 0xf3 && (nextByte == 0x88 || nextByte == 0x89 ||
369 nextByte == 0xc6 || nextByte == 0xc7)) {
Richard Smith5d5061032014-04-20 22:15:37 +0000370 insn->xAcquireRelease = true;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000371 if (nextByte != 0x90) // PAUSE instruction support
372 break;
373 }
374 if (isREX(insn, nextByte)) {
375 uint8_t nnextByte;
376 // Go to REX prefix after the current one
377 if (consumeByte(insn, &nnextByte))
Dave Zarzycki07fabee2013-03-25 18:59:38 +0000378 return -1;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000379 // We should be able to read next byte after REX prefix
380 if (lookAtByte(insn, &nnextByte))
Dave Zarzycki07fabee2013-03-25 18:59:38 +0000381 return -1;
382 unconsumeByte(insn);
383 }
Dave Zarzycki07fabee2013-03-25 18:59:38 +0000384 }
385
Sean Callanan04cc3072009-12-19 02:59:52 +0000386 switch (byte) {
387 case 0xf0: /* LOCK */
388 case 0xf2: /* REPNE/REPNZ */
389 case 0xf3: /* REP or REPE/REPZ */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000390 setPrefixPresent(insn, byte);
Sean Callanan04cc3072009-12-19 02:59:52 +0000391 break;
392 case 0x2e: /* CS segment override -OR- Branch not taken */
393 case 0x36: /* SS segment override -OR- Branch taken */
394 case 0x3e: /* DS segment override */
395 case 0x26: /* ES segment override */
396 case 0x64: /* FS segment override */
397 case 0x65: /* GS segment override */
398 switch (byte) {
399 case 0x2e:
400 insn->segmentOverride = SEG_OVERRIDE_CS;
401 break;
402 case 0x36:
403 insn->segmentOverride = SEG_OVERRIDE_SS;
404 break;
405 case 0x3e:
406 insn->segmentOverride = SEG_OVERRIDE_DS;
407 break;
408 case 0x26:
409 insn->segmentOverride = SEG_OVERRIDE_ES;
410 break;
411 case 0x64:
412 insn->segmentOverride = SEG_OVERRIDE_FS;
413 break;
414 case 0x65:
415 insn->segmentOverride = SEG_OVERRIDE_GS;
416 break;
417 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000418 debug("Unhandled override");
419 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +0000420 }
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000421 setPrefixPresent(insn, byte);
Sean Callanan04cc3072009-12-19 02:59:52 +0000422 break;
423 case 0x66: /* Operand-size override */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000424 insn->hasOpSize = true;
425 setPrefixPresent(insn, byte);
Sean Callanan04cc3072009-12-19 02:59:52 +0000426 break;
427 case 0x67: /* Address-size override */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000428 insn->hasAdSize = true;
429 setPrefixPresent(insn, byte);
Sean Callanan04cc3072009-12-19 02:59:52 +0000430 break;
431 default: /* Not a prefix byte */
Richard Smith5d5061032014-04-20 22:15:37 +0000432 isPrefix = false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000433 break;
434 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000435
Sean Callanan04cc3072009-12-19 02:59:52 +0000436 if (isPrefix)
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000437 dbgprintf(insn, "Found prefix 0x%hhx", byte);
Sean Callanan04cc3072009-12-19 02:59:52 +0000438 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000439
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000440 insn->vectorExtensionType = TYPE_NO_VEX_XOP;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000441
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000442 if (byte == 0x62) {
443 uint8_t byte1, byte2;
444
445 if (consumeByte(insn, &byte1)) {
446 dbgprintf(insn, "Couldn't read second byte of EVEX prefix");
447 return -1;
448 }
449
450 if (lookAtByte(insn, &byte2)) {
451 dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
452 return -1;
453 }
454
455 if ((insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) &&
456 ((~byte1 & 0xc) == 0xc) && ((byte2 & 0x4) == 0x4)) {
457 insn->vectorExtensionType = TYPE_EVEX;
Craig Topper273515e2014-10-07 07:29:48 +0000458 } else {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000459 unconsumeByte(insn); /* unconsume byte1 */
460 unconsumeByte(insn); /* unconsume byte */
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000461 }
462
463 if (insn->vectorExtensionType == TYPE_EVEX) {
464 insn->vectorExtensionPrefix[0] = byte;
465 insn->vectorExtensionPrefix[1] = byte1;
466 if (consumeByte(insn, &insn->vectorExtensionPrefix[2])) {
467 dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
468 return -1;
469 }
470 if (consumeByte(insn, &insn->vectorExtensionPrefix[3])) {
471 dbgprintf(insn, "Couldn't read fourth byte of EVEX prefix");
472 return -1;
473 }
474
475 /* We simulate the REX prefix for simplicity's sake */
476 if (insn->mode == MODE_64BIT) {
477 insn->rexPrefix = 0x40
478 | (wFromEVEX3of4(insn->vectorExtensionPrefix[2]) << 3)
479 | (rFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 2)
480 | (xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 1)
481 | (bFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 0);
482 }
483
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000484 dbgprintf(insn, "Found EVEX prefix 0x%hhx 0x%hhx 0x%hhx 0x%hhx",
485 insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
486 insn->vectorExtensionPrefix[2], insn->vectorExtensionPrefix[3]);
487 }
Craig Topper273515e2014-10-07 07:29:48 +0000488 } else if (byte == 0xc4) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000489 uint8_t byte1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000490
Sean Callananc3fd5232011-03-15 01:23:15 +0000491 if (lookAtByte(insn, &byte1)) {
492 dbgprintf(insn, "Couldn't read second byte of VEX");
493 return -1;
494 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000495
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000496 if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000497 insn->vectorExtensionType = TYPE_VEX_3B;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000498 else
Sean Callanan04cc3072009-12-19 02:59:52 +0000499 unconsumeByte(insn);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000500
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000501 if (insn->vectorExtensionType == TYPE_VEX_3B) {
502 insn->vectorExtensionPrefix[0] = byte;
503 consumeByte(insn, &insn->vectorExtensionPrefix[1]);
504 consumeByte(insn, &insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +0000505
506 /* We simulate the REX prefix for simplicity's sake */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000507
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000508 if (insn->mode == MODE_64BIT)
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000509 insn->rexPrefix = 0x40
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000510 | (wFromVEX3of3(insn->vectorExtensionPrefix[2]) << 3)
511 | (rFromVEX2of3(insn->vectorExtensionPrefix[1]) << 2)
512 | (xFromVEX2of3(insn->vectorExtensionPrefix[1]) << 1)
513 | (bFromVEX2of3(insn->vectorExtensionPrefix[1]) << 0);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000514
Craig Topper9e3e38a2013-10-03 05:17:48 +0000515 dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx 0x%hhx",
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000516 insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
517 insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +0000518 }
Craig Topper273515e2014-10-07 07:29:48 +0000519 } else if (byte == 0xc5) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000520 uint8_t byte1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000521
Sean Callananc3fd5232011-03-15 01:23:15 +0000522 if (lookAtByte(insn, &byte1)) {
523 dbgprintf(insn, "Couldn't read second byte of VEX");
524 return -1;
525 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000526
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000527 if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000528 insn->vectorExtensionType = TYPE_VEX_2B;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000529 else
Sean Callananc3fd5232011-03-15 01:23:15 +0000530 unconsumeByte(insn);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000531
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000532 if (insn->vectorExtensionType == TYPE_VEX_2B) {
533 insn->vectorExtensionPrefix[0] = byte;
534 consumeByte(insn, &insn->vectorExtensionPrefix[1]);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000535
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000536 if (insn->mode == MODE_64BIT)
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000537 insn->rexPrefix = 0x40
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000538 | (rFromVEX2of2(insn->vectorExtensionPrefix[1]) << 2);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000539
Craig Topper273515e2014-10-07 07:29:48 +0000540 switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000541 default:
542 break;
543 case VEX_PREFIX_66:
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000544 insn->hasOpSize = true;
Sean Callananc3fd5232011-03-15 01:23:15 +0000545 break;
546 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000547
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000548 dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx",
549 insn->vectorExtensionPrefix[0],
550 insn->vectorExtensionPrefix[1]);
Craig Topper9e3e38a2013-10-03 05:17:48 +0000551 }
Craig Topper273515e2014-10-07 07:29:48 +0000552 } else if (byte == 0x8f) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000553 uint8_t byte1;
554
555 if (lookAtByte(insn, &byte1)) {
556 dbgprintf(insn, "Couldn't read second byte of XOP");
557 return -1;
558 }
559
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000560 if ((byte1 & 0x38) != 0x0) /* 0 in these 3 bits is a POP instruction. */
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000561 insn->vectorExtensionType = TYPE_XOP;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000562 else
Craig Topper9e3e38a2013-10-03 05:17:48 +0000563 unconsumeByte(insn);
Craig Topper9e3e38a2013-10-03 05:17:48 +0000564
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000565 if (insn->vectorExtensionType == TYPE_XOP) {
566 insn->vectorExtensionPrefix[0] = byte;
567 consumeByte(insn, &insn->vectorExtensionPrefix[1]);
568 consumeByte(insn, &insn->vectorExtensionPrefix[2]);
Craig Topper9e3e38a2013-10-03 05:17:48 +0000569
570 /* We simulate the REX prefix for simplicity's sake */
571
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000572 if (insn->mode == MODE_64BIT)
Craig Topper9e3e38a2013-10-03 05:17:48 +0000573 insn->rexPrefix = 0x40
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000574 | (wFromXOP3of3(insn->vectorExtensionPrefix[2]) << 3)
575 | (rFromXOP2of3(insn->vectorExtensionPrefix[1]) << 2)
576 | (xFromXOP2of3(insn->vectorExtensionPrefix[1]) << 1)
577 | (bFromXOP2of3(insn->vectorExtensionPrefix[1]) << 0);
Craig Topper9e3e38a2013-10-03 05:17:48 +0000578
Craig Topper273515e2014-10-07 07:29:48 +0000579 switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000580 default:
581 break;
582 case VEX_PREFIX_66:
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000583 insn->hasOpSize = true;
Craig Topper9e3e38a2013-10-03 05:17:48 +0000584 break;
585 }
586
587 dbgprintf(insn, "Found XOP prefix 0x%hhx 0x%hhx 0x%hhx",
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000588 insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
589 insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +0000590 }
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000591 } else if (isREX(insn, byte)) {
592 if (lookAtByte(insn, &nextByte))
593 return -1;
594 insn->rexPrefix = byte;
595 dbgprintf(insn, "Found REX prefix 0x%hhx", byte);
596 } else
597 unconsumeByte(insn);
Sean Callananc3fd5232011-03-15 01:23:15 +0000598
Sean Callanan04cc3072009-12-19 02:59:52 +0000599 if (insn->mode == MODE_16BIT) {
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000600 insn->registerSize = (insn->hasOpSize ? 4 : 2);
601 insn->addressSize = (insn->hasAdSize ? 4 : 2);
602 insn->displacementSize = (insn->hasAdSize ? 4 : 2);
603 insn->immediateSize = (insn->hasOpSize ? 4 : 2);
Sean Callanan04cc3072009-12-19 02:59:52 +0000604 } else if (insn->mode == MODE_32BIT) {
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000605 insn->registerSize = (insn->hasOpSize ? 2 : 4);
606 insn->addressSize = (insn->hasAdSize ? 2 : 4);
607 insn->displacementSize = (insn->hasAdSize ? 2 : 4);
608 insn->immediateSize = (insn->hasOpSize ? 2 : 4);
Sean Callanan04cc3072009-12-19 02:59:52 +0000609 } else if (insn->mode == MODE_64BIT) {
610 if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
611 insn->registerSize = 8;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000612 insn->addressSize = (insn->hasAdSize ? 4 : 8);
Sean Callanan04cc3072009-12-19 02:59:52 +0000613 insn->displacementSize = 4;
614 insn->immediateSize = 4;
Sean Callanan04cc3072009-12-19 02:59:52 +0000615 } else {
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000616 insn->registerSize = (insn->hasOpSize ? 2 : 4);
617 insn->addressSize = (insn->hasAdSize ? 4 : 8);
618 insn->displacementSize = (insn->hasOpSize ? 2 : 4);
619 insn->immediateSize = (insn->hasOpSize ? 2 : 4);
Sean Callanan04cc3072009-12-19 02:59:52 +0000620 }
621 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000622
Sean Callanan04cc3072009-12-19 02:59:52 +0000623 return 0;
624}
625
626/*
627 * readOpcode - Reads the opcode (excepting the ModR/M byte in the case of
628 * extended or escape opcodes).
629 *
630 * @param insn - The instruction whose opcode is to be read.
631 * @return - 0 if the opcode could be read successfully; nonzero otherwise.
632 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000633static int readOpcode(struct InternalInstruction* insn) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000634 /* Determine the length of the primary opcode */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000635
Sean Callanan04cc3072009-12-19 02:59:52 +0000636 uint8_t current;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000637
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000638 dbgprintf(insn, "readOpcode()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000639
Sean Callanan04cc3072009-12-19 02:59:52 +0000640 insn->opcodeType = ONEBYTE;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000641
Craig Topper273515e2014-10-07 07:29:48 +0000642 if (insn->vectorExtensionType == TYPE_EVEX) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000643 switch (mmFromEVEX2of4(insn->vectorExtensionPrefix[1])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000644 default:
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000645 dbgprintf(insn, "Unhandled mm field for instruction (0x%hhx)",
646 mmFromEVEX2of4(insn->vectorExtensionPrefix[1]));
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000647 return -1;
Sean Callananc3fd5232011-03-15 01:23:15 +0000648 case VEX_LOB_0F:
Sean Callananc3fd5232011-03-15 01:23:15 +0000649 insn->opcodeType = TWOBYTE;
650 return consumeByte(insn, &insn->opcode);
651 case VEX_LOB_0F38:
Sean Callananc3fd5232011-03-15 01:23:15 +0000652 insn->opcodeType = THREEBYTE_38;
653 return consumeByte(insn, &insn->opcode);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000654 case VEX_LOB_0F3A:
Sean Callananc3fd5232011-03-15 01:23:15 +0000655 insn->opcodeType = THREEBYTE_3A;
656 return consumeByte(insn, &insn->opcode);
657 }
Craig Topper273515e2014-10-07 07:29:48 +0000658 } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000659 switch (mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])) {
660 default:
661 dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
662 mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
663 return -1;
664 case VEX_LOB_0F:
665 insn->opcodeType = TWOBYTE;
666 return consumeByte(insn, &insn->opcode);
667 case VEX_LOB_0F38:
668 insn->opcodeType = THREEBYTE_38;
669 return consumeByte(insn, &insn->opcode);
670 case VEX_LOB_0F3A:
671 insn->opcodeType = THREEBYTE_3A;
672 return consumeByte(insn, &insn->opcode);
673 }
Craig Topper273515e2014-10-07 07:29:48 +0000674 } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000675 insn->opcodeType = TWOBYTE;
676 return consumeByte(insn, &insn->opcode);
Craig Topper273515e2014-10-07 07:29:48 +0000677 } else if (insn->vectorExtensionType == TYPE_XOP) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000678 switch (mmmmmFromXOP2of3(insn->vectorExtensionPrefix[1])) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000679 default:
680 dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000681 mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
Craig Topper9e3e38a2013-10-03 05:17:48 +0000682 return -1;
683 case XOP_MAP_SELECT_8:
684 insn->opcodeType = XOP8_MAP;
685 return consumeByte(insn, &insn->opcode);
686 case XOP_MAP_SELECT_9:
687 insn->opcodeType = XOP9_MAP;
688 return consumeByte(insn, &insn->opcode);
689 case XOP_MAP_SELECT_A:
690 insn->opcodeType = XOPA_MAP;
691 return consumeByte(insn, &insn->opcode);
692 }
693 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000694
Sean Callanan04cc3072009-12-19 02:59:52 +0000695 if (consumeByte(insn, &current))
696 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000697
Sean Callanan04cc3072009-12-19 02:59:52 +0000698 if (current == 0x0f) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000699 dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000700
Sean Callanan04cc3072009-12-19 02:59:52 +0000701 if (consumeByte(insn, &current))
702 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000703
Sean Callanan04cc3072009-12-19 02:59:52 +0000704 if (current == 0x38) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000705 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000706
Sean Callanan04cc3072009-12-19 02:59:52 +0000707 if (consumeByte(insn, &current))
708 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000709
Sean Callanan04cc3072009-12-19 02:59:52 +0000710 insn->opcodeType = THREEBYTE_38;
711 } else if (current == 0x3a) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000712 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000713
Sean Callanan04cc3072009-12-19 02:59:52 +0000714 if (consumeByte(insn, &current))
715 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000716
Sean Callanan04cc3072009-12-19 02:59:52 +0000717 insn->opcodeType = THREEBYTE_3A;
718 } else {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000719 dbgprintf(insn, "Didn't find a three-byte escape prefix");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000720
Sean Callanan04cc3072009-12-19 02:59:52 +0000721 insn->opcodeType = TWOBYTE;
722 }
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000723 } else if (insn->mandatoryPrefix)
724 // The opcode with mandatory prefix must start with opcode escape.
725 // If not it's legacy repeat prefix
726 insn->mandatoryPrefix = 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000727
Sean Callanan04cc3072009-12-19 02:59:52 +0000728 /*
729 * At this point we have consumed the full opcode.
730 * Anything we consume from here on must be unconsumed.
731 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000732
Sean Callanan04cc3072009-12-19 02:59:52 +0000733 insn->opcode = current;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000734
Sean Callanan04cc3072009-12-19 02:59:52 +0000735 return 0;
736}
737
738static int readModRM(struct InternalInstruction* insn);
739
740/*
741 * getIDWithAttrMask - Determines the ID of an instruction, consuming
742 * the ModR/M byte as appropriate for extended and escape opcodes,
743 * and using a supplied attribute mask.
744 *
745 * @param instructionID - A pointer whose target is filled in with the ID of the
746 * instruction.
747 * @param insn - The instruction whose ID is to be determined.
748 * @param attrMask - The attribute mask to search.
749 * @return - 0 if the ModR/M could be read when needed or was not
750 * needed; nonzero otherwise.
751 */
752static int getIDWithAttrMask(uint16_t* instructionID,
753 struct InternalInstruction* insn,
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000754 uint16_t attrMask) {
Richard Smith5d5061032014-04-20 22:15:37 +0000755 bool hasModRMExtension;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000756
Richard Smith89ee75d2014-04-20 21:07:34 +0000757 InstructionContext instructionClass = contextForAttrs(attrMask);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000758
Sean Callanan04cc3072009-12-19 02:59:52 +0000759 hasModRMExtension = modRMRequired(insn->opcodeType,
760 instructionClass,
761 insn->opcode);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000762
Sean Callanan04cc3072009-12-19 02:59:52 +0000763 if (hasModRMExtension) {
Rafael Espindola9f9a1062011-01-06 16:48:42 +0000764 if (readModRM(insn))
765 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000766
Sean Callanan04cc3072009-12-19 02:59:52 +0000767 *instructionID = decode(insn->opcodeType,
768 instructionClass,
769 insn->opcode,
770 insn->modRM);
771 } else {
772 *instructionID = decode(insn->opcodeType,
773 instructionClass,
774 insn->opcode,
775 0);
776 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000777
Sean Callanan04cc3072009-12-19 02:59:52 +0000778 return 0;
779}
780
781/*
782 * is16BitEquivalent - Determines whether two instruction names refer to
783 * equivalent instructions but one is 16-bit whereas the other is not.
784 *
785 * @param orig - The instruction that is not 16-bit
786 * @param equiv - The instruction that is 16-bit
787 */
Mehdi Amini36d33fc2016-10-01 06:46:33 +0000788static bool is16BitEquivalent(const char *orig, const char *equiv) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000789 off_t i;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000790
Sean Callanan010b3732010-04-02 21:23:51 +0000791 for (i = 0;; i++) {
792 if (orig[i] == '\0' && equiv[i] == '\0')
Richard Smith5d5061032014-04-20 22:15:37 +0000793 return true;
Sean Callanan010b3732010-04-02 21:23:51 +0000794 if (orig[i] == '\0' || equiv[i] == '\0')
Richard Smith5d5061032014-04-20 22:15:37 +0000795 return false;
Sean Callanan010b3732010-04-02 21:23:51 +0000796 if (orig[i] != equiv[i]) {
797 if ((orig[i] == 'Q' || orig[i] == 'L') && equiv[i] == 'W')
Sean Callanan04cc3072009-12-19 02:59:52 +0000798 continue;
Sean Callanan010b3732010-04-02 21:23:51 +0000799 if ((orig[i] == '6' || orig[i] == '3') && equiv[i] == '1')
Sean Callanan04cc3072009-12-19 02:59:52 +0000800 continue;
Sean Callanan010b3732010-04-02 21:23:51 +0000801 if ((orig[i] == '4' || orig[i] == '2') && equiv[i] == '6')
Sean Callanan04cc3072009-12-19 02:59:52 +0000802 continue;
Richard Smith5d5061032014-04-20 22:15:37 +0000803 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000804 }
805 }
806}
807
808/*
Craig Topper0676b902014-10-07 07:29:50 +0000809 * is64Bit - Determines whether this instruction is a 64-bit instruction.
810 *
811 * @param name - The instruction that is not 16-bit
812 */
Mehdi Amini36d33fc2016-10-01 06:46:33 +0000813static bool is64Bit(const char *name) {
Craig Topper0676b902014-10-07 07:29:50 +0000814 off_t i;
815
816 for (i = 0;; ++i) {
817 if (name[i] == '\0')
818 return false;
819 if (name[i] == '6' && name[i+1] == '4')
820 return true;
821 }
822}
823
824/*
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000825 * getID - Determines the ID of an instruction, consuming the ModR/M byte as
826 * appropriate for extended and escape opcodes. Determines the attributes and
Sean Callanan04cc3072009-12-19 02:59:52 +0000827 * context for the instruction before doing so.
828 *
829 * @param insn - The instruction whose ID is to be determined.
830 * @return - 0 if the ModR/M could be read when needed or was not needed;
831 * nonzero otherwise.
832 */
Roman Divacky67923802012-09-05 21:17:34 +0000833static int getID(struct InternalInstruction* insn, const void *miiArg) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000834 uint16_t attrMask;
Sean Callanan04cc3072009-12-19 02:59:52 +0000835 uint16_t instructionID;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000836
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000837 dbgprintf(insn, "getID()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000838
Sean Callanan04cc3072009-12-19 02:59:52 +0000839 attrMask = ATTR_NONE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000840
Sean Callanan04cc3072009-12-19 02:59:52 +0000841 if (insn->mode == MODE_64BIT)
842 attrMask |= ATTR_64BIT;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000843
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000844 if (insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
845 attrMask |= (insn->vectorExtensionType == TYPE_EVEX) ? ATTR_EVEX : ATTR_VEX;
Sean Callananc3fd5232011-03-15 01:23:15 +0000846
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000847 if (insn->vectorExtensionType == TYPE_EVEX) {
848 switch (ppFromEVEX3of4(insn->vectorExtensionPrefix[2])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000849 case VEX_PREFIX_66:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000850 attrMask |= ATTR_OPSIZE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000851 break;
852 case VEX_PREFIX_F3:
853 attrMask |= ATTR_XS;
854 break;
855 case VEX_PREFIX_F2:
856 attrMask |= ATTR_XD;
857 break;
858 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000859
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000860 if (zFromEVEX4of4(insn->vectorExtensionPrefix[3]))
861 attrMask |= ATTR_EVEXKZ;
862 if (bFromEVEX4of4(insn->vectorExtensionPrefix[3]))
863 attrMask |= ATTR_EVEXB;
864 if (aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]))
865 attrMask |= ATTR_EVEXK;
866 if (lFromEVEX4of4(insn->vectorExtensionPrefix[3]))
867 attrMask |= ATTR_EVEXL;
868 if (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
869 attrMask |= ATTR_EVEXL2;
Craig Topper273515e2014-10-07 07:29:48 +0000870 } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000871 switch (ppFromVEX3of3(insn->vectorExtensionPrefix[2])) {
872 case VEX_PREFIX_66:
873 attrMask |= ATTR_OPSIZE;
874 break;
875 case VEX_PREFIX_F3:
876 attrMask |= ATTR_XS;
877 break;
878 case VEX_PREFIX_F2:
879 attrMask |= ATTR_XD;
880 break;
881 }
882
883 if (lFromVEX3of3(insn->vectorExtensionPrefix[2]))
Sean Callananc3fd5232011-03-15 01:23:15 +0000884 attrMask |= ATTR_VEXL;
Craig Topper273515e2014-10-07 07:29:48 +0000885 } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000886 switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000887 case VEX_PREFIX_66:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000888 attrMask |= ATTR_OPSIZE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000889 break;
890 case VEX_PREFIX_F3:
891 attrMask |= ATTR_XS;
892 break;
893 case VEX_PREFIX_F2:
894 attrMask |= ATTR_XD;
895 break;
896 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000897
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000898 if (lFromVEX2of2(insn->vectorExtensionPrefix[1]))
Craig Topper9e3e38a2013-10-03 05:17:48 +0000899 attrMask |= ATTR_VEXL;
Craig Topper273515e2014-10-07 07:29:48 +0000900 } else if (insn->vectorExtensionType == TYPE_XOP) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000901 switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000902 case VEX_PREFIX_66:
903 attrMask |= ATTR_OPSIZE;
904 break;
905 case VEX_PREFIX_F3:
906 attrMask |= ATTR_XS;
907 break;
908 case VEX_PREFIX_F2:
909 attrMask |= ATTR_XD;
910 break;
911 }
912
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000913 if (lFromXOP3of3(insn->vectorExtensionPrefix[2]))
Sean Callananc3fd5232011-03-15 01:23:15 +0000914 attrMask |= ATTR_VEXL;
Craig Topper273515e2014-10-07 07:29:48 +0000915 } else {
Sean Callananc3fd5232011-03-15 01:23:15 +0000916 return -1;
917 }
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000918 } else if (!insn->mandatoryPrefix) {
919 // If we don't have mandatory prefix we should use legacy prefixes here
920 if (insn->hasOpSize && (insn->mode != MODE_16BIT))
Sean Callananc3fd5232011-03-15 01:23:15 +0000921 attrMask |= ATTR_OPSIZE;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000922 if (insn->hasAdSize)
Craig Topper6491c802012-02-27 01:54:29 +0000923 attrMask |= ATTR_ADSIZE;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000924 if (insn->opcodeType == ONEBYTE) {
925 if (insn->repeatPrefix == 0xf3 && (insn->opcode == 0x90))
926 // Special support for PAUSE
927 attrMask |= ATTR_XS;
928 } else {
929 if (insn->repeatPrefix == 0xf2)
930 attrMask |= ATTR_XD;
931 else if (insn->repeatPrefix == 0xf3)
932 attrMask |= ATTR_XS;
933 }
934 } else {
935 switch (insn->mandatoryPrefix) {
936 case 0xf2:
Sean Callananc3fd5232011-03-15 01:23:15 +0000937 attrMask |= ATTR_XD;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000938 break;
939 case 0xf3:
940 attrMask |= ATTR_XS;
941 break;
942 case 0x66:
943 if (insn->mode != MODE_16BIT)
944 attrMask |= ATTR_OPSIZE;
945 break;
946 case 0x67:
947 attrMask |= ATTR_ADSIZE;
948 break;
949 }
Sean Callananc3fd5232011-03-15 01:23:15 +0000950 }
951
Craig Topperf18c8962011-10-04 06:30:42 +0000952 if (insn->rexPrefix & 0x08)
953 attrMask |= ATTR_REXW;
Craig Topperf01f1b52011-11-06 23:04:08 +0000954
David Woodhouse9c74fdb2014-01-20 12:02:48 +0000955 /*
956 * JCXZ/JECXZ need special handling for 16-bit mode because the meaning
957 * of the AdSize prefix is inverted w.r.t. 32-bit mode.
958 */
Craig Topper6e518772014-12-31 07:07:11 +0000959 if (insn->mode == MODE_16BIT && insn->opcodeType == ONEBYTE &&
960 insn->opcode == 0xE3)
961 attrMask ^= ATTR_ADSIZE;
David Woodhouse9c74fdb2014-01-20 12:02:48 +0000962
Vedant Kumarbf891b12015-08-26 16:20:29 +0000963 /*
964 * In 64-bit mode all f64 superscripted opcodes ignore opcode size prefix
965 * CALL/JMP/JCC instructions need to ignore 0x66 and consume 4 bytes
966 */
967
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000968 if ((insn->mode == MODE_64BIT) && insn->hasOpSize) {
Vedant Kumarbf891b12015-08-26 16:20:29 +0000969 switch (insn->opcode) {
970 case 0xE8:
971 case 0xE9:
Vedant Kumar44fccb72015-08-28 21:59:00 +0000972 // Take care of psubsb and other mmx instructions.
973 if (insn->opcodeType == ONEBYTE) {
Vedant Kumarbf891b12015-08-26 16:20:29 +0000974 attrMask ^= ATTR_OPSIZE;
975 insn->immediateSize = 4;
976 insn->displacementSize = 4;
977 }
978 break;
979 case 0x82:
980 case 0x83:
981 case 0x84:
982 case 0x85:
983 case 0x86:
984 case 0x87:
985 case 0x88:
986 case 0x89:
987 case 0x8A:
988 case 0x8B:
989 case 0x8C:
990 case 0x8D:
991 case 0x8E:
992 case 0x8F:
Vedant Kumar44fccb72015-08-28 21:59:00 +0000993 // Take care of lea and three byte ops.
994 if (insn->opcodeType == TWOBYTE) {
Vedant Kumarbf891b12015-08-26 16:20:29 +0000995 attrMask ^= ATTR_OPSIZE;
996 insn->immediateSize = 4;
Vedant Kumar44fccb72015-08-28 21:59:00 +0000997 insn->displacementSize = 4;
Vedant Kumarbf891b12015-08-26 16:20:29 +0000998 }
999 break;
1000 }
1001 }
1002
Craig Topper6e518772014-12-31 07:07:11 +00001003 if (getIDWithAttrMask(&instructionID, insn, attrMask))
1004 return -1;
David Woodhouse9c74fdb2014-01-20 12:02:48 +00001005
Sean Callanan04cc3072009-12-19 02:59:52 +00001006 /* The following clauses compensate for limitations of the tables. */
Craig Topperf01f1b52011-11-06 23:04:08 +00001007
Craig Topper0676b902014-10-07 07:29:50 +00001008 if (insn->mode != MODE_64BIT &&
1009 insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1010 /*
1011 * The tables can't distinquish between cases where the W-bit is used to
1012 * select register size and cases where its a required part of the opcode.
1013 */
1014 if ((insn->vectorExtensionType == TYPE_EVEX &&
1015 wFromEVEX3of4(insn->vectorExtensionPrefix[2])) ||
1016 (insn->vectorExtensionType == TYPE_VEX_3B &&
1017 wFromVEX3of3(insn->vectorExtensionPrefix[2])) ||
1018 (insn->vectorExtensionType == TYPE_XOP &&
1019 wFromXOP3of3(insn->vectorExtensionPrefix[2]))) {
1020
1021 uint16_t instructionIDWithREXW;
1022 if (getIDWithAttrMask(&instructionIDWithREXW,
1023 insn, attrMask | ATTR_REXW)) {
1024 insn->instructionID = instructionID;
1025 insn->spec = specifierForUID(instructionID);
1026 return 0;
1027 }
1028
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001029 auto SpecName = GetInstrName(instructionIDWithREXW, miiArg);
Craig Topper0676b902014-10-07 07:29:50 +00001030 // If not a 64-bit instruction. Switch the opcode.
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001031 if (!is64Bit(SpecName.data())) {
Craig Topper0676b902014-10-07 07:29:50 +00001032 insn->instructionID = instructionIDWithREXW;
1033 insn->spec = specifierForUID(instructionIDWithREXW);
1034 return 0;
1035 }
1036 }
1037 }
1038
Craig Topper99bcab72014-12-31 07:07:31 +00001039 /*
1040 * Absolute moves need special handling.
1041 * -For 16-bit mode because the meaning of the AdSize and OpSize prefixes are
1042 * inverted w.r.t.
1043 * -For 32-bit mode we need to ensure the ADSIZE prefix is observed in
1044 * any position.
1045 */
1046 if (insn->opcodeType == ONEBYTE && ((insn->opcode & 0xFC) == 0xA0)) {
1047 /* Make sure we observed the prefixes in any position. */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00001048 if (insn->hasAdSize)
Craig Topper99bcab72014-12-31 07:07:31 +00001049 attrMask |= ATTR_ADSIZE;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00001050 if (insn->hasOpSize)
Craig Topper99bcab72014-12-31 07:07:31 +00001051 attrMask |= ATTR_OPSIZE;
1052
1053 /* In 16-bit, invert the attributes. */
1054 if (insn->mode == MODE_16BIT)
1055 attrMask ^= ATTR_ADSIZE | ATTR_OPSIZE;
1056
1057 if (getIDWithAttrMask(&instructionID, insn, attrMask))
1058 return -1;
1059
1060 insn->instructionID = instructionID;
1061 insn->spec = specifierForUID(instructionID);
1062 return 0;
1063 }
1064
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00001065 if ((insn->mode == MODE_16BIT || insn->hasOpSize) &&
David Woodhouse5cf4c672014-01-20 12:02:35 +00001066 !(attrMask & ATTR_OPSIZE)) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001067 /*
1068 * The instruction tables make no distinction between instructions that
1069 * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a
1070 * particular spot (i.e., many MMX operations). In general we're
1071 * conservative, but in the specific case where OpSize is present but not
1072 * in the right place we check if there's a 16-bit operation.
1073 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001074
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +00001075 const struct InstructionSpecifier *spec;
Sean Callanan04cc3072009-12-19 02:59:52 +00001076 uint16_t instructionIDWithOpsize;
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001077 llvm::StringRef specName, specWithOpSizeName;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001078
Sean Callanan04cc3072009-12-19 02:59:52 +00001079 spec = specifierForUID(instructionID);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001080
Sean Callanan04cc3072009-12-19 02:59:52 +00001081 if (getIDWithAttrMask(&instructionIDWithOpsize,
1082 insn,
1083 attrMask | ATTR_OPSIZE)) {
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001084 /*
Sean Callanan04cc3072009-12-19 02:59:52 +00001085 * ModRM required with OpSize but not present; give up and return version
1086 * without OpSize set
1087 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001088
Sean Callanan04cc3072009-12-19 02:59:52 +00001089 insn->instructionID = instructionID;
1090 insn->spec = spec;
1091 return 0;
1092 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001093
Richard Smith89ee75d2014-04-20 21:07:34 +00001094 specName = GetInstrName(instructionID, miiArg);
1095 specWithOpSizeName = GetInstrName(instructionIDWithOpsize, miiArg);
Benjamin Kramer478e8de2012-02-11 14:50:54 +00001096
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001097 if (is16BitEquivalent(specName.data(), specWithOpSizeName.data()) &&
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00001098 (insn->mode == MODE_16BIT) ^ insn->hasOpSize) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001099 insn->instructionID = instructionIDWithOpsize;
Benjamin Kramer915e3d92012-02-11 16:01:02 +00001100 insn->spec = specifierForUID(instructionIDWithOpsize);
Sean Callanan04cc3072009-12-19 02:59:52 +00001101 } else {
1102 insn->instructionID = instructionID;
1103 insn->spec = spec;
1104 }
1105 return 0;
1106 }
Craig Topper21c33652011-10-02 16:56:09 +00001107
1108 if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 &&
1109 insn->rexPrefix & 0x01) {
1110 /*
1111 * NOOP shouldn't decode as NOOP if REX.b is set. Instead
1112 * it should decode as XCHG %r8, %eax.
1113 */
1114
1115 const struct InstructionSpecifier *spec;
1116 uint16_t instructionIDWithNewOpcode;
1117 const struct InstructionSpecifier *specWithNewOpcode;
1118
1119 spec = specifierForUID(instructionID);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001120
Craig Topperb58a9662011-10-05 03:29:32 +00001121 /* Borrow opcode from one of the other XCHGar opcodes */
Craig Topper21c33652011-10-02 16:56:09 +00001122 insn->opcode = 0x91;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001123
Craig Topper21c33652011-10-02 16:56:09 +00001124 if (getIDWithAttrMask(&instructionIDWithNewOpcode,
1125 insn,
1126 attrMask)) {
1127 insn->opcode = 0x90;
1128
1129 insn->instructionID = instructionID;
1130 insn->spec = spec;
1131 return 0;
1132 }
1133
1134 specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode);
1135
Craig Topperb58a9662011-10-05 03:29:32 +00001136 /* Change back */
Craig Topper21c33652011-10-02 16:56:09 +00001137 insn->opcode = 0x90;
1138
1139 insn->instructionID = instructionIDWithNewOpcode;
1140 insn->spec = specWithNewOpcode;
1141
1142 return 0;
1143 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001144
Sean Callanan04cc3072009-12-19 02:59:52 +00001145 insn->instructionID = instructionID;
1146 insn->spec = specifierForUID(insn->instructionID);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001147
Sean Callanan04cc3072009-12-19 02:59:52 +00001148 return 0;
1149}
1150
1151/*
1152 * readSIB - Consumes the SIB byte to determine addressing information for an
1153 * instruction.
1154 *
1155 * @param insn - The instruction whose SIB byte is to be read.
1156 * @return - 0 if the SIB byte was successfully read; nonzero otherwise.
1157 */
1158static int readSIB(struct InternalInstruction* insn) {
Richard Smith89ee75d2014-04-20 21:07:34 +00001159 SIBBase sibBaseBase = SIB_BASE_NONE;
Sean Callanan04cc3072009-12-19 02:59:52 +00001160 uint8_t index, base;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001161
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001162 dbgprintf(insn, "readSIB()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001163
Sean Callanan04cc3072009-12-19 02:59:52 +00001164 if (insn->consumedSIB)
1165 return 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001166
Richard Smith5d5061032014-04-20 22:15:37 +00001167 insn->consumedSIB = true;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001168
Sean Callanan04cc3072009-12-19 02:59:52 +00001169 switch (insn->addressSize) {
1170 case 2:
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001171 dbgprintf(insn, "SIB-based addressing doesn't work in 16-bit mode");
Sean Callanan04cc3072009-12-19 02:59:52 +00001172 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001173 case 4:
Craig Topperca2382d2017-10-21 20:03:20 +00001174 insn->sibIndexBase = SIB_INDEX_EAX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001175 sibBaseBase = SIB_BASE_EAX;
1176 break;
1177 case 8:
Craig Topperca2382d2017-10-21 20:03:20 +00001178 insn->sibIndexBase = SIB_INDEX_RAX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001179 sibBaseBase = SIB_BASE_RAX;
1180 break;
1181 }
1182
1183 if (consumeByte(insn, &insn->sib))
1184 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001185
Sean Callanan04cc3072009-12-19 02:59:52 +00001186 index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
Douglas Katzmanfcda6f82015-06-24 22:04:55 +00001187
Douglas Katzmanfcda6f82015-06-24 22:04:55 +00001188 if (index == 0x4) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001189 insn->sibIndex = SIB_INDEX_NONE;
Douglas Katzmanfcda6f82015-06-24 22:04:55 +00001190 } else {
Craig Topperca2382d2017-10-21 20:03:20 +00001191 insn->sibIndex = (SIBIndex)(insn->sibIndexBase + index);
Sean Callanan04cc3072009-12-19 02:59:52 +00001192 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001193
Douglas Katzmanfcda6f82015-06-24 22:04:55 +00001194 insn->sibScale = 1 << scaleFromSIB(insn->sib);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001195
Sean Callanan04cc3072009-12-19 02:59:52 +00001196 base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001197
Sean Callanan04cc3072009-12-19 02:59:52 +00001198 switch (base) {
1199 case 0x5:
Craig Topperfae5ac22014-02-17 10:03:43 +00001200 case 0xd:
Sean Callanan04cc3072009-12-19 02:59:52 +00001201 switch (modFromModRM(insn->modRM)) {
1202 case 0x0:
1203 insn->eaDisplacement = EA_DISP_32;
1204 insn->sibBase = SIB_BASE_NONE;
1205 break;
1206 case 0x1:
1207 insn->eaDisplacement = EA_DISP_8;
Craig Topperfae5ac22014-02-17 10:03:43 +00001208 insn->sibBase = (SIBBase)(sibBaseBase + base);
Sean Callanan04cc3072009-12-19 02:59:52 +00001209 break;
1210 case 0x2:
1211 insn->eaDisplacement = EA_DISP_32;
Craig Topperfae5ac22014-02-17 10:03:43 +00001212 insn->sibBase = (SIBBase)(sibBaseBase + base);
Sean Callanan04cc3072009-12-19 02:59:52 +00001213 break;
1214 case 0x3:
Sean Callanan010b3732010-04-02 21:23:51 +00001215 debug("Cannot have Mod = 0b11 and a SIB byte");
1216 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001217 }
1218 break;
1219 default:
Benjamin Kramer25bddae2011-02-27 18:13:53 +00001220 insn->sibBase = (SIBBase)(sibBaseBase + base);
Sean Callanan04cc3072009-12-19 02:59:52 +00001221 break;
1222 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001223
Sean Callanan04cc3072009-12-19 02:59:52 +00001224 return 0;
1225}
1226
1227/*
1228 * readDisplacement - Consumes the displacement of an instruction.
1229 *
1230 * @param insn - The instruction whose displacement is to be read.
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001231 * @return - 0 if the displacement byte was successfully read; nonzero
Sean Callanan04cc3072009-12-19 02:59:52 +00001232 * otherwise.
1233 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001234static int readDisplacement(struct InternalInstruction* insn) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001235 int8_t d8;
1236 int16_t d16;
1237 int32_t d32;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001238
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001239 dbgprintf(insn, "readDisplacement()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001240
Sean Callanan04cc3072009-12-19 02:59:52 +00001241 if (insn->consumedDisplacement)
1242 return 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001243
Richard Smith5d5061032014-04-20 22:15:37 +00001244 insn->consumedDisplacement = true;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +00001245 insn->displacementOffset = insn->readerCursor - insn->startLocation;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001246
Sean Callanan04cc3072009-12-19 02:59:52 +00001247 switch (insn->eaDisplacement) {
1248 case EA_DISP_NONE:
Richard Smith5d5061032014-04-20 22:15:37 +00001249 insn->consumedDisplacement = false;
Sean Callanan04cc3072009-12-19 02:59:52 +00001250 break;
1251 case EA_DISP_8:
1252 if (consumeInt8(insn, &d8))
1253 return -1;
1254 insn->displacement = d8;
1255 break;
1256 case EA_DISP_16:
1257 if (consumeInt16(insn, &d16))
1258 return -1;
1259 insn->displacement = d16;
1260 break;
1261 case EA_DISP_32:
1262 if (consumeInt32(insn, &d32))
1263 return -1;
1264 insn->displacement = d32;
1265 break;
1266 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001267
Richard Smith5d5061032014-04-20 22:15:37 +00001268 insn->consumedDisplacement = true;
Sean Callanan04cc3072009-12-19 02:59:52 +00001269 return 0;
1270}
1271
1272/*
1273 * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and
1274 * displacement) for an instruction and interprets it.
1275 *
1276 * @param insn - The instruction whose addressing information is to be read.
1277 * @return - 0 if the information was successfully read; nonzero otherwise.
1278 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001279static int readModRM(struct InternalInstruction* insn) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001280 uint8_t mod, rm, reg;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001281
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001282 dbgprintf(insn, "readModRM()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001283
Sean Callanan04cc3072009-12-19 02:59:52 +00001284 if (insn->consumedModRM)
1285 return 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001286
Rafael Espindola9f9a1062011-01-06 16:48:42 +00001287 if (consumeByte(insn, &insn->modRM))
1288 return -1;
Richard Smith5d5061032014-04-20 22:15:37 +00001289 insn->consumedModRM = true;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001290
Sean Callanan04cc3072009-12-19 02:59:52 +00001291 mod = modFromModRM(insn->modRM);
1292 rm = rmFromModRM(insn->modRM);
1293 reg = regFromModRM(insn->modRM);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001294
Sean Callanan04cc3072009-12-19 02:59:52 +00001295 /*
1296 * This goes by insn->registerSize to pick the correct register, which messes
1297 * up if we're using (say) XMM or 8-bit register operands. That gets fixed in
1298 * fixupReg().
1299 */
1300 switch (insn->registerSize) {
1301 case 2:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001302 insn->regBase = MODRM_REG_AX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001303 insn->eaRegBase = EA_REG_AX;
1304 break;
1305 case 4:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001306 insn->regBase = MODRM_REG_EAX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001307 insn->eaRegBase = EA_REG_EAX;
1308 break;
1309 case 8:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001310 insn->regBase = MODRM_REG_RAX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001311 insn->eaRegBase = EA_REG_RAX;
1312 break;
1313 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001314
Sean Callanan04cc3072009-12-19 02:59:52 +00001315 reg |= rFromREX(insn->rexPrefix) << 3;
1316 rm |= bFromREX(insn->rexPrefix) << 3;
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001317 if (insn->vectorExtensionType == TYPE_EVEX) {
1318 reg |= r2FromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1319 rm |= xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1320 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001321
Sean Callanan04cc3072009-12-19 02:59:52 +00001322 insn->reg = (Reg)(insn->regBase + reg);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001323
Sean Callanan04cc3072009-12-19 02:59:52 +00001324 switch (insn->addressSize) {
1325 case 2:
1326 insn->eaBaseBase = EA_BASE_BX_SI;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001327
Sean Callanan04cc3072009-12-19 02:59:52 +00001328 switch (mod) {
1329 case 0x0:
1330 if (rm == 0x6) {
1331 insn->eaBase = EA_BASE_NONE;
1332 insn->eaDisplacement = EA_DISP_16;
Sean Callanan010b3732010-04-02 21:23:51 +00001333 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001334 return -1;
1335 } else {
1336 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1337 insn->eaDisplacement = EA_DISP_NONE;
1338 }
1339 break;
1340 case 0x1:
1341 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1342 insn->eaDisplacement = EA_DISP_8;
Craig Topper399e39e2014-01-25 22:48:43 +00001343 insn->displacementSize = 1;
Sean Callanan010b3732010-04-02 21:23:51 +00001344 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001345 return -1;
1346 break;
1347 case 0x2:
1348 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1349 insn->eaDisplacement = EA_DISP_16;
Sean Callanan010b3732010-04-02 21:23:51 +00001350 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001351 return -1;
1352 break;
1353 case 0x3:
1354 insn->eaBase = (EABase)(insn->eaRegBase + rm);
Sean Callanan010b3732010-04-02 21:23:51 +00001355 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001356 return -1;
1357 break;
1358 }
1359 break;
1360 case 4:
1361 case 8:
1362 insn->eaBaseBase = (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001363
Sean Callanan04cc3072009-12-19 02:59:52 +00001364 switch (mod) {
1365 case 0x0:
1366 insn->eaDisplacement = EA_DISP_NONE; /* readSIB may override this */
Douglas Katzman6dc13972015-05-13 22:44:52 +00001367 // In determining whether RIP-relative mode is used (rm=5),
1368 // or whether a SIB byte is present (rm=4),
1369 // the extension bits (REX.b and EVEX.x) are ignored.
1370 switch (rm & 7) {
1371 case 0x4: // SIB byte is present
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001372 insn->eaBase = (insn->addressSize == 4 ?
Sean Callanan04cc3072009-12-19 02:59:52 +00001373 EA_BASE_sib : EA_BASE_sib64);
Craig Topper38afbfd2014-03-20 05:56:00 +00001374 if (readSIB(insn) || readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001375 return -1;
1376 break;
Douglas Katzman6dc13972015-05-13 22:44:52 +00001377 case 0x5: // RIP-relative
Sean Callanan04cc3072009-12-19 02:59:52 +00001378 insn->eaBase = EA_BASE_NONE;
1379 insn->eaDisplacement = EA_DISP_32;
Sean Callanan010b3732010-04-02 21:23:51 +00001380 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001381 return -1;
1382 break;
1383 default:
1384 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1385 break;
1386 }
1387 break;
1388 case 0x1:
Craig Topper399e39e2014-01-25 22:48:43 +00001389 insn->displacementSize = 1;
Alp Toker771f7652014-01-26 18:44:34 +00001390 /* FALLTHROUGH */
Sean Callanan04cc3072009-12-19 02:59:52 +00001391 case 0x2:
1392 insn->eaDisplacement = (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
Douglas Katzman6dc13972015-05-13 22:44:52 +00001393 switch (rm & 7) {
1394 case 0x4: // SIB byte is present
Sean Callanan04cc3072009-12-19 02:59:52 +00001395 insn->eaBase = EA_BASE_sib;
Craig Topper38afbfd2014-03-20 05:56:00 +00001396 if (readSIB(insn) || readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001397 return -1;
1398 break;
1399 default:
1400 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
Sean Callanan010b3732010-04-02 21:23:51 +00001401 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001402 return -1;
1403 break;
1404 }
1405 break;
1406 case 0x3:
1407 insn->eaDisplacement = EA_DISP_NONE;
1408 insn->eaBase = (EABase)(insn->eaRegBase + rm);
1409 break;
1410 }
1411 break;
1412 } /* switch (insn->addressSize) */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001413
Sean Callanan04cc3072009-12-19 02:59:52 +00001414 return 0;
1415}
1416
1417#define GENERIC_FIXUP_FUNC(name, base, prefix) \
Ahmed Bougacha85dc93c2016-07-14 14:53:21 +00001418 static uint16_t name(struct InternalInstruction *insn, \
1419 OperandType type, \
1420 uint8_t index, \
1421 uint8_t *valid) { \
Sean Callanan04cc3072009-12-19 02:59:52 +00001422 *valid = 1; \
1423 switch (type) { \
1424 default: \
Sean Callanan010b3732010-04-02 21:23:51 +00001425 debug("Unhandled register type"); \
1426 *valid = 0; \
1427 return 0; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001428 case TYPE_Rv: \
1429 return base + index; \
1430 case TYPE_R8: \
Sean Callanan010b3732010-04-02 21:23:51 +00001431 if (insn->rexPrefix && \
Sean Callanan04cc3072009-12-19 02:59:52 +00001432 index >= 4 && index <= 7) { \
1433 return prefix##_SPL + (index - 4); \
1434 } else { \
1435 return prefix##_AL + index; \
1436 } \
1437 case TYPE_R16: \
1438 return prefix##_AX + index; \
1439 case TYPE_R32: \
1440 return prefix##_EAX + index; \
1441 case TYPE_R64: \
1442 return prefix##_RAX + index; \
Craig Topperad944a12017-01-16 06:49:03 +00001443 case TYPE_ZMM: \
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001444 return prefix##_ZMM0 + index; \
Craig Topperad944a12017-01-16 06:49:03 +00001445 case TYPE_YMM: \
Sean Callananc3fd5232011-03-15 01:23:15 +00001446 return prefix##_YMM0 + index; \
Craig Topperad944a12017-01-16 06:49:03 +00001447 case TYPE_XMM: \
Sean Callanan04cc3072009-12-19 02:59:52 +00001448 return prefix##_XMM0 + index; \
Craig Topperad944a12017-01-16 06:49:03 +00001449 case TYPE_VK: \
Craig Topper9c26bcc2015-03-02 03:33:11 +00001450 if (index > 7) \
1451 *valid = 0; \
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001452 return prefix##_K0 + index; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001453 case TYPE_MM64: \
Craig Topperd5b39232014-12-26 18:19:44 +00001454 return prefix##_MM0 + (index & 0x7); \
Sean Callanan04cc3072009-12-19 02:59:52 +00001455 case TYPE_SEGMENTREG: \
Sean Callanan010b3732010-04-02 21:23:51 +00001456 if (index > 5) \
Sean Callanan04cc3072009-12-19 02:59:52 +00001457 *valid = 0; \
1458 return prefix##_ES + index; \
1459 case TYPE_DEBUGREG: \
Sean Callanan04cc3072009-12-19 02:59:52 +00001460 return prefix##_DR0 + index; \
Sean Callanane7e1cf92010-05-06 20:59:00 +00001461 case TYPE_CONTROLREG: \
Sean Callanane7e1cf92010-05-06 20:59:00 +00001462 return prefix##_CR0 + index; \
Ahmed Bougacha85dc93c2016-07-14 14:53:21 +00001463 case TYPE_BNDR: \
1464 if (index > 3) \
1465 *valid = 0; \
1466 return prefix##_BND0 + index; \
Craig Topperca2382d2017-10-21 20:03:20 +00001467 case TYPE_MVSIBX: \
1468 return prefix##_XMM0 + index; \
1469 case TYPE_MVSIBY: \
1470 return prefix##_YMM0 + index; \
1471 case TYPE_MVSIBZ: \
1472 return prefix##_ZMM0 + index; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001473 } \
1474 }
1475
1476/*
1477 * fixup*Value - Consults an operand type to determine the meaning of the
1478 * reg or R/M field. If the operand is an XMM operand, for example, an
1479 * operand would be XMM0 instead of AX, which readModRM() would otherwise
1480 * misinterpret it as.
1481 *
1482 * @param insn - The instruction containing the operand.
1483 * @param type - The operand type.
1484 * @param index - The existing value of the field as reported by readModRM().
1485 * @param valid - The address of a uint8_t. The target is set to 1 if the
1486 * field is valid for the register class; 0 if not.
Sean Callanan010b3732010-04-02 21:23:51 +00001487 * @return - The proper value.
Sean Callanan04cc3072009-12-19 02:59:52 +00001488 */
Sean Callanan2f9443f2009-12-22 02:07:42 +00001489GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase, MODRM_REG)
Sean Callanan04cc3072009-12-19 02:59:52 +00001490GENERIC_FIXUP_FUNC(fixupRMValue, insn->eaRegBase, EA_REG)
1491
1492/*
1493 * fixupReg - Consults an operand specifier to determine which of the
1494 * fixup*Value functions to use in correcting readModRM()'ss interpretation.
1495 *
1496 * @param insn - See fixup*Value().
1497 * @param op - The operand specifier.
1498 * @return - 0 if fixup was successful; -1 if the register returned was
1499 * invalid for its class.
1500 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001501static int fixupReg(struct InternalInstruction *insn,
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +00001502 const struct OperandSpecifier *op) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001503 uint8_t valid;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001504
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001505 dbgprintf(insn, "fixupReg()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001506
Sean Callanan04cc3072009-12-19 02:59:52 +00001507 switch ((OperandEncoding)op->encoding) {
1508 default:
Sean Callanan010b3732010-04-02 21:23:51 +00001509 debug("Expected a REG or R/M encoding in fixupReg");
1510 return -1;
Sean Callananc3fd5232011-03-15 01:23:15 +00001511 case ENCODING_VVVV:
1512 insn->vvvv = (Reg)fixupRegValue(insn,
1513 (OperandType)op->type,
1514 insn->vvvv,
1515 &valid);
1516 if (!valid)
1517 return -1;
1518 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001519 case ENCODING_REG:
1520 insn->reg = (Reg)fixupRegValue(insn,
1521 (OperandType)op->type,
1522 insn->reg - insn->regBase,
1523 &valid);
1524 if (!valid)
1525 return -1;
1526 break;
Adam Nemet5933c2f2014-07-17 17:04:56 +00001527 CASE_ENCODING_RM:
Sean Callanan04cc3072009-12-19 02:59:52 +00001528 if (insn->eaBase >= insn->eaRegBase) {
1529 insn->eaBase = (EABase)fixupRMValue(insn,
1530 (OperandType)op->type,
1531 insn->eaBase - insn->eaRegBase,
1532 &valid);
1533 if (!valid)
1534 return -1;
1535 }
1536 break;
1537 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001538
Sean Callanan04cc3072009-12-19 02:59:52 +00001539 return 0;
1540}
1541
1542/*
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001543 * readOpcodeRegister - Reads an operand from the opcode field of an
Sean Callanan04cc3072009-12-19 02:59:52 +00001544 * instruction and interprets it appropriately given the operand width.
1545 * Handles AddRegFrm instructions.
1546 *
Craig Topper91551182014-01-01 15:29:32 +00001547 * @param insn - the instruction whose opcode field is to be read.
Sean Callanan04cc3072009-12-19 02:59:52 +00001548 * @param size - The width (in bytes) of the register being specified.
1549 * 1 means AL and friends, 2 means AX, 4 means EAX, and 8 means
1550 * RAX.
Sean Callanan010b3732010-04-02 21:23:51 +00001551 * @return - 0 on success; nonzero otherwise.
Sean Callanan04cc3072009-12-19 02:59:52 +00001552 */
Sean Callanan010b3732010-04-02 21:23:51 +00001553static int readOpcodeRegister(struct InternalInstruction* insn, uint8_t size) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001554 dbgprintf(insn, "readOpcodeRegister()");
Sean Callanan04cc3072009-12-19 02:59:52 +00001555
Sean Callanan04cc3072009-12-19 02:59:52 +00001556 if (size == 0)
1557 size = insn->registerSize;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001558
Sean Callanan04cc3072009-12-19 02:59:52 +00001559 switch (size) {
1560 case 1:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001561 insn->opcodeRegister = (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001562 | (insn->opcode & 7)));
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001563 if (insn->rexPrefix &&
Sean Callanan010b3732010-04-02 21:23:51 +00001564 insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1565 insn->opcodeRegister < MODRM_REG_AL + 0x8) {
Sean Callanan2f9443f2009-12-22 02:07:42 +00001566 insn->opcodeRegister = (Reg)(MODRM_REG_SPL
1567 + (insn->opcodeRegister - MODRM_REG_AL - 4));
Sean Callanan04cc3072009-12-19 02:59:52 +00001568 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001569
Sean Callanan04cc3072009-12-19 02:59:52 +00001570 break;
1571 case 2:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001572 insn->opcodeRegister = (Reg)(MODRM_REG_AX
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001573 + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001574 | (insn->opcode & 7)));
Sean Callanan04cc3072009-12-19 02:59:52 +00001575 break;
1576 case 4:
Sean Callanan010b3732010-04-02 21:23:51 +00001577 insn->opcodeRegister = (Reg)(MODRM_REG_EAX
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001578 + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001579 | (insn->opcode & 7)));
Sean Callanan04cc3072009-12-19 02:59:52 +00001580 break;
1581 case 8:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001582 insn->opcodeRegister = (Reg)(MODRM_REG_RAX
1583 + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001584 | (insn->opcode & 7)));
Sean Callanan04cc3072009-12-19 02:59:52 +00001585 break;
1586 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001587
Sean Callanan010b3732010-04-02 21:23:51 +00001588 return 0;
Sean Callanan04cc3072009-12-19 02:59:52 +00001589}
1590
1591/*
1592 * readImmediate - Consumes an immediate operand from an instruction, given the
1593 * desired operand size.
1594 *
1595 * @param insn - The instruction whose operand is to be read.
1596 * @param size - The width (in bytes) of the operand.
1597 * @return - 0 if the immediate was successfully consumed; nonzero
1598 * otherwise.
1599 */
1600static int readImmediate(struct InternalInstruction* insn, uint8_t size) {
1601 uint8_t imm8;
1602 uint16_t imm16;
1603 uint32_t imm32;
1604 uint64_t imm64;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001605
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001606 dbgprintf(insn, "readImmediate()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001607
Sean Callanan010b3732010-04-02 21:23:51 +00001608 if (insn->numImmediatesConsumed == 2) {
1609 debug("Already consumed two immediates");
1610 return -1;
1611 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001612
Sean Callanan04cc3072009-12-19 02:59:52 +00001613 if (size == 0)
1614 size = insn->immediateSize;
1615 else
1616 insn->immediateSize = size;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +00001617 insn->immediateOffset = insn->readerCursor - insn->startLocation;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001618
Sean Callanan04cc3072009-12-19 02:59:52 +00001619 switch (size) {
1620 case 1:
1621 if (consumeByte(insn, &imm8))
1622 return -1;
1623 insn->immediates[insn->numImmediatesConsumed] = imm8;
1624 break;
1625 case 2:
1626 if (consumeUInt16(insn, &imm16))
1627 return -1;
1628 insn->immediates[insn->numImmediatesConsumed] = imm16;
1629 break;
1630 case 4:
1631 if (consumeUInt32(insn, &imm32))
1632 return -1;
1633 insn->immediates[insn->numImmediatesConsumed] = imm32;
1634 break;
1635 case 8:
1636 if (consumeUInt64(insn, &imm64))
1637 return -1;
1638 insn->immediates[insn->numImmediatesConsumed] = imm64;
1639 break;
1640 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001641
Sean Callanan04cc3072009-12-19 02:59:52 +00001642 insn->numImmediatesConsumed++;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001643
Sean Callanan04cc3072009-12-19 02:59:52 +00001644 return 0;
1645}
1646
1647/*
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001648 * readVVVV - Consumes vvvv from an instruction if it has a VEX prefix.
Sean Callananc3fd5232011-03-15 01:23:15 +00001649 *
1650 * @param insn - The instruction whose operand is to be read.
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001651 * @return - 0 if the vvvv was successfully consumed; nonzero
Sean Callananc3fd5232011-03-15 01:23:15 +00001652 * otherwise.
1653 */
1654static int readVVVV(struct InternalInstruction* insn) {
1655 dbgprintf(insn, "readVVVV()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001656
Richard Smith89ee75d2014-04-20 21:07:34 +00001657 int vvvv;
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001658 if (insn->vectorExtensionType == TYPE_EVEX)
Adam Nemet8ae70502014-06-24 01:42:32 +00001659 vvvv = (v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4 |
1660 vvvvFromEVEX3of4(insn->vectorExtensionPrefix[2]));
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001661 else if (insn->vectorExtensionType == TYPE_VEX_3B)
Richard Smith89ee75d2014-04-20 21:07:34 +00001662 vvvv = vvvvFromVEX3of3(insn->vectorExtensionPrefix[2]);
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001663 else if (insn->vectorExtensionType == TYPE_VEX_2B)
Richard Smith89ee75d2014-04-20 21:07:34 +00001664 vvvv = vvvvFromVEX2of2(insn->vectorExtensionPrefix[1]);
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001665 else if (insn->vectorExtensionType == TYPE_XOP)
Richard Smith89ee75d2014-04-20 21:07:34 +00001666 vvvv = vvvvFromXOP3of3(insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +00001667 else
1668 return -1;
1669
Craig Topper0d0be472011-10-03 08:14:29 +00001670 if (insn->mode != MODE_64BIT)
Richard Smith89ee75d2014-04-20 21:07:34 +00001671 vvvv &= 0x7;
Craig Topper0d0be472011-10-03 08:14:29 +00001672
Richard Smith89ee75d2014-04-20 21:07:34 +00001673 insn->vvvv = static_cast<Reg>(vvvv);
Sean Callananc3fd5232011-03-15 01:23:15 +00001674 return 0;
1675}
1676
1677/*
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001678 * readMaskRegister - Reads an mask register from the opcode field of an
1679 * instruction.
1680 *
1681 * @param insn - The instruction whose opcode field is to be read.
1682 * @return - 0 on success; nonzero otherwise.
1683 */
1684static int readMaskRegister(struct InternalInstruction* insn) {
1685 dbgprintf(insn, "readMaskRegister()");
1686
1687 if (insn->vectorExtensionType != TYPE_EVEX)
1688 return -1;
1689
Richard Smith89ee75d2014-04-20 21:07:34 +00001690 insn->writemask =
1691 static_cast<Reg>(aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]));
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001692 return 0;
1693}
1694
1695/*
Sean Callanan04cc3072009-12-19 02:59:52 +00001696 * readOperands - Consults the specifier for an instruction and consumes all
1697 * operands for that instruction, interpreting them as it goes.
1698 *
1699 * @param insn - The instruction whose operands are to be read and interpreted.
1700 * @return - 0 if all operands could be read; nonzero otherwise.
1701 */
1702static int readOperands(struct InternalInstruction* insn) {
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001703 int hasVVVV, needVVVV;
Craig Topper2ba766a2011-12-30 06:23:39 +00001704 int sawRegImm = 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001705
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001706 dbgprintf(insn, "readOperands()");
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001707
1708 /* If non-zero vvvv specified, need to make sure one of the operands
1709 uses it. */
1710 hasVVVV = !readVVVV(insn);
1711 needVVVV = hasVVVV && (insn->vvvv != 0);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001712
Patrik Hagglund31998382014-04-28 12:12:27 +00001713 for (const auto &Op : x86OperandSets[insn->spec->operands]) {
1714 switch (Op.encoding) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001715 case ENCODING_NONE:
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00001716 case ENCODING_SI:
David Woodhouseb33c2ef2014-01-22 15:08:21 +00001717 case ENCODING_DI:
Sean Callanan04cc3072009-12-19 02:59:52 +00001718 break;
Craig Topper33ac0642017-01-16 05:44:25 +00001719 CASE_ENCODING_VSIB:
1720 // VSIB can use the V2 bit so check only the other bits.
1721 if (needVVVV)
1722 needVVVV = hasVVVV & ((insn->vvvv & 0xf) != 0);
Craig Topper3173a1f2017-01-16 05:44:33 +00001723 if (readModRM(insn))
1724 return -1;
Craig Topperca2382d2017-10-21 20:03:20 +00001725
Craig Topper158bc642017-10-22 04:32:30 +00001726 // Reject if SIB wasn't used.
1727 if (insn->eaBase != EA_BASE_sib && insn->eaBase != EA_BASE_sib64)
1728 return -1;
1729
Craig Topperca2382d2017-10-21 20:03:20 +00001730 // If sibIndex was set to SIB_INDEX_NONE, index offset is 4.
1731 if (insn->sibIndex == SIB_INDEX_NONE)
1732 insn->sibIndex = (SIBIndex)4;
1733
1734 // If EVEX.v2 is set this is one of the 16-31 registers.
1735 if (insn->vectorExtensionType == TYPE_EVEX &&
1736 v2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
1737 insn->sibIndex = (SIBIndex)(insn->sibIndex + 16);
1738
1739 // Adjust the index register to the correct size.
1740 switch ((OperandType)Op.type) {
1741 default:
1742 debug("Unhandled VSIB index type");
Craig Topper3173a1f2017-01-16 05:44:33 +00001743 return -1;
Craig Topperca2382d2017-10-21 20:03:20 +00001744 case TYPE_MVSIBX:
1745 insn->sibIndex = (SIBIndex)(SIB_INDEX_XMM0 +
1746 (insn->sibIndex - insn->sibIndexBase));
1747 break;
1748 case TYPE_MVSIBY:
1749 insn->sibIndex = (SIBIndex)(SIB_INDEX_YMM0 +
1750 (insn->sibIndex - insn->sibIndexBase));
1751 break;
1752 case TYPE_MVSIBZ:
1753 insn->sibIndex = (SIBIndex)(SIB_INDEX_ZMM0 +
1754 (insn->sibIndex - insn->sibIndexBase));
1755 break;
1756 }
1757
Craig Topper3173a1f2017-01-16 05:44:33 +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_VSIB);
1761 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001762 case ENCODING_REG:
Adam Nemet5933c2f2014-07-17 17:04:56 +00001763 CASE_ENCODING_RM:
Sean Callanan04cc3072009-12-19 02:59:52 +00001764 if (readModRM(insn))
1765 return -1;
Patrik Hagglund31998382014-04-28 12:12:27 +00001766 if (fixupReg(insn, &Op))
Sean Callanan04cc3072009-12-19 02:59:52 +00001767 return -1;
Adam Nemet5933c2f2014-07-17 17:04:56 +00001768 // Apply the AVX512 compressed displacement scaling factor.
1769 if (Op.encoding != ENCODING_REG && insn->eaDisplacement == EA_DISP_8)
1770 insn->displacement *= 1 << (Op.encoding - ENCODING_RM);
Sean Callanan04cc3072009-12-19 02:59:52 +00001771 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001772 case ENCODING_IB:
Craig Topper2ba766a2011-12-30 06:23:39 +00001773 if (sawRegImm) {
Benjamin Kramer9c48f262012-01-04 22:06:45 +00001774 /* Saw a register immediate so don't read again and instead split the
1775 previous immediate. FIXME: This is a hack. */
Benjamin Kramer47aecca2012-01-01 17:55:36 +00001776 insn->immediates[insn->numImmediatesConsumed] =
1777 insn->immediates[insn->numImmediatesConsumed - 1] & 0xf;
1778 ++insn->numImmediatesConsumed;
Craig Topper2ba766a2011-12-30 06:23:39 +00001779 break;
1780 }
Sean Callanan04cc3072009-12-19 02:59:52 +00001781 if (readImmediate(insn, 1))
1782 return -1;
Craig Topperad944a12017-01-16 06:49:03 +00001783 if (Op.type == TYPE_XMM || Op.type == TYPE_YMM)
Craig Topper2ba766a2011-12-30 06:23:39 +00001784 sawRegImm = 1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001785 break;
1786 case ENCODING_IW:
1787 if (readImmediate(insn, 2))
1788 return -1;
1789 break;
1790 case ENCODING_ID:
1791 if (readImmediate(insn, 4))
1792 return -1;
1793 break;
1794 case ENCODING_IO:
1795 if (readImmediate(insn, 8))
1796 return -1;
1797 break;
1798 case ENCODING_Iv:
Sean Callanan010b3732010-04-02 21:23:51 +00001799 if (readImmediate(insn, insn->immediateSize))
1800 return -1;
Chris Lattnerd4758fc2010-04-16 21:15:15 +00001801 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001802 case ENCODING_Ia:
Sean Callanan010b3732010-04-02 21:23:51 +00001803 if (readImmediate(insn, insn->addressSize))
1804 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001805 break;
Craig Topper326008c2017-10-23 02:26:24 +00001806 case ENCODING_IRC:
1807 insn->RC = (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 1) |
1808 lFromEVEX4of4(insn->vectorExtensionPrefix[3]);
1809 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001810 case ENCODING_RB:
Sean Callanan010b3732010-04-02 21:23:51 +00001811 if (readOpcodeRegister(insn, 1))
1812 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001813 break;
1814 case ENCODING_RW:
Sean Callanan010b3732010-04-02 21:23:51 +00001815 if (readOpcodeRegister(insn, 2))
1816 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001817 break;
1818 case ENCODING_RD:
Sean Callanan010b3732010-04-02 21:23:51 +00001819 if (readOpcodeRegister(insn, 4))
1820 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001821 break;
1822 case ENCODING_RO:
Sean Callanan010b3732010-04-02 21:23:51 +00001823 if (readOpcodeRegister(insn, 8))
1824 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001825 break;
1826 case ENCODING_Rv:
Sean Callanan010b3732010-04-02 21:23:51 +00001827 if (readOpcodeRegister(insn, 0))
1828 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001829 break;
Craig Topper623b0d62014-01-01 14:22:37 +00001830 case ENCODING_FP:
Sean Callananc3fd5232011-03-15 01:23:15 +00001831 break;
1832 case ENCODING_VVVV:
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001833 needVVVV = 0; /* Mark that we have found a VVVV operand. */
1834 if (!hasVVVV)
Sean Callananc3fd5232011-03-15 01:23:15 +00001835 return -1;
Patrik Hagglund31998382014-04-28 12:12:27 +00001836 if (fixupReg(insn, &Op))
Sean Callananc3fd5232011-03-15 01:23:15 +00001837 return -1;
1838 break;
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001839 case ENCODING_WRITEMASK:
1840 if (readMaskRegister(insn))
1841 return -1;
1842 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001843 case ENCODING_DUP:
1844 break;
1845 default:
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001846 dbgprintf(insn, "Encountered an operand with an unknown encoding.");
Sean Callanan04cc3072009-12-19 02:59:52 +00001847 return -1;
1848 }
1849 }
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001850
1851 /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */
1852 if (needVVVV) return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001853
Sean Callanan04cc3072009-12-19 02:59:52 +00001854 return 0;
1855}
1856
1857/*
1858 * decodeInstruction - Reads and interprets a full instruction provided by the
1859 * user.
1860 *
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001861 * @param insn - A pointer to the instruction to be populated. Must be
Sean Callanan04cc3072009-12-19 02:59:52 +00001862 * pre-allocated.
1863 * @param reader - The function to be used to read the instruction's bytes.
1864 * @param readerArg - A generic argument to be passed to the reader to store
1865 * any internal state.
1866 * @param logger - If non-NULL, the function to be used to write log messages
1867 * and warnings.
1868 * @param loggerArg - A generic argument to be passed to the logger to store
1869 * any internal state.
1870 * @param startLoc - The address (in the reader's address space) of the first
1871 * byte in the instruction.
1872 * @param mode - The mode (real mode, IA-32e, or IA-32e in 64-bit mode) to
1873 * decode the instruction in.
1874 * @return - 0 if the instruction's memory could be read; nonzero if
1875 * not.
1876 */
Richard Smith89ee75d2014-04-20 21:07:34 +00001877int llvm::X86Disassembler::decodeInstruction(
1878 struct InternalInstruction *insn, byteReader_t reader,
1879 const void *readerArg, dlog_t logger, void *loggerArg, const void *miiArg,
1880 uint64_t startLoc, DisassemblerMode mode) {
Daniel Dunbarc745a622009-12-19 03:31:50 +00001881 memset(insn, 0, sizeof(struct InternalInstruction));
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001882
Sean Callanan04cc3072009-12-19 02:59:52 +00001883 insn->reader = reader;
1884 insn->readerArg = readerArg;
1885 insn->dlog = logger;
1886 insn->dlogArg = loggerArg;
1887 insn->startLocation = startLoc;
1888 insn->readerCursor = startLoc;
1889 insn->mode = mode;
1890 insn->numImmediatesConsumed = 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001891
Sean Callanan04cc3072009-12-19 02:59:52 +00001892 if (readPrefixes(insn) ||
1893 readOpcode(insn) ||
Benjamin Kramer478e8de2012-02-11 14:50:54 +00001894 getID(insn, miiArg) ||
Sean Callanan04cc3072009-12-19 02:59:52 +00001895 insn->instructionID == 0 ||
1896 readOperands(insn))
1897 return -1;
Craig Topperb8aec082012-08-01 07:39:18 +00001898
Patrik Hagglund31998382014-04-28 12:12:27 +00001899 insn->operands = x86OperandSets[insn->spec->operands];
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001900
Sean Callanan04cc3072009-12-19 02:59:52 +00001901 insn->length = insn->readerCursor - insn->startLocation;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001902
Benjamin Kramer4f672272010-03-18 12:18:36 +00001903 dbgprintf(insn, "Read from 0x%llx to 0x%llx: length %zu",
1904 startLoc, insn->readerCursor, insn->length);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001905
Sean Callanan04cc3072009-12-19 02:59:52 +00001906 if (insn->length > 15)
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001907 dbgprintf(insn, "Instruction exceeds 15-byte limit");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001908
Sean Callanan04cc3072009-12-19 02:59:52 +00001909 return 0;
1910}