PGMAllHandler.cpp revision f045189a81a321668f511d62a57e764431a79db1
/* $Id$ */
/** @file
* PGM - Page Manager / Monitor, Access Handlers.
*/
/*
* Copyright (C) 2006-2013 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_PGM
#ifdef VBOX_WITH_REM
#endif
#ifdef VBOX_WITH_REM
#endif
#include "PGMInternal.h"
#include "PGMInline.h"
#include <iprt/asm-amd64-x86.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 Pointer to the VM.
* @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 relocated.
* @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 for PGMPhysRead, PGMR0Trap0eHandlerNPMisconfig and others. */
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;
pNew->cAliasedPages = 0;
pNew->cTmpOffPages = 0;
/*
* Try insert into list.
*/
{
if (rc == VINF_PGM_SYNC_CR3)
#ifdef VBOX_WITH_REM
# ifndef IN_RING3
# else
# endif
#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 Pointer to the VM.
* @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. */
{
false /* allow updates of PTEs (instead of flushing) */, &fFlushTLBs);
}
/* next */
if (--cPages == 0)
break;
i++;
}
if (fFlushTLBs)
{
}
else
Log(("pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs: doesn't flush guest TLBs. rc=%Rrc; sync flags=%x VMCPU_FF_PGM_SYNC_CR3=%d\n", rc, VMMGetCpu(pVM)->pgm.s.fSyncFlags, VMCPU_FF_IS_SET(VMMGetCpu(pVM), VMCPU_FF_PGM_SYNC_CR3)));
return rc;
}
/**
* Register a physical page access handler.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @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, notify the REM about this change and clear
* the cache.
*/
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.
*/
#ifdef VBOX_WITH_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
#endif
}
/**
* pgmHandlerPhysicalResetRamFlags helper that checks for other handlers on
* edge pages.
*/
DECLINLINE(void) pgmHandlerPhysicalRecalcPageState(PVM pVM, RTGCPHYS GCPhys, bool fAbove, PPGMRAMRANGE *ppRamHint)
{
/*
* Look for other handlers.
*/
unsigned uState = PGM_PAGE_HNDL_PHYS_STATE_NONE;
for (;;)
{
PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.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)
{
/* This should normally not be necessary. */
bool fFlushTLBs ;
else
}
else
}
}
/**
* Resets an aliased page.
*
* @param pVM The VM.
* @param pPage The page.
* @param GCPhysPage The page address in case it comes in handy.
* @param fDoAccounting Whether to perform accounting. (Only set during
* reset where pgmR3PhysRamReset doesn't have the
* handler structure handy.)
*/
void pgmHandlerPhysicalResetAliasedPage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhysPage, bool fDoAccounting)
{
/*
* Flush any shadow page table references *first*.
*/
bool fFlushTLBs = false;
# ifdef IN_RC
# else
# endif
/*
*/
/* Flush its TLB entry. */
/*
* Do accounting for pgmR3PhysRamReset.
*/
if (fDoAccounting)
{
{
}
else
AssertFailed();
}
}
/**
* Resets ram range flags.
*
* @returns VBox status code.
* @retval VINF_SUCCESS when shadow PTs was successfully updated.
* @param pVM Pointer to the VM.
* @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 aliased MMIO pages to MMIO, since this aliasing is our business.
(We don't flip MMIO to RAM though, that's PGMPhys.cpp's job.) */
{
pCur->cAliasedPages--;
}
AssertMsg(pCur->enmType != PGMPHYSHANDLERTYPE_MMIO || PGM_PAGE_IS_MMIO(pPage), ("%RGp %R[pgmpage]\n", GCPhys, pPage));
}
else
/* next */
if (--cPages == 0)
break;
}
pCur->cAliasedPages = 0;
pCur->cTmpOffPages = 0;
/*
* 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 Pointer to the VM.
* @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.
*/
#ifdef VBOX_WITH_REM
# ifndef IN_RING3
# else
# endif
#endif
Log(("PGMHandlerPhysicalModify: GCPhysCurrent=%RGp -> GCPhys=%RGp GCPhysLast=%RGp\n",
return VINF_SUCCESS;
}
}
else
{
}
}
else
{
}
/*
* Invalid new location, flush the cache and free it.
* We've only gotta notify REM and free the memory.
*/
}
else
{
}
return rc;
}
/**
* Changes the user callback arguments associated with a physical access
* handler.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param GCPhys Start physical address of the handler.
* @param pvUserR3 User argument to the R3 handler.
* @param pvUserR0 User argument to the R0 handler.
* @param pvUserRC User argument to the RC handler. Values larger or
* equal to 0x10000 will be relocated automatically.
*/
VMMDECL(int) PGMHandlerPhysicalChangeUserArgs(PVM pVM, RTGCPHYS GCPhys, RTR3PTR pvUserR3, RTR0PTR pvUserR0, RTRCPTR pvUserRC)
{
/*
* Find the handler.
*/
int rc = VINF_SUCCESS;
PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
if (pCur)
{
/*
* Change arguments.
*/
}
else
{
}
return rc;
}
/**
* Splits a physical access handler in two.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @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 Pointer to the VM.
* @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(),
* PGMHandlerPhysicalPageAlias() or PGMHandlerPhysicalPageAliasHC().
*
* @returns VBox status code.
* @param pVM Pointer to the VM
* @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! */
{
STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,PhysHandlerReset)); /**@Todo move out of switch */
{
/*
* 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...
*/
if (pCur->cAliasedPages)
{
while (cLeft-- > 0)
{
{
false /*fDoAccounting*/);
--pCur->cAliasedPages;
#ifndef VBOX_STRICT
if (pCur->cAliasedPages == 0)
break;
#endif
}
pPage++;
}
}
}
else if (pCur->cTmpOffPages > 0)
{
/*
* Set the flags and flush shadow PT entries.
*/
}
pCur->cAliasedPages = 0;
pCur->cTmpOffPages = 0;
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 Pointer to the VM
* @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.
*/
{
pCur->cTmpOffPages++;
}
return VINF_SUCCESS;
}
AssertMsgFailed(("The page %#x is outside the range %#x-%#x\n",
return VERR_INVALID_PARAMETER;
}
return VERR_PGM_HANDLER_NOT_FOUND;
}
#ifndef IEM_VERIFICATION_MODE_FULL
/**
* 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 modifications 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 Pointer to the VM.
* @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);
{
{
AssertReturnStmt((pCur->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK, pgmUnlock(pVM), VERR_INVALID_PARAMETER);
/*
* 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",
pCur->cAliasedPages--;
}
/*
* 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",
pCur->cAliasedPages++;
/* Flush its TLB entry. */
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 arbitrary HC page in the shadow page tables.
*
* This differs from PGMHandlerPhysicalPageAlias in that the page doesn't need
* to be a known MMIO2 page and that only shadow paging may access the page.
* The latter distinction is important because the only use for this feature is
* for mapping the special APIC access page that VT-x uses to detect APIC MMIO
* operations, the page is shared between all guest CPUs and actually not
* written to. At least at the moment.
*
* The caller must do required page table modifications. You can get away
* without making any modifications 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.
*
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @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 HCPhysPageRemap The physical address of the HC page that
* serves as backing memory.
*
* @remark May cause a page pool flush if used on a page that is already
* aliased.
*/
VMMDECL(int) PGMHandlerPhysicalPageAliasHC(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysPage, RTHCPHYS HCPhysPageRemap)
{
/// 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);
{
{
AssertReturnStmt((pCur->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK, pgmUnlock(pVM), VERR_INVALID_PARAMETER);
/*
* Get and validate the pages.
*/
{
return VINF_PGM_HANDLER_ALREADY_ALIASED;
}
/*
* Do the actual remapping here.
* This page now serves as an alias for the backing memory
* specified as far as shadow paging is concerned.
*/
LogFlow(("PGMHandlerPhysicalPageAlias: %RGp (%R[pgmpage]) alias for %RHp\n",
pCur->cAliasedPages++;
/* Flush its TLB entry. */
return VINF_SUCCESS;
}
AssertMsgFailed(("The page %#x is outside the range %#x-%#x\n",
return VERR_INVALID_PARAMETER;
}
return VERR_PGM_HANDLER_NOT_FOUND;
}
#endif /* !IEM_VERIFICATION_MODE_FULL */
/**
* Checks if a physical range is handled
*
* @returns boolean
* @param pVM Pointer to the VM.
* @param GCPhys Start physical address earlier passed to PGMR3HandlerPhysicalRegister().
* @remarks Caller must take the PGM lock...
* @thread EMT.
*/
{
/*
* Find the handler.
*/
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 VM.
* @param GCPhys The address of the page with a disabled handler.
*
* @remarks The caller, PGMR3PhysTlbGCPhys2Ptr, must hold the PGM lock.
*/
{
if (!pCur)
{
AssertFailed();
return true;
}
/* Only whole pages can be disabled. */
return bRet;
}
/**
* Check if particular guest's VA is being monitored.
*
* @returns true or false
* @param pVM Pointer to the VM.
* @param GCPtr Virtual address.
* @remarks Will acquire the PGM lock.
* @thread 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 Pointer to the VM.
* @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 Pointer to the VM.
* @param pPhys2Virt The node we failed insert.
*/
{
/*
* First find the node which is conflicting with us.
*/
/** @todo Deal with partial overlapping. (Unlikely 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 Pointer to the VM.
*/
{
/*
* 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 VM.
*/
{
}
#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;
/** Pointer to the VM. */
} 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 Pointer to the VM.
*/
{
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 VBOX_WITH_REM
# 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
#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 */