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