/src/capstonenext/arch/X86/X86DisassemblerDecoder.c
Line | Count | Source |
1 | | /*===-- X86DisassemblerDecoder.c - Disassembler decoder ------------*- C -*-===* |
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 | | |
16 | | /* Capstone Disassembly Engine */ |
17 | | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */ |
18 | | |
19 | | #ifdef CAPSTONE_HAS_X86 |
20 | | |
21 | | #include <stdarg.h> /* for va_*() */ |
22 | | #if defined(CAPSTONE_HAS_OSXKERNEL) |
23 | | #include <libkern/libkern.h> |
24 | | #else |
25 | | #include <stdlib.h> /* for exit() */ |
26 | | #endif |
27 | | |
28 | | #include <string.h> |
29 | | |
30 | | #include "../../cs_priv.h" |
31 | | #include "../../utils.h" |
32 | | |
33 | | #include "X86DisassemblerDecoder.h" |
34 | | #include "X86Mapping.h" |
35 | | |
36 | | /// Specifies whether a ModR/M byte is needed and (if so) which |
37 | | /// instruction each possible value of the ModR/M byte corresponds to. Once |
38 | | /// this information is known, we have narrowed down to a single instruction. |
39 | | struct ModRMDecision { |
40 | | uint8_t modrm_type; |
41 | | uint16_t instructionIDs; |
42 | | }; |
43 | | |
44 | | /// Specifies which set of ModR/M->instruction tables to look at |
45 | | /// given a particular opcode. |
46 | | struct OpcodeDecision { |
47 | | struct ModRMDecision modRMDecisions[256]; |
48 | | }; |
49 | | |
50 | | /// Specifies which opcode->instruction tables to look at given |
51 | | /// a particular context (set of attributes). Since there are many possible |
52 | | /// contexts, the decoder first uses CONTEXTS_SYM to determine which context |
53 | | /// applies given a specific set of attributes. Hence there are only IC_max |
54 | | /// entries in this table, rather than 2^(ATTR_max). |
55 | | struct ContextDecision { |
56 | | struct OpcodeDecision opcodeDecisions[IC_max]; |
57 | | }; |
58 | | |
59 | | #ifdef CAPSTONE_X86_REDUCE |
60 | | #include "X86GenDisassemblerTables_reduce.inc" |
61 | | #include "X86GenDisassemblerTables_reduce2.inc" |
62 | | #include "X86Lookup16_reduce.inc" |
63 | | #else |
64 | | #include "X86GenDisassemblerTables.inc" |
65 | | #include "X86GenDisassemblerTables2.inc" |
66 | | #include "X86Lookup16.inc" |
67 | | #endif |
68 | | |
69 | | /* |
70 | | * contextForAttrs - Client for the instruction context table. Takes a set of |
71 | | * attributes and returns the appropriate decode context. |
72 | | * |
73 | | * @param attrMask - Attributes, from the enumeration attributeBits. |
74 | | * @return - The InstructionContext to use when looking up an |
75 | | * an instruction with these attributes. |
76 | | */ |
77 | | static InstructionContext contextForAttrs(uint16_t attrMask) |
78 | 795k | { |
79 | 795k | return CONTEXTS_SYM[attrMask]; |
80 | 795k | } |
81 | | |
82 | | /* |
83 | | * modRMRequired - Reads the appropriate instruction table to determine whether |
84 | | * the ModR/M byte is required to decode a particular instruction. |
85 | | * |
86 | | * @param type - The opcode type (i.e., how many bytes it has). |
87 | | * @param insnContext - The context for the instruction, as returned by |
88 | | * contextForAttrs. |
89 | | * @param opcode - The last byte of the instruction's opcode, not counting |
90 | | * ModR/M extensions and escapes. |
91 | | * @return - true if the ModR/M byte is required, false otherwise. |
92 | | */ |
93 | | static int modRMRequired(OpcodeType type, InstructionContext insnContext, |
94 | | uint16_t opcode) |
95 | 795k | { |
96 | 795k | const struct OpcodeDecision *decision = NULL; |
97 | 795k | const uint8_t *indextable = NULL; |
98 | 795k | unsigned int index; |
99 | | |
100 | 795k | switch (type) { |
101 | 0 | default: |
102 | 0 | return false; |
103 | 604k | case ONEBYTE: |
104 | 604k | decision = ONEBYTE_SYM; |
105 | 604k | indextable = index_x86DisassemblerOneByteOpcodes; |
106 | 604k | break; |
107 | 99.3k | case TWOBYTE: |
108 | 99.3k | decision = TWOBYTE_SYM; |
109 | 99.3k | indextable = index_x86DisassemblerTwoByteOpcodes; |
110 | 99.3k | break; |
111 | 33.3k | case THREEBYTE_38: |
112 | 33.3k | decision = THREEBYTE38_SYM; |
113 | 33.3k | indextable = index_x86DisassemblerThreeByte38Opcodes; |
114 | 33.3k | break; |
115 | 42.2k | case THREEBYTE_3A: |
116 | 42.2k | decision = THREEBYTE3A_SYM; |
117 | 42.2k | indextable = index_x86DisassemblerThreeByte3AOpcodes; |
118 | 42.2k | break; |
119 | 0 | #ifndef CAPSTONE_X86_REDUCE |
120 | 12.2k | case XOP8_MAP: |
121 | 12.2k | decision = XOP8_MAP_SYM; |
122 | 12.2k | indextable = index_x86DisassemblerXOP8Opcodes; |
123 | 12.2k | break; |
124 | 1.74k | case XOP9_MAP: |
125 | 1.74k | decision = XOP9_MAP_SYM; |
126 | 1.74k | indextable = index_x86DisassemblerXOP9Opcodes; |
127 | 1.74k | break; |
128 | 1.04k | case XOPA_MAP: |
129 | 1.04k | decision = XOPA_MAP_SYM; |
130 | 1.04k | indextable = index_x86DisassemblerXOPAOpcodes; |
131 | 1.04k | break; |
132 | 886 | case THREEDNOW_MAP: |
133 | | // 3DNow instructions always have ModRM byte |
134 | 886 | return true; |
135 | 795k | #endif |
136 | 795k | } |
137 | | |
138 | | // return decision->opcodeDecisions[insnContext].modRMDecisions[opcode].modrm_type != MODRM_ONEENTRY; |
139 | 794k | index = indextable[insnContext]; |
140 | 794k | if (index) |
141 | 790k | return decision[index - 1].modRMDecisions[opcode].modrm_type != |
142 | 790k | MODRM_ONEENTRY; |
143 | 3.39k | else |
144 | 3.39k | return false; |
145 | 794k | } |
146 | | |
147 | | /* |
148 | | * decode - Reads the appropriate instruction table to obtain the unique ID of |
149 | | * an instruction. |
150 | | * |
151 | | * @param type - See modRMRequired(). |
152 | | * @param insnContext - See modRMRequired(). |
153 | | * @param opcode - See modRMRequired(). |
154 | | * @param modRM - The ModR/M byte if required, or any value if not. |
155 | | * @return - The UID of the instruction, or 0 on failure. |
156 | | */ |
157 | | static InstrUID decode(OpcodeType type, InstructionContext insnContext, |
158 | | uint8_t opcode, uint8_t modRM) |
159 | 793k | { |
160 | 793k | const struct ModRMDecision *dec = NULL; |
161 | 793k | unsigned int index; |
162 | 793k | static const struct OpcodeDecision emptyDecision = { 0 }; |
163 | | |
164 | 793k | switch (type) { |
165 | 0 | default: |
166 | 0 | return 0; |
167 | 602k | case ONEBYTE: |
168 | | // Reserved x87 register encodings alias FCOM, FCOMP and FXCH. |
169 | 602k | if (opcode == 0xdc && (modRM & 0xf0) == 0xd0) |
170 | 567 | opcode = 0xd8; |
171 | 602k | else if ((opcode == 0xdd || opcode == 0xdf) && |
172 | 2.50k | (modRM & 0xf8) == 0xc8) |
173 | 204 | opcode = 0xd9; |
174 | 601k | else if (opcode == 0xde && (modRM & 0xf8) == 0xd0) { |
175 | 208 | opcode = 0xd8; |
176 | 208 | modRM |= 0x08; |
177 | 208 | } |
178 | | // dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode]; |
179 | 602k | index = index_x86DisassemblerOneByteOpcodes[insnContext]; |
180 | 602k | if (index) |
181 | 602k | dec = &ONEBYTE_SYM[index - 1].modRMDecisions[opcode]; |
182 | 214 | else |
183 | 214 | dec = &emptyDecision.modRMDecisions[opcode]; |
184 | 602k | break; |
185 | 99.1k | case TWOBYTE: |
186 | | // Reserved PREFETCH hints /4-/7 alias /0 on AMD processors. |
187 | 99.1k | if (opcode == 0x0d && modFromModRM(modRM) != 3 && |
188 | 421 | regFromModRM(modRM) >= 4) |
189 | 198 | modRM &= 0xc7; |
190 | | //dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode]; |
191 | 99.1k | index = index_x86DisassemblerTwoByteOpcodes[insnContext]; |
192 | 99.1k | if (index) |
193 | 98.1k | dec = &TWOBYTE_SYM[index - 1].modRMDecisions[opcode]; |
194 | 1.04k | else |
195 | 1.04k | dec = &emptyDecision.modRMDecisions[opcode]; |
196 | 99.1k | break; |
197 | 33.3k | case THREEBYTE_38: |
198 | | // dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode]; |
199 | 33.3k | index = index_x86DisassemblerThreeByte38Opcodes[insnContext]; |
200 | 33.3k | if (index) |
201 | 33.1k | dec = &THREEBYTE38_SYM[index - 1].modRMDecisions[opcode]; |
202 | 209 | else |
203 | 209 | dec = &emptyDecision.modRMDecisions[opcode]; |
204 | 33.3k | break; |
205 | 42.2k | case THREEBYTE_3A: |
206 | | //dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode]; |
207 | 42.2k | index = index_x86DisassemblerThreeByte3AOpcodes[insnContext]; |
208 | 42.2k | if (index) |
209 | 42.0k | dec = &THREEBYTE3A_SYM[index - 1].modRMDecisions[opcode]; |
210 | 217 | else |
211 | 217 | dec = &emptyDecision.modRMDecisions[opcode]; |
212 | 42.2k | break; |
213 | 0 | #ifndef CAPSTONE_X86_REDUCE |
214 | 12.2k | case XOP8_MAP: |
215 | | // dec = &XOP8_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode]; |
216 | 12.2k | index = index_x86DisassemblerXOP8Opcodes[insnContext]; |
217 | 12.2k | if (index) |
218 | 11.0k | dec = &XOP8_MAP_SYM[index - 1].modRMDecisions[opcode]; |
219 | 1.17k | else |
220 | 1.17k | dec = &emptyDecision.modRMDecisions[opcode]; |
221 | 12.2k | break; |
222 | 1.74k | case XOP9_MAP: |
223 | | // dec = &XOP9_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode]; |
224 | 1.74k | index = index_x86DisassemblerXOP9Opcodes[insnContext]; |
225 | 1.74k | if (index) |
226 | 1.47k | dec = &XOP9_MAP_SYM[index - 1].modRMDecisions[opcode]; |
227 | 267 | else |
228 | 267 | dec = &emptyDecision.modRMDecisions[opcode]; |
229 | 1.74k | break; |
230 | 1.04k | case XOPA_MAP: |
231 | | // dec = &XOPA_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode]; |
232 | 1.04k | index = index_x86DisassemblerXOPAOpcodes[insnContext]; |
233 | 1.04k | if (index) |
234 | 778 | dec = &XOPA_MAP_SYM[index - 1].modRMDecisions[opcode]; |
235 | 269 | else |
236 | 269 | dec = &emptyDecision.modRMDecisions[opcode]; |
237 | 1.04k | break; |
238 | 886 | case THREEDNOW_MAP: |
239 | | // dec = &THREEDNOW_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode]; |
240 | 886 | index = index_x86Disassembler3DNowOpcodes[insnContext]; |
241 | 886 | if (index) |
242 | 632 | dec = &THREEDNOW_MAP_SYM[index - 1] |
243 | 632 | .modRMDecisions[opcode]; |
244 | 254 | else |
245 | 254 | dec = &emptyDecision.modRMDecisions[opcode]; |
246 | 886 | break; |
247 | 793k | #endif |
248 | 793k | } |
249 | | |
250 | 793k | switch (dec->modrm_type) { |
251 | 0 | default: |
252 | | // debug("Corrupt table! Unknown modrm_type"); |
253 | 0 | return 0; |
254 | 338k | case MODRM_ONEENTRY: |
255 | 338k | return modRMTable[dec->instructionIDs]; |
256 | 351k | case MODRM_SPLITRM: |
257 | 351k | if (modFromModRM(modRM) == 0x3) |
258 | 86.4k | return modRMTable[dec->instructionIDs + 1]; |
259 | 264k | return modRMTable[dec->instructionIDs]; |
260 | 88.0k | case MODRM_SPLITREG: |
261 | 88.0k | if (modFromModRM(modRM) == 0x3) |
262 | 26.0k | return modRMTable[dec->instructionIDs + |
263 | 26.0k | ((modRM & 0x38) >> 3) + 8]; |
264 | 62.0k | return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)]; |
265 | 15.9k | case MODRM_SPLITMISC: |
266 | 15.9k | if (modFromModRM(modRM) == 0x3) |
267 | 2.52k | return modRMTable[dec->instructionIDs + (modRM & 0x3f) + |
268 | 2.52k | 8]; |
269 | 13.4k | return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)]; |
270 | 0 | case MODRM_FULL: |
271 | 0 | return modRMTable[dec->instructionIDs + modRM]; |
272 | 793k | } |
273 | 793k | } |
274 | | |
275 | | /* |
276 | | * specifierForUID - Given a UID, returns the name and operand specification for |
277 | | * that instruction. |
278 | | * |
279 | | * @param uid - The unique ID for the instruction. This should be returned by |
280 | | * decode(); specifierForUID will not check bounds. |
281 | | * @return - A pointer to the specification for that instruction. |
282 | | */ |
283 | | static const struct InstructionSpecifier *specifierForUID(InstrUID uid) |
284 | 665k | { |
285 | 665k | return &INSTRUCTIONS_SYM[uid]; |
286 | 665k | } |
287 | | |
288 | | /* |
289 | | * consumeByte - Uses the reader function provided by the user to consume one |
290 | | * byte from the instruction's memory and advance the cursor. |
291 | | * |
292 | | * @param insn - The instruction with the reader function to use. The cursor |
293 | | * for this instruction is advanced. |
294 | | * @param byte - A pointer to a pre-allocated memory buffer to be populated |
295 | | * with the data read. |
296 | | * @return - 0 if the read was successful; nonzero otherwise. |
297 | | */ |
298 | | static int consumeByte(struct InternalInstruction *insn, uint8_t *byte) |
299 | 2.11M | { |
300 | 2.11M | int ret = insn->reader(insn->readerArg, byte, insn->readerCursor); |
301 | | |
302 | 2.11M | if (!ret) |
303 | 2.11M | ++(insn->readerCursor); |
304 | | |
305 | 2.11M | return ret; |
306 | 2.11M | } |
307 | | |
308 | | /* |
309 | | * lookAtByte - Like consumeByte, but does not advance the cursor. |
310 | | * |
311 | | * @param insn - See consumeByte(). |
312 | | * @param byte - See consumeByte(). |
313 | | * @return - See consumeByte(). |
314 | | */ |
315 | | static int lookAtByte(struct InternalInstruction *insn, uint8_t *byte) |
316 | 163k | { |
317 | 163k | return insn->reader(insn->readerArg, byte, insn->readerCursor); |
318 | 163k | } |
319 | | |
320 | | static void unconsumeByte(struct InternalInstruction *insn) |
321 | 523k | { |
322 | 523k | insn->readerCursor--; |
323 | 523k | } |
324 | | |
325 | | #define CONSUME_FUNC(name, type) \ |
326 | | static int name(struct InternalInstruction *insn, type *ptr) \ |
327 | 118k | { \ |
328 | 118k | type combined = 0; \ |
329 | 118k | unsigned offset; \ |
330 | 376k | for (offset = 0; offset < sizeof(type); ++offset) { \ |
331 | 259k | uint8_t byte; \ |
332 | 259k | int ret = insn->reader(insn->readerArg, &byte, \ |
333 | 259k | insn->readerCursor + offset); \ |
334 | 259k | if (ret) \ |
335 | 259k | return ret; \ |
336 | 259k | combined = combined | \ |
337 | 257k | ((uint64_t)byte << (offset * 8)); \ |
338 | 257k | } \ |
339 | 118k | *ptr = combined; \ |
340 | 117k | insn->readerCursor += sizeof(type); \ |
341 | 117k | return 0; \ |
342 | 118k | } X86DisassemblerDecoder.c:consumeInt8 Line | Count | Source | 327 | 52.7k | { \ | 328 | 52.7k | type combined = 0; \ | 329 | 52.7k | unsigned offset; \ | 330 | 105k | for (offset = 0; offset < sizeof(type); ++offset) { \ | 331 | 52.7k | uint8_t byte; \ | 332 | 52.7k | int ret = insn->reader(insn->readerArg, &byte, \ | 333 | 52.7k | insn->readerCursor + offset); \ | 334 | 52.7k | if (ret) \ | 335 | 52.7k | return ret; \ | 336 | 52.7k | combined = combined | \ | 337 | 52.5k | ((uint64_t)byte << (offset * 8)); \ | 338 | 52.5k | } \ | 339 | 52.7k | *ptr = combined; \ | 340 | 52.5k | insn->readerCursor += sizeof(type); \ | 341 | 52.5k | return 0; \ | 342 | 52.7k | } |
X86DisassemblerDecoder.c:consumeInt16 Line | Count | Source | 327 | 9.85k | { \ | 328 | 9.85k | type combined = 0; \ | 329 | 9.85k | unsigned offset; \ | 330 | 29.4k | for (offset = 0; offset < sizeof(type); ++offset) { \ | 331 | 19.6k | uint8_t byte; \ | 332 | 19.6k | int ret = insn->reader(insn->readerArg, &byte, \ | 333 | 19.6k | insn->readerCursor + offset); \ | 334 | 19.6k | if (ret) \ | 335 | 19.6k | return ret; \ | 336 | 19.6k | combined = combined | \ | 337 | 19.6k | ((uint64_t)byte << (offset * 8)); \ | 338 | 19.6k | } \ | 339 | 9.85k | *ptr = combined; \ | 340 | 9.79k | insn->readerCursor += sizeof(type); \ | 341 | 9.79k | return 0; \ | 342 | 9.85k | } |
X86DisassemblerDecoder.c:consumeInt32 Line | Count | Source | 327 | 16.2k | { \ | 328 | 16.2k | type combined = 0; \ | 329 | 16.2k | unsigned offset; \ | 330 | 80.5k | for (offset = 0; offset < sizeof(type); ++offset) { \ | 331 | 64.5k | uint8_t byte; \ | 332 | 64.5k | int ret = insn->reader(insn->readerArg, &byte, \ | 333 | 64.5k | insn->readerCursor + offset); \ | 334 | 64.5k | if (ret) \ | 335 | 64.5k | return ret; \ | 336 | 64.5k | combined = combined | \ | 337 | 64.3k | ((uint64_t)byte << (offset * 8)); \ | 338 | 64.3k | } \ | 339 | 16.2k | *ptr = combined; \ | 340 | 16.0k | insn->readerCursor += sizeof(type); \ | 341 | 16.0k | return 0; \ | 342 | 16.2k | } |
X86DisassemblerDecoder.c:consumeUInt16 Line | Count | Source | 327 | 23.3k | { \ | 328 | 23.3k | type combined = 0; \ | 329 | 23.3k | unsigned offset; \ | 330 | 69.7k | for (offset = 0; offset < sizeof(type); ++offset) { \ | 331 | 46.5k | uint8_t byte; \ | 332 | 46.5k | int ret = insn->reader(insn->readerArg, &byte, \ | 333 | 46.5k | insn->readerCursor + offset); \ | 334 | 46.5k | if (ret) \ | 335 | 46.5k | return ret; \ | 336 | 46.5k | combined = combined | \ | 337 | 46.4k | ((uint64_t)byte << (offset * 8)); \ | 338 | 46.4k | } \ | 339 | 23.3k | *ptr = combined; \ | 340 | 23.1k | insn->readerCursor += sizeof(type); \ | 341 | 23.1k | return 0; \ | 342 | 23.3k | } |
X86DisassemblerDecoder.c:consumeUInt32 Line | Count | Source | 327 | 14.1k | { \ | 328 | 14.1k | type combined = 0; \ | 329 | 14.1k | unsigned offset; \ | 330 | 69.7k | for (offset = 0; offset < sizeof(type); ++offset) { \ | 331 | 55.9k | uint8_t byte; \ | 332 | 55.9k | int ret = insn->reader(insn->readerArg, &byte, \ | 333 | 55.9k | insn->readerCursor + offset); \ | 334 | 55.9k | if (ret) \ | 335 | 55.9k | return ret; \ | 336 | 55.9k | combined = combined | \ | 337 | 55.5k | ((uint64_t)byte << (offset * 8)); \ | 338 | 55.5k | } \ | 339 | 14.1k | *ptr = combined; \ | 340 | 13.8k | insn->readerCursor += sizeof(type); \ | 341 | 13.8k | return 0; \ | 342 | 14.1k | } |
X86DisassemblerDecoder.c:consumeUInt64 Line | Count | Source | 327 | 2.48k | { \ | 328 | 2.48k | type combined = 0; \ | 329 | 2.48k | unsigned offset; \ | 330 | 21.9k | for (offset = 0; offset < sizeof(type); ++offset) { \ | 331 | 19.5k | uint8_t byte; \ | 332 | 19.5k | int ret = insn->reader(insn->readerArg, &byte, \ | 333 | 19.5k | insn->readerCursor + offset); \ | 334 | 19.5k | if (ret) \ | 335 | 19.5k | return ret; \ | 336 | 19.5k | combined = combined | \ | 337 | 19.4k | ((uint64_t)byte << (offset * 8)); \ | 338 | 19.4k | } \ | 339 | 2.48k | *ptr = combined; \ | 340 | 2.41k | insn->readerCursor += sizeof(type); \ | 341 | 2.41k | return 0; \ | 342 | 2.48k | } |
|
343 | | |
344 | | /* |
345 | | * consume* - Use the reader function provided by the user to consume data |
346 | | * values of various sizes from the instruction's memory and advance the |
347 | | * cursor appropriately. These readers perform endian conversion. |
348 | | * |
349 | | * @param insn - See consumeByte(). |
350 | | * @param ptr - A pointer to a pre-allocated memory of appropriate size to |
351 | | * be populated with the data read. |
352 | | * @return - See consumeByte(). |
353 | | */ |
354 | | CONSUME_FUNC(consumeInt8, int8_t) |
355 | | CONSUME_FUNC(consumeInt16, int16_t) |
356 | | CONSUME_FUNC(consumeInt32, int32_t) |
357 | | CONSUME_FUNC(consumeUInt16, uint16_t) |
358 | | CONSUME_FUNC(consumeUInt32, uint32_t) |
359 | | CONSUME_FUNC(consumeUInt64, uint64_t) |
360 | | |
361 | | static bool isREX(struct InternalInstruction *insn, uint8_t prefix) |
362 | 680k | { |
363 | 680k | if (insn->mode == MODE_64BIT) |
364 | 290k | return prefix >= 0x40 && prefix <= 0x4f; |
365 | | |
366 | 389k | return false; |
367 | 680k | } |
368 | | |
369 | | /* |
370 | | * setGroup0Prefix - Updates the decoded instruction according to the group 0-prefix. |
371 | | * |
372 | | * @param insn - The instruction to be updated. |
373 | | * @param prefix - The group 0 prefix that is present. |
374 | | */ |
375 | | static void setGroup0Prefix(struct InternalInstruction *insn, uint8_t prefix) |
376 | 81.9k | { |
377 | 81.9k | switch (prefix) { |
378 | 38.8k | case 0xf0: // LOCK |
379 | 38.8k | insn->hasLockPrefix = true; |
380 | 38.8k | break; |
381 | | |
382 | 24.4k | case 0xf2: // REPNE/REPNZ |
383 | 43.1k | case 0xf3: // REP or REPE/REPZ |
384 | 43.1k | insn->repeatPrefix = prefix; |
385 | 43.1k | break; |
386 | 81.9k | } |
387 | 81.9k | } |
388 | | |
389 | | /* |
390 | | * setSegmentOverride - Overrides an instruction's prefix1 based on CPU mode. |
391 | | * |
392 | | * @param insn - The instruction to be overridden. |
393 | | * @param prefix - The segment override to use. |
394 | | * @param byte - The current decoded prefix byte. Must be a segment override. |
395 | | */ |
396 | | static void setSegmentOverride(struct InternalInstruction *insn, |
397 | | SegmentOverride prefix, uint8_t byte) |
398 | 18.8k | { |
399 | | // In 32-bit or 16-bit mode all segment override prefixes are used. |
400 | 18.8k | if (insn->mode != MODE_64BIT) { |
401 | 9.79k | insn->segmentOverride = prefix; |
402 | 9.79k | insn->prefix1 = byte; |
403 | 9.79k | return; |
404 | 9.79k | } |
405 | | |
406 | | // In 64-bit mode, the ES/CS/SS/DS segment overrides should be ignored. |
407 | | // In the case there are multiple segment overrides, do not override |
408 | | // an existing FS or GS segment prefix. |
409 | 9.05k | switch (insn->prefix1) { |
410 | 1.42k | case 0x64: // FS |
411 | 2.22k | case 0x65: // GS |
412 | 2.22k | return; |
413 | 9.05k | } |
414 | | |
415 | | // If the proposed override is for FS or GS, mark it overridden. |
416 | | // All other segment prefixes are ignored. |
417 | 6.83k | switch (byte) { |
418 | 1.16k | case 0x64: // FS |
419 | 2.77k | case 0x65: // GS |
420 | 2.77k | insn->segmentOverride = prefix; |
421 | 2.77k | break; |
422 | 6.83k | } |
423 | | |
424 | | // `prefix1` may later be used to decode the `notrack` prefix. |
425 | | // The `notrack` prefix reuses the DS segment override, so we |
426 | | // need to store the prefix even if it is ignored for the segment overrides. |
427 | 6.83k | insn->prefix1 = byte; |
428 | 6.83k | } |
429 | | |
430 | | /* |
431 | | * readPrefixes - Consumes all of an instruction's prefix bytes, and marks the |
432 | | * instruction as having them. Also sets the instruction's default operand, |
433 | | * address, and other relevant data sizes to report operands correctly. |
434 | | * |
435 | | * @param insn - The instruction whose prefixes are to be read. |
436 | | * @return - 0 if the instruction could be read until the end of the prefix |
437 | | * bytes, and no prefixes conflicted; nonzero otherwise. |
438 | | */ |
439 | | static int readPrefixes(struct InternalInstruction *insn) |
440 | 596k | { |
441 | 596k | bool isPrefix = true; |
442 | 596k | uint8_t byte = 0; |
443 | 596k | uint8_t nextByte; |
444 | | |
445 | 1.36M | while (isPrefix) { |
446 | | /* If we fail reading prefixes, just stop here and let the opcode reader deal with it */ |
447 | 771k | if (consumeByte(insn, &byte)) |
448 | 477 | return -1; |
449 | | |
450 | 771k | if (insn->readerCursor - 1 == insn->startLocation && |
451 | 596k | (byte == 0xf2 || byte == 0xf3)) { |
452 | | // prefix requires next byte |
453 | 35.1k | if (lookAtByte(insn, &nextByte)) |
454 | 102 | return -1; |
455 | | |
456 | 35.0k | if (isREX(insn, nextByte)) { |
457 | 2.96k | uint8_t nnextByte; |
458 | | |
459 | | // Go to REX prefix after the current one |
460 | 2.96k | if (consumeByte(insn, &nnextByte)) |
461 | 0 | return -1; |
462 | | |
463 | | // We should be able to read next byte after REX prefix |
464 | 2.96k | if (lookAtByte(insn, &nnextByte)) |
465 | 7 | return -1; |
466 | | |
467 | 2.96k | unconsumeByte(insn); |
468 | 2.96k | } |
469 | 35.0k | } |
470 | | |
471 | 771k | switch (byte) { |
472 | 38.8k | case 0xf0: /* LOCK */ |
473 | 63.2k | case 0xf2: /* REPNE/REPNZ */ |
474 | 81.9k | case 0xf3: /* REP or REPE/REPZ */ |
475 | | // only accept the last prefix |
476 | 81.9k | setGroup0Prefix(insn, byte); |
477 | 81.9k | insn->prefix0 = byte; |
478 | 81.9k | insn->rexPrefix = 0; |
479 | 81.9k | break; |
480 | | |
481 | 3.50k | case 0x2e: /* CS segment override -OR- Branch not taken */ |
482 | 5.60k | case 0x36: /* SS segment override -OR- Branch taken */ |
483 | 7.99k | case 0x3e: /* DS segment override */ |
484 | 10.4k | case 0x26: /* ES segment override */ |
485 | 13.8k | case 0x64: /* FS segment override */ |
486 | 18.8k | case 0x65: /* GS segment override */ |
487 | 18.8k | switch (byte) { |
488 | 3.50k | case 0x2e: |
489 | 3.50k | setSegmentOverride(insn, SEG_OVERRIDE_CS, byte); |
490 | 3.50k | break; |
491 | 2.09k | case 0x36: |
492 | 2.09k | setSegmentOverride(insn, SEG_OVERRIDE_SS, byte); |
493 | 2.09k | break; |
494 | 2.39k | case 0x3e: |
495 | 2.39k | setSegmentOverride(insn, SEG_OVERRIDE_DS, byte); |
496 | 2.39k | break; |
497 | 2.42k | case 0x26: |
498 | 2.42k | setSegmentOverride(insn, SEG_OVERRIDE_ES, byte); |
499 | 2.42k | break; |
500 | 3.41k | case 0x64: |
501 | 3.41k | setSegmentOverride(insn, SEG_OVERRIDE_FS, byte); |
502 | 3.41k | break; |
503 | 5.01k | case 0x65: |
504 | 5.01k | setSegmentOverride(insn, SEG_OVERRIDE_GS, byte); |
505 | 5.01k | break; |
506 | 0 | default: |
507 | | // debug("Unhandled override"); |
508 | 0 | return -1; |
509 | 18.8k | } |
510 | 18.8k | insn->rexPrefix = 0; |
511 | 18.8k | break; |
512 | | |
513 | 18.3k | case 0x66: /* Operand-size override */ |
514 | 18.3k | insn->hasOpSize = true; |
515 | 18.3k | insn->prefix2 = byte; |
516 | 18.3k | insn->rexPrefix = 0; |
517 | 18.3k | break; |
518 | | |
519 | 6.48k | case 0x67: /* Address-size override */ |
520 | 6.48k | insn->hasAdSize = true; |
521 | 6.48k | insn->prefix3 = byte; |
522 | 6.48k | insn->rexPrefix = 0; |
523 | 6.48k | break; |
524 | 645k | default: |
525 | 645k | if (isREX(insn, byte)) { |
526 | | /* REX prefix byte */ |
527 | 49.5k | insn->rexPrefix = byte; |
528 | 596k | } else { |
529 | | /* Not a prefix byte */ |
530 | 596k | isPrefix = false; |
531 | 596k | } |
532 | 645k | break; |
533 | 771k | } |
534 | 771k | } |
535 | | |
536 | 596k | insn->vectorExtensionType = TYPE_NO_VEX_XOP; |
537 | | |
538 | 596k | if (byte == 0x62) { |
539 | 61.6k | uint8_t byte1, byte2; |
540 | | |
541 | 61.6k | if (consumeByte(insn, &byte1)) { |
542 | | // dbgprintf(insn, "Couldn't read second byte of EVEX prefix"); |
543 | 65 | return -1; |
544 | 65 | } |
545 | | |
546 | 61.6k | if (lookAtByte(insn, &byte2)) { |
547 | | // dbgprintf(insn, "Couldn't read third byte of EVEX prefix"); |
548 | 72 | unconsumeByte(insn); /* unconsume byte1 */ |
549 | 72 | unconsumeByte(insn); /* unconsume byte */ |
550 | 61.5k | } else { |
551 | 61.5k | if ((insn->mode == MODE_64BIT || |
552 | 38.1k | (byte1 & 0xc0) == 0xc0) && |
553 | 57.7k | ((~byte1 & 0xc) == 0xc) && ((byte2 & 0x4) == 0x4)) { |
554 | 57.6k | insn->vectorExtensionType = TYPE_EVEX; |
555 | 57.6k | } else { |
556 | 3.94k | unconsumeByte(insn); /* unconsume byte1 */ |
557 | 3.94k | unconsumeByte(insn); /* unconsume byte */ |
558 | 3.94k | } |
559 | 61.5k | } |
560 | | |
561 | 61.6k | if (insn->vectorExtensionType == TYPE_EVEX) { |
562 | 57.6k | insn->vectorExtensionPrefix[0] = byte; |
563 | 57.6k | insn->vectorExtensionPrefix[1] = byte1; |
564 | 57.6k | if (consumeByte(insn, |
565 | 57.6k | &insn->vectorExtensionPrefix[2])) { |
566 | | // dbgprintf(insn, "Couldn't read third byte of EVEX prefix"); |
567 | 0 | return -1; |
568 | 0 | } |
569 | | |
570 | 57.6k | if (consumeByte(insn, |
571 | 57.6k | &insn->vectorExtensionPrefix[3])) { |
572 | | // dbgprintf(insn, "Couldn't read fourth byte of EVEX prefix"); |
573 | 29 | return -1; |
574 | 29 | } |
575 | | |
576 | | /* We simulate the REX prefix for simplicity's sake */ |
577 | 57.5k | if (insn->mode == MODE_64BIT) { |
578 | 23.2k | insn->rexPrefix = |
579 | 23.2k | 0x40 | |
580 | 23.2k | (wFromEVEX3of4( |
581 | 23.2k | insn->vectorExtensionPrefix[2]) |
582 | 23.2k | << 3) | |
583 | 23.2k | (rFromEVEX2of4( |
584 | 23.2k | insn->vectorExtensionPrefix[1]) |
585 | 23.2k | << 2) | |
586 | 23.2k | (xFromEVEX2of4( |
587 | 23.2k | insn->vectorExtensionPrefix[1]) |
588 | 23.2k | << 1) | |
589 | 23.2k | (bFromEVEX2of4( |
590 | 23.2k | insn->vectorExtensionPrefix[1]) |
591 | 23.2k | << 0); |
592 | 23.2k | } |
593 | | |
594 | | // dbgprintf(insn, "Found EVEX prefix 0x%hhx 0x%hhx 0x%hhx 0x%hhx", |
595 | | // insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1], |
596 | | // insn->vectorExtensionPrefix[2], insn->vectorExtensionPrefix[3]); |
597 | 57.5k | } |
598 | 534k | } else if (byte == 0xc4) { |
599 | 6.64k | uint8_t byte1; |
600 | | |
601 | 6.64k | if (lookAtByte(insn, &byte1)) { |
602 | | // dbgprintf(insn, "Couldn't read second byte of VEX"); |
603 | 15 | return -1; |
604 | 15 | } |
605 | | |
606 | 6.63k | if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) |
607 | 5.49k | insn->vectorExtensionType = TYPE_VEX_3B; |
608 | 1.14k | else |
609 | 1.14k | unconsumeByte(insn); |
610 | | |
611 | 6.63k | if (insn->vectorExtensionType == TYPE_VEX_3B) { |
612 | 5.49k | insn->vectorExtensionPrefix[0] = byte; |
613 | 5.49k | consumeByte(insn, &insn->vectorExtensionPrefix[1]); |
614 | 5.49k | consumeByte(insn, &insn->vectorExtensionPrefix[2]); |
615 | | |
616 | | /* We simulate the REX prefix for simplicity's sake */ |
617 | 5.49k | if (insn->mode == MODE_64BIT) |
618 | 2.60k | insn->rexPrefix = |
619 | 2.60k | 0x40 | |
620 | 2.60k | (wFromVEX3of3( |
621 | 2.60k | insn->vectorExtensionPrefix[2]) |
622 | 2.60k | << 3) | |
623 | 2.60k | (rFromVEX2of3( |
624 | 2.60k | insn->vectorExtensionPrefix[1]) |
625 | 2.60k | << 2) | |
626 | 2.60k | (xFromVEX2of3( |
627 | 2.60k | insn->vectorExtensionPrefix[1]) |
628 | 2.60k | << 1) | |
629 | 2.60k | (bFromVEX2of3( |
630 | 2.60k | insn->vectorExtensionPrefix[1]) |
631 | 2.60k | << 0); |
632 | | |
633 | | // dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx 0x%hhx", |
634 | | // insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1], |
635 | | // insn->vectorExtensionPrefix[2]); |
636 | 5.49k | } |
637 | 527k | } else if (byte == 0xc5) { |
638 | 10.3k | uint8_t byte1; |
639 | | |
640 | 10.3k | if (lookAtByte(insn, &byte1)) { |
641 | | // dbgprintf(insn, "Couldn't read second byte of VEX"); |
642 | 20 | return -1; |
643 | 20 | } |
644 | | |
645 | 10.2k | if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) |
646 | 9.20k | insn->vectorExtensionType = TYPE_VEX_2B; |
647 | 1.08k | else |
648 | 1.08k | unconsumeByte(insn); |
649 | | |
650 | 10.2k | if (insn->vectorExtensionType == TYPE_VEX_2B) { |
651 | 9.20k | insn->vectorExtensionPrefix[0] = byte; |
652 | 9.20k | consumeByte(insn, &insn->vectorExtensionPrefix[1]); |
653 | | |
654 | 9.20k | if (insn->mode == MODE_64BIT) |
655 | 1.69k | insn->rexPrefix = |
656 | 1.69k | 0x40 | |
657 | 1.69k | (rFromVEX2of2( |
658 | 1.69k | insn->vectorExtensionPrefix[1]) |
659 | 1.69k | << 2); |
660 | | |
661 | 9.20k | switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) { |
662 | 3.96k | default: |
663 | 3.96k | break; |
664 | 5.24k | case VEX_PREFIX_66: |
665 | 5.24k | insn->hasOpSize = true; |
666 | 5.24k | break; |
667 | 9.20k | } |
668 | | |
669 | | // dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx", |
670 | | // insn->vectorExtensionPrefix[0], |
671 | | // insn->vectorExtensionPrefix[1]); |
672 | 9.20k | } |
673 | 517k | } else if (byte == 0x8f) { |
674 | 8.21k | uint8_t byte1; |
675 | | |
676 | 8.21k | if (lookAtByte(insn, &byte1)) { |
677 | | // dbgprintf(insn, "Couldn't read second byte of XOP"); |
678 | 16 | return -1; |
679 | 16 | } |
680 | | |
681 | 8.19k | if ((byte1 & 0x38) != |
682 | 8.19k | 0x0) /* 0 in these 3 bits is a POP instruction. */ |
683 | 7.52k | insn->vectorExtensionType = TYPE_XOP; |
684 | 672 | else |
685 | 672 | unconsumeByte(insn); |
686 | | |
687 | 8.19k | if (insn->vectorExtensionType == TYPE_XOP) { |
688 | 7.52k | insn->vectorExtensionPrefix[0] = byte; |
689 | 7.52k | consumeByte(insn, &insn->vectorExtensionPrefix[1]); |
690 | 7.52k | consumeByte(insn, &insn->vectorExtensionPrefix[2]); |
691 | | |
692 | | /* We simulate the REX prefix for simplicity's sake */ |
693 | 7.52k | if (insn->mode == MODE_64BIT) |
694 | 1.90k | insn->rexPrefix = |
695 | 1.90k | 0x40 | |
696 | 1.90k | (wFromXOP3of3( |
697 | 1.90k | insn->vectorExtensionPrefix[2]) |
698 | 1.90k | << 3) | |
699 | 1.90k | (rFromXOP2of3( |
700 | 1.90k | insn->vectorExtensionPrefix[1]) |
701 | 1.90k | << 2) | |
702 | 1.90k | (xFromXOP2of3( |
703 | 1.90k | insn->vectorExtensionPrefix[1]) |
704 | 1.90k | << 1) | |
705 | 1.90k | (bFromXOP2of3( |
706 | 1.90k | insn->vectorExtensionPrefix[1]) |
707 | 1.90k | << 0); |
708 | | |
709 | 7.52k | switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) { |
710 | 7.51k | default: |
711 | 7.51k | break; |
712 | 7.51k | case VEX_PREFIX_66: |
713 | 11 | insn->hasOpSize = true; |
714 | 11 | break; |
715 | 7.52k | } |
716 | | |
717 | | // dbgprintf(insn, "Found XOP prefix 0x%hhx 0x%hhx 0x%hhx", |
718 | | // insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1], |
719 | | // insn->vectorExtensionPrefix[2]); |
720 | 7.52k | } |
721 | 8.19k | } else |
722 | 509k | unconsumeByte(insn); |
723 | | |
724 | 596k | if (insn->repeatPrefix != 0) { |
725 | 39.0k | if (lookAtByte(insn, &nextByte)) |
726 | 1 | return -1; |
727 | | |
728 | | /* |
729 | | * REP prefix is present, and any of the following conditions are |
730 | | * met: |
731 | | * - it is followed by a LOCK (0xf0) prefix |
732 | | * - it is followed by an xchg instruction (except for 0x90 - NOP/PAUSE) |
733 | | * then it should be disassembled as a xacquire/xrelease not repne/rep. |
734 | | */ |
735 | 39.0k | if ((insn->hasLockPrefix || ((nextByte & 0xfe) == 0x86 || |
736 | 37.6k | (nextByte & 0xf8) == 0x90)) && |
737 | 2.47k | nextByte != 0x90) { |
738 | 1.77k | insn->xAcquireRelease = insn->repeatPrefix; |
739 | 1.77k | } |
740 | | |
741 | | /* |
742 | | * Also if the REP prefix is 0xf3, and the following condition is met: |
743 | | * - it is followed by a "mov mem, reg" (opcode 0x88/0x89) or |
744 | | * "mov mem, imm" (opcode 0xc6/0xc7) instructions. |
745 | | * then it should be disassembled as an xrelease not rep. |
746 | | */ |
747 | 39.0k | if (insn->repeatPrefix == 0xf3 && |
748 | 17.0k | (nextByte == 0x88 || nextByte == 0x89 || nextByte == 0xc6 || |
749 | 16.4k | nextByte == 0xc7)) { |
750 | 804 | insn->xAcquireRelease = insn->repeatPrefix; |
751 | 804 | } |
752 | 39.0k | } |
753 | | |
754 | 596k | if (insn->mode == MODE_16BIT) { |
755 | 164k | insn->registerSize = (insn->hasOpSize ? 4 : 2); |
756 | 164k | insn->addressSize = (insn->hasAdSize ? 4 : 2); |
757 | 164k | insn->displacementSize = (insn->hasAdSize ? 4 : 2); |
758 | 164k | insn->immediateSize = (insn->hasOpSize ? 4 : 2); |
759 | 164k | insn->immSize = (insn->hasOpSize ? 4 : 2); |
760 | 431k | } else if (insn->mode == MODE_32BIT) { |
761 | 202k | insn->registerSize = (insn->hasOpSize ? 2 : 4); |
762 | 202k | insn->addressSize = (insn->hasAdSize ? 2 : 4); |
763 | 202k | insn->displacementSize = (insn->hasAdSize ? 2 : 4); |
764 | 202k | insn->immediateSize = (insn->hasOpSize ? 2 : 4); |
765 | 202k | insn->immSize = (insn->hasOpSize ? 2 : 4); |
766 | 228k | } else if (insn->mode == MODE_64BIT) { |
767 | 228k | if (insn->rexPrefix && wFromREX(insn->rexPrefix)) { |
768 | 45.2k | insn->registerSize = 8; |
769 | 45.2k | insn->addressSize = (insn->hasAdSize ? 4 : 8); |
770 | 45.2k | insn->displacementSize = 4; |
771 | 45.2k | insn->immediateSize = 4; |
772 | 45.2k | insn->immSize = 4; |
773 | 183k | } else { |
774 | 183k | insn->registerSize = (insn->hasOpSize ? 2 : 4); |
775 | 183k | insn->addressSize = (insn->hasAdSize ? 4 : 8); |
776 | 183k | insn->displacementSize = (insn->hasOpSize ? 2 : 4); |
777 | 183k | insn->immediateSize = (insn->hasOpSize ? 2 : 4); |
778 | 183k | insn->immSize = (insn->hasOpSize ? 4 : 8); |
779 | 183k | } |
780 | 228k | } |
781 | | |
782 | 596k | return 0; |
783 | 596k | } |
784 | | |
785 | | static int readModRM(struct InternalInstruction *insn); |
786 | | |
787 | | /* |
788 | | * readOpcode - Reads the opcode (excepting the ModR/M byte in the case of |
789 | | * extended or escape opcodes). |
790 | | * |
791 | | * @param insn - The instruction whose opcode is to be read. |
792 | | * @return - 0 if the opcode could be read successfully; nonzero otherwise. |
793 | | */ |
794 | | static int readOpcode(struct InternalInstruction *insn) |
795 | 596k | { |
796 | 596k | uint8_t current; |
797 | | |
798 | | // dbgprintf(insn, "readOpcode()"); |
799 | | |
800 | 596k | insn->opcodeType = ONEBYTE; |
801 | | |
802 | 596k | if (insn->vectorExtensionType == TYPE_EVEX) { |
803 | 57.5k | switch (mmFromEVEX2of4(insn->vectorExtensionPrefix[1])) { |
804 | 4 | default: |
805 | | // dbgprintf(insn, "Unhandled mm field for instruction (0x%hhx)", |
806 | | // mmFromEVEX2of4(insn->vectorExtensionPrefix[1])); |
807 | 4 | return -1; |
808 | 17.4k | case VEX_LOB_0F: |
809 | 17.4k | insn->opcodeType = TWOBYTE; |
810 | 17.4k | return consumeByte(insn, &insn->opcode); |
811 | 19.0k | case VEX_LOB_0F38: |
812 | 19.0k | insn->opcodeType = THREEBYTE_38; |
813 | 19.0k | return consumeByte(insn, &insn->opcode); |
814 | 21.0k | case VEX_LOB_0F3A: |
815 | 21.0k | insn->opcodeType = THREEBYTE_3A; |
816 | 21.0k | return consumeByte(insn, &insn->opcode); |
817 | 57.5k | } |
818 | 538k | } else if (insn->vectorExtensionType == TYPE_VEX_3B) { |
819 | 5.49k | switch (mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])) { |
820 | 25 | default: |
821 | | // dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)", |
822 | | // mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])); |
823 | 25 | return -1; |
824 | 688 | case VEX_LOB_0F: |
825 | | //insn->twoByteEscape = 0x0f; |
826 | 688 | insn->opcodeType = TWOBYTE; |
827 | 688 | return consumeByte(insn, &insn->opcode); |
828 | 2.85k | case VEX_LOB_0F38: |
829 | | //insn->twoByteEscape = 0x0f; |
830 | 2.85k | insn->opcodeType = THREEBYTE_38; |
831 | 2.85k | return consumeByte(insn, &insn->opcode); |
832 | 1.92k | case VEX_LOB_0F3A: |
833 | | //insn->twoByteEscape = 0x0f; |
834 | 1.92k | insn->opcodeType = THREEBYTE_3A; |
835 | 1.92k | return consumeByte(insn, &insn->opcode); |
836 | 5.49k | } |
837 | 532k | } else if (insn->vectorExtensionType == TYPE_VEX_2B) { |
838 | | //insn->twoByteEscape = 0x0f; |
839 | 9.20k | insn->opcodeType = TWOBYTE; |
840 | 9.20k | return consumeByte(insn, &insn->opcode); |
841 | 523k | } else if (insn->vectorExtensionType == TYPE_XOP) { |
842 | 7.52k | switch (mmmmmFromXOP2of3(insn->vectorExtensionPrefix[1])) { |
843 | 19 | default: |
844 | | // dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)", |
845 | | // mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])); |
846 | 19 | return -1; |
847 | 6.48k | case XOP_MAP_SELECT_8: |
848 | 6.48k | insn->opcodeType = XOP8_MAP; |
849 | 6.48k | return consumeByte(insn, &insn->opcode); |
850 | 747 | case XOP_MAP_SELECT_9: |
851 | 747 | insn->opcodeType = XOP9_MAP; |
852 | 747 | return consumeByte(insn, &insn->opcode); |
853 | 270 | case XOP_MAP_SELECT_A: |
854 | 270 | insn->opcodeType = XOPA_MAP; |
855 | 270 | return consumeByte(insn, &insn->opcode); |
856 | 7.52k | } |
857 | 7.52k | } |
858 | | |
859 | 516k | if (consumeByte(insn, ¤t)) |
860 | 0 | return -1; |
861 | | |
862 | | // save this first byte for MOVcr, MOVdr, MOVrc, MOVrd |
863 | 516k | insn->firstByte = current; |
864 | | |
865 | 516k | if (current == 0x0f) { |
866 | | // dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current); |
867 | 43.0k | insn->twoByteEscape = current; |
868 | | |
869 | 43.0k | if (consumeByte(insn, ¤t)) |
870 | 63 | return -1; |
871 | | |
872 | 43.0k | if (current == 0x38) { |
873 | | // dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current); |
874 | 1.25k | if (consumeByte(insn, ¤t)) |
875 | 2 | return -1; |
876 | | |
877 | 1.25k | insn->opcodeType = THREEBYTE_38; |
878 | 41.7k | } else if (current == 0x3a) { |
879 | | // dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current); |
880 | 506 | if (consumeByte(insn, ¤t)) |
881 | 2 | return -1; |
882 | | |
883 | 504 | insn->opcodeType = THREEBYTE_3A; |
884 | 41.2k | } else if (current == 0x0f) { |
885 | | // dbgprintf(insn, "Found a 3dnow escape prefix (0x%hhx)", current); |
886 | | // Consume operands before the opcode to comply with the 3DNow encoding |
887 | 532 | if (readModRM(insn)) |
888 | 6 | return -1; |
889 | | |
890 | 526 | if (consumeByte(insn, ¤t)) |
891 | 5 | return -1; |
892 | | |
893 | 521 | insn->opcodeType = THREEDNOW_MAP; |
894 | 40.7k | } else { |
895 | | // dbgprintf(insn, "Didn't find a three-byte escape prefix"); |
896 | 40.7k | insn->opcodeType = TWOBYTE; |
897 | 40.7k | } |
898 | 43.0k | } |
899 | | |
900 | | /* |
901 | | * At this point we have consumed the full opcode. |
902 | | * Anything we consume from here on must be unconsumed. |
903 | | */ |
904 | | |
905 | 516k | insn->opcode = current; |
906 | | |
907 | 516k | return 0; |
908 | 516k | } |
909 | | |
910 | | // Hacky for FEMMS |
911 | | #define GET_INSTRINFO_ENUM |
912 | | #ifndef CAPSTONE_X86_REDUCE |
913 | | #include "X86GenInstrInfo.inc" |
914 | | #else |
915 | | #include "X86GenInstrInfo_reduce.inc" |
916 | | #endif |
917 | | |
918 | | /* |
919 | | * getIDWithAttrMask - Determines the ID of an instruction, consuming |
920 | | * the ModR/M byte as appropriate for extended and escape opcodes, |
921 | | * and using a supplied attribute mask. |
922 | | * |
923 | | * @param instructionID - A pointer whose target is filled in with the ID of the |
924 | | * instruction. |
925 | | * @param insn - The instruction whose ID is to be determined. |
926 | | * @param attrMask - The attribute mask to search. |
927 | | * @return - 0 if the ModR/M could be read when needed or was not |
928 | | * needed; nonzero otherwise. |
929 | | */ |
930 | | static int getIDWithAttrMask(uint16_t *instructionID, |
931 | | struct InternalInstruction *insn, |
932 | | uint16_t attrMask) |
933 | 795k | { |
934 | 795k | bool hasModRMExtension; |
935 | | |
936 | 795k | InstructionContext instructionClass = contextForAttrs(attrMask); |
937 | | |
938 | 795k | hasModRMExtension = |
939 | 795k | modRMRequired(insn->opcodeType, instructionClass, insn->opcode); |
940 | | |
941 | 795k | if (hasModRMExtension) { |
942 | 457k | if (readModRM(insn)) |
943 | 1.75k | return -1; |
944 | | |
945 | 455k | *instructionID = decode(insn->opcodeType, instructionClass, |
946 | 455k | insn->opcode, insn->modRM); |
947 | 455k | } else { |
948 | 338k | *instructionID = decode(insn->opcodeType, instructionClass, |
949 | 338k | insn->opcode, 0); |
950 | 338k | } |
951 | | |
952 | 793k | return 0; |
953 | 795k | } |
954 | | |
955 | | /* |
956 | | * is16BitEquivalent - Determines whether two instruction names refer to |
957 | | * equivalent instructions but one is 16-bit whereas the other is not. |
958 | | * |
959 | | * @param orig - The instruction ID that is not 16-bit |
960 | | * @param equiv - The instruction ID that is 16-bit |
961 | | */ |
962 | | static bool is16BitEquivalent(unsigned orig, unsigned equiv) |
963 | 146k | { |
964 | 146k | size_t i; |
965 | 146k | uint16_t idx; |
966 | | |
967 | 146k | if ((idx = x86_16_bit_eq_lookup[orig]) != 0) { |
968 | 74.9k | for (i = idx - 1; i < ARR_SIZE(x86_16_bit_eq_tbl) && |
969 | 74.9k | x86_16_bit_eq_tbl[i].first == orig; |
970 | 73.0k | i++) { |
971 | 73.0k | if (x86_16_bit_eq_tbl[i].second == equiv) |
972 | 71.2k | return true; |
973 | 73.0k | } |
974 | 73.0k | } |
975 | | |
976 | 75.6k | return false; |
977 | 146k | } |
978 | | |
979 | | /* |
980 | | * is64Bit - Determines whether this instruction is a 64-bit instruction. |
981 | | * |
982 | | * @param name - The instruction that is not 16-bit |
983 | | */ |
984 | | static bool is64Bit(uint16_t id) |
985 | 23.2k | { |
986 | 23.2k | unsigned int i = find_insn(id); |
987 | 23.2k | if (i != -1) { |
988 | 23.1k | return insns[i].is64bit; |
989 | 23.1k | } |
990 | | |
991 | | // not found?? |
992 | 38 | return false; |
993 | 23.2k | } |
994 | | |
995 | | typedef enum { |
996 | | DO_NOT_RESOLVE = 0, |
997 | | IGNORE_REP = 1, |
998 | | IGNORE_DATA_SIZE = 2, |
999 | | } MandatoryPrefixResolution; |
1000 | | |
1001 | | /* |
1002 | | * shouldResolveMandatoryPrefixConflict - Resolves conflicts between the |
1003 | | * data size override prefix and the REP/REPNZ prefixes in the attribute |
1004 | | * mask when needed. |
1005 | | * |
1006 | | * We need to resolve these conflicts, because the TableGen lookups we |
1007 | | * perform distinguish between instructions with and without REP. |
1008 | | * For example, there may be an entry for SHLD with a DATA16 data size |
1009 | | * override prefix, but no entry for REP + DATA16. |
1010 | | * These entries are split, because in some cases the REP and DATA16 |
1011 | | * prefixes are used as mandatory prefixes. |
1012 | | * When both are mandatory prefixes, the effect of prefixing both |
1013 | | * to an instruction at the same time is not specified by |
1014 | | * reference manuals. |
1015 | | * |
1016 | | * Conflicts are resolved by one of these three resolutions: |
1017 | | * - If conflicts should not be resolved, take no action. |
1018 | | * - If conflicts should be resolved and the instruction has no |
1019 | | * mandatory prefixes, resolves in favor of data size override. |
1020 | | * - If conflicts should be resolved and the instruction has mandatory |
1021 | | * prefixes, resolves in favor of REP/REPNZ. |
1022 | | * |
1023 | | * @param insn - The instruction |
1024 | | * @param attrMask - The current attribute mask. |
1025 | | */ |
1026 | | static uint16_t resolveMandatoryPrefixConflict(struct InternalInstruction *insn, |
1027 | | uint16_t attrMask) |
1028 | 2.92k | { |
1029 | 2.92k | MandatoryPrefixResolution resolution = DO_NOT_RESOLVE; |
1030 | | |
1031 | | // We inspect the opcode map and opcode to determine how we need to resolve |
1032 | | // a mandatory prefix conflict. |
1033 | 2.92k | switch (insn->opcodeType) { |
1034 | | // No one-byte opcodes have mandatory prefixes. |
1035 | 218 | case ONEBYTE: |
1036 | 218 | resolution = DO_NOT_RESOLVE; |
1037 | 218 | break; |
1038 | 2.10k | case TWOBYTE: |
1039 | | // Exceptions for instructions that operate on data size-overridable |
1040 | | // operands. |
1041 | 2.10k | if ( |
1042 | | // XADD |
1043 | 2.10k | (insn->opcode & 0xFE) == 0xC0 |
1044 | | |
1045 | | // BSWAP |
1046 | 1.90k | || (insn->opcode & 0xF8) == 0xC8 |
1047 | | |
1048 | | // CMPXCHG, LSS, BTR, LFS, LGS, MOVZX |
1049 | 1.69k | || (insn->opcode & 0xF8) == 0xB0 |
1050 | | |
1051 | | // Group 16, various NOPs |
1052 | 1.49k | || (insn->opcode & 0xF8) == 0x18 |
1053 | | |
1054 | | // UD0 |
1055 | 1.29k | || insn->opcode == 0xFF) { |
1056 | 1.04k | resolution = IGNORE_REP; |
1057 | 1.04k | break; |
1058 | 1.04k | } |
1059 | | |
1060 | | // We inspect the instruction to determine if it operates on xmm |
1061 | | // registers or general-purpose registers. |
1062 | | // |
1063 | | // If it operates on general purpose registers, the data size override |
1064 | | // prefix is not a mandatory prefix and should not be ignored. |
1065 | | // In most cases, this also means that the REP prefix is not a mandatory |
1066 | | // prefix and should be ignored. |
1067 | | // |
1068 | | // If the instruction operates on xmm registers, the data size override |
1069 | | // is used to select the operation type (SS, SD, PS, or PD). |
1070 | | // In this case, the REP prefixes take priority over the data size [1] |
1071 | | // override prefixes, and when both are present the data size override |
1072 | | // prefix should be ignored. |
1073 | | // |
1074 | | // The exception is 0xB0, where the REP prefixes are mandatory prefixes |
1075 | | // but the data size override prefix should still be respected. |
1076 | | // For this case we return DO_NOT_RESOLVE, which returns attrMask as-is. |
1077 | | // |
1078 | | // [1]: https://stackoverflow.com/a/7197365 |
1079 | 1.05k | switch (insn->opcode & 0xf0) { |
1080 | 5 | case 0x10: |
1081 | 61 | case 0x50: |
1082 | 62 | case 0x60: |
1083 | 66 | case 0x70: |
1084 | 254 | case 0xC0: |
1085 | 254 | case 0xD0: |
1086 | 255 | case 0xE0: |
1087 | 258 | case 0xF0: |
1088 | 258 | resolution = IGNORE_DATA_SIZE; |
1089 | 258 | break; |
1090 | 9 | case 0x00: |
1091 | 10 | case 0x20: |
1092 | 21 | case 0x30: |
1093 | 29 | case 0x40: |
1094 | 476 | case 0x80: |
1095 | 480 | case 0x90: |
1096 | 480 | case 0xA0: |
1097 | 480 | resolution = IGNORE_REP; |
1098 | 480 | break; |
1099 | 315 | default: // 0xB0 |
1100 | 315 | resolution = DO_NOT_RESOLVE; |
1101 | 315 | break; |
1102 | 1.05k | } |
1103 | 1.05k | break; |
1104 | 1.05k | case THREEBYTE_38: |
1105 | | // Exception: the ADOX and CRC32 instructions. |
1106 | | // These ignore the data size override prefix even though they |
1107 | | // operate on general-purpose registers. |
1108 | 412 | if ((insn->opcode & 0xF0) == 0xF0) { |
1109 | 194 | resolution = IGNORE_DATA_SIZE; |
1110 | 194 | break; |
1111 | 194 | } |
1112 | | |
1113 | | // Do not need to be resolved, all REP+DATA16 combinations are UD |
1114 | | // or separately specified. |
1115 | 218 | resolution = DO_NOT_RESOLVE; |
1116 | 218 | break; |
1117 | 0 | case THREEBYTE_3A: |
1118 | | // Do not need to be resolved, all REP+DATA16 combinations are UD |
1119 | | // or separately specified. |
1120 | 0 | resolution = DO_NOT_RESOLVE; |
1121 | 0 | break; |
1122 | 0 | case XOP8_MAP: |
1123 | 0 | case XOP9_MAP: |
1124 | 0 | case XOPA_MAP: |
1125 | | // These instructions do not appear to operate on XMM/SSE registers, |
1126 | | // so the REP prefixes can be safely ignored. |
1127 | 0 | resolution = IGNORE_REP; |
1128 | 0 | break; |
1129 | 196 | case THREEDNOW_MAP: |
1130 | | // AMD Reference Manual Volume 3, Section 1.2.1, states that all |
1131 | | // 3DNow! instructions ignore the data size override prefix. |
1132 | 196 | resolution = IGNORE_DATA_SIZE; |
1133 | 196 | break; |
1134 | 2.92k | } |
1135 | | |
1136 | 2.92k | switch (resolution) { |
1137 | 1.52k | case IGNORE_REP: |
1138 | 1.52k | return attrMask & ~(ATTR_XD | ATTR_XS); |
1139 | 648 | case IGNORE_DATA_SIZE: |
1140 | 648 | return attrMask & ~ATTR_OPSIZE; |
1141 | 0 | default: |
1142 | 751 | case DO_NOT_RESOLVE: |
1143 | 751 | return attrMask; |
1144 | 2.92k | } |
1145 | 2.92k | } |
1146 | | |
1147 | | /* |
1148 | | * getID - Determines the ID of an instruction, consuming the ModR/M byte as |
1149 | | * appropriate for extended and escape opcodes. Determines the attributes and |
1150 | | * context for the instruction before doing so. |
1151 | | * |
1152 | | * @param insn - The instruction whose ID is to be determined. |
1153 | | * @return - 0 if the ModR/M could be read when needed or was not needed; |
1154 | | * nonzero otherwise. |
1155 | | */ |
1156 | | static int getID(struct InternalInstruction *insn, cs_mode mode) |
1157 | 595k | { |
1158 | 595k | uint16_t attrMask; |
1159 | 595k | uint16_t instructionID; |
1160 | 595k | bool rexWOverridesOpSize; |
1161 | | |
1162 | | /* REX.W overrides the operand-sized prefix for near RET in 64-bit mode */ |
1163 | 595k | rexWOverridesOpSize = insn->mode == MODE_64BIT && insn->hasOpSize && |
1164 | 12.3k | insn->opcodeType == ONEBYTE && |
1165 | 7.58k | (insn->opcode == 0xC2 || insn->opcode == 0xC3) && |
1166 | 866 | (insn->rexPrefix & 0x08); |
1167 | | |
1168 | 595k | attrMask = ATTR_NONE; |
1169 | | |
1170 | 595k | if (insn->mode == MODE_64BIT) |
1171 | 228k | attrMask |= ATTR_64BIT; |
1172 | | |
1173 | 595k | if (insn->vectorExtensionType != TYPE_NO_VEX_XOP) { |
1174 | 79.6k | attrMask |= (insn->vectorExtensionType == TYPE_EVEX) ? |
1175 | 57.5k | ATTR_EVEX : |
1176 | 79.6k | ATTR_VEX; |
1177 | | |
1178 | 79.6k | if (insn->vectorExtensionType == TYPE_EVEX) { |
1179 | 57.5k | switch (ppFromEVEX3of4( |
1180 | 57.5k | insn->vectorExtensionPrefix[2])) { |
1181 | 49.5k | case VEX_PREFIX_66: |
1182 | 49.5k | attrMask |= ATTR_OPSIZE; |
1183 | 49.5k | break; |
1184 | 2.36k | case VEX_PREFIX_F3: |
1185 | 2.36k | attrMask |= ATTR_XS; |
1186 | 2.36k | break; |
1187 | 1.64k | case VEX_PREFIX_F2: |
1188 | 1.64k | attrMask |= ATTR_XD; |
1189 | 1.64k | break; |
1190 | 57.5k | } |
1191 | | |
1192 | 57.5k | if (zFromEVEX4of4(insn->vectorExtensionPrefix[3])) |
1193 | 5.91k | attrMask |= ATTR_EVEXKZ; |
1194 | 57.5k | if (bFromEVEX4of4(insn->vectorExtensionPrefix[3])) |
1195 | 22.2k | attrMask |= ATTR_EVEXB; |
1196 | 57.5k | if (aaaFromEVEX4of4(insn->vectorExtensionPrefix[3])) |
1197 | 41.5k | attrMask |= ATTR_EVEXK; |
1198 | 57.5k | if (lFromEVEX4of4(insn->vectorExtensionPrefix[3])) |
1199 | 31.5k | attrMask |= ATTR_EVEXL; |
1200 | 57.5k | if (l2FromEVEX4of4(insn->vectorExtensionPrefix[3])) |
1201 | 25.4k | attrMask |= ATTR_EVEXL2; |
1202 | 57.5k | } else if (insn->vectorExtensionType == TYPE_VEX_3B) { |
1203 | 5.45k | switch (ppFromVEX3of3(insn->vectorExtensionPrefix[2])) { |
1204 | 4.74k | case VEX_PREFIX_66: |
1205 | 4.74k | attrMask |= ATTR_OPSIZE; |
1206 | 4.74k | break; |
1207 | 262 | case VEX_PREFIX_F3: |
1208 | 262 | attrMask |= ATTR_XS; |
1209 | 262 | break; |
1210 | 234 | case VEX_PREFIX_F2: |
1211 | 234 | attrMask |= ATTR_XD; |
1212 | 234 | break; |
1213 | 5.45k | } |
1214 | | |
1215 | 5.45k | if (lFromVEX3of3(insn->vectorExtensionPrefix[2])) |
1216 | 3.09k | attrMask |= ATTR_VEXL; |
1217 | 16.6k | } else if (insn->vectorExtensionType == TYPE_VEX_2B) { |
1218 | 9.18k | switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) { |
1219 | 5.23k | case VEX_PREFIX_66: |
1220 | 5.23k | attrMask |= ATTR_OPSIZE; |
1221 | 5.23k | break; |
1222 | 1.07k | case VEX_PREFIX_F3: |
1223 | 1.07k | attrMask |= ATTR_XS; |
1224 | 1.07k | break; |
1225 | 1.02k | case VEX_PREFIX_F2: |
1226 | 1.02k | attrMask |= ATTR_XD; |
1227 | 1.02k | break; |
1228 | 9.18k | } |
1229 | | |
1230 | 9.18k | if (lFromVEX2of2(insn->vectorExtensionPrefix[1])) |
1231 | 6.35k | attrMask |= ATTR_VEXL; |
1232 | 9.18k | } else if (insn->vectorExtensionType == TYPE_XOP) { |
1233 | 7.48k | switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) { |
1234 | 6 | case VEX_PREFIX_66: |
1235 | 6 | attrMask |= ATTR_OPSIZE; |
1236 | 6 | break; |
1237 | 7 | case VEX_PREFIX_F3: |
1238 | 7 | attrMask |= ATTR_XS; |
1239 | 7 | break; |
1240 | 5 | case VEX_PREFIX_F2: |
1241 | 5 | attrMask |= ATTR_XD; |
1242 | 5 | break; |
1243 | 7.48k | } |
1244 | | |
1245 | 7.48k | if (lFromXOP3of3(insn->vectorExtensionPrefix[2])) |
1246 | 685 | attrMask |= ATTR_VEXL; |
1247 | 7.48k | } else { |
1248 | 0 | return -1; |
1249 | 0 | } |
1250 | 516k | } else { |
1251 | 516k | if (insn->hasOpSize && insn->mode != MODE_16BIT && |
1252 | 14.8k | !rexWOverridesOpSize) { |
1253 | 14.5k | attrMask |= ATTR_OPSIZE; |
1254 | 14.5k | } |
1255 | 516k | if (insn->hasAdSize) |
1256 | 4.95k | attrMask |= ATTR_ADSIZE; |
1257 | 516k | if (insn->opcodeType == ONEBYTE) { |
1258 | 473k | if (insn->repeatPrefix == 0xf3 && |
1259 | 11.8k | (insn->opcode == 0x90)) |
1260 | | // Special support for PAUSE |
1261 | 508 | attrMask |= ATTR_XS; |
1262 | 473k | } else { |
1263 | 43.0k | if (insn->repeatPrefix == 0xf2) |
1264 | 6.06k | attrMask |= ATTR_XD; |
1265 | 36.9k | else if (insn->repeatPrefix == 0xf3) |
1266 | 4.29k | attrMask |= ATTR_XS; |
1267 | 43.0k | } |
1268 | | |
1269 | | /* BSF/BSR ignore F2; F3 selects TZCNT/LZCNT instead. */ |
1270 | 516k | if (insn->opcodeType == TWOBYTE && |
1271 | 40.7k | (insn->opcode == 0xBC || insn->opcode == 0xBD)) |
1272 | 680 | attrMask &= ~ATTR_XD; |
1273 | | |
1274 | 516k | if ((attrMask & ATTR_OPSIZE) && |
1275 | 14.5k | (attrMask & (ATTR_XD | ATTR_XS))) { |
1276 | 2.92k | attrMask = |
1277 | 2.92k | resolveMandatoryPrefixConflict(insn, attrMask); |
1278 | 2.92k | } |
1279 | 516k | } |
1280 | | |
1281 | 595k | if (insn->rexPrefix & 0x08) { |
1282 | 45.2k | attrMask |= ATTR_REXW; |
1283 | 45.2k | attrMask &= ~ATTR_ADSIZE; |
1284 | 45.2k | } |
1285 | | |
1286 | | /* |
1287 | | * JCXZ/JECXZ need special handling for 16-bit mode because the meaning |
1288 | | * of the AdSize prefix is inverted w.r.t. 32-bit mode. |
1289 | | */ |
1290 | 595k | if (insn->mode == MODE_16BIT && insn->opcodeType == ONEBYTE && |
1291 | 133k | insn->opcode == 0xE3) |
1292 | 970 | attrMask ^= ATTR_ADSIZE; |
1293 | | |
1294 | | /* |
1295 | | * CALL/JMP ignore 66 in 64-bit mode. Near Jcc preserve their previous |
1296 | | * behavior unless an Intel or AMD mode is selected. |
1297 | | */ |
1298 | 595k | if ((insn->mode == MODE_64BIT) && insn->hasOpSize) { |
1299 | 12.3k | switch (insn->opcode) { |
1300 | 400 | case 0xE8: |
1301 | 919 | case 0xE9: |
1302 | | // Take care of psubsb and other mmx instructions. |
1303 | 919 | if (insn->opcodeType == ONEBYTE) { |
1304 | 240 | attrMask ^= ATTR_OPSIZE; |
1305 | 240 | insn->immediateSize = 4; |
1306 | 240 | insn->displacementSize = 4; |
1307 | 240 | } |
1308 | 919 | break; |
1309 | 317 | case 0x80: |
1310 | 480 | case 0x81: |
1311 | 787 | case 0x82: |
1312 | 994 | case 0x83: |
1313 | 1.20k | case 0x84: |
1314 | 1.40k | case 0x85: |
1315 | 1.60k | case 0x86: |
1316 | 2.03k | case 0x87: |
1317 | 2.31k | case 0x88: |
1318 | 2.51k | case 0x89: |
1319 | 2.72k | case 0x8A: |
1320 | 2.91k | case 0x8B: |
1321 | 3.13k | case 0x8C: |
1322 | 3.33k | case 0x8D: |
1323 | 3.53k | case 0x8E: |
1324 | 3.73k | case 0x8F: |
1325 | | // Take care of lea and three byte ops. |
1326 | 3.73k | if (insn->opcodeType != TWOBYTE) |
1327 | 3.16k | break; |
1328 | | |
1329 | 565 | if ((x86_has_feature(mode, CS_MODE_X86_INTEL) || |
1330 | 565 | x86_has_feature(mode, CS_MODE_X86_AMD)) && |
1331 | 0 | insn->vectorExtensionType == TYPE_NO_VEX_XOP) { |
1332 | 0 | if ((x86_has_feature(mode, CS_MODE_X86_AMD)) && |
1333 | 0 | !wFromREX(insn->rexPrefix)) { |
1334 | 0 | attrMask |= ATTR_OPSIZE; |
1335 | 0 | insn->immediateSize = 2; |
1336 | 0 | insn->displacementSize = 2; |
1337 | 0 | insn->immSize = 2; |
1338 | 0 | } else { |
1339 | 0 | attrMask &= ~ATTR_OPSIZE; |
1340 | 0 | insn->immediateSize = 4; |
1341 | 0 | insn->displacementSize = 4; |
1342 | 0 | insn->immSize = 8; |
1343 | 0 | } |
1344 | 565 | } else if (insn->opcode >= 0x82) { |
1345 | 332 | attrMask ^= ATTR_OPSIZE; |
1346 | 332 | insn->immediateSize = 4; |
1347 | 332 | insn->displacementSize = 4; |
1348 | 332 | } |
1349 | 565 | break; |
1350 | 12.3k | } |
1351 | 12.3k | } |
1352 | | |
1353 | | /* The following clauses compensate for limitations of the tables. */ |
1354 | 595k | if (insn->mode != MODE_64BIT && |
1355 | 367k | insn->vectorExtensionType != TYPE_NO_VEX_XOP) { |
1356 | 50.2k | if (getIDWithAttrMask(&instructionID, insn, attrMask)) { |
1357 | 21 | return -1; |
1358 | 21 | } |
1359 | | |
1360 | | /* |
1361 | | * The tables can't distinguish between cases where the W-bit is used to |
1362 | | * select register size and cases where it's a required part of the opcode. |
1363 | | */ |
1364 | 50.2k | if ((insn->vectorExtensionType == TYPE_EVEX && |
1365 | 34.2k | wFromEVEX3of4(insn->vectorExtensionPrefix[2])) || |
1366 | 28.8k | (insn->vectorExtensionType == TYPE_VEX_3B && |
1367 | 2.87k | wFromVEX3of3(insn->vectorExtensionPrefix[2])) || |
1368 | 27.4k | (insn->vectorExtensionType == TYPE_XOP && |
1369 | 23.2k | wFromXOP3of3(insn->vectorExtensionPrefix[2]))) { |
1370 | 23.2k | uint16_t instructionIDWithREXW; |
1371 | | |
1372 | 23.2k | if (getIDWithAttrMask(&instructionIDWithREXW, insn, |
1373 | 23.2k | attrMask | ATTR_REXW)) { |
1374 | 3 | insn->instructionID = instructionID; |
1375 | 3 | insn->spec = specifierForUID(instructionID); |
1376 | 3 | return 0; |
1377 | 3 | } |
1378 | | |
1379 | | // If not a 64-bit instruction. Switch the opcode. |
1380 | 23.2k | if (!is64Bit(instructionIDWithREXW)) { |
1381 | 21.6k | insn->instructionID = instructionIDWithREXW; |
1382 | 21.6k | insn->spec = |
1383 | 21.6k | specifierForUID(instructionIDWithREXW); |
1384 | | |
1385 | 21.6k | return 0; |
1386 | 21.6k | } |
1387 | 23.2k | } |
1388 | 50.2k | } |
1389 | | |
1390 | | /* |
1391 | | * Absolute moves, umonitor, and movdir64b need special handling. |
1392 | | * -For 16-bit mode because the meaning of the AdSize and OpSize prefixes are |
1393 | | * inverted w.r.t. |
1394 | | * -For 32-bit mode we need to ensure the ADSIZE prefix is observed in |
1395 | | * any position. |
1396 | | */ |
1397 | 574k | if ((insn->opcodeType == ONEBYTE && ((insn->opcode & 0xFC) == 0xA0)) || |
1398 | 568k | (insn->opcodeType == TWOBYTE && (insn->opcode == 0xAE)) || |
1399 | 567k | (insn->opcodeType == THREEBYTE_38 && insn->opcode == 0xF8)) { |
1400 | | /* Make sure we observed the prefixes in any position. */ |
1401 | 6.79k | if (insn->hasAdSize) |
1402 | 357 | attrMask |= ATTR_ADSIZE; |
1403 | | |
1404 | 6.79k | if (insn->hasOpSize) |
1405 | 428 | attrMask |= ATTR_OPSIZE; |
1406 | | |
1407 | | /* In 16-bit, invert the attributes. */ |
1408 | 6.79k | if (insn->mode == MODE_16BIT) { |
1409 | 2.95k | attrMask ^= ATTR_ADSIZE; |
1410 | | |
1411 | | /* The OpSize attribute is only valid with the absolute moves. */ |
1412 | 2.95k | if (insn->opcodeType == ONEBYTE && |
1413 | 2.72k | ((insn->opcode & 0xFC) == 0xA0)) |
1414 | 2.72k | attrMask ^= ATTR_OPSIZE; |
1415 | 2.95k | } |
1416 | | |
1417 | 6.79k | if (getIDWithAttrMask(&instructionID, insn, attrMask)) { |
1418 | 12 | return -1; |
1419 | 12 | } |
1420 | | |
1421 | 6.77k | insn->instructionID = instructionID; |
1422 | 6.77k | insn->spec = specifierForUID(instructionID); |
1423 | | |
1424 | 6.77k | return 0; |
1425 | 6.79k | } |
1426 | 567k | if (getIDWithAttrMask(&instructionID, insn, attrMask)) { |
1427 | 1.71k | return -1; |
1428 | 1.71k | } |
1429 | | |
1430 | 565k | if ((insn->mode == MODE_16BIT || |
1431 | 413k | (insn->hasOpSize && !rexWOverridesOpSize)) && |
1432 | 170k | !(attrMask & ATTR_OPSIZE)) { |
1433 | | /* |
1434 | | * The instruction tables make no distinction between instructions that |
1435 | | * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a |
1436 | | * particular spot (i.e., many MMX operations). In general we're |
1437 | | * conservative, but in the specific case where OpSize is present but not |
1438 | | * in the right place we check if there's a 16-bit operation. |
1439 | | */ |
1440 | 146k | const struct InstructionSpecifier *spec; |
1441 | 146k | uint16_t instructionIDWithOpsize; |
1442 | | |
1443 | 146k | spec = specifierForUID(instructionID); |
1444 | | |
1445 | 146k | if (getIDWithAttrMask(&instructionIDWithOpsize, insn, |
1446 | 146k | attrMask | ATTR_OPSIZE)) { |
1447 | | /* |
1448 | | * ModRM required with OpSize but not present; give up and return version |
1449 | | * without OpSize set |
1450 | | */ |
1451 | 5 | insn->instructionID = instructionID; |
1452 | 5 | insn->spec = spec; |
1453 | | |
1454 | 5 | return 0; |
1455 | 5 | } |
1456 | | |
1457 | 146k | if (is16BitEquivalent(instructionID, instructionIDWithOpsize) && |
1458 | 71.2k | (insn->mode == MODE_16BIT) ^ insn->hasOpSize) { |
1459 | 70.6k | insn->instructionID = instructionIDWithOpsize; |
1460 | 70.6k | insn->spec = specifierForUID(instructionIDWithOpsize); |
1461 | 76.2k | } else { |
1462 | 76.2k | insn->instructionID = instructionID; |
1463 | 76.2k | insn->spec = spec; |
1464 | 76.2k | } |
1465 | | |
1466 | 146k | return 0; |
1467 | 146k | } |
1468 | | |
1469 | 418k | if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 && |
1470 | 2.16k | insn->rexPrefix & 0x01) { |
1471 | | /* |
1472 | | * NOOP shouldn't decode as NOOP if REX.b is set. Instead |
1473 | | * it should decode as XCHG %r8, %eax. |
1474 | | */ |
1475 | 621 | const struct InstructionSpecifier *spec; |
1476 | 621 | uint16_t instructionIDWithNewOpcode; |
1477 | 621 | const struct InstructionSpecifier *specWithNewOpcode; |
1478 | | |
1479 | 621 | spec = specifierForUID(instructionID); |
1480 | | |
1481 | | /* Borrow opcode from one of the other XCHGar opcodes */ |
1482 | 621 | insn->opcode = 0x91; |
1483 | | |
1484 | 621 | if (getIDWithAttrMask(&instructionIDWithNewOpcode, insn, |
1485 | 621 | attrMask)) { |
1486 | 0 | insn->opcode = 0x90; |
1487 | |
|
1488 | 0 | insn->instructionID = instructionID; |
1489 | 0 | insn->spec = spec; |
1490 | |
|
1491 | 0 | return 0; |
1492 | 0 | } |
1493 | | |
1494 | 621 | specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode); |
1495 | | |
1496 | | /* Change back */ |
1497 | 621 | insn->opcode = 0x90; |
1498 | | |
1499 | 621 | insn->instructionID = instructionIDWithNewOpcode; |
1500 | 621 | insn->spec = specWithNewOpcode; |
1501 | | |
1502 | 621 | return 0; |
1503 | 621 | } |
1504 | | |
1505 | 418k | insn->instructionID = instructionID; |
1506 | 418k | insn->spec = specifierForUID(insn->instructionID); |
1507 | | |
1508 | 418k | return 0; |
1509 | 418k | } |
1510 | | |
1511 | | /* |
1512 | | * readSIB - Consumes the SIB byte to determine addressing information for an |
1513 | | * instruction. |
1514 | | * |
1515 | | * @param insn - The instruction whose SIB byte is to be read. |
1516 | | * @return - 0 if the SIB byte was successfully read; nonzero otherwise. |
1517 | | */ |
1518 | | static int readSIB(struct InternalInstruction *insn) |
1519 | 17.0k | { |
1520 | 17.0k | SIBBase sibBaseBase = SIB_BASE_NONE; |
1521 | 17.0k | uint8_t index, base; |
1522 | | |
1523 | | // dbgprintf(insn, "readSIB()"); |
1524 | | |
1525 | 17.0k | if (insn->consumedSIB) |
1526 | 0 | return 0; |
1527 | | |
1528 | 17.0k | insn->consumedSIB = true; |
1529 | | |
1530 | 17.0k | switch (insn->addressSize) { |
1531 | 0 | case 2: |
1532 | | // dbgprintf(insn, "SIB-based addressing doesn't work in 16-bit mode"); |
1533 | 0 | return -1; |
1534 | 7.00k | case 4: |
1535 | 7.00k | insn->sibIndexBase = SIB_INDEX_EAX; |
1536 | 7.00k | sibBaseBase = SIB_BASE_EAX; |
1537 | 7.00k | break; |
1538 | 10.0k | case 8: |
1539 | 10.0k | insn->sibIndexBase = SIB_INDEX_RAX; |
1540 | 10.0k | sibBaseBase = SIB_BASE_RAX; |
1541 | 10.0k | break; |
1542 | 17.0k | } |
1543 | | |
1544 | 17.0k | if (consumeByte(insn, &insn->sib)) |
1545 | 53 | return -1; |
1546 | | |
1547 | 17.0k | index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3); |
1548 | | |
1549 | 17.0k | if (index == 0x4) { |
1550 | 4.36k | insn->sibIndex = SIB_INDEX_NONE; |
1551 | 12.6k | } else { |
1552 | 12.6k | insn->sibIndex = (SIBIndex)(insn->sibIndexBase + index); |
1553 | 12.6k | } |
1554 | | |
1555 | 17.0k | insn->sibScale = 1 << scaleFromSIB(insn->sib); |
1556 | | |
1557 | 17.0k | base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3); |
1558 | | |
1559 | 17.0k | switch (base) { |
1560 | 2.18k | case 0x5: |
1561 | 2.93k | case 0xd: |
1562 | 2.93k | switch (modFromModRM(insn->modRM)) { |
1563 | 1.79k | case 0x0: |
1564 | 1.79k | insn->eaDisplacement = EA_DISP_32; |
1565 | 1.79k | insn->sibBase = SIB_BASE_NONE; |
1566 | 1.79k | break; |
1567 | 921 | case 0x1: |
1568 | 921 | insn->eaDisplacement = EA_DISP_8; |
1569 | 921 | insn->sibBase = (SIBBase)(sibBaseBase + base); |
1570 | 921 | break; |
1571 | 221 | case 0x2: |
1572 | 221 | insn->eaDisplacement = EA_DISP_32; |
1573 | 221 | insn->sibBase = (SIBBase)(sibBaseBase + base); |
1574 | 221 | break; |
1575 | 0 | case 0x3: |
1576 | | // debug("Cannot have Mod = 0b11 and a SIB byte"); |
1577 | 0 | return -1; |
1578 | 2.93k | } |
1579 | 2.93k | break; |
1580 | 14.0k | default: |
1581 | 14.0k | insn->sibBase = (SIBBase)(sibBaseBase + base); |
1582 | 14.0k | break; |
1583 | 17.0k | } |
1584 | | |
1585 | 17.0k | return 0; |
1586 | 17.0k | } |
1587 | | |
1588 | | /* |
1589 | | * readDisplacement - Consumes the displacement of an instruction. |
1590 | | * |
1591 | | * @param insn - The instruction whose displacement is to be read. |
1592 | | * @return - 0 if the displacement byte was successfully read; nonzero |
1593 | | * otherwise. |
1594 | | */ |
1595 | | static int readDisplacement(struct InternalInstruction *insn) |
1596 | 107k | { |
1597 | 107k | int8_t d8; |
1598 | 107k | int16_t d16; |
1599 | 107k | int32_t d32; |
1600 | | |
1601 | | // dbgprintf(insn, "readDisplacement()"); |
1602 | | |
1603 | 107k | if (insn->consumedDisplacement) |
1604 | 0 | return 0; |
1605 | | |
1606 | 107k | insn->consumedDisplacement = true; |
1607 | 107k | insn->displacementOffset = insn->readerCursor - insn->startLocation; |
1608 | | |
1609 | 107k | switch (insn->eaDisplacement) { |
1610 | 28.2k | case EA_DISP_NONE: |
1611 | 28.2k | insn->consumedDisplacement = false; |
1612 | 28.2k | break; |
1613 | 52.7k | case EA_DISP_8: |
1614 | 52.7k | if (consumeInt8(insn, &d8)) |
1615 | 185 | return -1; |
1616 | 52.5k | insn->displacement = d8; |
1617 | 52.5k | insn->displacementSize = 1; |
1618 | 52.5k | break; |
1619 | 9.85k | case EA_DISP_16: |
1620 | 9.85k | if (consumeInt16(insn, &d16)) |
1621 | 61 | return -1; |
1622 | 9.79k | insn->displacement = d16; |
1623 | 9.79k | insn->displacementSize = 2; |
1624 | 9.79k | break; |
1625 | 16.2k | case EA_DISP_32: |
1626 | 16.2k | if (consumeInt32(insn, &d32)) |
1627 | 224 | return -1; |
1628 | 16.0k | insn->displacement = d32; |
1629 | 16.0k | insn->displacementSize = 4; |
1630 | 16.0k | break; |
1631 | 107k | } |
1632 | | |
1633 | 106k | return 0; |
1634 | 107k | } |
1635 | | |
1636 | | /* |
1637 | | * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and |
1638 | | * displacement) for an instruction and interprets it. |
1639 | | * |
1640 | | * @param insn - The instruction whose addressing information is to be read. |
1641 | | * @return - 0 if the information was successfully read; nonzero otherwise. |
1642 | | */ |
1643 | | static int readModRM(struct InternalInstruction *insn) |
1644 | 1.05M | { |
1645 | 1.05M | uint8_t mod, rm, reg, evexrm; |
1646 | | |
1647 | | // dbgprintf(insn, "readModRM()"); |
1648 | | |
1649 | 1.05M | if (insn->consumedModRM) |
1650 | 712k | return 0; |
1651 | | |
1652 | 343k | insn->modRMOffset = (uint8_t)(insn->readerCursor - insn->startLocation); |
1653 | | |
1654 | 343k | if (consumeByte(insn, &insn->modRM)) |
1655 | 1.24k | return -1; |
1656 | | |
1657 | 342k | insn->consumedModRM = true; |
1658 | | |
1659 | | // save original ModRM for later reference |
1660 | 342k | insn->orgModRM = insn->modRM; |
1661 | | |
1662 | | // handle MOVcr, MOVdr, MOVrc, MOVrd by pretending they have MRM.mod = 3 |
1663 | 342k | if ((insn->firstByte == 0x0f && insn->opcodeType == TWOBYTE) && |
1664 | 36.8k | (insn->opcode >= 0x20 && insn->opcode <= 0x23)) |
1665 | 640 | insn->modRM |= 0xC0; |
1666 | | |
1667 | 342k | mod = modFromModRM(insn->modRM); |
1668 | 342k | rm = rmFromModRM(insn->modRM); |
1669 | 342k | reg = regFromModRM(insn->modRM); |
1670 | | |
1671 | | /* |
1672 | | * This goes by insn->registerSize to pick the correct register, which messes |
1673 | | * up if we're using (say) XMM or 8-bit register operands. That gets fixed in |
1674 | | * fixupReg(). |
1675 | | */ |
1676 | 342k | switch (insn->registerSize) { |
1677 | 104k | case 2: |
1678 | 104k | insn->regBase = MODRM_REG_AX; |
1679 | 104k | insn->eaRegBase = EA_REG_AX; |
1680 | 104k | break; |
1681 | 201k | case 4: |
1682 | 201k | insn->regBase = MODRM_REG_EAX; |
1683 | 201k | insn->eaRegBase = EA_REG_EAX; |
1684 | 201k | break; |
1685 | 36.5k | case 8: |
1686 | 36.5k | insn->regBase = MODRM_REG_RAX; |
1687 | 36.5k | insn->eaRegBase = EA_REG_RAX; |
1688 | 36.5k | break; |
1689 | 342k | } |
1690 | | |
1691 | 342k | reg |= rFromREX(insn->rexPrefix) << 3; |
1692 | 342k | rm |= bFromREX(insn->rexPrefix) << 3; |
1693 | | |
1694 | 342k | evexrm = 0; |
1695 | 342k | if (insn->vectorExtensionType == TYPE_EVEX && |
1696 | 57.4k | insn->mode == MODE_64BIT) { |
1697 | 23.1k | reg |= r2FromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4; |
1698 | 23.1k | evexrm = xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4; |
1699 | 23.1k | } |
1700 | | |
1701 | 342k | insn->reg = (Reg)(insn->regBase + reg); |
1702 | | |
1703 | 342k | switch (insn->addressSize) { |
1704 | 92.2k | case 2: { |
1705 | 92.2k | EABase eaBaseBase = EA_BASE_BX_SI; |
1706 | | |
1707 | 92.2k | switch (mod) { |
1708 | 51.8k | case 0x0: |
1709 | 51.8k | if (rm == 0x6) { |
1710 | 3.30k | insn->eaBase = EA_BASE_NONE; |
1711 | 3.30k | insn->eaDisplacement = EA_DISP_16; |
1712 | 3.30k | if (readDisplacement(insn)) |
1713 | 17 | return -1; |
1714 | 48.5k | } else { |
1715 | 48.5k | insn->eaBase = (EABase)(eaBaseBase + rm); |
1716 | 48.5k | insn->eaDisplacement = EA_DISP_NONE; |
1717 | 48.5k | } |
1718 | 51.8k | break; |
1719 | 51.8k | case 0x1: |
1720 | 14.1k | insn->eaBase = (EABase)(eaBaseBase + rm); |
1721 | 14.1k | insn->eaDisplacement = EA_DISP_8; |
1722 | 14.1k | insn->displacementSize = 1; |
1723 | 14.1k | if (readDisplacement(insn)) |
1724 | 35 | return -1; |
1725 | 14.1k | break; |
1726 | 14.1k | case 0x2: |
1727 | 6.55k | insn->eaBase = (EABase)(eaBaseBase + rm); |
1728 | 6.55k | insn->eaDisplacement = EA_DISP_16; |
1729 | 6.55k | if (readDisplacement(insn)) |
1730 | 44 | return -1; |
1731 | 6.50k | break; |
1732 | 19.6k | case 0x3: |
1733 | 19.6k | insn->eaBase = (EABase)(insn->eaRegBase + rm); |
1734 | 19.6k | if (readDisplacement(insn)) |
1735 | 0 | return -1; |
1736 | 19.6k | break; |
1737 | 92.2k | } |
1738 | 92.1k | break; |
1739 | 92.2k | } |
1740 | | |
1741 | 119k | case 4: |
1742 | 250k | case 8: { |
1743 | 250k | EABase eaBaseBase = |
1744 | 250k | (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX); |
1745 | | |
1746 | 250k | switch (mod) { |
1747 | 0 | default: |
1748 | 0 | break; |
1749 | 130k | case 0x0: |
1750 | 130k | insn->eaDisplacement = |
1751 | 130k | EA_DISP_NONE; /* readSIB may override this */ |
1752 | | // In determining whether RIP-relative mode is used (rm=5), |
1753 | | // or whether a SIB byte is present (rm=4), |
1754 | | // the extension bits (REX.b and EVEX.x) are ignored. |
1755 | 130k | switch (rm & 7) { |
1756 | 10.4k | case 0x4: // SIB byte is present |
1757 | 10.4k | insn->eaBase = (insn->addressSize == 4 ? |
1758 | 4.33k | EA_BASE_sib : |
1759 | 10.4k | EA_BASE_sib64); |
1760 | 10.4k | if (readSIB(insn) || readDisplacement(insn)) |
1761 | 31 | return -1; |
1762 | 10.3k | break; |
1763 | 10.3k | case 0x5: // RIP-relative |
1764 | 3.47k | insn->eaBase = EA_BASE_NONE; |
1765 | 3.47k | insn->eaDisplacement = EA_DISP_32; |
1766 | 3.47k | if (readDisplacement(insn)) |
1767 | 45 | return -1; |
1768 | 3.43k | break; |
1769 | 116k | default: |
1770 | 116k | insn->eaBase = (EABase)(eaBaseBase + rm); |
1771 | 116k | break; |
1772 | 130k | } |
1773 | 130k | break; |
1774 | 130k | case 0x1: |
1775 | 38.5k | insn->displacementSize = 1; |
1776 | | /* FALLTHROUGH */ |
1777 | 49.5k | case 0x2: |
1778 | 49.5k | insn->eaDisplacement = |
1779 | 49.5k | (mod == 0x1 ? EA_DISP_8 : EA_DISP_32); |
1780 | 49.5k | switch (rm & 7) { |
1781 | 6.64k | case 0x4: // SIB byte is present |
1782 | 6.64k | insn->eaBase = EA_BASE_sib; |
1783 | 6.64k | if (readSIB(insn) || readDisplacement(insn)) |
1784 | 70 | return -1; |
1785 | 6.57k | break; |
1786 | 42.9k | default: |
1787 | 42.9k | insn->eaBase = (EABase)(eaBaseBase + rm); |
1788 | 42.9k | if (readDisplacement(insn)) |
1789 | 281 | return -1; |
1790 | 42.6k | break; |
1791 | 49.5k | } |
1792 | 49.1k | break; |
1793 | 69.8k | case 0x3: |
1794 | 69.8k | insn->eaDisplacement = EA_DISP_NONE; |
1795 | 69.8k | insn->eaBase = (EABase)(insn->eaRegBase + rm + evexrm); |
1796 | 69.8k | break; |
1797 | 250k | } |
1798 | | |
1799 | 249k | break; |
1800 | 250k | } |
1801 | 342k | } /* switch (insn->addressSize) */ |
1802 | | |
1803 | 341k | return 0; |
1804 | 342k | } |
1805 | | |
1806 | | #define GENERIC_FIXUP_FUNC(name, base, prefix, mask) \ |
1807 | | static uint16_t name(struct InternalInstruction *insn, \ |
1808 | | OperandType type, uint8_t index, uint8_t *valid) \ |
1809 | 402k | { \ |
1810 | 402k | *valid = 1; \ |
1811 | 402k | switch (type) { \ |
1812 | 0 | default: \ |
1813 | 0 | *valid = 0; \ |
1814 | 0 | return 0; \ |
1815 | 88.5k | case TYPE_Rv: \ |
1816 | 88.5k | return base + index; \ |
1817 | 106k | case TYPE_R8: \ |
1818 | 106k | index &= mask; \ |
1819 | 106k | if (index > 0xf) \ |
1820 | 106k | *valid = 0; \ |
1821 | 106k | if (insn->rexPrefix && index >= 4 && index <= 7) { \ |
1822 | 1.82k | return prefix##_SPL + (index - 4); \ |
1823 | 104k | } else { \ |
1824 | 104k | return prefix##_AL + index; \ |
1825 | 104k | } \ |
1826 | 106k | case TYPE_R16: \ |
1827 | 2.67k | index &= mask; \ |
1828 | 2.67k | if (index > 0xf) \ |
1829 | 2.67k | *valid = 0; \ |
1830 | 2.67k | return prefix##_AX + index; \ |
1831 | 106k | case TYPE_R32: \ |
1832 | 3.80k | index &= mask; \ |
1833 | 3.80k | if (index > 0xf) \ |
1834 | 3.80k | *valid = 0; \ |
1835 | 3.80k | return prefix##_EAX + index; \ |
1836 | 106k | case TYPE_R64: \ |
1837 | 12.0k | index &= mask; \ |
1838 | 12.0k | if (index > 0xf) \ |
1839 | 12.0k | *valid = 0; \ |
1840 | 12.0k | return prefix##_RAX + index; \ |
1841 | 106k | case TYPE_ZMM: \ |
1842 | 46.8k | return prefix##_ZMM0 + index; \ |
1843 | 106k | case TYPE_YMM: \ |
1844 | 33.4k | return prefix##_YMM0 + index; \ |
1845 | 106k | case TYPE_XMM: \ |
1846 | 70.6k | return prefix##_XMM0 + index; \ |
1847 | 106k | case TYPE_VK: \ |
1848 | 24.2k | index &= 0xf; \ |
1849 | 24.2k | if (index > 7) \ |
1850 | 24.2k | *valid = 0; \ |
1851 | 24.2k | return prefix##_K0 + index; \ |
1852 | 106k | case TYPE_MM64: \ |
1853 | 5.49k | return prefix##_MM0 + (index & 0x7); \ |
1854 | 106k | case TYPE_SEGMENTREG: \ |
1855 | 1.10k | if ((index & 7) > 5 || \ |
1856 | 1.10k | (insn->opcodeType == ONEBYTE && \ |
1857 | 1.10k | insn->opcode == 0x8E && (index & 7) == 1)) \ |
1858 | 1.10k | *valid = 0; \ |
1859 | 1.10k | return prefix##_ES + (index & 7); \ |
1860 | 106k | case TYPE_DEBUGREG: \ |
1861 | 413 | return prefix##_DR0 + index; \ |
1862 | 106k | case TYPE_CONTROLREG: \ |
1863 | 227 | return prefix##_CR0 + index; \ |
1864 | 106k | case TYPE_BNDR: \ |
1865 | 6.44k | if (index > 3) \ |
1866 | 6.44k | *valid = 0; \ |
1867 | 6.44k | return prefix##_BND0 + index; \ |
1868 | 106k | case TYPE_MVSIBX: \ |
1869 | 0 | return prefix##_XMM0 + index; \ |
1870 | 106k | case TYPE_MVSIBY: \ |
1871 | 0 | return prefix##_YMM0 + index; \ |
1872 | 106k | case TYPE_MVSIBZ: \ |
1873 | 0 | return prefix##_ZMM0 + index; \ |
1874 | 402k | } \ |
1875 | 402k | } X86DisassemblerDecoder.c:fixupRegValue Line | Count | Source | 1809 | 315k | { \ | 1810 | 315k | *valid = 1; \ | 1811 | 315k | switch (type) { \ | 1812 | 0 | default: \ | 1813 | 0 | *valid = 0; \ | 1814 | 0 | return 0; \ | 1815 | 66.2k | case TYPE_Rv: \ | 1816 | 66.2k | return base + index; \ | 1817 | 83.3k | case TYPE_R8: \ | 1818 | 83.3k | index &= mask; \ | 1819 | 83.3k | if (index > 0xf) \ | 1820 | 83.3k | *valid = 0; \ | 1821 | 83.3k | if (insn->rexPrefix && index >= 4 && index <= 7) { \ | 1822 | 1.20k | return prefix##_SPL + (index - 4); \ | 1823 | 82.1k | } else { \ | 1824 | 82.1k | return prefix##_AL + index; \ | 1825 | 82.1k | } \ | 1826 | 83.3k | case TYPE_R16: \ | 1827 | 2.12k | index &= mask; \ | 1828 | 2.12k | if (index > 0xf) \ | 1829 | 2.12k | *valid = 0; \ | 1830 | 2.12k | return prefix##_AX + index; \ | 1831 | 83.3k | case TYPE_R32: \ | 1832 | 2.43k | index &= mask; \ | 1833 | 2.43k | if (index > 0xf) \ | 1834 | 2.43k | *valid = 0; \ | 1835 | 2.43k | return prefix##_EAX + index; \ | 1836 | 83.3k | case TYPE_R64: \ | 1837 | 7.46k | index &= mask; \ | 1838 | 7.46k | if (index > 0xf) \ | 1839 | 7.46k | *valid = 0; \ | 1840 | 7.46k | return prefix##_RAX + index; \ | 1841 | 83.3k | case TYPE_ZMM: \ | 1842 | 37.5k | return prefix##_ZMM0 + index; \ | 1843 | 83.3k | case TYPE_YMM: \ | 1844 | 27.2k | return prefix##_YMM0 + index; \ | 1845 | 83.3k | case TYPE_XMM: \ | 1846 | 56.2k | return prefix##_XMM0 + index; \ | 1847 | 83.3k | case TYPE_VK: \ | 1848 | 22.5k | index &= 0xf; \ | 1849 | 22.5k | if (index > 7) \ | 1850 | 22.5k | *valid = 0; \ | 1851 | 22.5k | return prefix##_K0 + index; \ | 1852 | 83.3k | case TYPE_MM64: \ | 1853 | 3.29k | return prefix##_MM0 + (index & 0x7); \ | 1854 | 83.3k | case TYPE_SEGMENTREG: \ | 1855 | 1.10k | if ((index & 7) > 5 || \ | 1856 | 1.10k | (insn->opcodeType == ONEBYTE && \ | 1857 | 1.10k | insn->opcode == 0x8E && (index & 7) == 1)) \ | 1858 | 1.10k | *valid = 0; \ | 1859 | 1.10k | return prefix##_ES + (index & 7); \ | 1860 | 83.3k | case TYPE_DEBUGREG: \ | 1861 | 413 | return prefix##_DR0 + index; \ | 1862 | 83.3k | case TYPE_CONTROLREG: \ | 1863 | 227 | return prefix##_CR0 + index; \ | 1864 | 83.3k | case TYPE_BNDR: \ | 1865 | 5.43k | if (index > 3) \ | 1866 | 5.43k | *valid = 0; \ | 1867 | 5.43k | return prefix##_BND0 + index; \ | 1868 | 83.3k | case TYPE_MVSIBX: \ | 1869 | 0 | return prefix##_XMM0 + index; \ | 1870 | 83.3k | case TYPE_MVSIBY: \ | 1871 | 0 | return prefix##_YMM0 + index; \ | 1872 | 83.3k | case TYPE_MVSIBZ: \ | 1873 | 0 | return prefix##_ZMM0 + index; \ | 1874 | 315k | } \ | 1875 | 315k | } |
X86DisassemblerDecoder.c:fixupRMValue Line | Count | Source | 1809 | 86.2k | { \ | 1810 | 86.2k | *valid = 1; \ | 1811 | 86.2k | switch (type) { \ | 1812 | 0 | default: \ | 1813 | 0 | *valid = 0; \ | 1814 | 0 | return 0; \ | 1815 | 22.3k | case TYPE_Rv: \ | 1816 | 22.3k | return base + index; \ | 1817 | 22.7k | case TYPE_R8: \ | 1818 | 22.7k | index &= mask; \ | 1819 | 22.7k | if (index > 0xf) \ | 1820 | 22.7k | *valid = 0; \ | 1821 | 22.7k | if (insn->rexPrefix && index >= 4 && index <= 7) { \ | 1822 | 614 | return prefix##_SPL + (index - 4); \ | 1823 | 22.1k | } else { \ | 1824 | 22.1k | return prefix##_AL + index; \ | 1825 | 22.1k | } \ | 1826 | 22.7k | case TYPE_R16: \ | 1827 | 549 | index &= mask; \ | 1828 | 549 | if (index > 0xf) \ | 1829 | 549 | *valid = 0; \ | 1830 | 549 | return prefix##_AX + index; \ | 1831 | 22.7k | case TYPE_R32: \ | 1832 | 1.37k | index &= mask; \ | 1833 | 1.37k | if (index > 0xf) \ | 1834 | 1.37k | *valid = 0; \ | 1835 | 1.37k | return prefix##_EAX + index; \ | 1836 | 22.7k | case TYPE_R64: \ | 1837 | 4.60k | index &= mask; \ | 1838 | 4.60k | if (index > 0xf) \ | 1839 | 4.60k | *valid = 0; \ | 1840 | 4.60k | return prefix##_RAX + index; \ | 1841 | 22.7k | case TYPE_ZMM: \ | 1842 | 9.33k | return prefix##_ZMM0 + index; \ | 1843 | 22.7k | case TYPE_YMM: \ | 1844 | 6.20k | return prefix##_YMM0 + index; \ | 1845 | 22.7k | case TYPE_XMM: \ | 1846 | 14.3k | return prefix##_XMM0 + index; \ | 1847 | 22.7k | case TYPE_VK: \ | 1848 | 1.66k | index &= 0xf; \ | 1849 | 1.66k | if (index > 7) \ | 1850 | 1.66k | *valid = 0; \ | 1851 | 1.66k | return prefix##_K0 + index; \ | 1852 | 22.7k | case TYPE_MM64: \ | 1853 | 2.19k | return prefix##_MM0 + (index & 0x7); \ | 1854 | 22.7k | case TYPE_SEGMENTREG: \ | 1855 | 0 | if ((index & 7) > 5 || \ | 1856 | 0 | (insn->opcodeType == ONEBYTE && \ | 1857 | 0 | insn->opcode == 0x8E && (index & 7) == 1)) \ | 1858 | 0 | *valid = 0; \ | 1859 | 0 | return prefix##_ES + (index & 7); \ | 1860 | 22.7k | case TYPE_DEBUGREG: \ | 1861 | 0 | return prefix##_DR0 + index; \ | 1862 | 22.7k | case TYPE_CONTROLREG: \ | 1863 | 0 | return prefix##_CR0 + index; \ | 1864 | 22.7k | case TYPE_BNDR: \ | 1865 | 1.00k | if (index > 3) \ | 1866 | 1.00k | *valid = 0; \ | 1867 | 1.00k | return prefix##_BND0 + index; \ | 1868 | 22.7k | case TYPE_MVSIBX: \ | 1869 | 0 | return prefix##_XMM0 + index; \ | 1870 | 22.7k | case TYPE_MVSIBY: \ | 1871 | 0 | return prefix##_YMM0 + index; \ | 1872 | 22.7k | case TYPE_MVSIBZ: \ | 1873 | 0 | return prefix##_ZMM0 + index; \ | 1874 | 86.2k | } \ | 1875 | 86.2k | } |
|
1876 | | |
1877 | | /* |
1878 | | * fixup*Value - Consults an operand type to determine the meaning of the |
1879 | | * reg or R/M field. If the operand is an XMM operand, for example, an |
1880 | | * operand would be XMM0 instead of AX, which readModRM() would otherwise |
1881 | | * misinterpret it as. |
1882 | | * |
1883 | | * @param insn - The instruction containing the operand. |
1884 | | * @param type - The operand type. |
1885 | | * @param index - The existing value of the field as reported by readModRM(). |
1886 | | * @param valid - The address of a uint8_t. The target is set to 1 if the |
1887 | | * field is valid for the register class; 0 if not. |
1888 | | * @return - The proper value. |
1889 | | */ |
1890 | | GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase, MODRM_REG, 0x1f) |
1891 | | GENERIC_FIXUP_FUNC(fixupRMValue, insn->eaRegBase, EA_REG, 0xf) |
1892 | | |
1893 | | /* |
1894 | | * fixupReg - Consults an operand specifier to determine which of the |
1895 | | * fixup*Value functions to use in correcting readModRM()'ss interpretation. |
1896 | | * |
1897 | | * @param insn - See fixup*Value(). |
1898 | | * @param op - The operand specifier. |
1899 | | * @return - 0 if fixup was successful; -1 if the register returned was |
1900 | | * invalid for its class. |
1901 | | */ |
1902 | | static int fixupReg(struct InternalInstruction *insn, |
1903 | | const struct OperandSpecifier *op) |
1904 | 648k | { |
1905 | 648k | uint8_t valid; |
1906 | | |
1907 | 648k | switch ((OperandEncoding)op->encoding) { |
1908 | 0 | default: |
1909 | | // debug("Expected a REG or R/M encoding in fixupReg"); |
1910 | 0 | return -1; |
1911 | 55.6k | case ENCODING_VVVV: |
1912 | 55.6k | insn->vvvv = (Reg)fixupRegValue(insn, (OperandType)op->type, |
1913 | 55.6k | insn->vvvv, &valid); |
1914 | 55.6k | if (!valid) |
1915 | 1 | return -1; |
1916 | 55.6k | break; |
1917 | 260k | case ENCODING_REG: |
1918 | 260k | insn->reg = (Reg)fixupRegValue(insn, (OperandType)op->type, |
1919 | 260k | insn->reg - insn->regBase, |
1920 | 260k | &valid); |
1921 | 260k | if (!valid) |
1922 | 13 | return -1; |
1923 | 260k | break; |
1924 | 2.12M | CASE_ENCODING_RM: |
1925 | 2.12M | if (insn->eaBase >= insn->eaRegBase) { |
1926 | 86.2k | insn->eaBase = (EABase)fixupRMValue( |
1927 | 86.2k | insn, (OperandType)op->type, |
1928 | 86.2k | insn->eaBase - insn->eaRegBase, &valid); |
1929 | 86.2k | if (!valid) |
1930 | 3 | return -1; |
1931 | 86.2k | } |
1932 | 332k | break; |
1933 | 648k | } |
1934 | | |
1935 | 648k | return 0; |
1936 | 648k | } |
1937 | | |
1938 | | /* |
1939 | | * readOpcodeRegister - Reads an operand from the opcode field of an |
1940 | | * instruction and interprets it appropriately given the operand width. |
1941 | | * Handles AddRegFrm instructions. |
1942 | | * |
1943 | | * @param insn - the instruction whose opcode field is to be read. |
1944 | | * @param size - The width (in bytes) of the register being specified. |
1945 | | * 1 means AL and friends, 2 means AX, 4 means EAX, and 8 means |
1946 | | * RAX. |
1947 | | * @return - 0 on success; nonzero otherwise. |
1948 | | */ |
1949 | | static int readOpcodeRegister(struct InternalInstruction *insn, uint8_t size) |
1950 | 51.7k | { |
1951 | 51.7k | if (size == 0) |
1952 | 35.7k | size = insn->registerSize; |
1953 | | |
1954 | 51.7k | switch (size) { |
1955 | 6.59k | case 1: |
1956 | 6.59k | insn->opcodeRegister = |
1957 | 6.59k | (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3) | |
1958 | 6.59k | (insn->opcode & 7))); |
1959 | 6.59k | if (insn->rexPrefix && |
1960 | 612 | insn->opcodeRegister >= MODRM_REG_AL + 0x4 && |
1961 | 329 | insn->opcodeRegister < MODRM_REG_AL + 0x8) { |
1962 | 236 | insn->opcodeRegister = |
1963 | 236 | (Reg)(MODRM_REG_SPL + (insn->opcodeRegister - |
1964 | 236 | MODRM_REG_AL - 4)); |
1965 | 236 | } |
1966 | | |
1967 | 6.59k | break; |
1968 | 11.8k | case 2: |
1969 | 11.8k | insn->opcodeRegister = |
1970 | 11.8k | (Reg)(MODRM_REG_AX + ((bFromREX(insn->rexPrefix) << 3) | |
1971 | 11.8k | (insn->opcode & 7))); |
1972 | 11.8k | break; |
1973 | 23.7k | case 4: |
1974 | 23.7k | insn->opcodeRegister = (Reg)(MODRM_REG_EAX + |
1975 | 23.7k | ((bFromREX(insn->rexPrefix) << 3) | |
1976 | 23.7k | (insn->opcode & 7))); |
1977 | 23.7k | break; |
1978 | 9.53k | case 8: |
1979 | 9.53k | insn->opcodeRegister = (Reg)(MODRM_REG_RAX + |
1980 | 9.53k | ((bFromREX(insn->rexPrefix) << 3) | |
1981 | 9.53k | (insn->opcode & 7))); |
1982 | 9.53k | break; |
1983 | 51.7k | } |
1984 | | |
1985 | 51.7k | return 0; |
1986 | 51.7k | } |
1987 | | |
1988 | | /* |
1989 | | * readImmediate - Consumes an immediate operand from an instruction, given the |
1990 | | * desired operand size. |
1991 | | * |
1992 | | * @param insn - The instruction whose operand is to be read. |
1993 | | * @param size - The width (in bytes) of the operand. |
1994 | | * @return - 0 if the immediate was successfully consumed; nonzero |
1995 | | * otherwise. |
1996 | | */ |
1997 | | static int readImmediate(struct InternalInstruction *insn, uint8_t size) |
1998 | 170k | { |
1999 | 170k | uint8_t imm8; |
2000 | 170k | uint16_t imm16; |
2001 | 170k | uint32_t imm32; |
2002 | 170k | uint64_t imm64; |
2003 | | |
2004 | 170k | if (insn->numImmediatesConsumed == 2) { |
2005 | | // debug("Already consumed two immediates"); |
2006 | 0 | return -1; |
2007 | 0 | } |
2008 | | |
2009 | 170k | if (size == 0) |
2010 | 0 | size = insn->immediateSize; |
2011 | 170k | else |
2012 | 170k | insn->immediateSize = size; |
2013 | | |
2014 | 170k | insn->immediateOffset = insn->readerCursor - insn->startLocation; |
2015 | | |
2016 | 170k | switch (size) { |
2017 | 130k | case 1: |
2018 | 130k | if (consumeByte(insn, &imm8)) |
2019 | 523 | return -1; |
2020 | | |
2021 | 130k | insn->immediates[insn->numImmediatesConsumed] = imm8; |
2022 | 130k | break; |
2023 | 23.3k | case 2: |
2024 | 23.3k | if (consumeUInt16(insn, &imm16)) |
2025 | 188 | return -1; |
2026 | | |
2027 | 23.1k | insn->immediates[insn->numImmediatesConsumed] = imm16; |
2028 | 23.1k | break; |
2029 | 14.1k | case 4: |
2030 | 14.1k | if (consumeUInt32(insn, &imm32)) |
2031 | 344 | return -1; |
2032 | | |
2033 | 13.8k | insn->immediates[insn->numImmediatesConsumed] = imm32; |
2034 | 13.8k | break; |
2035 | 2.48k | case 8: |
2036 | 2.48k | if (consumeUInt64(insn, &imm64)) |
2037 | 79 | return -1; |
2038 | 2.41k | insn->immediates[insn->numImmediatesConsumed] = imm64; |
2039 | 2.41k | break; |
2040 | 170k | } |
2041 | | |
2042 | 169k | insn->numImmediatesConsumed++; |
2043 | | |
2044 | 169k | return 0; |
2045 | 170k | } |
2046 | | |
2047 | | /* |
2048 | | * readVVVV - Consumes vvvv from an instruction if it has a VEX prefix. |
2049 | | * |
2050 | | * @param insn - The instruction whose operand is to be read. |
2051 | | * @return - 0 if the vvvv was successfully consumed; nonzero |
2052 | | * otherwise. |
2053 | | */ |
2054 | | static int readVVVV(struct InternalInstruction *insn) |
2055 | 593k | { |
2056 | 593k | int vvvv; |
2057 | | |
2058 | 593k | if (insn->vectorExtensionType == TYPE_EVEX) |
2059 | 57.3k | vvvv = (v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4 | |
2060 | 57.3k | vvvvFromEVEX3of4(insn->vectorExtensionPrefix[2])); |
2061 | 535k | else if (insn->vectorExtensionType == TYPE_VEX_3B) |
2062 | 5.41k | vvvv = vvvvFromVEX3of3(insn->vectorExtensionPrefix[2]); |
2063 | 530k | else if (insn->vectorExtensionType == TYPE_VEX_2B) |
2064 | 9.12k | vvvv = vvvvFromVEX2of2(insn->vectorExtensionPrefix[1]); |
2065 | 521k | else if (insn->vectorExtensionType == TYPE_XOP) |
2066 | 7.43k | vvvv = vvvvFromXOP3of3(insn->vectorExtensionPrefix[2]); |
2067 | 513k | else |
2068 | 513k | return -1; |
2069 | | |
2070 | 79.3k | if (insn->mode != MODE_64BIT) |
2071 | 50.0k | vvvv &= 0xf; // Can only clear bit 4. Bit 3 must be cleared later. |
2072 | | |
2073 | 79.3k | insn->vvvv = (Reg)vvvv; |
2074 | | |
2075 | 79.3k | return 0; |
2076 | 593k | } |
2077 | | |
2078 | | /* |
2079 | | * readMaskRegister - Reads an mask register from the opcode field of an |
2080 | | * instruction. |
2081 | | * |
2082 | | * @param insn - The instruction whose opcode field is to be read. |
2083 | | * @return - 0 on success; nonzero otherwise. |
2084 | | */ |
2085 | | static int readMaskRegister(struct InternalInstruction *insn) |
2086 | 42.3k | { |
2087 | 42.3k | if (insn->vectorExtensionType != TYPE_EVEX) |
2088 | 0 | return -1; |
2089 | | |
2090 | 42.3k | insn->writemask = |
2091 | 42.3k | (Reg)(aaaFromEVEX4of4(insn->vectorExtensionPrefix[3])); |
2092 | | |
2093 | 42.3k | return 0; |
2094 | 42.3k | } |
2095 | | |
2096 | | /* |
2097 | | * readOperands - Consults the specifier for an instruction and consumes all |
2098 | | * operands for that instruction, interpreting them as it goes. |
2099 | | * |
2100 | | * @param insn - The instruction whose operands are to be read and interpreted. |
2101 | | * @return - 0 if all operands could be read; nonzero otherwise. |
2102 | | */ |
2103 | | static int readOperands(struct InternalInstruction *insn) |
2104 | 593k | { |
2105 | 593k | int hasVVVV, needVVVV; |
2106 | 593k | int sawRegImm = 0; |
2107 | 593k | int i; |
2108 | | |
2109 | | /* If non-zero vvvv specified, need to make sure one of the operands |
2110 | | uses it. */ |
2111 | 593k | hasVVVV = !readVVVV(insn); |
2112 | 593k | needVVVV = hasVVVV && (insn->vvvv != 0); |
2113 | | |
2114 | 593k | if ((insn->instructionID == X86_UD0 || |
2115 | 592k | insn->instructionID == X86_UD1) && |
2116 | 827 | readModRM(insn)) |
2117 | 9 | return -1; |
2118 | | |
2119 | 4.14M | for (i = 0; i < X86_MAX_OPERANDS; ++i) { |
2120 | 3.55M | const OperandSpecifier *op = |
2121 | 3.55M | &x86OperandSets[insn->spec->operands][i]; |
2122 | 3.55M | switch (op->encoding) { |
2123 | 2.43M | case ENCODING_NONE: |
2124 | 2.45M | case ENCODING_SI: |
2125 | 2.48M | case ENCODING_DI: |
2126 | 2.48M | break; |
2127 | | |
2128 | 25.5k | CASE_ENCODING_VSIB: |
2129 | | // VSIB can use the V2 bit so check only the other bits. |
2130 | 25.5k | if (needVVVV) |
2131 | 3.47k | needVVVV = hasVVVV & ((insn->vvvv & 0xf) != 0); |
2132 | | |
2133 | 25.5k | if (readModRM(insn)) |
2134 | 0 | return -1; |
2135 | | |
2136 | | // Reject if SIB wasn't used. |
2137 | 4.98k | if (insn->eaBase != EA_BASE_sib && |
2138 | 3.04k | insn->eaBase != EA_BASE_sib64) |
2139 | 8 | return -1; |
2140 | | |
2141 | | // If sibIndex was set to SIB_INDEX_NONE, index offset is 4. |
2142 | 4.97k | if (insn->sibIndex == SIB_INDEX_NONE) |
2143 | 664 | insn->sibIndex = |
2144 | 664 | (SIBIndex)(insn->sibIndexBase + 4); |
2145 | | |
2146 | | // If EVEX.v2 is set this is one of the 16-31 registers. |
2147 | 4.97k | if (insn->vectorExtensionType == TYPE_EVEX && |
2148 | 3.59k | insn->mode == MODE_64BIT && |
2149 | 2.54k | v2FromEVEX4of4(insn->vectorExtensionPrefix[3])) |
2150 | 2.11k | insn->sibIndex = |
2151 | 2.11k | (SIBIndex)(insn->sibIndex + 16); |
2152 | | |
2153 | | // Adjust the index register to the correct size. |
2154 | 4.97k | switch (op->type) { |
2155 | 0 | default: |
2156 | | // debug("Unhandled VSIB index type"); |
2157 | 0 | return -1; |
2158 | 1.62k | case TYPE_MVSIBX: |
2159 | 1.62k | insn->sibIndex = |
2160 | 1.62k | (SIBIndex)(SIB_INDEX_XMM0 + |
2161 | 1.62k | (insn->sibIndex - |
2162 | 1.62k | insn->sibIndexBase)); |
2163 | 1.62k | break; |
2164 | 1.70k | case TYPE_MVSIBY: |
2165 | 1.70k | insn->sibIndex = |
2166 | 1.70k | (SIBIndex)(SIB_INDEX_YMM0 + |
2167 | 1.70k | (insn->sibIndex - |
2168 | 1.70k | insn->sibIndexBase)); |
2169 | 1.70k | break; |
2170 | 1.64k | case TYPE_MVSIBZ: |
2171 | 1.64k | insn->sibIndex = |
2172 | 1.64k | (SIBIndex)(SIB_INDEX_ZMM0 + |
2173 | 1.64k | (insn->sibIndex - |
2174 | 1.64k | insn->sibIndexBase)); |
2175 | 1.64k | break; |
2176 | 4.97k | } |
2177 | | |
2178 | | // Apply the AVX512 compressed displacement scaling factor. |
2179 | 4.97k | if (op->encoding != ENCODING_REG && |
2180 | 4.97k | insn->eaDisplacement == EA_DISP_8) |
2181 | 1.03k | insn->displacement *= |
2182 | 1.03k | 1 << (op->encoding - ENCODING_VSIB); |
2183 | 4.97k | break; |
2184 | | |
2185 | 260k | case ENCODING_REG: |
2186 | 3.94M | CASE_ENCODING_RM: |
2187 | 3.94M | if (readModRM(insn)) |
2188 | 0 | return -1; |
2189 | | |
2190 | 592k | if (fixupReg(insn, op)) |
2191 | 16 | return -1; |
2192 | | |
2193 | | // Apply the AVX512 compressed displacement scaling factor. |
2194 | 592k | if (op->encoding != ENCODING_REG && |
2195 | 332k | insn->eaDisplacement == EA_DISP_8) |
2196 | 51.4k | insn->displacement *= |
2197 | 51.4k | 1 << (op->encoding - ENCODING_RM); |
2198 | 592k | break; |
2199 | | |
2200 | 132k | case ENCODING_IB: |
2201 | 132k | if (sawRegImm) { |
2202 | | /* Saw a register immediate so don't read again and instead split the |
2203 | | previous immediate. FIXME: This is a hack. */ |
2204 | 1.34k | insn->immediates[insn->numImmediatesConsumed] = |
2205 | 1.34k | insn->immediates |
2206 | 1.34k | [insn->numImmediatesConsumed - |
2207 | 1.34k | 1] & |
2208 | 1.34k | 0xf; |
2209 | 1.34k | ++insn->numImmediatesConsumed; |
2210 | 1.34k | break; |
2211 | 1.34k | } |
2212 | 130k | if (readImmediate(insn, 1)) |
2213 | 523 | return -1; |
2214 | 130k | if (op->type == TYPE_XMM || op->type == TYPE_YMM) |
2215 | 1.80k | sawRegImm = 1; |
2216 | 130k | break; |
2217 | | |
2218 | 9.05k | case ENCODING_IW: |
2219 | 9.05k | if (readImmediate(insn, 2)) |
2220 | 50 | return -1; |
2221 | 9.00k | break; |
2222 | | |
2223 | 9.00k | case ENCODING_ID: |
2224 | 3.60k | if (readImmediate(insn, 4)) |
2225 | 76 | return -1; |
2226 | 3.53k | break; |
2227 | | |
2228 | 3.53k | case ENCODING_IO: |
2229 | 532 | if (readImmediate(insn, 8)) |
2230 | 20 | return -1; |
2231 | 512 | break; |
2232 | | |
2233 | 20.9k | case ENCODING_Iv: |
2234 | 20.9k | if (readImmediate(insn, insn->immediateSize)) |
2235 | 361 | return -1; |
2236 | 20.5k | break; |
2237 | | |
2238 | 20.5k | case ENCODING_Ia: |
2239 | 5.89k | if (readImmediate(insn, insn->addressSize)) |
2240 | 104 | return -1; |
2241 | | /* Direct memory-offset (moffset) immediate will get mapped |
2242 | | to memory operand later. We want the encoding info to |
2243 | | reflect that as well. */ |
2244 | 5.79k | insn->displacementOffset = insn->immediateOffset; |
2245 | 5.79k | insn->consumedDisplacement = true; |
2246 | 5.79k | insn->displacementSize = insn->immediateSize; |
2247 | 5.79k | insn->displacement = |
2248 | 5.79k | insn->immediates[insn->numImmediatesConsumed - |
2249 | 5.79k | 1]; |
2250 | 5.79k | insn->immediateOffset = 0; |
2251 | 5.79k | insn->immediateSize = 0; |
2252 | 5.79k | break; |
2253 | | |
2254 | 3.08k | case ENCODING_IRC: |
2255 | 3.08k | insn->RC = |
2256 | 3.08k | (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]) |
2257 | 3.08k | << 1) | |
2258 | 3.08k | lFromEVEX4of4(insn->vectorExtensionPrefix[3]); |
2259 | 3.08k | break; |
2260 | | |
2261 | 6.59k | case ENCODING_RB: |
2262 | 6.59k | if (readOpcodeRegister(insn, 1)) |
2263 | 0 | return -1; |
2264 | 6.59k | break; |
2265 | | |
2266 | 6.59k | case ENCODING_RW: |
2267 | 0 | if (readOpcodeRegister(insn, 2)) |
2268 | 0 | return -1; |
2269 | 0 | break; |
2270 | | |
2271 | 0 | case ENCODING_RD: |
2272 | 0 | if (readOpcodeRegister(insn, 4)) |
2273 | 0 | return -1; |
2274 | 0 | break; |
2275 | | |
2276 | 9.34k | case ENCODING_RO: |
2277 | 9.34k | if (readOpcodeRegister(insn, 8)) |
2278 | 0 | return -1; |
2279 | 9.34k | break; |
2280 | | |
2281 | 35.7k | case ENCODING_Rv: |
2282 | 35.7k | if (readOpcodeRegister(insn, 0)) |
2283 | 0 | return -1; |
2284 | 35.7k | break; |
2285 | | |
2286 | 35.7k | case ENCODING_FP: |
2287 | 2.32k | break; |
2288 | | |
2289 | 55.6k | case ENCODING_VVVV: |
2290 | 55.6k | if (!hasVVVV) |
2291 | 0 | return -1; |
2292 | | |
2293 | 55.6k | needVVVV = |
2294 | 55.6k | 0; /* Mark that we have found a VVVV operand. */ |
2295 | | |
2296 | 55.6k | if (insn->mode != MODE_64BIT) |
2297 | 36.0k | insn->vvvv = (Reg)(insn->vvvv & 0x7); |
2298 | | |
2299 | 55.6k | if (fixupReg(insn, op)) |
2300 | 1 | return -1; |
2301 | 55.6k | break; |
2302 | | |
2303 | 55.6k | case ENCODING_WRITEMASK: |
2304 | 42.3k | if (readMaskRegister(insn)) |
2305 | 0 | return -1; |
2306 | 42.3k | break; |
2307 | | |
2308 | 139k | case ENCODING_DUP: |
2309 | 139k | break; |
2310 | | |
2311 | 0 | default: |
2312 | | // dbgprintf(insn, "Encountered an operand with an unknown encoding."); |
2313 | 0 | return -1; |
2314 | 3.55M | } |
2315 | 3.55M | } |
2316 | | |
2317 | | /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */ |
2318 | 592k | if (needVVVV) |
2319 | 8 | return -1; |
2320 | | |
2321 | 592k | return 0; |
2322 | 592k | } |
2323 | | |
2324 | | // return True if instruction is illegal to use with prefixes |
2325 | | // This also check & fix the isPrefixNN when a prefix is irrelevant. |
2326 | | static bool checkPrefix(struct InternalInstruction *insn) |
2327 | 593k | { |
2328 | | // LOCK prefix |
2329 | 593k | if (insn->hasLockPrefix) { |
2330 | 34.2k | switch (insn->instructionID) { |
2331 | 118 | default: |
2332 | | // invalid LOCK |
2333 | 118 | return true; |
2334 | | |
2335 | | // nop dword [rax] |
2336 | 194 | case X86_NOOPL: |
2337 | | |
2338 | | // DEC |
2339 | 260 | case X86_DEC16m: |
2340 | 480 | case X86_DEC32m: |
2341 | 674 | case X86_DEC64m: |
2342 | 741 | case X86_DEC8m: |
2343 | | |
2344 | | // ADC |
2345 | 954 | case X86_ADC16mi: |
2346 | 1.14k | case X86_ADC16mi8: |
2347 | 1.35k | case X86_ADC16mr: |
2348 | 1.55k | case X86_ADC32mi: |
2349 | 1.75k | case X86_ADC32mi8: |
2350 | 1.98k | case X86_ADC32mr: |
2351 | 2.18k | case X86_ADC64mi32: |
2352 | 2.38k | case X86_ADC64mi8: |
2353 | 2.58k | case X86_ADC64mr: |
2354 | 2.78k | case X86_ADC8mi: |
2355 | 2.98k | case X86_ADC8mi8: |
2356 | 3.23k | case X86_ADC8mr: |
2357 | 3.30k | case X86_ADC8rm: |
2358 | 3.51k | case X86_ADC16rm: |
2359 | 3.73k | case X86_ADC32rm: |
2360 | 3.83k | case X86_ADC64rm: |
2361 | | |
2362 | | // ADD |
2363 | 4.05k | case X86_ADD16mi: |
2364 | 4.27k | case X86_ADD16mi8: |
2365 | 4.50k | case X86_ADD16mr: |
2366 | 4.71k | case X86_ADD32mi: |
2367 | 4.92k | case X86_ADD32mi8: |
2368 | 5.20k | case X86_ADD32mr: |
2369 | 5.39k | case X86_ADD64mi32: |
2370 | 5.53k | case X86_ADD64mi8: |
2371 | 5.74k | case X86_ADD64mr: |
2372 | 5.98k | case X86_ADD8mi: |
2373 | 6.20k | case X86_ADD8mi8: |
2374 | 6.91k | case X86_ADD8mr: |
2375 | 7.11k | case X86_ADD8rm: |
2376 | 7.31k | case X86_ADD16rm: |
2377 | 7.54k | case X86_ADD32rm: |
2378 | 7.74k | case X86_ADD64rm: |
2379 | | |
2380 | | // AND |
2381 | 7.94k | case X86_AND16mi: |
2382 | 8.14k | case X86_AND16mi8: |
2383 | 8.38k | case X86_AND16mr: |
2384 | 8.58k | case X86_AND32mi: |
2385 | 8.93k | case X86_AND32mi8: |
2386 | 9.21k | case X86_AND32mr: |
2387 | 9.28k | case X86_AND64mi32: |
2388 | 9.61k | case X86_AND64mi8: |
2389 | 9.82k | case X86_AND64mr: |
2390 | 10.0k | case X86_AND8mi: |
2391 | 10.2k | case X86_AND8mi8: |
2392 | 10.5k | case X86_AND8mr: |
2393 | 10.8k | case X86_AND8rm: |
2394 | 11.1k | case X86_AND16rm: |
2395 | 11.3k | case X86_AND32rm: |
2396 | 11.5k | case X86_AND64rm: |
2397 | | |
2398 | | // BTC |
2399 | 11.7k | case X86_BTC16mi8: |
2400 | 11.9k | case X86_BTC16mr: |
2401 | 12.1k | case X86_BTC32mi8: |
2402 | 12.4k | case X86_BTC32mr: |
2403 | 12.6k | case X86_BTC64mi8: |
2404 | 13.0k | case X86_BTC64mr: |
2405 | | |
2406 | | // BTR |
2407 | 13.0k | case X86_BTR16mi8: |
2408 | 13.3k | case X86_BTR16mr: |
2409 | 13.3k | case X86_BTR32mi8: |
2410 | 13.6k | case X86_BTR32mr: |
2411 | 13.8k | case X86_BTR64mi8: |
2412 | 13.9k | case X86_BTR64mr: |
2413 | | |
2414 | | // BTS |
2415 | 14.1k | case X86_BTS16mi8: |
2416 | 14.3k | case X86_BTS16mr: |
2417 | 14.5k | case X86_BTS32mi8: |
2418 | 14.7k | case X86_BTS32mr: |
2419 | 14.9k | case X86_BTS64mi8: |
2420 | 15.1k | case X86_BTS64mr: |
2421 | | |
2422 | | // CMPXCHG |
2423 | 15.5k | case X86_CMPXCHG16B: |
2424 | 15.7k | case X86_CMPXCHG16rm: |
2425 | 15.9k | case X86_CMPXCHG32rm: |
2426 | 16.1k | case X86_CMPXCHG64rm: |
2427 | 16.3k | case X86_CMPXCHG8rm: |
2428 | 16.4k | case X86_CMPXCHG8B: |
2429 | | |
2430 | | // INC |
2431 | 16.6k | case X86_INC16m: |
2432 | 16.8k | case X86_INC32m: |
2433 | 17.0k | case X86_INC64m: |
2434 | 17.2k | case X86_INC8m: |
2435 | | |
2436 | | // NEG |
2437 | 17.4k | case X86_NEG16m: |
2438 | 17.6k | case X86_NEG32m: |
2439 | 17.7k | case X86_NEG64m: |
2440 | 17.9k | case X86_NEG8m: |
2441 | | |
2442 | | // NOT |
2443 | 18.1k | case X86_NOT16m: |
2444 | 18.3k | case X86_NOT32m: |
2445 | 18.5k | case X86_NOT64m: |
2446 | 18.7k | case X86_NOT8m: |
2447 | | |
2448 | | // OR |
2449 | 18.9k | case X86_OR16mi: |
2450 | 19.2k | case X86_OR16mi8: |
2451 | 19.3k | case X86_OR16mr: |
2452 | 19.6k | case X86_OR32mi: |
2453 | 19.8k | case X86_OR32mi8: |
2454 | 20.0k | case X86_OR32mr: |
2455 | 20.2k | case X86_OR64mi32: |
2456 | 20.5k | case X86_OR64mi8: |
2457 | 20.7k | case X86_OR64mr: |
2458 | 20.9k | case X86_OR8mi8: |
2459 | 21.0k | case X86_OR8mi: |
2460 | 21.4k | case X86_OR8mr: |
2461 | 21.6k | case X86_OR8rm: |
2462 | 21.9k | case X86_OR16rm: |
2463 | 22.2k | case X86_OR32rm: |
2464 | 22.2k | case X86_OR64rm: |
2465 | | |
2466 | | // SBB |
2467 | 22.4k | case X86_SBB16mi: |
2468 | 22.6k | case X86_SBB16mi8: |
2469 | 22.8k | case X86_SBB16mr: |
2470 | 23.1k | case X86_SBB32mi: |
2471 | 23.3k | case X86_SBB32mi8: |
2472 | 23.7k | case X86_SBB32mr: |
2473 | 23.9k | case X86_SBB64mi32: |
2474 | 24.1k | case X86_SBB64mi8: |
2475 | 24.4k | case X86_SBB64mr: |
2476 | 24.5k | case X86_SBB8mi: |
2477 | 24.7k | case X86_SBB8mi8: |
2478 | 24.9k | case X86_SBB8mr: |
2479 | | |
2480 | | // SUB |
2481 | 25.1k | case X86_SUB16mi: |
2482 | 25.3k | case X86_SUB16mi8: |
2483 | 25.5k | case X86_SUB16mr: |
2484 | 25.7k | case X86_SUB32mi: |
2485 | 25.9k | case X86_SUB32mi8: |
2486 | 26.2k | case X86_SUB32mr: |
2487 | 26.4k | case X86_SUB64mi32: |
2488 | 26.6k | case X86_SUB64mi8: |
2489 | 26.8k | case X86_SUB64mr: |
2490 | 26.9k | case X86_SUB8mi8: |
2491 | 27.1k | case X86_SUB8mi: |
2492 | 27.3k | case X86_SUB8mr: |
2493 | 27.6k | case X86_SUB8rm: |
2494 | 27.9k | case X86_SUB16rm: |
2495 | 28.1k | case X86_SUB32rm: |
2496 | 28.3k | case X86_SUB64rm: |
2497 | | |
2498 | | // XADD |
2499 | 28.5k | case X86_XADD16rm: |
2500 | 28.7k | case X86_XADD32rm: |
2501 | 29.0k | case X86_XADD64rm: |
2502 | 29.2k | case X86_XADD8rm: |
2503 | | |
2504 | | // XCHG |
2505 | 29.6k | case X86_XCHG16rm: |
2506 | 29.8k | case X86_XCHG32rm: |
2507 | 30.0k | case X86_XCHG64rm: |
2508 | 30.3k | case X86_XCHG8rm: |
2509 | | |
2510 | | // XOR |
2511 | 30.5k | case X86_XOR16mi: |
2512 | 30.7k | case X86_XOR16mi8: |
2513 | 30.9k | case X86_XOR16mr: |
2514 | 31.1k | case X86_XOR32mi: |
2515 | 31.6k | case X86_XOR32mi8: |
2516 | 31.8k | case X86_XOR32mr: |
2517 | 31.9k | case X86_XOR64mi32: |
2518 | 32.1k | case X86_XOR64mi8: |
2519 | 32.3k | case X86_XOR64mr: |
2520 | 32.5k | case X86_XOR8mi8: |
2521 | 32.7k | case X86_XOR8mi: |
2522 | 33.0k | case X86_XOR8mr: |
2523 | 33.3k | case X86_XOR8rm: |
2524 | 33.5k | case X86_XOR16rm: |
2525 | 33.8k | case X86_XOR32rm: |
2526 | 34.0k | case X86_XOR64rm: |
2527 | | |
2528 | | // this instruction can be used with LOCK prefix |
2529 | 34.0k | return false; |
2530 | 34.2k | } |
2531 | 34.2k | } |
2532 | | |
2533 | | #if 0 |
2534 | | // REPNE prefix |
2535 | | if (insn->repeatPrefix) { |
2536 | | // 0xf2 can be a part of instruction encoding, but not really a prefix. |
2537 | | // In such a case, clear it. |
2538 | | if (insn->twoByteEscape == 0x0f) { |
2539 | | insn->prefix0 = 0; |
2540 | | } |
2541 | | } |
2542 | | #endif |
2543 | | |
2544 | | // no invalid prefixes |
2545 | 559k | return false; |
2546 | 593k | } |
2547 | | |
2548 | | /* |
2549 | | * decodeInstruction - Reads and interprets a full instruction provided by the |
2550 | | * user. |
2551 | | * |
2552 | | * @param insn - A pointer to the instruction to be populated. Must be |
2553 | | * pre-allocated. |
2554 | | * @param reader - The function to be used to read the instruction's bytes. |
2555 | | * @param readerArg - A generic argument to be passed to the reader to store |
2556 | | * any internal state. |
2557 | | * @param startLoc - The address (in the reader's address space) of the first |
2558 | | * byte in the instruction. |
2559 | | * @param mode - Capstone mode flags. |
2560 | | * @return - 0 if instruction is valid; nonzero if not. |
2561 | | */ |
2562 | | int decodeInstruction(struct InternalInstruction *insn, byteReader_t reader, |
2563 | | const void *readerArg, uint64_t startLoc, cs_mode mode) |
2564 | 596k | { |
2565 | 596k | insn->reader = reader; |
2566 | 596k | insn->readerArg = readerArg; |
2567 | 596k | insn->startLocation = startLoc; |
2568 | 596k | insn->readerCursor = startLoc; |
2569 | 596k | if (x86_has_feature(mode, CS_MODE_16)) |
2570 | 165k | insn->mode = MODE_16BIT; |
2571 | 431k | else if (x86_has_feature(mode, CS_MODE_32)) |
2572 | 202k | insn->mode = MODE_32BIT; |
2573 | 228k | else |
2574 | 228k | insn->mode = MODE_64BIT; |
2575 | 596k | insn->numImmediatesConsumed = 0; |
2576 | | |
2577 | 596k | if (readPrefixes(insn) || readOpcode(insn) || getID(insn, mode) || |
2578 | 594k | insn->instructionID == 0 || checkPrefix(insn) || readOperands(insn)) |
2579 | 4.71k | return -1; |
2580 | | |
2581 | 592k | insn->length = (size_t)(insn->readerCursor - insn->startLocation); |
2582 | | |
2583 | | // instruction length must be <= 15 to be valid |
2584 | 592k | if (insn->length > 15) |
2585 | 22 | return -1; |
2586 | | |
2587 | 592k | if (insn->operandSize == 0) |
2588 | 592k | insn->operandSize = insn->registerSize; |
2589 | | |
2590 | 592k | insn->operands = &x86OperandSets[insn->spec->operands][0]; |
2591 | | |
2592 | 592k | return 0; |
2593 | 592k | } |
2594 | | |
2595 | | #endif |