2013-09-30 23:01:42 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// $Id$
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2017-01-22 22:53:42 +03:00
|
|
|
// Copyright (c) 2013-2017 Stanislav Shwartsman
|
2013-09-30 23:01:42 +04:00
|
|
|
// Written by Stanislav Shwartsman [sshwarts at sourceforge net]
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA B 02110-1301 USA
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "bochs.h"
|
2017-11-29 22:24:00 +03:00
|
|
|
#ifndef BX_STANDALONE_DECODER
|
2016-06-13 00:23:48 +03:00
|
|
|
#include "../cpu.h"
|
2017-11-29 22:24:00 +03:00
|
|
|
#endif
|
2013-09-30 23:01:42 +04:00
|
|
|
|
2017-11-27 23:25:04 +03:00
|
|
|
#include "instr.h"
|
|
|
|
#include "decoder.h"
|
2013-09-30 23:01:42 +04:00
|
|
|
#include "fetchdecode.h"
|
|
|
|
|
2017-12-02 00:18:16 +03:00
|
|
|
extern int fetchDecode32(const Bit8u *fetchPtr, bx_bool is_32, bxInstruction_c *i, unsigned remainingInPage);
|
2016-06-13 00:23:48 +03:00
|
|
|
#if BX_SUPPORT_X86_64
|
2017-12-02 00:18:16 +03:00
|
|
|
extern int fetchDecode64(const Bit8u *fetchPtr, bxInstruction_c *i, unsigned remainingInPage);
|
2016-06-13 00:23:48 +03:00
|
|
|
#endif
|
|
|
|
|
2013-09-30 23:01:42 +04:00
|
|
|
// table of all Bochs opcodes
|
|
|
|
extern struct bxIAOpcodeTable BxOpcodesTable[];
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
char* dis_sprintf(char *disbufptr, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsprintf(disbufptr, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
disbufptr += strlen(disbufptr);
|
|
|
|
return disbufptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* dis_putc(char *disbufptr, char symbol)
|
|
|
|
{
|
|
|
|
*disbufptr++ = symbol;
|
|
|
|
*disbufptr = 0;
|
|
|
|
return disbufptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *intel_general_16bit_regname[16] = {
|
|
|
|
"ax", "cx", "dx", "bx", "sp", "bp", "si", "di",
|
|
|
|
"r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w"
|
|
|
|
};
|
|
|
|
|
2013-10-01 22:47:55 +04:00
|
|
|
static const char *intel_general_32bit_regname[17] = {
|
2013-09-30 23:01:42 +04:00
|
|
|
"eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi",
|
2013-10-01 22:47:55 +04:00
|
|
|
"r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d", "eip"
|
2013-09-30 23:01:42 +04:00
|
|
|
};
|
|
|
|
|
2013-10-01 22:47:55 +04:00
|
|
|
static const char *intel_general_64bit_regname[17] = {
|
2013-09-30 23:01:42 +04:00
|
|
|
"rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
|
2013-10-01 22:47:55 +04:00
|
|
|
"r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", "rip"
|
2013-09-30 23:01:42 +04:00
|
|
|
};
|
|
|
|
|
2013-10-05 22:51:28 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
2013-09-30 23:01:42 +04:00
|
|
|
static const char *intel_general_8bit_regname_rex[16] = {
|
|
|
|
"al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil",
|
|
|
|
"r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b"
|
|
|
|
};
|
2013-10-05 22:51:28 +04:00
|
|
|
#endif
|
2013-09-30 23:01:42 +04:00
|
|
|
|
|
|
|
static const char *intel_general_8bit_regname[8] = {
|
|
|
|
"al", "cl", "dl", "bl", "ah", "ch", "dh", "bh"
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *intel_segment_name[8] = {
|
|
|
|
"es", "cs", "ss", "ds", "fs", "gs", "??", "??"
|
|
|
|
};
|
|
|
|
|
2013-11-30 00:48:31 +04:00
|
|
|
static const char *intel_vector_reg_name[4] = {
|
|
|
|
"xmm", "ymm", "???", "zmm"
|
|
|
|
};
|
|
|
|
|
2013-10-01 22:47:55 +04:00
|
|
|
#if BX_SUPPORT_EVEX
|
|
|
|
static const char *rounding_mode[4] = {
|
|
|
|
"round_nearest_even", "round_down", "round_up", "round_to_zero"
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2013-10-06 23:04:52 +04:00
|
|
|
#define BX_JUMP_TARGET_NOT_REQ ((bx_address)(-1))
|
|
|
|
|
2013-10-15 21:19:18 +04:00
|
|
|
char *resolve_sib_scale(char *disbufptr, const bxInstruction_c *i, const char *regname[], unsigned src_index)
|
2013-09-30 23:01:42 +04:00
|
|
|
{
|
2013-10-15 21:19:18 +04:00
|
|
|
unsigned sib_index = i->sibIndex(), sib_scale = i->sibScale();
|
2013-10-02 23:23:34 +04:00
|
|
|
|
2014-04-29 22:49:38 +04:00
|
|
|
#if BX_SUPPORT_AVX
|
2013-10-15 21:19:18 +04:00
|
|
|
if (src_index == BX_SRC_VSIB)
|
2013-11-30 00:52:34 +04:00
|
|
|
disbufptr = dis_sprintf(disbufptr, "%s%d", intel_vector_reg_name[i->getVL() - 1], sib_index);
|
2013-10-15 21:19:18 +04:00
|
|
|
else
|
2014-04-29 22:49:38 +04:00
|
|
|
#endif
|
2013-10-15 21:19:18 +04:00
|
|
|
disbufptr = dis_sprintf(disbufptr, "%s", regname[sib_index]);
|
|
|
|
|
|
|
|
if (sib_scale)
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "*%d", 1 << sib_scale);
|
|
|
|
|
|
|
|
return disbufptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *resolve_memref(char *disbufptr, const bxInstruction_c *i, const char *regname[], unsigned src_index)
|
|
|
|
{
|
|
|
|
unsigned sib_base = i->sibBase(), sib_index = i->sibIndex();
|
|
|
|
|
|
|
|
if (sib_index == 4 && src_index != BX_SRC_VSIB)
|
|
|
|
sib_index = BX_NIL_REGISTER;
|
2013-10-05 15:00:31 +04:00
|
|
|
|
2013-10-02 23:23:34 +04:00
|
|
|
if (sib_base == BX_NIL_REGISTER)
|
2013-10-01 00:16:52 +04:00
|
|
|
{
|
2013-10-02 23:23:34 +04:00
|
|
|
if (sib_index == BX_NIL_REGISTER)
|
2013-10-01 00:16:52 +04:00
|
|
|
{
|
2013-10-07 00:42:13 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
if (i->as64L()) {
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "0x" FMT_ADDRX, (Bit64u) i->displ32s());
|
|
|
|
return disbufptr;
|
2013-10-01 00:16:52 +04:00
|
|
|
}
|
2013-10-07 00:42:13 +04:00
|
|
|
#endif
|
|
|
|
if (i->as32L()) {
|
2013-10-06 23:04:52 +04:00
|
|
|
disbufptr = dis_sprintf(disbufptr, "0x%08x", (Bit32u) i->displ32s());
|
2013-10-01 00:16:52 +04:00
|
|
|
}
|
2013-10-07 00:42:13 +04:00
|
|
|
else {
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "0x%04x", (Bit32u) (Bit16u) i->displ16s());
|
|
|
|
}
|
2013-10-01 00:16:52 +04:00
|
|
|
return disbufptr;
|
|
|
|
}
|
2013-09-30 23:01:42 +04:00
|
|
|
|
2013-10-15 21:19:18 +04:00
|
|
|
disbufptr = dis_putc(disbufptr, '[');
|
|
|
|
disbufptr = resolve_sib_scale(disbufptr, i, regname, src_index);
|
2013-09-30 23:01:42 +04:00
|
|
|
}
|
2013-10-01 00:16:52 +04:00
|
|
|
else {
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "[%s", regname[i->sibBase()]);
|
2013-09-30 23:01:42 +04:00
|
|
|
|
2013-10-15 21:19:18 +04:00
|
|
|
if (sib_index != BX_NIL_REGISTER) {
|
|
|
|
disbufptr = dis_putc(disbufptr, '+');
|
|
|
|
disbufptr = resolve_sib_scale(disbufptr, i, regname, src_index);
|
2013-09-30 23:01:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-07 00:42:13 +04:00
|
|
|
if (i->as32L()) {
|
|
|
|
if (i->displ32s() != 0) {
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "%+d", i->displ32s());
|
2013-09-30 23:01:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2013-10-07 00:42:13 +04:00
|
|
|
if (i->displ16s() != 0) {
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "%+d", (Bit32s) i->displ16s());
|
2013-09-30 23:01:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-01 00:16:52 +04:00
|
|
|
disbufptr = dis_putc(disbufptr, ']');
|
2013-09-30 23:01:42 +04:00
|
|
|
return disbufptr;
|
|
|
|
}
|
|
|
|
|
2017-05-09 22:49:27 +03:00
|
|
|
char *resolve_memsize(char *disbufptr, const bxInstruction_c *i, unsigned src_index, unsigned src_type)
|
|
|
|
{
|
|
|
|
if (src_index == BX_SRC_RM) {
|
|
|
|
switch(src_type) {
|
|
|
|
case BX_GPR8:
|
|
|
|
case BX_GPR8_32: // 8-bit memory ref but 32-bit GPR
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "byte ptr ");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BX_GPR16:
|
|
|
|
case BX_GPR16_32: // 16-bit memory ref but 32-bit GPR
|
|
|
|
case BX_SEGREG:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "word ptr ");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BX_GPR32:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "dword ptr ");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BX_GPR64:
|
|
|
|
case BX_MMX_REG:
|
|
|
|
#if BX_SUPPORT_EVEX
|
|
|
|
case BX_KMASK_REG:
|
|
|
|
#endif
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "qword ptr ");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BX_FPU_REG:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "tbyte ptr ");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BX_VMM_REG:
|
|
|
|
#if BX_SUPPORT_AVX
|
|
|
|
if (i->getVL() > BX_NO_VL)
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "%sword ptr ", intel_vector_reg_name[i->getVL() - 1]);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "xmmword ptr ");
|
|
|
|
break;
|
|
|
|
|
2017-05-10 21:31:59 +03:00
|
|
|
default:
|
2017-05-09 22:49:27 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return disbufptr;
|
|
|
|
}
|
|
|
|
|
2017-01-22 22:53:42 +03:00
|
|
|
// disasembly of memory reference
|
2017-05-09 22:49:27 +03:00
|
|
|
char *resolve_memref(char *disbufptr, const bxInstruction_c *i, unsigned src_index, unsigned src_type)
|
2013-09-30 23:01:42 +04:00
|
|
|
{
|
2017-05-09 22:49:27 +03:00
|
|
|
disbufptr = resolve_memsize(disbufptr, i, src_index, src_type);
|
|
|
|
|
2013-10-01 00:16:52 +04:00
|
|
|
// seg:[base + index*scale + disp]
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "%s:", intel_segment_name[i->seg()]);
|
2013-10-02 23:23:34 +04:00
|
|
|
if (i->as64L()) {
|
2013-10-15 21:19:18 +04:00
|
|
|
disbufptr = resolve_memref(disbufptr, i, intel_general_64bit_regname, src_index);
|
2013-09-30 23:01:42 +04:00
|
|
|
}
|
2013-10-02 23:23:34 +04:00
|
|
|
else if (i->as32L()) {
|
2013-10-15 21:19:18 +04:00
|
|
|
disbufptr = resolve_memref(disbufptr, i, intel_general_32bit_regname, src_index);
|
2013-09-30 23:01:42 +04:00
|
|
|
}
|
|
|
|
else {
|
2013-10-15 21:19:18 +04:00
|
|
|
disbufptr = resolve_memref(disbufptr, i, intel_general_16bit_regname, src_index);
|
2013-09-30 23:01:42 +04:00
|
|
|
}
|
|
|
|
return disbufptr;
|
|
|
|
}
|
|
|
|
|
2017-01-22 22:53:42 +03:00
|
|
|
// disasembly of register reference
|
|
|
|
char *disasm_regref(char *disbufptr, const bxInstruction_c *i, unsigned src_num, unsigned src_type)
|
|
|
|
{
|
|
|
|
unsigned srcreg = i->getSrcReg(src_num);
|
|
|
|
|
|
|
|
switch(src_type) {
|
|
|
|
case BX_GPR8:
|
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
if (i->extend8bitL())
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "%s", intel_general_8bit_regname_rex[srcreg]);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "%s", intel_general_8bit_regname[srcreg]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BX_GPR16:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "%s", intel_general_16bit_regname[srcreg]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BX_GPR32:
|
|
|
|
case BX_GPR8_32: // 8-bit memory ref but 32-bit GPR
|
|
|
|
case BX_GPR16_32: // 16-bit memory ref but 32-bit GPR
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "%s", intel_general_32bit_regname[srcreg]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
case BX_GPR64:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "%s", intel_general_64bit_regname[srcreg]);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case BX_FPU_REG:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "st(%d)", srcreg & 0x7);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BX_MMX_REG:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "mm%d", srcreg & 0x7);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BX_VMM_REG:
|
|
|
|
#if BX_SUPPORT_AVX
|
|
|
|
if (i->getVL() > BX_NO_VL) {
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "%s%d", intel_vector_reg_name[i->getVL() - 1], srcreg);
|
|
|
|
#if BX_SUPPORT_EVEX
|
|
|
|
if (src_num == 0 && i->opmask()) {
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "{k%d}%s", i->opmask(),
|
|
|
|
i->isZeroMasking() ? "{z}" : "");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "xmm%d", srcreg);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
#if BX_SUPPORT_EVEX
|
|
|
|
case BX_KMASK_REG:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "k%d", srcreg);
|
|
|
|
assert(srcreg < 8);
|
|
|
|
if (src_num == 0 && i->opmask()) {
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "{k%d}%s", i->opmask(),
|
|
|
|
i->isZeroMasking() ? "{z}" : "");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case BX_SEGREG:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "%s", intel_segment_name[srcreg]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BX_CREG:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "cr%d", srcreg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BX_DREG:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "dr%d", srcreg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (src_type != BX_NO_REGISTER)
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "(unknown source type %d)", src_type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return disbufptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *disasm_immediate(char *disbufptr, const bxInstruction_c *i, unsigned src_type, bx_address cs_base, bx_address rip)
|
|
|
|
{
|
2017-06-05 23:49:04 +03:00
|
|
|
switch(src_type) {
|
|
|
|
case BX_DIRECT_MEMREF_B:
|
|
|
|
disbufptr = resolve_memsize(disbufptr, i, BX_SRC_RM, BX_GPR8);
|
|
|
|
break;
|
|
|
|
case BX_DIRECT_MEMREF_W:
|
|
|
|
disbufptr = resolve_memsize(disbufptr, i, BX_SRC_RM, BX_GPR16);
|
|
|
|
break;
|
|
|
|
case BX_DIRECT_MEMREF_D:
|
|
|
|
disbufptr = resolve_memsize(disbufptr, i, BX_SRC_RM, BX_GPR32);
|
|
|
|
break;
|
|
|
|
case BX_DIRECT_MEMREF_Q:
|
|
|
|
disbufptr = resolve_memsize(disbufptr, i, BX_SRC_RM, BX_GPR64);
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
};
|
|
|
|
|
2017-01-22 22:53:42 +03:00
|
|
|
switch(src_type) {
|
|
|
|
case BX_IMMB:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "0x%02x", i->Ib());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BX_IMMW:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "0x%04x", i->Iw());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BX_IMMD:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "0x%08x", i->Id());
|
|
|
|
break;
|
|
|
|
|
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
case BX_IMMD_SE:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "0x" FMT_ADDRX64, (Bit64u) (Bit32s) i->Id());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BX_IMMQ:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "0x" FMT_ADDRX64, i->Iq());
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case BX_IMMB2:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "0x%02x", i->Ib2());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BX_IMM_BrOff16:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, ".%+d", (Bit32s) (Bit16s) i->Iw());
|
|
|
|
if (cs_base != BX_JUMP_TARGET_NOT_REQ) {
|
|
|
|
Bit16u target = (rip + i->ilen() + (Bit16s) i->Iw()) & 0xffff;
|
|
|
|
disbufptr = dis_sprintf(disbufptr, " (0x%08x)", (Bit32u)(cs_base + target));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BX_IMM_BrOff32:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, ".%+d", (Bit32s) i->Id());
|
|
|
|
if (cs_base != BX_JUMP_TARGET_NOT_REQ) {
|
|
|
|
Bit32u target = (Bit32u)(rip + i->ilen() + (Bit32s) i->Id());
|
|
|
|
disbufptr = dis_sprintf(disbufptr, " (0x%08x)", (Bit32u) (cs_base + target));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
case BX_IMM_BrOff64:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, ".%+d", (Bit32s) i->Id());
|
|
|
|
if (cs_base != BX_JUMP_TARGET_NOT_REQ) {
|
|
|
|
Bit64u target = rip + i->ilen() + (Bit32s) i->Id();
|
|
|
|
disbufptr = dis_sprintf(disbufptr, " (0x" FMT_ADDRX ")", (Bit64u) (cs_base + target));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case BX_DIRECT_PTR:
|
|
|
|
if (i->os32L())
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "0x%04x:%08x", i->Iw2(), i->Id());
|
|
|
|
else
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "0x%04x:%04x", i->Iw2(), i->Iw());
|
|
|
|
break;
|
|
|
|
|
2017-06-05 23:49:04 +03:00
|
|
|
case BX_DIRECT_MEMREF_B:
|
|
|
|
case BX_DIRECT_MEMREF_W:
|
|
|
|
case BX_DIRECT_MEMREF_D:
|
|
|
|
case BX_DIRECT_MEMREF_Q:
|
2017-01-22 22:53:42 +03:00
|
|
|
disbufptr = dis_sprintf(disbufptr, "%s:", intel_segment_name[i->seg()]);
|
2017-06-05 23:49:04 +03:00
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
if (i->as64L())
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "0x" FMT_ADDRX, i->Iq());
|
2017-01-22 22:53:42 +03:00
|
|
|
else
|
2017-06-05 23:49:04 +03:00
|
|
|
#endif
|
|
|
|
if (i->as32L())
|
2017-01-22 22:53:42 +03:00
|
|
|
disbufptr = dis_sprintf(disbufptr, "0x%08x", i->Id());
|
2017-06-05 23:49:04 +03:00
|
|
|
else
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "0x%04x", i->Id());
|
2017-01-22 22:53:42 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "(unknown immediate form for disasm %d)", src_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return disbufptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *disasm_implicit_src(char *disbufptr, const bxInstruction_c *i, unsigned src_type)
|
|
|
|
{
|
|
|
|
switch(src_type) {
|
2017-06-05 23:28:33 +03:00
|
|
|
case BX_RSIREF_B:
|
|
|
|
case BX_RDIREF_B:
|
|
|
|
disbufptr = resolve_memsize(disbufptr, i, BX_SRC_RM, BX_GPR8);
|
|
|
|
break;
|
|
|
|
case BX_RSIREF_W:
|
|
|
|
case BX_RDIREF_W:
|
|
|
|
disbufptr = resolve_memsize(disbufptr, i, BX_SRC_RM, BX_GPR16);
|
|
|
|
break;
|
|
|
|
case BX_RSIREF_D:
|
|
|
|
case BX_RDIREF_D:
|
|
|
|
disbufptr = resolve_memsize(disbufptr, i, BX_SRC_RM, BX_GPR32);
|
|
|
|
break;
|
|
|
|
case BX_RSIREF_Q:
|
|
|
|
case BX_RDIREF_Q:
|
|
|
|
disbufptr = resolve_memsize(disbufptr, i, BX_SRC_RM, BX_GPR64);
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
};
|
|
|
|
|
|
|
|
switch(src_type) {
|
|
|
|
case BX_RSIREF_B:
|
|
|
|
case BX_RSIREF_W:
|
|
|
|
case BX_RSIREF_D:
|
|
|
|
case BX_RSIREF_Q:
|
2017-01-22 22:53:42 +03:00
|
|
|
disbufptr = dis_sprintf(disbufptr, "%s:", intel_segment_name[i->seg()]);
|
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
if (i->as64L()) {
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "[%s]", intel_general_64bit_regname[BX_64BIT_REG_RSI]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (i->as32L())
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "[%s]", intel_general_32bit_regname[BX_32BIT_REG_ESI]);
|
|
|
|
else
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "[%s]", intel_general_16bit_regname[BX_16BIT_REG_SI]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-06-05 23:28:33 +03:00
|
|
|
case BX_RDIREF_B:
|
|
|
|
case BX_RDIREF_W:
|
|
|
|
case BX_RDIREF_D:
|
|
|
|
case BX_RDIREF_Q:
|
2017-01-22 22:53:42 +03:00
|
|
|
disbufptr = dis_sprintf(disbufptr, "%s:", intel_segment_name[BX_SEG_REG_ES]);
|
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
if (i->as64L()) {
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "[%s]", intel_general_64bit_regname[BX_64BIT_REG_RDI]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (i->as32L())
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "[%s]", intel_general_32bit_regname[BX_32BIT_REG_EDI]);
|
|
|
|
else
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "[%s]", intel_general_16bit_regname[BX_16BIT_REG_DI]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BX_USECL:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "cl");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BX_USEDX:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "dx");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "(unknown implicit source for disasm %d)", src_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return disbufptr;
|
|
|
|
}
|
|
|
|
|
2013-10-02 00:17:21 +04:00
|
|
|
char* disasm(char *disbufptr, const bxInstruction_c *i, bx_address cs_base, bx_address rip)
|
2013-09-30 23:01:42 +04:00
|
|
|
{
|
2013-10-02 23:23:34 +04:00
|
|
|
#if BX_SUPPORT_HANDLERS_CHAINING_SPEEDUPS
|
2013-10-01 22:47:55 +04:00
|
|
|
if (i->getIaOpcode() == BX_INSERTED_OPCODE) {
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "(bochs inserted internal opcode)");
|
|
|
|
return disbufptr;
|
|
|
|
}
|
2013-10-02 23:23:34 +04:00
|
|
|
#endif
|
2013-10-01 22:47:55 +04:00
|
|
|
|
2017-11-27 23:25:04 +03:00
|
|
|
if (i->getIaOpcode() == BX_IA_ERROR) {
|
2013-10-01 22:47:55 +04:00
|
|
|
disbufptr = dis_sprintf(disbufptr, "(invalid)");
|
|
|
|
return disbufptr;
|
2013-09-30 23:01:42 +04:00
|
|
|
}
|
|
|
|
|
2017-11-27 23:25:04 +03:00
|
|
|
#ifndef BX_STANDALONE_DECODER
|
|
|
|
if (i->execute1 == &BX_CPU_C::BxError) {
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "(invalid)");
|
|
|
|
return disbufptr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-12-03 00:06:59 +04:00
|
|
|
const char *opname = i->getIaOpcodeNameShort(); // skip the "BX_IA_"
|
2013-09-30 23:01:42 +04:00
|
|
|
unsigned n;
|
2013-10-01 22:47:55 +04:00
|
|
|
#if BX_SUPPORT_EVEX
|
|
|
|
bx_bool is_vector = BX_FALSE;
|
|
|
|
#endif
|
2013-09-30 23:01:42 +04:00
|
|
|
|
2013-10-01 22:47:55 +04:00
|
|
|
if (! strncmp(opname, "V128_", 5) || ! strncmp(opname, "V256_", 5) || ! strncmp(opname, "V512_", 5)) {
|
|
|
|
opname += 5;
|
|
|
|
#if BX_SUPPORT_EVEX
|
|
|
|
is_vector = BX_TRUE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-11-09 01:43:21 +04:00
|
|
|
// Step 1: print prefixes
|
|
|
|
if (i->lockRepUsedValue() == 1)
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "lock ");
|
|
|
|
|
2013-10-01 22:47:55 +04:00
|
|
|
if (! strncmp(opname, "REP_", 4)) {
|
2013-09-30 23:01:42 +04:00
|
|
|
opname += 4;
|
|
|
|
|
2013-10-02 23:23:34 +04:00
|
|
|
if (i->repUsedL()) {
|
2013-11-09 01:43:21 +04:00
|
|
|
if (i->lockRepUsedValue() == 2)
|
2013-10-02 23:23:34 +04:00
|
|
|
disbufptr = dis_sprintf(disbufptr, "repne ");
|
|
|
|
else
|
|
|
|
disbufptr = dis_sprintf(disbufptr, "rep ");
|
|
|
|
}
|
2013-10-01 22:47:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Step 2: print opcode name
|
2013-09-30 23:01:42 +04:00
|
|
|
unsigned opname_len = strlen(opname);
|
|
|
|
for (n=0;n < opname_len; n++) {
|
|
|
|
if (opname[n] == '_') break;
|
|
|
|
disbufptr = dis_putc(disbufptr, tolower(opname[n]));
|
|
|
|
}
|
|
|
|
|
|
|
|
disbufptr = dis_putc(disbufptr, ' ');
|
|
|
|
|
2013-10-01 22:47:55 +04:00
|
|
|
// Step 3: print sources
|
2013-09-30 23:01:42 +04:00
|
|
|
Bit16u ia_opcode = i->getIaOpcode();
|
|
|
|
unsigned srcs_used = 0;
|
|
|
|
for (n = 0; n <= 3; n++) {
|
|
|
|
unsigned src = (unsigned) BxOpcodesTable[ia_opcode].src[n];
|
2017-06-05 22:55:40 +03:00
|
|
|
unsigned src_type = BX_DISASM_SRC_TYPE(src);
|
|
|
|
unsigned src_index = BX_DISASM_SRC_ORIGIN(src);
|
2017-05-09 22:34:03 +03:00
|
|
|
if (! src_type && src_index != BX_SRC_RM && src_index != BX_SRC_EVEX_RM) continue;
|
2013-09-30 23:01:42 +04:00
|
|
|
if (srcs_used++ > 0)
|
|
|
|
disbufptr = dis_sprintf(disbufptr, ", ");
|
|
|
|
|
2014-02-11 01:12:08 +04:00
|
|
|
if (! i->modC0() && (src_index == BX_SRC_RM || src_index == BX_SRC_EVEX_RM || src_index == BX_SRC_VSIB)) {
|
2017-05-09 22:49:27 +03:00
|
|
|
disbufptr = resolve_memref(disbufptr, i, src_index, src_type);
|
2013-12-08 21:01:30 +04:00
|
|
|
#if BX_SUPPORT_EVEX
|
2013-12-09 23:09:37 +04:00
|
|
|
// EVEX.z is ignored for memory destination forms
|
2016-07-05 23:42:25 +03:00
|
|
|
if (n == 0 && (src_index == BX_SRC_EVEX_RM || src_index == BX_SRC_VSIB || src_type == BX_VMM_REG) && i->opmask()) {
|
2013-12-09 23:09:37 +04:00
|
|
|
disbufptr = dis_sprintf(disbufptr, "{k%d}", i->opmask());
|
2013-12-08 21:01:30 +04:00
|
|
|
}
|
|
|
|
#endif
|
2013-09-30 23:01:42 +04:00
|
|
|
}
|
|
|
|
else {
|
2016-07-05 23:42:25 +03:00
|
|
|
if (src_index == BX_SRC_EVEX_RM) src_type = BX_VMM_REG;
|
2017-01-22 22:53:42 +03:00
|
|
|
|
2017-06-05 23:28:33 +03:00
|
|
|
if (src_index == BX_SRC_IMM) {
|
|
|
|
// this is immediate value (including branch targets)
|
|
|
|
disbufptr = disasm_immediate(disbufptr, i, src_type, cs_base, rip);
|
|
|
|
}
|
|
|
|
else if (src_index == BX_SRC_IMPLICIT) {
|
|
|
|
// this is implicit register or memory reference
|
|
|
|
disbufptr = disasm_implicit_src(disbufptr, i, src_type);
|
2013-09-30 23:01:42 +04:00
|
|
|
}
|
|
|
|
else {
|
2017-06-05 23:28:33 +03:00
|
|
|
// this is register reference
|
|
|
|
disbufptr = disasm_regref(disbufptr, i, n, src_type);
|
2013-09-30 23:01:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-01 22:47:55 +04:00
|
|
|
|
|
|
|
#if BX_SUPPORT_EVEX
|
|
|
|
if (is_vector && i->getEvexb()) {
|
|
|
|
if (! i->modC0())
|
2013-12-03 19:44:23 +04:00
|
|
|
disbufptr = dis_sprintf(disbufptr, " {broadcast}");
|
2013-10-01 22:47:55 +04:00
|
|
|
else
|
2013-12-03 19:44:23 +04:00
|
|
|
disbufptr = dis_sprintf(disbufptr, " {sae/%s}", rounding_mode[i->getRC()]);
|
2013-10-01 22:47:55 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return disbufptr;
|
2013-09-30 23:01:42 +04:00
|
|
|
}
|
2014-01-27 00:01:50 +04:00
|
|
|
|
2016-06-13 00:23:48 +03:00
|
|
|
char* disasm(const Bit8u *opcode, bool is_32, bool is_64, char *disbufptr, bxInstruction_c *i, bx_address cs_base, bx_address rip)
|
2014-01-27 00:01:50 +04:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2014-02-17 20:19:43 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
2014-01-27 00:01:50 +04:00
|
|
|
if (is_64)
|
2017-12-02 00:18:16 +03:00
|
|
|
ret = fetchDecode64(opcode, i, 16);
|
2014-01-27 00:01:50 +04:00
|
|
|
else
|
2014-02-17 20:19:43 +04:00
|
|
|
#endif
|
2017-12-02 00:18:16 +03:00
|
|
|
ret = fetchDecode32(opcode, is_32, i, 16);
|
2014-01-27 00:01:50 +04:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
sprintf(disbufptr, "decode failed");
|
|
|
|
else
|
|
|
|
::disasm(disbufptr, i, cs_base, rip);
|
|
|
|
|
|
|
|
return disbufptr;
|
|
|
|
}
|