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