PGMAllPool.cpp revision 2b88b28162a49c6fa769c03d56767a9806d4f456
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/* $Id$ */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/** @file
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * PGM Shadow Page Pool.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/*
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * available from http://www.virtualbox.org. This file is free software;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * you can redistribute it and/or modify it under the terms of the GNU
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * General Public License (GPL) as published by the Free Software
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * additional information or have any questions.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/*******************************************************************************
7b9f0c34e9ea328981c99e97054bdf8684d9d620vboxsync* Header Files *
9e7e0b61d29309a0ed7af9472c8d6d865f9e8a2dvboxsync*******************************************************************************/
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#define LOG_GROUP LOG_GROUP_PGM_POOL
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#include <VBox/pgm.h>
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#include <VBox/mm.h>
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync#include <VBox/em.h>
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#include <VBox/cpum.h>
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#ifdef IN_RC
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# include <VBox/patm.h>
b1cc3e87518139898395f96974ecff9e6bf228fbvboxsync#endif
b1cc3e87518139898395f96974ecff9e6bf228fbvboxsync#include "PGMInternal.h"
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#include <VBox/vm.h>
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync#include <VBox/disopcode.h>
388b6b190a5407548753b7fde12fa58134ec3563vboxsync#include <VBox/hwacc_vmx.h>
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#include <VBox/log.h>
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#include <VBox/err.h>
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync#include <iprt/asm.h>
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync/*******************************************************************************
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync* Internal Functions *
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync*******************************************************************************/
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync__BEGIN_DECLS
2f3883b126a405f92b19e829472f614c7352b4f9vboxsyncstatic void pgmPoolFlushAllInt(PPGMPOOL pPool);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#ifdef PGMPOOL_WITH_USER_TRACKING
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncDECLINLINE(unsigned) pgmPoolTrackGetShadowEntrySize(PGMPOOLKIND enmKind);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncDECLINLINE(unsigned) pgmPoolTrackGetGuestEntrySize(PGMPOOLKIND enmKind);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncstatic void pgmPoolTrackDeref(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#endif
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncstatic void pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#endif
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#ifdef PGMPOOL_WITH_CACHE
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncstatic int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#endif
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#ifdef PGMPOOL_WITH_MONITORING
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncstatic void pgmPoolMonitorModifiedRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#endif
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#ifndef IN_RING3
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncDECLEXPORT(int) pgmPoolAccessHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#endif
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync__END_DECLS
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync/**
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * Checks if the specified page pool kind is for a 4MB or 2MB guest page.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync *
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @returns true if it's the shadow of a 4MB or 2MB guest page, otherwise false.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @param enmKind The page kind.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncDECLINLINE(bool) pgmPoolIsBigPage(PGMPOOLKIND enmKind)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync{
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync switch (enmKind)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync return true;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync default:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync return false;
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync }
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync}
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync/**
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync * Maps a pool page into the current context.
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync *
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync * @returns Pointer to the mapping.
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync * @param pPGM Pointer to the PGM instance data.
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync * @param pPage The page to map.
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync */
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsyncvoid *pgmPoolMapPageFallback(PPGM pPGM, PPGMPOOLPAGE pPage)
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync{
e38a1f5933935fe8db152ea06dac5aa1be27d870vboxsync /* general pages are take care of by the inlined part, it
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync only ends up here in case of failure. */
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync AssertReleaseReturn(pPage->idx < PGMPOOL_IDX_FIRST, NULL);
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync/** @todo make sure HCPhys is valid for *all* indexes. */
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync /* special pages. */
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync# ifdef IN_RC
e38a1f5933935fe8db152ea06dac5aa1be27d870vboxsync switch (pPage->idx)
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync {
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync# ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync case PGMPOOL_IDX_PD:
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync case PGMPOOL_IDX_PDPT:
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync case PGMPOOL_IDX_AMD64_CR3:
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync return pPGM->pShwRootRC;
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync# else
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync case PGMPOOL_IDX_PD:
e38a1f5933935fe8db152ea06dac5aa1be27d870vboxsync return pPGM->pShw32BitPdRC;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOL_IDX_PAE_PD:
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync case PGMPOOL_IDX_PAE_PD_0:
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync return pPGM->apShwPaePDsRC[0];
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync case PGMPOOL_IDX_PAE_PD_1:
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync return pPGM->apShwPaePDsRC[1];
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync case PGMPOOL_IDX_PAE_PD_2:
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync return pPGM->apShwPaePDsRC[2];
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync case PGMPOOL_IDX_PAE_PD_3:
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync return pPGM->apShwPaePDsRC[3];
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync case PGMPOOL_IDX_PDPT:
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync return pPGM->pShwPaePdptRC;
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync# endif
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync default:
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync AssertReleaseMsgFailed(("Invalid index %d\n", pPage->idx));
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync return NULL;
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync }
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync# else /* VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
e38a1f5933935fe8db152ea06dac5aa1be27d870vboxsync RTHCPHYS HCPhys;
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync switch (pPage->idx)
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync {
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync# ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
581f0625e43a928987623d7cf59e1b1ab61ca6c8vboxsync case PGMPOOL_IDX_PD:
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync case PGMPOOL_IDX_PDPT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOL_IDX_AMD64_CR3:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync HCPhys = pPGM->HCPhysShwCR3;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOL_IDX_NESTED_ROOT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync HCPhys = pPGM->HCPhysShwNestedRoot;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
3f5dd727ecbcf3e99217c70f04bc2340beb9072cvboxsync# else
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOL_IDX_PD:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync HCPhys = pPGM->HCPhysShw32BitPD;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOL_IDX_PAE_PD_0:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync HCPhys = pPGM->aHCPhysPaePDs[0];
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOL_IDX_PAE_PD_1:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync HCPhys = pPGM->aHCPhysPaePDs[1];
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOL_IDX_PAE_PD_2:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync HCPhys = pPGM->aHCPhysPaePDs[2];
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOL_IDX_PAE_PD_3:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync HCPhys = pPGM->aHCPhysPaePDs[3];
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync break;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOL_IDX_PDPT:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync HCPhys = pPGM->HCPhysShwPaePdpt;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync break;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOL_IDX_NESTED_ROOT:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync HCPhys = pPGM->HCPhysShwNestedRoot;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync break;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOL_IDX_PAE_PD:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync AssertReleaseMsgFailed(("PGMPOOL_IDX_PAE_PD is not usable in VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 context\n"));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync return NULL;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync# endif
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync default:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync AssertReleaseMsgFailed(("Invalid index %d\n", pPage->idx));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync return NULL;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync AssertMsg(HCPhys && HCPhys != NIL_RTHCPHYS && !(PAGE_OFFSET_MASK & HCPhys), ("%RHp\n", HCPhys));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
fb41ad77bcfbdb3aaa1fc9503a37ee6a70dc6461vboxsync void *pv;
fb41ad77bcfbdb3aaa1fc9503a37ee6a70dc6461vboxsync pgmR0DynMapHCPageInlined(pPGM, HCPhys, &pv);
fb41ad77bcfbdb3aaa1fc9503a37ee6a70dc6461vboxsync return pv;
fb41ad77bcfbdb3aaa1fc9503a37ee6a70dc6461vboxsync# endif /* VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
fb41ad77bcfbdb3aaa1fc9503a37ee6a70dc6461vboxsync}
fb41ad77bcfbdb3aaa1fc9503a37ee6a70dc6461vboxsync#endif /* IN_RC || VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#ifdef PGMPOOL_WITH_MONITORING
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync/**
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * Determin the size of a write instruction.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @returns number of bytes written.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @param pDis The disassembler state.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncstatic unsigned pgmPoolDisasWriteSize(PDISCPUSTATE pDis)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync{
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /*
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * This is very crude and possibly wrong for some opcodes,
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * but since it's not really supposed to be called we can
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * probably live with that.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
fb41ad77bcfbdb3aaa1fc9503a37ee6a70dc6461vboxsync return DISGetParamSize(pDis, &pDis->param1);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync}
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync/**
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync * Flushes a chain of pages sharing the same access monitor.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync *
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @returns VBox status code suitable for scheduling.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param pPool The pool.
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync * @param pPage A page in the chain.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync */
cd5df721f068659172f3bf95de8fedeb465f057dvboxsyncint pgmPoolMonitorChainFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync{
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync LogFlow(("pgmPoolMonitorChainFlush: Flush page %RGp type=%d\n", pPage->GCPhys, pPage->enmKind));
c2590a722c15520a45551ed7dc02d4831fb1a48evboxsync
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync /*
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync * Find the list head.
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync uint16_t idx = pPage->idx;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync {
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync while (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync {
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync idx = pPage->iMonitoredPrev;
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync Assert(idx != pPage->idx);
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync pPage = &pPool->aPages[idx];
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync }
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync }
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync /*
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync * Iterate the list flushing each shadow page.
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync */
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync int rc = VINF_SUCCESS;
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync for (;;)
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync {
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync idx = pPage->iMonitoredNext;
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync Assert(idx != pPage->idx);
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync if (pPage->idx >= PGMPOOL_IDX_FIRST)
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync {
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync int rc2 = pgmPoolFlushPage(pPool, pPage);
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync if (rc2 == VERR_PGM_POOL_CLEARED && rc == VINF_SUCCESS)
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync rc = VINF_PGM_SYNC_CR3;
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync }
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync /* next */
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync if (idx == NIL_PGMPOOL_IDX)
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync break;
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync pPage = &pPool->aPages[idx];
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync }
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync return rc;
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync}
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync/**
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync * Wrapper for getting the current context pointer to the entry being modified.
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync *
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync * @returns Pointer to the current context mapping of the entry.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @param pPool The pool.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @param pvFault The fault virtual address.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @param GCPhysFault The fault physical address.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @param cbEntry The entry size.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#ifdef IN_RING3
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncDECLINLINE(const void *) pgmPoolMonitorGCPtr2CCPtr(PPGMPOOL pPool, RTHCPTR pvFault, RTGCPHYS GCPhysFault, const unsigned cbEntry)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#else
cd5df721f068659172f3bf95de8fedeb465f057dvboxsyncDECLINLINE(const void *) pgmPoolMonitorGCPtr2CCPtr(PPGMPOOL pPool, RTGCPTR pvFault, RTGCPHYS GCPhysFault, const unsigned cbEntry)
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync#endif
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync{
fb41ad77bcfbdb3aaa1fc9503a37ee6a70dc6461vboxsync#ifdef IN_RC
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync return (const void *)((RTGCUINTPTR)pvFault & ~(RTGCUINTPTR)(cbEntry - 1));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#elif defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync void *pvRet;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync int rc = PGMDynMapGCPageOff(pPool->pVMR0, GCPhysFault & ~(RTGCPHYS)(cbEntry - 1), &pvRet);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync AssertFatalRCSuccess(rc);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync return pvRet;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#elif defined(IN_RING0)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync void *pvRet;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync int rc = pgmRamGCPhys2HCPtr(&pPool->pVMR0->pgm.s, GCPhysFault & ~(RTGCPHYS)(cbEntry - 1), &pvRet);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync AssertFatalRCSuccess(rc);
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync return pvRet;
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync#elif defined(IN_RING3)
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync return (RTHCPTR)((uintptr_t)pvFault & ~(RTHCUINTPTR)(cbEntry - 1));
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync#else
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync# error "huh?"
4517f3d60a86111c8b70238b29d59f26c15291a7vboxsync#endif
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync}
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync/**
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync * Process shadow entries before they are changed by the guest.
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync *
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync * For PT entries we will clear them. For PD entries, we'll simply check
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync * for mapping conflicts and set the SyncCR3 FF if found.
f044158ee9eb7045a43f2c4ef2fbc07cb11329aevboxsync *
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync * @param pPool The pool.
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync * @param pPage The head page.
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync * @param GCPhysFault The guest physical fault address.
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync * @param uAddress In R0 and GC this is the guest context fault address (flat).
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync * In R3 this is the host context 'fault' address.
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync * @param pCpu The disassembler state for figuring out the write size.
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync * This need not be specified if the caller knows we won't do cross entry accesses.
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync */
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync#ifdef IN_RING3
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsyncvoid pgmPoolMonitorChainChanging(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault, RTHCPTR pvAddress, PDISCPUSTATE pCpu)
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync#else
f044158ee9eb7045a43f2c4ef2fbc07cb11329aevboxsyncvoid pgmPoolMonitorChainChanging(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault, RTGCPTR pvAddress, PDISCPUSTATE pCpu)
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync#endif
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync{
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync Assert(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync const unsigned off = GCPhysFault & PAGE_OFFSET_MASK;
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync const unsigned cbWrite = (pCpu) ? pgmPoolDisasWriteSize(pCpu) : 0;
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync LogFlow(("pgmPoolMonitorChainChanging: %RGv phys=%RGp kind=%d cbWrite=%d\n", pvAddress, GCPhysFault, pPage->enmKind, cbWrite));
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync for (;;)
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync {
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync union
f044158ee9eb7045a43f2c4ef2fbc07cb11329aevboxsync {
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync void *pv;
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync PX86PT pPT;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync PX86PTPAE pPTPae;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync PX86PD pPD;
4517f3d60a86111c8b70238b29d59f26c15291a7vboxsync PX86PDPAE pPDPae;
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync PX86PDPT pPDPT;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync PX86PML4 pPML4;
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync } uShw;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync switch (pPage->enmKind)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync const unsigned iShw = off / sizeof(X86PTE);
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync if (uShw.pPT->a[iShw].n.u1Present)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PCX86PTE pGstPte = (PCX86PTE)pgmPoolMonitorGCPtr2CCPtr(pPool, pvAddress, GCPhysFault, sizeof(*pGstPte));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Log4(("pgmPoolMonitorChainChanging 32_32: deref %016RX64 GCPhys %08RX32\n", uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK, pGstPte->u & X86_PTE_PG_MASK));
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage,
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK,
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pGstPte->u & X86_PTE_PG_MASK);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# endif
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uShw.pPT->a[iShw].u = 0;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync /* page/2 sized */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (!((off ^ pPage->GCPhys) & (PAGE_SIZE / 2)))
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync const unsigned iShw = (off / sizeof(X86PTE)) & (X86_PG_PAE_ENTRIES - 1);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (uShw.pPTPae->a[iShw].n.u1Present)
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PCX86PTE pGstPte = (PCX86PTE)pgmPoolMonitorGCPtr2CCPtr(pPool, pvAddress, GCPhysFault, sizeof(*pGstPte));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Log4(("pgmPoolMonitorChainChanging pae_32: deref %016RX64 GCPhys %08RX32\n", uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK, pGstPte->u & X86_PTE_PG_MASK));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage,
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync uShw.pPTPae->a[iShw].u & X86_PTE_PAE_PG_MASK,
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync pGstPte->u & X86_PTE_PG_MASK);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# endif
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uShw.pPTPae->a[iShw].u = 0;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync const unsigned iShw = off / sizeof(X86PTEPAE);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (uShw.pPTPae->a[iShw].n.u1Present)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PCX86PTEPAE pGstPte = (PCX86PTEPAE)pgmPoolMonitorGCPtr2CCPtr(pPool, pvAddress, GCPhysFault, sizeof(*pGstPte));
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync Log4(("pgmPoolMonitorChainChanging pae: deref %016RX64 GCPhys %016RX64\n", uShw.pPTPae->a[iShw].u & X86_PTE_PAE_PG_MASK, pGstPte->u & X86_PTE_PAE_PG_MASK));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage,
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uShw.pPTPae->a[iShw].u & X86_PTE_PAE_PG_MASK,
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pGstPte->u & X86_PTE_PAE_PG_MASK);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# endif
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync uShw.pPTPae->a[iShw].u = 0;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /* paranoia / a bit assumptive. */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if ( pCpu
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync && (off & 7)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync && (off & 7) + cbWrite > sizeof(X86PTEPAE))
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PTEPAE);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync AssertReturnVoid(iShw2 < RT_ELEMENTS(uShw.pPTPae->a));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync if (uShw.pPTPae->a[iShw2].n.u1Present)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PCX86PTEPAE pGstPte = (PCX86PTEPAE)pgmPoolMonitorGCPtr2CCPtr(pPool, pvAddress, GCPhysFault, sizeof(*pGstPte));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Log4(("pgmPoolMonitorChainChanging pae: deref %016RX64 GCPhys %016RX64\n", uShw.pPTPae->a[iShw2].u & X86_PTE_PAE_PG_MASK, pGstPte->u & X86_PTE_PAE_PG_MASK));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage,
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uShw.pPTPae->a[iShw2].u & X86_PTE_PAE_PG_MASK,
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pGstPte->u & X86_PTE_PAE_PG_MASK);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# endif
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uShw.pPTPae->a[iShw2].u = 0;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync }
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_32BIT_PD:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# else
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_ROOT_32BIT_PD:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync# endif
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync {
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync const unsigned iShw = off / sizeof(X86PTE); // ASSUMING 32-bit guest paging!
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync if (uShw.pPD->a[iShw].u & PGM_PDFLAGS_MAPPING)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTX_SUFF(pVM)->pgm.s));
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync STAM_COUNTER_INC(&(pPool->CTX_SUFF(pVM)->pgm.s.StatRZGuestCR3WriteConflict));
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync }
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /* paranoia / a bit assumptive. */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync else if ( pCpu
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync && (off & 3)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync && (off & 3) + cbWrite > sizeof(X86PTE))
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PTE);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync if ( iShw2 != iShw
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync && iShw2 < RT_ELEMENTS(uShw.pPD->a)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync && uShw.pPD->a[iShw2].u & PGM_PDFLAGS_MAPPING)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTX_SUFF(pVM)->pgm.s));
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync STAM_COUNTER_INC(&(pPool->CTX_SUFF(pVM)->pgm.s.StatRZGuestCR3WriteConflict));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#if 0 /* useful when running PGMAssertCR3(), a bit too troublesome for general use (TLBs). */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if ( uShw.pPD->a[iShw].n.u1Present
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync && !VM_FF_ISSET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3))
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("pgmPoolMonitorChainChanging: iShw=%#x: %RX32 -> freeing it!\n", iShw, uShw.pPD->a[iShw].u));
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync# ifdef IN_RC /* TLB load - we're pushing things a bit... */
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync ASMProbeReadByte(pvAddress);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# endif
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pgmPoolFree(pPool->CTX_SUFF(pVM), uShw.pPD->a[iShw].u & X86_PDE_PG_MASK, pPage->idx, iShw);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uShw.pPD->a[iShw].u = 0;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#endif
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# ifndef VBOX_WITH_PGMPOOL_PAGING_ONLY
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync case PGMPOOLKIND_ROOT_PAE_PD:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync unsigned iGst = off / sizeof(X86PDE); // ASSUMING 32-bit guest paging!
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync unsigned iShwPdpt = iGst / 256;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync unsigned iShw = (iGst % 256) * 2;
7e8ef90d3160234df0f254131b87af4243d79476vboxsync Assert(pPage->idx == PGMPOOL_IDX_PAE_PD);
7e8ef90d3160234df0f254131b87af4243d79476vboxsync PPGMPOOLPAGE pPage2 = pPage + 1 + iShwPdpt;
7e8ef90d3160234df0f254131b87af4243d79476vboxsync Assert(pPage2->idx == PGMPOOL_IDX_PAE_PD_0 + iShwPdpt);
7e8ef90d3160234df0f254131b87af4243d79476vboxsync uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage2);
7e8ef90d3160234df0f254131b87af4243d79476vboxsync for (unsigned i = 0; i < 2; i++, iShw++)
7e8ef90d3160234df0f254131b87af4243d79476vboxsync {
7e8ef90d3160234df0f254131b87af4243d79476vboxsync if ((uShw.pPDPae->a[iShw].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
7e8ef90d3160234df0f254131b87af4243d79476vboxsync {
7e8ef90d3160234df0f254131b87af4243d79476vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTX_SUFF(pVM)->pgm.s));
7e8ef90d3160234df0f254131b87af4243d79476vboxsync VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
7e8ef90d3160234df0f254131b87af4243d79476vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShwPdpt=%#x iShw=%#x!\n", iShwPdpt, iShw));
7e8ef90d3160234df0f254131b87af4243d79476vboxsync }
7e8ef90d3160234df0f254131b87af4243d79476vboxsync /* paranoia / a bit assumptive. */
7e8ef90d3160234df0f254131b87af4243d79476vboxsync else if ( pCpu
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync && (off & 3)
7e8ef90d3160234df0f254131b87af4243d79476vboxsync && (off & 3) + cbWrite > 4)
7e8ef90d3160234df0f254131b87af4243d79476vboxsync {
7e8ef90d3160234df0f254131b87af4243d79476vboxsync const unsigned iShw2 = iShw + 2;
7e8ef90d3160234df0f254131b87af4243d79476vboxsync if ( iShw2 < RT_ELEMENTS(uShw.pPDPae->a) /** @todo was completely wrong, it's better now after #1865 but still wrong from cross PD. */
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync && (uShw.pPDPae->a[iShw2].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync {
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTX_SUFF(pVM)->pgm.s));
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShwPdpt=%#x iShw2=%#x!\n", iShwPdpt, iShw2));
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync }
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync }
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync#if 0 /* useful when running PGMAssertCR3(), a bit too troublesome for general use (TLBs). */
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync if ( uShw.pPDPae->a[iShw].n.u1Present
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync && !VM_FF_ISSET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3))
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync {
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync LogFlow(("pgmPoolMonitorChainChanging: iShwPdpt=%#x iShw=%#x: %RX64 -> freeing it!\n", iShwPdpt, iShw, uShw.pPDPae->a[iShw].u));
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync# ifdef IN_RC /* TLB load - we're pushing things a bit... */
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync ASMProbeReadByte(pvAddress);
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync# endif
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync pgmPoolFree(pPool->CTX_SUFF(pVM), uShw.pPDPae->a[iShw].u & X86_PDE_PAE_PG_MASK, pPage->idx, iShw + iShwPdpt * X86_PG_PAE_ENTRIES);
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync uShw.pPDPae->a[iShw].u = 0;
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync }
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync#endif
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync }
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync break;
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# endif /* !VBOX_WITH_PGMPOOL_PAGING_ONLY */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync const unsigned iShw = off / sizeof(X86PDEPAE);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (uShw.pPDPae->a[iShw].u & PGM_PDFLAGS_MAPPING)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTX_SUFF(pVM)->pgm.s));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_COUNTER_INC(&(pPool->CTX_SUFF(pVM)->pgm.s.StatRZGuestCR3WriteConflict));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync#ifdef PGMPOOL_INVALIDATE_UPPER_SHADOW_TABLE_ENTRIES
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /*
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * Causes trouble when the guest uses a PDE to refer to the whole page table level
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * structure. (Invalidate here; faults later on when it tries to change the page
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * table entries -> recheck; probably only applies to the RC case.)
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync */
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync else
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync {
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync if (uShw.pPDPae->a[iShw].n.u1Present)
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync {
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync pgmPoolFree(pPool->CTX_SUFF(pVM),
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync uShw.pPDPae->a[iShw].u & X86_PDE_PAE_PG_MASK,
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /* Note: hardcoded PAE implementation dependency */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync (pPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD) ? PGMPOOL_IDX_PAE_PD : pPage->idx,
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync (pPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD) ? iShw + (pPage->idx - PGMPOOL_IDX_PAE_PD_0) * X86_PG_PAE_ENTRIES : iShw);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uShw.pPDPae->a[iShw].u = 0;
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync }
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync }
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync#endif
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync /* paranoia / a bit assumptive. */
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync if ( pCpu
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync && (off & 7)
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync && (off & 7) + cbWrite > sizeof(X86PDEPAE))
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync {
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDEPAE);
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync AssertReturnVoid(iShw2 < RT_ELEMENTS(uShw.pPDPae->a));
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync if ( iShw2 != iShw
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync && uShw.pPDPae->a[iShw2].u & PGM_PDFLAGS_MAPPING)
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync {
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTX_SUFF(pVM)->pgm.s));
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync STAM_COUNTER_INC(&(pPool->CTX_SUFF(pVM)->pgm.s.StatRZGuestCR3WriteConflict));
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync }
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync#ifdef PGMPOOL_INVALIDATE_UPPER_SHADOW_TABLE_ENTRIES
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync else if (uShw.pPDPae->a[iShw2].n.u1Present)
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync {
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync pgmPoolFree(pPool->CTX_SUFF(pVM),
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync uShw.pPDPae->a[iShw2].u & X86_PDE_PAE_PG_MASK,
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync /* Note: hardcoded PAE implementation dependency */
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync (pPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD) ? PGMPOOL_IDX_PAE_PD : pPage->idx,
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync (pPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD) ? iShw2 + (pPage->idx - PGMPOOL_IDX_PAE_PD_0) * X86_PG_PAE_ENTRIES : iShw2);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uShw.pPDPae->a[iShw2].u = 0;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#endif
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync }
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PDPT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# else
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_ROOT_PDPT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# endif
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /*
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Hopefully this doesn't happen very often:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * - touching unused parts of the page
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * - messing with the bits of pd pointers without changing the physical address
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync const unsigned iShw = off / sizeof(X86PDPE);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (iShw < X86_PG_PAE_PDPE_ENTRIES) /* don't use RT_ELEMENTS(uShw.pPDPT->a), because that's for long mode only */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (uShw.pPDPT->a[iShw].u & PGM_PLXFLAGS_MAPPING)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTX_SUFF(pVM)->pgm.s));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_COUNTER_INC(&(pPool->CTX_SUFF(pVM)->pgm.s.StatRZGuestCR3WriteConflict));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /* paranoia / a bit assumptive. */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync else if ( pCpu
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync && (off & 7)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync && (off & 7) + cbWrite > sizeof(X86PDPE))
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDPE);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if ( iShw2 != iShw
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync && iShw2 < X86_PG_PAE_PDPE_ENTRIES
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync && uShw.pPDPT->a[iShw2].u & PGM_PLXFLAGS_MAPPING)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTX_SUFF(pVM)->pgm.s));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_COUNTER_INC(&(pPool->CTX_SUFF(pVM)->pgm.s.StatRZGuestCR3WriteConflict));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync#ifndef IN_RC
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(pPage->enmKind == PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync const unsigned iShw = off / sizeof(X86PDEPAE);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (uShw.pPDPae->a[iShw].u & PGM_PDFLAGS_MAPPING)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTX_SUFF(pVM)->pgm.s));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync else
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (uShw.pPDPae->a[iShw].n.u1Present)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pgmPoolFree(pPool->CTX_SUFF(pVM),
fb41ad77bcfbdb3aaa1fc9503a37ee6a70dc6461vboxsync uShw.pPDPae->a[iShw].u & X86_PDE_PAE_PG_MASK,
fb41ad77bcfbdb3aaa1fc9503a37ee6a70dc6461vboxsync pPage->idx,
fb41ad77bcfbdb3aaa1fc9503a37ee6a70dc6461vboxsync iShw);
fb41ad77bcfbdb3aaa1fc9503a37ee6a70dc6461vboxsync uShw.pPDPae->a[iShw].u = 0;
fb41ad77bcfbdb3aaa1fc9503a37ee6a70dc6461vboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync /* paranoia / a bit assumptive. */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if ( pCpu
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync && (off & 7)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync && (off & 7) + cbWrite > sizeof(X86PDEPAE))
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDEPAE);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync AssertReturnVoid(iShw2 < RT_ELEMENTS(uShw.pPDPae->a));
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if ( iShw2 != iShw
fb41ad77bcfbdb3aaa1fc9503a37ee6a70dc6461vboxsync && uShw.pPDPae->a[iShw2].u & PGM_PDFLAGS_MAPPING)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTX_SUFF(pVM)->pgm.s));
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync else
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (uShw.pPDPae->a[iShw2].n.u1Present)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pgmPoolFree(pPool->CTX_SUFF(pVM),
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync uShw.pPDPae->a[iShw2].u & X86_PDE_PAE_PG_MASK,
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync pPage->idx,
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync iShw2);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync uShw.pPDPae->a[iShw2].u = 0;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync break;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync /*
c193ae401647c574d0ef52af57c32cf5d7c44966vboxsync * Hopefully this doesn't happen very often:
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync * - messing with the bits of pd pointers without changing the physical address
c193ae401647c574d0ef52af57c32cf5d7c44966vboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if (!VM_FF_ISSET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3))
c193ae401647c574d0ef52af57c32cf5d7c44966vboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync const unsigned iShw = off / sizeof(X86PDPE);
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync if (uShw.pPDPT->a[iShw].n.u1Present)
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync {
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPT->a[iShw].u));
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync pgmPoolFree(pPool->CTX_SUFF(pVM), uShw.pPDPT->a[iShw].u & X86_PDPE_PG_MASK, pPage->idx, iShw);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync uShw.pPDPT->a[iShw].u = 0;
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* paranoia / a bit assumptive. */
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync if ( pCpu
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync && (off & 7)
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync && (off & 7) + cbWrite > sizeof(X86PDPE))
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync {
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDPE);
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync if (uShw.pPDPT->a[iShw2].n.u1Present)
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPT->a[iShw2].u));
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync pgmPoolFree(pPool->CTX_SUFF(pVM), uShw.pPDPT->a[iShw2].u & X86_PDPE_PG_MASK, pPage->idx, iShw2);
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync uShw.pPDPT->a[iShw2].u = 0;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync }
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync }
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync break;
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync }
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync case PGMPOOLKIND_64BIT_PML4:
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync {
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync /*
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync * Hopefully this doesn't happen very often:
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync * - messing with the bits of pd pointers without changing the physical address
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync */
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync if (!VM_FF_ISSET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3))
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync {
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync const unsigned iShw = off / sizeof(X86PDPE);
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync if (uShw.pPML4->a[iShw].n.u1Present)
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPML4->a[iShw].u));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pgmPoolFree(pPool->CTX_SUFF(pVM), uShw.pPML4->a[iShw].u & X86_PML4E_PG_MASK, pPage->idx, iShw);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync uShw.pPML4->a[iShw].u = 0;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync }
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /* paranoia / a bit assumptive. */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync if ( pCpu
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync && (off & 7)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync && (off & 7) + cbWrite > sizeof(X86PDPE))
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PML4E);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync if (uShw.pPML4->a[iShw2].n.u1Present)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPML4->a[iShw2].u));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pgmPoolFree(pPool->CTX_SUFF(pVM), uShw.pPML4->a[iShw2].u & X86_PML4E_PG_MASK, pPage->idx, iShw2);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync uShw.pPML4->a[iShw2].u = 0;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync }
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync }
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync }
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync break;
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync }
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync#endif /* IN_RING0 */
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync default:
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync AssertFatalMsgFailed(("enmKind=%d\n", pPage->enmKind));
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync }
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync
4517f3d60a86111c8b70238b29d59f26c15291a7vboxsync /* next */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync if (pPage->iMonitoredNext == NIL_PGMPOOL_IDX)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync return;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pPage = &pPool->aPages[pPage->iMonitoredNext];
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync }
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync}
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync# ifndef IN_RING3
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync/**
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Checks if a access could be a fork operation in progress.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync *
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Meaning, that the guest is setuping up the parent process for Copy-On-Write.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync *
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @returns true if it's likly that we're forking, otherwise false.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param pPool The pool.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param pCpu The disassembled instruction.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param offFault The access offset.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsyncDECLINLINE(bool) pgmPoolMonitorIsForking(PPGMPOOL pPool, PDISCPUSTATE pCpu, unsigned offFault)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync{
dea3e7faa80f4aab41e08945b9308fd2e3ffe7fcvboxsync /*
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * i386 linux is using btr to clear X86_PTE_RW.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * The functions involved are (2.6.16 source inspection):
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * clear_bit
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * ptep_set_wrprotect
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * copy_one_pte
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * copy_pte_range
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * copy_pmd_range
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * copy_pud_range
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * copy_page_range
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * dup_mmap
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * dup_mm
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * copy_mm
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * copy_process
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * do_fork
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync if ( pCpu->pCurInstr->opcode == OP_BTR
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync && !(offFault & 4)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /** @todo Validate that the bit index is X86_PTE_RW. */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync )
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,Fork));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync return true;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync }
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync return false;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync}
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync/**
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Determine whether the page is likely to have been reused.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync *
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @returns true if we consider the page as being reused for a different purpose.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @returns false if we consider it to still be a paging page.
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync * @param pVM VM Handle.
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync * @param pPage The page in question.
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync * @param pRegFrame Trap register frame.
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync * @param pCpu The disassembly info for the faulting instruction.
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync * @param pvFault The fault address.
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync *
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync * @remark The REP prefix check is left to the caller because of STOSD/W.
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync */
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsyncDECLINLINE(bool) pgmPoolMonitorIsReused(PVM pVM, PPGMPOOLPAGE pPage, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu, RTGCPTR pvFault)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync{
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync#ifndef IN_RC
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync /** @todo could make this general, faulting close to rsp should be safe reuse heuristic. */
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync if ( HWACCMHasPendingIrq(pVM)
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync && (pRegFrame->rsp - pvFault) < 32)
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync {
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync /* Fault caused by stack writes while trying to inject an interrupt event. */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync Log(("pgmPoolMonitorIsReused: reused %RGv for interrupt stack (rsp=%RGv).\n", pvFault, pRegFrame->rsp));
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync return true;
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync }
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync#else
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync NOREF(pVM); NOREF(pvFault);
56484ba959c372f0196716100568e02412b0dbd5vboxsync#endif
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync
4517f3d60a86111c8b70238b29d59f26c15291a7vboxsync switch (pCpu->pCurInstr->opcode)
f044158ee9eb7045a43f2c4ef2fbc07cb11329aevboxsync {
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync /* call implies the actual push of the return address faulted */
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync case OP_CALL:
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync Log4(("pgmPoolMonitorIsReused: CALL\n"));
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync return true;
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync case OP_PUSH:
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync Log4(("pgmPoolMonitorIsReused: PUSH\n"));
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync return true;
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync case OP_PUSHF:
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync Log4(("pgmPoolMonitorIsReused: PUSHF\n"));
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync return true;
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync case OP_PUSHA:
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync Log4(("pgmPoolMonitorIsReused: PUSHA\n"));
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync return true;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync case OP_FXSAVE:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync Log4(("pgmPoolMonitorIsReused: FXSAVE\n"));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync return true;
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync case OP_MOVNTI: /* solaris - block_zero_no_xmm */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync Log4(("pgmPoolMonitorIsReused: MOVNTI\n"));
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync return true;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case OP_MOVNTDQ: /* solaris - hwblkclr & hwblkpagecopy */
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync Log4(("pgmPoolMonitorIsReused: MOVNTDQ\n"));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync return true;
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync case OP_MOVSWD:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case OP_STOSWD:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync if ( pCpu->prefix == (PREFIX_REP|PREFIX_REX)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync && pRegFrame->rcx >= 0x40
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync )
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync Assert(pCpu->mode == CPUMODE_64BIT);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync Log(("pgmPoolMonitorIsReused: OP_STOSQ\n"));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync return true;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync }
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync return false;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync }
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync if ( (pCpu->param1.flags & USE_REG_GEN32)
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync && (pCpu->param1.base.reg_gen == USE_REG_ESP))
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync Log4(("pgmPoolMonitorIsReused: ESP\n"));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync return true;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync //if (pPage->fCR3Mix)
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync // return false;
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync return false;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync}
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync/**
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync * Flushes the page being accessed.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync *
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @returns VBox status code suitable for scheduling.
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync * @param pVM The VM handle.
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync * @param pPool The pool.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pPage The pool page (head).
81dfa298981df637a707b142ebd03cb7d3385097vboxsync * @param pCpu The disassembly of the write instruction.
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync * @param pRegFrame The trap register frame.
81dfa298981df637a707b142ebd03cb7d3385097vboxsync * @param GCPhysFault The fault address as guest physical address.
81dfa298981df637a707b142ebd03cb7d3385097vboxsync * @param pvFault The fault address.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsyncstatic int pgmPoolAccessHandlerFlush(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync{
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync /*
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * First, do the flushing.
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync int rc = pgmPoolMonitorChainFlush(pPool, pPage);
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /*
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Emulate the instruction (xp/w2k problem, requires pc/cr2/sp detection).
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync uint32_t cbWritten;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync int rc2 = EMInterpretInstructionCPU(pVM, pCpu, pRegFrame, pvFault, &cbWritten);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync if (RT_SUCCESS(rc2))
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pRegFrame->rip += pCpu->opsize;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync else if (rc2 == VERR_EM_INTERPRETER)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync#ifdef IN_RC
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync if (PATMIsPatchGCAddr(pVM, (RTRCPTR)pRegFrame->eip))
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync {
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for patch code %04x:%RGv, ignoring.\n",
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync pRegFrame->cs, (RTGCPTR)pRegFrame->eip));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync rc = VINF_SUCCESS;
81dfa298981df637a707b142ebd03cb7d3385097vboxsync STAM_COUNTER_INC(&pPool->StatMonitorRZIntrFailPatch2);
81dfa298981df637a707b142ebd03cb7d3385097vboxsync }
81dfa298981df637a707b142ebd03cb7d3385097vboxsync else
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#endif
dea3e7faa80f4aab41e08945b9308fd2e3ffe7fcvboxsync {
dea3e7faa80f4aab41e08945b9308fd2e3ffe7fcvboxsync rc = VINF_EM_RAW_EMULATE_INSTR;
dea3e7faa80f4aab41e08945b9308fd2e3ffe7fcvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,EmulateInstr));
81dfa298981df637a707b142ebd03cb7d3385097vboxsync }
81dfa298981df637a707b142ebd03cb7d3385097vboxsync }
81dfa298981df637a707b142ebd03cb7d3385097vboxsync else
81dfa298981df637a707b142ebd03cb7d3385097vboxsync rc = rc2;
81dfa298981df637a707b142ebd03cb7d3385097vboxsync
81dfa298981df637a707b142ebd03cb7d3385097vboxsync /* See use in pgmPoolAccessHandlerSimple(). */
81dfa298981df637a707b142ebd03cb7d3385097vboxsync PGM_INVL_GUEST_TLBS();
81dfa298981df637a707b142ebd03cb7d3385097vboxsync
81dfa298981df637a707b142ebd03cb7d3385097vboxsync LogFlow(("pgmPoolAccessHandlerPT: returns %Rrc (flushed)\n", rc));
81dfa298981df637a707b142ebd03cb7d3385097vboxsync return rc;
81dfa298981df637a707b142ebd03cb7d3385097vboxsync
81dfa298981df637a707b142ebd03cb7d3385097vboxsync}
81dfa298981df637a707b142ebd03cb7d3385097vboxsync
81dfa298981df637a707b142ebd03cb7d3385097vboxsync
81dfa298981df637a707b142ebd03cb7d3385097vboxsync/**
81dfa298981df637a707b142ebd03cb7d3385097vboxsync * Handles the STOSD write accesses.
81dfa298981df637a707b142ebd03cb7d3385097vboxsync *
81dfa298981df637a707b142ebd03cb7d3385097vboxsync * @returns VBox status code suitable for scheduling.
81dfa298981df637a707b142ebd03cb7d3385097vboxsync * @param pVM The VM handle.
81dfa298981df637a707b142ebd03cb7d3385097vboxsync * @param pPool The pool.
81dfa298981df637a707b142ebd03cb7d3385097vboxsync * @param pPage The pool page (head).
81dfa298981df637a707b142ebd03cb7d3385097vboxsync * @param pCpu The disassembly of the write instruction.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pRegFrame The trap register frame.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param GCPhysFault The fault address as guest physical address.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pvFault The fault address.
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync */
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsyncDECLINLINE(int) pgmPoolAccessHandlerSTOSD(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync{
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync Assert(pCpu->mode == CPUMODE_32BIT);
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /*
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync * Increment the modification counter and insert it into the list
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync * of modified pages the first time.
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync if (!pPage->cModifications++)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pgmPoolMonitorModifiedInsert(pPool, pPage);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /*
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Execute REP STOSD.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync *
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * This ASSUMES that we're not invoked by Trap0e on in a out-of-sync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * write situation, meaning that it's safe to write here.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync PVMCPU pVCpu = VMMGetCpu(pPool->CTX_SUFF(pVM));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#endif
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync RTGCUINTPTR pu32 = (RTGCUINTPTR)pvFault;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync while (pRegFrame->ecx)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pgmPoolMonitorChainChanging(pPool, pPage, GCPhysFault, (RTGCPTR)pu32, NULL);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync PGMDynMapPopAutoSubset(pVCpu, iPrevSubset);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#else
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pgmPoolMonitorChainChanging(pPool, pPage, GCPhysFault, (RTGCPTR)pu32, NULL);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#endif
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#ifdef IN_RC
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync *(uint32_t *)pu32 = pRegFrame->eax;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#else
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync PGMPhysSimpleWriteGCPhys(pVM, GCPhysFault, &pRegFrame->eax, 4);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#endif
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pu32 += 4;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync GCPhysFault += 4;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pRegFrame->edi += 4;
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync pRegFrame->ecx--;
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync }
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync pRegFrame->rip += pCpu->opsize;
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync /* See use in pgmPoolAccessHandlerSimple(). */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync PGM_INVL_GUEST_TLBS();
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync LogFlow(("pgmPoolAccessHandlerSTOSD: returns\n"));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync return VINF_SUCCESS;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync}
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync/**
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Handles the simple write accesses.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync *
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @returns VBox status code suitable for scheduling.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param pVM The VM handle.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param pPool The pool.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param pPage The pool page (head).
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param pCpu The disassembly of the write instruction.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param pRegFrame The trap register frame.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param GCPhysFault The fault address as guest physical address.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param pvFault The fault address.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsyncDECLINLINE(int) pgmPoolAccessHandlerSimple(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync{
56484ba959c372f0196716100568e02412b0dbd5vboxsync /*
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Increment the modification counter and insert it into the list
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * of modified pages the first time.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync if (!pPage->cModifications++)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pgmPoolMonitorModifiedInsert(pPool, pPage);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /*
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Clear all the pages. ASSUMES that pvFault is readable.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync PVMCPU pVCpu = VMMGetCpu(pPool->CTX_SUFF(pVM));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pgmPoolMonitorChainChanging(pPool, pPage, GCPhysFault, pvFault, pCpu);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync PGMDynMapPopAutoSubset(pVCpu, iPrevSubset);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#else
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pgmPoolMonitorChainChanging(pPool, pPage, GCPhysFault, pvFault, pCpu);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#endif
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync /*
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * Interpret the instruction.
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync */
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync uint32_t cb;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync int rc = EMInterpretInstructionCPU(pVM, pCpu, pRegFrame, pvFault, &cb);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync if (RT_SUCCESS(rc))
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pRegFrame->rip += pCpu->opsize;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync else if (rc == VERR_EM_INTERPRETER)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for %04x:%RGv - opcode=%d\n",
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pRegFrame->cs, (RTGCPTR)pRegFrame->rip, pCpu->pCurInstr->opcode));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync rc = VINF_EM_RAW_EMULATE_INSTR;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,EmulateInstr));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync }
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /*
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Quick hack, with logging enabled we're getting stale
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * code TLBs but no data TLB for EIP and crash in EMInterpretDisasOne.
56484ba959c372f0196716100568e02412b0dbd5vboxsync * Flushing here is BAD and expensive, I think EMInterpretDisasOne will
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * have to be fixed to support this. But that'll have to wait till next week.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync *
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * An alternative is to keep track of the changed PTEs together with the
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * GCPhys from the guest PT. This may proove expensive though.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync *
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * At the moment, it's VITAL that it's done AFTER the instruction interpreting
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * because we need the stale TLBs in some cases (XP boot). This MUST be fixed properly!
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync PGM_INVL_GUEST_TLBS();
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync LogFlow(("pgmPoolAccessHandlerSimple: returns %Rrc cb=%d\n", rc, cb));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync return rc;
f044158ee9eb7045a43f2c4ef2fbc07cb11329aevboxsync}
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync/**
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * \#PF Handler callback for PT write accesses.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync *
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @returns VBox status code (appropriate for GC return).
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param pVM VM Handle.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param uErrorCode CPU Error code.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pRegFrame Trap register frame.
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * NULL on DMA and other non CPU access.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pvFault The fault address (cr2).
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param GCPhysFault The GC physical address corresponding to pvFault.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pvUser User argument.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync */
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsyncDECLEXPORT(int) pgmPoolAccessHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser)
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync{
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync STAM_PROFILE_START(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), a);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync PPGMPOOLPAGE pPage = (PPGMPOOLPAGE)pvUser;
56484ba959c372f0196716100568e02412b0dbd5vboxsync LogFlow(("pgmPoolAccessHandler: pvFault=%RGv pPage=%p:{.idx=%d} GCPhysFault=%RGp\n", pvFault, pPage, pPage->idx, GCPhysFault));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /*
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * We should ALWAYS have the list head as user parameter. This
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * is because we use that page to record the changes.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync Assert(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /*
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Disassemble the faulting instruction.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync DISCPUSTATE Cpu;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync int rc = EMInterpretDisasOne(pVM, pRegFrame, &Cpu, NULL);
f044158ee9eb7045a43f2c4ef2fbc07cb11329aevboxsync AssertRCReturn(rc, rc);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /*
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Check if it's worth dealing with.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync bool fReused = false;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if ( ( pPage->cModifications < 48 /** @todo #define */ /** @todo need to check that it's not mapping EIP. */ /** @todo adjust this! */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync || pPage->fCR3Mix)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync && !(fReused = pgmPoolMonitorIsReused(pVM, pPage, pRegFrame, &Cpu, pvFault))
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync && !pgmPoolMonitorIsForking(pPool, &Cpu, GCPhysFault & PAGE_OFFSET_MASK))
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /*
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Simple instructions, no REP prefix.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync if (!(Cpu.prefix & (PREFIX_REP | PREFIX_REPNE)))
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync rc = pgmPoolAccessHandlerSimple(pVM, pPool, pPage, &Cpu, pRegFrame, GCPhysFault, pvFault);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,Handled), a);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync return rc;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync }
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /*
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Windows is frequently doing small memset() operations (netio test 4k+).
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * We have to deal with these or we'll kill the cache and performance.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync if ( Cpu.pCurInstr->opcode == OP_STOSWD
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync && CPUMGetGuestCPL(pVM, pRegFrame) == 0
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync && pRegFrame->ecx <= 0x20
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync && pRegFrame->ecx * 4 <= PAGE_SIZE - ((uintptr_t)pvFault & PAGE_OFFSET_MASK)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync && !((uintptr_t)pvFault & 3)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync && (pRegFrame->eax == 0 || pRegFrame->eax == 0x80) /* the two values observed. */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync && Cpu.mode == CPUMODE_32BIT
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync && Cpu.opmode == CPUMODE_32BIT
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync && Cpu.addrmode == CPUMODE_32BIT
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync && Cpu.prefix == PREFIX_REP
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync && !pRegFrame->eflags.Bits.u1DF
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync )
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync rc = pgmPoolAccessHandlerSTOSD(pVM, pPool, pPage, &Cpu, pRegFrame, GCPhysFault, pvFault);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,RepStosd), a);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync return rc;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync }
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /* REP prefix, don't bother. */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,RepPrefix));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync Log4(("pgmPoolAccessHandler: eax=%#x ecx=%#x edi=%#x esi=%#x rip=%RGv opcode=%d prefix=%#x\n",
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pRegFrame->eax, pRegFrame->ecx, pRegFrame->edi, pRegFrame->esi, (RTGCPTR)pRegFrame->rip, Cpu.pCurInstr->opcode, Cpu.prefix));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync }
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /*
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Not worth it, so flush it.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync *
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * If we considered it to be reused, don't to back to ring-3
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * to emulate failed instructions since we usually cannot
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * interpret then. This may be a bit risky, in which case
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * the reuse detection must be fixed.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync rc = pgmPoolAccessHandlerFlush(pVM, pPool, pPage, &Cpu, pRegFrame, GCPhysFault, pvFault);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (rc == VINF_EM_RAW_EMULATE_INSTR && fReused)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc = VINF_SUCCESS;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,FlushPage), a);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync return rc;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync}
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync# endif /* !IN_RING3 */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#endif /* PGMPOOL_WITH_MONITORING */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#ifdef PGMPOOL_WITH_CACHE
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync
56484ba959c372f0196716100568e02412b0dbd5vboxsync/**
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync * Inserts a page into the GCPhys hash table.
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync *
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync * @param pPool The pool.
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * @param pPage The page.
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync */
687794577e2e35c3cae67e692a7f2130d1262a82vboxsyncDECLINLINE(void) pgmPoolHashInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync{
4fc4c719ffba45eb4136c34ad713ef9b21f69e3bvboxsync Log3(("pgmPoolHashInsert: %RGp\n", pPage->GCPhys));
4fc4c719ffba45eb4136c34ad713ef9b21f69e3bvboxsync Assert(pPage->GCPhys != NIL_RTGCPHYS); Assert(pPage->iNext == NIL_PGMPOOL_IDX);
4fc4c719ffba45eb4136c34ad713ef9b21f69e3bvboxsync uint16_t iHash = PGMPOOL_HASH(pPage->GCPhys);
4fc4c719ffba45eb4136c34ad713ef9b21f69e3bvboxsync pPage->iNext = pPool->aiHash[iHash];
4fc4c719ffba45eb4136c34ad713ef9b21f69e3bvboxsync pPool->aiHash[iHash] = pPage->idx;
4fc4c719ffba45eb4136c34ad713ef9b21f69e3bvboxsync}
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync/**
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Removes a page from the GCPhys hash table.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync *
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pPool The pool.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pPage The page.
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync */
f044158ee9eb7045a43f2c4ef2fbc07cb11329aevboxsyncDECLINLINE(void) pgmPoolHashRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync{
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync Log3(("pgmPoolHashRemove: %RGp\n", pPage->GCPhys));
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync uint16_t iHash = PGMPOOL_HASH(pPage->GCPhys);
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync if (pPool->aiHash[iHash] == pPage->idx)
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync pPool->aiHash[iHash] = pPage->iNext;
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync else
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync {
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync uint16_t iPrev = pPool->aiHash[iHash];
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync for (;;)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync const int16_t i = pPool->aPages[iPrev].iNext;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (i == pPage->idx)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pPool->aPages[iPrev].iNext = pPage->iNext;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync break;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if (i == NIL_PGMPOOL_IDX)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync AssertReleaseMsgFailed(("GCPhys=%RGp idx=%#x\n", pPage->GCPhys, pPage->idx));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync break;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync iPrev = i;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage->iNext = NIL_PGMPOOL_IDX;
234b7b45e964034cabd288cd60a28a020855cad4vboxsync}
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync/**
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Frees up one cache page.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync *
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @returns VBox status code.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @retval VINF_SUCCESS on success.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @retval VERR_PGM_POOL_CLEARED if the deregistration of a physical handler will cause a light weight pool flush.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pPool The pool.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param iUser The user index.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsyncstatic int pgmPoolCacheFreeOne(PPGMPOOL pPool, uint16_t iUser)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync{
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#ifndef IN_RC
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync const PVM pVM = pPool->CTX_SUFF(pVM);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#endif
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync Assert(pPool->iAgeHead != pPool->iAgeTail); /* We shouldn't be here if there < 2 cached entries! */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync STAM_COUNTER_INC(&pPool->StatCacheFreeUpOne);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /*
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Select one page from the tail of the age list.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync uint16_t iToFree = pPool->iAgeTail;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (iToFree == iUser)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync iToFree = pPool->aPages[iToFree].iAgePrev;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync/* This is the alternative to the SyncCR3 pgmPoolCacheUsed calls.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (pPool->aPages[iToFree].iUserHead != NIL_PGMPOOL_USER_INDEX)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync uint16_t i = pPool->aPages[iToFree].iAgePrev;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync for (unsigned j = 0; j < 10 && i != NIL_PGMPOOL_USER_INDEX; j++, i = pPool->aPages[i].iAgePrev)
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync {
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync if (pPool->aPages[iToFree].iUserHead == NIL_PGMPOOL_USER_INDEX)
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync continue;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync iToFree = i;
4517f3d60a86111c8b70238b29d59f26c15291a7vboxsync break;
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync }
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync }
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync*/
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync Assert(iToFree != iUser);
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync AssertRelease(iToFree != NIL_PGMPOOL_IDX);
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync PPGMPOOLPAGE pPage = &pPool->aPages[iToFree];
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /*
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync * Reject any attempts at flushing the currently active shadow CR3 mapping
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync */
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync if (PGMGetHyperCR3(pPool->CTX_SUFF(pVM)) == pPage->Core.Key)
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync {
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync /* Refresh the cr3 mapping by putting it at the head of the age list. */
56484ba959c372f0196716100568e02412b0dbd5vboxsync pgmPoolCacheUsed(pPool, pPage);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync return pgmPoolCacheFreeOne(pPool, iUser);
15c6c4ce0082362b8b81e15c3605f2d3aca69a21vboxsync }
f044158ee9eb7045a43f2c4ef2fbc07cb11329aevboxsync
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync int rc = pgmPoolFlushPage(pPool, pPage);
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync if (rc == VINF_SUCCESS)
15c6c4ce0082362b8b81e15c3605f2d3aca69a21vboxsync PGM_INVL_GUEST_TLBS(); /* see PT handler. */
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync return rc;
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync}
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync
15c6c4ce0082362b8b81e15c3605f2d3aca69a21vboxsync
4517f3d60a86111c8b70238b29d59f26c15291a7vboxsync/**
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync * Checks if a kind mismatch is really a page being reused
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync * or if it's just normal remappings.
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync *
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync * @returns true if reused and the cached page (enmKind1) should be flushed
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync * @returns false if not reused.
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync * @param enmKind1 The kind of the cached page.
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync * @param enmKind2 The kind of the requested page.
4517f3d60a86111c8b70238b29d59f26c15291a7vboxsync */
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsyncstatic bool pgmPoolCacheReusedByKind(PGMPOOLKIND enmKind1, PGMPOOLKIND enmKind2)
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync{
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync switch (enmKind1)
004d74842597dacc4009803171296dfcf9398c69vboxsync {
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /*
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Never reuse them. There is no remapping in non-paging mode.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
004d74842597dacc4009803171296dfcf9398c69vboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
004d74842597dacc4009803171296dfcf9398c69vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
004d74842597dacc4009803171296dfcf9398c69vboxsync case PGMPOOLKIND_PAE_PD_PHYS:
004d74842597dacc4009803171296dfcf9398c69vboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
004d74842597dacc4009803171296dfcf9398c69vboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
004d74842597dacc4009803171296dfcf9398c69vboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
004d74842597dacc4009803171296dfcf9398c69vboxsync return false;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#else
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync return true;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#endif
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync /*
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * It's perfectly fine to reuse these, except for PAE and non-paging stuff.
dea3e7faa80f4aab41e08945b9308fd2e3ffe7fcvboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
004d74842597dacc4009803171296dfcf9398c69vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
004d74842597dacc4009803171296dfcf9398c69vboxsync case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync switch (enmKind2)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
5366e994777f9d9391cf809dc77610f57270d75dvboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
81096b0da0061583a511da27088643aa949a1ec9vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_64BIT_PML4:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync return true;
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync default:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync return false;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /*
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * It's perfectly fine to reuse these, except for PAE and non-paging stuff.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_64BIT_PML4:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync switch (enmKind2)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync return true;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync default:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync return false;
5366e994777f9d9391cf809dc77610f57270d75dvboxsync }
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync /*
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync * These cannot be flushed, and it's common to reuse the PDs as PTs.
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync */
5366e994777f9d9391cf809dc77610f57270d75dvboxsync#ifndef VBOX_WITH_PGMPOOL_PAGING_ONLY
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_ROOT_32BIT_PD:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_ROOT_PAE_PD:
5366e994777f9d9391cf809dc77610f57270d75dvboxsync case PGMPOOLKIND_ROOT_PDPT:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#endif
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_ROOT_NESTED:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync return false;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync default:
81096b0da0061583a511da27088643aa949a1ec9vboxsync AssertFatalMsgFailed(("enmKind1=%d\n", enmKind1));
81096b0da0061583a511da27088643aa949a1ec9vboxsync }
81096b0da0061583a511da27088643aa949a1ec9vboxsync}
81096b0da0061583a511da27088643aa949a1ec9vboxsync
81096b0da0061583a511da27088643aa949a1ec9vboxsync
81096b0da0061583a511da27088643aa949a1ec9vboxsync/**
81096b0da0061583a511da27088643aa949a1ec9vboxsync * Attempts to satisfy a pgmPoolAlloc request from the cache.
81096b0da0061583a511da27088643aa949a1ec9vboxsync *
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync * @returns VBox status code.
81096b0da0061583a511da27088643aa949a1ec9vboxsync * @retval VINF_PGM_CACHED_PAGE on success.
81096b0da0061583a511da27088643aa949a1ec9vboxsync * @retval VERR_FILE_NOT_FOUND if not found.
81096b0da0061583a511da27088643aa949a1ec9vboxsync * @param pPool The pool.
81096b0da0061583a511da27088643aa949a1ec9vboxsync * @param GCPhys The GC physical address of the page we're gonna shadow.
5366e994777f9d9391cf809dc77610f57270d75dvboxsync * @param enmKind The kind of mapping.
56484ba959c372f0196716100568e02412b0dbd5vboxsync * @param iUser The shadow page pool index of the user table.
81096b0da0061583a511da27088643aa949a1ec9vboxsync * @param iUserTable The index into the user table (shadowed).
15c6c4ce0082362b8b81e15c3605f2d3aca69a21vboxsync * @param ppPage Where to store the pointer to the page.
81096b0da0061583a511da27088643aa949a1ec9vboxsync */
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsyncstatic int pgmPoolCacheAlloc(PPGMPOOL pPool, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
81096b0da0061583a511da27088643aa949a1ec9vboxsync{
81096b0da0061583a511da27088643aa949a1ec9vboxsync#ifndef IN_RC
81096b0da0061583a511da27088643aa949a1ec9vboxsync const PVM pVM = pPool->CTX_SUFF(pVM);
81096b0da0061583a511da27088643aa949a1ec9vboxsync#endif
81096b0da0061583a511da27088643aa949a1ec9vboxsync /*
81096b0da0061583a511da27088643aa949a1ec9vboxsync * Look up the GCPhys in the hash.
81096b0da0061583a511da27088643aa949a1ec9vboxsync */
81096b0da0061583a511da27088643aa949a1ec9vboxsync unsigned i = pPool->aiHash[PGMPOOL_HASH(GCPhys)];
81096b0da0061583a511da27088643aa949a1ec9vboxsync Log3(("pgmPoolCacheAlloc: %RGp kind %d iUser=%d iUserTable=%x SLOT=%d\n", GCPhys, enmKind, iUser, iUserTable, i));
81096b0da0061583a511da27088643aa949a1ec9vboxsync if (i != NIL_PGMPOOL_IDX)
81096b0da0061583a511da27088643aa949a1ec9vboxsync {
f044158ee9eb7045a43f2c4ef2fbc07cb11329aevboxsync do
81096b0da0061583a511da27088643aa949a1ec9vboxsync {
81096b0da0061583a511da27088643aa949a1ec9vboxsync PPGMPOOLPAGE pPage = &pPool->aPages[i];
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync Log3(("pgmPoolCacheAlloc: slot %d found page %RGp\n", i, pPage->GCPhys));
81096b0da0061583a511da27088643aa949a1ec9vboxsync if (pPage->GCPhys == GCPhys)
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync {
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync if ((PGMPOOLKIND)pPage->enmKind == enmKind)
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync int rc = pgmPoolTrackAddUser(pPool, pPage, iUser, iUserTable);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (RT_SUCCESS(rc))
5366e994777f9d9391cf809dc77610f57270d75dvboxsync {
5366e994777f9d9391cf809dc77610f57270d75dvboxsync *ppPage = pPage;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync STAM_COUNTER_INC(&pPool->StatCacheHits);
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync return VINF_PGM_CACHED_PAGE;
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync }
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync return rc;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync /*
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * The kind is different. In some cases we should now flush the page
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * as it has been reused, but in most cases this is normal remapping
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync * of PDs as PT or big pages using the GCPhys field in a slightly
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync * different way than the other kinds.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if (pgmPoolCacheReusedByKind((PGMPOOLKIND)pPage->enmKind, enmKind))
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync {
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync STAM_COUNTER_INC(&pPool->StatCacheKindMismatches);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pgmPoolFlushPage(pPool, pPage); /* ASSUMES that VERR_PGM_POOL_CLEARED will be returned by pgmPoolTracInsert. */
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync PGM_INVL_GUEST_TLBS(); /* see PT handler. */
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync break;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync }
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync }
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync /* next */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync i = pPage->iNext;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync } while (i != NIL_PGMPOOL_IDX);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync Log3(("pgmPoolCacheAlloc: Missed GCPhys=%RGp enmKind=%d\n", GCPhys, enmKind));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync STAM_COUNTER_INC(&pPool->StatCacheMisses);
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync return VERR_FILE_NOT_FOUND;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync}
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync/**
3ff8aa7d3c74cfbe8da5f77b8ea6c748cc79213avboxsync * Inserts a page into the cache.
5366e994777f9d9391cf809dc77610f57270d75dvboxsync *
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync * @param pPool The pool.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pPage The cached page.
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync * @param fCanBeCached Set if the page is fit for caching from the caller's point of view.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsyncstatic void pgmPoolCacheInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, bool fCanBeCached)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync{
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /*
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Insert into the GCPhys hash if the page is fit for that.
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync Assert(!pPage->fCached);
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync if (fCanBeCached)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pPage->fCached = true;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pgmPoolHashInsert(pPool, pPage);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync Log3(("pgmPoolCacheInsert: Caching %p:{.Core=%RHp, .idx=%d, .enmKind=%d, GCPhys=%RGp}\n",
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pPage, pPage->Core.Key, pPage->idx, pPage->enmKind, pPage->GCPhys));
dea3e7faa80f4aab41e08945b9308fd2e3ffe7fcvboxsync STAM_COUNTER_INC(&pPool->StatCacheCacheable);
dea3e7faa80f4aab41e08945b9308fd2e3ffe7fcvboxsync }
dea3e7faa80f4aab41e08945b9308fd2e3ffe7fcvboxsync else
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync Log3(("pgmPoolCacheInsert: Not caching %p:{.Core=%RHp, .idx=%d, .enmKind=%d, GCPhys=%RGp}\n",
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pPage, pPage->Core.Key, pPage->idx, pPage->enmKind, pPage->GCPhys));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync STAM_COUNTER_INC(&pPool->StatCacheUncacheable);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync }
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /*
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync * Insert at the head of the age list.
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync */
aff627cebfd8951ebb2949640489d07264738328vboxsync pPage->iAgePrev = NIL_PGMPOOL_IDX;
e94bac0e97b56692161549c95d72512b1eaa78ccvboxsync pPage->iAgeNext = pPool->iAgeHead;
e94bac0e97b56692161549c95d72512b1eaa78ccvboxsync if (pPool->iAgeHead != NIL_PGMPOOL_IDX)
e94bac0e97b56692161549c95d72512b1eaa78ccvboxsync pPool->aPages[pPool->iAgeHead].iAgePrev = pPage->idx;
4517f3d60a86111c8b70238b29d59f26c15291a7vboxsync else
4517f3d60a86111c8b70238b29d59f26c15291a7vboxsync pPool->iAgeTail = pPage->idx;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pPool->iAgeHead = pPage->idx;
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync}
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync/**
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync * Flushes a cached page.
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync *
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync * @param pPool The pool.
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync * @param pPage The cached page.
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync */
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsyncstatic void pgmPoolCacheFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync{
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync Log3(("pgmPoolCacheFlushPage: %RGp\n", pPage->GCPhys));
40c1a23e86c79b24a917a43c186b2e54504d12c1vboxsync
40c1a23e86c79b24a917a43c186b2e54504d12c1vboxsync /*
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * Remove the page from the hash.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync if (pPage->fCached)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPage->fCached = false;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pgmPoolHashRemove(pPool, pPage);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync }
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync else
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync Assert(pPage->iNext == NIL_PGMPOOL_IDX);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /*
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * Remove it from the age list.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync if (pPage->iAgeNext != NIL_PGMPOOL_IDX)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPool->aPages[pPage->iAgeNext].iAgePrev = pPage->iAgePrev;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync else
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPool->iAgeTail = pPage->iAgePrev;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync if (pPage->iAgePrev != NIL_PGMPOOL_IDX)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPool->aPages[pPage->iAgePrev].iAgeNext = pPage->iAgeNext;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync else
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPool->iAgeHead = pPage->iAgeNext;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPage->iAgeNext = NIL_PGMPOOL_IDX;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPage->iAgePrev = NIL_PGMPOOL_IDX;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync}
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#endif /* PGMPOOL_WITH_CACHE */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#ifdef PGMPOOL_WITH_MONITORING
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync/**
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * Looks for pages sharing the monitor.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync *
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @returns Pointer to the head page.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @returns NULL if not found.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @param pPool The Pool
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @param pNewPage The page which is going to be monitored.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsyncstatic PPGMPOOLPAGE pgmPoolMonitorGetPageByGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pNewPage)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync{
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#ifdef PGMPOOL_WITH_CACHE
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /*
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * Look up the GCPhys in the hash.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync RTGCPHYS GCPhys = pNewPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync unsigned i = pPool->aiHash[PGMPOOL_HASH(GCPhys)];
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync if (i == NIL_PGMPOOL_IDX)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync return NULL;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync do
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync PPGMPOOLPAGE pPage = &pPool->aPages[i];
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync if ( pPage->GCPhys - GCPhys < PAGE_SIZE
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync && pPage != pNewPage)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync switch (pPage->enmKind)
40c1a23e86c79b24a917a43c186b2e54504d12c1vboxsync {
40c1a23e86c79b24a917a43c186b2e54504d12c1vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_64BIT_PML4:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_32BIT_PD:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PDPT:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#else
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_ROOT_32BIT_PD:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_ROOT_PAE_PD:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_ROOT_PDPT:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#endif
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync /* find the head */
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync while (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
4517f3d60a86111c8b70238b29d59f26c15291a7vboxsync {
4517f3d60a86111c8b70238b29d59f26c15291a7vboxsync Assert(pPage->iMonitoredPrev != pPage->idx);
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync pPage = &pPool->aPages[pPage->iMonitoredPrev];
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync }
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync return pPage;
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync }
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync /* ignore, no monitoring. */
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
4517f3d60a86111c8b70238b29d59f26c15291a7vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync case PGMPOOLKIND_ROOT_NESTED:
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync case PGMPOOLKIND_PAE_PD_PHYS:
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync#ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync#endif
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync break;
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync default:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync AssertFatalMsgFailed(("enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* next */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync i = pPage->iNext;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync } while (i != NIL_PGMPOOL_IDX);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#endif
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync return NULL;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync}
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
16793803f63339106d16a55bc5b55ba9070b44e0vboxsync/**
16793803f63339106d16a55bc5b55ba9070b44e0vboxsync * Enabled write monitoring of a guest page.
16793803f63339106d16a55bc5b55ba9070b44e0vboxsync *
16793803f63339106d16a55bc5b55ba9070b44e0vboxsync * @returns VBox status code.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @retval VINF_SUCCESS on success.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @retval VERR_PGM_POOL_CLEARED if the registration of the physical handler will cause a light weight pool flush.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param pPool The pool.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pPage The cached page.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsyncstatic int pgmPoolMonitorInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync{
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync LogFlow(("pgmPoolMonitorInsert %RGp\n", pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1)));
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /*
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * Filter out the relevant kinds.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync switch (pPage->enmKind)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_64BIT_PML4:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync#ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_32BIT_PD:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PDPT:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#else
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_ROOT_PDPT:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync#endif
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync break;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_ROOT_NESTED:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /* Nothing to monitor here. */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync return VINF_SUCCESS;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PD_PHYS:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync /* Nothing to monitor here. */
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync return VINF_SUCCESS;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#else
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_ROOT_32BIT_PD:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_ROOT_PAE_PD:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#endif
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#ifdef PGMPOOL_WITH_MIXED_PT_CR3
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync break;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#endif
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync default:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync /*
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * Install handler.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync */
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync int rc;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync PPGMPOOLPAGE pPageHead = pgmPoolMonitorGetPageByGCPhys(pPool, pPage);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (pPageHead)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync {
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync Assert(pPageHead != pPage); Assert(pPageHead->iMonitoredNext != pPage->idx);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync Assert(pPageHead->iMonitoredPrev != pPage->idx);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPage->iMonitoredPrev = pPageHead->idx;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pPage->iMonitoredNext = pPageHead->iMonitoredNext;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (pPageHead->iMonitoredNext != NIL_PGMPOOL_IDX)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pPool->aPages[pPageHead->iMonitoredNext].iMonitoredPrev = pPage->idx;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPageHead->iMonitoredNext = pPage->idx;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync rc = VINF_SUCCESS;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync else
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync Assert(pPage->iMonitoredNext == NIL_PGMPOOL_IDX); Assert(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync PVM pVM = pPool->CTX_SUFF(pVM);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync const RTGCPHYS GCPhysPage = pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc = PGMHandlerPhysicalRegisterEx(pVM, PGMPHYSHANDLERTYPE_PHYSICAL_WRITE,
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync GCPhysPage, GCPhysPage + (PAGE_SIZE - 1),
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pPage),
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pPage),
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pPool->pfnAccessHandlerRC, MMHyperCCToRC(pVM, pPage),
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync pPool->pszAccessHandler);
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync /** @todo we should probably deal with out-of-memory conditions here, but for now increasing
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync * the heap size should suffice. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync AssertFatalRC(rc);
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync if (pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync rc = VERR_PGM_POOL_CLEARED;
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync }
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync pPage->fMonitored = true;
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync return rc;
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync}
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync/**
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync * Disables write monitoring of a guest page.
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync *
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync * @returns VBox status code.
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync * @retval VINF_SUCCESS on success.
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync * @retval VERR_PGM_POOL_CLEARED if the deregistration of the physical handler will cause a light weight pool flush.
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync * @param pPool The pool.
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync * @param pPage The cached page.
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync */
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsyncstatic int pgmPoolMonitorFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync{
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync /*
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync * Filter out the relevant kinds.
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync */
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync switch (pPage->enmKind)
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync {
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync case PGMPOOLKIND_64BIT_PML4:
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync#ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_32BIT_PD:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_PAE_PDPT:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#else
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_ROOT_PDPT:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#endif
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync break;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_ROOT_NESTED:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_PAE_PD_PHYS:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /* Nothing to monitor here. */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync return VINF_SUCCESS;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#ifndef VBOX_WITH_PGMPOOL_PAGING_ONLY
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_ROOT_32BIT_PD:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_ROOT_PAE_PD:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#endif
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#ifdef PGMPOOL_WITH_MIXED_PT_CR3
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync break;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#endif
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync default:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync }
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /*
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * Remove the page from the monitored list or uninstall it if last.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync const PVM pVM = pPool->CTX_SUFF(pVM);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync int rc;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync if ( pPage->iMonitoredNext != NIL_PGMPOOL_IDX
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync || pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync if (pPage->iMonitoredPrev == NIL_PGMPOOL_IDX)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync PPGMPOOLPAGE pNewHead = &pPool->aPages[pPage->iMonitoredNext];
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pNewHead->iMonitoredPrev = NIL_PGMPOOL_IDX;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pNewHead->fCR3Mix = pPage->fCR3Mix;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync rc = PGMHandlerPhysicalChangeCallbacks(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1),
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pNewHead),
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pNewHead),
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPool->pfnAccessHandlerRC, MMHyperCCToRC(pVM, pNewHead),
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPool->pszAccessHandler);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync AssertFatalRCSuccess(rc);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync }
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync else
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPool->aPages[pPage->iMonitoredPrev].iMonitoredNext = pPage->iMonitoredNext;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync if (pPage->iMonitoredNext != NIL_PGMPOOL_IDX)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPool->aPages[pPage->iMonitoredNext].iMonitoredPrev = pPage->iMonitoredPrev;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync }
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync rc = VINF_SUCCESS;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync }
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync }
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync else
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync rc = PGMHandlerPhysicalDeregister(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync AssertFatalRC(rc);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync if (pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync rc = VERR_PGM_POOL_CLEARED;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync }
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pPage->fMonitored = false;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /*
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Remove it from the list of modified pages (if in it).
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pgmPoolMonitorModifiedRemove(pPool, pPage);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync return rc;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync}
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync# ifdef PGMPOOL_WITH_MIXED_PT_CR3
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync/**
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Set or clear the fCR3Mix attribute in a chain of monitored pages.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync *
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param pPool The Pool.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param pPage A page in the chain.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param fCR3Mix The new fCR3Mix value.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsyncstatic void pgmPoolMonitorChainChangeCR3Mix(PPGMPOOL pPool, PPGMPOOLPAGE pPage, bool fCR3Mix)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync{
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /* current */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pPage->fCR3Mix = fCR3Mix;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /* before */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync int16_t idx = pPage->iMonitoredPrev;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync while (idx != NIL_PGMPOOL_IDX)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pPool->aPages[idx].fCR3Mix = fCR3Mix;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync idx = pPool->aPages[idx].iMonitoredPrev;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync }
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /* after */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync idx = pPage->iMonitoredNext;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync while (idx != NIL_PGMPOOL_IDX)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pPool->aPages[idx].fCR3Mix = fCR3Mix;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync idx = pPool->aPages[idx].iMonitoredNext;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync }
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync}
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync/**
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * Installs or modifies monitoring of a CR3 page (special).
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync *
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync * We're pretending the CR3 page is shadowed by the pool so we can use the
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync * generic mechanisms in detecting chained monitoring. (This also gives us a
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync * tast of what code changes are required to really pool CR3 shadow pages.)
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync *
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync * @returns VBox status code.
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync * @param pPool The pool.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param idxRoot The CR3 (root) page index.
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync * @param GCPhysCR3 The (new) CR3 value.
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync */
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsyncint pgmPoolMonitorMonitorCR3(PPGMPOOL pPool, uint16_t idxRoot, RTGCPHYS GCPhysCR3)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync{
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync Assert(idxRoot != NIL_PGMPOOL_IDX && idxRoot < PGMPOOL_IDX_FIRST);
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync PPGMPOOLPAGE pPage = &pPool->aPages[idxRoot];
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync LogFlow(("pgmPoolMonitorMonitorCR3: idxRoot=%d pPage=%p:{.GCPhys=%RGp, .fMonitored=%d} GCPhysCR3=%RGp\n",
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync idxRoot, pPage, pPage->GCPhys, pPage->fMonitored, GCPhysCR3));
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync /*
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync * The unlikely case where it already matches.
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync */
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync if (pPage->GCPhys == GCPhysCR3)
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync {
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync Assert(pPage->fMonitored);
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync return VINF_SUCCESS;
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync }
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync /*
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync * Flush the current monitoring and remove it from the hash.
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync */
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync int rc = VINF_SUCCESS;
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync if (pPage->fMonitored)
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync {
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync pgmPoolMonitorChainChangeCR3Mix(pPool, pPage, false);
6b9d50a0f466bd5a61458ed53925480ab28a3c17vboxsync rc = pgmPoolMonitorFlush(pPool, pPage);
f044158ee9eb7045a43f2c4ef2fbc07cb11329aevboxsync if (rc == VERR_PGM_POOL_CLEARED)
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync rc = VINF_SUCCESS;
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync else
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync AssertFatalRC(rc);
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync pgmPoolHashRemove(pPool, pPage);
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync }
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync /*
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync * Monitor the page at the new location and insert it into the hash.
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync */
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync pPage->GCPhys = GCPhysCR3;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync int rc2 = pgmPoolMonitorInsert(pPool, pPage);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if (rc2 != VERR_PGM_POOL_CLEARED)
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync {
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync AssertFatalRC(rc2);
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync if (rc2 != VINF_SUCCESS && rc == VINF_SUCCESS)
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync rc = rc2;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync }
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync pgmPoolHashInsert(pPool, pPage);
5366e994777f9d9391cf809dc77610f57270d75dvboxsync pgmPoolMonitorChainChangeCR3Mix(pPool, pPage, true);
5366e994777f9d9391cf809dc77610f57270d75dvboxsync return rc;
5366e994777f9d9391cf809dc77610f57270d75dvboxsync}
5366e994777f9d9391cf809dc77610f57270d75dvboxsync
5366e994777f9d9391cf809dc77610f57270d75dvboxsync
5366e994777f9d9391cf809dc77610f57270d75dvboxsync/**
5366e994777f9d9391cf809dc77610f57270d75dvboxsync * Removes the monitoring of a CR3 page (special).
5366e994777f9d9391cf809dc77610f57270d75dvboxsync *
5366e994777f9d9391cf809dc77610f57270d75dvboxsync * @returns VBox status code.
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync * @param pPool The pool.
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync * @param idxRoot The CR3 (root) page index.
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync */
5366e994777f9d9391cf809dc77610f57270d75dvboxsyncint pgmPoolMonitorUnmonitorCR3(PPGMPOOL pPool, uint16_t idxRoot)
5366e994777f9d9391cf809dc77610f57270d75dvboxsync{
5366e994777f9d9391cf809dc77610f57270d75dvboxsync Assert(idxRoot != NIL_PGMPOOL_IDX && idxRoot < PGMPOOL_IDX_FIRST);
5366e994777f9d9391cf809dc77610f57270d75dvboxsync PPGMPOOLPAGE pPage = &pPool->aPages[idxRoot];
5366e994777f9d9391cf809dc77610f57270d75dvboxsync LogFlow(("pgmPoolMonitorUnmonitorCR3: idxRoot=%d pPage=%p:{.GCPhys=%RGp, .fMonitored=%d}\n",
5366e994777f9d9391cf809dc77610f57270d75dvboxsync idxRoot, pPage, pPage->GCPhys, pPage->fMonitored));
5366e994777f9d9391cf809dc77610f57270d75dvboxsync
5366e994777f9d9391cf809dc77610f57270d75dvboxsync if (!pPage->fMonitored)
5366e994777f9d9391cf809dc77610f57270d75dvboxsync return VINF_SUCCESS;
5366e994777f9d9391cf809dc77610f57270d75dvboxsync
5366e994777f9d9391cf809dc77610f57270d75dvboxsync pgmPoolMonitorChainChangeCR3Mix(pPool, pPage, false);
5366e994777f9d9391cf809dc77610f57270d75dvboxsync int rc = pgmPoolMonitorFlush(pPool, pPage);
5366e994777f9d9391cf809dc77610f57270d75dvboxsync if (rc != VERR_PGM_POOL_CLEARED)
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync AssertFatalRC(rc);
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync else
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync rc = VINF_SUCCESS;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync pgmPoolHashRemove(pPool, pPage);
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync Assert(!pPage->fMonitored);
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync pPage->GCPhys = NIL_RTGCPHYS;
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync return rc;
7e8ef90d3160234df0f254131b87af4243d79476vboxsync}
7e8ef90d3160234df0f254131b87af4243d79476vboxsync
7e8ef90d3160234df0f254131b87af4243d79476vboxsync# endif /* PGMPOOL_WITH_MIXED_PT_CR3 */
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync/**
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync * Inserts the page into the list of modified pages.
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync *
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync * @param pPool The pool.
e2489bd9ef063ae006feaebc3318ffa4143f6e16vboxsync * @param pPage The page.
e2489bd9ef063ae006feaebc3318ffa4143f6e16vboxsync */
e2489bd9ef063ae006feaebc3318ffa4143f6e16vboxsyncvoid pgmPoolMonitorModifiedInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
e2489bd9ef063ae006feaebc3318ffa4143f6e16vboxsync{
e2489bd9ef063ae006feaebc3318ffa4143f6e16vboxsync Log3(("pgmPoolMonitorModifiedInsert: idx=%d\n", pPage->idx));
e2489bd9ef063ae006feaebc3318ffa4143f6e16vboxsync AssertMsg( pPage->iModifiedNext == NIL_PGMPOOL_IDX
e2489bd9ef063ae006feaebc3318ffa4143f6e16vboxsync && pPage->iModifiedPrev == NIL_PGMPOOL_IDX
e2489bd9ef063ae006feaebc3318ffa4143f6e16vboxsync && pPool->iModifiedHead != pPage->idx,
e2489bd9ef063ae006feaebc3318ffa4143f6e16vboxsync ("Next=%d Prev=%d idx=%d cModifications=%d Head=%d cModifiedPages=%d\n",
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync pPage->iModifiedNext, pPage->iModifiedPrev, pPage->idx, pPage->cModifications,
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync pPool->iModifiedHead, pPool->cModifiedPages));
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync pPage->iModifiedNext = pPool->iModifiedHead;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync if (pPool->iModifiedHead != NIL_PGMPOOL_IDX)
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync pPool->aPages[pPool->iModifiedHead].iModifiedPrev = pPage->idx;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync pPool->iModifiedHead = pPage->idx;
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync pPool->cModifiedPages++;
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync#ifdef VBOX_WITH_STATISTICS
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync if (pPool->cModifiedPages > pPool->cModifiedPagesHigh)
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync pPool->cModifiedPagesHigh = pPool->cModifiedPages;
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync#endif
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync}
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync
6b9d50a0f466bd5a61458ed53925480ab28a3c17vboxsync
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync/**
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync * Removes the page from the list of modified pages and resets the
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync * moficiation counter.
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync *
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync * @param pPool The pool.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param pPage The page which is believed to be in the list of modified pages.
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync */
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsyncstatic void pgmPoolMonitorModifiedRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync{
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync Log3(("pgmPoolMonitorModifiedRemove: idx=%d cModifications=%d\n", pPage->idx, pPage->cModifications));
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync if (pPool->iModifiedHead == pPage->idx)
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync {
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX);
6b9d50a0f466bd5a61458ed53925480ab28a3c17vboxsync pPool->iModifiedHead = pPage->iModifiedNext;
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync if (pPage->iModifiedNext != NIL_PGMPOOL_IDX)
6b9d50a0f466bd5a61458ed53925480ab28a3c17vboxsync {
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync pPool->aPages[pPage->iModifiedNext].iModifiedPrev = NIL_PGMPOOL_IDX;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPage->iModifiedNext = NIL_PGMPOOL_IDX;
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync }
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync pPool->cModifiedPages--;
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync }
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync else if (pPage->iModifiedPrev != NIL_PGMPOOL_IDX)
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync {
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync pPool->aPages[pPage->iModifiedPrev].iModifiedNext = pPage->iModifiedNext;
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync if (pPage->iModifiedNext != NIL_PGMPOOL_IDX)
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync {
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync pPool->aPages[pPage->iModifiedNext].iModifiedPrev = pPage->iModifiedPrev;
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync pPage->iModifiedNext = NIL_PGMPOOL_IDX;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync pPool->cModifiedPages--;
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync }
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync else
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX);
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync pPage->cModifications = 0;
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync}
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync/**
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync * Zaps the list of modified pages, resetting their modification counters in the process.
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync *
56484ba959c372f0196716100568e02412b0dbd5vboxsync * @param pVM The VM handle.
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsyncvoid pgmPoolMonitorModifiedClearAll(PVM pVM)
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync{
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync LogFlow(("pgmPoolMonitorModifiedClearAll: cModifiedPages=%d\n", pPool->cModifiedPages));
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync unsigned cPages = 0; NOREF(cPages);
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync uint16_t idx = pPool->iModifiedHead;
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync pPool->iModifiedHead = NIL_PGMPOOL_IDX;
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync while (idx != NIL_PGMPOOL_IDX)
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync {
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync PPGMPOOLPAGE pPage = &pPool->aPages[idx];
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync idx = pPage->iModifiedNext;
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync pPage->iModifiedNext = NIL_PGMPOOL_IDX;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
ccbdc11833996cb9f3be7868f1ebaefcacafb94dvboxsync pPage->cModifications = 0;
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync Assert(++cPages);
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync }
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync AssertMsg(cPages == pPool->cModifiedPages, ("%d != %d\n", cPages, pPool->cModifiedPages));
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync pPool->cModifiedPages = 0;
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync}
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync#ifdef IN_RING3
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync/**
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * Clear all shadow pages and clear all modification counters.
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync *
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * @param pVM The VM handle.
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * @remark Should only be used when monitoring is available, thus placed in
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * the PGMPOOL_WITH_MONITORING #ifdef.
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync */
687794577e2e35c3cae67e692a7f2130d1262a82vboxsyncvoid pgmPoolClearAll(PVM pVM)
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync{
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync STAM_PROFILE_START(&pPool->StatClearAll, c);
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync LogFlow(("pgmPoolClearAll: cUsedPages=%d\n", pPool->cUsedPages));
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync /*
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * Iterate all the pages until we've encountered all that in use.
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * This is simple but not quite optimal solution.
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync */
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync unsigned cModifiedPages = 0; NOREF(cModifiedPages);
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync unsigned cLeft = pPool->cUsedPages;
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync unsigned iPage = pPool->cCurPages;
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync while (--iPage >= PGMPOOL_IDX_FIRST)
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync {
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync if (pPage->GCPhys != NIL_RTGCPHYS)
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync {
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync switch (pPage->enmKind)
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync {
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync /*
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * We only care about shadow page tables.
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync */
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#ifdef PGMPOOL_WITH_USER_TRACKING
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (pPage->cPresent)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#endif
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync void *pvShw = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync STAM_PROFILE_START(&pPool->StatZeroPage, z);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync ASMMemZeroPage(pvShw);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#ifdef PGMPOOL_WITH_USER_TRACKING
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pPage->cPresent = 0;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pPage->iFirstPresent = ~0;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#endif
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* fall thru */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync default:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync Assert(!pPage->cModifications || ++cModifiedPages);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync Assert(pPage->iModifiedNext == NIL_PGMPOOL_IDX || pPage->cModifications);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX || pPage->cModifications);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPage->iModifiedNext = NIL_PGMPOOL_IDX;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPage->cModifications = 0;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync break;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync }
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync if (!--cLeft)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync break;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync /* swipe the special pages too. */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync for (iPage = PGMPOOL_IDX_FIRST_SPECIAL; iPage < PGMPOOL_IDX_FIRST; iPage++)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if (pPage->GCPhys != NIL_RTGCPHYS)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync Assert(!pPage->cModifications || ++cModifiedPages);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync Assert(pPage->iModifiedNext == NIL_PGMPOOL_IDX || pPage->cModifications);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX || pPage->cModifications);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage->iModifiedNext = NIL_PGMPOOL_IDX;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage->cModifications = 0;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#ifndef DEBUG_michael
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync AssertMsg(cModifiedPages == pPool->cModifiedPages, ("%d != %d\n", cModifiedPages, pPool->cModifiedPages));
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#endif
9e7e0b61d29309a0ed7af9472c8d6d865f9e8a2dvboxsync pPool->iModifiedHead = NIL_PGMPOOL_IDX;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->cModifiedPages = 0;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
9e7e0b61d29309a0ed7af9472c8d6d865f9e8a2dvboxsync#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
9e7e0b61d29309a0ed7af9472c8d6d865f9e8a2dvboxsync /*
9e7e0b61d29309a0ed7af9472c8d6d865f9e8a2dvboxsync * Clear all the GCPhys links and rebuild the phys ext free list.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync for (PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pRam;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pRam = pRam->CTX_SUFF(pNext))
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync unsigned iPage = pRam->cb >> PAGE_SHIFT;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync while (iPage-- > 0)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pRam->aPages[iPage].HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->iPhysExtFreeHead = 0;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync const unsigned cMaxPhysExts = pPool->cMaxPhysExts;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync for (unsigned i = 0; i < cMaxPhysExts; i++)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync paPhysExts[i].iNext = i + 1;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync paPhysExts[i].aidx[0] = NIL_PGMPOOL_IDX;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync paPhysExts[i].aidx[1] = NIL_PGMPOOL_IDX;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync paPhysExts[i].aidx[2] = NIL_PGMPOOL_IDX;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync paPhysExts[cMaxPhysExts - 1].iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#endif
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync pPool->cPresent = 0;
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync STAM_PROFILE_STOP(&pPool->StatClearAll, c);
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync}
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync#endif /* IN_RING3 */
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync/**
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync * Handle SyncCR3 pool tasks
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync *
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync * @returns VBox status code.
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync * @retval VINF_SUCCESS if successfully added.
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync * @retval VINF_PGM_SYNC_CR3 is it needs to be deferred to ring 3 (GC only)
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync * @param pVM The VM handle.
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync * @remark Should only be used when monitoring is available, thus placed in
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync * the PGMPOOL_WITH_MONITORING #ifdef.
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync */
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsyncint pgmPoolSyncCR3(PVM pVM)
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync{
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync /*
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync * When monitoring shadowed pages, we reset the modification counters on CR3 sync.
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync * Occasionally we will have to clear all the shadow page tables because we wanted
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync * to monitor a page which was mapped by too many shadowed page tables. This operation
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync * sometimes refered to as a 'lightweight flush'.
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync */
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync if (!(pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL))
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync pgmPoolMonitorModifiedClearAll(pVM);
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync else
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync {
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync# ifdef IN_RING3 /* Don't flush in ring-0 or raw mode, it's taking too long. */
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync pVM->pgm.s.fSyncFlags &= ~PGM_SYNC_CLEAR_PGM_POOL;
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync pgmPoolClearAll(pVM);
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync# else /* !IN_RING3 */
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync LogFlow(("SyncCR3: PGM_SYNC_CLEAR_PGM_POOL is set -> VINF_PGM_SYNC_CR3\n"));
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3); /** @todo no need to do global sync, right? */
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync return VINF_PGM_SYNC_CR3;
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync# endif /* !IN_RING3 */
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync }
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync return VINF_SUCCESS;
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync}
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync#endif /* PGMPOOL_WITH_MONITORING */
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync#ifdef PGMPOOL_WITH_USER_TRACKING
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync/**
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * Frees up at least one user entry.
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync *
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync * @returns VBox status code.
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync * @retval VINF_SUCCESS if successfully added.
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync * @param pPool The pool.
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync * @param iUser The user index.
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync */
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsyncstatic int pgmPoolTrackFreeOneUser(PPGMPOOL pPool, uint16_t iUser)
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync{
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync STAM_COUNTER_INC(&pPool->StatTrackFreeUpOneUser);
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync#ifdef PGMPOOL_WITH_CACHE
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync /*
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync * Just free cached pages in a braindead fashion.
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync */
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync /** @todo walk the age list backwards and free the first with usage. */
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync int rc = VINF_SUCCESS;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync do
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync {
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync int rc2 = pgmPoolCacheFreeOne(pPool, iUser);
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync if (RT_FAILURE(rc2) && rc == VINF_SUCCESS)
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync rc = rc2;
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync } while (pPool->iUserFreeHead == NIL_PGMPOOL_USER_INDEX);
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync return rc;
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync#else
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync /*
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync * Lazy approach.
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync */
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync /* @todo incompatible with long mode paging (cr3 root will be flushed) */
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync Assert(!CPUMIsGuestInLongMode(pVM));
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync pgmPoolFlushAllInt(pPool);
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync return VERR_PGM_POOL_FLUSHED;
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync#endif
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync}
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync/**
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * Inserts a page into the cache.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync *
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * This will create user node for the page, insert it into the GCPhys
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * hash, and insert it into the age list.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync *
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @returns VBox status code.
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync * @retval VINF_SUCCESS if successfully added.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @retval VERR_PGM_POOL_CLEARED if the deregistration of the physical handler will cause a light weight pool flush.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @param pPool The pool.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @param pPage The cached page.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @param GCPhys The GC physical address of the page we're gonna shadow.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @param iUser The user index.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @param iUserTable The user table index.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync */
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsyncDECLINLINE(int) pgmPoolTrackInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhys, uint16_t iUser, uint32_t iUserTable)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync{
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync int rc = VINF_SUCCESS;
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync PPGMPOOLUSER pUser = pPool->CTX_SUFF(paUsers);
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync LogFlow(("pgmPoolTrackInsert iUser %d iUserTable %d\n", iUser, iUserTable));
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync /*
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync * Find free a user node.
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync */
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync uint16_t i = pPool->iUserFreeHead;
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync if (i == NIL_PGMPOOL_USER_INDEX)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync {
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync int rc = pgmPoolTrackFreeOneUser(pPool, iUser);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync if (RT_FAILURE(rc))
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync return rc;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync i = pPool->iUserFreeHead;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync }
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync /*
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * Unlink the user node from the free list,
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * initialize and insert it into the user list.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPool->iUserFreeHead = pUser[i].iNext;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pUser[i].iNext = NIL_PGMPOOL_USER_INDEX;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pUser[i].iUser = iUser;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pUser[i].iUserTable = iUserTable;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPage->iUserHead = i;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync /*
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * Insert into cache and enable monitoring of the guest page if enabled.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync *
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * Until we implement caching of all levels, including the CR3 one, we'll
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * have to make sure we don't try monitor & cache any recursive reuse of
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * a monitored CR3 page. Because all windows versions are doing this we'll
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * have to be able to do combined access monitoring, CR3 + PT and
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * PD + PT (guest PAE).
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync *
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * Update:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * We're now cooperating with the CR3 monitor if an uncachable page is found.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync */
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync#if defined(PGMPOOL_WITH_MONITORING) || defined(PGMPOOL_WITH_CACHE)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync# ifdef PGMPOOL_WITH_MIXED_PT_CR3
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync const bool fCanBeMonitored = true;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync# else
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync bool fCanBeMonitored = pPool->CTX_SUFF(pVM)->pgm.s.GCPhysGstCR3Monitored == NIL_RTGCPHYS
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync || (GCPhys & X86_PTE_PAE_PG_MASK) != (pPool->CTX_SUFF(pVM)->pgm.s.GCPhysGstCR3Monitored & X86_PTE_PAE_PG_MASK)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync || pgmPoolIsBigPage((PGMPOOLKIND)pPage->enmKind);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync# endif
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync# ifdef PGMPOOL_WITH_CACHE
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pgmPoolCacheInsert(pPool, pPage, fCanBeMonitored); /* This can be expanded. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync# endif
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync if (fCanBeMonitored)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync {
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync# ifdef PGMPOOL_WITH_MONITORING
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync rc = pgmPoolMonitorInsert(pPool, pPage);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync if (rc == VERR_PGM_POOL_CLEARED)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync {
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync /* 'Failed' - free the usage, and keep it in the cache (if enabled). */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync# ifndef PGMPOOL_WITH_CACHE
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pgmPoolMonitorFlush(pPool, pPage);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync rc = VERR_PGM_POOL_FLUSHED;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync# endif
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pUser[i].iNext = pPool->iUserFreeHead;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pUser[i].iUser = NIL_PGMPOOL_IDX;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPool->iUserFreeHead = i;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync }
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync }
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync# endif
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync#endif /* PGMPOOL_WITH_MONITORING */
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync return rc;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync}
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync# ifdef PGMPOOL_WITH_CACHE /* (only used when the cache is enabled.) */
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync/**
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * Adds a user reference to a page.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync *
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * This will
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * This will move the page to the head of the
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync *
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @returns VBox status code.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @retval VINF_SUCCESS if successfully added.
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @param pPool The pool.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @param pPage The cached page.
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync * @param iUser The user index.
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync * @param iUserTable The user table.
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsyncstatic int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync{
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync LogFlow(("pgmPoolTrackAddUser iUser %d iUserTable %d\n", iUser, iUserTable));
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync# ifdef VBOX_STRICT
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync /*
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync * Check that the entry doesn't already exists.
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync */
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync if (pPage->iUserHead != NIL_PGMPOOL_USER_INDEX)
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync {
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync uint16_t i = pPage->iUserHead;
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync do
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync {
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync Assert(i < pPool->cMaxUsers);
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync AssertMsg(paUsers[i].iUser != iUser || paUsers[i].iUserTable != iUserTable, ("%x %x vs new %x %x\n", paUsers[i].iUser, paUsers[i].iUserTable, iUser, iUserTable));
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync i = paUsers[i].iNext;
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync } while (i != NIL_PGMPOOL_USER_INDEX);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync# endif
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync /*
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * Allocate a user node.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync uint16_t i = pPool->iUserFreeHead;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if (i == NIL_PGMPOOL_USER_INDEX)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync int rc = pgmPoolTrackFreeOneUser(pPool, iUser);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if (RT_FAILURE(rc))
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync return rc;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync i = pPool->iUserFreeHead;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->iUserFreeHead = paUsers[i].iNext;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync /*
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * Initialize the user node and insert it.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync paUsers[i].iNext = pPage->iUserHead;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync paUsers[i].iUser = iUser;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync paUsers[i].iUserTable = iUserTable;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage->iUserHead = i;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync# ifdef PGMPOOL_WITH_CACHE
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync /*
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * Tell the cache to update its replacement stats for this page.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pgmPoolCacheUsed(pPool, pPage);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync# endif
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync return VINF_SUCCESS;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync}
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync# endif /* PGMPOOL_WITH_CACHE */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync/**
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * Frees a user record associated with a page.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync *
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * This does not clear the entry in the user table, it simply replaces the
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * user record to the chain of free records.
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync *
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync * @param pPool The pool.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param HCPhys The HC physical address of the shadow page.
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync * @param iUser The shadow page pool index of the user table.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param iUserTable The index into the user table (shadowed).
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync */
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsyncstatic void pgmPoolTrackFreeUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync{
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync /*
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync * Unlink and free the specified user entry.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync */
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync /* Special: For PAE and 32-bit paging, there is usually no more than one user. */
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync uint16_t i = pPage->iUserHead;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync if ( i != NIL_PGMPOOL_USER_INDEX
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync && paUsers[i].iUser == iUser
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync && paUsers[i].iUserTable == iUserTable)
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync {
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync pPage->iUserHead = paUsers[i].iNext;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9e7e0b61d29309a0ed7af9472c8d6d865f9e8a2dvboxsync paUsers[i].iUser = NIL_PGMPOOL_IDX;
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync paUsers[i].iNext = pPool->iUserFreeHead;
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync pPool->iUserFreeHead = i;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync return;
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync }
9e7e0b61d29309a0ed7af9472c8d6d865f9e8a2dvboxsync
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync /* General: Linear search. */
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync uint16_t iPrev = NIL_PGMPOOL_USER_INDEX;
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync while (i != NIL_PGMPOOL_USER_INDEX)
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync {
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync if ( paUsers[i].iUser == iUser
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync && paUsers[i].iUserTable == iUserTable)
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync {
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync if (iPrev != NIL_PGMPOOL_USER_INDEX)
ecf100db90e8e3af96312908282d3c20e754fbe8vboxsync paUsers[iPrev].iNext = paUsers[i].iNext;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync else
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPage->iUserHead = paUsers[i].iNext;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync paUsers[i].iUser = NIL_PGMPOOL_IDX;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync paUsers[i].iNext = pPool->iUserFreeHead;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPool->iUserFreeHead = i;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync return;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync iPrev = i;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync i = paUsers[i].iNext;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /* Fatal: didn't find it */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync AssertFatalMsgFailed(("Didn't find the user entry! iUser=%#x iUserTable=%#x GCPhys=%RGp\n",
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync iUser, iUserTable, pPage->GCPhys));
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync}
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync/**
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync * Gets the entry size of a shadow table.
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync *
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param enmKind The kind of page.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync *
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @returns The size of the entry in bytes. That is, 4 or 8.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @returns If the kind is not for a table, an assertion is raised and 0 is
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * returned.
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsyncDECLINLINE(unsigned) pgmPoolTrackGetShadowEntrySize(PGMPOOLKIND enmKind)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync{
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync switch (enmKind)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync#ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync case PGMPOOLKIND_32BIT_PD:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#else
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync case PGMPOOLKIND_ROOT_32BIT_PD:
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync#endif
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync return 4;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_64BIT_PML4:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#ifndef VBOX_WITH_PGMPOOL_PAGING_ONLY
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_ROOT_PAE_PD:
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync case PGMPOOLKIND_ROOT_PDPT:
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync#endif
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync case PGMPOOLKIND_PAE_PDPT:
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync case PGMPOOLKIND_ROOT_NESTED:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync case PGMPOOLKIND_PAE_PD_PHYS:
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync return 8;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync default:
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync AssertFatalMsgFailed(("enmKind=%d\n", enmKind));
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync }
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync}
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync/**
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync * Gets the entry size of a guest table.
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync *
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync * @param enmKind The kind of page.
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync *
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync * @returns The size of the entry in bytes. That is, 0, 4 or 8.
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync * @returns If the kind is not for a table, an assertion is raised and 0 is
c99b597540585068d22dde4c9f74730305f24097vboxsync * returned.
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync */
c99b597540585068d22dde4c9f74730305f24097vboxsyncDECLINLINE(unsigned) pgmPoolTrackGetGuestEntrySize(PGMPOOLKIND enmKind)
c99b597540585068d22dde4c9f74730305f24097vboxsync{
c99b597540585068d22dde4c9f74730305f24097vboxsync switch (enmKind)
c99b597540585068d22dde4c9f74730305f24097vboxsync {
c99b597540585068d22dde4c9f74730305f24097vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
c99b597540585068d22dde4c9f74730305f24097vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
c99b597540585068d22dde4c9f74730305f24097vboxsync#ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
c99b597540585068d22dde4c9f74730305f24097vboxsync case PGMPOOLKIND_32BIT_PD:
c99b597540585068d22dde4c9f74730305f24097vboxsync#else
c99b597540585068d22dde4c9f74730305f24097vboxsync case PGMPOOLKIND_ROOT_32BIT_PD:
c99b597540585068d22dde4c9f74730305f24097vboxsync#endif
c99b597540585068d22dde4c9f74730305f24097vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
c99b597540585068d22dde4c9f74730305f24097vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
c99b597540585068d22dde4c9f74730305f24097vboxsync return 4;
c99b597540585068d22dde4c9f74730305f24097vboxsync
c99b597540585068d22dde4c9f74730305f24097vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
c99b597540585068d22dde4c9f74730305f24097vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
c99b597540585068d22dde4c9f74730305f24097vboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_64BIT_PML4:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync case PGMPOOLKIND_PAE_PDPT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#else
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_ROOT_PAE_PD:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_ROOT_PDPT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#endif
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync return 8;
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync case PGMPOOLKIND_ROOT_NESTED:
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync case PGMPOOLKIND_PAE_PD_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /** @todo can we return 0? (nobody is calling this...) */
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync AssertFailed();
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync return 0;
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync default:
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync AssertFatalMsgFailed(("enmKind=%d\n", enmKind));
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync }
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync}
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync/**
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync * Scans one shadow page table for mappings of a physical page.
c99b597540585068d22dde4c9f74730305f24097vboxsync *
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync * @param pVM The VM handle.
c99b597540585068d22dde4c9f74730305f24097vboxsync * @param pPhysPage The guest page in question.
c99b597540585068d22dde4c9f74730305f24097vboxsync * @param iShw The shadow page table.
c99b597540585068d22dde4c9f74730305f24097vboxsync * @param cRefs The number of references made in that PT.
02651f98b4320e70a300ba1ebe95270096ebfd4dvboxsync */
c99b597540585068d22dde4c9f74730305f24097vboxsyncstatic void pgmPoolTrackFlushGCPhysPTInt(PVM pVM, PCPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs)
c99b597540585068d22dde4c9f74730305f24097vboxsync{
c99b597540585068d22dde4c9f74730305f24097vboxsync LogFlow(("pgmPoolTrackFlushGCPhysPT: HCPhys=%RHp iShw=%d cRefs=%d\n", pPhysPage->HCPhys, iShw, cRefs));
c99b597540585068d22dde4c9f74730305f24097vboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
c99b597540585068d22dde4c9f74730305f24097vboxsync
c99b597540585068d22dde4c9f74730305f24097vboxsync /*
c99b597540585068d22dde4c9f74730305f24097vboxsync * Assert sanity.
c99b597540585068d22dde4c9f74730305f24097vboxsync */
c99b597540585068d22dde4c9f74730305f24097vboxsync Assert(cRefs == 1);
c99b597540585068d22dde4c9f74730305f24097vboxsync AssertFatalMsg(iShw < pPool->cCurPages && iShw != NIL_PGMPOOL_IDX, ("iShw=%d\n", iShw));
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync PPGMPOOLPAGE pPage = &pPool->aPages[iShw];
c99b597540585068d22dde4c9f74730305f24097vboxsync
c99b597540585068d22dde4c9f74730305f24097vboxsync /*
c99b597540585068d22dde4c9f74730305f24097vboxsync * Then, clear the actual mappings to the page in the shadow PT.
c99b597540585068d22dde4c9f74730305f24097vboxsync */
c99b597540585068d22dde4c9f74730305f24097vboxsync switch (pPage->enmKind)
c99b597540585068d22dde4c9f74730305f24097vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync {
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync const uint32_t u32 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PX86PT pPT = (PX86PT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX32 cRefs=%#x\n", i, pPT->a[i], cRefs));
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync pPT->a[i].u = 0;
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync cRefs--;
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync if (!cRefs)
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync return;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#ifdef LOG_ENABLED
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPT->a); i++)
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync {
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync RTLogPrintf("i=%d cRefs=%d\n", i, cRefs--);
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync pPT->a[i].u = 0;
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync }
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync#endif
5366e994777f9d9391cf809dc77610f57270d75dvboxsync AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
5366e994777f9d9391cf809dc77610f57270d75dvboxsync break;
5366e994777f9d9391cf809dc77610f57270d75dvboxsync }
5366e994777f9d9391cf809dc77610f57270d75dvboxsync
5366e994777f9d9391cf809dc77610f57270d75dvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync PX86PTPAE pPT = (PX86PTPAE)PGMPOOL_PAGE_2_PTR(pVM, pPage);
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync {
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX64 cRefs=%#x\n", i, pPT->a[i], cRefs));
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync pPT->a[i].u = 0;
e86baafe99d1f1eb37adcca5fdecfd06e7f13bc5vboxsync cRefs--;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if (!cRefs)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync return;
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync }
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync#ifdef LOG_ENABLED
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPT->a); i++)
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync {
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync RTLogPrintf("i=%d cRefs=%d\n", i, cRefs--);
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync pPT->a[i].u = 0;
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync }
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync#endif
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d u64=%RX64\n", cRefs, pPage->iFirstPresent, pPage->cPresent, u64));
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync break;
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync }
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PEPTPT pPT = (PEPTPT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
3641904508e6a0671c21a083a2ed03ebad8d976cvboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if ((pPT->a[i].u & (EPT_PTE_PG_MASK | X86_PTE_P)) == u64)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX64 cRefs=%#x\n", i, pPT->a[i], cRefs));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pPT->a[i].u = 0;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync cRefs--;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync if (!cRefs)
36f3c24e4ad9c6b813767db1faeabbe7e2ecc057vboxsync return;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync#ifdef LOG_ENABLED
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPT->a); i++)
36f3c24e4ad9c6b813767db1faeabbe7e2ecc057vboxsync if ((pPT->a[i].u & (EPT_PTE_PG_MASK | X86_PTE_P)) == u64)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync RTLogPrintf("i=%d cRefs=%d\n", i, cRefs--);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pPT->a[i].u = 0;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
36f3c24e4ad9c6b813767db1faeabbe7e2ecc057vboxsync#endif
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync break;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync default:
36f3c24e4ad9c6b813767db1faeabbe7e2ecc057vboxsync AssertFatalMsgFailed(("enmKind=%d iShw=%d\n", pPage->enmKind, iShw));
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync }
36f3c24e4ad9c6b813767db1faeabbe7e2ecc057vboxsync}
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
36f3c24e4ad9c6b813767db1faeabbe7e2ecc057vboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync/**
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * Scans one shadow page table for mappings of a physical page.
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync *
dea3e7faa80f4aab41e08945b9308fd2e3ffe7fcvboxsync * @param pVM The VM handle.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pPhysPage The guest page in question.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param iShw The shadow page table.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param cRefs The number of references made in that PT.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsyncvoid pgmPoolTrackFlushGCPhysPT(PVM pVM, PPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync{
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool); NOREF(pPool);
36f3c24e4ad9c6b813767db1faeabbe7e2ecc057vboxsync LogFlow(("pgmPoolTrackFlushGCPhysPT: HCPhys=%RHp iShw=%d cRefs=%d\n", pPhysPage->HCPhys, iShw, cRefs));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPT, f);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pgmPoolTrackFlushGCPhysPTInt(pVM, pPhysPage, iShw, cRefs);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pPhysPage->HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPT, f);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync}
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/**
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Flushes a list of shadow page tables mapping the same physical page.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * @param pVM The VM handle.
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * @param pPhysPage The guest page in question.
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync * @param iPhysExt The physical cross reference extent list to flush.
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync */
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsyncvoid pgmPoolTrackFlushGCPhysPTs(PVM pVM, PPGMPAGE pPhysPage, uint16_t iPhysExt)
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync{
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPTs, f);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("pgmPoolTrackFlushGCPhysPTs: HCPhys=%RHp iPhysExt\n", pPhysPage->HCPhys, iPhysExt));
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync const uint16_t iPhysExtStart = iPhysExt;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync PPGMPOOLPHYSEXT pPhysExt;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync do
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync {
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync Assert(iPhysExt < pPool->cMaxPhysExts);
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync if (pPhysExt->aidx[i] != NIL_PGMPOOL_IDX)
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync {
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync pgmPoolTrackFlushGCPhysPTInt(pVM, pPhysPage, pPhysExt->aidx[i], 1);
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync }
dea3e7faa80f4aab41e08945b9308fd2e3ffe7fcvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync /* next */
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync iPhysExt = pPhysExt->iNext;
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync } while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync /* insert the list into the free list and clear the ram range entry. */
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync pPhysExt->iNext = pPool->iPhysExtFreeHead;
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync pPool->iPhysExtFreeHead = iPhysExtStart;
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync pPhysPage->HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTs, f);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync}
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/**
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Scans all shadow page tables for mappings of a physical page.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * This may be slow, but it's most likely more efficient than cleaning
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * out the entire page pool / cache.
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync *
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync * @returns VBox status code.
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync * @retval VINF_SUCCESS if all references has been successfully cleared.
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync * @retval VINF_PGM_GCPHYS_ALIASED if we're better off with a CR3 sync and
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * a page pool cleaning.
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync *
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync * @param pVM The VM handle.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pPhysPage The guest page in question.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsyncint pgmPoolTrackFlushGCPhysPTsSlow(PVM pVM, PPGMPAGE pPhysPage)
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync{
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPTsSlow, s);
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: cUsedPages=%d cPresent=%d HCPhys=%RHp\n",
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync pPool->cUsedPages, pPool->cPresent, pPhysPage->HCPhys));
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync#if 1
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync /*
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync * There is a limit to what makes sense.
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync */
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync if (pPool->cPresent > 1024)
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync {
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: giving up... (cPresent=%d)\n", pPool->cPresent));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTsSlow, s);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync return VINF_PGM_GCPHYS_ALIASED;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync }
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#endif
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /*
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Iterate all the pages until we've encountered all that in use.
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync * This is simple but not quite optimal solution.
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync const uint32_t u32 = u64;
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync unsigned cLeft = pPool->cUsedPages;
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync unsigned iPage = pPool->cCurPages;
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync while (--iPage >= PGMPOOL_IDX_FIRST)
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync {
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync if (pPage->GCPhys != NIL_RTGCPHYS)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync switch (pPage->enmKind)
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync {
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync /*
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * We only care about shadow page tables.
18e3d4704ed7b57260b0d4e3ea12c224b3f81840vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync unsigned cPresent = pPage->cPresent;
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync PX86PT pPT = (PX86PT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync if (pPT->a[i].n.u1Present)
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync {
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync //Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX32\n", iPage, i, pPT->a[i]));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pPT->a[i].u = 0;
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (!--cPresent)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync }
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync unsigned cPresent = pPage->cPresent;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync PX86PTPAE pPT = (PX86PTPAE)PGMPOOL_PAGE_2_PTR(pVM, pPage);
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if (pPT->a[i].n.u1Present)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync {
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync //Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX64\n", iPage, i, pPT->a[i]));
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync pPT->a[i].u = 0;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync }
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync if (!--cPresent)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync break;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync }
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync break;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync if (!--cLeft)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync break;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync }
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync pPhysPage->HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTsSlow, s);
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync return VINF_SUCCESS;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync}
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync
dea3e7faa80f4aab41e08945b9308fd2e3ffe7fcvboxsync/**
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * Clears the user entry in a user table.
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync *
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync * This is used to remove all references to a page when flushing it.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsyncstatic void pgmPoolTrackClearPageUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PCPGMPOOLUSER pUser)
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync{
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync Assert(pUser->iUser != NIL_PGMPOOL_IDX);
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync Assert(pUser->iUser < pPool->cCurPages);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync uint32_t iUserTable = pUser->iUserTable;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync /*
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync * Map the user page.
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync */
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync PPGMPOOLPAGE pUserPage = &pPool->aPages[pUser->iUser];
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (pUserPage->enmKind == PGMPOOLKIND_ROOT_PAE_PD)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /* Must translate the fake 2048 entry PD to a 512 PD one since the R0 mapping is not linear. */
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync Assert(pUser->iUser == PGMPOOL_IDX_PAE_PD);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uint32_t iPdpt = iUserTable / X86_PG_PAE_ENTRIES;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync iUserTable %= X86_PG_PAE_ENTRIES;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pUserPage = &pPool->aPages[PGMPOOL_IDX_PAE_PD_0 + iPdpt];
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(pUserPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync }
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync#endif
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync union
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync uint64_t *pau64;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uint32_t *pau32;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync } u;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync u.pau64 = (uint64_t *)PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pUserPage);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /* Safety precaution in case we change the paging for other modes too in the future. */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync Assert(PGMGetHyperCR3(pPool->CTX_SUFF(pVM)) != pPage->Core.Key);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#ifdef VBOX_STRICT
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /*
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Some sanity checks.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync switch (pUserPage->enmKind)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync# ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_32BIT_PD:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync Assert(iUserTable < X86_PG_ENTRIES);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync break;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync# else
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_ROOT_32BIT_PD:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync Assert(iUserTable < X86_PG_ENTRIES);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync Assert(!(u.pau32[iUserTable] & PGM_PDFLAGS_MAPPING));
18e3d4704ed7b57260b0d4e3ea12c224b3f81840vboxsync break;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync# endif
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync# if !defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0) && !defined(VBOX_WITH_PGMPOOL_PAGING_ONLY)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_ROOT_PAE_PD:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync Assert(iUserTable < 2048 && pUser->iUser == PGMPOOL_IDX_PAE_PD);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync AssertMsg(!(u.pau64[iUserTable] & PGM_PDFLAGS_MAPPING), ("%llx %d\n", u.pau64[iUserTable], iUserTable));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync break;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync# endif
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync# ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_PAE_PDPT:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# else
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_ROOT_PDPT:
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync# endif
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(iUserTable < 4);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_PAE_PD_PHYS:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync Assert(iUserTable < X86_PG_PAE_ENTRIES);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync break;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(iUserTable < X86_PG_PAE_ENTRIES);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(!(u.pau64[iUserTable] & PGM_PDFLAGS_MAPPING));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync Assert(iUserTable < X86_PG_PAE_ENTRIES);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync break;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_64BIT_PML4:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /* GCPhys >> PAGE_SHIFT is the index here */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync break;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync Assert(iUserTable < X86_PG_PAE_ENTRIES);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync break;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync Assert(iUserTable < X86_PG_PAE_ENTRIES);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync break;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_ROOT_NESTED:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync Assert(iUserTable < X86_PG_PAE_ENTRIES);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync break;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync default:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync AssertMsgFailed(("enmKind=%d\n", pUserPage->enmKind));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync break;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync }
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#endif /* VBOX_STRICT */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /*
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Clear the entry in the user page.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync switch (pUserPage->enmKind)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /* 32-bit entries */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_32BIT_PD:
f044158ee9eb7045a43f2c4ef2fbc07cb11329aevboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#else
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_ROOT_32BIT_PD:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#endif
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync u.pau32[iUserTable] = 0;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync break;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /* 64-bit entries */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_PAE_PD_PHYS:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_64BIT_PML4:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# if !defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0) && !defined(VBOX_WITH_PGMPOOL_PAGING_ONLY)
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync case PGMPOOLKIND_ROOT_PAE_PD:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#endif
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PDPT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync#else
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_ROOT_PDPT:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync#endif
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_ROOT_NESTED:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync u.pau64[iUserTable] = 0;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync default:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync AssertFatalMsgFailed(("enmKind=%d iUser=%#x iUserTable=%#x\n", pUserPage->enmKind, pUser->iUser, pUser->iUserTable));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync }
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync}
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync/**
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Clears all users of a page.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsyncstatic void pgmPoolTrackClearPageUsers(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync{
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /*
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Free all the user records.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync uint16_t i = pPage->iUserHead;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync while (i != NIL_PGMPOOL_USER_INDEX)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /* Clear enter in user table. */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pgmPoolTrackClearPageUser(pPool, pPage, &paUsers[i]);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /* Free it. */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync const uint16_t iNext = paUsers[i].iNext;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync paUsers[i].iUser = NIL_PGMPOOL_IDX;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync paUsers[i].iNext = pPool->iUserFreeHead;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pPool->iUserFreeHead = i;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync /* Next. */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync i = iNext;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync }
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync}
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/**
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync * Allocates a new physical cross reference extent.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @returns Pointer to the allocated extent on success. NULL if we're out of them.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pVM The VM handle.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param piPhysExt Where to store the phys ext index.
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync */
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsyncPPGMPOOLPHYSEXT pgmPoolTrackPhysExtAlloc(PVM pVM, uint16_t *piPhysExt)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync{
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uint16_t iPhysExt = pPool->iPhysExtFreeHead;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (iPhysExt == NIL_PGMPOOL_PHYSEXT_INDEX)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_COUNTER_INC(&pPool->StamTrackPhysExtAllocFailures);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync return NULL;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PPGMPOOLPHYSEXT pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pPool->iPhysExtFreeHead = pPhysExt->iNext;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pPhysExt->iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *piPhysExt = iPhysExt;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync return pPhysExt;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync}
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/**
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync * Frees a physical cross reference extent.
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync *
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync * @param pVM The VM handle.
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync * @param iPhysExt The extent to free.
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync */
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsyncvoid pgmPoolTrackPhysExtFree(PVM pVM, uint16_t iPhysExt)
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync{
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync Assert(iPhysExt < pPool->cMaxPhysExts);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync PPGMPOOLPHYSEXT pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync pPhysExt->iNext = pPool->iPhysExtFreeHead;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync pPool->iPhysExtFreeHead = iPhysExt;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync}
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync/**
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * Frees a physical cross reference extent.
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync *
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * @param pVM The VM handle.
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * @param iPhysExt The extent to free.
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync */
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsyncvoid pgmPoolTrackPhysExtFreeList(PVM pVM, uint16_t iPhysExt)
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync{
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync const uint16_t iPhysExtStart = iPhysExt;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync PPGMPOOLPHYSEXT pPhysExt;
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync do
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(iPhysExt < pPool->cMaxPhysExts);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /* next */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync iPhysExt = pPhysExt->iNext;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync } while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync pPhysExt->iNext = pPool->iPhysExtFreeHead;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync pPool->iPhysExtFreeHead = iPhysExtStart;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync}
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/**
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Insert a reference into a list of physical cross reference extents.
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync *
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync * @returns The new ram range flags (top 16-bits).
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync *
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync * @param pVM The VM handle.
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * @param iPhysExt The physical extent index of the list head.
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * @param iShwPT The shadow page table index.
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsyncstatic uint16_t pgmPoolTrackPhysExtInsert(PVM pVM, uint16_t iPhysExt, uint16_t iShwPT)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync{
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /* special common case. */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (paPhysExts[iPhysExt].aidx[2] == NIL_PGMPOOL_IDX)
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync {
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync paPhysExts[iPhysExt].aidx[2] = iShwPT;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedMany);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("pgmPoolTrackPhysExtAddref: %d:{,,%d}\n", iPhysExt, iShwPT));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync return iPhysExt | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync }
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync /* general treatment. */
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync const uint16_t iPhysExtStart = iPhysExt;
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync unsigned cMax = 15;
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync for (;;)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(iPhysExt < pPool->cMaxPhysExts);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync for (unsigned i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
92e624e40b06b4dc6d0a8222e1de33bd3e879a63vboxsync if (paPhysExts[iPhysExt].aidx[i] == NIL_PGMPOOL_IDX)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync paPhysExts[iPhysExt].aidx[i] = iShwPT;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedMany);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("pgmPoolTrackPhysExtAddref: %d:{%d} i=%d cMax=%d\n", iPhysExt, iShwPT, i, cMax));
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync return iPhysExtStart | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (!--cMax)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatTrackOverflows);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pgmPoolTrackPhysExtFreeList(pVM, iPhysExtStart);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("pgmPoolTrackPhysExtAddref: overflow (1) iShwPT=%d\n", iShwPT));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync return MM_RAM_FLAGS_IDX_OVERFLOWED | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /* add another extent to the list. */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PPGMPOOLPHYSEXT pNew = pgmPoolTrackPhysExtAlloc(pVM, &iPhysExt);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (!pNew)
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync {
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatTrackOverflows);
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync pgmPoolTrackPhysExtFreeList(pVM, iPhysExtStart);
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync return MM_RAM_FLAGS_IDX_OVERFLOWED | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync }
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync pNew->iNext = iPhysExtStart;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pNew->aidx[0] = iShwPT;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("pgmPoolTrackPhysExtAddref: added new extent %d:{%d}->%d\n", iPhysExt, iShwPT, iPhysExtStart));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync return iPhysExt | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync}
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync/**
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * Add a reference to guest physical page where extents are in use.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync *
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync * @returns The new ram range flags (top 16-bits).
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync *
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pVM The VM handle.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param u16 The ram range flags (top 16-bits).
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param iShwPT The shadow page table index.
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync */
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsyncuint16_t pgmPoolTrackPhysExtAddref(PVM pVM, uint16_t u16, uint16_t iShwPT)
dea3e7faa80f4aab41e08945b9308fd2e3ffe7fcvboxsync{
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if ((u16 >> (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT)) != MM_RAM_FLAGS_CREFS_PHYSEXT)
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync /*
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * Convert to extent list.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync Assert((u16 >> (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT)) == 1);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync uint16_t iPhysExt;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync PPGMPOOLPHYSEXT pPhysExt = pgmPoolTrackPhysExtAlloc(pVM, &iPhysExt);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if (pPhysExt)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync {
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync LogFlow(("pgmPoolTrackPhysExtAddref: new extent: %d:{%d, %d}\n", iPhysExt, u16 & MM_RAM_FLAGS_IDX_MASK, iShwPT));
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliased);
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync pPhysExt->aidx[0] = u16 & MM_RAM_FLAGS_IDX_MASK;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pPhysExt->aidx[1] = iShwPT;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync u16 = iPhysExt | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
36f3c24e4ad9c6b813767db1faeabbe7e2ecc057vboxsync else
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync u16 = MM_RAM_FLAGS_IDX_OVERFLOWED | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync }
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync else if (u16 != (MM_RAM_FLAGS_IDX_OVERFLOWED | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT))))
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync {
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync /*
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync * Insert into the extent list.
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync */
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync u16 = pgmPoolTrackPhysExtInsert(pVM, u16 & MM_RAM_FLAGS_IDX_MASK, iShwPT);
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync }
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync else
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedLots);
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync return u16;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync}
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync/**
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * Clear references to guest physical memory.
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync *
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pPool The pool.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pPage The page.
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync * @param pPhysPage Pointer to the aPages entry in the ram range.
dea3e7faa80f4aab41e08945b9308fd2e3ffe7fcvboxsync */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsyncvoid pgmPoolTrackPhysExtDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PPGMPAGE pPhysPage)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync{
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync const unsigned cRefs = pPhysPage->HCPhys >> MM_RAM_FLAGS_CREFS_SHIFT; /** @todo PAGE FLAGS */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync AssertFatalMsg(cRefs == MM_RAM_FLAGS_CREFS_PHYSEXT, ("cRefs=%d HCPhys=%RHp pPage=%p:{.idx=%d}\n", cRefs, pPhysPage->HCPhys, pPage, pPage->idx));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync uint16_t iPhysExt = (pPhysPage->HCPhys >> MM_RAM_FLAGS_IDX_SHIFT) & MM_RAM_FLAGS_IDX_MASK;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if (iPhysExt != MM_RAM_FLAGS_IDX_OVERFLOWED)
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync uint16_t iPhysExtPrev = NIL_PGMPOOL_PHYSEXT_INDEX;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync do
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync {
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync Assert(iPhysExt < pPool->cMaxPhysExts);
81096b0da0061583a511da27088643aa949a1ec9vboxsync
81096b0da0061583a511da27088643aa949a1ec9vboxsync /*
81096b0da0061583a511da27088643aa949a1ec9vboxsync * Look for the shadow page and check if it's all freed.
81096b0da0061583a511da27088643aa949a1ec9vboxsync */
81096b0da0061583a511da27088643aa949a1ec9vboxsync for (unsigned i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
81096b0da0061583a511da27088643aa949a1ec9vboxsync {
81096b0da0061583a511da27088643aa949a1ec9vboxsync if (paPhysExts[iPhysExt].aidx[i] == pPage->idx)
81096b0da0061583a511da27088643aa949a1ec9vboxsync {
81096b0da0061583a511da27088643aa949a1ec9vboxsync paPhysExts[iPhysExt].aidx[i] = NIL_PGMPOOL_IDX;
81096b0da0061583a511da27088643aa949a1ec9vboxsync
81096b0da0061583a511da27088643aa949a1ec9vboxsync for (i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
81096b0da0061583a511da27088643aa949a1ec9vboxsync if (paPhysExts[iPhysExt].aidx[i] != NIL_PGMPOOL_IDX)
81096b0da0061583a511da27088643aa949a1ec9vboxsync {
81096b0da0061583a511da27088643aa949a1ec9vboxsync LogFlow(("pgmPoolTrackPhysExtDerefGCPhys: HCPhys=%RX64 idx=%d\n", pPhysPage->HCPhys, pPage->idx));
81096b0da0061583a511da27088643aa949a1ec9vboxsync return;
81096b0da0061583a511da27088643aa949a1ec9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /* we can free the node. */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PVM pVM = pPool->CTX_SUFF(pVM);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync const uint16_t iPhysExtNext = paPhysExts[iPhysExt].iNext;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync if ( iPhysExtPrev == NIL_PGMPOOL_PHYSEXT_INDEX
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync && iPhysExtNext == NIL_PGMPOOL_PHYSEXT_INDEX)
5366e994777f9d9391cf809dc77610f57270d75dvboxsync {
5366e994777f9d9391cf809dc77610f57270d75dvboxsync /* lonely node */
5366e994777f9d9391cf809dc77610f57270d75dvboxsync pgmPoolTrackPhysExtFree(pVM, iPhysExt);
5366e994777f9d9391cf809dc77610f57270d75dvboxsync LogFlow(("pgmPoolTrackPhysExtDerefGCPhys: HCPhys=%RX64 idx=%d lonely\n", pPhysPage->HCPhys, pPage->idx));
5366e994777f9d9391cf809dc77610f57270d75dvboxsync pPhysPage->HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
5366e994777f9d9391cf809dc77610f57270d75dvboxsync }
81096b0da0061583a511da27088643aa949a1ec9vboxsync else if (iPhysExtPrev == NIL_PGMPOOL_PHYSEXT_INDEX)
5366e994777f9d9391cf809dc77610f57270d75dvboxsync {
5366e994777f9d9391cf809dc77610f57270d75dvboxsync /* head */
5366e994777f9d9391cf809dc77610f57270d75dvboxsync LogFlow(("pgmPoolTrackPhysExtDerefGCPhys: HCPhys=%RX64 idx=%d head\n", pPhysPage->HCPhys, pPage->idx));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pPhysPage->HCPhys = (pPhysPage->HCPhys & MM_RAM_FLAGS_NO_REFS_MASK) /** @todo PAGE FLAGS */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync | ((uint64_t)MM_RAM_FLAGS_CREFS_PHYSEXT << MM_RAM_FLAGS_CREFS_SHIFT)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync | ((uint64_t)iPhysExtNext << MM_RAM_FLAGS_IDX_SHIFT);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pgmPoolTrackPhysExtFree(pVM, iPhysExt);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync else
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
5366e994777f9d9391cf809dc77610f57270d75dvboxsync /* in list */
5366e994777f9d9391cf809dc77610f57270d75dvboxsync LogFlow(("pgmPoolTrackPhysExtDerefGCPhys: HCPhys=%RX64 idx=%d\n", pPhysPage->HCPhys, pPage->idx));
5366e994777f9d9391cf809dc77610f57270d75dvboxsync paPhysExts[iPhysExtPrev].iNext = iPhysExtNext;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pgmPoolTrackPhysExtFree(pVM, iPhysExt);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync iPhysExt = iPhysExtNext;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync return;
81096b0da0061583a511da27088643aa949a1ec9vboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* next */
81096b0da0061583a511da27088643aa949a1ec9vboxsync iPhysExtPrev = iPhysExt;
81096b0da0061583a511da27088643aa949a1ec9vboxsync iPhysExt = paPhysExts[iPhysExt].iNext;
81096b0da0061583a511da27088643aa949a1ec9vboxsync } while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
81096b0da0061583a511da27088643aa949a1ec9vboxsync
81096b0da0061583a511da27088643aa949a1ec9vboxsync AssertFatalMsgFailed(("not-found! cRefs=%d HCPhys=%RHp pPage=%p:{.idx=%d}\n", cRefs, pPhysPage->HCPhys, pPage, pPage->idx));
81096b0da0061583a511da27088643aa949a1ec9vboxsync }
81096b0da0061583a511da27088643aa949a1ec9vboxsync else /* nothing to do */
81096b0da0061583a511da27088643aa949a1ec9vboxsync LogFlow(("pgmPoolTrackPhysExtDerefGCPhys: HCPhys=%RX64\n", pPhysPage->HCPhys));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync}
81096b0da0061583a511da27088643aa949a1ec9vboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
5366e994777f9d9391cf809dc77610f57270d75dvboxsync/**
5366e994777f9d9391cf809dc77610f57270d75dvboxsync * Clear references to guest physical memory.
5366e994777f9d9391cf809dc77610f57270d75dvboxsync *
5366e994777f9d9391cf809dc77610f57270d75dvboxsync * This is the same as pgmPoolTracDerefGCPhys except that the guest physical address
5366e994777f9d9391cf809dc77610f57270d75dvboxsync * is assumed to be correct, so the linear search can be skipped and we can assert
5366e994777f9d9391cf809dc77610f57270d75dvboxsync * at an earlier point.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync *
5366e994777f9d9391cf809dc77610f57270d75dvboxsync * @param pPool The pool.
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync * @param pPage The page.
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync * @param HCPhys The host physical address corresponding to the guest page.
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync * @param GCPhys The guest physical address corresponding to HCPhys.
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsync */
51da1b42a8a60004d6b24ebedd4aa3fe853e4b24vboxsyncstatic void pgmPoolTracDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhys)
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync{
f044158ee9eb7045a43f2c4ef2fbc07cb11329aevboxsync /*
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * Walk range list.
5366e994777f9d9391cf809dc77610f57270d75dvboxsync */
5366e994777f9d9391cf809dc77610f57270d75dvboxsync PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync while (pRam)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync RTGCPHYS off = GCPhys - pRam->GCPhys;
5366e994777f9d9391cf809dc77610f57270d75dvboxsync if (off < pRam->cb)
5366e994777f9d9391cf809dc77610f57270d75dvboxsync {
5366e994777f9d9391cf809dc77610f57270d75dvboxsync /* does it match? */
5366e994777f9d9391cf809dc77610f57270d75dvboxsync const unsigned iPage = off >> PAGE_SHIFT;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#ifdef LOG_ENABLED
2f3883b126a405f92b19e829472f614c7352b4f9vboxsyncRTHCPHYS HCPhysPage = PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]);
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsyncLog(("pgmPoolTracDerefGCPhys %RHp vs %RHp\n", HCPhysPage, HCPhys));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#endif
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync return;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync }
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync break;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync }
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync pRam = pRam->CTX_SUFF(pNext);
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync AssertFatalMsgFailed(("HCPhys=%RHp GCPhys=%RGp\n", HCPhys, GCPhys));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync}
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync/**
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * Clear references to guest physical memory.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync *
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pPool The pool.
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync * @param pPage The page.
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync * @param HCPhys The host physical address corresponding to the guest page.
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync * @param GCPhysHint The guest physical address which may corresponding to HCPhys.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync */
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsyncstatic void pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync{
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync /*
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * Walk range list.
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync while (pRam)
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync {
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync RTGCPHYS off = GCPhysHint - pRam->GCPhys;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if (off < pRam->cb)
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync /* does it match? */
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync const unsigned iPage = off >> PAGE_SHIFT;
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync Assert(PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]));
dea3e7faa80f4aab41e08945b9308fd2e3ffe7fcvboxsync if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync {
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync return;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync }
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync break;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync }
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync pRam = pRam->CTX_SUFF(pNext);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync /*
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync * Damn, the hint didn't work. We'll have to do an expensive linear search.
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync */
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync STAM_COUNTER_INC(&pPool->StatTrackLinearRamSearches);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync while (pRam)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync unsigned iPage = pRam->cb >> PAGE_SHIFT;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync while (iPage-- > 0)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync Log4(("pgmPoolTracDerefGCPhysHint: Linear HCPhys=%RHp GCPhysHint=%RGp GCPhysReal=%RGp\n",
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync HCPhys, GCPhysHint, pRam->GCPhys + (iPage << PAGE_SHIFT)));
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync return;
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync }
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pRam = pRam->CTX_SUFF(pNext);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync AssertFatalMsgFailed(("HCPhys=%RHp GCPhysHint=%RGp\n", HCPhys, GCPhysHint));
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync}
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync/**
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync * Clear references to guest physical memory in a 32-bit / 32-bit page table.
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync *
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync * @param pPool The pool.
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync * @param pPage The page.
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync * @param pShwPT The shadow page table (mapping of the page).
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pGstPT The guest page table.
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync */
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsyncDECLINLINE(void) pgmPoolTrackDerefPT32Bit32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT, PCX86PT pGstPT)
dea3e7faa80f4aab41e08945b9308fd2e3ffe7fcvboxsync{
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync if (pShwPT->a[i].n.u1Present)
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync Log4(("pgmPoolTrackDerefPT32Bit32Bit: i=%d pte=%RX32 hint=%RX32\n",
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync i, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK));
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync if (!--pPage->cPresent)
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync break;
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync}
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync/**
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Clear references to guest physical memory in a PAE / 32-bit page table.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pPool The pool.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pPage The page.
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * @param pShwPT The shadow page table (mapping of the page).
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * @param pGstPT The guest page table (just a half one).
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsyncDECLINLINE(void) pgmPoolTrackDerefPTPae32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PT pGstPT)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync{
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync if (pShwPT->a[i].n.u1Present)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Log4(("pgmPoolTrackDerefPTPae32Bit: i=%d pte=%RX32 hint=%RX32\n",
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync }
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync}
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync/**
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Clear references to guest physical memory in a PAE / PAE page table.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync *
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param pPool The pool.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param pPage The page.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param pShwPT The shadow page table (mapping of the page).
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param pGstPT The guest page table.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsyncDECLINLINE(void) pgmPoolTrackDerefPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PTPAE pGstPT)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync{
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync if (pShwPT->a[i].n.u1Present)
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync {
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync Log4(("pgmPoolTrackDerefPTPaePae: i=%d pte=%RX32 hint=%RX32\n",
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PAE_PG_MASK));
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PAE_PG_MASK);
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync }
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync}
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync/**
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * Clear references to guest physical memory in a 32-bit / 4MB page table.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync *
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param pPool The pool.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param pPage The page.
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync * @param pShwPT The shadow page table (mapping of the page).
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsync */
4171ffb38eb8720b2ae9a8d13e95103ab26cfd12vboxsyncDECLINLINE(void) pgmPoolTrackDerefPT32Bit4MB(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync{
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync RTGCPHYS GCPhys = pPage->GCPhys;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync if (pShwPT->a[i].n.u1Present)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Log4(("pgmPoolTrackDerefPT32Bit4MB: i=%d pte=%RX32 GCPhys=%RGp\n",
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync i, pShwPT->a[i].u & X86_PTE_PG_MASK, GCPhys));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, GCPhys);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync }
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync}
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/**
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Clear references to guest physical memory in a PAE / 2/4MB page table.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pPool The pool.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pPage The page.
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync * @param pShwPT The shadow page table (mapping of the page).
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsyncDECLINLINE(void) pgmPoolTrackDerefPTPaeBig(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync{
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync RTGCPHYS GCPhys = pPage->GCPhys;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync if (pShwPT->a[i].n.u1Present)
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync {
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync Log4(("pgmPoolTrackDerefPTPaeBig: i=%d pte=%RX64 hint=%RGp\n",
45655563f818c5d5bbf4b3d14aa48cbd92a871f1vboxsync i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, GCPhys));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, GCPhys);
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync}
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync#ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync/**
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Clear references to shadowed pages in a 32 bits page directory.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pPool The pool.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pPage The page.
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * @param pShwPD The shadow page directory (mapping of the page).
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync */
687794577e2e35c3cae67e692a7f2130d1262a82vboxsyncDECLINLINE(void) pgmPoolTrackDerefPD(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PD pShwPD)
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync{
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if ( pShwPD->a[i].n.u1Present
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync && !(pShwPD->a[i].u & PGM_PDFLAGS_MAPPING)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync )
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PG_MASK);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if (pSubPage)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
5366e994777f9d9391cf809dc77610f57270d75dvboxsync else
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync AssertFatalMsgFailed(("%x\n", pShwPD->a[i].u & X86_PDE_PG_MASK));
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync }
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync }
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync}
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync#endif
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync/**
45655563f818c5d5bbf4b3d14aa48cbd92a871f1vboxsync * Clear references to shadowed pages in a PAE (legacy or 64 bits) page directory.
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync *
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync * @param pPool The pool.
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync * @param pPage The page.
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync * @param pShwPD The shadow page directory (mapping of the page).
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync */
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsyncDECLINLINE(void) pgmPoolTrackDerefPDPae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPAE pShwPD)
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync{
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync {
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync if ( pShwPD->a[i].n.u1Present
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync#ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync && !(pShwPD->a[i].u & PGM_PDFLAGS_MAPPING)
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync#endif
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync )
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync {
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PAE_PG_MASK);
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync if (pSubPage)
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync else
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync AssertFatalMsgFailed(("%RX64\n", pShwPD->a[i].u & X86_PDE_PAE_PG_MASK));
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync }
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync }
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync}
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync/**
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync * Clear references to shadowed pages in a 64-bit page directory pointer table.
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync *
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync * @param pPool The pool.
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync * @param pPage The page.
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync * @param pShwPDPT The shadow page directory pointer table (mapping of the page).
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsyncDECLINLINE(void) pgmPoolTrackDerefPDPT64Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPT pShwPDPT)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync{
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPDPT->a); i++)
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync {
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync if ( pShwPDPT->a[i].n.u1Present
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync#ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
45655563f818c5d5bbf4b3d14aa48cbd92a871f1vboxsync && !(pShwPDPT->a[i].u & PGM_PLXFLAGS_MAPPING)
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync#endif
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync )
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync {
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & X86_PDPE_PG_MASK);
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync if (pSubPage)
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync else
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & X86_PDPE_PG_MASK));
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync }
dea3e7faa80f4aab41e08945b9308fd2e3ffe7fcvboxsync }
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync}
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync/**
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync * Clear references to shadowed pages in a 64-bit level 4 page table.
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync *
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync * @param pPool The pool.
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync * @param pPage The page.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param pShwPML4 The shadow page directory pointer table (mapping of the page).
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync */
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsyncDECLINLINE(void) pgmPoolTrackDerefPML464Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PML4 pShwPML4)
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync{
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPML4->a); i++)
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync {
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync if (pShwPML4->a[i].n.u1Present)
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync {
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPML4->a[i].u & X86_PDPE_PG_MASK);
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync if (pSubPage)
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync else
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync AssertFatalMsgFailed(("%RX64\n", pShwPML4->a[i].u & X86_PML4E_PG_MASK));
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync}
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/**
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Clear references to shadowed pages in an EPT page table.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
56484ba959c372f0196716100568e02412b0dbd5vboxsync * @param pPool The pool.
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * @param pPage The page.
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * @param pShwPML4 The shadow page directory pointer table (mapping of the page).
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync */
687794577e2e35c3cae67e692a7f2130d1262a82vboxsyncDECLINLINE(void) pgmPoolTrackDerefPTEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPT pShwPT)
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync{
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync RTGCPHYS GCPhys = pPage->GCPhys;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (pShwPT->a[i].n.u1Present)
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync {
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync Log4(("pgmPoolTrackDerefPTEPT: i=%d pte=%RX64 GCPhys=%RX64\n",
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync i, pShwPT->a[i].u & EPT_PTE_PG_MASK, pPage->GCPhys));
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & EPT_PTE_PG_MASK, GCPhys);
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync }
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync}
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync/**
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * Clear references to shadowed pages in an EPT page directory.
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync *
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * @param pPool The pool.
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * @param pPage The page.
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * @param pShwPD The shadow page directory (mapping of the page).
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync */
687794577e2e35c3cae67e692a7f2130d1262a82vboxsyncDECLINLINE(void) pgmPoolTrackDerefPDEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPD pShwPD)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync{
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync if (pShwPD->a[i].n.u1Present)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & EPT_PDE_PG_MASK);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (pSubPage)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync else
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync AssertFatalMsgFailed(("%RX64\n", pShwPD->a[i].u & EPT_PDE_PG_MASK));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync}
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/**
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync * Clear references to shadowed pages in an EPT page directory pointer table.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pPool The pool.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pPage The page.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pShwPDPT The shadow page directory pointer table (mapping of the page).
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync */
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsyncDECLINLINE(void) pgmPoolTrackDerefPDPTEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPDPT pShwPDPT)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync{
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPDPT->a); i++)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (pShwPDPT->a[i].n.u1Present)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & EPT_PDPTE_PG_MASK);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (pSubPage)
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync else
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & EPT_PDPTE_PG_MASK));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync }
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync}
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/**
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Clears all references made by this page.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * This includes other shadow pages and GC physical addresses.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync * @param pPool The pool.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pPage The page.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsyncstatic void pgmPoolTrackDeref(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync{
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync /*
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * Map the shadow page and take action according to the page kind.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync void *pvShw = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync switch (pPage->enmKind)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync void *pvGst;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync int rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pgmPoolTrackDerefPT32Bit32Bit(pPool, pPage, (PX86PT)pvShw, (PCX86PT)pvGst);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync break;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync void *pvGst;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync int rc = PGM_GCPHYS_2_PTR_EX(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pgmPoolTrackDerefPTPae32Bit(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PT)pvGst);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync break;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync }
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync {
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync void *pvGst;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync int rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pgmPoolTrackDerefPTPaePae(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PTPAE)pvGst);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync break;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync }
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS: /* treat it like a 4 MB page */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync {
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pgmPoolTrackDerefPT32Bit4MB(pPool, pPage, (PX86PT)pvShw);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync break;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync }
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS: /* treat it like a 2 MB page */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync {
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pgmPoolTrackDerefPTPaeBig(pPool, pPage, (PX86PTPAE)pvShw);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync break;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync }
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync#else /* !PGMPOOL_WITH_GCPHYS_TRACKING */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync break;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync#endif /* !PGMPOOL_WITH_GCPHYS_TRACKING */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pgmPoolTrackDerefPDPae(pPool, pPage, (PX86PDPAE)pvShw);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync break;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync#ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_32BIT_PD:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pgmPoolTrackDerefPD(pPool, pPage, (PX86PD)pvShw);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync break;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PDPT:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync#endif
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pgmPoolTrackDerefPDPT64Bit(pPool, pPage, (PX86PDPT)pvShw);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync break;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_64BIT_PML4:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pgmPoolTrackDerefPML464Bit(pPool, pPage, (PX86PML4)pvShw);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync break;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pgmPoolTrackDerefPTEPT(pPool, pPage, (PEPTPT)pvShw);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync break;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pgmPoolTrackDerefPDEPT(pPool, pPage, (PEPTPD)pvShw);
break;
case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
pgmPoolTrackDerefPDPTEPT(pPool, pPage, (PEPTPDPT)pvShw);
break;
default:
AssertFatalMsgFailed(("enmKind=%d\n", pPage->enmKind));
}
/* paranoia, clear the shadow page. Remove this laser (i.e. let Alloc and ClearAll do it). */
STAM_PROFILE_START(&pPool->StatZeroPage, z);
ASMMemZeroPage(pvShw);
STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
pPage->fZeroed = true;
}
#endif /* PGMPOOL_WITH_USER_TRACKING */
/**
* Flushes all the special root pages as part of a pgmPoolFlushAllInt operation.
*
* @param pPool The pool.
*/
static void pgmPoolFlushAllSpecialRoots(PPGMPOOL pPool)
{
#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
/* Start a subset so we won't run out of mapping space. */
PVMCPU pVCpu = VMMGetCpu(pPool->CTX_SUFF(pVM));
uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
#endif
/*
* These special pages are all mapped into the indexes 1..PGMPOOL_IDX_FIRST.
*/
Assert(NIL_PGMPOOL_IDX == 0);
for (unsigned i = 1; i < PGMPOOL_IDX_FIRST; i++)
{
/*
* Get the page address.
*/
PPGMPOOLPAGE pPage = &pPool->aPages[i];
union
{
uint64_t *pau64;
uint32_t *pau32;
} u;
/*
* Mark stuff not present.
*/
switch (pPage->enmKind)
{
#ifndef VBOX_WITH_PGMPOOL_PAGING_ONLY
case PGMPOOLKIND_ROOT_32BIT_PD:
u.pau64 = (uint64_t *)PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
for (unsigned iPage = 0; iPage < X86_PG_ENTRIES; iPage++)
if ((u.pau32[iPage] & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == X86_PDE_P)
u.pau32[iPage] = 0;
break;
case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
u.pau64 = (uint64_t *)PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
for (unsigned iPage = 0; iPage < X86_PG_PAE_ENTRIES; iPage++)
if ((u.pau64[iPage] & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == X86_PDE_P)
u.pau64[iPage] = 0;
break;
case PGMPOOLKIND_ROOT_PDPT:
/* Not root of shadowed pages currently, ignore it. */
break;
#endif
case PGMPOOLKIND_ROOT_NESTED:
u.pau64 = (uint64_t *)PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
ASMMemZero32(u.pau64, PAGE_SIZE);
break;
}
}
/*
* Paranoia (to be removed), flag a global CR3 sync.
*/
VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
/* Pop the subset. */
PGMDynMapPopAutoSubset(pVCpu, iPrevSubset);
#endif
}
/**
* Flushes the entire cache.
*
* It will assert a global CR3 flush (FF) and assumes the caller is aware of this
* and execute this CR3 flush.
*
* @param pPool The pool.
*/
static void pgmPoolFlushAllInt(PPGMPOOL pPool)
{
STAM_PROFILE_START(&pPool->StatFlushAllInt, a);
LogFlow(("pgmPoolFlushAllInt:\n"));
/*
* If there are no pages in the pool, there is nothing to do.
*/
if (pPool->cCurPages <= PGMPOOL_IDX_FIRST)
{
STAM_PROFILE_STOP(&pPool->StatFlushAllInt, a);
return;
}
/*
* Nuke the free list and reinsert all pages into it.
*/
for (unsigned i = pPool->cCurPages - 1; i >= PGMPOOL_IDX_FIRST; i--)
{
PPGMPOOLPAGE pPage = &pPool->aPages[i];
#ifdef IN_RING3
Assert(pPage->Core.Key == MMPage2Phys(pPool->pVMR3, pPage->pvPageR3));
#endif
#ifdef PGMPOOL_WITH_MONITORING
if (pPage->fMonitored)
pgmPoolMonitorFlush(pPool, pPage);
pPage->iModifiedNext = NIL_PGMPOOL_IDX;
pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
pPage->cModifications = 0;
#endif
pPage->GCPhys = NIL_RTGCPHYS;
pPage->enmKind = PGMPOOLKIND_FREE;
Assert(pPage->idx == i);
pPage->iNext = i + 1;
pPage->fZeroed = false; /* This could probably be optimized, but better safe than sorry. */
pPage->fSeenNonGlobal = false;
pPage->fMonitored= false;
pPage->fCached = false;
pPage->fReusedFlushPending = false;
pPage->fCR3Mix = false;
#ifdef PGMPOOL_WITH_USER_TRACKING
pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
#endif
#ifdef PGMPOOL_WITH_CACHE
pPage->iAgeNext = NIL_PGMPOOL_IDX;
pPage->iAgePrev = NIL_PGMPOOL_IDX;
#endif
}
pPool->aPages[pPool->cCurPages - 1].iNext = NIL_PGMPOOL_IDX;
pPool->iFreeHead = PGMPOOL_IDX_FIRST;
pPool->cUsedPages = 0;
#ifdef PGMPOOL_WITH_USER_TRACKING
/*
* Zap and reinitialize the user records.
*/
pPool->cPresent = 0;
pPool->iUserFreeHead = 0;
PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
const unsigned cMaxUsers = pPool->cMaxUsers;
for (unsigned i = 0; i < cMaxUsers; i++)
{
paUsers[i].iNext = i + 1;
paUsers[i].iUser = NIL_PGMPOOL_IDX;
paUsers[i].iUserTable = 0xfffffffe;
}
paUsers[cMaxUsers - 1].iNext = NIL_PGMPOOL_USER_INDEX;
#endif
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
/*
* Clear all the GCPhys links and rebuild the phys ext free list.
*/
for (PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
pRam;
pRam = pRam->CTX_SUFF(pNext))
{
unsigned iPage = pRam->cb >> PAGE_SHIFT;
while (iPage-- > 0)
pRam->aPages[iPage].HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
}
pPool->iPhysExtFreeHead = 0;
PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
const unsigned cMaxPhysExts = pPool->cMaxPhysExts;
for (unsigned i = 0; i < cMaxPhysExts; i++)
{
paPhysExts[i].iNext = i + 1;
paPhysExts[i].aidx[0] = NIL_PGMPOOL_IDX;
paPhysExts[i].aidx[1] = NIL_PGMPOOL_IDX;
paPhysExts[i].aidx[2] = NIL_PGMPOOL_IDX;
}
paPhysExts[cMaxPhysExts - 1].iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
#endif
#ifdef PGMPOOL_WITH_MONITORING
/*
* Just zap the modified list.
*/
pPool->cModifiedPages = 0;
pPool->iModifiedHead = NIL_PGMPOOL_IDX;
#endif
#ifdef PGMPOOL_WITH_CACHE
/*
* Clear the GCPhys hash and the age list.
*/
for (unsigned i = 0; i < RT_ELEMENTS(pPool->aiHash); i++)
pPool->aiHash[i] = NIL_PGMPOOL_IDX;
pPool->iAgeHead = NIL_PGMPOOL_IDX;
pPool->iAgeTail = NIL_PGMPOOL_IDX;
#endif
/*
* Flush all the special root pages.
* Reinsert active pages into the hash and ensure monitoring chains are correct.
*/
pgmPoolFlushAllSpecialRoots(pPool);
for (unsigned i = PGMPOOL_IDX_FIRST_SPECIAL; i < PGMPOOL_IDX_FIRST; i++)
{
PPGMPOOLPAGE pPage = &pPool->aPages[i];
pPage->iNext = NIL_PGMPOOL_IDX;
#ifdef PGMPOOL_WITH_MONITORING
pPage->iModifiedNext = NIL_PGMPOOL_IDX;
pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
pPage->cModifications = 0;
/* ASSUMES that we're not sharing with any of the other special pages (safe for now). */
pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
if (pPage->fMonitored)
{
PVM pVM = pPool->CTX_SUFF(pVM);
int rc = PGMHandlerPhysicalChangeCallbacks(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1),
pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pPage),
pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pPage),
pPool->pfnAccessHandlerRC, MMHyperCCToRC(pVM, pPage),
pPool->pszAccessHandler);
AssertFatalRCSuccess(rc);
# ifdef PGMPOOL_WITH_CACHE
pgmPoolHashInsert(pPool, pPage);
# endif
}
#endif
#ifdef PGMPOOL_WITH_USER_TRACKING
Assert(pPage->iUserHead == NIL_PGMPOOL_USER_INDEX); /* for now */
#endif
#ifdef PGMPOOL_WITH_CACHE
Assert(pPage->iAgeNext == NIL_PGMPOOL_IDX);
Assert(pPage->iAgePrev == NIL_PGMPOOL_IDX);
#endif
}
/*
* Finally, assert the FF.
*/
VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
STAM_PROFILE_STOP(&pPool->StatFlushAllInt, a);
}
/**
* Flushes a pool page.
*
* This moves the page to the free list after removing all user references to it.
* In GC this will cause a CR3 reload if the page is traced back to an active root page.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @retval VERR_PGM_POOL_CLEARED if the deregistration of the physical handler will cause a light weight pool flush.
* @param pPool The pool.
* @param HCPhys The HC physical address of the shadow page.
*/
int pgmPoolFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
{
int rc = VINF_SUCCESS;
STAM_PROFILE_START(&pPool->StatFlushPage, f);
LogFlow(("pgmPoolFlushPage: pPage=%p:{.Key=%RHp, .idx=%d, .enmKind=%d, .GCPhys=%RGp}\n",
pPage, pPage->Core.Key, pPage->idx, pPage->enmKind, pPage->GCPhys));
/*
* Quietly reject any attempts at flushing any of the special root pages.
*/
if (pPage->idx < PGMPOOL_IDX_FIRST)
{
Log(("pgmPoolFlushPage: special root page, rejected. enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
return VINF_SUCCESS;
}
/*
* Quietly reject any attempts at flushing the currently active shadow CR3 mapping
*/
if (PGMGetHyperCR3(pPool->CTX_SUFF(pVM)) == pPage->Core.Key)
{
#ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
AssertMsg(pPage->enmKind == PGMPOOLKIND_64BIT_PML4,
("Can't free the shadow CR3! (%RHp vs %RHp kind=%d\n", PGMGetHyperCR3(pPool->CTX_SUFF(pVM)), pPage->Core.Key, pPage->enmKind));
#endif
Log(("pgmPoolFlushPage: current active shadow CR3, rejected. enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
return VINF_SUCCESS;
}
#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
/* Start a subset so we won't run out of mapping space. */
PVMCPU pVCpu = VMMGetCpu(pPool->CTX_SUFF(pVM));
uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
#endif
/*
* Mark the page as being in need of a ASMMemZeroPage().
*/
pPage->fZeroed = false;
#ifdef PGMPOOL_WITH_USER_TRACKING
/*
* Clear the page.
*/
pgmPoolTrackClearPageUsers(pPool, pPage);
STAM_PROFILE_START(&pPool->StatTrackDeref,a);
pgmPoolTrackDeref(pPool, pPage);
STAM_PROFILE_STOP(&pPool->StatTrackDeref,a);
#endif
#ifdef PGMPOOL_WITH_CACHE
/*
* Flush it from the cache.
*/
pgmPoolCacheFlushPage(pPool, pPage);
#endif /* PGMPOOL_WITH_CACHE */
#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
/* Heavy stuff done. */
PGMDynMapPopAutoSubset(pVCpu, iPrevSubset);
#endif
#ifdef PGMPOOL_WITH_MONITORING
/*
* Deregistering the monitoring.
*/
if (pPage->fMonitored)
rc = pgmPoolMonitorFlush(pPool, pPage);
#endif
/*
* Free the page.
*/
Assert(pPage->iNext == NIL_PGMPOOL_IDX);
pPage->iNext = pPool->iFreeHead;
pPool->iFreeHead = pPage->idx;
pPage->enmKind = PGMPOOLKIND_FREE;
pPage->GCPhys = NIL_RTGCPHYS;
pPage->fReusedFlushPending = false;
pPool->cUsedPages--;
STAM_PROFILE_STOP(&pPool->StatFlushPage, f);
return rc;
}
/**
* Frees a usage of a pool page.
*
* The caller is responsible to updating the user table so that it no longer
* references the shadow page.
*
* @param pPool The pool.
* @param HCPhys The HC physical address of the shadow page.
* @param iUser The shadow page pool index of the user table.
* @param iUserTable The index into the user table (shadowed).
*/
void pgmPoolFreeByPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
{
STAM_PROFILE_START(&pPool->StatFree, a);
LogFlow(("pgmPoolFreeByPage: pPage=%p:{.Key=%RHp, .idx=%d, enmKind=%d} iUser=%#x iUserTable=%#x\n",
pPage, pPage->Core.Key, pPage->idx, pPage->enmKind, iUser, iUserTable));
Assert(pPage->idx >= PGMPOOL_IDX_FIRST);
#ifdef PGMPOOL_WITH_USER_TRACKING
pgmPoolTrackFreeUser(pPool, pPage, iUser, iUserTable);
#endif
#ifdef PGMPOOL_WITH_CACHE
if (!pPage->fCached)
#endif
pgmPoolFlushPage(pPool, pPage); /* ASSUMES that VERR_PGM_POOL_CLEARED can be ignored here. */
STAM_PROFILE_STOP(&pPool->StatFree, a);
}
/**
* Makes one or more free page free.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
*
* @param pPool The pool.
* @param iUser The user of the page.
*/
static int pgmPoolMakeMoreFreePages(PPGMPOOL pPool, uint16_t iUser)
{
LogFlow(("pgmPoolMakeMoreFreePages: iUser=%#x\n", iUser));
/*
* If the pool isn't full grown yet, expand it.
*/
if (pPool->cCurPages < pPool->cMaxPages)
{
STAM_PROFILE_ADV_SUSPEND(&pPool->StatAlloc, a);
#ifdef IN_RING3
int rc = PGMR3PoolGrow(pPool->pVMR3);
#else
int rc = CTXALLMID(VMM, CallHost)(pPool->CTX_SUFF(pVM), VMMCALLHOST_PGM_POOL_GROW, 0);
#endif
if (RT_FAILURE(rc))
return rc;
STAM_PROFILE_ADV_RESUME(&pPool->StatAlloc, a);
if (pPool->iFreeHead != NIL_PGMPOOL_IDX)
return VINF_SUCCESS;
}
#ifdef PGMPOOL_WITH_CACHE
/*
* Free one cached page.
*/
return pgmPoolCacheFreeOne(pPool, iUser);
#else
/*
* Flush the pool.
*
* If we have tracking enabled, it should be possible to come up with
* a cheap replacement strategy...
*/
/* @todo incompatible with long mode paging (cr3 root will be flushed) */
Assert(!CPUMIsGuestInLongMode(pVM));
pgmPoolFlushAllInt(pPool);
return VERR_PGM_POOL_FLUSHED;
#endif
}
/**
* Allocates a page from the pool.
*
* This page may actually be a cached page and not in need of any processing
* on the callers part.
*
* @returns VBox status code.
* @retval VINF_SUCCESS if a NEW page was allocated.
* @retval VINF_PGM_CACHED_PAGE if a CACHED page was returned.
* @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
* @param pVM The VM handle.
* @param GCPhys The GC physical address of the page we're gonna shadow.
* For 4MB and 2MB PD entries, it's the first address the
* shadow PT is covering.
* @param enmKind The kind of mapping.
* @param iUser The shadow page pool index of the user table.
* @param iUserTable The index into the user table (shadowed).
* @param ppPage Where to store the pointer to the page. NULL is stored here on failure.
*/
int pgmPoolAlloc(PVM pVM, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
{
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
STAM_PROFILE_ADV_START(&pPool->StatAlloc, a);
LogFlow(("pgmPoolAlloc: GCPhys=%RGp enmKind=%d iUser=%#x iUserTable=%#x\n", GCPhys, enmKind, iUser, iUserTable));
*ppPage = NULL;
/** @todo CSAM/PGMPrefetchPage messes up here during CSAMR3CheckGates
* (TRPMR3SyncIDT) because of FF priority. Try fix that?
* Assert(!(pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)); */
#ifdef PGMPOOL_WITH_CACHE
if (pPool->fCacheEnabled)
{
int rc2 = pgmPoolCacheAlloc(pPool, GCPhys, enmKind, iUser, iUserTable, ppPage);
if (RT_SUCCESS(rc2))
{
STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
LogFlow(("pgmPoolAlloc: cached returns %Rrc *ppPage=%p:{.Key=%RHp, .idx=%d}\n", rc2, *ppPage, (*ppPage)->Core.Key, (*ppPage)->idx));
return rc2;
}
}
#endif
/*
* Allocate a new one.
*/
int rc = VINF_SUCCESS;
uint16_t iNew = pPool->iFreeHead;
if (iNew == NIL_PGMPOOL_IDX)
{
rc = pgmPoolMakeMoreFreePages(pPool, iUser);
if (RT_FAILURE(rc))
{
if (rc != VERR_PGM_POOL_CLEARED)
{
Log(("pgmPoolAlloc: returns %Rrc (Free)\n", rc));
STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
return rc;
}
Log(("pgmPoolMakeMoreFreePages failed with %Rrc -> return VERR_PGM_POOL_FLUSHED\n", rc));
rc = VERR_PGM_POOL_FLUSHED;
}
iNew = pPool->iFreeHead;
AssertReleaseReturn(iNew != NIL_PGMPOOL_IDX, VERR_INTERNAL_ERROR);
}
/* unlink the free head */
PPGMPOOLPAGE pPage = &pPool->aPages[iNew];
pPool->iFreeHead = pPage->iNext;
pPage->iNext = NIL_PGMPOOL_IDX;
/*
* Initialize it.
*/
pPool->cUsedPages++; /* physical handler registration / pgmPoolTrackFlushGCPhysPTsSlow requirement. */
pPage->enmKind = enmKind;
pPage->GCPhys = GCPhys;
pPage->fSeenNonGlobal = false; /* Set this to 'true' to disable this feature. */
pPage->fMonitored = false;
pPage->fCached = false;
pPage->fReusedFlushPending = false;
pPage->fCR3Mix = false;
#ifdef PGMPOOL_WITH_MONITORING
pPage->cModifications = 0;
pPage->iModifiedNext = NIL_PGMPOOL_IDX;
pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
#endif
#ifdef PGMPOOL_WITH_USER_TRACKING
pPage->cPresent = 0;
pPage->iFirstPresent = ~0;
/*
* Insert into the tracking and cache. If this fails, free the page.
*/
int rc3 = pgmPoolTrackInsert(pPool, pPage, GCPhys, iUser, iUserTable);
if (RT_FAILURE(rc3))
{
if (rc3 != VERR_PGM_POOL_CLEARED)
{
pPool->cUsedPages--;
pPage->enmKind = PGMPOOLKIND_FREE;
pPage->GCPhys = NIL_RTGCPHYS;
pPage->iNext = pPool->iFreeHead;
pPool->iFreeHead = pPage->idx;
STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
Log(("pgmPoolAlloc: returns %Rrc (Insert)\n", rc3));
return rc3;
}
Log(("pgmPoolTrackInsert failed with %Rrc -> return VERR_PGM_POOL_FLUSHED\n", rc3));
rc = VERR_PGM_POOL_FLUSHED;
}
#endif /* PGMPOOL_WITH_USER_TRACKING */
/*
* Commit the allocation, clear the page and return.
*/
#ifdef VBOX_WITH_STATISTICS
if (pPool->cUsedPages > pPool->cUsedPagesHigh)
pPool->cUsedPagesHigh = pPool->cUsedPages;
#endif
if (!pPage->fZeroed)
{
STAM_PROFILE_START(&pPool->StatZeroPage, z);
void *pv = PGMPOOL_PAGE_2_PTR(pVM, pPage);
ASMMemZeroPage(pv);
STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
}
*ppPage = pPage;
LogFlow(("pgmPoolAlloc: returns %Rrc *ppPage=%p:{.Key=%RHp, .idx=%d, .fCached=%RTbool, .fMonitored=%RTbool}\n",
rc, pPage, pPage->Core.Key, pPage->idx, pPage->fCached, pPage->fMonitored));
STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
return rc;
}
/**
* Frees a usage of a pool page.
*
* @param pVM The VM handle.
* @param HCPhys The HC physical address of the shadow page.
* @param iUser The shadow page pool index of the user table.
* @param iUserTable The index into the user table (shadowed).
*/
void pgmPoolFree(PVM pVM, RTHCPHYS HCPhys, uint16_t iUser, uint32_t iUserTable)
{
LogFlow(("pgmPoolFree: HCPhys=%RHp iUser=%#x iUserTable=%#x\n", HCPhys, iUser, iUserTable));
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
pgmPoolFreeByPage(pPool, pgmPoolGetPage(pPool, HCPhys), iUser, iUserTable);
}
/**
* Gets a in-use page in the pool by it's physical address.
*
* @returns Pointer to the page.
* @param pVM The VM handle.
* @param HCPhys The HC physical address of the shadow page.
* @remark This function will NEVER return NULL. It will assert if HCPhys is invalid.
*/
PPGMPOOLPAGE pgmPoolGetPageByHCPhys(PVM pVM, RTHCPHYS HCPhys)
{
/** @todo profile this! */
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
PPGMPOOLPAGE pPage = pgmPoolGetPage(pPool, HCPhys);
Log3(("pgmPoolGetPageByHCPhys: HCPhys=%RHp -> %p:{.idx=%d .GCPhys=%RGp .enmKind=%d}\n",
HCPhys, pPage, pPage->idx, pPage->GCPhys, pPage->enmKind));
return pPage;
}
/**
* Flushes the entire cache.
*
* It will assert a global CR3 flush (FF) and assumes the caller is aware of this
* and execute this CR3 flush.
*
* @param pPool The pool.
*/
void pgmPoolFlushAll(PVM pVM)
{
LogFlow(("pgmPoolFlushAll:\n"));
pgmPoolFlushAllInt(pVM->pgm.s.CTX_SUFF(pPool));
}