Disasm.cpp revision 3fb5b9f7efbadea55ade5adf10b1279961af3ff6
/** @file
*
* VBox disassembler:
* Main
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#ifdef USING_VISUAL_STUDIO
# include <stdafx.h>
#endif
#include <VBox/disopcode.h>
#include "DisasmInternal.h"
#include "DisasmTables.h"
/**
* Disassembles a code block.
*
* @returns VBox error code
* @param pCpu Pointer to cpu structure which have DISCPUSTATE::mode
* set correctly.
* @param pvCodeBlock Pointer to the strunction to disassemble.
* @param cbMax Maximum number of bytes to disassemble.
* @param pcbSize Where to store the size of the instruction.
* NULL is allowed.
*
*
* @todo Define output callback.
* @todo Using signed integers as sizes is a bit odd. There are still
* some GCC warnings about mixing signed and unsigend integers.
* @todo Need to extend this interface to include a code address so we
* can dissassemble GC code. Perhaps a new function is better...
* @remark cbMax isn't respected as a boundry. DISInstr() will read beyond cbMax.
* This means *pcbSize >= cbMax sometimes.
*/
{
unsigned i = 0;
char szOutput[256];
while (i < cbMax)
{
unsigned cbInstr;
if (VBOX_FAILURE(rc))
return rc;
i += cbInstr;
}
if (pSize)
*pSize = i;
return true;
}
/**
* Disassembles one instruction
*
* @returns VBox error code
* @param pCpu Pointer to cpu structure which have DISCPUSTATE::mode
* set correctly.
* @param pu8Instruction Pointer to the strunction to disassemble.
* @param u32EipOffset Offset to add to instruction address to get the real virtual address
* @param pcbSize Where to store the size of the instruction.
* NULL is allowed.
* @param pszOutput Storage for disassembled instruction
*
* @todo Define output callback.
*/
DISDECL(int) DISInstr(PDISCPUSTATE pCpu, RTUINTPTR pu8Instruction, unsigned u32EipOffset, unsigned *pcbSize,
char *pszOutput)
{
}
/**
* Disassembles one instruction; only fully disassembly an instruction if it matches the filter criteria
*
* @returns VBox error code
* @param pCpu Pointer to cpu structure which have DISCPUSTATE::mode
* set correctly.
* @param pu8Instruction Pointer to the strunction to disassemble.
* @param u32EipOffset Offset to add to instruction address to get the real virtual address
* @param pcbSize Where to store the size of the instruction.
* NULL is allowed.
* @param pszOutput Storage for disassembled instruction
* @param uFilter Instruction type filter
*
* @todo Define output callback.
*/
DISDECL(int) DISInstrEx(PDISCPUSTATE pCpu, RTUINTPTR pu8Instruction, unsigned u32EipOffset, unsigned *pcbSize,
{
unsigned i = 0, prefixbytes;
const OPCODE *paOneByteMap;
#ifdef __L4ENV__
#endif
//reset instruction settings
pCpu->prefix_seg = 0;
pCpu->lastprefix = 0;
if (pszOutput)
*pszOutput = '\0';
{
}
else
{
}
prefixbytes = 0;
#ifndef __L4ENV__ /* Unfortunately, we have no exception handling in l4env */
try
#else
if (setjmp(jumpbuffer) == 0)
#endif
{
while(1)
{
/* Hardcoded assumption about OP_* values!! */
{
/* The REX prefix must precede the opcode byte(s). Any other placement is ignored. */
switch(opcode)
{
case OP_INVALID:
#if 0 //defined (DEBUG_Sander)
AssertMsgFailed(("Invalid opcode!!\n"));
#endif
return VERR_DIS_INVALID_OPCODE;
// segment override prefix byte
case OP_SEG:
/* Segment prefixes for CS, DS, ES and SS are ignored in long mode. */
{
}
i += sizeof(uint8_t);
prefixbytes++;
continue; //fetch the next byte
// lock prefix byte
case OP_LOCK:
i += sizeof(uint8_t);
prefixbytes++;
continue; //fetch the next byte
// address size override prefix byte
case OP_ADDRSIZE:
else
else
i += sizeof(uint8_t);
prefixbytes++;
continue; //fetch the next byte
// operand size override prefix byte
case OP_OPSIZE:
else
pCpu->opmode = CPUMODE_16BIT; /* for 32 and 64 bits mode (there is no 32 bits operand size override prefix) */
i += sizeof(uint8_t);
prefixbytes++;
continue; //fetch the next byte
// rep and repne are not really prefixes, but we'll treat them as such
case OP_REPE:
i += sizeof(uint8_t);
prefixbytes += sizeof(uint8_t);
continue; //fetch the next byte
case OP_REPNE:
i += sizeof(uint8_t);
prefixbytes += sizeof(uint8_t);
continue; //fetch the next byte
case OP_REX:
/* REX prefix byte */
break;
}
}
idx = i;
i += sizeof(uint8_t); //first opcode byte
if(pszOutput) {
disasmSprintf(pszOutput, pu8Instruction+i-1-prefixbytes, pCpu, &pCpu->param1, &pCpu->param2, &pCpu->param3);
}
i += inc;
prefixbytes = 0;
break;
}
}
#ifndef __L4ENV__
catch(...)
#else
else /* setjmp has returned a non-zero value: an exception occured */
#endif
{
if (pcbSize)
*pcbSize = 0;
return VERR_DIS_GEN_FAILURE;
}
if (pcbSize)
*pcbSize = i;
return VINF_SUCCESS;
}
//*****************************************************************************
//*****************************************************************************
{
char szByte[4];
int i;
for(i = len; i < 40; i++)
{
}
for(i = 0; i < size; i++)
{
}
return pszOutput;
}
//*****************************************************************************
//*****************************************************************************
void disasmSprintf(char *pszOutput, RTUINTPTR pu8Instruction, PDISCPUSTATE pCpu, OP_PARAMETER *pParam1, OP_PARAMETER *pParam2, OP_PARAMETER *pParam3)
{
int param = 1;
{
}
{
}
else
{
}
{
RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, "Invalid Opcode [%02X][%02X]", DISReadByte(pCpu, pu8Instruction), DISReadByte(pCpu, pu8Instruction+1) );
}
else
while(*lpszFormat)
{
switch(*lpszFormat)
{
case '%':
switch(*(lpszFormat+1))
{
case 'J': //Relative jump offset
{
{
}
else
{
}
else
{
}
else
{
AssertMsgFailed(("Oops!\n"));
return;
}
}
//no break;
case 'A': //direct address
case 'C': //control register
case 'D': //debug register
case 'E': //ModRM specifies parameter
case 'F': //Eflags register
case 'G': //ModRM selects general register
case 'I': //Immediate data
case 'M': //ModRM may only refer to memory
case 'O': //No ModRM byte
case 'P': //ModRM byte selects MMX register
case 'Q': //ModRM byte selects MMX register or memory address
case 'R': //ModRM byte may only refer to a general register
case 'S': //ModRM byte selects a segment register
case 'T': //ModRM byte selects a test register
case 'X': //DS:SI
case 'Y': //ES:DI
switch(param)
{
case 1:
break;
case 2:
break;
case 3:
break;
}
break;
case 'e': //register based on operand size (e.g. %eAX)
break;
default:
AssertMsgFailed(("Oops!\n"));
break;
}
//Go to the next parameter in the format string
break;
case ',':
param++;
//no break
default:
break;
}
if(*lpszFormat) lpszFormat++;
}
}
//*****************************************************************************
//*****************************************************************************