Craig Topper | abfe07e | 2014-10-07 07:29:46 +0000 | [diff] [blame] | 1 | //===-- X86DisassemblerDecoder.cpp - Disassembler decoder -----------------===// |
Richard Smith | 89ee75d | 2014-04-20 21:07:34 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is part of the X86 Disassembler. |
| 11 | // It contains the implementation of the instruction decoder. |
| 12 | // Documentation for the disassembler can be found in X86Disassembler.h. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 15 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 16 | #include <cstdarg> /* for va_*() */ |
| 17 | #include <cstdio> /* for vsnprintf() */ |
| 18 | #include <cstdlib> /* for exit() */ |
| 19 | #include <cstring> /* for memset() */ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 20 | |
| 21 | #include "X86DisassemblerDecoder.h" |
| 22 | |
Richard Smith | 89ee75d | 2014-04-20 21:07:34 +0000 | [diff] [blame] | 23 | using namespace llvm::X86Disassembler; |
| 24 | |
Richard Smith | ac15f1c | 2014-04-20 21:52:16 +0000 | [diff] [blame] | 25 | /// 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. |
| 28 | struct 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. |
| 35 | struct 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). |
| 44 | struct ContextDecision { |
| 45 | OpcodeDecision opcodeDecisions[IC_max]; |
| 46 | }; |
| 47 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 48 | #include "X86GenDisassemblerTables.inc" |
| 49 | |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 50 | #ifndef NDEBUG |
Richard Smith | 89ee75d | 2014-04-20 21:07:34 +0000 | [diff] [blame] | 51 | #define debug(s) do { Debug(__FILE__, __LINE__, s); } while (0) |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 52 | #else |
| 53 | #define debug(s) do { } while (0) |
| 54 | #endif |
| 55 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 56 | /* |
| 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 Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 64 | static InstructionContext contextForAttrs(uint16_t attrMask) { |
Richard Smith | 89ee75d | 2014-04-20 21:07:34 +0000 | [diff] [blame] | 65 | return static_cast<InstructionContext>(CONTEXTS_SYM[attrMask]); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 66 | } |
| 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 Smith | 5d506103 | 2014-04-20 22:15:37 +0000 | [diff] [blame] | 77 | * @return - true if the ModR/M byte is required, false otherwise. |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 78 | */ |
Sean Callanan | 588785c | 2009-12-22 22:51:40 +0000 | [diff] [blame] | 79 | static int modRMRequired(OpcodeType type, |
Craig Topper | 21c3365 | 2011-10-02 16:56:09 +0000 | [diff] [blame] | 80 | InstructionContext insnContext, |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 81 | uint16_t opcode) { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 82 | const struct ContextDecision* decision = nullptr; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 83 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 84 | 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 Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 97 | 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 Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 106 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 107 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 108 | return decision->opcodeDecisions[insnContext].modRMDecisions[opcode]. |
| 109 | modrm_type != MODRM_ONEENTRY; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 110 | } |
| 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 Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 120 | * @return - The UID of the instruction, or 0 on failure. |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 121 | */ |
Sean Callanan | 588785c | 2009-12-22 22:51:40 +0000 | [diff] [blame] | 122 | static InstrUID decode(OpcodeType type, |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 123 | InstructionContext insnContext, |
| 124 | uint8_t opcode, |
| 125 | uint8_t modRM) { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 126 | const struct ModRMDecision* dec = nullptr; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 127 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 128 | switch (type) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 129 | 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 Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 141 | 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 Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 150 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 151 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 152 | switch (dec->modrm_type) { |
| 153 | default: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 154 | debug("Corrupt table! Unknown modrm_type"); |
| 155 | return 0; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 156 | case MODRM_ONEENTRY: |
Craig Topper | 487e744 | 2012-02-09 07:45:30 +0000 | [diff] [blame] | 157 | return modRMTable[dec->instructionIDs]; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 158 | case MODRM_SPLITRM: |
| 159 | if (modFromModRM(modRM) == 0x3) |
Craig Topper | 487e744 | 2012-02-09 07:45:30 +0000 | [diff] [blame] | 160 | return modRMTable[dec->instructionIDs+1]; |
| 161 | return modRMTable[dec->instructionIDs]; |
Craig Topper | a0cd970 | 2012-02-09 08:58:07 +0000 | [diff] [blame] | 162 | 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 Topper | 963305b | 2012-09-13 05:45:42 +0000 | [diff] [blame] | 166 | 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 Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 170 | case MODRM_FULL: |
Craig Topper | 487e744 | 2012-02-09 07:45:30 +0000 | [diff] [blame] | 171 | return modRMTable[dec->instructionIDs+modRM]; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 172 | } |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 173 | } |
| 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 Kramer | de0a4fb | 2010-10-23 09:10:44 +0000 | [diff] [blame] | 183 | static const struct InstructionSpecifier *specifierForUID(InstrUID uid) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 184 | 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 Callanan | 588785c | 2009-12-22 22:51:40 +0000 | [diff] [blame] | 197 | static int consumeByte(struct InternalInstruction* insn, uint8_t* byte) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 198 | int ret = insn->reader(insn->readerArg, byte, insn->readerCursor); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 199 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 200 | if (!ret) |
| 201 | ++(insn->readerCursor); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 202 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 203 | 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 Callanan | 588785c | 2009-12-22 22:51:40 +0000 | [diff] [blame] | 213 | static int lookAtByte(struct InternalInstruction* insn, uint8_t* byte) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 214 | return insn->reader(insn->readerArg, byte, insn->readerCursor); |
| 215 | } |
| 216 | |
Sean Callanan | 588785c | 2009-12-22 22:51:40 +0000 | [diff] [blame] | 217 | static void unconsumeByte(struct InternalInstruction* insn) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 218 | insn->readerCursor--; |
| 219 | } |
| 220 | |
Sean Callanan | 588785c | 2009-12-22 22:51:40 +0000 | [diff] [blame] | 221 | #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 Smith | 228e6d4 | 2012-08-24 23:29:28 +0000 | [diff] [blame] | 232 | combined = combined | ((uint64_t)byte << (offset * 8)); \ |
Sean Callanan | 588785c | 2009-12-22 22:51:40 +0000 | [diff] [blame] | 233 | } \ |
| 234 | *ptr = combined; \ |
| 235 | insn->readerCursor += sizeof(type); \ |
| 236 | return 0; \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 237 | } |
| 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 | */ |
| 249 | CONSUME_FUNC(consumeInt8, int8_t) |
| 250 | CONSUME_FUNC(consumeInt16, int16_t) |
| 251 | CONSUME_FUNC(consumeInt32, int32_t) |
| 252 | CONSUME_FUNC(consumeUInt16, uint16_t) |
| 253 | CONSUME_FUNC(consumeUInt32, uint32_t) |
| 254 | CONSUME_FUNC(consumeUInt64, uint64_t) |
| 255 | |
| 256 | /* |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 257 | * dbgprintf - Uses the logging function provided by the user to log a single |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 258 | * 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 Callanan | 588785c | 2009-12-22 22:51:40 +0000 | [diff] [blame] | 264 | static void dbgprintf(struct InternalInstruction* insn, |
| 265 | const char* format, |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 266 | ...) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 267 | char buffer[256]; |
| 268 | va_list ap; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 269 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 270 | if (!insn->dlog) |
| 271 | return; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 272 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 273 | va_start(ap, format); |
| 274 | (void)vsnprintf(buffer, sizeof(buffer), format, ap); |
| 275 | va_end(ap); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 276 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 277 | insn->dlog(insn->dlogArg, buffer); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 280 | static bool isREX(struct InternalInstruction *insn, uint8_t prefix) { |
| 281 | if (insn->mode == MODE_64BIT) |
| 282 | return prefix >= 0x40 && prefix <= 0x4f; |
| 283 | return false; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /* |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 287 | * setPrefixPresent - Marks that a particular prefix is present as mandatory |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 288 | * |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 289 | * @param insn - The instruction to be marked as having the prefix. |
| 290 | * @param prefix - The prefix that is present. |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 291 | */ |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 292 | static 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 Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 318 | } |
| 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 | */ |
| 329 | static int readPrefixes(struct InternalInstruction* insn) { |
Richard Smith | 5d506103 | 2014-04-20 22:15:37 +0000 | [diff] [blame] | 330 | bool isPrefix = true; |
Ted Kremenek | 3c4408c | 2011-01-23 17:05:06 +0000 | [diff] [blame] | 331 | uint8_t byte = 0; |
Richard Mitton | 79917a9 | 2013-08-30 21:32:42 +0000 | [diff] [blame] | 332 | uint8_t nextByte; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 333 | |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 334 | dbgprintf(insn, "readPrefixes()"); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 335 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 336 | while (isPrefix) { |
Richard Mitton | 576ee00 | 2013-08-30 21:19:48 +0000 | [diff] [blame] | 337 | /* If we fail reading prefixes, just stop here and let the opcode reader deal with it */ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 338 | if (consumeByte(insn, &byte)) |
Richard Mitton | 576ee00 | 2013-08-30 21:19:48 +0000 | [diff] [blame] | 339 | break; |
Kevin Enderby | 014e1cd | 2012-03-09 17:52:49 +0000 | [diff] [blame] | 340 | |
Benjamin Kramer | adfc73d | 2012-03-10 15:10:06 +0000 | [diff] [blame] | 341 | /* |
Dave Zarzycki | 07fabee | 2013-03-25 18:59:38 +0000 | [diff] [blame] | 342 | * 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 Kramer | adfc73d | 2012-03-10 15:10:06 +0000 | [diff] [blame] | 344 | */ |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 345 | if (insn->readerCursor - 1 == insn->startLocation && byte == 0xf0) // LOCK |
Richard Mitton | 576ee00 | 2013-08-30 21:19:48 +0000 | [diff] [blame] | 346 | break; |
| 347 | |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 348 | if ((byte == 0xf2 || byte == 0xf3) && !lookAtByte(insn, &nextByte)) { |
Kevin Enderby | 35fd792 | 2013-06-20 22:32:18 +0000 | [diff] [blame] | 349 | /* |
| 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. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 356 | if (((nextByte == 0xf0) || |
| 357 | ((nextByte & 0xfe) == 0x86 || (nextByte & 0xf8) == 0x90))) { |
Richard Smith | 5d506103 | 2014-04-20 22:15:37 +0000 | [diff] [blame] | 358 | insn->xAcquireRelease = true; |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 359 | if (!(byte == 0xf3 && nextByte == 0x90)) // PAUSE instruction support |
| 360 | break; |
| 361 | } |
Kevin Enderby | 35fd792 | 2013-06-20 22:32:18 +0000 | [diff] [blame] | 362 | /* |
| 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. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 368 | if (byte == 0xf3 && (nextByte == 0x88 || nextByte == 0x89 || |
| 369 | nextByte == 0xc6 || nextByte == 0xc7)) { |
Richard Smith | 5d506103 | 2014-04-20 22:15:37 +0000 | [diff] [blame] | 370 | insn->xAcquireRelease = true; |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 371 | 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 Zarzycki | 07fabee | 2013-03-25 18:59:38 +0000 | [diff] [blame] | 378 | return -1; |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 379 | // We should be able to read next byte after REX prefix |
| 380 | if (lookAtByte(insn, &nnextByte)) |
Dave Zarzycki | 07fabee | 2013-03-25 18:59:38 +0000 | [diff] [blame] | 381 | return -1; |
| 382 | unconsumeByte(insn); |
| 383 | } |
Dave Zarzycki | 07fabee | 2013-03-25 18:59:38 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 386 | switch (byte) { |
| 387 | case 0xf0: /* LOCK */ |
| 388 | case 0xf2: /* REPNE/REPNZ */ |
| 389 | case 0xf3: /* REP or REPE/REPZ */ |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 390 | setPrefixPresent(insn, byte); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 391 | 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 Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 418 | debug("Unhandled override"); |
| 419 | return -1; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 420 | } |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 421 | setPrefixPresent(insn, byte); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 422 | break; |
| 423 | case 0x66: /* Operand-size override */ |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 424 | insn->hasOpSize = true; |
| 425 | setPrefixPresent(insn, byte); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 426 | break; |
| 427 | case 0x67: /* Address-size override */ |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 428 | insn->hasAdSize = true; |
| 429 | setPrefixPresent(insn, byte); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 430 | break; |
| 431 | default: /* Not a prefix byte */ |
Richard Smith | 5d506103 | 2014-04-20 22:15:37 +0000 | [diff] [blame] | 432 | isPrefix = false; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 433 | break; |
| 434 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 435 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 436 | if (isPrefix) |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 437 | dbgprintf(insn, "Found prefix 0x%hhx", byte); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 438 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 439 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 440 | insn->vectorExtensionType = TYPE_NO_VEX_XOP; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 441 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 442 | 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 Topper | 273515e | 2014-10-07 07:29:48 +0000 | [diff] [blame] | 458 | } else { |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 459 | unconsumeByte(insn); /* unconsume byte1 */ |
| 460 | unconsumeByte(insn); /* unconsume byte */ |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 461 | } |
| 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 Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 484 | 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 Topper | 273515e | 2014-10-07 07:29:48 +0000 | [diff] [blame] | 488 | } else if (byte == 0xc4) { |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 489 | uint8_t byte1; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 490 | |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 491 | if (lookAtByte(insn, &byte1)) { |
| 492 | dbgprintf(insn, "Couldn't read second byte of VEX"); |
| 493 | return -1; |
| 494 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 495 | |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 496 | if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 497 | insn->vectorExtensionType = TYPE_VEX_3B; |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 498 | else |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 499 | unconsumeByte(insn); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 500 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 501 | if (insn->vectorExtensionType == TYPE_VEX_3B) { |
| 502 | insn->vectorExtensionPrefix[0] = byte; |
| 503 | consumeByte(insn, &insn->vectorExtensionPrefix[1]); |
| 504 | consumeByte(insn, &insn->vectorExtensionPrefix[2]); |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 505 | |
| 506 | /* We simulate the REX prefix for simplicity's sake */ |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 507 | |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 508 | if (insn->mode == MODE_64BIT) |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 509 | insn->rexPrefix = 0x40 |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 510 | | (wFromVEX3of3(insn->vectorExtensionPrefix[2]) << 3) |
| 511 | | (rFromVEX2of3(insn->vectorExtensionPrefix[1]) << 2) |
| 512 | | (xFromVEX2of3(insn->vectorExtensionPrefix[1]) << 1) |
| 513 | | (bFromVEX2of3(insn->vectorExtensionPrefix[1]) << 0); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 514 | |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 515 | dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx 0x%hhx", |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 516 | insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1], |
| 517 | insn->vectorExtensionPrefix[2]); |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 518 | } |
Craig Topper | 273515e | 2014-10-07 07:29:48 +0000 | [diff] [blame] | 519 | } else if (byte == 0xc5) { |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 520 | uint8_t byte1; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 521 | |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 522 | if (lookAtByte(insn, &byte1)) { |
| 523 | dbgprintf(insn, "Couldn't read second byte of VEX"); |
| 524 | return -1; |
| 525 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 526 | |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 527 | if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 528 | insn->vectorExtensionType = TYPE_VEX_2B; |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 529 | else |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 530 | unconsumeByte(insn); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 531 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 532 | if (insn->vectorExtensionType == TYPE_VEX_2B) { |
| 533 | insn->vectorExtensionPrefix[0] = byte; |
| 534 | consumeByte(insn, &insn->vectorExtensionPrefix[1]); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 535 | |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 536 | if (insn->mode == MODE_64BIT) |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 537 | insn->rexPrefix = 0x40 |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 538 | | (rFromVEX2of2(insn->vectorExtensionPrefix[1]) << 2); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 539 | |
Craig Topper | 273515e | 2014-10-07 07:29:48 +0000 | [diff] [blame] | 540 | switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) { |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 541 | default: |
| 542 | break; |
| 543 | case VEX_PREFIX_66: |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 544 | insn->hasOpSize = true; |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 545 | break; |
| 546 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 547 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 548 | dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx", |
| 549 | insn->vectorExtensionPrefix[0], |
| 550 | insn->vectorExtensionPrefix[1]); |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 551 | } |
Craig Topper | 273515e | 2014-10-07 07:29:48 +0000 | [diff] [blame] | 552 | } else if (byte == 0x8f) { |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 553 | 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. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 560 | if ((byte1 & 0x38) != 0x0) /* 0 in these 3 bits is a POP instruction. */ |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 561 | insn->vectorExtensionType = TYPE_XOP; |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 562 | else |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 563 | unconsumeByte(insn); |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 564 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 565 | if (insn->vectorExtensionType == TYPE_XOP) { |
| 566 | insn->vectorExtensionPrefix[0] = byte; |
| 567 | consumeByte(insn, &insn->vectorExtensionPrefix[1]); |
| 568 | consumeByte(insn, &insn->vectorExtensionPrefix[2]); |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 569 | |
| 570 | /* We simulate the REX prefix for simplicity's sake */ |
| 571 | |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 572 | if (insn->mode == MODE_64BIT) |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 573 | insn->rexPrefix = 0x40 |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 574 | | (wFromXOP3of3(insn->vectorExtensionPrefix[2]) << 3) |
| 575 | | (rFromXOP2of3(insn->vectorExtensionPrefix[1]) << 2) |
| 576 | | (xFromXOP2of3(insn->vectorExtensionPrefix[1]) << 1) |
| 577 | | (bFromXOP2of3(insn->vectorExtensionPrefix[1]) << 0); |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 578 | |
Craig Topper | 273515e | 2014-10-07 07:29:48 +0000 | [diff] [blame] | 579 | switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) { |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 580 | default: |
| 581 | break; |
| 582 | case VEX_PREFIX_66: |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 583 | insn->hasOpSize = true; |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 584 | break; |
| 585 | } |
| 586 | |
| 587 | dbgprintf(insn, "Found XOP prefix 0x%hhx 0x%hhx 0x%hhx", |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 588 | insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1], |
| 589 | insn->vectorExtensionPrefix[2]); |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 590 | } |
Rafael Auler | de9ad4b | 2018-02-15 21:20:31 +0000 | [diff] [blame^] | 591 | } else if (byte == 0x0f) { |
| 592 | uint8_t byte1; |
| 593 | |
| 594 | // Check for AMD 3DNow without a REX prefix |
| 595 | if (consumeByte(insn, &byte1)) { |
| 596 | unconsumeByte(insn); |
| 597 | } else { |
| 598 | if (byte1 != 0x0f) { |
| 599 | unconsumeByte(insn); |
| 600 | unconsumeByte(insn); |
| 601 | } else { |
| 602 | dbgprintf(insn, "Found AMD 3DNow prefix 0f0f"); |
| 603 | insn->vectorExtensionType = TYPE_3DNOW; |
| 604 | } |
| 605 | } |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 606 | } else if (isREX(insn, byte)) { |
| 607 | if (lookAtByte(insn, &nextByte)) |
| 608 | return -1; |
| 609 | insn->rexPrefix = byte; |
| 610 | dbgprintf(insn, "Found REX prefix 0x%hhx", byte); |
Rafael Auler | de9ad4b | 2018-02-15 21:20:31 +0000 | [diff] [blame^] | 611 | |
| 612 | // Check for AMD 3DNow with a REX prefix |
| 613 | if (nextByte == 0x0f) { |
| 614 | consumeByte(insn, &nextByte); |
| 615 | uint8_t byte1; |
| 616 | |
| 617 | if (consumeByte(insn, &byte1)) { |
| 618 | unconsumeByte(insn); |
| 619 | } else { |
| 620 | if (byte1 != 0x0f) { |
| 621 | unconsumeByte(insn); |
| 622 | unconsumeByte(insn); |
| 623 | } else { |
| 624 | dbgprintf(insn, "Found AMD 3DNow prefix 0f0f"); |
| 625 | insn->vectorExtensionType = TYPE_3DNOW; |
| 626 | } |
| 627 | } |
| 628 | } |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 629 | } else |
| 630 | unconsumeByte(insn); |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 631 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 632 | if (insn->mode == MODE_16BIT) { |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 633 | insn->registerSize = (insn->hasOpSize ? 4 : 2); |
| 634 | insn->addressSize = (insn->hasAdSize ? 4 : 2); |
| 635 | insn->displacementSize = (insn->hasAdSize ? 4 : 2); |
| 636 | insn->immediateSize = (insn->hasOpSize ? 4 : 2); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 637 | } else if (insn->mode == MODE_32BIT) { |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 638 | insn->registerSize = (insn->hasOpSize ? 2 : 4); |
| 639 | insn->addressSize = (insn->hasAdSize ? 2 : 4); |
| 640 | insn->displacementSize = (insn->hasAdSize ? 2 : 4); |
| 641 | insn->immediateSize = (insn->hasOpSize ? 2 : 4); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 642 | } else if (insn->mode == MODE_64BIT) { |
| 643 | if (insn->rexPrefix && wFromREX(insn->rexPrefix)) { |
| 644 | insn->registerSize = 8; |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 645 | insn->addressSize = (insn->hasAdSize ? 4 : 8); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 646 | insn->displacementSize = 4; |
| 647 | insn->immediateSize = 4; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 648 | } else { |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 649 | insn->registerSize = (insn->hasOpSize ? 2 : 4); |
| 650 | insn->addressSize = (insn->hasAdSize ? 4 : 8); |
| 651 | insn->displacementSize = (insn->hasOpSize ? 2 : 4); |
| 652 | insn->immediateSize = (insn->hasOpSize ? 2 : 4); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 653 | } |
| 654 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 655 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 656 | return 0; |
| 657 | } |
| 658 | |
Rafael Auler | de9ad4b | 2018-02-15 21:20:31 +0000 | [diff] [blame^] | 659 | static int readModRM(struct InternalInstruction* insn); |
| 660 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 661 | /* |
| 662 | * readOpcode - Reads the opcode (excepting the ModR/M byte in the case of |
| 663 | * extended or escape opcodes). |
| 664 | * |
| 665 | * @param insn - The instruction whose opcode is to be read. |
| 666 | * @return - 0 if the opcode could be read successfully; nonzero otherwise. |
| 667 | */ |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 668 | static int readOpcode(struct InternalInstruction* insn) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 669 | /* Determine the length of the primary opcode */ |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 670 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 671 | uint8_t current; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 672 | |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 673 | dbgprintf(insn, "readOpcode()"); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 674 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 675 | insn->opcodeType = ONEBYTE; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 676 | |
Craig Topper | 273515e | 2014-10-07 07:29:48 +0000 | [diff] [blame] | 677 | if (insn->vectorExtensionType == TYPE_EVEX) { |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 678 | switch (mmFromEVEX2of4(insn->vectorExtensionPrefix[1])) { |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 679 | default: |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 680 | dbgprintf(insn, "Unhandled mm field for instruction (0x%hhx)", |
| 681 | mmFromEVEX2of4(insn->vectorExtensionPrefix[1])); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 682 | return -1; |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 683 | case VEX_LOB_0F: |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 684 | insn->opcodeType = TWOBYTE; |
| 685 | return consumeByte(insn, &insn->opcode); |
| 686 | case VEX_LOB_0F38: |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 687 | insn->opcodeType = THREEBYTE_38; |
| 688 | return consumeByte(insn, &insn->opcode); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 689 | case VEX_LOB_0F3A: |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 690 | insn->opcodeType = THREEBYTE_3A; |
| 691 | return consumeByte(insn, &insn->opcode); |
| 692 | } |
Craig Topper | 273515e | 2014-10-07 07:29:48 +0000 | [diff] [blame] | 693 | } else if (insn->vectorExtensionType == TYPE_VEX_3B) { |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 694 | switch (mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])) { |
| 695 | default: |
| 696 | dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)", |
| 697 | mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])); |
| 698 | return -1; |
| 699 | case VEX_LOB_0F: |
| 700 | insn->opcodeType = TWOBYTE; |
| 701 | return consumeByte(insn, &insn->opcode); |
| 702 | case VEX_LOB_0F38: |
| 703 | insn->opcodeType = THREEBYTE_38; |
| 704 | return consumeByte(insn, &insn->opcode); |
| 705 | case VEX_LOB_0F3A: |
| 706 | insn->opcodeType = THREEBYTE_3A; |
| 707 | return consumeByte(insn, &insn->opcode); |
| 708 | } |
Craig Topper | 273515e | 2014-10-07 07:29:48 +0000 | [diff] [blame] | 709 | } else if (insn->vectorExtensionType == TYPE_VEX_2B) { |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 710 | insn->opcodeType = TWOBYTE; |
| 711 | return consumeByte(insn, &insn->opcode); |
Craig Topper | 273515e | 2014-10-07 07:29:48 +0000 | [diff] [blame] | 712 | } else if (insn->vectorExtensionType == TYPE_XOP) { |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 713 | switch (mmmmmFromXOP2of3(insn->vectorExtensionPrefix[1])) { |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 714 | default: |
| 715 | dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)", |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 716 | mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])); |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 717 | return -1; |
| 718 | case XOP_MAP_SELECT_8: |
| 719 | insn->opcodeType = XOP8_MAP; |
| 720 | return consumeByte(insn, &insn->opcode); |
| 721 | case XOP_MAP_SELECT_9: |
| 722 | insn->opcodeType = XOP9_MAP; |
| 723 | return consumeByte(insn, &insn->opcode); |
| 724 | case XOP_MAP_SELECT_A: |
| 725 | insn->opcodeType = XOPA_MAP; |
| 726 | return consumeByte(insn, &insn->opcode); |
| 727 | } |
Rafael Auler | de9ad4b | 2018-02-15 21:20:31 +0000 | [diff] [blame^] | 728 | } else if (insn->vectorExtensionType == TYPE_3DNOW) { |
| 729 | // Consume operands before the opcode to comply with the 3DNow encoding |
| 730 | if (readModRM(insn)) |
| 731 | return -1; |
| 732 | insn->opcodeType = TWOBYTE; |
| 733 | return consumeByte(insn, &insn->opcode); |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 734 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 735 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 736 | if (consumeByte(insn, ¤t)) |
| 737 | return -1; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 738 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 739 | if (current == 0x0f) { |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 740 | dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 741 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 742 | if (consumeByte(insn, ¤t)) |
| 743 | return -1; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 744 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 745 | if (current == 0x38) { |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 746 | dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 747 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 748 | if (consumeByte(insn, ¤t)) |
| 749 | return -1; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 750 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 751 | insn->opcodeType = THREEBYTE_38; |
| 752 | } else if (current == 0x3a) { |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 753 | dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 754 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 755 | if (consumeByte(insn, ¤t)) |
| 756 | return -1; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 757 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 758 | insn->opcodeType = THREEBYTE_3A; |
| 759 | } else { |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 760 | dbgprintf(insn, "Didn't find a three-byte escape prefix"); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 761 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 762 | insn->opcodeType = TWOBYTE; |
| 763 | } |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 764 | } else if (insn->mandatoryPrefix) |
| 765 | // The opcode with mandatory prefix must start with opcode escape. |
| 766 | // If not it's legacy repeat prefix |
| 767 | insn->mandatoryPrefix = 0; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 768 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 769 | /* |
| 770 | * At this point we have consumed the full opcode. |
| 771 | * Anything we consume from here on must be unconsumed. |
| 772 | */ |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 773 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 774 | insn->opcode = current; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 775 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 776 | return 0; |
| 777 | } |
| 778 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 779 | /* |
| 780 | * getIDWithAttrMask - Determines the ID of an instruction, consuming |
| 781 | * the ModR/M byte as appropriate for extended and escape opcodes, |
| 782 | * and using a supplied attribute mask. |
| 783 | * |
| 784 | * @param instructionID - A pointer whose target is filled in with the ID of the |
| 785 | * instruction. |
| 786 | * @param insn - The instruction whose ID is to be determined. |
| 787 | * @param attrMask - The attribute mask to search. |
| 788 | * @return - 0 if the ModR/M could be read when needed or was not |
| 789 | * needed; nonzero otherwise. |
| 790 | */ |
| 791 | static int getIDWithAttrMask(uint16_t* instructionID, |
| 792 | struct InternalInstruction* insn, |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 793 | uint16_t attrMask) { |
Richard Smith | 5d506103 | 2014-04-20 22:15:37 +0000 | [diff] [blame] | 794 | bool hasModRMExtension; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 795 | |
Richard Smith | 89ee75d | 2014-04-20 21:07:34 +0000 | [diff] [blame] | 796 | InstructionContext instructionClass = contextForAttrs(attrMask); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 797 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 798 | hasModRMExtension = modRMRequired(insn->opcodeType, |
| 799 | instructionClass, |
| 800 | insn->opcode); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 801 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 802 | if (hasModRMExtension) { |
Rafael Espindola | 9f9a106 | 2011-01-06 16:48:42 +0000 | [diff] [blame] | 803 | if (readModRM(insn)) |
| 804 | return -1; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 805 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 806 | *instructionID = decode(insn->opcodeType, |
| 807 | instructionClass, |
| 808 | insn->opcode, |
| 809 | insn->modRM); |
| 810 | } else { |
| 811 | *instructionID = decode(insn->opcodeType, |
| 812 | instructionClass, |
| 813 | insn->opcode, |
| 814 | 0); |
| 815 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 816 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | /* |
| 821 | * is16BitEquivalent - Determines whether two instruction names refer to |
| 822 | * equivalent instructions but one is 16-bit whereas the other is not. |
| 823 | * |
| 824 | * @param orig - The instruction that is not 16-bit |
| 825 | * @param equiv - The instruction that is 16-bit |
| 826 | */ |
Mehdi Amini | 36d33fc | 2016-10-01 06:46:33 +0000 | [diff] [blame] | 827 | static bool is16BitEquivalent(const char *orig, const char *equiv) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 828 | off_t i; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 829 | |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 830 | for (i = 0;; i++) { |
| 831 | if (orig[i] == '\0' && equiv[i] == '\0') |
Richard Smith | 5d506103 | 2014-04-20 22:15:37 +0000 | [diff] [blame] | 832 | return true; |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 833 | if (orig[i] == '\0' || equiv[i] == '\0') |
Richard Smith | 5d506103 | 2014-04-20 22:15:37 +0000 | [diff] [blame] | 834 | return false; |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 835 | if (orig[i] != equiv[i]) { |
| 836 | if ((orig[i] == 'Q' || orig[i] == 'L') && equiv[i] == 'W') |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 837 | continue; |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 838 | if ((orig[i] == '6' || orig[i] == '3') && equiv[i] == '1') |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 839 | continue; |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 840 | if ((orig[i] == '4' || orig[i] == '2') && equiv[i] == '6') |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 841 | continue; |
Richard Smith | 5d506103 | 2014-04-20 22:15:37 +0000 | [diff] [blame] | 842 | return false; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | /* |
Craig Topper | 0676b90 | 2014-10-07 07:29:50 +0000 | [diff] [blame] | 848 | * is64Bit - Determines whether this instruction is a 64-bit instruction. |
| 849 | * |
| 850 | * @param name - The instruction that is not 16-bit |
| 851 | */ |
Mehdi Amini | 36d33fc | 2016-10-01 06:46:33 +0000 | [diff] [blame] | 852 | static bool is64Bit(const char *name) { |
Craig Topper | 0676b90 | 2014-10-07 07:29:50 +0000 | [diff] [blame] | 853 | off_t i; |
| 854 | |
| 855 | for (i = 0;; ++i) { |
| 856 | if (name[i] == '\0') |
| 857 | return false; |
| 858 | if (name[i] == '6' && name[i+1] == '4') |
| 859 | return true; |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | /* |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 864 | * getID - Determines the ID of an instruction, consuming the ModR/M byte as |
| 865 | * appropriate for extended and escape opcodes. Determines the attributes and |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 866 | * context for the instruction before doing so. |
| 867 | * |
| 868 | * @param insn - The instruction whose ID is to be determined. |
| 869 | * @return - 0 if the ModR/M could be read when needed or was not needed; |
| 870 | * nonzero otherwise. |
| 871 | */ |
Roman Divacky | 6792380 | 2012-09-05 21:17:34 +0000 | [diff] [blame] | 872 | static int getID(struct InternalInstruction* insn, const void *miiArg) { |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 873 | uint16_t attrMask; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 874 | uint16_t instructionID; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 875 | |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 876 | dbgprintf(insn, "getID()"); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 877 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 878 | attrMask = ATTR_NONE; |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 879 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 880 | if (insn->mode == MODE_64BIT) |
| 881 | attrMask |= ATTR_64BIT; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 882 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 883 | if (insn->vectorExtensionType != TYPE_NO_VEX_XOP) { |
| 884 | attrMask |= (insn->vectorExtensionType == TYPE_EVEX) ? ATTR_EVEX : ATTR_VEX; |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 885 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 886 | if (insn->vectorExtensionType == TYPE_EVEX) { |
| 887 | switch (ppFromEVEX3of4(insn->vectorExtensionPrefix[2])) { |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 888 | case VEX_PREFIX_66: |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 889 | attrMask |= ATTR_OPSIZE; |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 890 | break; |
| 891 | case VEX_PREFIX_F3: |
| 892 | attrMask |= ATTR_XS; |
| 893 | break; |
| 894 | case VEX_PREFIX_F2: |
| 895 | attrMask |= ATTR_XD; |
| 896 | break; |
| 897 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 898 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 899 | if (zFromEVEX4of4(insn->vectorExtensionPrefix[3])) |
| 900 | attrMask |= ATTR_EVEXKZ; |
| 901 | if (bFromEVEX4of4(insn->vectorExtensionPrefix[3])) |
| 902 | attrMask |= ATTR_EVEXB; |
| 903 | if (aaaFromEVEX4of4(insn->vectorExtensionPrefix[3])) |
| 904 | attrMask |= ATTR_EVEXK; |
| 905 | if (lFromEVEX4of4(insn->vectorExtensionPrefix[3])) |
| 906 | attrMask |= ATTR_EVEXL; |
| 907 | if (l2FromEVEX4of4(insn->vectorExtensionPrefix[3])) |
| 908 | attrMask |= ATTR_EVEXL2; |
Craig Topper | 273515e | 2014-10-07 07:29:48 +0000 | [diff] [blame] | 909 | } else if (insn->vectorExtensionType == TYPE_VEX_3B) { |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 910 | switch (ppFromVEX3of3(insn->vectorExtensionPrefix[2])) { |
| 911 | case VEX_PREFIX_66: |
| 912 | attrMask |= ATTR_OPSIZE; |
| 913 | break; |
| 914 | case VEX_PREFIX_F3: |
| 915 | attrMask |= ATTR_XS; |
| 916 | break; |
| 917 | case VEX_PREFIX_F2: |
| 918 | attrMask |= ATTR_XD; |
| 919 | break; |
| 920 | } |
| 921 | |
| 922 | if (lFromVEX3of3(insn->vectorExtensionPrefix[2])) |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 923 | attrMask |= ATTR_VEXL; |
Craig Topper | 273515e | 2014-10-07 07:29:48 +0000 | [diff] [blame] | 924 | } else if (insn->vectorExtensionType == TYPE_VEX_2B) { |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 925 | switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) { |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 926 | case VEX_PREFIX_66: |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 927 | attrMask |= ATTR_OPSIZE; |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 928 | break; |
| 929 | case VEX_PREFIX_F3: |
| 930 | attrMask |= ATTR_XS; |
| 931 | break; |
| 932 | case VEX_PREFIX_F2: |
| 933 | attrMask |= ATTR_XD; |
| 934 | break; |
| 935 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 936 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 937 | if (lFromVEX2of2(insn->vectorExtensionPrefix[1])) |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 938 | attrMask |= ATTR_VEXL; |
Craig Topper | 273515e | 2014-10-07 07:29:48 +0000 | [diff] [blame] | 939 | } else if (insn->vectorExtensionType == TYPE_XOP) { |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 940 | switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) { |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 941 | case VEX_PREFIX_66: |
| 942 | attrMask |= ATTR_OPSIZE; |
| 943 | break; |
| 944 | case VEX_PREFIX_F3: |
| 945 | attrMask |= ATTR_XS; |
| 946 | break; |
| 947 | case VEX_PREFIX_F2: |
| 948 | attrMask |= ATTR_XD; |
| 949 | break; |
| 950 | } |
| 951 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 952 | if (lFromXOP3of3(insn->vectorExtensionPrefix[2])) |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 953 | attrMask |= ATTR_VEXL; |
Rafael Auler | de9ad4b | 2018-02-15 21:20:31 +0000 | [diff] [blame^] | 954 | } else if (insn->vectorExtensionType == TYPE_3DNOW) { |
| 955 | attrMask |= ATTR_3DNOW; |
Craig Topper | 273515e | 2014-10-07 07:29:48 +0000 | [diff] [blame] | 956 | } else { |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 957 | return -1; |
| 958 | } |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 959 | } else if (!insn->mandatoryPrefix) { |
| 960 | // If we don't have mandatory prefix we should use legacy prefixes here |
| 961 | if (insn->hasOpSize && (insn->mode != MODE_16BIT)) |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 962 | attrMask |= ATTR_OPSIZE; |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 963 | if (insn->hasAdSize) |
Craig Topper | 6491c80 | 2012-02-27 01:54:29 +0000 | [diff] [blame] | 964 | attrMask |= ATTR_ADSIZE; |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 965 | if (insn->opcodeType == ONEBYTE) { |
| 966 | if (insn->repeatPrefix == 0xf3 && (insn->opcode == 0x90)) |
| 967 | // Special support for PAUSE |
| 968 | attrMask |= ATTR_XS; |
| 969 | } else { |
| 970 | if (insn->repeatPrefix == 0xf2) |
| 971 | attrMask |= ATTR_XD; |
| 972 | else if (insn->repeatPrefix == 0xf3) |
| 973 | attrMask |= ATTR_XS; |
| 974 | } |
| 975 | } else { |
| 976 | switch (insn->mandatoryPrefix) { |
| 977 | case 0xf2: |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 978 | attrMask |= ATTR_XD; |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 979 | break; |
| 980 | case 0xf3: |
| 981 | attrMask |= ATTR_XS; |
| 982 | break; |
| 983 | case 0x66: |
| 984 | if (insn->mode != MODE_16BIT) |
| 985 | attrMask |= ATTR_OPSIZE; |
| 986 | break; |
| 987 | case 0x67: |
| 988 | attrMask |= ATTR_ADSIZE; |
| 989 | break; |
| 990 | } |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Andrew V. Tischenko | f94da59 | 2017-10-30 12:02:06 +0000 | [diff] [blame] | 993 | if (insn->rexPrefix & 0x08) { |
Craig Topper | f18c896 | 2011-10-04 06:30:42 +0000 | [diff] [blame] | 994 | attrMask |= ATTR_REXW; |
Andrew V. Tischenko | f94da59 | 2017-10-30 12:02:06 +0000 | [diff] [blame] | 995 | attrMask &= ~ATTR_ADSIZE; |
| 996 | } |
Craig Topper | f01f1b5 | 2011-11-06 23:04:08 +0000 | [diff] [blame] | 997 | |
David Woodhouse | 9c74fdb | 2014-01-20 12:02:48 +0000 | [diff] [blame] | 998 | /* |
| 999 | * JCXZ/JECXZ need special handling for 16-bit mode because the meaning |
| 1000 | * of the AdSize prefix is inverted w.r.t. 32-bit mode. |
| 1001 | */ |
Craig Topper | 6e51877 | 2014-12-31 07:07:11 +0000 | [diff] [blame] | 1002 | if (insn->mode == MODE_16BIT && insn->opcodeType == ONEBYTE && |
| 1003 | insn->opcode == 0xE3) |
| 1004 | attrMask ^= ATTR_ADSIZE; |
David Woodhouse | 9c74fdb | 2014-01-20 12:02:48 +0000 | [diff] [blame] | 1005 | |
Vedant Kumar | bf891b1 | 2015-08-26 16:20:29 +0000 | [diff] [blame] | 1006 | /* |
| 1007 | * In 64-bit mode all f64 superscripted opcodes ignore opcode size prefix |
| 1008 | * CALL/JMP/JCC instructions need to ignore 0x66 and consume 4 bytes |
| 1009 | */ |
| 1010 | |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 1011 | if ((insn->mode == MODE_64BIT) && insn->hasOpSize) { |
Vedant Kumar | bf891b1 | 2015-08-26 16:20:29 +0000 | [diff] [blame] | 1012 | switch (insn->opcode) { |
| 1013 | case 0xE8: |
| 1014 | case 0xE9: |
Vedant Kumar | 44fccb7 | 2015-08-28 21:59:00 +0000 | [diff] [blame] | 1015 | // Take care of psubsb and other mmx instructions. |
| 1016 | if (insn->opcodeType == ONEBYTE) { |
Vedant Kumar | bf891b1 | 2015-08-26 16:20:29 +0000 | [diff] [blame] | 1017 | attrMask ^= ATTR_OPSIZE; |
| 1018 | insn->immediateSize = 4; |
| 1019 | insn->displacementSize = 4; |
| 1020 | } |
| 1021 | break; |
| 1022 | case 0x82: |
| 1023 | case 0x83: |
| 1024 | case 0x84: |
| 1025 | case 0x85: |
| 1026 | case 0x86: |
| 1027 | case 0x87: |
| 1028 | case 0x88: |
| 1029 | case 0x89: |
| 1030 | case 0x8A: |
| 1031 | case 0x8B: |
| 1032 | case 0x8C: |
| 1033 | case 0x8D: |
| 1034 | case 0x8E: |
| 1035 | case 0x8F: |
Vedant Kumar | 44fccb7 | 2015-08-28 21:59:00 +0000 | [diff] [blame] | 1036 | // Take care of lea and three byte ops. |
| 1037 | if (insn->opcodeType == TWOBYTE) { |
Vedant Kumar | bf891b1 | 2015-08-26 16:20:29 +0000 | [diff] [blame] | 1038 | attrMask ^= ATTR_OPSIZE; |
| 1039 | insn->immediateSize = 4; |
Vedant Kumar | 44fccb7 | 2015-08-28 21:59:00 +0000 | [diff] [blame] | 1040 | insn->displacementSize = 4; |
Vedant Kumar | bf891b1 | 2015-08-26 16:20:29 +0000 | [diff] [blame] | 1041 | } |
| 1042 | break; |
| 1043 | } |
| 1044 | } |
| 1045 | |
Craig Topper | 6e51877 | 2014-12-31 07:07:11 +0000 | [diff] [blame] | 1046 | if (getIDWithAttrMask(&instructionID, insn, attrMask)) |
| 1047 | return -1; |
David Woodhouse | 9c74fdb | 2014-01-20 12:02:48 +0000 | [diff] [blame] | 1048 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1049 | /* The following clauses compensate for limitations of the tables. */ |
Craig Topper | f01f1b5 | 2011-11-06 23:04:08 +0000 | [diff] [blame] | 1050 | |
Craig Topper | 0676b90 | 2014-10-07 07:29:50 +0000 | [diff] [blame] | 1051 | if (insn->mode != MODE_64BIT && |
| 1052 | insn->vectorExtensionType != TYPE_NO_VEX_XOP) { |
| 1053 | /* |
| 1054 | * The tables can't distinquish between cases where the W-bit is used to |
| 1055 | * select register size and cases where its a required part of the opcode. |
| 1056 | */ |
| 1057 | if ((insn->vectorExtensionType == TYPE_EVEX && |
| 1058 | wFromEVEX3of4(insn->vectorExtensionPrefix[2])) || |
| 1059 | (insn->vectorExtensionType == TYPE_VEX_3B && |
| 1060 | wFromVEX3of3(insn->vectorExtensionPrefix[2])) || |
| 1061 | (insn->vectorExtensionType == TYPE_XOP && |
| 1062 | wFromXOP3of3(insn->vectorExtensionPrefix[2]))) { |
| 1063 | |
| 1064 | uint16_t instructionIDWithREXW; |
| 1065 | if (getIDWithAttrMask(&instructionIDWithREXW, |
| 1066 | insn, attrMask | ATTR_REXW)) { |
| 1067 | insn->instructionID = instructionID; |
| 1068 | insn->spec = specifierForUID(instructionID); |
| 1069 | return 0; |
| 1070 | } |
| 1071 | |
Mehdi Amini | 36d33fc | 2016-10-01 06:46:33 +0000 | [diff] [blame] | 1072 | auto SpecName = GetInstrName(instructionIDWithREXW, miiArg); |
Craig Topper | 0676b90 | 2014-10-07 07:29:50 +0000 | [diff] [blame] | 1073 | // If not a 64-bit instruction. Switch the opcode. |
Mehdi Amini | 36d33fc | 2016-10-01 06:46:33 +0000 | [diff] [blame] | 1074 | if (!is64Bit(SpecName.data())) { |
Craig Topper | 0676b90 | 2014-10-07 07:29:50 +0000 | [diff] [blame] | 1075 | insn->instructionID = instructionIDWithREXW; |
| 1076 | insn->spec = specifierForUID(instructionIDWithREXW); |
| 1077 | return 0; |
| 1078 | } |
| 1079 | } |
| 1080 | } |
| 1081 | |
Craig Topper | 99bcab7 | 2014-12-31 07:07:31 +0000 | [diff] [blame] | 1082 | /* |
| 1083 | * Absolute moves need special handling. |
| 1084 | * -For 16-bit mode because the meaning of the AdSize and OpSize prefixes are |
| 1085 | * inverted w.r.t. |
| 1086 | * -For 32-bit mode we need to ensure the ADSIZE prefix is observed in |
| 1087 | * any position. |
| 1088 | */ |
| 1089 | if (insn->opcodeType == ONEBYTE && ((insn->opcode & 0xFC) == 0xA0)) { |
| 1090 | /* Make sure we observed the prefixes in any position. */ |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 1091 | if (insn->hasAdSize) |
Craig Topper | 99bcab7 | 2014-12-31 07:07:31 +0000 | [diff] [blame] | 1092 | attrMask |= ATTR_ADSIZE; |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 1093 | if (insn->hasOpSize) |
Craig Topper | 99bcab7 | 2014-12-31 07:07:31 +0000 | [diff] [blame] | 1094 | attrMask |= ATTR_OPSIZE; |
| 1095 | |
| 1096 | /* In 16-bit, invert the attributes. */ |
| 1097 | if (insn->mode == MODE_16BIT) |
| 1098 | attrMask ^= ATTR_ADSIZE | ATTR_OPSIZE; |
| 1099 | |
| 1100 | if (getIDWithAttrMask(&instructionID, insn, attrMask)) |
| 1101 | return -1; |
| 1102 | |
| 1103 | insn->instructionID = instructionID; |
| 1104 | insn->spec = specifierForUID(instructionID); |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 1108 | if ((insn->mode == MODE_16BIT || insn->hasOpSize) && |
David Woodhouse | 5cf4c67 | 2014-01-20 12:02:35 +0000 | [diff] [blame] | 1109 | !(attrMask & ATTR_OPSIZE)) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1110 | /* |
| 1111 | * The instruction tables make no distinction between instructions that |
| 1112 | * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a |
| 1113 | * particular spot (i.e., many MMX operations). In general we're |
| 1114 | * conservative, but in the specific case where OpSize is present but not |
| 1115 | * in the right place we check if there's a 16-bit operation. |
| 1116 | */ |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1117 | |
Benjamin Kramer | de0a4fb | 2010-10-23 09:10:44 +0000 | [diff] [blame] | 1118 | const struct InstructionSpecifier *spec; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1119 | uint16_t instructionIDWithOpsize; |
Mehdi Amini | 36d33fc | 2016-10-01 06:46:33 +0000 | [diff] [blame] | 1120 | llvm::StringRef specName, specWithOpSizeName; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1121 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1122 | spec = specifierForUID(instructionID); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1123 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1124 | if (getIDWithAttrMask(&instructionIDWithOpsize, |
| 1125 | insn, |
| 1126 | attrMask | ATTR_OPSIZE)) { |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1127 | /* |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1128 | * ModRM required with OpSize but not present; give up and return version |
| 1129 | * without OpSize set |
| 1130 | */ |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1131 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1132 | insn->instructionID = instructionID; |
| 1133 | insn->spec = spec; |
| 1134 | return 0; |
| 1135 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1136 | |
Richard Smith | 89ee75d | 2014-04-20 21:07:34 +0000 | [diff] [blame] | 1137 | specName = GetInstrName(instructionID, miiArg); |
| 1138 | specWithOpSizeName = GetInstrName(instructionIDWithOpsize, miiArg); |
Benjamin Kramer | 478e8de | 2012-02-11 14:50:54 +0000 | [diff] [blame] | 1139 | |
Mehdi Amini | 36d33fc | 2016-10-01 06:46:33 +0000 | [diff] [blame] | 1140 | if (is16BitEquivalent(specName.data(), specWithOpSizeName.data()) && |
Andrew V. Tischenko | bfc9061 | 2017-10-16 11:14:29 +0000 | [diff] [blame] | 1141 | (insn->mode == MODE_16BIT) ^ insn->hasOpSize) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1142 | insn->instructionID = instructionIDWithOpsize; |
Benjamin Kramer | 915e3d9 | 2012-02-11 16:01:02 +0000 | [diff] [blame] | 1143 | insn->spec = specifierForUID(instructionIDWithOpsize); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1144 | } else { |
| 1145 | insn->instructionID = instructionID; |
| 1146 | insn->spec = spec; |
| 1147 | } |
| 1148 | return 0; |
| 1149 | } |
Craig Topper | 21c3365 | 2011-10-02 16:56:09 +0000 | [diff] [blame] | 1150 | |
| 1151 | if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 && |
| 1152 | insn->rexPrefix & 0x01) { |
| 1153 | /* |
| 1154 | * NOOP shouldn't decode as NOOP if REX.b is set. Instead |
| 1155 | * it should decode as XCHG %r8, %eax. |
| 1156 | */ |
| 1157 | |
| 1158 | const struct InstructionSpecifier *spec; |
| 1159 | uint16_t instructionIDWithNewOpcode; |
| 1160 | const struct InstructionSpecifier *specWithNewOpcode; |
| 1161 | |
| 1162 | spec = specifierForUID(instructionID); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1163 | |
Craig Topper | b58a966 | 2011-10-05 03:29:32 +0000 | [diff] [blame] | 1164 | /* Borrow opcode from one of the other XCHGar opcodes */ |
Craig Topper | 21c3365 | 2011-10-02 16:56:09 +0000 | [diff] [blame] | 1165 | insn->opcode = 0x91; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1166 | |
Craig Topper | 21c3365 | 2011-10-02 16:56:09 +0000 | [diff] [blame] | 1167 | if (getIDWithAttrMask(&instructionIDWithNewOpcode, |
| 1168 | insn, |
| 1169 | attrMask)) { |
| 1170 | insn->opcode = 0x90; |
| 1171 | |
| 1172 | insn->instructionID = instructionID; |
| 1173 | insn->spec = spec; |
| 1174 | return 0; |
| 1175 | } |
| 1176 | |
| 1177 | specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode); |
| 1178 | |
Craig Topper | b58a966 | 2011-10-05 03:29:32 +0000 | [diff] [blame] | 1179 | /* Change back */ |
Craig Topper | 21c3365 | 2011-10-02 16:56:09 +0000 | [diff] [blame] | 1180 | insn->opcode = 0x90; |
| 1181 | |
| 1182 | insn->instructionID = instructionIDWithNewOpcode; |
| 1183 | insn->spec = specWithNewOpcode; |
| 1184 | |
| 1185 | return 0; |
| 1186 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1187 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1188 | insn->instructionID = instructionID; |
| 1189 | insn->spec = specifierForUID(insn->instructionID); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1190 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1191 | return 0; |
| 1192 | } |
| 1193 | |
| 1194 | /* |
| 1195 | * readSIB - Consumes the SIB byte to determine addressing information for an |
| 1196 | * instruction. |
| 1197 | * |
| 1198 | * @param insn - The instruction whose SIB byte is to be read. |
| 1199 | * @return - 0 if the SIB byte was successfully read; nonzero otherwise. |
| 1200 | */ |
| 1201 | static int readSIB(struct InternalInstruction* insn) { |
Richard Smith | 89ee75d | 2014-04-20 21:07:34 +0000 | [diff] [blame] | 1202 | SIBBase sibBaseBase = SIB_BASE_NONE; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1203 | uint8_t index, base; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1204 | |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 1205 | dbgprintf(insn, "readSIB()"); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1206 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1207 | if (insn->consumedSIB) |
| 1208 | return 0; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1209 | |
Richard Smith | 5d506103 | 2014-04-20 22:15:37 +0000 | [diff] [blame] | 1210 | insn->consumedSIB = true; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1211 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1212 | switch (insn->addressSize) { |
| 1213 | case 2: |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 1214 | dbgprintf(insn, "SIB-based addressing doesn't work in 16-bit mode"); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1215 | return -1; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1216 | case 4: |
Craig Topper | ca2382d | 2017-10-21 20:03:20 +0000 | [diff] [blame] | 1217 | insn->sibIndexBase = SIB_INDEX_EAX; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1218 | sibBaseBase = SIB_BASE_EAX; |
| 1219 | break; |
| 1220 | case 8: |
Craig Topper | ca2382d | 2017-10-21 20:03:20 +0000 | [diff] [blame] | 1221 | insn->sibIndexBase = SIB_INDEX_RAX; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1222 | sibBaseBase = SIB_BASE_RAX; |
| 1223 | break; |
| 1224 | } |
| 1225 | |
| 1226 | if (consumeByte(insn, &insn->sib)) |
| 1227 | return -1; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1228 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1229 | index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3); |
Douglas Katzman | fcda6f8 | 2015-06-24 22:04:55 +0000 | [diff] [blame] | 1230 | |
Douglas Katzman | fcda6f8 | 2015-06-24 22:04:55 +0000 | [diff] [blame] | 1231 | if (index == 0x4) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1232 | insn->sibIndex = SIB_INDEX_NONE; |
Douglas Katzman | fcda6f8 | 2015-06-24 22:04:55 +0000 | [diff] [blame] | 1233 | } else { |
Craig Topper | ca2382d | 2017-10-21 20:03:20 +0000 | [diff] [blame] | 1234 | insn->sibIndex = (SIBIndex)(insn->sibIndexBase + index); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1235 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1236 | |
Douglas Katzman | fcda6f8 | 2015-06-24 22:04:55 +0000 | [diff] [blame] | 1237 | insn->sibScale = 1 << scaleFromSIB(insn->sib); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1238 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1239 | base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1240 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1241 | switch (base) { |
| 1242 | case 0x5: |
Craig Topper | fae5ac2 | 2014-02-17 10:03:43 +0000 | [diff] [blame] | 1243 | case 0xd: |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1244 | switch (modFromModRM(insn->modRM)) { |
| 1245 | case 0x0: |
| 1246 | insn->eaDisplacement = EA_DISP_32; |
| 1247 | insn->sibBase = SIB_BASE_NONE; |
| 1248 | break; |
| 1249 | case 0x1: |
| 1250 | insn->eaDisplacement = EA_DISP_8; |
Craig Topper | fae5ac2 | 2014-02-17 10:03:43 +0000 | [diff] [blame] | 1251 | insn->sibBase = (SIBBase)(sibBaseBase + base); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1252 | break; |
| 1253 | case 0x2: |
| 1254 | insn->eaDisplacement = EA_DISP_32; |
Craig Topper | fae5ac2 | 2014-02-17 10:03:43 +0000 | [diff] [blame] | 1255 | insn->sibBase = (SIBBase)(sibBaseBase + base); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1256 | break; |
| 1257 | case 0x3: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1258 | debug("Cannot have Mod = 0b11 and a SIB byte"); |
| 1259 | return -1; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1260 | } |
| 1261 | break; |
| 1262 | default: |
Benjamin Kramer | 25bddae | 2011-02-27 18:13:53 +0000 | [diff] [blame] | 1263 | insn->sibBase = (SIBBase)(sibBaseBase + base); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1264 | break; |
| 1265 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1266 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | /* |
| 1271 | * readDisplacement - Consumes the displacement of an instruction. |
| 1272 | * |
| 1273 | * @param insn - The instruction whose displacement is to be read. |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1274 | * @return - 0 if the displacement byte was successfully read; nonzero |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1275 | * otherwise. |
| 1276 | */ |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1277 | static int readDisplacement(struct InternalInstruction* insn) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1278 | int8_t d8; |
| 1279 | int16_t d16; |
| 1280 | int32_t d32; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1281 | |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 1282 | dbgprintf(insn, "readDisplacement()"); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1283 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1284 | if (insn->consumedDisplacement) |
| 1285 | return 0; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1286 | |
Richard Smith | 5d506103 | 2014-04-20 22:15:37 +0000 | [diff] [blame] | 1287 | insn->consumedDisplacement = true; |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 1288 | insn->displacementOffset = insn->readerCursor - insn->startLocation; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1289 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1290 | switch (insn->eaDisplacement) { |
| 1291 | case EA_DISP_NONE: |
Richard Smith | 5d506103 | 2014-04-20 22:15:37 +0000 | [diff] [blame] | 1292 | insn->consumedDisplacement = false; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1293 | break; |
| 1294 | case EA_DISP_8: |
| 1295 | if (consumeInt8(insn, &d8)) |
| 1296 | return -1; |
| 1297 | insn->displacement = d8; |
| 1298 | break; |
| 1299 | case EA_DISP_16: |
| 1300 | if (consumeInt16(insn, &d16)) |
| 1301 | return -1; |
| 1302 | insn->displacement = d16; |
| 1303 | break; |
| 1304 | case EA_DISP_32: |
| 1305 | if (consumeInt32(insn, &d32)) |
| 1306 | return -1; |
| 1307 | insn->displacement = d32; |
| 1308 | break; |
| 1309 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1310 | |
Richard Smith | 5d506103 | 2014-04-20 22:15:37 +0000 | [diff] [blame] | 1311 | insn->consumedDisplacement = true; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1312 | return 0; |
| 1313 | } |
| 1314 | |
| 1315 | /* |
| 1316 | * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and |
| 1317 | * displacement) for an instruction and interprets it. |
| 1318 | * |
| 1319 | * @param insn - The instruction whose addressing information is to be read. |
| 1320 | * @return - 0 if the information was successfully read; nonzero otherwise. |
| 1321 | */ |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1322 | static int readModRM(struct InternalInstruction* insn) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1323 | uint8_t mod, rm, reg; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1324 | |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 1325 | dbgprintf(insn, "readModRM()"); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1326 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1327 | if (insn->consumedModRM) |
| 1328 | return 0; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1329 | |
Rafael Espindola | 9f9a106 | 2011-01-06 16:48:42 +0000 | [diff] [blame] | 1330 | if (consumeByte(insn, &insn->modRM)) |
| 1331 | return -1; |
Richard Smith | 5d506103 | 2014-04-20 22:15:37 +0000 | [diff] [blame] | 1332 | insn->consumedModRM = true; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1333 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1334 | mod = modFromModRM(insn->modRM); |
| 1335 | rm = rmFromModRM(insn->modRM); |
| 1336 | reg = regFromModRM(insn->modRM); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1337 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1338 | /* |
| 1339 | * This goes by insn->registerSize to pick the correct register, which messes |
| 1340 | * up if we're using (say) XMM or 8-bit register operands. That gets fixed in |
| 1341 | * fixupReg(). |
| 1342 | */ |
| 1343 | switch (insn->registerSize) { |
| 1344 | case 2: |
Sean Callanan | 2f9443f | 2009-12-22 02:07:42 +0000 | [diff] [blame] | 1345 | insn->regBase = MODRM_REG_AX; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1346 | insn->eaRegBase = EA_REG_AX; |
| 1347 | break; |
| 1348 | case 4: |
Sean Callanan | 2f9443f | 2009-12-22 02:07:42 +0000 | [diff] [blame] | 1349 | insn->regBase = MODRM_REG_EAX; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1350 | insn->eaRegBase = EA_REG_EAX; |
| 1351 | break; |
| 1352 | case 8: |
Sean Callanan | 2f9443f | 2009-12-22 02:07:42 +0000 | [diff] [blame] | 1353 | insn->regBase = MODRM_REG_RAX; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1354 | insn->eaRegBase = EA_REG_RAX; |
| 1355 | break; |
| 1356 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1357 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1358 | reg |= rFromREX(insn->rexPrefix) << 3; |
| 1359 | rm |= bFromREX(insn->rexPrefix) << 3; |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 1360 | if (insn->vectorExtensionType == TYPE_EVEX) { |
| 1361 | reg |= r2FromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4; |
| 1362 | rm |= xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4; |
| 1363 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1364 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1365 | insn->reg = (Reg)(insn->regBase + reg); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1366 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1367 | switch (insn->addressSize) { |
| 1368 | case 2: |
| 1369 | insn->eaBaseBase = EA_BASE_BX_SI; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1370 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1371 | switch (mod) { |
| 1372 | case 0x0: |
| 1373 | if (rm == 0x6) { |
| 1374 | insn->eaBase = EA_BASE_NONE; |
| 1375 | insn->eaDisplacement = EA_DISP_16; |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1376 | if (readDisplacement(insn)) |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1377 | return -1; |
| 1378 | } else { |
| 1379 | insn->eaBase = (EABase)(insn->eaBaseBase + rm); |
| 1380 | insn->eaDisplacement = EA_DISP_NONE; |
| 1381 | } |
| 1382 | break; |
| 1383 | case 0x1: |
| 1384 | insn->eaBase = (EABase)(insn->eaBaseBase + rm); |
| 1385 | insn->eaDisplacement = EA_DISP_8; |
Craig Topper | 399e39e | 2014-01-25 22:48:43 +0000 | [diff] [blame] | 1386 | insn->displacementSize = 1; |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1387 | if (readDisplacement(insn)) |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1388 | return -1; |
| 1389 | break; |
| 1390 | case 0x2: |
| 1391 | insn->eaBase = (EABase)(insn->eaBaseBase + rm); |
| 1392 | insn->eaDisplacement = EA_DISP_16; |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1393 | if (readDisplacement(insn)) |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1394 | return -1; |
| 1395 | break; |
| 1396 | case 0x3: |
| 1397 | insn->eaBase = (EABase)(insn->eaRegBase + rm); |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1398 | if (readDisplacement(insn)) |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1399 | return -1; |
| 1400 | break; |
| 1401 | } |
| 1402 | break; |
| 1403 | case 4: |
| 1404 | case 8: |
| 1405 | insn->eaBaseBase = (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1406 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1407 | switch (mod) { |
| 1408 | case 0x0: |
| 1409 | insn->eaDisplacement = EA_DISP_NONE; /* readSIB may override this */ |
Douglas Katzman | 6dc1397 | 2015-05-13 22:44:52 +0000 | [diff] [blame] | 1410 | // In determining whether RIP-relative mode is used (rm=5), |
| 1411 | // or whether a SIB byte is present (rm=4), |
| 1412 | // the extension bits (REX.b and EVEX.x) are ignored. |
| 1413 | switch (rm & 7) { |
| 1414 | case 0x4: // SIB byte is present |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1415 | insn->eaBase = (insn->addressSize == 4 ? |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1416 | EA_BASE_sib : EA_BASE_sib64); |
Craig Topper | 38afbfd | 2014-03-20 05:56:00 +0000 | [diff] [blame] | 1417 | if (readSIB(insn) || readDisplacement(insn)) |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1418 | return -1; |
| 1419 | break; |
Douglas Katzman | 6dc1397 | 2015-05-13 22:44:52 +0000 | [diff] [blame] | 1420 | case 0x5: // RIP-relative |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1421 | insn->eaBase = EA_BASE_NONE; |
| 1422 | insn->eaDisplacement = EA_DISP_32; |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1423 | if (readDisplacement(insn)) |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1424 | return -1; |
| 1425 | break; |
| 1426 | default: |
| 1427 | insn->eaBase = (EABase)(insn->eaBaseBase + rm); |
| 1428 | break; |
| 1429 | } |
| 1430 | break; |
| 1431 | case 0x1: |
Craig Topper | 399e39e | 2014-01-25 22:48:43 +0000 | [diff] [blame] | 1432 | insn->displacementSize = 1; |
Alp Toker | 771f765 | 2014-01-26 18:44:34 +0000 | [diff] [blame] | 1433 | /* FALLTHROUGH */ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1434 | case 0x2: |
| 1435 | insn->eaDisplacement = (mod == 0x1 ? EA_DISP_8 : EA_DISP_32); |
Douglas Katzman | 6dc1397 | 2015-05-13 22:44:52 +0000 | [diff] [blame] | 1436 | switch (rm & 7) { |
| 1437 | case 0x4: // SIB byte is present |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1438 | insn->eaBase = EA_BASE_sib; |
Craig Topper | 38afbfd | 2014-03-20 05:56:00 +0000 | [diff] [blame] | 1439 | if (readSIB(insn) || readDisplacement(insn)) |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1440 | return -1; |
| 1441 | break; |
| 1442 | default: |
| 1443 | insn->eaBase = (EABase)(insn->eaBaseBase + rm); |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1444 | if (readDisplacement(insn)) |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1445 | return -1; |
| 1446 | break; |
| 1447 | } |
| 1448 | break; |
| 1449 | case 0x3: |
| 1450 | insn->eaDisplacement = EA_DISP_NONE; |
| 1451 | insn->eaBase = (EABase)(insn->eaRegBase + rm); |
| 1452 | break; |
| 1453 | } |
| 1454 | break; |
| 1455 | } /* switch (insn->addressSize) */ |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1456 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1457 | return 0; |
| 1458 | } |
| 1459 | |
| 1460 | #define GENERIC_FIXUP_FUNC(name, base, prefix) \ |
Ahmed Bougacha | 85dc93c | 2016-07-14 14:53:21 +0000 | [diff] [blame] | 1461 | static uint16_t name(struct InternalInstruction *insn, \ |
| 1462 | OperandType type, \ |
| 1463 | uint8_t index, \ |
| 1464 | uint8_t *valid) { \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1465 | *valid = 1; \ |
| 1466 | switch (type) { \ |
| 1467 | default: \ |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1468 | debug("Unhandled register type"); \ |
| 1469 | *valid = 0; \ |
| 1470 | return 0; \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1471 | case TYPE_Rv: \ |
| 1472 | return base + index; \ |
| 1473 | case TYPE_R8: \ |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1474 | if (insn->rexPrefix && \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1475 | index >= 4 && index <= 7) { \ |
| 1476 | return prefix##_SPL + (index - 4); \ |
| 1477 | } else { \ |
| 1478 | return prefix##_AL + index; \ |
| 1479 | } \ |
| 1480 | case TYPE_R16: \ |
| 1481 | return prefix##_AX + index; \ |
| 1482 | case TYPE_R32: \ |
| 1483 | return prefix##_EAX + index; \ |
| 1484 | case TYPE_R64: \ |
| 1485 | return prefix##_RAX + index; \ |
Craig Topper | ad944a1 | 2017-01-16 06:49:03 +0000 | [diff] [blame] | 1486 | case TYPE_ZMM: \ |
Elena Demikhovsky | 003e7d7 | 2013-07-28 08:28:38 +0000 | [diff] [blame] | 1487 | return prefix##_ZMM0 + index; \ |
Craig Topper | ad944a1 | 2017-01-16 06:49:03 +0000 | [diff] [blame] | 1488 | case TYPE_YMM: \ |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1489 | return prefix##_YMM0 + index; \ |
Craig Topper | ad944a1 | 2017-01-16 06:49:03 +0000 | [diff] [blame] | 1490 | case TYPE_XMM: \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1491 | return prefix##_XMM0 + index; \ |
Craig Topper | ad944a1 | 2017-01-16 06:49:03 +0000 | [diff] [blame] | 1492 | case TYPE_VK: \ |
Craig Topper | 9c26bcc | 2015-03-02 03:33:11 +0000 | [diff] [blame] | 1493 | if (index > 7) \ |
| 1494 | *valid = 0; \ |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 1495 | return prefix##_K0 + index; \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1496 | case TYPE_MM64: \ |
Craig Topper | d5b3923 | 2014-12-26 18:19:44 +0000 | [diff] [blame] | 1497 | return prefix##_MM0 + (index & 0x7); \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1498 | case TYPE_SEGMENTREG: \ |
Andrew V. Tischenko | eff4fc0 | 2017-10-23 09:36:33 +0000 | [diff] [blame] | 1499 | if ((index & 7) > 5) \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1500 | *valid = 0; \ |
Andrew V. Tischenko | eff4fc0 | 2017-10-23 09:36:33 +0000 | [diff] [blame] | 1501 | return prefix##_ES + (index & 7); \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1502 | case TYPE_DEBUGREG: \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1503 | return prefix##_DR0 + index; \ |
Sean Callanan | e7e1cf9 | 2010-05-06 20:59:00 +0000 | [diff] [blame] | 1504 | case TYPE_CONTROLREG: \ |
Sean Callanan | e7e1cf9 | 2010-05-06 20:59:00 +0000 | [diff] [blame] | 1505 | return prefix##_CR0 + index; \ |
Ahmed Bougacha | 85dc93c | 2016-07-14 14:53:21 +0000 | [diff] [blame] | 1506 | case TYPE_BNDR: \ |
| 1507 | if (index > 3) \ |
| 1508 | *valid = 0; \ |
| 1509 | return prefix##_BND0 + index; \ |
Craig Topper | ca2382d | 2017-10-21 20:03:20 +0000 | [diff] [blame] | 1510 | case TYPE_MVSIBX: \ |
| 1511 | return prefix##_XMM0 + index; \ |
| 1512 | case TYPE_MVSIBY: \ |
| 1513 | return prefix##_YMM0 + index; \ |
| 1514 | case TYPE_MVSIBZ: \ |
| 1515 | return prefix##_ZMM0 + index; \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1516 | } \ |
| 1517 | } |
| 1518 | |
| 1519 | /* |
| 1520 | * fixup*Value - Consults an operand type to determine the meaning of the |
| 1521 | * reg or R/M field. If the operand is an XMM operand, for example, an |
| 1522 | * operand would be XMM0 instead of AX, which readModRM() would otherwise |
| 1523 | * misinterpret it as. |
| 1524 | * |
| 1525 | * @param insn - The instruction containing the operand. |
| 1526 | * @param type - The operand type. |
| 1527 | * @param index - The existing value of the field as reported by readModRM(). |
| 1528 | * @param valid - The address of a uint8_t. The target is set to 1 if the |
| 1529 | * field is valid for the register class; 0 if not. |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1530 | * @return - The proper value. |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1531 | */ |
Sean Callanan | 2f9443f | 2009-12-22 02:07:42 +0000 | [diff] [blame] | 1532 | GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase, MODRM_REG) |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1533 | GENERIC_FIXUP_FUNC(fixupRMValue, insn->eaRegBase, EA_REG) |
| 1534 | |
| 1535 | /* |
| 1536 | * fixupReg - Consults an operand specifier to determine which of the |
| 1537 | * fixup*Value functions to use in correcting readModRM()'ss interpretation. |
| 1538 | * |
| 1539 | * @param insn - See fixup*Value(). |
| 1540 | * @param op - The operand specifier. |
| 1541 | * @return - 0 if fixup was successful; -1 if the register returned was |
| 1542 | * invalid for its class. |
| 1543 | */ |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1544 | static int fixupReg(struct InternalInstruction *insn, |
Benjamin Kramer | de0a4fb | 2010-10-23 09:10:44 +0000 | [diff] [blame] | 1545 | const struct OperandSpecifier *op) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1546 | uint8_t valid; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1547 | |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 1548 | dbgprintf(insn, "fixupReg()"); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1549 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1550 | switch ((OperandEncoding)op->encoding) { |
| 1551 | default: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1552 | debug("Expected a REG or R/M encoding in fixupReg"); |
| 1553 | return -1; |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1554 | case ENCODING_VVVV: |
| 1555 | insn->vvvv = (Reg)fixupRegValue(insn, |
| 1556 | (OperandType)op->type, |
| 1557 | insn->vvvv, |
| 1558 | &valid); |
| 1559 | if (!valid) |
| 1560 | return -1; |
| 1561 | break; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1562 | case ENCODING_REG: |
| 1563 | insn->reg = (Reg)fixupRegValue(insn, |
| 1564 | (OperandType)op->type, |
| 1565 | insn->reg - insn->regBase, |
| 1566 | &valid); |
| 1567 | if (!valid) |
| 1568 | return -1; |
| 1569 | break; |
Adam Nemet | 5933c2f | 2014-07-17 17:04:56 +0000 | [diff] [blame] | 1570 | CASE_ENCODING_RM: |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1571 | if (insn->eaBase >= insn->eaRegBase) { |
| 1572 | insn->eaBase = (EABase)fixupRMValue(insn, |
| 1573 | (OperandType)op->type, |
| 1574 | insn->eaBase - insn->eaRegBase, |
| 1575 | &valid); |
| 1576 | if (!valid) |
| 1577 | return -1; |
| 1578 | } |
| 1579 | break; |
| 1580 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1581 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1582 | return 0; |
| 1583 | } |
| 1584 | |
| 1585 | /* |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1586 | * readOpcodeRegister - Reads an operand from the opcode field of an |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1587 | * instruction and interprets it appropriately given the operand width. |
| 1588 | * Handles AddRegFrm instructions. |
| 1589 | * |
Craig Topper | 9155118 | 2014-01-01 15:29:32 +0000 | [diff] [blame] | 1590 | * @param insn - the instruction whose opcode field is to be read. |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1591 | * @param size - The width (in bytes) of the register being specified. |
| 1592 | * 1 means AL and friends, 2 means AX, 4 means EAX, and 8 means |
| 1593 | * RAX. |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1594 | * @return - 0 on success; nonzero otherwise. |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1595 | */ |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1596 | static int readOpcodeRegister(struct InternalInstruction* insn, uint8_t size) { |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 1597 | dbgprintf(insn, "readOpcodeRegister()"); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1598 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1599 | if (size == 0) |
| 1600 | size = insn->registerSize; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1601 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1602 | switch (size) { |
| 1603 | case 1: |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1604 | insn->opcodeRegister = (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3) |
Craig Topper | 9155118 | 2014-01-01 15:29:32 +0000 | [diff] [blame] | 1605 | | (insn->opcode & 7))); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1606 | if (insn->rexPrefix && |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1607 | insn->opcodeRegister >= MODRM_REG_AL + 0x4 && |
| 1608 | insn->opcodeRegister < MODRM_REG_AL + 0x8) { |
Sean Callanan | 2f9443f | 2009-12-22 02:07:42 +0000 | [diff] [blame] | 1609 | insn->opcodeRegister = (Reg)(MODRM_REG_SPL |
| 1610 | + (insn->opcodeRegister - MODRM_REG_AL - 4)); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1611 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1612 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1613 | break; |
| 1614 | case 2: |
Sean Callanan | 2f9443f | 2009-12-22 02:07:42 +0000 | [diff] [blame] | 1615 | insn->opcodeRegister = (Reg)(MODRM_REG_AX |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1616 | + ((bFromREX(insn->rexPrefix) << 3) |
Craig Topper | 9155118 | 2014-01-01 15:29:32 +0000 | [diff] [blame] | 1617 | | (insn->opcode & 7))); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1618 | break; |
| 1619 | case 4: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1620 | insn->opcodeRegister = (Reg)(MODRM_REG_EAX |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1621 | + ((bFromREX(insn->rexPrefix) << 3) |
Craig Topper | 9155118 | 2014-01-01 15:29:32 +0000 | [diff] [blame] | 1622 | | (insn->opcode & 7))); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1623 | break; |
| 1624 | case 8: |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1625 | insn->opcodeRegister = (Reg)(MODRM_REG_RAX |
| 1626 | + ((bFromREX(insn->rexPrefix) << 3) |
Craig Topper | 9155118 | 2014-01-01 15:29:32 +0000 | [diff] [blame] | 1627 | | (insn->opcode & 7))); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1628 | break; |
| 1629 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1630 | |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1631 | return 0; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | /* |
| 1635 | * readImmediate - Consumes an immediate operand from an instruction, given the |
| 1636 | * desired operand size. |
| 1637 | * |
| 1638 | * @param insn - The instruction whose operand is to be read. |
| 1639 | * @param size - The width (in bytes) of the operand. |
| 1640 | * @return - 0 if the immediate was successfully consumed; nonzero |
| 1641 | * otherwise. |
| 1642 | */ |
| 1643 | static int readImmediate(struct InternalInstruction* insn, uint8_t size) { |
| 1644 | uint8_t imm8; |
| 1645 | uint16_t imm16; |
| 1646 | uint32_t imm32; |
| 1647 | uint64_t imm64; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1648 | |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 1649 | dbgprintf(insn, "readImmediate()"); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1650 | |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1651 | if (insn->numImmediatesConsumed == 2) { |
| 1652 | debug("Already consumed two immediates"); |
| 1653 | return -1; |
| 1654 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1655 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1656 | if (size == 0) |
| 1657 | size = insn->immediateSize; |
| 1658 | else |
| 1659 | insn->immediateSize = size; |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 1660 | insn->immediateOffset = insn->readerCursor - insn->startLocation; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1661 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1662 | switch (size) { |
| 1663 | case 1: |
| 1664 | if (consumeByte(insn, &imm8)) |
| 1665 | return -1; |
| 1666 | insn->immediates[insn->numImmediatesConsumed] = imm8; |
| 1667 | break; |
| 1668 | case 2: |
| 1669 | if (consumeUInt16(insn, &imm16)) |
| 1670 | return -1; |
| 1671 | insn->immediates[insn->numImmediatesConsumed] = imm16; |
| 1672 | break; |
| 1673 | case 4: |
| 1674 | if (consumeUInt32(insn, &imm32)) |
| 1675 | return -1; |
| 1676 | insn->immediates[insn->numImmediatesConsumed] = imm32; |
| 1677 | break; |
| 1678 | case 8: |
| 1679 | if (consumeUInt64(insn, &imm64)) |
| 1680 | return -1; |
| 1681 | insn->immediates[insn->numImmediatesConsumed] = imm64; |
| 1682 | break; |
| 1683 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1684 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1685 | insn->numImmediatesConsumed++; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1686 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1687 | return 0; |
| 1688 | } |
| 1689 | |
| 1690 | /* |
Craig Topper | 8dd7bbc | 2011-09-13 07:37:44 +0000 | [diff] [blame] | 1691 | * readVVVV - Consumes vvvv from an instruction if it has a VEX prefix. |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1692 | * |
| 1693 | * @param insn - The instruction whose operand is to be read. |
Craig Topper | 8dd7bbc | 2011-09-13 07:37:44 +0000 | [diff] [blame] | 1694 | * @return - 0 if the vvvv was successfully consumed; nonzero |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1695 | * otherwise. |
| 1696 | */ |
| 1697 | static int readVVVV(struct InternalInstruction* insn) { |
| 1698 | dbgprintf(insn, "readVVVV()"); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1699 | |
Richard Smith | 89ee75d | 2014-04-20 21:07:34 +0000 | [diff] [blame] | 1700 | int vvvv; |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 1701 | if (insn->vectorExtensionType == TYPE_EVEX) |
Adam Nemet | 8ae7050 | 2014-06-24 01:42:32 +0000 | [diff] [blame] | 1702 | vvvv = (v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4 | |
| 1703 | vvvvFromEVEX3of4(insn->vectorExtensionPrefix[2])); |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 1704 | else if (insn->vectorExtensionType == TYPE_VEX_3B) |
Richard Smith | 89ee75d | 2014-04-20 21:07:34 +0000 | [diff] [blame] | 1705 | vvvv = vvvvFromVEX3of3(insn->vectorExtensionPrefix[2]); |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 1706 | else if (insn->vectorExtensionType == TYPE_VEX_2B) |
Richard Smith | 89ee75d | 2014-04-20 21:07:34 +0000 | [diff] [blame] | 1707 | vvvv = vvvvFromVEX2of2(insn->vectorExtensionPrefix[1]); |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 1708 | else if (insn->vectorExtensionType == TYPE_XOP) |
Richard Smith | 89ee75d | 2014-04-20 21:07:34 +0000 | [diff] [blame] | 1709 | vvvv = vvvvFromXOP3of3(insn->vectorExtensionPrefix[2]); |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1710 | else |
| 1711 | return -1; |
| 1712 | |
Craig Topper | 0d0be47 | 2011-10-03 08:14:29 +0000 | [diff] [blame] | 1713 | if (insn->mode != MODE_64BIT) |
Richard Smith | 89ee75d | 2014-04-20 21:07:34 +0000 | [diff] [blame] | 1714 | vvvv &= 0x7; |
Craig Topper | 0d0be47 | 2011-10-03 08:14:29 +0000 | [diff] [blame] | 1715 | |
Richard Smith | 89ee75d | 2014-04-20 21:07:34 +0000 | [diff] [blame] | 1716 | insn->vvvv = static_cast<Reg>(vvvv); |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1717 | return 0; |
| 1718 | } |
| 1719 | |
| 1720 | /* |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 1721 | * readMaskRegister - Reads an mask register from the opcode field of an |
| 1722 | * instruction. |
| 1723 | * |
| 1724 | * @param insn - The instruction whose opcode field is to be read. |
| 1725 | * @return - 0 on success; nonzero otherwise. |
| 1726 | */ |
| 1727 | static int readMaskRegister(struct InternalInstruction* insn) { |
| 1728 | dbgprintf(insn, "readMaskRegister()"); |
| 1729 | |
| 1730 | if (insn->vectorExtensionType != TYPE_EVEX) |
| 1731 | return -1; |
| 1732 | |
Richard Smith | 89ee75d | 2014-04-20 21:07:34 +0000 | [diff] [blame] | 1733 | insn->writemask = |
| 1734 | static_cast<Reg>(aaaFromEVEX4of4(insn->vectorExtensionPrefix[3])); |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 1735 | return 0; |
| 1736 | } |
| 1737 | |
| 1738 | /* |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1739 | * readOperands - Consults the specifier for an instruction and consumes all |
| 1740 | * operands for that instruction, interpreting them as it goes. |
| 1741 | * |
| 1742 | * @param insn - The instruction whose operands are to be read and interpreted. |
| 1743 | * @return - 0 if all operands could be read; nonzero otherwise. |
| 1744 | */ |
| 1745 | static int readOperands(struct InternalInstruction* insn) { |
Craig Topper | 8dd7bbc | 2011-09-13 07:37:44 +0000 | [diff] [blame] | 1746 | int hasVVVV, needVVVV; |
Craig Topper | 2ba766a | 2011-12-30 06:23:39 +0000 | [diff] [blame] | 1747 | int sawRegImm = 0; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1748 | |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 1749 | dbgprintf(insn, "readOperands()"); |
Craig Topper | 8dd7bbc | 2011-09-13 07:37:44 +0000 | [diff] [blame] | 1750 | |
| 1751 | /* If non-zero vvvv specified, need to make sure one of the operands |
| 1752 | uses it. */ |
| 1753 | hasVVVV = !readVVVV(insn); |
| 1754 | needVVVV = hasVVVV && (insn->vvvv != 0); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1755 | |
Patrik Hagglund | 3199838 | 2014-04-28 12:12:27 +0000 | [diff] [blame] | 1756 | for (const auto &Op : x86OperandSets[insn->spec->operands]) { |
| 1757 | switch (Op.encoding) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1758 | case ENCODING_NONE: |
David Woodhouse | 2ef8d9c | 2014-01-22 15:08:08 +0000 | [diff] [blame] | 1759 | case ENCODING_SI: |
David Woodhouse | b33c2ef | 2014-01-22 15:08:21 +0000 | [diff] [blame] | 1760 | case ENCODING_DI: |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1761 | break; |
Craig Topper | 33ac064 | 2017-01-16 05:44:25 +0000 | [diff] [blame] | 1762 | CASE_ENCODING_VSIB: |
| 1763 | // VSIB can use the V2 bit so check only the other bits. |
| 1764 | if (needVVVV) |
| 1765 | needVVVV = hasVVVV & ((insn->vvvv & 0xf) != 0); |
Craig Topper | 3173a1f | 2017-01-16 05:44:33 +0000 | [diff] [blame] | 1766 | if (readModRM(insn)) |
| 1767 | return -1; |
Craig Topper | ca2382d | 2017-10-21 20:03:20 +0000 | [diff] [blame] | 1768 | |
Craig Topper | 158bc64 | 2017-10-22 04:32:30 +0000 | [diff] [blame] | 1769 | // Reject if SIB wasn't used. |
| 1770 | if (insn->eaBase != EA_BASE_sib && insn->eaBase != EA_BASE_sib64) |
| 1771 | return -1; |
| 1772 | |
Craig Topper | ca2382d | 2017-10-21 20:03:20 +0000 | [diff] [blame] | 1773 | // If sibIndex was set to SIB_INDEX_NONE, index offset is 4. |
| 1774 | if (insn->sibIndex == SIB_INDEX_NONE) |
| 1775 | insn->sibIndex = (SIBIndex)4; |
| 1776 | |
| 1777 | // If EVEX.v2 is set this is one of the 16-31 registers. |
| 1778 | if (insn->vectorExtensionType == TYPE_EVEX && |
| 1779 | v2FromEVEX4of4(insn->vectorExtensionPrefix[3])) |
| 1780 | insn->sibIndex = (SIBIndex)(insn->sibIndex + 16); |
| 1781 | |
| 1782 | // Adjust the index register to the correct size. |
| 1783 | switch ((OperandType)Op.type) { |
| 1784 | default: |
| 1785 | debug("Unhandled VSIB index type"); |
Craig Topper | 3173a1f | 2017-01-16 05:44:33 +0000 | [diff] [blame] | 1786 | return -1; |
Craig Topper | ca2382d | 2017-10-21 20:03:20 +0000 | [diff] [blame] | 1787 | case TYPE_MVSIBX: |
| 1788 | insn->sibIndex = (SIBIndex)(SIB_INDEX_XMM0 + |
| 1789 | (insn->sibIndex - insn->sibIndexBase)); |
| 1790 | break; |
| 1791 | case TYPE_MVSIBY: |
| 1792 | insn->sibIndex = (SIBIndex)(SIB_INDEX_YMM0 + |
| 1793 | (insn->sibIndex - insn->sibIndexBase)); |
| 1794 | break; |
| 1795 | case TYPE_MVSIBZ: |
| 1796 | insn->sibIndex = (SIBIndex)(SIB_INDEX_ZMM0 + |
| 1797 | (insn->sibIndex - insn->sibIndexBase)); |
| 1798 | break; |
| 1799 | } |
| 1800 | |
Craig Topper | 3173a1f | 2017-01-16 05:44:33 +0000 | [diff] [blame] | 1801 | // Apply the AVX512 compressed displacement scaling factor. |
| 1802 | if (Op.encoding != ENCODING_REG && insn->eaDisplacement == EA_DISP_8) |
| 1803 | insn->displacement *= 1 << (Op.encoding - ENCODING_VSIB); |
| 1804 | break; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1805 | case ENCODING_REG: |
Adam Nemet | 5933c2f | 2014-07-17 17:04:56 +0000 | [diff] [blame] | 1806 | CASE_ENCODING_RM: |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1807 | if (readModRM(insn)) |
| 1808 | return -1; |
Patrik Hagglund | 3199838 | 2014-04-28 12:12:27 +0000 | [diff] [blame] | 1809 | if (fixupReg(insn, &Op)) |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1810 | return -1; |
Adam Nemet | 5933c2f | 2014-07-17 17:04:56 +0000 | [diff] [blame] | 1811 | // Apply the AVX512 compressed displacement scaling factor. |
| 1812 | if (Op.encoding != ENCODING_REG && insn->eaDisplacement == EA_DISP_8) |
| 1813 | insn->displacement *= 1 << (Op.encoding - ENCODING_RM); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1814 | break; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1815 | case ENCODING_IB: |
Craig Topper | 2ba766a | 2011-12-30 06:23:39 +0000 | [diff] [blame] | 1816 | if (sawRegImm) { |
Benjamin Kramer | 9c48f26 | 2012-01-04 22:06:45 +0000 | [diff] [blame] | 1817 | /* Saw a register immediate so don't read again and instead split the |
| 1818 | previous immediate. FIXME: This is a hack. */ |
Benjamin Kramer | 47aecca | 2012-01-01 17:55:36 +0000 | [diff] [blame] | 1819 | insn->immediates[insn->numImmediatesConsumed] = |
| 1820 | insn->immediates[insn->numImmediatesConsumed - 1] & 0xf; |
| 1821 | ++insn->numImmediatesConsumed; |
Craig Topper | 2ba766a | 2011-12-30 06:23:39 +0000 | [diff] [blame] | 1822 | break; |
| 1823 | } |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1824 | if (readImmediate(insn, 1)) |
| 1825 | return -1; |
Craig Topper | ad944a1 | 2017-01-16 06:49:03 +0000 | [diff] [blame] | 1826 | if (Op.type == TYPE_XMM || Op.type == TYPE_YMM) |
Craig Topper | 2ba766a | 2011-12-30 06:23:39 +0000 | [diff] [blame] | 1827 | sawRegImm = 1; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1828 | break; |
| 1829 | case ENCODING_IW: |
| 1830 | if (readImmediate(insn, 2)) |
| 1831 | return -1; |
| 1832 | break; |
| 1833 | case ENCODING_ID: |
| 1834 | if (readImmediate(insn, 4)) |
| 1835 | return -1; |
| 1836 | break; |
| 1837 | case ENCODING_IO: |
| 1838 | if (readImmediate(insn, 8)) |
| 1839 | return -1; |
| 1840 | break; |
| 1841 | case ENCODING_Iv: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1842 | if (readImmediate(insn, insn->immediateSize)) |
| 1843 | return -1; |
Chris Lattner | d4758fc | 2010-04-16 21:15:15 +0000 | [diff] [blame] | 1844 | break; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1845 | case ENCODING_Ia: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1846 | if (readImmediate(insn, insn->addressSize)) |
| 1847 | return -1; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1848 | break; |
Craig Topper | 326008c | 2017-10-23 02:26:24 +0000 | [diff] [blame] | 1849 | case ENCODING_IRC: |
| 1850 | insn->RC = (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 1) | |
| 1851 | lFromEVEX4of4(insn->vectorExtensionPrefix[3]); |
| 1852 | break; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1853 | case ENCODING_RB: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1854 | if (readOpcodeRegister(insn, 1)) |
| 1855 | return -1; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1856 | break; |
| 1857 | case ENCODING_RW: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1858 | if (readOpcodeRegister(insn, 2)) |
| 1859 | return -1; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1860 | break; |
| 1861 | case ENCODING_RD: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1862 | if (readOpcodeRegister(insn, 4)) |
| 1863 | return -1; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1864 | break; |
| 1865 | case ENCODING_RO: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1866 | if (readOpcodeRegister(insn, 8)) |
| 1867 | return -1; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1868 | break; |
| 1869 | case ENCODING_Rv: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1870 | if (readOpcodeRegister(insn, 0)) |
| 1871 | return -1; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1872 | break; |
Craig Topper | 623b0d6 | 2014-01-01 14:22:37 +0000 | [diff] [blame] | 1873 | case ENCODING_FP: |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1874 | break; |
| 1875 | case ENCODING_VVVV: |
Craig Topper | 8dd7bbc | 2011-09-13 07:37:44 +0000 | [diff] [blame] | 1876 | needVVVV = 0; /* Mark that we have found a VVVV operand. */ |
| 1877 | if (!hasVVVV) |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1878 | return -1; |
Patrik Hagglund | 3199838 | 2014-04-28 12:12:27 +0000 | [diff] [blame] | 1879 | if (fixupReg(insn, &Op)) |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1880 | return -1; |
| 1881 | break; |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 1882 | case ENCODING_WRITEMASK: |
| 1883 | if (readMaskRegister(insn)) |
| 1884 | return -1; |
| 1885 | break; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1886 | case ENCODING_DUP: |
| 1887 | break; |
| 1888 | default: |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 1889 | dbgprintf(insn, "Encountered an operand with an unknown encoding."); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1890 | return -1; |
| 1891 | } |
| 1892 | } |
Craig Topper | 8dd7bbc | 2011-09-13 07:37:44 +0000 | [diff] [blame] | 1893 | |
| 1894 | /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */ |
| 1895 | if (needVVVV) return -1; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1896 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1897 | return 0; |
| 1898 | } |
| 1899 | |
| 1900 | /* |
| 1901 | * decodeInstruction - Reads and interprets a full instruction provided by the |
| 1902 | * user. |
| 1903 | * |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1904 | * @param insn - A pointer to the instruction to be populated. Must be |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1905 | * pre-allocated. |
| 1906 | * @param reader - The function to be used to read the instruction's bytes. |
| 1907 | * @param readerArg - A generic argument to be passed to the reader to store |
| 1908 | * any internal state. |
| 1909 | * @param logger - If non-NULL, the function to be used to write log messages |
| 1910 | * and warnings. |
| 1911 | * @param loggerArg - A generic argument to be passed to the logger to store |
| 1912 | * any internal state. |
| 1913 | * @param startLoc - The address (in the reader's address space) of the first |
| 1914 | * byte in the instruction. |
| 1915 | * @param mode - The mode (real mode, IA-32e, or IA-32e in 64-bit mode) to |
| 1916 | * decode the instruction in. |
| 1917 | * @return - 0 if the instruction's memory could be read; nonzero if |
| 1918 | * not. |
| 1919 | */ |
Richard Smith | 89ee75d | 2014-04-20 21:07:34 +0000 | [diff] [blame] | 1920 | int llvm::X86Disassembler::decodeInstruction( |
| 1921 | struct InternalInstruction *insn, byteReader_t reader, |
| 1922 | const void *readerArg, dlog_t logger, void *loggerArg, const void *miiArg, |
| 1923 | uint64_t startLoc, DisassemblerMode mode) { |
Daniel Dunbar | c745a62 | 2009-12-19 03:31:50 +0000 | [diff] [blame] | 1924 | memset(insn, 0, sizeof(struct InternalInstruction)); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1925 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1926 | insn->reader = reader; |
| 1927 | insn->readerArg = readerArg; |
| 1928 | insn->dlog = logger; |
| 1929 | insn->dlogArg = loggerArg; |
| 1930 | insn->startLocation = startLoc; |
| 1931 | insn->readerCursor = startLoc; |
| 1932 | insn->mode = mode; |
| 1933 | insn->numImmediatesConsumed = 0; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1934 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1935 | if (readPrefixes(insn) || |
| 1936 | readOpcode(insn) || |
Benjamin Kramer | 478e8de | 2012-02-11 14:50:54 +0000 | [diff] [blame] | 1937 | getID(insn, miiArg) || |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1938 | insn->instructionID == 0 || |
| 1939 | readOperands(insn)) |
| 1940 | return -1; |
Craig Topper | b8aec08 | 2012-08-01 07:39:18 +0000 | [diff] [blame] | 1941 | |
Patrik Hagglund | 3199838 | 2014-04-28 12:12:27 +0000 | [diff] [blame] | 1942 | insn->operands = x86OperandSets[insn->spec->operands]; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1943 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1944 | insn->length = insn->readerCursor - insn->startLocation; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1945 | |
Benjamin Kramer | 4f67227 | 2010-03-18 12:18:36 +0000 | [diff] [blame] | 1946 | dbgprintf(insn, "Read from 0x%llx to 0x%llx: length %zu", |
| 1947 | startLoc, insn->readerCursor, insn->length); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1948 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1949 | if (insn->length > 15) |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 1950 | dbgprintf(insn, "Instruction exceeds 15-byte limit"); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1951 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1952 | return 0; |
| 1953 | } |