PGMAll.cpp revision 95d69bacc6bbca7a047193301e80e1511625946e
/* $Id$ */
/** @file
* PGM - Page Manager and Monitor - All context code.
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (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_PGM
#include "PGMInternal.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Stated structure for PGM_GST_NAME(HandlerVirtualUpdate) that's
* passed to PGM_GST_NAME(VirtHandlerUpdateOne) during enumeration.
*/
typedef struct PGMHVUSTATE
{
/** The VM handle. */
/** The todo flags. */
/** The CR4 register value. */
} PGMHVUSTATE, *PPGMHVUSTATE;
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/** @def DUMP_PDE_BIG
* Debug routine for dumping a big PDE.
*/
#ifdef DEBUG_Sander
/** Debug routine for dumping a big PDE. */
{
Log(("%s: BIG %d u10PageNo=%08X P=%d W=%d U=%d CACHE=%d ACC=%d DIR=%d GBL=%d\n", pszPrefix, iPD, Pde.b.u10PageNo, Pde.b.u1Present, Pde.b.u1Write, Pde.b.u1User, Pde.b.u1CacheDisable, Pde.b.u1Accessed, Pde.b.u1Dirty, Pde.b.u1Global));
Log(("%s: BIG %d WRT=%d AVAIL=%X RSV=%X PAT=%d\n", pszPrefix, iPD, Pde.b.u1WriteThru, Pde.b.u3Available, Pde.b.u8PageNoHigh, Pde.b.u1PAT));
}
#define DUMP_PDE_BIG(a, b, c) pgmDumpPDEBig(a, b, c)
#else
#define DUMP_PDE_BIG(a, b, c) do { } while (0)
#endif
#if 1///@todo ndef RT_ARCH_AMD64
/*
* Shadow - 32-bit mode
*/
#define PGM_SHW_TYPE PGM_TYPE_32BIT
#include "PGMAllShw.h"
/* Guest - real mode */
#define PGM_GST_TYPE PGM_TYPE_REAL
#include "PGMAllGst.h"
#include "PGMAllBth.h"
/* Guest - protected mode */
#define PGM_GST_TYPE PGM_TYPE_PROT
#include "PGMAllGst.h"
#include "PGMAllBth.h"
/* Guest - 32-bit mode */
#define PGM_GST_TYPE PGM_TYPE_32BIT
#include "PGMAllGst.h"
#include "PGMAllBth.h"
#endif /* !RT_ARCH_AMD64 */
/*
* Shadow - PAE mode
*/
#define PGM_SHW_TYPE PGM_TYPE_PAE
#include "PGMAllShw.h"
/* Guest - real mode */
#define PGM_GST_TYPE PGM_TYPE_REAL
#include "PGMAllBth.h"
/* Guest - protected mode */
#define PGM_GST_TYPE PGM_TYPE_PROT
#include "PGMAllBth.h"
/* Guest - 32-bit mode */
#define PGM_GST_TYPE PGM_TYPE_32BIT
#include "PGMAllBth.h"
/* Guest - PAE mode */
#define PGM_GST_TYPE PGM_TYPE_PAE
#include "PGMAllGst.h"
#include "PGMAllBth.h"
/*
* Shadow - AMD64 mode
*/
#define PGM_SHW_TYPE PGM_TYPE_AMD64
#include "PGMAllShw.h"
/* Guest - real mode */
#define PGM_GST_TYPE PGM_TYPE_REAL
#include "PGMAllBth.h"
/* Guest - protected mode */
#define PGM_GST_TYPE PGM_TYPE_PROT
#include "PGMAllBth.h"
/* Guest - AMD64 mode */
#define PGM_GST_TYPE PGM_TYPE_AMD64
#include "PGMAllGst.h"
#include "PGMAllBth.h"
/**
* #PF Handler.
*
* @returns VBox status code (appropriate for trap handling and GC return).
* @param pVM VM Handle.
* @param uErr The trap error code.
* @param pRegFrame Trap register frame.
* @param pvFault The fault address.
*/
{
#ifdef VBOX_WITH_STATISTICS
/*
* Error code stats.
*/
if (uErr & X86_TRAP_PF_US)
{
if (!(uErr & X86_TRAP_PF_P))
{
if (uErr & X86_TRAP_PF_RW)
else
}
else if (uErr & X86_TRAP_PF_RW)
else if (uErr & X86_TRAP_PF_RSVD)
else
}
else
{ //supervisor
if (!(uErr & X86_TRAP_PF_P))
{
if (uErr & X86_TRAP_PF_RW)
else
}
else if (uErr & X86_TRAP_PF_RW)
else if (uErr & X86_TRAP_PF_RSVD)
}
#endif
/*
* Call the worker.
*/
if (rc == VINF_PGM_SYNCPAGE_MODIFIED_PDE)
rc = VINF_SUCCESS;
return rc;
}
/**
* Prefetch a page
*
* Typically used to sync commonly used pages before entering raw mode
* after a CR3 reload.
*
* @returns VBox status code suitable for scheduling.
* @retval VINF_SUCCESS on success.
* @retval VINF_PGM_SYNC_CR3 if we're out of shadow pages or something like that.
* @param pVM VM handle.
* @param GCPtrPage Page to invalidate.
*/
{
return rc;
}
/**
* Gets the mapping corresponding to the specified address (if any).
*
* @returns Pointer to the mapping.
* @returns NULL if not
*
* @param pVM The virtual machine.
* @param GCPtr The guest context pointer.
*/
{
while (pMapping)
{
break;
{
return pMapping;
}
}
return NULL;
}
/**
* Verifies a range of pages for read or write access
*
* Only checks the guest's page tables
*
* @returns VBox status code.
* @param pVM VM handle.
* @param Addr Guest virtual address to check
* @param cbSize Access size
* @param fAccess Access type (r/w, user/supervisor (X86_PTE_*))
*/
{
/*
* Validate input.
*/
{
return VERR_INVALID_PARAMETER;
}
if (VBOX_FAILURE(rc))
{
return VINF_EM_RAW_GUEST_TRAP;
}
/*
* Check if the access would cause a page fault
*
* Note that hypervisor page directories are not present in the guest's tables, so this check
* is sufficient.
*/
{
Log(("PGMIsValidAccess: access violation for %VGv attr %#llx vs %d:%d\n", Addr, fPage, fWrite, fUser));
return VINF_EM_RAW_GUEST_TRAP;
}
if ( VBOX_SUCCESS(rc)
return PGMIsValidAccess(pVM, Addr + PAGE_SIZE, (cbSize > PAGE_SIZE) ? cbSize - PAGE_SIZE : 1, fAccess);
return rc;
}
/**
* Verifies a range of pages for read or write access
*
* Supports handling of pages marked for dirty bit tracking and CSAM
*
* @returns VBox status code.
* @param pVM VM handle.
* @param Addr Guest virtual address to check
* @param cbSize Access size
* @param fAccess Access type (r/w, user/supervisor (X86_PTE_*))
*/
{
/*
* Validate input.
*/
{
return VERR_INVALID_PARAMETER;
}
if (VBOX_FAILURE(rc))
{
return VINF_EM_RAW_GUEST_TRAP;
}
/*
* Check if the access would cause a page fault
*
* Note that hypervisor page directories are not present in the guest's tables, so this check
* is sufficient.
*/
{
Log(("PGMVerifyAccess: access violation for %VGv attr %#llx vs %d:%d\n", Addr, fPageGst, fWrite, fUser));
return VINF_EM_RAW_GUEST_TRAP;
}
/*
* Next step is to verify if we protected this page for dirty bit tracking or for CSAM scanning
*/
if ( rc == VERR_PAGE_NOT_PRESENT
|| rc == VERR_PAGE_TABLE_NOT_PRESENT)
{
/*
* Page is not present in our page tables.
* Try to sync it!
*/
if (rc != VINF_SUCCESS)
return rc;
}
else
#if 0 /* def VBOX_STRICT; triggers too often now */
/*
* This check is a bit paranoid, but useful.
*/
/** @note this will assert when writing to monitored pages (a bit annoying actually) */
{
AssertMsgFailed(("Unexpected access violation for %VGv! rc=%Vrc write=%d user=%d\n",
return VINF_EM_RAW_GUEST_TRAP;
}
#endif
if ( VBOX_SUCCESS(rc)
{
/* Don't recursively call PGMVerifyAccess as we might run out of stack. */
for (;;)
{
else
cbSize = 1;
if (rc != VINF_SUCCESS)
break;
break;
}
}
return rc;
}
#ifndef IN_GC
/**
* Emulation of the invlpg instruction (HC only actually).
*
* @returns VBox status code.
* @param pVM VM handle.
* @param GCPtrPage Page to invalidate.
* @remark ASSUMES the page table entry or page directory is
* valid. Fairly safe, but there could be edge cases!
* @todo Flush page or page directory only if necessary!
*/
{
int rc;
/** @todo merge PGMGCInvalidatePage with this one */
#ifndef IN_RING3
/*
* Notify the recompiler so it can record this instruction.
* Failure happens when it's out of space. We'll return to HC in that case.
*/
if (VBOX_FAILURE(rc))
return rc;
#endif
#ifndef IN_RING0
/*
* Check if we have a pending update of the CR3 monitoring.
*/
if ( VBOX_SUCCESS(rc)
{
}
#endif
#ifdef IN_RING3
/*
* Inform CSAM about the flush
*/
/** @note this is to check if monitored pages have been changed; when we implement callbacks for virtual handlers, this is no longer required. */
#endif
return rc;
}
#endif
/**
* Executes an instruction using the interpreter.
*
* @returns VBox status code (appropriate for trap handling and GC return).
* @param pVM VM handle.
* @param pRegFrame Register frame.
* @param pvFault Fault address.
*/
{
#ifdef IN_RING0
/** @todo */
int rc = VINF_EM_RAW_EMULATE_INSTR;
#else
if (rc == VERR_EM_INTERPRETER)
if (rc != VINF_SUCCESS)
#endif
return rc;
}
/**
* Gets effective page information (from the VMM page directory).
*
* @returns VBox status.
* @param pVM VM Handle.
* @param GCPtr Guest Context virtual address of the page.
* @param pfFlags Where to store the flags. These are X86_PTE_*.
* @param pHCPhys Where to store the HC physical address of the page.
* This is page aligned.
* @remark You should use PGMMapGetPage() for pages in a mapping.
*/
{
}
/**
* Sets (replaces) the page flags for a range of pages in the shadow context.
*
* @returns VBox status.
* @param pVM VM handle.
* @param GCPtr The address of the first page.
* @param cb The size of the range in bytes.
* @param fFlags Page flags X86_PTE_*, excluding the page mask of course.
* @remark You must use PGMMapSetPage() for pages in a mapping.
*/
{
}
/**
* Modify page flags for a range of pages in the shadow context.
*
* The existing flags are ANDed with the fMask and ORed with the fFlags.
*
* @returns VBox status code.
* @param pVM VM handle.
* @param GCPtr Virtual address of the first page in the range.
* @param cb Size (in bytes) of the range to apply the modification to.
* @param fFlags The OR mask - page flags X86_PTE_*, excluding the page mask of course.
* @param fMask The AND mask - page flags X86_PTE_*.
* Be very CAREFUL when ~'ing constants which could be 32-bit!
* @remark You must use PGMMapModifyPage() for pages in a mapping.
*/
{
/*
* Validate input.
*/
if (fFlags & X86_PTE_PAE_PG_MASK)
{
return VERR_INVALID_PARAMETER;
}
if (!cb)
{
AssertFailed();
return VERR_INVALID_PARAMETER;
}
/*
* Align the input.
*/
GCPtr = (RTGCPTR)((RTGCUINTPTR)GCPtr & PAGE_BASE_GC_MASK); /** @todo this ain't necessary, right... */
/*
* Call worker.
*/
}
/**
* Gets effective Guest OS page information.
*
* When GCPtr is in a big page, the function will return as if it was a normal
* 4KB page. If the need for distinguishing between big and normal page becomes
* necessary at a later point, a PGMGstGetPage() will be created for that
* purpose.
*
* @returns VBox status.
* @param pVM VM Handle.
* @param GCPtr Guest Context virtual address of the page.
* @param pfFlags Where to store the flags. These are X86_PTE_*, even for big pages.
* @param pGCPhys Where to store the GC physical address of the page.
* This is page aligned. The fact that the
*/
{
}
/**
* Checks if the page is present.
*
* @returns true if the page is present.
* @returns false if the page is not present.
* @param pVM The VM handle.
* @param GCPtr Address within the page.
*/
{
return VBOX_SUCCESS(rc);
}
/**
* Sets (replaces) the page flags for a range of pages in the guest's tables.
*
* @returns VBox status.
* @param pVM VM handle.
* @param GCPtr The address of the first page.
* @param cb The size of the range in bytes.
* @param fFlags Page flags X86_PTE_*, excluding the page mask of course.
*/
{
}
/**
* Modify page flags for a range of pages in the guest's tables
*
* The existing flags are ANDed with the fMask and ORed with the fFlags.
*
* @returns VBox status code.
* @param pVM VM handle.
* @param GCPtr Virtual address of the first page in the range.
* @param cb Size (in bytes) of the range to apply the modification to.
* @param fFlags The OR mask - page flags X86_PTE_*, excluding the page mask of course.
* @param fMask The AND mask - page flags X86_PTE_*, excluding the page mask of course.
* Be very CAREFUL when ~'ing constants which could be 32-bit!
*/
{
/*
* Validate input.
*/
if (fFlags & X86_PTE_PAE_PG_MASK)
{
return VERR_INVALID_PARAMETER;
}
if (!cb)
{
AssertFailed();
return VERR_INVALID_PARAMETER;
}
/*
* Adjust input.
*/
/*
* Call worker.
*/
return rc;
}
/**
* Temporarily turns off the access monitoring of a page within a monitored
*
* Use this when no further \#PFs are required for that page. Be aware that
* a page directory sync might reset the flags, and turn on access monitoring
* for the page.
*
* The caller must do required page table modifications.
*
* @returns VBox status code.
* @param pVM VM Handle
* @param GCPhys Start physical address earlier passed to PGMR3HandlerPhysicalRegister().
* @param GCPhysPage Physical address of the page to turn off access monitoring for.
*/
{
/*
* Validate the range.
*/
PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys);
if (pCur)
{
{
/*
* Ok, check that the type is right and then clear the flag.
*/
unsigned fFlag;
{
break;
break;
case PGMPHYSHANDLERTYPE_MMIO:
return VERR_ACCESS_DENIED;
default:
return VERR_INTERNAL_ERROR;
}
/** @todo add a function which does both clear and set! */
/* clear and set */
if (VBOX_SUCCESS(rc))
rc = pgmRamFlagsSetByGCPhysWithHint(&pVM->pgm.s, GCPhysPage, MM_RAM_FLAGS_PHYSICAL_TEMP_OFF, &pHint);
return rc;
}
AssertMsgFailed(("The page %#x is outside the range %#x-%#x\n",
return VERR_INVALID_PARAMETER;
}
return VERR_PGM_HANDLER_NOT_FOUND;
}
/**
* Turns access monitoring of a page within a monitored
*
* The caller must do required page table modifications.
*
* @returns VBox status code.
* @param pVM VM Handle
* @param GCPhys Start physical address earlier passed to PGMR3HandlerPhysicalRegister().
* @param GCPhysPage Physical address of the page to turn on access monitoring for.
*/
{
/*
* Validate the range.
*/
PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys);
if (pCur)
{
{
/*
* Ok, check that the type is right and then clear the flag.
*/
unsigned fFlag;
{
break;
break;
case PGMPHYSHANDLERTYPE_MMIO:
return VERR_ACCESS_DENIED;
default:
return VERR_INTERNAL_ERROR;
}
/** @todo add a function which does both clear and set! */
/* set and clear */
if (VBOX_SUCCESS(rc))
rc = pgmRamFlagsClearByGCPhysWithHint(&pVM->pgm.s, GCPhysPage, MM_RAM_FLAGS_PHYSICAL_TEMP_OFF, &pHint);
return rc;
}
AssertMsgFailed(("The page %#x is outside the range %#x-%#x\n",
return VERR_INVALID_PARAMETER;
}
return VERR_PGM_HANDLER_NOT_FOUND;
}
/**
* Checks if a physical range is handled
*
* @returns boolean
* @param pVM VM Handle
* @param GCPhys Start physical address earlier passed to PGMR3HandlerPhysicalRegister().
*/
{
/*
* Find the handler.
*/
PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys);
if (pCur)
{
{
/*
* Validate type.
*/
{
case PGMPHYSHANDLERTYPE_MMIO:
return true;
default:
return false;
}
}
}
return false;
}
#ifdef VBOX_STRICT
{
Log(("PHYS2VIRT: Range %VGp-%VGp for virtual handler: %s\n", pCur->Core.Key, pCur->Core.KeyLast, pVirt->pszDesc));
return 0;
}
{
RTAvlroGCPhysDoWithAll(CTXSUFF(&pVM->pgm.s.pTrees)->PhysToVirtHandlers, true, pgmVirtHandlerDumpPhysRange, 0);
}
#endif /* VBOX_STRICT */
/**
* Gets the current CR3 register value for the shadow memory context.
* @returns CR3 value.
* @param pVM The VM handle.
*/
{
{
case PGMMODE_32_BIT:
case PGMMODE_PAE:
case PGMMODE_PAE_NX:
case PGMMODE_AMD64:
case PGMMODE_AMD64_NX:
default:
return ~0;
}
}
/**
* Gets the CR3 register value for the 32-Bit shadow memory context.
* @returns CR3 value.
* @param pVM The VM handle.
*/
{
}
/**
* Gets the CR3 register value for the PAE shadow memory context.
* @returns CR3 value.
* @param pVM The VM handle.
*/
{
}
/**
* Gets the CR3 register value for the AMD64 shadow memory context.
* @returns CR3 value.
* @param pVM The VM handle.
*/
{
}
/**
* Gets the current CR3 register value for the HC intermediate memory context.
* @returns CR3 value.
* @param pVM The VM handle.
*/
{
{
case SUPPAGINGMODE_32_BIT:
case SUPPAGINGMODE_PAE:
case SUPPAGINGMODE_PAE_GLOBAL:
case SUPPAGINGMODE_PAE_NX:
case SUPPAGINGMODE_AMD64:
case SUPPAGINGMODE_AMD64_NX:
default:
return ~0;
}
}
/**
* Gets the current CR3 register value for the GC intermediate memory context.
* @returns CR3 value.
* @param pVM The VM handle.
*/
{
{
case PGMMODE_32_BIT:
case PGMMODE_PAE:
case PGMMODE_PAE_NX:
case PGMMODE_AMD64:
case PGMMODE_AMD64_NX:
default:
return ~0;
}
}
/**
* Gets the CR3 register value for the 32-Bit intermediate memory context.
* @returns CR3 value.
* @param pVM The VM handle.
*/
{
}
/**
* Gets the CR3 register value for the PAE intermediate memory context.
* @returns CR3 value.
* @param pVM The VM handle.
*/
{
}
/**
* Gets the CR3 register value for the AMD64 intermediate memory context.
* @returns CR3 value.
* @param pVM The VM handle.
*/
{
}
/**
* Performs and schedules necessary updates following a CR3 load or reload.
*
* This will normally involve mapping the guest PD or nPDPTR
*
* @returns VBox status code.
* @retval VINF_PGM_SYNC_CR3 if monitoring requires a CR3 sync. This can
* safely be ignored and overridden since the FF will be set too then.
* @param pVM VM handle.
* @param cr3 The new cr3.
* @param fGlobal Indicates whether this is a global flush or not.
*/
{
/*
* Always flag the necessary updates; necessary for hardware acceleration
*/
if (fGlobal)
/*
* When in real or protected mode there is no TLB flushing, but
* REM is simple and we wish to keep it that way.
*/
return VINF_SUCCESS;
/*
* Remap the CR3 content and adjust the monitoring if CR3 was actually changed.
*/
int rc = VINF_SUCCESS;
else
{
{
}
if (fGlobal)
else
}
else
{
/*
* Check if we have a pending update of the CR3 monitoring.
*/
{
}
if (fGlobal)
else
}
return rc;
}
/**
* Synchronize the paging structures.
*
* This function is called in response to the VM_FF_PGM_SYNC_CR3 and
* VM_FF_PGM_SYNC_CR3_NONGLOBAL. Those two force action flags are set
* in several places, most importantly whenever the CR3 is loaded.
*
* @returns VBox status code.
* @param pVM The virtual machine.
* @param cr0 Guest context CR0 register
* @param cr3 Guest context CR3 register
* @param cr4 Guest context CR4 register
* @param fGlobal Including global page directories or not
*/
{
/*
* We might be called when we shouldn't.
*
* The mode switching will ensure that the PD is resynced
* after every mode switch. So, if we find ourselves here
* when in protected or real mode we can safely disable the
* FF and return immediately.
*/
{
return VINF_SUCCESS;
}
/* If global pages are not supported, then all flushes are global */
if (!(cr4 & X86_CR4_PGE))
fGlobal = true;
/*
* Let the 'Bth' function do the work and we'll just keep track of the flags.
*/
if (rc == VINF_SUCCESS)
{
{
}
/*
* Check if we have a pending update of the CR3 monitoring.
*/
{
}
}
/*
* Now flush the CR3 (guest context).
*/
if (rc == VINF_SUCCESS)
return rc;
}
/**
* Called whenever CR0 or CR4 in a way which may change
* the paging mode.
*
* @returns VBox status code fit for scheduling in GC and R0.
* @retval VINF_SUCCESS if the was no change, or it was successfully dealt with.
* @retval VINF_PGM_CHANGE_MODE if we're in GC or R0 and the mode changes.
* @param pVM VM handle.
* @param cr0 The new cr0.
* @param cr4 The new cr4.
* @param efer The new extended feature enable register.
*/
{
/*
* Calc the new guest mode.
*/
if (!(cr0 & X86_CR0_PE))
else if (!(cr0 & X86_CR0_PG))
else if (!(cr4 & X86_CR4_PAE))
else if (!(efer & MSR_K6_EFER_LME))
{
if (!(efer & MSR_K6_EFER_NXE))
else
}
else
{
if (!(efer & MSR_K6_EFER_NXE))
else
}
/*
* Did it change?
*/
return VINF_SUCCESS;
#ifdef IN_RING3
#else
Log(("PGMChangeMode: returns VINF_PGM_CHANGE_MODE.\n"));
return VINF_PGM_CHANGE_MODE;
#endif
}
/**
* Gets the current guest paging mode.
*
*
* @returns The current paging mode.
* @param pVM The VM handle.
*/
{
}
/**
* Gets the current shadow paging mode.
*
* @returns The current paging mode.
* @param pVM The VM handle.
*/
{
}
/**
* Get mode name.
*
* @returns read-only name string.
* @param enmMode The mode which name is desired.
*/
{
switch (enmMode)
{
case PGMMODE_REAL: return "real";
case PGMMODE_PROTECTED: return "protected";
case PGMMODE_32_BIT: return "32-bit";
case PGMMODE_PAE: return "PAE";
case PGMMODE_PAE_NX: return "PAE+NX";
case PGMMODE_AMD64: return "AMD64";
case PGMMODE_AMD64_NX: return "AMD64+NX";
default: return "unknown mode value";
}
}
/**
* Acquire the PGM lock.
*
* @returns VBox status code
* @param pVM The VM to operate on.
*/
{
#ifdef IN_GC
if (rc == VERR_SEM_BUSY)
if (rc == VERR_SEM_BUSY)
#endif
return rc;
}
/**
* Release the PGM lock.
*
* @returns VBox status code
* @param pVM The VM to operate on.
*/
{
}
#ifdef VBOX_STRICT
/**
* State structure used by the PGMAssertHandlerAndFlagsInSync() function
* and its AVL enumerators.
*/
typedef struct PGMAHAFIS
{
/** The VM handle. */
/** Number of errors. */
unsigned cErrors;
/** The flags we've found. */
unsigned fFlagsFound;
/** The flags we're matching up to.
* This is also on the stack as a const, thus only valid during enumeration. */
unsigned fFlags;
/** The current physical address. */
} PGMAHAFIS, *PPGMAHAFIS;
/**
* Verify virtual handler by matching physical address.
*
* @returns 0
* @param pNode Pointer to a PGMVIRTHANDLER.
* @param pvUser Pointer to user parameter.
*/
{
{
{
{
case PGMVIRTHANDLERTYPE_EIP:
case PGMVIRTHANDLERTYPE_WRITE: pState->fFlagsFound |= MM_RAM_FLAGS_VIRTUAL_HANDLER | MM_RAM_FLAGS_VIRTUAL_WRITE; break;
case PGMVIRTHANDLERTYPE_ALL: pState->fFlagsFound |= MM_RAM_FLAGS_VIRTUAL_HANDLER | MM_RAM_FLAGS_VIRTUAL_ALL; break;
/* hypervisor handlers need no flags and wouldn't have nowhere to put them in any case. */
return 0;
}
if ( (pState->fFlags & (MM_RAM_FLAGS_VIRTUAL_HANDLER | MM_RAM_FLAGS_VIRTUAL_WRITE | MM_RAM_FLAGS_VIRTUAL_ALL))
== pState->fFlagsFound)
break;
}
}
return 0;
}
/**
* Verify a virtual handler.
*
* @returns 0
* @param pNode Pointer to a PGMVIRTHANDLER.
* @param pvUser Pointer to user parameter.
*/
{
&& (pVirt->aPhysToVirt[0].Core.Key & PAGE_OFFSET_MASK) != ((RTGCUINTPTR)pVirt->GCPtr & PAGE_OFFSET_MASK))
{
AssertMsgFailed(("virt handler phys out has incorrect key! %VGp %VGv %s\n",
}
/*
* Calc flags.
*/
unsigned fFlags;
{
case PGMVIRTHANDLERTYPE_EIP:
case PGMVIRTHANDLERTYPE_WRITE: fFlags = MM_RAM_FLAGS_VIRTUAL_HANDLER | MM_RAM_FLAGS_VIRTUAL_WRITE; break;
case PGMVIRTHANDLERTYPE_ALL: fFlags = MM_RAM_FLAGS_VIRTUAL_HANDLER | MM_RAM_FLAGS_VIRTUAL_ALL; break;
/* hypervisor handlers need no flags and wouldn't have nowhere to put them in any case. */
return 0;
default:
return 0;
}
/*
* Check pages against flags.
*/
{
if (rc == VERR_PAGE_NOT_PRESENT)
{
{
AssertMsgFailed(("virt handler phys out of sync. %VGp GCPhysNew=~0 iPage=%#x %VGv %s\n",
}
continue;
}
AssertRCReturn(rc, 0);
{
AssertMsgFailed(("virt handler phys out of sync. %VGp GCPhysGst=%VGp iPage=%#x %VGv %s\n",
continue;
}
if (!pPage)
{
AssertMsgFailed(("virt handler getting ram flags. GCPhysGst=%VGp iPage=%#x %VGv %s\n",
continue;
}
{
AssertMsgFailed(("virt handler flags mismatch. HCPhys=%VHp fFlags=%#x GCPhysGst=%VGp iPage=%#x %VGv %s\n",
continue;
}
} /* for pages in virtual mapping. */
return 0;
}
/**
* Asserts that the handlers+guest-page-tables == ramrange-flags and
* that the physical addresses associated with virtual handlers are correct.
*
* @returns Number of mismatches.
* @param pVM The VM handle.
*/
{
/*
* Check the RAM flags against the handlers.
*/
{
{
| MM_RAM_FLAGS_PHYSICAL_HANDLER | MM_RAM_FLAGS_PHYSICAL_WRITE | MM_RAM_FLAGS_PHYSICAL_ALL | MM_RAM_FLAGS_PHYSICAL_TEMP_OFF);
if (fFlags)
{
/* physical first. (simple because of page alignment) */
if ( !(fFlags & MM_RAM_FLAGS_PHYSICAL_TEMP_OFF)
&& (fFlags & (MM_RAM_FLAGS_PHYSICAL_HANDLER | MM_RAM_FLAGS_PHYSICAL_WRITE | MM_RAM_FLAGS_PHYSICAL_ALL)))
{
PPGMPHYSHANDLER pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pPGM->CTXSUFF(pTrees)->PhysHandlers, State.GCPhys);
if (!pPhys)
{
pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pPGM->CTXSUFF(pTrees)->PhysHandlers, State.GCPhys, true);
if ( pPhys
}
if (pPhys)
{
{
case PGMPHYSHANDLERTYPE_PHYSICAL_WRITE: State.fFlagsFound |= MM_RAM_FLAGS_PHYSICAL_HANDLER | MM_RAM_FLAGS_PHYSICAL_WRITE; break;
case PGMPHYSHANDLERTYPE_MMIO:
case PGMPHYSHANDLERTYPE_PHYSICAL_ALL: State.fFlagsFound |= MM_RAM_FLAGS_PHYSICAL_HANDLER | MM_RAM_FLAGS_PHYSICAL_ALL; break;
}
if ( (fFlags & (MM_RAM_FLAGS_PHYSICAL_HANDLER | MM_RAM_FLAGS_PHYSICAL_WRITE | MM_RAM_FLAGS_PHYSICAL_ALL))
!= State.fFlagsFound)
{
AssertMsgFailed(("ram range vs phys handler flags mismatch. GCPhys=%#x fFlags=%#x fFlagsFound=%#x %s\n",
}
#ifdef IN_RING3
/* validate that REM is handling it. */
{
AssertMsgFailed(("ram range vs phys handler REM mismatch. GCPhys=%#x fFlags=%#x %s\n",
}
#endif
}
else
{
}
}
/* virtual flags. */
if (fFlags & (MM_RAM_FLAGS_VIRTUAL_HANDLER | MM_RAM_FLAGS_VIRTUAL_WRITE | MM_RAM_FLAGS_VIRTUAL_ALL))
{
RTAvlroGCPtrDoWithAll(CTXSUFF(&pVM->pgm.s.pTrees)->VirtHandlers, true, pgmVirtHandlerVerifyOneByPhysAddr, &State);
if ( (fFlags & (MM_RAM_FLAGS_VIRTUAL_HANDLER | MM_RAM_FLAGS_VIRTUAL_WRITE | MM_RAM_FLAGS_VIRTUAL_ALL))
!= State.fFlagsFound)
{
AssertMsgFailed(("ram range vs virt handler flags mismatch. GCPhys=%#x fFlags=%#x fFlagsFound=%#x\n",
}
}
}
} /* foreach page in ram range. */
} /* foreach ram range. */
/*
* Check that the physical addresses of the virtual handlers matches up.
*/
RTAvlroGCPtrDoWithAll(CTXSUFF(&pVM->pgm.s.pTrees)->VirtHandlers, true, pgmVirtHandlerVerifyOne, &State);
}
/**
* Asserts that there are no mapping conflicts.
*
* @returns Number of conflicts.
* @param pVM The VM Handle.
*/
{
unsigned cErrors = 0;
/*
* Check for mapping conflicts.
*/
{
/** @todo This is slow and should be optimized, but since it's just assertions I don't care now. */
{
if (rc != VERR_PAGE_TABLE_NOT_PRESENT)
{
cErrors++;
break;
}
}
}
return cErrors;
}
/**
* Asserts that everything related to the guest CR3 is correctly shadowed.
*
* This will call PGMAssertNoMappingConflicts() and PGMAssertHandlerAndFlagsInSync(),
* and assert the correctness of the guest CR3 mapping before asserting that the
* shadow page tables is in sync with the guest page tables.
*
* @returns Number of conflicts.
* @param pVM The VM Handle.
* @param cr3 The current guest CR3 register value.
* @param cr4 The current guest CR4 register value.
*/
{
return cErrors;
}
#endif /* VBOX_STRICT */