PGMAllPool.cpp revision 0e907bfa48d3b7f7ab8c2ec437eb9871fcc5c104
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * PGM Shadow Page Pool.
c58f1213e628a545081c70e26c6b67a841cff880vboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * available from http://www.virtualbox.org. This file is free software;
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * you can redistribute it and/or modify it under the terms of the GNU
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * General Public License (GPL) as published by the Free Software
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
0d12c7f9423f2745f8e282523d0930f91bff03b3vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
0d12c7f9423f2745f8e282523d0930f91bff03b3vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
0d12c7f9423f2745f8e282523d0930f91bff03b3vboxsync * additional information or have any questions.
0d12c7f9423f2745f8e282523d0930f91bff03b3vboxsync/*******************************************************************************
0d12c7f9423f2745f8e282523d0930f91bff03b3vboxsync* Header Files *
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync*******************************************************************************/
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync/*******************************************************************************
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync* Internal Functions *
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync*******************************************************************************/
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsyncDECLINLINE(unsigned) pgmPoolTrackGetShadowEntrySize(PGMPOOLKIND enmKind);
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsyncDECLINLINE(unsigned) pgmPoolTrackGetGuestEntrySize(PGMPOOLKIND enmKind);
c2046db2cc346cc299f0cd9b2d1e160179159cfcvboxsyncstatic void pgmPoolTrackDeref(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsyncstatic void pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint);
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsyncstatic int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable);
c2046db2cc346cc299f0cd9b2d1e160179159cfcvboxsyncstatic void pgmPoolMonitorModifiedRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsyncDECLEXPORT(int) pgmPoolAccessHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser);
2d8870843ff566fee9bd3a6a5942414254106479vboxsync * Checks if the specified page pool kind is for a 4MB or 2MB guest page.
2d8870843ff566fee9bd3a6a5942414254106479vboxsync * @returns true if it's the shadow of a 4MB or 2MB guest page, otherwise false.
2d8870843ff566fee9bd3a6a5942414254106479vboxsync * @param enmKind The page kind.
2d8870843ff566fee9bd3a6a5942414254106479vboxsyncDECLINLINE(bool) pgmPoolIsBigPage(PGMPOOLKIND enmKind)
044af0d1e6474076366759db86f101778c5f20ccvboxsync return true;
044af0d1e6474076366759db86f101778c5f20ccvboxsync return false;
044af0d1e6474076366759db86f101778c5f20ccvboxsync * Maps a pool page into the current context.
044af0d1e6474076366759db86f101778c5f20ccvboxsync * @returns Pointer to the mapping.
044af0d1e6474076366759db86f101778c5f20ccvboxsync * @param pVM The VM handle.
044af0d1e6474076366759db86f101778c5f20ccvboxsync * @param pPage The page to map.
044af0d1e6474076366759db86f101778c5f20ccvboxsync /* general pages. */
044af0d1e6474076366759db86f101778c5f20ccvboxsync Assert(pPage->idx < pVM->pgm.s.pPoolGC->cCurPages);
044af0d1e6474076366759db86f101778c5f20ccvboxsync int rc = PGMGCDynMapHCPage(pVM, pPage->Core.Key, &pv);
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync /* special pages. */
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync AssertReleaseMsgFailed(("Invalid index %d\n", pPage->idx));
044af0d1e6474076366759db86f101778c5f20ccvboxsync#endif /* IN_GC */
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * Determin the size of a write instruction.
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * @returns number of bytes written.
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * @param pDis The disassembler state.
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsyncstatic unsigned pgmPoolDisasWriteSize(PDISCPUSTATE pDis)
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync * This is very crude and possibly wrong for some opcodes,
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync * but since it's not really supposed to be called we can
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync * probably live with that.
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync * Flushes a chain of pages sharing the same access monitor.
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync * @returns VBox status code suitable for scheduling.
a1df400bbe9d64aad400442e56eb637019300a5evboxsync * @param pPool The pool.
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync * @param pPage A page in the chain.
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsyncint pgmPoolMonitorChainFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
a1df400bbe9d64aad400442e56eb637019300a5evboxsync LogFlow(("pgmPoolMonitorChainFlush: Flush page %VGp type=%d\n", pPage->GCPhys, pPage->enmKind));
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync * Find the list head.
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * Iterate the list flushing each shadow page.
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync if (rc2 == VERR_PGM_POOL_CLEARED && rc == VINF_SUCCESS)
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync * Wrapper for getting the current context pointer to the entry being modified.
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync * @returns Pointer to the current context mapping of the entry.
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync * @param pPool The pool.
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync * @param pvFault The fault virtual address.
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync * @param GCPhysFault The fault physical address.
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync * @param cbEntry The entry size.
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsyncDECLINLINE(const void *) pgmPoolMonitorGCPtr2CCPtr(PPGMPOOL pPool, RTHCPTR pvFault, RTGCPHYS GCPhysFault, const unsigned cbEntry)
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsyncDECLINLINE(const void *) pgmPoolMonitorGCPtr2CCPtr(PPGMPOOL pPool, RTGCPTR pvFault, RTGCPHYS GCPhysFault, const unsigned cbEntry)
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync return (const void *)((RTGCUINTPTR)pvFault & ~(RTGCUINTPTR)(cbEntry - 1));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync int rc = pgmRamGCPhys2HCPtr(&pPool->pVMHC->pgm.s, GCPhysFault & ~(RTGCPHYS)(cbEntry - 1), &pvRet);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync return (RTHCPTR)((uintptr_t)pvFault & ~(RTHCUINTPTR)(cbEntry - 1));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync * Process shadow entries before they are changed by the guest.
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync * For PT entries we will clear them. For PD entries, we'll simply check
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync * for mapping conflicts and set the SyncCR3 FF if found.
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync * @param pPool The pool.
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync * @param pPage The head page.
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync * @param GCPhysFault The guest physical fault address.
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync * @param uAddress In R0 and GC this is the guest context fault address (flat).
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync * In R3 this is the host context 'fault' address.
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync * @param pCpu The disassembler state for figuring out the write size.
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync * This need not be specified if the caller knows we won't do cross entry accesses.
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsyncvoid pgmPoolMonitorChainChanging(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault, RTHCPTR pvAddress, PDISCPUSTATE pCpu)
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsyncvoid pgmPoolMonitorChainChanging(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault, RTGCPTR pvAddress, PDISCPUSTATE pCpu)
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync const unsigned off = GCPhysFault & PAGE_OFFSET_MASK;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync const unsigned cbWrite = (pCpu) ? pgmPoolDisasWriteSize(pCpu) : 0;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync LogFlow(("pgmPoolMonitorChainChanging: %VGv phys=%VGp kind=%d cbWrite=%d\n", pvAddress, GCPhysFault, pPage->enmKind, cbWrite));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTXSUFF(pVM), pPage);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync PCX86PTE pGstPte = (PCX86PTE)pgmPoolMonitorGCPtr2CCPtr(pPool, pvAddress, GCPhysFault, sizeof(*pGstPte));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync 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));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync /* page/2 sized */
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync const unsigned iShw = (off / sizeof(X86PTE)) & (X86_PG_PAE_ENTRIES - 1);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync PCX86PTE pGstPte = (PCX86PTE)pgmPoolMonitorGCPtr2CCPtr(pPool, pvAddress, GCPhysFault, sizeof(*pGstPte));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync 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));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync PCX86PTEPAE pGstPte = (PCX86PTEPAE)pgmPoolMonitorGCPtr2CCPtr(pPool, pvAddress, GCPhysFault, sizeof(*pGstPte));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync Log4(("pgmPoolMonitorChainChanging pae: deref %VHp GCPhys %VGp\n", uShw.pPTPae->a[iShw].u & X86_PTE_PAE_PG_MASK, pGstPte->u & X86_PTE_PAE_PG_MASK));
0109a2240391a89f6556b1545e6cc57f9efab060vboxsync /* paranoia / a bit assumptive. */
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PTEPAE);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync AssertReturnVoid(iShw2 < ELEMENTS(uShw.pPTPae->a));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync PCX86PTEPAE pGstPte = (PCX86PTEPAE)pgmPoolMonitorGCPtr2CCPtr(pPool, pvAddress, GCPhysFault, sizeof(*pGstPte));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync Log4(("pgmPoolMonitorChainChanging pae: deref %VHp GCPhys %VGp\n", uShw.pPTPae->a[iShw2].u & X86_PTE_PAE_PG_MASK, pGstPte->u & X86_PTE_PAE_PG_MASK));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync const unsigned iShw = off / sizeof(X86PTE); // ASSUMING 32-bit guest paging!
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync /* paranoia / a bit assumptive. */
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PTE);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync#if 0 /* useful when running PGMAssertCR3(), a bit too troublesome for general use (TLBs). */
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync && !VM_FF_ISSET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3))
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync LogFlow(("pgmPoolMonitorChainChanging: iShw=%#x: %RX32 -> freeing it!\n", iShw, uShw.pPD->a[iShw].u));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync# ifdef IN_GC /* TLB load - we're pushing things a bit... */
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync pgmPoolFree(pPool->CTXSUFF(pVM), uShw.pPD->a[iShw].u & X86_PDE_PG_MASK, pPage->idx, iShw);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync unsigned iShw = (off / sizeof(X86PTE)) * 2; // ASSUMING 32-bit guest paging!
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync if ((uShw.pPDPae->a[iShw].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync /* paranoia / a bit assumptive. */
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync && (uShw.pPDPae->a[iShw2].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync#if 0 /* useful when running PGMAssertCR3(), a bit too troublesome for general use (TLBs). */
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync && !VM_FF_ISSET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3))
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync LogFlow(("pgmPoolMonitorChainChanging: iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync# ifdef IN_GC /* TLB load - we're pushing things a bit... */
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync pgmPoolFree(pPool->CTXSUFF(pVM), uShw.pPDPae->a[iShw].u & X86_PDE_PAE_PG_MASK, pPage->idx, iShw);
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
9af7167fa39f1f139899c989da973e59b9cccc3fvboxsync#ifdef PGMPOOL_INVALIDATE_UPPER_SHADOW_TABLE_ENTRIES
9af7167fa39f1f139899c989da973e59b9cccc3fvboxsync /* causes trouble when the guest uses a PDE to refer to the whole page table level structure. (invalidate here; faults later on when it tries
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync * to change the page table entries
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync * -> recheck; probably only applies to the GC case
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync /* Note: hardcoded PAE implementation dependency */
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync (pPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD) ? PGMPOOL_IDX_PAE_PD : pPage->idx,
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync (pPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD) ? iShw + (pPage->idx - PGMPOOL_IDX_PAE_PD_0) * X86_PG_PAE_ENTRIES : iShw);
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync /* paranoia / a bit assumptive. */
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDEPAE);
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync AssertReturnVoid(iShw2 < ELEMENTS(uShw.pPDPae->a));
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync#ifdef PGMPOOL_INVALIDATE_UPPER_SHADOW_TABLE_ENTRIES
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync /* Note: hardcoded PAE implementation dependency */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync (pPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD) ? PGMPOOL_IDX_PAE_PD : pPage->idx,
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync (pPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD) ? iShw2 + (pPage->idx - PGMPOOL_IDX_PAE_PD_0) * X86_PG_PAE_ENTRIES : iShw2);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync /* Hopefully this doesn't happen very often:
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * - touching unused parts of the page
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * - messing with the bits of pd pointers without changing the physical address
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync if (iShw < X86_PG_PAE_PDPE_ENTRIES) /* don't use ELEMENTS(uShw.pPDPT->a), because that's for long mode only */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync /* paranoia / a bit assumptive. */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDPE);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync Assert(pPage->enmKind == PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
31771163041e3661403a806eb3382d2a165c003bvboxsync /* paranoia / a bit assumptive. */
31771163041e3661403a806eb3382d2a165c003bvboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDEPAE);
31771163041e3661403a806eb3382d2a165c003bvboxsync AssertReturnVoid(iShw2 < ELEMENTS(uShw.pPDPae->a));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync /* Hopefully this doesn't happen very often:
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * - messing with the bits of pd pointers without changing the physical address
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync if (!VM_FF_ISSET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3))
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPT->a[iShw].u));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync pgmPoolFree(pPool->CTXSUFF(pVM), uShw.pPDPT->a[iShw].u & X86_PDPE_PG_MASK, pPage->idx, iShw);
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync /* paranoia / a bit assumptive. */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDPE);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPT->a[iShw2].u));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync pgmPoolFree(pPool->CTXSUFF(pVM), uShw.pPDPT->a[iShw2].u & X86_PDPE_PG_MASK, pPage->idx, iShw2);
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync /* Hopefully this doesn't happen very often:
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync * - messing with the bits of pd pointers without changing the physical address
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync if (!VM_FF_ISSET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3))
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPML4->a[iShw].u));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync pgmPoolFree(pPool->CTXSUFF(pVM), uShw.pPML4->a[iShw].u & X86_PML4E_PG_MASK, pPage->idx, iShw);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync /* paranoia / a bit assumptive. */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PML4E);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPML4->a[iShw2].u));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync pgmPoolFree(pPool->CTXSUFF(pVM), uShw.pPML4->a[iShw2].u & X86_PML4E_PG_MASK, pPage->idx, iShw2);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync#endif /* IN_RING0 */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync AssertFatalMsgFailed(("enmKind=%d\n", pPage->enmKind));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * Checks if a access could be a fork operation in progress.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * Meaning, that the guest is setuping up the parent process for Copy-On-Write.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @returns true if it's likly that we're forking, otherwise false.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @param pPool The pool.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @param pCpu The disassembled instruction.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @param offFault The access offset.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsyncDECLINLINE(bool) pgmPoolMonitorIsForking(PPGMPOOL pPool, PDISCPUSTATE pCpu, unsigned offFault)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * i386 linux is using btr to clear X86_PTE_RW.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * The functions involved are (2.6.16 source inspection):
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * clear_bit
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * ptep_set_wrprotect
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * copy_one_pte
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * copy_pte_range
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * copy_pmd_range
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * copy_pud_range
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * copy_page_range
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * copy_process
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync /** @todo Validate that the bit index is X86_PTE_RW. */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync STAM_COUNTER_INC(&pPool->CTXMID(StatMonitor,Fork));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync return true;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync return false;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * Determine whether the page is likely to have been reused.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @returns true if we consider the page as being reused for a different purpose.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @returns false if we consider it to still be a paging page.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @param pPage The page in question.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @param pRegFrame Trap register frame.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @param pCpu The disassembly info for the faulting insturction.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @param pvFault The fault address.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @remark The REP prefix check is left to the caller because of STOSD/W.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsyncDECLINLINE(bool) pgmPoolMonitorIsReused(PPGMPOOLPAGE pPage, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu, RTGCPTR pvFault)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync return true;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync return true;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync return true;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync return true;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync return true;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync case OP_MOVNTDQ: /* solaris - hwblkclr & hwblkpagecopy */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync return true;
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync return true;
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync return false;
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync return true;
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync //if (pPage->fCR3Mix)
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync // return false;
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync return false;
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync * Flushes the page being accessed.
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync * @returns VBox status code suitable for scheduling.
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync * @param pVM The VM handle.
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync * @param pPool The pool.
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync * @param pPage The pool page (head).
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync * @param pCpu The disassembly of the write instruction.
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync * @param pRegFrame The trap register frame.
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync * @param GCPhysFault The fault address as guest physical address.
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync * @param pvFault The fault address.
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsyncstatic int pgmPoolAccessHandlerFlush(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * First, do the flushing.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * Emulate the instruction (xp/w2k problem, requires pc/cr2/sp detection).
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync int rc2 = EMInterpretInstructionCPU(pVM, pCpu, pRegFrame, pvFault, &cbWritten);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync if (PATMIsPatchGCAddr(pVM, (RTRCPTR)pRegFrame->eip))
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for patch code %04x:%RGv, ignoring.\n",
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync STAM_COUNTER_INC(&pPool->StatMonitorGCIntrFailPatch2);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync STAM_COUNTER_INC(&pPool->CTXMID(StatMonitor,EmulateInstr));
9d020a0622f95aec3aabaff436a495e88dbbd71avboxsync /* See use in pgmPoolAccessHandlerSimple(). */
2d8870843ff566fee9bd3a6a5942414254106479vboxsync LogFlow(("pgmPoolAccessHandlerPT: returns %Vrc (flushed)\n", rc));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * Handles the STOSD write accesses.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @returns VBox status code suitable for scheduling.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @param pVM The VM handle.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @param pPool The pool.
2d8870843ff566fee9bd3a6a5942414254106479vboxsync * @param pPage The pool page (head).
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @param pCpu The disassembly of the write instruction.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @param pRegFrame The trap register frame.
a1df400bbe9d64aad400442e56eb637019300a5evboxsync * @param GCPhysFault The fault address as guest physical address.
a1df400bbe9d64aad400442e56eb637019300a5evboxsync * @param pvFault The fault address.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsyncDECLINLINE(int) pgmPoolAccessHandlerSTOSD(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
2d8870843ff566fee9bd3a6a5942414254106479vboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * Increment the modification counter and insert it into the list
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * of modified pages the first time.
9d020a0622f95aec3aabaff436a495e88dbbd71avboxsync * Execute REP STOSD.
9d020a0622f95aec3aabaff436a495e88dbbd71avboxsync * This ASSUMES that we're not invoked by Trap0e on in a out-of-sync
9d020a0622f95aec3aabaff436a495e88dbbd71avboxsync * write situation, meaning that it's safe to write here.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync pgmPoolMonitorChainChanging(pPool, pPage, GCPhysFault, (RTGCPTR)pu32, NULL);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync PGMPhysWriteGCPhys(pVM, GCPhysFault, &pRegFrame->eax, 4);
9d020a0622f95aec3aabaff436a495e88dbbd71avboxsync /* See use in pgmPoolAccessHandlerSimple(). */
044af0d1e6474076366759db86f101778c5f20ccvboxsync * Handles the simple write accesses.
044af0d1e6474076366759db86f101778c5f20ccvboxsync * @returns VBox status code suitable for scheduling.
044af0d1e6474076366759db86f101778c5f20ccvboxsync * @param pVM The VM handle.
044af0d1e6474076366759db86f101778c5f20ccvboxsync * @param pPool The pool.
044af0d1e6474076366759db86f101778c5f20ccvboxsync * @param pPage The pool page (head).
044af0d1e6474076366759db86f101778c5f20ccvboxsync * @param pCpu The disassembly of the write instruction.
044af0d1e6474076366759db86f101778c5f20ccvboxsync * @param pRegFrame The trap register frame.
044af0d1e6474076366759db86f101778c5f20ccvboxsync * @param GCPhysFault The fault address as guest physical address.
044af0d1e6474076366759db86f101778c5f20ccvboxsync * @param pvFault The fault address.
044af0d1e6474076366759db86f101778c5f20ccvboxsyncDECLINLINE(int) pgmPoolAccessHandlerSimple(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
682342827b0e80c493c820603508e79e76c42658vboxsync * Increment the modification counter and insert it into the list
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync * of modified pages the first time.
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync * Clear all the pages. ASSUMES that pvFault is readable.
682342827b0e80c493c820603508e79e76c42658vboxsync pgmPoolMonitorChainChanging(pPool, pPage, GCPhysFault, pvFault, pCpu);
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync * Interpret the instruction.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync int rc = EMInterpretInstructionCPU(pVM, pCpu, pRegFrame, pvFault, &cb);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for patch code %04x:%RGv - opcode=%d\n",
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync pRegFrame->cs, (RTGCPTR)pRegFrame->rip, pCpu->pCurInstr->opcode));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync STAM_COUNTER_INC(&pPool->CTXMID(StatMonitor,EmulateInstr));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * Quick hack, with logging enabled we're getting stale
682342827b0e80c493c820603508e79e76c42658vboxsync * code TLBs but no data TLB for EIP and crash in EMInterpretDisasOne.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * Flushing here is BAD and expensive, I think EMInterpretDisasOne will
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * have to be fixed to support this. But that'll have to wait till next week.
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * An alternative is to keep track of the changed PTEs together with the
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * GCPhys from the guest PT. This may proove expensive though.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * At the moment, it's VITAL that it's done AFTER the instruction interpreting
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * because we need the stale TLBs in some cases (XP boot). This MUST be fixed properly!
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync LogFlow(("pgmPoolAccessHandlerSimple: returns %Vrc cb=%d\n", rc, cb));
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * \#PF Handler callback for PT write accesses.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @returns VBox status code (appropriate for GC return).
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @param pVM VM Handle.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @param uErrorCode CPU Error code.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @param pRegFrame Trap register frame.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * NULL on DMA and other non CPU access.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @param pvFault The fault address (cr2).
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @param GCPhysFault The GC physical address corresponding to pvFault.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @param pvUser User argument.
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsyncDECLEXPORT(int) pgmPoolAccessHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser)
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync STAM_PROFILE_START(&pVM->pgm.s.CTXSUFF(pPool)->CTXSUFF(StatMonitor), a);
830a019ad79a45e6bf7a5419efd5a729a36e599evboxsync LogFlow(("pgmPoolAccessHandler: pvFault=%VGv pPage=%p:{.idx=%d} GCPhysFault=%VGp\n", pvFault, pPage, pPage->idx, GCPhysFault));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync * We should ALWAYS have the list head as user parameter. This
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync * is because we use that page to record the changes.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * Disassemble the faulting instruction.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync int rc = EMInterpretDisasOne(pVM, pRegFrame, &Cpu, NULL);
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * Check if it's worth dealing with.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync bool fReused = false;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync if ( ( pPage->cModifications < 48 /** @todo #define */ /** @todo need to check that it's not mapping EIP. */ /** @todo adjust this! */
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync && !(fReused = pgmPoolMonitorIsReused(pPage, pRegFrame, &Cpu, pvFault))
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync && !pgmPoolMonitorIsForking(pPool, &Cpu, GCPhysFault & PAGE_OFFSET_MASK))
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * Simple instructions, no REP prefix.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync rc = pgmPoolAccessHandlerSimple(pVM, pPool, pPage, &Cpu, pRegFrame, GCPhysFault, pvFault);
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTXSUFF(pPool)->CTXSUFF(StatMonitor), &pPool->CTXMID(StatMonitor,Handled), a);
31771163041e3661403a806eb3382d2a165c003bvboxsync * Windows is frequently doing small memset() operations (netio test 4k+).
31771163041e3661403a806eb3382d2a165c003bvboxsync * We have to deal with these or we'll kill the cache and performance.
31771163041e3661403a806eb3382d2a165c003bvboxsync && pRegFrame->ecx * 4 <= PAGE_SIZE - ((uintptr_t)pvFault & PAGE_OFFSET_MASK)
31771163041e3661403a806eb3382d2a165c003bvboxsync && (pRegFrame->eax == 0 || pRegFrame->eax == 0x80) /* the two values observed. */
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync rc = pgmPoolAccessHandlerSTOSD(pVM, pPool, pPage, &Cpu, pRegFrame, GCPhysFault, pvFault);
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTXSUFF(pPool)->CTXSUFF(StatMonitor), &pPool->CTXMID(StatMonitor,RepStosd), a);
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync /* REP prefix, don't bother. */
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync STAM_COUNTER_INC(&pPool->CTXMID(StatMonitor,RepPrefix));
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync Log4(("pgmPoolAccessHandler: eax=%#x ecx=%#x edi=%#x esi=%#x eip=%VGv opcode=%d prefix=%#x\n",
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync pRegFrame->eax, pRegFrame->ecx, pRegFrame->edi, pRegFrame->esi, pRegFrame->rip, Cpu.pCurInstr->opcode, Cpu.prefix));
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync * Not worth it, so flush it.
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync * If we considered it to be reused, don't to back to ring-3
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync * to emulate failed instructions since we usually cannot
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync * interpret then. This may be a bit risky, in which case
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync * the reuse detection must be fixed.
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync rc = pgmPoolAccessHandlerFlush(pVM, pPool, pPage, &Cpu, pRegFrame, GCPhysFault, pvFault);
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTXSUFF(pPool)->CTXSUFF(StatMonitor), &pPool->CTXMID(StatMonitor,FlushPage), a);
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync# endif /* !IN_RING3 */
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync#endif /* PGMPOOL_WITH_MONITORING */
d90eec53c9dcaa0f3d1054e8734ed46875b9093avboxsync * Inserts a page into the GCPhys hash table.
d90eec53c9dcaa0f3d1054e8734ed46875b9093avboxsync * @param pPool The pool.
d90eec53c9dcaa0f3d1054e8734ed46875b9093avboxsync * @param pPage The page.
d90eec53c9dcaa0f3d1054e8734ed46875b9093avboxsyncDECLINLINE(void) pgmPoolHashInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
d90eec53c9dcaa0f3d1054e8734ed46875b9093avboxsync Log3(("pgmPoolHashInsert: %VGp\n", pPage->GCPhys));
d90eec53c9dcaa0f3d1054e8734ed46875b9093avboxsync Assert(pPage->GCPhys != NIL_RTGCPHYS); Assert(pPage->iNext == NIL_PGMPOOL_IDX);
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * Removes a page from the GCPhys hash table.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @param pPool The pool.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @param pPage The page.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsyncDECLINLINE(void) pgmPoolHashRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync Log3(("pgmPoolHashRemove: %VGp\n", pPage->GCPhys));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync AssertReleaseMsgFailed(("GCPhys=%VGp idx=%#x\n", pPage->GCPhys, pPage->idx));
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync * Frees up one cache page.
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync * @returns VBox status code.
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync * @retval VINF_SUCCESS on success.
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync * @retval VERR_PGM_POOL_CLEARED if the deregistration of a physical handler will cause a light weight pool flush.
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync * @param pPool The pool.
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync * @param iUser The user index.
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsyncstatic int pgmPoolCacheFreeOne(PPGMPOOL pPool, uint16_t iUser)
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync Assert(pPool->iAgeHead != pPool->iAgeTail); /* We shouldn't be here if there < 2 cached entries! */
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * Select one page from the tail of the age list.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync/* This is the alternative to the SyncCR3 pgmPoolCacheUsed calls.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync if (pPool->aPages[iToFree].iUserHead != NIL_PGMPOOL_USER_INDEX)
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync uint16_t i = pPool->aPages[iToFree].iAgePrev;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync for (unsigned j = 0; j < 10 && i != NIL_PGMPOOL_USER_INDEX; j++, i = pPool->aPages[i].iAgePrev)
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync if (pPool->aPages[iToFree].iUserHead == NIL_PGMPOOL_USER_INDEX)
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync iToFree = i;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync int rc = pgmPoolFlushPage(pPool, &pPool->aPages[iToFree]);
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * Checks if a kind mismatch is really a page being reused
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * or if it's just normal remappings.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @returns true if reused and the cached page (enmKind1) should be flushed
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @returns false if not reused.
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * @param enmKind1 The kind of the cached page.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @param enmKind2 The kind of the requested page.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsyncstatic bool pgmPoolCacheReusedByKind(PGMPOOLKIND enmKind1, PGMPOOLKIND enmKind2)
044af0d1e6474076366759db86f101778c5f20ccvboxsync * Never reuse them. There is no remapping in non-paging mode.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync return true;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * It's perfectly fine to reuse these, except for PAE and non-paging stuff.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync return true;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync return false;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * It's perfectly fine to reuse these, except for PAE and non-paging stuff.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync return true;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync return false;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * These cannot be flushed, and it's common to reuse the PDs as PTs.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync return false;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Attempts to satisfy a pgmPoolAlloc request from the cache.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @returns VBox status code.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @retval VINF_PGM_CACHED_PAGE on success.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @retval VERR_FILE_NOT_FOUND if not found.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param pPool The pool.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param GCPhys The GC physical address of the page we're gonna shadow.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param enmKind The kind of mapping.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param iUser The shadow page pool index of the user table.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param iUserTable The index into the user table (shadowed).
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param ppPage Where to store the pointer to the page.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsyncstatic int pgmPoolCacheAlloc(PPGMPOOL pPool, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Look up the GCPhys in the hash.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync Log3(("pgmPoolCacheAlloc: %VGp kind %d iUser=%d iUserTable=%x SLOT=%d\n", GCPhys, enmKind, iUser, iUserTable, i));
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync Log3(("pgmPoolCacheAlloc: slot %d found page %VGp\n", i, pPage->GCPhys));
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync int rc = pgmPoolTrackAddUser(pPool, pPage, iUser, iUserTable);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * The kind is different. In some cases we should now flush the page
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * as it has been reused, but in most cases this is normal remapping
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * of PDs as PT or big pages using the GCPhys field in a slightly
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * different way than the other kinds.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync if (pgmPoolCacheReusedByKind((PGMPOOLKIND)pPage->enmKind, enmKind))
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pgmPoolFlushPage(pPool, pPage); /* ASSUMES that VERR_PGM_POOL_CLEARED will be returned by pgmPoolTracInsert. */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync } while (i != NIL_PGMPOOL_IDX);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync Log3(("pgmPoolCacheAlloc: Missed GCPhys=%RGp enmKind=%d\n", GCPhys, enmKind));
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Inserts a page into the cache.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param pPool The pool.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param pPage The cached page.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param fCanBeCached Set if the page is fit for caching from the caller's point of view.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsyncstatic void pgmPoolCacheInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, bool fCanBeCached)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Insert into the GCPhys hash if the page is fit for that.
2d8870843ff566fee9bd3a6a5942414254106479vboxsync Log3(("pgmPoolCacheInsert: Caching %p:{.Core=%RHp, .idx=%d, .enmKind=%d, GCPhys=%RGp}\n",
2d8870843ff566fee9bd3a6a5942414254106479vboxsync pPage, pPage->Core.Key, pPage->idx, pPage->enmKind, pPage->GCPhys));
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync Log3(("pgmPoolCacheInsert: Not caching %p:{.Core=%RHp, .idx=%d, .enmKind=%d, GCPhys=%RGp}\n",
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPage, pPage->Core.Key, pPage->idx, pPage->enmKind, pPage->GCPhys));
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Insert at the head of the age list.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPool->aPages[pPool->iAgeHead].iAgePrev = pPage->idx;
2d8870843ff566fee9bd3a6a5942414254106479vboxsync * Flushes a cached page.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param pPool The pool.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param pPage The cached page.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsyncstatic void pgmPoolCacheFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
2d8870843ff566fee9bd3a6a5942414254106479vboxsync Log3(("pgmPoolCacheFlushPage: %VGp\n", pPage->GCPhys));
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Remove the page from the hash.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Remove it from the age list.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPool->aPages[pPage->iAgeNext].iAgePrev = pPage->iAgePrev;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPool->aPages[pPage->iAgePrev].iAgeNext = pPage->iAgeNext;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync#endif /* PGMPOOL_WITH_CACHE */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Looks for pages sharing the monitor.
a1df400bbe9d64aad400442e56eb637019300a5evboxsync * @returns Pointer to the head page.
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * @returns NULL if not found.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param pPool The Pool
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param pNewPage The page which is going to be monitored.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsyncstatic PPGMPOOLPAGE pgmPoolMonitorGetPageByGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pNewPage)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Look up the GCPhys in the hash.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync RTGCPHYS GCPhys = pNewPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync /* find the head */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync /* ignore, no monitoring. */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync AssertFatalMsgFailed(("enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync } while (i != NIL_PGMPOOL_IDX);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Enabled write monitoring of a guest page.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @returns VBox status code.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @retval VINF_SUCCESS on success.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @retval VERR_PGM_POOL_CLEARED if the registration of the physical handler will cause a light weight pool flush.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param pPool The pool.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param pPage The cached page.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsyncstatic int pgmPoolMonitorInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync LogFlow(("pgmPoolMonitorInsert %VGp\n", pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1)));
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Filter out the relevant kinds.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync /* Nothing to monitor here. */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Install handler.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync PPGMPOOLPAGE pPageHead = pgmPoolMonitorGetPageByGCPhys(pPool, pPage);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync Assert(pPageHead != pPage); Assert(pPageHead->iMonitoredNext != pPage->idx);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPool->aPages[pPageHead->iMonitoredNext].iMonitoredPrev = pPage->idx;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync Assert(pPage->iMonitoredNext == NIL_PGMPOOL_IDX); Assert(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync const RTGCPHYS GCPhysPage = pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync rc = PGMHandlerPhysicalRegisterEx(pVM, PGMPHYSHANDLERTYPE_PHYSICAL_WRITE,
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pPage),
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pPage),
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync pPool->pfnAccessHandlerGC, MMHyperCCToGC(pVM, pPage),
45c3e41d012100c5f4a3f77e391e4c6da8f5b97avboxsync /** @todo we should probably deal with out-of-memory conditions here, but for now increasing
45c3e41d012100c5f4a3f77e391e4c6da8f5b97avboxsync * the heap size should suffice. */
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync if (pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync * Disables write monitoring of a guest page.
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync * @returns VBox status code.
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync * @retval VINF_SUCCESS on success.
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync * @retval VERR_PGM_POOL_CLEARED if the deregistration of the physical handler will cause a light weight pool flush.
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync * @param pPool The pool.
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync * @param pPage The cached page.
4dcf37df51439573b55cc16f53590c7b68cb1051vboxsyncstatic int pgmPoolMonitorFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync * Filter out the relevant kinds.
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync /* Nothing to monitor here. */
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync * Remove the page from the monitored list or uninstall it if last.
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync PPGMPOOLPAGE pNewHead = &pPool->aPages[pPage->iMonitoredNext];
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync rc = PGMHandlerPhysicalChangeCallbacks(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1),
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pNewHead),
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pNewHead),
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync pPool->pfnAccessHandlerGC, MMHyperCCToGC(pVM, pNewHead),
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync pPool->aPages[pPage->iMonitoredPrev].iMonitoredNext = pPage->iMonitoredNext;
560a1042a968cf73161ac78555eddc8d16e8bebdvboxsync pPool->aPages[pPage->iMonitoredNext].iMonitoredPrev = pPage->iMonitoredPrev;
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync rc = PGMHandlerPhysicalDeregister(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1));
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsync if (pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)
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)
#ifndef DEBUG_michael
AssertMsg(cModifiedPages == pPool->cModifiedPages, ("%d != %d\n", cModifiedPages, pPool->cModifiedPages));
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
pRam;
while (iPage-- > 0)
for (unsigned i = 0; i < cMaxPhysExts; i++)
# ifndef IN_GC
return VINF_PGM_SYNC_CR3;
return VINF_SUCCESS;
#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, uint32_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, uint32_t iUserTable)
# ifdef VBOX_STRICT
AssertMsg(paUsers[i].iUser != iUser || paUsers[i].iUserTable != iUserTable, ("%x %x vs new %x %x\n", paUsers[i].iUser, paUsers[i].iUserTable, iUser, iUserTable));
} while (i != NIL_PGMPOOL_USER_INDEX);
if (i == NIL_PGMPOOL_USER_INDEX)
return rc;
# ifdef PGMPOOL_WITH_CACHE
return VINF_SUCCESS;
static void pgmPoolTrackFreeUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
if ( i != NIL_PGMPOOL_USER_INDEX
while (i != NIL_PGMPOOL_USER_INDEX)
iPrev = i;
switch (enmKind)
case PGMPOOLKIND_ROOT_PAE_PD:
case PGMPOOLKIND_ROOT_PDPT:
case PGMPOOLKIND_ROOT_NESTED:
switch (enmKind)
case PGMPOOLKIND_ROOT_PAE_PD:
case PGMPOOLKIND_ROOT_PDPT:
case PGMPOOLKIND_ROOT_NESTED:
AssertFailed();
#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_NESTED:
case PGMPOOLKIND_ROOT_PAE_PD:
case PGMPOOLKIND_ROOT_PDPT:
case PGMPOOLKIND_ROOT_NESTED:
AssertFatalMsgFailed(("enmKind=%d iUser=%#x iUserTable=%#x\n", pUserPage->enmKind, pUser->iUser, pUser->iUserTable));
while (i != NIL_PGMPOOL_USER_INDEX)
i = iNext;
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
return NULL;
return pPhysExt;
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)
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPML4->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_PDPT:
case PGMPOOLKIND_ROOT_NESTED:
#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: special root page, rejected. enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
return VINF_SUCCESS;
Log(("pgmPoolFlushPage: current active shadow CR3, 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, uint32_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: cached 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;