EMAll.cpp revision 6406e7a354eb585e5503a926d765cdf98ff41f03
/* $Id$ */
/** @file
* EM - Execution Monitor(/Manager) - All contexts
*/
/*
* 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 *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_EM
#include "EMInternal.h"
#include <VBox/disopcode.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/*******************************************************************************
* 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.
*/
{
}
/**
* Flushes the REM translation blocks the next time we execute code there.
*
* @param pVM The VM handle.
*/
{
Log(("EMFlushREMTBs\n"));
}
#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 cb Number of bytes to read
* @param dwUserdata Callback specific user data (pCpu)
*
*/
{
#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)
{
if (VBOX_FAILURE(rc))
{
Log(("EMInterpretDisasOne: Failed to convert %RTsel:%VGv (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, SELMGetCpuModeFromSelector(pVM, pCtxCore->eflags, pCtxCore->cs, (PCPUMSELREGHID)&pCtxCore->csHid),
#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)
{
if (VBOX_SUCCESS(rc))
{
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)
{
}
#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_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_ADC: return "Adc";
case OP_BTC: return "Btc";
case OP_RDTSC: return "Rdtsc";
case OP_STI: return "Sti";
case OP_XADD: return "XAdd";
case OP_HLT: return "Hlt";
case OP_IRET: return "Iret";
case OP_CMPXCHG: return "CmpXchg";
case OP_CMPXCHG8B: return "CmpXchg8b";
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";
default:
return "???";
}
}
#endif
/**
* 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 */
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 (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:%08x\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 LOG_ENABLED
const char *pszInstr;
pszInstr = "Xor";
pszInstr = "Or";
pszInstr = "And";
else
pszInstr = "OrXorAnd??";
#endif
#ifdef IN_GC
if (TRPMHasTrap(pVM))
{
{
#endif
{
{
AssertMsgFailed(("%s at %VGv parameter mismatch %d vs %d!!\n", pszInstr, pRegFrame->rip, 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->rip, 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;
}
LogFlow(("emInterpretOrXorAnd %s %VGv %RX64 - %RX64 size %d (%d)\n", pszInstr, 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 (VBOX_SUCCESS(rc))
{
/* All done! */
return VINF_SUCCESS;
}
#ifdef IN_GC
}
}
#endif
return VERR_EM_INTERPRETER;
}
/**
*/
static int emInterpretLockOrXorAnd(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault,
{
void *pvParam1;
if(VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
if(VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
{
("%s at %VGv parameter mismatch %d vs %d!!\n", emGetMnemonic(pCpu), pRegFrame->rip, pCpu->param1.size, pCpu->param2.size),
/* 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
#else
if (VBOX_FAILURE(rc))
{
return VERR_EM_INTERPRETER;
}
#endif
# ifdef IN_GC
/* Safety check (in theory it could cross a page boundary and fault there though) */
AssertMsgReturn(GCPtrPar1 == pvFault, ("eip=%VGv, GCPtrPar1=%VGv pvFault=%VGv\n", pRegFrame->rip, GCPtrPar1, pvFault), VERR_EM_INTERPRETER);
# endif
/* Register and immediate data == PARMTYPE_IMMEDIATE */
/* Try emulate it with a one-shot #PF handler in place. */
RTGCUINTREG32 eflags = 0;
#ifdef IN_GC
#endif
#ifdef IN_GC
#endif
if (RT_FAILURE(rc))
{
Log(("%s %VGv imm%d=%RX64-> emulation failed due to page fault!\n", emGetMnemonic(pCpu), GCPtrPar1, pCpu->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, 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 LOG_ENABLED
const char *pszInstr;
pszInstr = "Sub";
pszInstr = "Add";
pszInstr = "Adc";
else
pszInstr = "AddSub??";
#endif
#ifdef IN_GC
if (TRPMHasTrap(pVM))
{
{
#endif
{
{
AssertMsgFailed(("%s at %VGv parameter mismatch %d vs %d!!\n", pszInstr, pRegFrame->rip, 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 LOG_ENABLED
const char *pszInstr;
pszInstr = "Btr";
pszInstr = "Bts";
pszInstr = "Btc";
else
pszInstr = "Bit??";
#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;
}
/**
* LOCK BTR/C/S Emulation.
*/
static int emInterpretLockBitTest(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault,
{
void *pvParam1;
if(VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
if(VBOX_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_GC
#else
if (VBOX_FAILURE(rc))
{
return VERR_EM_INTERPRETER;
}
#endif
Log2(("emInterpretLockBitTest %s: pvFault=%VGv GCPtrPar1=%VGv imm=%RX64\n", emGetMnemonic(pCpu), pvFault, GCPtrPar1, ValPar2));
#ifdef IN_GC
#endif
/* Try emulate it with a one-shot #PF handler in place. */
RTGCUINTREG32 eflags = 0;
#ifdef IN_GC
#endif
#ifdef IN_GC
#endif
if (RT_FAILURE(rc))
{
Log(("emInterpretLockBitTest %s: %VGv imm%d=%RX64 -> emulation failed due to page fault!\n",
return VERR_EM_INTERPRETER;
}
Log2(("emInterpretLockBitTest %s: GCPtrPar1=%VGv imm=%VX64 CF=%d\n", emGetMnemonic(pCpu), 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, 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;
}
#ifdef LOG_ENABLED
LogFlow(("EMInterpretInstruction at %VGv: OP_MOV %VGv <- %RX64 (%d) &val32=%VHv\n", pRegFrame->rip, pDest, val64, param2.size, &val64));
else
LogFlow(("EMInterpretInstruction at %VGv: OP_MOV %VGv <- %08X (%d) &val32=%VHv\n", pRegFrame->rip, pDest, (uint32_t)val64, param2.size, &val64));
#endif
#if 0 /* CSAM/PATM translates aliases which causes this to incorrectly trigger. See #2609 and #1498. */
#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->rip, pDest, pvFault), VERR_EM_INTERPRETER);
#endif
#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;
}
#ifdef LOG_ENABLED
else
LogFlow(("EMInterpretInstruction: OP_MOV %VGv -> %08X (%d)\n", pSrc, (uint32_t)val64, param1.size));
#endif
}
return VINF_SUCCESS;
#ifdef IN_GC
}
#endif
return VERR_EM_INTERPRETER;
}
#ifndef IN_GC
/*
* [REP] STOSWD emulation
*
*/
static int emInterpretStosWD(PVM pVM, PDISCPUSTATE pCpu, 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:%VGv (%VGv) cbSize=%d\n", pRegFrame->es, GCOffset, GCDest, cbSize));
if (VBOX_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;
LogFlow(("emInterpretStosWD dest=%04X:%VGv (%VGv) 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 */
rc = PGMVerifyAccess(pVM, GCDest - (offIncrement > 0) ? 0 : ((cTransfers-1) * cbSize), cTransfers * cbSize, X86_PTE_RW | X86_PTE_US);
if (rc != VINF_SUCCESS)
{
return VERR_EM_INTERPRETER;
}
/* REP case */
while (cTransfers)
{
if (VBOX_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
/*
* [LOCK] CMPXCHG emulation.
*/
#ifndef IN_GC
static int emInterpretCmpXchg(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
#ifdef LOG_ENABLED
const char *pszInstr;
pszInstr = "Lock CmpXchg";
else
pszInstr = "CmpXchg";
#endif
/* 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;
void *pvParam1;
{
case PARMTYPE_ADDRESS:
if (VBOX_FAILURE(rc))
{
return VERR_EM_INTERPRETER;
}
break;
default:
return VERR_EM_INTERPRETER;
}
{
case PARMTYPE_IMMEDIATE: /* register actually */
break;
default:
return VERR_EM_INTERPRETER;
}
else
LogFlow(("%s %VGv rax=%RX64 %RX64 ZF=%d\n", pszInstr, 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;
}
#else
static int emInterpretCmpXchg(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
#ifdef LOG_ENABLED
const char *pszInstr;
pszInstr = "Lock CmpXchg";
else
pszInstr = "CmpXchg";
#endif
/* 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))
{
{
{
case PARMTYPE_ADDRESS:
pParam1 = (RTRCPTR)emConvertToFlatAddr(pVM, pRegFrame, pCpu, &pCpu->param1, (RTGCPTR)(RTRCUINTPTR)pParam1);
/* Safety check (in theory it could cross a page boundary and fault there though) */
AssertMsgReturn(pParam1 == (RTRCPTR)pvFault, ("eip=%VGv pParam1=%VRv pvFault=%VGv\n", pRegFrame->rip, pParam1, pvFault), VERR_EM_INTERPRETER);
break;
default:
return VERR_EM_INTERPRETER;
}
{
case PARMTYPE_IMMEDIATE: /* register actually */
break;
default:
return VERR_EM_INTERPRETER;
}
else
if (VBOX_FAILURE(rc))
{
Log(("%s %VGv eax=%08x %08x -> emulation failed due to page fault!\n", pszInstr, pParam1, pRegFrame->eax, valpar));
return VERR_EM_INTERPRETER;
}
LogFlow(("%s %VRv eax=%08x %08x ZF=%d\n", pszInstr, pParam1, pRegFrame->eax, 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;
}
}
return VERR_EM_INTERPRETER;
}
/*
* [LOCK] CMPXCHG8B emulation.
*/
static int emInterpretCmpXchg8b(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
#ifdef LOG_ENABLED
const char *pszInstr;
pszInstr = "Lock CmpXchg8b";
else
pszInstr = "CmpXchg8b";
#endif
/* Source to make DISQueryParamVal read the register value - ugly hack */
if(VBOX_FAILURE(rc))
return VERR_EM_INTERPRETER;
if (TRPMHasTrap(pVM))
{
{
{
case PARMTYPE_ADDRESS:
pParam1 = (RTRCPTR)emConvertToFlatAddr(pVM, pRegFrame, pCpu, &pCpu->param1, (RTGCPTR)(RTRCUINTPTR)pParam1);
/* Safety check (in theory it could cross a page boundary and fault there though) */
AssertMsgReturn(pParam1 == (RTRCPTR)pvFault, ("eip=%VGv pParam1=%VRv pvFault=%VGv\n", pRegFrame->rip, pParam1, pvFault), VERR_EM_INTERPRETER);
break;
default:
return VERR_EM_INTERPRETER;
}
rc = EMGCEmulateLockCmpXchg8b(pParam1, &pRegFrame->eax, &pRegFrame->edx, pRegFrame->ebx, pRegFrame->ecx, &eflags);
else
rc = EMGCEmulateCmpXchg8b(pParam1, &pRegFrame->eax, &pRegFrame->edx, pRegFrame->ebx, pRegFrame->ecx, &eflags);
if (VBOX_FAILURE(rc))
{
Log(("%s %VGv=%08x eax=%08x -> emulation failed due to page fault!\n", pszInstr, pParam1, pRegFrame->eax));
return VERR_EM_INTERPRETER;
}
LogFlow(("%s %VGv=%08x eax=%08x ZF=%d\n", pszInstr, pParam1, 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;
}
}
return VERR_EM_INTERPRETER;
}
#endif
/*
* [LOCK] XADD emulation.
*/
#ifdef IN_GC
static int emInterpretXAdd(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))
{
{
{
case PARMTYPE_ADDRESS:
pParam1 = (RTRCPTR)emConvertToFlatAddr(pVM, pRegFrame, pCpu, &pCpu->param1, (RTGCPTR)(RTRCUINTPTR)pParam1);
/* Safety check (in theory it could cross a page boundary and fault there though) */
AssertMsgReturn(pParam1 == (RTRCPTR)pvFault, ("eip=%VGv pParam1=%VRv pvFault=%VGv\n", pRegFrame->rip, pParam1, pvFault), VERR_EM_INTERPRETER);
break;
default:
return VERR_EM_INTERPRETER;
}
else
if (VBOX_FAILURE(rc))
{
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;
}
}
return VERR_EM_INTERPRETER;
}
#endif
#ifdef IN_GC
/**
* 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;
}
#endif
/**
* 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.
*
*/
{
/* Note: operates the same in 64 and non-64 bits mode. */
CPUMGetGuestCpuId(pVM, pRegFrame->eax, &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;
}
static int emInterpretCpuId(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
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)
{
int rc;
if (SrcRegCrx == USE_REG_CR8)
{
val64 = 0;
}
else
{
}
else
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;
}
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)
{
int rc;
/** @todo Clean up this mess. */
{
}
else
{
}
if (VBOX_SUCCESS(rc))
{
switch (DestRegCrx)
{
case USE_REG_CR0:
#ifdef IN_GC
/* CR0.WP and CR0.AM changes require a reschedule run in ring 3. */
return VERR_EM_INTERPRETER;
#endif
{
/* 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. */
}
}
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:
/* 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) */
}
{
/* global flush */
}
# ifdef IN_GC
/* 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))
!= (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
case USE_REG_CR8:
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)
{
if ((pCpu->param1.flags == USE_REG_GEN32 || pCpu->param1.flags == USE_REG_GEN64) && pCpu->param2.flags == USE_REG_CR)
if (pCpu->param1.flags == USE_REG_CR && (pCpu->param2.flags == USE_REG_GEN32 || pCpu->param2.flags == USE_REG_GEN64))
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)
{
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 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)
{
{
}
else
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;
if((pCpu->param1.flags == USE_REG_GEN32 || pCpu->param1.flags == USE_REG_GEN64) && pCpu->param2.flags == USE_REG_DBG)
{
}
else
if(pCpu->param1.flags == USE_REG_DBG && (pCpu->param2.flags == USE_REG_GEN32 || pCpu->param2.flags == USE_REG_GEN64))
{
}
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;
}
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 */
/* Same behaviour in 32 & 64 bits mode */
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;
}
#ifdef LOG_ENABLED
static const char *emMSRtoString(unsigned uMsr)
{
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_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 "Unsupported MSR_IA32_TSC";
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";
}
return "Unknown MSR";
}
#endif
/**
* Interpret RDMSR
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pRegFrame The register frame.
*
*/
{
int rc;
/** @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. */
if (cpl != 0)
return VERR_EM_INTERPRETER; /* supervisor only */
if (!(u32Features & X86_CPUID_FEATURE_EDX_MSR))
return VERR_EM_INTERPRETER; /* not supported */
{
case MSR_IA32_APICBASE:
break;
case MSR_IA32_CR_PAT:
break;
case MSR_IA32_SYSENTER_CS:
break;
case MSR_IA32_SYSENTER_EIP:
break;
case MSR_IA32_SYSENTER_ESP:
break;
case MSR_K6_EFER:
break;
case MSR_K8_SF_MASK:
break;
case MSR_K6_STAR:
break;
case MSR_K8_LSTAR:
break;
case MSR_K8_CSTAR:
break;
case MSR_K8_FS_BASE:
break;
case MSR_K8_GS_BASE:
break;
case MSR_K8_KERNEL_GS_BASE:
break;
#if 0 /*def IN_RING0 */
case MSR_IA32_PLATFORM_ID:
case MSR_IA32_BIOS_SIGN_ID:
{
/* Available since the P6 family. VT-x implies that this feature is present. */
else
break;
}
/* no break */
#endif
default:
/* We should actually trigger a #GP here, but don't as that might cause more trouble. */
val = 0;
break;
}
Log(("EMInterpretRdmsr %s (%x) -> val=%VX64\n", emMSRtoString(pRegFrame->ecx), pRegFrame->ecx, val));
return VINF_SUCCESS;
}
/**
* RDMSR Emulation.
*/
static int emInterpretRdmsr(PVM pVM, PDISCPUSTATE pCpu, 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 pRegFrame The register frame.
*
*/
{
int rc;
/* Note: works the same in 32 and 64 bits modes. */
/* Get the current privilege level. */
if (cpl != 0)
return VERR_EM_INTERPRETER; /* supervisor only */
if (!(u32Features & X86_CPUID_FEATURE_EDX_MSR))
return VERR_EM_INTERPRETER; /* not supported */
{
case MSR_IA32_APICBASE:
break;
case MSR_IA32_CR_PAT:
break;
case MSR_IA32_SYSENTER_CS:
break;
case MSR_IA32_SYSENTER_EIP:
break;
case MSR_IA32_SYSENTER_ESP:
break;
case MSR_K6_EFER:
{
/* Filter out those bits the guest is allowed to change. (e.g. LMA is read-only) */
uMask |= MSR_K6_EFER_NXE;
uMask |= MSR_K6_EFER_LME;
uMask |= MSR_K6_EFER_SCE;
/* Check for illegal MSR_K6_EFER_LME transitions: not allowed to change LME if paging is enabled. (AMD Arch. Programmer's Manual Volume 2: Table 14-5) */
{
AssertMsgFailed(("Illegal MSR_K6_EFER_LME change: paging is enabled!!\n"));
return VERR_EM_INTERPRETER; /* @todo generate #GP(0) */
}
/* There are a few more: e.g. MSR_K6_EFER_LMSLE */
AssertMsg(!(val & ~(MSR_K6_EFER_NXE|MSR_K6_EFER_LME|MSR_K6_EFER_LMA /* ignored anyway */ |MSR_K6_EFER_SCE|MSR_K6_EFER_FFXSR)), ("Unexpected value %RX64\n", val));
/* AMD64 Achitecture Programmer's Manual: 15.15 TLB Control; flush the TLB if MSR_K6_EFER_NXE, MSR_K6_EFER_LME or MSR_K6_EFER_LMA are changed. */
if ((oldval & (MSR_K6_EFER_NXE|MSR_K6_EFER_LME|MSR_K6_EFER_LMA)) != (pCtx->msrEFER & (MSR_K6_EFER_NXE|MSR_K6_EFER_LME|MSR_K6_EFER_LMA)))
break;
}
case MSR_K8_SF_MASK:
break;
case MSR_K6_STAR:
break;
case MSR_K8_LSTAR:
break;
case MSR_K8_CSTAR:
break;
case MSR_K8_FS_BASE:
break;
case MSR_K8_GS_BASE:
break;
case MSR_K8_KERNEL_GS_BASE:
break;
default:
/* We should actually trigger a #GP here, but don't as that might cause more trouble. */
break;
}
return VINF_SUCCESS;
}
/**
* WRMSR Emulation.
*/
static int emInterpretWrmsr(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
{
}
/**
* 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;
#if (defined(VBOX_STRICT) || defined(LOG_ENABLED))
#endif
{
case opcode:\
else \
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;
#ifndef IN_GC
#endif
#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.
*
*/
{
}