2014-10-25 05:11:16 +08:00
|
|
|
/**************************************************************************************************
|
|
|
|
|
|
|
|
Verteron Disassembler Engine
|
|
|
|
Version 1.0
|
|
|
|
|
|
|
|
Remarks : Freeware, Copyright must be included
|
|
|
|
|
|
|
|
Original Author : Florian Bernd
|
|
|
|
Modifications :
|
|
|
|
|
2014-10-30 06:26:17 +08:00
|
|
|
Last change : 29. October 2014
|
2014-10-25 05:11:16 +08:00
|
|
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
|
|
|
|
**************************************************************************************************/
|
2015-03-16 23:37:15 +08:00
|
|
|
#include "VXInstructionDecoder.hpp"
|
2015-02-05 05:08:16 +08:00
|
|
|
#include <cstring>
|
2014-10-25 05:11:16 +08:00
|
|
|
|
|
|
|
namespace Verteron
|
|
|
|
{
|
|
|
|
|
|
|
|
bool VXInstructionDecoder::decodeRegisterOperand(VXInstructionInfo &info, VXOperandInfo &operand,
|
|
|
|
RegisterClass registerClass, uint8_t registerId, VXDefinedOperandSize operandSize) const
|
|
|
|
{
|
|
|
|
VXRegister reg = VXRegister::NONE;
|
|
|
|
uint16_t size = getEffectiveOperandSize(info, operandSize);
|
|
|
|
switch (registerClass)
|
|
|
|
{
|
|
|
|
case RegisterClass::GENERAL_PURPOSE:
|
|
|
|
switch (size)
|
|
|
|
{
|
|
|
|
case 64:
|
|
|
|
reg = static_cast<VXRegister>(static_cast<uint16_t>(VXRegister::RAX) + registerId);
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
reg = static_cast<VXRegister>(static_cast<uint16_t>(VXRegister::EAX) + registerId);
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
reg = static_cast<VXRegister>(static_cast<uint16_t>(VXRegister::AX) + registerId);
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
// TODO: Only REX? Or VEX too?
|
|
|
|
if (m_disassemblerMode == VXDisassemblerMode::M64BIT && (info.flags & IF_PREFIX_REX))
|
|
|
|
{
|
|
|
|
if (registerId >= 4)
|
|
|
|
{
|
|
|
|
reg = static_cast<VXRegister>(
|
|
|
|
static_cast<uint16_t>(VXRegister::SPL) + (registerId - 4));
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
reg = static_cast<VXRegister>(
|
|
|
|
static_cast<uint16_t>(VXRegister::AL) + registerId);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
reg = static_cast<VXRegister>(static_cast<uint16_t>(VXRegister::AL) + registerId);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
// TODO: Error?
|
|
|
|
reg = VXRegister::NONE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RegisterClass::MMX:
|
|
|
|
reg =
|
|
|
|
static_cast<VXRegister>(static_cast<uint16_t>(VXRegister::MM0) + (registerId & 0x07));
|
|
|
|
break;
|
|
|
|
case RegisterClass::CONTROL:
|
|
|
|
reg = static_cast<VXRegister>(static_cast<uint16_t>(VXRegister::CR0) + registerId);
|
|
|
|
break;
|
|
|
|
case RegisterClass::DEBUG:
|
|
|
|
reg = static_cast<VXRegister>(static_cast<uint16_t>(VXRegister::DR0) + registerId);
|
|
|
|
break;
|
|
|
|
case RegisterClass::SEGMENT:
|
|
|
|
if ((registerId & 7) > 5)
|
|
|
|
{
|
|
|
|
info.flags |= IF_ERROR_OPERAND;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
reg = static_cast<VXRegister>(static_cast<uint16_t>(VXRegister::ES) + (registerId & 0x07));
|
|
|
|
break;
|
|
|
|
case RegisterClass::XMM:
|
|
|
|
reg = static_cast<VXRegister>(registerId + static_cast<uint16_t>(
|
|
|
|
((size == 256) ? VXRegister::YMM0 : VXRegister::XMM0)));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
operand.type = VXOperandType::REGISTER;
|
|
|
|
operand.base = static_cast<VXRegister>(reg);
|
|
|
|
operand.size = size;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VXInstructionDecoder::decodeRegisterMemoryOperand(VXInstructionInfo &info,
|
|
|
|
VXOperandInfo &operand, RegisterClass registerClass, VXDefinedOperandSize operandSize)
|
|
|
|
{
|
2014-10-27 21:10:22 +08:00
|
|
|
if (!decodeModrm(info))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-25 05:11:16 +08:00
|
|
|
assert(info.flags & IF_MODRM);
|
|
|
|
// Decode register operand
|
|
|
|
if (info.modrm_mod == 3)
|
|
|
|
{
|
2014-10-27 21:10:22 +08:00
|
|
|
return decodeRegisterOperand(info, operand, registerClass, info.modrm_rm_ext,
|
2014-10-25 05:11:16 +08:00
|
|
|
operandSize);
|
|
|
|
}
|
|
|
|
// Decode memory operand
|
|
|
|
uint8_t offset = 0;
|
|
|
|
operand.type = VXOperandType::MEMORY;
|
|
|
|
operand.size = getEffectiveOperandSize(info, operandSize);
|
2014-10-27 21:10:22 +08:00
|
|
|
switch (info.address_mode)
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
|
|
|
case 16:
|
|
|
|
{
|
|
|
|
static const VXRegister bases[] = {
|
|
|
|
VXRegister::BX, VXRegister::BX, VXRegister::BP, VXRegister::BP,
|
|
|
|
VXRegister::SI, VXRegister::DI, VXRegister::BP, VXRegister::BX };
|
|
|
|
static const VXRegister indices[] = {
|
|
|
|
VXRegister::SI, VXRegister::DI, VXRegister::SI, VXRegister::DI,
|
|
|
|
VXRegister::NONE, VXRegister::NONE, VXRegister::NONE, VXRegister::NONE };
|
2014-10-27 21:10:22 +08:00
|
|
|
operand.base = static_cast<VXRegister>(bases[info.modrm_rm_ext & 0x07]);
|
|
|
|
operand.index = static_cast<VXRegister>(indices[info.modrm_rm_ext & 0x07]);
|
2014-10-25 05:11:16 +08:00
|
|
|
operand.scale = 0;
|
2014-10-27 21:10:22 +08:00
|
|
|
if (info.modrm_mod == 0 && info.modrm_rm_ext == 6) {
|
2014-10-25 05:11:16 +08:00
|
|
|
offset = 16;
|
|
|
|
operand.base = VXRegister::NONE;
|
|
|
|
} else if (info.modrm_mod == 1) {
|
|
|
|
offset = 8;
|
|
|
|
} else if (info.modrm_mod == 2) {
|
|
|
|
offset = 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
operand.base =
|
2014-10-27 21:10:22 +08:00
|
|
|
static_cast<VXRegister>(static_cast<uint16_t>(VXRegister::EAX) + info.modrm_rm_ext);
|
2014-10-25 05:11:16 +08:00
|
|
|
switch (info.modrm_mod)
|
|
|
|
{
|
|
|
|
case 0:
|
2014-10-27 21:10:22 +08:00
|
|
|
if (info.modrm_rm_ext == 5)
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
|
|
|
operand.base = VXRegister::NONE;
|
|
|
|
offset = 32;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
offset = 8;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
offset = 32;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2014-10-27 21:10:22 +08:00
|
|
|
if ((info.modrm_rm_ext & 0x07) == 4)
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
|
|
|
if (!decodeSIB(info))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
operand.base =
|
|
|
|
static_cast<VXRegister>(static_cast<uint16_t>(VXRegister::EAX) +
|
2014-10-27 21:10:22 +08:00
|
|
|
info.sib_base_ext);
|
2014-10-25 05:11:16 +08:00
|
|
|
operand.index =
|
|
|
|
static_cast<VXRegister>(static_cast<uint16_t>(VXRegister::EAX) +
|
2014-10-27 21:10:22 +08:00
|
|
|
info.sib_index_ext);
|
2014-10-25 05:11:16 +08:00
|
|
|
operand.scale = (1 << info.sib_scale) & ~1;
|
|
|
|
if (operand.index == VXRegister::ESP)
|
|
|
|
{
|
|
|
|
operand.index = VXRegister::NONE;
|
|
|
|
operand.scale = 0;
|
|
|
|
}
|
|
|
|
if (operand.base == VXRegister::EBP)
|
|
|
|
{
|
|
|
|
if (info.modrm_mod == 0)
|
|
|
|
{
|
|
|
|
operand.base = VXRegister::NONE;
|
|
|
|
}
|
|
|
|
if (info.modrm_mod == 1)
|
|
|
|
{
|
|
|
|
offset = 8;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
offset = 32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
operand.index = VXRegister::NONE;
|
|
|
|
operand.scale = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 64:
|
|
|
|
operand.base =
|
2014-10-27 21:10:22 +08:00
|
|
|
static_cast<VXRegister>(static_cast<uint16_t>(VXRegister::RAX) + info.modrm_rm_ext);
|
2014-10-25 05:11:16 +08:00
|
|
|
switch (info.modrm_mod)
|
|
|
|
{
|
|
|
|
case 0:
|
2014-10-27 21:10:22 +08:00
|
|
|
if ((info.modrm_rm_ext & 0x07) == 5)
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
|
|
|
info.flags |= IF_RELATIVE;
|
|
|
|
operand.base = VXRegister::RIP;
|
|
|
|
offset = 32;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
offset = 8;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
offset = 32;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2014-10-27 21:10:22 +08:00
|
|
|
if ((info.modrm_rm_ext & 0x07) == 4)
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
|
|
|
if (!decodeSIB(info))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
operand.base =
|
|
|
|
static_cast<VXRegister>(static_cast<uint16_t>(VXRegister::RAX) +
|
2014-10-27 21:10:22 +08:00
|
|
|
info.sib_base_ext);
|
2014-10-25 05:11:16 +08:00
|
|
|
operand.index =
|
|
|
|
static_cast<VXRegister>(static_cast<uint16_t>(VXRegister::RAX) +
|
2014-10-27 21:10:22 +08:00
|
|
|
info.sib_index_ext);
|
2014-10-25 05:11:16 +08:00
|
|
|
if (operand.index == VXRegister::RSP)
|
|
|
|
{
|
|
|
|
operand.index = VXRegister::NONE;
|
|
|
|
operand.scale = 0;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
operand.scale = (1 << info.sib_scale) & ~1;
|
|
|
|
}
|
|
|
|
if ((operand.base == VXRegister::RBP) || (operand.base == VXRegister::R13))
|
|
|
|
{
|
|
|
|
if (info.modrm_mod == 0)
|
|
|
|
{
|
|
|
|
operand.base = VXRegister::NONE;
|
|
|
|
}
|
|
|
|
if (info.modrm_mod == 1)
|
|
|
|
{
|
|
|
|
offset = 8;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
offset = 32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
operand.index = VXRegister::NONE;
|
|
|
|
operand.scale = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (offset)
|
|
|
|
{
|
|
|
|
if (!decodeDisplacement(info, operand, offset))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
operand.offset = 0;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VXInstructionDecoder::decodeImmediate(VXInstructionInfo &info, VXOperandInfo &operand,
|
|
|
|
VXDefinedOperandSize operandSize)
|
|
|
|
{
|
|
|
|
operand.type = VXOperandType::IMMEDIATE;
|
|
|
|
operand.size = getEffectiveOperandSize(info, operandSize);
|
|
|
|
switch (operand.size)
|
|
|
|
{
|
|
|
|
case 8:
|
|
|
|
operand.lval.ubyte = inputNext(info);
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
operand.lval.uword = inputNext<uint16_t>(info);
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
operand.lval.udword = inputNext<uint32_t>(info);
|
|
|
|
break;
|
|
|
|
case 64:
|
|
|
|
operand.lval.uqword = inputNext<uint64_t>(info);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// TODO: Maybe return false instead of assert
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
if (!operand.lval.uqword && (info.flags & IF_ERROR_MASK))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VXInstructionDecoder::decodeDisplacement(VXInstructionInfo &info, VXOperandInfo &operand,
|
|
|
|
uint8_t size)
|
|
|
|
{
|
|
|
|
switch (size)
|
|
|
|
{
|
|
|
|
case 8:
|
|
|
|
operand.offset = 8;
|
|
|
|
operand.lval.ubyte = inputNext(info);
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
operand.offset = 16;
|
|
|
|
operand.lval.uword = inputNext<uint16_t>(info);
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
operand.offset = 32;
|
|
|
|
operand.lval.udword = inputNext<uint32_t>(info);
|
|
|
|
break;
|
|
|
|
case 64:
|
|
|
|
operand.offset = 64;
|
|
|
|
operand.lval.uqword = inputNext<uint64_t>(info);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// TODO: Maybe return false instead of assert
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
if (!operand.lval.uqword && (info.flags & IF_ERROR_MASK))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VXInstructionDecoder::decodeModrm(VXInstructionInfo &info)
|
|
|
|
{
|
|
|
|
if (!(info.flags & IF_MODRM))
|
|
|
|
{
|
2014-10-27 21:10:22 +08:00
|
|
|
info.modrm = inputNext(info);
|
|
|
|
if (!info.modrm && (info.flags & IF_ERROR_MASK))
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
info.flags |= IF_MODRM;
|
|
|
|
info.modrm_mod = (info.modrm >> 6) & 0x03;
|
|
|
|
info.modrm_reg = (info.modrm >> 3) & 0x07;
|
|
|
|
info.modrm_rm = (info.modrm >> 0) & 0x07;
|
|
|
|
}
|
2014-10-27 21:10:22 +08:00
|
|
|
// The @c decodeModrm method might get called multiple times during the opcode- and the
|
|
|
|
// operand decoding, but the effective REX/VEX fields are not initialized before the end of
|
|
|
|
// the opcode decoding process. As the extended values are only used for the operand decoding,
|
|
|
|
// we should have no problems.
|
|
|
|
info.modrm_reg_ext = (info.eff_rexvex_r << 3) | info.modrm_reg;
|
|
|
|
info.modrm_rm_ext = (info.eff_rexvex_b << 3) | info.modrm_rm;
|
2014-10-25 05:11:16 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VXInstructionDecoder::decodeSIB(VXInstructionInfo &info)
|
|
|
|
{
|
|
|
|
assert(info.flags & IF_MODRM);
|
|
|
|
assert((info.modrm_rm & 0x7) == 4);
|
|
|
|
if (!(info.flags & IF_SIB))
|
|
|
|
{
|
2014-10-27 21:10:22 +08:00
|
|
|
info.sib = inputNext(info);
|
|
|
|
if (!info.sib && (info.flags & IF_ERROR_MASK))
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
info.flags |= IF_SIB;
|
|
|
|
info.sib_scale = (info.sib >> 6) & 0x03;
|
|
|
|
info.sib_index = (info.sib >> 3) & 0x07;
|
|
|
|
info.sib_base = (info.sib >> 0) & 0x07;
|
2014-10-27 21:10:22 +08:00
|
|
|
// The @c decodeSib method is only called during the operand decoding, so updating the
|
|
|
|
// extended values at this point should be safe.
|
|
|
|
info.sib_index_ext = (info.eff_rexvex_x << 3) | info.sib_index;
|
|
|
|
info.sib_base_ext = (info.eff_rexvex_b << 3) | info.sib_base;
|
2014-10-25 05:11:16 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VXInstructionDecoder::decodeVex(VXInstructionInfo &info)
|
|
|
|
{
|
|
|
|
if (!(info.flags & IF_PREFIX_VEX))
|
|
|
|
{
|
|
|
|
info.vex_op = inputCurrent();
|
|
|
|
switch (info.vex_op)
|
|
|
|
{
|
|
|
|
case 0xC4:
|
2014-10-27 21:10:22 +08:00
|
|
|
info.vex_b1 = inputNext(info);
|
2014-10-25 05:11:16 +08:00
|
|
|
if (!info.vex_b1 || (info.flags & IF_ERROR_MASK))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-27 21:10:22 +08:00
|
|
|
info.vex_b2 = inputNext(info);
|
2014-10-25 05:11:16 +08:00
|
|
|
if (!info.vex_b2 || (info.flags & IF_ERROR_MASK))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
info.vex_r = (info.vex_b1 >> 7) & 0x01;
|
|
|
|
info.vex_x = (info.vex_b1 >> 6) & 0x01;
|
|
|
|
info.vex_b = (info.vex_b1 >> 5) & 0x01;
|
|
|
|
info.vex_m_mmmm = (info.vex_b1 >> 0) & 0x1F;
|
|
|
|
info.vex_w = (info.vex_b2 >> 7) & 0x01;
|
|
|
|
info.vex_vvvv = (info.vex_b2 >> 3) & 0x0F;
|
|
|
|
info.vex_l = (info.vex_b2 >> 2) & 0x01;
|
|
|
|
info.vex_pp = (info.vex_b2 >> 0) & 0x03;
|
|
|
|
break;
|
|
|
|
case 0xC5:
|
2014-10-27 21:10:22 +08:00
|
|
|
info.vex_b1 = inputNext(info);
|
2014-10-25 05:11:16 +08:00
|
|
|
if (!info.vex_b1 || (info.flags & IF_ERROR_MASK))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
info.vex_r = (info.vex_b1 >> 7) & 0x01;
|
|
|
|
info.vex_x = 1;
|
|
|
|
info.vex_b = 1;
|
|
|
|
info.vex_m_mmmm = 1;
|
|
|
|
info.vex_w = 0;
|
|
|
|
info.vex_vvvv = (info.vex_b1 >> 3) & 0x0F;
|
|
|
|
info.vex_l = (info.vex_b1 >> 2) & 0x01;
|
|
|
|
info.vex_pp = (info.vex_b1 >> 0) & 0x03;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
if (info.vex_m_mmmm > 3)
|
|
|
|
{
|
|
|
|
// TODO: Add proper error flag
|
|
|
|
info.flags |= IF_ERROR_MASK;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
info.flags |= IF_PREFIX_VEX;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t VXInstructionDecoder::getEffectiveOperandSize(const VXInstructionInfo &info,
|
|
|
|
VXDefinedOperandSize operandSize) const
|
|
|
|
{
|
|
|
|
switch (operandSize)
|
|
|
|
{
|
|
|
|
case VXDefinedOperandSize::NA:
|
|
|
|
return 0;
|
|
|
|
case VXDefinedOperandSize::Z:
|
2014-10-27 21:10:22 +08:00
|
|
|
return (info.operand_mode == 16) ? 16 : 32;
|
2014-10-25 05:11:16 +08:00
|
|
|
case VXDefinedOperandSize::V:
|
2014-10-27 21:10:22 +08:00
|
|
|
return info.operand_mode;
|
2014-10-25 05:11:16 +08:00
|
|
|
case VXDefinedOperandSize::Y:
|
2014-10-27 21:10:22 +08:00
|
|
|
return (info.operand_mode == 16) ? 32 : info.operand_mode;
|
2014-10-25 05:11:16 +08:00
|
|
|
case VXDefinedOperandSize::X:
|
|
|
|
assert(info.vex_op != 0);
|
2014-10-27 21:10:22 +08:00
|
|
|
return (info.eff_vex_l) ?
|
2014-10-25 05:11:16 +08:00
|
|
|
getEffectiveOperandSize(info, VXDefinedOperandSize::QQ) :
|
|
|
|
getEffectiveOperandSize(info, VXDefinedOperandSize::DQ);
|
|
|
|
case VXDefinedOperandSize::RDQ:
|
|
|
|
return (m_disassemblerMode == VXDisassemblerMode::M64BIT) ? 64 : 32;
|
|
|
|
default:
|
2014-10-30 23:36:25 +08:00
|
|
|
return Internal::VDEGetSimpleOperandSize(operandSize);
|
2014-10-25 05:11:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 23:36:25 +08:00
|
|
|
bool VXInstructionDecoder::decodeOperands(VXInstructionInfo &info)
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
|
|
|
assert(info.instrDefinition);
|
|
|
|
// Always try to decode the first operand
|
|
|
|
if (!decodeOperand(info, info.operand[0], info.instrDefinition->operand[0].type,
|
|
|
|
info.instrDefinition->operand[0].size))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Decode other operands on demand
|
|
|
|
for (unsigned int i = 1; i < 4; ++i)
|
|
|
|
{
|
|
|
|
if (info.operand[i - 1].type != VXOperandType::NONE)
|
|
|
|
{
|
|
|
|
if (!decodeOperand(info, info.operand[i], info.instrDefinition->operand[i].type,
|
|
|
|
info.instrDefinition->operand[i].size))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-27 21:10:22 +08:00
|
|
|
// Update operand access modes
|
2014-10-30 23:36:25 +08:00
|
|
|
for (unsigned int i = 0; i < 4; ++i)
|
2014-10-27 21:10:22 +08:00
|
|
|
{
|
2014-10-30 23:36:25 +08:00
|
|
|
if (info.operand[i].type != VXOperandType::NONE)
|
2014-10-27 21:10:22 +08:00
|
|
|
{
|
2014-10-30 23:36:25 +08:00
|
|
|
info.operand[i].access_mode = VXOperandAccessMode::READ;
|
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
if (info.instrDefinition->flags & IDF_OPERAND1_WRITE)
|
|
|
|
{
|
|
|
|
info.operand[0].access_mode = VXOperandAccessMode::WRITE;
|
|
|
|
} else if (info.instrDefinition->flags & IDF_OPERAND1_READWRITE)
|
|
|
|
{
|
|
|
|
info.operand[0].access_mode = VXOperandAccessMode::READWRITE;
|
|
|
|
}
|
|
|
|
} else if (i == 1)
|
|
|
|
{
|
|
|
|
if (info.instrDefinition->flags & IDF_OPERAND2_WRITE)
|
|
|
|
{
|
|
|
|
info.operand[1].access_mode = VXOperandAccessMode::WRITE;
|
|
|
|
} else if (info.instrDefinition->flags & IDF_OPERAND2_READWRITE)
|
|
|
|
{
|
|
|
|
info.operand[1].access_mode = VXOperandAccessMode::READWRITE;
|
|
|
|
}
|
|
|
|
}
|
2014-10-27 21:10:22 +08:00
|
|
|
}
|
|
|
|
}
|
2014-10-25 05:11:16 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VXInstructionDecoder::decodeOperand(VXInstructionInfo &info, VXOperandInfo &operand,
|
|
|
|
VXDefinedOperandType operandType, VXDefinedOperandSize operandSize)
|
|
|
|
{
|
|
|
|
using namespace Internal;
|
|
|
|
operand.type = VXOperandType::NONE;
|
|
|
|
switch (operandType)
|
|
|
|
{
|
|
|
|
case VXDefinedOperandType::NONE:
|
|
|
|
break;
|
|
|
|
case VXDefinedOperandType::A:
|
|
|
|
operand.type = VXOperandType::POINTER;
|
2014-10-27 21:10:22 +08:00
|
|
|
if (info.operand_mode == 16)
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
|
|
|
operand.size = 32;
|
|
|
|
operand.lval.ptr.off = inputNext<uint16_t>(info);
|
|
|
|
operand.lval.ptr.seg = inputNext<uint16_t>(info);
|
|
|
|
} else {
|
|
|
|
operand.size = 48;
|
|
|
|
operand.lval.ptr.off = inputNext<uint32_t>(info);
|
|
|
|
operand.lval.ptr.seg = inputNext<uint16_t>(info);
|
|
|
|
}
|
|
|
|
if ((!operand.lval.ptr.off || !operand.lval.ptr.seg) && (info.flags & IF_ERROR_MASK))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VXDefinedOperandType::C:
|
2014-10-27 21:10:22 +08:00
|
|
|
if (!decodeModrm(info))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return decodeRegisterOperand(info, operand, RegisterClass::CONTROL, info.modrm_reg_ext,
|
2014-10-25 05:11:16 +08:00
|
|
|
operandSize);
|
|
|
|
case VXDefinedOperandType::D:
|
2014-10-27 21:10:22 +08:00
|
|
|
if (!decodeModrm(info))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return decodeRegisterOperand(info, operand, RegisterClass::DEBUG, info.modrm_reg_ext,
|
2014-10-25 05:11:16 +08:00
|
|
|
operandSize);
|
|
|
|
case VXDefinedOperandType::F:
|
|
|
|
// TODO: FAR flag
|
|
|
|
case VXDefinedOperandType::M:
|
|
|
|
// ModR/M byte may refer only to a register
|
|
|
|
if (info.modrm_mod == 3)
|
|
|
|
{
|
|
|
|
info.flags |= IF_ERROR_OPERAND;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
case VXDefinedOperandType::E:
|
|
|
|
return decodeRegisterMemoryOperand(info, operand, RegisterClass::GENERAL_PURPOSE,
|
|
|
|
operandSize);
|
|
|
|
case VXDefinedOperandType::G:
|
2014-10-27 21:10:22 +08:00
|
|
|
if (!decodeModrm(info))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-25 05:11:16 +08:00
|
|
|
return decodeRegisterOperand(info, operand, RegisterClass::GENERAL_PURPOSE,
|
2014-10-27 21:10:22 +08:00
|
|
|
info.modrm_reg_ext, operandSize);
|
2014-10-25 05:11:16 +08:00
|
|
|
case VXDefinedOperandType::H:
|
|
|
|
assert(info.vex_op != 0);
|
|
|
|
return decodeRegisterOperand(info, operand, RegisterClass::XMM, (0xF & ~info.vex_vvvv),
|
|
|
|
operandSize);
|
2014-10-27 21:10:22 +08:00
|
|
|
case VXDefinedOperandType::sI:
|
|
|
|
operand.signed_lval = true;
|
2014-10-25 05:11:16 +08:00
|
|
|
case VXDefinedOperandType::I:
|
|
|
|
return decodeImmediate(info, operand, operandSize);
|
|
|
|
case VXDefinedOperandType::I1:
|
|
|
|
operand.type = VXOperandType::CONSTANT;
|
|
|
|
operand.lval.udword = 1;
|
|
|
|
break;
|
|
|
|
case VXDefinedOperandType::J:
|
|
|
|
if (!decodeImmediate(info, operand, operandSize))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
operand.type = VXOperandType::REL_IMMEDIATE;
|
2014-10-30 06:26:17 +08:00
|
|
|
operand.signed_lval = true;
|
2014-10-25 05:11:16 +08:00
|
|
|
info.flags |= IF_RELATIVE;
|
|
|
|
break;
|
|
|
|
case VXDefinedOperandType::L:
|
|
|
|
{
|
|
|
|
assert(info.vex_op != 0);
|
|
|
|
uint8_t imm = inputNext(info);
|
|
|
|
if (!imm && (info.flags & IF_ERROR_MASK))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
uint8_t mask = (m_disassemblerMode == VXDisassemblerMode::M64BIT) ? 0xF : 0x7;
|
|
|
|
return decodeRegisterOperand(info, operand, RegisterClass::XMM, mask & (imm >> 4),
|
|
|
|
operandSize);
|
|
|
|
}
|
|
|
|
case VXDefinedOperandType::MR:
|
|
|
|
return decodeRegisterMemoryOperand(info, operand, RegisterClass::GENERAL_PURPOSE,
|
|
|
|
info.modrm_mod == 3 ?
|
2014-10-30 23:36:25 +08:00
|
|
|
VDEGetComplexOperandRegSize(operandSize) : VDEGetComplexOperandMemSize(operandSize));
|
2014-10-25 05:11:16 +08:00
|
|
|
case VXDefinedOperandType::MU:
|
|
|
|
return decodeRegisterMemoryOperand(info, operand, RegisterClass::XMM,
|
|
|
|
info.modrm_mod == 3 ?
|
2014-10-30 23:36:25 +08:00
|
|
|
VDEGetComplexOperandRegSize(operandSize) : VDEGetComplexOperandMemSize(operandSize));
|
2014-10-25 05:11:16 +08:00
|
|
|
case VXDefinedOperandType::N:
|
|
|
|
// ModR/M byte may refer only to memory
|
|
|
|
if (info.modrm_mod != 3)
|
|
|
|
{
|
|
|
|
info.flags |= IF_ERROR_OPERAND;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
case VXDefinedOperandType::Q:
|
|
|
|
return decodeRegisterMemoryOperand(info, operand, RegisterClass::MMX, operandSize);
|
|
|
|
case VXDefinedOperandType::O:
|
|
|
|
operand.type = VXOperandType::MEMORY;
|
|
|
|
operand.base = VXRegister::NONE;
|
|
|
|
operand.index = VXRegister::NONE;
|
|
|
|
operand.scale = 0;
|
|
|
|
operand.size = getEffectiveOperandSize(info, operandSize);
|
2014-10-27 21:10:22 +08:00
|
|
|
return decodeDisplacement(info, operand, info.address_mode);
|
2014-10-25 05:11:16 +08:00
|
|
|
case VXDefinedOperandType::P:
|
2014-10-27 21:10:22 +08:00
|
|
|
if (!decodeModrm(info))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return decodeRegisterOperand(info, operand, RegisterClass::MMX, info.modrm_reg_ext,
|
2014-10-25 05:11:16 +08:00
|
|
|
operandSize);
|
|
|
|
case VXDefinedOperandType::R:
|
|
|
|
// ModR/M byte may refer only to memory
|
|
|
|
if (info.modrm_mod != 3)
|
|
|
|
{
|
|
|
|
info.flags |= IF_ERROR_OPERAND;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return decodeRegisterMemoryOperand(info, operand, RegisterClass::GENERAL_PURPOSE,
|
|
|
|
operandSize);
|
|
|
|
case VXDefinedOperandType::S:
|
2014-10-27 21:10:22 +08:00
|
|
|
if (!decodeModrm(info))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return decodeRegisterOperand(info, operand, RegisterClass::SEGMENT, info.modrm_reg_ext,
|
2014-10-25 05:11:16 +08:00
|
|
|
operandSize);
|
|
|
|
case VXDefinedOperandType::U:
|
|
|
|
// ModR/M byte may refer only to memory
|
|
|
|
if (info.modrm_mod != 3)
|
|
|
|
{
|
|
|
|
info.flags |= IF_ERROR_OPERAND;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
case VXDefinedOperandType::W:
|
|
|
|
return decodeRegisterMemoryOperand(info, operand, RegisterClass::XMM, operandSize);
|
|
|
|
case VXDefinedOperandType::V:
|
2014-10-27 21:10:22 +08:00
|
|
|
if (!decodeModrm(info))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return decodeRegisterOperand(info, operand, RegisterClass::XMM, info.modrm_reg_ext,
|
2014-10-25 05:11:16 +08:00
|
|
|
operandSize);
|
|
|
|
case VXDefinedOperandType::R0:
|
|
|
|
case VXDefinedOperandType::R1:
|
|
|
|
case VXDefinedOperandType::R2:
|
|
|
|
case VXDefinedOperandType::R3:
|
|
|
|
case VXDefinedOperandType::R4:
|
|
|
|
case VXDefinedOperandType::R5:
|
|
|
|
case VXDefinedOperandType::R6:
|
|
|
|
case VXDefinedOperandType::R7:
|
|
|
|
return decodeRegisterOperand(info, operand, RegisterClass::GENERAL_PURPOSE,
|
2014-10-27 21:10:22 +08:00
|
|
|
((info.eff_rexvex_b << 3) | (static_cast<uint16_t>(operandType) -
|
2014-10-25 05:11:16 +08:00
|
|
|
static_cast<uint16_t>(VXDefinedOperandType::R0))), operandSize);
|
|
|
|
case VXDefinedOperandType::AL:
|
|
|
|
case VXDefinedOperandType::AX:
|
|
|
|
case VXDefinedOperandType::EAX:
|
|
|
|
case VXDefinedOperandType::RAX:
|
|
|
|
return decodeRegisterOperand(info, operand, RegisterClass::GENERAL_PURPOSE, 0,
|
|
|
|
operandSize);
|
|
|
|
case VXDefinedOperandType::CL:
|
|
|
|
case VXDefinedOperandType::CX:
|
|
|
|
case VXDefinedOperandType::ECX:
|
|
|
|
case VXDefinedOperandType::RCX:
|
|
|
|
return decodeRegisterOperand(info, operand, RegisterClass::GENERAL_PURPOSE, 1,
|
|
|
|
operandSize);
|
|
|
|
case VXDefinedOperandType::DL:
|
|
|
|
case VXDefinedOperandType::DX:
|
|
|
|
case VXDefinedOperandType::EDX:
|
|
|
|
case VXDefinedOperandType::RDX:
|
|
|
|
return decodeRegisterOperand(info, operand, RegisterClass::GENERAL_PURPOSE, 2,
|
|
|
|
operandSize);
|
|
|
|
case VXDefinedOperandType::ES:
|
|
|
|
case VXDefinedOperandType::CS:
|
|
|
|
case VXDefinedOperandType::SS:
|
|
|
|
case VXDefinedOperandType::DS:
|
|
|
|
case VXDefinedOperandType::FS:
|
|
|
|
case VXDefinedOperandType::GS:
|
|
|
|
if (m_disassemblerMode == VXDisassemblerMode::M64BIT)
|
|
|
|
{
|
|
|
|
if ((operandType != VXDefinedOperandType::FS) &&
|
|
|
|
(operandType != VXDefinedOperandType::GS))
|
|
|
|
{
|
|
|
|
info.flags |= IF_ERROR_OPERAND;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
operand.type = VXOperandType::REGISTER;
|
|
|
|
operand.base = static_cast<VXRegister>((static_cast<uint16_t>(operandType) -
|
|
|
|
static_cast<uint16_t>(VXDefinedOperandType::ES)) +
|
|
|
|
static_cast<uint16_t>(VXRegister::ES));
|
|
|
|
operand.size = 16;
|
|
|
|
break;
|
|
|
|
case VXDefinedOperandType::ST0:
|
|
|
|
case VXDefinedOperandType::ST1:
|
|
|
|
case VXDefinedOperandType::ST2:
|
|
|
|
case VXDefinedOperandType::ST3:
|
|
|
|
case VXDefinedOperandType::ST4:
|
|
|
|
case VXDefinedOperandType::ST5:
|
|
|
|
case VXDefinedOperandType::ST6:
|
|
|
|
case VXDefinedOperandType::ST7:
|
|
|
|
operand.type = VXOperandType::REGISTER;
|
|
|
|
operand.base = static_cast<VXRegister>((static_cast<uint16_t>(operandType) -
|
|
|
|
static_cast<uint16_t>(VXDefinedOperandType::ST0)) +
|
|
|
|
static_cast<uint16_t>(VXRegister::ST0));
|
|
|
|
operand.size = 80;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VXInstructionDecoder::resolveOperandAndAddressMode(VXInstructionInfo &info) const
|
|
|
|
{
|
|
|
|
assert(info.instrDefinition);
|
|
|
|
switch (m_disassemblerMode)
|
|
|
|
{
|
|
|
|
case VXDisassemblerMode::M16BIT:
|
2014-10-27 21:10:22 +08:00
|
|
|
info.operand_mode = (info.flags & IF_PREFIX_OPERAND_SIZE) ? 32 : 16;
|
|
|
|
info.address_mode = (info.flags & IF_PREFIX_ADDRESS_SIZE) ? 32 : 16;
|
2014-10-25 05:11:16 +08:00
|
|
|
break;
|
|
|
|
case VXDisassemblerMode::M32BIT:
|
2014-10-27 21:10:22 +08:00
|
|
|
info.operand_mode = (info.flags & IF_PREFIX_OPERAND_SIZE) ? 16 : 32;
|
|
|
|
info.address_mode = (info.flags & IF_PREFIX_ADDRESS_SIZE) ? 16 : 32;
|
2014-10-25 05:11:16 +08:00
|
|
|
break;
|
|
|
|
case VXDisassemblerMode::M64BIT:
|
2014-10-27 21:10:22 +08:00
|
|
|
if (info.eff_rexvex_w)
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
2014-10-27 21:10:22 +08:00
|
|
|
info.operand_mode = 64;
|
|
|
|
} else if ((info.flags & IF_PREFIX_OPERAND_SIZE))
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
2014-10-27 21:10:22 +08:00
|
|
|
info.operand_mode = 16;
|
2014-10-25 05:11:16 +08:00
|
|
|
} else
|
|
|
|
{
|
2014-10-27 21:10:22 +08:00
|
|
|
info.operand_mode = (info.instrDefinition->flags & IDF_DEFAULT_64) ? 64 : 32;
|
2014-10-25 05:11:16 +08:00
|
|
|
}
|
2014-10-27 21:10:22 +08:00
|
|
|
info.address_mode = (info.flags & IF_PREFIX_ADDRESS_SIZE) ? 32 : 64;
|
2014-10-25 05:11:16 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-27 21:10:22 +08:00
|
|
|
void VXInstructionDecoder::calculateEffectiveRexVexValues(VXInstructionInfo &info) const
|
|
|
|
{
|
|
|
|
assert(info.instrDefinition);
|
|
|
|
uint8_t rex = info.rex;
|
|
|
|
if (info.flags & IF_PREFIX_VEX)
|
|
|
|
{
|
|
|
|
switch (info.vex_op)
|
|
|
|
{
|
|
|
|
case 0xC4:
|
|
|
|
rex = ((~(info.vex_b1 >> 5) & 0x07) | ((info.vex_b2 >> 4) & 0x08));
|
|
|
|
break;
|
|
|
|
case 0xC5:
|
|
|
|
rex = (~(info.vex_b1 >> 5)) & 4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rex &= (info.instrDefinition->flags & 0x000F);
|
|
|
|
info.eff_rexvex_w = (rex >> 3) & 0x01;
|
|
|
|
info.eff_rexvex_r = (rex >> 2) & 0x01;
|
|
|
|
info.eff_rexvex_x = (rex >> 1) & 0x01;
|
|
|
|
info.eff_rexvex_b = (rex >> 0) & 0x01;
|
|
|
|
info.eff_vex_l = info.vex_l && (info.instrDefinition->flags & IDF_ACCEPTS_VEXL);
|
|
|
|
}
|
|
|
|
|
2014-10-25 05:11:16 +08:00
|
|
|
bool VXInstructionDecoder::decodePrefixes(VXInstructionInfo &info)
|
|
|
|
{
|
|
|
|
bool done = false;
|
|
|
|
do
|
|
|
|
{
|
2014-10-27 21:10:22 +08:00
|
|
|
switch (inputPeek(info))
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
|
|
|
case 0xF0:
|
|
|
|
info.flags |= IF_PREFIX_LOCK;
|
|
|
|
break;
|
|
|
|
case 0xF2:
|
|
|
|
// REPNZ and REPZ are mutally exclusive. The one that comes later has precedence.
|
2014-10-27 21:10:22 +08:00
|
|
|
info.flags |= IF_PREFIX_REP;
|
|
|
|
info.flags &= ~IF_PREFIX_REPNE;
|
2014-10-25 05:11:16 +08:00
|
|
|
break;
|
|
|
|
case 0xF3:
|
|
|
|
// REPNZ and REPZ are mutally exclusive. The one that comes later has precedence.
|
2014-10-27 21:10:22 +08:00
|
|
|
info.flags |= IF_PREFIX_REP;
|
|
|
|
info.flags &= ~IF_PREFIX_REPNE;
|
2014-10-25 05:11:16 +08:00
|
|
|
break;
|
|
|
|
case 0x2E:
|
|
|
|
info.flags |= IF_PREFIX_SEGMENT;
|
2014-10-27 21:10:22 +08:00
|
|
|
info.segment = VXRegister::CS;
|
2014-10-25 05:11:16 +08:00
|
|
|
break;
|
|
|
|
case 0x36:
|
|
|
|
info.flags |= IF_PREFIX_SEGMENT;
|
2014-10-27 21:10:22 +08:00
|
|
|
info.segment = VXRegister::SS;
|
2014-10-25 05:11:16 +08:00
|
|
|
break;
|
|
|
|
case 0x3E:
|
|
|
|
info.flags |= IF_PREFIX_SEGMENT;
|
2014-10-27 21:10:22 +08:00
|
|
|
info.segment = VXRegister::DS;
|
2014-10-25 05:11:16 +08:00
|
|
|
break;
|
|
|
|
case 0x26:
|
|
|
|
info.flags |= IF_PREFIX_SEGMENT;
|
2014-10-27 21:10:22 +08:00
|
|
|
info.segment = VXRegister::ES;
|
2014-10-25 05:11:16 +08:00
|
|
|
break;
|
|
|
|
case 0x64:
|
|
|
|
info.flags |= IF_PREFIX_SEGMENT;
|
2014-10-27 21:10:22 +08:00
|
|
|
info.segment = VXRegister::FS;
|
2014-10-25 05:11:16 +08:00
|
|
|
break;
|
|
|
|
case 0x65:
|
|
|
|
info.flags |= IF_PREFIX_SEGMENT;
|
2014-10-27 21:10:22 +08:00
|
|
|
info.segment = VXRegister::GS;
|
2014-10-25 05:11:16 +08:00
|
|
|
break;
|
|
|
|
case 0x66:
|
2014-10-27 21:10:22 +08:00
|
|
|
info.flags |= IF_PREFIX_OPERAND_SIZE;
|
2014-10-25 05:11:16 +08:00
|
|
|
break;
|
|
|
|
case 0x67:
|
2014-10-27 21:10:22 +08:00
|
|
|
info.flags |= IF_PREFIX_ADDRESS_SIZE;
|
2014-10-25 05:11:16 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if ((m_disassemblerMode == VXDisassemblerMode::M64BIT) &&
|
|
|
|
(inputCurrent() & 0xF0) == 0x40)
|
|
|
|
{
|
|
|
|
info.flags |= IF_PREFIX_REX;
|
|
|
|
info.rex = inputCurrent();
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Increase the input offset, if a prefix was found
|
|
|
|
if (!done)
|
|
|
|
{
|
|
|
|
if (!inputNext(info) && (info.flags & IF_ERROR_MASK))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (!done);
|
2014-10-27 21:10:22 +08:00
|
|
|
// TODO: Check for multiple prefixes of the same group
|
2014-10-25 05:11:16 +08:00
|
|
|
// Parse REX Prefix
|
|
|
|
if (info.flags & IF_PREFIX_REX)
|
|
|
|
{
|
|
|
|
info.rex_w = (info.rex >> 3) & 0x01;
|
|
|
|
info.rex_r = (info.rex >> 2) & 0x01;
|
|
|
|
info.rex_x = (info.rex >> 1) & 0x01;
|
|
|
|
info.rex_b = (info.rex >> 0) & 0x01;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VXInstructionDecoder::decodeOpcode(VXInstructionInfo &info)
|
|
|
|
{
|
|
|
|
using namespace Internal;
|
|
|
|
// Read first opcode byte
|
|
|
|
if (!inputNext(info) && (info.flags & IF_ERROR_MASK))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Update instruction info
|
|
|
|
info.opcode[0] = inputCurrent();
|
2014-10-27 21:10:22 +08:00
|
|
|
info.opcode_length = 1;
|
2014-10-25 05:11:16 +08:00
|
|
|
// Iterate through opcode tree
|
2014-10-30 23:36:25 +08:00
|
|
|
VXOpcodeTreeNode node = VDEGetOpcodeTreeChild(VDEGetOpcodeTreeRoot(), inputCurrent());
|
2014-10-25 05:11:16 +08:00
|
|
|
VXOpcodeTreeNodeType nodeType;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
uint16_t index = 0;
|
2014-10-30 23:36:25 +08:00
|
|
|
nodeType = VDEGetOpcodeNodeType(node);
|
2014-10-25 05:11:16 +08:00
|
|
|
switch (nodeType)
|
|
|
|
{
|
|
|
|
case VXOpcodeTreeNodeType::INSTRUCTION_DEFINITION:
|
|
|
|
{
|
2014-10-27 21:10:22 +08:00
|
|
|
// Check for invalid instruction
|
2014-10-30 23:36:25 +08:00
|
|
|
if (VDEGetOpcodeNodeValue(node) == 0)
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
2014-10-27 21:10:22 +08:00
|
|
|
info.flags |= IF_ERROR_INVALID;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Get instruction definition
|
2014-10-30 23:36:25 +08:00
|
|
|
const VXInstructionDefinition *instrDefinition = VDEGetInstructionDefinition(node);
|
2014-10-27 21:10:22 +08:00
|
|
|
// Check for invalid 64 bit instruction
|
|
|
|
if ((m_disassemblerMode == VXDisassemblerMode::M64BIT) &&
|
|
|
|
(instrDefinition->flags & IDF_INVALID_64))
|
|
|
|
{
|
|
|
|
info.flags |= IF_ERROR_INVALID_64;
|
2014-10-25 05:11:16 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-10-27 21:10:22 +08:00
|
|
|
// Update instruction info
|
|
|
|
info.instrDefinition = instrDefinition;
|
|
|
|
info.mnemonic = instrDefinition->mnemonic;
|
|
|
|
// Update effective REX/VEX values
|
|
|
|
calculateEffectiveRexVexValues(info);
|
|
|
|
// Resolve operand and address mode
|
|
|
|
resolveOperandAndAddressMode(info);
|
2014-10-25 05:11:16 +08:00
|
|
|
// Decode operands
|
|
|
|
if (!decodeOperands(info))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case VXOpcodeTreeNodeType::TABLE:
|
|
|
|
// Read next opcode byte
|
|
|
|
if (!inputNext(info) && (info.flags & IF_ERROR_MASK))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Update instruction info
|
2014-10-27 21:10:22 +08:00
|
|
|
assert((info.opcode_length > 0) && (info.opcode_length < 3));
|
|
|
|
info.opcode[info.opcode_length] = inputCurrent();
|
|
|
|
info.opcode_length++;
|
2014-10-25 05:11:16 +08:00
|
|
|
// Set child node index for next iteration
|
|
|
|
index = inputCurrent();
|
|
|
|
break;
|
|
|
|
case VXOpcodeTreeNodeType::MODRM_MOD:
|
|
|
|
// Decode modrm byte
|
|
|
|
if (!decodeModrm(info))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
index = (info.modrm_mod == 0x3) ? 1 : 0;
|
|
|
|
break;
|
|
|
|
case VXOpcodeTreeNodeType::MODRM_REG:
|
|
|
|
// Decode modrm byte
|
|
|
|
if (!decodeModrm(info))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
index = info.modrm_reg;
|
|
|
|
break;
|
|
|
|
case VXOpcodeTreeNodeType::MODRM_RM:
|
|
|
|
// Decode modrm byte
|
|
|
|
if (!decodeModrm(info))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
index = info.modrm_rm;
|
|
|
|
break;
|
|
|
|
case VXOpcodeTreeNodeType::MANDATORY:
|
|
|
|
// Check if there are any prefixes present
|
2014-10-27 21:10:22 +08:00
|
|
|
if (info.flags & IF_PREFIX_REP)
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
|
|
|
index = 1; // F2
|
2014-10-27 21:10:22 +08:00
|
|
|
} else if (info.flags & IF_PREFIX_REPNE)
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
|
|
|
index = 2; // F3
|
2014-10-27 21:10:22 +08:00
|
|
|
} else if (info.flags & IF_PREFIX_OPERAND_SIZE)
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
|
|
|
index = 3; // 66
|
|
|
|
}
|
2014-10-30 23:36:25 +08:00
|
|
|
if (VDEGetOpcodeTreeChild(node, index) == 0)
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
|
|
|
index = 0;
|
|
|
|
}
|
2014-10-30 23:36:25 +08:00
|
|
|
if (index && (VDEGetOpcodeTreeChild(node, index) != 0))
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
2014-10-27 21:10:22 +08:00
|
|
|
// Remove REP and REPNE prefix
|
|
|
|
info.flags &= ~IF_PREFIX_REP;
|
|
|
|
info.flags &= ~IF_PREFIX_REPNE;
|
|
|
|
// Remove OPERAND_SIZE prefix, if it was used as mandatory prefix for the
|
|
|
|
// instruction
|
2014-10-25 05:11:16 +08:00
|
|
|
if (index == 3)
|
|
|
|
{
|
2014-10-27 21:10:22 +08:00
|
|
|
info.flags &= ~IF_PREFIX_OPERAND_SIZE;
|
2014-10-25 05:11:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VXOpcodeTreeNodeType::X87:
|
|
|
|
// Decode modrm byte
|
|
|
|
if (!decodeModrm(info))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
index = info.modrm - 0xC0;
|
|
|
|
break;
|
|
|
|
case VXOpcodeTreeNodeType::ADDRESS_SIZE:
|
|
|
|
switch (m_disassemblerMode)
|
|
|
|
{
|
|
|
|
case VXDisassemblerMode::M16BIT:
|
2014-10-27 21:10:22 +08:00
|
|
|
index = (info.flags & IF_PREFIX_ADDRESS_SIZE) ? 1 : 0;
|
2014-10-25 05:11:16 +08:00
|
|
|
break;
|
|
|
|
case VXDisassemblerMode::M32BIT:
|
2014-10-27 21:10:22 +08:00
|
|
|
index = (info.flags & IF_PREFIX_ADDRESS_SIZE) ? 0 : 1;
|
2014-10-25 05:11:16 +08:00
|
|
|
break;
|
|
|
|
case VXDisassemblerMode::M64BIT:
|
2014-10-27 21:10:22 +08:00
|
|
|
index = (info.flags & IF_PREFIX_ADDRESS_SIZE) ? 1 : 2;
|
2014-10-25 05:11:16 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VXOpcodeTreeNodeType::OPERAND_SIZE:
|
|
|
|
switch (m_disassemblerMode)
|
|
|
|
{
|
|
|
|
case VXDisassemblerMode::M16BIT:
|
2014-10-27 21:10:22 +08:00
|
|
|
index = (info.flags & IF_PREFIX_OPERAND_SIZE) ? 1 : 0;
|
2014-10-25 05:11:16 +08:00
|
|
|
break;
|
|
|
|
case VXDisassemblerMode::M32BIT:
|
2014-10-27 21:10:22 +08:00
|
|
|
index = (info.flags & IF_PREFIX_OPERAND_SIZE) ? 0 : 1;
|
2014-10-25 05:11:16 +08:00
|
|
|
break;
|
|
|
|
case VXDisassemblerMode::M64BIT:
|
2014-10-27 21:10:22 +08:00
|
|
|
index = (info.rex_w) ? 2 : ((info.flags & IF_PREFIX_OPERAND_SIZE) ? 0 : 1);
|
2014-10-25 05:11:16 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VXOpcodeTreeNodeType::MODE:
|
|
|
|
index = (m_disassemblerMode != VXDisassemblerMode::M64BIT) ? 0 : 1;
|
|
|
|
break;
|
|
|
|
case VXOpcodeTreeNodeType::VENDOR:
|
|
|
|
switch (m_preferredVendor)
|
|
|
|
{
|
|
|
|
case VXInstructionSetVendor::ANY:
|
2014-10-30 23:36:25 +08:00
|
|
|
index = (VDEGetOpcodeTreeChild(node, 0) != 0) ? 0 : 1;
|
2014-10-25 05:11:16 +08:00
|
|
|
break;
|
|
|
|
case VXInstructionSetVendor::INTEL:
|
|
|
|
index = 1;
|
|
|
|
break;
|
|
|
|
case VXInstructionSetVendor::AMD:
|
|
|
|
index = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VXOpcodeTreeNodeType::AMD3DNOW:
|
|
|
|
{
|
|
|
|
// As all 3dnow instructions got the same operands and flag definitions, we just
|
|
|
|
// decode a random instruction and determine the specific opcode later.
|
2014-10-30 23:36:25 +08:00
|
|
|
assert(VDEGetOpcodeTreeChild(node, 0x0C) != 0);
|
2014-10-27 21:10:22 +08:00
|
|
|
const VXInstructionDefinition *instrDefinition =
|
2014-10-30 23:36:25 +08:00
|
|
|
VDEGetInstructionDefinition(VDEGetOpcodeTreeChild(node, 0x0C));
|
2014-10-27 21:10:22 +08:00
|
|
|
// Update instruction info
|
|
|
|
info.instrDefinition = instrDefinition;
|
|
|
|
info.mnemonic = instrDefinition->mnemonic;
|
|
|
|
// Update effective REX/VEX values
|
|
|
|
calculateEffectiveRexVexValues(info);
|
|
|
|
// Resolve operand and address mode
|
|
|
|
resolveOperandAndAddressMode(info);
|
2014-10-25 05:11:16 +08:00
|
|
|
// Decode operands
|
|
|
|
if (!decodeOperands(info))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Read the actual 3dnow opcode
|
|
|
|
info.opcode[2] = inputNext(info);
|
|
|
|
if (!info.opcode[2] && (info.flags & IF_ERROR_MASK))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-27 21:10:22 +08:00
|
|
|
// Update instruction info
|
|
|
|
instrDefinition =
|
2014-10-30 23:36:25 +08:00
|
|
|
VDEGetInstructionDefinition(VDEGetOpcodeTreeChild(node, info.opcode[2]));
|
2014-10-27 21:10:22 +08:00
|
|
|
if (!instrDefinition ||
|
|
|
|
(instrDefinition->mnemonic == VXInstructionMnemonic::INVALID))
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
|
|
|
info.flags |= IF_ERROR_INVALID;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
info.instrDefinition = instrDefinition;
|
2014-10-27 21:10:22 +08:00
|
|
|
info.mnemonic = instrDefinition->mnemonic;
|
|
|
|
// Update operand access modes
|
|
|
|
for (unsigned int i = 0; i < 4; ++i)
|
|
|
|
{
|
|
|
|
if (info.operand[i].type != VXOperandType::NONE)
|
|
|
|
{
|
|
|
|
info.operand[i - 1].access_mode = VXOperandAccessMode::READ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (info.operand[0].type != VXOperandType::NONE)
|
|
|
|
{
|
|
|
|
if (info.instrDefinition->flags & IDF_OPERAND1_WRITE)
|
|
|
|
{
|
|
|
|
info.operand[0].access_mode = VXOperandAccessMode::WRITE;
|
|
|
|
} else if (info.instrDefinition->flags & IDF_OPERAND1_READWRITE)
|
|
|
|
{
|
|
|
|
info.operand[0].access_mode = VXOperandAccessMode::READWRITE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (info.operand[1].type != VXOperandType::NONE)
|
|
|
|
{
|
|
|
|
if (info.instrDefinition->flags & IDF_OPERAND2_WRITE)
|
|
|
|
{
|
|
|
|
info.operand[1].access_mode = VXOperandAccessMode::WRITE;
|
|
|
|
} else if (info.instrDefinition->flags & IDF_OPERAND2_READWRITE)
|
|
|
|
{
|
|
|
|
info.operand[1].access_mode = VXOperandAccessMode::READWRITE;
|
|
|
|
}
|
|
|
|
}
|
2014-10-25 05:11:16 +08:00
|
|
|
// Terminate loop
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case VXOpcodeTreeNodeType::VEX:
|
|
|
|
if ((m_disassemblerMode == VXDisassemblerMode::M64BIT) ||
|
|
|
|
(((inputCurrent() >> 6) & 0x03) == 0x03))
|
|
|
|
{
|
|
|
|
// Decode vex prefix
|
|
|
|
if (!decodeVex(info))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-27 21:10:22 +08:00
|
|
|
// Update instruction info (error cases are checked by the @c decodeVex method)
|
2014-10-25 05:11:16 +08:00
|
|
|
switch (info.vex_m_mmmm)
|
|
|
|
{
|
|
|
|
case 1:
|
2014-10-27 21:10:22 +08:00
|
|
|
info.opcode_length = 1;
|
2014-10-25 05:11:16 +08:00
|
|
|
info.opcode[0] = 0x0F;
|
|
|
|
break;
|
|
|
|
case 2:
|
2014-10-27 21:10:22 +08:00
|
|
|
info.opcode_length = 2;
|
2014-10-25 05:11:16 +08:00
|
|
|
info.opcode[0] = 0x0F;
|
|
|
|
info.opcode[1] = 0x38;
|
|
|
|
break;
|
|
|
|
case 3:
|
2014-10-27 21:10:22 +08:00
|
|
|
info.opcode_length = 2;
|
2014-10-25 05:11:16 +08:00
|
|
|
info.opcode[0] = 0x0F;
|
|
|
|
info.opcode[1] = 0x3A;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Set child node index for next iteration
|
|
|
|
index = info.vex_m_mmmm + (info.vex_pp << 2);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
index = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VXOpcodeTreeNodeType::VEXW:
|
|
|
|
assert(info.flags & IF_PREFIX_VEX);
|
|
|
|
index = info.vex_w;
|
|
|
|
break;
|
|
|
|
case VXOpcodeTreeNodeType::VEXL:
|
|
|
|
assert(info.flags & IF_PREFIX_VEX);
|
|
|
|
index = info.vex_l;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2014-10-30 23:36:25 +08:00
|
|
|
node = VDEGetOpcodeTreeChild(node, index);
|
2014-10-25 05:11:16 +08:00
|
|
|
} while (nodeType != VXOpcodeTreeNodeType::INSTRUCTION_DEFINITION);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-27 21:10:22 +08:00
|
|
|
VXInstructionDecoder::VXInstructionDecoder()
|
|
|
|
: m_dataSource(nullptr)
|
|
|
|
, m_disassemblerMode(VXDisassemblerMode::M32BIT)
|
|
|
|
, m_preferredVendor(VXInstructionSetVendor::ANY)
|
|
|
|
, m_instructionPointer(0)
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
|
|
|
|
2014-10-27 21:10:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
VXInstructionDecoder::VXInstructionDecoder(VXBaseDataSource *input,
|
|
|
|
VXDisassemblerMode disassemblerMode, VXInstructionSetVendor preferredVendor,
|
|
|
|
uint64_t instructionPointer)
|
|
|
|
: m_dataSource(input)
|
2014-10-25 05:11:16 +08:00
|
|
|
, m_disassemblerMode(disassemblerMode)
|
2014-10-27 21:10:22 +08:00
|
|
|
, m_preferredVendor(preferredVendor)
|
|
|
|
, m_instructionPointer(instructionPointer)
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-10-27 21:10:22 +08:00
|
|
|
bool VXInstructionDecoder::decodeInstruction(VXInstructionInfo &info)
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
|
|
|
// Clear instruction info
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
// Set disassembler mode flags
|
|
|
|
switch (m_disassemblerMode)
|
|
|
|
{
|
|
|
|
case VXDisassemblerMode::M16BIT:
|
|
|
|
info.flags |= IF_DISASSEMBLER_MODE_16;
|
|
|
|
break;
|
|
|
|
case VXDisassemblerMode::M32BIT:
|
|
|
|
info.flags |= IF_DISASSEMBLER_MODE_32;
|
|
|
|
break;
|
|
|
|
case VXDisassemblerMode::M64BIT:
|
|
|
|
info.flags |= IF_DISASSEMBLER_MODE_64;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2014-10-27 21:10:22 +08:00
|
|
|
// Set instruction address
|
|
|
|
info.instrAddress = m_instructionPointer;
|
2014-10-25 05:11:16 +08:00
|
|
|
// Decode
|
|
|
|
if (!decodePrefixes(info) || !decodeOpcode(info))
|
|
|
|
{
|
|
|
|
goto DecodeError;
|
|
|
|
}
|
|
|
|
// SWAPGS is only valid in 64 bit mode
|
|
|
|
if ((info.mnemonic == VXInstructionMnemonic::SWAPGS) &&
|
|
|
|
(m_disassemblerMode != VXDisassemblerMode::M64BIT))
|
|
|
|
{
|
|
|
|
info.flags &= IF_ERROR_INVALID;
|
|
|
|
goto DecodeError;
|
|
|
|
}
|
|
|
|
// Handle aliases
|
|
|
|
if (info.mnemonic == VXInstructionMnemonic::XCHG)
|
|
|
|
{
|
|
|
|
if ((info.operand[0].type == VXOperandType::REGISTER &&
|
|
|
|
info.operand[0].base == VXRegister::AX &&
|
|
|
|
info.operand[1].type == VXOperandType::REGISTER &&
|
|
|
|
info.operand[1].base == VXRegister::AX) ||
|
|
|
|
(info.operand[0].type == VXOperandType::REGISTER &&
|
|
|
|
info.operand[0].base == VXRegister::EAX &&
|
|
|
|
info.operand[1].type == VXOperandType::REGISTER &&
|
|
|
|
info.operand[1].base == VXRegister::EAX))
|
|
|
|
{
|
|
|
|
info.mnemonic = VXInstructionMnemonic::NOP;
|
|
|
|
info.operand[0].type = VXOperandType::NONE;
|
|
|
|
info.operand[1].type = VXOperandType::NONE;
|
2014-10-30 23:36:25 +08:00
|
|
|
info.operand[0].access_mode = VXOperandAccessMode::NA;
|
|
|
|
info.operand[1].access_mode = VXOperandAccessMode::NA;
|
2014-10-25 05:11:16 +08:00
|
|
|
}
|
|
|
|
}
|
2014-10-27 21:10:22 +08:00
|
|
|
if ((info.mnemonic == VXInstructionMnemonic::NOP) && (info.flags & IF_PREFIX_REP))
|
2014-10-25 05:11:16 +08:00
|
|
|
{
|
|
|
|
info.mnemonic = VXInstructionMnemonic::PAUSE;
|
2014-10-27 21:10:22 +08:00
|
|
|
info.flags &= ~IF_PREFIX_REP;
|
2014-10-25 05:11:16 +08:00
|
|
|
}
|
|
|
|
// Increment instruction pointer
|
|
|
|
m_instructionPointer += info.length;
|
2014-10-27 21:10:22 +08:00
|
|
|
// Set instruction pointer
|
|
|
|
info.instrPointer = m_instructionPointer;
|
2014-10-25 05:11:16 +08:00
|
|
|
return true;
|
|
|
|
DecodeError:
|
|
|
|
// Increment instruction pointer.
|
|
|
|
m_instructionPointer += 1;
|
2014-10-27 21:10:22 +08:00
|
|
|
// Backup all error flags, the instruction length and the instruction address
|
2014-10-25 05:11:16 +08:00
|
|
|
uint32_t flags = info.flags & (IF_ERROR_MASK | 0x00000007);
|
|
|
|
uint8_t length = info.length;
|
2014-10-27 21:10:22 +08:00
|
|
|
uint8_t firstByte = info.data[0];
|
|
|
|
uint64_t instrAddress = info.instrAddress;
|
2014-10-25 05:11:16 +08:00
|
|
|
// Clear instruction info
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
// Restore saved values
|
|
|
|
info.flags = flags;
|
|
|
|
info.length = length;
|
2014-10-27 21:10:22 +08:00
|
|
|
info.data[0] = firstByte;
|
|
|
|
info.instrAddress = instrAddress;
|
2014-10-30 23:36:25 +08:00
|
|
|
info.instrDefinition = Internal::VDEGetInstructionDefinition(0);
|
2014-10-25 05:11:16 +08:00
|
|
|
// Return with error, if the end of the input source was reached while decoding the
|
|
|
|
// invalid instruction
|
|
|
|
if (info.flags & IF_ERROR_END_OF_INPUT)
|
|
|
|
{
|
|
|
|
info.length = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Decrement the input position, if more than one byte was read from the input data
|
|
|
|
// source while decoding the invalid instruction.
|
|
|
|
if (info.length != 1)
|
|
|
|
{
|
2014-10-27 21:10:22 +08:00
|
|
|
m_dataSource->setPosition(m_dataSource->getPosition() - info.length + 1);
|
2014-10-25 05:11:16 +08:00
|
|
|
info.length = 1;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|