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