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