PGMAllHandler.cpp revision a94e8933bf3027de349bcd78b76ec9f58c2827b6
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/* $Id$ */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/** @file
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * PGM - Page Manager / Monitor, Access Handlers.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
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 *
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
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/*******************************************************************************
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync* Header Files *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync*******************************************************************************/
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define LOG_GROUP LOG_GROUP_PGM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <VBox/dbgf.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <VBox/pgm.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <VBox/iom.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <VBox/mm.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <VBox/em.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <VBox/stam.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <VBox/rem.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <VBox/dbgf.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <VBox/rem.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include "PGMInternal.h"
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <VBox/vm.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <VBox/log.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <iprt/assert.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <iprt/asm.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <iprt/string.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <VBox/param.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <VBox/err.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <VBox/selm.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
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
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Register a access handler for a physical range.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
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 *
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.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
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 R3PTRTYPE(const char *) pszDesc)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
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
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Validate input.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgReturn(GCPhys < GCPhysLast, ("GCPhys >= GCPhysLast (%#x >= %#x)\n", GCPhys, GCPhysLast), VERR_INVALID_PARAMETER);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync switch (enmType)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync case PGMPHYSHANDLERTYPE_PHYSICAL_WRITE:
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync break;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync case PGMPHYSHANDLERTYPE_MMIO:
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync case PGMPHYSHANDLERTYPE_PHYSICAL_ALL:
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 break;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync default:
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("Invalid input enmType=%d!\n", enmType));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VERR_INVALID_PARAMETER;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgReturn( (RTRCUINTPTR)pvUserRC < 0x10000
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync || MMHyperR3ToRC(pVM, MMHyperRCToR3(pVM, pvUserRC)) == pvUserRC,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync ("Not RC pointer! pvUserRC=%RRv\n", pvUserRC),
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync VERR_INVALID_PARAMETER);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgReturn( (RTR0UINTPTR)pvUserR0 < 0x10000
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync || MMHyperR3ToR0(pVM, MMHyperR0ToR3(pVM, pvUserR0)) == pvUserR0,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync ("Not R0 pointer! pvUserR0=%RHv\n", pvUserR0),
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync VERR_INVALID_PARAMETER);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertPtrReturn(pfnHandlerR3, VERR_INVALID_POINTER);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertReturn(pfnHandlerR0, VERR_INVALID_PARAMETER);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertReturn(pfnHandlerRC, VERR_INVALID_PARAMETER);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
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 */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync while (pRam && GCPhys > pRam->GCPhysLast)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pRam = pRam->CTX_SUFF(pNext);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( !pRam
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync || GCPhysLast < pRam->GCPhys
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync || GCPhys > pRam->GCPhysLast)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifdef IN_RING3
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync DBGFR3Info(pVM, "phys", NULL, NULL);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("No RAM range for %RGp-%RGp\n", GCPhys, GCPhysLast));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VERR_PGM_HANDLER_PHYSICAL_NO_RAM_RANGE;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Allocate and initialize the new entry.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPHYSHANDLER pNew;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rc = MMHyperAlloc(pVM, sizeof(*pNew), 0, MM_TAG_PGM_HANDLERS, (void **)&pNew);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (RT_FAILURE(rc))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return rc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pNew->Core.Key = GCPhys;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pNew->Core.KeyLast = GCPhysLast;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pNew->enmType = enmType;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pNew->cPages = (GCPhysLast - (GCPhys & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pNew->pfnHandlerR3 = pfnHandlerR3;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pNew->pvUserR3 = pvUserR3;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pNew->pfnHandlerR0 = pfnHandlerR0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pNew->pvUserR0 = pvUserR0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pNew->pfnHandlerRC = pfnHandlerRC;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pNew->pvUserRC = pvUserRC;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pNew->pszDesc = pszDesc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmLock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Try insert into list.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (RTAvlroGCPhysInsert(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, &pNew->Core))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(pVM, pNew, pRam);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (rc == VINF_PGM_SYNC_CR3)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VINF_PGM_GCPHYS_ALIASED;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pVM->pgm.s.fPhysCacheFlushPending = true;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmUnlock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync HWACCMFlushAllTLBs(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifndef IN_RING3
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync REMNotifyHandlerPhysicalRegister(pVM, enmType, GCPhys, GCPhysLast - GCPhys + 1, !!pfnHandlerR3);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync REMR3NotifyHandlerPhysicalRegister(pVM, enmType, GCPhys, GCPhysLast - GCPhys + 1, !!pfnHandlerR3);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (rc != VINF_SUCCESS)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Log(("PGMHandlerPhysicalRegisterEx: returns %Rrc (%RGp-%RGp)\n", rc, GCPhys, GCPhysLast));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return rc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmUnlock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#if defined(IN_RING3) && defined(VBOX_STRICT)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync DBGFR3Info(pVM, "handlers", "phys nostats", NULL);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("Conflict! GCPhys=%RGp GCPhysLast=%RGp pszDesc=%s\n", GCPhys, GCPhysLast, pszDesc));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync MMHyperFree(pVM, pNew);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VERR_PGM_HANDLER_PHYSICAL_CONFLICT;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Sets ram range flags and attempts updating shadow PTs.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
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.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncstatic int pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(PVM pVM, PPGMPHYSHANDLER pCur, PPGMRAMRANGE pRam)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Iterate the guest ram pages updating the flags and flushing PT entries
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * mapping the page.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync bool fFlushTLBs = false;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rc = VINF_SUCCESS;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync const unsigned uState = pgmHandlerPhysicalCalcState(pCur);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync uint32_t cPages = pCur->cPages;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync uint32_t i = (pCur->Core.Key - pRam->GCPhys) >> PAGE_SHIFT;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync for (;;)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPAGE pPage = &pRam->aPages[i];
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsg(pCur->enmType != PGMPHYSHANDLERTYPE_MMIO || PGM_PAGE_IS_MMIO(pPage),
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync ("%RGp %R[pgmpage]\n", pRam->GCPhys + (i << PAGE_SHIFT), pPage));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* Only do upgrades. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) < uState)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, uState);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rc2 = pgmPoolTrackFlushGCPhys(pVM, pPage, &fFlushTLBs);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (rc2 != VINF_SUCCESS && rc == VINF_SUCCESS)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = rc2;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* next */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (--cPages == 0)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync break;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync i++;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (fFlushTLBs && rc == VINF_SUCCESS)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PGM_INVL_GUEST_TLBS(VMMGetCpu(pVM));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Log(("pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs: flushing guest TLBs\n"));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Log(("pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs: doesn't flush guest TLBs. rc=%Rrc\n", rc));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return rc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Register a physical page access handler.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @returns VBox status code.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pVM VM Handle.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param GCPhys Start physical address.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncVMMDECL(int) PGMHandlerPhysicalDeregister(PVM pVM, RTGCPHYS GCPhys)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Find the handler.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmLock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (pCur)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync LogFlow(("PGMHandlerPhysicalDeregister: Removing Range %RGp-%RGp %s\n",
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pCur->Core.Key, pCur->Core.KeyLast, R3STRING(pCur->pszDesc)));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Clear the page bits and notify the REM about this change.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmHandlerPhysicalResetRamFlags(pVM, pCur);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmHandlerPhysicalDeregisterNotifyREM(pVM, pCur);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmUnlock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync HWACCMFlushAllTLBs(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync MMHyperFree(pVM, pCur);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VINF_SUCCESS;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmUnlock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("Didn't find range starting at %RGp\n", GCPhys));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VERR_PGM_HANDLER_NOT_FOUND;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Shared code with modify.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncstatic void pgmHandlerPhysicalDeregisterNotifyREM(PVM pVM, PPGMPHYSHANDLER pCur)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync RTGCPHYS GCPhysStart = pCur->Core.Key;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync RTGCPHYS GCPhysLast = pCur->Core.KeyLast;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Page align the range.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
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 */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( (pCur->Core.Key & PAGE_OFFSET_MASK)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync || ((pCur->Core.KeyLast + 1) & PAGE_OFFSET_MASK))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert(pCur->enmType != PGMPHYSHANDLERTYPE_MMIO);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (GCPhysStart & PAGE_OFFSET_MASK)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhysStart);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( pPage
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != PGM_PAGE_HNDL_PHYS_STATE_NONE)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync RTGCPHYS GCPhys = (GCPhysStart + (PAGE_SIZE - 1)) & X86_PTE_PAE_PG_MASK;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( GCPhys > GCPhysLast
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync || GCPhys < GCPhysStart)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync GCPhysStart = GCPhys;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync GCPhysStart &= X86_PTE_PAE_PG_MASK;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert(!pPage || PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_MMIO); /* these are page aligned atm! */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (GCPhysLast & PAGE_OFFSET_MASK)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhysLast);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( pPage
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != PGM_PAGE_HNDL_PHYS_STATE_NONE)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync RTGCPHYS GCPhys = (GCPhysLast & X86_PTE_PAE_PG_MASK) - 1;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( GCPhys < GCPhysStart
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync || GCPhys > GCPhysLast)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync GCPhysLast = GCPhys;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync GCPhysLast |= PAGE_OFFSET_MASK;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert(!pPage || PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_MMIO); /* these are page aligned atm! */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Tell REM.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync const bool fRestoreAsRAM = pCur->pfnHandlerR3
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && pCur->enmType != PGMPHYSHANDLERTYPE_MMIO; /** @todo this isn't entirely correct. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifndef IN_RING3
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync REMNotifyHandlerPhysicalDeregister(pVM, pCur->enmType, GCPhysStart, GCPhysLast - GCPhysStart + 1, !!pCur->pfnHandlerR3, fRestoreAsRAM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync REMR3NotifyHandlerPhysicalDeregister(pVM, pCur->enmType, GCPhysStart, GCPhysLast - GCPhysStart + 1, !!pCur->pfnHandlerR3, fRestoreAsRAM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * pgmHandlerPhysicalResetRamFlags helper that checks for
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * other handlers on edge pages.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncDECLINLINE(void) pgmHandlerPhysicalRecalcPageState(PPGM pPGM, RTGCPHYS GCPhys, bool fAbove, PPGMRAMRANGE *ppRamHint)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Look for other handlers.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync unsigned uState = PGM_PAGE_HNDL_PHYS_STATE_NONE;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync for (;;)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pPGM->CTX_SUFF(pTrees)->PhysHandlers, GCPhys, fAbove);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( !pCur
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync || ((fAbove ? pCur->Core.Key : pCur->Core.KeyLast) >> PAGE_SHIFT) != (GCPhys >> PAGE_SHIFT))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync break;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync unsigned uThisState = pgmHandlerPhysicalCalcState(pCur);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync uState = RT_MAX(uState, uThisState);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* next? */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync RTGCPHYS GCPhysNext = fAbove
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync ? pCur->Core.KeyLast + 1
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync : pCur->Core.Key - 1;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ((GCPhysNext >> PAGE_SHIFT) != (GCPhys >> PAGE_SHIFT))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync break;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync GCPhys = GCPhysNext;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Update if we found something that is a higher priority
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * state than the current.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (uState != PGM_PAGE_HNDL_PHYS_STATE_NONE)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPAGE pPage;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rc = pgmPhysGetPageWithHintEx(pPGM, GCPhys, &pPage, ppRamHint);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( RT_SUCCESS(rc)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) < uState)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, uState);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertRC(rc);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Resets an aliased page.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pVM The VM.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pPage The page.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param GCPhysPage The page address in case it comes in handy.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncvoid pgmHandlerPhysicalResetAliasedPage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhysPage)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
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
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Flush any shadow page table references *first*.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync bool fFlushTLBs = false;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rc = pgmPoolTrackFlushGCPhys(pVM, pPage, &fFlushTLBs);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertLogRelRCReturnVoid(rc);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync# ifdef IN_RC
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (fFlushTLBs && rc != VINF_PGM_SYNC_CR3)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PGM_INVL_GUEST_TLBS(VMMGetCpu0(pVM));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync# else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync HWACCMFlushAllTLBs(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync# endif
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pVM->pgm.s.fPhysCacheFlushPending = true;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Make it an MMIO/Zero page.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PGM_PAGE_SET_HCPHYS(pPage, pVM->pgm.s.HCPhysZeroPg);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PGM_PAGE_SET_TYPE(pPage, PGMPAGETYPE_MMIO);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ZERO);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PGM_PAGE_SET_PAGEID(pPage, NIL_GMM_PAGEID);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_ALL);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync NOREF(GCPhysPage);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Resets ram range flags.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
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 *
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).
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncstatic void pgmHandlerPhysicalResetRamFlags(PVM pVM, PPGMPHYSHANDLER pCur)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Iterate the guest ram pages updating the state.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync RTUINT cPages = pCur->cPages;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync RTGCPHYS GCPhys = pCur->Core.Key;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMRAMRANGE pRamHint = NULL;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGM pPGM = &pVM->pgm.s;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync for (;;)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPAGE pPage;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rc = pgmPhysGetPageWithHintEx(pPGM, GCPhys, &pPage, &pRamHint);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (RT_SUCCESS(rc))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
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 }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertRC(rc);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* next */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (--cPages == 0)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync break;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync GCPhys += PAGE_SIZE;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Check for partial start and end pages.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (pCur->Core.Key & PAGE_OFFSET_MASK)
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}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Modify a physical page access handler.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Modification can only be done to the range it self, not the type or anything else.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
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.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncVMMDECL(int) PGMHandlerPhysicalModify(PVM pVM, RTGCPHYS GCPhysCurrent, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Remove it.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmLock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhysCurrent);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (pCur)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Clear the ram flags. (We're gonna move or free it!)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmHandlerPhysicalResetRamFlags(pVM, pCur);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync const bool fRestoreAsRAM = pCur->pfnHandlerR3
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && pCur->enmType != PGMPHYSHANDLERTYPE_MMIO; /** @todo this isn't entirely correct. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Validate the new range, modify and reinsert.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (GCPhysLast >= GCPhys)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
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 */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync while (pRam && GCPhys > pRam->GCPhysLast)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pRam = pRam->CTX_SUFF(pNext);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( pRam
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && GCPhys <= pRam->GCPhysLast
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && GCPhysLast >= pRam->GCPhys)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pCur->Core.Key = GCPhys;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pCur->Core.KeyLast = GCPhysLast;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pCur->cPages = (GCPhysLast - (GCPhys & X86_PTE_PAE_PG_MASK) + 1) >> PAGE_SHIFT;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (RTAvlroGCPhysInsert(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, &pCur->Core))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Set ram flags, flush shadow PT entries and finally tell REM about this.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(pVM, pCur, pRam);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pVM->pgm.s.fPhysCacheFlushPending = true;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifndef IN_RING3
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync REMNotifyHandlerPhysicalModify(pVM, pCur->enmType, GCPhysCurrent, GCPhys,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pCur->Core.KeyLast - GCPhys + 1, !!pCur->pfnHandlerR3, fRestoreAsRAM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync REMR3NotifyHandlerPhysicalModify(pVM, pCur->enmType, GCPhysCurrent, GCPhys,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pCur->Core.KeyLast - GCPhys + 1, !!pCur->pfnHandlerR3, fRestoreAsRAM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmUnlock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync HWACCMFlushAllTLBs(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Log(("PGMHandlerPhysicalModify: GCPhysCurrent=%RGp -> GCPhys=%RGp GCPhysLast=%RGp\n",
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync GCPhysCurrent, GCPhys, GCPhysLast));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VINF_SUCCESS;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("Conflict! GCPhys=%RGp GCPhysLast=%RGp\n", GCPhys, GCPhysLast));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VERR_PGM_HANDLER_PHYSICAL_CONFLICT;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("No RAM range for %RGp-%RGp\n", GCPhys, GCPhysLast));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VERR_PGM_HANDLER_PHYSICAL_NO_RAM_RANGE;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("Invalid range %RGp-%RGp\n", GCPhys, GCPhysLast));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VERR_INVALID_PARAMETER;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Invalid new location, free it.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * We've only gotta notify REM and free the memory.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmHandlerPhysicalDeregisterNotifyREM(pVM, pCur);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync MMHyperFree(pVM, pCur);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("Didn't find range starting at %RGp\n", GCPhysCurrent));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VERR_PGM_HANDLER_NOT_FOUND;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmUnlock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return rc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Changes the callbacks associated with a physical access handler.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
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.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
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 R3PTRTYPE(const char *) pszDesc)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Get the handler.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rc = VINF_SUCCESS;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmLock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (pCur)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Change callbacks.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pCur->pfnHandlerR3 = pfnHandlerR3;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pCur->pvUserR3 = pvUserR3;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pCur->pfnHandlerR0 = pfnHandlerR0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pCur->pvUserR0 = pvUserR0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pCur->pfnHandlerRC = pfnHandlerRC;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pCur->pvUserRC = pvUserRC;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pCur->pszDesc = pszDesc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("Didn't find range starting at %RGp\n", GCPhys));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VERR_PGM_HANDLER_NOT_FOUND;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmUnlock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return rc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Splits a physical access handler in two.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
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.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncVMMDECL(int) PGMHandlerPhysicalSplit(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysSplit)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertReturn(GCPhys < GCPhysSplit, VERR_INVALID_PARAMETER);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Do the allocation without owning the lock.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPHYSHANDLER pNew;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rc = MMHyperAlloc(pVM, sizeof(*pNew), 0, MM_TAG_PGM_HANDLERS, (void **)&pNew);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (RT_FAILURE(rc))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return rc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Get the handler.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmLock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (RT_LIKELY(pCur))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (RT_LIKELY(GCPhysSplit <= pCur->Core.KeyLast))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Create new handler node for the 2nd half.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *pNew = *pCur;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pNew->Core.Key = GCPhysSplit;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pNew->cPages = (pNew->Core.KeyLast - (pNew->Core.Key & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pCur->Core.KeyLast = GCPhysSplit - 1;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pCur->cPages = (pCur->Core.KeyLast - (pCur->Core.Key & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (RT_LIKELY(RTAvlroGCPhysInsert(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, &pNew->Core)))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync LogFlow(("PGMHandlerPhysicalSplit: %RGp-%RGp and %RGp-%RGp\n",
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pCur->Core.Key, pCur->Core.KeyLast, pNew->Core.Key, pNew->Core.KeyLast));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmUnlock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VINF_SUCCESS;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("whu?\n"));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VERR_INTERNAL_ERROR;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("outside range: %RGp-%RGp split %RGp\n", pCur->Core.Key, pCur->Core.KeyLast, GCPhysSplit));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VERR_INVALID_PARAMETER;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("Didn't find range starting at %RGp\n", GCPhys));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VERR_PGM_HANDLER_NOT_FOUND;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmUnlock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync MMHyperFree(pVM, pNew);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return rc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Joins up two adjacent physical access handlers which has the same callbacks.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
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.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncVMMDECL(int) PGMHandlerPhysicalJoin(PVM pVM, RTGCPHYS GCPhys1, RTGCPHYS GCPhys2)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Get the handlers.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmLock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPHYSHANDLER pCur1 = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys1);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (RT_LIKELY(pCur1))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPHYSHANDLER pCur2 = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys2);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (RT_LIKELY(pCur2))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Make sure that they are adjacent, and that they've got the same callbacks.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (RT_LIKELY(pCur1->Core.KeyLast + 1 == pCur2->Core.Key))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (RT_LIKELY( pCur1->pfnHandlerRC == pCur2->pfnHandlerRC
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && pCur1->pfnHandlerR0 == pCur2->pfnHandlerR0
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && pCur1->pfnHandlerR3 == pCur2->pfnHandlerR3))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPHYSHANDLER pCur3 = (PPGMPHYSHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys2);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (RT_LIKELY(pCur3 == pCur2))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pCur1->Core.KeyLast = pCur2->Core.KeyLast;
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 pgmUnlock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync MMHyperFree(pVM, pCur2);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VINF_SUCCESS;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert(pCur3 == pCur2);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VERR_INTERNAL_ERROR;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("mismatching handlers\n"));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VERR_ACCESS_DENIED;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("not adjacent: %RGp-%RGp %RGp-%RGp\n",
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pCur1->Core.Key, pCur1->Core.KeyLast, pCur2->Core.Key, pCur2->Core.KeyLast));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VERR_INVALID_PARAMETER;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("Didn't find range starting at %RGp\n", GCPhys2));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VERR_PGM_HANDLER_NOT_FOUND;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("Didn't find range starting at %RGp\n", GCPhys1));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VERR_PGM_HANDLER_NOT_FOUND;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmUnlock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return rc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Resets any modifications to individual pages in a physical
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * page access handler region.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * This is used in pair with PGMHandlerPhysicalPageTempOff() or
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * PGMHandlerPhysicalPageAlias().
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
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().
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncVMMDECL(int) PGMHandlerPhysicalReset(PVM pVM, RTGCPHYS GCPhys)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmLock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Find the handler.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (RT_LIKELY(pCur))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Validate type.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync switch (pCur->enmType)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync case PGMPHYSHANDLERTYPE_PHYSICAL_WRITE:
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync case PGMPHYSHANDLERTYPE_PHYSICAL_ALL:
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync case PGMPHYSHANDLERTYPE_MMIO: /* NOTE: Only use when clearing MMIO ranges with aliased MMIO2 pages! */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
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 Assert(pRam);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert(pRam->GCPhys <= pCur->Core.Key);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert(pRam->GCPhysLast >= pCur->Core.KeyLast);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (pCur->enmType == PGMPHYSHANDLERTYPE_MMIO)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
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 */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPAGE pPage = &pRam->aPages[(pCur->Core.Key - pRam->GCPhys) >> PAGE_SHIFT];
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync uint32_t cLeft = pCur->cPages;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync while (cLeft-- > 0)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
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 pPage++;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Set the flags and flush shadow PT entries.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(pVM, pCur, pRam);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pVM->pgm.s.fPhysCacheFlushPending = true;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync HWACCMFlushAllTLBs(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VINF_SUCCESS;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync break;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Invalid.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync default:
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("Invalid type %d! Corruption!\n", pCur->enmType));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VERR_INTERNAL_ERROR;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync break;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("Didn't find MMIO Range starting at %#x\n", GCPhys));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VERR_PGM_HANDLER_NOT_FOUND;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmUnlock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return rc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Temporarily turns off the access monitoring of a page within a monitored
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * physical write/all page access handler region.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
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 *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * The caller must do required page table modifications.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
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.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncVMMDECL(int) PGMHandlerPhysicalPageTempOff(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysPage)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Validate the range.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (RT_LIKELY(pCur))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (RT_LIKELY( GCPhysPage >= pCur->Core.Key
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && GCPhysPage <= pCur->Core.KeyLast))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert(!(pCur->Core.Key & PAGE_OFFSET_MASK));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert((pCur->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertReturn( pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_WRITE
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync || pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_ALL,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync VERR_ACCESS_DENIED);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Change the page status.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPAGE pPage;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhysPage, &pPage);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertRCReturn(rc, rc);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_DISABLED);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifndef IN_RC
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync HWACCMInvalidatePhysPage(VMMGetCpu(pVM), GCPhysPage);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VINF_SUCCESS;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("The page %#x is outside the range %#x-%#x\n",
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync GCPhysPage, pCur->Core.Key, pCur->Core.KeyLast));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VERR_INVALID_PARAMETER;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("Specified physical handler start address %#x is invalid.\n", GCPhys));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VERR_PGM_HANDLER_NOT_FOUND;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Replaces an MMIO page with an MMIO2 page.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
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 *
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 *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Call PGMHandlerPhysicalReset() to restore the MMIO page.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
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 *
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 *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @remark May cause a page pool flush if used on a page that is already
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * aliased.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
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.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncVMMDECL(int) PGMHandlerPhysicalPageAlias(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysPage, RTGCPHYS GCPhysPageRemap)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/// Assert(!IOMIsLockOwner(pVM)); /* We mustn't own any other locks when calling this */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Lookup and validate the range.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (RT_LIKELY(pCur))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (RT_LIKELY( GCPhysPage >= pCur->Core.Key
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && GCPhysPage <= pCur->Core.KeyLast))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
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
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Get and validate the two pages.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPAGE pPageRemap;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhysPageRemap, &pPageRemap);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertRCReturn(rc, rc);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgReturn(PGM_PAGE_GET_TYPE(pPageRemap) == PGMPAGETYPE_MMIO2,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync ("GCPhysPageRemap=%RGp %R[pgmpage]\n", GCPhysPageRemap, pPageRemap),
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync VERR_PGM_PHYS_NOT_MMIO2);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPAGE pPage;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhysPage, &pPage);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertRCReturn(rc, rc);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_MMIO)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgReturn(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO2_ALIAS_MMIO,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync ("GCPhysPage=%RGp %R[pgmpage]\n", GCPhysPage, pPage),
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync VERR_PGM_PHYS_NOT_MMIO2);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (PGM_PAGE_GET_HCPHYS(pPage) == PGM_PAGE_GET_HCPHYS(pPage))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VINF_PGM_HANDLER_ALREADY_ALIASED;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * The page is already mapped as some other page, reset it
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * to an MMIO/ZERO page before doing the new mapping.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
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 }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert(PGM_PAGE_IS_ZERO(pPage));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Do the actual remapping here.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * This page now serves as an alias for the backing memory specified.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync LogFlow(("PGMHandlerPhysicalPageAlias: %RGp (%R[pgmpage]) alias for %RGp (%R[pgmpage])\n",
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync GCPhysPage, pPage, GCPhysPageRemap, pPageRemap ));
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
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifndef IN_RC
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync HWACCMInvalidatePhysPage(VMMGetCpu(pVM), GCPhysPage);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VINF_SUCCESS;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("The page %#x is outside the range %#x-%#x\n",
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync GCPhysPage, pCur->Core.Key, pCur->Core.KeyLast));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VERR_INVALID_PARAMETER;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("Specified physical handler start address %#x is invalid.\n", GCPhys));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VERR_PGM_HANDLER_NOT_FOUND;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Checks if a physical range is handled
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
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.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncVMMDECL(bool) PGMHandlerPhysicalIsRegistered(PVM pVM, RTGCPHYS GCPhys)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Find the handler.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (pCur)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert(GCPhys >= pCur->Core.Key && GCPhys <= pCur->Core.KeyLast);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert( pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_WRITE
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync || pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_ALL
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync || pCur->enmType == PGMPHYSHANDLERTYPE_MMIO);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return true;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return false;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Checks if it's an disabled all access handler or write access handler at the
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * given address.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @returns true if it's an all access handler, false if it's a write access
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * handler.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pVM Pointer to the shared VM structure.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param GCPhys The address of the page with a disabled handler.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @remarks The caller, PGMR3PhysTlbGCPhys2Ptr, must hold the PGM lock.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncbool pgmHandlerPhysicalIsAll(PVM pVM, RTGCPHYS GCPhys)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertReturn(pCur, true);
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}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Check if particular guest's VA is being monitored.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
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.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncVMMDECL(bool) PGMHandlerVirtualIsRegistered(PVM pVM, RTGCPTR GCPtr)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmLock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrGet(&pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers, GCPtr);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmUnlock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return pCur != NULL;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Search for virtual handler with matching physical address
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
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.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncint pgmHandlerVirtualFindByPhysAddr(PVM pVM, RTGCPHYS GCPhys, PPGMVIRTHANDLER *ppVirt, unsigned *piPage)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync STAM_PROFILE_START(&pVM->pgm.s.CTX_MID_Z(Stat,VirtHandlerSearchByPhys), a);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert(ppVirt);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmLock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPHYS2VIRTHANDLER pCur;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pCur = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, GCPhys);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (pCur)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* found a match! */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *ppVirt = (PPGMVIRTHANDLER)((uintptr_t)pCur + pCur->offVirtHandler);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *piPage = pCur - &(*ppVirt)->aPhysToVirt[0];
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmUnlock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertRelease(pCur->offNextAlias & PGMPHYS2VIRTHANDLER_IS_HEAD);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif
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 return VINF_SUCCESS;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmUnlock(pVM);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *ppVirt = NULL;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync STAM_PROFILE_STOP(&pVM->pgm.s.CTX_MID_Z(Stat,VirtHandlerSearchByPhys), a);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VERR_PGM_HANDLER_NOT_FOUND;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Deal with aliases in phys2virt.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * As pointed out by the various todos, this currently only deals with
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * aliases where the two ranges match 100%.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pVM The VM handle.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pPhys2Virt The node we failed insert.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncstatic void pgmHandlerVirtualInsertAliased(PVM pVM, PPGMPHYS2VIRTHANDLER pPhys2Virt)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * First find the node which is conflicting with us.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
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#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertReleaseMsg(pHead != pPhys2Virt, ("%RGp-%RGp offVirtHandler=%#RX32\n",
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (RT_UNLIKELY(!pHead || pHead->Core.KeyLast != pPhys2Virt->Core.KeyLast))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /** @todo do something clever here... */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync LogRel(("pgmHandlerVirtualInsertAliased: %RGp-%RGp\n", pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pPhys2Virt->offNextAlias = 0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Insert ourselves as the next node.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (!(pHead->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pPhys2Virt->offNextAlias = PGMPHYS2VIRTHANDLER_IN_TREE;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPHYS2VIRTHANDLER pNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pHead + (pHead->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pPhys2Virt->offNextAlias = ((intptr_t)pNext - (intptr_t)pPhys2Virt)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync | PGMPHYS2VIRTHANDLER_IN_TREE;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
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}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Resets one virtual handler range.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
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 *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @returns 0
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pNode Pointer to a PGMVIRTHANDLER.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pvUser The VM handle.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncDECLCALLBACK(int) pgmHandlerVirtualResetOne(PAVLROGCPTRNODECORE pNode, void *pvUser)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)pNode;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PVM pVM = (PVM)pvUser;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert(PGMIsLockOwner(pVM));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Iterate the pages and apply the new state.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync unsigned uState = pgmHandlerVirtualCalcState(pCur);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMRAMRANGE pRamHint = NULL;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync RTGCUINTPTR offPage = ((RTGCUINTPTR)pCur->Core.Key & PAGE_OFFSET_MASK);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync RTGCUINTPTR cbLeft = pCur->cb;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync for (unsigned iPage = 0; iPage < pCur->cPages; iPage++)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPHYS2VIRTHANDLER pPhys2Virt = &pCur->aPhysToVirt[iPage];
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (pPhys2Virt->Core.Key != NIL_RTGCPHYS)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Update the page state wrt virtual handlers.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPAGE pPage;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rc = pgmPhysGetPageWithHintEx(&pVM->pgm.s, pPhys2Virt->Core.Key, &pPage, &pRamHint);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( RT_SUCCESS(rc)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) < uState)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PGM_PAGE_SET_HNDL_VIRT_STATE(pPage, uState);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertRC(rc);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Need to insert the page in the Phys2Virt lookup tree?
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (pPhys2Virt->Core.KeyLast == NIL_RTGCPHYS)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertRelease(!pPhys2Virt->offNextAlias);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync unsigned cbPhys = cbLeft;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (cbPhys > PAGE_SIZE - offPage)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync cbPhys = PAGE_SIZE - offPage;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert(iPage == pCur->cPages - 1);
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 pgmHandlerVirtualInsertAliased(pVM, pPhys2Virt);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
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#endif
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 }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync cbLeft -= PAGE_SIZE - offPage;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync offPage = 0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return 0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#if defined(VBOX_STRICT) || defined(LOG_ENABLED)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Worker for pgmHandlerVirtualDumpPhysPages.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @returns 0 (continue enumeration).
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pNode The virtual handler node.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pvUser User argument, unused.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncstatic DECLCALLBACK(int) pgmHandlerVirtualDumpPhysPagesCallback(PAVLROGCPHYSNODECORE pNode, void *pvUser)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
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 return 0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Assertion / logging helper for dumping all the
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * virtual handlers to the log.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pVM Pointer to the shared VM structure.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncvoid pgmHandlerVirtualDumpPhysPages(PVM pVM)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync RTAvlroGCPhysDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, true /* from left */,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pgmHandlerVirtualDumpPhysPagesCallback, 0);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif /* VBOX_STRICT || LOG_ENABLED */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifdef VBOX_STRICT
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * State structure used by the PGMAssertHandlerAndFlagsInSync() function
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * and its AVL enumerators.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsynctypedef struct PGMAHAFIS
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /** The current physical address. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync RTGCPHYS GCPhys;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /** The state we've calculated. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync unsigned uVirtStateFound;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /** The state we're matching up to. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync unsigned uVirtState;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /** Number of errors. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync unsigned cErrors;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /** The VM handle. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PVM pVM;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync} PGMAHAFIS, *PPGMAHAFIS;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#if 0 /* unused */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Verify virtual handler by matching physical address.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @returns 0
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pNode Pointer to a PGMVIRTHANDLER.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pvUser Pointer to user parameter.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncstatic DECLCALLBACK(int) pgmHandlerVirtualVerifyOneByPhysAddr(PAVLROGCPTRNODECORE pNode, void *pvUser)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)pNode;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMAHAFIS pState = (PPGMAHAFIS)pvUser;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync for (unsigned iPage = 0; iPage < pCur->cPages; iPage++)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ((pCur->aPhysToVirt[iPage].Core.Key & X86_PTE_PAE_PG_MASK) == pState->GCPhys)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync unsigned uState = pgmHandlerVirtualCalcState(pCur);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (pState->uVirtState < uState)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync error
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (pState->uVirtState == uState)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync break; //??
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return 0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif /* unused */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Verify a virtual handler (enumeration callback).
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Called by PGMAssertHandlerAndFlagsInSync to check the sanity of all
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * the virtual handlers, esp. that the physical addresses matches up.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @returns 0
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pNode Pointer to a PGMVIRTHANDLER.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pvUser Pointer to a PPGMAHAFIS structure.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncstatic DECLCALLBACK(int) pgmHandlerVirtualVerifyOne(PAVLROGCPTRNODECORE pNode, void *pvUser)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMVIRTHANDLER pVirt = (PPGMVIRTHANDLER)pNode;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMAHAFIS pState = (PPGMAHAFIS)pvUser;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PVM pVM = pState->pVM;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Validate the type and calc state.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync switch (pVirt->enmType)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync case PGMVIRTHANDLERTYPE_WRITE:
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync case PGMVIRTHANDLERTYPE_ALL:
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync break;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync default:
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("unknown/wrong enmType=%d\n", pVirt->enmType));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pState->cErrors++;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return 0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync const unsigned uState = pgmHandlerVirtualCalcState(pVirt);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Check key alignment.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( (pVirt->aPhysToVirt[0].Core.Key & PAGE_OFFSET_MASK) != ((RTGCUINTPTR)pVirt->Core.Key & PAGE_OFFSET_MASK)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && pVirt->aPhysToVirt[0].Core.Key != NIL_RTGCPHYS)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
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 pState->cErrors++;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
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 {
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 pState->cErrors++;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Check pages for sanity and state.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync RTGCUINTPTR GCPtr = (RTGCUINTPTR)pVirt->Core.Key;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync for (unsigned iPage = 0; iPage < pVirt->cPages; iPage++, GCPtr += PAGE_SIZE)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync for (unsigned i=0;i<pVM->cCPUs;i++)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PVMCPU pVCpu = &pVM->aCpus[i];
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync RTGCPHYS GCPhysGst;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync uint64_t fGst;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rc = PGMGstGetPage(pVCpu, (RTGCPTR)GCPtr, &fGst, &GCPhysGst);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( rc == VERR_PAGE_NOT_PRESENT
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync || rc == VERR_PAGE_TABLE_NOT_PRESENT)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (pVirt->aPhysToVirt[iPage].Core.Key != NIL_RTGCPHYS)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
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 pState->cErrors++;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync continue;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertRCReturn(rc, 0);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ((pVirt->aPhysToVirt[iPage].Core.Key & X86_PTE_PAE_PG_MASK) != GCPhysGst)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
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 pState->cErrors++;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync continue;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhysGst);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (!pPage)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("virt handler getting ram flags. GCPhysGst=%RGp iPage=%#x %RGv %s\n",
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync GCPhysGst, iPage, GCPtr, R3STRING(pVirt->pszDesc)));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pState->cErrors++;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync continue;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) < uState)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
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 pState->cErrors++;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync continue;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync } /* for each VCPU */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync } /* for pages in virtual mapping. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return 0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Asserts that the handlers+guest-page-tables == ramrange-flags and
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * that the physical addresses associated with virtual handlers are correct.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @returns Number of mismatches.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pVM The VM handle.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncVMMDECL(unsigned) PGMAssertHandlerAndFlagsInSync(PVM pVM)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGM pPGM = &pVM->pgm.s;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PGMAHAFIS State;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync State.GCPhys = 0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync State.uVirtState = 0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync State.uVirtStateFound = 0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync State.cErrors = 0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync State.pVM = pVM;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Check the RAM flags against the handlers.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync for (PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges); pRam; pRam = pRam->CTX_SUFF(pNext))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync const unsigned cPages = pRam->cb >> PAGE_SHIFT;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync for (unsigned iPage = 0; iPage < cPages; iPage++)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PGMPAGE const *pPage = &pRam->aPages[iPage];
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (PGM_PAGE_HAS_ANY_HANDLERS(pPage))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync State.GCPhys = pRam->GCPhys + (iPage << PAGE_SHIFT);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Physical first - calculate the state based on the handlers
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * active on the page, then compare.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (PGM_PAGE_HAS_ANY_PHYSICAL_HANDLERS(pPage))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* the first */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPHYSHANDLER pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pPGM->CTX_SUFF(pTrees)->PhysHandlers, State.GCPhys);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (!pPhys)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pPGM->CTX_SUFF(pTrees)->PhysHandlers, State.GCPhys, true);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( pPhys
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && pPhys->Core.Key > (State.GCPhys + PAGE_SIZE - 1))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pPhys = NULL;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert(!pPhys || pPhys->Core.Key >= State.GCPhys);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (pPhys)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync unsigned uState = pgmHandlerPhysicalCalcState(pPhys);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* more? */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync while (pPhys->Core.KeyLast < (State.GCPhys | PAGE_OFFSET_MASK))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPHYSHANDLER pPhys2 = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pPGM->CTX_SUFF(pTrees)->PhysHandlers,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pPhys->Core.KeyLast + 1, true);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( !pPhys2
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync || pPhys2->Core.Key > (State.GCPhys | PAGE_OFFSET_MASK))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync break;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync unsigned uState2 = pgmHandlerPhysicalCalcState(pPhys2);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync uState = RT_MAX(uState, uState2);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pPhys = pPhys2;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* compare.*/
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != uState
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != PGM_PAGE_HNDL_PHYS_STATE_DISABLED)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
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 State.cErrors++;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifdef IN_RING3
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* validate that REM is handling it. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( !REMR3IsPageAccessHandled(pVM, State.GCPhys)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* ignore shadowed ROM for the time being. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync && PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_ROM_SHADOW)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
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 State.cErrors++;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("ram range vs phys handler mismatch. no handler for GCPhys=%RGp\n", State.GCPhys));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync State.cErrors++;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Virtual handlers.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync State.uVirtState = PGM_PAGE_GET_HNDL_VIRT_STATE(pPage);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#if 1
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* locate all the matching physical ranges. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync State.uVirtStateFound = PGM_PAGE_HNDL_VIRT_STATE_NONE;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync RTGCPHYS GCPhysKey = State.GCPhys;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync for (;;)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMPHYS2VIRTHANDLER pPhys2Virt = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync GCPhysKey, true /* above-or-equal */);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ( !pPhys2Virt
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync || (pPhys2Virt->Core.Key & X86_PTE_PAE_PG_MASK) != State.GCPhys)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync break;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* the head */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync GCPhysKey = pPhys2Virt->Core.KeyLast;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)((uintptr_t)pPhys2Virt + pPhys2Virt->offVirtHandler);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync unsigned uState = pgmHandlerVirtualCalcState(pCur);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync State.uVirtStateFound = RT_MAX(State.uVirtStateFound, uState);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* any aliases */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync while (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pPhys2Virt = (PPGMPHYS2VIRTHANDLER)((uintptr_t)pPhys2Virt + (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pCur = (PPGMVIRTHANDLER)((uintptr_t)pPhys2Virt + pPhys2Virt->offVirtHandler);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync uState = pgmHandlerVirtualCalcState(pCur);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync State.uVirtStateFound = RT_MAX(State.uVirtStateFound, uState);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* done? */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if ((GCPhysKey & X86_PTE_PAE_PG_MASK) != State.GCPhys)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync break;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* very slow */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync RTAvlroGCPtrDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers, true, pgmHandlerVirtualVerifyOneByPhysAddr, &State);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (State.uVirtState != State.uVirtStateFound)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("ram range vs virt handler flags mismatch. GCPhys=%RGp uVirtState=%#x uVirtStateFound=%#x\n",
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync State.GCPhys, State.uVirtState, State.uVirtStateFound));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync State.cErrors++;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync } /* foreach page in ram range. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync } /* foreach ram range. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Check that the physical addresses of the virtual handlers matches up
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * and that they are otherwise sane.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync RTAvlroGCPtrDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers, true, pgmHandlerVirtualVerifyOne, &State);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Do the reverse check for physical handlers.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /** @todo */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return State.cErrors;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif /* VBOX_STRICT */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync