PGMAllPool.cpp revision 689e03ca2c98ca6feaab8d9de6e4687a98f14ccb
248c89033c87fed7229aa29bbbc4f4698fb13687vboxsync * PGM Shadow Page Pool.
248c89033c87fed7229aa29bbbc4f4698fb13687vboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * available from http://www.virtualbox.org. This file is free software;
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * you can redistribute it and/or modify it under the terms of the GNU
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * General Public License (GPL) as published by the Free Software
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * additional information or have any questions.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync/*******************************************************************************
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync* Header Files *
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync*******************************************************************************/
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync/*******************************************************************************
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync* Internal Functions *
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync*******************************************************************************/
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsyncDECLINLINE(unsigned) pgmPoolTrackGetShadowEntrySize(PGMPOOLKIND enmKind);
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsyncDECLINLINE(unsigned) pgmPoolTrackGetGuestEntrySize(PGMPOOLKIND enmKind);
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsyncstatic void pgmPoolTrackDeref(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsyncstatic void pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint);
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsyncstatic int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsyncstatic void pgmPoolMonitorModifiedRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsyncDECLEXPORT(int) pgmPoolAccessHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsyncstatic const char *pgmPoolPoolKindToStr(uint8_t enmKind);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsyncvoid pgmPoolTrackFlushGCPhysPT(PVM pVM, PPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsyncvoid pgmPoolTrackFlushGCPhysPTs(PVM pVM, PPGMPAGE pPhysPage, uint16_t iPhysExt);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsyncint pgmPoolTrackFlushGCPhysPTsSlow(PVM pVM, PPGMPAGE pPhysPage);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsyncPPGMPOOLPHYSEXT pgmPoolTrackPhysExtAlloc(PVM pVM, uint16_t *piPhysExt);
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsyncvoid pgmPoolTrackPhysExtFree(PVM pVM, uint16_t iPhysExt);
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsyncvoid pgmPoolTrackPhysExtFreeList(PVM pVM, uint16_t iPhysExt);
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync * Checks if the specified page pool kind is for a 4MB or 2MB guest page.
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync * @returns true if it's the shadow of a 4MB or 2MB guest page, otherwise false.
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync * @param enmKind The page kind.
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsyncDECLINLINE(bool) pgmPoolIsBigPage(PGMPOOLKIND enmKind)
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync return true;
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync return false;
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync/** @def PGMPOOL_PAGE_2_LOCKED_PTR
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Maps a pool page pool into the current context and lock it (RC only).
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @returns VBox status code.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pVM The VM handle.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pPage The pool page.
ffb50166c9adb4ae583b914d405197035cf890advboxsync * @remark In RC this uses PGMGCDynMapHCPage(), so it will consume of the
ffb50166c9adb4ae583b914d405197035cf890advboxsync * small page window employeed by that function. Be careful.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @remark There is no need to assert on the result.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsyncDECLINLINE(void *) PGMPOOL_PAGE_2_LOCKED_PTR(PVM pVM, PPGMPOOLPAGE pPage)
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync void *pv = pgmPoolMapPageInlined(&pVM->pgm.s, pPage);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* Make sure the dynamic mapping will not be reused. */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync# define PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage) PGMPOOL_PAGE_2_PTR(pVM, pPage)
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync/** @def PGMPOOL_UNLOCK_PTR
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Unlock a previously locked dynamic caching (RC only).
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @returns VBox status code.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pVM The VM handle.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pPage The pool page.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @remark In RC this uses PGMGCDynMapHCPage(), so it will consume of the
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * small page window employeed by that function. Be careful.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @remark There is no need to assert on the result.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsyncDECLINLINE(void) PGMPOOL_UNLOCK_PTR(PVM pVM, void *pvPage)
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync# define PGMPOOL_UNLOCK_PTR(pVM, pPage) do {} while (0)
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Determin the size of a write instruction.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @returns number of bytes written.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pDis The disassembler state.
b4d7b4dbcc45b8bde7502aa129440d92d7ffd038vboxsyncstatic unsigned pgmPoolDisasWriteSize(PDISCPUSTATE pDis)
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * This is very crude and possibly wrong for some opcodes,
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * but since it's not really supposed to be called we can
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * probably live with that.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Flushes a chain of pages sharing the same access monitor.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @returns VBox status code suitable for scheduling.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pPool The pool.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pPage A page in the chain.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsyncint pgmPoolMonitorChainFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync LogFlow(("pgmPoolMonitorChainFlush: Flush page %RGp type=%d\n", pPage->GCPhys, pPage->enmKind));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Find the list head.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Iterate the list flushing each shadow page.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Wrapper for getting the current context pointer to the entry being modified.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @returns VBox status code suitable for scheduling.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @param pVM VM Handle.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pvDst Destination address
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pvSrc Source guest virtual address.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param GCPhysSrc The source guest physical address.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param cb Size of data to read
11b175175a0ed424b8e8354acda681ad0adde0f8vboxsyncDECLINLINE(int) pgmPoolPhysSimpleReadGCPhys(PVM pVM, void *pvDst, CTXTYPE(RTGCPTR, RTHCPTR, RTGCPTR) pvSrc, RTGCPHYS GCPhysSrc, size_t cb)
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync memcpy(pvDst, (RTHCPTR)((uintptr_t)pvSrc & ~(RTHCUINTPTR)(cb - 1)), cb);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* @todo in RC we could attempt to use the virtual address, although this can cause many faults (PAE Windows XP guest). */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync return PGMPhysSimpleReadGCPhys(pVM, pvDst, GCPhysSrc & ~(RTGCPHYS)(cb - 1), cb);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Process shadow entries before they are changed by the guest.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * For PT entries we will clear them. For PD entries, we'll simply check
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * for mapping conflicts and set the SyncCR3 FF if found.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pVCpu VMCPU handle
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pPool The pool.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pPage The head page.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param GCPhysFault The guest physical fault address.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param uAddress In R0 and GC this is the guest context fault address (flat).
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * In R3 this is the host context 'fault' address.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pCpu The disassembler state for figuring out the write size.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * This need not be specified if the caller knows we won't do cross entry accesses.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsyncvoid pgmPoolMonitorChainChanging(PVMCPU pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault, CTXTYPE(RTGCPTR, RTHCPTR, RTGCPTR) pvAddress, PDISCPUSTATE pCpu)
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync const unsigned off = GCPhysFault & PAGE_OFFSET_MASK;
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync const unsigned cbWrite = (pCpu) ? pgmPoolDisasWriteSize(pCpu) : 0;
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("pgmPoolMonitorChainChanging: %RGv phys=%RGp kind=%s cbWrite=%d\n", (RTGCPTR)pvAddress, GCPhysFault, pgmPoolPoolKindToStr(pPage->enmKind), cbWrite));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT iShw=%x\n", iShw));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress, GCPhysFault, sizeof(GstPte));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync 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));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* page/2 sized */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync const unsigned iShw = (off / sizeof(X86PTE)) & (X86_PG_PAE_ENTRIES - 1);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("PGMPOOLKIND_PAE_PT_FOR_32BIT_PT iShw=%x\n", iShw));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress, GCPhysFault, sizeof(GstPte));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync 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));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync 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));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if (iShwPdpt == pPage->enmKind - (unsigned)PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync for (unsigned i = 0; i < 2; i++)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if ((uShw.pPDPae->a[iShw + i].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShwPdpt=%#x iShw=%#x!\n", iShwPdpt, iShw+i));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync# endif /* !IN_RING0 */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw+i, uShw.pPDPae->a[iShw+i].u));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync /* paranoia / a bit assumptive. */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync if ((uShw.pPDPae->a[iShw2].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShwPdpt=%#x iShw2=%#x!\n", iShwPdpt, iShw2));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync# endif /* !IN_RING0 */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress, GCPhysFault, sizeof(GstPte));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync Log4(("pgmPoolMonitorChainChanging pae: deref %016RX64 GCPhys %016RX64\n", uShw.pPTPae->a[iShw].u & X86_PTE_PAE_PG_MASK, GstPte.u & X86_PTE_PAE_PG_MASK));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* paranoia / a bit assumptive. */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PTEPAE);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, (RTHCPTR)((RTHCUINTPTR)pvAddress + sizeof(GstPte)), GCPhysFault + sizeof(GstPte), sizeof(GstPte));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress + sizeof(GstPte), GCPhysFault + sizeof(GstPte), sizeof(GstPte));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync Log4(("pgmPoolMonitorChainChanging pae: deref %016RX64 GCPhys %016RX64\n", uShw.pPTPae->a[iShw2].u & X86_PTE_PAE_PG_MASK, GstPte.u & X86_PTE_PAE_PG_MASK));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync const unsigned iShw = off / sizeof(X86PTE); // ASSUMING 32-bit guest paging!
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("pgmPoolMonitorChainChanging: PGMPOOLKIND_32BIT_PD %x\n", iShw));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync# endif /* !IN_RING0 */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync# endif /* !IN_RING0 */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("pgmPoolMonitorChainChanging: 32 bit pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPD->a[iShw].u));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync /* paranoia / a bit assumptive. */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PTE);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync# endif /* !IN_RING0 */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync# endif /* !IN_RING0 */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("pgmPoolMonitorChainChanging: 32 bit pd iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPD->a[iShw2].u));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync#if 0 /* useful when running PGMAssertCR3(), a bit too troublesome for general use (TLBs). */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync LogFlow(("pgmPoolMonitorChainChanging: iShw=%#x: %RX32 -> freeing it!\n", iShw, uShw.pPD->a[iShw].u));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync# ifdef IN_RC /* TLB load - we're pushing things a bit... */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pgmPoolFree(pVM, uShw.pPD->a[iShw].u & X86_PDE_PG_MASK, pPage->idx, iShw);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync#endif /* !IN_RING0 */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Causes trouble when the guest uses a PDE to refer to the whole page table level
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * structure. (Invalidate here; faults later on when it tries to change the page
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * table entries -> recheck; probably only applies to the RC case.)
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync# endif /* !IN_RING0 */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* paranoia / a bit assumptive. */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDEPAE);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync#endif /* !IN_RING0 */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync# endif /* !IN_RING0 */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Hopefully this doesn't happen very often:
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * - touching unused parts of the page
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * - messing with the bits of pd pointers without changing the physical address
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* PDPT roots are not page aligned; 32 byte only! */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync const unsigned offPdpt = GCPhysFault - pPage->GCPhys;
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync if (iShw < X86_PG_PAE_PDPE_ENTRIES) /* don't use RT_ELEMENTS(uShw.pPDPT->a), because that's for long mode only */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected pdpt conflict at iShw=%#x!\n", iShw));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync# endif /* !IN_RING0 */
c99b597540585068d22dde4c9f74730305f24097vboxsync# endif /* !IN_RING0 */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPT->a[iShw].u));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* paranoia / a bit assumptive. */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync const unsigned iShw2 = (offPdpt + cbWrite - 1) / sizeof(X86PDPE);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync# endif /* !IN_RING0 */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync# endif /* !IN_RING0 */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPT->a[iShw2].u));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync Assert(!(uShw.pPDPae->a[iShw].u & PGM_PDFLAGS_MAPPING));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync /* paranoia / a bit assumptive. */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDEPAE);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync Assert(!(uShw.pPDPae->a[iShw2].u & PGM_PDFLAGS_MAPPING));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
510567648d46488f4166e5f69ffffe3eeeeec4d9vboxsync * Hopefully this doesn't happen very often:
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * - messing with the bits of pd pointers without changing the physical address
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPT->a[iShw].u));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pgmPoolFree(pVM, uShw.pPDPT->a[iShw].u & X86_PDPE_PG_MASK, pPage->idx, iShw);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync /* paranoia / a bit assumptive. */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDPE);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPT->a[iShw2].u));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync pgmPoolFree(pVM, uShw.pPDPT->a[iShw2].u & X86_PDPE_PG_MASK, pPage->idx, iShw2);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Hopefully this doesn't happen very often:
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * - messing with the bits of pd pointers without changing the physical address
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPML4->a[iShw].u));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync pgmPoolFree(pVM, uShw.pPML4->a[iShw].u & X86_PML4E_PG_MASK, pPage->idx, iShw);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* paranoia / a bit assumptive. */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PML4E);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPML4->a[iShw2].u));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync pgmPoolFree(pVM, uShw.pPML4->a[iShw2].u & X86_PML4E_PG_MASK, pPage->idx, iShw2);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync#endif /* IN_RING0 */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync AssertFatalMsgFailed(("enmKind=%d\n", pPage->enmKind));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Checks if a access could be a fork operation in progress.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Meaning, that the guest is setuping up the parent process for Copy-On-Write.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @returns true if it's likly that we're forking, otherwise false.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pPool The pool.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pCpu The disassembled instruction.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param offFault The access offset.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsyncDECLINLINE(bool) pgmPoolMonitorIsForking(PPGMPOOL pPool, PDISCPUSTATE pCpu, unsigned offFault)
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * i386 linux is using btr to clear X86_PTE_RW.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * The functions involved are (2.6.16 source inspection):
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * clear_bit
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * ptep_set_wrprotect
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * copy_one_pte
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * copy_pte_range
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * copy_pmd_range
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * copy_pud_range
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * copy_page_range
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * copy_process
cd5df721f068659172f3bf95de8fedeb465f057dvboxsync /** @todo Validate that the bit index is X86_PTE_RW. */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,Fork));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync return true;
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync return false;
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Determine whether the page is likely to have been reused.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @returns true if we consider the page as being reused for a different purpose.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @returns false if we consider it to still be a paging page.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pVM VM Handle.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pPage The page in question.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pRegFrame Trap register frame.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pCpu The disassembly info for the faulting instruction.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pvFault The fault address.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @remark The REP prefix check is left to the caller because of STOSD/W.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsyncDECLINLINE(bool) pgmPoolMonitorIsReused(PVM pVM, PPGMPOOLPAGE pPage, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu, RTGCPTR pvFault)
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /** @todo could make this general, faulting close to rsp should be safe reuse heuristic. */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* Fault caused by stack writes while trying to inject an interrupt event. */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync Log(("pgmPoolMonitorIsReused: reused %RGv for interrupt stack (rsp=%RGv).\n", pvFault, pRegFrame->rsp));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync return true;
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* call implies the actual push of the return address faulted */
2e4df4fd9eace3c61be68bb5eb12a93f9a79334dvboxsync return true;
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync return true;
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync return true;
2e4df4fd9eace3c61be68bb5eb12a93f9a79334dvboxsync return true;
2e4df4fd9eace3c61be68bb5eb12a93f9a79334dvboxsync return true;
2e4df4fd9eace3c61be68bb5eb12a93f9a79334dvboxsync return true;
2e4df4fd9eace3c61be68bb5eb12a93f9a79334dvboxsync case OP_MOVNTDQ: /* solaris - hwblkclr & hwblkpagecopy */
2e4df4fd9eace3c61be68bb5eb12a93f9a79334dvboxsync return true;
2e4df4fd9eace3c61be68bb5eb12a93f9a79334dvboxsync return true;
2e4df4fd9eace3c61be68bb5eb12a93f9a79334dvboxsync return false;
2e4df4fd9eace3c61be68bb5eb12a93f9a79334dvboxsync return true;
2e4df4fd9eace3c61be68bb5eb12a93f9a79334dvboxsync //if (pPage->fCR3Mix)
2e4df4fd9eace3c61be68bb5eb12a93f9a79334dvboxsync // return false;
2e4df4fd9eace3c61be68bb5eb12a93f9a79334dvboxsync return false;
2e4df4fd9eace3c61be68bb5eb12a93f9a79334dvboxsync * Flushes the page being accessed.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @returns VBox status code suitable for scheduling.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pVM The VM handle.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pVCpu The VMCPU handle.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pPool The pool.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pPage The pool page (head).
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pCpu The disassembly of the write instruction.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pRegFrame The trap register frame.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param GCPhysFault The fault address as guest physical address.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pvFault The fault address.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsyncstatic int pgmPoolAccessHandlerFlush(PVM pVM, PVMCPU pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * First, do the flushing.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Emulate the instruction (xp/w2k problem, requires pc/cr2/sp detection).
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync int rc2 = EMInterpretInstructionCPU(pVM, pVCpu, pCpu, pRegFrame, pvFault, &cbWritten);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (PATMIsPatchGCAddr(pVM, (RTRCPTR)pRegFrame->eip))
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for patch code %04x:%RGv, ignoring.\n",
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pPool->StatMonitorRZIntrFailPatch2);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,EmulateInstr));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* See use in pgmPoolAccessHandlerSimple(). */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("pgmPoolAccessHandlerPT: returns %Rrc (flushed)\n", rc));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Handles the STOSD write accesses.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @returns VBox status code suitable for scheduling.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pVM The VM handle.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pPool The pool.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pPage The pool page (head).
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pCpu The disassembly of the write instruction.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pRegFrame The trap register frame.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param GCPhysFault The fault address as guest physical address.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pvFault The fault address.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsyncDECLINLINE(int) pgmPoolAccessHandlerSTOSD(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
ffb50166c9adb4ae583b914d405197035cf890advboxsync * Increment the modification counter and insert it into the list
ffb50166c9adb4ae583b914d405197035cf890advboxsync * of modified pages the first time.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Execute REP STOSD.
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync * This ASSUMES that we're not invoked by Trap0e on in a out-of-sync
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync * write situation, meaning that it's safe to write here.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, (RTGCPTR)pu32, NULL);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, (RTGCPTR)pu32, NULL);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync PGMPhysSimpleWriteGCPhys(pVM, GCPhysFault, &pRegFrame->eax, 4);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* See use in pgmPoolAccessHandlerSimple(). */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * Handles the simple write accesses.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @returns VBox status code suitable for scheduling.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pVM The VM handle.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pVCpu The VMCPU handle.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pPool The pool.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pPage The pool page (head).
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pCpu The disassembly of the write instruction.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pRegFrame The trap register frame.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param GCPhysFault The fault address as guest physical address.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * @param pvFault The fault address.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsyncDECLINLINE(int) pgmPoolAccessHandlerSimple(PVM pVM, PVMCPU pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * Increment the modification counter and insert it into the list
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * of modified pages the first time.
d3b1b01528fe21777281edf167f8deca06f86e39vboxsync * Clear all the pages. ASSUMES that pvFault is readable.
d3b1b01528fe21777281edf167f8deca06f86e39vboxsync uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
d3b1b01528fe21777281edf167f8deca06f86e39vboxsync pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, pvFault, pCpu);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, pvFault, pCpu);
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * Interpret the instruction.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync int rc = EMInterpretInstructionCPU(pVM, pVCpu, pCpu, pRegFrame, pvFault, &cb);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for %04x:%RGv - opcode=%d\n",
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync pRegFrame->cs, (RTGCPTR)pRegFrame->rip, pCpu->pCurInstr->opcode));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,EmulateInstr));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Quick hack, with logging enabled we're getting stale
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * code TLBs but no data TLB for EIP and crash in EMInterpretDisasOne.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Flushing here is BAD and expensive, I think EMInterpretDisasOne will
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * have to be fixed to support this. But that'll have to wait till next week.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * An alternative is to keep track of the changed PTEs together with the
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * GCPhys from the guest PT. This may proove expensive though.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * At the moment, it's VITAL that it's done AFTER the instruction interpreting
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * because we need the stale TLBs in some cases (XP boot). This MUST be fixed properly!
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync LogFlow(("pgmPoolAccessHandlerSimple: returns %Rrc cb=%d\n", rc, cb));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * \#PF Handler callback for PT write accesses.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @returns VBox status code (appropriate for GC return).
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @param pVM VM Handle.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @param uErrorCode CPU Error code.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @param pRegFrame Trap register frame.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * NULL on DMA and other non CPU access.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @param pvFault The fault address (cr2).
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param GCPhysFault The GC physical address corresponding to pvFault.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @param pvUser User argument.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsyncDECLEXPORT(int) pgmPoolAccessHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser)
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync STAM_PROFILE_START(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), a);
0dd3967035b8a02985920baa57f948dc542b9388vboxsync LogFlow(("pgmPoolAccessHandler: pvFault=%RGv pPage=%p:{.idx=%d} GCPhysFault=%RGp\n", pvFault, pPage, pPage->idx, GCPhysFault));
0dd3967035b8a02985920baa57f948dc542b9388vboxsync * We should ALWAYS have the list head as user parameter. This
0dd3967035b8a02985920baa57f948dc542b9388vboxsync * is because we use that page to record the changes.
6475559a7e0e52892efbab4fbdedc879f6866109vboxsync * Disassemble the faulting instruction.
0dd3967035b8a02985920baa57f948dc542b9388vboxsync int rc = EMInterpretDisasOne(pVM, pVCpu, pRegFrame, &Cpu, NULL);
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * Check if it's worth dealing with.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync bool fReused = false;
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync if ( ( pPage->cModifications < 48 /** @todo #define */ /** @todo need to check that it's not mapping EIP. */ /** @todo adjust this! */
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync && !(fReused = pgmPoolMonitorIsReused(pVM, pPage, pRegFrame, &Cpu, pvFault))
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync && !pgmPoolMonitorIsForking(pPool, &Cpu, GCPhysFault & PAGE_OFFSET_MASK))
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * Simple instructions, no REP prefix.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync rc = pgmPoolAccessHandlerSimple(pVM, pVCpu, pPool, pPage, &Cpu, pRegFrame, GCPhysFault, pvFault);
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,Handled), a);
0dd3967035b8a02985920baa57f948dc542b9388vboxsync * Windows is frequently doing small memset() operations (netio test 4k+).
0dd3967035b8a02985920baa57f948dc542b9388vboxsync * We have to deal with these or we'll kill the cache and performance.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync && pRegFrame->ecx * 4 <= PAGE_SIZE - ((uintptr_t)pvFault & PAGE_OFFSET_MASK)
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync && (pRegFrame->eax == 0 || pRegFrame->eax == 0x80) /* the two values observed. */
f75c6db919d277952ca03b7acf643e5e3ac96cafvboxsync rc = pgmPoolAccessHandlerSTOSD(pVM, pPool, pPage, &Cpu, pRegFrame, GCPhysFault, pvFault);
f75c6db919d277952ca03b7acf643e5e3ac96cafvboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,RepStosd), a);
f75c6db919d277952ca03b7acf643e5e3ac96cafvboxsync /* REP prefix, don't bother. */
f75c6db919d277952ca03b7acf643e5e3ac96cafvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,RepPrefix));
f75c6db919d277952ca03b7acf643e5e3ac96cafvboxsync Log4(("pgmPoolAccessHandler: eax=%#x ecx=%#x edi=%#x esi=%#x rip=%RGv opcode=%d prefix=%#x\n",
f75c6db919d277952ca03b7acf643e5e3ac96cafvboxsync pRegFrame->eax, pRegFrame->ecx, pRegFrame->edi, pRegFrame->esi, (RTGCPTR)pRegFrame->rip, Cpu.pCurInstr->opcode, Cpu.prefix));
f75c6db919d277952ca03b7acf643e5e3ac96cafvboxsync * Not worth it, so flush it.
f75c6db919d277952ca03b7acf643e5e3ac96cafvboxsync * If we considered it to be reused, don't go back to ring-3
805a319b88bdf29b369da48402c58897a5e8b65dvboxsync * to emulate failed instructions since we usually cannot
805a319b88bdf29b369da48402c58897a5e8b65dvboxsync * interpret then. This may be a bit risky, in which case
0c80e8c5ac4249337af378ff41c60033c9fff59fvboxsync * the reuse detection must be fixed.
805a319b88bdf29b369da48402c58897a5e8b65dvboxsync rc = pgmPoolAccessHandlerFlush(pVM, pVCpu, pPool, pPage, &Cpu, pRegFrame, GCPhysFault, pvFault);
805a319b88bdf29b369da48402c58897a5e8b65dvboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,FlushPage), a);
805a319b88bdf29b369da48402c58897a5e8b65dvboxsync# endif /* !IN_RING3 */
805a319b88bdf29b369da48402c58897a5e8b65dvboxsync#endif /* PGMPOOL_WITH_MONITORING */
805a319b88bdf29b369da48402c58897a5e8b65dvboxsync * Inserts a page into the GCPhys hash table.
805a319b88bdf29b369da48402c58897a5e8b65dvboxsync * @param pPool The pool.
805a319b88bdf29b369da48402c58897a5e8b65dvboxsync * @param pPage The page.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsyncDECLINLINE(void) pgmPoolHashInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync Log3(("pgmPoolHashInsert: %RGp\n", pPage->GCPhys));
3c6306a66deef467e3c13483dd6529e1e1c6b822vboxsync Assert(pPage->GCPhys != NIL_RTGCPHYS); Assert(pPage->iNext == NIL_PGMPOOL_IDX);
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * Removes a page from the GCPhys hash table.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @param pPool The pool.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @param pPage The page.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsyncDECLINLINE(void) pgmPoolHashRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync Log3(("pgmPoolHashRemove: %RGp\n", pPage->GCPhys));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync AssertReleaseMsgFailed(("GCPhys=%RGp idx=%#x\n", pPage->GCPhys, pPage->idx));
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * Frees up one cache page.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @returns VBox status code.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @retval VINF_SUCCESS on success.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @param pPool The pool.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @param iUser The user index.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsyncstatic int pgmPoolCacheFreeOne(PPGMPOOL pPool, uint16_t iUser)
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync Assert(pPool->iAgeHead != pPool->iAgeTail); /* We shouldn't be here if there < 2 cached entries! */
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * Select one page from the tail of the age list.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync/* This is the alternative to the SyncCR3 pgmPoolCacheUsed calls.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync if (pPool->aPages[iToFree].iUserHead != NIL_PGMPOOL_USER_INDEX)
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync uint16_t i = pPool->aPages[iToFree].iAgePrev;
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync for (unsigned j = 0; j < 10 && i != NIL_PGMPOOL_USER_INDEX; j++, i = pPool->aPages[i].iAgePrev)
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync if (pPool->aPages[iToFree].iUserHead == NIL_PGMPOOL_USER_INDEX)
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync iToFree = i;
ffb50166c9adb4ae583b914d405197035cf890advboxsync * Reject any attempts at flushing the currently active shadow CR3 mapping.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * Call pgmPoolCacheUsed to move the page to the head of the age list.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync if (!pgmPoolIsPageLocked(&pPool->CTX_SUFF(pVM)->pgm.s, pPage))
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync LogFlow(("pgmPoolCacheFreeOne: refuse CR3 mapping\n"));
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync AssertLogRelReturn(iLoop < 8192, VERR_INTERNAL_ERROR);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Found a usable page, flush it and return.
ffb50166c9adb4ae583b914d405197035cf890advboxsync * Checks if a kind mismatch is really a page being reused
ffb50166c9adb4ae583b914d405197035cf890advboxsync * or if it's just normal remappings.
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync * @returns true if reused and the cached page (enmKind1) should be flushed
ffb50166c9adb4ae583b914d405197035cf890advboxsync * @returns false if not reused.
ffb50166c9adb4ae583b914d405197035cf890advboxsync * @param enmKind1 The kind of the cached page.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @param enmKind2 The kind of the requested page.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsyncstatic bool pgmPoolCacheReusedByKind(PGMPOOLKIND enmKind1, PGMPOOLKIND enmKind2)
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * Never reuse them. There is no remapping in non-paging mode.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync case PGMPOOLKIND_PAE_PDPT_FOR_32BIT: /* never reuse them for other types */
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync return false;
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * It's perfectly fine to reuse these, except for PAE and non-paging stuff.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync return true;
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync return false;
b4d7b4dbcc45b8bde7502aa129440d92d7ffd038vboxsync * It's perfectly fine to reuse these, except for PAE and non-paging stuff.
ffb50166c9adb4ae583b914d405197035cf890advboxsync return true;
ffb50166c9adb4ae583b914d405197035cf890advboxsync return false;
3c6306a66deef467e3c13483dd6529e1e1c6b822vboxsync * These cannot be flushed, and it's common to reuse the PDs as PTs.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync return false;
ffb50166c9adb4ae583b914d405197035cf890advboxsync * Attempts to satisfy a pgmPoolAlloc request from the cache.
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync * @returns VBox status code.
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync * @retval VINF_PGM_CACHED_PAGE on success.
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync * @retval VERR_FILE_NOT_FOUND if not found.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @param pPool The pool.
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync * @param GCPhys The GC physical address of the page we're gonna shadow.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @param enmKind The kind of mapping.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @param iUser The shadow page pool index of the user table.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @param iUserTable The index into the user table (shadowed).
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @param ppPage Where to store the pointer to the page.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsyncstatic int pgmPoolCacheAlloc(PPGMPOOL pPool, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * Look up the GCPhys in the hash.
ffb50166c9adb4ae583b914d405197035cf890advboxsync Log3(("pgmPoolCacheAlloc: %RGp kind %s iUser=%x iUserTable=%x SLOT=%d\n", GCPhys, pgmPoolPoolKindToStr(enmKind), iUser, iUserTable, i));
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync Log4(("pgmPoolCacheAlloc: slot %d found page %RGp\n", i, pPage->GCPhys));
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync /* Put it at the start of the use list to make sure pgmPoolTrackAddUser
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * doesn't flush it in case there are no more free use records.
ffb50166c9adb4ae583b914d405197035cf890advboxsync int rc = pgmPoolTrackAddUser(pPool, pPage, iUser, iUserTable);
ffb50166c9adb4ae583b914d405197035cf890advboxsync * The kind is different. In some cases we should now flush the page
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync * as it has been reused, but in most cases this is normal remapping
ffb50166c9adb4ae583b914d405197035cf890advboxsync * of PDs as PT or big pages using the GCPhys field in a slightly
ffb50166c9adb4ae583b914d405197035cf890advboxsync * different way than the other kinds.
ffb50166c9adb4ae583b914d405197035cf890advboxsync if (pgmPoolCacheReusedByKind((PGMPOOLKIND)pPage->enmKind, enmKind))
ffb50166c9adb4ae583b914d405197035cf890advboxsync } while (i != NIL_PGMPOOL_IDX);
6475559a7e0e52892efbab4fbdedc879f6866109vboxsync Log3(("pgmPoolCacheAlloc: Missed GCPhys=%RGp enmKind=%s\n", GCPhys, pgmPoolPoolKindToStr(enmKind)));
6475559a7e0e52892efbab4fbdedc879f6866109vboxsync * Inserts a page into the cache.
6475559a7e0e52892efbab4fbdedc879f6866109vboxsync * @param pPool The pool.
6475559a7e0e52892efbab4fbdedc879f6866109vboxsync * @param pPage The cached page.
6475559a7e0e52892efbab4fbdedc879f6866109vboxsync * @param fCanBeCached Set if the page is fit for caching from the caller's point of view.
6475559a7e0e52892efbab4fbdedc879f6866109vboxsyncstatic void pgmPoolCacheInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, bool fCanBeCached)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Insert into the GCPhys hash if the page is fit for that.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync Log3(("pgmPoolCacheInsert: Caching %p:{.Core=%RHp, .idx=%d, .enmKind=%s, GCPhys=%RGp}\n",
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), pPage->GCPhys));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync Log3(("pgmPoolCacheInsert: Not caching %p:{.Core=%RHp, .idx=%d, .enmKind=%s, GCPhys=%RGp}\n",
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), pPage->GCPhys));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Insert at the head of the age list.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync pPool->aPages[pPool->iAgeHead].iAgePrev = pPage->idx;
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Flushes a cached page.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pPool The pool.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pPage The cached page.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsyncstatic void pgmPoolCacheFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync Log3(("pgmPoolCacheFlushPage: %RGp\n", pPage->GCPhys));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Remove the page from the hash.
#ifdef PGMPOOL_WITH_MONITORING
#ifdef PGMPOOL_WITH_CACHE
if (i == NIL_PGMPOOL_IDX)
return NULL;
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_PAE_PDPT:
return pPage;
case PGMPOOLKIND_ROOT_NESTED:
case PGMPOOLKIND_PAE_PD_PHYS:
} while (i != NIL_PGMPOOL_IDX);
return NULL;
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_PAE_PDPT:
case PGMPOOLKIND_ROOT_NESTED:
return VINF_SUCCESS;
case PGMPOOLKIND_PAE_PD_PHYS:
return VINF_SUCCESS;
#ifdef PGMPOOL_WITH_MIXED_PT_CR3
int rc;
if (pPageHead)
Assert(!(pVM->pgm.s.fGlobalSyncFlags & PGM_GLOBAL_SYNC_CLEAR_PGM_POOL) || VMCPU_FF_ISSET(VMMGetCpu(pVM), VMCPU_FF_PGM_SYNC_CR3));
return rc;
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_PAE_PDPT:
case PGMPOOLKIND_ROOT_NESTED:
case PGMPOOLKIND_PAE_PD_PHYS:
return VINF_SUCCESS;
#ifdef PGMPOOL_WITH_MIXED_PT_CR3
int rc;
AssertMsg(!(pVM->pgm.s.fGlobalSyncFlags & PGM_GLOBAL_SYNC_CLEAR_PGM_POOL) || VMCPU_FF_ISSET(VMMGetCpu(pVM), VMCPU_FF_PGM_SYNC_CR3),
return rc;
#ifdef VBOX_WITH_STATISTICS
Log3(("pgmPoolMonitorModifiedRemove: idx=%d cModifications=%d\n", pPage->idx, pPage->cModifications));
#ifdef IN_RING3
#ifdef PGMPOOL_WITH_USER_TRACKING
#ifdef PGMPOOL_WITH_USER_TRACKING
if (!--cLeft)
#ifndef DEBUG_michael
AssertMsg(cModifiedPages == pPool->cModifiedPages, ("%d != %d\n", cModifiedPages, pPool->cModifiedPages));
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
pRam;
while (iPage-- > 0)
for (unsigned i = 0; i < cMaxPhysExts; i++)
return VINF_SUCCESS;
return VINF_PGM_SYNC_CR3;
return VINF_SUCCESS;
#ifdef PGMPOOL_WITH_USER_TRACKING
#ifdef PGMPOOL_WITH_CACHE
return rc;
return VERR_PGM_POOL_FLUSHED;
DECLINLINE(int) pgmPoolTrackInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhys, uint16_t iUser, uint32_t iUserTable)
#ifdef VBOX_STRICT
AssertMsg(paUsers[i].iUser != iUser || paUsers[i].iUserTable != iUserTable, ("%x %x vs new %x %x\n", paUsers[i].iUser, paUsers[i].iUserTable, iUser, iUserTable));
} while (i != NIL_PGMPOOL_USER_INDEX);
if (i == NIL_PGMPOOL_USER_INDEX)
return rc;
# ifdef PGMPOOL_WITH_MIXED_PT_CR3
const bool fCanBeMonitored = true;
|| (GCPhys & X86_PTE_PAE_PG_MASK) != (pPool->CTX_SUFF(pVM)->pgm.s.GCPhysGstCR3Monitored & X86_PTE_PAE_PG_MASK)
# ifdef PGMPOOL_WITH_CACHE
if (fCanBeMonitored)
# ifdef PGMPOOL_WITH_MONITORING
return rc;
static int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
Log3(("pgmPoolTrackAddUser GCPhys = %RGp iUser %x iUserTable %x\n", pPage->GCPhys, iUser, iUserTable));
# ifdef VBOX_STRICT
* Check that the entry doesn't already exists. We only allow multiple users of top-level paging structures (SHW_POOL_ROOT_IDX).
AssertMsg(iUser != PGMPOOL_IDX_PD || iUser != PGMPOOL_IDX_PDPT || iUser != PGMPOOL_IDX_NESTED_ROOT || iUser != PGMPOOL_IDX_AMD64_CR3 ||
paUsers[i].iUser != iUser || paUsers[i].iUserTable != iUserTable, ("%x %x vs new %x %x\n", paUsers[i].iUser, paUsers[i].iUserTable, iUser, iUserTable));
} while (i != NIL_PGMPOOL_USER_INDEX);
if (i == NIL_PGMPOOL_USER_INDEX)
return rc;
# ifdef PGMPOOL_WITH_CACHE
return VINF_SUCCESS;
static void pgmPoolTrackFreeUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
if ( i != NIL_PGMPOOL_USER_INDEX
while (i != NIL_PGMPOOL_USER_INDEX)
iPrev = i;
switch (enmKind)
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_PAE_PDPT:
case PGMPOOLKIND_ROOT_NESTED:
case PGMPOOLKIND_PAE_PD_PHYS:
switch (enmKind)
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_PAE_PDPT:
case PGMPOOLKIND_ROOT_NESTED:
case PGMPOOLKIND_PAE_PD_PHYS:
AssertFailed();
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
static void pgmPoolTrackFlushGCPhysPTInt(PVM pVM, PCPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs)
LogFlow(("pgmPoolTrackFlushGCPhysPT: pPhysPage=%R[pgmpage] iShw=%d cRefs=%d\n", pPhysPage, iShw, cRefs));
pPT->a[i].u = 0;
cRefs--;
if (!cRefs)
#ifdef LOG_ENABLED
RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
pPT->a[i].u = 0;
AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
pPT->a[i].u = 0;
cRefs--;
if (!cRefs)
#ifdef LOG_ENABLED
RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
pPT->a[i].u = 0;
AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d u64=%RX64\n", cRefs, pPage->iFirstPresent, pPage->cPresent, u64));
pPT->a[i].u = 0;
cRefs--;
if (!cRefs)
#ifdef LOG_ENABLED
RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
pPT->a[i].u = 0;
AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
LogFlow(("pgmPoolTrackFlushGCPhysPT: pPhysPage=%R[pgmpage] iShw=%d cRefs=%d\n", pPhysPage, iShw, cRefs));
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
if (u16)
*pfFlushTLBs = true;
*pfFlushTLBs = true;
return rc;
return VINF_PGM_GCPHYS_ALIASED;
pPT->a[i].u = 0;
if (!--cPresent)
pPT->a[i].u = 0;
if (!--cPresent)
if (!--cLeft)
return VINF_SUCCESS;
LogFlow(("pgmPoolTrackClearPageUser: clear %x in %s (%RGp) (flushing %s)\n", iUserTable, pgmPoolPoolKindToStr(pUserPage->enmKind), pUserPage->Core.Key, pgmPoolPoolKindToStr(pPage->enmKind)));
#ifdef VBOX_STRICT
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_PAE_PDPT:
case PGMPOOLKIND_PAE_PD_PHYS:
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_ROOT_NESTED:
case PGMPOOLKIND_32BIT_PD:
#if defined(IN_RC)
/* In 32 bits PAE mode we *must* invalidate the TLB when changing a PDPT entry; the CPU fetches them only during cr3 load, so any
ASMReloadCR3();
case PGMPOOLKIND_PAE_PD_PHYS:
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_PAE_PDPT:
case PGMPOOLKIND_ROOT_NESTED:
AssertFatalMsgFailed(("enmKind=%d iUser=%#x iUserTable=%#x\n", pUserPage->enmKind, pUser->iUser, pUser->iUserTable));
while (i != NIL_PGMPOOL_USER_INDEX)
i = iNext;
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
return NULL;
return pPhysExt;
if (!--cMax)
if (!pNew)
LogFlow(("pgmPoolTrackPhysExtAddref: added new extent %d:{%d}->%d\n", iPhysExt, iShwPT, iPhysExtStart));
if (pPhysExt)
LogFlow(("pgmPoolTrackPhysExtAddref: new extent: %d:{%d, %d}\n", iPhysExt, PGMPOOL_TD_GET_IDX(u16), iShwPT));
return u16;
AssertFatalMsg(cRefs == PGMPOOL_TD_CREFS_PHYSEXT, ("cRefs=%d pPhysPage=%R[pgmpage] pPage=%p:{.idx=%d}\n", cRefs, pPhysPage, pPage, pPage->idx));
Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d lonely\n", pPhysPage, pPage->idx));
Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d head\n", pPhysPage, pPage->idx));
AssertFatalMsgFailed(("not-found! cRefs=%d pPhysPage=%R[pgmpage] pPage=%p:{.idx=%d}\n", cRefs, pPhysPage, pPage, pPage->idx));
static void pgmPoolTracDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhys)
while (pRam)
#ifdef LOG_ENABLED
static void pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint)
while (pRam)
while (pRam)
while (iPage-- > 0)
DECLINLINE(void) pgmPoolTrackDerefPT32Bit32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT, PCX86PT pGstPT)
pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
DECLINLINE(void) pgmPoolTrackDerefPTPae32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PT pGstPT)
pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
DECLINLINE(void) pgmPoolTrackDerefPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PTPAE pGstPT)
pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PAE_PG_MASK);
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PG_MASK);
if (pSubPage)
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PAE_PG_MASK);
if (pSubPage)
for (unsigned i = 0; i < X86_PG_PAE_PDPE_ENTRIES; i++)
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & X86_PDPE_PG_MASK);
if (pSubPage)
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & X86_PDPE_PG_MASK);
if (pSubPage)
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPML4->a[i].u & X86_PDPE_PG_MASK);
if (pSubPage)
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & EPT_PDE_PG_MASK);
if (pSubPage)
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & EPT_PDPTE_PG_MASK);
if (pSubPage)
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
void *pvGst;
void *pvGst;
void *pvGst;
case PGMPOOLKIND_PAE_PD_PHYS:
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_PAE_PDPT:
case PGMPOOLKIND_64BIT_PML4:
#ifdef IN_RING3
#ifdef PGMPOOL_WITH_MONITORING
#ifdef PGMPOOL_WITH_USER_TRACKING
#ifdef PGMPOOL_WITH_CACHE
#ifdef PGMPOOL_WITH_USER_TRACKING
for (unsigned i = 0; i < cMaxUsers; i++)
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
pRam;
while (iPage-- > 0)
for (unsigned i = 0; i < cMaxPhysExts; i++)
#ifdef PGMPOOL_WITH_MONITORING
#ifdef PGMPOOL_WITH_CACHE
#ifdef PGMPOOL_WITH_MONITORING
# ifdef PGMPOOL_WITH_CACHE
#ifdef PGMPOOL_WITH_USER_TRACKING
#ifdef PGMPOOL_WITH_CACHE
Log(("pgmPoolFlushPage: special root page, rejected. enmKind=%s idx=%d\n", pgmPoolPoolKindToStr(pPage->enmKind), pPage->idx));
return VINF_SUCCESS;
("Can't free the shadow CR3! (%RHp vs %RHp kind=%d\n", PGMGetHyperCR3(VMMGetCpu(pVM)), pPage->Core.Key, pPage->enmKind));
Log(("pgmPoolFlushPage: current active shadow CR3, rejected. enmKind=%s idx=%d\n", pgmPoolPoolKindToStr(pPage->enmKind), pPage->idx));
return VINF_SUCCESS;
#ifdef PGMPOOL_WITH_USER_TRACKING
#ifdef PGMPOOL_WITH_CACHE
#ifdef PGMPOOL_WITH_MONITORING
return rc;
#ifdef PGMPOOL_WITH_USER_TRACKING
#ifdef PGMPOOL_WITH_CACHE
#if defined(IN_RC)
/* Hack alert: we can't deal with jumps to ring 3 when called from MapCR3 and allocating pages for PAE PDs. */
#ifdef IN_RING3
return rc;
return VINF_SUCCESS;
#ifdef PGMPOOL_WITH_CACHE
return VERR_PGM_POOL_FLUSHED;
int pgmPoolAlloc(PVM pVM, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
LogFlow(("pgmPoolAlloc: GCPhys=%RGp enmKind=%s iUser=%#x iUserTable=%#x\n", GCPhys, pgmPoolPoolKindToStr(enmKind), iUser, iUserTable));
/** @todo CSAM/PGMPrefetchPage messes up here during CSAMR3CheckGates
* Assert(!(pVM->pgm.s.fGlobalSyncFlags & PGM_GLOBAL_SYNC_CLEAR_PGM_POOL)); */
#ifdef PGMPOOL_WITH_CACHE
LogFlow(("pgmPoolAlloc: cached returns %Rrc *ppPage=%p:{.Key=%RHp, .idx=%d}\n", rc2, *ppPage, (*ppPage)->Core.Key, (*ppPage)->idx));
return rc2;
return rc;
pPool->cUsedPages++; /* physical handler registration / pgmPoolTrackFlushGCPhysPTsSlow requirement. */
#ifdef PGMPOOL_WITH_MONITORING
#ifdef PGMPOOL_WITH_USER_TRACKING
return rc3;
#ifdef VBOX_WITH_STATISTICS
LogFlow(("pgmPoolAlloc: returns %Rrc *ppPage=%p:{.Key=%RHp, .idx=%d, .fCached=%RTbool, .fMonitored=%RTbool}\n",
return rc;
#ifdef IN_RING3
#ifdef LOG_ENABLED
switch(enmKind)
case PGMPOOLKIND_INVALID:
case PGMPOOLKIND_FREE:
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_PAE_PD_PHYS:
case PGMPOOLKIND_PAE_PDPT:
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_ROOT_NESTED: