PGMAllPool.cpp revision 3218249923b5e2e183f49ef5ac859e4d0c831913
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 void pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint);
afed5ab737f4aacfae3fe73776f40e989190a7cavboxsyncstatic int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic void pgmPoolMonitorModifiedRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncDECLEXPORT(int) pgmPoolAccessHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser);
2daaccf68be3773aee600c5c3e48bcf5401418a6vboxsyncstatic const char *pgmPoolPoolKindToStr(uint8_t enmKind);
7666082b743c5e146a8cee6cc794ff4bc3fd0ffdvboxsyncvoid pgmPoolTrackFlushGCPhysPT(PVM pVM, PPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs);
7666082b743c5e146a8cee6cc794ff4bc3fd0ffdvboxsyncvoid pgmPoolTrackFlushGCPhysPTs(PVM pVM, PPGMPAGE pPhysPage, uint16_t iPhysExt);
7666082b743c5e146a8cee6cc794ff4bc3fd0ffdvboxsyncint pgmPoolTrackFlushGCPhysPTsSlow(PVM pVM, PPGMPAGE pPhysPage);
7666082b743c5e146a8cee6cc794ff4bc3fd0ffdvboxsyncPPGMPOOLPHYSEXT pgmPoolTrackPhysExtAlloc(PVM pVM, uint16_t *piPhysExt);
7666082b743c5e146a8cee6cc794ff4bc3fd0ffdvboxsyncvoid pgmPoolTrackPhysExtFree(PVM pVM, uint16_t iPhysExt);
7666082b743c5e146a8cee6cc794ff4bc3fd0ffdvboxsyncvoid 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)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return true;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync 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. */
7b80828e5760a8814fe6cd494d2715a4544fbddcvboxsync# 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)
c17f5c90f2cb60b38ecabebce128724c6ff2d036vboxsync# define PGMPOOL_UNLOCK_PTR(pVM, pPage) do {} while (0)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Determin the size of a write instruction.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns number of bytes written.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pDis The disassembler state.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic unsigned pgmPoolDisasWriteSize(PDISCPUSTATE pDis)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * 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.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage A page in the chain.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncint pgmPoolMonitorChainFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
b1c3cdef473df2fbc621d5da81acc82dbfb8a11avboxsync LogFlow(("pgmPoolMonitorChainFlush: Flush page %RGp type=%d\n", pPage->GCPhys, pPage->enmKind));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Find the list head.
3ecf9412133496b2aeb090cfd33a286404ec59fbvboxsync * Iterate the list flushing each shadow page.
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsync * Wrapper for getting the current context pointer to the entry being modified.
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync * @returns VBox status code suitable for scheduling.
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync * @param pVM VM Handle.
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync * @param pvDst Destination address
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync * @param pvSrc Source guest virtual address.
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync * @param GCPhysSrc The source guest physical address.
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync * @param cb Size of data to read
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsyncDECLINLINE(int) pgmPoolPhysSimpleReadGCPhys(PVM pVM, void *pvDst, CTXTYPE(RTGCPTR, RTHCPTR, RTGCPTR) pvSrc, RTGCPHYS GCPhysSrc, size_t cb)
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync memcpy(pvDst, (RTHCPTR)((uintptr_t)pvSrc & ~(RTHCUINTPTR)(cb - 1)), cb);
72ef2b9fc5ffc01d0dabd5052d6e8baa3a952773vboxsync /* @todo in RC we could attempt to use the virtual address, although this can cause many faults (PAE Windows XP guest). */
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync return PGMPhysSimpleReadGCPhys(pVM, pvDst, GCPhysSrc & ~(RTGCPHYS)(cb - 1), cb);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Process shadow entries before they are changed by the guest.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * 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.
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync * @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 pCpu 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.
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsyncvoid pgmPoolMonitorChainChanging(PVMCPU pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault, CTXTYPE(RTGCPTR, RTHCPTR, RTGCPTR) pvAddress, PDISCPUSTATE pCpu)
e08de24d4792d31b7f2aac29db5cb8840d940009vboxsync const unsigned off = GCPhysFault & PAGE_OFFSET_MASK;
9782b553bdb12385214a3ac596aff1476bcb7cbdvboxsync const unsigned cbWrite = (pCpu) ? pgmPoolDisasWriteSize(pCpu) : 0;
e50404712a2b5234c42bdf9740bddab5729ba188vboxsync LogFlow(("pgmPoolMonitorChainChanging: %RGv phys=%RGp kind=%s cbWrite=%d\n", (RTGCPTR)pvAddress, GCPhysFault, pgmPoolPoolKindToStr(pPage->enmKind), cbWrite));
247b55faa8d054157f2481e68caca36f4dc9542cvboxsync LogFlow(("PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT iShw=%x\n", iShw));
22e281e75ed636601178296c6daebda8f1d17c59vboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress, GCPhysFault, sizeof(GstPte));
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync 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 const unsigned iShw = (off / sizeof(X86PTE)) & (X86_PG_PAE_ENTRIES - 1);
247b55faa8d054157f2481e68caca36f4dc9542cvboxsync LogFlow(("PGMPOOLKIND_PAE_PT_FOR_32BIT_PT iShw=%x\n", iShw));
22e281e75ed636601178296c6daebda8f1d17c59vboxsync 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));
e50404712a2b5234c42bdf9740bddab5729ba188vboxsync 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));
57399ab65e2825c324fb9dcb4642d4ae2c232509vboxsync if (iShwPdpt == pPage->enmKind - (unsigned)PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD)
57399ab65e2825c324fb9dcb4642d4ae2c232509vboxsync for (unsigned i = 0; i < 2; i++)
1843553dbdf4e46417158b4c6348c503adf10740vboxsync if ((uShw.pPDPae->a[iShw + i].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
1843553dbdf4e46417158b4c6348c503adf10740vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShwPdpt=%#x iShw=%#x!\n", iShwPdpt, iShw+i));
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync# endif /* !IN_RING0 */
1843553dbdf4e46417158b4c6348c503adf10740vboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw+i, uShw.pPDPae->a[iShw+i].u));
1843553dbdf4e46417158b4c6348c503adf10740vboxsync /* paranoia / a bit assumptive. */
1843553dbdf4e46417158b4c6348c503adf10740vboxsync if ((uShw.pPDPae->a[iShw2].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
1843553dbdf4e46417158b4c6348c503adf10740vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShwPdpt=%#x iShw2=%#x!\n", iShwPdpt, iShw2));
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync# endif /* !IN_RING0 */
1843553dbdf4e46417158b4c6348c503adf10740vboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
22e281e75ed636601178296c6daebda8f1d17c59vboxsync int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress, GCPhysFault, sizeof(GstPte));
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync 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));
0e77737b0ba913683e614db11463b31ca67aacbevboxsync /* paranoia / a bit assumptive. */
e08de24d4792d31b7f2aac29db5cb8840d940009vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PTEPAE);
22e281e75ed636601178296c6daebda8f1d17c59vboxsync 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));
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync 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));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync const unsigned iShw = off / sizeof(X86PTE); // ASSUMING 32-bit guest paging!
8e972b677df5ee27b99211fc7e456a5aa50f3e68vboxsync LogFlow(("pgmPoolMonitorChainChanging: PGMPOOLKIND_32BIT_PD %x\n", iShw));
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync# endif /* !IN_RING0 */
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync# endif /* !IN_RING0 */
806d0b554daa555364af5f87bc96eccbe760db7avboxsync LogFlow(("pgmPoolMonitorChainChanging: 32 bit pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPD->a[iShw].u));
d98e61ba075ed7d0b567a5d884bc85d643fe3de7vboxsync /* paranoia / a bit assumptive. */
e08de24d4792d31b7f2aac29db5cb8840d940009vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PTE);
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
806d0b554daa555364af5f87bc96eccbe760db7avboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync# endif /* !IN_RING0 */
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync# endif /* !IN_RING0 */
806d0b554daa555364af5f87bc96eccbe760db7avboxsync LogFlow(("pgmPoolMonitorChainChanging: 32 bit pd iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPD->a[iShw2].u));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync#if 0 /* useful when running PGMAssertCR3(), a bit too troublesome for general use (TLBs). */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolMonitorChainChanging: iShw=%#x: %RX32 -> freeing it!\n", iShw, uShw.pPD->a[iShw].u));
7e960d3a0a8a3a84d7aba2cca45d72b1c31cc97bvboxsync# ifdef IN_RC /* TLB load - we're pushing things a bit... */
22e281e75ed636601178296c6daebda8f1d17c59vboxsync pgmPoolFree(pVM, uShw.pPD->a[iShw].u & X86_PDE_PG_MASK, pPage->idx, iShw);
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
79b24ef0ab7cd4a03a3571b3954c52ab8b573137vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync#endif /* !IN_RING0 */
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync * Causes trouble when the guest uses a PDE to refer to the whole page table level
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync * structure. (Invalidate here; faults later on when it tries to change the page
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync * table entries -> recheck; probably only applies to the RC case.)
1157941288c9533575a655c660af55d13da31eefvboxsync# endif /* !IN_RING0 */
5e797edc29f96c8367de4fbf5874171c24a89ba7vboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
79b24ef0ab7cd4a03a3571b3954c52ab8b573137vboxsync /* paranoia / a bit assumptive. */
e08de24d4792d31b7f2aac29db5cb8840d940009vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDEPAE);
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
79b24ef0ab7cd4a03a3571b3954c52ab8b573137vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync#endif /* !IN_RING0 */
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync# endif /* !IN_RING0 */
3dde2f85d4cf477621a3128887a2c08a8bca7c01vboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync * Hopefully this doesn't happen very often:
7082d29724f6c3788977a51591b0379fd3acbf72vboxsync * - touching unused parts of the page
7082d29724f6c3788977a51591b0379fd3acbf72vboxsync * - messing with the bits of pd pointers without changing the physical address
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync /* PDPT roots are not page aligned; 32 byte only! */
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync const unsigned offPdpt = GCPhysFault - pPage->GCPhys;
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync if (iShw < X86_PG_PAE_PDPE_ENTRIES) /* don't use RT_ELEMENTS(uShw.pPDPT->a), because that's for long mode only */
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
5cf54b3ffeb7ee90685dcaec329ef71a729f5947vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected pdpt conflict at iShw=%#x!\n", iShw));
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync# endif /* !IN_RING0 */
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync# endif /* !IN_RING0 */
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPT->a[iShw].u));
f9147fe1eaa4e35287f8f39282c7f92f0d7de0b7vboxsync /* paranoia / a bit assumptive. */
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync const unsigned iShw2 = (offPdpt + cbWrite - 1) / sizeof(X86PDPE);
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync# endif /* !IN_RING0 */
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync# endif /* !IN_RING0 */
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPT->a[iShw2].u));
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync Assert(!(uShw.pPDPae->a[iShw].u & PGM_PDFLAGS_MAPPING));
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
65b61798a61dd4c32cce448db1dac70bba8d5cf5vboxsync /* paranoia / a bit assumptive. */
e08de24d4792d31b7f2aac29db5cb8840d940009vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDEPAE);
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync Assert(!(uShw.pPDPae->a[iShw2].u & PGM_PDFLAGS_MAPPING));
65b61798a61dd4c32cce448db1dac70bba8d5cf5vboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync * Hopefully this doesn't happen very often:
46df4404c8dbbf3672e7aae8cd0b2770356e5b73vboxsync * - messing with the bits of pd pointers without changing the physical address
3dde2f85d4cf477621a3128887a2c08a8bca7c01vboxsync LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPT->a[iShw].u));
22e281e75ed636601178296c6daebda8f1d17c59vboxsync pgmPoolFree(pVM, uShw.pPDPT->a[iShw].u & X86_PDPE_PG_MASK, pPage->idx, iShw);
3dde2f85d4cf477621a3128887a2c08a8bca7c01vboxsync /* paranoia / a bit assumptive. */
e08de24d4792d31b7f2aac29db5cb8840d940009vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDPE);
3dde2f85d4cf477621a3128887a2c08a8bca7c01vboxsync LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPT->a[iShw2].u));
22e281e75ed636601178296c6daebda8f1d17c59vboxsync pgmPoolFree(pVM, uShw.pPDPT->a[iShw2].u & X86_PDPE_PG_MASK, pPage->idx, iShw2);
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync * Hopefully this doesn't happen very often:
585f64d6f624f9e683321dabeb21b0eb2e6aa473vboxsync * - messing with the bits of pd pointers without changing the physical address
585f64d6f624f9e683321dabeb21b0eb2e6aa473vboxsync LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPML4->a[iShw].u));
22e281e75ed636601178296c6daebda8f1d17c59vboxsync pgmPoolFree(pVM, uShw.pPML4->a[iShw].u & X86_PML4E_PG_MASK, pPage->idx, iShw);
585f64d6f624f9e683321dabeb21b0eb2e6aa473vboxsync /* paranoia / a bit assumptive. */
e08de24d4792d31b7f2aac29db5cb8840d940009vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PML4E);
585f64d6f624f9e683321dabeb21b0eb2e6aa473vboxsync LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPML4->a[iShw2].u));
22e281e75ed636601178296c6daebda8f1d17c59vboxsync pgmPoolFree(pVM, uShw.pPML4->a[iShw2].u & X86_PML4E_PG_MASK, pPage->idx, iShw2);
65b61798a61dd4c32cce448db1dac70bba8d5cf5vboxsync#endif /* IN_RING0 */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertFatalMsgFailed(("enmKind=%d\n", pPage->enmKind));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Checks if a access could be a fork operation in progress.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Meaning, that the guest is setuping up the parent process for Copy-On-Write.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns true if it's likly that we're forking, otherwise false.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pCpu The disassembled instruction.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param offFault The access offset.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncDECLINLINE(bool) pgmPoolMonitorIsForking(PPGMPOOL pPool, PDISCPUSTATE pCpu, unsigned offFault)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * i386 linux is using btr to clear X86_PTE_RW.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * The functions involved are (2.6.16 source inspection):
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * clear_bit
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * ptep_set_wrprotect
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * copy_one_pte
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * copy_pte_range
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * copy_pmd_range
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * copy_pud_range
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * copy_page_range
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * copy_process
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /** @todo Validate that the bit index is X86_PTE_RW. */
5b6e2c9a765c3c72295acc15791af8a700746956vboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,Fork));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return true;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return false;
e9a217d585085a6a6d129d27ca0d96a1b8e6d0eevboxsync * Determine whether the page is likely to have been reused.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns true if we consider the page as being reused for a different purpose.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns false if we consider it to still be a paging page.
e17bd6c32a8dd64f2d42838f9028216465e2caf0vboxsync * @param pVM VM Handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The page in question.
e9a217d585085a6a6d129d27ca0d96a1b8e6d0eevboxsync * @param pRegFrame Trap register frame.
e17bd6c32a8dd64f2d42838f9028216465e2caf0vboxsync * @param pCpu The disassembly info for the faulting instruction.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pvFault The fault address.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @remark The REP prefix check is left to the caller because of STOSD/W.
e17bd6c32a8dd64f2d42838f9028216465e2caf0vboxsyncDECLINLINE(bool) pgmPoolMonitorIsReused(PVM pVM, PPGMPOOLPAGE pPage, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu, RTGCPTR pvFault)
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync /** @todo could make this general, faulting close to rsp should be safe reuse heuristic. */
e17bd6c32a8dd64f2d42838f9028216465e2caf0vboxsync /* Fault caused by stack writes while trying to inject an interrupt event. */
08bc90fc2848c80bf8270bedc883745b8398e186vboxsync Log(("pgmPoolMonitorIsReused: reused %RGv for interrupt stack (rsp=%RGv).\n", pvFault, pRegFrame->rsp));
e17bd6c32a8dd64f2d42838f9028216465e2caf0vboxsync return true;
49748bb305bd71f672cd083af208f4bb08c5d6abvboxsync /* call implies the actual push of the return address faulted */
49748bb305bd71f672cd083af208f4bb08c5d6abvboxsync return true;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return true;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return true;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return true;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return true;
b9ca93dd1ad44cb8b27679dc5624be2f7b7f7af5vboxsync return true;
b9ca93dd1ad44cb8b27679dc5624be2f7b7f7af5vboxsync case OP_MOVNTDQ: /* solaris - hwblkclr & hwblkpagecopy */
b9ca93dd1ad44cb8b27679dc5624be2f7b7f7af5vboxsync return true;
e9a217d585085a6a6d129d27ca0d96a1b8e6d0eevboxsync return true;
e9a217d585085a6a6d129d27ca0d96a1b8e6d0eevboxsync return false;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return true;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync //if (pPage->fCR3Mix)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync // return false;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return false;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Flushes the page being accessed.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code suitable for scheduling.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync * @param pVCpu The VMCPU handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The pool page (head).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pCpu 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.
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsyncstatic int pgmPoolAccessHandlerFlush(PVM pVM, PVMCPU pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * First, do the flushing.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Emulate the instruction (xp/w2k problem, requires pc/cr2/sp detection).
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync int rc2 = EMInterpretInstructionCPU(pVM, pVCpu, pCpu, pRegFrame, pvFault, &cbWritten);
3964eef78ab9593263a3a982e26216d4d166869cvboxsync if (PATMIsPatchGCAddr(pVM, (RTRCPTR)pRegFrame->eip))
f02b41a7e54fc4e6b714f4e60260d94614d6e2e7vboxsync LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for patch code %04x:%RGv, ignoring.\n",
5b6e2c9a765c3c72295acc15791af8a700746956vboxsync STAM_COUNTER_INC(&pPool->StatMonitorRZIntrFailPatch2);
5b6e2c9a765c3c72295acc15791af8a700746956vboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,EmulateInstr));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* See use in pgmPoolAccessHandlerSimple(). */
b3547e42ce5221377866e3fa041b3086b7cc1562vboxsync 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 pCpu 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 pCpu,
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Increment the modification counter and insert it into the list
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * of modified pages the first time.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Execute REP STOSD.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This ASSUMES that we're not invoked by Trap0e on in a out-of-sync
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * write situation, meaning that it's safe to write here.
3241f1be564f7351b07ce8a807673fa77a7847bcvboxsync uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, (RTGCPTR)pu32, NULL);
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, (RTGCPTR)pu32, NULL);
eb90d5a431eee79842fb6c676c05aa9ca51ca6a9vboxsync PGMPhysSimpleWriteGCPhys(pVM, GCPhysFault, &pRegFrame->eax, 4);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* See use in pgmPoolAccessHandlerSimple(). */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Handles the simple write accesses.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code suitable for scheduling.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync * @param pVCpu The VMCPU handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The pool page (head).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pCpu 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.
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsyncDECLINLINE(int) pgmPoolAccessHandlerSimple(PVM pVM, PVMCPU pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Increment the modification counter and insert it into the list
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * of modified pages the first time.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear all the pages. ASSUMES that pvFault is readable.
3241f1be564f7351b07ce8a807673fa77a7847bcvboxsync uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, pvFault, pCpu);
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, pvFault, pCpu);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Interpret the instruction.
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync int rc = EMInterpretInstructionCPU(pVM, pVCpu, pCpu, pRegFrame, pvFault, &cb);
3e6d3b0af632bdcd931b5149915c7b8be1a732cdvboxsync LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for %04x:%RGv - opcode=%d\n",
47b5427d1a541bcd269bc625c35b19d849071edfvboxsync pRegFrame->cs, (RTGCPTR)pRegFrame->rip, pCpu->pCurInstr->opcode));
5b6e2c9a765c3c72295acc15791af8a700746956vboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,EmulateInstr));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Quick hack, with logging enabled we're getting stale
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * code TLBs but no data TLB for EIP and crash in EMInterpretDisasOne.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * 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!
b3547e42ce5221377866e3fa041b3086b7cc1562vboxsync 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.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param uErrorCode CPU Error code.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pRegFrame Trap register frame.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * NULL on DMA and other non CPU access.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @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)
5b6e2c9a765c3c72295acc15791af8a700746956vboxsync STAM_PROFILE_START(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), a);
b1c3cdef473df2fbc621d5da81acc82dbfb8a11avboxsync LogFlow(("pgmPoolAccessHandler: pvFault=%RGv pPage=%p:{.idx=%d} GCPhysFault=%RGp\n", pvFault, pPage, pPage->idx, GCPhysFault));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * We should ALWAYS have the list head as user parameter. This
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * is because we use that page to record the changes.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Disassemble the faulting instruction.
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync int rc = EMInterpretDisasOne(pVM, pVCpu, pRegFrame, &Cpu, NULL);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Check if it's worth dealing with.
0f5d1b2abd9e82c7ee46f1327287c44856604bcbvboxsync bool fReused = false;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if ( ( pPage->cModifications < 48 /** @todo #define */ /** @todo need to check that it's not mapping EIP. */ /** @todo adjust this! */
e17bd6c32a8dd64f2d42838f9028216465e2caf0vboxsync && !(fReused = pgmPoolMonitorIsReused(pVM, pPage, pRegFrame, &Cpu, pvFault))
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync && !pgmPoolMonitorIsForking(pPool, &Cpu, GCPhysFault & PAGE_OFFSET_MASK))
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Simple instructions, no REP prefix.
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync rc = pgmPoolAccessHandlerSimple(pVM, pVCpu, pPool, pPage, &Cpu, pRegFrame, GCPhysFault, pvFault);
5b6e2c9a765c3c72295acc15791af8a700746956vboxsync 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.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync && pRegFrame->ecx * 4 <= PAGE_SIZE - ((uintptr_t)pvFault & PAGE_OFFSET_MASK)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync && (pRegFrame->eax == 0 || pRegFrame->eax == 0x80) /* the two values observed. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = pgmPoolAccessHandlerSTOSD(pVM, pPool, pPage, &Cpu, pRegFrame, GCPhysFault, pvFault);
5b6e2c9a765c3c72295acc15791af8a700746956vboxsync 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. */
5b6e2c9a765c3c72295acc15791af8a700746956vboxsync STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,RepPrefix));
08bc90fc2848c80bf8270bedc883745b8398e186vboxsync Log4(("pgmPoolAccessHandler: eax=%#x ecx=%#x edi=%#x esi=%#x rip=%RGv opcode=%d prefix=%#x\n",
79b07d7ae28b916539c6397e010a5cc05e300551vboxsync pRegFrame->eax, pRegFrame->ecx, pRegFrame->edi, pRegFrame->esi, (RTGCPTR)pRegFrame->rip, Cpu.pCurInstr->opcode, Cpu.prefix));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Not worth it, so flush it.
bc8991845b5bd7e98758795748d3d76408756793vboxsync * If we considered it to be reused, don't go back to ring-3
c20b837b2c912dd4dcaaa676e15acd2b230108f0vboxsync * to emulate failed instructions since we usually cannot
0f5d1b2abd9e82c7ee46f1327287c44856604bcbvboxsync * interpret then. This may be a bit risky, in which case
0f5d1b2abd9e82c7ee46f1327287c44856604bcbvboxsync * the reuse detection must be fixed.
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync rc = pgmPoolAccessHandlerFlush(pVM, pVCpu, pPool, pPage, &Cpu, pRegFrame, GCPhysFault, pvFault);
5b6e2c9a765c3c72295acc15791af8a700746956vboxsync 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#endif /* PGMPOOL_WITH_MONITORING */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Inserts a page into the GCPhys hash table.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncDECLINLINE(void) pgmPoolHashInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
b1c3cdef473df2fbc621d5da81acc82dbfb8a11avboxsync Log3(("pgmPoolHashInsert: %RGp\n", pPage->GCPhys));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(pPage->GCPhys != NIL_RTGCPHYS); Assert(pPage->iNext == NIL_PGMPOOL_IDX);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Removes a page from the GCPhys hash table.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncDECLINLINE(void) pgmPoolHashRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
b1c3cdef473df2fbc621d5da81acc82dbfb8a11avboxsync Log3(("pgmPoolHashRemove: %RGp\n", pPage->GCPhys));
b1c3cdef473df2fbc621d5da81acc82dbfb8a11avboxsync 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.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @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.
1ce20e84ae29a712029d1e213740388d7e158d5cvboxsync/* This is the alternative to the SyncCR3 pgmPoolCacheUsed calls.
1ce20e84ae29a712029d1e213740388d7e158d5cvboxsync if (pPool->aPages[iToFree].iUserHead != NIL_PGMPOOL_USER_INDEX)
1ce20e84ae29a712029d1e213740388d7e158d5cvboxsync uint16_t i = pPool->aPages[iToFree].iAgePrev;
1ce20e84ae29a712029d1e213740388d7e158d5cvboxsync for (unsigned j = 0; j < 10 && i != NIL_PGMPOOL_USER_INDEX; j++, i = pPool->aPages[i].iAgePrev)
1ce20e84ae29a712029d1e213740388d7e158d5cvboxsync if (pPool->aPages[iToFree].iUserHead == NIL_PGMPOOL_USER_INDEX)
1ce20e84ae29a712029d1e213740388d7e158d5cvboxsync iToFree = i;
1ce20e84ae29a712029d1e213740388d7e158d5cvboxsync * Reject any attempts at flushing the currently active shadow CR3 mapping.
1ce20e84ae29a712029d1e213740388d7e158d5cvboxsync * Call pgmPoolCacheUsed to move the page to the head of the age list.
1ce20e84ae29a712029d1e213740388d7e158d5cvboxsync if (!pgmPoolIsPageLocked(&pPool->CTX_SUFF(pVM)->pgm.s, pPage))
1ce20e84ae29a712029d1e213740388d7e158d5cvboxsync LogFlow(("pgmPoolCacheFreeOne: refuse CR3 mapping\n"));
1ce20e84ae29a712029d1e213740388d7e158d5cvboxsync AssertLogRelReturn(iLoop < 8192, VERR_INTERNAL_ERROR);
1ce20e84ae29a712029d1e213740388d7e158d5cvboxsync * Found a usable page, flush it and return.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Checks if a kind mismatch is really a page being reused
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * or if it's just normal remappings.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns true if reused and the cached page (enmKind1) should be flushed
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns false if not reused.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param enmKind1 The kind of the cached page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param enmKind2 The kind of the requested page.
223cf005b18af2c21352a70693ebaf0582f68ebcvboxsyncstatic bool pgmPoolCacheReusedByKind(PGMPOOLKIND enmKind1, PGMPOOLKIND enmKind2)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Never reuse them. There is no remapping in non-paging mode.
b55721e255f55546462b62c2d09259bb7bf8ef90vboxsync case PGMPOOLKIND_PAE_PDPT_FOR_32BIT: /* never reuse them for other types */
e85d76a7e5a047db3cdc8576ff5f412c7b73bbabvboxsync return false;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * 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 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.
afed5ab737f4aacfae3fe73776f40e989190a7cavboxsyncstatic int pgmPoolCacheAlloc(PPGMPOOL pPool, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Look up the GCPhys in the hash.
e50404712a2b5234c42bdf9740bddab5729ba188vboxsync Log3(("pgmPoolCacheAlloc: %RGp kind %s iUser=%x iUserTable=%x SLOT=%d\n", GCPhys, pgmPoolPoolKindToStr(enmKind), iUser, iUserTable, i));
fa7355597d7eba5b4ca8a23056ebe1b8fb306fdbvboxsync Log4(("pgmPoolCacheAlloc: slot %d found page %RGp\n", i, pPage->GCPhys));
57399ab65e2825c324fb9dcb4642d4ae2c232509vboxsync /* Put it at the start of the use list to make sure pgmPoolTrackAddUser
3a21cbe769e7500039a2d17c794a911f7acb2dadvboxsync * doesn't flush it in case there are no more free use records.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = pgmPoolTrackAddUser(pPool, pPage, iUser, iUserTable);
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.
223cf005b18af2c21352a70693ebaf0582f68ebcvboxsync if (pgmPoolCacheReusedByKind((PGMPOOLKIND)pPage->enmKind, enmKind))
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync } while (i != NIL_PGMPOOL_IDX);
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync 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.
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync Log3(("pgmPoolCacheInsert: Caching %p:{.Core=%RHp, .idx=%d, .enmKind=%s, GCPhys=%RGp}\n",
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), pPage->GCPhys));
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync Log3(("pgmPoolCacheInsert: Not caching %p:{.Core=%RHp, .idx=%d, .enmKind=%s, GCPhys=%RGp}\n",
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync 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)
b1c3cdef473df2fbc621d5da81acc82dbfb8a11avboxsync 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;
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync#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)
b1c3cdef473df2fbc621d5da81acc82dbfb8a11avboxsync LogFlow(("pgmPoolMonitorInsert %RGp\n", pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1)));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Filter out the relevant kinds.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Nothing to monitor here. */
a3db4ff1e0aa7f94c9176ef16264df572eec0aebvboxsync /* Nothing to monitor here. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Install handler.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PPGMPOOLPAGE pPageHead = pgmPoolMonitorGetPageByGCPhys(pPool, pPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(pPageHead != pPage); Assert(pPageHead->iMonitoredNext != pPage->idx);
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),
09d4e754a010196fe0d9d972b6ccc005ecb8b116vboxsync 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. */
6ce4679e84cffad4a2867786f94632e1eb935b9dvboxsync AssertFatalMsgRC(rc, ("PGMHandlerPhysicalRegisterEx %RGp failed with %Rrc\n", GCPhysPage, rc));
e0b9d3c357adf9b7d05f55540e86f22943fc4b23vboxsync Assert(!(pVM->pgm.s.fGlobalSyncFlags & PGM_GLOBAL_SYNC_CLEAR_PGM_POOL) || VMCPU_FF_ISSET(VMMGetCpu(pVM), VMCPU_FF_PGM_SYNC_CR3));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Disables 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 pgmPoolMonitorFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * 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),
09d4e754a010196fe0d9d972b6ccc005ecb8b116vboxsync 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));
e0b9d3c357adf9b7d05f55540e86f22943fc4b23vboxsync AssertMsg(!(pVM->pgm.s.fGlobalSyncFlags & PGM_GLOBAL_SYNC_CLEAR_PGM_POOL) || VMCPU_FF_ISSET(VMMGetCpu(pVM), VMCPU_FF_PGM_SYNC_CR3),
e0b9d3c357adf9b7d05f55540e86f22943fc4b23vboxsync ("%#x %#x\n", pVM->pgm.s.fGlobalSyncFlags, pVM->fGlobalForcedActions));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Remove it from the list of modified pages (if in it).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * 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,
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync 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));
bc8991845b5bd7e98758795748d3d76408756793vboxsync * Callback to clear all shadow pages and clear all modification counters.
bc8991845b5bd7e98758795748d3d76408756793vboxsync * @returns VBox status code.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
bc8991845b5bd7e98758795748d3d76408756793vboxsync * @param pvUser Unused parameter
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @remark Should only be used when monitoring is available, thus placed in
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * the PGMPOOL_WITH_MONITORING #ifdef.
bc8991845b5bd7e98758795748d3d76408756793vboxsyncDECLCALLBACK(int) pgmPoolClearAll(PVM pVM, void *pvUser)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolClearAll: cUsedPages=%d\n", pPool->cUsedPages));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Iterate all the pages until we've encountered all that in use.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This is simple but not quite optimal solution.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync unsigned cModifiedPages = 0; NOREF(cModifiedPages);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * We only care about shadow page tables.
6b022885f2cb6a55167609edecd89570cd80001dvboxsync void *pvShw = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* fall thru */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(!pPage->cModifications || ++cModifiedPages);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(pPage->iModifiedNext == NIL_PGMPOOL_IDX || pPage->cModifications);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX || pPage->cModifications);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* swipe the special pages too. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (iPage = PGMPOOL_IDX_FIRST_SPECIAL; iPage < PGMPOOL_IDX_FIRST; iPage++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(!pPage->cModifications || ++cModifiedPages);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(pPage->iModifiedNext == NIL_PGMPOOL_IDX || pPage->cModifications);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX || pPage->cModifications);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertMsg(cModifiedPages == pPool->cModifiedPages, ("%d != %d\n", cModifiedPages, pPool->cModifiedPages));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear all the GCPhys links and rebuild the phys ext free list.
6b022885f2cb6a55167609edecd89570cd80001dvboxsync for (PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync while (iPage-- > 0)
6b022885f2cb6a55167609edecd89570cd80001dvboxsync PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = 0; i < cMaxPhysExts; i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync paPhysExts[cMaxPhysExts - 1].iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
06de36c1229f14000638fa27993b13c9133e85f5vboxsync#endif /* IN_RING3 */
5dda7f07dab8a954e6c4cf2378b15f921e60d9aavboxsync * Handle SyncCR3 pool tasks
5dda7f07dab8a954e6c4cf2378b15f921e60d9aavboxsync * @returns VBox status code.
5dda7f07dab8a954e6c4cf2378b15f921e60d9aavboxsync * @retval VINF_SUCCESS if successfully added.
5dda7f07dab8a954e6c4cf2378b15f921e60d9aavboxsync * @retval VINF_PGM_SYNC_CR3 is it needs to be deferred to ring 3 (GC only)
5dda7f07dab8a954e6c4cf2378b15f921e60d9aavboxsync * @param pVM The VM handle.
5dda7f07dab8a954e6c4cf2378b15f921e60d9aavboxsync * @remark Should only be used when monitoring is available, thus placed in
5dda7f07dab8a954e6c4cf2378b15f921e60d9aavboxsync * the PGMPOOL_WITH_MONITORING #ifdef.
5dda7f07dab8a954e6c4cf2378b15f921e60d9aavboxsync * When monitoring shadowed pages, we reset the modification counters on CR3 sync.
5dda7f07dab8a954e6c4cf2378b15f921e60d9aavboxsync * Occasionally we will have to clear all the shadow page tables because we wanted
5dda7f07dab8a954e6c4cf2378b15f921e60d9aavboxsync * to monitor a page which was mapped by too many shadowed page tables. This operation
5dda7f07dab8a954e6c4cf2378b15f921e60d9aavboxsync * sometimes refered to as a 'lightweight flush'.
26824086a3f6b36cd911058f1d9b4c0b944706fbvboxsync# ifdef IN_RING3 /* Don't flush in ring-0 or raw mode, it's taking too long. */
011dbe77c1ead943be7659a741ff712dc6eb5637vboxsync if (ASMBitTestAndClear(&pVM->pgm.s.fGlobalSyncFlags, PGM_GLOBAL_SYNC_CLEAR_PGM_POOL_BIT))
bc8991845b5bd7e98758795748d3d76408756793vboxsync VMMR3AtomicExecuteHandler(pVM, pgmPoolClearAll, NULL);
06de36c1229f14000638fa27993b13c9133e85f5vboxsync# else /* !IN_RING3 */
9a99bb9ca6c5bc8ef1977e01e13b05e5cc58319bvboxsync if (pVM->pgm.s.fGlobalSyncFlags & PGM_GLOBAL_SYNC_CLEAR_PGM_POOL)
37dd732eb95fb61d5baceacad99de39038cde38bvboxsync LogFlow(("SyncCR3: PGM_GLOBAL_SYNC_CLEAR_PGM_POOL is set -> VINF_PGM_SYNC_CR3\n"));
e0b9d3c357adf9b7d05f55540e86f22943fc4b23vboxsync VMCPU_FF_SET(VMMGetCpu(pVM), VMCPU_FF_PGM_SYNC_CR3); /** @todo no need to do global sync, right? */
06de36c1229f14000638fa27993b13c9133e85f5vboxsync# endif /* !IN_RING3 */
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync#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.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iUser The user index.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic int pgmPoolTrackFreeOneUser(PPGMPOOL pPool, uint16_t iUser)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * 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.
750d4d0506a38b2e80c997075d40aad474e675fbvboxsync /* @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.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iUserTable The user table index.
afed5ab737f4aacfae3fe73776f40e989190a7cavboxsyncDECLINLINE(int) pgmPoolTrackInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhys, uint16_t iUser, uint32_t iUserTable)
2c080f6fc6ab70995d6390eeaa199d590c1a34d5vboxsync LogFlow(("pgmPoolTrackInsert GCPhys=%RGp iUser %x iUserTable %x\n", GCPhys, iUser, iUserTable));
32f98f3d0fb00168315b4ece1f0808df4cd88248vboxsync * Check that the entry doesn't already exists.
32f98f3d0fb00168315b4ece1f0808df4cd88248vboxsync AssertMsg(paUsers[i].iUser != iUser || paUsers[i].iUserTable != iUserTable, ("%x %x vs new %x %x\n", paUsers[i].iUser, paUsers[i].iUserTable, iUser, iUserTable));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Find free a user node.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Unlink the user node from the free list,
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * initialize and insert it into the user list.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Insert into cache and enable monitoring of the guest page if enabled.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Until we implement caching of all levels, including the CR3 one, we'll
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * have to make sure we don't try monitor & cache any recursive reuse of
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * a monitored CR3 page. Because all windows versions are doing this we'll
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * have to be able to do combined access monitoring, CR3 + PT and
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * 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;
6b022885f2cb6a55167609edecd89570cd80001dvboxsync bool fCanBeMonitored = pPool->CTX_SUFF(pVM)->pgm.s.GCPhysGstCR3Monitored == NIL_RTGCPHYS
6b022885f2cb6a55167609edecd89570cd80001dvboxsync || (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 */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync# ifdef PGMPOOL_WITH_CACHE /* (only used when the cache is enabled.) */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Adds a user reference to a page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This will move the page to the head of the
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 iUser The user index.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iUserTable The user table.
afed5ab737f4aacfae3fe73776f40e989190a7cavboxsyncstatic int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
907da3b41f191bef4c8eb94accb9d16fcbe77019vboxsync Log3(("pgmPoolTrackAddUser GCPhys = %RGp iUser %x iUserTable %x\n", pPage->GCPhys, iUser, iUserTable));
689e03ca2c98ca6feaab8d9de6e4687a98f14ccbvboxsync * Check that the entry doesn't already exists. We only allow multiple users of top-level paging structures (SHW_POOL_ROOT_IDX).
689e03ca2c98ca6feaab8d9de6e4687a98f14ccbvboxsync AssertMsg(iUser != PGMPOOL_IDX_PD || iUser != PGMPOOL_IDX_PDPT || iUser != PGMPOOL_IDX_NESTED_ROOT || iUser != PGMPOOL_IDX_AMD64_CR3 ||
689e03ca2c98ca6feaab8d9de6e4687a98f14ccbvboxsync 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 * 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).
afed5ab737f4aacfae3fe73776f40e989190a7cavboxsyncstatic void pgmPoolTrackFreeUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Unlink and free the specified user entry.
2f1b3d7474489e653b75edf9492132a0f56aab4dvboxsync Log3(("pgmPoolTrackFreeUser %RGp %x %x\n", pPage->GCPhys, iUser, iUserTable));
ab1239d8445aa1fd4afddb7d0c8b90bbdc742010vboxsync /* Special: For PAE and 32-bit paging, there is usually no more than one user. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* General: Linear search. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Fatal: didn't find it */
b1c3cdef473df2fbc621d5da81acc82dbfb8a11avboxsync AssertFatalMsgFailed(("Didn't find the user entry! iUser=%#x iUserTable=%#x GCPhys=%RGp\n",
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Gets the entry size of a shadow table.
19b725c530eb49600728765de7ed451cbe290740vboxsync * @param enmKind The kind of page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns The size of the entry in bytes. That is, 4 or 8.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns If the kind is not for a table, an assertion is raised and 0 is
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * returned.
223cf005b18af2c21352a70693ebaf0582f68ebcvboxsyncDECLINLINE(unsigned) pgmPoolTrackGetShadowEntrySize(PGMPOOLKIND enmKind)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Gets the entry size of a guest table.
19b725c530eb49600728765de7ed451cbe290740vboxsync * @param enmKind The kind of page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns The size of the entry in bytes. That is, 0, 4 or 8.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns If the kind is not for a table, an assertion is raised and 0 is
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * returned.
223cf005b18af2c21352a70693ebaf0582f68ebcvboxsyncDECLINLINE(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 * @param pVM The VM handle.
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsync * @param pPhysPage The guest page in question.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iShw The shadow page table.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param cRefs The number of references made in that PT.
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsyncstatic void pgmPoolTrackFlushGCPhysPTInt(PVM pVM, PCPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs)
bee80f3f77b15c166b7debd080a70e5231ba6751vboxsync LogFlow(("pgmPoolTrackFlushGCPhysPT: pPhysPage=%R[pgmpage] iShw=%d cRefs=%d\n", pPhysPage, iShw, cRefs));
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.
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsync const uint32_t u32 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PX86PT pPT = (PX86PT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync 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 pPT->a[i].u = 0;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync RTLogPrintf("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 pPT->a[i].u = 0;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsync const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PX86PTPAE pPT = (PX86PTPAE)PGMPOOL_PAGE_2_PTR(pVM, pPage);
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX64 cRefs=%#x\n", i, pPT->a[i], cRefs));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPT->a[i].u = 0;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
8c9d2065ca6efecd5661174e382314868c0d4b94vboxsync pPT->a[i].u = 0;
467e62e1aa999ec951e820d74498ef79dd1bedbdvboxsync AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d u64=%RX64\n", cRefs, pPage->iFirstPresent, pPage->cPresent, u64));
8c9d2065ca6efecd5661174e382314868c0d4b94vboxsync const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
8c9d2065ca6efecd5661174e382314868c0d4b94vboxsync PEPTPT pPT = (PEPTPT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
8c9d2065ca6efecd5661174e382314868c0d4b94vboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
8c9d2065ca6efecd5661174e382314868c0d4b94vboxsync if ((pPT->a[i].u & (EPT_PTE_PG_MASK | X86_PTE_P)) == u64)
8c9d2065ca6efecd5661174e382314868c0d4b94vboxsync Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX64 cRefs=%#x\n", i, pPT->a[i], cRefs));
8c9d2065ca6efecd5661174e382314868c0d4b94vboxsync pPT->a[i].u = 0;
8c9d2065ca6efecd5661174e382314868c0d4b94vboxsync RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
8c9d2065ca6efecd5661174e382314868c0d4b94vboxsync if ((pPT->a[i].u & (EPT_PTE_PG_MASK | X86_PTE_P)) == u64)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPT->a[i].u = 0;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync 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.
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsync * @param pPhysPage The guest page in question.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iShw The shadow page table.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param cRefs The number of references made in that PT.
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsyncvoid pgmPoolTrackFlushGCPhysPT(PVM pVM, PPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs)
50df3da42ff6589b0ecc4f50f2288811bc370186vboxsync PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool); NOREF(pPool);
bee80f3f77b15c166b7debd080a70e5231ba6751vboxsync LogFlow(("pgmPoolTrackFlushGCPhysPT: pPhysPage=%R[pgmpage] iShw=%d cRefs=%d\n", pPhysPage, iShw, cRefs));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPT, f);
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsync pgmPoolTrackFlushGCPhysPTInt(pVM, pPhysPage, iShw, cRefs);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPT, f);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Flushes a list of shadow page tables mapping the same physical page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
223cf005b18af2c21352a70693ebaf0582f68ebcvboxsync * @param pPhysPage The guest page in question.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iPhysExt The physical cross reference extent list to flush.
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsyncvoid pgmPoolTrackFlushGCPhysPTs(PVM pVM, PPGMPAGE pPhysPage, uint16_t iPhysExt)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPTs, f);
bee80f3f77b15c166b7debd080a70e5231ba6751vboxsync LogFlow(("pgmPoolTrackFlushGCPhysPTs: pPhysPage=%R[pgmpage] iPhysExt\n", pPhysPage, iPhysExt));
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsync pgmPoolTrackFlushGCPhysPTInt(pVM, pPhysPage, 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);
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
11c599b71a8c202a506b8623af57432c3d415ddavboxsync * Flushes all shadow page table mappings of the given guest page.
11c599b71a8c202a506b8623af57432c3d415ddavboxsync * This is typically called when the host page backing the guest one has been
11c599b71a8c202a506b8623af57432c3d415ddavboxsync * replaced or when the page protection was changed due to an access handler.
11c599b71a8c202a506b8623af57432c3d415ddavboxsync * @returns VBox status code.
11c599b71a8c202a506b8623af57432c3d415ddavboxsync * @retval VINF_SUCCESS if all references has been successfully cleared.
965a56f05878cf7c9634998e98d0bed41ebf21dbvboxsync * @retval VINF_PGM_SYNC_CR3 if we're better off with a CR3 sync and a page
965a56f05878cf7c9634998e98d0bed41ebf21dbvboxsync * pool cleaning. FF and sync flags are set.
11c599b71a8c202a506b8623af57432c3d415ddavboxsync * @param pVM The VM handle.
11c599b71a8c202a506b8623af57432c3d415ddavboxsync * @param pPhysPage The guest page in question.
11c599b71a8c202a506b8623af57432c3d415ddavboxsync * @param pfFlushTLBs This is set to @a true if the shadow TLBs should be
11c599b71a8c202a506b8623af57432c3d415ddavboxsync * flushed, it is NOT touched if this isn't necessary.
11c599b71a8c202a506b8623af57432c3d415ddavboxsync * The caller MUST initialized this to @a false.
11c599b71a8c202a506b8623af57432c3d415ddavboxsyncint pgmPoolTrackFlushGCPhys(PVM pVM, PPGMPAGE pPhysPage, bool *pfFlushTLBs)
11c599b71a8c202a506b8623af57432c3d415ddavboxsync const uint16_t u16 = PGM_PAGE_GET_TRACKING(pPhysPage);
11c599b71a8c202a506b8623af57432c3d415ddavboxsync * The zero page is currently screwing up the tracking and we'll
11c599b71a8c202a506b8623af57432c3d415ddavboxsync * have to flush the whole shebang. Unless VBOX_WITH_NEW_LAZY_PAGE_ALLOC
11c599b71a8c202a506b8623af57432c3d415ddavboxsync * is defined, zero pages won't normally be mapped. Some kind of solution
11c599b71a8c202a506b8623af57432c3d415ddavboxsync * will be needed for this problem of course, but it will have to wait...
11c599b71a8c202a506b8623af57432c3d415ddavboxsync /* Start a subset here because pgmPoolTrackFlushGCPhysPTsSlow and
11c599b71a8c202a506b8623af57432c3d415ddavboxsync pgmPoolTrackFlushGCPhysPTs will/may kill the pool otherwise. */
3241f1be564f7351b07ce8a807673fa77a7847bcvboxsync uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
11c599b71a8c202a506b8623af57432c3d415ddavboxsync if (PGMPOOL_TD_GET_CREFS(u16) != PGMPOOL_TD_CREFS_PHYSEXT)
11c599b71a8c202a506b8623af57432c3d415ddavboxsync else if (u16 != PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED))
11c599b71a8c202a506b8623af57432c3d415ddavboxsync pgmPoolTrackFlushGCPhysPTs(pVM, pPhysPage, PGMPOOL_TD_GET_IDX(u16));
11c599b71a8c202a506b8623af57432c3d415ddavboxsync rc = pgmPoolTrackFlushGCPhysPTsSlow(pVM, pPhysPage);
11c599b71a8c202a506b8623af57432c3d415ddavboxsync /* Start a subset here because pgmPoolTrackFlushGCPhysPTsSlow kill the pool otherwise. */
3241f1be564f7351b07ce8a807673fa77a7847bcvboxsync uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
11c599b71a8c202a506b8623af57432c3d415ddavboxsync rc = pgmPoolTrackFlushGCPhysPTsSlow(pVM, pPhysPage);
37dd732eb95fb61d5baceacad99de39038cde38bvboxsync pVM->pgm.s.fGlobalSyncFlags |= PGM_GLOBAL_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
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * a page pool cleaning.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
223cf005b18af2c21352a70693ebaf0582f68ebcvboxsync * @param pPhysPage The guest page in question.
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsyncint pgmPoolTrackFlushGCPhysPTsSlow(PVM pVM, PPGMPAGE pPhysPage)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPTsSlow, s);
bee80f3f77b15c166b7debd080a70e5231ba6751vboxsync LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: cUsedPages=%d cPresent=%d pPhysPage=%R[pgmpage]\n",
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * There is a limit to what makes sense.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync 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.
223cf005b18af2c21352a70693ebaf0582f68ebcvboxsync 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);
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync 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(("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);
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync //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.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This is used to remove all references to a page when flushing it.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic void pgmPoolTrackClearPageUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PCPGMPOOLUSER pUser)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Map the user page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PPGMPOOLPAGE pUserPage = &pPool->aPages[pUser->iUser];
6b022885f2cb6a55167609edecd89570cd80001dvboxsync u.pau64 = (uint64_t *)PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pUserPage);
762632a567a0397ff5d6ea93cb1fce60728e7ed3vboxsync LogFlow(("pgmPoolTrackClearPageUser: clear %x in %s (%RGp) (flushing %s)\n", iUserTable, pgmPoolPoolKindToStr(pUserPage->enmKind), pUserPage->Core.Key, pgmPoolPoolKindToStr(pPage->enmKind)));
1b7f3a92e42fb9cabf96cf31c20481da466edf76vboxsync /* Safety precaution in case we change the paging for other modes too in the future. */
f8b934f4817639cddebe4d3c2ae25d39a91fe448vboxsync Assert(!pgmPoolIsPageLocked(&pPool->CTX_SUFF(pVM)->pgm.s, pPage));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Some sanity checks.
da936e0446fb2b56b813d5d938f1dfc6e4bf8b13vboxsync Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
da936e0446fb2b56b813d5d938f1dfc6e4bf8b13vboxsync Assert(!(u.pau64[iUserTable] & PGM_PDFLAGS_MAPPING));
da936e0446fb2b56b813d5d938f1dfc6e4bf8b13vboxsync Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
da936e0446fb2b56b813d5d938f1dfc6e4bf8b13vboxsync Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
585f64d6f624f9e683321dabeb21b0eb2e6aa473vboxsync /* 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 */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* 64-bit entries */
e751d1e1e4ae424d4846a23da32f663d14960796vboxsync /* 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
e751d1e1e4ae424d4846a23da32f663d14960796vboxsync * non-present PDPT will continue to cause page faults.
e751d1e1e4ae424d4846a23da32f663d14960796vboxsync /* no break */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertFatalMsgFailed(("enmKind=%d iUser=%#x iUserTable=%#x\n", pUserPage->enmKind, pUser->iUser, pUser->iUserTable));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clears all users of a page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic void pgmPoolTrackClearPageUsers(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Free all the user records.
907da3b41f191bef4c8eb94accb9d16fcbe77019vboxsync LogFlow(("pgmPoolTrackClearPageUsers %RGp\n", pPage->GCPhys));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Clear enter in user table. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackClearPageUser(pPool, pPage, &paUsers[i]);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Free it. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* 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.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param piPhysExt Where to store the phys ext index.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncPPGMPOOLPHYSEXT pgmPoolTrackPhysExtAlloc(PVM pVM, uint16_t *piPhysExt)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_COUNTER_INC(&pPool->StamTrackPhysExtAllocFailures);
6b022885f2cb6a55167609edecd89570cd80001dvboxsync 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)
6b022885f2cb6a55167609edecd89570cd80001dvboxsync PPGMPOOLPHYSEXT pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync 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)
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Insert a reference into a list of physical cross reference extents.
65ff326ac3915638f5c4e79c2c3e836aeeeb86devboxsync * @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)
6b022885f2cb6a55167609edecd89570cd80001dvboxsync 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(("pgmPoolTrackPhysExtAddref: %d:{,,%d}\n", iPhysExt, iShwPT));
65ff326ac3915638f5c4e79c2c3e836aeeeb86devboxsync return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExt);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* general treatment. */
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync 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(("pgmPoolTrackPhysExtAddref: %d:{%d} i=%d cMax=%d\n", iPhysExt, iShwPT, i, cMax));
65ff326ac3915638f5c4e79c2c3e836aeeeb86devboxsync return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExtStart);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolTrackPhysExtAddref: overflow (1) iShwPT=%d\n", iShwPT));
65ff326ac3915638f5c4e79c2c3e836aeeeb86devboxsync return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* add another extent to the list. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PPGMPOOLPHYSEXT pNew = pgmPoolTrackPhysExtAlloc(pVM, &iPhysExt);
65ff326ac3915638f5c4e79c2c3e836aeeeb86devboxsync return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolTrackPhysExtAddref: added new extent %d:{%d}->%d\n", iPhysExt, iShwPT, iPhysExtStart));
65ff326ac3915638f5c4e79c2c3e836aeeeb86devboxsync return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExt);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Add a reference to guest physical page where extents are in use.
65ff326ac3915638f5c4e79c2c3e836aeeeb86devboxsync * @returns The new tracking data for PGMPAGE.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param u16 The ram range flags (top 16-bits).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iShwPT The shadow page table index.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncuint16_t pgmPoolTrackPhysExtAddref(PVM pVM, uint16_t u16, uint16_t iShwPT)
65ff326ac3915638f5c4e79c2c3e836aeeeb86devboxsync if (PGMPOOL_TD_GET_CREFS(u16) != PGMPOOL_TD_CREFS_PHYSEXT)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Convert to extent list.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PPGMPOOLPHYSEXT pPhysExt = pgmPoolTrackPhysExtAlloc(pVM, &iPhysExt);
65ff326ac3915638f5c4e79c2c3e836aeeeb86devboxsync LogFlow(("pgmPoolTrackPhysExtAddref: new extent: %d:{%d, %d}\n", iPhysExt, PGMPOOL_TD_GET_IDX(u16), iShwPT));
65ff326ac3915638f5c4e79c2c3e836aeeeb86devboxsync u16 = PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExt);
ba15b5303eb3e9ebd10e3d357f51335399f9d28dvboxsync u16 = PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED);
ba15b5303eb3e9ebd10e3d357f51335399f9d28dvboxsync else if (u16 != PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED))
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Insert into the extent list.
65ff326ac3915638f5c4e79c2c3e836aeeeb86devboxsync u16 = pgmPoolTrackPhysExtInsert(pVM, PGMPOOL_TD_GET_IDX(u16), iShwPT);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedLots);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear references to guest physical memory.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The page.
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsync * @param pPhysPage Pointer to the aPages entry in the ram range.
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsyncvoid pgmPoolTrackPhysExtDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PPGMPAGE pPhysPage)
65ff326ac3915638f5c4e79c2c3e836aeeeb86devboxsync const unsigned cRefs = PGM_PAGE_GET_TD_CREFS(pPhysPage);
bee80f3f77b15c166b7debd080a70e5231ba6751vboxsync AssertFatalMsg(cRefs == PGMPOOL_TD_CREFS_PHYSEXT, ("cRefs=%d pPhysPage=%R[pgmpage] pPage=%p:{.idx=%d}\n", cRefs, pPhysPage, pPage, pPage->idx));
65ff326ac3915638f5c4e79c2c3e836aeeeb86devboxsync uint16_t iPhysExt = PGM_PAGE_GET_TD_IDX(pPhysPage);
6b022885f2cb6a55167609edecd89570cd80001dvboxsync PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Look for the shadow page and check if it's all freed.
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync for (unsigned i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync for (i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (paPhysExts[iPhysExt].aidx[i] != NIL_PGMPOOL_IDX)
bee80f3f77b15c166b7debd080a70e5231ba6751vboxsync Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d\n", pPhysPage, pPage->idx));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* we can free the node. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync const uint16_t iPhysExtNext = paPhysExts[iPhysExt].iNext;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* lonely node */
bee80f3f77b15c166b7debd080a70e5231ba6751vboxsync Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d lonely\n", pPhysPage, pPage->idx));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync else if (iPhysExtPrev == NIL_PGMPOOL_PHYSEXT_INDEX)
bee80f3f77b15c166b7debd080a70e5231ba6751vboxsync Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d head\n", pPhysPage, pPage->idx));
65ff326ac3915638f5c4e79c2c3e836aeeeb86devboxsync PGM_PAGE_SET_TRACKING(pPhysPage, PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExtNext));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* in list */
bee80f3f77b15c166b7debd080a70e5231ba6751vboxsync Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d\n", pPhysPage, pPage->idx));
bee80f3f77b15c166b7debd080a70e5231ba6751vboxsync AssertFatalMsgFailed(("not-found! cRefs=%d pPhysPage=%R[pgmpage] pPage=%p:{.idx=%d}\n", cRefs, pPhysPage, pPage, pPage->idx));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync else /* nothing to do */
bee80f3f77b15c166b7debd080a70e5231ba6751vboxsync Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage]\n", pPhysPage));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear references to guest physical memory.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This is the same as pgmPoolTracDerefGCPhys except that the guest physical address
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * is assumed to be correct, so the linear search can be skipped and we can assert
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * at an earlier point.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param HCPhys The host physical address corresponding to the guest page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param GCPhys The guest physical address corresponding to HCPhys.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic void pgmPoolTracDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhys)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Walk range list.
6b022885f2cb6a55167609edecd89570cd80001dvboxsync PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* does it match? */
fe49486f3c0fc3c327138e9a8f9ea9911d7c0d64vboxsyncRTHCPHYS HCPhysPage = PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]);
8cc5fcd8d00ac5e7e66de8b9abbf5e1f77b2b577vboxsyncLog2(("pgmPoolTracDerefGCPhys %RHp vs %RHp\n", HCPhysPage, HCPhys));
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsync if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsync pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
b1c3cdef473df2fbc621d5da81acc82dbfb8a11avboxsync 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.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic void pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint)
fa7355597d7eba5b4ca8a23056ebe1b8fb306fdbvboxsync Log4(("pgmPoolTracDerefGCPhysHint %RHp %RGp\n", HCPhys, GCPhysHint));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Walk range list.
6b022885f2cb6a55167609edecd89570cd80001dvboxsync PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* does it match? */
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsync if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsync 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);
6b022885f2cb6a55167609edecd89570cd80001dvboxsync pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync while (iPage-- > 0)
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsync if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
b1c3cdef473df2fbc621d5da81acc82dbfb8a11avboxsync Log4(("pgmPoolTracDerefGCPhysHint: Linear HCPhys=%RHp GCPhysHint=%RGp GCPhysReal=%RGp\n",
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync HCPhys, GCPhysHint, pRam->GCPhys + (iPage << PAGE_SHIFT)));
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsync pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
b1c3cdef473df2fbc621d5da81acc82dbfb8a11avboxsync AssertFatalMsgFailed(("HCPhys=%RHp GCPhysHint=%RGp\n", HCPhys, GCPhysHint));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear references to guest physical memory in a 32-bit / 32-bit 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.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncDECLINLINE(void) pgmPoolTrackDerefPT32Bit32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT, PCX86PT pGstPT)
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log4(("pgmPoolTrackDerefPT32Bit32Bit: i=%d pte=%RX32 hint=%RX32\n",
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync i, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear references to guest physical memory in a PAE / 32-bit 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 (just a half one).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncDECLINLINE(void) pgmPoolTrackDerefPTPae32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PT pGstPT)
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++)
fa7355597d7eba5b4ca8a23056ebe1b8fb306fdbvboxsync Log4(("pgmPoolTrackDerefPTPae32Bit: i=%d pte=%RX64 hint=%RX32\n",
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear 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.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncDECLINLINE(void) pgmPoolTrackDerefPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PTPAE pGstPT)
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync for (unsigned i = 0; 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));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync 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.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pShwPT The shadow page table (mapping of the page).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncDECLINLINE(void) pgmPoolTrackDerefPT32Bit4MB(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT)
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync for (unsigned i = 0; 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)
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
b1c3cdef473df2fbc621d5da81acc82dbfb8a11avboxsync Log4(("pgmPoolTrackDerefPTPaeBig: i=%d pte=%RX64 hint=%RGp\n",
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, GCPhys);
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync * Clear references to shadowed pages in a 32 bits page directory.
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync * @param pPool The pool.
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync * @param pPage The page.
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync * @param pShwPD The shadow page directory (mapping of the page).
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsyncDECLINLINE(void) pgmPoolTrackDerefPD(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PD pShwPD)
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PG_MASK);
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
9f1e0743f565b1975bb4efd7311b30cfa0d3e384vboxsync AssertFatalMsgFailed(("%x\n", pShwPD->a[i].u & X86_PDE_PG_MASK));
10e1bc06b2908a0af56d92ffdbadd25b36a5ef61vboxsync * Clear references to shadowed pages in a PAE (legacy or 64 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) pgmPoolTrackDerefPDPae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPAE pShwPD)
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync 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);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync 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! */
ef4275be1971a6d17d45121d075a4f41659fa6c9vboxsync * Clear references to shadowed pages in a PAE page directory pointer table.
ef4275be1971a6d17d45121d075a4f41659fa6c9vboxsync * @param pPool The pool.
ef4275be1971a6d17d45121d075a4f41659fa6c9vboxsync * @param pPage The page.
ef4275be1971a6d17d45121d075a4f41659fa6c9vboxsync * @param pShwPDPT The shadow page directory pointer table (mapping of the page).
ef4275be1971a6d17d45121d075a4f41659fa6c9vboxsyncDECLINLINE(void) pgmPoolTrackDerefPDPTPae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPT pShwPDPT)
ef4275be1971a6d17d45121d075a4f41659fa6c9vboxsync for (unsigned i = 0; i < X86_PG_PAE_PDPE_ENTRIES; i++)
ef4275be1971a6d17d45121d075a4f41659fa6c9vboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & X86_PDPE_PG_MASK);
ef4275be1971a6d17d45121d075a4f41659fa6c9vboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
ef4275be1971a6d17d45121d075a4f41659fa6c9vboxsync 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.
8f7bc6ad2b7bbcb4b3b96248cd2478e45f2e3b88vboxsync * @param pShwPDPT The shadow page directory pointer table (mapping of the page).
8f7bc6ad2b7bbcb4b3b96248cd2478e45f2e3b88vboxsyncDECLINLINE(void) pgmPoolTrackDerefPDPT64Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPT pShwPDPT)
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPDPT->a); i++)
ef4275be1971a6d17d45121d075a4f41659fa6c9vboxsync Assert(!(pShwPDPT->a[i].u & PGM_PLXFLAGS_MAPPING));
8f7bc6ad2b7bbcb4b3b96248cd2478e45f2e3b88vboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & X86_PDPE_PG_MASK);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
8f7bc6ad2b7bbcb4b3b96248cd2478e45f2e3b88vboxsync 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! */
40f47bc231ff6c86a7ddc3295eed7010e889e78avboxsync * Clear references to shadowed pages in a 64-bit level 4 page table.
40f47bc231ff6c86a7ddc3295eed7010e889e78avboxsync * @param pPool The pool.
40f47bc231ff6c86a7ddc3295eed7010e889e78avboxsync * @param pPage The page.
40f47bc231ff6c86a7ddc3295eed7010e889e78avboxsync * @param pShwPML4 The shadow page directory pointer table (mapping of the page).
40f47bc231ff6c86a7ddc3295eed7010e889e78avboxsyncDECLINLINE(void) pgmPoolTrackDerefPML464Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PML4 pShwPML4)
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPML4->a); i++)
40f47bc231ff6c86a7ddc3295eed7010e889e78avboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPML4->a[i].u & X86_PDPE_PG_MASK);
40f47bc231ff6c86a7ddc3295eed7010e889e78avboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
40f47bc231ff6c86a7ddc3295eed7010e889e78avboxsync AssertFatalMsgFailed(("%RX64\n", pShwPML4->a[i].u & X86_PML4E_PG_MASK));
40f47bc231ff6c86a7ddc3295eed7010e889e78avboxsync /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * Clear references to shadowed pages in an EPT page table.
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * @param pPool The pool.
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * @param pPage The page.
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * @param pShwPML4 The shadow page directory pointer table (mapping of the page).
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsyncDECLINLINE(void) pgmPoolTrackDerefPTEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPT pShwPT)
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync Log4(("pgmPoolTrackDerefPTEPT: i=%d pte=%RX64 GCPhys=%RX64\n",
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync i, pShwPT->a[i].u & EPT_PTE_PG_MASK, pPage->GCPhys));
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & EPT_PTE_PG_MASK, GCPhys);
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * Clear references to shadowed pages in an EPT page directory.
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * @param pPool The pool.
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * @param pPage The page.
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * @param pShwPD The shadow page directory (mapping of the page).
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsyncDECLINLINE(void) pgmPoolTrackDerefPDEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPD pShwPD)
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & EPT_PDE_PG_MASK);
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync AssertFatalMsgFailed(("%RX64\n", pShwPD->a[i].u & EPT_PDE_PG_MASK));
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * Clear references to shadowed pages in an EPT page directory pointer table.
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * @param pPool The pool.
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * @param pPage The page.
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * @param pShwPDPT The shadow page directory pointer table (mapping of the page).
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsyncDECLINLINE(void) pgmPoolTrackDerefPDPTEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPDPT pShwPDPT)
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pShwPDPT->a); i++)
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & EPT_PDPTE_PG_MASK);
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & EPT_PDPTE_PG_MASK));
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clears all references made by this page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This includes other shadow pages and GC physical addresses.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic void pgmPoolTrackDeref(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Map the shadow page and take action according to the page kind.
22e281e75ed636601178296c6daebda8f1d17c59vboxsync void *pvShw = PGMPOOL_PAGE_2_LOCKED_PTR(pPool->CTX_SUFF(pVM), pPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
6b022885f2cb6a55167609edecd89570cd80001dvboxsync 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);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
6b022885f2cb6a55167609edecd89570cd80001dvboxsync 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);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
6b022885f2cb6a55167609edecd89570cd80001dvboxsync int rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackDerefPTPaePae(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PTPAE)pvGst);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
9c425bdea5f0991df62922b1584b805a86f2f898vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS: /* treat it like a 4 MB page */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackDerefPT32Bit4MB(pPool, pPage, (PX86PT)pvShw);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS: /* treat it like a 2 MB page */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackDerefPTPaeBig(pPool, pPage, (PX86PTPAE)pvShw);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync#else /* !PGMPOOL_WITH_GCPHYS_TRACKING */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync#endif /* !PGMPOOL_WITH_GCPHYS_TRACKING */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pgmPoolTrackDerefPDPae(pPool, pPage, (PX86PDPAE)pvShw);
ef4275be1971a6d17d45121d075a4f41659fa6c9vboxsync pgmPoolTrackDerefPDPTPae(pPool, pPage, (PX86PDPT)pvShw);
8f7bc6ad2b7bbcb4b3b96248cd2478e45f2e3b88vboxsync pgmPoolTrackDerefPDPT64Bit(pPool, pPage, (PX86PDPT)pvShw);
40f47bc231ff6c86a7ddc3295eed7010e889e78avboxsync pgmPoolTrackDerefPML464Bit(pPool, pPage, (PX86PML4)pvShw);
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync pgmPoolTrackDerefPTEPT(pPool, pPage, (PEPTPT)pvShw);
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync pgmPoolTrackDerefPDEPT(pPool, pPage, (PEPTPD)pvShw);
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync 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). */
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync#endif /* PGMPOOL_WITH_USER_TRACKING */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Flushes the entire cache.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * It will assert a global CR3 flush (FF) and assumes the caller is aware of this
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * and execute this CR3 flush.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
72a925d2c24794318caf689fdee5a90dfae6a4b6vboxsync * @remark Only used during reset now, we might want to rename and/or move it.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * If there are no pages in the pool, there is nothing to do.
72a925d2c24794318caf689fdee5a90dfae6a4b6vboxsync * Exit the shadow mode since we're going to clear everything,
72a925d2c24794318caf689fdee5a90dfae6a4b6vboxsync * including the root page.
72a925d2c24794318caf689fdee5a90dfae6a4b6vboxsync /** @todo Need to synchronize this across all VCPUs! */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Nuke the free list and reinsert all pages into it.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = pPool->cCurPages - 1; i >= PGMPOOL_IDX_FIRST; i--)
5ab935df052385fd74329486db23ea63b2004b41vboxsync Assert(pPage->Core.Key == MMPage2Phys(pVM, pPage->pvPageR3));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPage->fZeroed = false; /* This could probably be optimized, but better safe than sorry. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPool->aPages[pPool->cCurPages - 1].iNext = NIL_PGMPOOL_IDX;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Zap and reinitialize the user records.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = 0; i < cMaxUsers; i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync paUsers[cMaxUsers - 1].iNext = NIL_PGMPOOL_USER_INDEX;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear all the GCPhys links and rebuild the phys ext free list.
5ab935df052385fd74329486db23ea63b2004b41vboxsync for (PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync while (iPage-- > 0)
6b022885f2cb6a55167609edecd89570cd80001dvboxsync PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = 0; i < cMaxPhysExts; i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync paPhysExts[cMaxPhysExts - 1].iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Just zap the modified list.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear the GCPhys hash and the age list.
e4ea543752422f1139923e3e506c625b0a1827c5vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPool->aiHash); i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Reinsert active pages into the hash and ensure monitoring chains are correct.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = PGMPOOL_IDX_FIRST_SPECIAL; i < PGMPOOL_IDX_FIRST; i++)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* ASSUMES that we're not sharing with any of the other special pages (safe for now). */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = PGMHandlerPhysicalChangeCallbacks(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1),
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pPage),
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pPage),
09d4e754a010196fe0d9d972b6ccc005ecb8b116vboxsync pPool->pfnAccessHandlerRC, MMHyperCCToRC(pVM, pPage),
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(pPage->iUserHead == NIL_PGMPOOL_USER_INDEX); /* for now */
e0b9d3c357adf9b7d05f55540e86f22943fc4b23vboxsync * Re-enter the shadowing mode and assert Sync CR3 FF.
5e9dc1c79a0a8fc61c32f6dbbe21ce385fdbebd2vboxsync#endif /* IN_RING3 */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Flushes a pool page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This moves the page to the free list after removing all user references to it.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * In GC this will cause a CR3 reload if the page is traced back to an active root page.
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)
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync LogFlow(("pgmPoolFlushPage: pPage=%p:{.Key=%RHp, .idx=%d, .enmKind=%s, .GCPhys=%RGp}\n",
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), pPage->GCPhys));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Quietly reject any attempts at flushing any of the special root pages.
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync Log(("pgmPoolFlushPage: special root page, rejected. enmKind=%s idx=%d\n", pgmPoolPoolKindToStr(pPage->enmKind), pPage->idx));
1b7f3a92e42fb9cabf96cf31c20481da466edf76vboxsync * Quietly reject any attempts at flushing the currently active shadow CR3 mapping
0aeb5f500280603a51364071670efeb2617f771cvboxsync AssertMsg( pPage->enmKind == PGMPOOLKIND_64BIT_PML4
0aeb5f500280603a51364071670efeb2617f771cvboxsync || pPage->enmKind == PGMPOOLKIND_PAE_PDPT_FOR_32BIT
7e2fce486e7e2e45eb61d78ee206ba42ed89e016vboxsync || pPage->enmKind == PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD
7e2fce486e7e2e45eb61d78ee206ba42ed89e016vboxsync || pPage->enmKind == PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD
7e2fce486e7e2e45eb61d78ee206ba42ed89e016vboxsync || pPage->enmKind == PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD
7e2fce486e7e2e45eb61d78ee206ba42ed89e016vboxsync || pPage->enmKind == PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD,
41b8e304f190c3d901bcfee5e73c11bc2c42ce13vboxsync ("Can't free the shadow CR3! (%RHp vs %RHp kind=%d\n", PGMGetHyperCR3(VMMGetCpu(pVM)), pPage->Core.Key, pPage->enmKind));
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync Log(("pgmPoolFlushPage: current active shadow CR3, rejected. enmKind=%s idx=%d\n", pgmPoolPoolKindToStr(pPage->enmKind), pPage->idx));
50e0e1bb8996934b3f38c117a456f825052bfb0cvboxsync /* Start a subset so we won't run out of mapping space. */
3241f1be564f7351b07ce8a807673fa77a7847bcvboxsync uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Mark the page as being in need of a ASMMemZeroPage().
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clear the page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Flush it from the cache.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync#endif /* PGMPOOL_WITH_CACHE */
50e0e1bb8996934b3f38c117a456f825052bfb0cvboxsync /* Heavy stuff done. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Deregistering the monitoring.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Free the page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Frees a usage of a pool page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * The caller is responsible to updating the user table so that it no longer
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * 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.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iUserTable The index into the user table (shadowed).
afed5ab737f4aacfae3fe73776f40e989190a7cavboxsyncvoid pgmPoolFreeByPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync LogFlow(("pgmPoolFreeByPage: pPage=%p:{.Key=%RHp, .idx=%d, enmKind=%s} iUser=%#x iUserTable=%#x\n",
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync 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.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
5e0e35e65847aae0955835d5c4b4e76143e580c1vboxsync * @param enmKind Page table kind
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param iUser The user of the page.
5e0e35e65847aae0955835d5c4b4e76143e580c1vboxsyncstatic int pgmPoolMakeMoreFreePages(PPGMPOOL pPool, PGMPOOLKIND enmKind, uint16_t iUser)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("pgmPoolMakeMoreFreePages: iUser=%#x\n", iUser));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * If the pool isn't full grown yet, expand it.
5e0e35e65847aae0955835d5c4b4e76143e580c1vboxsync /* Hack alert: we can't deal with jumps to ring 3 when called from MapCR3 and allocating pages for PAE PDs. */
5e0e35e65847aae0955835d5c4b4e76143e580c1vboxsync && (enmKind < PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD || enmKind > PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD)
6b022885f2cb6a55167609edecd89570cd80001dvboxsync int rc = CTXALLMID(VMM, CallHost)(pPool->CTX_SUFF(pVM), VMMCALLHOST_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...
750d4d0506a38b2e80c997075d40aad474e675fbvboxsync /* @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.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VINF_SUCCESS if a NEW page was allocated.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VINF_PGM_CACHED_PAGE if a CACHED page was returned.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @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 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.
afed5ab737f4aacfae3fe73776f40e989190a7cavboxsyncint pgmPoolAlloc(PVM pVM, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync LogFlow(("pgmPoolAlloc: GCPhys=%RGp enmKind=%s iUser=%#x iUserTable=%#x\n", GCPhys, pgmPoolPoolKindToStr(enmKind), iUser, iUserTable));
fc17c860479b1d7f3efdbb72584c14f0df09adb4vboxsync /** @todo CSAM/PGMPrefetchPage messes up here during CSAMR3CheckGates
fc17c860479b1d7f3efdbb72584c14f0df09adb4vboxsync * (TRPMR3SyncIDT) because of FF priority. Try fix that?
37dd732eb95fb61d5baceacad99de39038cde38bvboxsync * Assert(!(pVM->pgm.s.fGlobalSyncFlags & PGM_GLOBAL_SYNC_CLEAR_PGM_POOL)); */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc2 = pgmPoolCacheAlloc(pPool, GCPhys, enmKind, iUser, iUserTable, ppPage);
8da94e4d6813b682cdb38eb9b151ada72d91b40evboxsync LogFlow(("pgmPoolAlloc: cached returns %Rrc *ppPage=%p:{.Key=%RHp, .idx=%d}\n", rc2, *ppPage, (*ppPage)->Core.Key, (*ppPage)->idx));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Allocate a new one.
5e0e35e65847aae0955835d5c4b4e76143e580c1vboxsync rc = pgmPoolMakeMoreFreePages(pPool, enmKind, iUser);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertReleaseReturn(iNew != NIL_PGMPOOL_IDX, VERR_INTERNAL_ERROR);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* unlink the free head */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Initialize it.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPool->cUsedPages++; /* physical handler registration / pgmPoolTrackFlushGCPhysPTsSlow requirement. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pPage->fSeenNonGlobal = false; /* Set this to 'true' to disable this feature. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Insert into the tracking and cache. If this fails, free the page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc3 = pgmPoolTrackInsert(pPool, pPage, GCPhys, iUser, iUserTable);
750d4d0506a38b2e80c997075d40aad474e675fbvboxsync Log(("pgmPoolAlloc: returns %Rrc (Insert)\n", rc3));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync#endif /* PGMPOOL_WITH_USER_TRACKING */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Commit the allocation, clear the page and return.
8da94e4d6813b682cdb38eb9b151ada72d91b40evboxsync LogFlow(("pgmPoolAlloc: returns %Rrc *ppPage=%p:{.Key=%RHp, .idx=%d, .fCached=%RTbool, .fMonitored=%RTbool}\n",
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc, pPage, pPage->Core.Key, pPage->idx, pPage->fCached, pPage->fMonitored));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Frees a usage of a pool page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
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).
afed5ab737f4aacfae3fe73776f40e989190a7cavboxsyncvoid pgmPoolFree(PVM pVM, RTHCPHYS HCPhys, uint16_t iUser, uint32_t iUserTable)
8da94e4d6813b682cdb38eb9b151ada72d91b40evboxsync LogFlow(("pgmPoolFree: HCPhys=%RHp iUser=%#x iUserTable=%#x\n", HCPhys, iUser, iUserTable));
223cf005b18af2c21352a70693ebaf0582f68ebcvboxsync pgmPoolFreeByPage(pPool, pgmPoolGetPage(pPool, HCPhys), iUser, iUserTable);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Flushes the entire cache.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * It will assert a global CR3 flush (FF) and assumes the caller is aware of this
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * and execute this CR3 flush.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPool The pool.
d410f63700f660eab1bbf8d6e107b6b7373fee72vboxsync#endif /* IN_RING3 */
0aeb5f500280603a51364071670efeb2617f771cvboxsyncstatic const char *pgmPoolPoolKindToStr(uint8_t enmKind)
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_INVALID";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_FREE";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_32BIT_PT_FOR_PHYS";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_PAE_PT_FOR_PHYS";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_PAE_PT_FOR_32BIT_PT";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_PAE_PT_FOR_PAE_PT";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_PAE_PT_FOR_PAE_2MB";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_32BIT_PD";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_32BIT_PD_PHYS";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_PAE_PD_FOR_PAE_PD";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_PAE_PD_PHYS";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_PAE_PDPT_FOR_32BIT";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_PAE_PDPT";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_PAE_PDPT_PHYS";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_64BIT_PDPT_FOR_PHYS";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_64BIT_PD_FOR_PHYS";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_64BIT_PML4";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_EPT_PDPT_FOR_PHYS";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_EPT_PD_FOR_PHYS";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_EPT_PT_FOR_PHYS";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "PGMPOOLKIND_ROOT_NESTED";
0174432b2b1a760b89840ba696f7ba51def65dddvboxsync return "Unknown kind!";
d410f63700f660eab1bbf8d6e107b6b7373fee72vboxsync#endif /* LOG_ENABLED*/