| 1 | ///////////////////////////////////////////////////////////////////////// |
| 2 | // $Id: dis_decode.cc 11873 2013-10-10 21:00:26Z sshwarts $ |
| 3 | ///////////////////////////////////////////////////////////////////////// |
| 4 | // |
| 5 | // Copyright (c) 2005-2012 Stanislav Shwartsman |
| 6 | // Written by Stanislav Shwartsman [sshwarts at sourceforge net] |
| 7 | // |
| 8 | // This library is free software; you can redistribute it and/or |
| 9 | // modify it under the terms of the GNU Lesser General Public |
| 10 | // License as published by the Free Software Foundation; either |
| 11 | // version 2 of the License, or (at your option) any later version. |
| 12 | // |
| 13 | // This library is distributed in the hope that it will be useful, |
| 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | // Lesser General Public License for more details. |
| 17 | // |
| 18 | // You should have received a copy of the GNU Lesser General Public |
| 19 | // License along with this library; if not, write to the Free Software |
| 20 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdarg.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "disasm.h" |
| 27 | #include "dis_tables.h" |
| 28 | |
| 29 | #define OPCODE(entry) ((BxDisasmOpcodeInfo_t*) entry->OpcodeInfo) |
| 30 | #define OPCODE_TABLE(entry) ((BxDisasmOpcodeTable_t*) entry->OpcodeInfo) |
| 31 | |
| 32 | static const unsigned char instruction_has_modrm[512] = { |
| 33 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 34 | /* ------------------------------- */ |
| 35 | /* 00 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, |
| 36 | /* 10 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, |
| 37 | /* 20 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, |
| 38 | /* 30 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, |
| 39 | /* 40 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 40 | /* 50 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 41 | /* 60 */ 0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0, |
| 42 | /* 70 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 43 | /* 80 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 44 | /* 90 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 45 | /* A0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 46 | /* B0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 47 | /* C0 */ 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0, |
| 48 | /* D0 */ 1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1, |
| 49 | /* E0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 50 | /* F0 */ 0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1, |
| 51 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 52 | /* ------------------------------- */ |
| 53 | 1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,1, /* 0F 00 */ |
| 54 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0F 10 */ |
| 55 | 1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1, /* 0F 20 */ |
| 56 | 0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0, /* 0F 30 */ |
| 57 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0F 40 */ |
| 58 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0F 50 */ |
| 59 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0F 60 */ |
| 60 | 1,1,1,1,1,1,1,0,1,1,0,0,1,1,1,1, /* 0F 70 */ |
| 61 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0F 80 */ |
| 62 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0F 90 */ |
| 63 | 0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1, /* 0F A0 */ |
| 64 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0F B0 */ |
| 65 | 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0, /* 0F C0 */ |
| 66 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0F D0 */ |
| 67 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0F E0 */ |
| 68 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0 /* 0F F0 */ |
| 69 | /* ------------------------------- */ |
| 70 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 71 | }; |
| 72 | |
| 73 | unsigned disassembler::disasm(bx_bool is_32, bx_bool is_64, bx_address cs_base, bx_address ip, const Bit8u *instr, char *disbuf) |
| 74 | { |
| 75 | x86_insn insn = decode(is_32, is_64, cs_base, ip, instr, disbuf); |
| 76 | return insn.ilen; |
| 77 | } |
| 78 | |
| 79 | x86_insn disassembler::decode(bx_bool is_32, bx_bool is_64, bx_address cs_base, bx_address ip, const Bit8u *instr, char *disbuf) |
| 80 | { |
| 81 | if (is_64) is_32 = 1; |
| 82 | x86_insn insn(is_32, is_64); |
| 83 | const Bit8u *instruction_begin = instruction = instr; |
| 84 | resolve_modrm = NULL; |
| 85 | |
| 86 | db_eip = ip; |
| 87 | db_cs_base = cs_base; // cs linear base (cs_base for PM & cs<<4 for RM & VM) |
| 88 | |
| 89 | disbufptr = disbuf; // start sprintf()'ing into beginning of buffer |
| 90 | |
| 91 | #define SSE_PREFIX_NONE 0 |
| 92 | #define SSE_PREFIX_66 1 |
| 93 | #define SSE_PREFIX_F3 2 |
| 94 | #define SSE_PREFIX_F2 3 /* only one SSE prefix could be used */ |
| 95 | unsigned sse_prefix = SSE_PREFIX_NONE, sse_opcode = 0; |
| 96 | unsigned rex_prefix = 0, prefixes = 0; |
| 97 | |
| 98 | for(;;) |
| 99 | { |
| 100 | insn.b1 = fetch_byte(); |
| 101 | prefixes++; |
| 102 | |
| 103 | switch(insn.b1) { |
| 104 | case 0x40: // rex |
| 105 | case 0x41: |
| 106 | case 0x42: |
| 107 | case 0x43: |
| 108 | case 0x44: |
| 109 | case 0x45: |
| 110 | case 0x46: |
| 111 | case 0x47: |
| 112 | case 0x48: |
| 113 | case 0x49: |
| 114 | case 0x4A: |
| 115 | case 0x4B: |
| 116 | case 0x4C: |
| 117 | case 0x4D: |
| 118 | case 0x4E: |
| 119 | case 0x4F: |
| 120 | if (! is_64) break; |
| 121 | rex_prefix = insn.b1; |
| 122 | continue; |
| 123 | |
| 124 | case 0x26: // ES: |
| 125 | case 0x2e: // CS: |
| 126 | case 0x36: // SS: |
| 127 | case 0x3e: // DS: |
| 128 | if (! is_64) insn.seg_override = (insn.b1 >> 3) & 3; |
| 129 | rex_prefix = 0; |
| 130 | continue; |
| 131 | |
| 132 | case 0x64: // FS: |
| 133 | case 0x65: // GS: |
| 134 | insn.seg_override = insn.b1 & 0xf; |
| 135 | rex_prefix = 0; |
| 136 | continue; |
| 137 | |
| 138 | case 0x66: // operand size override |
| 139 | if (!insn.os_64) insn.os_32 = !is_32; |
| 140 | if (!sse_prefix) sse_prefix = SSE_PREFIX_66; |
| 141 | rex_prefix = 0; |
| 142 | continue; |
| 143 | |
| 144 | case 0x67: // address size override |
| 145 | if (!is_64) insn.as_32 = !is_32; |
| 146 | insn.as_64 = 0; |
| 147 | rex_prefix = 0; |
| 148 | continue; |
| 149 | |
| 150 | case 0xf0: // lock |
| 151 | rex_prefix = 0; |
| 152 | continue; |
| 153 | |
| 154 | case 0xf2: // repne |
| 155 | case 0xf3: // rep |
| 156 | sse_prefix = (insn.b1 & 0xf) ^ 1; |
| 157 | rex_prefix = 0; |
| 158 | continue; |
| 159 | |
| 160 | // no more prefixes |
| 161 | default: |
| 162 | break; |
| 163 | } |
| 164 | |
| 165 | break; |
| 166 | } |
| 167 | |
| 168 | if (insn.b1 == 0x0f) |
| 169 | { |
| 170 | insn.b1 = 0x100 | fetch_byte(); |
| 171 | } |
| 172 | |
| 173 | if (rex_prefix) { |
| 174 | insn.extend8b = 1; |
| 175 | if (rex_prefix & 0x8) { |
| 176 | insn.os_64 = 1; |
| 177 | insn.os_32 = 1; |
| 178 | } |
| 179 | if (rex_prefix & 0x4) insn.rex_r = 8; |
| 180 | if (rex_prefix & 0x2) insn.rex_x = 8; |
| 181 | if (rex_prefix & 0x1) insn.rex_b = 8; |
| 182 | } |
| 183 | |
| 184 | const BxDisasmOpcodeTable_t *opcode_table, *entry; |
| 185 | |
| 186 | if (is_64) { |
| 187 | if (insn.os_64) |
| 188 | opcode_table = BxDisasmOpcodes64q; |
| 189 | else if (insn.os_32) |
| 190 | opcode_table = BxDisasmOpcodes64d; |
| 191 | else |
| 192 | opcode_table = BxDisasmOpcodes64w; |
| 193 | } else { |
| 194 | if (insn.os_32) |
| 195 | opcode_table = BxDisasmOpcodes32; |
| 196 | else |
| 197 | opcode_table = BxDisasmOpcodes16; |
| 198 | } |
| 199 | |
| 200 | entry = opcode_table + insn.b1; |
| 201 | |
| 202 | if ((insn.b1 & ~1) == 0xc4 && (is_64 || (peek_byte() & 0xc0) == 0xc0)) |
| 203 | { |
| 204 | if (sse_prefix) |
| 205 | dis_sprintf("(bad vex+rex prefix) "); |
| 206 | if (rex_prefix) |
| 207 | dis_sprintf("(bad vex+sse prefix) "); |
| 208 | |
| 209 | // decode 0xC4 or 0xC5 VEX prefix |
| 210 | sse_prefix = decode_vex(&insn); |
| 211 | if (insn.b1 < 256 || insn.b1 >= 1024) |
| 212 | entry = &BxDisasmGroupSSE_ERR[0]; |
| 213 | else |
| 214 | entry = BxDisasmOpcodesAVX + (insn.b1 - 256); |
| 215 | } |
| 216 | /* |
| 217 | if (insn.b1== 0x62 && (is_64 || (peek_byte() & 0xc0) == 0xc0)) |
| 218 | { |
| 219 | if (sse_prefix) |
| 220 | dis_sprintf("(bad evex+rex prefix) "); |
| 221 | if (rex_prefix) |
| 222 | dis_sprintf("(bad evex+sse prefix) "); |
| 223 | |
| 224 | // decode 0x62 EVEX prefix |
| 225 | sse_prefix = decode_evex(&insn); |
| 226 | if (insn.b1 < 256 || insn.b1 >= 1024) |
| 227 | entry = &BxDisasmGroupSSE_ERR[0]; |
| 228 | // else |
| 229 | // entry = BxDisasmOpcodesEVEX + (insn.b1 - 256); |
| 230 | } |
| 231 | */ |
| 232 | else if (insn.b1 == 0x8f && (is_64 || (peek_byte() & 0xc0) == 0xc0) && (peek_byte() & 0x8) == 0x8) |
| 233 | { |
| 234 | if (sse_prefix) |
| 235 | dis_sprintf("(bad xop+rex prefix) "); |
| 236 | if (rex_prefix) |
| 237 | dis_sprintf("(bad xop+sse prefix) "); |
| 238 | |
| 239 | // decode 0x8F XOP prefix |
| 240 | sse_prefix = decode_xop(&insn); |
| 241 | if (insn.b1 >= 768 || sse_prefix != 0) |
| 242 | entry = &BxDisasmGroupSSE_ERR[0]; |
| 243 | else |
| 244 | entry = BxDisasmOpcodesXOP + insn.b1; |
| 245 | } |
| 246 | |
| 247 | if (insn.b1 >= 512 || instruction_has_modrm[insn.b1] || insn.is_xop > 0) |
| 248 | { |
| 249 | // take 3rd byte for 3-byte opcode |
| 250 | if (entry->Attr == _GRP3BOP) { |
| 251 | entry = &(OPCODE_TABLE(entry)[fetch_byte()]); |
| 252 | } |
| 253 | |
| 254 | decode_modrm(&insn); |
| 255 | } |
| 256 | |
| 257 | int attr = entry->Attr; |
| 258 | while(attr) |
| 259 | { |
| 260 | switch(attr) { |
| 261 | case _GROUPN: |
| 262 | entry = &(OPCODE_TABLE(entry)[insn.nnn & 7]); |
| 263 | break; |
| 264 | |
| 265 | case _GRPSSE66: |
| 266 | /* SSE opcode group with only prefix 0x66 allowed */ |
| 267 | sse_opcode = 1; |
| 268 | if (sse_prefix != SSE_PREFIX_66) |
| 269 | entry = &(BxDisasmGroupSSE_ERR[sse_prefix]); |
| 270 | attr = 0; |
| 271 | continue; |
| 272 | |
| 273 | case _GRPSSEF2: |
| 274 | /* SSE opcode group with only prefix 0xF2 allowed */ |
| 275 | sse_opcode = 1; |
| 276 | if (sse_prefix != SSE_PREFIX_F2) |
| 277 | entry = &(BxDisasmGroupSSE_ERR[sse_prefix]); |
| 278 | attr = 0; |
| 279 | continue; |
| 280 | |
| 281 | case _GRPSSEF3: |
| 282 | /* SSE opcode group with only prefix 0xF3 allowed */ |
| 283 | sse_opcode = 1; |
| 284 | if (sse_prefix != SSE_PREFIX_F3) |
| 285 | entry = &(BxDisasmGroupSSE_ERR[sse_prefix]); |
| 286 | attr = 0; |
| 287 | continue; |
| 288 | |
| 289 | case _GRPSSENONE: |
| 290 | /* SSE opcode group with no prefix only allowed */ |
| 291 | sse_opcode = 1; |
| 292 | if (sse_prefix != SSE_PREFIX_NONE) |
| 293 | entry = &(BxDisasmGroupSSE_ERR[sse_prefix]); |
| 294 | attr = 0; |
| 295 | continue; |
| 296 | |
| 297 | case _GRPSSE: |
| 298 | sse_opcode = 1; |
| 299 | /* For SSE opcodes, look into another 4 entries table |
| 300 | with the opcode prefixes (NONE, 0x66, 0xF2, 0xF3) */ |
| 301 | entry = &(OPCODE_TABLE(entry)[sse_prefix]); |
| 302 | break; |
| 303 | |
| 304 | case _GRPSSE2: |
| 305 | sse_opcode = 1; |
| 306 | /* For SSE opcodes, look into another 2 entries table |
| 307 | with the opcode prefixes (NONE, 0x66) |
| 308 | SSE prefixes 0xF2 and 0xF3 are not allowed */ |
| 309 | if (sse_prefix > SSE_PREFIX_66) |
| 310 | entry = &(BxDisasmGroupSSE_ERR[sse_prefix]); |
| 311 | else |
| 312 | entry = &(OPCODE_TABLE(entry)[sse_prefix]); |
| 313 | break; |
| 314 | |
| 315 | case _SPLIT11B: |
| 316 | entry = &(OPCODE_TABLE(entry)[insn.mod != 3]); /* REG/MEM */ |
| 317 | break; |
| 318 | |
| 319 | case _GRPRM: |
| 320 | entry = &(OPCODE_TABLE(entry)[insn.rm & 7]); |
| 321 | break; |
| 322 | |
| 323 | case _GRPFP: |
| 324 | if(insn.mod != 3) |
| 325 | { |
| 326 | entry = &(OPCODE_TABLE(entry)[insn.nnn & 7]); |
| 327 | } else { |
| 328 | int index = (insn.b1-0xD8)*64 + (insn.modrm & 0x3f); |
| 329 | entry = &(BxDisasmOpcodeInfoFP[index]); |
| 330 | } |
| 331 | break; |
| 332 | |
| 333 | case _GRP3DNOW: |
| 334 | entry = &(BxDisasm3DNowGroup[fetch_byte()]); |
| 335 | break; |
| 336 | |
| 337 | case _GRP64B: |
| 338 | entry = &(OPCODE_TABLE(entry)[insn.os_64 ? 2 : insn.os_32]); |
| 339 | if (sse_prefix == SSE_PREFIX_66) |
| 340 | sse_prefix = 0; |
| 341 | break; |
| 342 | |
| 343 | case _GRPVEXW: |
| 344 | entry = &(OPCODE_TABLE(entry)[insn.vex_w]); |
| 345 | break; |
| 346 | |
| 347 | default: |
| 348 | printf("Internal disassembler error - unknown attribute !\n"); |
| 349 | return x86_insn(is_32, is_64); |
| 350 | } |
| 351 | |
| 352 | /* get additional attributes from group table */ |
| 353 | attr = entry->Attr; |
| 354 | } |
| 355 | |
| 356 | #define BRANCH_NOT_TAKEN 0x2E |
| 357 | #define BRANCH_TAKEN 0x3E |
| 358 | |
| 359 | unsigned branch_hint = 0; |
| 360 | |
| 361 | // print prefixes |
| 362 | for(unsigned i=0;i<prefixes;i++) |
| 363 | { |
| 364 | Bit8u prefix_byte = *(instr+i); |
| 365 | |
| 366 | if (prefix_byte == 0xF0) dis_sprintf("lock "); |
| 367 | |
| 368 | if (! insn.is_xop && ! insn.is_vex) { |
| 369 | if (insn.b1 == 0x90 && !insn.rex_b && prefix_byte == 0xF3) |
| 370 | continue; |
| 371 | |
| 372 | if (prefix_byte == 0xF3 || prefix_byte == 0xF2) { |
| 373 | if (! sse_opcode) { |
| 374 | const BxDisasmOpcodeTable_t *prefix = &(opcode_table[prefix_byte]); |
| 375 | dis_sprintf("%s ", OPCODE(prefix)->IntelOpcode); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | // branch hint for jcc instructions |
| 380 | if ((insn.b1 >= 0x070 && insn.b1 <= 0x07F) || |
| 381 | (insn.b1 >= 0x180 && insn.b1 <= 0x18F)) |
| 382 | { |
| 383 | if (prefix_byte == BRANCH_NOT_TAKEN || prefix_byte == BRANCH_TAKEN) |
| 384 | branch_hint = prefix_byte; |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | const BxDisasmOpcodeInfo_t *opcode = OPCODE(entry); |
| 390 | |
| 391 | if (! insn.is_xop && ! insn.is_vex) { |
| 392 | // patch jecx opcode |
| 393 | if (insn.b1 == 0xE3 && insn.as_32 && !insn.as_64) |
| 394 | opcode = &Ia_jecxz_Jb; |
| 395 | |
| 396 | // fix nop opcode |
| 397 | if (insn.b1 == 0x90) { |
| 398 | if (sse_prefix == SSE_PREFIX_F3) |
| 399 | opcode = &Ia_pause; |
| 400 | else if (!insn.rex_b) |
| 401 | opcode = &Ia_nop; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | // print instruction disassembly |
| 406 | if (intel_mode) |
| 407 | print_disassembly_intel(&insn, opcode); |
| 408 | else |
| 409 | print_disassembly_att (&insn, opcode); |
| 410 | |
| 411 | if (branch_hint == BRANCH_NOT_TAKEN) |
| 412 | { |
| 413 | dis_sprintf(", not taken"); |
| 414 | } |
| 415 | else if (branch_hint == BRANCH_TAKEN) |
| 416 | { |
| 417 | dis_sprintf(", taken"); |
| 418 | } |
| 419 | |
| 420 | if (insn.is_vex < 0) |
| 421 | dis_sprintf(" (bad vex)"); |
| 422 | else if (insn.is_evex < 0) |
| 423 | dis_sprintf(" (bad evex)"); |
| 424 | else if (insn.is_xop < 0) |
| 425 | dis_sprintf(" (bad xop)"); |
| 426 | |
| 427 | insn.ilen = (unsigned)(instruction - instruction_begin); |
| 428 | |
| 429 | return insn; |
| 430 | } |
| 431 | |
| 432 | unsigned disassembler::decode_vex(x86_insn *insn) |
| 433 | { |
| 434 | insn->is_vex = 1; |
| 435 | |
| 436 | unsigned b2 = fetch_byte(), vex_opcode_extension = 1; |
| 437 | |
| 438 | insn->rex_r = (b2 & 0x80) ? 0 : 0x8; |
| 439 | |
| 440 | if (insn->b1 == 0xc4) { |
| 441 | // decode 3-byte VEX prefix |
| 442 | insn->rex_x = (b2 & 0x40) ? 0 : 0x8; |
| 443 | if (insn->is_64) |
| 444 | insn->rex_b = (b2 & 0x20) ? 0 : 0x8; |
| 445 | |
| 446 | vex_opcode_extension = b2 & 0x1f; |
| 447 | if (! vex_opcode_extension || vex_opcode_extension > 3) |
| 448 | insn->is_vex = -1; |
| 449 | |
| 450 | b2 = fetch_byte(); // fetch VEX3 byte |
| 451 | if (b2 & 0x80) { |
| 452 | insn->os_64 = 1; |
| 453 | insn->os_32 = 1; |
| 454 | insn->vex_w = 1; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | insn->vex_vvv = 15 - ((b2 >> 3) & 0xf); |
| 459 | if (! insn->is_64) insn->vex_vvv &= 7; |
| 460 | insn->vex_l = (b2 >> 2) & 0x1; |
| 461 | insn->b1 = fetch_byte() + 256 * vex_opcode_extension; |
| 462 | return b2 & 0x3; |
| 463 | } |
| 464 | |
| 465 | unsigned disassembler::decode_evex(x86_insn *insn) |
| 466 | { |
| 467 | insn->is_evex = 1; |
| 468 | |
| 469 | Bit32u evex = fetch_dword(); |
| 470 | |
| 471 | // check for reserved EVEX bits |
| 472 | if ((evex & 0x0c) != 0 || (evex & 0x400) == 0) { |
| 473 | insn->is_evex = -1; |
| 474 | } |
| 475 | |
| 476 | unsigned evex_opcext = evex & 0x3; |
| 477 | if (evex_opcext == 0) { |
| 478 | insn->is_evex = -1; |
| 479 | } |
| 480 | |
| 481 | if (insn->is_64) { |
| 482 | insn->rex_r = ((evex >> 4) & 0x8) ^ 0x8; |
| 483 | insn->rex_r |= (evex & 0x10) ^ 0x10; |
| 484 | insn->rex_x = ((evex >> 3) & 0x8) ^ 0x8; |
| 485 | insn->rex_b = ((evex >> 2) & 0x8) ^ 0x8; |
| 486 | insn->rex_b |= (insn->rex_x << 1); |
| 487 | } |
| 488 | |
| 489 | unsigned sse_prefix = (evex >> 8) & 0x3; |
| 490 | |
| 491 | insn->vex_vvv = 15 - ((evex >> 11) & 0xf); |
| 492 | unsigned evex_v = ((evex >> 15) & 0x10) ^ 0x10; |
| 493 | insn->vex_vvv |= evex_v; |
| 494 | if (! insn->is_64) insn->vex_vvv &= 7; |
| 495 | |
| 496 | insn->vex_w = (evex >> 15) & 0x1; |
| 497 | if (insn->vex_w) { |
| 498 | insn->os_64 = 1; |
| 499 | insn->os_32 = 1; |
| 500 | } |
| 501 | |
| 502 | insn->evex_b = (evex >> 20) & 0x1; |
| 503 | insn->evex_ll_rc = (evex >> 21) & 0x3; |
| 504 | insn->evex_z = (evex >> 23) & 0x1; |
| 505 | |
| 506 | insn->b1 = (evex >> 24); |
| 507 | insn->b1 += 256 * (evex_opcext-1); |
| 508 | |
| 509 | return sse_prefix; |
| 510 | } |
| 511 | |
| 512 | unsigned disassembler::decode_xop(x86_insn *insn) |
| 513 | { |
| 514 | insn->is_xop = 1; |
| 515 | |
| 516 | unsigned b2 = fetch_byte(); |
| 517 | |
| 518 | insn->rex_r = (b2 & 0x80) ? 0 : 0x8; |
| 519 | insn->rex_x = (b2 & 0x40) ? 0 : 0x8; |
| 520 | if (insn->is_64) |
| 521 | insn->rex_b = (b2 & 0x20) ? 0 : 0x8; |
| 522 | |
| 523 | unsigned xop_opcode_extension = (b2 & 0x1f) - 8; |
| 524 | if (xop_opcode_extension >= 3) |
| 525 | insn->is_xop = -1; |
| 526 | |
| 527 | b2 = fetch_byte(); // fetch VEX3 byte |
| 528 | if (b2 & 0x80) { |
| 529 | insn->os_64 = 1; |
| 530 | insn->os_32 = 1; |
| 531 | insn->vex_w = 1; |
| 532 | } |
| 533 | |
| 534 | insn->vex_vvv = 15 - ((b2 >> 3) & 0xf); |
| 535 | if (! insn->is_64) insn->vex_vvv &= 7; |
| 536 | insn->vex_l = (b2 >> 2) & 0x1; |
| 537 | insn->b1 = fetch_byte() + 256 * xop_opcode_extension; |
| 538 | |
| 539 | return b2 & 0x3; |
| 540 | } |
| 541 | |
| 542 | void disassembler::dis_sprintf(const char *fmt, ...) |
| 543 | { |
| 544 | va_list ap; |
| 545 | |
| 546 | va_start(ap, fmt); |
| 547 | vsprintf(disbufptr, fmt, ap); |
| 548 | va_end(ap); |
| 549 | |
| 550 | disbufptr += strlen(disbufptr); |
| 551 | } |
| 552 | |
| 553 | void disassembler::dis_putc(char symbol) |
| 554 | { |
| 555 | *disbufptr++ = symbol; |
| 556 | *disbufptr = 0; |
| 557 | } |