EMAll.cpp revision 8d5210f02ffa3d8f8f21a917c1ee0d83c664e644
/* $Id$ */
/** @file
* EM - Execution Monitor(/Manager) - All contexts
*/
/*
* Copyright (C) 2006-2007 Oracle Corporation
*
* 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_EM
#include "EMInternal.h"
#include <VBox/disopcode.h>
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** @def EM_ASSERT_FAULT_RETURN
* Safety check.
*
* Could in theory misfire on a cross page boundary access...
*
* Currently disabled because the CSAM (+ PATM) patch monitoring occasionally
* turns up an alias page instead of the original faulting one and annoying the
* heck out of anyone running a debug build. See @bugref{2609} and @bugref{1931}.
*/
#if 0
#else
#endif
/* Used to pass information during instruction disassembly. */
typedef struct
{
} EMDISSTATE, *PEMDISSTATE;
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
DECLINLINE(int) emInterpretInstructionCPU(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize, EMCODETYPE enmCodeType = EMCODETYPE_SUPERVISOR);
/**
* Get the current execution manager status.
*
* @returns Current status.
* @param pVCpu The VMCPU to operate on.
*/
{
}
/**
* Sets the current execution manager status. (use only when you know what you're doing!)
*
* @param pVCpu The VMCPU to operate on.
*/
{
/* Only allowed combination: */
}
/**
* 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 cb Number of bytes to read
* @param dwUserdata Callback specific user data (pDis)
*
*/
{
# ifdef IN_RING0
int rc;
{
for (unsigned i=0; i<cb; i++)
{
}
return VINF_SUCCESS;
}
{
}
else
{
if (rc == VERR_ACCESS_DENIED)
{
/* Recently flushed; access the data manually. */
}
}
else /* the hypervisor region is always present. */
# endif /* IN_RING3 */
return VINF_SUCCESS;
}
#ifndef IN_RC
DECLINLINE(int) emDisCoreOne(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, RTGCUINTPTR InstrGC, uint32_t *pOpsize)
{
if (RT_SUCCESS(rc))
{
}
else
{
{
if (rc == VERR_PAGE_TABLE_NOT_PRESENT)
return rc;
}
}
}
#else /* IN_RC */
DECLINLINE(int) emDisCoreOne(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, RTGCUINTPTR InstrGC, uint32_t *pOpsize)
{
}
#endif /* IN_RC */
/**
* Disassembles one instruction.
*
* @returns VBox status code, see SELMToFlatEx and EMInterpretDisasOneEx for
* details.
* @retval VERR_INTERNAL_ERROR on DISCoreOneEx failure.
*
* @param pVM The VM handle.
* @param pVCpu The VMCPU handle.
* @param pCtxCore The context core (used for both the mode and instruction).
* @param pDis Where to return the parsed instruction info.
* @param pcbInstr Where to return the instruction size. (optional)
*/
VMMDECL(int) EMInterpretDisasOne(PVM pVM, PVMCPU pVCpu, PCCPUMCTXCORE pCtxCore, PDISCPUSTATE pDis, unsigned *pcbInstr)
{
if (RT_FAILURE(rc))
{
Log(("EMInterpretDisasOne: Failed to convert %RTsel:%RGv (cpl=%d) - rc=%Rrc !!\n",
return rc;
}
}
/**
* Disassembles one instruction.
*
*
* @returns VBox status code.
* @retval VERR_INTERNAL_ERROR on DISCoreOneEx failure.
*
* @param pVM The VM handle.
* @param pVCpu The VMCPU handle.
* @param GCPtrInstr The flat address of the instruction.
* @param pCtxCore The context core (used to determine the cpu mode).
* @param pDis Where to return the parsed instruction info.
* @param pcbInstr Where to return the instruction size. (optional)
*/
VMMDECL(int) EMInterpretDisasOneEx(PVM pVM, PVMCPU pVCpu, RTGCUINTPTR GCPtrInstr, PCCPUMCTXCORE pCtxCore, PDISCPUSTATE pDis, unsigned *pcbInstr)
{
int rc;
#ifdef IN_RC
#else /* ring 0/3 */
if (RT_SUCCESS(rc))
{
}
else
{
{
if (rc == VERR_PAGE_TABLE_NOT_PRESENT)
return rc;
}
}
#endif
rc = DISCoreOneEx(GCPtrInstr, SELMGetCpuModeFromSelector(pVM, pCtxCore->eflags, pCtxCore->cs, (PCPUMSELREGHID)&pCtxCore->csHid),
EMReadBytes, &State,
if (RT_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 pVCpu The VMCPU 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 (!)
*/
VMMDECL(int) EMInterpretInstruction(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
if (RT_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 pVCpu The VMCPU handle.
* @param pDis 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).
* @param enmCodeType Code type (user/supervisor)
*
* @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)
*/
VMMDECL(int) EMInterpretInstructionCPUEx(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize, EMCODETYPE enmCodeType)
{
if (RT_SUCCESS(rc))
else
return rc;
}
/**
* Interpret a port I/O instruction.
*
* @returns VBox status code suitable for scheduling.
* @param pVM The VM handle.
* @param pVCpu The VMCPU handle.
* @param pCtxCore The context core. This will be updated on successful return.
* @param pDis The instruction to interpret.
* @param cbOp The size of the instruction.
* @remark This may raise exceptions.
*/
VMMDECL(VBOXSTRICTRC) EMInterpretPortIO(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, PDISCPUSTATE pDis, uint32_t cbOp)
{
/*
* Hand it on to IOM.
*/
#ifdef IN_RC
if (IOM_SUCCESS(rcStrict))
return rcStrict;
#else
AssertReleaseMsgFailed(("not implemented\n"));
return VERR_NOT_IMPLEMENTED;
#endif
}
DECLINLINE(int) emRamRead(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCPTR GCPtrSrc, uint32_t cb)
{
#ifdef IN_RC
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.
*/
#endif
}
DECLINLINE(int) emRamWrite(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, RTGCPTR GCPtrDst, const void *pvSrc, uint32_t cb)
{
/* Don't use MMGCRamWrite here as it does not respect zero pages, shared
pages or write monitored pages. */
}
/** Convert sel:addr to a flat GC address. */
DECLINLINE(RTGCPTR) emConvertToFlatAddr(PVM pVM, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pDis, POP_PARAMETER pParam, RTGCPTR pvAddr)
{
}
#if defined(VBOX_STRICT) || defined(LOG_ENABLED)
/**
* Get the mnemonic for the disassembled instruction.
*
* of limited space.
*/
{
{
case OP_XCHG: return "Xchg";
case OP_DEC: return "Dec";
case OP_INC: return "Inc";
case OP_POP: return "Pop";
case OP_OR: return "Or";
case OP_AND: return "And";
case OP_MOV: return "Mov";
case OP_INVLPG: return "InvlPg";
case OP_CPUID: return "CpuId";
case OP_MOV_CR: return "MovCRx";
case OP_MOV_DR: return "MovDRx";
case OP_LLDT: return "LLdt";
case OP_LGDT: return "LGdt";
case OP_LIDT: return "LIdt";
case OP_CLTS: return "Clts";
case OP_MONITOR: return "Monitor";
case OP_MWAIT: return "MWait";
case OP_RDMSR: return "Rdmsr";
case OP_WRMSR: return "Wrmsr";
case OP_ADD: return "Add";
case OP_ADC: return "Adc";
case OP_SUB: return "Sub";
case OP_SBB: return "Sbb";
case OP_RDTSC: return "Rdtsc";
case OP_STI: return "Sti";
case OP_CLI: return "Cli";
case OP_XADD: return "XAdd";
case OP_HLT: return "Hlt";
case OP_IRET: return "Iret";
case OP_MOVNTPS: return "MovNTPS";
case OP_STOSWD: return "StosWD";
case OP_WBINVD: return "WbInvd";
case OP_XOR: return "Xor";
case OP_BTR: return "Btr";
case OP_BTS: return "Bts";
case OP_BTC: return "Btc";
case OP_LMSW: return "Lmsw";
case OP_SMSW: return "Smsw";
default:
return "???";
}
}
#endif /* VBOX_STRICT || LOG_ENABLED */
/**
* XCHG instruction emulation.
*/
static int emInterpretXchg(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
/* Source to make DISQueryParamVal read the register value - ugly hack */
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
#ifdef IN_RC
if (TRPMHasTrap(pVCpu))
{
{
#endif
{
case PARMTYPE_IMMEDIATE: /* register type is translated to this one too */
break;
case PARMTYPE_ADDRESS:
if (RT_FAILURE(rc))
{
return VERR_EM_INTERPRETER;
}
break;
default:
AssertFailed();
return VERR_EM_INTERPRETER;
}
{
case PARMTYPE_ADDRESS:
if (RT_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 (RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
}
else
{
if (RT_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 (RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
}
else
{
if (RT_FAILURE(rc))
{
return VERR_EM_INTERPRETER;
}
}
return VINF_SUCCESS;
#ifdef IN_RC
}
}
#endif
return VERR_EM_INTERPRETER;
}
/**
* INC and DEC emulation.
*/
static int emInterpretIncDec(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize,
{
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
#ifdef IN_RC
if (TRPMHasTrap(pVCpu))
{
{
#endif
{
#ifdef IN_RC
/* Safety check (in theory it could cross a page boundary and fault there though) */
#endif
if (RT_FAILURE(rc))
{
return VERR_EM_INTERPRETER;
}
}
else
{
AssertFailed();
return VERR_EM_INTERPRETER;
}
/* Write result back */
if (RT_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_RC
}
}
#endif
return VERR_EM_INTERPRETER;
}
/**
* POP Emulation.
*/
static int emInterpretPop(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
#ifdef IN_RC
if (TRPMHasTrap(pVCpu))
{
{
#endif
/* Read stack value first */
if (SELMGetCpuModeFromSelector(pVM, pRegFrame->eflags, pRegFrame->ss, &pRegFrame->ssHid) == CPUMODE_16BIT)
return VERR_EM_INTERPRETER; /* No legacy 16 bits stuff here, please. */
/* Convert address; don't bother checking limits etc, as we only read here */
if (pStackVal == 0)
return VERR_EM_INTERPRETER;
if (RT_FAILURE(rc))
{
return VERR_EM_INTERPRETER;
}
{
/* pop [esp+xx] uses esp after the actual pop! */
)
EM_ASSERT_FAULT_RETURN(pParam1 == pvFault || (RTGCPTR)pRegFrame->esp == pvFault, VERR_EM_INTERPRETER);
if (RT_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_RC
}
}
#endif
return VERR_EM_INTERPRETER;
}
/**
*/
static int emInterpretOrXorAnd(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize,
{
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
#ifdef IN_RC
if (TRPMHasTrap(pVCpu))
{
{
#endif
{
{
AssertMsgFailed(("%s at %RGv parameter mismatch %d vs %d!!\n", emGetMnemonic(pDis), (RTGCPTR)pRegFrame->rip, pDis->param1.size, pDis->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 */
{
if (RT_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;
}
LogFlow(("emInterpretOrXorAnd %s %RGv %RX64 - %RX64 size %d (%d)\n", emGetMnemonic(pDis), pParam1, valpar1, valpar2, param2.size, param1.size));
/* 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 (RT_SUCCESS(rc))
{
/* All done! */
return VINF_SUCCESS;
}
#ifdef IN_RC
}
}
#endif
return VERR_EM_INTERPRETER;
}
/**
*/
static int emInterpretLockOrXorAnd(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault,
{
void *pvParam1;
#endif
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
{
("%s at %RGv parameter mismatch %d vs %d!!\n", emGetMnemonic(pDis), (RTGCPTR)pRegFrame->rip, pDis->param1.size, pDis->param2.size),
/* Or %Ev, Ib -> just a hack to save some space; the data width of the 1st parameter determines the real width */
}
#ifdef IN_RC
/* Safety check (in theory it could cross a page boundary and fault there though) */
#endif
/* Register and immediate data == PARMTYPE_IMMEDIATE */
/* The destination is always a virtual address */
/* Try emulate it with a one-shot #PF handler in place. (RC) */
RTGCUINTREG32 eflags = 0;
if (RT_FAILURE(rc))
{
Log(("%s %RGv imm%d=%RX64-> emulation failed due to page fault!\n", emGetMnemonic(pDis), GCPtrPar1, pDis->param2.size*8, ValPar2));
return VERR_EM_INTERPRETER;
}
/* 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;
}
/**
* ADD, ADC & SUB Emulation.
*/
static int emInterpretAddSub(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize,
{
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
#ifdef IN_RC
if (TRPMHasTrap(pVCpu))
{
{
#endif
{
{
AssertMsgFailed(("%s at %RGv parameter mismatch %d vs %d!!\n", emGetMnemonic(pDis), (RTGCPTR)pRegFrame->rip, pDis->param1.size, pDis->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 */
{
if (RT_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 (RT_SUCCESS(rc))
{
/* All done! */
return VINF_SUCCESS;
}
#ifdef IN_RC
}
}
#endif
return VERR_EM_INTERPRETER;
}
/**
* ADC Emulation.
*/
static int emInterpretAdc(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
else
}
/**
* BTR/C/S Emulation.
*/
static int emInterpretBitTest(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize,
{
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
#ifdef IN_RC
if (TRPMHasTrap(pVCpu))
{
{
#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;
}
Log2(("emInterpret%s: pvFault=%RGv pParam1=%RGv val2=%x\n", emGetMnemonic(pDis), pvFault, pParam1, valpar2));
if (RT_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 (RT_SUCCESS(rc))
{
/* All done! */
*pcbSize = 1;
return VINF_SUCCESS;
}
#ifdef IN_RC
}
}
#endif
return VERR_EM_INTERPRETER;
}
/**
* LOCK BTR/C/S Emulation.
*/
static int emInterpretLockBitTest(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault,
{
void *pvParam1;
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
/* The destination is always a virtual address */
return VERR_EM_INTERPRETER;
/* Register and immediate data == PARMTYPE_IMMEDIATE */
/* Adjust the parameters so what we're dealing with is a bit within the byte pointed to. */
ValPar2 &= 7;
#ifdef IN_RC
EM_ASSERT_FAULT_RETURN((RTGCPTR)((RTGCUINTPTR)GCPtrPar1 & ~(RTGCUINTPTR)3) == pvFault, VERR_EM_INTERPRETER);
#endif
Log2(("emInterpretLockBitTest %s: pvFault=%RGv GCPtrPar1=%RGv imm=%RX64\n", emGetMnemonic(pDis), pvFault, GCPtrPar1, ValPar2));
/* Try emulate it with a one-shot #PF handler in place. (RC) */
RTGCUINTREG32 eflags = 0;
if (RT_FAILURE(rc))
{
Log(("emInterpretLockBitTest %s: %RGv imm%d=%RX64 -> emulation failed due to page fault!\n",
return VERR_EM_INTERPRETER;
}
Log2(("emInterpretLockBitTest %s: GCPtrPar1=%RGv imm=%RX64 CF=%d\n", emGetMnemonic(pDis), GCPtrPar1, ValPar2, !!(eflags & X86_EFL_CF)));
/* 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))
*pcbSize = 1;
return VINF_SUCCESS;
}
/**
* MOV emulation.
*/
static int emInterpretMov(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
#ifdef IN_RC
if (TRPMHasTrap(pVCpu))
{
{
#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;
}
#ifdef LOG_ENABLED
LogFlow(("EMInterpretInstruction at %RGv: OP_MOV %RGv <- %RX64 (%d) &val64=%RHv\n", (RTGCPTR)pRegFrame->rip, pDest, val64, param2.size, &val64));
else
LogFlow(("EMInterpretInstruction at %08RX64: OP_MOV %RGv <- %08X (%d) &val64=%RHv\n", pRegFrame->rip, pDest, (uint32_t)val64, param2.size, &val64));
#endif
if (RT_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;
}
if (RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
/* Destination */
{
case PARMTYPE_REGISTER:
{
default:
return VERR_EM_INTERPRETER;
}
if (RT_FAILURE(rc))
return rc;
break;
default:
return VERR_EM_INTERPRETER;
}
#ifdef LOG_ENABLED
else
LogFlow(("EMInterpretInstruction: OP_MOV %RGv -> %08X (%d)\n", pSrc, (uint32_t)val64, param1.size));
#endif
}
return VINF_SUCCESS;
#ifdef IN_RC
}
#endif
return VERR_EM_INTERPRETER;
}
#ifndef IN_RC
/**
* [REP] STOSWD emulation
*/
static int emInterpretStosWD(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
int rc;
int offIncrement;
/* Don't support any but these three prefix bytes. */
return VERR_EM_INTERPRETER;
{
case CPUMODE_16BIT:
break;
case CPUMODE_32BIT:
break;
case CPUMODE_64BIT:
break;
default:
AssertFailed();
return VERR_EM_INTERPRETER;
}
{
case CPUMODE_16BIT:
cbSize = 2;
break;
case CPUMODE_32BIT:
cbSize = 4;
break;
case CPUMODE_64BIT:
cbSize = 8;
break;
default:
AssertFailed();
return VERR_EM_INTERPRETER;
}
{
LogFlow(("emInterpretStosWD dest=%04X:%RGv (%RGv) cbSize=%d\n", pRegFrame->es, GCOffset, GCDest, cbSize));
if (RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
/* Update (e/r)di. */
{
case CPUMODE_16BIT:
break;
case CPUMODE_32BIT:
break;
case CPUMODE_64BIT:
break;
default:
AssertFailed();
return VERR_EM_INTERPRETER;
}
}
else
{
if (!cTransfers)
return VINF_SUCCESS;
/*
* Do *not* try emulate cross page stuff here because we don't know what might
* be waiting for us on the subsequent pages. The caller has only asked us to
* ignore access handlers fro the current page.
* This also fends off big stores which would quickly kill PGMR0DynMap.
*/
|| cTransfers > PAGE_SIZE
{
Log(("STOSWD is crosses pages, chicken out to the recompiler; GCDest=%RGv cbSize=%#x offIncrement=%d cTransfers=%#x\n",
return VERR_EM_INTERPRETER;
}
LogFlow(("emInterpretStosWD dest=%04X:%RGv (%RGv) cbSize=%d cTransfers=%x DF=%d\n", pRegFrame->es, GCOffset, GCDest, cbSize, cTransfers, pRegFrame->eflags.Bits.u1DF));
/* Access verification first; we currently can't recover properly from traps inside this instruction */
cTransfers * cbSize,
if (rc != VINF_SUCCESS)
{
return VERR_EM_INTERPRETER;
}
/* REP case */
while (cTransfers)
{
if (RT_FAILURE(rc))
{
break;
}
GCOffset += offIncrement;
GCDest += offIncrement;
cTransfers--;
}
/* Update the registers. */
{
case CPUMODE_16BIT:
break;
case CPUMODE_32BIT:
break;
case CPUMODE_64BIT:
break;
default:
AssertFailed();
return VERR_EM_INTERPRETER;
}
}
return rc;
}
#endif /* !IN_RC */
/**
* [LOCK] CMPXCHG emulation.
*/
static int emInterpretCmpXchg(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
#endif
/* Source to make DISQueryParamVal read the register value - ugly hack */
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
{
case PARMTYPE_IMMEDIATE: /* register actually */
break;
default:
return VERR_EM_INTERPRETER;
}
void *pvParam1;
{
case PARMTYPE_ADDRESS:
break;
default:
return VERR_EM_INTERPRETER;
}
else
LogFlow(("%s %RGv rax=%RX64 %RX64 ZF=%d\n", emGetMnemonic(pDis), GCPtrPar1, pRegFrame->rax, valpar, !!(eflags & X86_EFL_ZF)));
/* 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;
}
/**
* [LOCK] CMPXCHG8B emulation.
*/
static int emInterpretCmpXchg8b(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
/* Source to make DISQueryParamVal read the register value - ugly hack */
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
void *pvParam1;
{
case PARMTYPE_ADDRESS:
break;
default:
return VERR_EM_INTERPRETER;
}
eflags = EMEmulateLockCmpXchg8b(pvParam1, &pRegFrame->eax, &pRegFrame->edx, pRegFrame->ebx, pRegFrame->ecx);
else
eflags = EMEmulateCmpXchg8b(pvParam1, &pRegFrame->eax, &pRegFrame->edx, pRegFrame->ebx, pRegFrame->ecx);
LogFlow(("%s %RGv=%08x eax=%08x ZF=%d\n", emGetMnemonic(pDis), pvParam1, pRegFrame->eax, !!(eflags & X86_EFL_ZF)));
/* Update guest's eflags and finish; note that *only* ZF is affected. */
| (eflags & (X86_EFL_ZF));
*pcbSize = 8;
return VINF_SUCCESS;
}
#ifdef IN_RC /** @todo test+enable for HWACCM as well. */
/**
* [LOCK] XADD emulation.
*/
static int emInterpretXAdd(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
void *pvParamReg2;
/* Source to make DISQueryParamVal read the register value - ugly hack */
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
#ifdef IN_RC
if (TRPMHasTrap(pVCpu))
{
{
#endif
void *pvParam1;
{
case PARMTYPE_ADDRESS:
GCPtrPar1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->param1, (RTRCUINTPTR)param1.val.val64);
#ifdef IN_RC
#endif
break;
default:
return VERR_EM_INTERPRETER;
}
else
LogFlow(("XAdd %RGv=%p reg=%08llx ZF=%d\n", GCPtrPar1, pvParam1, *(uint64_t *)pvParamReg2, !!(eflags & X86_EFL_ZF) ));
/* 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))
*pcbSize = cbParamReg2;
return VINF_SUCCESS;
#ifdef IN_RC
}
}
return VERR_EM_INTERPRETER;
#endif
}
#endif /* IN_RC */
#ifdef IN_RC
/**
* Interpret IRET (currently only to V86 code)
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pVCpu The VMCPU 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;
}
#endif /* IN_RC */
/**
* IRET Emulation.
*/
static int emInterpretIret(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
/* only allow direct calls to EMInterpretIret for now */
return VERR_EM_INTERPRETER;
}
/**
* WBINVD Emulation.
*/
static int emInterpretWbInvd(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
/* Nothing to do. */
return VINF_SUCCESS;
}
/**
* Interpret INVLPG
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pVCpu The VMCPU 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_RC
#endif
if ( rc == VINF_SUCCESS
return VINF_SUCCESS;
return rc;
}
/**
* INVLPG Emulation.
*/
static int emInterpretInvlPg(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
if(RT_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_RC
#endif
if ( rc == VINF_SUCCESS
return VINF_SUCCESS;
return rc;
}
/**
* Interpret CPUID given the parameters in the CPU context
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pVCpu The VMCPU handle.
* @param pRegFrame The register frame.
*
*/
{
/* cpuid clears the high dwords of the affected 64 bits registers. */
/* Note: operates the same in 64 and non-64 bits mode. */
CPUMGetGuestCpuId(pVCpu, iLeaf, &pRegFrame->eax, &pRegFrame->ebx, &pRegFrame->ecx, &pRegFrame->edx);
Log(("Emulate: CPUID %x -> %08x %08x %08x %08x\n", iLeaf, pRegFrame->eax, pRegFrame->ebx, pRegFrame->ecx, pRegFrame->edx));
return VINF_SUCCESS;
}
/**
* CPUID Emulation.
*/
static int emInterpretCpuId(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
return rc;
}
/**
* Interpret CRx read
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pVCpu The VMCPU handle.
* @param pRegFrame The register frame.
* @param DestRegGen General purpose register index (USE_REG_E**))
* @param SrcRegCRx CRx register index (USE_REG_CR*)
*
*/
VMMDECL(int) EMInterpretCRxRead(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, uint32_t DestRegGen, uint32_t SrcRegCrx)
{
else
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
return VERR_EM_INTERPRETER;
}
/**
* Interpret CLTS
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pVCpu The VMCPU handle.
*
*/
{
if (!(cr0 & X86_CR0_TS))
return VINF_SUCCESS;
}
/**
* CLTS Emulation.
*/
static int emInterpretClts(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
}
/**
* Update CRx
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pVCpu The VMCPU handle.
* @param pRegFrame The register frame.
* @param DestRegCRx CRx register index (USE_REG_CR*)
* @param val New CRx value
*
*/
static int emUpdateCRx(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, uint32_t DestRegCrx, uint64_t val)
{
/** @todo Clean up this mess. */
switch (DestRegCrx)
{
case USE_REG_CR0:
#ifdef IN_RC
/* CR0.WP and CR0.AM changes require a reschedule run in ring 3. */
return VERR_EM_INTERPRETER;
#endif
rc = VINF_SUCCESS;
{
/* global flush */
}
if (msrEFER & MSR_K6_EFER_LME)
{
if ( !(oldval & X86_CR0_PG)
&& (val & X86_CR0_PG))
{
/* Illegal to have an active 64 bits CS selector (AMD Arch. Programmer's Manual Volume 2: Table 14-5) */
{
AssertMsgFailed(("Illegal enabling of paging with CS.u1Long = 1!!\n"));
return VERR_EM_INTERPRETER; /* @todo generate #GP(0) */
}
/* Illegal to switch to long mode before activating PAE first (AMD Arch. Programmer's Manual Volume 2: Table 14-5) */
{
AssertMsgFailed(("Illegal enabling of paging with PAE disabled!!\n"));
return VERR_EM_INTERPRETER; /* @todo generate #GP(0) */
}
}
else
if ( (oldval & X86_CR0_PG)
&& !(val & X86_CR0_PG))
{
msrEFER &= ~MSR_K6_EFER_LMA;
/* @todo Do we need to cut off rip here? High dword of rip is undefined, so it shouldn't really matter. */
}
}
rc2 = PGMChangeMode(pVCpu, CPUMGetGuestCR0(pVCpu), CPUMGetGuestCR4(pVCpu), CPUMGetGuestEFER(pVCpu));
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 rc;
case USE_REG_CR4:
/* Illegal to disable PAE when long mode is active. (AMD Arch. Programmer's Manual Volume 2: Table 14-5) */
if ( (msrEFER & MSR_K6_EFER_LMA)
&& (oldval & X86_CR4_PAE)
&& !(val & X86_CR4_PAE))
{
return VERR_EM_INTERPRETER; /** @todo generate #GP(0) */
}
rc = VINF_SUCCESS;
{
/* global flush */
}
/* Feeling extremely lazy. */
# ifdef IN_RC
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))
!= (val & (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
rc2 = PGMChangeMode(pVCpu, CPUMGetGuestCR0(pVCpu), CPUMGetGuestCR4(pVCpu), CPUMGetGuestEFER(pVCpu));
case USE_REG_CR8:
return PDMApicSetTPR(pVCpu, val << 4); /* cr8 bits 3-0 correspond to bits 7-4 of the task priority mmio register. */
default:
AssertFailed();
case USE_REG_CR1: /* illegal op */
break;
}
return VERR_EM_INTERPRETER;
}
/**
* Interpret CRx write
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pVCpu The VMCPU handle.
* @param pRegFrame The register frame.
* @param DestRegCRx CRx register index (USE_REG_CR*)
* @param SrcRegGen General purpose register index (USE_REG_E**))
*
*/
VMMDECL(int) EMInterpretCRxWrite(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, uint32_t DestRegCrx, uint32_t SrcRegGen)
{
int rc;
{
}
else
{
}
if (RT_SUCCESS(rc))
return VERR_EM_INTERPRETER;
}
/**
* Interpret LMSW
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pVCpu The VMCPU handle.
* @param pRegFrame The register frame.
* @param u16Data LMSW source data.
*
*/
{
/* Only PE, MP, EM and TS can be changed; note that PE can't be cleared by this instruction. */
}
/**
* LMSW Emulation.
*/
static int emInterpretLmsw(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
{
case PARMTYPE_IMMEDIATE:
case PARMTYPE_ADDRESS:
return VERR_EM_INTERPRETER;
break;
default:
return VERR_EM_INTERPRETER;
}
}
#ifdef EM_EMULATE_SMSW
/**
* SMSW Emulation.
*/
static int emInterpretSmsw(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
{
case PARMTYPE_IMMEDIATE:
return VERR_EM_INTERPRETER;
break;
case PARMTYPE_ADDRESS:
{
/* Actually forced to 16 bits regardless of the operand size. */
return VERR_EM_INTERPRETER;
if (RT_FAILURE(rc))
{
return VERR_EM_INTERPRETER;
}
break;
}
default:
return VERR_EM_INTERPRETER;
}
return rc;
}
#endif
/**
* MOV CRx
*/
static int emInterpretMovCRx(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
if ((pDis->param1.flags == USE_REG_GEN32 || pDis->param1.flags == USE_REG_GEN64) && pDis->param2.flags == USE_REG_CR)
return EMInterpretCRxRead(pVM, pVCpu, pRegFrame, pDis->param1.base.reg_gen, pDis->param2.base.reg_ctrl);
if (pDis->param1.flags == USE_REG_CR && (pDis->param2.flags == USE_REG_GEN32 || pDis->param2.flags == USE_REG_GEN64))
return EMInterpretCRxWrite(pVM, pVCpu, pRegFrame, pDis->param1.base.reg_ctrl, pDis->param2.base.reg_gen);
return VERR_EM_INTERPRETER;
}
/**
* Interpret DRx write
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pVCpu The VMCPU handle.
* @param pRegFrame The register frame.
* @param DestRegDRx DRx register index (USE_REG_DR*)
* @param SrcRegGen General purpose register index (USE_REG_E**))
*
*/
VMMDECL(int) EMInterpretDRxWrite(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, uint32_t DestRegDrx, uint32_t SrcRegGen)
{
int rc;
{
}
else
{
}
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
return rc;
}
return VERR_EM_INTERPRETER;
}
/**
* Interpret DRx read
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pVCpu The VMCPU handle.
* @param pRegFrame The register frame.
* @param DestRegGen General purpose register index (USE_REG_E**))
* @param SrcRegDRx DRx register index (USE_REG_DR*)
*
*/
VMMDECL(int) EMInterpretDRxRead(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, uint32_t DestRegGen, uint32_t SrcRegDrx)
{
{
}
else
if (RT_SUCCESS(rc))
return VINF_SUCCESS;
return VERR_EM_INTERPRETER;
}
/**
* MOV DRx
*/
static int emInterpretMovDRx(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
int rc = VERR_EM_INTERPRETER;
if((pDis->param1.flags == USE_REG_GEN32 || pDis->param1.flags == USE_REG_GEN64) && pDis->param2.flags == USE_REG_DBG)
{
rc = EMInterpretDRxRead(pVM, pVCpu, pRegFrame, pDis->param1.base.reg_gen, pDis->param2.base.reg_dbg);
}
else
if(pDis->param1.flags == USE_REG_DBG && (pDis->param2.flags == USE_REG_GEN32 || pDis->param2.flags == USE_REG_GEN64))
{
rc = EMInterpretDRxWrite(pVM, pVCpu, pRegFrame, pDis->param1.base.reg_dbg, pDis->param2.base.reg_gen);
}
else
AssertMsgFailed(("Unexpected debug register move\n"));
return rc;
}
/**
* LLDT Emulation.
*/
static int emInterpretLLdt(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
if(RT_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;
}
#ifdef IN_RING0
/* Only for the VT-x real-mode emulation case. */
return VINF_SUCCESS;
#else
if (sel == 0)
{
if (CPUMGetHyperLDTR(pVCpu) == 0)
{
// this simple case is most frequent in Windows 2000 (31k - boot & shutdown)
return VINF_SUCCESS;
}
}
//still feeling lazy
return VERR_EM_INTERPRETER;
#endif
}
#ifdef IN_RING0
/**
*/
static int emInterpretLIGdt(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
/* Only for the VT-x real-mode emulation case. */
if(RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
{
case PARMTYPE_ADDRESS:
break;
default:
return VERR_EM_INTERPRETER;
}
else
return VINF_SUCCESS;
}
#endif
#ifdef IN_RC
/**
* STI Emulation.
*
* @remark the instruction following sti is guaranteed to be executed before any interrupts are dispatched
*/
static int emInterpretSti(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
if(!pGCState)
{
return VERR_EM_INTERPRETER;
}
return VINF_SUCCESS;
}
#endif /* IN_RC */
/**
* HLT Emulation.
*/
static int emInterpretHlt(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
return VINF_EM_HALT;
}
/**
* Interpret RDTSC
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pVCpu The VMCPU handle.
* @param pRegFrame The register frame.
*
*/
{
if (uCR4 & X86_CR4_TSD)
return VERR_EM_INTERPRETER; /* genuine #GP */
/* Same behaviour in 32 & 64 bits mode */
return VINF_SUCCESS;
}
/**
* Interpret RDTSCP
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pVCpu The VMCPU handle.
* @param pCtx The CPU context.
*
*/
{
{
AssertFailed();
return VERR_EM_INTERPRETER; /* genuine #UD */
}
if (uCR4 & X86_CR4_TSD)
return VERR_EM_INTERPRETER; /* genuine #GP */
/* Same behaviour in 32 & 64 bits mode */
/* Low dword of the TSC_AUX msr only. */
return VINF_SUCCESS;
}
/**
* RDTSC Emulation.
*/
static int emInterpretRdtsc(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
}
/**
* Interpret RDPMC
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pVCpu The VMCPU handle.
* @param pRegFrame The register frame.
*
*/
{
/* If X86_CR4_PCE is not set, then CPL must be zero. */
if ( !(uCR4 & X86_CR4_PCE)
{
return VERR_EM_INTERPRETER; /* genuine #GP */
}
/* Just return zero here; rather tricky to properly emulate this, especially as the specs are a mess. */
/* @todo We should trigger a #GP here if the cpu doesn't support the index in ecx. */
return VINF_SUCCESS;
}
/**
* RDPMC Emulation
*/
static int emInterpretRdpmc(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
}
/**
* MONITOR Emulation.
*/
{
{
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;
}
static int emInterpretMonitor(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
}
/**
* MWAIT Emulation.
*/
{
/* 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 */
/*
* CPUID.05H.ECX[0] defines support for power management extensions (eax)
* CPUID.05H.ECX[1] defines support for interrupts as break events for mwait even when IF=0
*/
{
return VERR_EM_INTERPRETER; /* illegal value. */
}
{
{
Log(("EMInterpretMWait: unsupported X86_CPUID_MWAIT_ECX_BREAKIRQIF0 -> recompiler\n"));
return VERR_EM_INTERPRETER; /* illegal value. */
}
}
else
/** @todo not completely correct */
return VINF_EM_HALT;
}
static int emInterpretMWait(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
}
#ifdef LOG_ENABLED
{
switch (uMsr)
{
case MSR_IA32_APICBASE:
return "MSR_IA32_APICBASE";
case MSR_IA32_CR_PAT:
return "MSR_IA32_CR_PAT";
case MSR_IA32_SYSENTER_CS:
return "MSR_IA32_SYSENTER_CS";
case MSR_IA32_SYSENTER_EIP:
return "MSR_IA32_SYSENTER_EIP";
case MSR_IA32_SYSENTER_ESP:
return "MSR_IA32_SYSENTER_ESP";
case MSR_K6_EFER:
return "MSR_K6_EFER";
case MSR_K8_SF_MASK:
return "MSR_K8_SF_MASK";
case MSR_K6_STAR:
return "MSR_K6_STAR";
case MSR_K8_LSTAR:
return "MSR_K8_LSTAR";
case MSR_K8_CSTAR:
return "MSR_K8_CSTAR";
case MSR_K8_FS_BASE:
return "MSR_K8_FS_BASE";
case MSR_K8_GS_BASE:
return "MSR_K8_GS_BASE";
case MSR_K8_KERNEL_GS_BASE:
return "MSR_K8_KERNEL_GS_BASE";
case MSR_K8_TSC_AUX:
return "MSR_K8_TSC_AUX";
case MSR_IA32_BIOS_SIGN_ID:
return "Unsupported MSR_IA32_BIOS_SIGN_ID";
case MSR_IA32_PLATFORM_ID:
return "Unsupported MSR_IA32_PLATFORM_ID";
case MSR_IA32_BIOS_UPDT_TRIG:
return "Unsupported MSR_IA32_BIOS_UPDT_TRIG";
case MSR_IA32_TSC:
return "MSR_IA32_TSC";
case MSR_IA32_MISC_ENABLE:
return "Unsupported MSR_IA32_MISC_ENABLE";
case MSR_IA32_MTRR_CAP:
return "Unsupported MSR_IA32_MTRR_CAP";
case MSR_IA32_MCP_CAP:
return "Unsupported MSR_IA32_MCP_CAP";
case MSR_IA32_MCP_STATUS:
return "Unsupported MSR_IA32_MCP_STATUS";
case MSR_IA32_MCP_CTRL:
return "Unsupported MSR_IA32_MCP_CTRL";
case MSR_IA32_MTRR_DEF_TYPE:
return "Unsupported MSR_IA32_MTRR_DEF_TYPE";
case MSR_K7_EVNTSEL0:
return "Unsupported MSR_K7_EVNTSEL0";
case MSR_K7_EVNTSEL1:
return "Unsupported MSR_K7_EVNTSEL1";
case MSR_K7_EVNTSEL2:
return "Unsupported MSR_K7_EVNTSEL2";
case MSR_K7_EVNTSEL3:
return "Unsupported MSR_K7_EVNTSEL3";
case MSR_IA32_MC0_CTL:
return "Unsupported MSR_IA32_MC0_CTL";
case MSR_IA32_MC0_STATUS:
return "Unsupported MSR_IA32_MC0_STATUS";
case MSR_IA32_PERFEVTSEL0:
return "Unsupported MSR_IA32_PERFEVTSEL0";
case MSR_IA32_PERFEVTSEL1:
return "Unsupported MSR_IA32_PERFEVTSEL1";
case MSR_IA32_PERF_STATUS:
return "MSR_IA32_PERF_STATUS";
case MSR_IA32_PLATFORM_INFO:
return "MSR_IA32_PLATFORM_INFO";
case MSR_IA32_PERF_CTL:
return "Unsupported MSR_IA32_PERF_CTL";
case MSR_K7_PERFCTR0:
return "Unsupported MSR_K7_PERFCTR0";
case MSR_K7_PERFCTR1:
return "Unsupported MSR_K7_PERFCTR1";
case MSR_K7_PERFCTR2:
return "Unsupported MSR_K7_PERFCTR2";
case MSR_K7_PERFCTR3:
return "Unsupported MSR_K7_PERFCTR3";
case MSR_IA32_PMC0:
return "Unsupported MSR_IA32_PMC0";
case MSR_IA32_PMC1:
return "Unsupported MSR_IA32_PMC1";
case MSR_IA32_PMC2:
return "Unsupported MSR_IA32_PMC2";
case MSR_IA32_PMC3:
return "Unsupported MSR_IA32_PMC3";
}
return "Unknown MSR";
}
#endif /* LOG_ENABLED */
/**
* Interpret RDMSR
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pVCpu The VMCPU handle.
* @param pRegFrame The register frame.
*/
{
/** @todo According to the Intel manuals, there's a REX version of RDMSR that is slightly different.
* That version clears the high dwords of both RDX & RAX */
/* Get the current privilege level. */
return VERR_EM_INTERPRETER; /* supervisor only */
{
return VERR_EM_INTERPRETER;
}
LogFlow(("EMInterpretRdmsr %s (%x) -> %RX64\n", emMSRtoString(pRegFrame->ecx), pRegFrame->ecx, uValue));
return rc;
}
/**
* RDMSR Emulation.
*/
static int emInterpretRdmsr(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
/* Note: The Intel manual claims there's a REX version of RDMSR that's slightly
different, so we play safe by completely disassembling the instruction. */
}
/**
* Interpret WRMSR
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pVCpu The VMCPU handle.
* @param pRegFrame The register frame.
*/
{
/* Check the current privilege level, this instruction is supervisor only. */
return VERR_EM_INTERPRETER; /** @todo raise \#GP(0) */
if (rc != VINF_SUCCESS)
{
return VERR_EM_INTERPRETER;
}
return rc;
}
/**
* WRMSR Emulation.
*/
static int emInterpretWrmsr(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
}
/**
* Internal worker.
* @copydoc EMInterpretInstructionCPU
*/
DECLINLINE(int) emInterpretInstructionCPU(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault,
{
*pcbSize = 0;
if (enmCodeType == EMCODETYPE_SUPERVISOR)
{
/*
* 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;
}
}
else
Log2(("emInterpretInstructionCPU allowed to interpret user-level code!!\n"));
#ifdef IN_RC
)
)
#else
)
)
)
#endif
{
//Log(("EMInterpretInstruction: wrong prefix!!\n"));
return VERR_EM_INTERPRETER;
}
#if HC_ARCH_BITS == 32
/*
* Unable to emulate most >4 bytes accesses in 32 bits mode.
* Whitelisted instructions are safe.
*/
{
&& uOpCode != OP_CMPXCHG8B
/** @todo OP_BTS or is that a different kind of failure? */
# endif
)
{
# ifdef VBOX_WITH_STATISTICS
{
default:
break;
}
# endif /* VBOX_WITH_STATISTICS */
return VERR_EM_INTERPRETER;
}
}
#endif
int rc;
#if (defined(VBOX_STRICT) || defined(LOG_ENABLED))
#endif
{
/*
* Macros for generating the right case statements.
*/
case opcode:\
else \
if (RT_SUCCESS(rc)) \
else \
return rc
case opcode:\
if (RT_SUCCESS(rc)) \
else \
return rc
case opcode:\
if (RT_SUCCESS(rc)) \
else \
return rc
case opcode:\
if (RT_SUCCESS(rc)) \
else \
return rc
case opcode: STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Failed##Instr)); return VERR_EM_INTERPRETER;
/*
* The actual case statements.
*/
#ifndef IN_RC
#endif
#ifdef IN_RING0
#endif
#ifdef EM_EMULATE_SMSW
#endif
#ifdef IN_RC
#endif
#ifdef VBOX_WITH_STATISTICS
# ifndef IN_RC
# endif
#endif
default:
return VERR_EM_INTERPRETER;
} /* switch (opcode) */
AssertFailed();
return VERR_INTERNAL_ERROR;
}
/**
* Sets the PC for which interrupts should be inhibited.
*
* @param pVCpu The VMCPU 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 pVCpu The VMCPU handle.
*
*/
{
}
/**
* Locks REM execution to a single VCpu
*
* @param pVM VM handle.
*/
{
return; /* early init */
}
/**
* Unlocks REM execution
*
* @param pVM VM handle.
*/
{
return; /* early init */
}
/**
* Check if this VCPU currently owns the REM lock.
*
* @param pVM The VM to operate on.
*/
{
}
/**
* Try to acquire the REM lock.
*
* @returns VBox status code
* @param pVM The VM to operate on.
*/
{
}
/**
* Determine if we should continue after encountering a hlt or mwait instruction
*
* @returns boolean
* @param pVCpu The VMCPU to operate on.
* @param pCtx Current CPU context
*/
{
|| ((pVCpu->em.s.mwait.fWait & (EMMWAIT_FLAG_ACTIVE | EMMWAIT_FLAG_BREAKIRQIF0)) == (EMMWAIT_FLAG_ACTIVE | EMMWAIT_FLAG_BREAKIRQIF0)))
{
}
return false;
}