Jia Liu | b22310f | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | /*===-- X86DisassemblerDecoder.c - Disassembler decoder ------------*- C -*-===* |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +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 | *===----------------------------------------------------------------------===*/ |
| 15 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 16 | #include <stdarg.h> /* for va_*() */ |
| 17 | #include <stdio.h> /* for vsnprintf() */ |
| 18 | #include <stdlib.h> /* for exit() */ |
Daniel Dunbar | c745a62 | 2009-12-19 03:31:50 +0000 | [diff] [blame] | 19 | #include <string.h> /* for memset() */ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 20 | |
| 21 | #include "X86DisassemblerDecoder.h" |
| 22 | |
| 23 | #include "X86GenDisassemblerTables.inc" |
| 24 | |
| 25 | #define TRUE 1 |
| 26 | #define FALSE 0 |
| 27 | |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 28 | #ifndef NDEBUG |
| 29 | #define debug(s) do { x86DisassemblerDebug(__FILE__, __LINE__, s); } while (0) |
| 30 | #else |
| 31 | #define debug(s) do { } while (0) |
| 32 | #endif |
| 33 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | * contextForAttrs - Client for the instruction context table. Takes a set of |
| 37 | * attributes and returns the appropriate decode context. |
| 38 | * |
| 39 | * @param attrMask - Attributes, from the enumeration attributeBits. |
| 40 | * @return - The InstructionContext to use when looking up an |
| 41 | * an instruction with these attributes. |
| 42 | */ |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 43 | static InstructionContext contextForAttrs(uint16_t attrMask) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 44 | return CONTEXTS_SYM[attrMask]; |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * modRMRequired - Reads the appropriate instruction table to determine whether |
| 49 | * the ModR/M byte is required to decode a particular instruction. |
| 50 | * |
| 51 | * @param type - The opcode type (i.e., how many bytes it has). |
| 52 | * @param insnContext - The context for the instruction, as returned by |
| 53 | * contextForAttrs. |
| 54 | * @param opcode - The last byte of the instruction's opcode, not counting |
| 55 | * ModR/M extensions and escapes. |
| 56 | * @return - TRUE if the ModR/M byte is required, FALSE otherwise. |
| 57 | */ |
Sean Callanan | 588785c | 2009-12-22 22:51:40 +0000 | [diff] [blame] | 58 | static int modRMRequired(OpcodeType type, |
Craig Topper | 21c3365 | 2011-10-02 16:56:09 +0000 | [diff] [blame] | 59 | InstructionContext insnContext, |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 60 | uint16_t opcode) { |
Daniel Dunbar | 8b532de | 2009-12-22 01:41:37 +0000 | [diff] [blame] | 61 | const struct ContextDecision* decision = 0; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 62 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 63 | switch (type) { |
| 64 | case ONEBYTE: |
| 65 | decision = &ONEBYTE_SYM; |
| 66 | break; |
| 67 | case TWOBYTE: |
| 68 | decision = &TWOBYTE_SYM; |
| 69 | break; |
| 70 | case THREEBYTE_38: |
| 71 | decision = &THREEBYTE38_SYM; |
| 72 | break; |
| 73 | case THREEBYTE_3A: |
| 74 | decision = &THREEBYTE3A_SYM; |
| 75 | break; |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 76 | case XOP8_MAP: |
| 77 | decision = &XOP8_MAP_SYM; |
| 78 | break; |
| 79 | case XOP9_MAP: |
| 80 | decision = &XOP9_MAP_SYM; |
| 81 | break; |
| 82 | case XOPA_MAP: |
| 83 | decision = &XOPA_MAP_SYM; |
| 84 | break; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 85 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 86 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 87 | return decision->opcodeDecisions[insnContext].modRMDecisions[opcode]. |
| 88 | modrm_type != MODRM_ONEENTRY; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* |
| 92 | * decode - Reads the appropriate instruction table to obtain the unique ID of |
| 93 | * an instruction. |
| 94 | * |
| 95 | * @param type - See modRMRequired(). |
| 96 | * @param insnContext - See modRMRequired(). |
| 97 | * @param opcode - See modRMRequired(). |
| 98 | * @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] | 99 | * @return - The UID of the instruction, or 0 on failure. |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 100 | */ |
Sean Callanan | 588785c | 2009-12-22 22:51:40 +0000 | [diff] [blame] | 101 | static InstrUID decode(OpcodeType type, |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 102 | InstructionContext insnContext, |
| 103 | uint8_t opcode, |
| 104 | uint8_t modRM) { |
Duncan Sands | ae22c60 | 2012-02-05 14:20:11 +0000 | [diff] [blame] | 105 | const struct ModRMDecision* dec = 0; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 106 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 107 | switch (type) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 108 | case ONEBYTE: |
| 109 | dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode]; |
| 110 | break; |
| 111 | case TWOBYTE: |
| 112 | dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode]; |
| 113 | break; |
| 114 | case THREEBYTE_38: |
| 115 | dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode]; |
| 116 | break; |
| 117 | case THREEBYTE_3A: |
| 118 | dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode]; |
| 119 | break; |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 120 | case XOP8_MAP: |
| 121 | dec = &XOP8_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode]; |
| 122 | break; |
| 123 | case XOP9_MAP: |
| 124 | dec = &XOP9_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode]; |
| 125 | break; |
| 126 | case XOPA_MAP: |
| 127 | dec = &XOPA_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode]; |
| 128 | break; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 129 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 130 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 131 | switch (dec->modrm_type) { |
| 132 | default: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 133 | debug("Corrupt table! Unknown modrm_type"); |
| 134 | return 0; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 135 | case MODRM_ONEENTRY: |
Craig Topper | 487e744 | 2012-02-09 07:45:30 +0000 | [diff] [blame] | 136 | return modRMTable[dec->instructionIDs]; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 137 | case MODRM_SPLITRM: |
| 138 | if (modFromModRM(modRM) == 0x3) |
Craig Topper | 487e744 | 2012-02-09 07:45:30 +0000 | [diff] [blame] | 139 | return modRMTable[dec->instructionIDs+1]; |
| 140 | return modRMTable[dec->instructionIDs]; |
Craig Topper | a0cd970 | 2012-02-09 08:58:07 +0000 | [diff] [blame] | 141 | case MODRM_SPLITREG: |
| 142 | if (modFromModRM(modRM) == 0x3) |
| 143 | return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)+8]; |
| 144 | return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)]; |
Craig Topper | 963305b | 2012-09-13 05:45:42 +0000 | [diff] [blame] | 145 | case MODRM_SPLITMISC: |
| 146 | if (modFromModRM(modRM) == 0x3) |
| 147 | return modRMTable[dec->instructionIDs+(modRM & 0x3f)+8]; |
| 148 | return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)]; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 149 | case MODRM_FULL: |
Craig Topper | 487e744 | 2012-02-09 07:45:30 +0000 | [diff] [blame] | 150 | return modRMTable[dec->instructionIDs+modRM]; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 151 | } |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /* |
| 155 | * specifierForUID - Given a UID, returns the name and operand specification for |
| 156 | * that instruction. |
| 157 | * |
| 158 | * @param uid - The unique ID for the instruction. This should be returned by |
| 159 | * decode(); specifierForUID will not check bounds. |
| 160 | * @return - A pointer to the specification for that instruction. |
| 161 | */ |
Benjamin Kramer | de0a4fb | 2010-10-23 09:10:44 +0000 | [diff] [blame] | 162 | static const struct InstructionSpecifier *specifierForUID(InstrUID uid) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 163 | return &INSTRUCTIONS_SYM[uid]; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * consumeByte - Uses the reader function provided by the user to consume one |
| 168 | * byte from the instruction's memory and advance the cursor. |
| 169 | * |
| 170 | * @param insn - The instruction with the reader function to use. The cursor |
| 171 | * for this instruction is advanced. |
| 172 | * @param byte - A pointer to a pre-allocated memory buffer to be populated |
| 173 | * with the data read. |
| 174 | * @return - 0 if the read was successful; nonzero otherwise. |
| 175 | */ |
Sean Callanan | 588785c | 2009-12-22 22:51:40 +0000 | [diff] [blame] | 176 | static int consumeByte(struct InternalInstruction* insn, uint8_t* byte) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 177 | int ret = insn->reader(insn->readerArg, byte, insn->readerCursor); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 178 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 179 | if (!ret) |
| 180 | ++(insn->readerCursor); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 181 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * lookAtByte - Like consumeByte, but does not advance the cursor. |
| 187 | * |
| 188 | * @param insn - See consumeByte(). |
| 189 | * @param byte - See consumeByte(). |
| 190 | * @return - See consumeByte(). |
| 191 | */ |
Sean Callanan | 588785c | 2009-12-22 22:51:40 +0000 | [diff] [blame] | 192 | static int lookAtByte(struct InternalInstruction* insn, uint8_t* byte) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 193 | return insn->reader(insn->readerArg, byte, insn->readerCursor); |
| 194 | } |
| 195 | |
Sean Callanan | 588785c | 2009-12-22 22:51:40 +0000 | [diff] [blame] | 196 | static void unconsumeByte(struct InternalInstruction* insn) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 197 | insn->readerCursor--; |
| 198 | } |
| 199 | |
Sean Callanan | 588785c | 2009-12-22 22:51:40 +0000 | [diff] [blame] | 200 | #define CONSUME_FUNC(name, type) \ |
| 201 | static int name(struct InternalInstruction* insn, type* ptr) { \ |
| 202 | type combined = 0; \ |
| 203 | unsigned offset; \ |
| 204 | for (offset = 0; offset < sizeof(type); ++offset) { \ |
| 205 | uint8_t byte; \ |
| 206 | int ret = insn->reader(insn->readerArg, \ |
| 207 | &byte, \ |
| 208 | insn->readerCursor + offset); \ |
| 209 | if (ret) \ |
| 210 | return ret; \ |
Richard Smith | 228e6d4 | 2012-08-24 23:29:28 +0000 | [diff] [blame] | 211 | combined = combined | ((uint64_t)byte << (offset * 8)); \ |
Sean Callanan | 588785c | 2009-12-22 22:51:40 +0000 | [diff] [blame] | 212 | } \ |
| 213 | *ptr = combined; \ |
| 214 | insn->readerCursor += sizeof(type); \ |
| 215 | return 0; \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* |
| 219 | * consume* - Use the reader function provided by the user to consume data |
| 220 | * values of various sizes from the instruction's memory and advance the |
| 221 | * cursor appropriately. These readers perform endian conversion. |
| 222 | * |
| 223 | * @param insn - See consumeByte(). |
| 224 | * @param ptr - A pointer to a pre-allocated memory of appropriate size to |
| 225 | * be populated with the data read. |
| 226 | * @return - See consumeByte(). |
| 227 | */ |
| 228 | CONSUME_FUNC(consumeInt8, int8_t) |
| 229 | CONSUME_FUNC(consumeInt16, int16_t) |
| 230 | CONSUME_FUNC(consumeInt32, int32_t) |
| 231 | CONSUME_FUNC(consumeUInt16, uint16_t) |
| 232 | CONSUME_FUNC(consumeUInt32, uint32_t) |
| 233 | CONSUME_FUNC(consumeUInt64, uint64_t) |
| 234 | |
| 235 | /* |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 236 | * 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] | 237 | * message, typically without a carriage-return. |
| 238 | * |
| 239 | * @param insn - The instruction containing the logging function. |
| 240 | * @param format - See printf(). |
| 241 | * @param ... - See printf(). |
| 242 | */ |
Sean Callanan | 588785c | 2009-12-22 22:51:40 +0000 | [diff] [blame] | 243 | static void dbgprintf(struct InternalInstruction* insn, |
| 244 | const char* format, |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 245 | ...) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 246 | char buffer[256]; |
| 247 | va_list ap; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 248 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 249 | if (!insn->dlog) |
| 250 | return; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 251 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 252 | va_start(ap, format); |
| 253 | (void)vsnprintf(buffer, sizeof(buffer), format, ap); |
| 254 | va_end(ap); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 255 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 256 | insn->dlog(insn->dlogArg, buffer); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 257 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * setPrefixPresent - Marks that a particular prefix is present at a particular |
| 263 | * location. |
| 264 | * |
| 265 | * @param insn - The instruction to be marked as having the prefix. |
| 266 | * @param prefix - The prefix that is present. |
| 267 | * @param location - The location where the prefix is located (in the address |
| 268 | * space of the instruction's reader). |
| 269 | */ |
Sean Callanan | 588785c | 2009-12-22 22:51:40 +0000 | [diff] [blame] | 270 | static void setPrefixPresent(struct InternalInstruction* insn, |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 271 | uint8_t prefix, |
| 272 | uint64_t location) |
| 273 | { |
| 274 | insn->prefixPresent[prefix] = 1; |
| 275 | insn->prefixLocations[prefix] = location; |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * isPrefixAtLocation - Queries an instruction to determine whether a prefix is |
| 280 | * present at a given location. |
| 281 | * |
| 282 | * @param insn - The instruction to be queried. |
| 283 | * @param prefix - The prefix. |
| 284 | * @param location - The location to query. |
| 285 | * @return - Whether the prefix is at that location. |
| 286 | */ |
Sean Callanan | 588785c | 2009-12-22 22:51:40 +0000 | [diff] [blame] | 287 | static BOOL isPrefixAtLocation(struct InternalInstruction* insn, |
| 288 | uint8_t prefix, |
| 289 | uint64_t location) |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 290 | { |
| 291 | if (insn->prefixPresent[prefix] == 1 && |
| 292 | insn->prefixLocations[prefix] == location) |
| 293 | return TRUE; |
| 294 | else |
| 295 | return FALSE; |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * readPrefixes - Consumes all of an instruction's prefix bytes, and marks the |
| 300 | * instruction as having them. Also sets the instruction's default operand, |
| 301 | * address, and other relevant data sizes to report operands correctly. |
| 302 | * |
| 303 | * @param insn - The instruction whose prefixes are to be read. |
| 304 | * @return - 0 if the instruction could be read until the end of the prefix |
| 305 | * bytes, and no prefixes conflicted; nonzero otherwise. |
| 306 | */ |
| 307 | static int readPrefixes(struct InternalInstruction* insn) { |
| 308 | BOOL isPrefix = TRUE; |
| 309 | BOOL prefixGroups[4] = { FALSE }; |
| 310 | uint64_t prefixLocation; |
Ted Kremenek | 3c4408c | 2011-01-23 17:05:06 +0000 | [diff] [blame] | 311 | uint8_t byte = 0; |
Richard Mitton | 79917a9 | 2013-08-30 21:32:42 +0000 | [diff] [blame] | 312 | uint8_t nextByte; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 313 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 314 | BOOL hasAdSize = FALSE; |
| 315 | BOOL hasOpSize = FALSE; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 316 | |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 317 | dbgprintf(insn, "readPrefixes()"); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 318 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 319 | while (isPrefix) { |
| 320 | prefixLocation = insn->readerCursor; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 321 | |
Richard Mitton | 576ee00 | 2013-08-30 21:19:48 +0000 | [diff] [blame] | 322 | /* 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] | 323 | if (consumeByte(insn, &byte)) |
Richard Mitton | 576ee00 | 2013-08-30 21:19:48 +0000 | [diff] [blame] | 324 | break; |
Kevin Enderby | 014e1cd | 2012-03-09 17:52:49 +0000 | [diff] [blame] | 325 | |
Benjamin Kramer | adfc73d | 2012-03-10 15:10:06 +0000 | [diff] [blame] | 326 | /* |
Dave Zarzycki | 07fabee | 2013-03-25 18:59:38 +0000 | [diff] [blame] | 327 | * If the byte is a LOCK/REP/REPNE prefix and not a part of the opcode, then |
| 328 | * break and let it be disassembled as a normal "instruction". |
Benjamin Kramer | adfc73d | 2012-03-10 15:10:06 +0000 | [diff] [blame] | 329 | */ |
Richard Mitton | 576ee00 | 2013-08-30 21:19:48 +0000 | [diff] [blame] | 330 | if (insn->readerCursor - 1 == insn->startLocation && byte == 0xf0) |
| 331 | break; |
| 332 | |
Dave Zarzycki | 07fabee | 2013-03-25 18:59:38 +0000 | [diff] [blame] | 333 | if (insn->readerCursor - 1 == insn->startLocation |
Richard Mitton | 576ee00 | 2013-08-30 21:19:48 +0000 | [diff] [blame] | 334 | && (byte == 0xf2 || byte == 0xf3) |
| 335 | && !lookAtByte(insn, &nextByte)) |
| 336 | { |
Kevin Enderby | 35fd792 | 2013-06-20 22:32:18 +0000 | [diff] [blame] | 337 | /* |
| 338 | * If the byte is 0xf2 or 0xf3, and any of the following conditions are |
| 339 | * met: |
| 340 | * - it is followed by a LOCK (0xf0) prefix |
| 341 | * - it is followed by an xchg instruction |
| 342 | * then it should be disassembled as a xacquire/xrelease not repne/rep. |
| 343 | */ |
| 344 | if ((byte == 0xf2 || byte == 0xf3) && |
| 345 | ((nextByte == 0xf0) | |
| 346 | ((nextByte & 0xfe) == 0x86 || (nextByte & 0xf8) == 0x90))) |
| 347 | insn->xAcquireRelease = TRUE; |
| 348 | /* |
| 349 | * Also if the byte is 0xf3, and the following condition is met: |
| 350 | * - it is followed by a "mov mem, reg" (opcode 0x88/0x89) or |
| 351 | * "mov mem, imm" (opcode 0xc6/0xc7) instructions. |
| 352 | * then it should be disassembled as an xrelease not rep. |
| 353 | */ |
| 354 | if (byte == 0xf3 && |
| 355 | (nextByte == 0x88 || nextByte == 0x89 || |
| 356 | nextByte == 0xc6 || nextByte == 0xc7)) |
| 357 | insn->xAcquireRelease = TRUE; |
Dave Zarzycki | 07fabee | 2013-03-25 18:59:38 +0000 | [diff] [blame] | 358 | if (insn->mode == MODE_64BIT && (nextByte & 0xf0) == 0x40) { |
| 359 | if (consumeByte(insn, &nextByte)) |
| 360 | return -1; |
| 361 | if (lookAtByte(insn, &nextByte)) |
| 362 | return -1; |
| 363 | unconsumeByte(insn); |
| 364 | } |
| 365 | if (nextByte != 0x0f && nextByte != 0x90) |
| 366 | break; |
| 367 | } |
| 368 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 369 | switch (byte) { |
| 370 | case 0xf0: /* LOCK */ |
| 371 | case 0xf2: /* REPNE/REPNZ */ |
| 372 | case 0xf3: /* REP or REPE/REPZ */ |
| 373 | if (prefixGroups[0]) |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 374 | dbgprintf(insn, "Redundant Group 1 prefix"); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 375 | prefixGroups[0] = TRUE; |
| 376 | setPrefixPresent(insn, byte, prefixLocation); |
| 377 | break; |
| 378 | case 0x2e: /* CS segment override -OR- Branch not taken */ |
| 379 | case 0x36: /* SS segment override -OR- Branch taken */ |
| 380 | case 0x3e: /* DS segment override */ |
| 381 | case 0x26: /* ES segment override */ |
| 382 | case 0x64: /* FS segment override */ |
| 383 | case 0x65: /* GS segment override */ |
| 384 | switch (byte) { |
| 385 | case 0x2e: |
| 386 | insn->segmentOverride = SEG_OVERRIDE_CS; |
| 387 | break; |
| 388 | case 0x36: |
| 389 | insn->segmentOverride = SEG_OVERRIDE_SS; |
| 390 | break; |
| 391 | case 0x3e: |
| 392 | insn->segmentOverride = SEG_OVERRIDE_DS; |
| 393 | break; |
| 394 | case 0x26: |
| 395 | insn->segmentOverride = SEG_OVERRIDE_ES; |
| 396 | break; |
| 397 | case 0x64: |
| 398 | insn->segmentOverride = SEG_OVERRIDE_FS; |
| 399 | break; |
| 400 | case 0x65: |
| 401 | insn->segmentOverride = SEG_OVERRIDE_GS; |
| 402 | break; |
| 403 | default: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 404 | debug("Unhandled override"); |
| 405 | return -1; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 406 | } |
| 407 | if (prefixGroups[1]) |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 408 | dbgprintf(insn, "Redundant Group 2 prefix"); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 409 | prefixGroups[1] = TRUE; |
| 410 | setPrefixPresent(insn, byte, prefixLocation); |
| 411 | break; |
| 412 | case 0x66: /* Operand-size override */ |
| 413 | if (prefixGroups[2]) |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 414 | dbgprintf(insn, "Redundant Group 3 prefix"); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 415 | prefixGroups[2] = TRUE; |
| 416 | hasOpSize = TRUE; |
| 417 | setPrefixPresent(insn, byte, prefixLocation); |
| 418 | break; |
| 419 | case 0x67: /* Address-size override */ |
| 420 | if (prefixGroups[3]) |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 421 | dbgprintf(insn, "Redundant Group 4 prefix"); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 422 | prefixGroups[3] = TRUE; |
| 423 | hasAdSize = TRUE; |
| 424 | setPrefixPresent(insn, byte, prefixLocation); |
| 425 | break; |
| 426 | default: /* Not a prefix byte */ |
| 427 | isPrefix = FALSE; |
| 428 | break; |
| 429 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 430 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 431 | if (isPrefix) |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 432 | dbgprintf(insn, "Found prefix 0x%hhx", byte); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 433 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 434 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 435 | insn->vectorExtensionType = TYPE_NO_VEX_XOP; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 436 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 437 | if (byte == 0x62) { |
| 438 | uint8_t byte1, byte2; |
| 439 | |
| 440 | if (consumeByte(insn, &byte1)) { |
| 441 | dbgprintf(insn, "Couldn't read second byte of EVEX prefix"); |
| 442 | return -1; |
| 443 | } |
| 444 | |
| 445 | if (lookAtByte(insn, &byte2)) { |
| 446 | dbgprintf(insn, "Couldn't read third byte of EVEX prefix"); |
| 447 | return -1; |
| 448 | } |
| 449 | |
| 450 | if ((insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) && |
| 451 | ((~byte1 & 0xc) == 0xc) && ((byte2 & 0x4) == 0x4)) { |
| 452 | insn->vectorExtensionType = TYPE_EVEX; |
| 453 | } |
| 454 | else { |
| 455 | unconsumeByte(insn); /* unconsume byte1 */ |
| 456 | unconsumeByte(insn); /* unconsume byte */ |
| 457 | insn->necessaryPrefixLocation = insn->readerCursor - 2; |
| 458 | } |
| 459 | |
| 460 | if (insn->vectorExtensionType == TYPE_EVEX) { |
| 461 | insn->vectorExtensionPrefix[0] = byte; |
| 462 | insn->vectorExtensionPrefix[1] = byte1; |
| 463 | if (consumeByte(insn, &insn->vectorExtensionPrefix[2])) { |
| 464 | dbgprintf(insn, "Couldn't read third byte of EVEX prefix"); |
| 465 | return -1; |
| 466 | } |
| 467 | if (consumeByte(insn, &insn->vectorExtensionPrefix[3])) { |
| 468 | dbgprintf(insn, "Couldn't read fourth byte of EVEX prefix"); |
| 469 | return -1; |
| 470 | } |
| 471 | |
| 472 | /* We simulate the REX prefix for simplicity's sake */ |
| 473 | if (insn->mode == MODE_64BIT) { |
| 474 | insn->rexPrefix = 0x40 |
| 475 | | (wFromEVEX3of4(insn->vectorExtensionPrefix[2]) << 3) |
| 476 | | (rFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 2) |
| 477 | | (xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 1) |
| 478 | | (bFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 0); |
| 479 | } |
| 480 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 481 | dbgprintf(insn, "Found EVEX prefix 0x%hhx 0x%hhx 0x%hhx 0x%hhx", |
| 482 | insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1], |
| 483 | insn->vectorExtensionPrefix[2], insn->vectorExtensionPrefix[3]); |
| 484 | } |
| 485 | } |
| 486 | else if (byte == 0xc4) { |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 487 | uint8_t byte1; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 488 | |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 489 | if (lookAtByte(insn, &byte1)) { |
| 490 | dbgprintf(insn, "Couldn't read second byte of VEX"); |
| 491 | return -1; |
| 492 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 493 | |
Craig Topper | 45faba9 | 2011-09-26 05:12:43 +0000 | [diff] [blame] | 494 | if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) { |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 495 | insn->vectorExtensionType = TYPE_VEX_3B; |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 496 | insn->necessaryPrefixLocation = insn->readerCursor - 1; |
| 497 | } |
| 498 | else { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 499 | unconsumeByte(insn); |
| 500 | insn->necessaryPrefixLocation = insn->readerCursor - 1; |
| 501 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 502 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 503 | if (insn->vectorExtensionType == TYPE_VEX_3B) { |
| 504 | insn->vectorExtensionPrefix[0] = byte; |
| 505 | consumeByte(insn, &insn->vectorExtensionPrefix[1]); |
| 506 | consumeByte(insn, &insn->vectorExtensionPrefix[2]); |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 507 | |
| 508 | /* We simulate the REX prefix for simplicity's sake */ |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 509 | |
Craig Topper | 31854ba | 2011-10-03 07:51:09 +0000 | [diff] [blame] | 510 | if (insn->mode == MODE_64BIT) { |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 511 | insn->rexPrefix = 0x40 |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 512 | | (wFromVEX3of3(insn->vectorExtensionPrefix[2]) << 3) |
| 513 | | (rFromVEX2of3(insn->vectorExtensionPrefix[1]) << 2) |
| 514 | | (xFromVEX2of3(insn->vectorExtensionPrefix[1]) << 1) |
| 515 | | (bFromVEX2of3(insn->vectorExtensionPrefix[1]) << 0); |
Craig Topper | 31854ba | 2011-10-03 07:51:09 +0000 | [diff] [blame] | 516 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 517 | |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 518 | dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx 0x%hhx", |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 519 | insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1], |
| 520 | insn->vectorExtensionPrefix[2]); |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 521 | } |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 522 | } |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 523 | else if (byte == 0xc5) { |
| 524 | uint8_t byte1; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 525 | |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 526 | if (lookAtByte(insn, &byte1)) { |
| 527 | dbgprintf(insn, "Couldn't read second byte of VEX"); |
| 528 | return -1; |
| 529 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 530 | |
Craig Topper | 45faba9 | 2011-09-26 05:12:43 +0000 | [diff] [blame] | 531 | if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) { |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 532 | insn->vectorExtensionType = TYPE_VEX_2B; |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 533 | } |
| 534 | else { |
| 535 | unconsumeByte(insn); |
| 536 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 537 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 538 | if (insn->vectorExtensionType == TYPE_VEX_2B) { |
| 539 | insn->vectorExtensionPrefix[0] = byte; |
| 540 | consumeByte(insn, &insn->vectorExtensionPrefix[1]); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 541 | |
Craig Topper | 31854ba | 2011-10-03 07:51:09 +0000 | [diff] [blame] | 542 | if (insn->mode == MODE_64BIT) { |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 543 | insn->rexPrefix = 0x40 |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 544 | | (rFromVEX2of2(insn->vectorExtensionPrefix[1]) << 2); |
Craig Topper | 31854ba | 2011-10-03 07:51:09 +0000 | [diff] [blame] | 545 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 546 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 547 | switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 548 | { |
| 549 | default: |
| 550 | break; |
| 551 | case VEX_PREFIX_66: |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 552 | hasOpSize = TRUE; |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 553 | break; |
| 554 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 555 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 556 | dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx", |
| 557 | insn->vectorExtensionPrefix[0], |
| 558 | insn->vectorExtensionPrefix[1]); |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | else if (byte == 0x8f) { |
| 562 | uint8_t byte1; |
| 563 | |
| 564 | if (lookAtByte(insn, &byte1)) { |
| 565 | dbgprintf(insn, "Couldn't read second byte of XOP"); |
| 566 | return -1; |
| 567 | } |
| 568 | |
Craig Topper | 9eb8837 | 2013-10-03 06:29:59 +0000 | [diff] [blame] | 569 | 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] | 570 | insn->vectorExtensionType = TYPE_XOP; |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 571 | insn->necessaryPrefixLocation = insn->readerCursor - 1; |
| 572 | } |
| 573 | else { |
| 574 | unconsumeByte(insn); |
| 575 | insn->necessaryPrefixLocation = insn->readerCursor - 1; |
| 576 | } |
| 577 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 578 | if (insn->vectorExtensionType == TYPE_XOP) { |
| 579 | insn->vectorExtensionPrefix[0] = byte; |
| 580 | consumeByte(insn, &insn->vectorExtensionPrefix[1]); |
| 581 | consumeByte(insn, &insn->vectorExtensionPrefix[2]); |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 582 | |
| 583 | /* We simulate the REX prefix for simplicity's sake */ |
| 584 | |
| 585 | if (insn->mode == MODE_64BIT) { |
| 586 | insn->rexPrefix = 0x40 |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 587 | | (wFromXOP3of3(insn->vectorExtensionPrefix[2]) << 3) |
| 588 | | (rFromXOP2of3(insn->vectorExtensionPrefix[1]) << 2) |
| 589 | | (xFromXOP2of3(insn->vectorExtensionPrefix[1]) << 1) |
| 590 | | (bFromXOP2of3(insn->vectorExtensionPrefix[1]) << 0); |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 593 | switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 594 | { |
| 595 | default: |
| 596 | break; |
| 597 | case VEX_PREFIX_66: |
| 598 | hasOpSize = TRUE; |
| 599 | break; |
| 600 | } |
| 601 | |
| 602 | dbgprintf(insn, "Found XOP prefix 0x%hhx 0x%hhx 0x%hhx", |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 603 | insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1], |
| 604 | insn->vectorExtensionPrefix[2]); |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | else { |
| 608 | if (insn->mode == MODE_64BIT) { |
| 609 | if ((byte & 0xf0) == 0x40) { |
| 610 | uint8_t opcodeByte; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 611 | |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 612 | if (lookAtByte(insn, &opcodeByte) || ((opcodeByte & 0xf0) == 0x40)) { |
| 613 | dbgprintf(insn, "Redundant REX prefix"); |
| 614 | return -1; |
| 615 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 616 | |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 617 | insn->rexPrefix = byte; |
| 618 | insn->necessaryPrefixLocation = insn->readerCursor - 2; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 619 | |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 620 | dbgprintf(insn, "Found REX prefix 0x%hhx", byte); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 621 | } else { |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 622 | unconsumeByte(insn); |
| 623 | insn->necessaryPrefixLocation = insn->readerCursor - 1; |
| 624 | } |
| 625 | } else { |
| 626 | unconsumeByte(insn); |
| 627 | insn->necessaryPrefixLocation = insn->readerCursor - 1; |
| 628 | } |
| 629 | } |
| 630 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 631 | if (insn->mode == MODE_16BIT) { |
| 632 | insn->registerSize = (hasOpSize ? 4 : 2); |
| 633 | insn->addressSize = (hasAdSize ? 4 : 2); |
| 634 | insn->displacementSize = (hasAdSize ? 4 : 2); |
| 635 | insn->immediateSize = (hasOpSize ? 4 : 2); |
| 636 | } else if (insn->mode == MODE_32BIT) { |
| 637 | insn->registerSize = (hasOpSize ? 2 : 4); |
| 638 | insn->addressSize = (hasAdSize ? 2 : 4); |
| 639 | insn->displacementSize = (hasAdSize ? 2 : 4); |
Sean Callanan | 9f6c622 | 2010-10-22 01:24:11 +0000 | [diff] [blame] | 640 | insn->immediateSize = (hasOpSize ? 2 : 4); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 641 | } else if (insn->mode == MODE_64BIT) { |
| 642 | if (insn->rexPrefix && wFromREX(insn->rexPrefix)) { |
| 643 | insn->registerSize = 8; |
| 644 | insn->addressSize = (hasAdSize ? 4 : 8); |
| 645 | insn->displacementSize = 4; |
| 646 | insn->immediateSize = 4; |
| 647 | } else if (insn->rexPrefix) { |
| 648 | insn->registerSize = (hasOpSize ? 2 : 4); |
| 649 | insn->addressSize = (hasAdSize ? 4 : 8); |
| 650 | insn->displacementSize = (hasOpSize ? 2 : 4); |
| 651 | insn->immediateSize = (hasOpSize ? 2 : 4); |
| 652 | } else { |
| 653 | insn->registerSize = (hasOpSize ? 2 : 4); |
| 654 | insn->addressSize = (hasAdSize ? 4 : 8); |
| 655 | insn->displacementSize = (hasOpSize ? 2 : 4); |
| 656 | insn->immediateSize = (hasOpSize ? 2 : 4); |
| 657 | } |
| 658 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 659 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * readOpcode - Reads the opcode (excepting the ModR/M byte in the case of |
| 665 | * extended or escape opcodes). |
| 666 | * |
| 667 | * @param insn - The instruction whose opcode is to be read. |
| 668 | * @return - 0 if the opcode could be read successfully; nonzero otherwise. |
| 669 | */ |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 670 | static int readOpcode(struct InternalInstruction* insn) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 671 | /* Determine the length of the primary opcode */ |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 672 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 673 | uint8_t current; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 674 | |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 675 | dbgprintf(insn, "readOpcode()"); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 676 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 677 | insn->opcodeType = ONEBYTE; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 678 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 679 | if (insn->vectorExtensionType == TYPE_EVEX) |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 680 | { |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 681 | switch (mmFromEVEX2of4(insn->vectorExtensionPrefix[1])) { |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 682 | default: |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 683 | dbgprintf(insn, "Unhandled mm field for instruction (0x%hhx)", |
| 684 | mmFromEVEX2of4(insn->vectorExtensionPrefix[1])); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 685 | return -1; |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 686 | case VEX_LOB_0F: |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 687 | insn->opcodeType = TWOBYTE; |
| 688 | return consumeByte(insn, &insn->opcode); |
| 689 | case VEX_LOB_0F38: |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 690 | insn->opcodeType = THREEBYTE_38; |
| 691 | return consumeByte(insn, &insn->opcode); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 692 | case VEX_LOB_0F3A: |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 693 | insn->opcodeType = THREEBYTE_3A; |
| 694 | return consumeByte(insn, &insn->opcode); |
| 695 | } |
| 696 | } |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 697 | else if (insn->vectorExtensionType == TYPE_VEX_3B) { |
| 698 | switch (mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])) { |
| 699 | default: |
| 700 | dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)", |
| 701 | mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])); |
| 702 | return -1; |
| 703 | case VEX_LOB_0F: |
| 704 | insn->opcodeType = TWOBYTE; |
| 705 | return consumeByte(insn, &insn->opcode); |
| 706 | case VEX_LOB_0F38: |
| 707 | insn->opcodeType = THREEBYTE_38; |
| 708 | return consumeByte(insn, &insn->opcode); |
| 709 | case VEX_LOB_0F3A: |
| 710 | insn->opcodeType = THREEBYTE_3A; |
| 711 | return consumeByte(insn, &insn->opcode); |
| 712 | } |
| 713 | } |
| 714 | else if (insn->vectorExtensionType == TYPE_VEX_2B) { |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 715 | insn->opcodeType = TWOBYTE; |
| 716 | return consumeByte(insn, &insn->opcode); |
| 717 | } |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 718 | else if (insn->vectorExtensionType == TYPE_XOP) { |
| 719 | switch (mmmmmFromXOP2of3(insn->vectorExtensionPrefix[1])) { |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 720 | default: |
| 721 | dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)", |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 722 | mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])); |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 723 | return -1; |
| 724 | case XOP_MAP_SELECT_8: |
| 725 | insn->opcodeType = XOP8_MAP; |
| 726 | return consumeByte(insn, &insn->opcode); |
| 727 | case XOP_MAP_SELECT_9: |
| 728 | insn->opcodeType = XOP9_MAP; |
| 729 | return consumeByte(insn, &insn->opcode); |
| 730 | case XOP_MAP_SELECT_A: |
| 731 | insn->opcodeType = XOPA_MAP; |
| 732 | return consumeByte(insn, &insn->opcode); |
| 733 | } |
| 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 | } |
| 764 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 765 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 766 | /* |
| 767 | * At this point we have consumed the full opcode. |
| 768 | * Anything we consume from here on must be unconsumed. |
| 769 | */ |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 770 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 771 | insn->opcode = current; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 772 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | static int readModRM(struct InternalInstruction* insn); |
| 777 | |
| 778 | /* |
| 779 | * getIDWithAttrMask - Determines the ID of an instruction, consuming |
| 780 | * the ModR/M byte as appropriate for extended and escape opcodes, |
| 781 | * and using a supplied attribute mask. |
| 782 | * |
| 783 | * @param instructionID - A pointer whose target is filled in with the ID of the |
| 784 | * instruction. |
| 785 | * @param insn - The instruction whose ID is to be determined. |
| 786 | * @param attrMask - The attribute mask to search. |
| 787 | * @return - 0 if the ModR/M could be read when needed or was not |
| 788 | * needed; nonzero otherwise. |
| 789 | */ |
| 790 | static int getIDWithAttrMask(uint16_t* instructionID, |
| 791 | struct InternalInstruction* insn, |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 792 | uint16_t attrMask) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 793 | BOOL hasModRMExtension; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 794 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 795 | uint16_t instructionClass; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 796 | |
| 797 | instructionClass = contextForAttrs(attrMask); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 798 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 799 | hasModRMExtension = modRMRequired(insn->opcodeType, |
| 800 | instructionClass, |
| 801 | insn->opcode); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 802 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 803 | if (hasModRMExtension) { |
Rafael Espindola | 9f9a106 | 2011-01-06 16:48:42 +0000 | [diff] [blame] | 804 | if (readModRM(insn)) |
| 805 | return -1; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 806 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 807 | *instructionID = decode(insn->opcodeType, |
| 808 | instructionClass, |
| 809 | insn->opcode, |
| 810 | insn->modRM); |
| 811 | } else { |
| 812 | *instructionID = decode(insn->opcodeType, |
| 813 | instructionClass, |
| 814 | insn->opcode, |
| 815 | 0); |
| 816 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 817 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | /* |
| 822 | * is16BitEquivalent - Determines whether two instruction names refer to |
| 823 | * equivalent instructions but one is 16-bit whereas the other is not. |
| 824 | * |
| 825 | * @param orig - The instruction that is not 16-bit |
| 826 | * @param equiv - The instruction that is 16-bit |
| 827 | */ |
Joerg Sonnenberger | 2b86e48 | 2012-10-29 17:56:15 +0000 | [diff] [blame] | 828 | static BOOL is16BitEquivalent(const char* orig, const char* equiv) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 829 | off_t i; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 830 | |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 831 | for (i = 0;; i++) { |
| 832 | if (orig[i] == '\0' && equiv[i] == '\0') |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 833 | return TRUE; |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 834 | if (orig[i] == '\0' || equiv[i] == '\0') |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 835 | return FALSE; |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 836 | if (orig[i] != equiv[i]) { |
| 837 | if ((orig[i] == 'Q' || orig[i] == 'L') && equiv[i] == 'W') |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 838 | continue; |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 839 | if ((orig[i] == '6' || orig[i] == '3') && equiv[i] == '1') |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 840 | continue; |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 841 | if ((orig[i] == '4' || orig[i] == '2') && equiv[i] == '6') |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 842 | continue; |
| 843 | return FALSE; |
| 844 | } |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | /* |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 849 | * getID - Determines the ID of an instruction, consuming the ModR/M byte as |
| 850 | * appropriate for extended and escape opcodes. Determines the attributes and |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 851 | * context for the instruction before doing so. |
| 852 | * |
| 853 | * @param insn - The instruction whose ID is to be determined. |
| 854 | * @return - 0 if the ModR/M could be read when needed or was not needed; |
| 855 | * nonzero otherwise. |
| 856 | */ |
Roman Divacky | 6792380 | 2012-09-05 21:17:34 +0000 | [diff] [blame] | 857 | static int getID(struct InternalInstruction* insn, const void *miiArg) { |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 858 | uint16_t attrMask; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 859 | uint16_t instructionID; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 860 | |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 861 | dbgprintf(insn, "getID()"); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 862 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 863 | attrMask = ATTR_NONE; |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 864 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 865 | if (insn->mode == MODE_64BIT) |
| 866 | attrMask |= ATTR_64BIT; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 867 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 868 | if (insn->vectorExtensionType != TYPE_NO_VEX_XOP) { |
| 869 | attrMask |= (insn->vectorExtensionType == TYPE_EVEX) ? ATTR_EVEX : ATTR_VEX; |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 870 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 871 | if (insn->vectorExtensionType == TYPE_EVEX) { |
| 872 | switch (ppFromEVEX3of4(insn->vectorExtensionPrefix[2])) { |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 873 | case VEX_PREFIX_66: |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 874 | attrMask |= ATTR_OPSIZE; |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 875 | break; |
| 876 | case VEX_PREFIX_F3: |
| 877 | attrMask |= ATTR_XS; |
| 878 | break; |
| 879 | case VEX_PREFIX_F2: |
| 880 | attrMask |= ATTR_XD; |
| 881 | break; |
| 882 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 883 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 884 | if (zFromEVEX4of4(insn->vectorExtensionPrefix[3])) |
| 885 | attrMask |= ATTR_EVEXKZ; |
| 886 | if (bFromEVEX4of4(insn->vectorExtensionPrefix[3])) |
| 887 | attrMask |= ATTR_EVEXB; |
| 888 | if (aaaFromEVEX4of4(insn->vectorExtensionPrefix[3])) |
| 889 | attrMask |= ATTR_EVEXK; |
| 890 | if (lFromEVEX4of4(insn->vectorExtensionPrefix[3])) |
| 891 | attrMask |= ATTR_EVEXL; |
| 892 | if (l2FromEVEX4of4(insn->vectorExtensionPrefix[3])) |
| 893 | attrMask |= ATTR_EVEXL2; |
| 894 | } |
| 895 | else if (insn->vectorExtensionType == TYPE_VEX_3B) { |
| 896 | switch (ppFromVEX3of3(insn->vectorExtensionPrefix[2])) { |
| 897 | case VEX_PREFIX_66: |
| 898 | attrMask |= ATTR_OPSIZE; |
| 899 | break; |
| 900 | case VEX_PREFIX_F3: |
| 901 | attrMask |= ATTR_XS; |
| 902 | break; |
| 903 | case VEX_PREFIX_F2: |
| 904 | attrMask |= ATTR_XD; |
| 905 | break; |
| 906 | } |
| 907 | |
| 908 | if (lFromVEX3of3(insn->vectorExtensionPrefix[2])) |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 909 | attrMask |= ATTR_VEXL; |
| 910 | } |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 911 | else if (insn->vectorExtensionType == TYPE_VEX_2B) { |
| 912 | switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) { |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 913 | case VEX_PREFIX_66: |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 914 | attrMask |= ATTR_OPSIZE; |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 915 | break; |
| 916 | case VEX_PREFIX_F3: |
| 917 | attrMask |= ATTR_XS; |
| 918 | break; |
| 919 | case VEX_PREFIX_F2: |
| 920 | attrMask |= ATTR_XD; |
| 921 | break; |
| 922 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 923 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 924 | if (lFromVEX2of2(insn->vectorExtensionPrefix[1])) |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 925 | attrMask |= ATTR_VEXL; |
| 926 | } |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 927 | else if (insn->vectorExtensionType == TYPE_XOP) { |
| 928 | switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) { |
Craig Topper | 9e3e38a | 2013-10-03 05:17:48 +0000 | [diff] [blame] | 929 | case VEX_PREFIX_66: |
| 930 | attrMask |= ATTR_OPSIZE; |
| 931 | break; |
| 932 | case VEX_PREFIX_F3: |
| 933 | attrMask |= ATTR_XS; |
| 934 | break; |
| 935 | case VEX_PREFIX_F2: |
| 936 | attrMask |= ATTR_XD; |
| 937 | break; |
| 938 | } |
| 939 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 940 | if (lFromXOP3of3(insn->vectorExtensionPrefix[2])) |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 941 | attrMask |= ATTR_VEXL; |
| 942 | } |
| 943 | else { |
| 944 | return -1; |
| 945 | } |
| 946 | } |
| 947 | else { |
David Woodhouse | 5cf4c67 | 2014-01-20 12:02:35 +0000 | [diff] [blame] | 948 | if (insn->mode != MODE_16BIT && isPrefixAtLocation(insn, 0x66, insn->necessaryPrefixLocation)) |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 949 | attrMask |= ATTR_OPSIZE; |
Craig Topper | 6491c80 | 2012-02-27 01:54:29 +0000 | [diff] [blame] | 950 | else if (isPrefixAtLocation(insn, 0x67, insn->necessaryPrefixLocation)) |
| 951 | attrMask |= ATTR_ADSIZE; |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 952 | else if (isPrefixAtLocation(insn, 0xf3, insn->necessaryPrefixLocation)) |
| 953 | attrMask |= ATTR_XS; |
| 954 | else if (isPrefixAtLocation(insn, 0xf2, insn->necessaryPrefixLocation)) |
| 955 | attrMask |= ATTR_XD; |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 956 | } |
| 957 | |
Craig Topper | f18c896 | 2011-10-04 06:30:42 +0000 | [diff] [blame] | 958 | if (insn->rexPrefix & 0x08) |
| 959 | attrMask |= ATTR_REXW; |
Craig Topper | f01f1b5 | 2011-11-06 23:04:08 +0000 | [diff] [blame] | 960 | |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 961 | if (getIDWithAttrMask(&instructionID, insn, attrMask)) |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 962 | return -1; |
Craig Topper | f01f1b5 | 2011-11-06 23:04:08 +0000 | [diff] [blame] | 963 | |
David Woodhouse | 9c74fdb | 2014-01-20 12:02:48 +0000 | [diff] [blame] | 964 | /* |
| 965 | * JCXZ/JECXZ need special handling for 16-bit mode because the meaning |
| 966 | * of the AdSize prefix is inverted w.r.t. 32-bit mode. |
| 967 | */ |
| 968 | if (insn->mode == MODE_16BIT && insn->opcode == 0xE3) { |
| 969 | const struct InstructionSpecifier *spec; |
| 970 | spec = specifierForUID(instructionID); |
| 971 | |
| 972 | /* |
| 973 | * Check for Ii8PCRel instructions. We could alternatively do a |
| 974 | * string-compare on the names, but this is probably cheaper. |
| 975 | */ |
| 976 | if (x86OperandSets[spec->operands][0].type == TYPE_REL8) { |
| 977 | attrMask ^= ATTR_ADSIZE; |
| 978 | if (getIDWithAttrMask(&instructionID, insn, attrMask)) |
| 979 | return -1; |
| 980 | } |
| 981 | } |
| 982 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 983 | /* The following clauses compensate for limitations of the tables. */ |
Craig Topper | f01f1b5 | 2011-11-06 23:04:08 +0000 | [diff] [blame] | 984 | |
David Woodhouse | 5cf4c67 | 2014-01-20 12:02:35 +0000 | [diff] [blame] | 985 | if ((insn->mode == MODE_16BIT || insn->prefixPresent[0x66]) && |
| 986 | !(attrMask & ATTR_OPSIZE)) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 987 | /* |
| 988 | * The instruction tables make no distinction between instructions that |
| 989 | * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a |
| 990 | * particular spot (i.e., many MMX operations). In general we're |
| 991 | * conservative, but in the specific case where OpSize is present but not |
| 992 | * in the right place we check if there's a 16-bit operation. |
| 993 | */ |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 994 | |
Benjamin Kramer | de0a4fb | 2010-10-23 09:10:44 +0000 | [diff] [blame] | 995 | const struct InstructionSpecifier *spec; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 996 | uint16_t instructionIDWithOpsize; |
Benjamin Kramer | 915e3d9 | 2012-02-11 16:01:02 +0000 | [diff] [blame] | 997 | const char *specName, *specWithOpSizeName; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 998 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 999 | spec = specifierForUID(instructionID); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1000 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1001 | if (getIDWithAttrMask(&instructionIDWithOpsize, |
| 1002 | insn, |
| 1003 | attrMask | ATTR_OPSIZE)) { |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1004 | /* |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1005 | * ModRM required with OpSize but not present; give up and return version |
| 1006 | * without OpSize set |
| 1007 | */ |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1008 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1009 | insn->instructionID = instructionID; |
| 1010 | insn->spec = spec; |
| 1011 | return 0; |
| 1012 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1013 | |
Benjamin Kramer | 915e3d9 | 2012-02-11 16:01:02 +0000 | [diff] [blame] | 1014 | specName = x86DisassemblerGetInstrName(instructionID, miiArg); |
| 1015 | specWithOpSizeName = |
Benjamin Kramer | 478e8de | 2012-02-11 14:50:54 +0000 | [diff] [blame] | 1016 | x86DisassemblerGetInstrName(instructionIDWithOpsize, miiArg); |
| 1017 | |
David Woodhouse | 5cf4c67 | 2014-01-20 12:02:35 +0000 | [diff] [blame] | 1018 | if (is16BitEquivalent(specName, specWithOpSizeName) && |
| 1019 | (insn->mode == MODE_16BIT) ^ insn->prefixPresent[0x66]) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1020 | insn->instructionID = instructionIDWithOpsize; |
Benjamin Kramer | 915e3d9 | 2012-02-11 16:01:02 +0000 | [diff] [blame] | 1021 | insn->spec = specifierForUID(instructionIDWithOpsize); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1022 | } else { |
| 1023 | insn->instructionID = instructionID; |
| 1024 | insn->spec = spec; |
| 1025 | } |
| 1026 | return 0; |
| 1027 | } |
Craig Topper | 21c3365 | 2011-10-02 16:56:09 +0000 | [diff] [blame] | 1028 | |
| 1029 | if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 && |
| 1030 | insn->rexPrefix & 0x01) { |
| 1031 | /* |
| 1032 | * NOOP shouldn't decode as NOOP if REX.b is set. Instead |
| 1033 | * it should decode as XCHG %r8, %eax. |
| 1034 | */ |
| 1035 | |
| 1036 | const struct InstructionSpecifier *spec; |
| 1037 | uint16_t instructionIDWithNewOpcode; |
| 1038 | const struct InstructionSpecifier *specWithNewOpcode; |
| 1039 | |
| 1040 | spec = specifierForUID(instructionID); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1041 | |
Craig Topper | b58a966 | 2011-10-05 03:29:32 +0000 | [diff] [blame] | 1042 | /* Borrow opcode from one of the other XCHGar opcodes */ |
Craig Topper | 21c3365 | 2011-10-02 16:56:09 +0000 | [diff] [blame] | 1043 | insn->opcode = 0x91; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1044 | |
Craig Topper | 21c3365 | 2011-10-02 16:56:09 +0000 | [diff] [blame] | 1045 | if (getIDWithAttrMask(&instructionIDWithNewOpcode, |
| 1046 | insn, |
| 1047 | attrMask)) { |
| 1048 | insn->opcode = 0x90; |
| 1049 | |
| 1050 | insn->instructionID = instructionID; |
| 1051 | insn->spec = spec; |
| 1052 | return 0; |
| 1053 | } |
| 1054 | |
| 1055 | specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode); |
| 1056 | |
Craig Topper | b58a966 | 2011-10-05 03:29:32 +0000 | [diff] [blame] | 1057 | /* Change back */ |
Craig Topper | 21c3365 | 2011-10-02 16:56:09 +0000 | [diff] [blame] | 1058 | insn->opcode = 0x90; |
| 1059 | |
| 1060 | insn->instructionID = instructionIDWithNewOpcode; |
| 1061 | insn->spec = specWithNewOpcode; |
| 1062 | |
| 1063 | return 0; |
| 1064 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1065 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1066 | insn->instructionID = instructionID; |
| 1067 | insn->spec = specifierForUID(insn->instructionID); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1068 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1069 | return 0; |
| 1070 | } |
| 1071 | |
| 1072 | /* |
| 1073 | * readSIB - Consumes the SIB byte to determine addressing information for an |
| 1074 | * instruction. |
| 1075 | * |
| 1076 | * @param insn - The instruction whose SIB byte is to be read. |
| 1077 | * @return - 0 if the SIB byte was successfully read; nonzero otherwise. |
| 1078 | */ |
| 1079 | static int readSIB(struct InternalInstruction* insn) { |
Daniel Dunbar | 8b532de | 2009-12-22 01:41:37 +0000 | [diff] [blame] | 1080 | SIBIndex sibIndexBase = 0; |
| 1081 | SIBBase sibBaseBase = 0; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1082 | uint8_t index, base; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1083 | |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 1084 | dbgprintf(insn, "readSIB()"); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1085 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1086 | if (insn->consumedSIB) |
| 1087 | return 0; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1088 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1089 | insn->consumedSIB = TRUE; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1090 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1091 | switch (insn->addressSize) { |
| 1092 | case 2: |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 1093 | dbgprintf(insn, "SIB-based addressing doesn't work in 16-bit mode"); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1094 | return -1; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1095 | case 4: |
| 1096 | sibIndexBase = SIB_INDEX_EAX; |
| 1097 | sibBaseBase = SIB_BASE_EAX; |
| 1098 | break; |
| 1099 | case 8: |
| 1100 | sibIndexBase = SIB_INDEX_RAX; |
| 1101 | sibBaseBase = SIB_BASE_RAX; |
| 1102 | break; |
| 1103 | } |
| 1104 | |
| 1105 | if (consumeByte(insn, &insn->sib)) |
| 1106 | return -1; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1107 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1108 | index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3); |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 1109 | if (insn->vectorExtensionType == TYPE_EVEX) |
| 1110 | index |= v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1111 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1112 | switch (index) { |
| 1113 | case 0x4: |
| 1114 | insn->sibIndex = SIB_INDEX_NONE; |
| 1115 | break; |
| 1116 | default: |
Benjamin Kramer | 25bddae | 2011-02-27 18:13:53 +0000 | [diff] [blame] | 1117 | insn->sibIndex = (SIBIndex)(sibIndexBase + index); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1118 | if (insn->sibIndex == SIB_INDEX_sib || |
| 1119 | insn->sibIndex == SIB_INDEX_sib64) |
| 1120 | insn->sibIndex = SIB_INDEX_NONE; |
| 1121 | break; |
| 1122 | } |
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 | switch (scaleFromSIB(insn->sib)) { |
| 1125 | case 0: |
| 1126 | insn->sibScale = 1; |
| 1127 | break; |
| 1128 | case 1: |
| 1129 | insn->sibScale = 2; |
| 1130 | break; |
| 1131 | case 2: |
| 1132 | insn->sibScale = 4; |
| 1133 | break; |
| 1134 | case 3: |
| 1135 | insn->sibScale = 8; |
| 1136 | break; |
| 1137 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1138 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1139 | base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1140 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1141 | switch (base) { |
| 1142 | case 0x5: |
Craig Topper | fae5ac2 | 2014-02-17 10:03:43 +0000 | [diff] [blame] | 1143 | case 0xd: |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1144 | switch (modFromModRM(insn->modRM)) { |
| 1145 | case 0x0: |
| 1146 | insn->eaDisplacement = EA_DISP_32; |
| 1147 | insn->sibBase = SIB_BASE_NONE; |
| 1148 | break; |
| 1149 | case 0x1: |
| 1150 | insn->eaDisplacement = EA_DISP_8; |
Craig Topper | fae5ac2 | 2014-02-17 10:03:43 +0000 | [diff] [blame] | 1151 | insn->sibBase = (SIBBase)(sibBaseBase + base); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1152 | break; |
| 1153 | case 0x2: |
| 1154 | insn->eaDisplacement = EA_DISP_32; |
Craig Topper | fae5ac2 | 2014-02-17 10:03:43 +0000 | [diff] [blame] | 1155 | insn->sibBase = (SIBBase)(sibBaseBase + base); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1156 | break; |
| 1157 | case 0x3: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1158 | debug("Cannot have Mod = 0b11 and a SIB byte"); |
| 1159 | return -1; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1160 | } |
| 1161 | break; |
| 1162 | default: |
Benjamin Kramer | 25bddae | 2011-02-27 18:13:53 +0000 | [diff] [blame] | 1163 | insn->sibBase = (SIBBase)(sibBaseBase + base); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1164 | break; |
| 1165 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1166 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1167 | return 0; |
| 1168 | } |
| 1169 | |
| 1170 | /* |
| 1171 | * readDisplacement - Consumes the displacement of an instruction. |
| 1172 | * |
| 1173 | * @param insn - The instruction whose displacement is to be read. |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1174 | * @return - 0 if the displacement byte was successfully read; nonzero |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1175 | * otherwise. |
| 1176 | */ |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1177 | static int readDisplacement(struct InternalInstruction* insn) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1178 | int8_t d8; |
| 1179 | int16_t d16; |
| 1180 | int32_t d32; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1181 | |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 1182 | dbgprintf(insn, "readDisplacement()"); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1183 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1184 | if (insn->consumedDisplacement) |
| 1185 | return 0; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1186 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1187 | insn->consumedDisplacement = TRUE; |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 1188 | insn->displacementOffset = insn->readerCursor - insn->startLocation; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1189 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1190 | switch (insn->eaDisplacement) { |
| 1191 | case EA_DISP_NONE: |
| 1192 | insn->consumedDisplacement = FALSE; |
| 1193 | break; |
| 1194 | case EA_DISP_8: |
| 1195 | if (consumeInt8(insn, &d8)) |
| 1196 | return -1; |
| 1197 | insn->displacement = d8; |
| 1198 | break; |
| 1199 | case EA_DISP_16: |
| 1200 | if (consumeInt16(insn, &d16)) |
| 1201 | return -1; |
| 1202 | insn->displacement = d16; |
| 1203 | break; |
| 1204 | case EA_DISP_32: |
| 1205 | if (consumeInt32(insn, &d32)) |
| 1206 | return -1; |
| 1207 | insn->displacement = d32; |
| 1208 | break; |
| 1209 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1210 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1211 | insn->consumedDisplacement = TRUE; |
| 1212 | return 0; |
| 1213 | } |
| 1214 | |
| 1215 | /* |
| 1216 | * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and |
| 1217 | * displacement) for an instruction and interprets it. |
| 1218 | * |
| 1219 | * @param insn - The instruction whose addressing information is to be read. |
| 1220 | * @return - 0 if the information was successfully read; nonzero otherwise. |
| 1221 | */ |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1222 | static int readModRM(struct InternalInstruction* insn) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1223 | uint8_t mod, rm, reg; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1224 | |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 1225 | dbgprintf(insn, "readModRM()"); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1226 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1227 | if (insn->consumedModRM) |
| 1228 | return 0; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1229 | |
Rafael Espindola | 9f9a106 | 2011-01-06 16:48:42 +0000 | [diff] [blame] | 1230 | if (consumeByte(insn, &insn->modRM)) |
| 1231 | return -1; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1232 | insn->consumedModRM = TRUE; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1233 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1234 | mod = modFromModRM(insn->modRM); |
| 1235 | rm = rmFromModRM(insn->modRM); |
| 1236 | reg = regFromModRM(insn->modRM); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1237 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1238 | /* |
| 1239 | * This goes by insn->registerSize to pick the correct register, which messes |
| 1240 | * up if we're using (say) XMM or 8-bit register operands. That gets fixed in |
| 1241 | * fixupReg(). |
| 1242 | */ |
| 1243 | switch (insn->registerSize) { |
| 1244 | case 2: |
Sean Callanan | 2f9443f | 2009-12-22 02:07:42 +0000 | [diff] [blame] | 1245 | insn->regBase = MODRM_REG_AX; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1246 | insn->eaRegBase = EA_REG_AX; |
| 1247 | break; |
| 1248 | case 4: |
Sean Callanan | 2f9443f | 2009-12-22 02:07:42 +0000 | [diff] [blame] | 1249 | insn->regBase = MODRM_REG_EAX; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1250 | insn->eaRegBase = EA_REG_EAX; |
| 1251 | break; |
| 1252 | case 8: |
Sean Callanan | 2f9443f | 2009-12-22 02:07:42 +0000 | [diff] [blame] | 1253 | insn->regBase = MODRM_REG_RAX; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1254 | insn->eaRegBase = EA_REG_RAX; |
| 1255 | break; |
| 1256 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1257 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1258 | reg |= rFromREX(insn->rexPrefix) << 3; |
| 1259 | rm |= bFromREX(insn->rexPrefix) << 3; |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 1260 | if (insn->vectorExtensionType == TYPE_EVEX) { |
| 1261 | reg |= r2FromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4; |
| 1262 | rm |= xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4; |
| 1263 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1264 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1265 | insn->reg = (Reg)(insn->regBase + reg); |
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 | switch (insn->addressSize) { |
| 1268 | case 2: |
| 1269 | insn->eaBaseBase = EA_BASE_BX_SI; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1270 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1271 | switch (mod) { |
| 1272 | case 0x0: |
| 1273 | if (rm == 0x6) { |
| 1274 | insn->eaBase = EA_BASE_NONE; |
| 1275 | insn->eaDisplacement = EA_DISP_16; |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1276 | if (readDisplacement(insn)) |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1277 | return -1; |
| 1278 | } else { |
| 1279 | insn->eaBase = (EABase)(insn->eaBaseBase + rm); |
| 1280 | insn->eaDisplacement = EA_DISP_NONE; |
| 1281 | } |
| 1282 | break; |
| 1283 | case 0x1: |
| 1284 | insn->eaBase = (EABase)(insn->eaBaseBase + rm); |
| 1285 | insn->eaDisplacement = EA_DISP_8; |
Craig Topper | 399e39e | 2014-01-25 22:48:43 +0000 | [diff] [blame] | 1286 | insn->displacementSize = 1; |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1287 | if (readDisplacement(insn)) |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1288 | return -1; |
| 1289 | break; |
| 1290 | case 0x2: |
| 1291 | insn->eaBase = (EABase)(insn->eaBaseBase + rm); |
| 1292 | insn->eaDisplacement = EA_DISP_16; |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1293 | if (readDisplacement(insn)) |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1294 | return -1; |
| 1295 | break; |
| 1296 | case 0x3: |
| 1297 | insn->eaBase = (EABase)(insn->eaRegBase + rm); |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1298 | if (readDisplacement(insn)) |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1299 | return -1; |
| 1300 | break; |
| 1301 | } |
| 1302 | break; |
| 1303 | case 4: |
| 1304 | case 8: |
| 1305 | insn->eaBaseBase = (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1306 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1307 | switch (mod) { |
| 1308 | case 0x0: |
| 1309 | insn->eaDisplacement = EA_DISP_NONE; /* readSIB may override this */ |
| 1310 | switch (rm) { |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 1311 | case 0x14: |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1312 | case 0x4: |
| 1313 | case 0xc: /* in case REXW.b is set */ |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1314 | insn->eaBase = (insn->addressSize == 4 ? |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1315 | EA_BASE_sib : EA_BASE_sib64); |
Craig Topper | 38afbfd | 2014-03-20 05:56:00 +0000 | [diff] [blame^] | 1316 | if (readSIB(insn) || readDisplacement(insn)) |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1317 | return -1; |
| 1318 | break; |
| 1319 | case 0x5: |
| 1320 | insn->eaBase = EA_BASE_NONE; |
| 1321 | insn->eaDisplacement = EA_DISP_32; |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1322 | if (readDisplacement(insn)) |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1323 | return -1; |
| 1324 | break; |
| 1325 | default: |
| 1326 | insn->eaBase = (EABase)(insn->eaBaseBase + rm); |
| 1327 | break; |
| 1328 | } |
| 1329 | break; |
| 1330 | case 0x1: |
Craig Topper | 399e39e | 2014-01-25 22:48:43 +0000 | [diff] [blame] | 1331 | insn->displacementSize = 1; |
Alp Toker | 771f765 | 2014-01-26 18:44:34 +0000 | [diff] [blame] | 1332 | /* FALLTHROUGH */ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1333 | case 0x2: |
| 1334 | insn->eaDisplacement = (mod == 0x1 ? EA_DISP_8 : EA_DISP_32); |
| 1335 | switch (rm) { |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 1336 | case 0x14: |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1337 | case 0x4: |
| 1338 | case 0xc: /* in case REXW.b is set */ |
| 1339 | insn->eaBase = EA_BASE_sib; |
Craig Topper | 38afbfd | 2014-03-20 05:56:00 +0000 | [diff] [blame^] | 1340 | if (readSIB(insn) || readDisplacement(insn)) |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1341 | return -1; |
| 1342 | break; |
| 1343 | default: |
| 1344 | insn->eaBase = (EABase)(insn->eaBaseBase + rm); |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1345 | if (readDisplacement(insn)) |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1346 | return -1; |
| 1347 | break; |
| 1348 | } |
| 1349 | break; |
| 1350 | case 0x3: |
| 1351 | insn->eaDisplacement = EA_DISP_NONE; |
| 1352 | insn->eaBase = (EABase)(insn->eaRegBase + rm); |
| 1353 | break; |
| 1354 | } |
| 1355 | break; |
| 1356 | } /* switch (insn->addressSize) */ |
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 | return 0; |
| 1359 | } |
| 1360 | |
| 1361 | #define GENERIC_FIXUP_FUNC(name, base, prefix) \ |
| 1362 | static uint8_t name(struct InternalInstruction *insn, \ |
| 1363 | OperandType type, \ |
| 1364 | uint8_t index, \ |
| 1365 | uint8_t *valid) { \ |
| 1366 | *valid = 1; \ |
| 1367 | switch (type) { \ |
| 1368 | default: \ |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1369 | debug("Unhandled register type"); \ |
| 1370 | *valid = 0; \ |
| 1371 | return 0; \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1372 | case TYPE_Rv: \ |
| 1373 | return base + index; \ |
| 1374 | case TYPE_R8: \ |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1375 | if (insn->rexPrefix && \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1376 | index >= 4 && index <= 7) { \ |
| 1377 | return prefix##_SPL + (index - 4); \ |
| 1378 | } else { \ |
| 1379 | return prefix##_AL + index; \ |
| 1380 | } \ |
| 1381 | case TYPE_R16: \ |
| 1382 | return prefix##_AX + index; \ |
| 1383 | case TYPE_R32: \ |
| 1384 | return prefix##_EAX + index; \ |
| 1385 | case TYPE_R64: \ |
| 1386 | return prefix##_RAX + index; \ |
Elena Demikhovsky | 003e7d7 | 2013-07-28 08:28:38 +0000 | [diff] [blame] | 1387 | case TYPE_XMM512: \ |
| 1388 | return prefix##_ZMM0 + index; \ |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1389 | case TYPE_XMM256: \ |
| 1390 | return prefix##_YMM0 + index; \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1391 | case TYPE_XMM128: \ |
| 1392 | case TYPE_XMM64: \ |
| 1393 | case TYPE_XMM32: \ |
| 1394 | case TYPE_XMM: \ |
| 1395 | return prefix##_XMM0 + index; \ |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 1396 | case TYPE_VK1: \ |
| 1397 | case TYPE_VK8: \ |
| 1398 | case TYPE_VK16: \ |
| 1399 | return prefix##_K0 + index; \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1400 | case TYPE_MM64: \ |
| 1401 | case TYPE_MM32: \ |
| 1402 | case TYPE_MM: \ |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1403 | if (index > 7) \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1404 | *valid = 0; \ |
| 1405 | return prefix##_MM0 + index; \ |
| 1406 | case TYPE_SEGMENTREG: \ |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1407 | if (index > 5) \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1408 | *valid = 0; \ |
| 1409 | return prefix##_ES + index; \ |
| 1410 | case TYPE_DEBUGREG: \ |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1411 | if (index > 7) \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1412 | *valid = 0; \ |
| 1413 | return prefix##_DR0 + index; \ |
Sean Callanan | e7e1cf9 | 2010-05-06 20:59:00 +0000 | [diff] [blame] | 1414 | case TYPE_CONTROLREG: \ |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1415 | if (index > 8) \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1416 | *valid = 0; \ |
Sean Callanan | e7e1cf9 | 2010-05-06 20:59:00 +0000 | [diff] [blame] | 1417 | return prefix##_CR0 + index; \ |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1418 | } \ |
| 1419 | } |
| 1420 | |
| 1421 | /* |
| 1422 | * fixup*Value - Consults an operand type to determine the meaning of the |
| 1423 | * reg or R/M field. If the operand is an XMM operand, for example, an |
| 1424 | * operand would be XMM0 instead of AX, which readModRM() would otherwise |
| 1425 | * misinterpret it as. |
| 1426 | * |
| 1427 | * @param insn - The instruction containing the operand. |
| 1428 | * @param type - The operand type. |
| 1429 | * @param index - The existing value of the field as reported by readModRM(). |
| 1430 | * @param valid - The address of a uint8_t. The target is set to 1 if the |
| 1431 | * field is valid for the register class; 0 if not. |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1432 | * @return - The proper value. |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1433 | */ |
Sean Callanan | 2f9443f | 2009-12-22 02:07:42 +0000 | [diff] [blame] | 1434 | GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase, MODRM_REG) |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1435 | GENERIC_FIXUP_FUNC(fixupRMValue, insn->eaRegBase, EA_REG) |
| 1436 | |
| 1437 | /* |
| 1438 | * fixupReg - Consults an operand specifier to determine which of the |
| 1439 | * fixup*Value functions to use in correcting readModRM()'ss interpretation. |
| 1440 | * |
| 1441 | * @param insn - See fixup*Value(). |
| 1442 | * @param op - The operand specifier. |
| 1443 | * @return - 0 if fixup was successful; -1 if the register returned was |
| 1444 | * invalid for its class. |
| 1445 | */ |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1446 | static int fixupReg(struct InternalInstruction *insn, |
Benjamin Kramer | de0a4fb | 2010-10-23 09:10:44 +0000 | [diff] [blame] | 1447 | const struct OperandSpecifier *op) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1448 | uint8_t valid; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1449 | |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 1450 | dbgprintf(insn, "fixupReg()"); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1451 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1452 | switch ((OperandEncoding)op->encoding) { |
| 1453 | default: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1454 | debug("Expected a REG or R/M encoding in fixupReg"); |
| 1455 | return -1; |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1456 | case ENCODING_VVVV: |
| 1457 | insn->vvvv = (Reg)fixupRegValue(insn, |
| 1458 | (OperandType)op->type, |
| 1459 | insn->vvvv, |
| 1460 | &valid); |
| 1461 | if (!valid) |
| 1462 | return -1; |
| 1463 | break; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1464 | case ENCODING_REG: |
| 1465 | insn->reg = (Reg)fixupRegValue(insn, |
| 1466 | (OperandType)op->type, |
| 1467 | insn->reg - insn->regBase, |
| 1468 | &valid); |
| 1469 | if (!valid) |
| 1470 | return -1; |
| 1471 | break; |
| 1472 | case ENCODING_RM: |
| 1473 | if (insn->eaBase >= insn->eaRegBase) { |
| 1474 | insn->eaBase = (EABase)fixupRMValue(insn, |
| 1475 | (OperandType)op->type, |
| 1476 | insn->eaBase - insn->eaRegBase, |
| 1477 | &valid); |
| 1478 | if (!valid) |
| 1479 | return -1; |
| 1480 | } |
| 1481 | break; |
| 1482 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1483 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1484 | return 0; |
| 1485 | } |
| 1486 | |
| 1487 | /* |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1488 | * readOpcodeRegister - Reads an operand from the opcode field of an |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1489 | * instruction and interprets it appropriately given the operand width. |
| 1490 | * Handles AddRegFrm instructions. |
| 1491 | * |
Craig Topper | 9155118 | 2014-01-01 15:29:32 +0000 | [diff] [blame] | 1492 | * @param insn - the instruction whose opcode field is to be read. |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1493 | * @param size - The width (in bytes) of the register being specified. |
| 1494 | * 1 means AL and friends, 2 means AX, 4 means EAX, and 8 means |
| 1495 | * RAX. |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1496 | * @return - 0 on success; nonzero otherwise. |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1497 | */ |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1498 | static int readOpcodeRegister(struct InternalInstruction* insn, uint8_t size) { |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 1499 | dbgprintf(insn, "readOpcodeRegister()"); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1500 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1501 | if (size == 0) |
| 1502 | size = insn->registerSize; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1503 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1504 | switch (size) { |
| 1505 | case 1: |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1506 | insn->opcodeRegister = (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3) |
Craig Topper | 9155118 | 2014-01-01 15:29:32 +0000 | [diff] [blame] | 1507 | | (insn->opcode & 7))); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1508 | if (insn->rexPrefix && |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1509 | insn->opcodeRegister >= MODRM_REG_AL + 0x4 && |
| 1510 | insn->opcodeRegister < MODRM_REG_AL + 0x8) { |
Sean Callanan | 2f9443f | 2009-12-22 02:07:42 +0000 | [diff] [blame] | 1511 | insn->opcodeRegister = (Reg)(MODRM_REG_SPL |
| 1512 | + (insn->opcodeRegister - MODRM_REG_AL - 4)); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1513 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1514 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1515 | break; |
| 1516 | case 2: |
Sean Callanan | 2f9443f | 2009-12-22 02:07:42 +0000 | [diff] [blame] | 1517 | insn->opcodeRegister = (Reg)(MODRM_REG_AX |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1518 | + ((bFromREX(insn->rexPrefix) << 3) |
Craig Topper | 9155118 | 2014-01-01 15:29:32 +0000 | [diff] [blame] | 1519 | | (insn->opcode & 7))); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1520 | break; |
| 1521 | case 4: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1522 | insn->opcodeRegister = (Reg)(MODRM_REG_EAX |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1523 | + ((bFromREX(insn->rexPrefix) << 3) |
Craig Topper | 9155118 | 2014-01-01 15:29:32 +0000 | [diff] [blame] | 1524 | | (insn->opcode & 7))); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1525 | break; |
| 1526 | case 8: |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1527 | insn->opcodeRegister = (Reg)(MODRM_REG_RAX |
| 1528 | + ((bFromREX(insn->rexPrefix) << 3) |
Craig Topper | 9155118 | 2014-01-01 15:29:32 +0000 | [diff] [blame] | 1529 | | (insn->opcode & 7))); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1530 | break; |
| 1531 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1532 | |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1533 | return 0; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | /* |
| 1537 | * readImmediate - Consumes an immediate operand from an instruction, given the |
| 1538 | * desired operand size. |
| 1539 | * |
| 1540 | * @param insn - The instruction whose operand is to be read. |
| 1541 | * @param size - The width (in bytes) of the operand. |
| 1542 | * @return - 0 if the immediate was successfully consumed; nonzero |
| 1543 | * otherwise. |
| 1544 | */ |
| 1545 | static int readImmediate(struct InternalInstruction* insn, uint8_t size) { |
| 1546 | uint8_t imm8; |
| 1547 | uint16_t imm16; |
| 1548 | uint32_t imm32; |
| 1549 | uint64_t imm64; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1550 | |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 1551 | dbgprintf(insn, "readImmediate()"); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1552 | |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1553 | if (insn->numImmediatesConsumed == 2) { |
| 1554 | debug("Already consumed two immediates"); |
| 1555 | return -1; |
| 1556 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1557 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1558 | if (size == 0) |
| 1559 | size = insn->immediateSize; |
| 1560 | else |
| 1561 | insn->immediateSize = size; |
Kevin Enderby | 6fbcd8d | 2012-02-23 18:18:17 +0000 | [diff] [blame] | 1562 | insn->immediateOffset = insn->readerCursor - insn->startLocation; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1563 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1564 | switch (size) { |
| 1565 | case 1: |
| 1566 | if (consumeByte(insn, &imm8)) |
| 1567 | return -1; |
| 1568 | insn->immediates[insn->numImmediatesConsumed] = imm8; |
| 1569 | break; |
| 1570 | case 2: |
| 1571 | if (consumeUInt16(insn, &imm16)) |
| 1572 | return -1; |
| 1573 | insn->immediates[insn->numImmediatesConsumed] = imm16; |
| 1574 | break; |
| 1575 | case 4: |
| 1576 | if (consumeUInt32(insn, &imm32)) |
| 1577 | return -1; |
| 1578 | insn->immediates[insn->numImmediatesConsumed] = imm32; |
| 1579 | break; |
| 1580 | case 8: |
| 1581 | if (consumeUInt64(insn, &imm64)) |
| 1582 | return -1; |
| 1583 | insn->immediates[insn->numImmediatesConsumed] = imm64; |
| 1584 | break; |
| 1585 | } |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1586 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1587 | insn->numImmediatesConsumed++; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1588 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1589 | return 0; |
| 1590 | } |
| 1591 | |
| 1592 | /* |
Craig Topper | 8dd7bbc | 2011-09-13 07:37:44 +0000 | [diff] [blame] | 1593 | * readVVVV - Consumes vvvv from an instruction if it has a VEX prefix. |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1594 | * |
| 1595 | * @param insn - The instruction whose operand is to be read. |
Craig Topper | 8dd7bbc | 2011-09-13 07:37:44 +0000 | [diff] [blame] | 1596 | * @return - 0 if the vvvv was successfully consumed; nonzero |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1597 | * otherwise. |
| 1598 | */ |
| 1599 | static int readVVVV(struct InternalInstruction* insn) { |
| 1600 | dbgprintf(insn, "readVVVV()"); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1601 | |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 1602 | if (insn->vectorExtensionType == TYPE_EVEX) |
| 1603 | insn->vvvv = vvvvFromEVEX3of4(insn->vectorExtensionPrefix[2]); |
| 1604 | else if (insn->vectorExtensionType == TYPE_VEX_3B) |
| 1605 | insn->vvvv = vvvvFromVEX3of3(insn->vectorExtensionPrefix[2]); |
| 1606 | else if (insn->vectorExtensionType == TYPE_VEX_2B) |
| 1607 | insn->vvvv = vvvvFromVEX2of2(insn->vectorExtensionPrefix[1]); |
| 1608 | else if (insn->vectorExtensionType == TYPE_XOP) |
| 1609 | insn->vvvv = vvvvFromXOP3of3(insn->vectorExtensionPrefix[2]); |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1610 | else |
| 1611 | return -1; |
| 1612 | |
Craig Topper | 0d0be47 | 2011-10-03 08:14:29 +0000 | [diff] [blame] | 1613 | if (insn->mode != MODE_64BIT) |
| 1614 | insn->vvvv &= 0x7; |
| 1615 | |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1616 | return 0; |
| 1617 | } |
| 1618 | |
| 1619 | /* |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 1620 | * readMaskRegister - Reads an mask register from the opcode field of an |
| 1621 | * instruction. |
| 1622 | * |
| 1623 | * @param insn - The instruction whose opcode field is to be read. |
| 1624 | * @return - 0 on success; nonzero otherwise. |
| 1625 | */ |
| 1626 | static int readMaskRegister(struct InternalInstruction* insn) { |
| 1627 | dbgprintf(insn, "readMaskRegister()"); |
| 1628 | |
| 1629 | if (insn->vectorExtensionType != TYPE_EVEX) |
| 1630 | return -1; |
| 1631 | |
| 1632 | insn->writemask = aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]); |
| 1633 | return 0; |
| 1634 | } |
| 1635 | |
| 1636 | /* |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1637 | * readOperands - Consults the specifier for an instruction and consumes all |
| 1638 | * operands for that instruction, interpreting them as it goes. |
| 1639 | * |
| 1640 | * @param insn - The instruction whose operands are to be read and interpreted. |
| 1641 | * @return - 0 if all operands could be read; nonzero otherwise. |
| 1642 | */ |
| 1643 | static int readOperands(struct InternalInstruction* insn) { |
| 1644 | int index; |
Craig Topper | 8dd7bbc | 2011-09-13 07:37:44 +0000 | [diff] [blame] | 1645 | int hasVVVV, needVVVV; |
Craig Topper | 2ba766a | 2011-12-30 06:23:39 +0000 | [diff] [blame] | 1646 | int sawRegImm = 0; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1647 | |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 1648 | dbgprintf(insn, "readOperands()"); |
Craig Topper | 8dd7bbc | 2011-09-13 07:37:44 +0000 | [diff] [blame] | 1649 | |
| 1650 | /* If non-zero vvvv specified, need to make sure one of the operands |
| 1651 | uses it. */ |
| 1652 | hasVVVV = !readVVVV(insn); |
| 1653 | needVVVV = hasVVVV && (insn->vvvv != 0); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1654 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1655 | for (index = 0; index < X86_MAX_OPERANDS; ++index) { |
Craig Topper | b8aec08 | 2012-08-01 07:39:18 +0000 | [diff] [blame] | 1656 | switch (x86OperandSets[insn->spec->operands][index].encoding) { |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1657 | case ENCODING_NONE: |
David Woodhouse | 2ef8d9c | 2014-01-22 15:08:08 +0000 | [diff] [blame] | 1658 | case ENCODING_SI: |
David Woodhouse | b33c2ef | 2014-01-22 15:08:21 +0000 | [diff] [blame] | 1659 | case ENCODING_DI: |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1660 | break; |
| 1661 | case ENCODING_REG: |
| 1662 | case ENCODING_RM: |
| 1663 | if (readModRM(insn)) |
| 1664 | return -1; |
Craig Topper | b8aec08 | 2012-08-01 07:39:18 +0000 | [diff] [blame] | 1665 | if (fixupReg(insn, &x86OperandSets[insn->spec->operands][index])) |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1666 | return -1; |
| 1667 | break; |
| 1668 | case ENCODING_CB: |
| 1669 | case ENCODING_CW: |
| 1670 | case ENCODING_CD: |
| 1671 | case ENCODING_CP: |
| 1672 | case ENCODING_CO: |
| 1673 | case ENCODING_CT: |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 1674 | dbgprintf(insn, "We currently don't hande code-offset encodings"); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1675 | return -1; |
| 1676 | case ENCODING_IB: |
Craig Topper | 2ba766a | 2011-12-30 06:23:39 +0000 | [diff] [blame] | 1677 | if (sawRegImm) { |
Benjamin Kramer | 9c48f26 | 2012-01-04 22:06:45 +0000 | [diff] [blame] | 1678 | /* Saw a register immediate so don't read again and instead split the |
| 1679 | previous immediate. FIXME: This is a hack. */ |
Benjamin Kramer | 47aecca | 2012-01-01 17:55:36 +0000 | [diff] [blame] | 1680 | insn->immediates[insn->numImmediatesConsumed] = |
| 1681 | insn->immediates[insn->numImmediatesConsumed - 1] & 0xf; |
| 1682 | ++insn->numImmediatesConsumed; |
Craig Topper | 2ba766a | 2011-12-30 06:23:39 +0000 | [diff] [blame] | 1683 | break; |
| 1684 | } |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1685 | if (readImmediate(insn, 1)) |
| 1686 | return -1; |
Craig Topper | b8aec08 | 2012-08-01 07:39:18 +0000 | [diff] [blame] | 1687 | if (x86OperandSets[insn->spec->operands][index].type == TYPE_IMM3 && |
Sean Callanan | 1efe661 | 2010-04-07 21:42:19 +0000 | [diff] [blame] | 1688 | insn->immediates[insn->numImmediatesConsumed - 1] > 7) |
| 1689 | return -1; |
Craig Topper | b8aec08 | 2012-08-01 07:39:18 +0000 | [diff] [blame] | 1690 | if (x86OperandSets[insn->spec->operands][index].type == TYPE_IMM5 && |
Craig Topper | 7629d63 | 2012-04-03 05:20:24 +0000 | [diff] [blame] | 1691 | insn->immediates[insn->numImmediatesConsumed - 1] > 31) |
| 1692 | return -1; |
Craig Topper | b8aec08 | 2012-08-01 07:39:18 +0000 | [diff] [blame] | 1693 | if (x86OperandSets[insn->spec->operands][index].type == TYPE_XMM128 || |
| 1694 | x86OperandSets[insn->spec->operands][index].type == TYPE_XMM256) |
Craig Topper | 2ba766a | 2011-12-30 06:23:39 +0000 | [diff] [blame] | 1695 | sawRegImm = 1; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1696 | break; |
| 1697 | case ENCODING_IW: |
| 1698 | if (readImmediate(insn, 2)) |
| 1699 | return -1; |
| 1700 | break; |
| 1701 | case ENCODING_ID: |
| 1702 | if (readImmediate(insn, 4)) |
| 1703 | return -1; |
| 1704 | break; |
| 1705 | case ENCODING_IO: |
| 1706 | if (readImmediate(insn, 8)) |
| 1707 | return -1; |
| 1708 | break; |
| 1709 | case ENCODING_Iv: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1710 | if (readImmediate(insn, insn->immediateSize)) |
| 1711 | return -1; |
Chris Lattner | d4758fc | 2010-04-16 21:15:15 +0000 | [diff] [blame] | 1712 | break; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1713 | case ENCODING_Ia: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1714 | if (readImmediate(insn, insn->addressSize)) |
| 1715 | return -1; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1716 | break; |
| 1717 | case ENCODING_RB: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1718 | if (readOpcodeRegister(insn, 1)) |
| 1719 | return -1; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1720 | break; |
| 1721 | case ENCODING_RW: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1722 | if (readOpcodeRegister(insn, 2)) |
| 1723 | return -1; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1724 | break; |
| 1725 | case ENCODING_RD: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1726 | if (readOpcodeRegister(insn, 4)) |
| 1727 | return -1; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1728 | break; |
| 1729 | case ENCODING_RO: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1730 | if (readOpcodeRegister(insn, 8)) |
| 1731 | return -1; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1732 | break; |
| 1733 | case ENCODING_Rv: |
Sean Callanan | 010b373 | 2010-04-02 21:23:51 +0000 | [diff] [blame] | 1734 | if (readOpcodeRegister(insn, 0)) |
| 1735 | return -1; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1736 | break; |
Craig Topper | 623b0d6 | 2014-01-01 14:22:37 +0000 | [diff] [blame] | 1737 | case ENCODING_FP: |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1738 | break; |
| 1739 | case ENCODING_VVVV: |
Craig Topper | 8dd7bbc | 2011-09-13 07:37:44 +0000 | [diff] [blame] | 1740 | needVVVV = 0; /* Mark that we have found a VVVV operand. */ |
| 1741 | if (!hasVVVV) |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1742 | return -1; |
Craig Topper | b8aec08 | 2012-08-01 07:39:18 +0000 | [diff] [blame] | 1743 | if (fixupReg(insn, &x86OperandSets[insn->spec->operands][index])) |
Sean Callanan | c3fd523 | 2011-03-15 01:23:15 +0000 | [diff] [blame] | 1744 | return -1; |
| 1745 | break; |
Elena Demikhovsky | 371e363 | 2013-12-25 11:40:51 +0000 | [diff] [blame] | 1746 | case ENCODING_WRITEMASK: |
| 1747 | if (readMaskRegister(insn)) |
| 1748 | return -1; |
| 1749 | break; |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1750 | case ENCODING_DUP: |
| 1751 | break; |
| 1752 | default: |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 1753 | dbgprintf(insn, "Encountered an operand with an unknown encoding."); |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1754 | return -1; |
| 1755 | } |
| 1756 | } |
Craig Topper | 8dd7bbc | 2011-09-13 07:37:44 +0000 | [diff] [blame] | 1757 | |
| 1758 | /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */ |
| 1759 | if (needVVVV) return -1; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1760 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1761 | return 0; |
| 1762 | } |
| 1763 | |
| 1764 | /* |
| 1765 | * decodeInstruction - Reads and interprets a full instruction provided by the |
| 1766 | * user. |
| 1767 | * |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1768 | * @param insn - A pointer to the instruction to be populated. Must be |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1769 | * pre-allocated. |
| 1770 | * @param reader - The function to be used to read the instruction's bytes. |
| 1771 | * @param readerArg - A generic argument to be passed to the reader to store |
| 1772 | * any internal state. |
| 1773 | * @param logger - If non-NULL, the function to be used to write log messages |
| 1774 | * and warnings. |
| 1775 | * @param loggerArg - A generic argument to be passed to the logger to store |
| 1776 | * any internal state. |
| 1777 | * @param startLoc - The address (in the reader's address space) of the first |
| 1778 | * byte in the instruction. |
| 1779 | * @param mode - The mode (real mode, IA-32e, or IA-32e in 64-bit mode) to |
| 1780 | * decode the instruction in. |
| 1781 | * @return - 0 if the instruction's memory could be read; nonzero if |
| 1782 | * not. |
| 1783 | */ |
| 1784 | int decodeInstruction(struct InternalInstruction* insn, |
| 1785 | byteReader_t reader, |
Roman Divacky | 6792380 | 2012-09-05 21:17:34 +0000 | [diff] [blame] | 1786 | const void* readerArg, |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1787 | dlog_t logger, |
| 1788 | void* loggerArg, |
Roman Divacky | 6792380 | 2012-09-05 21:17:34 +0000 | [diff] [blame] | 1789 | const void* miiArg, |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1790 | uint64_t startLoc, |
| 1791 | DisassemblerMode mode) { |
Daniel Dunbar | c745a62 | 2009-12-19 03:31:50 +0000 | [diff] [blame] | 1792 | memset(insn, 0, sizeof(struct InternalInstruction)); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1793 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1794 | insn->reader = reader; |
| 1795 | insn->readerArg = readerArg; |
| 1796 | insn->dlog = logger; |
| 1797 | insn->dlogArg = loggerArg; |
| 1798 | insn->startLocation = startLoc; |
| 1799 | insn->readerCursor = startLoc; |
| 1800 | insn->mode = mode; |
| 1801 | insn->numImmediatesConsumed = 0; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1802 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1803 | if (readPrefixes(insn) || |
| 1804 | readOpcode(insn) || |
Benjamin Kramer | 478e8de | 2012-02-11 14:50:54 +0000 | [diff] [blame] | 1805 | getID(insn, miiArg) || |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1806 | insn->instructionID == 0 || |
| 1807 | readOperands(insn)) |
| 1808 | return -1; |
Craig Topper | b8aec08 | 2012-08-01 07:39:18 +0000 | [diff] [blame] | 1809 | |
| 1810 | insn->operands = &x86OperandSets[insn->spec->operands][0]; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1811 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1812 | insn->length = insn->readerCursor - insn->startLocation; |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1813 | |
Benjamin Kramer | 4f67227 | 2010-03-18 12:18:36 +0000 | [diff] [blame] | 1814 | dbgprintf(insn, "Read from 0x%llx to 0x%llx: length %zu", |
| 1815 | startLoc, insn->readerCursor, insn->length); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1816 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1817 | if (insn->length > 15) |
Nuno Lopes | 3ed6d60 | 2009-12-19 12:07:00 +0000 | [diff] [blame] | 1818 | dbgprintf(insn, "Instruction exceeds 15-byte limit"); |
NAKAMURA Takumi | dde7fa8 | 2013-03-25 20:55:43 +0000 | [diff] [blame] | 1819 | |
Sean Callanan | 04cc307 | 2009-12-19 02:59:52 +0000 | [diff] [blame] | 1820 | return 0; |
| 1821 | } |