PGMAllPool.cpp revision 5fdc5337b17150e278798aeafaafde9ee56eba16
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/* $Id$ */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/** @file
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * PGM Shadow Page Pool.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/*
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * available from http://www.virtualbox.org. This file is free software;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * you can redistribute it and/or modify it under the terms of the GNU
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * General Public License (GPL) as published by the Free Software
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * additional information or have any questions.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/*******************************************************************************
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync* Header Files *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync*******************************************************************************/
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#define LOG_GROUP LOG_GROUP_PGM_POOL
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#include <VBox/pgm.h>
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#include <VBox/mm.h>
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#include <VBox/em.h>
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#include <VBox/cpum.h>
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#ifdef IN_RC
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# include <VBox/patm.h>
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#endif
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#include "../PGMInternal.h"
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#include <VBox/vm.h>
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#include "../PGMInline.h"
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#include <VBox/disopcode.h>
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#include <VBox/hwacc_vmx.h>
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#include <VBox/log.h>
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#include <VBox/err.h>
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#include <iprt/asm.h>
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#include <iprt/string.h>
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/*******************************************************************************
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync* Internal Functions *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync*******************************************************************************/
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncRT_C_DECLS_BEGIN
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncstatic void pgmPoolFlushAllInt(PPGMPOOL pPool);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncDECLINLINE(unsigned) pgmPoolTrackGetShadowEntrySize(PGMPOOLKIND enmKind);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncDECLINLINE(unsigned) pgmPoolTrackGetGuestEntrySize(PGMPOOLKIND enmKind);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncstatic void pgmPoolTrackDeref(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncstatic int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncstatic void pgmPoolMonitorModifiedRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#ifndef IN_RING3
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncDECLEXPORT(int) pgmPoolAccessHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#endif
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#ifdef LOG_ENABLED
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncstatic const char *pgmPoolPoolKindToStr(uint8_t enmKind);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#endif
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#if defined(VBOX_STRICT) && defined(PGMPOOL_WITH_OPTIMIZED_DIRTY_PT)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncstatic void pgmPoolTrackCheckPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PTPAE pGstPT);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#endif
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncint pgmPoolTrackFlushGCPhysPTsSlow(PVM pVM, PPGMPAGE pPhysPage);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncPPGMPOOLPHYSEXT pgmPoolTrackPhysExtAlloc(PVM pVM, uint16_t *piPhysExt);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncvoid pgmPoolTrackPhysExtFree(PVM pVM, uint16_t iPhysExt);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncvoid pgmPoolTrackPhysExtFreeList(PVM pVM, uint16_t iPhysExt);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncRT_C_DECLS_END
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/**
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Checks if the specified page pool kind is for a 4MB or 2MB guest page.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @returns true if it's the shadow of a 4MB or 2MB guest page, otherwise false.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param enmKind The page kind.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncDECLINLINE(bool) pgmPoolIsBigPage(PGMPOOLKIND enmKind)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync{
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync switch (enmKind)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync return true;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync default:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync return false;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync}
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync/** @def PGMPOOL_PAGE_2_LOCKED_PTR
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Maps a pool page pool into the current context and lock it (RC only).
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @returns VBox status code.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pVM The VM handle.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pPage The pool page.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @remark In RC this uses PGMGCDynMapHCPage(), so it will consume of the
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * small page window employeed by that function. Be careful.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @remark There is no need to assert on the result.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync#if defined(IN_RC)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsyncDECLINLINE(void *) PGMPOOL_PAGE_2_LOCKED_PTR(PVM pVM, PPGMPOOLPAGE pPage)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync{
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync void *pv = pgmPoolMapPageInlined(&pVM->pgm.s, pPage);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* Make sure the dynamic mapping will not be reused. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (pv)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync PGMDynLockHCPage(pVM, (uint8_t *)pv);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync return pv;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync}
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#else
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# define PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage) PGMPOOL_PAGE_2_PTR(pVM, pPage)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#endif
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/** @def PGMPOOL_UNLOCK_PTR
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Unlock a previously locked dynamic caching (RC only).
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @returns VBox status code.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pVM The VM handle.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pPage The pool page.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @remark In RC this uses PGMGCDynMapHCPage(), so it will consume of the
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * small page window employeed by that function. Be careful.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @remark There is no need to assert on the result.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#if defined(IN_RC)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncDECLINLINE(void) PGMPOOL_UNLOCK_PTR(PVM pVM, void *pvPage)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync{
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (pvPage)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PGMDynUnlockHCPage(pVM, (uint8_t *)pvPage);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync}
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#else
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync# define PGMPOOL_UNLOCK_PTR(pVM, pPage) do {} while (0)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#endif
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/**
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Flushes a chain of pages sharing the same access monitor.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @returns VBox status code suitable for scheduling.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPool The pool.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPage A page in the chain.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsyncint pgmPoolMonitorChainFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync{
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainFlush: Flush page %RGp type=%d\n", pPage->GCPhys, pPage->enmKind));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /*
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Find the list head.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uint16_t idx = pPage->idx;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync while (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync idx = pPage->iMonitoredPrev;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(idx != pPage->idx);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage = &pPool->aPages[idx];
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /*
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Iterate the list flushing each shadow page.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync int rc = VINF_SUCCESS;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync for (;;)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync idx = pPage->iMonitoredNext;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(idx != pPage->idx);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPage->idx >= PGMPOOL_IDX_FIRST)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync int rc2 = pgmPoolFlushPage(pPool, pPage);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync AssertRC(rc2);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* next */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (idx == NIL_PGMPOOL_IDX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync break;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage = &pPool->aPages[idx];
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return rc;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync}
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Wrapper for getting the current context pointer to the entry being modified.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @returns VBox status code suitable for scheduling.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pVM VM Handle.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pvDst Destination address
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pvSrc Source guest virtual address.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param GCPhysSrc The source guest physical address.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param cb Size of data to read
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsyncDECLINLINE(int) pgmPoolPhysSimpleReadGCPhys(PVM pVM, void *pvDst, CTXTYPE(RTGCPTR, RTHCPTR, RTGCPTR) pvSrc, RTGCPHYS GCPhysSrc, size_t cb)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync{
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#if defined(IN_RING3)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync memcpy(pvDst, (RTHCPTR)((uintptr_t)pvSrc & ~(RTHCUINTPTR)(cb - 1)), cb);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return VINF_SUCCESS;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#else
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* @todo in RC we could attempt to use the virtual address, although this can cause many faults (PAE Windows XP guest). */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return PGMPhysSimpleReadGCPhys(pVM, pvDst, GCPhysSrc & ~(RTGCPHYS)(cb - 1), cb);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#endif
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync}
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Process shadow entries before they are changed by the guest.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * For PT entries we will clear them. For PD entries, we'll simply check
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * for mapping conflicts and set the SyncCR3 FF if found.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pVCpu VMCPU handle
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPool The pool.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPage The head page.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param GCPhysFault The guest physical fault address.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param uAddress In R0 and GC this is the guest context fault address (flat).
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * In R3 this is the host context 'fault' address.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param cbWrite Write size; might be zero if the caller knows we're not crossing entry boundaries
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsyncvoid pgmPoolMonitorChainChanging(PVMCPU pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault, CTXTYPE(RTGCPTR, RTHCPTR, RTGCPTR) pvAddress, unsigned cbWrite)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync{
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync AssertMsg(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX, ("%#x (idx=%#x)\n", pPage->iMonitoredPrev, pPage->idx));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync const unsigned off = GCPhysFault & PAGE_OFFSET_MASK;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PVM pVM = pPool->CTX_SUFF(pVM);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync LogFlow(("pgmPoolMonitorChainChanging: %RGv phys=%RGp cbWrite=%d\n", (RTGCPTR)(CTXTYPE(RTGCPTR, uintptr_t, RTGCPTR))pvAddress, GCPhysFault, cbWrite));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync for (;;)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync union
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync void *pv;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PX86PT pPT;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PX86PTPAE pPTPae;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PX86PD pPD;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PX86PDPAE pPDPae;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PX86PDPT pPDPT;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PX86PML4 pPML4;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync } uShw;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync LogFlow(("pgmPoolMonitorChainChanging: page idx=%d phys=%RGp (next=%d) kind=%s\n", pPage->idx, pPage->GCPhys, pPage->iMonitoredNext, pgmPoolPoolKindToStr(pPage->enmKind), cbWrite));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync uShw.pv = NULL;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync switch (pPage->enmKind)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPT));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync const unsigned iShw = off / sizeof(X86PTE);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync LogFlow(("PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT iShw=%x\n", iShw));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (uShw.pPT->a[iShw].n.u1Present)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync X86PTE GstPte;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress, GCPhysFault, sizeof(GstPte));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync AssertRC(rc);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync 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));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync GstPte.u & X86_PTE_PG_MASK);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMAtomicWriteSize(&uShw.pPT->a[iShw], 0);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* page/2 sized */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPT));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (!((off ^ pPage->GCPhys) & (PAGE_SIZE / 2)))
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync const unsigned iShw = (off / sizeof(X86PTE)) & (X86_PG_PAE_ENTRIES - 1);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("PGMPOOLKIND_PAE_PT_FOR_32BIT_PT iShw=%x\n", iShw));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (uShw.pPTPae->a[iShw].n.u1Present)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync X86PTE GstPte;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress, GCPhysFault, sizeof(GstPte));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertRC(rc);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync 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));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pPTPae->a[iShw].u & X86_PTE_PAE_PG_MASK,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync GstPte.u & X86_PTE_PG_MASK);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMAtomicWriteSize(&uShw.pPTPae->a[iShw], 0);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync unsigned iGst = off / sizeof(X86PDE);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync unsigned iShwPdpt = iGst / 256;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync unsigned iShw = (iGst % 256) * 2;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync 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));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPD));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (iShwPdpt == pPage->enmKind - (unsigned)PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync for (unsigned i = 0; i < 2; i++)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# ifndef IN_RING0
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ((uShw.pPDPae->a[iShw + i].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShwPdpt=%#x iShw=%#x!\n", iShwPdpt, iShw+i));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync else
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# endif /* !IN_RING0 */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (uShw.pPDPae->a[iShw+i].n.u1Present)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw+i, uShw.pPDPae->a[iShw+i].u));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolFree(pVM,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pPDPae->a[iShw+i].u & X86_PDE_PAE_PG_MASK,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->idx,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync iShw + i);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMAtomicWriteSize(&uShw.pPDPae->a[iShw+i], 0);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* paranoia / a bit assumptive. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ( (off & 3)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync && (off & 3) + cbWrite > 4)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync const unsigned iShw2 = iShw + 2 + i;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (iShw2 < RT_ELEMENTS(uShw.pPDPae->a))
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# ifndef IN_RING0
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ((uShw.pPDPae->a[iShw2].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShwPdpt=%#x iShw2=%#x!\n", iShwPdpt, iShw2));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync else
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# endif /* !IN_RING0 */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (uShw.pPDPae->a[iShw2].n.u1Present)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolFree(pVM,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pPDPae->a[iShw2].u & X86_PDE_PAE_PG_MASK,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->idx,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync iShw2);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMAtomicWriteSize(&uShw.pPDPae->a[iShw2].u, 0);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync const unsigned iShw = off / sizeof(X86PTEPAE);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPT));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (uShw.pPTPae->a[iShw].n.u1Present)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync X86PTEPAE GstPte;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress, GCPhysFault, sizeof(GstPte));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertRC(rc);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync 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));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pPTPae->a[iShw].u & X86_PTE_PAE_PG_MASK,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync GstPte.u & X86_PTE_PAE_PG_MASK);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMAtomicWriteSize(&uShw.pPTPae->a[iShw].u, 0);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* paranoia / a bit assumptive. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ( (off & 7)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync && (off & 7) + cbWrite > sizeof(X86PTEPAE))
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PTEPAE);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertBreak(iShw2 < RT_ELEMENTS(uShw.pPTPae->a));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (uShw.pPTPae->a[iShw2].n.u1Present)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync X86PTEPAE GstPte;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# ifdef IN_RING3
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, (RTHCPTR)((RTHCUINTPTR)pvAddress + sizeof(GstPte)), GCPhysFault + sizeof(GstPte), sizeof(GstPte));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# else
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress + sizeof(GstPte), GCPhysFault + sizeof(GstPte), sizeof(GstPte));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# endif
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertRC(rc);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync 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));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pPTPae->a[iShw2].u & X86_PTE_PAE_PG_MASK,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync GstPte.u & X86_PTE_PAE_PG_MASK);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMAtomicWriteSize(&uShw.pPTPae->a[iShw2].u ,0);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync const unsigned iShw = off / sizeof(X86PTE); // ASSUMING 32-bit guest paging!
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: PGMPOOLKIND_32BIT_PD %x\n", iShw));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPD));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# ifndef IN_RING0
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (uShw.pPD->a[iShw].u & PGM_PDFLAGS_MAPPING)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# endif /* !IN_RING0 */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# ifndef IN_RING0
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync else
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# endif /* !IN_RING0 */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (uShw.pPD->a[iShw].n.u1Present)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: 32 bit pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPD->a[iShw].u));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolFree(pVM,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pPD->a[iShw].u & X86_PDE_PAE_PG_MASK,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->idx,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync iShw);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMAtomicWriteSize(&uShw.pPD->a[iShw].u, 0);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* paranoia / a bit assumptive. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ( (off & 3)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync && (off & 3) + cbWrite > sizeof(X86PTE))
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PTE);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ( iShw2 != iShw
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync && iShw2 < RT_ELEMENTS(uShw.pPD->a))
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# ifndef IN_RING0
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (uShw.pPD->a[iShw2].u & PGM_PDFLAGS_MAPPING)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# endif /* !IN_RING0 */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# ifndef IN_RING0
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync else
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# endif /* !IN_RING0 */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (uShw.pPD->a[iShw2].n.u1Present)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: 32 bit pd iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPD->a[iShw2].u));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolFree(pVM,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pPD->a[iShw2].u & X86_PDE_PAE_PG_MASK,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->idx,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync iShw2);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMAtomicWriteSize(&uShw.pPD->a[iShw2].u, 0);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#if 0 /* useful when running PGMAssertCR3(), a bit too troublesome for general use (TLBs). */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ( uShw.pPD->a[iShw].n.u1Present
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync && !VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3))
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: iShw=%#x: %RX32 -> freeing it!\n", iShw, uShw.pPD->a[iShw].u));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# ifdef IN_RC /* TLB load - we're pushing things a bit... */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMProbeReadByte(pvAddress);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# endif
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolFree(pVM, uShw.pPD->a[iShw].u & X86_PDE_PG_MASK, pPage->idx, iShw);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMAtomicWriteSize(&uShw.pPD->a[iShw].u, 0);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#endif
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync const unsigned iShw = off / sizeof(X86PDEPAE);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPD));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#ifndef IN_RING0
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (uShw.pPDPae->a[iShw].u & PGM_PDFLAGS_MAPPING)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#endif /* !IN_RING0 */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /*
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Causes trouble when the guest uses a PDE to refer to the whole page table level
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * structure. (Invalidate here; faults later on when it tries to change the page
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * table entries -> recheck; probably only applies to the RC case.)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# ifndef IN_RING0
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync else
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# endif /* !IN_RING0 */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (uShw.pPDPae->a[iShw].n.u1Present)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolFree(pVM,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pPDPae->a[iShw].u & X86_PDE_PAE_PG_MASK,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->idx,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync iShw);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMAtomicWriteSize(&uShw.pPDPae->a[iShw].u, 0);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* paranoia / a bit assumptive. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ( (off & 7)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync && (off & 7) + cbWrite > sizeof(X86PDEPAE))
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDEPAE);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertBreak(iShw2 < RT_ELEMENTS(uShw.pPDPae->a));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#ifndef IN_RING0
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ( iShw2 != iShw
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync && uShw.pPDPae->a[iShw2].u & PGM_PDFLAGS_MAPPING)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#endif /* !IN_RING0 */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# ifndef IN_RING0
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync else
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# endif /* !IN_RING0 */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (uShw.pPDPae->a[iShw2].n.u1Present)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolFree(pVM,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pPDPae->a[iShw2].u & X86_PDE_PAE_PG_MASK,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->idx,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync iShw2);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMAtomicWriteSize(&uShw.pPDPae->a[iShw2].u, 0);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PDPT:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPDPT));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /*
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Hopefully this doesn't happen very often:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * - touching unused parts of the page
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * - messing with the bits of pd pointers without changing the physical address
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* PDPT roots are not page aligned; 32 byte only! */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync const unsigned offPdpt = GCPhysFault - pPage->GCPhys;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync const unsigned iShw = offPdpt / sizeof(X86PDPE);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (iShw < X86_PG_PAE_PDPE_ENTRIES) /* don't use RT_ELEMENTS(uShw.pPDPT->a), because that's for long mode only */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# ifndef IN_RING0
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (uShw.pPDPT->a[iShw].u & PGM_PLXFLAGS_MAPPING)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected pdpt conflict at iShw=%#x!\n", iShw));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# endif /* !IN_RING0 */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# ifndef IN_RING0
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync else
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# endif /* !IN_RING0 */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (uShw.pPDPT->a[iShw].n.u1Present)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPT->a[iShw].u));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolFree(pVM,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pPDPT->a[iShw].u & X86_PDPE_PG_MASK,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->idx,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync iShw);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMAtomicWriteSize(&uShw.pPDPT->a[iShw].u, 0);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* paranoia / a bit assumptive. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ( (offPdpt & 7)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync && (offPdpt & 7) + cbWrite > sizeof(X86PDPE))
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync const unsigned iShw2 = (offPdpt + cbWrite - 1) / sizeof(X86PDPE);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ( iShw2 != iShw
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync && iShw2 < X86_PG_PAE_PDPE_ENTRIES)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# ifndef IN_RING0
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (uShw.pPDPT->a[iShw2].u & PGM_PLXFLAGS_MAPPING)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# endif /* !IN_RING0 */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# ifndef IN_RING0
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync else
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# endif /* !IN_RING0 */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (uShw.pPDPT->a[iShw2].n.u1Present)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPT->a[iShw2].u));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolFree(pVM,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pPDPT->a[iShw2].u & X86_PDPE_PG_MASK,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->idx,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync iShw2);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMAtomicWriteSize(&uShw.pPDPT->a[iShw2].u, 0);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#ifndef IN_RC
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPD));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync const unsigned iShw = off / sizeof(X86PDEPAE);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(!(uShw.pPDPae->a[iShw].u & PGM_PDFLAGS_MAPPING));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (uShw.pPDPae->a[iShw].n.u1Present)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolFree(pVM,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pPDPae->a[iShw].u & X86_PDE_PAE_PG_MASK,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->idx,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync iShw);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMAtomicWriteSize(&uShw.pPDPae->a[iShw].u, 0);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* paranoia / a bit assumptive. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ( (off & 7)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync && (off & 7) + cbWrite > sizeof(X86PDEPAE))
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDEPAE);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertBreak(iShw2 < RT_ELEMENTS(uShw.pPDPae->a));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(!(uShw.pPDPae->a[iShw2].u & PGM_PDFLAGS_MAPPING));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (uShw.pPDPae->a[iShw2].n.u1Present)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolFree(pVM,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pPDPae->a[iShw2].u & X86_PDE_PAE_PG_MASK,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->idx,
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync iShw2);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMAtomicWriteSize(&uShw.pPDPae->a[iShw2].u, 0);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPDPT));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /*
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Hopefully this doesn't happen very often:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * - messing with the bits of pd pointers without changing the physical address
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync const unsigned iShw = off / sizeof(X86PDPE);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (uShw.pPDPT->a[iShw].n.u1Present)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPT->a[iShw].u));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolFree(pVM, uShw.pPDPT->a[iShw].u & X86_PDPE_PG_MASK, pPage->idx, iShw);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMAtomicWriteSize(&uShw.pPDPT->a[iShw].u, 0);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* paranoia / a bit assumptive. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ( (off & 7)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync && (off & 7) + cbWrite > sizeof(X86PDPE))
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDPE);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (uShw.pPDPT->a[iShw2].n.u1Present)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPT->a[iShw2].u));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolFree(pVM, uShw.pPDPT->a[iShw2].u & X86_PDPE_PG_MASK, pPage->idx, iShw2);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMAtomicWriteSize(&uShw.pPDPT->a[iShw2].u, 0);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PML4:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPML4));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /*
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Hopefully this doesn't happen very often:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * - messing with the bits of pd pointers without changing the physical address
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync const unsigned iShw = off / sizeof(X86PDPE);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (uShw.pPML4->a[iShw].n.u1Present)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPML4->a[iShw].u));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolFree(pVM, uShw.pPML4->a[iShw].u & X86_PML4E_PG_MASK, pPage->idx, iShw);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMAtomicWriteSize(&uShw.pPML4->a[iShw].u, 0);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* paranoia / a bit assumptive. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ( (off & 7)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync && (off & 7) + cbWrite > sizeof(X86PDPE))
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PML4E);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (uShw.pPML4->a[iShw2].n.u1Present)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPML4->a[iShw2].u));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolFree(pVM, uShw.pPML4->a[iShw2].u & X86_PML4E_PG_MASK, pPage->idx, iShw2);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMAtomicWriteSize(&uShw.pPML4->a[iShw2].u, 0);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#endif /* IN_RING0 */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync default:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertFatalMsgFailed(("enmKind=%d\n", pPage->enmKind));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PGMPOOL_UNLOCK_PTR(pVM, uShw.pv);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* next */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (pPage->iMonitoredNext == NIL_PGMPOOL_IDX)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync return;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage = &pPool->aPages[pPage->iMonitoredNext];
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync}
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# ifndef IN_RING3
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/**
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Checks if a access could be a fork operation in progress.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Meaning, that the guest is setting up the parent process for Copy-On-Write.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @returns true if it's likly that we're forking, otherwise false.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pPool The pool.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pDis The disassembled instruction.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param offFault The access offset.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncDECLINLINE(bool) pgmPoolMonitorIsForking(PPGMPOOL pPool, PDISCPUSTATE pDis, unsigned offFault)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync{
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /*
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * i386 linux is using btr to clear X86_PTE_RW.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * The functions involved are (2.6.16 source inspection):
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * clear_bit
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * ptep_set_wrprotect
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * copy_one_pte
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * copy_pte_range
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * copy_pmd_range
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * copy_pud_range
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * copy_page_range
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * dup_mmap
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * dup_mm
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * copy_mm
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * copy_process
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * do_fork
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if ( pDis->pCurInstr->opcode == OP_BTR
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync && !(offFault & 4)
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync /** @todo Validate that the bit index is X86_PTE_RW. */
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync )
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync {
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,Fork));
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync return true;
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync }
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync return false;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync}
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync/**
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * Determine whether the page is likely to have been reused.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync *
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @returns true if we consider the page as being reused for a different purpose.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @returns false if we consider it to still be a paging page.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pVM VM Handle.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pVCpu VMCPU Handle.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pRegFrame Trap register frame.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pDis The disassembly info for the faulting instruction.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pvFault The fault address.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync *
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @remark The REP prefix check is left to the caller because of STOSD/W.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsyncDECLINLINE(bool) pgmPoolMonitorIsReused(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pDis, RTGCPTR pvFault)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync{
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync#ifndef IN_RC
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /** @todo could make this general, faulting close to rsp should be a safe reuse heuristic. */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if ( HWACCMHasPendingIrq(pVM)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync && (pRegFrame->rsp - pvFault) < 32)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /* Fault caused by stack writes while trying to inject an interrupt event. */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Log(("pgmPoolMonitorIsReused: reused %RGv for interrupt stack (rsp=%RGv).\n", pvFault, pRegFrame->rsp));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync return true;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#else
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync NOREF(pVM); NOREF(pvFault);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#endif
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync LogFlow(("Reused instr %RGv %d at %RGv param1.flags=%x param1.reg=%d\n", pRegFrame->rip, pDis->pCurInstr->opcode, pvFault, pDis->param1.flags, pDis->param1.base.reg_gen));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* Non-supervisor mode write means it's used for something else. */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (CPUMGetGuestCPL(pVCpu, pRegFrame) != 0)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return true;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync switch (pDis->pCurInstr->opcode)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /* call implies the actual push of the return address faulted */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case OP_CALL:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Log4(("pgmPoolMonitorIsReused: CALL\n"));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync return true;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case OP_PUSH:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Log4(("pgmPoolMonitorIsReused: PUSH\n"));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync return true;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case OP_PUSHF:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Log4(("pgmPoolMonitorIsReused: PUSHF\n"));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return true;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case OP_PUSHA:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Log4(("pgmPoolMonitorIsReused: PUSHA\n"));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return true;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case OP_FXSAVE:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Log4(("pgmPoolMonitorIsReused: FXSAVE\n"));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync return true;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case OP_MOVNTI: /* solaris - block_zero_no_xmm */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Log4(("pgmPoolMonitorIsReused: MOVNTI\n"));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync return true;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case OP_MOVNTDQ: /* solaris - hwblkclr & hwblkpagecopy */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Log4(("pgmPoolMonitorIsReused: MOVNTDQ\n"));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync return true;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case OP_MOVSWD:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case OP_STOSWD:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if ( pDis->prefix == (PREFIX_REP|PREFIX_REX)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync && pRegFrame->rcx >= 0x40
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync )
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Assert(pDis->mode == CPUMODE_64BIT);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Log(("pgmPoolMonitorIsReused: OP_STOSQ\n"));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync return true;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync return false;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if ( ( (pDis->param1.flags & USE_REG_GEN32)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync || (pDis->param1.flags & USE_REG_GEN64))
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync && (pDis->param1.base.reg_gen == USE_REG_ESP))
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Log4(("pgmPoolMonitorIsReused: ESP\n"));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync return true;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync return false;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync}
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync/**
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * Flushes the page being accessed.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync *
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @returns VBox status code suitable for scheduling.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pVM The VM handle.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pVCpu The VMCPU handle.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pPool The pool.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pPage The pool page (head).
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pDis The disassembly of the write instruction.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pRegFrame The trap register frame.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param GCPhysFault The fault address as guest physical address.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pvFault The fault address.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsyncstatic int pgmPoolAccessHandlerFlush(PVM pVM, PVMCPU pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pDis,
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync{
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /*
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * First, do the flushing.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync int rc = pgmPoolMonitorChainFlush(pPool, pPage);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /*
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * Emulate the instruction (xp/w2k problem, requires pc/cr2/sp detection). Must do this in raw mode (!); XP boot will fail otherwise
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync uint32_t cbWritten;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync int rc2 = EMInterpretInstructionCPUEx(pVM, pVCpu, pDis, pRegFrame, pvFault, &cbWritten, EMCODETYPE_ALL);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if (RT_SUCCESS(rc2))
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pRegFrame->rip += pDis->opsize;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync else if (rc2 == VERR_EM_INTERPRETER)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync#ifdef IN_RC
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if (PATMIsPatchGCAddr(pVM, pRegFrame->eip))
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for patch code %04x:%RGv, ignoring.\n",
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pRegFrame->cs, (RTGCPTR)pRegFrame->eip));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync rc = VINF_SUCCESS;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync STAM_COUNTER_INC(&pPool->StatMonitorRZIntrFailPatch2);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync else
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync#endif
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync rc = VINF_EM_RAW_EMULATE_INSTR;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,EmulateInstr));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync else
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync rc = rc2;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync LogFlow(("pgmPoolAccessHandlerPT: returns %Rrc (flushed)\n", rc));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return rc;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync}
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync/**
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * Handles the STOSD write accesses.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync *
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @returns VBox status code suitable for scheduling.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pVM The VM handle.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pPool The pool.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pPage The pool page (head).
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pDis The disassembly of the write instruction.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pRegFrame The trap register frame.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param GCPhysFault The fault address as guest physical address.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pvFault The fault address.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsyncDECLINLINE(int) pgmPoolAccessHandlerSTOSD(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pDis,
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync{
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync unsigned uIncrement = pDis->param1.size;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Assert(pDis->mode == CPUMODE_32BIT || pDis->mode == CPUMODE_64BIT);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Assert(pRegFrame->rcx <= 0x20);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#ifdef VBOX_STRICT
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (pDis->opmode == CPUMODE_32BIT)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(uIncrement == 4);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync else
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Assert(uIncrement == 8);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync#endif
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Log3(("pgmPoolAccessHandlerSTOSD\n"));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /*
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * Increment the modification counter and insert it into the list
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * of modified pages the first time.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if (!pPage->cModifications++)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolMonitorModifiedInsert(pPool, pPage);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /*
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * Execute REP STOSD.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * This ASSUMES that we're not invoked by Trap0e on in a out-of-sync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * write situation, meaning that it's safe to write here.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync PVMCPU pVCpu = VMMGetCpu(pPool->CTX_SUFF(pVM));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync RTGCUINTPTR pu32 = (RTGCUINTPTR)pvFault;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync while (pRegFrame->rcx)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, (RTGCPTR)pu32, uIncrement);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync PGMDynMapPopAutoSubset(pVCpu, iPrevSubset);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync#else
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, (RTGCPTR)pu32, uIncrement);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#endif
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#ifdef IN_RC
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync *(uint32_t *)(uintptr_t)pu32 = pRegFrame->eax;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync#else
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync PGMPhysSimpleWriteGCPhys(pVM, GCPhysFault, &pRegFrame->rax, uIncrement);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync#endif
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pu32 += uIncrement;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync GCPhysFault += uIncrement;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pRegFrame->rdi += uIncrement;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pRegFrame->rcx--;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pRegFrame->rip += pDis->opsize;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync LogFlow(("pgmPoolAccessHandlerSTOSD: returns\n"));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync return VINF_SUCCESS;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync}
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync/**
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * Handles the simple write accesses.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync *
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @returns VBox status code suitable for scheduling.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pVM The VM handle.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pVCpu The VMCPU handle.
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync * @param pPool The pool.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPage The pool page (head).
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pDis The disassembly of the write instruction.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pRegFrame The trap register frame.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param GCPhysFault The fault address as guest physical address.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pvFault The fault address.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pfReused Reused state (out)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsyncDECLINLINE(int) pgmPoolAccessHandlerSimple(PVM pVM, PVMCPU pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pDis,
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault, bool *pfReused)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync{
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Log3(("pgmPoolAccessHandlerSimple\n"));
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync /*
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * Increment the modification counter and insert it into the list
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * of modified pages the first time.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if (!pPage->cModifications++)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pgmPoolMonitorModifiedInsert(pPool, pPage);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /*
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * Clear all the pages. ASSUMES that pvFault is readable.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, pvFault, DISGetParamSize(pDis, &pDis->param1));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync PGMDynMapPopAutoSubset(pVCpu, iPrevSubset);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync#else
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, pvFault, DISGetParamSize(pDis, &pDis->param1));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync#endif
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /*
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * Interpret the instruction.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync uint32_t cb;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync int rc = EMInterpretInstructionCPUEx(pVM, pVCpu, pDis, pRegFrame, pvFault, &cb, EMCODETYPE_ALL);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if (RT_SUCCESS(rc))
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pRegFrame->rip += pDis->opsize;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync else if (rc == VERR_EM_INTERPRETER)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for %04x:%RGv - opcode=%d\n",
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pRegFrame->cs, (RTGCPTR)pRegFrame->rip, pDis->pCurInstr->opcode));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync rc = VINF_EM_RAW_EMULATE_INSTR;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,EmulateInstr));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#if 0 /* experimental code */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (rc == VINF_SUCCESS)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync switch (pPage->enmKind)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync X86PTEPAE GstPte;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvFault, GCPhysFault, sizeof(GstPte));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync AssertRC(rc);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /* Check the new value written by the guest. If present and with a bogus physical address, then
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * it's fairly safe to assume the guest is reusing the PT.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if (GstPte.n.u1Present)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync RTHCPHYS HCPhys = -1;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync int rc = PGMPhysGCPhys2HCPhys(pVM, GstPte.u & X86_PTE_PAE_PG_MASK, &HCPhys);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if (rc != VINF_SUCCESS)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *pfReused = true;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pPool->StatForceFlushReused);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync break;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync#endif
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolAccessHandlerSimple: returns %Rrc cb=%d\n", rc, cb));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync return rc;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync}
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync/**
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * \#PF Handler callback for PT write accesses.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @returns VBox status code (appropriate for GC return).
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pVM VM Handle.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param uErrorCode CPU Error code.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pRegFrame Trap register frame.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * NULL on DMA and other non CPU access.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pvFault The fault address (cr2).
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param GCPhysFault The GC physical address corresponding to pvFault.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pvUser User argument.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsyncDECLEXPORT(int) pgmPoolAccessHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync{
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync STAM_PROFILE_START(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), a);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PPGMPOOLPAGE pPage = (PPGMPOOLPAGE)pvUser;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PVMCPU pVCpu = VMMGetCpu(pVM);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync unsigned cMaxModifications;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync bool fForcedFlush = false;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolAccessHandler: pvFault=%RGv pPage=%p:{.idx=%d} GCPhysFault=%RGp\n", pvFault, pPage, pPage->idx, GCPhysFault));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pgmLock(pVM);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if (PHYS_PAGE_ADDRESS(GCPhysFault) != PHYS_PAGE_ADDRESS(pPage->GCPhys))
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /* Pool page changed while we were waiting for the lock; ignore. */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Log(("CPU%d: pgmPoolAccessHandler pgm pool page for %RGp changed (to %RGp) while waiting!\n", pVCpu->idCpu, PHYS_PAGE_ADDRESS(GCPhysFault), PHYS_PAGE_ADDRESS(pPage->GCPhys)));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,Handled), a);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pgmUnlock(pVM);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync return VINF_SUCCESS;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPage->fDirty)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(VMCPU_FF_ISSET(pVCpu, VMCPU_FF_TLB_FLUSH));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmUnlock(pVM);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync return VINF_SUCCESS; /* SMP guest case where we were blocking on the pgm lock while the same page was being marked dirty. */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync#endif
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#if 0 /* test code defined(VBOX_STRICT) && defined(PGMPOOL_WITH_OPTIMIZED_DIRTY_PT) */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if (pPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_PAE_PT)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync void *pvShw = PGMPOOL_PAGE_2_LOCKED_PTR(pPool->CTX_SUFF(pVM), pPage);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync void *pvGst;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync int rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pgmPoolTrackCheckPTPaePae(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PTPAE)pvGst);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync#endif
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /*
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * Disassemble the faulting instruction.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PDISCPUSTATE pDis = &pVCpu->pgm.s.DisState;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync int rc = EMInterpretDisasOne(pVM, pVCpu, pRegFrame, pDis, NULL);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if (RT_UNLIKELY(rc != VINF_SUCCESS))
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync AssertMsg(rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("Unexpected rc %d\n", rc));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pgmUnlock(pVM);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync return rc;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Assert(pPage->enmKind != PGMPOOLKIND_FREE);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /*
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * We should ALWAYS have the list head as user parameter. This
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * is because we use that page to record the changes.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Assert(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync#ifdef IN_RING0
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /* Maximum nr of modifications depends on the page type. */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if (pPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_PAE_PT)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync cMaxModifications = 4;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync else
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync cMaxModifications = 24;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync#else
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync cMaxModifications = 48;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync#endif
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /*
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * Incremental page table updates should weight more than random ones.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * (Only applies when started from offset 0)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pVCpu->pgm.s.cPoolAccessHandler++;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if ( pPage->pvLastAccessHandlerRip >= pRegFrame->rip - 0x40 /* observed loops in Windows 7 x64 */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync && pPage->pvLastAccessHandlerRip < pRegFrame->rip + 0x40
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync && pvFault == (pPage->pvLastAccessHandlerFault + pDis->param1.size)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync && pVCpu->pgm.s.cPoolAccessHandler == (pPage->cLastAccessHandlerCount + 1))
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Log(("Possible page reuse cMods=%d -> %d (locked=%d type=%s)\n", pPage->cModifications, pPage->cModifications * 2, pgmPoolIsPageLocked(&pVM->pgm.s, pPage), pgmPoolPoolKindToStr(pPage->enmKind)));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage->cModifications = pPage->cModifications * 2;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pPage->pvLastAccessHandlerFault = pvFault;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pPage->cLastAccessHandlerCount = pVCpu->pgm.s.cPoolAccessHandler;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if (pPage->cModifications >= cMaxModifications)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FlushReinit));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync fForcedFlush = true;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if (pPage->cModifications >= cMaxModifications)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Log(("Mod overflow %RGv cMods=%d (locked=%d type=%s)\n", pvFault, pPage->cModifications, pgmPoolIsPageLocked(&pVM->pgm.s, pPage), pgmPoolPoolKindToStr(pPage->enmKind)));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /*
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * Check if it's worth dealing with.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync bool fReused = false;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync bool fNotReusedNotForking = false;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if ( ( pPage->cModifications < cMaxModifications /** @todo #define */ /** @todo need to check that it's not mapping EIP. */ /** @todo adjust this! */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync || pgmPoolIsPageLocked(&pVM->pgm.s, pPage)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync )
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync && !(fReused = pgmPoolMonitorIsReused(pVM, pVCpu, pRegFrame, pDis, pvFault))
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync && !pgmPoolMonitorIsForking(pPool, pDis, GCPhysFault & PAGE_OFFSET_MASK))
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /*
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * Simple instructions, no REP prefix.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if (!(pDis->prefix & (PREFIX_REP | PREFIX_REPNE)))
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync rc = pgmPoolAccessHandlerSimple(pVM, pVCpu, pPool, pPage, pDis, pRegFrame, GCPhysFault, pvFault, &fReused);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if (fReused)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync goto flushPage;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /* A mov instruction to change the first page table entry will be remembered so we can detect
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * full page table changes early on. This will reduce the amount of unnecessary traps we'll take.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if ( rc == VINF_SUCCESS
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync && pDis->pCurInstr->opcode == OP_MOV
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync && (pvFault & PAGE_OFFSET_MASK) == 0)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pPage->pvLastAccessHandlerFault = pvFault;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pPage->cLastAccessHandlerCount = pVCpu->pgm.s.cPoolAccessHandler;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pPage->pvLastAccessHandlerRip = pRegFrame->rip;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /* Make sure we don't kick out a page too quickly. */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if (pPage->cModifications > 8)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pPage->cModifications = 2;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync else
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if (pPage->pvLastAccessHandlerFault == pvFault)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /* ignore the 2nd write to this page table entry. */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pPage->cLastAccessHandlerCount = pVCpu->pgm.s.cPoolAccessHandler;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync else
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pPage->pvLastAccessHandlerFault = 0;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pPage->pvLastAccessHandlerRip = 0;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,Handled), a);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pgmUnlock(pVM);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync return rc;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /*
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Windows is frequently doing small memset() operations (netio test 4k+).
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * We have to deal with these or we'll kill the cache and performance.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if ( pDis->pCurInstr->opcode == OP_STOSWD
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync && !pRegFrame->eflags.Bits.u1DF
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync && pDis->opmode == pDis->mode
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync && pDis->addrmode == pDis->mode)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync bool fValidStosd = false;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ( pDis->mode == CPUMODE_32BIT
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync && pDis->prefix == PREFIX_REP
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync && pRegFrame->ecx <= 0x20
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync && pRegFrame->ecx * 4 <= PAGE_SIZE - ((uintptr_t)pvFault & PAGE_OFFSET_MASK)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync && !((uintptr_t)pvFault & 3)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync && (pRegFrame->eax == 0 || pRegFrame->eax == 0x80) /* the two values observed. */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync )
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync fValidStosd = true;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pRegFrame->rcx &= 0xffffffff; /* paranoia */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync else
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if ( pDis->mode == CPUMODE_64BIT
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync && pDis->prefix == (PREFIX_REP | PREFIX_REX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync && pRegFrame->rcx <= 0x20
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync && pRegFrame->rcx * 8 <= PAGE_SIZE - ((uintptr_t)pvFault & PAGE_OFFSET_MASK)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync && !((uintptr_t)pvFault & 7)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync && (pRegFrame->rax == 0 || pRegFrame->rax == 0x80) /* the two values observed. */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync )
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync fValidStosd = true;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (fValidStosd)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync rc = pgmPoolAccessHandlerSTOSD(pVM, pPool, pPage, pDis, pRegFrame, GCPhysFault, pvFault);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,RepStosd), a);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmUnlock(pVM);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return rc;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* REP prefix, don't bother. */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,RepPrefix));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Log4(("pgmPoolAccessHandler: eax=%#x ecx=%#x edi=%#x esi=%#x rip=%RGv opcode=%d prefix=%#x\n",
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pRegFrame->eax, pRegFrame->ecx, pRegFrame->edi, pRegFrame->esi, (RTGCPTR)pRegFrame->rip, pDis->pCurInstr->opcode, pDis->prefix));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync fNotReusedNotForking = true;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#if defined(PGMPOOL_WITH_OPTIMIZED_DIRTY_PT) && defined(IN_RING0)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* E.g. Windows 7 x64 initializes page tables and touches some pages in the table during the process. This
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * leads to pgm pool trashing and an excessive amount of write faults due to page monitoring.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if ( pPage->cModifications >= cMaxModifications
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync && !fForcedFlush
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync && pPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_PAE_PT
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync && ( fNotReusedNotForking
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync || ( !pgmPoolMonitorIsReused(pVM, pVCpu, pRegFrame, pDis, pvFault)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync && !pgmPoolMonitorIsForking(pPool, pDis, GCPhysFault & PAGE_OFFSET_MASK))
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync )
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync )
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(!pgmPoolIsPageLocked(&pVM->pgm.s, pPage));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(pPage->fDirty == false);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* Flush any monitored duplicates as we will disable write protection. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ( pPage->iMonitoredNext != NIL_PGMPOOL_IDX
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync || pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PPGMPOOLPAGE pPageHead = pPage;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* Find the monitor head. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync while (pPageHead->iMonitoredPrev != NIL_PGMPOOL_IDX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPageHead = &pPool->aPages[pPageHead->iMonitoredPrev];
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync while (pPageHead)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync unsigned idxNext = pPageHead->iMonitoredNext;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPageHead != pPage)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync STAM_COUNTER_INC(&pPool->StatDirtyPageDupFlush);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log(("Flush duplicate page idx=%d GCPhys=%RGp type=%s\n", pPageHead->idx, pPageHead->GCPhys, pgmPoolPoolKindToStr(pPageHead->enmKind)));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync int rc2 = pgmPoolFlushPage(pPool, pPageHead);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertRC(rc2);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (idxNext == NIL_PGMPOOL_IDX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync break;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPageHead = &pPool->aPages[idxNext];
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* The flushing above might fail for locked pages, so double check. */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if ( pPage->iMonitoredNext == NIL_PGMPOOL_IDX
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync && pPage->iMonitoredPrev == NIL_PGMPOOL_IDX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolAddDirtyPage(pVM, pPool, pPage);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* Temporarily allow write access to the page table again. */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync rc = PGMHandlerPhysicalPageTempOff(pVM, pPage->GCPhys, pPage->GCPhys);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (rc == VINF_SUCCESS)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync rc = PGMShwModifyPage(pVCpu, pvFault, 1, X86_PTE_RW, ~(uint64_t)X86_PTE_RW);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync AssertMsg(rc == VINF_SUCCESS
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* In the SMP case the page table might be removed while we wait for the PGM lock in the trap handler. */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync || rc == VERR_PAGE_TABLE_NOT_PRESENT
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync || rc == VERR_PAGE_NOT_PRESENT,
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync ("PGMShwModifyPage -> GCPtr=%RGv rc=%d\n", pvFault, rc));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage->pvDirtyFault = pvFault;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync STAM_PROFILE_STOP(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), a);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmUnlock(pVM);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return rc;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#endif /* PGMPOOL_WITH_OPTIMIZED_DIRTY_PT */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FlushModOverflow));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncflushPage:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /*
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Not worth it, so flush it.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * If we considered it to be reused, don't go back to ring-3
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * to emulate failed instructions since we usually cannot
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * interpret then. This may be a bit risky, in which case
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * the reuse detection must be fixed.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync rc = pgmPoolAccessHandlerFlush(pVM, pVCpu, pPool, pPage, pDis, pRegFrame, GCPhysFault, pvFault);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ( rc == VINF_EM_RAW_EMULATE_INSTR
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync && fReused)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* Make sure that the current instruction still has shadow page backing, otherwise we'll end up in a loop. */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (PGMShwGetPage(pVCpu, pRegFrame->rip, NULL, NULL) == VINF_SUCCESS)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync rc = VINF_SUCCESS; /* safe to restart the instruction. */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,FlushPage), a);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmUnlock(pVM);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return rc;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync}
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync# endif /* !IN_RING3 */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync# ifdef VBOX_STRICT
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Check references to guest physical memory in a PAE / PAE page table.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPool The pool.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPage The page.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pShwPT The shadow page table (mapping of the page).
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pGstPT The guest page table.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsyncstatic void pgmPoolTrackCheckPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PTPAE pGstPT)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync{
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync unsigned cErrors = 0;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync int LastRc = -1; /* initialized to shut up gcc */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync unsigned LastPTE = ~0U; /* initialized to shut up gcc */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync RTHCPHYS LastHCPhys = NIL_RTHCPHYS; /* initialized to shut up gcc */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#ifdef VBOX_STRICT
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync for (unsigned i = 0; i < RT_MIN(RT_ELEMENTS(pShwPT->a), pPage->iFirstPresent); i++)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertMsg(!pShwPT->a[i].n.u1Present, ("Unexpected PTE: idx=%d %RX64 (first=%d)\n", i, pShwPT->a[i].u, pPage->iFirstPresent));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#endif
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pShwPT->a[i].n.u1Present)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync RTHCPHYS HCPhys = NIL_RTHCPHYS;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync int rc = PGMPhysGCPhys2HCPhys(pPool->CTX_SUFF(pVM), pGstPT->a[i].u & X86_PTE_PAE_PG_MASK, &HCPhys);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if ( rc != VINF_SUCCESS
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync || (pShwPT->a[i].u & X86_PTE_PAE_PG_MASK) != HCPhys)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log(("rc=%d idx=%d guest %RX64 shw=%RX64 vs %RHp\n", rc, i, pGstPT->a[i].u, pShwPT->a[i].u, HCPhys));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync LastPTE = i;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LastRc = rc;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LastHCPhys = HCPhys;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync cErrors++;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync RTHCPHYS HCPhysPT = NIL_RTHCPHYS;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync rc = PGMPhysGCPhys2HCPhys(pPool->CTX_SUFF(pVM), pPage->GCPhys, &HCPhysPT);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync AssertRC(rc);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync for (unsigned iPage = 0; iPage < pPool->cCurPages; iPage++)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PPGMPOOLPAGE pTempPage = &pPool->aPages[iPage];
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (pTempPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_PAE_PT)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PX86PTPAE pShwPT2 = (PX86PTPAE)PGMPOOL_PAGE_2_LOCKED_PTR(pPool->CTX_SUFF(pVM), pTempPage);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync for (unsigned j = 0; j < RT_ELEMENTS(pShwPT->a); j++)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ( pShwPT2->a[j].n.u1Present
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync && pShwPT2->a[j].n.u1Write
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync && ((pShwPT2->a[j].u & X86_PTE_PAE_PG_MASK) == HCPhysPT))
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log(("GCPhys=%RGp idx=%d %RX64 vs %RX64\n", pTempPage->GCPhys, j, pShwPT->a[j].u, pShwPT2->a[j].u));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertMsg(!cErrors, ("cErrors=%d: last rc=%d idx=%d guest %RX64 shw=%RX64 vs %RHp\n", cErrors, LastRc, LastPTE, pGstPT->a[LastPTE].u, pShwPT->a[LastPTE].u, LastHCPhys));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync}
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync# endif /* VBOX_STRICT */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Clear references to guest physical memory in a PAE / PAE page table.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @returns nr of changed PTEs
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPool The pool.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPage The page.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pShwPT The shadow page table (mapping of the page).
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pGstPT The guest page table.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pOldGstPT The old cached guest page table.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param fAllowRemoval Bail out as soon as we encounter an invalid PTE
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pfFlush Flush reused page table (out)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsyncDECLINLINE(unsigned) pgmPoolTrackFlushPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PTPAE pGstPT, PCX86PTPAE pOldGstPT, bool fAllowRemoval, bool *pfFlush)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync{
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync unsigned cChanged = 0;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#ifdef VBOX_STRICT
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync for (unsigned i = 0; i < RT_MIN(RT_ELEMENTS(pShwPT->a), pPage->iFirstPresent); i++)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync AssertMsg(!pShwPT->a[i].n.u1Present, ("Unexpected PTE: idx=%d %RX64 (first=%d)\n", i, pShwPT->a[i].u, pPage->iFirstPresent));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#endif
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *pfFlush = false;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* Check the new value written by the guest. If present and with a bogus physical address, then
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * it's fairly safe to assume the guest is reusing the PT.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if ( fAllowRemoval
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync && pGstPT->a[i].n.u1Present)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (!PGMPhysIsGCPhysValid(pPool->CTX_SUFF(pVM), pGstPT->a[i].u & X86_PTE_PAE_PG_MASK))
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *pfFlush = true;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return ++cChanged;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pShwPT->a[i].n.u1Present)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* If the old cached PTE is identical, then there's no need to flush the shadow copy. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ((pGstPT->a[i].u & X86_PTE_PAE_PG_MASK) == (pOldGstPT->a[i].u & X86_PTE_PAE_PG_MASK))
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#ifdef VBOX_STRICT
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync RTHCPHYS HCPhys = NIL_RTGCPHYS;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync int rc = PGMPhysGCPhys2HCPhys(pPool->CTX_SUFF(pVM), pGstPT->a[i].u & X86_PTE_PAE_PG_MASK, &HCPhys);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync AssertMsg(rc == VINF_SUCCESS && (pShwPT->a[i].u & X86_PTE_PAE_PG_MASK) == HCPhys, ("rc=%d guest %RX64 old %RX64 shw=%RX64 vs %RHp\n", rc, pGstPT->a[i].u, pOldGstPT->a[i].u, pShwPT->a[i].u, HCPhys));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#endif
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync uint64_t uHostAttr = pShwPT->a[i].u & (X86_PTE_P | X86_PTE_US | X86_PTE_A | X86_PTE_D | X86_PTE_G | X86_PTE_PAE_NX);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync bool fHostRW = !!(pShwPT->a[i].u & X86_PTE_RW);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync uint64_t uGuestAttr = pGstPT->a[i].u & (X86_PTE_P | X86_PTE_US | X86_PTE_A | X86_PTE_D | X86_PTE_G | X86_PTE_PAE_NX);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync bool fGuestRW = !!(pGstPT->a[i].u & X86_PTE_RW);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ( uHostAttr == uGuestAttr
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync && fHostRW <= fGuestRW)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync continue;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync cChanged++;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* Something was changed, so flush it. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Log4(("pgmPoolTrackDerefPTPaePae: i=%d pte=%RX64 hint=%RX64\n",
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pOldGstPT->a[i].u & X86_PTE_PAE_PG_MASK));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pOldGstPT->a[i].u & X86_PTE_PAE_PG_MASK);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync ASMAtomicWriteSize(&pShwPT->a[i].u, 0);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return cChanged;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync}
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Flush a dirty page
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pVM VM Handle.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPool The pool.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param idxSlot Dirty array slot index
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param fAllowRemoval Allow a reused page table to be removed
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsyncstatic void pgmPoolFlushDirtyPage(PVM pVM, PPGMPOOL pPool, unsigned idxSlot, bool fAllowRemoval = false)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync{
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PPGMPOOLPAGE pPage;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync unsigned idxPage;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(idxSlot < RT_ELEMENTS(pPool->aIdxDirtyPages));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPool->aIdxDirtyPages[idxSlot] == NIL_PGMPOOL_IDX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync idxPage = pPool->aIdxDirtyPages[idxSlot];
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync AssertRelease(idxPage != NIL_PGMPOOL_IDX);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage = &pPool->aPages[idxPage];
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(pPage->idx == idxPage);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(pPage->iMonitoredNext == NIL_PGMPOOL_IDX && pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync AssertMsg(pPage->fDirty, ("Page %RGp (slot=%d) not marked dirty!", pPage->GCPhys, idxSlot));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log(("Flush dirty page %RGp cMods=%d\n", pPage->GCPhys, pPage->cModifications));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* First write protect the page again to catch all write accesses. (before checking for changes -> SMP) */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync int rc = PGMHandlerPhysicalReset(pVM, pPage->GCPhys);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(rc == VINF_SUCCESS);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage->fDirty = false;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#ifdef VBOX_STRICT
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync uint64_t fFlags = 0;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync RTHCPHYS HCPhys;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync rc = PGMShwGetPage(VMMGetCpu(pVM), pPage->pvDirtyFault, &fFlags, &HCPhys);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertMsg( ( rc == VINF_SUCCESS
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync && (!(fFlags & X86_PTE_RW) || HCPhys != pPage->Core.Key))
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* In the SMP case the page table might be removed while we wait for the PGM lock in the trap handler. */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync || rc == VERR_PAGE_TABLE_NOT_PRESENT
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync || rc == VERR_PAGE_NOT_PRESENT,
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync ("PGMShwGetPage -> GCPtr=%RGv rc=%d flags=%RX64\n", pPage->pvDirtyFault, rc, fFlags));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#endif
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* Flush those PTEs that have changed. */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync STAM_PROFILE_START(&pPool->StatTrackDeref,a);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync void *pvShw = PGMPOOL_PAGE_2_LOCKED_PTR(pPool->CTX_SUFF(pVM), pPage);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync void *pvGst;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync bool fFlush;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync unsigned cChanges = pgmPoolTrackFlushPTPaePae(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PTPAE)pvGst, (PCX86PTPAE)&pPool->aDirtyPages[idxSlot][0], fAllowRemoval, &fFlush);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync STAM_PROFILE_STOP(&pPool->StatTrackDeref,a);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /** Note: we might want to consider keeping the dirty page active in case there were many changes. */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* This page is likely to be modified again, so reduce the nr of modifications just a bit here. */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(pPage->cModifications);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (cChanges < 4)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage->cModifications = 1; /* must use > 0 here */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync else
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->cModifications = RT_MAX(1, pPage->cModifications / 2);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync STAM_COUNTER_INC(&pPool->StatResetDirtyPages);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPool->cDirtyPages == RT_ELEMENTS(pPool->aIdxDirtyPages))
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPool->idxFreeDirtyPage = idxSlot;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->cDirtyPages--;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->aIdxDirtyPages[idxSlot] = NIL_PGMPOOL_IDX;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(pPool->cDirtyPages <= RT_ELEMENTS(pPool->aIdxDirtyPages));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (fFlush)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(fAllowRemoval);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log(("Flush reused page table!\n"));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmPoolFlushPage(pPool, pPage);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pPool->StatForceFlushReused);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync else
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log(("Removed dirty page %RGp cMods=%d cChanges=%d\n", pPage->GCPhys, pPage->cModifications, cChanges));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync}
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync# ifndef IN_RING3
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Add a new dirty page
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pVM VM Handle.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pPool The pool.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pPage The page.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsyncvoid pgmPoolAddDirtyPage(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync{
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync unsigned idxFree;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(PGMIsLocked(pVM));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync AssertCompile(RT_ELEMENTS(pPool->aIdxDirtyPages) == 8 || RT_ELEMENTS(pPool->aIdxDirtyPages) == 16);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(!pPage->fDirty);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync idxFree = pPool->idxFreeDirtyPage;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(idxFree < RT_ELEMENTS(pPool->aIdxDirtyPages));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(pPage->iMonitoredNext == NIL_PGMPOOL_IDX && pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPool->cDirtyPages >= RT_ELEMENTS(pPool->aIdxDirtyPages))
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync STAM_COUNTER_INC(&pPool->StatDirtyPageOverFlowFlush);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmPoolFlushDirtyPage(pVM, pPool, idxFree, true /* allow removal of reused page tables*/);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(pPool->cDirtyPages < RT_ELEMENTS(pPool->aIdxDirtyPages));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync AssertMsg(pPool->aIdxDirtyPages[idxFree] == NIL_PGMPOOL_IDX, ("idxFree=%d cDirtyPages=%d\n", idxFree, pPool->cDirtyPages));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log(("Add dirty page %RGp (slot=%d)\n", pPage->GCPhys, idxFree));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* Make a copy of the guest page table as we require valid GCPhys addresses when removing
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * references to physical pages. (the HCPhys linear lookup is *extremely* expensive!)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync void *pvShw = PGMPOOL_PAGE_2_LOCKED_PTR(pPool->CTX_SUFF(pVM), pPage);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync void *pvGst;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync int rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync memcpy(&pPool->aDirtyPages[idxFree][0], pvGst, PAGE_SIZE);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#ifdef VBOX_STRICT
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmPoolTrackCheckPTPaePae(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PTPAE)pvGst);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#endif
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync STAM_COUNTER_INC(&pPool->StatDirtyPage);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage->fDirty = true;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage->idxDirty = idxFree;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->aIdxDirtyPages[idxFree] = pPage->idx;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->cDirtyPages++;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->idxFreeDirtyPage = (pPool->idxFreeDirtyPage + 1) & (RT_ELEMENTS(pPool->aIdxDirtyPages) - 1);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if ( pPool->cDirtyPages < RT_ELEMENTS(pPool->aIdxDirtyPages)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync && pPool->aIdxDirtyPages[pPool->idxFreeDirtyPage] != NIL_PGMPOOL_IDX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync unsigned i;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync for (i = 1; i < RT_ELEMENTS(pPool->aIdxDirtyPages); i++)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync idxFree = (pPool->idxFreeDirtyPage + i) & (RT_ELEMENTS(pPool->aIdxDirtyPages) - 1);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPool->aIdxDirtyPages[idxFree] == NIL_PGMPOOL_IDX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->idxFreeDirtyPage = idxFree;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync break;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(i != RT_ELEMENTS(pPool->aIdxDirtyPages));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(pPool->cDirtyPages == RT_ELEMENTS(pPool->aIdxDirtyPages) || pPool->aIdxDirtyPages[pPool->idxFreeDirtyPage] == NIL_PGMPOOL_IDX);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync}
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync# endif /* !IN_RING3 */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Check if the specified page is dirty (not write monitored)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @return dirty or not
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pVM VM Handle.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param GCPhys Guest physical address
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsyncbool pgmPoolIsDirtyPage(PVM pVM, RTGCPHYS GCPhys)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync{
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(PGMIsLocked(pVM));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (!pPool->cDirtyPages)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return false;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync GCPhys = GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPool->aIdxDirtyPages); i++)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (pPool->aIdxDirtyPages[i] != NIL_PGMPOOL_IDX)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PPGMPOOLPAGE pPage;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync unsigned idxPage = pPool->aIdxDirtyPages[i];
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage = &pPool->aPages[idxPage];
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPage->GCPhys == GCPhys)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return true;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync return false;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync}
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Reset all dirty pages by reinstating page monitoring.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pVM VM Handle.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsyncvoid pgmPoolResetDirtyPages(PVM pVM)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync{
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(PGMIsLocked(pVM));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(pPool->cDirtyPages <= RT_ELEMENTS(pPool->aIdxDirtyPages));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if (!pPool->cDirtyPages)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync return;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Log(("pgmPoolResetDirtyPages\n"));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPool->aIdxDirtyPages); i++)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolFlushDirtyPage(pVM, pPool, i, true /* allow removal of reused page tables*/);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pPool->idxFreeDirtyPage = 0;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if ( pPool->cDirtyPages != RT_ELEMENTS(pPool->aIdxDirtyPages)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync && pPool->aIdxDirtyPages[pPool->idxFreeDirtyPage] != NIL_PGMPOOL_IDX)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync unsigned i;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync for (i = 1; i < RT_ELEMENTS(pPool->aIdxDirtyPages); i++)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if (pPool->aIdxDirtyPages[i] == NIL_PGMPOOL_IDX)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pPool->idxFreeDirtyPage = i;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync break;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync AssertMsg(i != RT_ELEMENTS(pPool->aIdxDirtyPages), ("cDirtyPages %d", pPool->cDirtyPages));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(pPool->aIdxDirtyPages[pPool->idxFreeDirtyPage] == NIL_PGMPOOL_IDX || pPool->cDirtyPages == RT_ELEMENTS(pPool->aIdxDirtyPages));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync return;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync}
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Reset all dirty pages by reinstating page monitoring.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pVM VM Handle.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param GCPhysPT Physical address of the page table
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsyncvoid pgmPoolInvalidateDirtyPage(PVM pVM, RTGCPHYS GCPhysPT)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync{
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(PGMIsLocked(pVM));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(pPool->cDirtyPages <= RT_ELEMENTS(pPool->aIdxDirtyPages));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync unsigned idxDirtyPage = RT_ELEMENTS(pPool->aIdxDirtyPages);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (!pPool->cDirtyPages)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync GCPhysPT = GCPhysPT & ~(RTGCPHYS)(PAGE_SIZE - 1);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPool->aIdxDirtyPages); i++)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPool->aIdxDirtyPages[i] != NIL_PGMPOOL_IDX)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync unsigned idxPage = pPool->aIdxDirtyPages[i];
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PPGMPOOLPAGE pPage = &pPool->aPages[idxPage];
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (pPage->GCPhys == GCPhysPT)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync idxDirtyPage = i;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync break;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (idxDirtyPage != RT_ELEMENTS(pPool->aIdxDirtyPages))
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolFlushDirtyPage(pVM, pPool, idxDirtyPage, true /* allow removal of reused page tables*/);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if ( pPool->cDirtyPages != RT_ELEMENTS(pPool->aIdxDirtyPages)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync && pPool->aIdxDirtyPages[pPool->idxFreeDirtyPage] != NIL_PGMPOOL_IDX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync unsigned i;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync for (i = 0; i < RT_ELEMENTS(pPool->aIdxDirtyPages); i++)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPool->aIdxDirtyPages[i] == NIL_PGMPOOL_IDX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->idxFreeDirtyPage = i;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync break;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync AssertMsg(i != RT_ELEMENTS(pPool->aIdxDirtyPages), ("cDirtyPages %d", pPool->cDirtyPages));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync}
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync# endif /* PGMPOOL_WITH_OPTIMIZED_DIRTY_PT */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Inserts a page into the GCPhys hash table.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPool The pool.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPage The page.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsyncDECLINLINE(void) pgmPoolHashInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync{
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Log3(("pgmPoolHashInsert: %RGp\n", pPage->GCPhys));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(pPage->GCPhys != NIL_RTGCPHYS); Assert(pPage->iNext == NIL_PGMPOOL_IDX);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uint16_t iHash = PGMPOOL_HASH(pPage->GCPhys);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->iNext = pPool->aiHash[iHash];
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPool->aiHash[iHash] = pPage->idx;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync}
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/**
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Removes a page from the GCPhys hash table.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pPool The pool.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pPage The page.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncDECLINLINE(void) pgmPoolHashRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync{
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Log3(("pgmPoolHashRemove: %RGp\n", pPage->GCPhys));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uint16_t iHash = PGMPOOL_HASH(pPage->GCPhys);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (pPool->aiHash[iHash] == pPage->idx)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPool->aiHash[iHash] = pPage->iNext;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync else
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uint16_t iPrev = pPool->aiHash[iHash];
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync for (;;)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync const int16_t i = pPool->aPages[iPrev].iNext;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (i == pPage->idx)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPool->aPages[iPrev].iNext = pPage->iNext;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (i == NIL_PGMPOOL_IDX)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertReleaseMsgFailed(("GCPhys=%RGp idx=%#x\n", pPage->GCPhys, pPage->idx));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync iPrev = i;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->iNext = NIL_PGMPOOL_IDX;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync}
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/**
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Frees up one cache page.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @returns VBox status code.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @retval VINF_SUCCESS on success.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pPool The pool.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param iUser The user index.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncstatic int pgmPoolCacheFreeOne(PPGMPOOL pPool, uint16_t iUser)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync{
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#ifndef IN_RC
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync const PVM pVM = pPool->CTX_SUFF(pVM);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#endif
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(pPool->iAgeHead != pPool->iAgeTail); /* We shouldn't be here if there < 2 cached entries! */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pPool->StatCacheFreeUpOne);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /*
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Select one page from the tail of the age list.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PPGMPOOLPAGE pPage;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync for (unsigned iLoop = 0; ; iLoop++)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uint16_t iToFree = pPool->iAgeTail;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (iToFree == iUser)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync iToFree = pPool->aPages[iToFree].iAgePrev;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/* This is the alternative to the SyncCR3 pgmPoolCacheUsed calls.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (pPool->aPages[iToFree].iUserHead != NIL_PGMPOOL_USER_INDEX)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync uint16_t i = pPool->aPages[iToFree].iAgePrev;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync for (unsigned j = 0; j < 10 && i != NIL_PGMPOOL_USER_INDEX; j++, i = pPool->aPages[i].iAgePrev)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (pPool->aPages[iToFree].iUserHead == NIL_PGMPOOL_USER_INDEX)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync continue;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync iToFree = i;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync*/
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(iToFree != iUser);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertRelease(iToFree != NIL_PGMPOOL_IDX);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pPage = &pPool->aPages[iToFree];
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /*
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * Reject any attempts at flushing the currently active shadow CR3 mapping.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * Call pgmPoolCacheUsed to move the page to the head of the age list.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if (!pgmPoolIsPageLocked(&pPool->CTX_SUFF(pVM)->pgm.s, pPage))
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync break;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync LogFlow(("pgmPoolCacheFreeOne: refuse CR3 mapping\n"));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pgmPoolCacheUsed(pPool, pPage);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync AssertLogRelReturn(iLoop < 8192, VERR_INTERNAL_ERROR);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /*
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * Found a usable page, flush it and return.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync int rc = pgmPoolFlushPage(pPool, pPage);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /* This flush was initiated by us and not the guest, so explicitly flush the TLB. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* todo: find out why this is necessary; pgmPoolFlushPage should trigger a flush if one is really needed. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (rc == VINF_SUCCESS)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync PGM_INVL_ALL_VCPU_TLBS(pVM);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync return rc;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync}
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync/**
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * Checks if a kind mismatch is really a page being reused
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * or if it's just normal remappings.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync *
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @returns true if reused and the cached page (enmKind1) should be flushed
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @returns false if not reused.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param enmKind1 The kind of the cached page.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param enmKind2 The kind of the requested page.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsyncstatic bool pgmPoolCacheReusedByKind(PGMPOOLKIND enmKind1, PGMPOOLKIND enmKind2)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync{
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync switch (enmKind1)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /*
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * Never reuse them. There is no remapping in non-paging mode.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case PGMPOOLKIND_PAE_PD_PHYS:
54ddecd74894d3a29fd83ae727844894fef707bevboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
54ddecd74894d3a29fd83ae727844894fef707bevboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case PGMPOOLKIND_PAE_PDPT_FOR_32BIT: /* never reuse them for other types */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync return false;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /*
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * It's perfectly fine to reuse these, except for PAE and non-paging stuff.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case PGMPOOLKIND_32BIT_PD:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case PGMPOOLKIND_PAE_PDPT:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync switch (enmKind2)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PML4:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync return true;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync default:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync return false;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /*
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * It's perfectly fine to reuse these, except for PAE and non-paging stuff.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PML4:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync switch (enmKind2)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync return true;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync default:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync return false;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /*
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * These cannot be flushed, and it's common to reuse the PDs as PTs.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_ROOT_NESTED:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync return false;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync default:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync AssertFatalMsgFailed(("enmKind1=%d\n", enmKind1));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync}
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Attempts to satisfy a pgmPoolAlloc request from the cache.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @returns VBox status code.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @retval VINF_PGM_CACHED_PAGE on success.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @retval VERR_FILE_NOT_FOUND if not found.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPool The pool.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param GCPhys The GC physical address of the page we're gonna shadow.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param enmKind The kind of mapping.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param enmAccess Access type for the mapping (only relevant for big pages)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param iUser The shadow page pool index of the user table.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param iUserTable The index into the user table (shadowed).
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param ppPage Where to store the pointer to the page.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsyncstatic int pgmPoolCacheAlloc(PPGMPOOL pPool, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, PGMPOOLACCESS enmAccess, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync{
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#ifndef IN_RC
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync const PVM pVM = pPool->CTX_SUFF(pVM);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#endif
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /*
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Look up the GCPhys in the hash.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync unsigned i = pPool->aiHash[PGMPOOL_HASH(GCPhys)];
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log3(("pgmPoolCacheAlloc: %RGp kind %s iUser=%x iUserTable=%x SLOT=%d\n", GCPhys, pgmPoolPoolKindToStr(enmKind), iUser, iUserTable, i));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (i != NIL_PGMPOOL_IDX)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync do
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PPGMPOOLPAGE pPage = &pPool->aPages[i];
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Log4(("pgmPoolCacheAlloc: slot %d found page %RGp\n", i, pPage->GCPhys));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (pPage->GCPhys == GCPhys)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ( (PGMPOOLKIND)pPage->enmKind == enmKind
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync && (PGMPOOLACCESS)pPage->enmAccess == enmAccess)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* Put it at the start of the use list to make sure pgmPoolTrackAddUser
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * doesn't flush it in case there are no more free use records.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolCacheUsed(pPool, pPage);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync int rc = pgmPoolTrackAddUser(pPool, pPage, iUser, iUserTable);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if (RT_SUCCESS(rc))
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Assert((PGMPOOLKIND)pPage->enmKind == enmKind);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *ppPage = pPage;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync if (pPage->cModifications)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pPage->cModifications = 1; /* reset counter (can't use 0, or else it will be reinserted in the modified list) */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pPool->StatCacheHits);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync return VINF_PGM_CACHED_PAGE;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync return rc;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ((PGMPOOLKIND)pPage->enmKind != enmKind)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /*
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * The kind is different. In some cases we should now flush the page
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * as it has been reused, but in most cases this is normal remapping
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * of PDs as PT or big pages using the GCPhys field in a slightly
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * different way than the other kinds.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (pgmPoolCacheReusedByKind((PGMPOOLKIND)pPage->enmKind, enmKind))
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pPool->StatCacheKindMismatches);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolFlushPage(pPool, pPage);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* next */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync i = pPage->iNext;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync } while (i != NIL_PGMPOOL_IDX);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Log3(("pgmPoolCacheAlloc: Missed GCPhys=%RGp enmKind=%s\n", GCPhys, pgmPoolPoolKindToStr(enmKind)));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pPool->StatCacheMisses);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync return VERR_FILE_NOT_FOUND;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync}
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/**
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Inserts a page into the cache.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pPool The pool.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pPage The cached page.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param fCanBeCached Set if the page is fit for caching from the caller's point of view.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncstatic void pgmPoolCacheInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, bool fCanBeCached)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync{
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /*
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Insert into the GCPhys hash if the page is fit for that.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(!pPage->fCached);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (fCanBeCached)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage->fCached = true;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolHashInsert(pPool, pPage);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Log3(("pgmPoolCacheInsert: Caching %p:{.Core=%RHp, .idx=%d, .enmKind=%s, GCPhys=%RGp}\n",
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), pPage->GCPhys));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pPool->StatCacheCacheable);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync else
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log3(("pgmPoolCacheInsert: Not caching %p:{.Core=%RHp, .idx=%d, .enmKind=%s, GCPhys=%RGp}\n",
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), pPage->GCPhys));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync STAM_COUNTER_INC(&pPool->StatCacheUncacheable);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /*
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Insert at the head of the age list.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage->iAgePrev = NIL_PGMPOOL_IDX;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->iAgeNext = pPool->iAgeHead;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPool->iAgeHead != NIL_PGMPOOL_IDX)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPool->aPages[pPool->iAgeHead].iAgePrev = pPage->idx;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync else
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->iAgeTail = pPage->idx;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->iAgeHead = pPage->idx;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync}
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Flushes a cached page.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPool The pool.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPage The cached page.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncstatic void pgmPoolCacheFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync{
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log3(("pgmPoolCacheFlushPage: %RGp\n", pPage->GCPhys));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /*
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Remove the page from the hash.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPage->fCached)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage->fCached = false;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmPoolHashRemove(pPool, pPage);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync else
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync Assert(pPage->iNext == NIL_PGMPOOL_IDX);
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /*
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync * Remove it from the age list.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync if (pPage->iAgeNext != NIL_PGMPOOL_IDX)
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync pPool->aPages[pPage->iAgeNext].iAgePrev = pPage->iAgePrev;
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync else
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync pPool->iAgeTail = pPage->iAgePrev;
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync if (pPage->iAgePrev != NIL_PGMPOOL_IDX)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPool->aPages[pPage->iAgePrev].iAgeNext = pPage->iAgeNext;
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync else
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync pPool->iAgeHead = pPage->iAgeNext;
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync pPage->iAgeNext = NIL_PGMPOOL_IDX;
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync pPage->iAgePrev = NIL_PGMPOOL_IDX;
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync}
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync/**
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync * Looks for pages sharing the monitor.
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync *
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync * @returns Pointer to the head page.
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync * @returns NULL if not found.
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync * @param pPool The Pool
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync * @param pNewPage The page which is going to be monitored.
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync */
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsyncstatic PPGMPOOLPAGE pgmPoolMonitorGetPageByGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pNewPage)
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync{
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync /*
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync * Look up the GCPhys in the hash.
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync */
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync RTGCPHYS GCPhys = pNewPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync unsigned i = pPool->aiHash[PGMPOOL_HASH(GCPhys)];
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync if (i == NIL_PGMPOOL_IDX)
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync return NULL;
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync do
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync {
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync PPGMPOOLPAGE pPage = &pPool->aPages[i];
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync if ( pPage->GCPhys - GCPhys < PAGE_SIZE
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync && pPage != pNewPage)
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync {
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync switch (pPage->enmKind)
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync {
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_64BIT_PML4:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_32BIT_PD:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_PAE_PDPT:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync {
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync /* find the head */
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync while (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync {
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync Assert(pPage->iMonitoredPrev != pPage->idx);
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync pPage = &pPool->aPages[pPage->iMonitoredPrev];
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync }
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync return pPage;
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync }
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync /* ignore, no monitoring. */
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_ROOT_NESTED:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_PAE_PD_PHYS:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync break;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync default:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync AssertFatalMsgFailed(("enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync }
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync }
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync /* next */
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync i = pPage->iNext;
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync } while (i != NIL_PGMPOOL_IDX);
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync return NULL;
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync}
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync/**
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync * Enabled write monitoring of a guest page.
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync *
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync * @returns VBox status code.
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync * @retval VINF_SUCCESS on success.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPool The pool.
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync * @param pPage The cached page.
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync */
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsyncstatic int pgmPoolMonitorInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync{
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync LogFlow(("pgmPoolMonitorInsert %RGp\n", pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1)));
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /*
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync * Filter out the relevant kinds.
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync */
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync switch (pPage->enmKind)
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
8689d10cc9774b5c64bbfcf02b8ae32e4c321aa6vboxsync case PGMPOOLKIND_64BIT_PML4:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_32BIT_PD:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_PAE_PDPT:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_ROOT_NESTED:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* Nothing to monitor here. */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return VINF_SUCCESS;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_PAE_PD_PHYS:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* Nothing to monitor here. */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return VINF_SUCCESS;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync default:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /*
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Install handler.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync int rc;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PPGMPOOLPAGE pPageHead = pgmPoolMonitorGetPageByGCPhys(pPool, pPage);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPageHead)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(pPageHead != pPage); Assert(pPageHead->iMonitoredNext != pPage->idx);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(pPageHead->iMonitoredPrev != pPage->idx);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPageHead->fDirty)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmPoolFlushDirtyPage(pPool->CTX_SUFF(pVM), pPool, pPageHead->idxDirty, false /* do not remove */);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#endif
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->iMonitoredPrev = pPageHead->idx;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage->iMonitoredNext = pPageHead->iMonitoredNext;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPageHead->iMonitoredNext != NIL_PGMPOOL_IDX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->aPages[pPageHead->iMonitoredNext].iMonitoredPrev = pPage->idx;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPageHead->iMonitoredNext = pPage->idx;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync rc = VINF_SUCCESS;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync else
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(pPage->iMonitoredNext == NIL_PGMPOOL_IDX); Assert(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PVM pVM = pPool->CTX_SUFF(pVM);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync const RTGCPHYS GCPhysPage = pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync rc = PGMHandlerPhysicalRegisterEx(pVM, PGMPHYSHANDLERTYPE_PHYSICAL_WRITE,
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync GCPhysPage, GCPhysPage + (PAGE_SIZE - 1),
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pPage),
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pPage),
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->pfnAccessHandlerRC, MMHyperCCToRC(pVM, pPage),
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->pszAccessHandler);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /** @todo we should probably deal with out-of-memory conditions here, but for now increasing
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * the heap size should suffice. */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync AssertFatalMsgRC(rc, ("PGMHandlerPhysicalRegisterEx %RGp failed with %Rrc\n", GCPhysPage, rc));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(!(VMMGetCpu(pVM)->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL) || VMCPU_FF_ISSET(VMMGetCpu(pVM), VMCPU_FF_PGM_SYNC_CR3));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage->fMonitored = true;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync return rc;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync}
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Disables write monitoring of a guest page.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @returns VBox status code.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @retval VINF_SUCCESS on success.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPool The pool.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPage The cached page.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncstatic int pgmPoolMonitorFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync{
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /*
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Filter out the relevant kinds.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync switch (pPage->enmKind)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PML4:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_32BIT_PD:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_PAE_PDPT:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_ROOT_NESTED:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_PAE_PD_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* Nothing to monitor here. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync return VINF_SUCCESS;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync default:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /*
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Remove the page from the monitored list or uninstall it if last.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync const PVM pVM = pPool->CTX_SUFF(pVM);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync int rc;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ( pPage->iMonitoredNext != NIL_PGMPOOL_IDX
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync || pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPage->iMonitoredPrev == NIL_PGMPOOL_IDX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PPGMPOOLPAGE pNewHead = &pPool->aPages[pPage->iMonitoredNext];
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pNewHead->iMonitoredPrev = NIL_PGMPOOL_IDX;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync rc = PGMHandlerPhysicalChangeCallbacks(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1),
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pNewHead),
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pNewHead),
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPool->pfnAccessHandlerRC, MMHyperCCToRC(pVM, pNewHead),
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPool->pszAccessHandler);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertFatalRCSuccess(rc);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync else
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->aPages[pPage->iMonitoredPrev].iMonitoredNext = pPage->iMonitoredNext;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPage->iMonitoredNext != NIL_PGMPOOL_IDX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->aPages[pPage->iMonitoredNext].iMonitoredPrev = pPage->iMonitoredPrev;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync rc = VINF_SUCCESS;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync else
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync rc = PGMHandlerPhysicalDeregister(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync AssertFatalRC(rc);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#ifdef VBOX_STRICT
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PVMCPU pVCpu = VMMGetCpu(pVM);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#endif
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync AssertMsg(!(pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL) || VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3),
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync ("%#x %#x\n", pVCpu->pgm.s.fSyncFlags, pVM->fGlobalForcedActions));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage->fMonitored = false;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /*
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Remove it from the list of modified pages (if in it).
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmPoolMonitorModifiedRemove(pPool, pPage);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return rc;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync}
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Inserts the page into the list of modified pages.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPool The pool.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPage The page.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsyncvoid pgmPoolMonitorModifiedInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync{
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log3(("pgmPoolMonitorModifiedInsert: idx=%d\n", pPage->idx));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync AssertMsg( pPage->iModifiedNext == NIL_PGMPOOL_IDX
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync && pPage->iModifiedPrev == NIL_PGMPOOL_IDX
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync && pPool->iModifiedHead != pPage->idx,
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync ("Next=%d Prev=%d idx=%d cModifications=%d Head=%d cModifiedPages=%d\n",
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage->iModifiedNext, pPage->iModifiedPrev, pPage->idx, pPage->cModifications,
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->iModifiedHead, pPool->cModifiedPages));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage->iModifiedNext = pPool->iModifiedHead;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPool->iModifiedHead != NIL_PGMPOOL_IDX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->aPages[pPool->iModifiedHead].iModifiedPrev = pPage->idx;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->iModifiedHead = pPage->idx;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->cModifiedPages++;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#ifdef VBOX_WITH_STATISTICS
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPool->cModifiedPages > pPool->cModifiedPagesHigh)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->cModifiedPagesHigh = pPool->cModifiedPages;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#endif
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync}
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Removes the page from the list of modified pages and resets the
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * moficiation counter.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPool The pool.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPage The page which is believed to be in the list of modified pages.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsyncstatic void pgmPoolMonitorModifiedRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync{
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log3(("pgmPoolMonitorModifiedRemove: idx=%d cModifications=%d\n", pPage->idx, pPage->cModifications));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPool->iModifiedHead == pPage->idx)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->iModifiedHead = pPage->iModifiedNext;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPage->iModifiedNext != NIL_PGMPOOL_IDX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->aPages[pPage->iModifiedNext].iModifiedPrev = NIL_PGMPOOL_IDX;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage->iModifiedNext = NIL_PGMPOOL_IDX;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->cModifiedPages--;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync else if (pPage->iModifiedPrev != NIL_PGMPOOL_IDX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->aPages[pPage->iModifiedPrev].iModifiedNext = pPage->iModifiedNext;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPage->iModifiedNext != NIL_PGMPOOL_IDX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->aPages[pPage->iModifiedNext].iModifiedPrev = pPage->iModifiedPrev;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage->iModifiedNext = NIL_PGMPOOL_IDX;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->cModifiedPages--;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync else
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->cModifications = 0;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync}
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/**
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * Zaps the list of modified pages, resetting their modification counters in the process.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync *
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pVM The VM handle.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsyncstatic void pgmPoolMonitorModifiedClearAll(PVM pVM)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync{
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pgmLock(pVM);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync LogFlow(("pgmPoolMonitorModifiedClearAll: cModifiedPages=%d\n", pPool->cModifiedPages));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync unsigned cPages = 0; NOREF(cPages);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolResetDirtyPages(pVM);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#endif
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uint16_t idx = pPool->iModifiedHead;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPool->iModifiedHead = NIL_PGMPOOL_IDX;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync while (idx != NIL_PGMPOOL_IDX)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PPGMPOOLPAGE pPage = &pPool->aPages[idx];
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync idx = pPage->iModifiedNext;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->iModifiedNext = NIL_PGMPOOL_IDX;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pPage->cModifications = 0;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(++cPages);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync AssertMsg(cPages == pPool->cModifiedPages, ("%d != %d\n", cPages, pPool->cModifiedPages));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pPool->cModifiedPages = 0;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmUnlock(pVM);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync}
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/**
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Handle SyncCR3 pool tasks
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @returns VBox status code.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @retval VINF_SUCCESS if successfully added.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @retval VINF_PGM_SYNC_CR3 is it needs to be deferred to ring 3 (GC only)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pVCpu The VMCPU handle.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @remark Should only be used when monitoring is available, thus placed in
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * the PGMPOOL_WITH_MONITORING #ifdef.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsyncint pgmPoolSyncCR3(PVMCPU pVCpu)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync{
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync PVM pVM = pVCpu->CTX_SUFF(pVM);
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync LogFlow(("pgmPoolSyncCR3\n"));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync /*
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * When monitoring shadowed pages, we reset the modification counters on CR3 sync.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * Occasionally we will have to clear all the shadow page tables because we wanted
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * to monitor a page which was mapped by too many shadowed page tables. This operation
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * sometimes refered to as a 'lightweight flush'.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync */
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync# ifdef IN_RING3 /* Don't flush in ring-0 or raw mode, it's taking too long. */
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync if (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync pgmR3PoolClearAll(pVM);
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync# else /* !IN_RING3 */
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync if (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Log(("SyncCR3: PGM_SYNC_CLEAR_PGM_POOL is set -> VINF_PGM_SYNC_CR3\n"));
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3); /** @todo no need to do global sync, right? */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync /* Make sure all other VCPUs return to ring 3. */
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync if (pVM->cCpus > 1)
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync {
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync VM_FF_SET(pVM, VM_FF_PGM_POOL_FLUSH_PENDING);
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync PGM_INVL_ALL_VCPU_TLBS(pVM);
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync }
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync return VINF_PGM_SYNC_CR3;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync }
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync# endif /* !IN_RING3 */
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync else
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync pgmPoolMonitorModifiedClearAll(pVM);
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync return VINF_SUCCESS;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync}
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Frees up at least one user entry.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @returns VBox status code.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @retval VINF_SUCCESS if successfully added.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pPool The pool.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param iUser The user index.
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsyncstatic int pgmPoolTrackFreeOneUser(PPGMPOOL pPool, uint16_t iUser)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync{
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync STAM_COUNTER_INC(&pPool->StatTrackFreeUpOneUser);
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync /*
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * Just free cached pages in a braindead fashion.
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync /** @todo walk the age list backwards and free the first with usage. */
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync int rc = VINF_SUCCESS;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync do
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync {
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync int rc2 = pgmPoolCacheFreeOne(pPool, iUser);
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync if (RT_FAILURE(rc2) && rc == VINF_SUCCESS)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync rc = rc2;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync } while (pPool->iUserFreeHead == NIL_PGMPOOL_USER_INDEX);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync return rc;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync}
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync/**
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * Inserts a page into the cache.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * This will create user node for the page, insert it into the GCPhys
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * hash, and insert it into the age list.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync *
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @returns VBox status code.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @retval VINF_SUCCESS if successfully added.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @param pPool The pool.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @param pPage The cached page.
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync * @param GCPhys The GC physical address of the page we're gonna shadow.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @param iUser The user index.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param iUserTable The user table index.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncDECLINLINE(int) pgmPoolTrackInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhys, uint16_t iUser, uint32_t iUserTable)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync{
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync int rc = VINF_SUCCESS;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync LogFlow(("pgmPoolTrackInsert GCPhys=%RGp iUser %x iUserTable %x\n", GCPhys, iUser, iUserTable));
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync#ifdef VBOX_STRICT
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync /*
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * Check that the entry doesn't already exists.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync if (pPage->iUserHead != NIL_PGMPOOL_USER_INDEX)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync {
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync uint16_t i = pPage->iUserHead;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync do
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync {
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Assert(i < pPool->cMaxUsers);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync AssertMsg(paUsers[i].iUser != iUser || paUsers[i].iUserTable != iUserTable, ("%x %x vs new %x %x\n", paUsers[i].iUser, paUsers[i].iUserTable, iUser, iUserTable));
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync i = paUsers[i].iNext;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync } while (i != NIL_PGMPOOL_USER_INDEX);
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync }
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync#endif
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync /*
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * Find free a user node.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync */
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync uint16_t i = pPool->iUserFreeHead;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync if (i == NIL_PGMPOOL_USER_INDEX)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync {
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync rc = pgmPoolTrackFreeOneUser(pPool, iUser);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync if (RT_FAILURE(rc))
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync return rc;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync i = pPool->iUserFreeHead;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync }
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync /*
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync * Unlink the user node from the free list,
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * initialize and insert it into the user list.
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync */
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync pPool->iUserFreeHead = paUsers[i].iNext;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync paUsers[i].iNext = NIL_PGMPOOL_USER_INDEX;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync paUsers[i].iUser = iUser;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync paUsers[i].iUserTable = iUserTable;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync pPage->iUserHead = i;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync /*
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync * Insert into cache and enable monitoring of the guest page if enabled.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync *
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * Until we implement caching of all levels, including the CR3 one, we'll
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync * have to make sure we don't try monitor & cache any recursive reuse of
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * a monitored CR3 page. Because all windows versions are doing this we'll
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * have to be able to do combined access monitoring, CR3 + PT and
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * PD + PT (guest PAE).
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync *
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * Update:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * We're now cooperating with the CR3 monitor if an uncachable page is found.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync */
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync const bool fCanBeMonitored = true;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync pgmPoolCacheInsert(pPool, pPage, fCanBeMonitored); /* This can be expanded. */
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync if (fCanBeMonitored)
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync {
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync rc = pgmPoolMonitorInsert(pPool, pPage);
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync AssertRC(rc);
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync }
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync return rc;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync}
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync/**
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * Adds a user reference to a page.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync *
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * This will move the page to the head of the
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync *
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * @returns VBox status code.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * @retval VINF_SUCCESS if successfully added.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * @param pPool The pool.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * @param pPage The cached page.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * @param iUser The user index.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * @param iUserTable The user table.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync */
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsyncstatic int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync{
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync Log3(("pgmPoolTrackAddUser GCPhys = %RGp iUser %x iUserTable %x\n", pPage->GCPhys, iUser, iUserTable));
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync# ifdef VBOX_STRICT
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync /*
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * Check that the entry doesn't already exists. We only allow multiple users of top-level paging structures (SHW_POOL_ROOT_IDX).
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync */
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync if (pPage->iUserHead != NIL_PGMPOOL_USER_INDEX)
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync {
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync uint16_t i = pPage->iUserHead;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync do
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync {
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync Assert(i < pPool->cMaxUsers);
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync AssertMsg(iUser != PGMPOOL_IDX_PD || iUser != PGMPOOL_IDX_PDPT || iUser != PGMPOOL_IDX_NESTED_ROOT || iUser != PGMPOOL_IDX_AMD64_CR3 ||
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync paUsers[i].iUser != iUser || paUsers[i].iUserTable != iUserTable, ("%x %x vs new %x %x\n", paUsers[i].iUser, paUsers[i].iUserTable, iUser, iUserTable));
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync i = paUsers[i].iNext;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync } while (i != NIL_PGMPOOL_USER_INDEX);
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync }
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync# endif
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync /*
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * Allocate a user node.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync */
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync uint16_t i = pPool->iUserFreeHead;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync if (i == NIL_PGMPOOL_USER_INDEX)
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync {
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync int rc = pgmPoolTrackFreeOneUser(pPool, iUser);
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync if (RT_FAILURE(rc))
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync return rc;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync i = pPool->iUserFreeHead;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync }
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync pPool->iUserFreeHead = paUsers[i].iNext;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync /*
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * Initialize the user node and insert it.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync */
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync paUsers[i].iNext = pPage->iUserHead;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync paUsers[i].iUser = iUser;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync paUsers[i].iUserTable = iUserTable;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync pPage->iUserHead = i;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync# ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync if (pPage->fDirty)
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync pgmPoolFlushDirtyPage(pPool->CTX_SUFF(pVM), pPool, pPage->idxDirty, false /* do not remove */);
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync# endif
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync /*
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * Tell the cache to update its replacement stats for this page.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync */
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync pgmPoolCacheUsed(pPool, pPage);
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync return VINF_SUCCESS;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync}
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync/**
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * Frees a user record associated with a page.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync *
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * This does not clear the entry in the user table, it simply replaces the
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * user record to the chain of free records.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync *
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * @param pPool The pool.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * @param HCPhys The HC physical address of the shadow page.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * @param iUser The shadow page pool index of the user table.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * @param iUserTable The index into the user table (shadowed).
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync */
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsyncstatic void pgmPoolTrackFreeUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync{
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync /*
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * Unlink and free the specified user entry.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync */
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync Log3(("pgmPoolTrackFreeUser %RGp %x %x\n", pPage->GCPhys, iUser, iUserTable));
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync /* Special: For PAE and 32-bit paging, there is usually no more than one user. */
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync uint16_t i = pPage->iUserHead;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync if ( i != NIL_PGMPOOL_USER_INDEX
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync && paUsers[i].iUser == iUser
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync && paUsers[i].iUserTable == iUserTable)
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync {
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync pPage->iUserHead = paUsers[i].iNext;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync paUsers[i].iUser = NIL_PGMPOOL_IDX;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync paUsers[i].iNext = pPool->iUserFreeHead;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync pPool->iUserFreeHead = i;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync return;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync }
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync /* General: Linear search. */
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync uint16_t iPrev = NIL_PGMPOOL_USER_INDEX;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync while (i != NIL_PGMPOOL_USER_INDEX)
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync {
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync if ( paUsers[i].iUser == iUser
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync && paUsers[i].iUserTable == iUserTable)
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync {
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync if (iPrev != NIL_PGMPOOL_USER_INDEX)
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync paUsers[iPrev].iNext = paUsers[i].iNext;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync else
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync pPage->iUserHead = paUsers[i].iNext;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync paUsers[i].iUser = NIL_PGMPOOL_IDX;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync paUsers[i].iNext = pPool->iUserFreeHead;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync pPool->iUserFreeHead = i;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync return;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync }
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync iPrev = i;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync i = paUsers[i].iNext;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync }
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync /* Fatal: didn't find it */
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync AssertFatalMsgFailed(("Didn't find the user entry! iUser=%#x iUserTable=%#x GCPhys=%RGp\n",
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync iUser, iUserTable, pPage->GCPhys));
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync}
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync/**
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * Gets the entry size of a shadow table.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync *
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * @param enmKind The kind of page.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync *
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * @returns The size of the entry in bytes. That is, 4 or 8.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * @returns If the kind is not for a table, an assertion is raised and 0 is
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * returned.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync */
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsyncDECLINLINE(unsigned) pgmPoolTrackGetShadowEntrySize(PGMPOOLKIND enmKind)
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync{
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync switch (enmKind)
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync {
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_32BIT_PD:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync return 4;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_64BIT_PML4:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PDPT:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_ROOT_NESTED:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PD_PHYS:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync return 8;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync default:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync AssertFatalMsgFailed(("enmKind=%d\n", enmKind));
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync }
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync}
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync/**
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * Gets the entry size of a guest table.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync *
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * @param enmKind The kind of page.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync *
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * @returns The size of the entry in bytes. That is, 0, 4 or 8.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * @returns If the kind is not for a table, an assertion is raised and 0 is
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * returned.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync */
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsyncDECLINLINE(unsigned) pgmPoolTrackGetGuestEntrySize(PGMPOOLKIND enmKind)
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync{
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync switch (enmKind)
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync {
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_32BIT_PD:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync return 4;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_64BIT_PML4:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PDPT:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync return 8;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_ROOT_NESTED:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PD_PHYS:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync /** @todo can we return 0? (nobody is calling this...) */
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync AssertFailed();
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync return 0;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync default:
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync AssertFatalMsgFailed(("enmKind=%d\n", enmKind));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync}
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync/**
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * Scans one shadow page table for mappings of a physical page.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync *
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @returns true/false indicating removal of all relevant PTEs
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @param pVM The VM handle.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @param pPhysPage The guest page in question.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @param fFlushPTEs Flush PTEs or allow them to be updated (e.g. in case of an RW bit change)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @param iShw The shadow page table.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @param cRefs The number of references made in that PT.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync */
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsyncstatic bool pgmPoolTrackFlushGCPhysPTInt(PVM pVM, PCPGMPAGE pPhysPage, bool fFlushPTEs, uint16_t iShw, uint16_t cRefs)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync{
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync LogFlow(("pgmPoolTrackFlushGCPhysPT: pPhysPage=%RHp iShw=%d cRefs=%d\n", PGM_PAGE_GET_HCPHYS(pPhysPage), iShw, cRefs));
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync bool bRet = false;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync /*
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * Assert sanity.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync */
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Assert(cRefs == 1);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync AssertFatalMsg(iShw < pPool->cCurPages && iShw != NIL_PGMPOOL_IDX, ("iShw=%d\n", iShw));
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync PPGMPOOLPAGE pPage = &pPool->aPages[iShw];
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync /*
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * Then, clear the actual mappings to the page in the shadow PT.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync */
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync switch (pPage->enmKind)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync {
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync {
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync const uint32_t u32 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PX86PT pPT = (PX86PT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync uint32_t u32AndMask, u32OrMask;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync u32AndMask = 0;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync u32OrMask = 0;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (!fFlushPTEs)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync {
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync switch (PGM_PAGE_GET_HNDL_PHYS_STATE(pPhysPage))
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync {
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync case PGM_PAGE_HNDL_PHYS_STATE_NONE: /** No handler installed. */
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync case PGM_PAGE_HNDL_PHYS_STATE_DISABLED: /** Monitoring is temporarily disabled. */
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync u32OrMask = X86_PTE_RW;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync u32AndMask = UINT32_MAX;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync bRet = true;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync STAM_COUNTER_INC(&pPool->StatTrackFlushEntryKeep);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync break;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync case PGM_PAGE_HNDL_PHYS_STATE_WRITE: /** Write access is monitored. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync u32OrMask = 0;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync u32AndMask = ~X86_PTE_RW;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync bRet = true;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync STAM_COUNTER_INC(&pPool->StatTrackFlushEntryKeep);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync break;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync default:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pPool->StatTrackFlushEntry);
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync }
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync else
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync STAM_COUNTER_INC(&pPool->StatTrackFlushEntry);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync /* Update the counter if we're removing references. */
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync if (!u32AndMask)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync {
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Assert(pPage->cPresent >= cRefs);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Assert(pPool->cPresent >= cRefs);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync pPage->cPresent -= cRefs;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync pPool->cPresent -= cRefs;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync }
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync {
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync X86PTE Pte;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX32 cRefs=%#x\n", i, pPT->a[i], cRefs));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Pte.u = (pPT->a[i].u & u32AndMask) | u32OrMask;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync if (Pte.u & PGM_PTFLAGS_TRACK_DIRTY)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Pte.n.u1Write = 0; /* need to disallow writes when dirty bit tracking is still active. */
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync ASMAtomicWriteSize(&pPT->a[i].u, Pte.u);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync cRefs--;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync if (!cRefs)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync return bRet;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync }
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync#ifdef LOG_ENABLED
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Log(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPT->a); i++)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync {
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Log(("i=%d cRefs=%d\n", i, cRefs--));
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync }
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync#endif
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync break;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync }
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync {
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync PX86PTPAE pPT = (PX86PTPAE)PGMPOOL_PAGE_2_PTR(pVM, pPage);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync uint64_t u64AndMask, u64OrMask;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync u64OrMask = 0;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync u64AndMask = 0;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync if (!fFlushPTEs)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync {
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync switch (PGM_PAGE_GET_HNDL_PHYS_STATE(pPhysPage))
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync {
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync case PGM_PAGE_HNDL_PHYS_STATE_NONE: /** No handler installed. */
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync case PGM_PAGE_HNDL_PHYS_STATE_DISABLED: /** Monitoring is temporarily disabled. */
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync u64OrMask = X86_PTE_RW;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync u64AndMask = UINT64_MAX;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync bRet = true;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync STAM_COUNTER_INC(&pPool->StatTrackFlushEntryKeep);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync break;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGM_PAGE_HNDL_PHYS_STATE_WRITE: /** Write access is monitored. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync u64OrMask = 0;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync u64AndMask = ~((uint64_t)X86_PTE_RW);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync bRet = true;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync STAM_COUNTER_INC(&pPool->StatTrackFlushEntryKeep);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync break;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync default:
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync STAM_COUNTER_INC(&pPool->StatTrackFlushEntry);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync else
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync STAM_COUNTER_INC(&pPool->StatTrackFlushEntry);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync /* Update the counter if we're removing references. */
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync if (!u64AndMask)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Assert(pPage->cPresent >= cRefs);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(pPool->cPresent >= cRefs);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync pPage->cPresent -= cRefs;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync pPool->cPresent -= cRefs;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync {
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync X86PTEPAE Pte;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX64 cRefs=%#x\n", i, pPT->a[i], cRefs));
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Pte.u = (pPT->a[i].u & u64AndMask) | u64OrMask;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync if (Pte.u & PGM_PTFLAGS_TRACK_DIRTY)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Pte.n.u1Write = 0; /* need to disallow writes when dirty bit tracking is still active. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMAtomicWriteSize(&pPT->a[i].u, Pte.u);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync cRefs--;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync if (!cRefs)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync return bRet;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync }
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync#ifdef LOG_ENABLED
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Log(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPT->a); i++)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync {
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync Log(("i=%d cRefs=%d\n", i, cRefs--));
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#endif
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d u64=%RX64\n", cRefs, pPage->iFirstPresent, pPage->cPresent, u64));
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync break;
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync }
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync {
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync PEPTPT pPT = (PEPTPT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync if ((pPT->a[i].u & (EPT_PTE_PG_MASK | X86_PTE_P)) == u64)
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync {
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX64 cRefs=%#x\n", i, pPT->a[i], cRefs));
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync STAM_COUNTER_INC(&pPool->StatTrackFlushEntry);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPT->a[i].u = 0;
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync cRefs--;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* Update the counter as we're removing references. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(pPage->cPresent);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(pPool->cPresent);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->cPresent--;
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync pPool->cPresent--;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (!cRefs)
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync return bRet;
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#ifdef LOG_ENABLED
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Log(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPT->a); i++)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ((pPT->a[i].u & (EPT_PTE_PG_MASK | X86_PTE_P)) == u64)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Log(("i=%d cRefs=%d\n", i, cRefs--));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#endif
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync }
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync#ifdef PGM_WITH_LARGE_PAGES
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync /* Large page case only. */
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(HWACCMIsNestedPagingActive(pVM));
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync Assert(cRefs == 1);
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PDE4M_P | X86_PDE4M_PS;
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync PEPTPD pPD = (PEPTPD)PGMPOOL_PAGE_2_PTR(pVM, pPage);
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPD->a); i++)
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync if ((pPD->a[i].u & (EPT_PDE2M_PG_MASK | X86_PDE4M_P | X86_PDE4M_PS)) == u64)
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync {
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pde=%RX64 cRefs=%#x\n", i, pPD->a[i], cRefs));
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync STAM_COUNTER_INC(&pPool->StatTrackFlushEntry);
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync pPD->a[i].u = 0;
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync cRefs--;
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync /* Update the counter as we're removing references. */
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync Assert(pPage->cPresent);
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync Assert(pPool->cPresent);
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync pPage->cPresent--;
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync pPool->cPresent--;
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync if (!cRefs)
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync return bRet;
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync }
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync# ifdef LOG_ENABLED
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync Log(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPD->a); i++)
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync if ((pPD->a[i].u & (EPT_PDE2M_PG_MASK | X86_PDE4M_P | X86_PDE4M_PS)) == u64)
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync {
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync Log(("i=%d cRefs=%d\n", i, cRefs--));
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# endif
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync break;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync }
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync /* AMD-V nested paging - @todo merge with EPT as we only check the parts that are identical. */
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync case PGMPOOLKIND_PAE_PD_PHYS:
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync {
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Assert(HWACCMIsNestedPagingActive(pVM));
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Assert(cRefs == 1);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PDE4M_P | X86_PDE4M_PS;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync PX86PD pPD = (PX86PD)PGMPOOL_PAGE_2_PTR(pVM, pPage);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPD->a); i++)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync if ((pPD->a[i].u & (X86_PDE2M_PAE_PG_MASK | X86_PDE4M_P | X86_PDE4M_PS)) == u64)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync {
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pde=%RX64 cRefs=%#x\n", i, pPD->a[i], cRefs));
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync STAM_COUNTER_INC(&pPool->StatTrackFlushEntry);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync pPD->a[i].u = 0;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync cRefs--;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync /* Update the counter as we're removing references. */
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Assert(pPage->cPresent);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Assert(pPool->cPresent);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync pPage->cPresent--;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync pPool->cPresent--;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync if (!cRefs)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync return bRet;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync }
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync# ifdef LOG_ENABLED
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Log(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPD->a); i++)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync if ((pPD->a[i].u & (X86_PDE2M_PAE_PG_MASK | X86_PDE4M_P | X86_PDE4M_PS)) == u64)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync {
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Log(("i=%d cRefs=%d\n", i, cRefs--));
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync }
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync# endif
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync break;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync }
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync#endif /* PGM_WITH_LARGE_PAGES */
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync default:
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync AssertFatalMsgFailed(("enmKind=%d iShw=%d\n", pPage->enmKind, iShw));
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync }
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync return bRet;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync}
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync/**
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync * Scans one shadow page table for mappings of a physical page.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync *
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @param pVM The VM handle.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @param pPhysPage The guest page in question.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @param fFlushPTEs Flush PTEs or allow them to be updated (e.g. in case of an RW bit change)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync * @param iShw The shadow page table.
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync * @param cRefs The number of references made in that PT.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync */
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsyncstatic void pgmPoolTrackFlushGCPhysPT(PVM pVM, PPGMPAGE pPhysPage, bool fFlushPTEs, uint16_t iShw, uint16_t cRefs)
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync{
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool); NOREF(pPool);
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync /* We should only come here with when there's only one reference to this physical page. */
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync Assert(PGMPOOL_TD_GET_CREFS(PGM_PAGE_GET_TRACKING(pPhysPage)) == 1);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Assert(cRefs == 1);
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Log2(("pgmPoolTrackFlushGCPhysPT: pPhysPage=%RHp iShw=%d cRefs=%d\n", PGM_PAGE_GET_HCPHYS(pPhysPage), iShw, cRefs));
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPT, f);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync bool fKeptPTEs = pgmPoolTrackFlushGCPhysPTInt(pVM, pPhysPage, fFlushPTEs, iShw, cRefs);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync if (!fKeptPTEs)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync PGM_PAGE_SET_TRACKING(pPhysPage, 0);
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPT, f);
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync}
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync/**
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * Flushes a list of shadow page tables mapping the same physical page.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync *
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync * @param pVM The VM handle.
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync * @param pPhysPage The guest page in question.
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync * @param fFlushPTEs Flush PTEs or allow them to be updated (e.g. in case of an RW bit change)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @param iPhysExt The physical cross reference extent list to flush.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync */
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsyncstatic void pgmPoolTrackFlushGCPhysPTs(PVM pVM, PPGMPAGE pPhysPage, bool fFlushPTEs, uint16_t iPhysExt)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync{
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Assert(PGMIsLockOwner(pVM));
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync bool fKeepList = false;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPTs, f);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Log2(("pgmPoolTrackFlushGCPhysPTs: pPhysPage=%RHp iPhysExt\n", PGM_PAGE_GET_HCPHYS(pPhysPage), iPhysExt));
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync const uint16_t iPhysExtStart = iPhysExt;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync PPGMPOOLPHYSEXT pPhysExt;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync do
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync {
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Assert(iPhysExt < pPool->cMaxPhysExts);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync {
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync if (pPhysExt->aidx[i] != NIL_PGMPOOL_IDX)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync {
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync bool fKeptPTEs = pgmPoolTrackFlushGCPhysPTInt(pVM, pPhysPage, fFlushPTEs, pPhysExt->aidx[i], 1);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync if (!fKeptPTEs)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync else
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync fKeepList = true;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync }
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync }
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync /* next */
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync iPhysExt = pPhysExt->iNext;
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync } while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync if (!fKeepList)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync {
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync /* insert the list into the free list and clear the ram range entry. */
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync pPhysExt->iNext = pPool->iPhysExtFreeHead;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync pPool->iPhysExtFreeHead = iPhysExtStart;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync PGM_PAGE_SET_TRACKING(pPhysPage, 0);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync }
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTs, f);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync}
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync/**
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync * Flushes all shadow page table mappings of the given guest page.
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync *
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync * This is typically called when the host page backing the guest one has been
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * replaced or when the page protection was changed due to an access handler.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync *
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @returns VBox status code.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @retval VINF_SUCCESS if all references has been successfully cleared.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @retval VINF_PGM_SYNC_CR3 if we're better off with a CR3 sync and a page
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * pool cleaning. FF and sync flags are set.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync *
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @param pVM The VM handle.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @param GCPhysPage GC physical address of the page in question
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @param pPhysPage The guest page in question.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @param fFlushPTEs Flush PTEs or allow them to be updated (e.g. in case of an RW bit change)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * @param pfFlushTLBs This is set to @a true if the shadow TLBs should be
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * flushed, it is NOT touched if this isn't necessary.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync * The caller MUST initialized this to @a false.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync */
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsyncint pgmPoolTrackUpdateGCPhys(PVM pVM, RTGCPHYS GCPhysPage, PPGMPAGE pPhysPage, bool fFlushPTEs, bool *pfFlushTLBs)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync{
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync PVMCPU pVCpu = VMMGetCpu(pVM);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync pgmLock(pVM);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync int rc = VINF_SUCCESS;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync#ifdef PGM_WITH_LARGE_PAGES
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync /* Is this page part of a large page? */
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync if (PGM_PAGE_GET_PDE_TYPE(pPhysPage) == PGM_PAGE_PDE_TYPE_PDE)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync {
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync PPGMPAGE pPhysBase;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync RTGCPHYS GCPhysBase = GCPhysPage & X86_PDE2M_PAE_PG_MASK;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync GCPhysPage &= X86_PDE_PAE_PG_MASK;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync /* Fetch the large page base. */
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync if (GCPhysBase != GCPhysPage)
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync {
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync pPhysBase = pgmPhysGetPage(&pVM->pgm.s, GCPhysBase);
dc45a8f3e936581748c248e00ce572cfe3ea331evboxsync AssertFatal(pPhysBase);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync }
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync else
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync pPhysBase = pPhysPage;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Log(("pgmPoolTrackUpdateGCPhys: update large page PDE for %RGp (%RGp)\n", GCPhysBase, GCPhysPage));
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync if (PGM_PAGE_GET_PDE_TYPE(pPhysBase) == PGM_PAGE_PDE_TYPE_PDE)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync {
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync /* Mark the large page as disabled as we need to break it up to change a single page in the 2 MB range. */
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync PGM_PAGE_SET_PDE_TYPE(pPhysBase, PGM_PAGE_PDE_TYPE_PDE_DISABLED);
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync /* Update the base as that *only* that one has a reference and there's only one PDE to clear. */
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync rc = pgmPoolTrackUpdateGCPhys(pVM, GCPhysBase, pPhysBase, fFlushPTEs, pfFlushTLBs);
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync *pfFlushTLBs = true;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync pgmUnlock(pVM);
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync return rc;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync }
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync }
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync#else
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync NOREF(GCPhysPage);
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync#endif /* PGM_WITH_LARGE_PAGES */
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync const uint16_t u16 = PGM_PAGE_GET_TRACKING(pPhysPage);
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync if (u16)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync {
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync /*
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync * The zero page is currently screwing up the tracking and we'll
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync * have to flush the whole shebang. Unless VBOX_WITH_NEW_LAZY_PAGE_ALLOC
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync * is defined, zero pages won't normally be mapped. Some kind of solution
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync * will be needed for this problem of course, but it will have to wait...
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync */
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync if (PGM_PAGE_IS_ZERO(pPhysPage))
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync rc = VINF_PGM_GCPHYS_ALIASED;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync else
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync {
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync /* Start a subset here because pgmPoolTrackFlushGCPhysPTsSlow and
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync pgmPoolTrackFlushGCPhysPTs will/may kill the pool otherwise. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# endif
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (PGMPOOL_TD_GET_CREFS(u16) != PGMPOOL_TD_CREFS_PHYSEXT)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTrackFlushGCPhysPT(pVM,
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync pPhysPage,
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync fFlushPTEs,
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync PGMPOOL_TD_GET_IDX(u16),
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync PGMPOOL_TD_GET_CREFS(u16));
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync else if (u16 != PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED))
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTrackFlushGCPhysPTs(pVM, pPhysPage, fFlushPTEs, PGMPOOL_TD_GET_IDX(u16));
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync else
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync rc = pgmPoolTrackFlushGCPhysPTsSlow(pVM, pPhysPage);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *pfFlushTLBs = true;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PGMDynMapPopAutoSubset(pVCpu, iPrevSubset);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync# endif
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync if (rc == VINF_PGM_GCPHYS_ALIASED)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync {
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync pVCpu->pgm.s.fSyncFlags |= PGM_SYNC_CLEAR_PGM_POOL;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync rc = VINF_PGM_SYNC_CR3;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync }
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync pgmUnlock(pVM);
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync return rc;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync}
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/**
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Scans all shadow page tables for mappings of a physical page.
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * This may be slow, but it's most likely more efficient than cleaning
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * out the entire page pool / cache.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync *
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @returns VBox status code.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @retval VINF_SUCCESS if all references has been successfully cleared.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @retval VINF_PGM_GCPHYS_ALIASED if we're better off with a CR3 sync and
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync * a page pool cleaning.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pVM The VM handle.
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * @param pPhysPage The guest page in question.
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync */
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsyncint pgmPoolTrackFlushGCPhysPTsSlow(PVM pVM, PPGMPAGE pPhysPage)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync{
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPTsSlow, s);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: cUsedPages=%d cPresent=%d pPhysPage=%R[pgmpage]\n",
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync pPool->cUsedPages, pPool->cPresent, pPhysPage));
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync#if 1
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync /*
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync * There is a limit to what makes sense.
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync */
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync if (pPool->cPresent > 1024)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync {
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: giving up... (cPresent=%d)\n", pPool->cPresent));
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTsSlow, s);
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync return VINF_PGM_GCPHYS_ALIASED;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync }
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync#endif
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync /*
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync * Iterate all the pages until we've encountered all that in use.
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync * This is simple but not quite optimal solution.
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync */
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync const uint32_t u32 = u64;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync unsigned cLeft = pPool->cUsedPages;
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync unsigned iPage = pPool->cCurPages;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync while (--iPage >= PGMPOOL_IDX_FIRST)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync {
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync if ( pPage->GCPhys != NIL_RTGCPHYS
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync && pPage->cPresent)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync {
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync switch (pPage->enmKind)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync /*
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync * We only care about shadow page tables.
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync */
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync {
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync unsigned cPresent = pPage->cPresent;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync PX86PT pPT = (PX86PT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (pPT->a[i].n.u1Present)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync {
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync //Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX32\n", iPage, i, pPT->a[i]));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPT->a[i].u = 0;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync /* Update the counter as we're removing references. */
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync Assert(pPage->cPresent);
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync Assert(pPool->cPresent);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->cPresent--;
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync pPool->cPresent--;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (!--cPresent)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync break;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync }
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync unsigned cPresent = pPage->cPresent;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PX86PTPAE pPT = (PX86PTPAE)PGMPOOL_PAGE_2_PTR(pVM, pPage);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (pPT->a[i].n.u1Present)
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync {
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync //Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX64\n", iPage, i, pPT->a[i]));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPT->a[i].u = 0;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* Update the counter as we're removing references. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(pPage->cPresent);
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync Assert(pPool->cPresent);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->cPresent--;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPool->cPresent--;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (!--cPresent)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#ifndef IN_RC
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync unsigned cPresent = pPage->cPresent;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PEPTPT pPT = (PEPTPT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync if (pPT->a[i].n.u1Present)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync {
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync if ((pPT->a[i].u & (EPT_PTE_PG_MASK | X86_PTE_P)) == u64)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync {
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync //Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX64\n", iPage, i, pPT->a[i]));
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync pPT->a[i].u = 0;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync /* Update the counter as we're removing references. */
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync Assert(pPage->cPresent);
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync Assert(pPool->cPresent);
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync pPage->cPresent--;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync pPool->cPresent--;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync }
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync if (!--cPresent)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync break;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync }
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync break;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync }
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync#endif
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (!--cLeft)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync }
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync PGM_PAGE_SET_TRACKING(pPhysPage, 0);
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTsSlow, s);
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync return VINF_SUCCESS;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync}
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync/**
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Clears the user entry in a user table.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * This is used to remove all references to a page when flushing it.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncstatic void pgmPoolTrackClearPageUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PCPGMPOOLUSER pUser)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync{
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(pUser->iUser != NIL_PGMPOOL_IDX);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(pUser->iUser < pPool->cCurPages);
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync uint32_t iUserTable = pUser->iUserTable;
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /*
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Map the user page.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PPGMPOOLPAGE pUserPage = &pPool->aPages[pUser->iUser];
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync union
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uint64_t *pau64;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uint32_t *pau32;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync } u;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync u.pau64 = (uint64_t *)PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pUserPage);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolTrackClearPageUser: clear %x in %s (%RGp) (flushing %s)\n", iUserTable, pgmPoolPoolKindToStr(pUserPage->enmKind), pUserPage->Core.Key, pgmPoolPoolKindToStr(pPage->enmKind)));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* Safety precaution in case we change the paging for other modes too in the future. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(!pgmPoolIsPageLocked(&pPool->CTX_SUFF(pVM)->pgm.s, pPage));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#ifdef VBOX_STRICT
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /*
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Some sanity checks.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync switch (pUserPage->enmKind)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(iUserTable < X86_PG_ENTRIES);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PDPT:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(iUserTable < 4);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_PAE_PD_PHYS:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync Assert(iUserTable < X86_PG_PAE_ENTRIES);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(iUserTable < X86_PG_PAE_ENTRIES);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(!(u.pau64[iUserTable] & PGM_PDFLAGS_MAPPING));
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync break;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync Assert(iUserTable < X86_PG_PAE_ENTRIES);
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync break;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_64BIT_PML4:
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* GCPhys >> PAGE_SHIFT is the index here */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(iUserTable < X86_PG_PAE_ENTRIES);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(iUserTable < X86_PG_PAE_ENTRIES);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync case PGMPOOLKIND_ROOT_NESTED:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(iUserTable < X86_PG_PAE_ENTRIES);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync default:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertMsgFailed(("enmKind=%d\n", pUserPage->enmKind));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#endif /* VBOX_STRICT */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /*
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Clear the entry in the user page.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync */
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync switch (pUserPage->enmKind)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* 32-bit entries */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMAtomicWriteSize(&u.pau32[iUserTable], 0);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* 64-bit entries */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#if defined(IN_RC)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* 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
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * non-present PDPT will continue to cause page faults.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMReloadCR3();
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#endif
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync /* no break */
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync case PGMPOOLKIND_PAE_PD_PHYS:
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync case PGMPOOLKIND_64BIT_PML4:
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync case PGMPOOLKIND_PAE_PDPT:
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync case PGMPOOLKIND_ROOT_NESTED:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMAtomicWriteSize(&u.pau64[iUserTable], 0);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync default:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertFatalMsgFailed(("enmKind=%d iUser=%#x iUserTable=%#x\n", pUserPage->enmKind, pUser->iUser, pUser->iUserTable));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync}
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/**
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Clears all users of a page.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncstatic void pgmPoolTrackClearPageUsers(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync{
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync /*
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Free all the user records.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolTrackClearPageUsers %RGp\n", pPage->GCPhys));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync uint16_t i = pPage->iUserHead;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync while (i != NIL_PGMPOOL_USER_INDEX)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* Clear enter in user table. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTrackClearPageUser(pPool, pPage, &paUsers[i]);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* Free it. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync const uint16_t iNext = paUsers[i].iNext;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync paUsers[i].iUser = NIL_PGMPOOL_IDX;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync paUsers[i].iNext = pPool->iUserFreeHead;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPool->iUserFreeHead = i;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* Next. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync i = iNext;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync}
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/**
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Allocates a new physical cross reference extent.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * @returns Pointer to the allocated extent on success. NULL if we're out of them.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pVM The VM handle.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param piPhysExt Where to store the phys ext index.
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync */
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsyncPPGMPOOLPHYSEXT pgmPoolTrackPhysExtAlloc(PVM pVM, uint16_t *piPhysExt)
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync{
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync Assert(PGMIsLockOwner(pVM));
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
c9c755c35cd95c006f7ecdb07f2fa8d64686ae3dvboxsync uint16_t iPhysExt = pPool->iPhysExtFreeHead;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (iPhysExt == NIL_PGMPOOL_PHYSEXT_INDEX)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pPool->StamTrackPhysExtAllocFailures);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync return NULL;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PPGMPOOLPHYSEXT pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPool->iPhysExtFreeHead = pPhysExt->iNext;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPhysExt->iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *piPhysExt = iPhysExt;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync return pPhysExt;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync}
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync/**
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync * Frees a physical cross reference extent.
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync *
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync * @param pVM The VM handle.
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync * @param iPhysExt The extent to free.
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync */
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsyncvoid pgmPoolTrackPhysExtFree(PVM pVM, uint16_t iPhysExt)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync{
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync Assert(PGMIsLockOwner(pVM));
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync Assert(iPhysExt < pPool->cMaxPhysExts);
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync PPGMPOOLPHYSEXT pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync pPhysExt->iNext = pPool->iPhysExtFreeHead;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync pPool->iPhysExtFreeHead = iPhysExt;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync}
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync/**
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync * Frees a physical cross reference extent.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pVM The VM handle.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param iPhysExt The extent to free.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncvoid pgmPoolTrackPhysExtFreeList(PVM pVM, uint16_t iPhysExt)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync{
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(PGMIsLockOwner(pVM));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync const uint16_t iPhysExtStart = iPhysExt;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PPGMPOOLPHYSEXT pPhysExt;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync do
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(iPhysExt < pPool->cMaxPhysExts);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* next */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync iPhysExt = pPhysExt->iNext;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync } while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPhysExt->iNext = pPool->iPhysExtFreeHead;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPool->iPhysExtFreeHead = iPhysExtStart;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync}
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/**
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Insert a reference into a list of physical cross reference extents.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @returns The new tracking data for PGMPAGE.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pVM The VM handle.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param iPhysExt The physical extent index of the list head.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param iShwPT The shadow page table index.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncstatic uint16_t pgmPoolTrackPhysExtInsert(PVM pVM, uint16_t iPhysExt, uint16_t iShwPT)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync{
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(PGMIsLockOwner(pVM));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* special common case. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (paPhysExts[iPhysExt].aidx[2] == NIL_PGMPOOL_IDX)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync paPhysExts[iPhysExt].aidx[2] = iShwPT;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedMany);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolTrackPhysExtInsert: %d:{,,%d}\n", iPhysExt, iShwPT));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExt);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* general treatment. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync const uint16_t iPhysExtStart = iPhysExt;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync unsigned cMax = 15;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync for (;;)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(iPhysExt < pPool->cMaxPhysExts);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync for (unsigned i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (paPhysExts[iPhysExt].aidx[i] == NIL_PGMPOOL_IDX)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync paPhysExts[iPhysExt].aidx[i] = iShwPT;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedMany);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolTrackPhysExtInsert: %d:{%d} i=%d cMax=%d\n", iPhysExt, iShwPT, i, cMax));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExtStart);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (!--cMax)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatTrackOverflows);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTrackPhysExtFreeList(pVM, iPhysExtStart);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolTrackPhysExtInsert: overflow (1) iShwPT=%d\n", iShwPT));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* add another extent to the list. */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PPGMPOOLPHYSEXT pNew = pgmPoolTrackPhysExtAlloc(pVM, &iPhysExt);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (!pNew)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatTrackNoExtentsLeft);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTrackPhysExtFreeList(pVM, iPhysExtStart);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolTrackPhysExtInsert: pgmPoolTrackPhysExtAlloc failed iShwPT=%d\n", iShwPT));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pNew->iNext = iPhysExtStart;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pNew->aidx[0] = iShwPT;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolTrackPhysExtInsert: added new extent %d:{%d}->%d\n", iPhysExt, iShwPT, iPhysExtStart));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExt);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync}
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/**
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Add a reference to guest physical page where extents are in use.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @returns The new tracking data for PGMPAGE.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pVM The VM handle.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param u16 The ram range flags (top 16-bits).
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param iShwPT The shadow page table index.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncuint16_t pgmPoolTrackPhysExtAddref(PVM pVM, uint16_t u16, uint16_t iShwPT)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync{
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmLock(pVM);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (PGMPOOL_TD_GET_CREFS(u16) != PGMPOOL_TD_CREFS_PHYSEXT)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /*
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Convert to extent list.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(PGMPOOL_TD_GET_CREFS(u16) == 1);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync uint16_t iPhysExt;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PPGMPOOLPHYSEXT pPhysExt = pgmPoolTrackPhysExtAlloc(pVM, &iPhysExt);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pPhysExt)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync LogFlow(("pgmPoolTrackPhysExtAddref: new extent: %d:{%d, %d}\n", iPhysExt, PGMPOOL_TD_GET_IDX(u16), iShwPT));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliased);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPhysExt->aidx[0] = PGMPOOL_TD_GET_IDX(u16);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPhysExt->aidx[1] = iShwPT;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync u16 = PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExt);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync else
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync u16 = PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync else if (u16 != PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED))
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /*
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Insert into the extent list.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync u16 = pgmPoolTrackPhysExtInsert(pVM, PGMPOOL_TD_GET_IDX(u16), iShwPT);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync else
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedLots);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmUnlock(pVM);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return u16;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync}
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Clear references to guest physical memory.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPool The pool.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPage The page.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pPhysPage Pointer to the aPages entry in the ram range.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncvoid pgmPoolTrackPhysExtDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PPGMPAGE pPhysPage)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync{
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync const unsigned cRefs = PGM_PAGE_GET_TD_CREFS(pPhysPage);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync AssertFatalMsg(cRefs == PGMPOOL_TD_CREFS_PHYSEXT, ("cRefs=%d pPhysPage=%R[pgmpage] pPage=%p:{.idx=%d}\n", cRefs, pPhysPage, pPage, pPage->idx));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync uint16_t iPhysExt = PGM_PAGE_GET_TD_IDX(pPhysPage);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (iPhysExt != PGMPOOL_TD_IDX_OVERFLOWED)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PVM pVM = pPool->CTX_SUFF(pVM);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmLock(pVM);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync uint16_t iPhysExtPrev = NIL_PGMPOOL_PHYSEXT_INDEX;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync do
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(iPhysExt < pPool->cMaxPhysExts);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /*
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Look for the shadow page and check if it's all freed.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync for (unsigned i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (paPhysExts[iPhysExt].aidx[i] == pPage->idx)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync paPhysExts[iPhysExt].aidx[i] = NIL_PGMPOOL_IDX;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync for (i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (paPhysExts[iPhysExt].aidx[i] != NIL_PGMPOOL_IDX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d\n", pPhysPage, pPage->idx));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmUnlock(pVM);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* we can free the node. */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync const uint16_t iPhysExtNext = paPhysExts[iPhysExt].iNext;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if ( iPhysExtPrev == NIL_PGMPOOL_PHYSEXT_INDEX
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync && iPhysExtNext == NIL_PGMPOOL_PHYSEXT_INDEX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* lonely node */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmPoolTrackPhysExtFree(pVM, iPhysExt);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d lonely\n", pPhysPage, pPage->idx));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PGM_PAGE_SET_TRACKING(pPhysPage, 0);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync else if (iPhysExtPrev == NIL_PGMPOOL_PHYSEXT_INDEX)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* head */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d head\n", pPhysPage, pPage->idx));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PGM_PAGE_SET_TRACKING(pPhysPage, PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExtNext));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmPoolTrackPhysExtFree(pVM, iPhysExt);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync else
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* in list */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d\n", pPhysPage, pPage->idx));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync paPhysExts[iPhysExtPrev].iNext = iPhysExtNext;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmPoolTrackPhysExtFree(pVM, iPhysExt);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync iPhysExt = iPhysExtNext;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmUnlock(pVM);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* next */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync iPhysExtPrev = iPhysExt;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync iPhysExt = paPhysExts[iPhysExt].iNext;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync } while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmUnlock(pVM);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync AssertFatalMsgFailed(("not-found! cRefs=%d pPhysPage=%R[pgmpage] pPage=%p:{.idx=%d}\n", cRefs, pPhysPage, pPage, pPage->idx));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync else /* nothing to do */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage]\n", pPhysPage));
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync}
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync/**
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * Clear references to guest physical memory.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync *
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * This is the same as pgmPoolTracDerefGCPhys except that the guest physical address
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * is assumed to be correct, so the linear search can be skipped and we can assert
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * at an earlier point.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync *
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * @param pPool The pool.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * @param pPage The page.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * @param HCPhys The host physical address corresponding to the guest page.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync * @param GCPhys The guest physical address corresponding to HCPhys.
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync */
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsyncstatic void pgmPoolTracDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhys)
ddcb64df779903f563ad28b5b018bcf94ba22a56vboxsync{
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /*
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Walk range list.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync while (pRam)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync RTGCPHYS off = GCPhys - pRam->GCPhys;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (off < pRam->cb)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* does it match? */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync const unsigned iPage = off >> PAGE_SHIFT;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#ifdef LOG_ENABLED
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync RTHCPHYS HCPhysPage = PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log2(("pgmPoolTracDerefGCPhys %RHp vs %RHp\n", HCPhysPage, HCPhys));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync#endif
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(pPage->cPresent);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(pPool->cPresent);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->cPresent--;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPool->cPresent--;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pRam = pRam->CTX_SUFF(pNext);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertFatalMsgFailed(("HCPhys=%RHp GCPhys=%RGp\n", HCPhys, GCPhys));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync}
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Clear references to guest physical memory.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pPool The pool.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPage The page.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param HCPhys The host physical address corresponding to the guest page.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param GCPhysHint The guest physical address which may corresponding to HCPhys.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsyncvoid pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync{
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log4(("pgmPoolTracDerefGCPhysHint %RHp %RGp\n", HCPhys, GCPhysHint));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /*
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Walk range list.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync while (pRam)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync RTGCPHYS off = GCPhysHint - pRam->GCPhys;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (off < pRam->cb)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /* does it match? */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync const unsigned iPage = off >> PAGE_SHIFT;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Assert(pPage->cPresent);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(pPool->cPresent);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPage->cPresent--;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->cPresent--;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync break;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pRam = pRam->CTX_SUFF(pNext);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync /*
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Damn, the hint didn't work. We'll have to do an expensive linear search.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync STAM_COUNTER_INC(&pPool->StatTrackLinearRamSearches);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync while (pRam)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync unsigned iPage = pRam->cb >> PAGE_SHIFT;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync while (iPage-- > 0)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log4(("pgmPoolTracDerefGCPhysHint: Linear HCPhys=%RHp GCPhysHint=%RGp GCPhysReal=%RGp\n",
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync HCPhys, GCPhysHint, pRam->GCPhys + (iPage << PAGE_SHIFT)));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(pPage->cPresent);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(pPool->cPresent);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->cPresent--;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pPool->cPresent--;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync return;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pRam = pRam->CTX_SUFF(pNext);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertFatalMsgFailed(("HCPhys=%RHp GCPhysHint=%RGp\n", HCPhys, GCPhysHint));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync}
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync/**
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync * Clear references to guest physical memory in a 32-bit / 32-bit page table.
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync *
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync * @param pPool The pool.
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync * @param pPage The page.
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync * @param pShwPT The shadow page table (mapping of the page).
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync * @param pGstPT The guest page table.
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync */
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsyncDECLINLINE(void) pgmPoolTrackDerefPT32Bit32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT, PCX86PT pGstPT)
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync{
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync if (pShwPT->a[i].n.u1Present)
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync {
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync Log4(("pgmPoolTrackDerefPT32Bit32Bit: i=%d pte=%RX32 hint=%RX32\n",
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync i, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK));
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync if (!pPage->cPresent)
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync break;
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync }
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync}
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync/**
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync * Clear references to guest physical memory in a PAE / 32-bit page table.
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync *
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync * @param pPool The pool.
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync * @param pPage The page.
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync * @param pShwPT The shadow page table (mapping of the page).
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync * @param pGstPT The guest page table (just a half one).
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync */
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsyncDECLINLINE(void) pgmPoolTrackDerefPTPae32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PT pGstPT)
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync{
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync if (pShwPT->a[i].n.u1Present)
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync {
ece539e847eaf73e9a1910b562d998dc0afc5580vboxsync Log4(("pgmPoolTrackDerefPTPae32Bit: i=%d pte=%RX64 hint=%RX32\n",
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (!pPage->cPresent)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync}
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Clear references to guest physical memory in a PAE / PAE page table.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPool The pool.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pPage The page.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pShwPT The shadow page table (mapping of the page).
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pGstPT The guest page table.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsyncDECLINLINE(void) pgmPoolTrackDerefPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PTPAE pGstPT)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync{
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pShwPT->a[i].n.u1Present)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log4(("pgmPoolTrackDerefPTPaePae: i=%d pte=%RX32 hint=%RX32\n",
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PAE_PG_MASK));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PAE_PG_MASK);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (!pPage->cPresent)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync}
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Clear references to guest physical memory in a 32-bit / 4MB page table.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPool The pool.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPage The page.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pShwPT The shadow page table (mapping of the page).
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsyncDECLINLINE(void) pgmPoolTrackDerefPT32Bit4MB(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync{
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync RTGCPHYS GCPhys = pPage->GCPhys + PAGE_SIZE * pPage->iFirstPresent;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pShwPT->a[i].n.u1Present)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log4(("pgmPoolTrackDerefPT32Bit4MB: i=%d pte=%RX32 GCPhys=%RGp\n",
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync i, pShwPT->a[i].u & X86_PTE_PG_MASK, GCPhys));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, GCPhys);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (!pPage->cPresent)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync}
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Clear references to guest physical memory in a PAE / 2/4MB page table.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPool The pool.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPage The page.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pShwPT The shadow page table (mapping of the page).
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsyncDECLINLINE(void) pgmPoolTrackDerefPTPaeBig(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync{
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync RTGCPHYS GCPhys = pPage->GCPhys + PAGE_SIZE * pPage->iFirstPresent;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pShwPT->a[i].n.u1Present)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log4(("pgmPoolTrackDerefPTPaeBig: i=%d pte=%RX64 hint=%RGp\n",
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, GCPhys));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, GCPhys);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (!pPage->cPresent)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync}
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Clear references to shadowed pages in an EPT page table.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pPool The pool.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPage The page.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pShwPML4 The shadow page directory pointer table (mapping of the page).
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncDECLINLINE(void) pgmPoolTrackDerefPTEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPT pShwPT)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync{
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync RTGCPHYS GCPhys = pPage->GCPhys + PAGE_SIZE * pPage->iFirstPresent;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (pShwPT->a[i].n.u1Present)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync Log4(("pgmPoolTrackDerefPTEPT: i=%d pte=%RX64 GCPhys=%RX64\n",
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync i, pShwPT->a[i].u & EPT_PTE_PG_MASK, pPage->GCPhys));
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & EPT_PTE_PG_MASK, GCPhys);
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if (!pPage->cPresent)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync break;
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync }
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync}
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync/**
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * Clear references to shadowed pages in a 32 bits page directory.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pPool The pool.
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync * @param pPage The page.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pShwPD The shadow page directory (mapping of the page).
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync */
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsyncDECLINLINE(void) pgmPoolTrackDerefPD(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PD pShwPD)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync{
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync if ( pShwPD->a[i].n.u1Present
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync && !(pShwPD->a[i].u & PGM_PDFLAGS_MAPPING)
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync )
50671c30431539dd7d6ff6a5f2ceb6c9f9f471b2vboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PG_MASK);
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync if (pSubPage)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync else
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync AssertFatalMsgFailed(("%x\n", pShwPD->a[i].u & X86_PDE_PG_MASK));
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync }
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync }
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync}
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync/**
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * Clear references to shadowed pages in a PAE (legacy or 64 bits) page directory.
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync *
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * @param pPool The pool.
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * @param pPage The page.
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * @param pShwPD The shadow page directory (mapping of the page).
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync */
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsyncDECLINLINE(void) pgmPoolTrackDerefPDPae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPAE pShwPD)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync{
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if ( pShwPD->a[i].n.u1Present
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync && !(pShwPD->a[i].u & PGM_PDFLAGS_MAPPING)
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync )
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync {
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync#ifdef PGM_WITH_LARGE_PAGES
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync if (pShwPD->a[i].b.u1Size)
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync {
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync Log4(("pgmPoolTrackDerefPDPae: i=%d pde=%RX64 GCPhys=%RX64\n",
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync i, pShwPD->a[i].u & X86_PDE2M_PAE_PG_MASK, pPage->GCPhys));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTracDerefGCPhys(pPool, pPage, pShwPD->a[i].u & X86_PDE2M_PAE_PG_MASK, pPage->GCPhys /* == base of 2 MB page */);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync else
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#endif
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync {
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PAE_PG_MASK);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (pSubPage)
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync else
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync AssertFatalMsgFailed(("%RX64\n", pShwPD->a[i].u & X86_PDE_PAE_PG_MASK));
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync}
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync/**
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * Clear references to shadowed pages in a PAE page directory pointer table.
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync *
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * @param pPool The pool.
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * @param pPage The page.
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * @param pShwPDPT The shadow page directory pointer table (mapping of the page).
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync */
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsyncDECLINLINE(void) pgmPoolTrackDerefPDPTPae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPT pShwPDPT)
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync{
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync for (unsigned i = 0; i < X86_PG_PAE_PDPE_ENTRIES; i++)
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync {
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync if ( pShwPDPT->a[i].n.u1Present
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync && !(pShwPDPT->a[i].u & PGM_PLXFLAGS_MAPPING)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync )
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & X86_PDPE_PG_MASK);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (pSubPage)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync else
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & X86_PDPE_PG_MASK));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync}
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync/**
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * Clear references to shadowed pages in a 64-bit page directory pointer table.
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync *
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * @param pPool The pool.
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * @param pPage The page.
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * @param pShwPDPT The shadow page directory pointer table (mapping of the page).
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync */
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsyncDECLINLINE(void) pgmPoolTrackDerefPDPT64Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPT pShwPDPT)
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync{
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPDPT->a); i++)
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync {
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync Assert(!(pShwPDPT->a[i].u & PGM_PLXFLAGS_MAPPING));
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync if (pShwPDPT->a[i].n.u1Present)
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync {
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & X86_PDPE_PG_MASK);
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync if (pSubPage)
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync else
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & X86_PDPE_PG_MASK));
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync }
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync }
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync}
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync/**
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * Clear references to shadowed pages in a 64-bit level 4 page table.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * @param pPool The pool.
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * @param pPage The page.
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * @param pShwPML4 The shadow page directory pointer table (mapping of the page).
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync */
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsyncDECLINLINE(void) pgmPoolTrackDerefPML464Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PML4 pShwPML4)
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync{
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPML4->a); i++)
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync {
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync if (pShwPML4->a[i].n.u1Present)
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync {
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPML4->a[i].u & X86_PDPE_PG_MASK);
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync if (pSubPage)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync else
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync AssertFatalMsgFailed(("%RX64\n", pShwPML4->a[i].u & X86_PML4E_PG_MASK));
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync }
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync }
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync}
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/**
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * Clear references to shadowed pages in an EPT page directory.
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync *
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * @param pPool The pool.
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * @param pPage The page.
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * @param pShwPD The shadow page directory (mapping of the page).
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncDECLINLINE(void) pgmPoolTrackDerefPDEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPD pShwPD)
87c5113417e917cdf64545d4f8e0a27047cea783vboxsync{
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync {
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync if (pShwPD->a[i].n.u1Present)
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync#ifdef PGM_WITH_LARGE_PAGES
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync if (pShwPD->a[i].b.u1Size)
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync {
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync Log4(("pgmPoolTrackDerefPDEPT: i=%d pde=%RX64 GCPhys=%RX64\n",
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync i, pShwPD->a[i].u & X86_PDE2M_PAE_PG_MASK, pPage->GCPhys));
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync pgmPoolTracDerefGCPhys(pPool, pPage, pShwPD->a[i].u & X86_PDE2M_PAE_PG_MASK, pPage->GCPhys /* == base of 2 MB page */);
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync }
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync else
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync#endif
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync {
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & EPT_PDE_PG_MASK);
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync if (pSubPage)
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync else
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync AssertFatalMsgFailed(("%RX64\n", pShwPD->a[i].u & EPT_PDE_PG_MASK));
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync }
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync }
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync}
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync/**
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * Clear references to shadowed pages in an EPT page directory pointer table.
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync *
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * @param pPool The pool.
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * @param pPage The page.
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync * @param pShwPDPT The shadow page directory pointer table (mapping of the page).
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync */
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsyncDECLINLINE(void) pgmPoolTrackDerefPDPTEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPDPT pShwPDPT)
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync{
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPDPT->a); i++)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync if (pShwPDPT->a[i].n.u1Present)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & EPT_PDPTE_PG_MASK);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync if (pSubPage)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync else
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & EPT_PDPTE_PG_MASK));
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync }
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync }
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync}
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync/**
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * Clears all references made by this page.
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync *
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * This includes other shadow pages and GC physical addresses.
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync *
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * @param pPool The pool.
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * @param pPage The page.
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync */
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsyncstatic void pgmPoolTrackDeref(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync{
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync /*
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync * Map the shadow page and take action according to the page kind.
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync */
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync void *pvShw = PGMPOOL_PAGE_2_LOCKED_PTR(pPool->CTX_SUFF(pVM), pPage);
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync switch (pPage->enmKind)
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync {
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync {
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync void *pvGst;
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync int rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync pgmPoolTrackDerefPT32Bit32Bit(pPool, pPage, (PX86PT)pvShw, (PCX86PT)pvGst);
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync break;
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync }
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync {
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync void *pvGst;
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync int rc = PGM_GCPHYS_2_PTR_EX(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync pgmPoolTrackDerefPTPae32Bit(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PT)pvGst);
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
9a59b271202449cf51ec3ac13b8ef1b4b9810850vboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync void *pvGst;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync int rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTrackDerefPTPaePae(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PTPAE)pvGst);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS: /* treat it like a 4 MB page */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTrackDerefPT32Bit4MB(pPool, pPage, (PX86PT)pvShw);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS: /* treat it like a 2 MB page */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync {
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTrackDerefPTPaeBig(pPool, pPage, (PX86PTPAE)pvShw);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PD_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTrackDerefPDPae(pPool, pPage, (PX86PDPAE)pvShw);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_32BIT_PD_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_32BIT_PD:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTrackDerefPD(pPool, pPage, (PX86PD)pvShw);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PDPT:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_PAE_PDPT_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTrackDerefPDPTPae(pPool, pPage, (PX86PDPT)pvShw);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTrackDerefPDPT64Bit(pPool, pPage, (PX86PDPT)pvShw);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_64BIT_PML4:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTrackDerefPML464Bit(pPool, pPage, (PX86PML4)pvShw);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_EPT_PT_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTrackDerefPTEPT(pPool, pPage, (PEPTPT)pvShw);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_EPT_PD_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTrackDerefPDEPT(pPool, pPage, (PEPTPD)pvShw);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pgmPoolTrackDerefPDPTEPT(pPool, pPage, (PEPTPDPT)pvShw);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync break;
6f34d825e07e8dcd535b37d7685ab62220a23addvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync default:
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync AssertFatalMsgFailed(("enmKind=%d\n", pPage->enmKind));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync }
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync /* paranoia, clear the shadow page. Remove this laser (i.e. let Alloc and ClearAll do it). */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_PROFILE_START(&pPool->StatZeroPage, z);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync ASMMemZeroPage(pvShw);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage->fZeroed = true;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PGMPOOL_UNLOCK_PTR(pPool->CTX_SUFF(pVM), pvShw);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync Assert(!pPage->cPresent);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync}
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync/**
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * Flushes a pool page.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * This moves the page to the free list after removing all user references to it.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync *
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @returns VBox status code.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @retval VINF_SUCCESS on success.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param pPool The pool.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param HCPhys The HC physical address of the shadow page.
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync * @param fFlush Flush the TLBS when required (should only be false in very specific use cases!!)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync */
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsyncint pgmPoolFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage, bool fFlush)
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync{
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync PVM pVM = pPool->CTX_SUFF(pVM);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync bool fFlushRequired = false;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync int rc = VINF_SUCCESS;
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync STAM_PROFILE_START(&pPool->StatFlushPage, f);
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync LogFlow(("pgmPoolFlushPage: pPage=%p:{.Key=%RHp, .idx=%d, .enmKind=%s, .GCPhys=%RGp}\n",
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), pPage->GCPhys));
9888fffcfbe2d41dce14a1249b12cb88cc9b149fvboxsync
/*
* 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 an ASMMemZeroPage().
*/
pPage->fZeroed = false;
#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
if (pPage->fDirty)
pgmPoolFlushDirtyPage(pVM, pPool, pPage->idxDirty, false /* do not remove */);
#endif
/* If there are any users of this table, then we *must* issue a tlb flush on all VCPUs. */
if (pPage->iUserHead != NIL_PGMPOOL_USER_INDEX)
fFlushRequired = true;
/*
* Clear the page.
*/
pgmPoolTrackClearPageUsers(pPool, pPage);
STAM_PROFILE_START(&pPool->StatTrackDeref,a);
pgmPoolTrackDeref(pPool, pPage);
STAM_PROFILE_STOP(&pPool->StatTrackDeref,a);
/*
* Flush it from the cache.
*/
pgmPoolCacheFlushPage(pPool, pPage);
#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
/* Heavy stuff done. */
PGMDynMapPopAutoSubset(pVCpu, iPrevSubset);
#endif
/*
* Deregistering the monitoring.
*/
if (pPage->fMonitored)
rc = pgmPoolMonitorFlush(pPool, pPage);
/*
* Free the page.
*/
Assert(pPage->iNext == NIL_PGMPOOL_IDX);
pPage->iNext = pPool->iFreeHead;
pPool->iFreeHead = pPage->idx;
pPage->enmKind = PGMPOOLKIND_FREE;
pPage->enmAccess = PGMPOOLACCESS_DONTCARE;
pPage->GCPhys = NIL_RTGCPHYS;
pPage->fReusedFlushPending = false;
pPool->cUsedPages--;
/* Flush the TLBs of all VCPUs if required. */
if ( fFlushRequired
&& fFlush)
{
PGM_INVL_ALL_VCPU_TLBS(pVM);
}
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);
pgmPoolTrackFreeUser(pPool, pPage, iUser, iUserTable);
if (!pPage->fCached)
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)
{
PVM pVM = pPool->CTX_SUFF(pVM);
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(pVM);
#else
int rc = VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_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;
}
/*
* Free one cached page.
*/
return pgmPoolCacheFreeOne(pPool, iUser);
}
/**
* 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 enmAccess Access type for the mapping (only relevant for big pages)
* @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.
* @param fLockPage Lock the page
*/
int pgmPoolAllocEx(PVM pVM, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, PGMPOOLACCESS enmAccess, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage, bool fLockPage)
{
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_SYNC_CLEAR_PGM_POOL)); */
pgmLock(pVM);
if (pPool->fCacheEnabled)
{
int rc2 = pgmPoolCacheAlloc(pPool, GCPhys, enmKind, enmAccess, iUser, iUserTable, ppPage);
if (RT_SUCCESS(rc2))
{
if (fLockPage)
pgmPoolLockPage(pPool, *ppPage);
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;
}
}
/*
* 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->enmAccess = enmAccess;
pPage->GCPhys = GCPhys;
pPage->fSeenNonGlobal = false; /* Set this to 'true' to disable this feature. */
pPage->fMonitored = false;
pPage->fCached = false;
#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
pPage->fDirty = false;
#endif
pPage->fReusedFlushPending = false;
pPage->cModifications = 0;
pPage->iModifiedNext = NIL_PGMPOOL_IDX;
pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
pPage->cPresent = 0;
pPage->iFirstPresent = NIL_PGMPOOL_PRESENT_INDEX;
pPage->pvLastAccessHandlerFault = 0;
pPage->cLastAccessHandlerCount = 0;
pPage->pvLastAccessHandlerRip = 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->enmAccess = PGMPOOLACCESS_DONTCARE;
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;
}
/*
* 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;
if (fLockPage)
pgmPoolLockPage(pPool, 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);
Assert(PGMIsLockOwner(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 /* currently only used in ring 3; save some space in the R0 & GC modules (left it here as we might need it elsewhere later on) */
/**
* Flush the specified page if present
*
* @param pVM The VM handle.
* @param GCPhys Guest physical address of the page to flush
*/
void pgmPoolFlushPageByGCPhys(PVM pVM, RTGCPHYS GCPhys)
{
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
VM_ASSERT_EMT(pVM);
/*
* Look up the GCPhys in the hash.
*/
GCPhys = GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
unsigned i = pPool->aiHash[PGMPOOL_HASH(GCPhys)];
if (i == NIL_PGMPOOL_IDX)
return;
do
{
PPGMPOOLPAGE pPage = &pPool->aPages[i];
if (pPage->GCPhys - GCPhys < PAGE_SIZE)
{
switch (pPage->enmKind)
{
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
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_64BIT_PD_FOR_64BIT_PD:
case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_PAE_PDPT:
{
Log(("PGMPoolFlushPage: found pgm pool pages for %RGp\n", GCPhys));
#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
if (pPage->fDirty)
STAM_COUNTER_INC(&pPool->StatForceFlushDirtyPage);
else
#endif
STAM_COUNTER_INC(&pPool->StatForceFlushPage);
Assert(!pgmPoolIsPageLocked(&pVM->pgm.s, pPage));
pgmPoolMonitorChainFlush(pPool, pPage);
return;
}
/* ignore, no monitoring. */
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:
case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
case PGMPOOLKIND_EPT_PD_FOR_PHYS:
case PGMPOOLKIND_EPT_PT_FOR_PHYS:
case PGMPOOLKIND_ROOT_NESTED:
case PGMPOOLKIND_PAE_PD_PHYS:
case PGMPOOLKIND_PAE_PDPT_PHYS:
case PGMPOOLKIND_32BIT_PD_PHYS:
case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
break;
default:
AssertFatalMsgFailed(("enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
}
}
/* next */
i = pPage->iNext;
} while (i != NIL_PGMPOOL_IDX);
return;
}
#endif /* IN_RING3 */
#ifdef IN_RING3
/**
* Reset CPU on hot plugging.
*
* @param pVM The VM handle.
* @param pVCpu The virtual CPU.
*/
void pgmR3PoolResetUnpluggedCpu(PVM pVM, PVMCPU pVCpu)
{
pgmR3ExitShadowModeBeforePoolFlush(pVM, pVCpu);
pgmR3ReEnterShadowModeAfterPoolFlush(pVM, pVCpu);
VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
VMCPU_FF_SET(pVCpu, VMCPU_FF_TLB_FLUSH);
}
/**
* 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 pgmR3PoolReset(PVM pVM)
{
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
Assert(PGMIsLockOwner(pVM));
STAM_PROFILE_START(&pPool->StatR3Reset, a);
LogFlow(("pgmR3PoolReset:\n"));
/*
* If there are no pages in the pool, there is nothing to do.
*/
if (pPool->cCurPages <= PGMPOOL_IDX_FIRST)
{
STAM_PROFILE_STOP(&pPool->StatR3Reset, a);
return;
}
/*
* Exit the shadow mode since we're going to clear everything,
* including the root page.
*/
for (VMCPUID 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));
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;
pPage->GCPhys = NIL_RTGCPHYS;
pPage->enmKind = PGMPOOLKIND_FREE;
pPage->enmAccess = PGMPOOLACCESS_DONTCARE;
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;
#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
pPage->fDirty = false;
#endif
pPage->fCached = false;
pPage->fReusedFlushPending = false;
pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
pPage->iAgeNext = NIL_PGMPOOL_IDX;
pPage->iAgePrev = NIL_PGMPOOL_IDX;
pPage->cLocked = 0;
}
pPool->aPages[pPool->cCurPages - 1].iNext = NIL_PGMPOOL_IDX;
pPool->iFreeHead = PGMPOOL_IDX_FIRST;
pPool->cUsedPages = 0;
/*
* 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;
/*
* 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;
/*
* Just zap the modified list.
*/
pPool->cModifiedPages = 0;
pPool->iModifiedHead = NIL_PGMPOOL_IDX;
/*
* 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;
#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
/* Clear all dirty pages. */
pPool->idxFreeDirtyPage = 0;
pPool->cDirtyPages = 0;
for (unsigned i = 0; i < RT_ELEMENTS(pPool->aIdxDirtyPages); i++)
pPool->aIdxDirtyPages[i] = 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;
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);
pgmPoolHashInsert(pPool, pPage);
}
Assert(pPage->iUserHead == NIL_PGMPOOL_USER_INDEX); /* for now */
Assert(pPage->iAgeNext == NIL_PGMPOOL_IDX);
Assert(pPage->iAgePrev == NIL_PGMPOOL_IDX);
}
for (VMCPUID i = 0; i < pVM->cCpus; i++)
{
/*
* Re-enter the shadowing mode and assert Sync CR3 FF.
*/
PVMCPU pVCpu = &pVM->aCpus[i];
pgmR3ReEnterShadowModeAfterPoolFlush(pVM, pVCpu);
VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
VMCPU_FF_SET(pVCpu, VMCPU_FF_TLB_FLUSH);
}
STAM_PROFILE_STOP(&pPool->StatR3Reset, a);
}
#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*/