PGMAllPool.cpp revision d816d1a76c08a84682e0388792f47eb43425b8f7
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/* $Id$ */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/** @file
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * PGM Shadow Page Pool.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/*
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync *
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
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync/*******************************************************************************
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync* Header Files *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync*******************************************************************************/
7b9f0c34e9ea328981c99e97054bdf8684d9d620vboxsync#define LOG_GROUP LOG_GROUP_PGM_POOL
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#include <VBox/pgm.h>
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#include <VBox/mm.h>
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#include <VBox/em.h>
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync#include <VBox/cpum.h>
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#ifdef IN_RC
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync# include <VBox/patm.h>
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#endif
b1cc3e87518139898395f96974ecff9e6bf228fbvboxsync#include "PGMInternal.h"
b1cc3e87518139898395f96974ecff9e6bf228fbvboxsync#include <VBox/vm.h>
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#include <VBox/disopcode.h>
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync#include <VBox/hwacc_vmx.h>
388b6b190a5407548753b7fde12fa58134ec3563vboxsync
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync#include <VBox/log.h>
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#include <VBox/err.h>
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#include <iprt/asm.h>
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync#include <iprt/string.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#ifdef LOG_ENABLED
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncstatic const char *pgmPoolPoolKindToStr(uint8_t enmKind);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#endif
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncvoid pgmPoolTrackFlushGCPhysPT(PVM pVM, PPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncvoid pgmPoolTrackFlushGCPhysPTs(PVM pVM, PPGMPAGE pPhysPage, uint16_t iPhysExt);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncint pgmPoolTrackFlushGCPhysPTsSlow(PVM pVM, PPGMPAGE pPhysPage);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncPPGMPOOLPHYSEXT pgmPoolTrackPhysExtAlloc(PVM pVM, uint16_t *piPhysExt);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncvoid pgmPoolTrackPhysExtFree(PVM pVM, uint16_t iPhysExt);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncvoid pgmPoolTrackPhysExtFreeList(PVM pVM, uint16_t iPhysExt);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
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.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsyncDECLINLINE(bool) pgmPoolIsBigPage(PGMPOOLKIND enmKind)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync{
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync switch (enmKind)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync return true;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync default:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync return false;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync}
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/** @def PGMPOOL_PAGE_2_LOCKED_PTR
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Maps a pool page pool into the current context and lock it (RC only).
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @returns VBox status code.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pVM The VM handle.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pPage The pool page.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @remark In RC this uses PGMGCDynMapHCPage(), so it will consume of the
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * small page window employeed by that function. Be careful.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @remark There is no need to assert on the result.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#if defined(IN_RC)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsyncDECLINLINE(void *) PGMPOOL_PAGE_2_LOCKED_PTR(PVM pVM, PPGMPOOLPAGE pPage)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync{
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync void *pv = pgmPoolMapPageInlined(&pVM->pgm.s, pPage);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Make sure the dynamic mapping will not be reused. */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync if (pv)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync PGMDynLockHCPage(pVM, (uint8_t *)pv);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync return pv;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync}
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#else
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync# define PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage) PGMPOOL_PAGE_2_PTR(pVM, pPage)
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync#endif
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
c142e0f462e6c921a73d84ee50f41396b3a99db5vboxsync/** @def PGMPOOL_UNLOCK_PTR
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Unlock a previously locked dynamic caching (RC only).
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync * @returns VBox status code.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pVM The VM handle.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param pPage The pool page.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync *
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @remark In RC this uses PGMGCDynMapHCPage(), so it will consume of the
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * small page window employeed by that function. Be careful.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @remark There is no need to assert on the result.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#if defined(IN_RC)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncDECLINLINE(void) PGMPOOL_UNLOCK_PTR(PVM pVM, void *pvPage)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync{
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (pvPage)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync PGMDynUnlockHCPage(pVM, (uint8_t *)pvPage);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync}
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#else
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync# define PGMPOOL_UNLOCK_PTR(pVM, pPage) do {} while (0)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#endif
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#ifdef PGMPOOL_WITH_MONITORING
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync/**
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * Determin the size of a write instruction.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @returns number of bytes written.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param pDis The disassembler state.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsyncstatic unsigned pgmPoolDisasWriteSize(PDISCPUSTATE pDis)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync{
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync /*
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * This is very crude and possibly wrong for some opcodes,
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * but since it's not really supposed to be called we can
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * probably live with that.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync return DISGetParamSize(pDis, &pDis->param1);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync}
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync/**
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Flushes a chain of pages sharing the same access monitor.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync *
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @returns VBox status code suitable for scheduling.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pPool The pool.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pPage A page in the chain.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsyncint pgmPoolMonitorChainFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync{
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("pgmPoolMonitorChainFlush: Flush page %RGp type=%d\n", pPage->GCPhys, pPage->enmKind));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /*
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * Find the list head.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uint16_t idx = pPage->idx;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync while (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync {
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync idx = pPage->iMonitoredPrev;
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync Assert(idx != pPage->idx);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage = &pPool->aPages[idx];
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync }
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync }
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync /*
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync * Iterate the list flushing each shadow page.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync */
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync int rc = VINF_SUCCESS;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync for (;;)
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync idx = pPage->iMonitoredNext;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync Assert(idx != pPage->idx);
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync if (pPage->idx >= PGMPOOL_IDX_FIRST)
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync {
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync int rc2 = pgmPoolFlushPage(pPool, pPage);
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync AssertRC(rc2);
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync }
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync /* next */
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync if (idx == NIL_PGMPOOL_IDX)
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync break;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage = &pPool->aPages[idx];
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync return rc;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync}
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync/**
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync * Wrapper for getting the current context pointer to the entry being modified.
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync *
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync * @returns VBox status code suitable for scheduling.
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync * @param pVM VM Handle.
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync * @param pvDst Destination address
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync * @param pvSrc Source guest virtual address.
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync * @param GCPhysSrc The source guest physical address.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param cb Size of data to read
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsyncDECLINLINE(int) pgmPoolPhysSimpleReadGCPhys(PVM pVM, void *pvDst, CTXTYPE(RTGCPTR, RTHCPTR, RTGCPTR) pvSrc, RTGCPHYS GCPhysSrc, size_t cb)
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync{
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#if defined(IN_RING3)
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync memcpy(pvDst, (RTHCPTR)((uintptr_t)pvSrc & ~(RTHCUINTPTR)(cb - 1)), cb);
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync return VINF_SUCCESS;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync#else
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync /* @todo in RC we could attempt to use the virtual address, although this can cause many faults (PAE Windows XP guest). */
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync return PGMPhysSimpleReadGCPhys(pVM, pvDst, GCPhysSrc & ~(RTGCPHYS)(cb - 1), cb);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#endif
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync}
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync/**
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync * Process shadow entries before they are changed by the guest.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * For PT entries we will clear them. For PD entries, we'll simply check
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * for mapping conflicts and set the SyncCR3 FF if found.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pVCpu VMCPU handle
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync * @param pPool The pool.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pPage The head page.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param GCPhysFault The guest physical fault address.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param uAddress In R0 and GC this is the guest context fault address (flat).
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * In R3 this is the host context 'fault' address.
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * @param pCpu The disassembler state for figuring out the write size.
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * This need not be specified if the caller knows we won't do cross entry accesses.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsyncvoid pgmPoolMonitorChainChanging(PVMCPU pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault, CTXTYPE(RTGCPTR, RTHCPTR, RTGCPTR) pvAddress, PDISCPUSTATE pCpu)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync{
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync const unsigned off = GCPhysFault & PAGE_OFFSET_MASK;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync const unsigned cbWrite = (pCpu) ? pgmPoolDisasWriteSize(pCpu) : 0;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PVM pVM = pPool->CTX_SUFF(pVM);
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("pgmPoolMonitorChainChanging: %RGv phys=%RGp kind=%s cbWrite=%d\n", (RTGCPTR)pvAddress, GCPhysFault, pgmPoolPoolKindToStr(pPage->enmKind), cbWrite));
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync for (;;)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync union
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync void *pv;
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync PX86PT pPT;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PX86PTPAE pPTPae;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PX86PD pPD;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PX86PDPAE pPDPae;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PX86PDPT pPDPT;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync PX86PML4 pPML4;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync } uShw;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uShw.pv = NULL;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync switch (pPage->enmKind)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync const unsigned iShw = off / sizeof(X86PTE);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT iShw=%x\n", iShw));
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync if (uShw.pPT->a[iShw].n.u1Present)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync X86PTE GstPte;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress, GCPhysFault, sizeof(GstPte));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync AssertRC(rc);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Log4(("pgmPoolMonitorChainChanging 32_32: deref %016RX64 GCPhys %08RX32\n", uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK, GstPte.u & X86_PTE_PG_MASK));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage,
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK,
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync GstPte.u & X86_PTE_PG_MASK);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync# endif
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync ASMAtomicWriteSize(&uShw.pPT->a[iShw], 0);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /* page/2 sized */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync if (!((off ^ pPage->GCPhys) & (PAGE_SIZE / 2)))
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync const unsigned iShw = (off / sizeof(X86PTE)) & (X86_PG_PAE_ENTRIES - 1);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("PGMPOOLKIND_PAE_PT_FOR_32BIT_PT iShw=%x\n", iShw));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (uShw.pPTPae->a[iShw].n.u1Present)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync X86PTE GstPte;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress, GCPhysFault, sizeof(GstPte));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync AssertRC(rc);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync Log4(("pgmPoolMonitorChainChanging pae_32: deref %016RX64 GCPhys %08RX32\n", uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK, GstPte.u & X86_PTE_PG_MASK));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage,
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uShw.pPTPae->a[iShw].u & X86_PTE_PAE_PG_MASK,
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync GstPte.u & X86_PTE_PG_MASK);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# endif
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync ASMAtomicWriteSize(&uShw.pPTPae->a[iShw], 0);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync break;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync }
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync unsigned iGst = off / sizeof(X86PDE);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync unsigned iShwPdpt = iGst / 256;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync unsigned iShw = (iGst % 256) * 2;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync LogFlow(("pgmPoolMonitorChainChanging PAE for 32 bits: iGst=%x iShw=%x idx = %d page idx=%d\n", iGst, iShw, iShwPdpt, pPage->enmKind - PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD));
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync if (iShwPdpt == pPage->enmKind - (unsigned)PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync for (unsigned i = 0; i < 2; i++)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync# ifndef IN_RING0
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync if ((uShw.pPDPae->a[iShw + i].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShwPdpt=%#x iShw=%#x!\n", iShwPdpt, iShw+i));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync else
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# endif /* !IN_RING0 */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (uShw.pPDPae->a[iShw+i].n.u1Present)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw+i, uShw.pPDPae->a[iShw+i].u));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pgmPoolFree(pVM,
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uShw.pPDPae->a[iShw+i].u & X86_PDE_PAE_PG_MASK,
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync pPage->idx,
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync iShw + i);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync ASMAtomicWriteSize(&uShw.pPDPae->a[iShw+i], 0);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /* paranoia / a bit assumptive. */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if ( pCpu
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync && (off & 3)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync && (off & 3) + cbWrite > 4)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync const unsigned iShw2 = iShw + 2 + i;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync if (iShw2 < RT_ELEMENTS(uShw.pPDPae->a))
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# ifndef IN_RING0
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if ((uShw.pPDPae->a[iShw2].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
7e8ef90d3160234df0f254131b87af4243d79476vboxsync Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
7e8ef90d3160234df0f254131b87af4243d79476vboxsync VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
7e8ef90d3160234df0f254131b87af4243d79476vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShwPdpt=%#x iShw2=%#x!\n", iShwPdpt, iShw2));
7e8ef90d3160234df0f254131b87af4243d79476vboxsync break;
7e8ef90d3160234df0f254131b87af4243d79476vboxsync }
7e8ef90d3160234df0f254131b87af4243d79476vboxsync else
7e8ef90d3160234df0f254131b87af4243d79476vboxsync# endif /* !IN_RING0 */
7e8ef90d3160234df0f254131b87af4243d79476vboxsync if (uShw.pPDPae->a[iShw2].n.u1Present)
7e8ef90d3160234df0f254131b87af4243d79476vboxsync {
7e8ef90d3160234df0f254131b87af4243d79476vboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
7e8ef90d3160234df0f254131b87af4243d79476vboxsync pgmPoolFree(pVM,
7e8ef90d3160234df0f254131b87af4243d79476vboxsync uShw.pPDPae->a[iShw2].u & X86_PDE_PAE_PG_MASK,
7e8ef90d3160234df0f254131b87af4243d79476vboxsync pPage->idx,
7e8ef90d3160234df0f254131b87af4243d79476vboxsync iShw2);
7e8ef90d3160234df0f254131b87af4243d79476vboxsync ASMAtomicWriteSize(&uShw.pPDPae->a[iShw2].u, 0);
7e8ef90d3160234df0f254131b87af4243d79476vboxsync }
7e8ef90d3160234df0f254131b87af4243d79476vboxsync }
7e8ef90d3160234df0f254131b87af4243d79476vboxsync }
7e8ef90d3160234df0f254131b87af4243d79476vboxsync }
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync {
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync const unsigned iShw = off / sizeof(X86PTEPAE);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (uShw.pPTPae->a[iShw].n.u1Present)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync X86PTEPAE GstPte;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress, GCPhysFault, sizeof(GstPte));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync AssertRC(rc);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync Log4(("pgmPoolMonitorChainChanging pae: deref %016RX64 GCPhys %016RX64\n", uShw.pPTPae->a[iShw].u & X86_PTE_PAE_PG_MASK, GstPte.u & X86_PTE_PAE_PG_MASK));
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage,
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync uShw.pPTPae->a[iShw].u & X86_PTE_PAE_PG_MASK,
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync GstPte.u & X86_PTE_PAE_PG_MASK);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync# endif
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync ASMAtomicWriteSize(&uShw.pPTPae->a[iShw].u, 0);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync }
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync /* paranoia / a bit assumptive. */
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync if ( pCpu
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync && (off & 7)
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync && (off & 7) + cbWrite > sizeof(X86PTEPAE))
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PTEPAE);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync AssertBreak(iShw2 < RT_ELEMENTS(uShw.pPTPae->a));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync if (uShw.pPTPae->a[iShw2].n.u1Present)
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync {
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync X86PTEPAE GstPte;
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync# ifdef IN_RING3
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, (RTHCPTR)((RTHCUINTPTR)pvAddress + sizeof(GstPte)), GCPhysFault + sizeof(GstPte), sizeof(GstPte));
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync# else
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress + sizeof(GstPte), GCPhysFault + sizeof(GstPte), sizeof(GstPte));
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync# endif
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync AssertRC(rc);
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync Log4(("pgmPoolMonitorChainChanging pae: deref %016RX64 GCPhys %016RX64\n", uShw.pPTPae->a[iShw2].u & X86_PTE_PAE_PG_MASK, GstPte.u & X86_PTE_PAE_PG_MASK));
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage,
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync uShw.pPTPae->a[iShw2].u & X86_PTE_PAE_PG_MASK,
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync GstPte.u & X86_PTE_PAE_PG_MASK);
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync# endif
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync ASMAtomicWriteSize(&uShw.pPTPae->a[iShw2].u ,0);
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync }
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync }
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync break;
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync }
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync case PGMPOOLKIND_32BIT_PD:
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync {
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync const unsigned iShw = off / sizeof(X86PTE); // ASSUMING 32-bit guest paging!
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync LogFlow(("pgmPoolMonitorChainChanging: PGMPOOLKIND_32BIT_PD %x\n", iShw));
1cd59fdf671ca60c64d77e3f7046aaecf7003824vboxsync# ifndef IN_RING0
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (uShw.pPD->a[iShw].u & PGM_PDFLAGS_MAPPING)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# endif /* !IN_RING0 */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# ifndef IN_RING0
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync else
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# endif /* !IN_RING0 */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (uShw.pPD->a[iShw].n.u1Present)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("pgmPoolMonitorChainChanging: 32 bit pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPD->a[iShw].u));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pgmPoolFree(pVM,
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync uShw.pPD->a[iShw].u & X86_PDE_PAE_PG_MASK,
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pPage->idx,
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync iShw);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync ASMAtomicWriteSize(&uShw.pPD->a[iShw].u, 0);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /* paranoia / a bit assumptive. */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if ( pCpu
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync && (off & 3)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync && (off & 3) + cbWrite > sizeof(X86PTE))
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync {
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PTE);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if ( iShw2 != iShw
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync && iShw2 < RT_ELEMENTS(uShw.pPD->a))
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# ifndef IN_RING0
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (uShw.pPD->a[iShw2].u & PGM_PDFLAGS_MAPPING)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# endif /* !IN_RING0 */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# ifndef IN_RING0
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync else
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# endif /* !IN_RING0 */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (uShw.pPD->a[iShw2].n.u1Present)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("pgmPoolMonitorChainChanging: 32 bit pd iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPD->a[iShw2].u));
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync pgmPoolFree(pVM,
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync uShw.pPD->a[iShw2].u & X86_PDE_PAE_PG_MASK,
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pPage->idx,
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync iShw2);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync ASMAtomicWriteSize(&uShw.pPD->a[iShw2].u, 0);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
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 && !VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3))
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("pgmPoolMonitorChainChanging: iShw=%#x: %RX32 -> freeing it!\n", iShw, uShw.pPD->a[iShw].u));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# ifdef IN_RC /* TLB load - we're pushing things a bit... */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync ASMProbeReadByte(pvAddress);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# endif
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pgmPoolFree(pVM, uShw.pPD->a[iShw].u & X86_PDE_PG_MASK, pPage->idx, iShw);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync ASMAtomicWriteSize(&uShw.pPD->a[iShw].u, 0);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#endif
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync break;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync const unsigned iShw = off / sizeof(X86PDEPAE);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#ifndef IN_RING0
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if (uShw.pPDPae->a[iShw].u & PGM_PDFLAGS_MAPPING)
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync break;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#endif /* !IN_RING0 */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync /*
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Causes trouble when the guest uses a PDE to refer to the whole page table level
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * structure. (Invalidate here; faults later on when it tries to change the page
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * table entries -> recheck; probably only applies to the RC case.)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync# ifndef IN_RING0
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync else
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync# endif /* !IN_RING0 */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync {
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync if (uShw.pPDPae->a[iShw].n.u1Present)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pgmPoolFree(pVM,
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync uShw.pPDPae->a[iShw].u & X86_PDE_PAE_PG_MASK,
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage->idx,
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync iShw);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync ASMAtomicWriteSize(&uShw.pPDPae->a[iShw].u, 0);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync /* paranoia / a bit assumptive. */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if ( pCpu
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync && (off & 7)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync && (off & 7) + cbWrite > sizeof(X86PDEPAE))
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDEPAE);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync AssertBreak(iShw2 < RT_ELEMENTS(uShw.pPDPae->a));
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#ifndef IN_RING0
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if ( iShw2 != iShw
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync && uShw.pPDPae->a[iShw2].u & PGM_PDFLAGS_MAPPING)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync break;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#endif /* !IN_RING0 */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync# ifndef IN_RING0
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync else
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync# endif /* !IN_RING0 */
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));
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync pgmPoolFree(pVM,
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync uShw.pPDPae->a[iShw2].u & X86_PDE_PAE_PG_MASK,
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync pPage->idx,
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync iShw2);
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync ASMAtomicWriteSize(&uShw.pPDPae->a[iShw2].u, 0);
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync }
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync }
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync break;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync }
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync case PGMPOOLKIND_PAE_PDPT:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync {
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync /*
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync * Hopefully this doesn't happen very often:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync * - touching unused parts of the page
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync * - messing with the bits of pd pointers without changing the physical address
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync /* PDPT roots are not page aligned; 32 byte only! */
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync const unsigned offPdpt = GCPhysFault - pPage->GCPhys;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync const unsigned iShw = offPdpt / sizeof(X86PDPE);
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync if (iShw < X86_PG_PAE_PDPE_ENTRIES) /* don't use RT_ELEMENTS(uShw.pPDPT->a), because that's for long mode only */
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync {
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync# ifndef IN_RING0
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync if (uShw.pPDPT->a[iShw].u & PGM_PLXFLAGS_MAPPING)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync {
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected pdpt conflict at iShw=%#x!\n", iShw));
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync break;
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync }
5366e994777f9d9391cf809dc77610f57270d75dvboxsync# endif /* !IN_RING0 */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync# ifndef IN_RING0
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync else
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync# endif /* !IN_RING0 */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if (uShw.pPDPT->a[iShw].n.u1Present)
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync {
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPT->a[iShw].u));
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync pgmPoolFree(pVM,
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync uShw.pPDPT->a[iShw].u & X86_PDPE_PG_MASK,
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync pPage->idx,
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync iShw);
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync ASMAtomicWriteSize(&uShw.pPDPT->a[iShw].u, 0);
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync }
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync /* paranoia / a bit assumptive. */
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync if ( pCpu
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync && (offPdpt & 7)
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync && (offPdpt & 7) + cbWrite > sizeof(X86PDPE))
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync const unsigned iShw2 = (offPdpt + cbWrite - 1) / sizeof(X86PDPE);
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync if ( iShw2 != iShw
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync && iShw2 < X86_PG_PAE_PDPE_ENTRIES)
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync# ifndef IN_RING0
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync if (uShw.pPDPT->a[iShw2].u & PGM_PLXFLAGS_MAPPING)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync break;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync# endif /* !IN_RING0 */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync# ifndef IN_RING0
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync else
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync# endif /* !IN_RING0 */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if (uShw.pPDPT->a[iShw2].n.u1Present)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPT->a[iShw2].u));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pgmPoolFree(pVM,
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync uShw.pPDPT->a[iShw2].u & X86_PDPE_PG_MASK,
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync pPage->idx,
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync iShw2);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync ASMAtomicWriteSize(&uShw.pPDPT->a[iShw2].u, 0);
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync }
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync }
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync }
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync break;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync }
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync#ifndef IN_RC
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync {
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync const unsigned iShw = off / sizeof(X86PDEPAE);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync Assert(!(uShw.pPDPae->a[iShw].u & PGM_PDFLAGS_MAPPING));
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync if (uShw.pPDPae->a[iShw].n.u1Present)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync pgmPoolFree(pVM,
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync uShw.pPDPae->a[iShw].u & X86_PDE_PAE_PG_MASK,
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pPage->idx,
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync iShw);
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync ASMAtomicWriteSize(&uShw.pPDPae->a[iShw].u, 0);
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync }
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync /* paranoia / a bit assumptive. */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if ( pCpu
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync && (off & 7)
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync && (off & 7) + cbWrite > sizeof(X86PDEPAE))
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDEPAE);
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync AssertBreak(iShw2 < RT_ELEMENTS(uShw.pPDPae->a));
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync Assert(!(uShw.pPDPae->a[iShw2].u & PGM_PDFLAGS_MAPPING));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if (uShw.pPDPae->a[iShw2].n.u1Present)
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync {
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync pgmPoolFree(pVM,
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync uShw.pPDPae->a[iShw2].u & X86_PDE_PAE_PG_MASK,
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync pPage->idx,
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync iShw2);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync ASMAtomicWriteSize(&uShw.pPDPae->a[iShw2].u, 0);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync break;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync /*
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync * Hopefully this doesn't happen very often:
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * - messing with the bits of pd pointers without changing the physical address
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync */
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync if (!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3))
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync {
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync const unsigned iShw = off / sizeof(X86PDPE);
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync if (uShw.pPDPT->a[iShw].n.u1Present)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync {
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPT->a[iShw].u));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pgmPoolFree(pVM, uShw.pPDPT->a[iShw].u & X86_PDPE_PG_MASK, pPage->idx, iShw);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync ASMAtomicWriteSize(&uShw.pPDPT->a[iShw].u, 0);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync /* paranoia / a bit assumptive. */
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync if ( pCpu
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync && (off & 7)
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync && (off & 7) + cbWrite > sizeof(X86PDPE))
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync {
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDPE);
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync if (uShw.pPDPT->a[iShw2].n.u1Present)
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync {
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPT->a[iShw2].u));
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync pgmPoolFree(pVM, uShw.pPDPT->a[iShw2].u & X86_PDPE_PG_MASK, pPage->idx, iShw2);
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync ASMAtomicWriteSize(&uShw.pPDPT->a[iShw2].u, 0);
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync }
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync }
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync }
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync break;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync case PGMPOOLKIND_64BIT_PML4:
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync {
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync /*
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync * Hopefully this doesn't happen very often:
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync * - messing with the bits of pd pointers without changing the physical address
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync */
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync if (!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3))
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync const unsigned iShw = off / sizeof(X86PDPE);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if (uShw.pPML4->a[iShw].n.u1Present)
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync {
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPML4->a[iShw].u));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pgmPoolFree(pVM, uShw.pPML4->a[iShw].u & X86_PML4E_PG_MASK, pPage->idx, iShw);
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync ASMAtomicWriteSize(&uShw.pPML4->a[iShw].u, 0);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* paranoia / a bit assumptive. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if ( pCpu
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync && (off & 7)
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync && (off & 7) + cbWrite > sizeof(X86PDPE))
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync {
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PML4E);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (uShw.pPML4->a[iShw2].n.u1Present)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPML4->a[iShw2].u));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pgmPoolFree(pVM, uShw.pPML4->a[iShw2].u & X86_PML4E_PG_MASK, pPage->idx, iShw2);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync ASMAtomicWriteSize(&uShw.pPML4->a[iShw2].u, 0);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync break;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#endif /* IN_RING0 */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync default:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync AssertFatalMsgFailed(("enmKind=%d\n", pPage->enmKind));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync PGMPOOL_UNLOCK_PTR(pVM, uShw.pv);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* next */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (pPage->iMonitoredNext == NIL_PGMPOOL_IDX)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync return;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pPage = &pPool->aPages[pPage->iMonitoredNext];
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync}
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync# ifndef IN_RING3
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync/**
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Checks if a access could be a fork operation in progress.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync *
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Meaning, that the guest is setuping up the parent process for Copy-On-Write.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync *
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @returns true if it's likly that we're forking, otherwise false.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pPool The pool.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pCpu The disassembled instruction.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param offFault The access offset.
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync */
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsyncDECLINLINE(bool) pgmPoolMonitorIsForking(PPGMPOOL pPool, PDISCPUSTATE pCpu, unsigned offFault)
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync{
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync /*
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync * i386 linux is using btr to clear X86_PTE_RW.
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * The functions involved are (2.6.16 source inspection):
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync * clear_bit
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * ptep_set_wrprotect
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * copy_one_pte
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * copy_pte_range
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * copy_pmd_range
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * copy_pud_range
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * copy_page_range
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * dup_mmap
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * dup_mm
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * copy_mm
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * copy_process
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * do_fork
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync */
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync if ( pCpu->pCurInstr->opcode == OP_BTR
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync && !(offFault & 4)
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync /** @todo Validate that the bit index is X86_PTE_RW. */
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync )
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync {
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,Fork));
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync return true;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync return false;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync}
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync/**
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Determine whether the page is likely to have been reused.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync *
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @returns true if we consider the page as being reused for a different purpose.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @returns false if we consider it to still be a paging page.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pVM VM Handle.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pRegFrame Trap register frame.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pCpu The disassembly info for the faulting instruction.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pvFault The fault address.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync *
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @remark The REP prefix check is left to the caller because of STOSD/W.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncDECLINLINE(bool) pgmPoolMonitorIsReused(PVM pVM, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu, RTGCPTR pvFault)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync{
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#ifndef IN_RC
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /** @todo could make this general, faulting close to rsp should be safe reuse heuristic. */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if ( HWACCMHasPendingIrq(pVM)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync && (pRegFrame->rsp - pvFault) < 32)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Fault caused by stack writes while trying to inject an interrupt event. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync Log(("pgmPoolMonitorIsReused: reused %RGv for interrupt stack (rsp=%RGv).\n", pvFault, pRegFrame->rsp));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync return true;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#else
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync NOREF(pVM); NOREF(pvFault);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#endif
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync switch (pCpu->pCurInstr->opcode)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* call implies the actual push of the return address faulted */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case OP_CALL:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync Log4(("pgmPoolMonitorIsReused: CALL\n"));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync return true;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case OP_PUSH:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync Log4(("pgmPoolMonitorIsReused: PUSH\n"));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync return true;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case OP_PUSHF:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync Log4(("pgmPoolMonitorIsReused: PUSHF\n"));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync return true;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case OP_PUSHA:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync Log4(("pgmPoolMonitorIsReused: PUSHA\n"));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync return true;
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync case OP_FXSAVE:
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync Log4(("pgmPoolMonitorIsReused: FXSAVE\n"));
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync return true;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync case OP_MOVNTI: /* solaris - block_zero_no_xmm */
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync Log4(("pgmPoolMonitorIsReused: MOVNTI\n"));
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync return true;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync case OP_MOVNTDQ: /* solaris - hwblkclr & hwblkpagecopy */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync Log4(("pgmPoolMonitorIsReused: MOVNTDQ\n"));
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync return true;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case OP_MOVSWD:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case OP_STOSWD:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if ( pCpu->prefix == (PREFIX_REP|PREFIX_REX)
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync && pRegFrame->rcx >= 0x40
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync )
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync {
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync Assert(pCpu->mode == CPUMODE_64BIT);
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync Log(("pgmPoolMonitorIsReused: OP_STOSQ\n"));
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync return true;
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync }
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync return false;
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync }
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync if ( (pCpu->param1.flags & USE_REG_GEN32)
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync && (pCpu->param1.base.reg_gen == USE_REG_ESP))
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync {
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync Log4(("pgmPoolMonitorIsReused: ESP\n"));
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync return true;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync }
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync return false;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync}
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync/**
004d74842597dacc4009803171296dfcf9398c69vboxsync * Flushes the page being accessed.
004d74842597dacc4009803171296dfcf9398c69vboxsync *
004d74842597dacc4009803171296dfcf9398c69vboxsync * @returns VBox status code suitable for scheduling.
004d74842597dacc4009803171296dfcf9398c69vboxsync * @param pVM The VM handle.
004d74842597dacc4009803171296dfcf9398c69vboxsync * @param pVCpu The VMCPU handle.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pPool The pool.
004d74842597dacc4009803171296dfcf9398c69vboxsync * @param pPage The pool page (head).
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pCpu The disassembly of the write instruction.
004d74842597dacc4009803171296dfcf9398c69vboxsync * @param pRegFrame The trap register frame.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param GCPhysFault The fault address as guest physical address.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pvFault The fault address.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsyncstatic int pgmPoolAccessHandlerFlush(PVM pVM, PVMCPU pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
004d74842597dacc4009803171296dfcf9398c69vboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync{
004d74842597dacc4009803171296dfcf9398c69vboxsync /*
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * First, do the flushing.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync */
004d74842597dacc4009803171296dfcf9398c69vboxsync int rc = pgmPoolMonitorChainFlush(pPool, pPage);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync /*
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Emulate the instruction (xp/w2k problem, requires pc/cr2/sp detection).
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync uint32_t cbWritten;
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync int rc2 = EMInterpretInstructionCPU(pVM, pVCpu, pCpu, pRegFrame, pvFault, &cbWritten);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if (RT_SUCCESS(rc2))
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pRegFrame->rip += pCpu->opsize;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync else if (rc2 == VERR_EM_INTERPRETER)
004d74842597dacc4009803171296dfcf9398c69vboxsync {
004d74842597dacc4009803171296dfcf9398c69vboxsync#ifdef IN_RC
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (PATMIsPatchGCAddr(pVM, (RTRCPTR)pRegFrame->eip))
5366e994777f9d9391cf809dc77610f57270d75dvboxsync {
5366e994777f9d9391cf809dc77610f57270d75dvboxsync LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for patch code %04x:%RGv, ignoring.\n",
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pRegFrame->cs, (RTGCPTR)pRegFrame->eip));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc = VINF_SUCCESS;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync STAM_COUNTER_INC(&pPool->StatMonitorRZIntrFailPatch2);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync else
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#endif
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc = VINF_EM_RAW_EMULATE_INSTR;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,EmulateInstr));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync else
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc = rc2;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* See use in pgmPoolAccessHandlerSimple(). */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync PGM_INVL_GUEST_TLBS();
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync LogFlow(("pgmPoolAccessHandlerPT: returns %Rrc (flushed)\n", rc));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync return rc;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync}
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync/**
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Handles the STOSD write accesses.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync *
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @returns VBox status code suitable for scheduling.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pVM The VM handle.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pPool The pool.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pPage The pool page (head).
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pCpu The disassembly of the write instruction.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pRegFrame The trap register frame.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param GCPhysFault The fault address as guest physical address.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pvFault The fault address.
5366e994777f9d9391cf809dc77610f57270d75dvboxsync */
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsyncDECLINLINE(int) pgmPoolAccessHandlerSTOSD(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync{
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync Assert(pCpu->mode == CPUMODE_32BIT);
5366e994777f9d9391cf809dc77610f57270d75dvboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync Log3(("pgmPoolAccessHandlerSTOSD\n"));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
5366e994777f9d9391cf809dc77610f57270d75dvboxsync /*
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Increment the modification counter and insert it into the list
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * of modified pages the first time.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (!pPage->cModifications++)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pgmPoolMonitorModifiedInsert(pPool, pPage);
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync /*
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync * Execute REP STOSD.
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync *
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync * This ASSUMES that we're not invoked by Trap0e on in a out-of-sync
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync * write situation, meaning that it's safe to write here.
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync */
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync PVMCPU pVCpu = VMMGetCpu(pPool->CTX_SUFF(pVM));
5366e994777f9d9391cf809dc77610f57270d75dvboxsync RTGCUINTPTR pu32 = (RTGCUINTPTR)pvFault;
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync while (pRegFrame->ecx)
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync {
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, (RTGCPTR)pu32, NULL);
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync PGMDynMapPopAutoSubset(pVCpu, iPrevSubset);
5366e994777f9d9391cf809dc77610f57270d75dvboxsync#else
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, (RTGCPTR)pu32, NULL);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#endif
5366e994777f9d9391cf809dc77610f57270d75dvboxsync#ifdef IN_RC
5366e994777f9d9391cf809dc77610f57270d75dvboxsync *(uint32_t *)pu32 = pRegFrame->eax;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync#else
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync PGMPhysSimpleWriteGCPhys(pVM, GCPhysFault, &pRegFrame->eax, 4);
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync#endif
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync pu32 += 4;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync GCPhysFault += 4;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pRegFrame->edi += 4;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync pRegFrame->ecx--;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pRegFrame->rip += pCpu->opsize;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync#ifdef IN_RC
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync /* See use in pgmPoolAccessHandlerSimple(). */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync PGM_INVL_GUEST_TLBS();
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync#endif
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync LogFlow(("pgmPoolAccessHandlerSTOSD: returns\n"));
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync return VINF_SUCCESS;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync}
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync/**
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * Handles the simple write accesses.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync *
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync * @returns VBox status code suitable for scheduling.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pVM The VM handle.
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync * @param pVCpu The VMCPU handle.
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync * @param pPool The pool.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pPage The pool page (head).
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync * @param pCpu The disassembly of the write instruction.
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync * @param pRegFrame The trap register frame.
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync * @param GCPhysFault The fault address as guest physical address.
3ff8aa7d3c74cfbe8da5f77b8ea6c748cc79213avboxsync * @param pvFault The fault address.
5366e994777f9d9391cf809dc77610f57270d75dvboxsync */
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsyncDECLINLINE(int) pgmPoolAccessHandlerSimple(PVM pVM, PVMCPU pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync{
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync Log3(("pgmPoolAccessHandlerSimple\n"));
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync /*
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync * Increment the modification counter and insert it into the list
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync * of modified pages the first time.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if (!pPage->cModifications++)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pgmPoolMonitorModifiedInsert(pPool, pPage);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync /*
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync * Clear all the pages. ASSUMES that pvFault is readable.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, pvFault, pCpu);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync PGMDynMapPopAutoSubset(pVCpu, iPrevSubset);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync#else
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, pvFault, pCpu);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync#endif
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync /*
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync * Interpret the instruction.
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync uint32_t cb;
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync int rc = EMInterpretInstructionCPU(pVM, pVCpu, pCpu, pRegFrame, pvFault, &cb);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if (RT_SUCCESS(rc))
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync pRegFrame->rip += pCpu->opsize;
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync else if (rc == VERR_EM_INTERPRETER)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for %04x:%RGv - opcode=%d\n",
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync pRegFrame->cs, (RTGCPTR)pRegFrame->rip, pCpu->pCurInstr->opcode));
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync rc = VINF_EM_RAW_EMULATE_INSTR;
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,EmulateInstr));
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync }
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync#ifdef IN_RC
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync /*
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync * Quick hack, with logging enabled we're getting stale
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync * code TLBs but no data TLB for EIP and crash in EMInterpretDisasOne.
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync * Flushing here is BAD and expensive, I think EMInterpretDisasOne will
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync * have to be fixed to support this. But that'll have to wait till next week.
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync *
40c1a23e86c79b24a917a43c186b2e54504d12c1vboxsync * An alternative is to keep track of the changed PTEs together with the
40c1a23e86c79b24a917a43c186b2e54504d12c1vboxsync * GCPhys from the guest PT. This may proove expensive though.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync *
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * At the moment, it's VITAL that it's done AFTER the instruction interpreting
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * because we need the stale TLBs in some cases (XP boot). This MUST be fixed properly!
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync PGM_INVL_GUEST_TLBS();
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#endif
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync LogFlow(("pgmPoolAccessHandlerSimple: returns %Rrc cb=%d\n", rc, cb));
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync return rc;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync}
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync/**
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * \#PF Handler callback for PT write accesses.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync *
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @returns VBox status code (appropriate for GC return).
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @param pVM VM Handle.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @param uErrorCode CPU Error code.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @param pRegFrame Trap register frame.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * NULL on DMA and other non CPU access.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @param pvFault The fault address (cr2).
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @param GCPhysFault The GC physical address corresponding to pvFault.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @param pvUser User argument.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncDECLEXPORT(int) pgmPoolAccessHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync{
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync STAM_PROFILE_START(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), a);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync PPGMPOOLPAGE pPage = (PPGMPOOLPAGE)pvUser;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync PVMCPU pVCpu = VMMGetCpu(pVM);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync LogFlow(("pgmPoolAccessHandler: pvFault=%RGv pPage=%p:{.idx=%d} GCPhysFault=%RGp\n", pvFault, pPage, pPage->idx, GCPhysFault));
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /*
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * We should ALWAYS have the list head as user parameter. This
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * is because we use that page to record the changes.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync Assert(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /*
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * Disassemble the faulting instruction.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync DISCPUSTATE Cpu;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync int rc = EMInterpretDisasOne(pVM, pVCpu, pRegFrame, &Cpu, NULL);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync AssertRCReturn(rc, rc);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pgmLock(pVM);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync AssertMsg(PHYS_PAGE_ADDRESS(GCPhysFault) == PHYS_PAGE_ADDRESS(pPage->GCPhys), ("%RGp vs %RGp\n", PHYS_PAGE_ADDRESS(GCPhysFault), pPage->GCPhys));
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /*
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * Check if it's worth dealing with.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync bool fReused = false;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync if ( ( pPage->cModifications < 48 /** @todo #define */ /** @todo need to check that it's not mapping EIP. */ /** @todo adjust this! */
40c1a23e86c79b24a917a43c186b2e54504d12c1vboxsync || pgmPoolIsPageLocked(&pVM->pgm.s, pPage)
40c1a23e86c79b24a917a43c186b2e54504d12c1vboxsync )
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync && !(fReused = pgmPoolMonitorIsReused(pVM, pRegFrame, &Cpu, pvFault))
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync && !pgmPoolMonitorIsForking(pPool, &Cpu, GCPhysFault & PAGE_OFFSET_MASK))
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync /*
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * Simple instructions, no REP prefix.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if (!(Cpu.prefix & (PREFIX_REP | PREFIX_REPNE)))
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync rc = pgmPoolAccessHandlerSimple(pVM, pVCpu, pPool, pPage, &Cpu, pRegFrame, GCPhysFault, pvFault);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,Handled), a);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pgmUnlock(pVM);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync return rc;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync /*
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * Windows is frequently doing small memset() operations (netio test 4k+).
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * We have to deal with these or we'll kill the cache and performance.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if ( Cpu.pCurInstr->opcode == OP_STOSWD
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync && CPUMGetGuestCPL(pVCpu, pRegFrame) == 0
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync && pRegFrame->ecx <= 0x20
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync && pRegFrame->ecx * 4 <= PAGE_SIZE - ((uintptr_t)pvFault & PAGE_OFFSET_MASK)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync && !((uintptr_t)pvFault & 3)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync && (pRegFrame->eax == 0 || pRegFrame->eax == 0x80) /* the two values observed. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync && Cpu.mode == CPUMODE_32BIT
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync && Cpu.opmode == CPUMODE_32BIT
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync && Cpu.addrmode == CPUMODE_32BIT
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync && Cpu.prefix == PREFIX_REP
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync && !pRegFrame->eflags.Bits.u1DF
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync )
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc = pgmPoolAccessHandlerSTOSD(pVM, pPool, pPage, &Cpu, pRegFrame, GCPhysFault, pvFault);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,RepStosd), a);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pgmUnlock(pVM);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync return rc;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync /* REP prefix, don't bother. */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,RepPrefix));
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync Log4(("pgmPoolAccessHandler: eax=%#x ecx=%#x edi=%#x esi=%#x rip=%RGv opcode=%d prefix=%#x\n",
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pRegFrame->eax, pRegFrame->ecx, pRegFrame->edi, pRegFrame->esi, (RTGCPTR)pRegFrame->rip, Cpu.pCurInstr->opcode, Cpu.prefix));
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync }
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync /*
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * Not worth it, so flush it.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync *
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * If we considered it to be reused, don't go back to ring-3
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * to emulate failed instructions since we usually cannot
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * interpret then. This may be a bit risky, in which case
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * the reuse detection must be fixed.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync rc = pgmPoolAccessHandlerFlush(pVM, pVCpu, pPool, pPage, &Cpu, pRegFrame, GCPhysFault, pvFault);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync if (rc == VINF_EM_RAW_EMULATE_INSTR && fReused)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync rc = VINF_SUCCESS;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,FlushPage), a);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pgmUnlock(pVM);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync return rc;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync}
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync# endif /* !IN_RING3 */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync#endif /* PGMPOOL_WITH_MONITORING */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#ifdef PGMPOOL_WITH_CACHE
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync/**
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * Inserts a page into the GCPhys hash table.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync *
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @param pPool The pool.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param pPage The page.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncDECLINLINE(void) pgmPoolHashInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync{
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync Log3(("pgmPoolHashInsert: %RGp\n", pPage->GCPhys));
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync Assert(pPage->GCPhys != NIL_RTGCPHYS); Assert(pPage->iNext == NIL_PGMPOOL_IDX);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync uint16_t iHash = PGMPOOL_HASH(pPage->GCPhys);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPage->iNext = pPool->aiHash[iHash];
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPool->aiHash[iHash] = pPage->idx;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync}
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync/**
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * Removes a page from the GCPhys hash table.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync *
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param pPool The pool.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param pPage The page.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsyncDECLINLINE(void) pgmPoolHashRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync{
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync Log3(("pgmPoolHashRemove: %RGp\n", pPage->GCPhys));
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync uint16_t iHash = PGMPOOL_HASH(pPage->GCPhys);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (pPool->aiHash[iHash] == pPage->idx)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pPool->aiHash[iHash] = pPage->iNext;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync else
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync {
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync uint16_t iPrev = pPool->aiHash[iHash];
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync for (;;)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync const int16_t i = pPool->aPages[iPrev].iNext;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if (i == pPage->idx)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->aPages[iPrev].iNext = pPage->iNext;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync break;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync if (i == NIL_PGMPOOL_IDX)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync AssertReleaseMsgFailed(("GCPhys=%RGp idx=%#x\n", pPage->GCPhys, pPage->idx));
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync break;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync }
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync iPrev = i;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pPage->iNext = NIL_PGMPOOL_IDX;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync}
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync/**
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * Frees up one cache page.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync *
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @returns VBox status code.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @retval VINF_SUCCESS on success.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param pPool The pool.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param iUser The user index.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync */
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 PPGMPOOLPAGE pPage;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync for (unsigned iLoop = 0; ; iLoop++)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
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 {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync uint16_t i = pPool->aPages[iToFree].iAgePrev;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync for (unsigned j = 0; j < 10 && i != NIL_PGMPOOL_USER_INDEX; j++, i = pPool->aPages[i].iAgePrev)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if (pPool->aPages[iToFree].iUserHead == NIL_PGMPOOL_USER_INDEX)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync continue;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync iToFree = i;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync break;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync*/
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync Assert(iToFree != iUser);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync AssertRelease(iToFree != NIL_PGMPOOL_IDX);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPage = &pPool->aPages[iToFree];
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /*
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * Reject any attempts at flushing the currently active shadow CR3 mapping.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * Call pgmPoolCacheUsed to move the page to the head of the age list.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync if (!pgmPoolIsPageLocked(&pPool->CTX_SUFF(pVM)->pgm.s, pPage))
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync break;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync LogFlow(("pgmPoolCacheFreeOne: refuse CR3 mapping\n"));
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pgmPoolCacheUsed(pPool, pPage);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync AssertLogRelReturn(iLoop < 8192, VERR_INTERNAL_ERROR);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync }
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /*
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * Found a usable page, flush it and return.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync int rc = pgmPoolFlushPage(pPool, pPage);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync if (rc == VINF_SUCCESS)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync PGM_INVL_GUEST_TLBS(); /* see PT handler. */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync return rc;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync}
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync/**
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * Checks if a kind mismatch is really a page being reused
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * or if it's just normal remappings.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync *
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @returns true if reused and the cached page (enmKind1) should be flushed
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @returns false if not reused.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @param enmKind1 The kind of the cached page.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @param enmKind2 The kind of the requested page.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncstatic bool pgmPoolCacheReusedByKind(PGMPOOLKIND enmKind1, PGMPOOLKIND enmKind2)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync{
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync switch (enmKind1)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync {
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /*
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * Never reuse them. There is no remapping in non-paging mode.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PD_PHYS:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PDPT_FOR_32BIT: /* never reuse them for other types */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync return false;
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync /*
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * It's perfectly fine to reuse these, except for PAE and non-paging stuff.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_32BIT_PD:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync case PGMPOOLKIND_PAE_PDPT:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync switch (enmKind2)
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync case PGMPOOLKIND_64BIT_PML4:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync return true;
6b9d50a0f466bd5a61458ed53925480ab28a3c17vboxsync default:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync return false;
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync }
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync /*
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * It's perfectly fine to reuse these, except for PAE and non-paging stuff.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync case PGMPOOLKIND_64BIT_PML4:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync switch (enmKind2)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
5366e994777f9d9391cf809dc77610f57270d75dvboxsync case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
5366e994777f9d9391cf809dc77610f57270d75dvboxsync case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
5366e994777f9d9391cf809dc77610f57270d75dvboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
5366e994777f9d9391cf809dc77610f57270d75dvboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
5366e994777f9d9391cf809dc77610f57270d75dvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
5366e994777f9d9391cf809dc77610f57270d75dvboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
5366e994777f9d9391cf809dc77610f57270d75dvboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
5366e994777f9d9391cf809dc77610f57270d75dvboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
5366e994777f9d9391cf809dc77610f57270d75dvboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync return true;
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync default:
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync return false;
5366e994777f9d9391cf809dc77610f57270d75dvboxsync }
5366e994777f9d9391cf809dc77610f57270d75dvboxsync
5366e994777f9d9391cf809dc77610f57270d75dvboxsync /*
5366e994777f9d9391cf809dc77610f57270d75dvboxsync * These cannot be flushed, and it's common to reuse the PDs as PTs.
5366e994777f9d9391cf809dc77610f57270d75dvboxsync */
5366e994777f9d9391cf809dc77610f57270d75dvboxsync case PGMPOOLKIND_ROOT_NESTED:
5366e994777f9d9391cf809dc77610f57270d75dvboxsync return false;
5366e994777f9d9391cf809dc77610f57270d75dvboxsync
5366e994777f9d9391cf809dc77610f57270d75dvboxsync default:
5366e994777f9d9391cf809dc77610f57270d75dvboxsync AssertFatalMsgFailed(("enmKind1=%d\n", enmKind1));
5366e994777f9d9391cf809dc77610f57270d75dvboxsync }
5366e994777f9d9391cf809dc77610f57270d75dvboxsync}
5366e994777f9d9391cf809dc77610f57270d75dvboxsync
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync/**
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync * Attempts to satisfy a pgmPoolAlloc request from the cache.
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync *
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync * @returns VBox status code.
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync * @retval VINF_PGM_CACHED_PAGE on success.
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync * @retval VERR_FILE_NOT_FOUND if not found.
7e8ef90d3160234df0f254131b87af4243d79476vboxsync * @param pPool The pool.
7e8ef90d3160234df0f254131b87af4243d79476vboxsync * @param GCPhys The GC physical address of the page we're gonna shadow.
7e8ef90d3160234df0f254131b87af4243d79476vboxsync * @param enmKind The kind of mapping.
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync * @param iUser The shadow page pool index of the user table.
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync * @param iUserTable The index into the user table (shadowed).
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync * @param ppPage Where to store the pointer to the page.
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync */
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsyncstatic int pgmPoolCacheAlloc(PPGMPOOL pPool, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
e2489bd9ef063ae006feaebc3318ffa4143f6e16vboxsync{
e2489bd9ef063ae006feaebc3318ffa4143f6e16vboxsync#ifndef IN_RC
e2489bd9ef063ae006feaebc3318ffa4143f6e16vboxsync const PVM pVM = pPool->CTX_SUFF(pVM);
e2489bd9ef063ae006feaebc3318ffa4143f6e16vboxsync#endif
e2489bd9ef063ae006feaebc3318ffa4143f6e16vboxsync /*
e2489bd9ef063ae006feaebc3318ffa4143f6e16vboxsync * Look up the GCPhys in the hash.
e2489bd9ef063ae006feaebc3318ffa4143f6e16vboxsync */
e2489bd9ef063ae006feaebc3318ffa4143f6e16vboxsync unsigned i = pPool->aiHash[PGMPOOL_HASH(GCPhys)];
e2489bd9ef063ae006feaebc3318ffa4143f6e16vboxsync Log3(("pgmPoolCacheAlloc: %RGp kind %s iUser=%x iUserTable=%x SLOT=%d\n", GCPhys, pgmPoolPoolKindToStr(enmKind), iUser, iUserTable, i));
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync if (i != NIL_PGMPOOL_IDX)
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync {
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync do
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync {
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync PPGMPOOLPAGE pPage = &pPool->aPages[i];
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync Log4(("pgmPoolCacheAlloc: slot %d found page %RGp\n", i, pPage->GCPhys));
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync if (pPage->GCPhys == GCPhys)
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync {
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync if ((PGMPOOLKIND)pPage->enmKind == enmKind)
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync {
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync /* Put it at the start of the use list to make sure pgmPoolTrackAddUser
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync * doesn't flush it in case there are no more free use records.
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync */
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync pgmPoolCacheUsed(pPool, pPage);
6b9d50a0f466bd5a61458ed53925480ab28a3c17vboxsync
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync int rc = pgmPoolTrackAddUser(pPool, pPage, iUser, iUserTable);
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync if (RT_SUCCESS(rc))
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync {
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync Assert((PGMPOOLKIND)pPage->enmKind == enmKind);
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync *ppPage = pPage;
6b9d50a0f466bd5a61458ed53925480ab28a3c17vboxsync STAM_COUNTER_INC(&pPool->StatCacheHits);
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync return VINF_PGM_CACHED_PAGE;
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync }
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync return rc;
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync }
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync /*
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync * The kind is different. In some cases we should now flush the page
6b9d50a0f466bd5a61458ed53925480ab28a3c17vboxsync * as it has been reused, but in most cases this is normal remapping
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync * of PDs as PT or big pages using the GCPhys field in a slightly
6b9d50a0f466bd5a61458ed53925480ab28a3c17vboxsync * different way than the other kinds.
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync if (pgmPoolCacheReusedByKind((PGMPOOLKIND)pPage->enmKind, enmKind))
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync {
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync STAM_COUNTER_INC(&pPool->StatCacheKindMismatches);
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync pgmPoolFlushPage(pPool, pPage);
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync PGM_INVL_GUEST_TLBS(); /* see PT handler. */
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync break;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync }
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync }
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync /* next */
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync i = pPage->iNext;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync } while (i != NIL_PGMPOOL_IDX);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync Log3(("pgmPoolCacheAlloc: Missed GCPhys=%RGp enmKind=%s\n", GCPhys, pgmPoolPoolKindToStr(enmKind)));
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync STAM_COUNTER_INC(&pPool->StatCacheMisses);
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync return VERR_FILE_NOT_FOUND;
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync}
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync/**
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync * Inserts a page into the cache.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync *
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param pPool The pool.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param pPage The cached page.
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync * @param fCanBeCached Set if the page is fit for caching from the caller's point of view.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync */
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsyncstatic void pgmPoolCacheInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, bool fCanBeCached)
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync{
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync /*
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync * Insert into the GCPhys hash if the page is fit for that.
ccbdc11833996cb9f3be7868f1ebaefcacafb94dvboxsync */
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync Assert(!pPage->fCached);
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync if (fCanBeCached)
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync {
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync pPage->fCached = true;
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync pgmPoolHashInsert(pPool, pPage);
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync Log3(("pgmPoolCacheInsert: Caching %p:{.Core=%RHp, .idx=%d, .enmKind=%s, GCPhys=%RGp}\n",
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), pPage->GCPhys));
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync STAM_COUNTER_INC(&pPool->StatCacheCacheable);
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync }
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync else
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync {
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync Log3(("pgmPoolCacheInsert: Not caching %p:{.Core=%RHp, .idx=%d, .enmKind=%s, GCPhys=%RGp}\n",
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), pPage->GCPhys));
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync STAM_COUNTER_INC(&pPool->StatCacheUncacheable);
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync }
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync /*
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync * Insert at the head of the age list.
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync */
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync pPage->iAgePrev = NIL_PGMPOOL_IDX;
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync pPage->iAgeNext = pPool->iAgeHead;
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync if (pPool->iAgeHead != NIL_PGMPOOL_IDX)
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync pPool->aPages[pPool->iAgeHead].iAgePrev = pPage->idx;
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync else
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync pPool->iAgeTail = pPage->idx;
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync pPool->iAgeHead = pPage->idx;
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync}
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync/**
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * Flushes a cached page.
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync *
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * @param pPool The pool.
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * @param pPage The cached page.
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync */
687794577e2e35c3cae67e692a7f2130d1262a82vboxsyncstatic void pgmPoolCacheFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync{
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync Log3(("pgmPoolCacheFlushPage: %RGp\n", pPage->GCPhys));
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /*
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Remove the page from the hash.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (pPage->fCached)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync {
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pPage->fCached = false;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pgmPoolHashRemove(pPool, pPage);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync }
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync else
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync Assert(pPage->iNext == NIL_PGMPOOL_IDX);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /*
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Remove it from the age list.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (pPage->iAgeNext != NIL_PGMPOOL_IDX)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pPool->aPages[pPage->iAgeNext].iAgePrev = pPage->iAgePrev;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync else
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pPool->iAgeTail = pPage->iAgePrev;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (pPage->iAgePrev != NIL_PGMPOOL_IDX)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pPool->aPages[pPage->iAgePrev].iAgeNext = pPage->iAgeNext;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync else
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pPool->iAgeHead = pPage->iAgeNext;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pPage->iAgeNext = NIL_PGMPOOL_IDX;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pPage->iAgePrev = NIL_PGMPOOL_IDX;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync}
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#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.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsyncstatic PPGMPOOLPAGE pgmPoolMonitorGetPageByGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pNewPage)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync{
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#ifdef PGMPOOL_WITH_CACHE
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync /*
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * Look up the GCPhys in the hash.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync RTGCPHYS GCPhys = pNewPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync unsigned i = pPool->aiHash[PGMPOOL_HASH(GCPhys)];
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if (i == NIL_PGMPOOL_IDX)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync return NULL;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync do
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync PPGMPOOLPAGE pPage = &pPool->aPages[i];
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if ( pPage->GCPhys - GCPhys < PAGE_SIZE
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync && pPage != pNewPage)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync switch (pPage->enmKind)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync 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_PD0_FOR_32BIT_PD:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PD3_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 case PGMPOOLKIND_32BIT_PD:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PDPT:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync /* find the head */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync while (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync Assert(pPage->iMonitoredPrev != pPage->idx);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage = &pPool->aPages[pPage->iMonitoredPrev];
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync return pPage;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync /* ignore, no monitoring. */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync 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:
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync break;
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync default:
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync AssertFatalMsgFailed(("enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync }
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync }
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync /* next */
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync i = pPage->iNext;
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync } while (i != NIL_PGMPOOL_IDX);
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync#endif
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync return NULL;
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync}
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync/**
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync * Enabled write monitoring of a guest page.
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync *
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync * @returns VBox status code.
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync * @retval VINF_SUCCESS on success.
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync * @param pPool The pool.
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync * @param pPage The cached page.
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync */
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsyncstatic int pgmPoolMonitorInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync{
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync LogFlow(("pgmPoolMonitorInsert %RGp\n", pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1)));
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync /*
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync * Filter out the relevant kinds.
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync */
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync switch (pPage->enmKind)
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync {
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_64BIT_PML4:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync case PGMPOOLKIND_32BIT_PD:
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync case PGMPOOLKIND_PAE_PDPT:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync break;
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync case PGMPOOLKIND_ROOT_NESTED:
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync /* Nothing to monitor here. */
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync return VINF_SUCCESS;
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync case PGMPOOLKIND_PAE_PD_PHYS:
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync /* Nothing to monitor here. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync return VINF_SUCCESS;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync#ifdef PGMPOOL_WITH_MIXED_PT_CR3
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync break;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync#else
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#endif
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync default:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync }
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync /*
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * Install handler.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync int rc;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync PPGMPOOLPAGE pPageHead = pgmPoolMonitorGetPageByGCPhys(pPool, pPage);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync if (pPageHead)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync {
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync Assert(pPageHead != pPage); Assert(pPageHead->iMonitoredNext != pPage->idx);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync Assert(pPageHead->iMonitoredPrev != pPage->idx);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPage->iMonitoredPrev = pPageHead->idx;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPage->iMonitoredNext = pPageHead->iMonitoredNext;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync if (pPageHead->iMonitoredNext != NIL_PGMPOOL_IDX)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPool->aPages[pPageHead->iMonitoredNext].iMonitoredPrev = pPage->idx;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPageHead->iMonitoredNext = pPage->idx;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync rc = VINF_SUCCESS;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync }
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync else
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync {
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync Assert(pPage->iMonitoredNext == NIL_PGMPOOL_IDX); Assert(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync PVM pVM = pPool->CTX_SUFF(pVM);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync const RTGCPHYS GCPhysPage = pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync rc = PGMHandlerPhysicalRegisterEx(pVM, PGMPHYSHANDLERTYPE_PHYSICAL_WRITE,
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync GCPhysPage, GCPhysPage + (PAGE_SIZE - 1),
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pPage),
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pPage),
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPool->pfnAccessHandlerRC, MMHyperCCToRC(pVM, pPage),
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPool->pszAccessHandler);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync /** @todo we should probably deal with out-of-memory conditions here, but for now increasing
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * the heap size should suffice. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync AssertFatalMsgRC(rc, ("PGMHandlerPhysicalRegisterEx %RGp failed with %Rrc\n", GCPhysPage, rc));
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync Assert(!(pVM->pgm.s.fGlobalSyncFlags & PGM_GLOBAL_SYNC_CLEAR_PGM_POOL) || VMCPU_FF_ISSET(VMMGetCpu(pVM), VMCPU_FF_PGM_SYNC_CR3));
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync }
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPage->fMonitored = true;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync return rc;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync}
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync/**
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * Disables write monitoring of a guest page.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync *
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @returns VBox status code.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @retval VINF_SUCCESS on success.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @param pPool The pool.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @param pPage The cached page.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsyncstatic int pgmPoolMonitorFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync{
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync /*
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * Filter out the relevant kinds.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync switch (pPage->enmKind)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync {
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync 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:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_64BIT_PML4:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_32BIT_PD:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PDPT:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync break;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_ROOT_NESTED:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PD_PHYS:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync /* Nothing to monitor here. */
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync return VINF_SUCCESS;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync#ifdef PGMPOOL_WITH_MIXED_PT_CR3
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync break;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync#endif
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync default:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync /*
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * Remove the page from the monitored list or uninstall it if last.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync const PVM pVM = pPool->CTX_SUFF(pVM);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync int rc;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if ( pPage->iMonitoredNext != NIL_PGMPOOL_IDX
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync || pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if (pPage->iMonitoredPrev == NIL_PGMPOOL_IDX)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync PPGMPOOLPAGE pNewHead = &pPool->aPages[pPage->iMonitoredNext];
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pNewHead->iMonitoredPrev = NIL_PGMPOOL_IDX;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync rc = PGMHandlerPhysicalChangeCallbacks(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1),
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pNewHead),
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pNewHead),
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->pfnAccessHandlerRC, MMHyperCCToRC(pVM, pNewHead),
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->pszAccessHandler);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync AssertFatalRCSuccess(rc);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync else
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->aPages[pPage->iMonitoredPrev].iMonitoredNext = pPage->iMonitoredNext;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if (pPage->iMonitoredNext != NIL_PGMPOOL_IDX)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->aPages[pPage->iMonitoredNext].iMonitoredPrev = pPage->iMonitoredPrev;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync rc = VINF_SUCCESS;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync else
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync rc = PGMHandlerPhysicalDeregister(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1));
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync AssertFatalRC(rc);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync AssertMsg(!(pVM->pgm.s.fGlobalSyncFlags & PGM_GLOBAL_SYNC_CLEAR_PGM_POOL) || VMCPU_FF_ISSET(VMMGetCpu(pVM), VMCPU_FF_PGM_SYNC_CR3),
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync ("%#x %#x\n", pVM->pgm.s.fGlobalSyncFlags, pVM->fGlobalForcedActions));
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage->fMonitored = false;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync /*
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * Remove it from the list of modified pages (if in it).
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pgmPoolMonitorModifiedRemove(pPool, pPage);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync return rc;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync}
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync/**
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * Inserts the page into the list of modified pages.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync *
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param pPool The pool.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param pPage The page.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsyncvoid pgmPoolMonitorModifiedInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync{
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync Log3(("pgmPoolMonitorModifiedInsert: idx=%d\n", pPage->idx));
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync AssertMsg( pPage->iModifiedNext == NIL_PGMPOOL_IDX
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync && pPage->iModifiedPrev == NIL_PGMPOOL_IDX
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync && pPool->iModifiedHead != pPage->idx,
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync ("Next=%d Prev=%d idx=%d cModifications=%d Head=%d cModifiedPages=%d\n",
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage->iModifiedNext, pPage->iModifiedPrev, pPage->idx, pPage->cModifications,
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->iModifiedHead, pPool->cModifiedPages));
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage->iModifiedNext = pPool->iModifiedHead;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if (pPool->iModifiedHead != NIL_PGMPOOL_IDX)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->aPages[pPool->iModifiedHead].iModifiedPrev = pPage->idx;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->iModifiedHead = pPage->idx;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->cModifiedPages++;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#ifdef VBOX_WITH_STATISTICS
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync if (pPool->cModifiedPages > pPool->cModifiedPagesHigh)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->cModifiedPagesHigh = pPool->cModifiedPages;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#endif
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync}
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync/**
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * Removes the page from the list of modified pages and resets the
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * moficiation counter.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync *
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param pPool The pool.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param pPage The page which is believed to be in the list of modified pages.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsyncstatic void pgmPoolMonitorModifiedRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync{
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync Log3(("pgmPoolMonitorModifiedRemove: idx=%d cModifications=%d\n", pPage->idx, pPage->cModifications));
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if (pPool->iModifiedHead == pPage->idx)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->iModifiedHead = pPage->iModifiedNext;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if (pPage->iModifiedNext != NIL_PGMPOOL_IDX)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->aPages[pPage->iModifiedNext].iModifiedPrev = NIL_PGMPOOL_IDX;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage->iModifiedNext = NIL_PGMPOOL_IDX;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->cModifiedPages--;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync else if (pPage->iModifiedPrev != NIL_PGMPOOL_IDX)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->aPages[pPage->iModifiedPrev].iModifiedNext = pPage->iModifiedNext;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if (pPage->iModifiedNext != NIL_PGMPOOL_IDX)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->aPages[pPage->iModifiedNext].iModifiedPrev = pPage->iModifiedPrev;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage->iModifiedNext = NIL_PGMPOOL_IDX;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->cModifiedPages--;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync else
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage->cModifications = 0;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync}
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync/**
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * Zaps the list of modified pages, resetting their modification counters in the process.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync *
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param pVM The VM handle.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsyncvoid pgmPoolMonitorModifiedClearAll(PVM pVM)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync{
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pgmLock(pVM);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync LogFlow(("pgmPoolMonitorModifiedClearAll: cModifiedPages=%d\n", pPool->cModifiedPages));
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync unsigned cPages = 0; NOREF(cPages);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync uint16_t idx = pPool->iModifiedHead;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->iModifiedHead = NIL_PGMPOOL_IDX;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync while (idx != NIL_PGMPOOL_IDX)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync PPGMPOOLPAGE pPage = &pPool->aPages[idx];
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync idx = pPage->iModifiedNext;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage->iModifiedNext = NIL_PGMPOOL_IDX;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPage->cModifications = 0;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync Assert(++cPages);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync AssertMsg(cPages == pPool->cModifiedPages, ("%d != %d\n", cPages, pPool->cModifiedPages));
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPool->cModifiedPages = 0;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pgmUnlock(pVM);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync}
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync#ifdef IN_RING3
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync/**
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * Callback to clear all shadow pages and clear all modification counters.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync *
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @returns VBox status code.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param pVM The VM handle.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @param pvUser Unused parameter
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * @remark Should only be used when monitoring is available, thus placed in
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync * the PGMPOOL_WITH_MONITORING #ifdef.
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsyncDECLCALLBACK(int) pgmPoolClearAll(PVM pVM, void *pvUser)
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync{
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync NOREF(pvUser);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync STAM_PROFILE_START(&pPool->StatClearAll, c);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync LogFlow(("pgmPoolClearAll: cUsedPages=%d\n", pPool->cUsedPages));
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pgmLock(pVM);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync /*
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Iterate all the pages until we've encountered all that in use.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * This is simple but not quite optimal solution.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync unsigned cModifiedPages = 0; NOREF(cModifiedPages);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync unsigned cLeft = pPool->cUsedPages;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync unsigned iPage = pPool->cCurPages;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync while (--iPage >= PGMPOOL_IDX_FIRST)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync if (pPage->GCPhys != NIL_RTGCPHYS)
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync {
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync switch (pPage->enmKind)
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync {
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync /*
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * We only care about shadow page tables.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync {
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync#ifdef PGMPOOL_WITH_USER_TRACKING
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync if (pPage->cPresent)
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync#endif
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync {
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync void *pvShw = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync STAM_PROFILE_START(&pPool->StatZeroPage, z);
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync ASMMemZeroPage(pvShw);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync#ifdef PGMPOOL_WITH_USER_TRACKING
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync pPage->cPresent = 0;
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync pPage->iFirstPresent = ~0;
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync#endif
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync }
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync /* fall thru */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync default:
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync Assert(!pPage->cModifications || ++cModifiedPages);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(pPage->iModifiedNext == NIL_PGMPOOL_IDX || pPage->cModifications);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX || pPage->cModifications);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pPage->iModifiedNext = NIL_PGMPOOL_IDX;
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pPage->cModifications = 0;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync if (!--cLeft)
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync break;
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync }
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync }
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync /* swipe the special pages too. */
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync for (iPage = PGMPOOL_IDX_FIRST_SPECIAL; iPage < PGMPOOL_IDX_FIRST; iPage++)
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync {
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync if (pPage->GCPhys != NIL_RTGCPHYS)
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(!pPage->cModifications || ++cModifiedPages);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(pPage->iModifiedNext == NIL_PGMPOOL_IDX || pPage->cModifications);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX || pPage->cModifications);
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync pPage->iModifiedNext = NIL_PGMPOOL_IDX;
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync pPage->cModifications = 0;
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync }
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync }
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync#ifndef DEBUG_michael
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync AssertMsg(cModifiedPages == pPool->cModifiedPages, ("%d != %d\n", cModifiedPages, pPool->cModifiedPages));
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync#endif
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync pPool->iModifiedHead = NIL_PGMPOOL_IDX;
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync pPool->cModifiedPages = 0;
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync
c99b597540585068d22dde4c9f74730305f24097vboxsync#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync /*
c99b597540585068d22dde4c9f74730305f24097vboxsync * Clear all the GCPhys links and rebuild the phys ext free list.
c99b597540585068d22dde4c9f74730305f24097vboxsync */
c99b597540585068d22dde4c9f74730305f24097vboxsync for (PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
c99b597540585068d22dde4c9f74730305f24097vboxsync pRam;
c99b597540585068d22dde4c9f74730305f24097vboxsync pRam = pRam->CTX_SUFF(pNext))
c99b597540585068d22dde4c9f74730305f24097vboxsync {
c99b597540585068d22dde4c9f74730305f24097vboxsync unsigned iPage = pRam->cb >> PAGE_SHIFT;
c99b597540585068d22dde4c9f74730305f24097vboxsync while (iPage-- > 0)
c99b597540585068d22dde4c9f74730305f24097vboxsync PGM_PAGE_SET_TRACKING(&pRam->aPages[iPage], 0);
c99b597540585068d22dde4c9f74730305f24097vboxsync }
c99b597540585068d22dde4c9f74730305f24097vboxsync
c99b597540585068d22dde4c9f74730305f24097vboxsync pPool->iPhysExtFreeHead = 0;
c99b597540585068d22dde4c9f74730305f24097vboxsync PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync const unsigned cMaxPhysExts = pPool->cMaxPhysExts;
c99b597540585068d22dde4c9f74730305f24097vboxsync for (unsigned i = 0; i < cMaxPhysExts; i++)
c99b597540585068d22dde4c9f74730305f24097vboxsync {
c99b597540585068d22dde4c9f74730305f24097vboxsync paPhysExts[i].iNext = i + 1;
c99b597540585068d22dde4c9f74730305f24097vboxsync paPhysExts[i].aidx[0] = NIL_PGMPOOL_IDX;
c99b597540585068d22dde4c9f74730305f24097vboxsync paPhysExts[i].aidx[1] = NIL_PGMPOOL_IDX;
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync paPhysExts[i].aidx[2] = NIL_PGMPOOL_IDX;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync paPhysExts[cMaxPhysExts - 1].iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#endif
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pPool->cPresent = 0;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pgmUnlock(pVM);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PGM_INVL_GUEST_TLBS();
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_PROFILE_STOP(&pPool->StatClearAll, c);
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync return VINF_SUCCESS;
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync}
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync#endif /* IN_RING3 */
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync/**
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync * Handle SyncCR3 pool tasks
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync *
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync * @returns VBox status code.
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync * @retval VINF_SUCCESS if successfully added.
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync * @retval VINF_PGM_SYNC_CR3 is it needs to be deferred to ring 3 (GC only)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pVM The VM handle.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @remark Should only be used when monitoring is available, thus placed in
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * the PGMPOOL_WITH_MONITORING #ifdef.
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync */
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsyncint pgmPoolSyncCR3(PVM pVM)
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync{
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync LogFlow(("pgmPoolSyncCR3\n"));
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync /*
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync * When monitoring shadowed pages, we reset the modification counters on CR3 sync.
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync * Occasionally we will have to clear all the shadow page tables because we wanted
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync * to monitor a page which was mapped by too many shadowed page tables. This operation
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync * sometimes refered to as a 'lightweight flush'.
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync */
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync# ifdef IN_RING3 /* Don't flush in ring-0 or raw mode, it's taking too long. */
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync if (ASMBitTestAndClear(&pVM->pgm.s.fGlobalSyncFlags, PGM_GLOBAL_SYNC_CLEAR_PGM_POOL_BIT))
c99b597540585068d22dde4c9f74730305f24097vboxsync {
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync VMMR3AtomicExecuteHandler(pVM, pgmPoolClearAll, NULL);
c99b597540585068d22dde4c9f74730305f24097vboxsync# else /* !IN_RING3 */
c99b597540585068d22dde4c9f74730305f24097vboxsync if (pVM->pgm.s.fGlobalSyncFlags & PGM_GLOBAL_SYNC_CLEAR_PGM_POOL)
c99b597540585068d22dde4c9f74730305f24097vboxsync {
02651f98b4320e70a300ba1ebe95270096ebfd4dvboxsync LogFlow(("SyncCR3: PGM_GLOBAL_SYNC_CLEAR_PGM_POOL is set -> VINF_PGM_SYNC_CR3\n"));
c99b597540585068d22dde4c9f74730305f24097vboxsync VMCPU_FF_SET(VMMGetCpu(pVM), VMCPU_FF_PGM_SYNC_CR3); /** @todo no need to do global sync, right? */
c99b597540585068d22dde4c9f74730305f24097vboxsync return VINF_PGM_SYNC_CR3;
c99b597540585068d22dde4c9f74730305f24097vboxsync# endif /* !IN_RING3 */
c99b597540585068d22dde4c9f74730305f24097vboxsync }
c99b597540585068d22dde4c9f74730305f24097vboxsync else
c99b597540585068d22dde4c9f74730305f24097vboxsync pgmPoolMonitorModifiedClearAll(pVM);
c99b597540585068d22dde4c9f74730305f24097vboxsync
c99b597540585068d22dde4c9f74730305f24097vboxsync return VINF_SUCCESS;
c99b597540585068d22dde4c9f74730305f24097vboxsync}
c99b597540585068d22dde4c9f74730305f24097vboxsync
0fd108a555ae02f2fb557d5f2c40281999b60d15vboxsync#endif /* PGMPOOL_WITH_MONITORING */
c99b597540585068d22dde4c9f74730305f24097vboxsync#ifdef PGMPOOL_WITH_USER_TRACKING
c99b597540585068d22dde4c9f74730305f24097vboxsync
c99b597540585068d22dde4c9f74730305f24097vboxsync/**
c99b597540585068d22dde4c9f74730305f24097vboxsync * Frees up at least one user entry.
c99b597540585068d22dde4c9f74730305f24097vboxsync *
c99b597540585068d22dde4c9f74730305f24097vboxsync * @returns VBox status code.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @retval VINF_SUCCESS if successfully added.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pPool The pool.
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync * @param iUser The user index.
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsyncstatic int pgmPoolTrackFreeOneUser(PPGMPOOL pPool, uint16_t iUser)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync{
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_COUNTER_INC(&pPool->StatTrackFreeUpOneUser);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#ifdef PGMPOOL_WITH_CACHE
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync /*
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * Just free cached pages in a braindead fashion.
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync */
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync /** @todo walk the age list backwards and free the first with usage. */
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync int rc = VINF_SUCCESS;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync do
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync int rc2 = pgmPoolCacheFreeOne(pPool, iUser);
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync if (RT_FAILURE(rc2) && rc == VINF_SUCCESS)
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync rc = rc2;
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync } while (pPool->iUserFreeHead == NIL_PGMPOOL_USER_INDEX);
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync return rc;
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync#else
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync /*
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync * Lazy approach.
5366e994777f9d9391cf809dc77610f57270d75dvboxsync */
5366e994777f9d9391cf809dc77610f57270d75dvboxsync /* @todo This path no longer works (CR3 root pages will be flushed)!! */
5366e994777f9d9391cf809dc77610f57270d75dvboxsync AssertCompileFailed();
5366e994777f9d9391cf809dc77610f57270d75dvboxsync Assert(!CPUMIsGuestInLongMode(pVM));
5366e994777f9d9391cf809dc77610f57270d75dvboxsync pgmPoolFlushAllInt(pPool);
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync return VERR_PGM_POOL_FLUSHED;
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync#endif
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync}
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync/**
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync * Inserts a page into the cache.
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync *
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync * This will create user node for the page, insert it into the GCPhys
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync * hash, and insert it into the age list.
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync *
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @returns VBox status code.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @retval VINF_SUCCESS if successfully added.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync * @param pPool The pool.
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync * @param pPage The cached page.
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync * @param GCPhys The GC physical address of the page we're gonna shadow.
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync * @param iUser The user index.
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync * @param iUserTable The user table index.
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync */
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsyncDECLINLINE(int) pgmPoolTrackInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhys, uint16_t iUser, uint32_t iUserTable)
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync{
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync int rc = VINF_SUCCESS;
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync LogFlow(("pgmPoolTrackInsert GCPhys=%RGp iUser %x iUserTable %x\n", GCPhys, iUser, iUserTable));
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync#ifdef VBOX_STRICT
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync /*
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync * Check that the entry doesn't already exists.
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync */
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync if (pPage->iUserHead != NIL_PGMPOOL_USER_INDEX)
35e6d303696e46d969aaf9a59cc381333a483b0bvboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uint16_t i = pPage->iUserHead;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync do
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
3641904508e6a0671c21a083a2ed03ebad8d976cvboxsync Assert(i < pPool->cMaxUsers);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync AssertMsg(paUsers[i].iUser != iUser || paUsers[i].iUserTable != iUserTable, ("%x %x vs new %x %x\n", paUsers[i].iUser, paUsers[i].iUserTable, iUser, iUserTable));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync i = paUsers[i].iNext;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync } while (i != NIL_PGMPOOL_USER_INDEX);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync#endif
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
36f3c24e4ad9c6b813767db1faeabbe7e2ecc057vboxsync /*
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * Find free a user node.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync uint16_t i = pPool->iUserFreeHead;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if (i == NIL_PGMPOOL_USER_INDEX)
36f3c24e4ad9c6b813767db1faeabbe7e2ecc057vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync int rc = pgmPoolTrackFreeOneUser(pPool, iUser);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (RT_FAILURE(rc))
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync return rc;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync i = pPool->iUserFreeHead;
36f3c24e4ad9c6b813767db1faeabbe7e2ecc057vboxsync }
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync /*
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * Unlink the user node from the free list,
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * initialize and insert it into the user list.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync */
36f3c24e4ad9c6b813767db1faeabbe7e2ecc057vboxsync pPool->iUserFreeHead = paUsers[i].iNext;
ca3db470494a8b6eaec69ea37468a5cda65e2da8vboxsync paUsers[i].iNext = NIL_PGMPOOL_USER_INDEX;
36f3c24e4ad9c6b813767db1faeabbe7e2ecc057vboxsync paUsers[i].iUser = iUser;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync paUsers[i].iUserTable = iUserTable;
36f3c24e4ad9c6b813767db1faeabbe7e2ecc057vboxsync pPage->iUserHead = i;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync /*
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync * Insert into cache and enable monitoring of the guest page if enabled.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync *
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * Until we implement caching of all levels, including the CR3 one, we'll
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * have to make sure we don't try monitor & cache any recursive reuse of
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * a monitored CR3 page. Because all windows versions are doing this we'll
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * have to be able to do combined access monitoring, CR3 + PT and
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * PD + PT (guest PAE).
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync *
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * Update:
36f3c24e4ad9c6b813767db1faeabbe7e2ecc057vboxsync * We're now cooperating with the CR3 monitor if an uncachable page is found.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync#if defined(PGMPOOL_WITH_MONITORING) || defined(PGMPOOL_WITH_CACHE)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# ifdef PGMPOOL_WITH_MIXED_PT_CR3
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync const bool fCanBeMonitored = true;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# else
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync bool fCanBeMonitored = pPool->CTX_SUFF(pVM)->pgm.s.GCPhysGstCR3Monitored == NIL_RTGCPHYS
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync || (GCPhys & X86_PTE_PAE_PG_MASK) != (pPool->CTX_SUFF(pVM)->pgm.s.GCPhysGstCR3Monitored & X86_PTE_PAE_PG_MASK)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync || pgmPoolIsBigPage((PGMPOOLKIND)pPage->enmKind);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# endif
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# ifdef PGMPOOL_WITH_CACHE
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync pgmPoolCacheInsert(pPool, pPage, fCanBeMonitored); /* This can be expanded. */
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync# endif
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync if (fCanBeMonitored)
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync {
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync# ifdef PGMPOOL_WITH_MONITORING
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync rc = pgmPoolMonitorInsert(pPool, pPage);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync AssertRC(rc);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# endif
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync#endif /* PGMPOOL_WITH_MONITORING */
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync return rc;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync}
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync# ifdef PGMPOOL_WITH_CACHE /* (only used when the cache is enabled.) */
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync/**
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync * Adds a user reference to a page.
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync *
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync * This will move the page to the head of the
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync *
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync * @returns VBox status code.
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync * @retval VINF_SUCCESS if successfully added.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pPool The pool.
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync * @param pPage The cached page.
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync * @param iUser The user index.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param iUserTable The user table.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync */
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsyncstatic int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync{
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync Log3(("pgmPoolTrackAddUser GCPhys = %RGp iUser %x iUserTable %x\n", pPage->GCPhys, iUser, iUserTable));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# ifdef VBOX_STRICT
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /*
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync * Check that the entry doesn't already exists. We only allow multiple users of top-level paging structures (SHW_POOL_ROOT_IDX).
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (pPage->iUserHead != NIL_PGMPOOL_USER_INDEX)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uint16_t i = pPage->iUserHead;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync do
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync {
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync Assert(i < pPool->cMaxUsers);
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync AssertMsg(iUser != PGMPOOL_IDX_PD || iUser != PGMPOOL_IDX_PDPT || iUser != PGMPOOL_IDX_NESTED_ROOT || iUser != PGMPOOL_IDX_AMD64_CR3 ||
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync paUsers[i].iUser != iUser || paUsers[i].iUserTable != iUserTable, ("%x %x vs new %x %x\n", paUsers[i].iUser, paUsers[i].iUserTable, iUser, iUserTable));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync i = paUsers[i].iNext;
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync } while (i != NIL_PGMPOOL_USER_INDEX);
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# endif
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /*
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync * Allocate a user node.
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync */
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync uint16_t i = pPool->iUserFreeHead;
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync if (i == NIL_PGMPOOL_USER_INDEX)
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync {
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync int rc = pgmPoolTrackFreeOneUser(pPool, iUser);
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync if (RT_FAILURE(rc))
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync return rc;
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync i = pPool->iUserFreeHead;
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync }
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync pPool->iUserFreeHead = paUsers[i].iNext;
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync /*
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync * Initialize the user node and insert it.
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync */
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync paUsers[i].iNext = pPage->iUserHead;
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync paUsers[i].iUser = iUser;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync paUsers[i].iUserTable = iUserTable;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pPage->iUserHead = i;
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync# ifdef PGMPOOL_WITH_CACHE
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync /*
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync * Tell the cache to update its replacement stats for this page.
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync */
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync pgmPoolCacheUsed(pPool, pPage);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync# endif
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync return VINF_SUCCESS;
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync}
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync# endif /* PGMPOOL_WITH_CACHE */
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync/**
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync * Frees a user record associated with a page.
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync *
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync * This does not clear the entry in the user table, it simply replaces the
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync * user record to the chain of free records.
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pPool The pool.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param HCPhys The HC physical address of the shadow page.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param iUser The shadow page pool index of the user table.
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync * @param iUserTable The index into the user table (shadowed).
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsyncstatic void pgmPoolTrackFreeUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync{
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /*
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * Unlink and free the specified user entry.
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync */
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync Log3(("pgmPoolTrackFreeUser %RGp %x %x\n", pPage->GCPhys, iUser, iUserTable));
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync /* Special: For PAE and 32-bit paging, there is usually no more than one user. */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uint16_t i = pPage->iUserHead;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if ( i != NIL_PGMPOOL_USER_INDEX
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync && paUsers[i].iUser == iUser
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync && paUsers[i].iUserTable == iUserTable)
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pPage->iUserHead = paUsers[i].iNext;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync paUsers[i].iUser = NIL_PGMPOOL_IDX;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync paUsers[i].iNext = pPool->iUserFreeHead;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync pPool->iUserFreeHead = i;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync return;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync }
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync /* General: Linear search. */
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync uint16_t iPrev = NIL_PGMPOOL_USER_INDEX;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync while (i != NIL_PGMPOOL_USER_INDEX)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if ( paUsers[i].iUser == iUser
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync && paUsers[i].iUserTable == iUserTable)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if (iPrev != NIL_PGMPOOL_USER_INDEX)
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync paUsers[iPrev].iNext = paUsers[i].iNext;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync else
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync pPage->iUserHead = paUsers[i].iNext;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync paUsers[i].iUser = NIL_PGMPOOL_IDX;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync paUsers[i].iNext = pPool->iUserFreeHead;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync pPool->iUserFreeHead = i;
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync return;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync iPrev = i;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync i = paUsers[i].iNext;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync }
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync /* Fatal: didn't find it */
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync AssertFatalMsgFailed(("Didn't find the user entry! iUser=%#x iUserTable=%#x GCPhys=%RGp\n",
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync iUser, iUserTable, pPage->GCPhys));
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync}
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync/**
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync * Gets the entry size of a shadow table.
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync *
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync * @param enmKind The kind of page.
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @returns The size of the entry in bytes. That is, 4 or 8.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @returns If the kind is not for a table, an assertion is raised and 0 is
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * returned.
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsyncDECLINLINE(unsigned) pgmPoolTrackGetShadowEntrySize(PGMPOOLKIND enmKind)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync{
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync switch (enmKind)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_32BIT_PD:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync return 4;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_64BIT_PML4:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PDPT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_ROOT_NESTED:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PD_PHYS:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync return 8;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync default:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync AssertFatalMsgFailed(("enmKind=%d\n", enmKind));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync}
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync/**
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Gets the entry size of a guest table.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param enmKind The kind of page.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * @returns The size of the entry in bytes. That is, 0, 4 or 8.
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * @returns If the kind is not for a table, an assertion is raised and 0 is
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * returned.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsyncDECLINLINE(unsigned) pgmPoolTrackGetGuestEntrySize(PGMPOOLKIND enmKind)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync{
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync switch (enmKind)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_32BIT_PD:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync return 4;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_64BIT_PML4:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_PAE_PDPT:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync return 8;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_ROOT_NESTED:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_PAE_PD_PHYS:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync /** @todo can we return 0? (nobody is calling this...) */
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync AssertFailed();
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync return 0;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync default:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync AssertFatalMsgFailed(("enmKind=%d\n", enmKind));
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync }
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync}
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/**
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Scans one shadow page table for mappings of a physical page.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync * @param pVM The VM handle.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pPhysPage The guest page in question.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param iShw The shadow page table.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param cRefs The number of references made in that PT.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsyncstatic void pgmPoolTrackFlushGCPhysPTInt(PVM pVM, PCPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs)
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync{
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync LogFlow(("pgmPoolTrackFlushGCPhysPT: pPhysPage=%R[pgmpage] iShw=%d cRefs=%d\n", pPhysPage, iShw, cRefs));
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /*
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Assert sanity.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync Assert(cRefs == 1);
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync AssertFatalMsg(iShw < pPool->cCurPages && iShw != NIL_PGMPOOL_IDX, ("iShw=%d\n", iShw));
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync PPGMPOOLPAGE pPage = &pPool->aPages[iShw];
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync /*
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * Then, clear the actual mappings to the page in the shadow PT.
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync switch (pPage->enmKind)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync const uint32_t u32 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PX86PT pPT = (PX86PT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX32 cRefs=%#x\n", i, pPT->a[i], cRefs));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pPT->a[i].u = 0;
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync cRefs--;
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync if (!cRefs)
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync return;
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync }
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync#ifdef LOG_ENABLED
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPT->a); i++)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
92e624e40b06b4dc6d0a8222e1de33bd3e879a63vboxsync RTLogPrintf("i=%d cRefs=%d\n", i, cRefs--);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pPT->a[i].u = 0;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#endif
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync break;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PX86PTPAE pPT = (PX86PTPAE)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_PAE_PG_MASK | X86_PTE_P)) == u64)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX64 cRefs=%#x\n", i, pPT->a[i], cRefs));
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync pPT->a[i].u = 0;
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync cRefs--;
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync if (!cRefs)
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync return;
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#ifdef LOG_ENABLED
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPT->a); i++)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync RTLogPrintf("i=%d cRefs=%d\n", i, cRefs--);
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync pPT->a[i].u = 0;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync#endif
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d u64=%RX64\n", cRefs, pPage->iFirstPresent, pPage->cPresent, u64));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync break;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync {
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync PEPTPT pPT = (PEPTPT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync if ((pPT->a[i].u & (EPT_PTE_PG_MASK | X86_PTE_P)) == u64)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX64 cRefs=%#x\n", i, pPT->a[i], cRefs));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pPT->a[i].u = 0;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync cRefs--;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if (!cRefs)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync return;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync#ifdef LOG_ENABLED
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPT->a); i++)
58c0567dee3cc3ebe62dec1e27f8e35bac4ddeb0vboxsync 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;
36f3c24e4ad9c6b813767db1faeabbe7e2ecc057vboxsync }
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync#endif
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync break;
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync }
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync default:
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync AssertFatalMsgFailed(("enmKind=%d iShw=%d\n", pPage->enmKind, iShw));
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync }
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync}
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync/**
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * Scans one shadow page table for mappings of a physical page.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync *
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pVM The VM handle.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pPhysPage The guest page in question.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param iShw The shadow page table.
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync * @param cRefs The number of references made in that PT.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsyncvoid pgmPoolTrackFlushGCPhysPT(PVM pVM, PPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs)
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync{
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool); NOREF(pPool);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync LogFlow(("pgmPoolTrackFlushGCPhysPT: pPhysPage=%R[pgmpage] iShw=%d cRefs=%d\n", pPhysPage, iShw, cRefs));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPT, f);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pgmPoolTrackFlushGCPhysPTInt(pVM, pPhysPage, iShw, cRefs);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync PGM_PAGE_SET_TRACKING(pPhysPage, 0);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPT, f);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync}
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync/**
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * Flushes a list of shadow page tables mapping the same physical page.
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync *
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync * @param pVM The VM handle.
9e17ca2e9d797e845e3284141dd4086a4b817ae5vboxsync * @param pPhysPage The guest page in question.
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync * @param iPhysExt The physical cross reference extent list to flush.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsyncvoid pgmPoolTrackFlushGCPhysPTs(PVM pVM, PPGMPAGE pPhysPage, uint16_t iPhysExt)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync{
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPTs, f);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync LogFlow(("pgmPoolTrackFlushGCPhysPTs: pPhysPage=%R[pgmpage] iPhysExt\n", pPhysPage, iPhysExt));
5366e994777f9d9391cf809dc77610f57270d75dvboxsync
5366e994777f9d9391cf809dc77610f57270d75dvboxsync const uint16_t iPhysExtStart = iPhysExt;
5366e994777f9d9391cf809dc77610f57270d75dvboxsync PPGMPOOLPHYSEXT pPhysExt;
5366e994777f9d9391cf809dc77610f57270d75dvboxsync do
5366e994777f9d9391cf809dc77610f57270d75dvboxsync {
5366e994777f9d9391cf809dc77610f57270d75dvboxsync Assert(iPhysExt < pPool->cMaxPhysExts);
5366e994777f9d9391cf809dc77610f57270d75dvboxsync pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
5366e994777f9d9391cf809dc77610f57270d75dvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
5366e994777f9d9391cf809dc77610f57270d75dvboxsync if (pPhysExt->aidx[i] != NIL_PGMPOOL_IDX)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pgmPoolTrackFlushGCPhysPTInt(pVM, pPhysPage, pPhysExt->aidx[i], 1);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync /* next */
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync iPhysExt = pPhysExt->iNext;
5366e994777f9d9391cf809dc77610f57270d75dvboxsync } while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
5366e994777f9d9391cf809dc77610f57270d75dvboxsync
5366e994777f9d9391cf809dc77610f57270d75dvboxsync /* insert the list into the free list and clear the ram range entry. */
5366e994777f9d9391cf809dc77610f57270d75dvboxsync pPhysExt->iNext = pPool->iPhysExtFreeHead;
5366e994777f9d9391cf809dc77610f57270d75dvboxsync pPool->iPhysExtFreeHead = iPhysExtStart;
5366e994777f9d9391cf809dc77610f57270d75dvboxsync PGM_PAGE_SET_TRACKING(pPhysPage, 0);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTs, f);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync}
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync/**
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * Flushes all shadow page table mappings of the given guest page.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync *
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * This is typically called when the host page backing the guest one has been
5366e994777f9d9391cf809dc77610f57270d75dvboxsync * replaced or when the page protection was changed due to an access handler.
5366e994777f9d9391cf809dc77610f57270d75dvboxsync *
5366e994777f9d9391cf809dc77610f57270d75dvboxsync * @returns VBox status code.
5366e994777f9d9391cf809dc77610f57270d75dvboxsync * @retval VINF_SUCCESS if all references has been successfully cleared.
5366e994777f9d9391cf809dc77610f57270d75dvboxsync * @retval VINF_PGM_SYNC_CR3 if we're better off with a CR3 sync and a page
5366e994777f9d9391cf809dc77610f57270d75dvboxsync * pool cleaning. FF and sync flags are set.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync *
5366e994777f9d9391cf809dc77610f57270d75dvboxsync * @param pVM The VM handle.
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync * @param pPhysPage The guest page in question.
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsync * @param pfFlushTLBs This is set to @a true if the shadow TLBs should be
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * flushed, it is NOT touched if this isn't necessary.
5366e994777f9d9391cf809dc77610f57270d75dvboxsync * The caller MUST initialized this to @a false.
5366e994777f9d9391cf809dc77610f57270d75dvboxsync */
d8e12fa5dd1c35282b98cb165e42b6b395cf971bvboxsyncint pgmPoolTrackFlushGCPhys(PVM pVM, PPGMPAGE pPhysPage, bool *pfFlushTLBs)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync{
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pgmLock(pVM);
5366e994777f9d9391cf809dc77610f57270d75dvboxsync int rc = VINF_SUCCESS;
5366e994777f9d9391cf809dc77610f57270d75dvboxsync#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
5366e994777f9d9391cf809dc77610f57270d75dvboxsync const uint16_t u16 = PGM_PAGE_GET_TRACKING(pPhysPage);
5366e994777f9d9391cf809dc77610f57270d75dvboxsync if (u16)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /*
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * The zero page is currently screwing up the tracking and we'll
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync * have to flush the whole shebang. Unless VBOX_WITH_NEW_LAZY_PAGE_ALLOC
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * is defined, zero pages won't normally be mapped. Some kind of solution
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * will be needed for this problem of course, but it will have to wait...
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (PGM_PAGE_IS_ZERO(pPhysPage))
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync rc = VINF_PGM_GCPHYS_ALIASED;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync else
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync {
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync /* Start a subset here because pgmPoolTrackFlushGCPhysPTsSlow and
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync pgmPoolTrackFlushGCPhysPTs will/may kill the pool otherwise. */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PVMCPU pVCpu = VMMGetCpu(pVM);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync# endif
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync if (PGMPOOL_TD_GET_CREFS(u16) != PGMPOOL_TD_CREFS_PHYSEXT)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pgmPoolTrackFlushGCPhysPT(pVM,
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pPhysPage,
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync PGMPOOL_TD_GET_IDX(u16),
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync PGMPOOL_TD_GET_CREFS(u16));
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync else if (u16 != PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED))
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync pgmPoolTrackFlushGCPhysPTs(pVM, pPhysPage, PGMPOOL_TD_GET_IDX(u16));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync else
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync rc = pgmPoolTrackFlushGCPhysPTsSlow(pVM, pPhysPage);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync *pfFlushTLBs = true;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync PGMDynMapPopAutoSubset(pVCpu, iPrevSubset);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync# endif
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync }
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync#elif defined(PGMPOOL_WITH_CACHE)
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync if (PGM_PAGE_IS_ZERO(pPhysPage))
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync rc = VINF_PGM_GCPHYS_ALIASED;
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync else
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync /* Start a subset here because pgmPoolTrackFlushGCPhysPTsSlow kill the pool otherwise. */
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync PVMCPU pVCpu = VMMGetCpu(pVM);
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync# endif
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync rc = pgmPoolTrackFlushGCPhysPTsSlow(pVM, pPhysPage);
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync if (rc == VINF_SUCCESS)
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync *pfFlushTLBs = true;
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync }
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync PGMDynMapPopAutoSubset(pVCpu, iPrevSubset);
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync# endif
4121d226ac899f17e13aff3aff42b603c8b5c1fevboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#else
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync rc = VINF_PGM_GCPHYS_ALIASED;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#endif
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (rc == VINF_PGM_GCPHYS_ALIASED)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pVM->pgm.s.fGlobalSyncFlags |= PGM_GLOBAL_SYNC_CLEAR_PGM_POOL;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync for (unsigned i=0;i<pVM->cCPUs;i++)
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync {
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync PVMCPU pVCpu = &pVM->aCpus[i];
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync }
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync rc = VINF_PGM_SYNC_CR3;
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pgmUnlock(pVM);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync return rc;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync}
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync/**
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * Scans all shadow page tables for mappings of a physical page.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync *
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync * This may be slow, but it's most likely more efficient than cleaning
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync * out the entire page pool / cache.
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync *
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync * @returns VBox status code.
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync * @retval VINF_SUCCESS if all references has been successfully cleared.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @retval VINF_PGM_GCPHYS_ALIASED if we're better off with a CR3 sync and
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync * a page pool cleaning.
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync *
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pVM The VM handle.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pPhysPage The guest page in question.
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync */
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsyncint pgmPoolTrackFlushGCPhysPTsSlow(PVM pVM, PPGMPAGE pPhysPage)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync{
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPTsSlow, s);
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: cUsedPages=%d cPresent=%d pPhysPage=%R[pgmpage]\n",
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync pPool->cUsedPages, pPool->cPresent, pPhysPage));
e378dfdadd62aadc0a012c9953322d979d7606e6vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#if 1
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /*
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * There is a limit to what makes sense.
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (pPool->cPresent > 1024)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: giving up... (cPresent=%d)\n", pPool->cPresent));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTsSlow, s);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync return VINF_PGM_GCPHYS_ALIASED;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#endif
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /*
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Iterate all the pages until we've encountered all that in use.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * This is simple but not quite optimal solution.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync const uint32_t u32 = u64;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync unsigned cLeft = pPool->cUsedPages;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync unsigned iPage = pPool->cCurPages;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync while (--iPage >= PGMPOOL_IDX_FIRST)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync if (pPage->GCPhys != NIL_RTGCPHYS)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync switch (pPage->enmKind)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /*
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * We only care about shadow page tables.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync unsigned cPresent = pPage->cPresent;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PX86PT pPT = (PX86PT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync if (pPT->a[i].n.u1Present)
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync {
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
45655563f818c5d5bbf4b3d14aa48cbd92a871f1vboxsync {
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync //Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX32\n", iPage, i, pPT->a[i]));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pPT->a[i].u = 0;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (!--cPresent)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync }
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync break;
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync }
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync {
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync unsigned cPresent = pPage->cPresent;
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync PX86PTPAE pPT = (PX86PTPAE)PGMPOOL_PAGE_2_PTR(pVM, pPage);
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync if (pPT->a[i].n.u1Present)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync //Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX64\n", iPage, i, pPT->a[i]));
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync pPT->a[i].u = 0;
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync }
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync if (!--cPresent)
5366e994777f9d9391cf809dc77610f57270d75dvboxsync break;
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync }
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync break;
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync }
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync }
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync if (!--cLeft)
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync break;
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync }
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync }
45655563f818c5d5bbf4b3d14aa48cbd92a871f1vboxsync
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync PGM_PAGE_SET_TRACKING(pPhysPage, 0);
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTsSlow, s);
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync return VINF_SUCCESS;
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync}
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync/**
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync * Clears the user entry in a user table.
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync *
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync * This is used to remove all references to a page when flushing it.
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync */
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsyncstatic void pgmPoolTrackClearPageUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PCPGMPOOLUSER pUser)
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync{
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync Assert(pUser->iUser != NIL_PGMPOOL_IDX);
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync Assert(pUser->iUser < pPool->cCurPages);
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync uint32_t iUserTable = pUser->iUserTable;
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync /*
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync * Map the user page.
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync */
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync PPGMPOOLPAGE pUserPage = &pPool->aPages[pUser->iUser];
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync union
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync {
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync uint64_t *pau64;
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync uint32_t *pau32;
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync } u;
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync u.pau64 = (uint64_t *)PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pUserPage);
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync LogFlow(("pgmPoolTrackClearPageUser: clear %x in %s (%RGp) (flushing %s)\n", iUserTable, pgmPoolPoolKindToStr(pUserPage->enmKind), pUserPage->Core.Key, pgmPoolPoolKindToStr(pPage->enmKind)));
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync /* Safety precaution in case we change the paging for other modes too in the future. */
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync Assert(!pgmPoolIsPageLocked(&pPool->CTX_SUFF(pVM)->pgm.s, pPage));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#ifdef VBOX_STRICT
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /*
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Some sanity checks.
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync */
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync switch (pUserPage->enmKind)
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync {
45655563f818c5d5bbf4b3d14aa48cbd92a871f1vboxsync case PGMPOOLKIND_32BIT_PD:
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync Assert(iUserTable < X86_PG_ENTRIES);
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync break;
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync case PGMPOOLKIND_PAE_PDPT:
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync Assert(iUserTable < 4);
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync break;
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync case PGMPOOLKIND_PAE_PD_PHYS:
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync Assert(iUserTable < X86_PG_PAE_ENTRIES);
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync break;
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync Assert(iUserTable < X86_PG_PAE_ENTRIES);
c55bf74b54ecdfb5ebc4e5d90b620d0fee31737evboxsync Assert(!(u.pau64[iUserTable] & PGM_PDFLAGS_MAPPING));
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync break;
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync Assert(iUserTable < X86_PG_PAE_ENTRIES);
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync break;
cc1ef2ef9bbc6a0ff964928d61b7298e5bfcce5fvboxsync case PGMPOOLKIND_64BIT_PML4:
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync /* GCPhys >> PAGE_SHIFT is the index here */
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync break;
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync Assert(iUserTable < X86_PG_PAE_ENTRIES);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync Assert(iUserTable < X86_PG_PAE_ENTRIES);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_ROOT_NESTED:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(iUserTable < X86_PG_PAE_ENTRIES);
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync break;
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync default:
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync AssertMsgFailed(("enmKind=%d\n", pUserPage->enmKind));
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync break;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#endif /* VBOX_STRICT */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /*
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * Clear the entry in the user page.
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync */
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync switch (pUserPage->enmKind)
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync {
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync /* 32-bit entries */
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync case PGMPOOLKIND_32BIT_PD:
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync u.pau32[iUserTable] = 0;
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync break;
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync /* 64-bit entries */
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#if defined(IN_RC)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /* In 32 bits PAE mode we *must* invalidate the TLB when changing a PDPT entry; the CPU fetches them only during cr3 load, so any
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * non-present PDPT will continue to cause page faults.
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync ASMReloadCR3();
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#endif
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /* no break */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PD_PHYS:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_64BIT_PML4:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PDPT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_ROOT_NESTED:
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync u.pau64[iUserTable] = 0;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync default:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync AssertFatalMsgFailed(("enmKind=%d iUser=%#x iUserTable=%#x\n", pUserPage->enmKind, pUser->iUser, pUser->iUserTable));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync}
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/**
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Clears all users of a page.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsyncstatic void pgmPoolTrackClearPageUsers(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync{
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /*
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Free all the user records.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync LogFlow(("pgmPoolTrackClearPageUsers %RGp\n", pPage->GCPhys));
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uint16_t i = pPage->iUserHead;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync while (i != NIL_PGMPOOL_USER_INDEX)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /* Clear enter in user table. */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pgmPoolTrackClearPageUser(pPool, pPage, &paUsers[i]);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync /* Free it. */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync const uint16_t iNext = paUsers[i].iNext;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync paUsers[i].iUser = NIL_PGMPOOL_IDX;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync paUsers[i].iNext = pPool->iUserFreeHead;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pPool->iUserFreeHead = i;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync /* Next. */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync i = iNext;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync}
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync/**
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * 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.
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * @param piPhysExt Where to store the phys ext index.
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsyncPPGMPOOLPHYSEXT pgmPoolTrackPhysExtAlloc(PVM pVM, uint16_t *piPhysExt)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync{
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(PGMIsLockOwner(pVM));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uint16_t iPhysExt = pPool->iPhysExtFreeHead;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (iPhysExt == NIL_PGMPOOL_PHYSEXT_INDEX)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync STAM_COUNTER_INC(&pPool->StamTrackPhysExtAllocFailures);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync return NULL;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync }
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync PPGMPOOLPHYSEXT pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPool->iPhysExtFreeHead = pPhysExt->iNext;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPhysExt->iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync *piPhysExt = iPhysExt;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync return pPhysExt;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync}
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync/**
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * Frees a physical cross reference extent.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync *
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @param pVM The VM handle.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @param iPhysExt The extent to free.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsyncvoid pgmPoolTrackPhysExtFree(PVM pVM, uint16_t iPhysExt)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync{
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync Assert(PGMIsLockOwner(pVM));
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync Assert(iPhysExt < pPool->cMaxPhysExts);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync PPGMPOOLPHYSEXT pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPhysExt->iNext = pPool->iPhysExtFreeHead;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPool->iPhysExtFreeHead = iPhysExt;
b8bb9c9f6b8ebfd0a7d6df0c0289f9fe80241750vboxsync}
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync/**
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * Frees a physical cross reference extent.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync *
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @param pVM The VM handle.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @param iPhysExt The extent to free.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsyncvoid pgmPoolTrackPhysExtFreeList(PVM pVM, uint16_t iPhysExt)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync{
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync Assert(PGMIsLockOwner(pVM));
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync const uint16_t iPhysExtStart = iPhysExt;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync PPGMPOOLPHYSEXT pPhysExt;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync do
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync {
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync Assert(iPhysExt < pPool->cMaxPhysExts);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync /* next */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync iPhysExt = pPhysExt->iNext;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync } while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPhysExt->iNext = pPool->iPhysExtFreeHead;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pPool->iPhysExtFreeHead = iPhysExtStart;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync}
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync/**
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * Insert a reference into a list of physical cross reference extents.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync *
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @returns The new tracking data for PGMPAGE.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync *
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @param pVM The VM handle.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @param iPhysExt The physical extent index of the list head.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @param iShwPT The shadow page table index.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync *
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsyncstatic uint16_t pgmPoolTrackPhysExtInsert(PVM pVM, uint16_t iPhysExt, uint16_t iShwPT)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync{
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync Assert(PGMIsLockOwner(pVM));
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync
/* special common case. */
if (paPhysExts[iPhysExt].aidx[2] == NIL_PGMPOOL_IDX)
{
paPhysExts[iPhysExt].aidx[2] = iShwPT;
STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedMany);
LogFlow(("pgmPoolTrackPhysExtAddref: %d:{,,%d}\n", iPhysExt, iShwPT));
return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExt);
}
/* general treatment. */
const uint16_t iPhysExtStart = iPhysExt;
unsigned cMax = 15;
for (;;)
{
Assert(iPhysExt < pPool->cMaxPhysExts);
for (unsigned i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
if (paPhysExts[iPhysExt].aidx[i] == NIL_PGMPOOL_IDX)
{
paPhysExts[iPhysExt].aidx[i] = iShwPT;
STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedMany);
LogFlow(("pgmPoolTrackPhysExtAddref: %d:{%d} i=%d cMax=%d\n", iPhysExt, iShwPT, i, cMax));
return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExtStart);
}
if (!--cMax)
{
STAM_COUNTER_INC(&pVM->pgm.s.StatTrackOverflows);
pgmPoolTrackPhysExtFreeList(pVM, iPhysExtStart);
LogFlow(("pgmPoolTrackPhysExtAddref: overflow (1) iShwPT=%d\n", iShwPT));
return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED);
}
}
/* add another extent to the list. */
PPGMPOOLPHYSEXT pNew = pgmPoolTrackPhysExtAlloc(pVM, &iPhysExt);
if (!pNew)
{
STAM_COUNTER_INC(&pVM->pgm.s.StatTrackOverflows);
pgmPoolTrackPhysExtFreeList(pVM, iPhysExtStart);
return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED);
}
pNew->iNext = iPhysExtStart;
pNew->aidx[0] = iShwPT;
LogFlow(("pgmPoolTrackPhysExtAddref: added new extent %d:{%d}->%d\n", iPhysExt, iShwPT, iPhysExtStart));
return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExt);
}
/**
* Add a reference to guest physical page where extents are in use.
*
* @returns The new tracking data for PGMPAGE.
*
* @param pVM The VM handle.
* @param u16 The ram range flags (top 16-bits).
* @param iShwPT The shadow page table index.
*/
uint16_t pgmPoolTrackPhysExtAddref(PVM pVM, uint16_t u16, uint16_t iShwPT)
{
pgmLock(pVM);
if (PGMPOOL_TD_GET_CREFS(u16) != PGMPOOL_TD_CREFS_PHYSEXT)
{
/*
* Convert to extent list.
*/
Assert(PGMPOOL_TD_GET_CREFS(u16) == 1);
uint16_t iPhysExt;
PPGMPOOLPHYSEXT pPhysExt = pgmPoolTrackPhysExtAlloc(pVM, &iPhysExt);
if (pPhysExt)
{
LogFlow(("pgmPoolTrackPhysExtAddref: new extent: %d:{%d, %d}\n", iPhysExt, PGMPOOL_TD_GET_IDX(u16), iShwPT));
STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliased);
pPhysExt->aidx[0] = PGMPOOL_TD_GET_IDX(u16);
pPhysExt->aidx[1] = iShwPT;
u16 = PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExt);
}
else
u16 = PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED);
}
else if (u16 != PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED))
{
/*
* Insert into the extent list.
*/
u16 = pgmPoolTrackPhysExtInsert(pVM, PGMPOOL_TD_GET_IDX(u16), iShwPT);
}
else
STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedLots);
pgmUnlock(pVM);
return u16;
}
/**
* Clear references to guest physical memory.
*
* @param pPool The pool.
* @param pPage The page.
* @param pPhysPage Pointer to the aPages entry in the ram range.
*/
void pgmPoolTrackPhysExtDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PPGMPAGE pPhysPage)
{
const unsigned cRefs = PGM_PAGE_GET_TD_CREFS(pPhysPage);
AssertFatalMsg(cRefs == PGMPOOL_TD_CREFS_PHYSEXT, ("cRefs=%d pPhysPage=%R[pgmpage] pPage=%p:{.idx=%d}\n", cRefs, pPhysPage, pPage, pPage->idx));
uint16_t iPhysExt = PGM_PAGE_GET_TD_IDX(pPhysPage);
if (iPhysExt != PGMPOOL_TD_IDX_OVERFLOWED)
{
PVM pVM = pPool->CTX_SUFF(pVM);
pgmLock(pVM);
uint16_t iPhysExtPrev = NIL_PGMPOOL_PHYSEXT_INDEX;
PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
do
{
Assert(iPhysExt < pPool->cMaxPhysExts);
/*
* Look for the shadow page and check if it's all freed.
*/
for (unsigned i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
{
if (paPhysExts[iPhysExt].aidx[i] == pPage->idx)
{
paPhysExts[iPhysExt].aidx[i] = NIL_PGMPOOL_IDX;
for (i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
if (paPhysExts[iPhysExt].aidx[i] != NIL_PGMPOOL_IDX)
{
Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d\n", pPhysPage, pPage->idx));
pgmUnlock(pVM);
return;
}
/* we can free the node. */
const uint16_t iPhysExtNext = paPhysExts[iPhysExt].iNext;
if ( iPhysExtPrev == NIL_PGMPOOL_PHYSEXT_INDEX
&& iPhysExtNext == NIL_PGMPOOL_PHYSEXT_INDEX)
{
/* lonely node */
pgmPoolTrackPhysExtFree(pVM, iPhysExt);
Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d lonely\n", pPhysPage, pPage->idx));
PGM_PAGE_SET_TRACKING(pPhysPage, 0);
}
else if (iPhysExtPrev == NIL_PGMPOOL_PHYSEXT_INDEX)
{
/* head */
Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d head\n", pPhysPage, pPage->idx));
PGM_PAGE_SET_TRACKING(pPhysPage, PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExtNext));
pgmPoolTrackPhysExtFree(pVM, iPhysExt);
}
else
{
/* in list */
Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d\n", pPhysPage, pPage->idx));
paPhysExts[iPhysExtPrev].iNext = iPhysExtNext;
pgmPoolTrackPhysExtFree(pVM, iPhysExt);
}
iPhysExt = iPhysExtNext;
pgmUnlock(pVM);
return;
}
}
/* next */
iPhysExtPrev = iPhysExt;
iPhysExt = paPhysExts[iPhysExt].iNext;
} while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
pgmUnlock(pVM);
AssertFatalMsgFailed(("not-found! cRefs=%d pPhysPage=%R[pgmpage] pPage=%p:{.idx=%d}\n", cRefs, pPhysPage, pPage, pPage->idx));
}
else /* nothing to do */
Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage]\n", pPhysPage));
}
/**
* Clear references to guest physical memory.
*
* This is the same as pgmPoolTracDerefGCPhys except that the guest physical address
* is assumed to be correct, so the linear search can be skipped and we can assert
* at an earlier point.
*
* @param pPool The pool.
* @param pPage The page.
* @param HCPhys The host physical address corresponding to the guest page.
* @param GCPhys The guest physical address corresponding to HCPhys.
*/
static void pgmPoolTracDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhys)
{
/*
* Walk range list.
*/
PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
while (pRam)
{
RTGCPHYS off = GCPhys - pRam->GCPhys;
if (off < pRam->cb)
{
/* does it match? */
const unsigned iPage = off >> PAGE_SHIFT;
Assert(PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]));
#ifdef LOG_ENABLED
RTHCPHYS HCPhysPage = PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]);
Log2(("pgmPoolTracDerefGCPhys %RHp vs %RHp\n", HCPhysPage, HCPhys));
#endif
if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
{
pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
return;
}
break;
}
pRam = pRam->CTX_SUFF(pNext);
}
AssertFatalMsgFailed(("HCPhys=%RHp GCPhys=%RGp\n", HCPhys, GCPhys));
}
/**
* Clear references to guest physical memory.
*
* @param pPool The pool.
* @param pPage The page.
* @param HCPhys The host physical address corresponding to the guest page.
* @param GCPhysHint The guest physical address which may corresponding to HCPhys.
*/
static void pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint)
{
Log4(("pgmPoolTracDerefGCPhysHint %RHp %RGp\n", HCPhys, GCPhysHint));
/*
* Walk range list.
*/
PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
while (pRam)
{
RTGCPHYS off = GCPhysHint - pRam->GCPhys;
if (off < pRam->cb)
{
/* does it match? */
const unsigned iPage = off >> PAGE_SHIFT;
Assert(PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]));
if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
{
pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
return;
}
break;
}
pRam = pRam->CTX_SUFF(pNext);
}
/*
* Damn, the hint didn't work. We'll have to do an expensive linear search.
*/
STAM_COUNTER_INC(&pPool->StatTrackLinearRamSearches);
pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
while (pRam)
{
unsigned iPage = pRam->cb >> PAGE_SHIFT;
while (iPage-- > 0)
{
if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
{
Log4(("pgmPoolTracDerefGCPhysHint: Linear HCPhys=%RHp GCPhysHint=%RGp GCPhysReal=%RGp\n",
HCPhys, GCPhysHint, pRam->GCPhys + (iPage << PAGE_SHIFT)));
pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
return;
}
}
pRam = pRam->CTX_SUFF(pNext);
}
AssertFatalMsgFailed(("HCPhys=%RHp GCPhysHint=%RGp\n", HCPhys, GCPhysHint));
}
/**
* Clear references to guest physical memory in a 32-bit / 32-bit page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
* @param pGstPT The guest page table.
*/
DECLINLINE(void) pgmPoolTrackDerefPT32Bit32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT, PCX86PT pGstPT)
{
for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
if (pShwPT->a[i].n.u1Present)
{
Log4(("pgmPoolTrackDerefPT32Bit32Bit: i=%d pte=%RX32 hint=%RX32\n",
i, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK));
pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
if (!--pPage->cPresent)
break;
}
}
/**
* Clear references to guest physical memory in a PAE / 32-bit page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
* @param pGstPT The guest page table (just a half one).
*/
DECLINLINE(void) pgmPoolTrackDerefPTPae32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PT pGstPT)
{
for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++)
if (pShwPT->a[i].n.u1Present)
{
Log4(("pgmPoolTrackDerefPTPae32Bit: i=%d pte=%RX64 hint=%RX32\n",
i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK));
pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
}
}
/**
* Clear references to guest physical memory in a PAE / PAE page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
* @param pGstPT The guest page table.
*/
DECLINLINE(void) pgmPoolTrackDerefPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PTPAE pGstPT)
{
for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++)
if (pShwPT->a[i].n.u1Present)
{
Log4(("pgmPoolTrackDerefPTPaePae: i=%d pte=%RX32 hint=%RX32\n",
i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PAE_PG_MASK));
pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PAE_PG_MASK);
}
}
/**
* Clear references to guest physical memory in a 32-bit / 4MB page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPT32Bit4MB(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT)
{
RTGCPHYS GCPhys = pPage->GCPhys;
for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
if (pShwPT->a[i].n.u1Present)
{
Log4(("pgmPoolTrackDerefPT32Bit4MB: i=%d pte=%RX32 GCPhys=%RGp\n",
i, pShwPT->a[i].u & X86_PTE_PG_MASK, GCPhys));
pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, GCPhys);
}
}
/**
* Clear references to guest physical memory in a PAE / 2/4MB page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPTPaeBig(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT)
{
RTGCPHYS GCPhys = pPage->GCPhys;
for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
if (pShwPT->a[i].n.u1Present)
{
Log4(("pgmPoolTrackDerefPTPaeBig: i=%d pte=%RX64 hint=%RGp\n",
i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, GCPhys));
pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, GCPhys);
}
}
#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
/**
* Clear references to shadowed pages in a 32 bits page directory.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPD The shadow page directory (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPD(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PD pShwPD)
{
for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
{
if ( pShwPD->a[i].n.u1Present
&& !(pShwPD->a[i].u & PGM_PDFLAGS_MAPPING)
)
{
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PG_MASK);
if (pSubPage)
pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
else
AssertFatalMsgFailed(("%x\n", pShwPD->a[i].u & X86_PDE_PG_MASK));
}
}
}
/**
* Clear references to shadowed pages in a PAE (legacy or 64 bits) page directory.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPD The shadow page directory (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPDPae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPAE pShwPD)
{
for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
{
if ( pShwPD->a[i].n.u1Present
&& !(pShwPD->a[i].u & PGM_PDFLAGS_MAPPING)
)
{
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PAE_PG_MASK);
if (pSubPage)
pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
else
AssertFatalMsgFailed(("%RX64\n", pShwPD->a[i].u & X86_PDE_PAE_PG_MASK));
/** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
}
}
}
/**
* Clear references to shadowed pages in a PAE page directory pointer table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPDPT The shadow page directory pointer table (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPDPTPae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPT pShwPDPT)
{
for (unsigned i = 0; i < X86_PG_PAE_PDPE_ENTRIES; i++)
{
if ( pShwPDPT->a[i].n.u1Present
&& !(pShwPDPT->a[i].u & PGM_PLXFLAGS_MAPPING)
)
{
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & X86_PDPE_PG_MASK);
if (pSubPage)
pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
else
AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & X86_PDPE_PG_MASK));
}
}
}
/**
* Clear references to shadowed pages in a 64-bit page directory pointer table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPDPT The shadow page directory pointer table (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPDPT64Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPT pShwPDPT)
{
for (unsigned i = 0; i < RT_ELEMENTS(pShwPDPT->a); i++)
{
Assert(!(pShwPDPT->a[i].u & PGM_PLXFLAGS_MAPPING));
if (pShwPDPT->a[i].n.u1Present)
{
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & X86_PDPE_PG_MASK);
if (pSubPage)
pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
else
AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & X86_PDPE_PG_MASK));
/** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
}
}
}
/**
* Clear references to shadowed pages in a 64-bit level 4 page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPML4 The shadow page directory pointer table (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPML464Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PML4 pShwPML4)
{
for (unsigned i = 0; i < RT_ELEMENTS(pShwPML4->a); i++)
{
if (pShwPML4->a[i].n.u1Present)
{
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPML4->a[i].u & X86_PDPE_PG_MASK);
if (pSubPage)
pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
else
AssertFatalMsgFailed(("%RX64\n", pShwPML4->a[i].u & X86_PML4E_PG_MASK));
/** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
}
}
}
/**
* Clear references to shadowed pages in an EPT page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPML4 The shadow page directory pointer table (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPTEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPT pShwPT)
{
RTGCPHYS GCPhys = pPage->GCPhys;
for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
if (pShwPT->a[i].n.u1Present)
{
Log4(("pgmPoolTrackDerefPTEPT: i=%d pte=%RX64 GCPhys=%RX64\n",
i, pShwPT->a[i].u & EPT_PTE_PG_MASK, pPage->GCPhys));
pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & EPT_PTE_PG_MASK, GCPhys);
}
}
/**
* Clear references to shadowed pages in an EPT page directory.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPD The shadow page directory (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPDEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPD pShwPD)
{
for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
{
if (pShwPD->a[i].n.u1Present)
{
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & EPT_PDE_PG_MASK);
if (pSubPage)
pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
else
AssertFatalMsgFailed(("%RX64\n", pShwPD->a[i].u & EPT_PDE_PG_MASK));
/** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
}
}
}
/**
* Clear references to shadowed pages in an EPT page directory pointer table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPDPT The shadow page directory pointer table (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPDPTEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPDPT pShwPDPT)
{
for (unsigned i = 0; i < RT_ELEMENTS(pShwPDPT->a); i++)
{
if (pShwPDPT->a[i].n.u1Present)
{
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & EPT_PDPTE_PG_MASK);
if (pSubPage)
pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
else
AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & EPT_PDPTE_PG_MASK));
/** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
}
}
}
/**
* Clears all references made by this page.
*
* This includes other shadow pages and GC physical addresses.
*
* @param pPool The pool.
* @param pPage The page.
*/
static void pgmPoolTrackDeref(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
{
/*
* Map the shadow page and take action according to the page kind.
*/
void *pvShw = PGMPOOL_PAGE_2_LOCKED_PTR(pPool->CTX_SUFF(pVM), pPage);
switch (pPage->enmKind)
{
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
{
STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
void *pvGst;
int rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
pgmPoolTrackDerefPT32Bit32Bit(pPool, pPage, (PX86PT)pvShw, (PCX86PT)pvGst);
STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
break;
}
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
{
STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
void *pvGst;
int rc = PGM_GCPHYS_2_PTR_EX(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
pgmPoolTrackDerefPTPae32Bit(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PT)pvGst);
STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
break;
}
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
{
STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
void *pvGst;
int rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
pgmPoolTrackDerefPTPaePae(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PTPAE)pvGst);
STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
break;
}
case PGMPOOLKIND_32BIT_PT_FOR_PHYS: /* treat it like a 4 MB page */
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
{
STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
pgmPoolTrackDerefPT32Bit4MB(pPool, pPage, (PX86PT)pvShw);
STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
break;
}
case PGMPOOLKIND_PAE_PT_FOR_PHYS: /* treat it like a 2 MB page */
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
{
STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
pgmPoolTrackDerefPTPaeBig(pPool, pPage, (PX86PTPAE)pvShw);
STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
break;
}
#else /* !PGMPOOL_WITH_GCPHYS_TRACKING */
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
break;
#endif /* !PGMPOOL_WITH_GCPHYS_TRACKING */
case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
case PGMPOOLKIND_PAE_PD_PHYS:
case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
pgmPoolTrackDerefPDPae(pPool, pPage, (PX86PDPAE)pvShw);
break;
case PGMPOOLKIND_32BIT_PD_PHYS:
case PGMPOOLKIND_32BIT_PD:
pgmPoolTrackDerefPD(pPool, pPage, (PX86PD)pvShw);
break;
case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
case PGMPOOLKIND_PAE_PDPT:
case PGMPOOLKIND_PAE_PDPT_PHYS:
pgmPoolTrackDerefPDPTPae(pPool, pPage, (PX86PDPT)pvShw);
break;
case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
pgmPoolTrackDerefPDPT64Bit(pPool, pPage, (PX86PDPT)pvShw);
break;
case PGMPOOLKIND_64BIT_PML4:
pgmPoolTrackDerefPML464Bit(pPool, pPage, (PX86PML4)pvShw);
break;
case PGMPOOLKIND_EPT_PT_FOR_PHYS:
pgmPoolTrackDerefPTEPT(pPool, pPage, (PEPTPT)pvShw);
break;
case PGMPOOLKIND_EPT_PD_FOR_PHYS:
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;
PGMPOOL_UNLOCK_PTR(pPool->CTX_SUFF(pVM), pvShw);
}
#endif /* PGMPOOL_WITH_USER_TRACKING */
#ifdef IN_RING3
/**
* 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.
*
* @remark Only used during reset now, we might want to rename and/or move it.
*/
static void pgmPoolFlushAllInt(PPGMPOOL pPool)
{
PVM pVM = pPool->CTX_SUFF(pVM);
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;
}
/*
* Exit the shadow mode since we're going to clear everything,
* including the root page.
*/
/** @todo Need to synchronize this across all VCPUs! */
Assert(pVM->cCPUs == 1);
for (unsigned i=0;i<pVM->cCPUs;i++)
{
PVMCPU pVCpu = &pVM->aCpus[i];
pgmR3ExitShadowModeBeforePoolFlush(pVM, pVCpu);
}
/*
* 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];
Assert(pPage->Core.Key == MMPage2Phys(pVM, pPage->pvPageR3));
#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;
#ifdef PGMPOOL_WITH_USER_TRACKING
pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
#else
pPage->fCR3Mix = false;
#endif
#ifdef PGMPOOL_WITH_CACHE
pPage->iAgeNext = NIL_PGMPOOL_IDX;
pPage->iAgePrev = NIL_PGMPOOL_IDX;
#endif
pPage->cLocked = 0;
}
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 = pVM->pgm.s.CTX_SUFF(pRamRanges);
pRam;
pRam = pRam->CTX_SUFF(pNext))
{
unsigned iPage = pRam->cb >> PAGE_SHIFT;
while (iPage-- > 0)
PGM_PAGE_SET_TRACKING(&pRam->aPages[iPage], 0);
}
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
/*
* Reinsert active pages into the hash and ensure monitoring chains are correct.
*/
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)
{
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
}
for (unsigned i=0;i<pVM->cCPUs;i++)
{
PVMCPU pVCpu = &pVM->aCpus[i];
/*
* Re-enter the shadowing mode and assert Sync CR3 FF.
*/
pgmR3ReEnterShadowModeAfterPoolFlush(pVM, pVCpu);
VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
}
STAM_PROFILE_STOP(&pPool->StatFlushAllInt, a);
}
#endif /* IN_RING3 */
/**
* 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.
* @param pPool The pool.
* @param HCPhys The HC physical address of the shadow page.
*/
int pgmPoolFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
{
PVM pVM = pPool->CTX_SUFF(pVM);
int rc = VINF_SUCCESS;
STAM_PROFILE_START(&pPool->StatFlushPage, f);
LogFlow(("pgmPoolFlushPage: pPage=%p:{.Key=%RHp, .idx=%d, .enmKind=%s, .GCPhys=%RGp}\n",
pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), pPage->GCPhys));
/*
* Quietly reject any attempts at flushing any of the special root pages.
*/
if (pPage->idx < PGMPOOL_IDX_FIRST)
{
AssertFailed(); /* can no longer happen */
Log(("pgmPoolFlushPage: special root page, rejected. enmKind=%s idx=%d\n", pgmPoolPoolKindToStr(pPage->enmKind), pPage->idx));
return VINF_SUCCESS;
}
pgmLock(pVM);
/*
* Quietly reject any attempts at flushing the currently active shadow CR3 mapping
*/
if (pgmPoolIsPageLocked(&pVM->pgm.s, pPage))
{
AssertMsg( pPage->enmKind == PGMPOOLKIND_64BIT_PML4
|| pPage->enmKind == PGMPOOLKIND_PAE_PDPT
|| pPage->enmKind == PGMPOOLKIND_PAE_PDPT_FOR_32BIT
|| pPage->enmKind == PGMPOOLKIND_32BIT_PD
|| pPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD
|| pPage->enmKind == PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD
|| pPage->enmKind == PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD
|| pPage->enmKind == PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD
|| pPage->enmKind == PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD,
("Can't free the shadow CR3! (%RHp vs %RHp kind=%d\n", PGMGetHyperCR3(VMMGetCpu(pVM)), pPage->Core.Key, pPage->enmKind));
Log(("pgmPoolFlushPage: current active shadow CR3, rejected. enmKind=%s idx=%d\n", pgmPoolPoolKindToStr(pPage->enmKind), pPage->idx));
pgmUnlock(pVM);
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(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--;
pgmUnlock(pVM);
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)
{
PVM pVM = pPool->CTX_SUFF(pVM);
STAM_PROFILE_START(&pPool->StatFree, a);
LogFlow(("pgmPoolFreeByPage: pPage=%p:{.Key=%RHp, .idx=%d, enmKind=%s} iUser=%#x iUserTable=%#x\n",
pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), iUser, iUserTable));
Assert(pPage->idx >= PGMPOOL_IDX_FIRST);
pgmLock(pVM);
#ifdef PGMPOOL_WITH_USER_TRACKING
pgmPoolTrackFreeUser(pPool, pPage, iUser, iUserTable);
#endif
#ifdef PGMPOOL_WITH_CACHE
if (!pPage->fCached)
#endif
pgmPoolFlushPage(pPool, pPage);
pgmUnlock(pVM);
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 enmKind Page table kind
* @param iUser The user of the page.
*/
static int pgmPoolMakeMoreFreePages(PPGMPOOL pPool, PGMPOOLKIND enmKind, uint16_t iUser)
{
LogFlow(("pgmPoolMakeMoreFreePages: iUser=%#x\n", iUser));
/*
* If the pool isn't full grown yet, expand it.
*/
if ( pPool->cCurPages < pPool->cMaxPages
#if defined(IN_RC)
/* Hack alert: we can't deal with jumps to ring 3 when called from MapCR3 and allocating pages for PAE PDs. */
&& enmKind != PGMPOOLKIND_PAE_PD_FOR_PAE_PD
&& (enmKind < PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD || enmKind > PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD)
#endif
)
{
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 This path no longer works (CR3 root pages will be flushed)!! */
AssertCompileFailed();
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=%s iUser=%#x iUserTable=%#x\n", GCPhys, pgmPoolPoolKindToStr(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.fGlobalSyncFlags & PGM_GLOBAL_SYNC_CLEAR_PGM_POOL)); */
pgmLock(pVM);
#ifdef PGMPOOL_WITH_CACHE
if (pPool->fCacheEnabled)
{
int rc2 = pgmPoolCacheAlloc(pPool, GCPhys, enmKind, iUser, iUserTable, ppPage);
if (RT_SUCCESS(rc2))
{
pgmUnlock(pVM);
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, enmKind, iUser);
if (RT_FAILURE(rc))
{
pgmUnlock(pVM);
Log(("pgmPoolAlloc: returns %Rrc (Free)\n", rc));
STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
return rc;
}
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;
#ifdef PGMPOOL_WITH_MONITORING
pPage->cModifications = 0;
pPage->iModifiedNext = NIL_PGMPOOL_IDX;
pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
#else
pPage->fCR3Mix = false;
#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))
{
pPool->cUsedPages--;
pPage->enmKind = PGMPOOLKIND_FREE;
pPage->GCPhys = NIL_RTGCPHYS;
pPage->iNext = pPool->iFreeHead;
pPool->iFreeHead = pPage->idx;
pgmUnlock(pVM);
STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
Log(("pgmPoolAlloc: returns %Rrc (Insert)\n", rc3));
return rc3;
}
#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;
pgmUnlock(pVM);
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);
}
/**
* Internal worker for finding a 'in-use' shadow page give by it's physical address.
*
* @returns Pointer to the shadow page structure.
* @param pPool The pool.
* @param HCPhys The HC physical address of the shadow page.
*/
PPGMPOOLPAGE pgmPoolGetPage(PPGMPOOL pPool, RTHCPHYS HCPhys)
{
PVM pVM = pPool->CTX_SUFF(pVM);
/*
* Look up the page.
*/
pgmLock(pVM);
PPGMPOOLPAGE pPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, HCPhys & X86_PTE_PAE_PG_MASK);
pgmUnlock(pVM);
AssertFatalMsg(pPage && pPage->enmKind != PGMPOOLKIND_FREE, ("HCPhys=%RHp pPage=%p idx=%d\n", HCPhys, pPage, (pPage) ? pPage->idx : 0));
return pPage;
}
#ifdef IN_RING3
/**
* 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));
}
#endif /* IN_RING3 */
#ifdef LOG_ENABLED
static const char *pgmPoolPoolKindToStr(uint8_t enmKind)
{
switch(enmKind)
{
case PGMPOOLKIND_INVALID:
return "PGMPOOLKIND_INVALID";
case PGMPOOLKIND_FREE:
return "PGMPOOLKIND_FREE";
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
return "PGMPOOLKIND_32BIT_PT_FOR_PHYS";
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
return "PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT";
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
return "PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB";
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
return "PGMPOOLKIND_PAE_PT_FOR_PHYS";
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
return "PGMPOOLKIND_PAE_PT_FOR_32BIT_PT";
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
return "PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB";
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
return "PGMPOOLKIND_PAE_PT_FOR_PAE_PT";
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
return "PGMPOOLKIND_PAE_PT_FOR_PAE_2MB";
case PGMPOOLKIND_32BIT_PD:
return "PGMPOOLKIND_32BIT_PD";
case PGMPOOLKIND_32BIT_PD_PHYS:
return "PGMPOOLKIND_32BIT_PD_PHYS";
case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
return "PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD";
case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
return "PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD";
case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
return "PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD";
case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
return "PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD";
case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
return "PGMPOOLKIND_PAE_PD_FOR_PAE_PD";
case PGMPOOLKIND_PAE_PD_PHYS:
return "PGMPOOLKIND_PAE_PD_PHYS";
case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
return "PGMPOOLKIND_PAE_PDPT_FOR_32BIT";
case PGMPOOLKIND_PAE_PDPT:
return "PGMPOOLKIND_PAE_PDPT";
case PGMPOOLKIND_PAE_PDPT_PHYS:
return "PGMPOOLKIND_PAE_PDPT_PHYS";
case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
return "PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT";
case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
return "PGMPOOLKIND_64BIT_PDPT_FOR_PHYS";
case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
return "PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD";
case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
return "PGMPOOLKIND_64BIT_PD_FOR_PHYS";
case PGMPOOLKIND_64BIT_PML4:
return "PGMPOOLKIND_64BIT_PML4";
case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
return "PGMPOOLKIND_EPT_PDPT_FOR_PHYS";
case PGMPOOLKIND_EPT_PD_FOR_PHYS:
return "PGMPOOLKIND_EPT_PD_FOR_PHYS";
case PGMPOOLKIND_EPT_PT_FOR_PHYS:
return "PGMPOOLKIND_EPT_PT_FOR_PHYS";
case PGMPOOLKIND_ROOT_NESTED:
return "PGMPOOLKIND_ROOT_NESTED";
}
return "Unknown kind!";
}
#endif /* LOG_ENABLED*/