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