/* $Id$ */
/** @file
* HM SVM (AMD-V) - Host Context Ring-0.
*/
/*
* Copyright (C) 2013-2015 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 *
*******************************************************************************/
#include <iprt/asm-amd64-x86.h>
#include "HMInternal.h"
#include "HMSVMR0.h"
#ifdef DEBUG_ramshankar
# define HMSVM_SYNC_FULL_GUEST_STATE
# define HMSVM_ALWAYS_TRAP_ALL_XCPTS
# define HMSVM_ALWAYS_TRAP_PF
# define HMSVM_ALWAYS_TRAP_TASK_SWITCH
#endif
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
#ifdef VBOX_WITH_STATISTICS
if ((u64ExitCode) == SVM_EXIT_NPF) \
else \
} while (0)
#else
#endif
/** If we decide to use a function table approach this can be useful to
* switch to a "static DECLCALLBACK(int)". */
#define HMSVM_EXIT_DECL static int
/** @name Segment attribute conversion between CPU and AMD-V VMCB format.
*
* The CPU format of the segment attribute is described in X86DESCATTRBITS
* which is 16-bits (i.e. includes 4 bits of the segment limit).
*
* The AMD-V VMCB format the segment attribute is compact 12-bits (strictly
* only the attribute bits and nothing else). Upper 4-bits are unused.
*
* @{ */
/** @} */
* @{ */
do \
{ \
} while (0)
do \
{ \
} while (0)
/** @} */
/** Macro for checking and returning from the using function for
* \#VMEXIT intercepts that maybe caused during delivering of another
* event in the guest. */
#define HMSVM_CHECK_EXIT_DUE_TO_EVENT_DELIVERY() \
do \
{ \
return VINF_SUCCESS; \
return rc; \
} while (0)
/** Macro for upgrading a @a a_rc to VINF_EM_DBG_STEPPED after emulating an
* instruction that exited. */
do { \
(a_rc) = VINF_EM_DBG_STEPPED; \
} while (0)
/** Assert that preemption is disabled or covered by thread-context hooks. */
/** Assert that we haven't migrated CPUs when thread-context hooks are not
* used. */
("Illegal migration! Entered on CPU %u Current %u\n", \
/** Exception bitmap mask for all contributory exceptions.
*
* Page fault is deliberately excluded here as it's conditional as to whether
* it's contributory or benign. Page faults are handled separately.
*/
#define HMSVM_CONTRIBUTORY_XCPT_MASK ( RT_BIT(X86_XCPT_GP) | RT_BIT(X86_XCPT_NP) | RT_BIT(X86_XCPT_SS) | RT_BIT(X86_XCPT_TS) \
| RT_BIT(X86_XCPT_DE))
/** @name VMCB Clean Bits.
*
* These flags are used for VMCB-state caching. A set VMCB Clean bit indicates
* AMD-V doesn't need to reload the corresponding value(s) from the VMCB in
* memory.
*
* @{ */
/** All intercepts vectors, TSC offset, PAUSE filter counter. */
/** I/O permission bitmap, MSR permission bitmap. */
/** ASID. */
/** TRP: V_TPR, V_IRQ, V_INTR_PRIO, V_IGN_TPR, V_INTR_MASKING,
V_INTR_VECTOR. */
/** Nested Paging: Nested CR3 (nCR3), PAT. */
/** Control registers (CR0, CR3, CR4, EFER). */
/** Debug registers (DR6, DR7). */
/** GDT, IDT limit and base. */
/** Segment register: CS, SS, DS, ES limit and base. */
/** CR2.*/
/** Last-branch record (DbgCtlMsr, br_from, br_to, lastint_from, lastint_to) */
/** AVIC (AVIC APIC_BAR; AVIC APIC_BACKING_PAGE, AVIC
PHYSICAL_TABLE and AVIC LOGICAL_TABLE Pointers). */
/** Mask of all valid VMCB Clean bits. */
/** @} */
/** @name SVM transient.
*
* A state structure for holding miscellaneous information across AMD-V
* VMRUN/#VMEXIT operation, restored after the transition.
*
* @{ */
typedef struct SVMTRANSIENT
{
#if HC_ARCH_BITS == 32
#endif
/** The #VMEXIT exit code (the EXITCODE field in the VMCB). */
/** The guest's TPR value used for TPR shadowing. */
/** Alignment. */
/** Whether the guest FPU state was active at the time of #VMEXIT. */
bool fWasGuestFPUStateActive;
/** Whether the guest debug state was active at the time of #VMEXIT. */
/** Whether the hyper debug state was active at the time of #VMEXIT. */
/** Whether the TSC offset mode needs to be updated. */
bool fUpdateTscOffsetting;
/** Whether the TSC_AUX MSR needs restoring on #VMEXIT. */
bool fRestoreTscAuxMsr;
/** Whether the #VMEXIT was caused by a page-fault during delivery of a
* contributary exception or a page-fault. */
bool fVectoringDoublePF;
/** Whether the #VMEXIT was caused by a page-fault during delivery of an
* external interrupt or NMI. */
bool fVectoringPF;
/** @} */
/**
* MSRPM (MSR permission bitmap) read permissions (for guest RDMSR).
*/
typedef enum SVMMSREXITREAD
{
/** Reading this MSR causes a #VMEXIT. */
/** Reading this MSR does not cause a #VMEXIT. */
/**
* MSRPM (MSR permission bitmap) write permissions (for guest WRMSR).
*/
typedef enum SVMMSREXITWRITE
{
/** Writing to this MSR causes a #VMEXIT. */
/** Writing to this MSR does not cause a #VMEXIT. */
/**
* SVM #VMEXIT handler.
*
* @returns VBox status code.
* @param pVCpu Pointer to the VMCPU.
* @param pMixedCtx Pointer to the guest-CPU context.
* @param pSvmTransient Pointer to the SVM-transient structure.
*/
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static void hmR0SvmSetMsrPermission(PVMCPU pVCpu, unsigned uMsr, SVMMSREXITREAD enmRead, SVMMSREXITWRITE enmWrite);
/** @name #VMEXIT handlers.
* @{
*/
/** @} */
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** Ring-0 memory object for the IO bitmap. */
/** Physical address of the IO bitmap. */
/** Virtual address of the IO bitmap. */
/**
* Sets up and activates AMD-V on the current CPU.
*
* @returns VBox status code.
* @param pCpu Pointer to the CPU info struct.
* @param pVM Pointer to the VM (can be NULL after a resume!).
* @param pvCpuPage Pointer to the global CPU page.
* @param HCPhysCpuPage Physical address of the global CPU page.
* @param fEnabledByHost Whether the host OS has already initialized AMD-V.
* @param pvArg Unused on AMD-V.
*/
VMMR0DECL(int) SVMR0EnableCpu(PHMGLOBALCPUINFO pCpu, PVM pVM, void *pvCpuPage, RTHCPHYS HCPhysCpuPage, bool fEnabledByHost,
void *pvArg)
{
/* Paranoid: Disable interrupt as, in theory, interrupt handlers might mess with EFER. */
/*
* We must turn on AMD-V and setup the host state physical address, as those MSRs are per CPU.
*/
if (u64HostEfer & MSR_K6_EFER_SVME)
{
/* If the VBOX_HWVIRTEX_IGNORE_SVM_IN_USE is active, then we blindly use AMD-V. */
if ( pVM
{
pCpu->fIgnoreAMDVInUseError = true;
}
if (!pCpu->fIgnoreAMDVInUseError)
{
return VERR_SVM_IN_USE;
}
}
/* Turn on AMD-V in the EFER MSR. */
/* Write the physical page address where the CPU will store the host state while executing the VM. */
/* Restore interrupts. */
/*
* Theoretically, other hypervisors may have used ASIDs, ideally we should flush all non-zero ASIDs
* when enabling SVM. AMD doesn't have an SVM instruction to flush all ASIDs (flushing is done
* upon VMRUN). Therefore, just set the fFlushAsidBeforeUse flag which instructs hmR0SvmSetupTLB()
* to flush the TLB with before using a new ASID.
*/
pCpu->fFlushAsidBeforeUse = true;
/*
* Ensure each VCPU scheduled on this CPU gets a new VPID on resume. See @bugref{6255}.
*/
++pCpu->cTlbFlushes;
return VINF_SUCCESS;
}
/**
* Deactivates AMD-V on the current CPU.
*
* @returns VBox status code.
* @param pCpu Pointer to the CPU info struct.
* @param pvCpuPage Pointer to the global CPU page.
* @param HCPhysCpuPage Physical address of the global CPU page.
*/
{
/* Paranoid: Disable interrupts as, in theory, interrupt handlers might mess with EFER. */
/* Turn off AMD-V in the EFER MSR. */
/* Invalidate host state physical address. */
/* Restore interrupts. */
return VINF_SUCCESS;
}
/**
* Does global AMD-V initialization (called during module initialization).
*
* @returns VBox status code.
*/
{
/*
* Allocate 12 KB for the IO bitmap. Since this is non-optional and we always intercept all IO accesses, it's done
* once globally here instead of per-VM.
*/
if (RT_FAILURE(rc))
return rc;
/* Set all bits to intercept all IO accesses. */
return VINF_SUCCESS;
}
/**
* Does global AMD-V termination (called during module termination).
*/
{
if (g_hMemObjIOBitmap != NIL_RTR0MEMOBJ)
{
g_pvIOBitmap = NULL;
g_HCPhysIOBitmap = 0;
}
}
/**
* Frees any allocated per-VCPU structures for a VM.
*
* @param pVM Pointer to the VM.
*/
{
{
{
}
{
}
{
}
}
}
/**
* Does per-VM AMD-V initialization.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
*/
{
/*
* Check for an AMD CPU erratum which requires us to flush the TLB before every world-switch.
*/
{
Log4(("SVMR0InitVM: AMD cpu with erratum 170 family %#x model %#x stepping %#x\n", u32Family, u32Model, u32Stepping));
}
/*
* Initialize the R0 memory objects up-front so we can properly cleanup on allocation failures.
*/
{
}
{
/*
* Allocate one page for the host-context VM control block (VMCB). This is used for additional host-state (such as
* FS, GS, Kernel GS Base, etc.) apart from the host-state save area specified in MSR_K8_VM_HSAVE_PA.
*/
rc = RTR0MemObjAllocCont(&pVCpu->hm.s.svm.hMemObjVmcbHost, 1 << PAGE_SHIFT, false /* fExecutable */);
if (RT_FAILURE(rc))
goto failure_cleanup;
pVCpu->hm.s.svm.HCPhysVmcbHost = RTR0MemObjGetPagePhysAddr(pVCpu->hm.s.svm.hMemObjVmcbHost, 0 /* iPage */);
/*
* Allocate one page for the guest-state VMCB.
*/
if (RT_FAILURE(rc))
goto failure_cleanup;
/*
* Allocate two pages (8 KB) for the MSR permission bitmap. There doesn't seem to be a way to convince
* SVM to not require one.
*/
rc = RTR0MemObjAllocCont(&pVCpu->hm.s.svm.hMemObjMsrBitmap, 2 << PAGE_SHIFT, false /* fExecutable */);
if (RT_FAILURE(rc))
goto failure_cleanup;
pVCpu->hm.s.svm.HCPhysMsrBitmap = RTR0MemObjGetPagePhysAddr(pVCpu->hm.s.svm.hMemObjMsrBitmap, 0 /* iPage */);
/* Set all bits to intercept all MSR accesses (changed later on). */
}
return VINF_SUCCESS;
return rc;
}
/**
* Does per-VM AMD-V termination.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
*/
{
return VINF_SUCCESS;
}
/**
* Sets the permission bits for the specified MSR in the MSRPM.
*
* @param pVCpu Pointer to the VMCPU.
* @param uMsr The MSR for which the access permissions are being set.
* @param enmRead MSR read permissions.
* @param enmWrite MSR write permissions.
*/
static void hmR0SvmSetMsrPermission(PVMCPU pVCpu, unsigned uMsr, SVMMSREXITREAD enmRead, SVMMSREXITWRITE enmWrite)
{
unsigned ulBit;
/*
* Layout:
* Byte offset MSR range
* 0x000 - 0x7ff 0x00000000 - 0x00001fff
* 0x800 - 0xfff 0xc0000000 - 0xc0001fff
* 0x1000 - 0x17ff 0xc0010000 - 0xc0011fff
* 0x1800 - 0x1fff Reserved
*/
if (uMsr <= 0x00001FFF)
{
/* Pentium-compatible MSRs. */
}
else if ( uMsr >= 0xC0000000
&& uMsr <= 0xC0001FFF)
{
/* AMD Sixth Generation x86 Processor MSRs. */
pbMsrBitmap += 0x800;
}
else if ( uMsr >= 0xC0010000
&& uMsr <= 0xC0011FFF)
{
/* AMD Seventh and Eighth Generation Processor MSRs. */
pbMsrBitmap += 0x1000;
}
else
{
AssertFailed();
return;
}
if (enmRead == SVMMSREXIT_INTERCEPT_READ)
else
if (enmWrite == SVMMSREXIT_INTERCEPT_WRITE)
else
}
/**
* Sets up AMD-V for the specified VM.
* This function is only called once per-VM during initalization.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
*/
{
{
/* Initialize the #VMEXIT history array with end-of-array markers (UINT16_MAX). */
/* Trap exceptions unconditionally (debug purposes). */
#ifdef HMSVM_ALWAYS_TRAP_PF
#endif
#ifdef HMSVM_ALWAYS_TRAP_ALL_XCPTS
/* If you add any exceptions here, make sure to update hmR0SvmHandleExit(). */
;
#endif
/* Set up unconditional intercepts and conditions. */
| SVM_CTRL1_INTERCEPT_NMI /* Non-maskable interrupts causes a #VMEXIT. */
| SVM_CTRL1_INTERCEPT_INIT /* INIT signal causes a #VMEXIT. */
| SVM_CTRL1_INTERCEPT_RDPMC /* RDPMC causes a #VMEXIT. */
| SVM_CTRL1_INTERCEPT_CPUID /* CPUID causes a #VMEXIT. */
| SVM_CTRL1_INTERCEPT_RSM /* RSM causes a #VMEXIT. */
| SVM_CTRL1_INTERCEPT_HLT /* HLT causes a #VMEXIT. */
| SVM_CTRL1_INTERCEPT_INOUT_BITMAP /* Use the IOPM to cause IOIO #VMEXITs. */
| SVM_CTRL1_INTERCEPT_MSR_SHADOW /* MSR access not covered by MSRPM causes a #VMEXIT.*/
| SVM_CTRL1_INTERCEPT_INVLPGA /* INVLPGA causes a #VMEXIT. */
| SVM_CTRL1_INTERCEPT_SHUTDOWN /* Shutdown events causes a #VMEXIT. */
| SVM_CTRL1_INTERCEPT_FERR_FREEZE; /* Intercept "freezing" during legacy FPU handling. */
| SVM_CTRL2_INTERCEPT_VMMCALL /* VMMCALL causes a #VMEXIT. */
| SVM_CTRL2_INTERCEPT_VMLOAD /* VMLOAD causes a #VMEXIT. */
| SVM_CTRL2_INTERCEPT_VMSAVE /* VMSAVE causes a #VMEXIT. */
| SVM_CTRL2_INTERCEPT_STGI /* STGI causes a #VMEXIT. */
| SVM_CTRL2_INTERCEPT_CLGI /* CLGI causes a #VMEXIT. */
| SVM_CTRL2_INTERCEPT_SKINIT /* SKINIT causes a #VMEXIT. */
| SVM_CTRL2_INTERCEPT_WBINVD /* WBINVD causes a #VMEXIT. */
| SVM_CTRL2_INTERCEPT_MONITOR /* MONITOR causes a #VMEXIT. */
| SVM_CTRL2_INTERCEPT_MWAIT /* MWAIT causes a #VMEXIT. */
| SVM_CTRL2_INTERCEPT_XSETBV; /* XSETBV causes a #VMEXIT. */
/* CR0, CR4 reads must be intercepted, our shadow values are not necessarily the same as the guest's. */
/* CR0, CR4 writes must be intercepted for the same reasons as above. */
/* Intercept all DRx reads and writes by default. Changed later on. */
/* Ignore the priority in the TPR. This is necessary for delivering PIC style (ExtInt) interrupts and we currently
deliver both PIC and APIC interrupts alike. See hmR0SvmInjectPendingEvent() */
/* Set IO and MSR bitmap permission bitmap physical addresses. */
/* No LBR virtualization. */
/* Initially set all VMCB clean bits to 0 indicating that everything should be loaded from the VMCB in memory. */
/* The host ASID MBZ, for the guest start with 1. */
/*
* Setup the PAT MSR (applicable for Nested Paging only).
* The default value should be 0x0007040600070406ULL, but we want to treat all guest memory as WB,
* so choose type 6 for all PAT slots.
*/
/* Setup Nested Paging. This doesn't change throughout the execution time of the VM. */
/* Without Nested Paging, we need additionally intercepts. */
{
/* Intercept INVLPG and task switches (may change CR3, EFLAGS, LDT). */
/* Page faults must be intercepted to implement shadow paging. */
}
#endif
/* Apply the exceptions intercepts needed by the GIM provider. */
/*
*/
hmR0SvmSetMsrPermission(pVCpu, MSR_K8_SF_MASK, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
hmR0SvmSetMsrPermission(pVCpu, MSR_K8_FS_BASE, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
hmR0SvmSetMsrPermission(pVCpu, MSR_K8_GS_BASE, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
hmR0SvmSetMsrPermission(pVCpu, MSR_K8_KERNEL_GS_BASE, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
hmR0SvmSetMsrPermission(pVCpu, MSR_IA32_SYSENTER_CS, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
hmR0SvmSetMsrPermission(pVCpu, MSR_IA32_SYSENTER_ESP, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
hmR0SvmSetMsrPermission(pVCpu, MSR_IA32_SYSENTER_EIP, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
}
return VINF_SUCCESS;
}
/**
* Invalidates a guest page by guest virtual address.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
* @param GCVirt Guest virtual address of the page to invalidate.
*/
{
bool fFlushPending = pVM->hm.s.svm.fAlwaysFlushTLB || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_TLB_FLUSH);
/* Skip it if a TLB flush is already pending. */
if (!fFlushPending)
{
#if HC_ARCH_BITS == 32
/* If we get a flush in 64-bit guest mode, then force a full TLB flush. INVLPGA takes only 32-bit addresses. */
if (CPUMIsGuestInLongMode(pVCpu))
else
#endif
{
}
}
return VINF_SUCCESS;
}
/**
* Flushes the appropriate tagged-TLB entries.
*
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
*/
{
/*
* Force a TLB flush for the first world switch if the current CPU differs from the one we ran on last.
* This can happen both for start & resume due to long jumps back to ring-3.
* If the TLB flush count changed, another VM (VCPU rather) has hit the ASID limit while flushing the TLB,
* so we cannot reuse the ASIDs without flushing.
*/
bool fNewAsid = false;
{
fNewAsid = true;
}
/* Set TLB flush state as checked until we return from the world switch. */
/* Check for explicit TLB shootdowns. */
{
}
{
/*
* This is the AMD erratum 170. We need to flush the entire TLB for each world switch. Sad.
*/
/* Clear the VMCB Clean Bit for NP while flushing the TLB. See @bugref{7152}. */
}
{
/* Clear the VMCB Clean Bit for NP while flushing the TLB. See @bugref{7152}. */
if (fNewAsid)
{
++pCpu->uCurrentAsid;
bool fHitASIDLimit = false;
{
fHitASIDLimit = true;
{
pCpu->fFlushAsidBeforeUse = true;
}
else
{
pCpu->fFlushAsidBeforeUse = false;
}
}
if ( !fHitASIDLimit
&& pCpu->fFlushAsidBeforeUse)
{
else
{
pCpu->fFlushAsidBeforeUse = false;
}
}
}
else
{
else
}
}
/** @todo We never set VMCPU_FF_TLB_SHOOTDOWN anywhere so this path should
* not be executed. See hmQueueInvlPage() where it is commented
* out. Support individual entry flushing someday. */
#if 0
else
{
{
/* Deal with pending TLB shootdown actions which were queued when we were not executing code. */
}
}
#endif
/* Update VMCB with the ASID. */
{
}
("Flush count mismatch for cpu %d (%x vs %x)\n", pCpu->idCpu, pVCpu->hm.s.cTlbFlushes, pCpu->cTlbFlushes));
#ifdef VBOX_WITH_STATISTICS
{
}
else
{
}
#endif
}
/** @name 64-bit guest on 32-bit host OS helper functions.
*
* The host CPU is still 64-bit capable but the host OS is running in 32-bit
* bits for the 32->64 switcher.
*
* @{ */
#if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
/**
* Prepares for and executes VMRUN (64-bit guests on a 32-bit host).
*
* @returns VBox status code.
* @param HCPhysVmcbHost Physical address of host VMCB.
* @param HCPhysVmcb Physical address of the VMCB.
* @param pCtx Pointer to the guest-CPU context.
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
*/
DECLASM(int) SVMR0VMSwitcherRun64(RTHCPHYS HCPhysVmcbHost, RTHCPHYS HCPhysVmcb, PCPUMCTX pCtx, PVM pVM, PVMCPU pVCpu)
{
aParam[5] = 0;
aParam[7] = 0;
return SVMR0Execute64BitsHandler(pVM, pVCpu, pCtx, HM64ON32OP_SVMRCVMRun64, RT_ELEMENTS(aParam), &aParam[0]);
}
/**
* Executes the specified VMRUN handler in 64-bit mode.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
* @param pCtx Pointer to the guest-CPU context.
* @param enmOp The operation to perform.
* @param cParams Number of parameters.
* @param paParam Array of 32-bit parameters.
*/
{
/* Disable interrupts. */
#endif
for (int i = (int)cParams - 1; i >= 0; i--)
/* Call the switcher. */
int rc = pVM->hm.s.pfnHost32ToGuest64R0(pVM, RT_OFFSETOF(VM, aCpus[pVCpu->idCpu].cpum) - RT_OFFSETOF(VM, cpum));
/* Restore interrupts. */
return rc;
}
#endif /* HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS) */
/** @} */
/**
* Adds an exception to the intercept exception bitmap in the VMCB and updates
* the corresponding VMCB Clean bit.
*
* @param pVmcb Pointer to the VM control block.
* @param u32Xcpt The value of the exception (X86_XCPT_*).
*/
{
{
}
}
/**
* Removes an exception from the intercept-exception bitmap in the VMCB and
* updates the corresponding VMCB Clean bit.
*
* @param pVmcb Pointer to the VM control block.
* @param u32Xcpt The value of the exception (X86_XCPT_*).
*/
{
#ifndef HMSVM_ALWAYS_TRAP_ALL_XCPTS
{
}
#endif
}
/**
* Loads the guest CR0 control register into the guest-state area in the VMCB.
* Although the guest CR0 is a separate field in the VMCB we have to consider
* the FPU state itself which is shared between the host and the guest.
*
* @returns VBox status code.
* @param pVM Pointer to the VMCPU.
* @param pVmcb Pointer to the VM control block.
* @param pCtx Pointer to the guest-CPU context.
*
* @remarks No-long-jump zone!!!
*/
{
/*
* Guest CR0.
*/
{
/* Always enable caching. */
/*
* When Nested Paging is not available use shadow page tables and intercept #PFs (the latter done in SVMR0SetupVM()).
*/
{
u64GuestCR0 |= X86_CR0_WP; /* Guest CPL 0 writes to its read-only pages should cause a #PF #VMEXIT. */
}
/*
* Guest FPU bits.
*/
bool fInterceptNM = false;
bool fInterceptMF = false;
u64GuestCR0 |= X86_CR0_NE; /* Use internal x87 FPU exceptions handling rather than external interrupts. */
{
/* Catch floating point exceptions if we need to report them to the guest in a different way. */
{
Log4(("hmR0SvmLoadGuestControlRegs: Intercepting Guest CR0.MP Old-style FPU handling!!!\n"));
fInterceptMF = true;
}
}
else
{
fInterceptNM = true; /* Guest FPU inactive, #VMEXIT on #NM for lazy FPU loading. */
| X86_CR0_MP; /* FWAIT/WAIT should not ignore CR0.TS and should generate #NM. */
}
/*
* Update the exception intercept bitmap.
*/
if (fInterceptNM)
else
if (fInterceptMF)
else
}
}
/**
* Loads the guest control registers (CR2, CR3, CR4) into the VMCB.
*
* @returns VBox status code.
* @param pVCpu Pointer to the VMCPU.
* @param pVmcb Pointer to the VM control block.
* @param pCtx Pointer to the guest-CPU context.
*
* @remarks No-long-jump zone!!!
*/
{
/*
* Guest CR2.
*/
{
}
/*
* Guest CR3.
*/
{
{
#if HC_ARCH_BITS == 32
if (CPUMIsGuestInLongModeEx(pCtx))
else
#endif
}
else
}
/*
* Guest CR4.
* ASSUMES this is done everytime we get in from ring-3! (XCR0)
*/
{
{
{
case PGMMODE_REAL:
case PGMMODE_PROTECTED: /* Protected mode, no paging. */
AssertFailed();
case PGMMODE_32_BIT: /* 32-bit paging. */
u64GuestCR4 &= ~X86_CR4_PAE;
break;
case PGMMODE_PAE: /* PAE paging. */
case PGMMODE_PAE_NX: /* PAE paging with NX enabled. */
/** Must use PAE paging as we could use physical memory > 4 GB */
break;
case PGMMODE_AMD64: /* 64-bit AMD paging (long mode). */
case PGMMODE_AMD64_NX: /* 64-bit AMD paging (long mode) with NX enabled. */
#ifdef VBOX_ENABLE_64_BITS_GUESTS
break;
#else
AssertFailed();
#endif
default: /* shut up gcc */
AssertFailed();
}
}
/* Whether to save/load/restore XCR0 during world switch depends on CR4.OSXSAVE and host+guest XCR0. */
}
return VINF_SUCCESS;
}
/**
* Loads the guest segment registers into the VMCB.
*
* @returns VBox status code.
* @param pVCpu Pointer to the VMCPU.
* @param pVmcb Pointer to the VM control block.
* @param pCtx Pointer to the guest-CPU context.
*
* @remarks No-long-jump zone!!!
*/
{
/* Guest Segment registers: CS, SS, DS, ES, FS, GS. */
{
}
/* Guest TR. */
{
}
/* Guest LDTR. */
{
}
/* Guest GDTR. */
{
}
/* Guest IDTR. */
{
}
}
/**
* Loads the guest MSRs into the VMCB.
*
* @param pVCpu Pointer to the VMCPU.
* @param pVmcb Pointer to the VM control block.
* @param pCtx Pointer to the guest-CPU context.
*
* @remarks No-long-jump zone!!!
*/
{
/* Guest Sysenter MSRs. */
/*
* Guest EFER MSR.
* AMD-V requires guest EFER.SVME to be set. Weird.
* See AMD spec. 15.5.1 "Basic Operation" | "Canonicalization and Consistency Checks".
*/
{
}
/* 64-bit MSRs. */
if (CPUMIsGuestInLongModeEx(pCtx))
{
}
else
{
/* If the guest isn't in 64-bit mode, clear MSR_K6_LME bit from guest EFER otherwise AMD-V expects amd64 shadow paging. */
{
}
}
* be writable in 32-bit mode. Clarify with AMD spec. */
}
/**
* Loads the guest state into the VMCB and programs the necessary intercepts
* accordingly.
*
* @param pVCpu Pointer to the VMCPU.
* @param pVmcb Pointer to the VM control block.
* @param pCtx Pointer to the guest-CPU context.
*
* @remarks No-long-jump zone!!!
* @remarks Requires EFLAGS to be up-to-date in the VMCB!
*/
{
return;
Assert((pCtx->dr[6] & X86_DR6_RA1_MASK) == X86_DR6_RA1_MASK); Assert((pCtx->dr[6] & X86_DR6_RAZ_MASK) == 0);
Assert((pCtx->dr[7] & X86_DR7_RA1_MASK) == X86_DR7_RA1_MASK); Assert((pCtx->dr[7] & X86_DR7_RAZ_MASK) == 0);
bool fInterceptDB = false;
bool fInterceptMovDRx = false;
/*
* Anyone single stepping on the host side? If so, we'll have to use the
* trap flag in the guest EFLAGS since AMD-V doesn't have a trap flag on
* the VMM level like the VT-x implementations does.
*/
if (fStepping)
{
fInterceptDB = true;
fInterceptMovDRx = true; /* Need clean DR6, no guest mess. */
}
if ( fStepping
{
/*
* Use the combined guest and host DRx values found in the hypervisor
* register set because the debugger has breakpoints active or someone
* is single stepping on the host side.
*
* Note! DBGF expects a clean DR6 state before executing guest code.
*/
#if HC_ARCH_BITS == 32 && defined(VBOX_WITH_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
if ( CPUMIsGuestInLongModeEx(pCtx)
{
}
else
#endif
if (!CPUMIsHyperDebugStateActive(pVCpu))
{
}
/* Update DR6 & DR7. (The other DRx values are handled by CPUM one way or the other.) */
{
}
/** @todo If we cared, we could optimize to allow the guest to read registers
* with the same values. */
fInterceptDB = true;
fInterceptMovDRx = true;
Log5(("hmR0SvmLoadSharedDebugState: Loaded hyper DRx\n"));
}
else
{
/*
* Update DR6, DR7 with the guest values if necessary.
*/
{
}
/*
* If the guest has enabled debug registers, we need to load them prior to
* executing guest code so they'll trigger at the right time.
*/
{
#if HC_ARCH_BITS == 32 && defined(VBOX_WITH_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
if ( CPUMIsGuestInLongModeEx(pCtx)
{
}
else
#endif
if (!CPUMIsGuestDebugStateActive(pVCpu))
{
}
Log5(("hmR0SvmLoadSharedDebugState: Loaded guest DRx\n"));
}
/*
* If no debugging enabled, we'll lazy load DR0-3. We don't need to
* intercept #DB as DR6 is updated in the VMCB.
*/
#if HC_ARCH_BITS == 32 && defined(VBOX_WITH_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
else if ( !CPUMIsGuestDebugStateActivePending(pVCpu)
#else
else if (!CPUMIsGuestDebugStateActive(pVCpu))
#endif
{
fInterceptMovDRx = true;
}
}
/*
* Set up the intercepts.
*/
if (fInterceptDB)
else
if (fInterceptMovDRx)
{
{
}
}
else
{
{
}
}
}
/**
* Loads the guest APIC state (currently just the TPR).
*
* @returns VBox status code.
* @param pVCpu Pointer to the VMCPU.
* @param pVmcb Pointer to the VM control block.
* @param pCtx Pointer to the guest-CPU context.
*/
{
return VINF_SUCCESS;
bool fPendingIntr;
/* Assume that we need to trap all TPR accesses and thus need not check on
every #VMEXIT if we should update the TPR. */
/* 32-bit guests uses LSTAR MSR for patching guest code which touches the TPR. */
{
/* If there are interrupts pending, intercept LSTAR writes, otherwise don't intercept reads or writes. */
if (fPendingIntr)
else
{
}
}
else
{
/* Bits 3-0 of the VTPR field correspond to bits 7-4 of the TPR (which is the Task-Priority Class). */
/* If there are interrupts pending, intercept CR8 writes to evaluate ASAP if we can deliver the interrupt to the guest. */
if (fPendingIntr)
else
{
}
}
return rc;
}
/**
* Loads the exception interrupts required for guest execution in the VMCB.
*
* @returns VBox status code.
* @param pVCpu Pointer to the VMCPU.
* @param pVmcb Pointer to the VM control block.
* @param pCtx Pointer to the guest-CPU context.
*/
{
int rc = VINF_SUCCESS;
{
/* The remaining intercepts are handled elsewhere, e.g. in hmR0SvmLoadSharedCR0(). */
else
}
return rc;
}
/**
* Sets up the appropriate function to run guest code.
*
* @returns VBox status code.
* @param pVCpu Pointer to the VMCPU.
* @param pCtx Pointer to the guest-CPU context.
*
* @remarks No-long-jump zone!!!
*/
{
if (CPUMIsGuestInLongModeEx(pCtx))
{
#ifndef VBOX_ENABLE_64_BITS_GUESTS
#endif
/* 32-bit host. We need to switch to 64-bit before running the 64-bit guest. */
#else
/* 64-bit host or hybrid host. */
#endif
}
else
{
/* Guest is not in long mode, use the 32-bit handler. */
}
return VINF_SUCCESS;
}
/**
* Enters the AMD-V session.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
* @param pCpu Pointer to the CPU info struct.
*/
{
return VINF_SUCCESS;
}
/**
* Thread-context callback for AMD-V.
*
* @param enmEvent The thread-context event.
* @param pVCpu Pointer to the VMCPU.
* @thread EMT(pVCpu)
*/
{
switch (enmEvent)
{
{
/* No longjmps (log-flush, locks) in this fragile context. */
{
}
/* Leave HM context, takes care of local init (term). */
/* Restore longjmp state. */
break;
}
case RTTHREADCTXEVENT_RESUMED:
{
/* No longjmps (log-flush, locks) in this fragile context. */
/*
* Initialize the bare minimum state required for HM. This takes care of
* initializing AMD-V if necessary (onlined CPUs, local init etc.)
*/
/* Restore longjmp state. */
break;
}
default:
break;
}
}
/**
* Saves the host state.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
*
* @remarks No-long-jump zone!!!
*/
{
/* Nothing to do here. AMD-V does this for us automatically during the world-switch. */
return VINF_SUCCESS;
}
/**
* Loads the guest state into the VMCB.
*
* The CPU state will be loaded from these fields on every successful VM-entry.
* Also sets up the appropriate VMRUN function to execute guest code based on
* the guest CPU mode.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
* @param pCtx Pointer to the guest-CPU context.
*
* @remarks No-long-jump zone!!!
*/
{
AssertLogRelMsgRCReturn(rc, ("hmR0SvmLoadGuestControlRegs! rc=%Rrc (pVM=%p pVCpu=%p)\n", rc, pVM, pVCpu), rc);
AssertLogRelMsgRCReturn(rc, ("hmR0SvmLoadGuestApicState! rc=%Rrc (pVM=%p pVCpu=%p)\n", rc, pVM, pVCpu), rc);
AssertLogRelMsgRCReturn(rc, ("hmR0SvmLoadGuestXcptIntercepts! rc=%Rrc (pVM=%p pVCpu=%p)\n", rc, pVM, pVCpu), rc);
AssertLogRelMsgRCReturn(rc, ("hmR0SvmSetupVMRunHandler! rc=%Rrc (pVM=%p pVCpu=%p)\n", rc, pVM, pVCpu), rc);
/* Clear any unused and reserved bits. */
| HM_CHANGED_GUEST_LAZY_MSRS /* Unused. */
| HM_CHANGED_SVM_RESERVED1 /* Reserved. */
/* All the guest state bits should be loaded except maybe the host context and/or shared host/guest bits. */
Log4(("Load: CS:RIP=%04x:%RX64 EFL=%#x SS:RSP=%04x:%RX64\n", pCtx->cs.Sel, pCtx->rip, pCtx->eflags.u, pCtx->ss, pCtx->rsp));
return rc;
}
/**
* Loads the state shared between the host and guest into the
* VMCB.
*
* @param pVCpu Pointer to the VMCPU.
* @param pVmcb Pointer to the VM control block.
* @param pCtx Pointer to the guest-CPU context.
*
* @remarks No-long-jump zone!!!
*/
{
/* Unused on AMD-V. */
}
/**
* Saves the entire guest state from the VMCB into the
* guest-CPU context. Currently there is no residual state left in the CPU that
* is not updated in the VMCB.
*
* @returns VBox status code.
* @param pVCpu Pointer to the VMCPU.
* @param pMixedCtx Pointer to the guest-CPU context. The data may be
* out-of-sync. Make sure to update the required fields
* before using them.
*/
{
/*
* Guest interrupt shadow.
*/
/*
* Guest Control registers: CR2, CR3 (handled at the end) - accesses to other control registers are always intercepted.
*/
/*
* Guest MSRs.
*/
/*
* Guest segment registers (includes FS, GS base MSRs for 64-bit guests).
*/
/*
* Correct the hidden CS granularity bit. Haven't seen it being wrong in any other
* register (yet).
*/
/** @todo SELM might need to be fixed as it too should not care about the
* granularity bit. See @bugref{6785}. */
{
}
#ifdef VBOX_STRICT
#endif
/*
* Sync the hidden SS DPL field. AMD CPUs have a separate CPL field in the VMCB and uses that
* and thus it's possible that when the CPL changes during guest execution that the SS DPL
* isn't updated by AMD-V. Observed on some AMD Fusion CPUs with 64-bit guests.
* See AMD spec. 15.5.1 "Basic operation".
*/
/*
* Guest TR.
* Fixup TR attributes so it's compatible with Intel. Important when saved-states are used
* between Intel and AMD. See @bugref{6208} comment #39.
*/
/*
* Guest Descriptor-Table registers.
*/
/*
* Guest Debug registers.
*/
{
}
else
{
}
/*
* With Nested Paging, CR3 changes are not intercepted. Therefore, sync. it now.
* This is done as the very last step of syncing the guest state, as PGMUpdateCR3() may cause longjmp's to ring-3.
*/
{
}
}
/**
* Does the necessary state syncing before returning to ring-3 for any reason
* (longjmp, preemption, voluntary exits to ring-3) from AMD-V.
*
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
* @param pMixedCtx Pointer to the guest-CPU context.
*
* @remarks No-long-jmp zone!!!
*/
{
/*
* !!! IMPORTANT !!!
* If you modify code here, make sure to check whether hmR0SvmCallRing3Callback() needs to be updated too.
*/
/* Restore host FPU state if necessary and resync on next R0 reentry .*/
{
}
/*
* Restore host debug registers if necessary and resync on next R0 reentry.
*/
#ifdef VBOX_STRICT
{
}
#endif
}
/**
* Leaves the AMD-V session.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
* @param pCtx Pointer to the guest-CPU context.
*/
{
/* When thread-context hooks are used, we can avoid doing the leave again if we had been preempted before
and done this from the SVMR0ThreadCtxCallback(). */
{
}
/*
* !!! IMPORTANT !!!
* If you modify code here, make sure to check whether hmR0SvmCallRing3Callback() needs to be updated too.
*/
/* Deregister hook now that we've left HM context before re-enabling preemption. */
/* Leave HM context. This takes care of local init (term). */
return rc;
}
/**
* Does the necessary state syncing before doing a longjmp to ring-3.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
* @param pCtx Pointer to the guest-CPU context.
*
* @remarks No-long-jmp zone!!!
*/
{
}
/**
* VMMRZCallRing3() callback wrapper which saves the guest state (or restores
* any remaining host state) before we longjump to ring-3 and possibly get
* preempted.
*
* @param pVCpu Pointer to the VMCPU.
* @param enmOperation The operation causing the ring-3 longjump.
* @param pvUser The user argument (pointer to the possibly
* out-of-date guest-CPU context).
*/
{
{
/*
* !!! IMPORTANT !!!
* If you modify code here, make sure to check whether hmR0SvmLeave() and hmR0SvmLeaveSession() needs
* to be updated too. This is a stripped down version which gets out ASAP trying to not trigger any assertion.
*/
/* Restore host FPU state if necessary and resync on next R0 reentry .*/
/* Restore host debug registers if necessary and resync on next R0 reentry. */
/* Deregister the hook now that we've left HM context before re-enabling preemption. */
/* Leave HM context. This takes care of local init (term). */
return VINF_SUCCESS;
}
Log4(("hmR0SvmCallRing3Callback->hmR0SvmLongJmpToRing3\n"));
return VINF_SUCCESS;
}
/**
* Take necessary actions before going back to ring-3.
*
* An action requires us to go back to ring-3. This function does the necessary
* steps before we can safely return to ring-3. This is not the same as longjmps
* to ring-3, this is voluntary.
*
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
* @param pCtx Pointer to the guest-CPU context.
* @param rcExit The reason for exiting to ring-3. Can be
* VINF_VMM_UNKNOWN_RING3_CALL.
*/
{
/* Please, no longjumps here (any logging shouldn't flush jump back to ring-3). NO LOGGING BEFORE THIS POINT! */
/* We need to do this only while truly exiting the "inner loop" back to ring-3 and -not- for any longjmp to ring3. */
{
}
/* If we're emulating an instruction, we shouldn't have any TRPM traps pending
and if we're injecting an event we should have a TRPM trap pending. */
/* Sync. the necessary state for going back to ring-3. */
{
}
/* On our way back from ring-3 reload the guest state if there is a possibility of it being changed. */
if (rcExit != VINF_EM_RAW_INTERRUPT)
/* We do -not- want any longjmp notifications after this! We must return to ring-3 ASAP. */
}
/**
* Updates the use of TSC offsetting mode for the CPU and adjusts the necessary
* intercepts.
*
* @param pVM The shared VM handle.
* @param pVCpu Pointer to the VMCPU.
*
* @remarks No-long-jump zone!!!
*/
{
bool fParavirtTsc;
if (fCanUseRealTsc)
{
}
else
{
}
/** @todo later optimize this to be done elsewhere and not before every
* VM-entry. */
if (fParavirtTsc)
{
}
}
/**
* Sets an event as a pending event to be injected into the guest.
*
* @param pVCpu Pointer to the VMCPU.
* @param pEvent Pointer to the SVM event.
* @param GCPtrFaultAddress The fault-address (CR2) in case it's a
* page-fault.
*
* @remarks Statistics counter assumes this is a guest event being reflected to
* the guest i.e. 'StatInjectPendingReflect' is incremented always.
*/
DECLINLINE(void) hmR0SvmSetPendingEvent(PVMCPU pVCpu, PSVMEVENT pEvent, RTGCUINTPTR GCPtrFaultAddress)
{
Log4(("hmR0SvmSetPendingEvent: u=%#RX64 u8Vector=%#x Type=%#x ErrorCodeValid=%RTbool ErrorCode=%#RX32\n", pEvent->u,
pEvent->n.u8Vector, (uint8_t)pEvent->n.u3Type, !!pEvent->n.u1ErrorCodeValid, pEvent->n.u32ErrorCode));
}
/**
* Injects an event into the guest upon VMRUN by updating the relevant field
* in the VMCB.
*
* @param pVCpu Pointer to the VMCPU.
* @param pVmcb Pointer to the guest VM control block.
* @param pCtx Pointer to the guest-CPU context.
* @param pEvent Pointer to the event.
*
* @remarks No-long-jump zone!!!
* @remarks Requires CR0!
*/
DECLINLINE(void) hmR0SvmInjectEventVmcb(PVMCPU pVCpu, PSVMVMCB pVmcb, PCPUMCTX pCtx, PSVMEVENT pEvent)
{
Log4(("hmR0SvmInjectEventVmcb: u=%#RX64 u8Vector=%#x Type=%#x ErrorCodeValid=%RTbool ErrorCode=%#RX32\n", pEvent->u,
pEvent->n.u8Vector, (uint8_t)pEvent->n.u3Type, !!pEvent->n.u1ErrorCodeValid, pEvent->n.u32ErrorCode));
}
/**
* Converts any TRPM trap into a pending HM event. This is typically used when
* entering from ring-3 (not longjmp returns).
*
* @param pVCpu Pointer to the VMCPU.
*/
{
Event.u = 0;
/* Refer AMD spec. 15.20 "Event Injection" for the format. */
if (enmTrpmEvent == TRPM_TRAP)
{
switch (uVector)
{
case X86_XCPT_NMI:
{
break;
}
case X86_XCPT_PF:
case X86_XCPT_DF:
case X86_XCPT_TS:
case X86_XCPT_NP:
case X86_XCPT_SS:
case X86_XCPT_GP:
case X86_XCPT_AC:
{
break;
}
}
}
else if (enmTrpmEvent == TRPM_HARDWARE_INT)
else if (enmTrpmEvent == TRPM_SOFTWARE_INT)
else
Log4(("TRPM->HM event: u=%#RX64 u8Vector=%#x uErrorCodeValid=%RTbool uErrorCode=%#RX32\n", Event.u, Event.n.u8Vector,
}
/**
* Converts any pending SVM event into a TRPM trap. Typically used when leaving
* AMD-V to execute any instruction.
*
* @param pvCpu Pointer to the VMCPU.
*/
{
Assert(TRPMQueryTrap(pVCpu, NULL /* pu8TrapNo */, NULL /* pEnmType */) == VERR_TRPM_NO_ACTIVE_TRAP);
switch (uVectorType)
{
case SVM_EVENT_EXTERNAL_IRQ:
break;
case SVM_EVENT_SOFTWARE_INT:
break;
case SVM_EVENT_EXCEPTION:
case SVM_EVENT_NMI:
break;
default:
break;
}
if (Event.n.u1ErrorCodeValid)
if ( uVectorType == SVM_EVENT_EXCEPTION
&& uVector == X86_XCPT_PF)
{
}
else if (uVectorType == SVM_EVENT_SOFTWARE_INT)
{
}
}
/**
* Gets the guest's interrupt-shadow.
*
* @returns The guest's interrupt-shadow.
* @param pVCpu Pointer to the VMCPU.
* @param pCtx Pointer to the guest-CPU context.
*
* @remarks No-long-jump zone!!!
* @remarks Has side-effects with VMCPU_FF_INHIBIT_INTERRUPTS force-flag.
*/
{
/*
* Instructions like STI and MOV SS inhibit interrupts till the next instruction completes. Check if we should
* inhibit interrupts or clear any existing interrupt-inhibition.
*/
uint32_t uIntrState = 0;
{
{
/*
* We can clear the inhibit force flag as even if we go back to the recompiler without executing guest code in
* AMD-V, the flag's condition to be cleared is met and thus the cleared state is correct.
*/
}
else
}
return uIntrState;
}
/**
* Sets the virtual interrupt intercept control in the VMCB which
* instructs AMD-V to cause a #VMEXIT as soon as the guest is in a state to
* receive interrupts.
*
* @param pVmcb Pointer to the VM control block.
*/
{
{
pVmcb->ctrl.IntCtrl.n.u8VIrqVector = 0; /* Not necessary as we #VMEXIT for delivering the interrupt. */
Log4(("Setting VINTR intercept\n"));
}
}
/**
* Sets the IRET intercept control in the VMCB which instructs AMD-V to cause a
* #VMEXIT as soon as a guest starts executing an IRET. This is used to unblock
* virtual NMIs.
*
* @param pVmcb Pointer to the VM control block.
*/
{
{
Log4(("Setting IRET intercept\n"));
}
}
/**
* Clears the IRET intercept control in the VMCB.
*
* @param pVmcb Pointer to the VM control block.
*/
{
{
Log4(("Clearing IRET intercept\n"));
}
}
/**
* Evaluates the event to be delivered to the guest and sets it as the pending
* event.
*
* @param pVCpu Pointer to the VMCPU.
* @param pCtx Pointer to the guest-CPU context.
*/
{
Log4Func(("\n"));
Event.u = 0;
/** @todo SMI. SMIs take priority over NMIs. */
if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INTERRUPT_NMI)) /* NMI. NMIs take priority over regular interrupts . */
{
if (fBlockNmi)
else if (fIntShadow)
else
{
Log4(("Pending NMI\n"));
}
}
{
/*
* Check if the guest can receive external interrupts (PIC/APIC). Once we do PDMGetInterrupt() we -must- deliver
* the interrupt ASAP. We must not execute any guest code until we inject the interrupt which is why it is
* evaluated here and not set as pending, solely based on the force-flags.
*/
if ( !fBlockInt
&& !fIntShadow)
{
if (RT_SUCCESS(rc))
{
}
else
{
/** @todo Does this actually happen? If not turn it into an assertion. */
}
}
else
}
}
/**
* Injects any pending events into the guest if the guest is in a state to
* receive them.
*
* @param pVCpu Pointer to the VMCPU.
* @param pCtx Pointer to the guest-CPU context.
*/
{
{
#ifdef VBOX_STRICT
{
Assert(!fIntShadow);
}
Assert(!fIntShadow);
#endif
Log4(("Injecting pending HM event.\n"));
#ifdef VBOX_WITH_STATISTICS
else
#endif
}
/* Update the guest interrupt shadow in the VMCB. */
}
/**
* Reports world-switch error and dumps some useful debug info.
*
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
* @param rcVMRun The return code from VMRUN (or
* VERR_SVM_INVALID_GUEST_STATE for invalid
* guest-state).
* @param pCtx Pointer to the guest-CPU context.
*/
{
if (rcVMRun == VERR_SVM_INVALID_GUEST_STATE)
{
#ifdef VBOX_STRICT
#else
#endif /* VBOX_STRICT */
}
else
}
/**
* Check per-VM and per-VCPU force flag actions that require us to go back to
* ring-3 for one reason or another.
*
* @returns VBox status code (information status code included).
* @retval VINF_SUCCESS if we don't have any actions that require going back to
* ring-3.
* @retval VINF_PGM_SYNC_CR3 if we have pending PGM CR3 sync.
* @retval VINF_EM_PENDING_REQUEST if we have pending requests (like hardware
* interrupts)
* @retval VINF_PGM_POOL_FLUSH_PENDING if PGM is doing a pool flush and requires
* all EMTs to be in ring-3.
* @retval VINF_EM_RAW_TO_R3 if there is pending DMA requests.
* @retval VINF_EM_NO_MEMORY PGM is out of memory, we need to return
* to the EM loop.
*
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
* @param pCtx Pointer to the guest-CPU context.
*/
{
/* On AMD-V we don't need to update CR3, PAE PDPES lazily. See hmR0SvmSaveGuestState(). */
{
/* Pending PGM C3 sync. */
{
int rc = PGMSyncCR3(pVCpu, pCtx->cr0, pCtx->cr3, pCtx->cr4, VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3));
if (rc != VINF_SUCCESS)
{
return rc;
}
}
/* Pending HM-to-R3 operations (critsects, timers, EMT rendezvous etc.) */
/* -XXX- what was that about single stepping? */
{
int rc = RT_UNLIKELY(VM_FF_IS_PENDING(pVM, VM_FF_PGM_NO_MEMORY)) ? VINF_EM_NO_MEMORY : VINF_EM_RAW_TO_R3;
return rc;
}
/* Pending VM request packets, such as hardware interrupts. */
{
Log4(("hmR0SvmCheckForceFlags: Pending VM request forcing us back to ring-3\n"));
return VINF_EM_PENDING_REQUEST;
}
/* Pending PGM pool flushes. */
{
Log4(("hmR0SvmCheckForceFlags: PGM pool flush pending forcing us back to ring-3\n"));
return VINF_PGM_POOL_FLUSH_PENDING;
}
/* Pending DMA requests. */
{
Log4(("hmR0SvmCheckForceFlags: Pending DMA request forcing us back to ring-3\n"));
return VINF_EM_RAW_TO_R3;
}
}
return VINF_SUCCESS;
}
/**
* Does the preparations before executing guest code in AMD-V.
*
* This may cause longjmps to ring-3 and may even result in rescheduling to the
* recompiler. We must be cautious what we do here regarding committing
* guest-state information into the the VMCB assuming we assuredly execute the
* guest in AMD-V. If we fall back to the recompiler after updating the VMCB and
* clearing the common-state (TRPM/forceflags), we must undo those changes so
* that the recompiler can (and should) use them when it resumes guest
* execution. Otherwise such operations must be done when we can no longer
* exit to ring-3.
*
* @returns VBox status code (informational status codes included).
* @retval VINF_SUCCESS if we can proceed with running the guest.
* @retval VINF_* scheduling changes, we have to go back to ring-3.
*
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
* @param pCtx Pointer to the guest-CPU context.
* @param pSvmTransient Pointer to the SVM transient structure.
*/
{
/* Check force flag actions that might require us to go back to ring-3. */
if (rc != VINF_SUCCESS)
return rc;
if (TRPMHasTrap(pVCpu))
#ifdef HMSVM_SYNC_FULL_GUEST_STATE
#endif
/* Load the guest bits that are not shared with the host in any way since we can longjmp or get preempted. */
/*
* If we're not intercepting TPR changes in the guest, save the guest TPR before the world-switch
* so we can update it on the way back if the guest changed the TPR.
*/
{
else
{
}
}
/*
* No longjmps to ring-3 from this point on!!!
* Asserts() will still longjmp to ring-3 (but won't return), which is intentional, better than a kernel panic.
* This also disables flushing of the R0-logger instance (if any).
*/
/*
* We disable interrupts so that we don't miss any interrupts that would flag preemption (IPI/timers etc.)
* when thread-context hooks aren't used and we've been running with preemption disabled for a while.
*
* We need to check for force-flags that could've possible been altered since we last checked them (e.g.
* by PDMGetInterrupt() leaving the PDM critical section, see @bugref{6398}).
*
* We also check a couple of other force-flags as a last opportunity to get the EMT back to ring-3 before
* executing guest code.
*/
{
return VINF_EM_RAW_TO_R3;
}
{
return VINF_EM_RAW_INTERRUPT;
}
/*
* If we are injecting an NMI, we must set VMCPU_FF_BLOCK_NMIS only when we are going to execute
* guest code for certain (no exits to ring-3). Otherwise, we could re-read the flag on re-entry into
* AMD-V and conclude that NMI inhibition is active when we have not even delivered the NMI.
*
* With VT-x, this is handled by the Guest interruptibility information VMCS field which will set the
* VMCS field after actually delivering the NMI which we read on VM-exit to determine the state.
*/
{
{
}
}
return VINF_SUCCESS;
}
/**
* Prepares to run guest code in AMD-V and we've committed to doing so. This
* means there is no backing out to ring-3 or anywhere else at this
* point.
*
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
* @param pCtx Pointer to the guest-CPU context.
* @param pSvmTransient Pointer to the SVM transient structure.
*
* @remarks Called with preemption disabled.
* @remarks No-long-jump zone!!!
*/
static void hmR0SvmPreRunGuestCommitted(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
{
{
}
/* Load the state shared between host and guest (FPU, debug). */
HMCPU_CF_CLEAR(pVCpu, HM_CHANGED_HOST_CONTEXT); /* Preemption might set this, nothing to do on AMD-V. */
/* Setup TSC offsetting. */
{
pSvmTransient->fUpdateTscOffsetting = false;
}
/* If we've migrating CPUs, mark the VMCB Clean bits as dirty. */
/* Store status of the shared guest-host state at the time of VMRUN. */
#if HC_ARCH_BITS == 32 && defined(VBOX_WITH_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
if (CPUMIsGuestInLongModeEx(pCtx))
{
}
else
#endif
{
}
/* Flush the appropriate tagged-TLB entries. */
ASMAtomicWriteBool(&pVCpu->hm.s.fCheckedTLBFlush, true); /* Used for TLB-shootdowns, set this across the world switch. */
to start executing. */
/*
* Save the current Host TSC_AUX and write the guest TSC_AUX to the host, so that
* RDTSCPs (that don't cause exits) reads the guest MSR. See @bugref{3324}.
*
* This should be done -after- any RDTSCPs for obtaining the host timestamp (TM, STAM etc).
*/
{
hmR0SvmSetMsrPermission(pVCpu, MSR_K8_TSC_AUX, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
pSvmTransient->fRestoreTscAuxMsr = true;
}
else
{
hmR0SvmSetMsrPermission(pVCpu, MSR_K8_TSC_AUX, SVMMSREXIT_INTERCEPT_READ, SVMMSREXIT_INTERCEPT_WRITE);
pSvmTransient->fRestoreTscAuxMsr = false;
}
/* If VMCB Clean bits isn't supported by the CPU, simply mark all state-bits as dirty, indicating (re)load-from-VMCB. */
}
/**
* Wrapper for running the guest code in AMD-V.
*
* @returns VBox strict status code.
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
* @param pCtx Pointer to the guest-CPU context.
*
* @remarks No-long-jump zone!!!
*/
{
/*
* 64-bit Windows uses XMM registers in the kernel as the Microsoft compiler expresses floating-point operations
* using SSE instructions. Some XMM registers (XMM6-XMM15) are callee-saved and thus the need for this XMM wrapper.
* Refer MSDN docs. "Configuring Programs for 64-bit / x64 Software Conventions / Register Usage" for details.
*/
#ifdef VBOX_WITH_KERNEL_USING_XMM
return HMR0SVMRunWrapXMM(pVCpu->hm.s.svm.HCPhysVmcbHost, pVCpu->hm.s.svm.HCPhysVmcb, pCtx, pVM, pVCpu,
#else
return pVCpu->hm.s.svm.pfnVMRun(pVCpu->hm.s.svm.HCPhysVmcbHost, pVCpu->hm.s.svm.HCPhysVmcb, pCtx, pVM, pVCpu);
#endif
}
/**
* Performs some essential restoration of state after running guest code in
* AMD-V.
*
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
* @param pMixedCtx Pointer to the guest-CPU context. The data maybe
* out-of-sync. Make sure to update the required fields
* before using them.
* @param pSvmTransient Pointer to the SVM transient structure.
* @param rcVMRun Return code of VMRUN.
*
* @remarks Called with interrupts disabled.
* @remarks No-long-jump zone!!! This function will however re-enable longjmps
* unconditionally when it is safe to do so.
*/
static void hmR0SvmPostRunGuest(PVM pVM, PVMCPU pVCpu, PCPUMCTX pMixedCtx, PSVMTRANSIENT pSvmTransient, int rcVMRun)
{
ASMAtomicWriteBool(&pVCpu->hm.s.fCheckedTLBFlush, false); /* See HMInvalidatePageOnAllVCpus(): used for TLB-shootdowns. */
ASMAtomicIncU32(&pVCpu->hm.s.cWorldSwitchExits); /* Initialized in vmR3CreateUVM(): used for TLB-shootdowns. */
pVmcb->ctrl.u64VmcbCleanBits = HMSVM_VMCB_CLEAN_ALL; /* Mark the VMCB-state cache as unmodified by VMM. */
{
}
/* If VMRUN failed, we can bail out early. This does -not- cover SVM_EXIT_INVALID. */
{
return;
}
pSvmTransient->fVectoringDoublePF = false; /* Vectoring double page-fault needs to be determined later. */
hmR0SvmSaveGuestState(pVCpu, pMixedCtx); /* Save the guest state from the VMCB to the guest-CPU context. */
{
{
/* TPR patching (for 32-bit guests) uses LSTAR MSR for holding the TPR value, otherwise uses the VTPR. */
{
}
{
}
}
}
}
/**
* Runs the guest code using AMD-V.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
*/
{
SvmTransient.fUpdateTscOffsetting = true;
int rc = VERR_INTERNAL_ERROR_5;
for (;; cLoops++)
{
Assert(!HMR0SuspendPending());
/* Preparatory work for running guest code, this may force us to return
to ring-3. This bugger disables interrupts on VINF_SUCCESS! */
if (rc != VINF_SUCCESS)
break;
/*
* No longjmps to ring-3 from this point on!!!
* Asserts() will still longjmp to ring-3 (but won't return), which is intentional, better than a kernel panic.
* This also disables flushing of the R0-logger instance (if any).
*/
/* Restore any residual host-state and save any bits shared between host
and guest into the guest-CPU state. Re-enables interrupts! */
|| SvmTransient.u64ExitCode == (uint64_t)SVM_EXIT_INVALID)) /* Check for invalid guest-state errors. */
{
if (rc == VINF_SUCCESS)
break;
}
/* Handle the #VMEXIT. */
if (rc != VINF_SUCCESS)
break;
{
break;
}
}
return rc;
}
/**
* Runs the guest code using AMD-V in single step mode.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
* @param pCtx Pointer to the guest-CPU context.
*/
{
SvmTransient.fUpdateTscOffsetting = true;
int rc = VERR_INTERNAL_ERROR_5;
for (;; cLoops++)
{
Assert(!HMR0SuspendPending());
/* Preparatory work for running guest code, this may force us to return
to ring-3. This bugger disables interrupts on VINF_SUCCESS! */
if (rc != VINF_SUCCESS)
break;
/*
* No longjmps to ring-3 from this point on!!!
* Asserts() will still longjmp to ring-3 (but won't return), which is intentional, better than a kernel panic.
* This also disables flushing of the R0-logger instance (if any).
*/
/*
* Restore any residual host-state and save any bits shared between host and guest into the guest-CPU state.
* This will also re-enable longjmps to ring-3 when it has reached a safe point!!!
*/
|| SvmTransient.u64ExitCode == (uint64_t)SVM_EXIT_INVALID)) /* Check for invalid guest-state errors. */
{
if (rc == VINF_SUCCESS)
return rc;
}
/* Handle the #VMEXIT. */
if (rc != VINF_SUCCESS)
break;
{
break;
}
/*
* Did the RIP change, if so, consider it a single step.
* Otherwise, make sure one of the TFs gets set.
*/
{
break;
}
}
/*
* Clear the X86_EFL_TF if necessary.
*/
{
}
return rc;
}
/**
* Runs the guest code using AMD-V.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
* @param pCtx Pointer to the guest-CPU context.
*/
{
int rc;
else
if (rc == VERR_EM_INTERPRETER)
else if (rc == VINF_EM_RESET)
/* Prepare to return to ring-3. This will remove longjmp notifications. */
return rc;
}
/**
* Handles a #VMEXIT (for all EXITCODE values except SVM_EXIT_INVALID).
*
* @returns VBox status code (informational status codes included).
* @param pVCpu Pointer to the VMCPU.
* @param pCtx Pointer to the guest-CPU context.
* @param pSvmTransient Pointer to the SVM transient structure.
*/
{
/*
* The ordering of the case labels is based on most-frequently-occurring #VMEXITs for most guests under
* normal workloads (for some definition of "normal").
*/
switch (pSvmTransient->u64ExitCode)
{
case SVM_EXIT_NPF:
case SVM_EXIT_IOIO:
case SVM_EXIT_RDTSC:
case SVM_EXIT_RDTSCP:
case SVM_EXIT_CPUID:
case SVM_EXIT_EXCEPTION_E: /* X86_XCPT_PF */
case SVM_EXIT_EXCEPTION_7: /* X86_XCPT_NM */
case SVM_EXIT_EXCEPTION_6: /* X86_XCPT_UD */
case SVM_EXIT_EXCEPTION_10: /* X86_XCPT_MF */
case SVM_EXIT_EXCEPTION_1: /* X86_XCPT_DB */
case SVM_EXIT_MONITOR:
case SVM_EXIT_MWAIT:
case SVM_EXIT_HLT:
case SVM_EXIT_READ_CR0:
case SVM_EXIT_READ_CR3:
case SVM_EXIT_READ_CR4:
case SVM_EXIT_WRITE_CR0:
case SVM_EXIT_WRITE_CR3:
case SVM_EXIT_WRITE_CR4:
case SVM_EXIT_WRITE_CR8:
case SVM_EXIT_VMMCALL:
case SVM_EXIT_VINTR:
case SVM_EXIT_INTR:
case SVM_EXIT_FERR_FREEZE:
case SVM_EXIT_NMI:
case SVM_EXIT_MSR:
case SVM_EXIT_INVLPG:
case SVM_EXIT_WBINVD:
case SVM_EXIT_INVD:
case SVM_EXIT_RDPMC:
default:
{
switch (pSvmTransient->u64ExitCode)
{
case SVM_EXIT_READ_DR14: case SVM_EXIT_READ_DR15:
case SVM_EXIT_WRITE_DR10: case SVM_EXIT_WRITE_DR11: case SVM_EXIT_WRITE_DR12: case SVM_EXIT_WRITE_DR13:
case SVM_EXIT_WRITE_DR14: case SVM_EXIT_WRITE_DR15:
case SVM_EXIT_XSETBV:
case SVM_EXIT_TASK_SWITCH:
case SVM_EXIT_IRET:
case SVM_EXIT_SHUTDOWN:
case SVM_EXIT_SMI:
case SVM_EXIT_INIT:
{
/*
* We don't intercept NMIs. As for INIT signals, it really shouldn't ever happen here. If it ever does,
* we want to know about it so log the exit code and bail.
*/
AssertMsgFailed(("hmR0SvmHandleExit: Unexpected exit %#RX32\n", (uint32_t)pSvmTransient->u64ExitCode));
return VERR_SVM_UNEXPECTED_EXIT;
}
case SVM_EXIT_INVLPGA:
case SVM_EXIT_RSM:
case SVM_EXIT_VMRUN:
case SVM_EXIT_VMLOAD:
case SVM_EXIT_VMSAVE:
case SVM_EXIT_STGI:
case SVM_EXIT_CLGI:
case SVM_EXIT_SKINIT:
#ifdef HMSVM_ALWAYS_TRAP_ALL_XCPTS
case SVM_EXIT_EXCEPTION_0: /* X86_XCPT_DE */
/* SVM_EXIT_EXCEPTION_1: */ /* X86_XCPT_DB - Handled above. */
case SVM_EXIT_EXCEPTION_2: /* X86_XCPT_NMI */
case SVM_EXIT_EXCEPTION_3: /* X86_XCPT_BP */
case SVM_EXIT_EXCEPTION_4: /* X86_XCPT_OF */
case SVM_EXIT_EXCEPTION_5: /* X86_XCPT_BR */
/* case SVM_EXIT_EXCEPTION_6: */ /* X86_XCPT_UD - Handled above. */
/* SVM_EXIT_EXCEPTION_7: */ /* X86_XCPT_NM - Handled above. */
case SVM_EXIT_EXCEPTION_8: /* X86_XCPT_DF */
case SVM_EXIT_EXCEPTION_9: /* X86_XCPT_CO_SEG_OVERRUN */
case SVM_EXIT_EXCEPTION_A: /* X86_XCPT_TS */
case SVM_EXIT_EXCEPTION_B: /* X86_XCPT_NP */
case SVM_EXIT_EXCEPTION_C: /* X86_XCPT_SS */
case SVM_EXIT_EXCEPTION_D: /* X86_XCPT_GP */
/* SVM_EXIT_EXCEPTION_E: */ /* X86_XCPT_PF - Handled above. */
/* SVM_EXIT_EXCEPTION_10: */ /* X86_XCPT_MF - Handled above. */
case SVM_EXIT_EXCEPTION_11: /* X86_XCPT_AC */
case SVM_EXIT_EXCEPTION_12: /* X86_XCPT_MC */
case SVM_EXIT_EXCEPTION_13: /* X86_XCPT_XF */
case SVM_EXIT_EXCEPTION_F: /* Reserved */
{
Event.u = 0;
{
case X86_XCPT_DE:
break;
case X86_XCPT_BP:
/** Saves the wrong EIP on the stack (pointing to the int3) instead of the
* next instruction. */
/** @todo Investigate this later. */
break;
case X86_XCPT_NP:
break;
case X86_XCPT_SS:
break;
case X86_XCPT_GP:
break;
default:
AssertMsgFailed(("hmR0SvmHandleExit: Unexpected exit caused by exception %#x\n", Event.n.u8Vector));
return VERR_SVM_UNEXPECTED_XCPT_EXIT;
}
Log4(("#Xcpt: Vector=%#x at CS:RIP=%04x:%RGv\n", Event.n.u8Vector, pCtx->cs.Sel, (RTGCPTR)pCtx->rip));
return VINF_SUCCESS;
}
#endif /* HMSVM_ALWAYS_TRAP_ALL_XCPTS */
default:
{
return VERR_SVM_UNKNOWN_EXIT;
}
}
}
}
return VERR_INTERNAL_ERROR_5; /* Should never happen. */
}
#ifdef DEBUG
# define HMSVM_ASSERT_PREEMPT_CPUID_VAR() \
# define HMSVM_ASSERT_PREEMPT_CPUID() \
do \
{ \
RTCPUID const idAssertCpuNow = RTThreadPreemptIsEnabled(NIL_RTTHREAD) ? NIL_RTCPUID : RTMpCpuId(); \
} while (0)
# define HMSVM_VALIDATE_EXIT_HANDLER_PARAMS() \
do { \
Assert(ASMIntAreEnabled()); \
Log4Func(("vcpu[%u] -v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-\n", (uint32_t)pVCpu->idCpu)); \
if (VMMR0IsLogFlushDisabled(pVCpu)) \
} while (0)
#else /* Release builds */
# define HMSVM_VALIDATE_EXIT_HANDLER_PARAMS() do { NOREF(pVCpu); NOREF(pCtx); NOREF(pSvmTransient); } while (0)
#endif
/**
* Worker for hmR0SvmInterpretInvlpg().
*
* @return VBox status code.
* @param pVCpu Pointer to the VMCPU.
* @param pCpu Pointer to the disassembler state.
* @param pCtx The guest CPU context.
*/
{
if (RT_FAILURE(rc))
return VERR_EM_INTERPRETER;
{
return VERR_EM_INTERPRETER;
}
else
{
}
return rc;
}
/**
* Interprets INVLPG.
*
* @returns VBox status code.
* @retval VINF_* Scheduling instructions.
* @retval VERR_EM_INTERPRETER Something we can't cope with.
* @retval VERR_* Fatal errors.
*
* @param pVM Pointer to the VM.
* @param pCtx The guest CPU context.
*
* @remarks Updates the RIP if the instruction was executed successfully.
*/
{
/* Only allow 32 & 64 bit code. */
{
if ( RT_SUCCESS(rc)
{
if (RT_SUCCESS(rc))
return rc;
}
else
Log4(("hmR0SvmInterpretInvlpg: EMInterpretDisasCurrent returned %Rrc uOpCode=%#x\n", rc, pDis->pCurInstr->uOpcode));
}
return VERR_EM_INTERPRETER;
}
/**
* Sets an invalid-opcode (#UD) exception as pending-for-injection into the VM.
*
* @param pVCpu Pointer to the VMCPU.
*/
{
Event.u = 0;
}
/**
* Sets a debug (#DB) exception as pending-for-injection into the VM.
*
* @param pVCpu Pointer to the VMCPU.
*/
{
Event.u = 0;
}
/**
* Sets a page fault (#PF) exception as pending-for-injection into the VM.
*
* @param pVCpu Pointer to the VMCPU.
* @param pCtx Pointer to the guest-CPU context.
* @param u32ErrCode The error-code for the page-fault.
* @param uFaultAddress The page fault address (CR2).
*
* @remarks This updates the guest CR2 with @a uFaultAddress!
*/
DECLINLINE(void) hmR0SvmSetPendingXcptPF(PVMCPU pVCpu, PCPUMCTX pCtx, uint32_t u32ErrCode, RTGCUINTPTR uFaultAddress)
{
Event.u = 0;
/* Update CR2 of the guest. */
{
}
}
/**
* Sets a device-not-available (#NM) exception as pending-for-injection into the
* VM.
*
* @param pVCpu Pointer to the VMCPU.
*/
{
Event.u = 0;
}
/**
* Sets a math-fault (#MF) exception as pending-for-injection into the VM.
*
* @param pVCpu Pointer to the VMCPU.
*/
{
Event.u = 0;
}
/**
* Sets a double fault (#DF) exception as pending-for-injection into the VM.
*
* @param pVCpu Pointer to the VMCPU.
*/
{
Event.u = 0;
Event.n.u32ErrorCode = 0;
}
/**
* Emulates a simple MOV TPR (CR8) instruction, used for TPR patching on 32-bit
* guests. This simply looks up the patch record at EIP and does the required.
*
* like how we want it to be (e.g. not followed by shr 4 as is usually done for
* TPR). See hmR3ReplaceTprInstr() for the details.
*
* @returns VBox status code.
* @retval VINF_SUCCESS if the access was handled successfully.
* @retval VERR_NOT_FOUND if no patch record for this RIP could be found.
* @retval VERR_SVM_UNEXPECTED_PATCH_TYPE if the found patch type is invalid.
*
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
* @param pCtx Pointer to the guest-CPU context.
*/
{
/*
* We do this in a loop as we increment the RIP after a successful emulation
* and the new RIP may be a patched instruction which needs emulation as well.
*/
bool fPatchFound = false;
for (;;)
{
bool fPending;
if (!pPatch)
break;
fPatchFound = true;
{
case HMTPRINSTR_READ:
{
break;
}
case HMTPRINSTR_WRITE_REG:
case HMTPRINSTR_WRITE_IMM:
{
{
}
else
break;
}
default:
return VERR_SVM_UNEXPECTED_PATCH_TYPE;
}
}
if (fPatchFound)
return VINF_SUCCESS;
return VERR_NOT_FOUND;
}
/**
* Determines if an exception is a contributory exception.
*
* Contributory exceptions are ones which can cause double-faults unless the
* original exception was a benign exception. Page-fault is intentionally not
* included here as it's a conditional contributory exception.
*
* @returns true if the exception is contributory, false otherwise.
* @param uVector The exception vector.
*/
{
switch (uVector)
{
case X86_XCPT_GP:
case X86_XCPT_SS:
case X86_XCPT_NP:
case X86_XCPT_TS:
case X86_XCPT_DE:
return true;
default:
break;
}
return false;
}
/**
* Handle a condition that occurred while delivering an event through the guest
* IDT.
*
* @returns VBox status code (informational error codes included).
* @retval VINF_SUCCESS if we should continue handling the #VMEXIT.
* @retval VINF_HM_DOUBLE_FAULT if a #DF condition was detected and we ought to
* continue execution of the guest which will delivery the #DF.
* @retval VINF_EM_RESET if we detected a triple-fault condition.
*
* @param pVCpu Pointer to the VMCPU.
* @param pCtx Pointer to the guest-CPU context.
* @param pSvmTransient Pointer to the SVM transient structure.
*
* @remarks No-long-jump zone!!!
*/
static int hmR0SvmCheckExitDueToEventDelivery(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
{
int rc = VINF_SUCCESS;
/* See AMD spec. 15.7.3 "EXITINFO Pseudo-Code". The EXITINTINFO (if valid) contains the prior exception (IDT vector)
* that was trying to be delivered to the guest which caused a #VMEXIT which was intercepted (Exit vector). */
{
typedef enum
{
SVMREFLECTXCPT_XCPT, /* Reflect the exception to the guest or for further evaluation by VMM. */
SVMREFLECTXCPT_DF, /* Reflect the exception as a double-fault to the guest. */
SVMREFLECTXCPT_TF, /* Indicate a triple faulted state to the VMM. */
SVMREFLECTXCPT_NONE /* Nothing to reflect. */
bool fReflectingNmi = false;
{
{
#ifdef VBOX_STRICT
&& uExitVector == X86_XCPT_PF)
{
}
#endif
if ( uExitVector == X86_XCPT_PF
&& uIdtVector == X86_XCPT_PF)
{
pSvmTransient->fVectoringDoublePF = true;
}
|| uIdtVector == X86_XCPT_PF))
{
Log4(("IDT: Pending vectoring #DF %#RX64 uIdtVector=%#x uExitVector=%#x\n", pVCpu->hm.s.Event.u64IntInfo,
}
else if (uIdtVector == X86_XCPT_DF)
{
Log4(("IDT: Pending vectoring triple-fault %#RX64 uIdtVector=%#x uExitVector=%#x\n",
}
else
}
else
{
/*
* If event delivery caused an #VMEXIT that is not an exception (e.g. #NPF) then reflect the original
* exception to the guest after handling the #VMEXIT.
*/
}
}
{
{
if (uExitVector == X86_XCPT_PF)
{
pSvmTransient->fVectoringPF = true;
}
}
}
/* else: Ignore software interrupts (INT n) as they reoccur when restarting the instruction. */
switch (enmReflect)
{
case SVMREFLECTXCPT_XCPT:
{
/* If we are re-injecting the NMI, clear NMI blocking. */
if (fReflectingNmi)
/* If uExitVector is #PF, CR2 value will be updated from the VMCB if it's a guest #PF. See hmR0SvmExitXcptPF(). */
Log4(("IDT: Pending vectoring event %#RX64 ErrValid=%RTbool Err=%#RX32\n", pVmcb->ctrl.ExitIntInfo.u,
break;
}
case SVMREFLECTXCPT_DF:
{
break;
}
case SVMREFLECTXCPT_TF:
{
rc = VINF_EM_RESET;
break;
}
default:
break;
}
}
return rc;
}
/**
* Advances the guest RIP in the if the NRIP_SAVE feature is supported by the
* CPU, otherwise advances the RIP by @a cb bytes.
*
* @param pVCpu Pointer to the VMCPU.
* @param pCtx Pointer to the guest-CPU context.
* @param cb RIP increment value in bytes.
*
* @remarks Use this function only from #VMEXIT's where the NRIP value is valid
* when NRIP_SAVE is supported by the CPU!
*/
{
{
}
else
}
/* -=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #VMEXIT handlers -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/* -=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/** @name #VMEXIT handlers.
* @{
*/
/**
* #VMEXIT handler for external interrupts, NMIs, FPU assertion freeze and INIT
* signals (SVM_EXIT_INTR, SVM_EXIT_NMI, SVM_EXIT_FERR_FREEZE, SVM_EXIT_INIT).
*/
{
/*
* AMD-V has no preemption timer and the generic periodic preemption timer has no way to signal -before- the timer
* fires if the current interrupt is our own timer or a some other host interrupt. We also cannot examine what
* interrupt it is until the host actually take the interrupt.
*
* Going back to executing guest code here unconditionally causes random scheduling problems (observed on an
* AMD Phenom 9850 Quad-Core on Windows 64-bit host).
*/
return VINF_EM_RAW_INTERRUPT;
}
/**
* #VMEXIT handler for WBINVD (SVM_EXIT_WBINVD). Conditional #VMEXIT.
*/
{
int rc = VINF_SUCCESS;
return rc;
}
/**
* #VMEXIT handler for INVD (SVM_EXIT_INVD). Unconditional #VMEXIT.
*/
{
int rc = VINF_SUCCESS;
return rc;
}
/**
* #VMEXIT handler for INVD (SVM_EXIT_CPUID). Conditional #VMEXIT.
*/
{
{
}
else
{
}
return rc;
}
/**
* #VMEXIT handler for RDTSC (SVM_EXIT_RDTSC). Conditional #VMEXIT.
*/
{
{
pSvmTransient->fUpdateTscOffsetting = true;
/* Single step check. */
}
else
{
}
return rc;
}
/**
* #VMEXIT handler for RDTSCP (SVM_EXIT_RDTSCP). Conditional #VMEXIT.
*/
{
{
pSvmTransient->fUpdateTscOffsetting = true;
}
else
{
}
return rc;
}
/**
* #VMEXIT handler for RDPMC (SVM_EXIT_RDPMC). Conditional #VMEXIT.
*/
{
{
}
else
{
}
return rc;
}
/**
* #VMEXIT handler for INVLPG (SVM_EXIT_INVLPG). Conditional #VMEXIT.
*/
{
/** @todo Decode Assist. */
return rc;
}
/**
* #VMEXIT handler for HLT (SVM_EXIT_HLT). Conditional #VMEXIT.
*/
{
if (rc != VINF_SUCCESS)
return rc;
}
/**
* #VMEXIT handler for MONITOR (SVM_EXIT_MONITOR). Conditional #VMEXIT.
*/
{
{
}
else
{
AssertMsg(rc == VERR_EM_INTERPRETER, ("hmR0SvmExitMonitor: EMInterpretMonitor failed with %Rrc\n", rc));
}
return rc;
}
/**
* #VMEXIT handler for MWAIT (SVM_EXIT_MWAIT). Conditional #VMEXIT.
*/
{
if ( rc == VINF_EM_HALT
|| rc == VINF_SUCCESS)
{
if ( rc == VINF_EM_HALT
{
rc = VINF_SUCCESS;
}
}
else
{
AssertMsg(rc == VERR_EM_INTERPRETER, ("hmR0SvmExitMwait: EMInterpretMWait failed with %Rrc\n", rc));
}
("hmR0SvmExitMwait: EMInterpretMWait failed rc=%Rrc\n", rc));
return rc;
}
/**
* #VMEXIT handler for shutdown (triple-fault) (SVM_EXIT_SHUTDOWN).
* Conditional #VMEXIT.
*/
{
return VINF_EM_RESET;
}
/**
* #VMEXIT handler for CRx reads (SVM_EXIT_READ_CR*). Conditional #VMEXIT.
*/
{
/** @todo Decode Assist. */
AssertMsg(rc == VINF_SUCCESS || rc == VERR_EM_INTERPRETER || rc == VINF_PGM_CHANGE_MODE || rc == VINF_PGM_SYNC_CR3,
("hmR0SvmExitReadCRx: EMInterpretInstruction failed rc=%Rrc\n", rc));
return rc;
}
/**
* #VMEXIT handler for CRx writes (SVM_EXIT_WRITE_CR*). Conditional #VMEXIT.
*/
{
/** @todo Decode Assist. */
if (rc == VINF_SUCCESS)
{
/* RIP has been updated by EMInterpretInstruction(). */
{
case 0: /* CR0. */
break;
case 3: /* CR3. */
break;
case 4: /* CR4. */
break;
case 8: /* CR8 (TPR). */
break;
default:
AssertMsgFailed(("hmR0SvmExitWriteCRx: Invalid/Unexpected Write-CRx exit. u64ExitCode=%#RX64 %#x CRx=%#RX64\n",
break;
}
}
else
return rc;
}
/**
* #VMEXIT handler for instructions that result in a #UD exception delivered to
* the guest.
*/
HMSVM_EXIT_DECL hmR0SvmExitSetPendingXcptUD(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
{
return VINF_SUCCESS;
}
/**
* #VMEXIT handler for MSR read and writes (SVM_EXIT_MSR). Conditional #VMEXIT.
*/
{
int rc;
{
/* Handle TPR patching; intercepted LSTAR write. */
{
{
/* Our patch code uses LSTAR for TPR caching for 32-bit guests. */
}
rc = VINF_SUCCESS;
return rc;
}
{
{
}
else
}
else
{
else
AssertMsg(rc == VERR_EM_INTERPRETER, ("hmR0SvmExitMsr: WrMsr. EMInterpretInstruction failed rc=%Rrc\n", rc));
}
if (rc == VINF_SUCCESS)
{
/* If this is an X2APIC WRMSR access, update the APIC state as well. */
{
/*
* We've already saved the APIC related guest-state (TPR) in hmR0SvmPostRunGuest(). When full APIC register
* virtualization is implemented we'll have to make sure APIC state is saved from the VMCB before
* EMInterpretWrmsr() changes it.
*/
}
pSvmTransient->fUpdateTscOffsetting = true;
}
}
else
{
/* MSR Read access. */
{
{
}
else
}
else
{
AssertMsg(rc == VERR_EM_INTERPRETER, ("hmR0SvmExitMsr: RdMsr. EMInterpretInstruction failed rc=%Rrc\n", rc));
/* RIP updated by EMInterpretInstruction(). */
}
}
/* RIP has been updated by EMInterpret[Rd|Wr]msr(). */
return rc;
}
/**
* #VMEXIT handler for DRx read (SVM_EXIT_READ_DRx). Conditional #VMEXIT.
*/
{
/* We should -not- get this #VMEXIT if the guest's debug registers were active. */
{
AssertMsgFailed(("hmR0SvmHandleExit: Unexpected exit %#RX32\n", (uint32_t)pSvmTransient->u64ExitCode));
return VERR_SVM_UNEXPECTED_EXIT;
}
/*
* Lazy DR0-3 loading.
*/
{
Log5(("hmR0SvmExitReadDRx: Lazy loading guest debug registers\n"));
/* Don't intercept DRx read and writes. */
/* We're playing with the host CPU state here, make sure we don't preempt or longjmp. */
/* Save the host & load the guest debug state, restart execution of the MOV DRx instruction. */
return VINF_SUCCESS;
}
/*
*/
/** @todo Decode assist. */
{
/* Not necessary for read accesses but whatever doesn't hurt for now, will be fixed with decode assist. */
/** @todo CPUM should set this flag! */
}
else
return VBOXSTRICTRC_TODO(rc);
}
/**
* #VMEXIT handler for DRx write (SVM_EXIT_WRITE_DRx). Conditional #VMEXIT.
*/
{
/* For now it's the same since we interpret the instruction anyway. Will change when using of Decode Assist is implemented. */
return rc;
}
/**
* #VMEXIT handler for XCRx write (SVM_EXIT_XSETBV). Conditional #VMEXIT.
*/
{
/** @todo decode assists... */
if (rcStrict == VINF_IEM_RAISED_XCPT)
return VBOXSTRICTRC_TODO(rcStrict);
}
/**
* #VMEXIT handler for I/O instructions (SVM_EXIT_IOIO). Conditional #VMEXIT.
*/
{
/* I/O operation lookup arrays. */
static uint32_t const s_aIOSize[8] = { 0, 1, 2, 0, 4, 0, 0, 0 }; /* Size of the I/O accesses in bytes. */
static uint32_t const s_aIOOpAnd[8] = { 0, 0xff, 0xffff, 0, 0xffffffff, 0, 0, 0 }; /* AND masks for saving
/* Refer AMD spec. 15.10.2 "IN and OUT Behaviour" and Figure 15-2. "EXITINFO1 for IOIO Intercept" for the format. */
if (RT_UNLIKELY(!cbValue))
{
return VERR_EM_INTERPRETER;
}
if (IoExitInfo.n.u1STR)
{
/** @todo Huh? why can't we use the segment prefix information given by AMD-V
* in EXITINFO1? Investigate once this thing is up and running. */
if (rcStrict == VINF_SUCCESS)
{
{
}
else
{
}
}
else
}
else
{
{
if (rcStrict == VINF_IOM_R3_IOPORT_WRITE)
HMR0SavePendingIOPortWrite(pVCpu, pCtx->rip, pVmcb->ctrl.u64ExitInfo2, IoExitInfo.n.u16Port, uAndVal, cbValue);
}
else
{
if (IOM_SUCCESS(rcStrict))
{
}
else if (rcStrict == VINF_IOM_R3_IOPORT_READ)
HMR0SavePendingIOPortRead(pVCpu, pCtx->rip, pVmcb->ctrl.u64ExitInfo2, IoExitInfo.n.u16Port, uAndVal, cbValue);
}
}
if (IOM_SUCCESS(rcStrict))
{
/* AMD-V saves the RIP of the instruction following the IO instruction in EXITINFO2. */
/*
* If any I/O breakpoints are armed, we need to check if one triggered
* and take appropriate action.
* Note that the I/O breakpoint type is undefined if CR4.DE is 0.
*/
/** @todo Optimize away the DBGFBpIsHwIoArmed call by having DBGF tell the
* execution engines about whether hyper BPs and such are pending. */
|| DBGFBpIsHwIoArmed(pVM)))
{
/* We're playing with the host CPU state here, make sure we don't preempt or longjmp. */
if (rcStrict2 == VINF_EM_RAW_GUEST_TRAP)
{
/* Raise #DB. */
}
/* rcStrict is VINF_SUCCESS or in [VINF_EM_FIRST..VINF_EM_LAST]. */
else if ( rcStrict2 != VINF_SUCCESS
}
}
#ifdef VBOX_STRICT
if (rcStrict == VINF_IOM_R3_IOPORT_READ)
else if (rcStrict == VINF_IOM_R3_IOPORT_WRITE)
else
{
/** @todo r=bird: This is missing a bunch of VINF_EM_FIRST..VINF_EM_LAST
* statuses, that the VMM device and some others may return. See
* IOM_SUCCESS() for guidance. */
|| rcStrict == VINF_SUCCESS
}
#endif
return VBOXSTRICTRC_TODO(rcStrict);
}
/**
* #VMEXIT handler for Nested Page-faults (SVM_EXIT_NPF). Conditional
* #VMEXIT.
*/
{
/* See AMD spec. 15.25.6 "Nested versus Guest Page Faults, Fault Ordering" for VMCB details for #NPF. */
Log4(("#NPF at CS:RIP=%04x:%#RX64 faultaddr=%RGp errcode=%#x \n", pCtx->cs.Sel, pCtx->rip, GCPhysFaultAddr, u32ErrCode));
#ifdef VBOX_HM_WITH_GUEST_PATCHING
/* TPR patching for 32-bit guests, using the reserved bit in the page tables for MMIO regions. */
|| (u32ErrCode & (X86_TRAP_PF_P | X86_TRAP_PF_RSVD)) == (X86_TRAP_PF_P | X86_TRAP_PF_RSVD)) /* MMIO page. */
&& !CPUMGetGuestCPL(pVCpu)
{
{
/* Only attempt to patch the instruction once. */
if (!pPatch)
return VINF_EM_HM_PATCH_TPR_INSTR;
}
}
#endif
/*
* Determine the nested paging mode.
*/
#if HC_ARCH_BITS == 32
if (CPUMIsGuestInLongModeEx(pCtx))
else
#endif
/*
* MMIO optimization using the reserved (RSVD) bit in the guest page tables for MMIO pages.
*/
int rc;
{
VBOXSTRICTRC rc2 = PGMR0Trap0eHandlerNPMisconfig(pVM, pVCpu, enmNestedPagingMode, CPUMCTX2CORE(pCtx), GCPhysFaultAddr,
/*
* If we succeed, resume guest execution.
* If we fail in interpreting the instruction because we couldn't get the guest physical address
* of the page containing the instruction via the guest's page tables (we would invalidate the guest page
* in the host TLB), resume execution which would cause a guest page fault to let the guest handle this
* weird case. See @bugref{6043}.
*/
if ( rc == VINF_SUCCESS
|| rc == VERR_PAGE_NOT_PRESENT)
{
/* Successfully handled MMIO operation. */
rc = VINF_SUCCESS;
}
return rc;
}
rc = PGMR0Trap0eHandlerNestedPaging(pVM, pVCpu, enmNestedPagingMode, u32ErrCode, CPUMCTX2CORE(pCtx), GCPhysFaultAddr);
Log4(("#NPF: PGMR0Trap0eHandlerNestedPaging returned %Rrc CS:RIP=%04x:%#RX64\n", rc, pCtx->cs.Sel, pCtx->rip));
/*
* Same case as PGMR0Trap0eHandlerNPMisconfig(). See comment above, @bugref{6043}.
*/
if ( rc == VINF_SUCCESS
|| rc == VERR_PAGE_NOT_PRESENT)
{
/* We've successfully synced our shadow page tables. */
rc = VINF_SUCCESS;
}
return rc;
}
/**
* #VMEXIT handler for virtual interrupt (SVM_EXIT_VINTR). Conditional #VMEXIT.
*/
{
pVmcb->ctrl.IntCtrl.n.u1VIrqValid = 0; /* No virtual interrupts pending, we'll inject the current one/NMI before reentry. */
/* Indicate that we no longer need to #VMEXIT when the guest is ready to receive interrupts/NMIs, it is now ready. */
/* Deliver the pending interrupt/NMI via hmR0SvmEvaluatePendingEvent() and resume guest execution. */
return VINF_SUCCESS;
}
/**
* #VMEXIT handler for task switches (SVM_EXIT_TASK_SWITCH). Conditional #VMEXIT.
*/
{
#ifndef HMSVM_ALWAYS_TRAP_TASK_SWITCH
#endif
/* Check if this task-switch occurred while delivery an event through the guest IDT. */
&& pVCpu->hm.s.Event.fPending) /** @todo fPending cannot be 'true', see hmR0SvmInjectPendingEvent(). See @bugref{7362}.*/
{
/*
* AMD-V does not provide us with the original exception but we have it in u64IntInfo since we
* injected the event during VM-entry.
*/
Log4(("hmR0SvmExitTaskSwitch: TS occurred during event delivery.\n"));
return VINF_EM_RAW_INJECT_TRPM_EVENT;
}
/** @todo Emulate task switch someday, currently just going back to ring-3 for
* emulation. */
return VERR_EM_INTERPRETER;
}
/**
* #VMEXIT handler for VMMCALL (SVM_EXIT_VMMCALL). Conditional #VMEXIT.
*/
{
/* First check if this is a patched VMMCALL for mov TPR */
if (rc == VINF_SUCCESS)
{
return VINF_SUCCESS;
}
else if (rc == VERR_NOT_FOUND)
{
{
if (RT_SUCCESS(rc))
{
/* If the hypercall changes anything other than guest general-purpose registers,
we would need to reload the guest changed bits here before VM-reentry. */
return VINF_SUCCESS;
}
}
}
return VINF_SUCCESS;
}
/**
* #VMEXIT handler for IRET (SVM_EXIT_IRET). Conditional #VMEXIT.
*/
{
/* Clear NMI blocking. */
/* Indicate that we no longer need to #VMEXIT when the guest is ready to receive NMIs, it is now ready. */
/* Deliver the pending NMI via hmR0SvmEvaluatePendingEvent() and resume guest execution. */
return VINF_SUCCESS;
}
/**
* #VMEXIT handler for page-fault exceptions (SVM_EXIT_EXCEPTION_E). Conditional
* #VMEXIT.
*/
{
/* See AMD spec. 15.12.15 "#PF (Page Fault)". */
#if defined(HMSVM_ALWAYS_TRAP_ALL_XCPTS) || defined(HMSVM_ALWAYS_TRAP_PF)
{
if (!pSvmTransient->fVectoringDoublePF)
{
/* A genuine guest #PF, reflect it to the guest. */
Log4(("#PF: Guest page fault at %04X:%RGv FaultAddr=%RGv ErrCode=%#x\n", pCtx->cs.Sel, (RTGCPTR)pCtx->rip,
}
else
{
/* A guest page-fault occurred during delivery of a page-fault. Inject #DF. */
Log4(("Pending #DF due to vectoring #PF. NP\n"));
}
return VINF_SUCCESS;
}
#endif
#ifdef VBOX_HM_WITH_GUEST_PATCHING
/* Shortcut for APIC TPR reads and writes; only applicable to 32-bit guests. */
&& !CPUMGetGuestCPL(pVCpu)
{
/* Check if the page at the fault-address is the APIC base. */
if ( rc2 == VINF_SUCCESS
&& GCPhysPage == GCPhysApicBase)
{
/* Only attempt to patch the instruction once. */
if (!pPatch)
return VINF_EM_HM_PATCH_TPR_INSTR;
}
}
#endif
Log4(("#PF: uFaultAddress=%#RX64 CS:RIP=%#04x:%#RX64 u32ErrCode %#RX32 cr3=%#RX64\n", uFaultAddress, pCtx->cs.Sel,
/* If it's a vectoring #PF, emulate injecting the original event injection as PGMTrap0eHandler() is incapable
of differentiating between instruction emulation and event injection that caused a #PF. See @bugref{6607}. */
if (pSvmTransient->fVectoringPF)
{
return VINF_EM_RAW_INJECT_TRPM_EVENT;
}
if (rc == VINF_SUCCESS)
{
/* Successfully synced shadow pages tables or emulated an MMIO instruction. */
return rc;
}
else if (rc == VINF_EM_RAW_GUEST_TRAP)
{
if (!pSvmTransient->fVectoringDoublePF)
{
/* It's a guest page fault and needs to be reflected to the guest. */
}
else
{
/* A guest page-fault occurred during delivery of a page-fault. Inject #DF. */
Log4(("#PF: Pending #DF due to vectoring #PF\n"));
}
return VINF_SUCCESS;
}
return rc;
}
/**
* #VMEXIT handler for device-not-available exceptions (SVM_EXIT_EXCEPTION_7).
* Conditional #VMEXIT.
*/
{
/* We're playing with the host CPU state here, make sure we don't preempt or longjmp. */
int rc;
/* If the guest FPU was active at the time of the #NM exit, then it's a guest fault. */
{
}
else
{
#ifndef HMSVM_ALWAYS_TRAP_ALL_XCPTS
#endif
}
if (rc == VINF_SUCCESS)
{
/* Guest FPU state was activated, we'll want to change CR0 FPU intercepts before the next VM-reentry. */
}
else
{
/* Forward #NM to the guest. */
}
return VINF_SUCCESS;
}
/**
* #VMEXIT handler for undefined opcode (SVM_EXIT_EXCEPTION_6).
* Conditional #VMEXIT.
*/
{
else
return VINF_SUCCESS;
}
/**
* #VMEXIT handler for math-fault exceptions (SVM_EXIT_EXCEPTION_10).
* Conditional #VMEXIT.
*/
{
{
unsigned cbOp;
if (RT_SUCCESS(rc))
{
/* Convert a #MF into a FERR -> IRQ 13. See @bugref{6117}. */
if (RT_SUCCESS(rc))
}
else
Log4(("hmR0SvmExitXcptMF: EMInterpretDisasCurrent returned %Rrc uOpCode=%#x\n", rc, pDis->pCurInstr->uOpcode));
return rc;
}
return VINF_SUCCESS;
}
/**
* #VMEXIT handler for debug exceptions (SVM_EXIT_EXCEPTION_1). Conditional
* #VMEXIT.
*/
{
/* This can be a fault-type #DB (instruction breakpoint) or a trap-type #DB (data breakpoint). However, for both cases
DR6 and DR7 are updated to what the exception handler expects. See AMD spec. 15.12.2 "#DB (Debug)". */
int rc = DBGFRZTrap01Handler(pVM, pVCpu, CPUMCTX2CORE(pCtx), pVmcb->guest.u64DR6, pVCpu->hm.s.fSingleInstruction);
if (rc == VINF_EM_RAW_GUEST_TRAP)
{
/* Reflect the exception back to the guest. */
rc = VINF_SUCCESS;
}
/*
* Update DR6.
*/
{
}
else
{
}
return rc;
}
/** @} */