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