EMAll.cpp revision 8c0c69900803583540d2baa8766ed91f0c03f873
/* $Id$ */
/** @file
* EM - Execution Monitor(/Manager) - All contexts
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* 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 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_EM
#include "EMInternal.h"
#include <VBox/disopcode.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
typedef DECLCALLBACK(uint32_t) PFN_EMULATE_PARAM3(uint32_t *pu32Param1, uint32_t val2, size_t val3);
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
DECLINLINE(int) emInterpretInstructionCPU(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize);
/**
* Get the current execution manager status.
*
* @returns Current status.
*/
{
}
#ifndef IN_GC
/**
* Read callback for disassembly function; supports reading bytes that cross a page boundary
*
* @returns VBox status code.
* @param pSrc GC source pointer
* @param pDest HC destination pointer
* @param size Number of bytes to read
* @param dwUserdata Callback specific user data (pCpu)
*
*/
DECLCALLBACK(int32_t) EMReadBytes(RTHCUINTPTR pSrc, uint8_t *pDest, uint32_t size, RTHCUINTPTR dwUserdata)
{
#ifdef IN_RING0
#else
{
}
else
{
{
{
}
}
}
#endif /* IN_RING0 */
return VINF_SUCCESS;
}
{
}
#else
{
}
#endif
/**
* Disassembles one instruction.
*
* @param pVM The VM handle.
* @param pCtxCore The context core (used for both the mode and instruction).
* @param pCpu Where to return the parsed instruction info.
* @param pcbInstr Where to return the instruction size. (optional)
*/
EMDECL(int) EMInterpretDisasOne(PVM pVM, PCCPUMCTXCORE pCtxCore, PDISCPUSTATE pCpu, unsigned *pcbInstr)
{
int rc = SELMValidateAndConvertCSAddr(pVM, pCtxCore->eflags, pCtxCore->ss, pCtxCore->cs, (PCPUMSELREGHID)&pCtxCore->csHid, (RTGCPTR)pCtxCore->eip, &GCPtrInstr);
if (VBOX_FAILURE(rc))
{
Log(("EMInterpretDisasOne: Failed to convert %RTsel:%RX32 (cpl=%d) - rc=%Vrc !!\n",
return rc;
}
}
/**
* Disassembles one instruction.
*
*
* @param pVM The VM handle.
* @param GCPtrInstr The flat address of the instruction.
* @param pCtxCore The context core (used to determin the cpu mode).
* @param pCpu Where to return the parsed instruction info.
* @param pcbInstr Where to return the instruction size. (optional)
*/
EMDECL(int) EMInterpretDisasOneEx(PVM pVM, RTGCUINTPTR GCPtrInstr, PCCPUMCTXCORE pCtxCore, PDISCPUSTATE pCpu, unsigned *pcbInstr)
{
int rc = DISCoreOneEx(GCPtrInstr, SELMIsSelector32Bit(pVM, pCtxCore->eflags, pCtxCore->cs, (PCPUMSELREGHID)&pCtxCore->csHid) ? CPUMODE_32BIT : CPUMODE_16BIT,
#ifdef IN_GC
#else
#endif
if (VBOX_SUCCESS(rc))
return VINF_SUCCESS;
return VERR_INTERNAL_ERROR;
}
/**
* Interprets the current instruction.
*
* @returns VBox status code.
* @retval VINF_* Scheduling instructions.
* @retval VERR_EM_INTERPRETER Something we can't cope with.
* @retval VERR_* Fatal errors.
*
* @param pVM The VM handle.
* @param pRegFrame The register frame.
* Updates the EIP if an instruction was executed successfully.
* @param pvFault The fault address (CR2).
* @param pcbSize Size of the write (if applicable).
*
* @remark Invalid opcode exceptions have a higher priority than GP (see Intel
* Architecture System Developers Manual, Vol 3, 5.5) so we don't need
* to worry about e.g. invalid modrm combinations (!)
*/
EMDECL(int) EMInterpretInstruction(PVM pVM, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
int rc = SELMValidateAndConvertCSAddr(pVM, pRegFrame->eflags, pRegFrame->ss, pRegFrame->cs, &pRegFrame->csHid, (RTGCPTR)pRegFrame->eip, &pbCode);
if (VBOX_SUCCESS(rc))
{
Cpu.mode = SELMIsSelector32Bit(pVM, pRegFrame->eflags, pRegFrame->cs, &pRegFrame->csHid) ? CPUMODE_32BIT : CPUMODE_16BIT;
if (VBOX_SUCCESS(rc))
{
if (VBOX_SUCCESS(rc))
{
}
return rc;
}
}
return VERR_EM_INTERPRETER;
}
/**
* Interprets the current instruction using the supplied DISCPUSTATE structure.
*
* EIP is *NOT* updated!
*
* @returns VBox status code.
* @retval VINF_* Scheduling instructions. When these are returned, it
* starts to get a bit tricky to know whether code was
* executed or not... We'll address this when it becomes a problem.
* @retval VERR_EM_INTERPRETER Something we can't cope with.
* @retval VERR_* Fatal errors.
*
* @param pVM The VM handle.
* @param pCpu The disassembler cpu state for the instruction to be interpreted.
* @param pRegFrame The register frame. EIP is *NOT* changed!
* @param pvFault The fault address (CR2).
* @param pcbSize Size of the write (if applicable).
*
* @remark Invalid opcode exceptions have a higher priority than GP (see Intel
* Architecture System Developers Manual, Vol 3, 5.5) so we don't need
* to worry about e.g. invalid modrm combinations (!)
*
* @todo At this time we do NOT check if the instruction overwrites vital information.
* Make sure this can't happen!! (will add some assertions/checks later)
*/
EMDECL(int) EMInterpretInstructionCPU(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
if (VBOX_SUCCESS(rc))
else
return rc;
}
/**
* Interpret a port I/O instruction.
*
* @returns VBox status code suitable for scheduling.
* @param pVM The VM handle.
* @param pCtxCore The context core. This will be updated on successful return.
* @param pCpu The instruction to interpret.
* @param cbOp The size of the instruction.
* @remark This may raise exceptions.
*/
{
/*
* Hand it on to IOM.
*/
#ifdef IN_GC
if (IOM_SUCCESS(rc))
return rc;
#else
AssertReleaseMsgFailed(("not implemented\n"));
return VERR_NOT_IMPLEMENTED;
#endif
}
{
#ifdef IN_GC
return rc;
/*
* The page pool cache may end up here in some cases because it
* flushed one of the shadow mappings used by the trapping
* instruction and it either flushed the TLB or the CPU reused it.
*/
return VINF_SUCCESS;
#else
#endif
}
{
#ifdef IN_GC
return rc;
/*
* The page pool cache may end up here in some cases because it
* flushed one of the shadow mappings used by the trapping
* instruction and it either flushed the TLB or the CPU reused it.
* We want to play safe here, verifying that we've got write
* access doesn't cost us much (see PGMPhysGCPtr2GCPhys()).
*/
if (RT_FAILURE(rc))
return rc;
if ( !(fFlags & X86_PTE_RW)
return VERR_ACCESS_DENIED;
return VINF_SUCCESS;
#else
#endif
}
/* Convert sel:addr to a flat GC address */
static RTGCPTR emConvertToFlatAddr(PVM pVM, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu, POP_PARAMETER pParam, RTGCPTR pvAddr)
{
int prefix_seg, rc;
if (VBOX_FAILURE(rc))
return pvAddr;
}
/**
* XCHG instruction emulation.
*/
static int emInterpretXchg(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
/* Source to make DISQueryParamVal read the register value - ugly hack */
if(VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
if(VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
#ifdef IN_GC
if (TRPMHasTrap(pVM))
{
{
#endif
{
case PARMTYPE_IMMEDIATE: /* register type is translated to this one too */
break;
case PARMTYPE_ADDRESS:
#ifdef IN_GC
/* Safety check (in theory it could cross a page boundary and fault there though) */
#endif
if (VBOX_FAILURE(rc))
{
return VERR_EM_INTERPRETER;
}
break;
default:
AssertFailed();
return VERR_EM_INTERPRETER;
}
{
case PARMTYPE_ADDRESS:
#ifdef IN_GC
/* Safety check (in theory it could cross a page boundary and fault there though) */
#endif
if (VBOX_FAILURE(rc))
{
}
break;
case PARMTYPE_IMMEDIATE:
break;
default:
AssertFailed();
return VERR_EM_INTERPRETER;
}
/* Write value of parameter 2 to parameter 1 (reg or memory address) */
if (pParam1 == 0)
{
{
case 1: //special case for AH etc
default: AssertFailedReturn(VERR_EM_INTERPRETER);
}
if (VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
}
else
{
if (VBOX_FAILURE(rc))
{
return VERR_EM_INTERPRETER;
}
}
/* Write value of parameter 1 to parameter 2 (reg or memory address) */
if (pParam2 == 0)
{
{
case 1: //special case for AH etc
default: AssertFailedReturn(VERR_EM_INTERPRETER);
}
if (VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
}
else
{
if (VBOX_FAILURE(rc))
{
return VERR_EM_INTERPRETER;
}
}
return VINF_SUCCESS;
#ifdef IN_GC
}
}
#endif
return VERR_EM_INTERPRETER;
}
/**
* INC and DEC emulation.
*/
static int emInterpretIncDec(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize,
{
if(VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
#ifdef IN_GC
if (TRPMHasTrap(pVM))
{
{
#endif
{
#ifdef IN_GC
/* Safety check (in theory it could cross a page boundary and fault there though) */
#endif
if (VBOX_FAILURE(rc))
{
return VERR_EM_INTERPRETER;
}
}
else
{
AssertFailed();
return VERR_EM_INTERPRETER;
}
/* Write result back */
if (VBOX_FAILURE(rc))
{
return VERR_EM_INTERPRETER;
}
/* Update guest's eflags and finish. */
pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
/* All done! */
return VINF_SUCCESS;
#ifdef IN_GC
}
}
#endif
return VERR_EM_INTERPRETER;
}
/**
* POP Emulation.
*/
static int emInterpretPop(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
if(VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
#ifdef IN_GC
if (TRPMHasTrap(pVM))
{
{
#endif
/* Read stack value first */
return VERR_EM_INTERPRETER; /* No legacy 16 bits stuff here, please. */
/* Convert address; don't bother checking limits etc, as we only read here */
pStackVal = SELMToFlat(pVM, pRegFrame->eflags, pRegFrame->ss, &pRegFrame->ssHid, (RTGCPTR)pRegFrame->esp);
if (pStackVal == 0)
return VERR_EM_INTERPRETER;
if (VBOX_FAILURE(rc))
{
return VERR_EM_INTERPRETER;
}
{
/* pop [esp+xx] uses esp after the actual pop! */
)
#ifdef IN_GC
/* Safety check (in theory it could cross a page boundary and fault there though) */
AssertMsgReturn(pParam1 == pvFault || (RTGCPTR)pRegFrame->esp == pvFault, ("%VGv != %VGv ss:esp=%04X:%VGv\n", pParam1, pvFault, pRegFrame->ss, pRegFrame->esp), VERR_EM_INTERPRETER);
#endif
if (VBOX_FAILURE(rc))
{
return VERR_EM_INTERPRETER;
}
/* Update ESP as the last step */
}
else
{
#ifndef DEBUG_bird // annoying assertion.
AssertFailed();
#endif
return VERR_EM_INTERPRETER;
}
/* All done! */
return VINF_SUCCESS;
#ifdef IN_GC
}
}
#endif
return VERR_EM_INTERPRETER;
}
/**
*/
static int emInterpretOrXorAnd(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize,
{
if(VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
if(VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
#ifdef DEBUG
const char *pszInstr;
pszInstr = "Xor";
else
pszInstr = "Or";
else
pszInstr = "And";
#endif
#ifdef IN_GC
if (TRPMHasTrap(pVM))
{
{
#endif
{
{
AssertMsgFailed(("%s at %VGv parameter mismatch %d vs %d!!\n", pszInstr, pRegFrame->eip, pCpu->param1.size, pCpu->param2.size)); /* should never happen! */
return VERR_EM_INTERPRETER;
}
/* Or %Ev, Ib -> just a hack to save some space; the data width of the 1st parameter determines the real width */
}
/* The destination is always a virtual address */
{
#ifdef IN_GC
/* Safety check (in theory it could cross a page boundary and fault there though) */
AssertMsgReturn(pParam1 == pvFault, ("eip=%VGv, pParam1=%VGv pvFault=%VGv\n", pRegFrame->eip, pParam1, pvFault), VERR_EM_INTERPRETER);
#endif
if (VBOX_FAILURE(rc))
{
return VERR_EM_INTERPRETER;
}
}
else
{
AssertFailed();
return VERR_EM_INTERPRETER;
}
/* Register or immediate data */
{
case PARMTYPE_IMMEDIATE: /* both immediate data and register (ugly) */
break;
default:
AssertFailed();
return VERR_EM_INTERPRETER;
}
/* Data read, emulate instruction. */
/* Update guest's eflags and finish. */
pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
/* And write it back */
if (VBOX_SUCCESS(rc))
{
/* All done! */
return VINF_SUCCESS;
}
#ifdef IN_GC
}
}
#endif
return VERR_EM_INTERPRETER;
}
/**
* ADD, ADC & SUB Emulation.
*/
static int emInterpretAddSub(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize,
{
if(VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
if(VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
#ifdef DEBUG
const char *pszInstr;
pszInstr = "Sub";
else
pszInstr = "Add";
else
pszInstr = "Adc";
#endif
#ifdef IN_GC
if (TRPMHasTrap(pVM))
{
{
#endif
{
{
AssertMsgFailed(("%s at %VGv parameter mismatch %d vs %d!!\n", pszInstr, pRegFrame->eip, pCpu->param1.size, pCpu->param2.size)); /* should never happen! */
return VERR_EM_INTERPRETER;
}
/* Or %Ev, Ib -> just a hack to save some space; the data width of the 1st parameter determines the real width */
}
/* The destination is always a virtual address */
{
#ifdef IN_GC
/* Safety check (in theory it could cross a page boundary and fault there though) */
#endif
if (VBOX_FAILURE(rc))
{
return VERR_EM_INTERPRETER;
}
}
else
{
#ifndef DEBUG_bird
AssertFailed();
#endif
return VERR_EM_INTERPRETER;
}
/* Register or immediate data */
{
case PARMTYPE_IMMEDIATE: /* both immediate data and register (ugly) */
break;
default:
AssertFailed();
return VERR_EM_INTERPRETER;
}
/* Data read, emulate instruction. */
/* Update guest's eflags and finish. */
pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
/* And write it back */
if (VBOX_SUCCESS(rc))
{
/* All done! */
return VINF_SUCCESS;
}
#ifdef IN_GC
}
}
#endif
return VERR_EM_INTERPRETER;
}
/**
* ADC Emulation.
*/
static int emInterpretAdc(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
else
}
/**
* BTR/C/S Emulation.
*/
static int emInterpretBitTest(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize,
{
if(VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
if(VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
#ifdef DEBUG
const char *pszInstr;
pszInstr = "Btr";
else
pszInstr = "Bts";
else
pszInstr = "Btc";
#endif
#ifdef IN_GC
if (TRPMHasTrap(pVM))
{
{
#endif
/* The destination is always a virtual address */
return VERR_EM_INTERPRETER;
/* Register or immediate data */
{
case PARMTYPE_IMMEDIATE: /* both immediate data and register (ugly) */
break;
default:
AssertFailed();
return VERR_EM_INTERPRETER;
}
#ifdef IN_GC
/* Safety check. */
AssertMsgReturn((RTGCPTR)((RTGCUINTPTR)pParam1 & ~3) == pvFault, ("pParam1=%VGv pvFault=%VGv\n", pParam1, pvFault), VERR_EM_INTERPRETER);
#endif
if (VBOX_FAILURE(rc))
{
return VERR_EM_INTERPRETER;
}
/* Data read, emulate bit test instruction. */
/* Update guest's eflags and finish. */
pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
/* And write it back */
if (VBOX_SUCCESS(rc))
{
/* All done! */
*pcbSize = 1;
return VINF_SUCCESS;
}
#ifdef IN_GC
}
}
#endif
return VERR_EM_INTERPRETER;
}
/**
* MOV emulation.
*/
static int emInterpretMov(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
if(VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
if(VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
#ifdef IN_GC
if (TRPMHasTrap(pVM))
{
{
#else
/** @todo Make this the default and don't rely on TRPM information. */
{
#endif
{
case PARMTYPE_IMMEDIATE:
return VERR_EM_INTERPRETER;
/* fallthru */
case PARMTYPE_ADDRESS:
break;
default:
AssertFailed();
return VERR_EM_INTERPRETER;
}
{
case PARMTYPE_IMMEDIATE: /* register type is translated to this one too */
break;
default:
return VERR_EM_INTERPRETER;
}
LogFlow(("EMInterpretInstruction at %08x: OP_MOV %08X <- %08X (%d) &val32=%08x\n", pRegFrame->eip, pDest, val32, param2.size, &val32));
#ifdef IN_GC
/* Safety check (in theory it could cross a page boundary and fault there though) */
AssertMsgReturn(pDest == pvFault, ("eip=%VGv pDest=%VGv pvFault=%VGv\n", pRegFrame->eip, pDest, pvFault), VERR_EM_INTERPRETER);
#endif
if (VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
}
else
{ /* read fault */
/* Source */
{
case PARMTYPE_IMMEDIATE:
return VERR_EM_INTERPRETER;
/* fallthru */
case PARMTYPE_ADDRESS:
break;
default:
return VERR_EM_INTERPRETER;
}
#ifdef IN_GC
/* Safety check (in theory it could cross a page boundary and fault there though) */
#endif
if (VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
/* Destination */
{
case PARMTYPE_REGISTER:
{
default:
return VERR_EM_INTERPRETER;
}
if (VBOX_FAILURE(rc))
return rc;
break;
default:
return VERR_EM_INTERPRETER;
}
}
return VINF_SUCCESS;
#ifdef IN_GC
}
#endif
return VERR_EM_INTERPRETER;
}
#ifdef IN_GC
static int emInterpretCmpXchg(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
/* Source to make DISQueryParamVal read the register value - ugly hack */
if(VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
if(VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
if (TRPMHasTrap(pVM))
{
{
#ifdef VBOX_STRICT
#endif
{
case PARMTYPE_ADDRESS:
/* Safety check (in theory it could cross a page boundary and fault there though) */
AssertMsgReturn(pParam1 == pvFault, ("eip=%VGv pParam1=%VGv pvFault=%VGv\n", pRegFrame->eip, pParam1, pvFault), VERR_EM_INTERPRETER);
#ifdef VBOX_STRICT
if (VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
#endif
break;
default:
return VERR_EM_INTERPRETER;
}
{
case PARMTYPE_IMMEDIATE: /* register actually */
break;
default:
return VERR_EM_INTERPRETER;
}
#ifdef VBOX_STRICT
#endif
else
#ifdef VBOX_STRICT
LogFlow(("CmpXchg %VGv=%08x eax=%08x %08x ZF=%d\n", pParam1, valpar1, pRegFrame->eax, valpar, !!(eflags & X86_EFL_ZF)));
#endif
/* Update guest's eflags and finish. */
pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
return VINF_SUCCESS;
}
}
return VERR_EM_INTERPRETER;
}
#endif
/**
* Interpret IRET (currently only to V86 code)
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pRegFrame The register frame.
*
*/
{
int rc;
/* Mask away all reserved bits */
uMask = X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_TF | X86_EFL_IF | X86_EFL_DF | X86_EFL_OF | X86_EFL_IOPL | X86_EFL_NT | X86_EFL_RF | X86_EFL_VM | X86_EFL_AC | X86_EFL_VIF | X86_EFL_VIP | X86_EFL_ID;
#ifndef IN_RING0
#endif
return VINF_SUCCESS;
}
/**
* IRET Emulation.
*/
static int emInterpretIret(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
/* only allow direct calls to EMInterpretIret for now */
return VERR_EM_INTERPRETER;
}
/**
* INVLPG Emulation.
*/
/**
* Interpret INVLPG
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pRegFrame The register frame.
* @param pAddrGC Operand address
*
*/
{
int rc;
/** @todo is addr always a flat linear address or ds based
* (in absence of segment override prefixes)????
*/
#ifdef IN_GC
// Note: we could also use PGMFlushPage here, but it currently doesn't always use invlpg!!!!!!!!!!
#else
#endif
if (VBOX_SUCCESS(rc))
return VINF_SUCCESS;
/** @todo r=bird: we shouldn't ignore returns codes like this... I'm 99% sure the error is fatal. */
return VERR_EM_INTERPRETER;
}
static int emInterpretInvlPg(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
if(VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
{
case PARMTYPE_IMMEDIATE:
case PARMTYPE_ADDRESS:
return VERR_EM_INTERPRETER;
break;
default:
return VERR_EM_INTERPRETER;
}
/** @todo is addr always a flat linear address or ds based
* (in absence of segment override prefixes)????
*/
#ifdef IN_GC
// Note: we could also use PGMFlushPage here, but it currently doesn't always use invlpg!!!!!!!!!!
#else
#endif
if (VBOX_SUCCESS(rc))
return VINF_SUCCESS;
/** @todo r=bird: we shouldn't ignore returns codes like this... I'm 99% sure the error is fatal. */
return VERR_EM_INTERPRETER;
}
/**
* CPUID Emulation.
*/
/**
* Interpret CPUID given the parameters in the CPU context
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pRegFrame The register frame.
*
*/
{
CPUMGetGuestCpuId(pVM, pRegFrame->eax, &pRegFrame->eax, &pRegFrame->ebx, &pRegFrame->ecx, &pRegFrame->edx);
return VINF_SUCCESS;
}
static int emInterpretCpuId(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
Log(("Emulate: CPUID %x -> %08x %08x %08x %08x\n", iLeaf, pRegFrame->eax, pRegFrame->ebx, pRegFrame->ecx, pRegFrame->edx));
return rc;
}
/**
* MOV CRx Emulation.
*/
/**
* Interpret CRx read
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pRegFrame The register frame.
* @param DestRegGen General purpose register index (USE_REG_E**))
* @param SrcRegCRx CRx register index (USE_REG_CR*)
*
*/
EMDECL(int) EMInterpretCRxRead(PVM pVM, PCPUMCTXCORE pRegFrame, uint32_t DestRegGen, uint32_t SrcRegCrx)
{
if(VBOX_SUCCESS(rc))
{
return VINF_SUCCESS;
}
return VERR_EM_INTERPRETER;
}
/**
* Interpret LMSW
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param u16Data LMSW source data.
*
*/
{
/* don't use this path to go into protected mode! */
if (!(OldCr0 & X86_CR0_PE))
return VERR_EM_INTERPRETER;
/* Only PE, MP, EM and TS can be changed; note that PE can't be cleared by this instruction. */
#ifdef IN_GC
/* Need to change the hyper CR0? Doing it the lazy way then. */
{
}
#endif
}
/**
* Interpret CLTS
*
* @returns VBox status code.
* @param pVM The VM handle.
*
*/
{
if (!(Cr0 & X86_CR0_TS))
return VINF_SUCCESS;
#ifdef IN_GC
/* Need to change the hyper CR0? Doing it the lazy way then. */
#endif
}
static int emInterpretClts(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
return EMInterpretCLTS(pVM);
}
/**
* Interpret CRx write
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pRegFrame The register frame.
* @param DestRegCRx CRx register index (USE_REG_CR*)
* @param SrcRegGen General purpose register index (USE_REG_E**))
*
*/
EMDECL(int) EMInterpretCRxWrite(PVM pVM, PCPUMCTXCORE pRegFrame, uint32_t DestRegCrx, uint32_t SrcRegGen)
{
/** @todo Clean up this mess. */
if (VBOX_SUCCESS(rc))
{
switch (DestRegCrx)
{
case USE_REG_CR0:
#ifndef IN_RING3
/* CR0.WP changes require a reschedule run in ring 3. */
return VERR_EM_INTERPRETER;
#endif
rc = CPUMSetGuestCR0(pVM, val32); AssertRC(rc); /** @todo CPUSetGuestCR0 stuff should be void, this is silly. */
{
/* global flush */
}
# ifdef IN_GC
/* Feeling extremely lazy. */
{
}
# endif
case USE_REG_CR2:
return VINF_SUCCESS;
case USE_REG_CR3:
/* Reloading the current CR3 means the guest just wants to flush the TLBs */
{
/* flush */
}
return VINF_SUCCESS;
case USE_REG_CR4:
#ifndef IN_RING3
/** @todo is flipping of the X86_CR4_PAE bit handled correctly here? */
#endif
{
/* global flush */
}
# ifndef IN_RING3 /** @todo check this out IN_RING0! */
/* Feeling extremely lazy. */
if ( (oldval & (X86_CR4_OSFSXR|X86_CR4_OSXMMEEXCPT|X86_CR4_PCE|X86_CR4_MCE|X86_CR4_PAE|X86_CR4_DE|X86_CR4_TSD|X86_CR4_PVI|X86_CR4_VME))
!= (val32 & (X86_CR4_OSFSXR|X86_CR4_OSXMMEEXCPT|X86_CR4_PCE|X86_CR4_MCE|X86_CR4_PAE|X86_CR4_DE|X86_CR4_TSD|X86_CR4_PVI|X86_CR4_VME)))
{
}
# endif
default:
AssertFailed();
case USE_REG_CR1: /* illegal op */
break;
}
}
return VERR_EM_INTERPRETER;
}
static int emInterpretMovCRx(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
return EMInterpretCRxWrite(pVM, pRegFrame, pCpu->param1.base.reg_ctrl, pCpu->param2.base.reg_gen32);
return VERR_EM_INTERPRETER;
}
/**
* MOV DRx
*/
/**
* Interpret DRx write
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pRegFrame The register frame.
* @param DestRegDRx DRx register index (USE_REG_DR*)
* @param SrcRegGen General purpose register index (USE_REG_E**))
*
*/
EMDECL(int) EMInterpretDRxWrite(PVM pVM, PCPUMCTXCORE pRegFrame, uint32_t DestRegDrx, uint32_t SrcRegGen)
{
if (VBOX_SUCCESS(rc))
{
if (VBOX_SUCCESS(rc))
return rc;
}
return VERR_EM_INTERPRETER;
}
/**
* Interpret DRx read
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pRegFrame The register frame.
* @param DestRegGen General purpose register index (USE_REG_E**))
* @param SrcRegDRx DRx register index (USE_REG_DR*)
*
*/
EMDECL(int) EMInterpretDRxRead(PVM pVM, PCPUMCTXCORE pRegFrame, uint32_t DestRegGen, uint32_t SrcRegDrx)
{
if (VBOX_SUCCESS(rc))
return VINF_SUCCESS;
return VERR_EM_INTERPRETER;
}
static int emInterpretMovDRx(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
int rc = VERR_EM_INTERPRETER;
{
}
else
{
}
else
AssertMsgFailed(("Unexpected debug register move\n"));
return rc;
}
/**
* LLDT Emulation.
*/
static int emInterpretLLdt(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
if(VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
{
case PARMTYPE_ADDRESS:
return VERR_EM_INTERPRETER; //feeling lazy right now
case PARMTYPE_IMMEDIATE:
return VERR_EM_INTERPRETER;
break;
default:
return VERR_EM_INTERPRETER;
}
if (sel == 0)
{
if (CPUMGetHyperLDTR(pVM) == 0)
{
// this simple case is most frequent in Windows 2000 (31k - boot & shutdown)
return VINF_SUCCESS;
}
}
//still feeling lazy
return VERR_EM_INTERPRETER;
}
#ifdef IN_GC
/**
* STI Emulation.
*
* @remark the instruction following sti is guaranteed to be executed before any interrupts are dispatched
*/
static int emInterpretSti(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
if(!pGCState)
{
return VERR_EM_INTERPRETER;
}
Assert(pvFault == SELMToFlat(pVM, pRegFrame->eflags, pRegFrame->cs, &pRegFrame->csHid, (RTGCPTR)pRegFrame->eip));
return VINF_SUCCESS;
}
#endif /* IN_GC */
/**
* HLT Emulation.
*/
static int emInterpretHlt(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
return VINF_EM_HALT;
}
/**
* RDTSC Emulation.
*/
/**
* Interpret RDTSC
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pRegFrame The register frame.
*
*/
{
if (uCR4 & X86_CR4_TSD)
return VERR_EM_INTERPRETER; /* genuine #GP */
return VINF_SUCCESS;
}
static int emInterpretRdtsc(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
}
/**
* MONITOR Emulation.
*/
static int emInterpretMonitor(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
return VERR_EM_INTERPRETER; /* illegal value. */
/* Get the current privilege level. */
if (cpl != 0)
return VERR_EM_INTERPRETER; /* supervisor only */
if (!(u32ExtFeatures & X86_CPUID_FEATURE_ECX_MONITOR))
return VERR_EM_INTERPRETER; /* not supported */
return VINF_SUCCESS;
}
/**
* MWAIT Emulation.
*/
static int emInterpretMWait(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
return VERR_EM_INTERPRETER; /* illegal value. */
/* Get the current privilege level. */
if (cpl != 0)
return VERR_EM_INTERPRETER; /* supervisor only */
if (!(u32ExtFeatures & X86_CPUID_FEATURE_ECX_MONITOR))
return VERR_EM_INTERPRETER; /* not supported */
/** @todo not completely correct */
return VINF_EM_HALT;
}
/**
* Internal worker.
* @copydoc EMInterpretInstructionCPU
*/
DECLINLINE(int) emInterpretInstructionCPU(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
*pcbSize = 0;
/*
* Only supervisor guest code!!
* And no complicated prefixes.
*/
/* Get the current privilege level. */
if ( cpl != 0
{
Log(("WARNING: refusing instruction emulation for user-mode code!!\n"));
return VERR_EM_INTERPRETER;
}
#ifdef IN_GC
)
)
#else
#endif
{
//Log(("EMInterpretInstruction: wrong prefix!!\n"));
return VERR_EM_INTERPRETER;
}
int rc;
{
case opcode:\
if (VBOX_SUCCESS(rc)) \
else \
return rc
case opcode:\
if (VBOX_SUCCESS(rc)) \
else \
return rc
case opcode:\
if (VBOX_SUCCESS(rc)) \
else \
return rc
case opcode: STAM_COUNTER_INC(&pVM->em.s.CTXSUFF(pStats)->CTXMID(Stat,Failed##Instr)); return VERR_EM_INTERPRETER;
#ifdef IN_GC
#endif
#ifdef VBOX_WITH_STATISTICS
#ifndef IN_GC
#endif
#endif
default:
return VERR_EM_INTERPRETER;
}
AssertFailed();
return VERR_INTERNAL_ERROR;
}
/**
* Sets the PC for which interrupts should be inhibited.
*
* @param pVM The VM handle.
* @param PC The PC.
*/
{
}
/**
* Gets the PC for which interrupts should be inhibited.
*
* There are a few instructions which inhibits or delays interrupts
* for the instruction following them. These instructions are:
* - STI
* - POP SS
*
* @returns The PC for which interrupts should be inhibited.
* @param pVM VM handle.
*
*/
{
}