TRPMAll.cpp revision e64031e20c39650a7bc902a3e1aba613b9415dee
/* $Id$ */
/** @file
* TRPM - Trap Monitor - Any Context.
*/
/*
* 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_TRPM
#include "TRPMInternal.h"
/**
* If no trap is active active an error code is returned.
*
* @returns VBox status code.
* @param pVCpu VMCPU handle.
* @param pu8TrapNo Where to store the trap number.
* @param pEnmType Where to store the trap type
*/
{
/*
* Check if we have a trap at present.
*/
{
if (pu8TrapNo)
if (pEnmType)
return VINF_SUCCESS;
}
return VERR_TRPM_NO_ACTIVE_TRAP;
}
/**
* Gets the trap number for the current trap.
*
* The caller is responsible for making sure there is an active trap which
* takes an error code when making this request.
*
* @returns The current trap number.
* @param pVCpu VMCPU handle.
*/
{
}
/**
* Gets the error code for the current trap.
*
* The caller is responsible for making sure there is an active trap which
* takes an error code when making this request.
*
* @returns Error code.
* @param pVCpu VMCPU handle.
*/
{
#ifdef VBOX_STRICT
{
case 0x0a:
case 0x0b:
case 0x0c:
case 0x0d:
case 0x0e:
case 0x11:
case 0x08:
break;
default:
break;
}
#endif
}
/**
* Gets the fault address for the current trap.
*
* The caller is responsible for making sure there is an active trap 0x0e when
* making this request.
*
* @returns Fault address associated with the trap.
* @param pVCpu VMCPU handle.
*/
{
}
/**
*
* The caller is responsible for making sure there is an active trap
* when making this request.
*
* @returns VBox status code.
* @param pVCpu VMCPU handle.
*/
{
/*
* Cannot reset non-existing trap!
*/
{
AssertMsgFailed(("No active trap!\n"));
return VERR_TRPM_NO_ACTIVE_TRAP;
}
/*
* Reset it.
*/
return VINF_SUCCESS;
}
/**
*
* The caller is responsible for making sure there is no active trap
* when making this request.
*
* @returns VBox status code.
* @param pVCpu VMCPU handle.
* @param u8TrapNo The trap vector to assert.
* @param enmType Trap type.
*/
{
/*
* Cannot assert a trap when one is already active.
*/
{
return VERR_TRPM_ACTIVE_TRAP;
}
return VINF_SUCCESS;
}
/**
* Sets the error code of the current trap.
* (This function is for use in trap handlers and such.)
*
* The caller is responsible for making sure there is an active trap
* which takes an errorcode when making this request.
*
* @param pVCpu VMCPU handle.
* @param uErrorCode The new error code.
*/
{
#ifdef VBOX_STRICT
{
case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e:
AssertMsg(uErrorCode != ~(RTGCUINT)0, ("Invalid uErrorCode=%#x u8TrapNo=%d\n", uErrorCode, pVCpu->trpm.s.uActiveVector));
break;
case 0x11: case 0x08:
AssertMsg(uErrorCode == 0, ("Invalid uErrorCode=%#x u8TrapNo=%d\n", uErrorCode, pVCpu->trpm.s.uActiveVector));
break;
default:
AssertMsg(uErrorCode == ~(RTGCUINT)0, ("Invalid uErrorCode=%#x u8TrapNo=%d\n", uErrorCode, pVCpu->trpm.s.uActiveVector));
break;
}
#endif
}
/**
* Sets the error code of the current trap.
* (This function is for use in trap handlers and such.)
*
* The caller is responsible for making sure there is an active trap 0e
* when making this request.
*
* @param pVCpu VMCPU handle.
* @param uCR2 The new fault address (cr2 register).
*/
{
}
/**
* interrupt or not.
*
* The caller is responsible for making sure there is an active trap
* when making this request.
*
* @returns true if software interrupt, false if not.
*
* @param pVCpu VMCPU handle.
*/
{
}
/**
* Check if there is an active trap.
*
* @returns true if trap active, false if not.
* @param pVCpu VMCPU handle.
*/
{
}
/**
* If no trap is active active an error code is returned.
*
* @returns VBox status code.
* @param pVCpu VMCPU handle.
* @param pu8TrapNo Where to store the trap number.
* @param pEnmType Where to store the trap type
* @param puErrorCode Where to store the error code associated with some traps.
* ~0U is stored if the trap has no error code.
* @param puCR2 Where to store the CR2 associated with a trap 0E.
*/
VMMDECL(int) TRPMQueryTrapAll(PVMCPU pVCpu, uint8_t *pu8TrapNo, TRPMEVENT *pEnmType, PRTGCUINT puErrorCode, PRTGCUINTPTR puCR2)
{
/*
* Check if we have a trap at present.
*/
return VERR_TRPM_NO_ACTIVE_TRAP;
if (pu8TrapNo)
if (pEnmType)
if (puErrorCode)
if (puCR2)
return VINF_SUCCESS;
}
/**
* Save the active trap.
*
* Any function which uses temporary trap handlers should
* probably also use this facility to save the original trap.
*
* @param pVM VM handle.
*/
{
}
/**
* Restore a saved trap.
*
* Multiple restores of a saved trap is possible.
*
* @param pVM VM handle.
*/
{
}
#ifndef IN_RING0
/**
* Forward trap or interrupt to the guest's handler
*
*
* @returns VBox status code.
* or does not return at all (when the trap is actually forwarded)
*
* @param pVM The VM to operate on.
* @param pRegFrame Pointer to the register frame for the trap.
* @param iGate Trap or interrupt gate number
* @param opsize Instruction size (only relevant for software interrupts)
* @param enmError TRPM_TRAP_HAS_ERRORCODE or TRPM_TRAP_NO_ERRORCODE.
* @param enmType TRPM event type
* @param iOrgTrap The original trap.
* @internal
*/
VMMDECL(int) TRPMForwardTrap(PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, uint32_t iGate, uint32_t opsize, TRPMERRORCODE enmError, TRPMEVENT enmType, int32_t iOrgTrap)
{
#ifdef TRPM_FORWARD_TRAPS_IN_GC
# if defined(VBOX_STRICT) || defined(LOG_ENABLED)
else
switch (iGate) {
case 14:
{
int rc;
# ifdef IN_RC
# else
# endif
if (RT_SUCCESS(rc))
}
/* no break */
case 8:
case 10:
case 11:
case 12:
case 13:
case 17:
break;
default:
break;
}
# endif /* VBOX_STRICT || LOG_ENABLED */
/* Retrieve the eflags including the virtualized bits. */
/* Note: hackish as the cpumctxcore structure doesn't contain the right value */
/* VMCPU_FF_INHIBIT_INTERRUPTS should be cleared upfront or don't call this function at all for dispatching hardware interrupts. */
/*
* If it's a real guest trap and the guest's page fault handler is marked as safe for GC execution, then we call it directly.
* Well, only if the IF flag is set.
*/
/** @todo if the trap handler was modified and marked invalid, then we should *now* go back to the host context and install a new patch. */
#ifndef VBOX_RAW_V86
&& !(eflags.Bits.u1VM) /** @todo implement when needed (illegal for same privilege level transfers). */
#endif
)
{
int rc;
Assert(!VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT | VMCPU_FF_TRPM_SYNC_IDT | VMCPU_FF_SELM_SYNC_TSS));
goto failure;
/* Get the current privilege level. */
/*
* BIG TODO: The checks are not complete. see trap and interrupt dispatching section in Intel docs for details
* All very obscure, but still necessary.
* Currently only some CS & TSS selector checks are missing.
*
*/
#ifdef IN_RC
#else
#endif
if (RT_FAILURE(rc))
{
/* The page might be out of sync. */ /** @todo might cross a page boundary) */
rc = PGMPrefetchPage(pVCpu, pIDTEntry); /** @todo r=bird: rainy day: this isn't entirely safe because of access bit virtualiziation and CSAM. */
if (rc != VINF_SUCCESS)
{
goto failure;
}
#ifdef IN_RC
#else
#endif
}
if ( RT_SUCCESS(rc)
&& (GuestIdte.Gen.u5Type2 == VBOX_IDTE_TYPE2_TRAP_32 || GuestIdte.Gen.u5Type2 == VBOX_IDTE_TYPE2_INT_32)
&& (enmType == TRPM_TRAP || enmType == TRPM_HARDWARE_INT || cpl <= GuestIdte.Gen.u2DPL) /* CPL <= DPL if software int */
)
{
/* Note: SELMValidateAndConvertCSAddr checks for code type, memory type, selector validity. */
/** @todo dpl <= cpl else GPF */
/* Note: don't use current eflags as we might be in V86 mode and the IDT always contains protected mode selectors */
rc = SELMValidateAndConvertCSAddr(pVM, fakeflags, 0, GuestIdte.Gen.u16SegSel, NULL, pHandler, &dummy);
if (rc == VINF_SUCCESS)
{
bool fConforming = false;
int idx = 0;
goto failure;
#ifdef IN_RC
#else
#endif
if (RT_FAILURE(rc))
{
/* The page might be out of sync. */ /** @todo might cross a page boundary) */
rc = PGMPrefetchPage(pVCpu, pGdtEntry); /** @todo r=bird: rainy day: this isn't entirely safe because of access bit virtualiziation and CSAM. */
if (rc != VINF_SUCCESS)
{
goto failure;
}
#ifdef IN_RC
#else
#endif
if (RT_FAILURE(rc))
{
goto failure;
}
}
{
Log(("Conforming code selector\n"));
fConforming = true;
}
/** @todo check descriptor type!! */
{
if (RT_FAILURE(rc))
goto failure;
if ( !esp_r0
|| !ss_r0
|| SELMToFlatBySelEx(pVM, fakeflags, ss_r0, (RTGCPTR)esp_r0, NULL, SELMTOFLAT_FLAGS_CPL1, (PRTGCPTR)&pTrapStackGC, NULL) != VINF_SUCCESS
)
{
goto failure;
}
}
else
{
|| SELMToFlatBySelEx(pVM, fakeflags, ss_r0, (RTGCPTR)esp_r0, NULL, SELMTOFLAT_FLAGS_CPL1, (PRTGCPTR)&pTrapStackGC, NULL) != VINF_SUCCESS)
{
goto failure;
}
}
else
{
goto failure;
}
/*
* Build trap stack frame on guest handler's stack
*/
#ifdef IN_RC
/* Check maximum amount we need (10 when executing in V86 mode) */
rc = PGMVerifyAccess(pVCpu, (RTGCUINTPTR)pTrapStackGC - 10*sizeof(uint32_t), 10 * sizeof(uint32_t), X86_PTE_RW);
#else
Assert(eflags.Bits.u1VM || (pRegFrame->ss & X86_SEL_RPL) == 0 || (pRegFrame->ss & X86_SEL_RPL) == 3);
/* Check maximum amount we need (10 when executing in V86 mode) */
if ((pTrapStackGC >> PAGE_SHIFT) != ((pTrapStackGC - 10*sizeof(uint32_t)) >> PAGE_SHIFT)) /* fail if we cross a page boundary */
goto failure;
if (RT_FAILURE(rc))
{
goto failure;
}
#endif
if (rc == VINF_SUCCESS)
{
/** if eflags.Bits.u1VM then push gs, fs, ds, es */
{
Log(("TRAP%02X: (VM) Handler %04X:%RGv Stack %04X:%08X RPL=%d CR2=%08X\n", iGate, GuestIdte.Gen.u16SegSel, pHandler, ss_r0, esp_r0, (pRegFrame->ss & X86_SEL_RPL), pVCpu->trpm.s.uActiveCR2));
/* clear ds, es, fs & gs in current context */
}
else
Log(("TRAP%02X: Handler %04X:%RGv Stack %04X:%08X RPL=%d CR2=%08X\n", iGate, GuestIdte.Gen.u16SegSel, pHandler, ss_r0, esp_r0, (pRegFrame->ss & X86_SEL_RPL), pVCpu->trpm.s.uActiveCR2));
{
else
}
/* Note: We use the eflags copy, that includes the virtualized bits! */
else
if (enmType == TRPM_SOFTWARE_INT)
{
}
else
if (enmError == TRPM_TRAP_HAS_ERRORCODE)
{
}
/* Adjust ESP accordingly */
#ifdef DEBUG
for (int j = idx; j < 0; j++)
Log4(("eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n"
"eip=%08x esp=%08x ebp=%08x iopl=%d\n"
"cs=%04x ds=%04x es=%04x fs=%04x gs=%04x eflags=%08x\n",
#endif
Log(("PATM Handler %RRv Adjusted stack %08X new EFLAGS=%08X idx=%d dpl=%d cpl=%d\n", pVM->trpm.s.aGuestTrapHandler[iGate], esp_r0, eflags.u32, idx, dpl, cpl));
/* Make sure the internal guest context structure is up-to-date. */
#ifdef IN_RC
/* Note: shouldn't be necessary */
/* Turn off interrupts for interrupt gates. */
/* The virtualized bits must be removed again!! */
CPUMGCCallGuestTrapHandler(pRegFrame, GuestIdte.Gen.u16SegSel | 1, pVM->trpm.s.aGuestTrapHandler[iGate], eflags.u32, ss_r0, (RTRCPTR)esp_r0);
/* does not return */
#else
/* Turn off interrupts for interrupt gates. */
return VINF_SUCCESS;
#endif
}
else
Log(("TRAP%02X: PGMVerifyAccess %RGv failed with %Rrc -> forward to REM\n", iGate, pTrapStackGC, rc));
}
else
}
else
Log(("MMRamRead %RGv size %d failed with %Rrc\n", (RTGCUINTPTR)GCPtrIDT + sizeof(VBOXIDTE) * iGate, sizeof(GuestIdte), rc));
}
else
{
#ifdef VBOX_WITH_STATISTICS
#endif
}
Log(("TRAP%02X: forwarding to REM (ss rpl=%d eflags=%08X VMIF=%d handler=%08X\n", iGate, pRegFrame->ss & X86_SEL_RPL, pRegFrame->eflags.u32, PATMAreInterruptsEnabledByCtxCore(pVM, pRegFrame), pVM->trpm.s.aGuestTrapHandler[iGate]));
#endif
return VINF_EM_RAW_GUEST_TRAP;
}
#endif /* !IN_RING0 */
/**
* Raises a cpu exception which doesn't take an error code.
*
* This function may or may not dispatch the exception before returning.
*
* @returns VBox status code fit for scheduling.
* @retval VINF_EM_RAW_GUEST_TRAP if the exception was left pending.
* @retval VINF_TRPM_XCPT_DISPATCHED if the exception was raised and dispatched for raw-mode execution.
* @retval VINF_EM_RESCHEDULE_REM if the exception was dispatched and cannot be executed in raw-mode.
*
* @param pVM The VM handle.
* @param pCtxCore The CPU context core.
* @param enmXcpt The exception.
*/
{
LogFlow(("TRPMRaiseXcptErr: cs:eip=%RTsel:%RX32 enmXcpt=%#x\n", pCtxCore->cs, pCtxCore->eip, enmXcpt));
/** @todo dispatch the trap. */
return VINF_EM_RAW_GUEST_TRAP;
}
/**
* Raises a cpu exception with an errorcode.
*
* This function may or may not dispatch the exception before returning.
*
* @returns VBox status code fit for scheduling.
* @retval VINF_EM_RAW_GUEST_TRAP if the exception was left pending.
* @retval VINF_TRPM_XCPT_DISPATCHED if the exception was raised and dispatched for raw-mode execution.
* @retval VINF_EM_RESCHEDULE_REM if the exception was dispatched and cannot be executed in raw-mode.
*
* @param pVM The VM handle.
* @param pCtxCore The CPU context core.
* @param enmXcpt The exception.
* @param uErr The error code.
*/
{
LogFlow(("TRPMRaiseXcptErr: cs:eip=%RTsel:%RX32 enmXcpt=%#x uErr=%RX32\n", pCtxCore->cs, pCtxCore->eip, enmXcpt, uErr));
/** @todo dispatch the trap. */
return VINF_EM_RAW_GUEST_TRAP;
}
/**
* Raises a cpu exception with an errorcode and CR2.
*
* This function may or may not dispatch the exception before returning.
*
* @returns VBox status code fit for scheduling.
* @retval VINF_EM_RAW_GUEST_TRAP if the exception was left pending.
* @retval VINF_TRPM_XCPT_DISPATCHED if the exception was raised and dispatched for raw-mode execution.
* @retval VINF_EM_RESCHEDULE_REM if the exception was dispatched and cannot be executed in raw-mode.
*
* @param pVM The VM handle.
* @param pCtxCore The CPU context core.
* @param enmXcpt The exception.
* @param uErr The error code.
* @param uCR2 The CR2 value.
*/
VMMDECL(int) TRPMRaiseXcptErrCR2(PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, X86XCPT enmXcpt, uint32_t uErr, RTGCUINTPTR uCR2)
{
LogFlow(("TRPMRaiseXcptErr: cs:eip=%RTsel:%RX32 enmXcpt=%#x uErr=%RX32 uCR2=%RGv\n", pCtxCore->cs, pCtxCore->eip, enmXcpt, uErr, uCR2));
/** @todo dispatch the trap. */
return VINF_EM_RAW_GUEST_TRAP;
}
/**
*
* @returns VBox status code.
* @param pVM The VM to operate on.
*/
{
/*
* Validate.
*/
{
return VERR_INVALID_PARAMETER;
}
#ifdef IN_RING3
#else
AssertFailed();
#endif
return VINF_SUCCESS;
}