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