PGMAllHandler.cpp revision 1efd6f2fd1ffc7bdaca227fe6e1bc5b6783a17d6
/* $Id$ */
/** @file
* PGM - Page Manager / Monitor, Access Handlers.
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_PGM
#include "PGMInternal.h"
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static int pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(PVM pVM, PPGMPHYSHANDLER pCur, PPGMRAMRANGE pRam);
/**
* Register a access handler for a physical range.
*
* @returns VBox status code.
* @retval VINF_SUCCESS when successfully installed.
* @retval VINF_PGM_GCPHYS_ALIASED when the shadow PTs could be updated because
* flagged together with a pool clearing.
* @retval VERR_PGM_HANDLER_PHYSICAL_CONFLICT if the range conflicts with an existing
* one. A debug assertion is raised.
*
* @param pVM VM Handle.
* @param enmType Handler type. Any of the PGMPHYSHANDLERTYPE_PHYSICAL* enums.
* @param GCPhys Start physical address.
* @param GCPhysLast Last physical address. (inclusive)
* @param pfnHandlerR3 The R3 handler.
* @param pvUserR3 User argument to the R3 handler.
* @param pfnHandlerR0 The R0 handler.
* @param pvUserR0 User argument to the R0 handler.
* @param pfnHandlerRC The RC handler.
* @param pvUserRC User argument to the RC handler. This can be a value
* less that 0x10000 or a (non-null) pointer that is
* automatically relocatated.
* @param pszDesc Pointer to description string. This must not be freed.
*/
VMMDECL(int) PGMHandlerPhysicalRegisterEx(PVM pVM, PGMPHYSHANDLERTYPE enmType, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast,
{
Log(("PGMHandlerPhysicalRegisterEx: enmType=%d GCPhys=%RGp GCPhysLast=%RGp pfnHandlerR3=%RHv pvUserR3=%RHv pfnHandlerR0=%RHv pvUserR0=%RHv pfnHandlerGC=%RRv pvUserGC=%RRv pszDesc=%s\n",
enmType, GCPhys, GCPhysLast, pfnHandlerR3, pvUserR3, pfnHandlerR0, pvUserR0, pfnHandlerRC, pvUserRC, R3STRING(pszDesc)));
/*
* Validate input.
*/
AssertMsgReturn(GCPhys < GCPhysLast, ("GCPhys >= GCPhysLast (%#x >= %#x)\n", GCPhys, GCPhysLast), VERR_INVALID_PARAMETER);
switch (enmType)
{
break;
case PGMPHYSHANDLERTYPE_MMIO:
/* Simplification in PGMPhysRead among other places. */
AssertMsgReturn((GCPhysLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK, ("%RGp\n", GCPhysLast), VERR_INVALID_PARAMETER);
break;
default:
return VERR_INVALID_PARAMETER;
}
("Not RC pointer! pvUserRC=%RRv\n", pvUserRC),
("Not R0 pointer! pvUserR0=%RHv\n", pvUserR0),
/*
* We require the range to be within registered ram.
* There is no apparent need to support ranges which cover more than one ram range.
*/
if ( !pRam
{
#ifdef IN_RING3
#endif
}
/*
* Allocate and initialize the new entry.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Try insert into list.
*/
{
if (rc == VINF_PGM_SYNC_CR3)
#ifndef IN_RING3
#else
#endif
if (rc != VINF_SUCCESS)
return rc;
}
#if defined(IN_RING3) && defined(VBOX_STRICT)
#endif
AssertMsgFailed(("Conflict! GCPhys=%RGp GCPhysLast=%RGp pszDesc=%s\n", GCPhys, GCPhysLast, pszDesc));
}
/**
* Sets ram range flags and attempts updating shadow PTs.
*
* @returns VBox status code.
* @retval VINF_SUCCESS when shadow PTs was successfully updated.
* @retval VINF_PGM_SYNC_CR3 when the shadow PTs could be updated because
* @param pVM The VM handle.
* @param pCur The physical handler.
* @param pRam The RAM range.
*/
static int pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(PVM pVM, PPGMPHYSHANDLER pCur, PPGMRAMRANGE pRam)
{
/*
* Iterate the guest ram pages updating the flags and flushing PT entries
* mapping the page.
*/
bool fFlushTLBs = false;
int rc = VINF_SUCCESS;
for (;;)
{
/* Only do upgrades. */
{
}
/* next */
if (--cPages == 0)
break;
i++;
}
{
Log(("pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs: flushing guest TLBs\n"));
}
else
return rc;
}
/**
* Register a physical page access handler.
*
* @returns VBox status code.
* @param pVM VM Handle.
* @param GCPhys Start physical address.
*/
{
/*
* Find the handler.
*/
PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
if (pCur)
{
LogFlow(("PGMHandlerPhysicalDeregister: Removing Range %RGp-%RGp %s\n",
/*
* Clear the page bits and notify the REM about this change.
*/
return VINF_SUCCESS;
}
return VERR_PGM_HANDLER_NOT_FOUND;
}
/**
* Shared code with modify.
*/
{
/*
* Page align the range.
*
* Since we've reset (recalculated) the physical handler state of all pages
* we can make use of the page states to figure out whether a page should be
* included in the REM notification or not.
*/
{
if (GCPhysStart & PAGE_OFFSET_MASK)
{
if ( pPage
{
if ( GCPhys > GCPhysLast
|| GCPhys < GCPhysStart)
return;
}
else
}
if (GCPhysLast & PAGE_OFFSET_MASK)
{
if ( pPage
{
if ( GCPhys < GCPhysStart
|| GCPhys > GCPhysLast)
return;
GCPhysLast = GCPhys;
}
else
}
}
/*
* Tell REM.
*/
#ifndef IN_RING3
REMNotifyHandlerPhysicalDeregister(pVM, pCur->enmType, GCPhysStart, GCPhysLast - GCPhysStart + 1, !!pCur->pfnHandlerR3, fRestoreAsRAM);
#else
REMR3NotifyHandlerPhysicalDeregister(pVM, pCur->enmType, GCPhysStart, GCPhysLast - GCPhysStart + 1, !!pCur->pfnHandlerR3, fRestoreAsRAM);
#endif
}
/**
* pgmHandlerPhysicalResetRamFlags helper that checks for
* other handlers on edge pages.
*/
DECLINLINE(void) pgmHandlerPhysicalRecalcPageState(PPGM pPGM, RTGCPHYS GCPhys, bool fAbove, PPGMRAMRANGE *ppRamHint)
{
/*
* Look for other handlers.
*/
unsigned uState = PGM_PAGE_HNDL_PHYS_STATE_NONE;
for (;;)
{
PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pPGM->CTX_SUFF(pTrees)->PhysHandlers, GCPhys, fAbove);
if ( !pCur
break;
/* next? */
break;
GCPhys = GCPhysNext;
}
/*
* Update if we found something that is a higher priority
* state than the current.
*/
if (uState != PGM_PAGE_HNDL_PHYS_STATE_NONE)
{
if ( RT_SUCCESS(rc)
else
}
}
/**
* Resets an aliased page.
*
* @param pVM The VM.
* @param pPage The page.
* @param GCPhysPage The page address in case it comes in handy.
*/
{
/*
* Flush any shadow page table references *first*.
*/
bool fFlushTLBs = false;
# ifdef IN_RC
# else
# endif
/*
*/
}
/**
* Resets ram range flags.
*
* @returns VBox status code.
* @retval VINF_SUCCESS when shadow PTs was successfully updated.
* @param pVM The VM handle.
* @param pCur The physical handler.
*
* @remark We don't start messing with the shadow page tables, as we've already got code
* in Trap0e which deals with out of sync handler flags (originally conceived for
* global pages).
*/
{
/*
* Iterate the guest ram pages updating the state.
*/
for (;;)
{
if (RT_SUCCESS(rc))
{
/* Reset MMIO2 for MMIO pages to MMIO, since this aliasing is our business.
(We don't flip MMIO to RAM though, that's PGMPhys.cpp's job.) */
AssertMsg(pCur->enmType != PGMPHYSHANDLERTYPE_MMIO || PGM_PAGE_IS_MMIO(pPage), ("%RGp %R[pgmpage]\n", GCPhys, pPage));
}
else
/* next */
if (--cPages == 0)
break;
}
/*
* Check for partial start and end pages.
*/
}
/**
* Modify a physical page access handler.
*
* Modification can only be done to the range it self, not the type or anything else.
*
* @returns VBox status code.
* For all return codes other than VERR_PGM_HANDLER_NOT_FOUND and VINF_SUCCESS the range is deregistered
* and a new registration must be performed!
* @param pVM VM handle.
* @param GCPhysCurrent Current location.
* @param GCPhys New location.
* @param GCPhysLast New last location.
*/
VMMDECL(int) PGMHandlerPhysicalModify(PVM pVM, RTGCPHYS GCPhysCurrent, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast)
{
/*
* Remove it.
*/
int rc;
PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhysCurrent);
if (pCur)
{
/*
* Clear the ram flags. (We're gonna move or free it!)
*/
/*
* Validate the new range, modify and reinsert.
*/
if (GCPhysLast >= GCPhys)
{
/*
* We require the range to be within registered ram.
* There is no apparent need to support ranges which cover more than one ram range.
*/
if ( pRam
{
{
/*
* Set ram flags, flush shadow PT entries and finally tell REM about this.
*/
#ifndef IN_RING3
#else
#endif
Log(("PGMHandlerPhysicalModify: GCPhysCurrent=%RGp -> GCPhys=%RGp GCPhysLast=%RGp\n",
return VINF_SUCCESS;
}
}
else
{
}
}
else
{
}
/*
* Invalid new location, free it.
* We've only gotta notify REM and free the memory.
*/
}
else
{
}
return rc;
}
/**
* Changes the callbacks associated with a physical access handler.
*
* @returns VBox status code.
* @param pVM VM Handle.
* @param GCPhys Start physical address.
* @param pfnHandlerR3 The R3 handler.
* @param pvUserR3 User argument to the R3 handler.
* @param pfnHandlerR0 The R0 handler.
* @param pvUserR0 User argument to the R0 handler.
* @param pfnHandlerRC The RC handler.
* @param pvUserRC User argument to the RC handler. Values larger or
* equal to 0x10000 will be relocated automatically.
* @param pszDesc Pointer to description string. This must not be freed.
*/
{
/*
* Get the handler.
*/
int rc = VINF_SUCCESS;
PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
if (pCur)
{
/*
* Change callbacks.
*/
}
else
{
}
return rc;
}
/**
* Splits a physical access handler in two.
*
* @returns VBox status code.
* @param pVM VM Handle.
* @param GCPhys Start physical address of the handler.
* @param GCPhysSplit The split address.
*/
{
/*
* Do the allocation without owning the lock.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Get the handler.
*/
PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
{
{
/*
* Create new handler node for the 2nd half.
*/
pNew->cPages = (pNew->Core.KeyLast - (pNew->Core.Key & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
pCur->cPages = (pCur->Core.KeyLast - (pCur->Core.Key & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
{
LogFlow(("PGMHandlerPhysicalSplit: %RGp-%RGp and %RGp-%RGp\n",
return VINF_SUCCESS;
}
AssertMsgFailed(("whu?\n"));
}
else
{
AssertMsgFailed(("outside range: %RGp-%RGp split %RGp\n", pCur->Core.Key, pCur->Core.KeyLast, GCPhysSplit));
}
}
else
{
}
return rc;
}
/**
* Joins up two adjacent physical access handlers which has the same callbacks.
*
* @returns VBox status code.
* @param pVM VM Handle.
* @param GCPhys1 Start physical address of the first handler.
* @param GCPhys2 Start physical address of the second handler.
*/
{
/*
* Get the handlers.
*/
int rc;
PPGMPHYSHANDLER pCur1 = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys1);
{
PPGMPHYSHANDLER pCur2 = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys2);
{
/*
* Make sure that they are adjacent, and that they've got the same callbacks.
*/
{
{
PPGMPHYSHANDLER pCur3 = (PPGMPHYSHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys2);
{
pCur1->cPages = (pCur1->Core.KeyLast - (pCur1->Core.Key & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
LogFlow(("PGMHandlerPhysicalJoin: %RGp-%RGp %RGp-%RGp\n",
return VINF_SUCCESS;
}
}
else
{
AssertMsgFailed(("mismatching handlers\n"));
}
}
else
{
AssertMsgFailed(("not adjacent: %RGp-%RGp %RGp-%RGp\n",
}
}
else
{
}
}
else
{
}
return rc;
}
/**
* Resets any modifications to individual pages in a physical
* page access handler region.
*
* This is used in pair with PGMHandlerPhysicalPageTempOff() or
* PGMHandlerPhysicalPageAlias().
*
* @returns VBox status code.
* @param pVM VM Handle
* @param GCPhys The start address of the handler regions, i.e. what you
* passed to PGMR3HandlerPhysicalRegister(),
* PGMHandlerPhysicalRegisterEx() or
* PGMHandlerPhysicalModify().
*/
{
/*
* Find the handler.
*/
int rc;
PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
{
/*
* Validate type.
*/
{
case PGMPHYSHANDLERTYPE_MMIO: /* NOTE: Only use when clearing MMIO ranges with aliased MMIO2 pages! */
{
{
/*
* Reset all the PGMPAGETYPE_MMIO2_ALIAS_MMIO pages first and that's it.
* This could probably be optimized a bit wrt to flushing, but I'm too lazy
* to do that now...
*/
while (cLeft-- > 0)
{
pgmHandlerPhysicalResetAliasedPage(pVM, pPage, pRam->GCPhys + ((RTGCPHYS)(uintptr_t)(pPage - &pRam->aPages[0]) << PAGE_SHIFT));
pPage++;
}
}
else
{
/*
* Set the flags and flush shadow PT entries.
*/
}
rc = VINF_SUCCESS;
break;
}
/*
* Invalid.
*/
default:
break;
}
}
else
{
}
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 The start address of the access handler. This
* must be a fully page aligned range or we risk
* messing up other handlers installed for the
* start and end pages.
* @param GCPhysPage The physical address of the page to turn off
* access monitoring for.
*/
{
/*
* Validate the range.
*/
PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
{
{
/*
* Change the page status.
*/
#ifndef IN_RC
#endif
return VINF_SUCCESS;
}
AssertMsgFailed(("The page %#x is outside the range %#x-%#x\n",
return VERR_INVALID_PARAMETER;
}
return VERR_PGM_HANDLER_NOT_FOUND;
}
/**
* Replaces an MMIO page with an MMIO2 page.
*
* This is a worker for IOMMMIOMapMMIO2Page that works in a similar way to
* PGMHandlerPhysicalPageTempOff but for an MMIO page. Since an MMIO page has no
* backing, the caller must provide a replacement page. For various reasons the
* replacement page must be an MMIO2 page.
*
* The caller must do required page table modifications. You can get away
* without making any modifations since it's an MMIO page, the cost is an extra
* \#PF which will the resync the page.
*
* Call PGMHandlerPhysicalReset() to restore the MMIO page.
*
* The caller may still get handler callback even after this call and must be
* able to deal correctly with such calls. The reason for these callbacks are
* either that we're executing in the recompiler (which doesn't know about this
* arrangement) or that we've been restored from saved state (where we won't
* save the change).
*
* @returns VBox status code.
* @param pVM The VM handle
* @param GCPhys The start address of the access handler. This
* must be a fully page aligned range or we risk
* messing up other handlers installed for the
* start and end pages.
* @param GCPhysPage The physical address of the page to turn off
* access monitoring for.
* @param GCPhysPageRemap The physical address of the MMIO2 page that
* serves as backing memory.
*
* @remark May cause a page pool flush if used on a page that is already
* aliased.
*
* @note This trick does only work reliably if the two pages are never ever
* mapped in the same page table. If they are the page pool code will
* be confused should either of them be flushed. See the special case
* of zero page aliasing mentioned in #3170.
*
*/
VMMDECL(int) PGMHandlerPhysicalPageAlias(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysPage, RTGCPHYS GCPhysPageRemap)
{
/// Assert(!IOMIsLockOwner(pVM)); /* We mustn't own any other locks when calling this */
/*
* Lookup and validate the range.
*/
PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
{
{
/*
* Get and validate the two pages.
*/
{
return VINF_PGM_HANDLER_ALREADY_ALIASED;
/*
* The page is already mapped as some other page, reset it
*/
Log(("PGMHandlerPhysicalPageAlias: GCPhysPage=%RGp (%R[pgmpage]; %RHp -> %RHp\n",
}
/*
* Do the actual remapping here.
* This page now serves as an alias for the backing memory specified.
*/
LogFlow(("PGMHandlerPhysicalPageAlias: %RGp (%R[pgmpage]) alias for %RGp (%R[pgmpage])\n",
#ifndef IN_RC
#endif
return VINF_SUCCESS;
}
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().
* @remarks Caller must take the PGM lock...
* @threads EMT.
*/
{
/*
* Find the handler.
*/
PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
if (pCur)
{
return true;
}
return false;
}
/**
* Checks if it's an disabled all access handler or write access handler at the
* given address.
*
* @returns true if it's an all access handler, false if it's a write access
* handler.
* @param pVM Pointer to the shared VM structure.
* @param GCPhys The address of the page with a disabled handler.
*
* @remarks The caller, PGMR3PhysTlbGCPhys2Ptr, must hold the PGM lock.
*/
{
PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
AssertReturn(pCur, true);
/* Only whole pages can be disabled. */
}
/**
* Check if particular guest's VA is being monitored.
*
* @returns true or false
* @param pVM VM handle.
* @param GCPtr Virtual address.
* @remarks Will acquire the PGM lock.
* @threads Any.
*/
{
PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrGet(&pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers, GCPtr);
}
/**
* Search for virtual handler with matching physical address
*
* @returns VBox status code
* @param pVM The VM handle.
* @param GCPhys GC physical address to search for.
* @param ppVirt Where to store the pointer to the virtual handler structure.
* @param piPage Where to store the pointer to the index of the cached physical page.
*/
int pgmHandlerVirtualFindByPhysAddr(PVM pVM, RTGCPHYS GCPhys, PPGMVIRTHANDLER *ppVirt, unsigned *piPage)
{
pCur = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, GCPhys);
if (pCur)
{
/* found a match! */
#endif
LogFlow(("PHYS2VIRT: found match for %RGp -> %RGv *piPage=%#x\n", GCPhys, (*ppVirt)->Core.Key, *piPage));
return VINF_SUCCESS;
}
return VERR_PGM_HANDLER_NOT_FOUND;
}
/**
* Deal with aliases in phys2virt.
*
* As pointed out by the various todos, this currently only deals with
* aliases where the two ranges match 100%.
*
* @param pVM The VM handle.
* @param pPhys2Virt The node we failed insert.
*/
{
/*
* First find the node which is conflicting with us.
*/
/** @todo Deal with partial overlapping. (Unlikly situation, so I'm too lazy to do anything about it now.) */
/** @todo check if the current head node covers the ground we do. This is highly unlikely
* and I'm too lazy to implement this now as it will require sorting the list and stuff like that. */
PPGMPHYS2VIRTHANDLER pHead = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key);
#endif
{
/** @todo do something clever here... */
LogRel(("pgmHandlerVirtualInsertAliased: %RGp-%RGp\n", pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast));
pPhys2Virt->offNextAlias = 0;
return;
}
/*
* Insert ourselves as the next node.
*/
else
{
PPGMPHYS2VIRTHANDLER pNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pHead + (pHead->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
}
Log(("pgmHandlerVirtualInsertAliased: %RGp-%RGp offNextAlias=%#RX32\n", pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias));
}
/**
* Resets one virtual handler range.
*
* This is called by HandlerVirtualUpdate when it has detected some kind of
* problem and have started clearing the virtual handler page states (or
* when there have been registration/deregistrations). For this reason this
* function will only update the page status if it's lower than desired.
*
* @returns 0
* @param pNode Pointer to a PGMVIRTHANDLER.
* @param pvUser The VM handle.
*/
{
/*
* Iterate the pages and apply the new state.
*/
{
{
/*
* Update the page state wrt virtual handlers.
*/
if ( RT_SUCCESS(rc)
else
/*
* Need to insert the page in the Phys2Virt lookup tree?
*/
{
#endif
else
else
AssertReleaseMsg(RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key) == &pPhys2Virt->Core,
("%RGp-%RGp offNextAlias=%#RX32\n",
#endif
Log2(("PHYS2VIRT: Insert physical range %RGp-%RGp offNextAlias=%#RX32 %s\n",
pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias, R3STRING(pCur->pszDesc)));
}
}
offPage = 0;
}
return 0;
}
#if defined(VBOX_STRICT) || defined(LOG_ENABLED)
/**
* Worker for pgmHandlerVirtualDumpPhysPages.
*
* @returns 0 (continue enumeration).
* @param pNode The virtual handler node.
* @param pvUser User argument, unused.
*/
static DECLCALLBACK(int) pgmHandlerVirtualDumpPhysPagesCallback(PAVLROGCPHYSNODECORE pNode, void *pvUser)
{
Log(("PHYS2VIRT: Range %RGp-%RGp for virtual handler: %s\n", pCur->Core.Key, pCur->Core.KeyLast, pVirt->pszDesc));
return 0;
}
/**
* Assertion / logging helper for dumping all the
* virtual handlers to the log.
*
* @param pVM Pointer to the shared VM structure.
*/
{
}
#endif /* VBOX_STRICT || LOG_ENABLED */
#ifdef VBOX_STRICT
/**
* State structure used by the PGMAssertHandlerAndFlagsInSync() function
* and its AVL enumerators.
*/
typedef struct PGMAHAFIS
{
/** The current physical address. */
/** The state we've calculated. */
unsigned uVirtStateFound;
/** The state we're matching up to. */
unsigned uVirtState;
/** Number of errors. */
unsigned cErrors;
/** The VM handle. */
} PGMAHAFIS, *PPGMAHAFIS;
#if 0 /* unused */
/**
* Verify virtual handler by matching physical address.
*
* @returns 0
* @param pNode Pointer to a PGMVIRTHANDLER.
* @param pvUser Pointer to user parameter.
*/
static DECLCALLBACK(int) pgmHandlerVirtualVerifyOneByPhysAddr(PAVLROGCPTRNODECORE pNode, void *pvUser)
{
{
{
{
}
break; //??
}
}
return 0;
}
#endif /* unused */
/**
* Verify a virtual handler (enumeration callback).
*
* Called by PGMAssertHandlerAndFlagsInSync to check the sanity of all
* the virtual handlers, esp. that the physical addresses matches up.
*
* @returns 0
* @param pNode Pointer to a PGMVIRTHANDLER.
* @param pvUser Pointer to a PPGMAHAFIS structure.
*/
{
/*
* Validate the type and calc state.
*/
{
case PGMVIRTHANDLERTYPE_WRITE:
case PGMVIRTHANDLERTYPE_ALL:
break;
default:
return 0;
}
/*
* Check key alignment.
*/
if ( (pVirt->aPhysToVirt[0].Core.Key & PAGE_OFFSET_MASK) != ((RTGCUINTPTR)pVirt->Core.Key & PAGE_OFFSET_MASK)
{
AssertMsgFailed(("virt handler phys has incorrect key! %RGp %RGv %s\n",
}
if ( (pVirt->aPhysToVirt[pVirt->cPages - 1].Core.KeyLast & PAGE_OFFSET_MASK) != ((RTGCUINTPTR)pVirt->Core.KeyLast & PAGE_OFFSET_MASK)
{
AssertMsgFailed(("virt handler phys has incorrect key! %RGp %RGv %s\n",
pVirt->aPhysToVirt[pVirt->cPages - 1].Core.KeyLast, pVirt->Core.KeyLast, R3STRING(pVirt->pszDesc)));
}
/*
* Check pages for sanity and state.
*/
{
{
if ( rc == VERR_PAGE_NOT_PRESENT
|| rc == VERR_PAGE_TABLE_NOT_PRESENT)
{
{
AssertMsgFailed(("virt handler phys out of sync. %RGp GCPhysNew=~0 iPage=%#x %RGv %s\n",
}
continue;
}
AssertRCReturn(rc, 0);
{
AssertMsgFailed(("virt handler phys out of sync. %RGp GCPhysGst=%RGp iPage=%#x %RGv %s\n",
continue;
}
if (!pPage)
{
AssertMsgFailed(("virt handler getting ram flags. GCPhysGst=%RGp iPage=%#x %RGv %s\n",
continue;
}
{
AssertMsgFailed(("virt handler state mismatch. pPage=%R[pgmpage] GCPhysGst=%RGp iPage=%#x %RGv state=%d expected>=%d %s\n",
pPage, GCPhysGst, iPage, GCPtr, PGM_PAGE_GET_HNDL_VIRT_STATE(pPage), uState, R3STRING(pVirt->pszDesc)));
continue;
}
} /* for each VCPU */
} /* 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.
*/
{
State.uVirtState = 0;
State.uVirtStateFound = 0;
/*
* Check the RAM flags against the handlers.
*/
{
{
{
/*
* Physical first - calculate the state based on the handlers
* active on the page, then compare.
*/
{
/* the first */
PPGMPHYSHANDLER pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pPGM->CTX_SUFF(pTrees)->PhysHandlers, State.GCPhys);
if (!pPhys)
{
pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pPGM->CTX_SUFF(pTrees)->PhysHandlers, State.GCPhys, true);
if ( pPhys
}
if (pPhys)
{
/* more? */
{
PPGMPHYSHANDLER pPhys2 = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pPGM->CTX_SUFF(pTrees)->PhysHandlers,
if ( !pPhys2
break;
}
/* compare.*/
{
AssertMsgFailed(("ram range vs phys handler flags mismatch. GCPhys=%RGp state=%d expected=%d %s\n",
}
#ifdef IN_RING3
/* validate that REM is handling it. */
/* ignore shadowed ROM for the time being. */
{
AssertMsgFailed(("ram range vs phys handler REM mismatch. GCPhys=%RGp state=%d %s\n",
}
#endif
}
else
{
AssertMsgFailed(("ram range vs phys handler mismatch. no handler for GCPhys=%RGp\n", State.GCPhys));
}
}
/*
* Virtual handlers.
*/
{
#if 1
/* locate all the matching physical ranges. */
for (;;)
{
PPGMPHYS2VIRTHANDLER pPhys2Virt = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers,
GCPhysKey, true /* above-or-equal */);
if ( !pPhys2Virt
break;
/* the head */
/* any aliases */
{
pPhys2Virt = (PPGMPHYS2VIRTHANDLER)((uintptr_t)pPhys2Virt + (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
}
/* done? */
break;
}
#else
/* very slow */
RTAvlroGCPtrDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers, true, pgmHandlerVirtualVerifyOneByPhysAddr, &State);
#endif
{
AssertMsgFailed(("ram range vs virt handler flags mismatch. GCPhys=%RGp uVirtState=%#x uVirtStateFound=%#x\n",
}
}
}
} /* foreach page in ram range. */
} /* foreach ram range. */
/*
* Check that the physical addresses of the virtual handlers matches up
* and that they are otherwise sane.
*/
RTAvlroGCPtrDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers, true, pgmHandlerVirtualVerifyOne, &State);
/*
* Do the reverse check for physical handlers.
*/
/** @todo */
}
#endif /* VBOX_STRICT */