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