PGMAllPool.cpp revision c33db29e7b41467a35675031f5f5233839909083
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * PGM Shadow Page Pool.
5654aa8329bbe2838fa5733f28c1a0461c9e6453vboxsync * Copyright (C) 2006-2007 innotek GmbH
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * available from http://www.virtualbox.org. This file is free software;
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * 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.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync/*******************************************************************************
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync* Header Files *
f84cd77241a1c4b9106a92280611c659243e10d1vboxsync*******************************************************************************/
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync/*******************************************************************************
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync* Internal Functions *
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync*******************************************************************************/
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsyncDECLINLINE(unsigned) pgmPoolTrackGetShadowEntrySize(PGMPOOLKIND enmKind);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsyncDECLINLINE(unsigned) pgmPoolTrackGetGuestEntrySize(PGMPOOLKIND enmKind);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsyncstatic void pgmPoolTrackDeref(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
289060a0c3cb1d509f2cb01fca060796212376f6vboxsyncstatic void pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint);
289060a0c3cb1d509f2cb01fca060796212376f6vboxsyncstatic int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint16_t iUserTable);
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsyncstatic void pgmPoolMonitorModifiedRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsyncDECLEXPORT(int) pgmPoolAccessHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser);
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync * Checks if the specified page pool kind is for a 4MB or 2MB guest page.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * @returns true if it's the shadow of a 4MB or 2MB guest page, otherwise false.
faf968cea88f2ab4bcc3325b17bc8b095a8e3642vboxsync * @param enmKind The page kind.
c7a00ac75c7941df2afb62e6fd7ffdf1795e6c76vboxsyncDECLINLINE(bool) pgmPoolIsBigPage(PGMPOOLKIND enmKind)
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync return true;
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync return false;
f94f82d66536c7332c347dd9a3a9f0f8c79247f4vboxsync * Maps a pool page into the current context.
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync * @returns Pointer to the mapping.
43dff6077acb4176145b18bdb862eb73620182d2vboxsync * @param pVM The VM handle.
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync * @param pPage The page to map.
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync /* general pages. */
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync Assert(pPage->idx < pVM->pgm.s.pPoolGC->cCurPages);
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync int rc = PGMGCDynMapHCPage(pVM, pPage->Core.Key, &pv);
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync /* special pages. */
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync AssertReleaseMsgFailed(("Invalid index %d\n", pPage->idx));
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync#endif /* IN_GC */
5654aa8329bbe2838fa5733f28c1a0461c9e6453vboxsync * Determin the size of a write instruction.
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * @returns number of bytes written.
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * @param pDis The disassembler state.
ff78b877ed7acd25e2d384570a938441455d6a95vboxsyncstatic unsigned pgmPoolDisasWriteSize(PDISCPUSTATE pDis)
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * This is very crude and possibly wrong for some opcodes,
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * but since it's not really supposed to be called we can
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * probably live with that.
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * Flushes a chain of pages sharing the same access monitor.
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * @returns VBox status code suitable for scheduling.
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * @param pPool The pool.
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * @param pPage A page in the chain.
ff78b877ed7acd25e2d384570a938441455d6a95vboxsyncint pgmPoolMonitorChainFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * Find the list head.
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * Itereate the list flushing each shadow page.
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync if (rc2 == VERR_PGM_POOL_CLEARED && rc == VINF_SUCCESS)
c7a00ac75c7941df2afb62e6fd7ffdf1795e6c76vboxsync * Wrapper for getting the current context pointer to the entry being modified.
c7a00ac75c7941df2afb62e6fd7ffdf1795e6c76vboxsync * @returns Pointer to the current context mapping of the entry.
c7a00ac75c7941df2afb62e6fd7ffdf1795e6c76vboxsync * @param pPool The pool.
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync * @param pvFault The fault virtual address.
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync * @param GCPhysFault The fault physical address.
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync * @param cbEntry The entry size.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsyncDECLINLINE(const void *) pgmPoolMonitorGCPtr2CCPtr(PPGMPOOL pPool, RTHCPTR pvFault, RTGCPHYS GCPhysFault, const unsigned cbEntry)
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsyncDECLINLINE(const void *) pgmPoolMonitorGCPtr2CCPtr(PPGMPOOL pPool, RTGCPTR pvFault, RTGCPHYS GCPhysFault, const unsigned cbEntry)
2508d15edddcae0b79002fae3fe103d6c4836810vboxsync return (RTGCPTR)((RTGCUINTPTR)pvFault & ~(RTGCUINTPTR)(cbEntry - 1));
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync int rc = pgmRamGCPhys2HCPtr(&pPool->pVMHC->pgm.s, GCPhysFault & ~(RTGCPHYS)(cbEntry - 1), &pvRet);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync return (RTHCPTR)((uintptr_t)pvFault & ~(RTHCUINTPTR)(cbEntry - 1));
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync * Process shadow entries before they are changed by the guest.
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync * For PT entries we will clear them. For PD entries, we'll simply check
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync * for mapping conflicts and set the SyncCR3 FF if found.
2afbe132eb7931e0125141eabe3a48e08f1ffab5vboxsync * @param pPool The pool.
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync * @param pPage The head page.
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync * @param GCPhysFault The guest physical fault address.
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync * @param uAddress In R0 and GC this is the guest context fault address (flat).
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync * In R3 this is the host context 'fault' address.
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync * @param pCpu The disassembler state for figuring out the write size.
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync * This need not be specified if the caller knows we won't do cross entry accesses.
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsyncvoid pgmPoolMonitorChainChanging(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault, RTHCPTR pvAddress, PDISCPUSTATE pCpu)
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsyncvoid pgmPoolMonitorChainChanging(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault, RTGCPTR pvAddress, PDISCPUSTATE pCpu)
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync const unsigned off = GCPhysFault & PAGE_OFFSET_MASK;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync LogFlow(("pgmPoolMonitorChainChanging: %VGv phys=%VGp kind=%d\n", pvAddress, GCPhysFault, pPage->enmKind));
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTXSUFF(pVM), pPage);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync PCX86PTE pGstPte = (PCX86PTE)pgmPoolMonitorGCPtr2CCPtr(pPool, pvAddress, GCPhysFault, sizeof(*pGstPte));
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync Log4(("pgmPoolMonitorChainChanging 32_32: deref %VHp GCPhys %VGp\n", uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK, pGstPte->u & X86_PTE_PG_MASK));
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync /* page/2 sized */
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync const unsigned iShw = (off / sizeof(X86PTE)) & (X86_PG_PAE_ENTRIES - 1);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync PCX86PTE pGstPte = (PCX86PTE)pgmPoolMonitorGCPtr2CCPtr(pPool, pvAddress, GCPhysFault, sizeof(*pGstPte));
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync Log4(("pgmPoolMonitorChainChanging pae_32: deref %VHp GCPhys %VGp\n", uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK, pGstPte->u & X86_PTE_PG_MASK));
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync PCX86PTEPAE pGstPte = (PCX86PTEPAE)pgmPoolMonitorGCPtr2CCPtr(pPool, pvAddress, GCPhysFault, sizeof(*pGstPte));
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync Log4(("pgmPoolMonitorChainChanging pae_32: deref %VHp GCPhys %VGp\n", uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK, pGstPte->u & X86_PTE_PAE_PG_MASK));
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync const unsigned iShw = off / sizeof(X86PTE); // ASSUMING 32-bit guest paging!
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync /* paranoia / a bit assumptive. */
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync const unsigned iShw2 = (off + pgmPoolDisasWriteSize(pCpu) - 1) / sizeof(X86PTE);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync#if 0 /* useful when running PGMAssertCR3(), a bit too troublesome for general use (TLBs). */
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync && !VM_FF_ISSET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3))
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync LogFlow(("pgmPoolMonitorChainChanging: iShw=%#x: %RX32 -> freeing it!\n", iShw, uShw.pPD->a[iShw].u));
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync# ifdef IN_GC /* TLB load - we're pushing things a bit... */
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pgmPoolFree(pPool->CTXSUFF(pVM), uShw.pPD->a[iShw].u & X86_PDE_PG_MASK, pPage->idx, iShw);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync unsigned iShw = (off / sizeof(X86PTE)) * 2; // ASSUMING 32-bit guest paging!
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync if ((uShw.pPDPae->a[iShw].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync /* paranoia / a bit assumptive. */
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync && (uShw.pPDPae->a[iShw2].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync#if 0 /* useful when running PGMAssertCR3(), a bit too troublesome for general use (TLBs). */
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync && !VM_FF_ISSET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3))
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync LogFlow(("pgmPoolMonitorChainChanging: iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync# ifdef IN_GC /* TLB load - we're pushing things a bit... */
df25990f935e7fd32acd9be9a156aff8d10facf2vboxsync pgmPoolFree(pPool->CTXSUFF(pVM), uShw.pPDPae->a[iShw].u & X86_PDE_PAE_PG_MASK, pPage->idx, iShw);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync AssertFatalMsgFailed(("enmKind=%d\n", pPage->enmKind));
3f1e0eea71cabeb90529e546f16eb7aee513fde9vboxsync * Checks if a access could be a fork operation in progress.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * Meaning, that the guest is setuping up the parent process for Copy-On-Write.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @returns true if it's likly that we're forking, otherwise false.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @param pPool The pool.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @param pCpu The disassembled instruction.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @param offFault The access offset.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsyncDECLINLINE(bool) pgmPoolMonitorIsForking(PPGMPOOL pPool, PDISCPUSTATE pCpu, unsigned offFault)
1986f56777969a25707ab214f8dd070804be666cvboxsync * i386 linux is using btr to clear X86_PTE_RW.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * The functions involved are (2.6.16 source inspection):
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * clear_bit
1986f56777969a25707ab214f8dd070804be666cvboxsync * ptep_set_wrprotect
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * copy_one_pte
1986f56777969a25707ab214f8dd070804be666cvboxsync * copy_pte_range
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * copy_pmd_range
1986f56777969a25707ab214f8dd070804be666cvboxsync * copy_pud_range
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * copy_page_range
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * copy_process
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync /** @todo Validate that the bit index is X86_PTE_RW. */
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_COUNTER_INC(&pPool->CTXMID(StatMonitor,Fork));
1986f56777969a25707ab214f8dd070804be666cvboxsync return true;
1986f56777969a25707ab214f8dd070804be666cvboxsync return false;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * Determin whether the page is likely to have been reused.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @returns true if we consider the page as being reused for a different purpose.
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync * @returns false if we consider it to still be a paging page.
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync * @param pPage The page in question.
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync * @param pCpu The disassembly info for the faulting insturction.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @param pvFault The fault address.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @remark The REP prefix check is left to the caller because of STOSD/W.
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsyncDECLINLINE(bool) pgmPoolMonitorIsReused(PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu, RTGCPTR pvFault)
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync return true;
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync return true;
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync return true;
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync return true;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync return true;
0bc7c910e57c78c68e89122e2244cc073d1ef06evboxsync case OP_MOVNTDQ: /* solaris - hwblkclr & hwblkpagecopy */
addc480d0d7650db6323467bbdab6c21836a2928vboxsync return true;
3c941112ffb137d71a8e457fcc3915f2d464ed2avboxsync return true;
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync //if (pPage->fCR3Mix)
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync // return false;
3c941112ffb137d71a8e457fcc3915f2d464ed2avboxsync return false;
3c941112ffb137d71a8e457fcc3915f2d464ed2avboxsync * Flushes the page being accessed.
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync * @returns VBox status code suitable for scheduling.
1986f56777969a25707ab214f8dd070804be666cvboxsync * @param pVM The VM handle.
1986f56777969a25707ab214f8dd070804be666cvboxsync * @param pPool The pool.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @param pPage The pool page (head).
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @param pCpu The disassembly of the write instruction.
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync * @param pRegFrame The trap register frame.
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync * @param GCPhysFault The fault address as guest physical address.
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync * @param pvFault The fault address.
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsyncstatic int pgmPoolAccessHandlerFlush(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
67b4b089c50d0ab8ad847dddd8c0e0812fdadc9evboxsync * First, do the flushing.
150283991b1a312acbe86c67d3420f6463b38878vboxsync * Emulate the instruction (xp/w2k problem, requires pc/cr2/sp detection).
150283991b1a312acbe86c67d3420f6463b38878vboxsync int rc2 = EMInterpretInstructionCPU(pVM, pCpu, pRegFrame, pvFault, &cbWritten);
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync if (PATMIsPatchGCAddr(pVM, (RTGCPTR)pRegFrame->eip))
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for patch code %04x:%RGv, ignoring.\n",
150283991b1a312acbe86c67d3420f6463b38878vboxsync STAM_COUNTER_INC(&pPool->StatMonitorGCIntrFailPatch2);
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync STAM_COUNTER_INC(&pPool->CTXMID(StatMonitor,EmulateInstr));
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync /* See use in pgmPoolAccessHandlerSimple(). */
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync LogFlow(("pgmPoolAccessHandlerPT: returns %Vrc (flushed)\n", rc));
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync * Handles the STOSD write accesses.
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync * @returns VBox status code suitable for scheduling.
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync * @param pVM The VM handle.
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync * @param pPool The pool.
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync * @param pPage The pool page (head).
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync * @param pCpu The disassembly of the write instruction.
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync * @param pRegFrame The trap register frame.
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync * @param GCPhysFault The fault address as guest physical address.
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync * @param pvFault The fault address.
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsyncDECLINLINE(int) pgmPoolAccessHandlerSTOSD(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync * Increment the modification counter and insert it into the list
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync * of modified pages the first time.
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync * Execute REP STOSD.
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync * This ASSUMES that we're not invoked by Trap0e on in a out-of-sync
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync * write situation, meaning that it's safe to write here.
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync pgmPoolMonitorChainChanging(pPool, pPage, GCPhysFault, pu32, NULL);
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync PGMPhysWriteGCPhys(pVM, GCPhysFault, &pRegFrame->eax, 4);
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync /* See use in pgmPoolAccessHandlerSimple(). */
return VINF_SUCCESS;
DECLINLINE(int) pgmPoolAccessHandlerSimple(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
# ifdef IN_GC
#ifndef DEBUG_sandervl
AssertMsgFailed(("pgmPoolAccessHandlerPTWorker: Interpretation failed for patch code %04x:%RGv - opcode=%d\n",
return rc;
DECLEXPORT(int) pgmPoolAccessHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser)
LogFlow(("pgmPoolAccessHandler: pvFault=%p pPage=%p:{.idx=%d} GCPhysFault=%VGp\n", pvFault, pPage, pPage->idx, GCPhysFault));
bool fReused = false;
if ( ( pPage->cModifications < 48 /** @todo #define */ /** @todo need to check that it's not mapping EIP. */ /** @todo adjust this! */
STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTXSUFF(pPool)->CTXSUFF(StatMonitor), &pPool->CTXMID(StatMonitor,Handled), a);
return rc;
STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTXSUFF(pPool)->CTXSUFF(StatMonitor), &pPool->CTXMID(StatMonitor,RepStosd), a);
return rc;
pRegFrame->eax, pRegFrame->ecx, pRegFrame->edi, pRegFrame->esi, pRegFrame->eip, Cpu.pCurInstr->opcode, Cpu.prefix));
STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTXSUFF(pPool)->CTXSUFF(StatMonitor), &pPool->CTXMID(StatMonitor,FlushPage), a);
return rc;
#ifdef PGMPOOL_WITH_CACHE
if (i == NIL_PGMPOOL_IDX)
iPrev = i;
* @retval VERR_PGM_POOL_CLEARED if the deregistration of a physical handler will cause a light weight pool flush.
return rc;
switch (enmKind1)
switch (enmKind2)
switch (enmKind2)
case PGMPOOLKIND_ROOT_PAE_PD:
case PGMPOOLKIND_ROOT_PDPT:
case PGMPOOLKIND_ROOT_PML4:
static int pgmPoolCacheAlloc(PPGMPOOL pPool, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint16_t iUserTable, PPPGMPOOLPAGE ppPage)
Log3(("pgmPoolCacheAlloc %VGp kind %d iUser=%d iUserTable=%x SLOT=%d\n", GCPhys, enmKind, iUser, iUserTable, i));
if (i != NIL_PGMPOOL_IDX)
return VINF_PGM_CACHED_PAGE;
return rc;
pgmPoolFlushPage(pPool, pPage); /* ASSUMES that VERR_PGM_POOL_CLEARED will be returned by pgmPoolTracInsert. */
} while (i != NIL_PGMPOOL_IDX);
return VERR_FILE_NOT_FOUND;
if (fCanBeCached)
#ifdef PGMPOOL_WITH_MONITORING
#ifdef PGMPOOL_WITH_CACHE
if (i == NIL_PGMPOOL_IDX)
return NULL;
case PGMPOOLKIND_ROOT_PAE_PD:
case PGMPOOLKIND_ROOT_PDPT:
case PGMPOOLKIND_ROOT_PML4:
return pPage;
} while (i != NIL_PGMPOOL_IDX);
return NULL;
* @retval VERR_PGM_POOL_CLEARED if the registration of the physical handler will cause a light weight pool flush.
return VINF_SUCCESS;
case PGMPOOLKIND_ROOT_PAE_PD:
#ifdef PGMPOOL_WITH_MIXED_PT_CR3
case PGMPOOLKIND_ROOT_PDPT:
case PGMPOOLKIND_ROOT_PML4:
int rc;
if (pPageHead)
return rc;
* @retval VERR_PGM_POOL_CLEARED if the deregistration of the physical handler will cause a light weight pool flush.
return VINF_SUCCESS;
case PGMPOOLKIND_ROOT_PAE_PD:
#ifdef PGMPOOL_WITH_MIXED_PT_CR3
case PGMPOOLKIND_ROOT_PDPT:
case PGMPOOLKIND_ROOT_PML4:
int rc;
return rc;
#ifdef PGMPOOL_WITH_MIXED_PT_CR3
LogFlow(("pgmPoolMonitorMonitorCR3: idxRoot=%d pPage=%p:{.GCPhys=%VGp, .fMonitored=%d} GCPhysCR3=%VGp\n",
return VINF_SUCCESS;
return rc;
return VINF_SUCCESS;
return rc;
#ifdef VBOX_WITH_STATISTICS
Log3(("pgmPoolMonitorModifiedRemove: idx=%d cModifications=%d\n", pPage->idx, pPage->cModifications));
#ifdef PGMPOOL_WITH_USER_TRACKING
#ifdef PGMPOOL_WITH_USER_TRACKING
if (!--cLeft)
AssertMsg(cModifiedPages == pPool->cModifiedPages, ("%d != %d\n", cModifiedPages, pPool->cModifiedPages));
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
pRam;
while (iPage-- > 0)
for (unsigned i = 0; i < cMaxPhysExts; i++)
#ifdef PGMPOOL_WITH_USER_TRACKING
#ifdef PGMPOOL_WITH_CACHE
return rc;
return VERR_PGM_POOL_FLUSHED;
* @retval VERR_PGM_POOL_CLEARED if the deregistration of the physical handler will cause a light weight pool flush.
DECLINLINE(int) pgmPoolTrackInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhys, uint16_t iUser, uint16_t iUserTable)
if (i == NIL_PGMPOOL_USER_INDEX)
return rc;
# ifdef PGMPOOL_WITH_MIXED_PT_CR3
const bool fCanBeMonitored = true;
|| (GCPhys & X86_PTE_PAE_PG_MASK) != (pPool->CTXSUFF(pVM)->pgm.s.GCPhysGstCR3Monitored & X86_PTE_PAE_PG_MASK)
# ifdef PGMPOOL_WITH_CACHE
if (fCanBeMonitored)
# ifdef PGMPOOL_WITH_MONITORING
# ifndef PGMPOOL_WITH_CACHE
return rc;
static int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint16_t iUserTable)
# ifdef VBOX_STRICT
AssertMsg(paUsers[i].iUser != iUser || paUsers[i].iUserTable != iUserTable, ("%x %x vs new %x %x\n", paUsers[i].iUser, paUsers[i].iUserTable, iUser, iUserTable));
} while (i != NIL_PGMPOOL_USER_INDEX);
if (i == NIL_PGMPOOL_USER_INDEX)
return rc;
# ifdef PGMPOOL_WITH_CACHE
return VINF_SUCCESS;
static void pgmPoolTrackFreeUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint16_t iUserTable)
if ( i != NIL_PGMPOOL_USER_INDEX
while (i != NIL_PGMPOOL_USER_INDEX)
iPrev = i;
switch (enmKind)
case PGMPOOLKIND_ROOT_PAE_PD:
case PGMPOOLKIND_ROOT_PDPT:
case PGMPOOLKIND_ROOT_PML4:
switch (enmKind)
case PGMPOOLKIND_ROOT_PAE_PD:
case PGMPOOLKIND_ROOT_PDPT:
case PGMPOOLKIND_ROOT_PML4:
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
static void pgmPoolTrackFlushGCPhysPTInt(PVM pVM, PCPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs)
LogFlow(("pgmPoolTrackFlushGCPhysPT: HCPhys=%RHp iShw=%d cRefs=%d\n", pPhysPage->HCPhys, iShw, cRefs));
pPT->a[i].u = 0;
cRefs--;
if (!cRefs)
RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
pPT->a[i].u = 0;
AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
pPT->a[i].u = 0;
cRefs--;
if (!cRefs)
RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
pPT->a[i].u = 0;
AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
LogFlow(("pgmPoolTrackFlushGCPhysPT: HCPhys=%RHp iShw=%d cRefs=%d\n", pPhysPage->HCPhys, iShw, cRefs));
return VINF_PGM_GCPHYS_ALIASED;
pPT->a[i].u = 0;
if (!--cPresent)
pPT->a[i].u = 0;
if (!--cPresent)
if (!--cLeft)
return VINF_SUCCESS;
#ifdef VBOX_STRICT
case PGMPOOLKIND_ROOT_PAE_PD:
case PGMPOOLKIND_ROOT_PDPT:
case PGMPOOLKIND_ROOT_PML4:
case PGMPOOLKIND_ROOT_PAE_PD:
case PGMPOOLKIND_ROOT_PDPT:
case PGMPOOLKIND_ROOT_PML4:
AssertFatalMsgFailed(("enmKind=%d iUser=%#x iUserTable=%#x\n", pUserPage->enmKind, pUser->iUser, pUser->iUserTable));
while (i != NIL_PGMPOOL_USER_INDEX)
i = iNext;
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
return NULL;
return pPhysExt;
return iPhysExt | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
return iPhysExtStart | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
if (!--cMax)
return MM_RAM_FLAGS_IDX_OVERFLOWED | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
if (!pNew)
return MM_RAM_FLAGS_IDX_OVERFLOWED | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
LogFlow(("pgmPoolTrackPhysExtAddref: added new extent %d:{%d}->%d\n", iPhysExt, iShwPT, iPhysExtStart));
return iPhysExt | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
if (pPhysExt)
LogFlow(("pgmPoolTrackPhysExtAddref: new extent: %d:{%d, %d}\n", iPhysExt, u16 & MM_RAM_FLAGS_IDX_MASK, iShwPT));
u16 = iPhysExt | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
u16 = MM_RAM_FLAGS_IDX_OVERFLOWED | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
else if (u16 != (MM_RAM_FLAGS_IDX_OVERFLOWED | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT))))
return u16;
AssertFatalMsg(cRefs == MM_RAM_FLAGS_CREFS_PHYSEXT, ("cRefs=%d HCPhys=%RHp pPage=%p:{.idx=%d}\n", cRefs, pPhysPage->HCPhys, pPage, pPage->idx));
LogFlow(("pgmPoolTrackPhysExtDerefGCPhys: HCPhys=%RX64 idx=%d lonely\n", pPhysPage->HCPhys, pPage->idx));
LogFlow(("pgmPoolTrackPhysExtDerefGCPhys: HCPhys=%RX64 idx=%d head\n", pPhysPage->HCPhys, pPage->idx));
AssertFatalMsgFailed(("not-found! cRefs=%d HCPhys=%RHp pPage=%p:{.idx=%d}\n", cRefs, pPhysPage->HCPhys, pPage, pPage->idx));
static void pgmPoolTracDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhys)
while (pRam)
static void pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint)
while (pRam)
while (pRam)
while (iPage-- > 0)
DECLINLINE(void) pgmPoolTrackDerefPT32Bit32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT, PCX86PT pGstPT)
pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
DECLINLINE(void) pgmPoolTrackDerefPTPae32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PT pGstPT)
pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
DECLINLINE(void) pgmPoolTrackDerefPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PTPAE pGstPT)
pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PAE_PG_MASK);
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PAE_PG_MASK);
if (pSubPage)
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & X86_PDPE_PG_MASK);
if (pSubPage)
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
void *pvGst;
void *pvGst;
void *pvGst;
case PGMPOOLKIND_ROOT_PAE_PD:
case PGMPOOLKIND_ROOT_PML4:
case PGMPOOLKIND_ROOT_PDPT:
#ifdef IN_RING3
#ifdef PGMPOOL_WITH_MONITORING
#ifdef PGMPOOL_WITH_USER_TRACKING
#ifdef PGMPOOL_WITH_CACHE
#ifdef PGMPOOL_WITH_USER_TRACKING
for (unsigned i = 0; i < cMaxUsers; i++)
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
pRam;
while (iPage-- > 0)
for (unsigned i = 0; i < cMaxPhysExts; i++)
#ifdef PGMPOOL_WITH_MONITORING
#ifdef PGMPOOL_WITH_CACHE
#ifdef PGMPOOL_WITH_MONITORING
# ifdef PGMPOOL_WITH_CACHE
#ifdef PGMPOOL_WITH_USER_TRACKING
#ifdef PGMPOOL_WITH_CACHE
* @retval VERR_PGM_POOL_CLEARED if the deregistration of the physical handler will cause a light weight pool flush.
Log(("pgmPoolFlushPage: specaial root page, rejected. enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
return VINF_SUCCESS;
#ifdef PGMPOOL_WITH_USER_TRACKING
#ifdef PGMPOOL_WITH_CACHE
#ifdef PGMPOOL_WITH_MONITORING
return rc;
#ifdef PGMPOOL_WITH_USER_TRACKING
#ifdef PGMPOOL_WITH_CACHE
#ifdef IN_RING3
return rc;
return VINF_SUCCESS;
#ifdef PGMPOOL_WITH_CACHE
return VERR_PGM_POOL_FLUSHED;
int pgmPoolAlloc(PVM pVM, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint16_t iUserTable, PPPGMPOOLPAGE ppPage)
LogFlow(("pgmPoolAlloc: GCPhys=%VGp enmKind=%d iUser=%#x iUserTable=%#x\n", GCPhys, enmKind, iUser, iUserTable));
#ifdef PGMPOOL_WITH_CACHE
LogFlow(("pgmPoolAlloc: returns %Vrc *ppPage=%p:{.Key=%VHp, .idx=%d}\n", rc2, *ppPage, (*ppPage)->Core.Key, (*ppPage)->idx));
return rc2;
return rc;
pPool->cUsedPages++; /* physical handler registration / pgmPoolTrackFlushGCPhysPTsSlow requirement. */
#ifdef PGMPOOL_WITH_MONITORING
#ifdef PGMPOOL_WITH_USER_TRACKING
return rc3;
#ifdef VBOX_WITH_STATISTICS
LogFlow(("pgmPoolAlloc: returns %Vrc *ppPage=%p:{.Key=%VHp, .idx=%d, .fCached=%RTbool, .fMonitored=%RTbool}\n",
return rc;
return pPage;