PGMAllPool.cpp revision af269095d009ccbc2ee6ad27129512b0535562b3
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * PGM Shadow Page Pool.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * available from http://www.virtualbox.org. This file is free software;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License (GPL) as published by the Free Software
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * additional information or have any questions.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync/*******************************************************************************
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync* Header Files *
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync*******************************************************************************/
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync/*******************************************************************************
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync* Internal Functions *
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync*******************************************************************************/
223cf005b18af2c21352a70693ebaf0582f68ebcvboxsyncDECLINLINE(unsigned) pgmPoolTrackGetShadowEntrySize(PGMPOOLKIND enmKind);
223cf005b18af2c21352a70693ebaf0582f68ebcvboxsyncDECLINLINE(unsigned) pgmPoolTrackGetGuestEntrySize(PGMPOOLKIND enmKind);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic void pgmPoolTrackDeref(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable);
afed5ab737f4aacfae3fe73776f40e989190a7cavboxsyncstatic void pgmPoolMonitorModifiedRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncDECLEXPORT(int) pgmPoolAccessHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic const char *pgmPoolPoolKindToStr(uint8_t enmKind);
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync#if defined(VBOX_STRICT) && defined(PGMPOOL_WITH_OPTIMIZED_DIRTY_PT)
2daaccf68be3773aee600c5c3e48bcf5401418a6vboxsyncstatic void pgmPoolTrackCheckPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PTPAE pGstPT);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncint pgmPoolTrackFlushGCPhysPTsSlow(PVM pVM, PPGMPAGE pPhysPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncPPGMPOOLPHYSEXT pgmPoolTrackPhysExtAlloc(PVM pVM, uint16_t *piPhysExt);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncvoid pgmPoolTrackPhysExtFree(PVM pVM, uint16_t iPhysExt);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncvoid pgmPoolTrackPhysExtFreeList(PVM pVM, uint16_t iPhysExt);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Checks if the specified page pool kind is for a 4MB or 2MB guest page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns true if it's the shadow of a 4MB or 2MB guest page, otherwise false.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param enmKind The page kind.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncDECLINLINE(bool) pgmPoolIsBigPage(PGMPOOLKIND enmKind)
22e281e75ed636601178296c6daebda8f1d17c59vboxsync return true;
22e281e75ed636601178296c6daebda8f1d17c59vboxsync return false;
22e281e75ed636601178296c6daebda8f1d17c59vboxsync/** @def PGMPOOL_PAGE_2_LOCKED_PTR
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * Maps a pool page pool into the current context and lock it (RC only).
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * @returns VBox status code.
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * @param pVM The VM handle.
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * @param pPage The pool page.
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * @remark In RC this uses PGMGCDynMapHCPage(), so it will consume of the
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * small page window employeed by that function. Be careful.
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * @remark There is no need to assert on the result.
22e281e75ed636601178296c6daebda8f1d17c59vboxsyncDECLINLINE(void *) PGMPOOL_PAGE_2_LOCKED_PTR(PVM pVM, PPGMPOOLPAGE pPage)
22e281e75ed636601178296c6daebda8f1d17c59vboxsync void *pv = pgmPoolMapPageInlined(&pVM->pgm.s, pPage);
22e281e75ed636601178296c6daebda8f1d17c59vboxsync /* Make sure the dynamic mapping will not be reused. */
22e281e75ed636601178296c6daebda8f1d17c59vboxsync# define PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage) PGMPOOL_PAGE_2_PTR(pVM, pPage)
22e281e75ed636601178296c6daebda8f1d17c59vboxsync/** @def PGMPOOL_UNLOCK_PTR
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * Unlock a previously locked dynamic caching (RC only).
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * @returns VBox status code.
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * @param pVM The VM handle.
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * @param pPage The pool page.
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * @remark In RC this uses PGMGCDynMapHCPage(), so it will consume of the
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * small page window employeed by that function. Be careful.
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * @remark There is no need to assert on the result.
22e281e75ed636601178296c6daebda8f1d17c59vboxsyncDECLINLINE(void) PGMPOOL_UNLOCK_PTR(PVM pVM, void *pvPage)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync# define PGMPOOL_UNLOCK_PTR(pVM, pPage) do {} while (0)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Determin the size of a write instruction.
726fc44ad0bd65a178ad4c3ab46ebd6cd3208e99vboxsync * @returns number of bytes written.
726fc44ad0bd65a178ad4c3ab46ebd6cd3208e99vboxsync * @param pDis The disassembler state.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic unsigned pgmPoolDisasWriteSize(PDISCPUSTATE pDis)
7e960d3a0a8a3a84d7aba2cca45d72b1c31cc97bvboxsync * This is very crude and possibly wrong for some opcodes,
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * but since it's not really supposed to be called we can
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * probably live with that.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Flushes a chain of pages sharing the same access monitor.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code suitable for scheduling.
fdea543f71872a3ec3909536a4fce37ab7aa3a8bvboxsync * @param pPool The pool.
726fc44ad0bd65a178ad4c3ab46ebd6cd3208e99vboxsync * @param pPage A page in the chain.
726fc44ad0bd65a178ad4c3ab46ebd6cd3208e99vboxsyncint pgmPoolMonitorChainFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
726fc44ad0bd65a178ad4c3ab46ebd6cd3208e99vboxsync LogFlow(("pgmPoolMonitorChainFlush: Flush page %RGp type=%d\n", pPage->GCPhys, pPage->enmKind));
8f7bc6ad2b7bbcb4b3b96248cd2478e45f2e3b88vboxsync * Find the list head.
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync * Iterate the list flushing each shadow page.
726fc44ad0bd65a178ad4c3ab46ebd6cd3208e99vboxsync * Wrapper for getting the current context pointer to the entry being modified.
50df3da42ff6589b0ecc4f50f2288811bc370186vboxsync * @returns VBox status code suitable for scheduling.
726fc44ad0bd65a178ad4c3ab46ebd6cd3208e99vboxsync * @param pVM VM Handle.
50df3da42ff6589b0ecc4f50f2288811bc370186vboxsync * @param pvDst Destination address
26824086a3f6b36cd911058f1d9b4c0b944706fbvboxsync * @param pvSrc Source guest virtual address.
26824086a3f6b36cd911058f1d9b4c0b944706fbvboxsync * @param GCPhysSrc The source guest physical address.
26824086a3f6b36cd911058f1d9b4c0b944706fbvboxsync * @param cb Size of data to read
68ef804c4ec232c58e2c03c8fc6afe3765c5c0d1vboxsyncDECLINLINE(int) pgmPoolPhysSimpleReadGCPhys(PVM pVM, void *pvDst, CTXTYPE(RTGCPTR, RTHCPTR, RTGCPTR) pvSrc, RTGCPHYS GCPhysSrc, size_t cb)
50df3da42ff6589b0ecc4f50f2288811bc370186vboxsync memcpy(pvDst, (RTHCPTR)((uintptr_t)pvSrc & ~(RTHCUINTPTR)(cb - 1)), cb);
50df3da42ff6589b0ecc4f50f2288811bc370186vboxsync /* @todo in RC we could attempt to use the virtual address, although this can cause many faults (PAE Windows XP guest). */
26824086a3f6b36cd911058f1d9b4c0b944706fbvboxsync return PGMPhysSimpleReadGCPhys(pVM, pvDst, GCPhysSrc & ~(RTGCPHYS)(cb - 1), cb);
50df3da42ff6589b0ecc4f50f2288811bc370186vboxsync * Process shadow entries before they are changed by the guest.
7e960d3a0a8a3a84d7aba2cca45d72b1c31cc97bvboxsync * For PT entries we will clear them. For PD entries, we'll simply check
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * for mapping conflicts and set the SyncCR3 FF if found.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVCpu VMCPU handle
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The head page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param GCPhysFault The guest physical fault address.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param uAddress In R0 and GC this is the guest context fault address (flat).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * In R3 this is the host context 'fault' address.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pDis The disassembler state for figuring out the write size.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This need not be specified if the caller knows we won't do cross entry accesses.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncvoid pgmPoolMonitorChainChanging(PVMCPU pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault, CTXTYPE(RTGCPTR, RTHCPTR, RTGCPTR) pvAddress, PDISCPUSTATE pDis)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertMsg(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX, ("%#x (idx=%#x)\n", pPage->iMonitoredPrev, pPage->idx));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync const unsigned off = GCPhysFault & PAGE_OFFSET_MASK;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync const unsigned cbWrite = pDis ? pgmPoolDisasWriteSize(pDis) : 0;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolMonitorChainChanging: %RGv phys=%RGp cbWrite=%d\n", (RTGCPTR)pvAddress, GCPhysFault, cbWrite));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolMonitorChainChanging: page idx=%d phys=%RGp (next=%d) kind=%s\n", pPage->idx, pPage->GCPhys, pPage->iMonitoredNext, pgmPoolPoolKindToStr(pPage->enmKind), cbWrite));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPT));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT iShw=%x\n", iShw));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress, GCPhysFault, sizeof(GstPte));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync 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));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* page/2 sized */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPT));
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsync const unsigned iShw = (off / sizeof(X86PTE)) & (X86_PG_PAE_ENTRIES - 1);
223cf005b18af2c21352a70693ebaf0582f68ebcvboxsync LogFlow(("PGMPOOLKIND_PAE_PT_FOR_32BIT_PT iShw=%x\n", iShw));
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress, GCPhysFault, sizeof(GstPte));
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync 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));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync 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));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPD));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (iShwPdpt == pPage->enmKind - (unsigned)PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD)
e3058907e95856baada756f4bc59dafc1ce6c6b4vboxsync for (unsigned i = 0; i < 2; i++)
e08de24d4792d31b7f2aac29db5cb8840d940009vboxsync if ((uShw.pPDPae->a[iShw + i].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
e50404712a2b5234c42bdf9740bddab5729ba188vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShwPdpt=%#x iShw=%#x!\n", iShwPdpt, iShw+i));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync# endif /* !IN_RING0 */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw+i, uShw.pPDPae->a[iShw+i].u));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* paranoia / a bit assumptive. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if ((uShw.pPDPae->a[iShw2].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShwPdpt=%#x iShw2=%#x!\n", iShwPdpt, iShw2));
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync# endif /* !IN_RING0 */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPT));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress, GCPhysFault, sizeof(GstPte));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync 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));
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync /* paranoia / a bit assumptive. */
22e281e75ed636601178296c6daebda8f1d17c59vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PTEPAE);
1843553dbdf4e46417158b4c6348c503adf10740vboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, (RTHCPTR)((RTHCUINTPTR)pvAddress + sizeof(GstPte)), GCPhysFault + sizeof(GstPte), sizeof(GstPte));
22e281e75ed636601178296c6daebda8f1d17c59vboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress + sizeof(GstPte), GCPhysFault + sizeof(GstPte), sizeof(GstPte));
806d0b554daa555364af5f87bc96eccbe760db7avboxsync 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));
1843553dbdf4e46417158b4c6348c503adf10740vboxsync const unsigned iShw = off / sizeof(X86PTE); // ASSUMING 32-bit guest paging!
1843553dbdf4e46417158b4c6348c503adf10740vboxsync LogFlow(("pgmPoolMonitorChainChanging: PGMPOOLKIND_32BIT_PD %x\n", iShw));
1843553dbdf4e46417158b4c6348c503adf10740vboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPD));
1843553dbdf4e46417158b4c6348c503adf10740vboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
1843553dbdf4e46417158b4c6348c503adf10740vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
1843553dbdf4e46417158b4c6348c503adf10740vboxsync# endif /* !IN_RING0 */
1843553dbdf4e46417158b4c6348c503adf10740vboxsync# endif /* !IN_RING0 */
1843553dbdf4e46417158b4c6348c503adf10740vboxsync LogFlow(("pgmPoolMonitorChainChanging: 32 bit pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPD->a[iShw].u));
533ffcb943c4af2c5fe6385d816d0ba3eda9383bvboxsync /* paranoia / a bit assumptive. */
533ffcb943c4af2c5fe6385d816d0ba3eda9383bvboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PTE);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync# endif /* !IN_RING0 */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync# endif /* !IN_RING0 */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolMonitorChainChanging: 32 bit pd iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPD->a[iShw2].u));
2d53f6e472561965d363674e17f48d3bdffc24d3vboxsync#if 0 /* useful when running PGMAssertCR3(), a bit too troublesome for general use (TLBs). */
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync LogFlow(("pgmPoolMonitorChainChanging: iShw=%#x: %RX32 -> freeing it!\n", iShw, uShw.pPD->a[iShw].u));
22e281e75ed636601178296c6daebda8f1d17c59vboxsync# ifdef IN_RC /* TLB load - we're pushing things a bit... */
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync pgmPoolFree(pVM, uShw.pPD->a[iShw].u & X86_PDE_PG_MASK, pPage->idx, iShw);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPD));
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
8e972b677df5ee27b99211fc7e456a5aa50f3e68vboxsync#endif /* !IN_RING0 */
8e972b677df5ee27b99211fc7e456a5aa50f3e68vboxsync * Causes trouble when the guest uses a PDE to refer to the whole page table level
8e972b677df5ee27b99211fc7e456a5aa50f3e68vboxsync * structure. (Invalidate here; faults later on when it tries to change the page
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync * table entries -> recheck; probably only applies to the RC case.)
22e281e75ed636601178296c6daebda8f1d17c59vboxsync# endif /* !IN_RING0 */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
806d0b554daa555364af5f87bc96eccbe760db7avboxsync /* paranoia / a bit assumptive. */
806d0b554daa555364af5f87bc96eccbe760db7avboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDEPAE);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
e08de24d4792d31b7f2aac29db5cb8840d940009vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync#endif /* !IN_RING0 */
806d0b554daa555364af5f87bc96eccbe760db7avboxsync# endif /* !IN_RING0 */
22e281e75ed636601178296c6daebda8f1d17c59vboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
806d0b554daa555364af5f87bc96eccbe760db7avboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPDPT));
806d0b554daa555364af5f87bc96eccbe760db7avboxsync * Hopefully this doesn't happen very often:
806d0b554daa555364af5f87bc96eccbe760db7avboxsync * - touching unused parts of the page
806d0b554daa555364af5f87bc96eccbe760db7avboxsync * - messing with the bits of pd pointers without changing the physical address
806d0b554daa555364af5f87bc96eccbe760db7avboxsync /* PDPT roots are not page aligned; 32 byte only! */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync const unsigned offPdpt = GCPhysFault - pPage->GCPhys;
22e281e75ed636601178296c6daebda8f1d17c59vboxsync if (iShw < X86_PG_PAE_PDPE_ENTRIES) /* don't use RT_ELEMENTS(uShw.pPDPT->a), because that's for long mode only */
22e281e75ed636601178296c6daebda8f1d17c59vboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected pdpt conflict at iShw=%#x!\n", iShw));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync# endif /* !IN_RING0 */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync# endif /* !IN_RING0 */
65eb75d34c65a45c0945358715253950d63e440cvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPT->a[iShw].u));
d98e61ba075ed7d0b567a5d884bc85d643fe3de7vboxsync /* paranoia / a bit assumptive. */
d98e61ba075ed7d0b567a5d884bc85d643fe3de7vboxsync const unsigned iShw2 = (offPdpt + cbWrite - 1) / sizeof(X86PDPE);
d98e61ba075ed7d0b567a5d884bc85d643fe3de7vboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
22e281e75ed636601178296c6daebda8f1d17c59vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
d98e61ba075ed7d0b567a5d884bc85d643fe3de7vboxsync# endif /* !IN_RING0 */
d98e61ba075ed7d0b567a5d884bc85d643fe3de7vboxsync# endif /* !IN_RING0 */
da936e0446fb2b56b813d5d938f1dfc6e4bf8b13vboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPT->a[iShw2].u));
3dde2f85d4cf477621a3128887a2c08a8bca7c01vboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPD));
79b24ef0ab7cd4a03a3571b3954c52ab8b573137vboxsync Assert(!(uShw.pPDPae->a[iShw].u & PGM_PDFLAGS_MAPPING));
22e281e75ed636601178296c6daebda8f1d17c59vboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync /* paranoia / a bit assumptive. */
5e797edc29f96c8367de4fbf5874171c24a89ba7vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDEPAE);
5e797edc29f96c8367de4fbf5874171c24a89ba7vboxsync Assert(!(uShw.pPDPae->a[iShw2].u & PGM_PDFLAGS_MAPPING));
22e281e75ed636601178296c6daebda8f1d17c59vboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
03319aeaef07ef63a404237f2cb56199131f4a03vboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPDPT));
3dde2f85d4cf477621a3128887a2c08a8bca7c01vboxsync * Hopefully this doesn't happen very often:
3dde2f85d4cf477621a3128887a2c08a8bca7c01vboxsync * - messing with the bits of pd pointers without changing the physical address
79b24ef0ab7cd4a03a3571b3954c52ab8b573137vboxsync LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPT->a[iShw].u));
79b24ef0ab7cd4a03a3571b3954c52ab8b573137vboxsync pgmPoolFree(pVM, uShw.pPDPT->a[iShw].u & X86_PDPE_PG_MASK, pPage->idx, iShw);
22e281e75ed636601178296c6daebda8f1d17c59vboxsync /* paranoia / a bit assumptive. */
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDPE);
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPT->a[iShw2].u));
3dde2f85d4cf477621a3128887a2c08a8bca7c01vboxsync pgmPoolFree(pVM, uShw.pPDPT->a[iShw2].u & X86_PDPE_PG_MASK, pPage->idx, iShw2);
9a12d720d4db6c26d09600ddab781ad8df5e1826vboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPML4));
3dde2f85d4cf477621a3128887a2c08a8bca7c01vboxsync * Hopefully this doesn't happen very often:
3dde2f85d4cf477621a3128887a2c08a8bca7c01vboxsync * - messing with the bits of pd pointers without changing the physical address
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPML4->a[iShw].u));
f9147fe1eaa4e35287f8f39282c7f92f0d7de0b7vboxsync pgmPoolFree(pVM, uShw.pPML4->a[iShw].u & X86_PML4E_PG_MASK, pPage->idx, iShw);
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync /* paranoia / a bit assumptive. */
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PML4E);
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPML4->a[iShw2].u));
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync pgmPoolFree(pVM, uShw.pPML4->a[iShw2].u & X86_PML4E_PG_MASK, pPage->idx, iShw2);
f827fea1108b8f8a1a5f63318f6ec3cf4a9e7010vboxsync#endif /* IN_RING0 */
22e281e75ed636601178296c6daebda8f1d17c59vboxsync AssertFatalMsgFailed(("enmKind=%d\n", pPage->enmKind));
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * Checks if a access could be a fork operation in progress.
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync * Meaning, that the guest is setting up the parent process for Copy-On-Write.
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync * @returns true if it's likly that we're forking, otherwise false.
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync * @param pPool The pool.
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync * @param pDis The disassembled instruction.
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync * @param offFault The access offset.
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsyncDECLINLINE(bool) pgmPoolMonitorIsForking(PPGMPOOL pPool, PDISCPUSTATE pDis, unsigned offFault)
f9147fe1eaa4e35287f8f39282c7f92f0d7de0b7vboxsync * i386 linux is using btr to clear X86_PTE_RW.
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync * The functions involved are (2.6.16 source inspection):
f9147fe1eaa4e35287f8f39282c7f92f0d7de0b7vboxsync * clear_bit
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync * ptep_set_wrprotect
f9147fe1eaa4e35287f8f39282c7f92f0d7de0b7vboxsync * copy_one_pte
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync * copy_pte_range
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync * copy_pmd_range
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync * copy_pud_range
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * copy_page_range
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync * copy_process
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync /** @todo Validate that the bit index is X86_PTE_RW. */
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,Fork));
22e281e75ed636601178296c6daebda8f1d17c59vboxsync return true;
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync return false;
f9147fe1eaa4e35287f8f39282c7f92f0d7de0b7vboxsync * Determine whether the page is likely to have been reused.
f9147fe1eaa4e35287f8f39282c7f92f0d7de0b7vboxsync * @returns true if we consider the page as being reused for a different purpose.
f9147fe1eaa4e35287f8f39282c7f92f0d7de0b7vboxsync * @returns false if we consider it to still be a paging page.
f9147fe1eaa4e35287f8f39282c7f92f0d7de0b7vboxsync * @param pVM VM Handle.
f9147fe1eaa4e35287f8f39282c7f92f0d7de0b7vboxsync * @param pVCpu VMCPU Handle.
7e960d3a0a8a3a84d7aba2cca45d72b1c31cc97bvboxsync * @param pRegFrame Trap register frame.
65b61798a61dd4c32cce448db1dac70bba8d5cf5vboxsync * @param pDis The disassembly info for the faulting instruction.
65b61798a61dd4c32cce448db1dac70bba8d5cf5vboxsync * @param pvFault The fault address.
65b61798a61dd4c32cce448db1dac70bba8d5cf5vboxsync * @remark The REP prefix check is left to the caller because of STOSD/W.
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsyncDECLINLINE(bool) pgmPoolMonitorIsReused(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pDis, RTGCPTR pvFault)
22e281e75ed636601178296c6daebda8f1d17c59vboxsync /** @todo could make this general, faulting close to rsp should be a safe reuse heuristic. */
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync /* Fault caused by stack writes while trying to inject an interrupt event. */
65b61798a61dd4c32cce448db1dac70bba8d5cf5vboxsync Log(("pgmPoolMonitorIsReused: reused %RGv for interrupt stack (rsp=%RGv).\n", pvFault, pRegFrame->rsp));
65b61798a61dd4c32cce448db1dac70bba8d5cf5vboxsync return true;
22e281e75ed636601178296c6daebda8f1d17c59vboxsync 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));
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync /* Non-supervisor mode write means it's used for something else. */
65b61798a61dd4c32cce448db1dac70bba8d5cf5vboxsync return true;
8e8844a522f5d335f177a0313b03067d79cce201vboxsync /* call implies the actual push of the return address faulted */
65b61798a61dd4c32cce448db1dac70bba8d5cf5vboxsync return true;
65b61798a61dd4c32cce448db1dac70bba8d5cf5vboxsync return true;
46df4404c8dbbf3672e7aae8cd0b2770356e5b73vboxsync return true;
46df4404c8dbbf3672e7aae8cd0b2770356e5b73vboxsync return true;
22e281e75ed636601178296c6daebda8f1d17c59vboxsync return true;
22e281e75ed636601178296c6daebda8f1d17c59vboxsync return true;
3dde2f85d4cf477621a3128887a2c08a8bca7c01vboxsync case OP_MOVNTDQ: /* solaris - hwblkclr & hwblkpagecopy */
3dde2f85d4cf477621a3128887a2c08a8bca7c01vboxsync return true;
e08de24d4792d31b7f2aac29db5cb8840d940009vboxsync return true;
3dde2f85d4cf477621a3128887a2c08a8bca7c01vboxsync return false;
46df4404c8dbbf3672e7aae8cd0b2770356e5b73vboxsync return true;
5db1d52ffbcaa46c3d944c6c2d9c552306817d9avboxsync return false;
585f64d6f624f9e683321dabeb21b0eb2e6aa473vboxsync * Flushes the page being accessed.
35eac6afc99c1d18789ea5fe75379695aec478b9vboxsync * @returns VBox status code suitable for scheduling.
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * @param pVM The VM handle.
35eac6afc99c1d18789ea5fe75379695aec478b9vboxsync * @param pVCpu The VMCPU handle.
585f64d6f624f9e683321dabeb21b0eb2e6aa473vboxsync * @param pPool The pool.
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * @param pPage The pool page (head).
585f64d6f624f9e683321dabeb21b0eb2e6aa473vboxsync * @param pDis The disassembly of the write instruction.
585f64d6f624f9e683321dabeb21b0eb2e6aa473vboxsync * @param pRegFrame The trap register frame.
585f64d6f624f9e683321dabeb21b0eb2e6aa473vboxsync * @param GCPhysFault The fault address as guest physical address.
585f64d6f624f9e683321dabeb21b0eb2e6aa473vboxsync * @param pvFault The fault address.
585f64d6f624f9e683321dabeb21b0eb2e6aa473vboxsyncstatic int pgmPoolAccessHandlerFlush(PVM pVM, PVMCPU pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pDis,
585f64d6f624f9e683321dabeb21b0eb2e6aa473vboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
585f64d6f624f9e683321dabeb21b0eb2e6aa473vboxsync * First, do the flushing.
585f64d6f624f9e683321dabeb21b0eb2e6aa473vboxsync * Emulate the instruction (xp/w2k problem, requires pc/cr2/sp detection). Must do this in raw mode (!); XP boot will fail otherwise
585f64d6f624f9e683321dabeb21b0eb2e6aa473vboxsync int rc2 = EMInterpretInstructionCPU(pVM, pVCpu, pDis, pRegFrame, pvFault, &cbWritten);
65b61798a61dd4c32cce448db1dac70bba8d5cf5vboxsync if (PATMIsPatchGCAddr(pVM, (RTRCPTR)pRegFrame->eip))
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for patch code %04x:%RGv, ignoring.\n",
22e281e75ed636601178296c6daebda8f1d17c59vboxsync STAM_COUNTER_INC(&pPool->StatMonitorRZIntrFailPatch2);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,EmulateInstr));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* See use in pgmPoolAccessHandlerSimple(). */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolAccessHandlerPT: returns %Rrc (flushed)\n", rc));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Handles the STOSD write accesses.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code suitable for scheduling.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The pool page (head).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pDis The disassembly of the write instruction.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pRegFrame The trap register frame.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param GCPhysFault The fault address as guest physical address.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pvFault The fault address.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncDECLINLINE(int) pgmPoolAccessHandlerSTOSD(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pDis,
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(pDis->mode == CPUMODE_32BIT || pDis->mode == CPUMODE_64BIT);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Increment the modification counter and insert it into the list
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * of modified pages the first time.
e17bd6c32a8dd64f2d42838f9028216465e2caf0vboxsync * Execute REP STOSD.
e9a217d585085a6a6d129d27ca0d96a1b8e6d0eevboxsync * This ASSUMES that we're not invoked by Trap0e on in a out-of-sync
e17bd6c32a8dd64f2d42838f9028216465e2caf0vboxsync * write situation, meaning that it's safe to write here.
7e960d3a0a8a3a84d7aba2cca45d72b1c31cc97bvboxsync uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, (RTGCPTR)pu32, NULL);
e17bd6c32a8dd64f2d42838f9028216465e2caf0vboxsync pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, (RTGCPTR)pu32, NULL);
e17bd6c32a8dd64f2d42838f9028216465e2caf0vboxsync PGMPhysSimpleWriteGCPhys(pVM, GCPhysFault, &pRegFrame->rax, uIncrement);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* See use in pgmPoolAccessHandlerSimple(). */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Handles the simple write accesses.
b9ca93dd1ad44cb8b27679dc5624be2f7b7f7af5vboxsync * @returns VBox status code suitable for scheduling.
b9ca93dd1ad44cb8b27679dc5624be2f7b7f7af5vboxsync * @param pVM The VM handle.
b9ca93dd1ad44cb8b27679dc5624be2f7b7f7af5vboxsync * @param pVCpu The VMCPU handle.
b9ca93dd1ad44cb8b27679dc5624be2f7b7f7af5vboxsync * @param pPool The pool.
b9ca93dd1ad44cb8b27679dc5624be2f7b7f7af5vboxsync * @param pPage The pool page (head).
b9ca93dd1ad44cb8b27679dc5624be2f7b7f7af5vboxsync * @param pDis The disassembly of the write instruction.
7c3417bbf525c03163d54d151a277a981d5d61b6vboxsync * @param pRegFrame The trap register frame.
e9a217d585085a6a6d129d27ca0d96a1b8e6d0eevboxsync * @param GCPhysFault The fault address as guest physical address.
8b984478b755f4d3091b977d9beac9fb7434279fvboxsync * @param pvFault The fault address.
8b984478b755f4d3091b977d9beac9fb7434279fvboxsync * @param pfReused Reused state (out)
e9a217d585085a6a6d129d27ca0d96a1b8e6d0eevboxsyncDECLINLINE(int) pgmPoolAccessHandlerSimple(PVM pVM, PVMCPU pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pDis,
51d4024e2984de499ecd878341898f71f55cf9e0vboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault, bool *pfReused)
e9a217d585085a6a6d129d27ca0d96a1b8e6d0eevboxsync * Increment the modification counter and insert it into the list
e9a217d585085a6a6d129d27ca0d96a1b8e6d0eevboxsync * of modified pages the first time.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear all the pages. ASSUMES that pvFault is readable.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, pvFault, pDis);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, pvFault, pDis);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Interpret the instruction.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = EMInterpretInstructionCPU(pVM, pVCpu, pDis, pRegFrame, pvFault, &cb);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for %04x:%RGv - opcode=%d\n",
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pRegFrame->cs, (RTGCPTR)pRegFrame->rip, pDis->pCurInstr->opcode));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,EmulateInstr));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync#if 0 /* experimental code */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvFault, GCPhysFault, sizeof(GstPte));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Check the new value written by the guest. If present and with a bogus physical address, then
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * it's fairly safe to assume the guest is reusing the PT.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = PGMPhysGCPhys2HCPhys(pVM, GstPte.u & X86_PTE_PAE_PG_MASK, &HCPhys);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Quick hack, with logging enabled we're getting stale
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * code TLBs but no data TLB for EIP and crash in EMInterpretDisasOne.
b3547e42ce5221377866e3fa041b3086b7cc1562vboxsync * Flushing here is BAD and expensive, I think EMInterpretDisasOne will
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * have to be fixed to support this. But that'll have to wait till next week.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * An alternative is to keep track of the changed PTEs together with the
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * GCPhys from the guest PT. This may proove expensive though.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * At the moment, it's VITAL that it's done AFTER the instruction interpreting
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * because we need the stale TLBs in some cases (XP boot). This MUST be fixed properly!
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolAccessHandlerSimple: returns %Rrc cb=%d\n", rc, cb));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * \#PF Handler callback for PT write accesses.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code (appropriate for GC return).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM VM Handle.
e7081fdf0305eaa621e0dd6decf8b28c33febc58vboxsync * @param uErrorCode CPU Error code.
3123bb2477edc752585e4bbd8e4cfedaf87997d1vboxsync * @param pRegFrame Trap register frame.
247b55faa8d054157f2481e68caca36f4dc9542cvboxsync * NULL on DMA and other non CPU access.
247b55faa8d054157f2481e68caca36f4dc9542cvboxsync * @param pvFault The fault address (cr2).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param GCPhysFault The GC physical address corresponding to pvFault.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pvUser User argument.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncDECLEXPORT(int) pgmPoolAccessHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_START(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), a);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync bool fForcedFlush = false;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolAccessHandler: pvFault=%RGv pPage=%p:{.idx=%d} GCPhysFault=%RGp\n", pvFault, pPage, pPage->idx, GCPhysFault));
0975ae0a0fb615c945150c48e4a73187c1f4f84dvboxsync if (PHYS_PAGE_ADDRESS(GCPhysFault) != PHYS_PAGE_ADDRESS(pPage->GCPhys))
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Pool page changed while we were waiting for the lock; ignore. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync 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)));
0975ae0a0fb615c945150c48e4a73187c1f4f84dvboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,Handled), a);
78a205e3fc6719d59e8c561b3d287d3a4f879852vboxsync return VINF_SUCCESS; /* SMP guest case where we were blocking on the pgm lock while the same page was being marked dirty. */
78a205e3fc6719d59e8c561b3d287d3a4f879852vboxsync#if 0 /* test code defined(VBOX_STRICT) && defined(PGMPOOL_WITH_OPTIMIZED_DIRTY_PT) */
78a205e3fc6719d59e8c561b3d287d3a4f879852vboxsync if (pPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_PAE_PT)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync void *pvShw = PGMPOOL_PAGE_2_LOCKED_PTR(pPool->CTX_SUFF(pVM), pPage);
47b5427d1a541bcd269bc625c35b19d849071edfvboxsync int rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackCheckPTPaePae(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PTPAE)pvGst);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Disassemble the faulting instruction.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = EMInterpretDisasOne(pVM, pVCpu, pRegFrame, pDis, NULL);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertMsg(rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("Unexpected rc %d\n", rc));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * We should ALWAYS have the list head as user parameter. This
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * is because we use that page to record the changes.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Maximum nr of modifications depends on the page type. */
247b55faa8d054157f2481e68caca36f4dc9542cvboxsync if (pPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_PAE_PT)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Incremental page table updates should weight more than random ones.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * (Only applies when started from offset 0)
0975ae0a0fb615c945150c48e4a73187c1f4f84dvboxsync if ( pPage->pvLastAccessHandlerRip >= pRegFrame->rip - 0x40 /* observed loops in Windows 7 x64 */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync && pPage->pvLastAccessHandlerRip < pRegFrame->rip + 0x40
0975ae0a0fb615c945150c48e4a73187c1f4f84dvboxsync && pvFault == (pPage->pvLastAccessHandlerFault + pDis->param1.size)
0975ae0a0fb615c945150c48e4a73187c1f4f84dvboxsync && pVCpu->pgm.s.cPoolAccessHandler == (pPage->cLastAccessHandlerCount + 1))
0975ae0a0fb615c945150c48e4a73187c1f4f84dvboxsync 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)));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPage->cLastAccessHandlerCount = pVCpu->pgm.s.cPoolAccessHandler;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FlushReinit));
47b5427d1a541bcd269bc625c35b19d849071edfvboxsync Log(("Mod overflow %VGv cMods=%d (locked=%d type=%s)\n", pvFault, pPage->cModifications, pgmPoolIsPageLocked(&pVM->pgm.s, pPage), pgmPoolPoolKindToStr(pPage->enmKind)));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Check if it's worth dealing with.
5cf54b3ffeb7ee90685dcaec329ef71a729f5947vboxsync bool fReused = false;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if ( ( pPage->cModifications < cMaxModifications /** @todo #define */ /** @todo need to check that it's not mapping EIP. */ /** @todo adjust this! */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync && !(fReused = pgmPoolMonitorIsReused(pVM, pVCpu, pRegFrame, pDis, pvFault))
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync && !pgmPoolMonitorIsForking(pPool, pDis, GCPhysFault & PAGE_OFFSET_MASK))
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Simple instructions, no REP prefix.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = pgmPoolAccessHandlerSimple(pVM, pVCpu, pPool, pPage, pDis, pRegFrame, GCPhysFault, pvFault, &fReused);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* A mov instruction to change the first page table entry will be remembered so we can detect
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * full page table changes early on. This will reduce the amount of unnecessary traps we'll take.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPage->cLastAccessHandlerCount = pVCpu->pgm.s.cPoolAccessHandler;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Make sure we don't kick out a page too quickly. */
5b6e2c9a765c3c72295acc15791af8a700746956vboxsync /* ignore the 2nd write to this page table entry. */
50df3da42ff6589b0ecc4f50f2288811bc370186vboxsync pPage->cLastAccessHandlerCount = pVCpu->pgm.s.cPoolAccessHandler;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,Handled), a);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Windows is frequently doing small memset() operations (netio test 4k+).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * We have to deal with these or we'll kill the cache and performance.
230400db1dd754a1ab26f138a3fcb8a75f8c6a58vboxsync bool fValidStosd = false;
20a793635760c71752932fde640ba328cab84348vboxsync && pRegFrame->ecx * 4 <= PAGE_SIZE - ((uintptr_t)pvFault & PAGE_OFFSET_MASK)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync && (pRegFrame->eax == 0 || pRegFrame->eax == 0x80) /* the two values observed. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync && pRegFrame->rcx * 8 <= PAGE_SIZE - ((uintptr_t)pvFault & PAGE_OFFSET_MASK)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync && (pRegFrame->rax == 0 || pRegFrame->rax == 0x80) /* the two values observed. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = pgmPoolAccessHandlerSTOSD(pVM, pPool, pPage, pDis, pRegFrame, GCPhysFault, pvFault);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,RepStosd), a);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* REP prefix, don't bother. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,RepPrefix));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log4(("pgmPoolAccessHandler: eax=%#x ecx=%#x edi=%#x esi=%#x rip=%RGv opcode=%d prefix=%#x\n",
5b6e2c9a765c3c72295acc15791af8a700746956vboxsync pRegFrame->eax, pRegFrame->ecx, pRegFrame->edi, pRegFrame->esi, (RTGCPTR)pRegFrame->rip, pDis->pCurInstr->opcode, pDis->prefix));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync#if defined(PGMPOOL_WITH_OPTIMIZED_DIRTY_PT) && defined(IN_RING0)
5b6e2c9a765c3c72295acc15791af8a700746956vboxsync /* E.g. Windows 7 x64 initializes page tables and touches some pages in the table during the process. This
08bc90fc2848c80bf8270bedc883745b8398e186vboxsync * leads to pgm pool trashing and an excessive amount of write faults due to page monitoring.
0f5d1b2abd9e82c7ee46f1327287c44856604bcbvboxsync || ( !pgmPoolMonitorIsReused(pVM, pVCpu, pRegFrame, pDis, pvFault)
c20b837b2c912dd4dcaaa676e15acd2b230108f0vboxsync && !pgmPoolMonitorIsForking(pPool, pDis, GCPhysFault & PAGE_OFFSET_MASK))
0f5d1b2abd9e82c7ee46f1327287c44856604bcbvboxsync /* Flush any monitored duplicates as we will disable write protection. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Find the monitor head. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync while (pPageHead->iMonitoredPrev != NIL_PGMPOOL_IDX)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPageHead = &pPool->aPages[pPageHead->iMonitoredPrev];
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log(("Flush duplicate page idx=%d GCPhys=%RGp type=%s\n", pPageHead->idx, pPageHead->GCPhys, pgmPoolPoolKindToStr(pPageHead->enmKind)));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* The flushing above might fail for locked pages, so double check. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Temporarily allow write access to the page table again. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = PGMHandlerPhysicalPageTempOff(pVM, pPage->GCPhys, pPage->GCPhys);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = PGMShwModifyPage(pVCpu, pvFault, 1, X86_PTE_RW, ~(uint64_t)X86_PTE_RW);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* In the SMP case the page table might be removed while we wait for the PGM lock in the trap handler. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync ("PGMShwModifyPage -> GCPtr=%RGv rc=%d\n", pvFault, rc));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_STOP(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), a);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync#endif /* PGMPOOL_WITH_OPTIMIZED_DIRTY_PT */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FlushModOverflow));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Not worth it, so flush it.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * If we considered it to be reused, don't go back to ring-3
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * to emulate failed instructions since we usually cannot
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * interpret then. This may be a bit risky, in which case
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * the reuse detection must be fixed.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = pgmPoolAccessHandlerFlush(pVM, pVCpu, pPool, pPage, pDis, pRegFrame, GCPhysFault, pvFault);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Make sure that the current instruction still has shadow page backing, otherwise we'll end up in a loop. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (PGMShwGetPage(pVCpu, pRegFrame->rip, NULL, NULL) == VINF_SUCCESS)
7e960d3a0a8a3a84d7aba2cca45d72b1c31cc97bvboxsync rc = VINF_SUCCESS; /* safe to restart the instruction. */
b45d66c0e496e2fd861479202f3d43aad592bd14vboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,FlushPage), a);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync# endif /* !IN_RING3 */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Check references to guest physical memory in a PAE / PAE page table.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pShwPT The shadow page table (mapping of the page).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pGstPT The guest page table.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic void pgmPoolTrackCheckPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PTPAE pGstPT)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync unsigned cErrors = 0;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync unsigned LastPTE = ~0U; /* initialized to shut up gcc */
8b03ab0bcd5c238021bc8a43d887dd9d0870c0f5vboxsync RTHCPHYS LastHCPhys = NIL_RTHCPHYS; /* initialized to shut up gcc */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = 0; i < RT_MIN(RT_ELEMENTS(pShwPT->a), pPage->iFirstPresent); i++)
8b03ab0bcd5c238021bc8a43d887dd9d0870c0f5vboxsync AssertMsg(!pShwPT->a[i].n.u1Present, ("Unexpected PTE: idx=%d %RX64 (first=%d)\n", i, pShwPT->a[i].u, pPage->iFirstPresent));
8b03ab0bcd5c238021bc8a43d887dd9d0870c0f5vboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
7e2fce486e7e2e45eb61d78ee206ba42ed89e016vboxsync int rc = PGMPhysGCPhys2HCPhys(pPool->CTX_SUFF(pVM), pGstPT->a[i].u & X86_PTE_PAE_PG_MASK, &HCPhys);
7e2fce486e7e2e45eb61d78ee206ba42ed89e016vboxsync || (pShwPT->a[i].u & X86_PTE_PAE_PG_MASK) != HCPhys)
b851fba305c4c9efe256db2aae648b8879930885vboxsync Log(("rc=%d idx=%d guest %RX64 shw=%RX64 vs %RHp\n", rc, i, pGstPT->a[i].u, pShwPT->a[i].u, HCPhys));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = PGMPhysGCPhys2HCPhys(pPool->CTX_SUFF(pVM), pPage->GCPhys, &HCPhysPT);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned iPage = 0; iPage < pPool->cCurPages; iPage++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (pTempPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_PAE_PT)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PX86PTPAE pShwPT2 = (PX86PTPAE)PGMPOOL_PAGE_2_LOCKED_PTR(pPool->CTX_SUFF(pVM), pTempPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned j = 0; j < RT_ELEMENTS(pShwPT->a); j++)
223cf005b18af2c21352a70693ebaf0582f68ebcvboxsync && ((pShwPT2->a[j].u & X86_PTE_PAE_PG_MASK) == HCPhysPT))
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log(("GCPhys=%RGp idx=%d %RX64 vs %RX64\n", pTempPage->GCPhys, j, pShwPT->a[j].u, pShwPT2->a[j].u));
e85d76a7e5a047db3cdc8576ff5f412c7b73bbabvboxsync AssertMsg(!cErrors, ("cErrors=%d: last rc=%d idx=%d guest %RX64 shw=%RX64 vs %RHp\n", cErrors, LastRc, LastPTE, pGstPT->a[LastPTE].u, pShwPT->a[LastPTE].u, LastHCPhys));
9b45880674da6f82ca27cc28b0272de3dd3cc7dfvboxsync# endif /* VBOX_STRICT */
e85d76a7e5a047db3cdc8576ff5f412c7b73bbabvboxsync * Clear references to guest physical memory in a PAE / PAE page table.
e85d76a7e5a047db3cdc8576ff5f412c7b73bbabvboxsync * @returns nr of changed PTEs
b55721e255f55546462b62c2d09259bb7bf8ef90vboxsync * @param pPool The pool.
e85d76a7e5a047db3cdc8576ff5f412c7b73bbabvboxsync * @param pPage The page.
e85d76a7e5a047db3cdc8576ff5f412c7b73bbabvboxsync * @param pShwPT The shadow page table (mapping of the page).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pGstPT The guest page table.
e85d76a7e5a047db3cdc8576ff5f412c7b73bbabvboxsync * @param pOldGstPT The old cached guest page table.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param fAllowRemoval Bail out as soon as we encounter an invalid PTE
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pfFlush Flush reused page table (out)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncDECLINLINE(unsigned) pgmPoolTrackFlushPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PTPAE pGstPT, PCX86PTPAE pOldGstPT, bool fAllowRemoval, bool *pfFlush)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync unsigned cChanged = 0;
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync for (unsigned i = 0; i < RT_MIN(RT_ELEMENTS(pShwPT->a), pPage->iFirstPresent); i++)
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync AssertMsg(!pShwPT->a[i].n.u1Present, ("Unexpected PTE: idx=%d %RX64 (first=%d)\n", i, pShwPT->a[i].u, pPage->iFirstPresent));
0f59af434918ae6ade24245a5f963e4f2618b89evboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Check the new value written by the guest. If present and with a bogus physical address, then
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * it's fairly safe to assume the guest is reusing the PT.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (!PGMPhysIsGCPhysValid(pPool->CTX_SUFF(pVM), pGstPT->a[i].u & X86_PTE_PAE_PG_MASK))
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* If the old cached PTE is identical, then there's no need to flush the shadow copy. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if ((pGstPT->a[i].u & X86_PTE_PAE_PG_MASK) == (pOldGstPT->a[i].u & X86_PTE_PAE_PG_MASK))
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = PGMPhysGCPhys2HCPhys(pPool->CTX_SUFF(pVM), pGstPT->a[i].u & X86_PTE_PAE_PG_MASK, &HCPhys);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertMsg(rc == VINF_SUCCESS && (pShwPT->a[i].u & X86_PTE_PAE_PG_MASK) == HCPhys, ("rc=%d guest %RX64 old %RX64 shw=%RX64 vs %RHp\n", rc, pGstPT->a[i].u, pOldGstPT->a[i].u, pShwPT->a[i].u, HCPhys));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync uint64_t uHostAttr = pShwPT->a[i].u & (X86_PTE_P | X86_PTE_US | X86_PTE_A | X86_PTE_D | X86_PTE_G | X86_PTE_PAE_NX);
10e1bc06b2908a0af56d92ffdbadd25b36a5ef61vboxsync 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);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Something was changed, so flush it. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log4(("pgmPoolTrackDerefPTPaePae: i=%d pte=%RX64 hint=%RX64\n",
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pOldGstPT->a[i].u & X86_PTE_PAE_PG_MASK));
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pOldGstPT->a[i].u & X86_PTE_PAE_PG_MASK);
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * Flush a dirty page
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM VM Handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param idxSlot Dirty array slot index
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param fAllowRemoval Allow a reused page table to be removed
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic void pgmPoolFlushDirtyPage(PVM pVM, PPGMPOOL pPool, unsigned idxSlot, bool fAllowRemoval = false)
8f7bc6ad2b7bbcb4b3b96248cd2478e45f2e3b88vboxsync Assert(idxSlot < RT_ELEMENTS(pPool->aIdxDirtyPages));
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync if (pPool->aIdxDirtyPages[idxSlot] == NIL_PGMPOOL_IDX)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(pPage->iMonitoredNext == NIL_PGMPOOL_IDX && pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertMsg(pPage->fDirty, ("Page %RGp (slot=%d) not marked dirty!", pPage->GCPhys, idxSlot));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log(("Flush dirty page %RGp cMods=%d\n", pPage->GCPhys, pPage->cModifications));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* First write protect the page again to catch all write accesses. (before checking for changes -> SMP) */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = PGMHandlerPhysicalReset(pVM, pPage->GCPhys);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = PGMShwGetPage(VMMGetCpu(pVM), pPage->pvDirtyFault, &fFlags, &HCPhys);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync && (!(fFlags & X86_PTE_RW) || HCPhys != pPage->Core.Key))
afed5ab737f4aacfae3fe73776f40e989190a7cavboxsync /* In the SMP case the page table might be removed while we wait for the PGM lock in the trap handler. */
6b022885f2cb6a55167609edecd89570cd80001dvboxsync ("PGMShwGetPage -> GCPtr=%RGv rc=%d flags=%RX64\n", pPage->pvDirtyFault, rc, fFlags));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Flush those PTEs that have changed. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync void *pvShw = PGMPOOL_PAGE_2_LOCKED_PTR(pPool->CTX_SUFF(pVM), pPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync unsigned cChanges = pgmPoolTrackFlushPTPaePae(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PTPAE)pvGst, (PCX86PTPAE)&pPool->aDirtyPages[idxSlot][0], fAllowRemoval, &fFlush);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /** Note: we might want to consider keeping the dirty page active in case there were many changes. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* This page is likely to be modified again, so reduce the nr of modifications just a bit here. */
3a21cbe769e7500039a2d17c794a911f7acb2dadvboxsync pPage->cModifications = RT_MAX(1, pPage->cModifications / 2);
3a21cbe769e7500039a2d17c794a911f7acb2dadvboxsync if (pPool->cDirtyPages == RT_ELEMENTS(pPool->aIdxDirtyPages))
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(pPool->cDirtyPages <= RT_ELEMENTS(pPool->aIdxDirtyPages));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log(("Removed dirty page %RGp cMods=%d cChanges=%d\n", pPage->GCPhys, pPage->cModifications, cChanges));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Add a new dirty page
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM VM Handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncvoid pgmPoolAddDirtyPage(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertCompile(RT_ELEMENTS(pPool->aIdxDirtyPages) == 8 || RT_ELEMENTS(pPool->aIdxDirtyPages) == 16);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(idxFree < RT_ELEMENTS(pPool->aIdxDirtyPages));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(pPage->iMonitoredNext == NIL_PGMPOOL_IDX && pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (pPool->cDirtyPages >= RT_ELEMENTS(pPool->aIdxDirtyPages))
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_COUNTER_INC(&pPool->StatDirtyPageOverFlowFlush);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolFlushDirtyPage(pVM, pPool, idxFree, true /* allow removal of reused page tables*/);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(pPool->cDirtyPages < RT_ELEMENTS(pPool->aIdxDirtyPages));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertMsg(pPool->aIdxDirtyPages[idxFree] == NIL_PGMPOOL_IDX, ("idxFree=%d cDirtyPages=%d\n", idxFree, pPool->cDirtyPages));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log(("Add dirty page %RGp (slot=%d)\n", pPage->GCPhys, idxFree));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Make a copy of the guest page table as we require valid GCPhys addresses when removing
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * references to physical pages. (the HCPhys linear lookup is *extremely* expensive!)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync void *pvShw = PGMPOOL_PAGE_2_LOCKED_PTR(pPool->CTX_SUFF(pVM), pPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync memcpy(&pPool->aDirtyPages[idxFree][0], pvGst, PAGE_SIZE);
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync pgmPoolTrackCheckPTPaePae(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PTPAE)pvGst);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPool->idxFreeDirtyPage = (pPool->idxFreeDirtyPage + 1) & (RT_ELEMENTS(pPool->aIdxDirtyPages) - 1);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if ( pPool->cDirtyPages < RT_ELEMENTS(pPool->aIdxDirtyPages)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync && pPool->aIdxDirtyPages[pPool->idxFreeDirtyPage] != NIL_PGMPOOL_IDX)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync unsigned i;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (i = 1; i < RT_ELEMENTS(pPool->aIdxDirtyPages); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync idxFree = (pPool->idxFreeDirtyPage + i) & (RT_ELEMENTS(pPool->aIdxDirtyPages) - 1);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (pPool->aIdxDirtyPages[idxFree] == NIL_PGMPOOL_IDX)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(pPool->cDirtyPages == RT_ELEMENTS(pPool->aIdxDirtyPages) || pPool->aIdxDirtyPages[pPool->idxFreeDirtyPage] == NIL_PGMPOOL_IDX);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync# endif /* !IN_RING3 */
b1c3cdef473df2fbc621d5da81acc82dbfb8a11avboxsync * Check if the specified page is dirty (not write monitored)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @return dirty or not
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM VM Handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param GCPhys Guest physical address
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return false;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPool->aIdxDirtyPages); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return true;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return false;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Reset all dirty pages by reinstating page monitoring.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM VM Handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(pPool->cDirtyPages <= RT_ELEMENTS(pPool->aIdxDirtyPages));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPool->aIdxDirtyPages); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolFlushDirtyPage(pVM, pPool, i, true /* allow removal of reused page tables*/);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if ( pPool->cDirtyPages != RT_ELEMENTS(pPool->aIdxDirtyPages)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync && pPool->aIdxDirtyPages[pPool->idxFreeDirtyPage] != NIL_PGMPOOL_IDX)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync unsigned i;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (i = 1; i < RT_ELEMENTS(pPool->aIdxDirtyPages); i++)
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync AssertMsg(i != RT_ELEMENTS(pPool->aIdxDirtyPages), ("cDirtyPages %d", pPool->cDirtyPages));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(pPool->aIdxDirtyPages[pPool->idxFreeDirtyPage] == NIL_PGMPOOL_IDX || pPool->cDirtyPages == RT_ELEMENTS(pPool->aIdxDirtyPages));
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync * Reset all dirty pages by reinstating page monitoring.
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync * @param pVM VM Handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param GCPhysPT Physical address of the page table
8f7bc6ad2b7bbcb4b3b96248cd2478e45f2e3b88vboxsyncvoid pgmPoolInvalidateDirtyPage(PVM pVM, RTGCPHYS GCPhysPT)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(pPool->cDirtyPages <= RT_ELEMENTS(pPool->aIdxDirtyPages));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync unsigned idxDirtyPage = RT_ELEMENTS(pPool->aIdxDirtyPages);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPool->aIdxDirtyPages); i++)
57f41f24b7a290eba73c63449f44017d7917561dvboxsync if (idxDirtyPage != RT_ELEMENTS(pPool->aIdxDirtyPages))
57f41f24b7a290eba73c63449f44017d7917561dvboxsync pgmPoolFlushDirtyPage(pVM, pPool, idxDirtyPage, true /* allow removal of reused page tables*/);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if ( pPool->cDirtyPages != RT_ELEMENTS(pPool->aIdxDirtyPages)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync && pPool->aIdxDirtyPages[pPool->idxFreeDirtyPage] != NIL_PGMPOOL_IDX)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync unsigned i;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (i = 0; i < RT_ELEMENTS(pPool->aIdxDirtyPages); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertMsg(i != RT_ELEMENTS(pPool->aIdxDirtyPages), ("cDirtyPages %d", pPool->cDirtyPages));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync# endif /* PGMPOOL_WITH_OPTIMIZED_DIRTY_PT */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync#endif /* PGMPOOL_WITH_MONITORING */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Inserts a page into the GCPhys hash table.
b1c3cdef473df2fbc621d5da81acc82dbfb8a11avboxsync * @param pPool The pool.
c33db29e7b41467a35675031f5f5233839909083vboxsync * @param pPage The page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncDECLINLINE(void) pgmPoolHashInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log3(("pgmPoolHashInsert: %RGp\n", pPage->GCPhys));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(pPage->GCPhys != NIL_RTGCPHYS); Assert(pPage->iNext == NIL_PGMPOOL_IDX);
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync * Removes a page from the GCPhys hash table.
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync * @param pPool The pool.
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync * @param pPage The page.
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsyncDECLINLINE(void) pgmPoolHashRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync Log3(("pgmPoolHashRemove: %RGp\n", pPage->GCPhys));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertReleaseMsgFailed(("GCPhys=%RGp idx=%#x\n", pPage->GCPhys, pPage->idx));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Frees up one cache page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VINF_SUCCESS on success.
e16fc293d8bdb6174849b7b4c6f7d9ef5f03ea83vboxsync * @param pPool The pool.
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync * @param iUser The user index.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic int pgmPoolCacheFreeOne(PPGMPOOL pPool, uint16_t iUser)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(pPool->iAgeHead != pPool->iAgeTail); /* We shouldn't be here if there < 2 cached entries! */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Select one page from the tail of the age list.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync/* This is the alternative to the SyncCR3 pgmPoolCacheUsed calls.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (pPool->aPages[iToFree].iUserHead != NIL_PGMPOOL_USER_INDEX)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync uint16_t i = pPool->aPages[iToFree].iAgePrev;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned j = 0; j < 10 && i != NIL_PGMPOOL_USER_INDEX; j++, i = pPool->aPages[i].iAgePrev)
6b022885f2cb6a55167609edecd89570cd80001dvboxsync if (pPool->aPages[iToFree].iUserHead == NIL_PGMPOOL_USER_INDEX)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync iToFree = i;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Reject any attempts at flushing the currently active shadow CR3 mapping.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Call pgmPoolCacheUsed to move the page to the head of the age list.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (!pgmPoolIsPageLocked(&pPool->CTX_SUFF(pVM)->pgm.s, pPage))
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolCacheFreeOne: refuse CR3 mapping\n"));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertLogRelReturn(iLoop < 8192, VERR_INTERNAL_ERROR);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Found a usable page, flush it and return.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* This flush was initiated by us and not the guest, so explicitly flush the TLB. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Checks if a kind mismatch is really a page being reused
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * or if it's just normal remappings.
a27fbcbb29ffc2196c2ebd0f2dad92f40c7ec65dvboxsync * @returns true if reused and the cached page (enmKind1) should be flushed
a27fbcbb29ffc2196c2ebd0f2dad92f40c7ec65dvboxsync * @returns false if not reused.
10e1bc06b2908a0af56d92ffdbadd25b36a5ef61vboxsync * @param enmKind1 The kind of the cached page.
315f443a509c31db47b8f5cb94d26e54c3d5c497vboxsync * @param enmKind2 The kind of the requested page.
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsyncstatic bool pgmPoolCacheReusedByKind(PGMPOOLKIND enmKind1, PGMPOOLKIND enmKind2)
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync * Never reuse them. There is no remapping in non-paging mode.
9b45880674da6f82ca27cc28b0272de3dd3cc7dfvboxsync case PGMPOOLKIND_PAE_PDPT_FOR_32BIT: /* never reuse them for other types */
9b45880674da6f82ca27cc28b0272de3dd3cc7dfvboxsync return false;
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * It's perfectly fine to reuse these, except for PAE and non-paging stuff.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return true;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return false;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * It's perfectly fine to reuse these, except for PAE and non-paging stuff.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return true;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return false;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * These cannot be flushed, and it's common to reuse the PDs as PTs.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return false;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Attempts to satisfy a pgmPoolAlloc request from the cache.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VINF_PGM_CACHED_PAGE on success.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VERR_FILE_NOT_FOUND if not found.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param GCPhys The GC physical address of the page we're gonna shadow.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param enmKind The kind of mapping.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param enmAccess Access type for the mapping (only relevant for big pages)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iUser The shadow page pool index of the user table.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iUserTable The index into the user table (shadowed).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param ppPage Where to store the pointer to the page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic int pgmPoolCacheAlloc(PPGMPOOL pPool, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, PGMPOOLACCESS enmAccess, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Look up the GCPhys in the hash.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log3(("pgmPoolCacheAlloc: %RGp kind %s iUser=%x iUserTable=%x SLOT=%d\n", GCPhys, pgmPoolPoolKindToStr(enmKind), iUser, iUserTable, i));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log4(("pgmPoolCacheAlloc: slot %d found page %RGp\n", i, pPage->GCPhys));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Put it at the start of the use list to make sure pgmPoolTrackAddUser
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * doesn't flush it in case there are no more free use records.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = pgmPoolTrackAddUser(pPool, pPage, iUser, iUserTable);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPage->cModifications = 1; /* reset counter (can't use 0, or else it will be reinserted in the modified list) */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * The kind is different. In some cases we should now flush the page
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * as it has been reused, but in most cases this is normal remapping
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * of PDs as PT or big pages using the GCPhys field in a slightly
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * different way than the other kinds.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (pgmPoolCacheReusedByKind((PGMPOOLKIND)pPage->enmKind, enmKind))
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync } while (i != NIL_PGMPOOL_IDX);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log3(("pgmPoolCacheAlloc: Missed GCPhys=%RGp enmKind=%s\n", GCPhys, pgmPoolPoolKindToStr(enmKind)));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Inserts a page into the cache.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The cached page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param fCanBeCached Set if the page is fit for caching from the caller's point of view.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic void pgmPoolCacheInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, bool fCanBeCached)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Insert into the GCPhys hash if the page is fit for that.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log3(("pgmPoolCacheInsert: Caching %p:{.Core=%RHp, .idx=%d, .enmKind=%s, GCPhys=%RGp}\n",
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), pPage->GCPhys));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log3(("pgmPoolCacheInsert: Not caching %p:{.Core=%RHp, .idx=%d, .enmKind=%s, GCPhys=%RGp}\n",
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), pPage->GCPhys));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Insert at the head of the age list.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPool->aPages[pPool->iAgeHead].iAgePrev = pPage->idx;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Flushes a cached page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The cached page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic void pgmPoolCacheFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log3(("pgmPoolCacheFlushPage: %RGp\n", pPage->GCPhys));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Remove the page from the hash.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Remove it from the age list.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPool->aPages[pPage->iAgeNext].iAgePrev = pPage->iAgePrev;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPool->aPages[pPage->iAgePrev].iAgeNext = pPage->iAgeNext;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync#endif /* PGMPOOL_WITH_CACHE */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Looks for pages sharing the monitor.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns Pointer to the head page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns NULL if not found.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The Pool
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pNewPage The page which is going to be monitored.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic PPGMPOOLPAGE pgmPoolMonitorGetPageByGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pNewPage)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Look up the GCPhys in the hash.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync RTGCPHYS GCPhys = pNewPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* find the head */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* ignore, no monitoring. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertFatalMsgFailed(("enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync } while (i != NIL_PGMPOOL_IDX);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Enabled write monitoring of a guest page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VINF_SUCCESS on success.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The cached page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic int pgmPoolMonitorInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolMonitorInsert %RGp\n", pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1)));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Filter out the relevant kinds.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Nothing to monitor here. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Nothing to monitor here. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
18db01bbe01d709ed64ef78717e98b94b7fee056vboxsync * Install handler.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PPGMPOOLPAGE pPageHead = pgmPoolMonitorGetPageByGCPhys(pPool, pPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(pPageHead != pPage); Assert(pPageHead->iMonitoredNext != pPage->idx);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolFlushDirtyPage(pPool->CTX_SUFF(pVM), pPool, pPageHead->idxDirty, false /* do not remove */);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPool->aPages[pPageHead->iMonitoredNext].iMonitoredPrev = pPage->idx;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(pPage->iMonitoredNext == NIL_PGMPOOL_IDX); Assert(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync const RTGCPHYS GCPhysPage = pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = PGMHandlerPhysicalRegisterEx(pVM, PGMPHYSHANDLERTYPE_PHYSICAL_WRITE,
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pPage),
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pPage),
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPool->pfnAccessHandlerRC, MMHyperCCToRC(pVM, pPage),
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /** @todo we should probably deal with out-of-memory conditions here, but for now increasing
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * the heap size should suffice. */
06de36c1229f14000638fa27993b13c9133e85f5vboxsync AssertFatalMsgRC(rc, ("PGMHandlerPhysicalRegisterEx %RGp failed with %Rrc\n", GCPhysPage, rc));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(!(VMMGetCpu(pVM)->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL) || VMCPU_FF_ISSET(VMMGetCpu(pVM), VMCPU_FF_PGM_SYNC_CR3));
5dda7f07dab8a954e6c4cf2378b15f921e60d9aavboxsync * Disables write monitoring of a guest page.
5dda7f07dab8a954e6c4cf2378b15f921e60d9aavboxsync * @returns VBox status code.
5dda7f07dab8a954e6c4cf2378b15f921e60d9aavboxsync * @retval VINF_SUCCESS on success.
5dda7f07dab8a954e6c4cf2378b15f921e60d9aavboxsync * @param pPool The pool.
5dda7f07dab8a954e6c4cf2378b15f921e60d9aavboxsync * @param pPage The cached page.
5dda7f07dab8a954e6c4cf2378b15f921e60d9aavboxsyncstatic int pgmPoolMonitorFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
5dda7f07dab8a954e6c4cf2378b15f921e60d9aavboxsync * Filter out the relevant kinds.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Nothing to monitor here. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Remove the page from the monitored list or uninstall it if last.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PPGMPOOLPAGE pNewHead = &pPool->aPages[pPage->iMonitoredNext];
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = PGMHandlerPhysicalChangeCallbacks(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1),
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pNewHead),
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pNewHead),
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPool->pfnAccessHandlerRC, MMHyperCCToRC(pVM, pNewHead),
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPool->aPages[pPage->iMonitoredPrev].iMonitoredNext = pPage->iMonitoredNext;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPool->aPages[pPage->iMonitoredNext].iMonitoredPrev = pPage->iMonitoredPrev;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = PGMHandlerPhysicalDeregister(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1));
32f98f3d0fb00168315b4ece1f0808df4cd88248vboxsync AssertMsg(!(pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL) || VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3),
32f98f3d0fb00168315b4ece1f0808df4cd88248vboxsync ("%#x %#x\n", pVCpu->pgm.s.fSyncFlags, pVM->fGlobalForcedActions));
32f98f3d0fb00168315b4ece1f0808df4cd88248vboxsync * Remove it from the list of modified pages (if in it).
32f98f3d0fb00168315b4ece1f0808df4cd88248vboxsync * Inserts the page into the list of modified pages.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncvoid pgmPoolMonitorModifiedInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log3(("pgmPoolMonitorModifiedInsert: idx=%d\n", pPage->idx));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync ("Next=%d Prev=%d idx=%d cModifications=%d Head=%d cModifiedPages=%d\n",
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPage->iModifiedNext, pPage->iModifiedPrev, pPage->idx, pPage->cModifications,
32f98f3d0fb00168315b4ece1f0808df4cd88248vboxsync pPool->aPages[pPool->iModifiedHead].iModifiedPrev = pPage->idx;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (pPool->cModifiedPages > pPool->cModifiedPagesHigh)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Removes the page from the list of modified pages and resets the
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * moficiation counter.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The page which is believed to be in the list of modified pages.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic void pgmPoolMonitorModifiedRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log3(("pgmPoolMonitorModifiedRemove: idx=%d cModifications=%d\n", pPage->idx, pPage->cModifications));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPool->aPages[pPage->iModifiedNext].iModifiedPrev = NIL_PGMPOOL_IDX;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPool->aPages[pPage->iModifiedPrev].iModifiedNext = pPage->iModifiedNext;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPool->aPages[pPage->iModifiedNext].iModifiedPrev = pPage->iModifiedPrev;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Zaps the list of modified pages, resetting their modification counters in the process.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolMonitorModifiedClearAll: cModifiedPages=%d\n", pPool->cModifiedPages));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertMsg(cPages == pPool->cModifiedPages, ("%d != %d\n", cPages, pPool->cModifiedPages));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Handle SyncCR3 pool tasks
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VINF_SUCCESS if successfully added.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VINF_PGM_SYNC_CR3 is it needs to be deferred to ring 3 (GC only)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVCpu The VMCPU handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @remark Should only be used when monitoring is available, thus placed in
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * the PGMPOOL_WITH_MONITORING #ifdef.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * When monitoring shadowed pages, we reset the modification counters on CR3 sync.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Occasionally we will have to clear all the shadow page tables because we wanted
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * to monitor a page which was mapped by too many shadowed page tables. This operation
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * sometimes refered to as a 'lightweight flush'.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync# ifdef IN_RING3 /* Don't flush in ring-0 or raw mode, it's taking too long. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync# else /* !IN_RING3 */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("SyncCR3: PGM_SYNC_CLEAR_PGM_POOL is set -> VINF_PGM_SYNC_CR3\n"));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3); /** @todo no need to do global sync, right? */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync# endif /* !IN_RING3 */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync#endif /* PGMPOOL_WITH_MONITORING */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Frees up at least one user entry.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VINF_SUCCESS if successfully added.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
afed5ab737f4aacfae3fe73776f40e989190a7cavboxsync * @param iUser The user index.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic int pgmPoolTrackFreeOneUser(PPGMPOOL pPool, uint16_t iUser)
2f1b3d7474489e653b75edf9492132a0f56aab4dvboxsync * Just free cached pages in a braindead fashion.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /** @todo walk the age list backwards and free the first with usage. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync } while (pPool->iUserFreeHead == NIL_PGMPOOL_USER_INDEX);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Lazy approach.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* @todo This path no longer works (CR3 root pages will be flushed)!! */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Inserts a page into the cache.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This will create user node for the page, insert it into the GCPhys
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * hash, and insert it into the age list.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VINF_SUCCESS if successfully added.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The cached page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param GCPhys The GC physical address of the page we're gonna shadow.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iUser The user index.
b1c3cdef473df2fbc621d5da81acc82dbfb8a11avboxsync * @param iUserTable The user table index.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncDECLINLINE(int) pgmPoolTrackInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhys, uint16_t iUser, uint32_t iUserTable)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolTrackInsert GCPhys=%RGp iUser %x iUserTable %x\n", GCPhys, iUser, iUserTable));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Check that the entry doesn't already exists.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertMsg(paUsers[i].iUser != iUser || paUsers[i].iUserTable != iUserTable, ("%x %x vs new %x %x\n", paUsers[i].iUser, paUsers[i].iUserTable, iUser, iUserTable));
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync * Find free a user node.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Unlink the user node from the free list,
10e1bc06b2908a0af56d92ffdbadd25b36a5ef61vboxsync * initialize and insert it into the user list.
9b45880674da6f82ca27cc28b0272de3dd3cc7dfvboxsync * Insert into cache and enable monitoring of the guest page if enabled.
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * Until we implement caching of all levels, including the CR3 one, we'll
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * have to make sure we don't try monitor & cache any recursive reuse of
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * a monitored CR3 page. Because all windows versions are doing this we'll
75fca099c974cbbd48f132af1378c60232614760vboxsync * have to be able to do combined access monitoring, CR3 + PT and
75fca099c974cbbd48f132af1378c60232614760vboxsync * PD + PT (guest PAE).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * We're now cooperating with the CR3 monitor if an uncachable page is found.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync#if defined(PGMPOOL_WITH_MONITORING) || defined(PGMPOOL_WITH_CACHE)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync const bool fCanBeMonitored = true;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync bool fCanBeMonitored = pPool->CTX_SUFF(pVM)->pgm.s.GCPhysGstCR3Monitored == NIL_RTGCPHYS
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync || (GCPhys & X86_PTE_PAE_PG_MASK) != (pPool->CTX_SUFF(pVM)->pgm.s.GCPhysGstCR3Monitored & X86_PTE_PAE_PG_MASK)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolCacheInsert(pPool, pPage, fCanBeMonitored); /* This can be expanded. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync#endif /* PGMPOOL_WITH_MONITORING */
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync# ifdef PGMPOOL_WITH_CACHE /* (only used when the cache is enabled.) */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Adds a user reference to a page.
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync * This will move the page to the head of the
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync * @returns VBox status code.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VINF_SUCCESS if successfully added.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The cached page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iUser The user index.
10e1bc06b2908a0af56d92ffdbadd25b36a5ef61vboxsync * @param iUserTable The user table.
5db1d52ffbcaa46c3d944c6c2d9c552306817d9avboxsyncstatic int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log3(("pgmPoolTrackAddUser GCPhys = %RGp iUser %x iUserTable %x\n", pPage->GCPhys, iUser, iUserTable));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Check that the entry doesn't already exists. We only allow multiple users of top-level paging structures (SHW_POOL_ROOT_IDX).
ec588a4ac8429a8b6c744544818b3ce3b2c75690vboxsync AssertMsg(iUser != PGMPOOL_IDX_PD || iUser != PGMPOOL_IDX_PDPT || iUser != PGMPOOL_IDX_NESTED_ROOT || iUser != PGMPOOL_IDX_AMD64_CR3 ||
75fca099c974cbbd48f132af1378c60232614760vboxsync paUsers[i].iUser != iUser || paUsers[i].iUserTable != iUserTable, ("%x %x vs new %x %x\n", paUsers[i].iUser, paUsers[i].iUserTable, iUser, iUserTable));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Allocate a user node.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Initialize the user node and insert it.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolFlushDirtyPage(pPool->CTX_SUFF(pVM), pPool, pPage->idxDirty, false /* do not remove */);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Tell the cache to update its replacement stats for this page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync# endif /* PGMPOOL_WITH_CACHE */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Frees a user record associated with a page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This does not clear the entry in the user table, it simply replaces the
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * user record to the chain of free records.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param HCPhys The HC physical address of the shadow page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iUser The shadow page pool index of the user table.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iUserTable The index into the user table (shadowed).
467e62e1aa999ec951e820d74498ef79dd1bedbdvboxsyncstatic void pgmPoolTrackFreeUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Unlink and free the specified user entry.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log3(("pgmPoolTrackFreeUser %RGp %x %x\n", pPage->GCPhys, iUser, iUserTable));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Special: For PAE and 32-bit paging, there is usually no more than one user. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* General: Linear search. */
8c9d2065ca6efecd5661174e382314868c0d4b94vboxsync /* Fatal: didn't find it */
8c9d2065ca6efecd5661174e382314868c0d4b94vboxsync AssertFatalMsgFailed(("Didn't find the user entry! iUser=%#x iUserTable=%#x GCPhys=%RGp\n",
8c9d2065ca6efecd5661174e382314868c0d4b94vboxsync * Gets the entry size of a shadow table.
8c9d2065ca6efecd5661174e382314868c0d4b94vboxsync * @param enmKind The kind of page.
8c9d2065ca6efecd5661174e382314868c0d4b94vboxsync * @returns The size of the entry in bytes. That is, 4 or 8.
8c9d2065ca6efecd5661174e382314868c0d4b94vboxsync * @returns If the kind is not for a table, an assertion is raised and 0 is
467e62e1aa999ec951e820d74498ef79dd1bedbdvboxsync * returned.
8c9d2065ca6efecd5661174e382314868c0d4b94vboxsyncDECLINLINE(unsigned) pgmPoolTrackGetShadowEntrySize(PGMPOOLKIND enmKind)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Gets the entry size of a guest table.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param enmKind The kind of page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns The size of the entry in bytes. That is, 0, 4 or 8.
bee80f3f77b15c166b7debd080a70e5231ba6751vboxsync * @returns If the kind is not for a table, an assertion is raised and 0 is
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * returned.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncDECLINLINE(unsigned) pgmPoolTrackGetGuestEntrySize(PGMPOOLKIND enmKind)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /** @todo can we return 0? (nobody is calling this...) */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Scans one shadow page table for mappings of a physical page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns true/false indicating removal of all relevant PTEs
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPhysPage The guest page in question.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param fFlushPTEs Flush PTEs or allow them to be updated (e.g. in case of an RW bit change)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iShw The shadow page table.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param cRefs The number of references made in that PT.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pfKeptPTEs Flag indicating removal of all relevant PTEs (out)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic bool pgmPoolTrackFlushGCPhysPTInt(PVM pVM, PCPGMPAGE pPhysPage, bool fFlushPTEs, uint16_t iShw, uint16_t cRefs)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolTrackFlushGCPhysPT: pPhysPage=%RHp iShw=%d cRefs=%d\n", PGM_PAGE_GET_HCPHYS(pPhysPage), iShw, cRefs));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync bool bRet = false;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Assert sanity.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertFatalMsg(iShw < pPool->cCurPages && iShw != NIL_PGMPOOL_IDX, ("iShw=%d\n", iShw));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Then, clear the actual mappings to the page in the shadow PT.
9c425bdea5f0991df62922b1584b805a86f2f898vboxsync const uint32_t u32 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PX86PT pPT = (PX86PT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync case PGM_PAGE_HNDL_PHYS_STATE_NONE: /** No handler installed. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync case PGM_PAGE_HNDL_PHYS_STATE_DISABLED: /** Monitoring is temporarily disabled. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync case PGM_PAGE_HNDL_PHYS_STATE_WRITE: /** Write access is monitored. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX32 cRefs=%#x\n", i, pPT->a[i], cRefs));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Pte.n.u1Write = 0; /* need to disallow writes when dirty bit tracking is still active. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
7bae75e0b207aa4d4cad2a951271ad1a0e8ab9fdvboxsync const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
da936e0446fb2b56b813d5d938f1dfc6e4bf8b13vboxsync PX86PTPAE pPT = (PX86PTPAE)PGMPOOL_PAGE_2_PTR(pVM, pPage);
da936e0446fb2b56b813d5d938f1dfc6e4bf8b13vboxsync case PGM_PAGE_HNDL_PHYS_STATE_NONE: /** No handler installed. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync case PGM_PAGE_HNDL_PHYS_STATE_DISABLED: /** Monitoring is temporarily disabled. */
762632a567a0397ff5d6ea93cb1fce60728e7ed3vboxsync case PGM_PAGE_HNDL_PHYS_STATE_WRITE: /** Write access is monitored. */
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX64 cRefs=%#x\n", i, pPT->a[i], cRefs));
da936e0446fb2b56b813d5d938f1dfc6e4bf8b13vboxsync Pte.n.u1Write = 0; /* need to disallow writes when dirty bit tracking is still active. */
da936e0446fb2b56b813d5d938f1dfc6e4bf8b13vboxsync Log(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d u64=%RX64\n", cRefs, pPage->iFirstPresent, pPage->cPresent, u64));
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync PEPTPT pPT = (PEPTPT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
75fca099c974cbbd48f132af1378c60232614760vboxsync if ((pPT->a[i].u & (EPT_PTE_PG_MASK | X86_PTE_P)) == u64)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX64 cRefs=%#x\n", i, pPT->a[i], cRefs));
da936e0446fb2b56b813d5d938f1dfc6e4bf8b13vboxsync pPT->a[i].u = 0;
4cad44a5f7cfa7db8d5fb255a1df3b3cd1687853vboxsync Log(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
da936e0446fb2b56b813d5d938f1dfc6e4bf8b13vboxsync if ((pPT->a[i].u & (EPT_PTE_PG_MASK | X86_PTE_P)) == u64)
da936e0446fb2b56b813d5d938f1dfc6e4bf8b13vboxsync AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
da936e0446fb2b56b813d5d938f1dfc6e4bf8b13vboxsync AssertFatalMsgFailed(("enmKind=%d iShw=%d\n", pPage->enmKind, iShw));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Scans one shadow page table for mappings of a physical page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPhysPage The guest page in question.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param fFlushPTEs Flush PTEs or allow them to be updated (e.g. in case of an RW bit change)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iShw The shadow page table.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param cRefs The number of references made in that PT.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic void pgmPoolTrackFlushGCPhysPT(PVM pVM, PPGMPAGE pPhysPage, bool fFlushPTEs, uint16_t iShw, uint16_t cRefs)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool); NOREF(pPool);
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync Log2(("pgmPoolTrackFlushGCPhysPT: pPhysPage=%RHp iShw=%d cRefs=%d\n", PGM_PAGE_GET_HCPHYS(pPhysPage), iShw, cRefs));
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPT, f);
75fca099c974cbbd48f132af1378c60232614760vboxsync bool fKeptPTEs = pgmPoolTrackFlushGCPhysPTInt(pVM, pPhysPage, fFlushPTEs, iShw, cRefs);
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPT, f);
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync * Flushes a list of shadow page tables mapping the same physical page.
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync * @param pVM The VM handle.
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync * @param pPhysPage The guest page in question.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param fFlushPTEs Flush PTEs or allow them to be updated (e.g. in case of an RW bit change)
e751d1e1e4ae424d4846a23da32f663d14960796vboxsync * @param iPhysExt The physical cross reference extent list to flush.
e751d1e1e4ae424d4846a23da32f663d14960796vboxsyncstatic void pgmPoolTrackFlushGCPhysPTs(PVM pVM, PPGMPAGE pPhysPage, bool fFlushPTEs, uint16_t iPhysExt)
e751d1e1e4ae424d4846a23da32f663d14960796vboxsync bool fKeepList = false;
2b88b28162a49c6fa769c03d56767a9806d4f456vboxsync STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPTs, f);
10e1bc06b2908a0af56d92ffdbadd25b36a5ef61vboxsync Log2(("pgmPoolTrackFlushGCPhysPTs: pPhysPage=%RHp iPhysExt\n", PGM_PAGE_GET_HCPHYS(pPhysPage), iPhysExt));
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
54fe68d807196dc565de24055996e634ee40ee92vboxsync bool fKeptPTEs = pgmPoolTrackFlushGCPhysPTInt(pVM, pPhysPage, fFlushPTEs, pPhysExt->aidx[i], 1);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* insert the list into the free list and clear the ram range entry. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTs, f);
907da3b41f191bef4c8eb94accb9d16fcbe77019vboxsync#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Flushes all shadow page table mappings of the given guest page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This is typically called when the host page backing the guest one has been
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * replaced or when the page protection was changed due to an access handler.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VINF_SUCCESS if all references has been successfully cleared.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VINF_PGM_SYNC_CR3 if we're better off with a CR3 sync and a page
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * pool cleaning. FF and sync flags are set.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPhysPage The guest page in question.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param fFlushPTEs Flush PTEs or allow them to be updated (e.g. in case of an RW bit change)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pfFlushTLBs This is set to @a true if the shadow TLBs should be
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * flushed, it is NOT touched if this isn't necessary.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * The caller MUST initialized this to @a false.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncint pgmPoolTrackUpdateGCPhys(PVM pVM, PPGMPAGE pPhysPage, bool fFlushPTEs, bool *pfFlushTLBs)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync const uint16_t u16 = PGM_PAGE_GET_TRACKING(pPhysPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * The zero page is currently screwing up the tracking and we'll
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * have to flush the whole shebang. Unless VBOX_WITH_NEW_LAZY_PAGE_ALLOC
50df3da42ff6589b0ecc4f50f2288811bc370186vboxsync * is defined, zero pages won't normally be mapped. Some kind of solution
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * will be needed for this problem of course, but it will have to wait...
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Start a subset here because pgmPoolTrackFlushGCPhysPTsSlow and
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackFlushGCPhysPTs will/may kill the pool otherwise. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (PGMPOOL_TD_GET_CREFS(u16) != PGMPOOL_TD_CREFS_PHYSEXT)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync else if (u16 != PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED))
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackFlushGCPhysPTs(pVM, pPhysPage, fFlushPTEs, PGMPOOL_TD_GET_IDX(u16));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = pgmPoolTrackFlushGCPhysPTsSlow(pVM, pPhysPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Start a subset here because pgmPoolTrackFlushGCPhysPTsSlow kills the pool otherwise. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = pgmPoolTrackFlushGCPhysPTsSlow(pVM, pPhysPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pVCpu->pgm.s.fSyncFlags |= PGM_SYNC_CLEAR_PGM_POOL;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Scans all shadow page tables for mappings of a physical page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This may be slow, but it's most likely more efficient than cleaning
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * out the entire page pool / cache.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VINF_SUCCESS if all references has been successfully cleared.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VINF_PGM_GCPHYS_ALIASED if we're better off with a CR3 sync and
50df3da42ff6589b0ecc4f50f2288811bc370186vboxsync * a page pool cleaning.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPhysPage The guest page in question.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncint pgmPoolTrackFlushGCPhysPTsSlow(PVM pVM, PPGMPAGE pPhysPage)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPTsSlow, s);
65ff326ac3915638f5c4e79c2c3e836aeeeb86devboxsync LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: cUsedPages=%d cPresent=%d pPhysPage=%R[pgmpage]\n",
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * There is a limit to what makes sense.
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: giving up... (cPresent=%d)\n", pPool->cPresent));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTsSlow, s);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Iterate all the pages until we've encountered all that in use.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This is simple but not quite optimal solution.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * We only care about shadow page tables.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PX86PT pPT = (PX86PT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
65ff326ac3915638f5c4e79c2c3e836aeeeb86devboxsync //Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX32\n", iPage, i, pPT->a[i]));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPT->a[i].u = 0;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PX86PTPAE pPT = (PX86PTPAE)PGMPOOL_PAGE_2_PTR(pVM, pPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
65ff326ac3915638f5c4e79c2c3e836aeeeb86devboxsync if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
65ff326ac3915638f5c4e79c2c3e836aeeeb86devboxsync //Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX64\n", iPage, i, pPT->a[i]));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPT->a[i].u = 0;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTsSlow, s);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clears the user entry in a user table.
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsync * This is used to remove all references to a page when flushing it.
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsyncstatic void pgmPoolTrackClearPageUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PCPGMPOOLUSER pUser)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Map the user page.
6b022885f2cb6a55167609edecd89570cd80001dvboxsync PPGMPOOLPAGE pUserPage = &pPool->aPages[pUser->iUser];
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync u.pau64 = (uint64_t *)PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pUserPage);
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync LogFlow(("pgmPoolTrackClearPageUser: clear %x in %s (%RGp) (flushing %s)\n", iUserTable, pgmPoolPoolKindToStr(pUserPage->enmKind), pUserPage->Core.Key, pgmPoolPoolKindToStr(pPage->enmKind)));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Safety precaution in case we change the paging for other modes too in the future. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(!pgmPoolIsPageLocked(&pPool->CTX_SUFF(pVM)->pgm.s, pPage));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Some sanity checks.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(!(u.pau64[iUserTable] & PGM_PDFLAGS_MAPPING));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* GCPhys >> PAGE_SHIFT is the index here */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertMsgFailed(("enmKind=%d\n", pUserPage->enmKind));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync#endif /* VBOX_STRICT */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear the entry in the user page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* 32-bit entries */
6b022885f2cb6a55167609edecd89570cd80001dvboxsync /* 64-bit entries */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* 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
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsync * non-present PDPT will continue to cause page faults.
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync /* no break */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertFatalMsgFailed(("enmKind=%d iUser=%#x iUserTable=%#x\n", pUserPage->enmKind, pUser->iUser, pUser->iUserTable));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clears all users of a page.
fa7355597d7eba5b4ca8a23056ebe1b8fb306fdbvboxsyncstatic void pgmPoolTrackClearPageUsers(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Free all the user records.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolTrackClearPageUsers %RGp\n", pPage->GCPhys));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Clear enter in user table. */
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsync pgmPoolTrackClearPageUser(pPool, pPage, &paUsers[i]);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Free it. */
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync /* Next. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Allocates a new physical cross reference extent.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns Pointer to the allocated extent on success. NULL if we're out of them.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsync * @param piPhysExt Where to store the phys ext index.
b1c3cdef473df2fbc621d5da81acc82dbfb8a11avboxsyncPPGMPOOLPHYSEXT pgmPoolTrackPhysExtAlloc(PVM pVM, uint16_t *piPhysExt)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_COUNTER_INC(&pPool->StamTrackPhysExtAllocFailures);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PPGMPOOLPHYSEXT pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Frees a physical cross reference extent.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iPhysExt The extent to free.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncvoid pgmPoolTrackPhysExtFree(PVM pVM, uint16_t iPhysExt)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PPGMPOOLPHYSEXT pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Frees a physical cross reference extent.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iPhysExt The extent to free.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncvoid pgmPoolTrackPhysExtFreeList(PVM pVM, uint16_t iPhysExt)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Insert a reference into a list of physical cross reference extents.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns The new tracking data for PGMPAGE.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iPhysExt The physical extent index of the list head.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iShwPT The shadow page table index.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic uint16_t pgmPoolTrackPhysExtInsert(PVM pVM, uint16_t iPhysExt, uint16_t iShwPT)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* special common case. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (paPhysExts[iPhysExt].aidx[2] == NIL_PGMPOOL_IDX)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedMany);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolTrackPhysExtInsert: %d:{,,%d}\n", iPhysExt, iShwPT));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExt);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* general treatment. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (paPhysExts[iPhysExt].aidx[i] == NIL_PGMPOOL_IDX)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedMany);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolTrackPhysExtInsert: %d:{%d} i=%d cMax=%d\n", iPhysExt, iShwPT, i, cMax));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExtStart);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolTrackPhysExtInsert: overflow (1) iShwPT=%d\n", iShwPT));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED);
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync /* add another extent to the list. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PPGMPOOLPHYSEXT pNew = pgmPoolTrackPhysExtAlloc(pVM, &iPhysExt);
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync LogFlow(("pgmPoolTrackPhysExtInsert: pgmPoolTrackPhysExtAlloc failed iShwPT=%d\n", iShwPT));
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED);
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync LogFlow(("pgmPoolTrackPhysExtInsert: added new extent %d:{%d}->%d\n", iPhysExt, iShwPT, iPhysExtStart));
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExt);
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync * Add a reference to guest physical page where extents are in use.
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync * @returns The new tracking data for PGMPAGE.
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync * @param pVM The VM handle.
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync * @param u16 The ram range flags (top 16-bits).
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync * @param iShwPT The shadow page table index.
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsyncuint16_t pgmPoolTrackPhysExtAddref(PVM pVM, uint16_t u16, uint16_t iShwPT)
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync if (PGMPOOL_TD_GET_CREFS(u16) != PGMPOOL_TD_CREFS_PHYSEXT)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Convert to extent list.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PPGMPOOLPHYSEXT pPhysExt = pgmPoolTrackPhysExtAlloc(pVM, &iPhysExt);
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync LogFlow(("pgmPoolTrackPhysExtAddref: new extent: %d:{%d, %d}\n", iPhysExt, PGMPOOL_TD_GET_IDX(u16), iShwPT));
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync u16 = PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExt);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync u16 = PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync else if (u16 != PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED))
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Insert into the extent list.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync u16 = pgmPoolTrackPhysExtInsert(pVM, PGMPOOL_TD_GET_IDX(u16), iShwPT);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedLots);
8f7bc6ad2b7bbcb4b3b96248cd2478e45f2e3b88vboxsync * Clear references to guest physical memory.
8f7bc6ad2b7bbcb4b3b96248cd2478e45f2e3b88vboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The page.
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync * @param pPhysPage Pointer to the aPages entry in the ram range.
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsyncvoid pgmPoolTrackPhysExtDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PPGMPAGE pPhysPage)
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync const unsigned cRefs = PGM_PAGE_GET_TD_CREFS(pPhysPage);
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync AssertFatalMsg(cRefs == PGMPOOL_TD_CREFS_PHYSEXT, ("cRefs=%d pPhysPage=%R[pgmpage] pPage=%p:{.idx=%d}\n", cRefs, pPhysPage, pPage, pPage->idx));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync uint16_t iPhysExt = PGM_PAGE_GET_TD_IDX(pPhysPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
40f47bc231ff6c86a7ddc3295eed7010e889e78avboxsync * Look for the shadow page and check if it's all freed.
40f47bc231ff6c86a7ddc3295eed7010e889e78avboxsync for (unsigned i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync for (i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
40f47bc231ff6c86a7ddc3295eed7010e889e78avboxsync if (paPhysExts[iPhysExt].aidx[i] != NIL_PGMPOOL_IDX)
40f47bc231ff6c86a7ddc3295eed7010e889e78avboxsync Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d\n", pPhysPage, pPage->idx));
40f47bc231ff6c86a7ddc3295eed7010e889e78avboxsync /* we can free the node. */
40f47bc231ff6c86a7ddc3295eed7010e889e78avboxsync const uint16_t iPhysExtNext = paPhysExts[iPhysExt].iNext;
40f47bc231ff6c86a7ddc3295eed7010e889e78avboxsync /* lonely node */
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d lonely\n", pPhysPage, pPage->idx));
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync else if (iPhysExtPrev == NIL_PGMPOOL_PHYSEXT_INDEX)
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d head\n", pPhysPage, pPage->idx));
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync PGM_PAGE_SET_TRACKING(pPhysPage, PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExtNext));
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync /* in list */
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d\n", pPhysPage, pPage->idx));
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync AssertFatalMsgFailed(("not-found! cRefs=%d pPhysPage=%R[pgmpage] pPage=%p:{.idx=%d}\n", cRefs, pPhysPage, pPage, pPage->idx));
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync else /* nothing to do */
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage]\n", pPhysPage));
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * Clear references to guest physical memory.
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * This is the same as pgmPoolTracDerefGCPhys except that the guest physical address
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * is assumed to be correct, so the linear search can be skipped and we can assert
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * at an earlier point.
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync * @param pPool The pool.
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * @param pPage The page.
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * @param HCPhys The host physical address corresponding to the guest page.
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * @param GCPhys The guest physical address corresponding to HCPhys.
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsyncstatic void pgmPoolTracDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhys)
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * Walk range list.
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync /* does it match? */
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsyncRTHCPHYS HCPhysPage = PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]);
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsyncLog2(("pgmPoolTracDerefGCPhys %RHp vs %RHp\n", HCPhysPage, HCPhys));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertFatalMsgFailed(("HCPhys=%RHp GCPhys=%RGp\n", HCPhys, GCPhys));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear references to guest physical memory.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param HCPhys The host physical address corresponding to the guest page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param GCPhysHint The guest physical address which may corresponding to HCPhys.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncvoid pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint)
6b022885f2cb6a55167609edecd89570cd80001dvboxsync Log4(("pgmPoolTracDerefGCPhysHint %RHp %RGp\n", HCPhys, GCPhysHint));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Walk range list.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* does it match? */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Damn, the hint didn't work. We'll have to do an expensive linear search.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_COUNTER_INC(&pPool->StatTrackLinearRamSearches);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync while (iPage-- > 0)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
d86aa8a95e908fa0d5e88ed662ea0a919f17756fvboxsync Log4(("pgmPoolTracDerefGCPhysHint: Linear HCPhys=%RHp GCPhysHint=%RGp GCPhysReal=%RGp\n",
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync HCPhys, GCPhysHint, pRam->GCPhys + (iPage << PAGE_SHIFT)));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertFatalMsgFailed(("HCPhys=%RHp GCPhysHint=%RGp\n", HCPhys, GCPhysHint));
d86aa8a95e908fa0d5e88ed662ea0a919f17756fvboxsync * Clear references to guest physical memory in a 32-bit / 32-bit page table.
9c425bdea5f0991df62922b1584b805a86f2f898vboxsync * @param pPool The pool.
9c425bdea5f0991df62922b1584b805a86f2f898vboxsync * @param pPage The page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pShwPT The shadow page table (mapping of the page).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pGstPT The guest page table.
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsyncDECLINLINE(void) pgmPoolTrackDerefPT32Bit32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT, PCX86PT pGstPT)
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
f7929b81a7a4044bd14d5ea16f5c94703b84c1cdvboxsync Log4(("pgmPoolTrackDerefPT32Bit32Bit: i=%d pte=%RX32 hint=%RX32\n",
10e1bc06b2908a0af56d92ffdbadd25b36a5ef61vboxsync i, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK));
9c425bdea5f0991df62922b1584b805a86f2f898vboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync * Clear references to guest physical memory in a PAE / 32-bit page table.
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync * @param pPool The pool.
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync * @param pPage The page.
db548661809056af7b58115fdcf6077043d13863vboxsync * @param pShwPT The shadow page table (mapping of the page).
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync * @param pGstPT The guest page table (just a half one).
8f7bc6ad2b7bbcb4b3b96248cd2478e45f2e3b88vboxsyncDECLINLINE(void) pgmPoolTrackDerefPTPae32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PT pGstPT)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
40f47bc231ff6c86a7ddc3295eed7010e889e78avboxsync Log4(("pgmPoolTrackDerefPTPae32Bit: i=%d pte=%RX64 hint=%RX32\n",
40f47bc231ff6c86a7ddc3295eed7010e889e78avboxsync i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK));
40f47bc231ff6c86a7ddc3295eed7010e889e78avboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * Clear references to guest physical memory in a PAE / PAE page table.
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * @param pPool The pool.
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * @param pPage The page.
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * @param pShwPT The shadow page table (mapping of the page).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pGstPT The guest page table.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncDECLINLINE(void) pgmPoolTrackDerefPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PTPAE pGstPT)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log4(("pgmPoolTrackDerefPTPaePae: i=%d pte=%RX32 hint=%RX32\n",
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PAE_PG_MASK));
22e281e75ed636601178296c6daebda8f1d17c59vboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PAE_PG_MASK);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear references to guest physical memory in a 32-bit / 4MB page table.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The page.
f3c0757250e08fe6b4890aee39c38c5688715682vboxsync * @param pShwPT The shadow page table (mapping of the page).
f3c0757250e08fe6b4890aee39c38c5688715682vboxsyncDECLINLINE(void) pgmPoolTrackDerefPT32Bit4MB(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT)
f3c0757250e08fe6b4890aee39c38c5688715682vboxsync RTGCPHYS GCPhys = pPage->GCPhys + PAGE_SIZE * pPage->iFirstPresent;
f3c0757250e08fe6b4890aee39c38c5688715682vboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log4(("pgmPoolTrackDerefPT32Bit4MB: i=%d pte=%RX32 GCPhys=%RGp\n",
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, GCPhys);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear references to guest physical memory in a PAE / 2/4MB page table.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pShwPT The shadow page table (mapping of the page).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncDECLINLINE(void) pgmPoolTrackDerefPTPaeBig(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync RTGCPHYS GCPhys = pPage->GCPhys + PAGE_SIZE * pPage->iFirstPresent;
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log4(("pgmPoolTrackDerefPTPaeBig: i=%d pte=%RX64 hint=%RGp\n",
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, GCPhys);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear references to shadowed pages in an EPT page table.
8f7bc6ad2b7bbcb4b3b96248cd2478e45f2e3b88vboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pShwPML4 The shadow page directory pointer table (mapping of the page).
ec588a4ac8429a8b6c744544818b3ce3b2c75690vboxsyncDECLINLINE(void) pgmPoolTrackDerefPTEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPT pShwPT)
68ef804c4ec232c58e2c03c8fc6afe3765c5c0d1vboxsync RTGCPHYS GCPhys = pPage->GCPhys + PAGE_SIZE * pPage->iFirstPresent;
ec588a4ac8429a8b6c744544818b3ce3b2c75690vboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log4(("pgmPoolTrackDerefPTEPT: i=%d pte=%RX64 GCPhys=%RX64\n",
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync i, pShwPT->a[i].u & EPT_PTE_PG_MASK, pPage->GCPhys));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & EPT_PTE_PG_MASK, GCPhys);
f3c0757250e08fe6b4890aee39c38c5688715682vboxsync#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear references to shadowed pages in a 32 bits page directory.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pShwPD The shadow page directory (mapping of the page).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncDECLINLINE(void) pgmPoolTrackDerefPD(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PD pShwPD)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PG_MASK);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertFatalMsgFailed(("%x\n", pShwPD->a[i].u & X86_PDE_PG_MASK));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear references to shadowed pages in a PAE (legacy or 64 bits) page directory.
5ab935df052385fd74329486db23ea63b2004b41vboxsync * @param pPool The pool.
5ab935df052385fd74329486db23ea63b2004b41vboxsync * @param pPage The page.
5ab935df052385fd74329486db23ea63b2004b41vboxsync * @param pShwPD The shadow page directory (mapping of the page).
5ab935df052385fd74329486db23ea63b2004b41vboxsyncDECLINLINE(void) pgmPoolTrackDerefPDPae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPAE pShwPD)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PAE_PG_MASK);
5ab935df052385fd74329486db23ea63b2004b41vboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertFatalMsgFailed(("%RX64\n", pShwPD->a[i].u & X86_PDE_PAE_PG_MASK));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear references to shadowed pages in a PAE page directory pointer table.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pShwPDPT The shadow page directory pointer table (mapping of the page).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncDECLINLINE(void) pgmPoolTrackDerefPDPTPae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPT pShwPDPT)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = 0; i < X86_PG_PAE_PDPE_ENTRIES; i++)
6ee326f4243bcf103c001da26f5d914360b6d312vboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & X86_PDPE_PG_MASK);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & X86_PDPE_PG_MASK));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear references to shadowed pages in a 64-bit page directory pointer table.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pShwPDPT The shadow page directory pointer table (mapping of the page).
6b022885f2cb6a55167609edecd89570cd80001dvboxsyncDECLINLINE(void) pgmPoolTrackDerefPDPT64Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPT pShwPDPT)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPDPT->a); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(!(pShwPDPT->a[i].u & PGM_PLXFLAGS_MAPPING));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & X86_PDPE_PG_MASK);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & X86_PDPE_PG_MASK));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear references to shadowed pages in a 64-bit level 4 page table.
57399ab65e2825c324fb9dcb4642d4ae2c232509vboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pShwPML4 The shadow page directory pointer table (mapping of the page).
6b022885f2cb6a55167609edecd89570cd80001dvboxsyncDECLINLINE(void) pgmPoolTrackDerefPML464Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PML4 pShwPML4)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPML4->a); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPML4->a[i].u & X86_PDPE_PG_MASK);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertFatalMsgFailed(("%RX64\n", pShwPML4->a[i].u & X86_PML4E_PG_MASK));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear references to shadowed pages in an EPT page directory.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pShwPD The shadow page directory (mapping of the page).
e4ea543752422f1139923e3e506c625b0a1827c5vboxsyncDECLINLINE(void) pgmPoolTrackDerefPDEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPD pShwPD)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & EPT_PDE_PG_MASK);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertFatalMsgFailed(("%RX64\n", pShwPD->a[i].u & EPT_PDE_PG_MASK));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear references to shadowed pages in an EPT page directory pointer table.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pShwPDPT The shadow page directory pointer table (mapping of the page).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncDECLINLINE(void) pgmPoolTrackDerefPDPTEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPDPT pShwPDPT)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPDPT->a); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & EPT_PDPTE_PG_MASK);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & EPT_PDPTE_PG_MASK));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clears all references made by this page.
26824086a3f6b36cd911058f1d9b4c0b944706fbvboxsync * This includes other shadow pages and GC physical addresses.
26824086a3f6b36cd911058f1d9b4c0b944706fbvboxsync * @param pPool The pool.
5ab935df052385fd74329486db23ea63b2004b41vboxsync * @param pPage The page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic void pgmPoolTrackDeref(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Map the shadow page and take action according to the page kind.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync void *pvShw = PGMPOOL_PAGE_2_LOCKED_PTR(pPool->CTX_SUFF(pVM), pPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackDerefPT32Bit32Bit(pPool, pPage, (PX86PT)pvShw, (PCX86PT)pvGst);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = PGM_GCPHYS_2_PTR_EX(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackDerefPTPae32Bit(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PT)pvGst);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
1b7f3a92e42fb9cabf96cf31c20481da466edf76vboxsync STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
1b7f3a92e42fb9cabf96cf31c20481da466edf76vboxsync int rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
1b7f3a92e42fb9cabf96cf31c20481da466edf76vboxsync pgmPoolTrackDerefPTPaePae(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PTPAE)pvGst);
61a20f87ecfcf850a70f6832abfb56e581bc6593vboxsync STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
0aeb5f500280603a51364071670efeb2617f771cvboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS: /* treat it like a 4 MB page */
6c849982f467f6a3b6c2e01f8f7109391715c29cvboxsync STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
7e2fce486e7e2e45eb61d78ee206ba42ed89e016vboxsync pgmPoolTrackDerefPT32Bit4MB(pPool, pPage, (PX86PT)pvShw);
7e2fce486e7e2e45eb61d78ee206ba42ed89e016vboxsync STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
0aeb5f500280603a51364071670efeb2617f771cvboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS: /* treat it like a 2 MB page */
79b07d7ae28b916539c6397e010a5cc05e300551vboxsync STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
61a20f87ecfcf850a70f6832abfb56e581bc6593vboxsync pgmPoolTrackDerefPTPaeBig(pPool, pPage, (PX86PTPAE)pvShw);
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
50e0e1bb8996934b3f38c117a456f825052bfb0cvboxsync#else /* !PGMPOOL_WITH_GCPHYS_TRACKING */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync#endif /* !PGMPOOL_WITH_GCPHYS_TRACKING */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackDerefPDPae(pPool, pPage, (PX86PDPAE)pvShw);
50e0e1bb8996934b3f38c117a456f825052bfb0cvboxsync pgmPoolTrackDerefPDPTPae(pPool, pPage, (PX86PDPT)pvShw);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackDerefPDPT64Bit(pPool, pPage, (PX86PDPT)pvShw);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackDerefPML464Bit(pPool, pPage, (PX86PML4)pvShw);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackDerefPTEPT(pPool, pPage, (PEPTPT)pvShw);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackDerefPDEPT(pPool, pPage, (PEPTPD)pvShw);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackDerefPDPTEPT(pPool, pPage, (PEPTPDPT)pvShw);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertFatalMsgFailed(("enmKind=%d\n", pPage->enmKind));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* paranoia, clear the shadow page. Remove this laser (i.e. let Alloc and ClearAll do it). */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync#endif /* PGMPOOL_WITH_USER_TRACKING */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Flushes a pool page.
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync * This moves the page to the free list after removing all user references to it.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VINF_SUCCESS on success.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param HCPhys The HC physical address of the shadow page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncint pgmPoolFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolFlushPage: pPage=%p:{.Key=%RHp, .idx=%d, .enmKind=%s, .GCPhys=%RGp}\n",
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), pPage->GCPhys));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Quietly reject any attempts at flushing any of the special root pages.
5e0e35e65847aae0955835d5c4b4e76143e580c1vboxsync Log(("pgmPoolFlushPage: special root page, rejected. enmKind=%s idx=%d\n", pgmPoolPoolKindToStr(pPage->enmKind), pPage->idx));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Quietly reject any attempts at flushing the currently active shadow CR3 mapping
5e0e35e65847aae0955835d5c4b4e76143e580c1vboxsync AssertMsg( pPage->enmKind == PGMPOOLKIND_64BIT_PML4
5e0e35e65847aae0955835d5c4b4e76143e580c1vboxsync || pPage->enmKind == PGMPOOLKIND_PAE_PDPT_FOR_32BIT
5e0e35e65847aae0955835d5c4b4e76143e580c1vboxsync || pPage->enmKind == PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync || pPage->enmKind == PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync || pPage->enmKind == PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync || pPage->enmKind == PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD,
6b022885f2cb6a55167609edecd89570cd80001dvboxsync ("Can't free the shadow CR3! (%RHp vs %RHp kind=%d\n", PGMGetHyperCR3(VMMGetCpu(pVM)), pPage->Core.Key, pPage->enmKind));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log(("pgmPoolFlushPage: current active shadow CR3, rejected. enmKind=%s idx=%d\n", pgmPoolPoolKindToStr(pPage->enmKind), pPage->idx));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Start a subset so we won't run out of mapping space. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Mark the page as being in need of an ASMMemZeroPage().
26824086a3f6b36cd911058f1d9b4c0b944706fbvboxsync pgmPoolFlushDirtyPage(pVM, pPool, pPage->idxDirty, false /* do not remove */);
c657bea826c7455c93bd45eaebab63a4c7742c42vboxsync * Clear the page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Flush it from the cache.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync#endif /* PGMPOOL_WITH_CACHE */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Heavy stuff done. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Deregistering the monitoring.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Free the page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* REMOVE ME: experiment */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* end experiment */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Frees a usage of a pool page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * The caller is responsible to updating the user table so that it no longer
5e0e35e65847aae0955835d5c4b4e76143e580c1vboxsync * references the shadow page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param HCPhys The HC physical address of the shadow page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iUser The shadow page pool index of the user table.
b3547e42ce5221377866e3fa041b3086b7cc1562vboxsync * @param iUserTable The index into the user table (shadowed).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncvoid pgmPoolFreeByPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolFreeByPage: pPage=%p:{.Key=%RHp, .idx=%d, enmKind=%s} iUser=%#x iUserTable=%#x\n",
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), iUser, iUserTable));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackFreeUser(pPool, pPage, iUser, iUserTable);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Makes one or more free page free.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VINF_SUCCESS on success.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
6ee326f4243bcf103c001da26f5d914360b6d312vboxsync * @param pPool The pool.
6ee326f4243bcf103c001da26f5d914360b6d312vboxsync * @param enmKind Page table kind
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iUser The user of the page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic int pgmPoolMakeMoreFreePages(PPGMPOOL pPool, PGMPOOLKIND enmKind, uint16_t iUser)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolMakeMoreFreePages: iUser=%#x\n", iUser));
1a25adaca81841abf5e6cdfed02eaff64941357dvboxsync * If the pool isn't full grown yet, expand it.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Hack alert: we can't deal with jumps to ring 3 when called from MapCR3 and allocating pages for PAE PDs. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync && (enmKind < PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD || enmKind > PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_PGM_POOL_GROW, 0);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Free one cached page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Flush the pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * If we have tracking enabled, it should be possible to come up with
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * a cheap replacement strategy...
8da94e4d6813b682cdb38eb9b151ada72d91b40evboxsync /* @todo This path no longer works (CR3 root pages will be flushed)!! */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Allocates a page from the pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This page may actually be a cached page and not in need of any processing
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * on the callers part.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code.
afed5ab737f4aacfae3fe73776f40e989190a7cavboxsync * @retval VINF_SUCCESS if a NEW page was allocated.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VINF_PGM_CACHED_PAGE if a CACHED page was returned.
8da94e4d6813b682cdb38eb9b151ada72d91b40evboxsync * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
50df3da42ff6589b0ecc4f50f2288811bc370186vboxsync * @param pVM The VM handle.
223cf005b18af2c21352a70693ebaf0582f68ebcvboxsync * @param GCPhys The GC physical address of the page we're gonna shadow.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * For 4MB and 2MB PD entries, it's the first address the
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * shadow PT is covering.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param enmKind The kind of mapping.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param enmAccess Access type for the mapping (only relevant for big pages)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iUser The shadow page pool index of the user table.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iUserTable The index into the user table (shadowed).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param ppPage Where to store the pointer to the page. NULL is stored here on failure.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param fLockPage Lock the page
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncint pgmPoolAllocEx(PVM pVM, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, PGMPOOLACCESS enmAccess, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage, bool fLockPage)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolAlloc: GCPhys=%RGp enmKind=%s iUser=%#x iUserTable=%#x\n", GCPhys, pgmPoolPoolKindToStr(enmKind), iUser, iUserTable));
223cf005b18af2c21352a70693ebaf0582f68ebcvboxsync /** @todo CSAM/PGMPrefetchPage messes up here during CSAMR3CheckGates
e50404712a2b5234c42bdf9740bddab5729ba188vboxsync * (TRPMR3SyncIDT) because of FF priority. Try fix that?
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync * Assert(!(pVM->pgm.s.fGlobalSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)); */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc2 = pgmPoolCacheAlloc(pPool, GCPhys, enmKind, enmAccess, iUser, iUserTable, ppPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolAlloc: cached returns %Rrc *ppPage=%p:{.Key=%RHp, .idx=%d}\n", rc2, *ppPage, (*ppPage)->Core.Key, (*ppPage)->idx));
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync * Allocate a new one.
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync rc = pgmPoolMakeMoreFreePages(pPool, enmKind, iUser);
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync AssertReleaseReturn(iNew != NIL_PGMPOOL_IDX, VERR_INTERNAL_ERROR);
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync /* unlink the free head */
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync * Initialize it.
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync pPool->cUsedPages++; /* physical handler registration / pgmPoolTrackFlushGCPhysPTsSlow requirement. */
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync pPage->fSeenNonGlobal = false; /* Set this to 'true' to disable this feature. */
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync * Insert into the tracking and cache. If this fails, free the page.
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync int rc3 = pgmPoolTrackInsert(pPool, pPage, GCPhys, iUser, iUserTable);
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync Log(("pgmPoolAlloc: returns %Rrc (Insert)\n", rc3));
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync#endif /* PGMPOOL_WITH_USER_TRACKING */
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync * Commit the allocation, clear the page and return.
if (fLockPage)
LogFlow(("pgmPoolAlloc: returns %Rrc *ppPage=%p:{.Key=%RHp, .idx=%d, .fCached=%RTbool, .fMonitored=%RTbool}\n",
return rc;
PPGMPOOLPAGE pPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, HCPhys & X86_PTE_PAE_PG_MASK);
AssertFatalMsg(pPage && pPage->enmKind != PGMPOOLKIND_FREE, ("HCPhys=%RHp pPage=%p idx=%d\n", HCPhys, pPage, (pPage) ? pPage->idx : 0));
return pPage;
#ifdef IN_RING3 /* currently only used in ring 3; save some space in the R0 & GC modules (left it here as we might need it elsewhere later on) */
#ifdef PGMPOOL_WITH_CACHE
if (i == NIL_PGMPOOL_IDX)
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_PAE_PDPT:
case PGMPOOLKIND_ROOT_NESTED:
case PGMPOOLKIND_PAE_PD_PHYS:
} while (i != NIL_PGMPOOL_IDX);
#ifdef IN_RING3
#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
#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: