PGMAllPool.cpp revision 7ffaa1c47a862e0f3cab608bdf6834e06706d735
/* $Id$ */
/** @file
* PGM Shadow Page Pool.
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_PGM_POOL
#ifdef IN_RC
#endif
#include "PGMInternal.h"
#include <VBox/disopcode.h>
#include <VBox/hwacc_vmx.h>
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
#ifdef PGMPOOL_WITH_USER_TRACKING
#endif
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
static void pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint);
#endif
#ifdef PGMPOOL_WITH_CACHE
static int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable);
#endif
#ifdef PGMPOOL_WITH_MONITORING
#endif
#ifndef IN_RING3
DECLEXPORT(int) pgmPoolAccessHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser);
#endif
#ifdef LOG_ENABLED
#endif
/**
* Checks if the specified page pool kind is for a 4MB or 2MB guest page.
*
* @returns true if it's the shadow of a 4MB or 2MB guest page, otherwise false.
* @param enmKind The page kind.
*/
{
switch (enmKind)
{
return true;
default:
return false;
}
}
/** @def PGMPOOL_PAGE_2_LOCKED_PTR
* Maps a pool page pool into the current context and lock it (RC only).
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pPage The pool page.
*
* @remark In RC this uses PGMGCDynMapHCPage(), so it will consume of the
* small page window employeed by that function. Be careful.
* @remark There is no need to assert on the result.
*/
#if defined(IN_RC)
{
/* Make sure the dynamic mapping will not be reused. */
if (pv)
return pv;
}
#else
#endif
/** @def PGMPOOL_UNLOCK_PTR
* Unlock a previously locked dynamic caching (RC only).
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pPage The pool page.
*
* @remark In RC this uses PGMGCDynMapHCPage(), so it will consume of the
* small page window employeed by that function. Be careful.
* @remark There is no need to assert on the result.
*/
#if defined(IN_RC)
{
if (pvPage)
}
#else
#endif
#if !defined(VBOX_WITH_PGMPOOL_PAGING_ONLY) && (defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0))
/**
* Maps a pool page into the current context.
*
* @returns Pointer to the mapping.
* @param pPGM Pointer to the PGM instance data.
* @param pPage The page to map.
*/
{
/* general pages are take care of by the inlined part, it
only ends up here in case of failure. */
/** @todo make sure HCPhys is valid for *all* indexes. */
/* special pages. */
# ifdef IN_RC
{
case PGMPOOL_IDX_PD:
return pPGM->pShw32BitPdRC;
case PGMPOOL_IDX_PAE_PD:
case PGMPOOL_IDX_PAE_PD_0:
return pPGM->apShwPaePDsRC[0];
case PGMPOOL_IDX_PAE_PD_1:
case PGMPOOL_IDX_PAE_PD_2:
case PGMPOOL_IDX_PAE_PD_3:
case PGMPOOL_IDX_PDPT:
return pPGM->pShwPaePdptRC;
default:
return NULL;
}
# else /* VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
{
case PGMPOOL_IDX_PD:
break;
case PGMPOOL_IDX_PAE_PD_0:
break;
case PGMPOOL_IDX_PAE_PD_1:
break;
case PGMPOOL_IDX_PAE_PD_2:
break;
case PGMPOOL_IDX_PAE_PD_3:
break;
case PGMPOOL_IDX_PDPT:
break;
case PGMPOOL_IDX_NESTED_ROOT:
break;
case PGMPOOL_IDX_PAE_PD:
AssertReleaseMsgFailed(("PGMPOOL_IDX_PAE_PD is not usable in VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 context\n"));
return NULL;
default:
return NULL;
}
void *pv;
return pv;
# endif /* VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
}
#endif /* IN_RC || VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
#ifdef PGMPOOL_WITH_MONITORING
/**
* Determin the size of a write instruction.
* @returns number of bytes written.
* @param pDis The disassembler state.
*/
{
/*
* This is very crude and possibly wrong for some opcodes,
* but since it's not really supposed to be called we can
* probably live with that.
*/
}
/**
* Flushes a chain of pages sharing the same access monitor.
*
* @returns VBox status code suitable for scheduling.
* @param pPool The pool.
* @param pPage A page in the chain.
*/
{
/*
* Find the list head.
*/
{
{
}
}
/*
* Iterate the list flushing each shadow page.
*/
int rc = VINF_SUCCESS;
for (;;)
{
{
}
/* next */
if (idx == NIL_PGMPOOL_IDX)
break;
}
return rc;
}
/**
* Wrapper for getting the current context pointer to the entry being modified.
*
* @returns VBox status code suitable for scheduling.
* @param pVM VM Handle.
* @param pvDst Destination address
* @param pvSrc Source guest virtual address.
* @param GCPhysSrc The source guest physical address.
* @param cb Size of data to read
*/
DECLINLINE(int) pgmPoolPhysSimpleReadGCPhys(PVM pVM, void *pvDst, CTXTYPE(RTGCPTR, RTHCPTR, RTGCPTR) pvSrc, RTGCPHYS GCPhysSrc, size_t cb)
{
#if defined(IN_RING3)
return VINF_SUCCESS;
#else
/* @todo in RC we could attempt to use the virtual address, although this can cause many faults (PAE Windows XP guest). */
#endif
}
/**
* Process shadow entries before they are changed by the guest.
*
* For PT entries we will clear them. For PD entries, we'll simply check
* for mapping conflicts and set the SyncCR3 FF if found.
*
* @param pPool The pool.
* @param pPage The head page.
* @param GCPhysFault The guest physical fault address.
* @param uAddress In R0 and GC this is the guest context fault address (flat).
* In R3 this is the host context 'fault' address.
* @param pCpu The disassembler state for figuring out the write size.
* This need not be specified if the caller knows we won't do cross entry accesses.
*/
void pgmPoolMonitorChainChanging(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault, CTXTYPE(RTGCPTR, RTHCPTR, RTGCPTR) pvAddress, PDISCPUSTATE pCpu)
{
LogFlow(("pgmPoolMonitorChainChanging: %RGv phys=%RGp kind=%s cbWrite=%d\n", (RTGCPTR)pvAddress, GCPhysFault, pgmPoolPoolKindToStr(pPage->enmKind), cbWrite));
for (;;)
{
union
{
void *pv;
} uShw;
{
{
{
# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
Log4(("pgmPoolMonitorChainChanging 32_32: deref %016RX64 GCPhys %08RX32\n", uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK, GstPte.u & X86_PTE_PG_MASK));
GstPte.u & X86_PTE_PG_MASK);
# endif
}
break;
}
/* page/2 sized */
{
{
{
# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
Log4(("pgmPoolMonitorChainChanging pae_32: deref %016RX64 GCPhys %08RX32\n", uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK, GstPte.u & X86_PTE_PG_MASK));
GstPte.u & X86_PTE_PG_MASK);
# endif
}
}
break;
}
# ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
{
LogFlow(("pgmPoolMonitorChainChanging PAE for 32 bits: iGst=%x iShw=%x idx = %d page idx=%d\n", iGst, iShw, iShwPdpt, pPage->enmKind - PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD));
{
for (unsigned i = 0; i < 2; i++)
{
# ifndef IN_RING0
if ((uShw.pPDPae->a[iShw + i].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
{
LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShwPdpt=%#x iShw=%#x!\n", iShwPdpt, iShw+i));
break;
}
else
# endif /* !IN_RING0 */
{
LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw+i, uShw.pPDPae->a[iShw+i].u));
iShw + i);
}
/* paranoia / a bit assumptive. */
if ( pCpu
&& (off & 3)
{
{
# ifndef IN_RING0
if ((uShw.pPDPae->a[iShw2].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
{
LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShwPdpt=%#x iShw2=%#x!\n", iShwPdpt, iShw2));
break;
}
else
# endif /* !IN_RING0 */
{
LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
iShw2);
}
}
}
}
}
break;
}
# endif
{
{
# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
Log4(("pgmPoolMonitorChainChanging pae: deref %016RX64 GCPhys %016RX64\n", uShw.pPTPae->a[iShw].u & X86_PTE_PAE_PG_MASK, GstPte.u & X86_PTE_PAE_PG_MASK));
GstPte.u & X86_PTE_PAE_PG_MASK);
# endif
}
/* paranoia / a bit assumptive. */
if ( pCpu
&& (off & 7)
{
{
# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
# ifdef IN_RING3
int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, (RTHCPTR)((RTHCUINTPTR)pvAddress + sizeof(GstPte)), GCPhysFault + sizeof(GstPte), sizeof(GstPte));
# else
int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress + sizeof(GstPte), GCPhysFault + sizeof(GstPte), sizeof(GstPte));
# endif
Log4(("pgmPoolMonitorChainChanging pae: deref %016RX64 GCPhys %016RX64\n", uShw.pPTPae->a[iShw2].u & X86_PTE_PAE_PG_MASK, GstPte.u & X86_PTE_PAE_PG_MASK));
GstPte.u & X86_PTE_PAE_PG_MASK);
# endif
}
}
break;
}
# ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
case PGMPOOLKIND_32BIT_PD:
# else
# endif
{
# ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
# endif
# ifndef IN_RING0
{
break;
}
# endif /* !IN_RING0 */
# ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
# ifndef IN_RING0
else
# endif /* !IN_RING0 */
{
{
LogFlow(("pgmPoolMonitorChainChanging: 32 bit pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPD->a[iShw].u));
iShw);
}
}
# endif
/* paranoia / a bit assumptive. */
if ( pCpu
&& (off & 3)
{
{
# ifndef IN_RING0
{
}
# endif /* !IN_RING0 */
# ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
# ifndef IN_RING0
else
# endif /* !IN_RING0 */
{
{
LogFlow(("pgmPoolMonitorChainChanging: 32 bit pd iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPD->a[iShw2].u));
iShw2);
}
}
# endif
}
}
#if 0 /* useful when running PGMAssertCR3(), a bit too troublesome for general use (TLBs). */
{
LogFlow(("pgmPoolMonitorChainChanging: iShw=%#x: %RX32 -> freeing it!\n", iShw, uShw.pPD->a[iShw].u));
# ifdef IN_RC /* TLB load - we're pushing things a bit... */
# endif
}
#endif
break;
}
# ifndef VBOX_WITH_PGMPOOL_PAGING_ONLY
case PGMPOOLKIND_ROOT_PAE_PD:
{
for (unsigned i = 0; i < 2; i++, iShw++)
{
if ((uShw.pPDPae->a[iShw].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
{
LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShwPdpt=%#x iShw=%#x!\n", iShwPdpt, iShw));
}
/* paranoia / a bit assumptive. */
else if ( pCpu
&& (off & 3)
{
if ( iShw2 < RT_ELEMENTS(uShw.pPDPae->a) /** @todo was completely wrong, it's better now after #1865 but still wrong from cross PD. */
&& (uShw.pPDPae->a[iShw2].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
{
LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShwPdpt=%#x iShw2=%#x!\n", iShwPdpt, iShw2));
}
}
#if 0 /* useful when running PGMAssertCR3(), a bit too troublesome for general use (TLBs). */
{
LogFlow(("pgmPoolMonitorChainChanging: iShwPdpt=%#x iShw=%#x: %RX64 -> freeing it!\n", iShwPdpt, iShw, uShw.pPDPae->a[iShw].u));
# ifdef IN_RC /* TLB load - we're pushing things a bit... */
# endif
pgmPoolFree(pVM, uShw.pPDPae->a[iShw].u & X86_PDE_PAE_PG_MASK, pPage->idx, iShw + iShwPdpt * X86_PG_PAE_ENTRIES);
}
#endif
}
break;
}
# endif /* !VBOX_WITH_PGMPOOL_PAGING_ONLY */
{
#ifndef IN_RING0
{
break;
}
#endif /* !IN_RING0 */
/*
* 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 to change the page
* table entries -> recheck; probably only applies to the RC case.)
*/
# ifndef IN_RING0
else
# endif /* !IN_RING0 */
{
{
LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
# ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
iShw);
# else
/* Note: hardcoded PAE implementation dependency */
(pPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD) ? iShw + (pPage->idx - PGMPOOL_IDX_PAE_PD_0) * X86_PG_PAE_ENTRIES : iShw);
# endif
}
}
#endif
/* paranoia / a bit assumptive. */
if ( pCpu
&& (off & 7)
{
#ifndef IN_RING0
{
}
#endif /* !IN_RING0 */
# ifndef IN_RING0
else
# endif /* !IN_RING0 */
{
LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
# ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
iShw2);
# else
/* Note: hardcoded PAE implementation dependency */
(pPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD) ? iShw2 + (pPage->idx - PGMPOOL_IDX_PAE_PD_0) * X86_PG_PAE_ENTRIES : iShw2);
# endif
}
#endif
}
break;
}
# ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
case PGMPOOLKIND_PAE_PDPT:
# else
case PGMPOOLKIND_ROOT_PDPT:
# endif
{
/*
* Hopefully this doesn't happen very often:
* - touching unused parts of the page
* - messing with the bits of pd pointers without changing the physical address
*/
# ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
/* PDPT roots are not page aligned; 32 byte only! */
# else
# endif
if (iShw < X86_PG_PAE_PDPE_ENTRIES) /* don't use RT_ELEMENTS(uShw.pPDPT->a), because that's for long mode only */
{
# ifndef IN_RING0
{
break;
}
# endif /* !IN_RING0 */
# ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
# ifndef IN_RING0
else
# endif /* !IN_RING0 */
{
LogFlow(("pgmPoolMonitorChainChanging: pae pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPT->a[iShw].u));
iShw);
}
# endif
/* paranoia / a bit assumptive. */
if ( pCpu
&& (offPdpt & 7)
{
&& iShw2 < X86_PG_PAE_PDPE_ENTRIES)
{
# ifndef IN_RING0
{
}
# endif /* !IN_RING0 */
# ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
# ifndef IN_RING0
else
# endif /* !IN_RING0 */
{
LogFlow(("pgmPoolMonitorChainChanging: pae pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPT->a[iShw2].u));
iShw2);
}
# endif
}
}
}
break;
}
#ifndef IN_RC
{
{
LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
iShw);
}
/* paranoia / a bit assumptive. */
if ( pCpu
&& (off & 7)
{
{
LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
iShw2);
}
}
break;
}
{
/*
* Hopefully this doesn't happen very often:
* - messing with the bits of pd pointers without changing the physical address
*/
# ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
# endif
{
{
LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPT->a[iShw].u));
}
/* paranoia / a bit assumptive. */
if ( pCpu
&& (off & 7)
{
{
LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPT->a[iShw2].u));
}
}
}
break;
}
case PGMPOOLKIND_64BIT_PML4:
{
/*
* Hopefully this doesn't happen very often:
* - messing with the bits of pd pointers without changing the physical address
*/
# ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
# endif
{
{
LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPML4->a[iShw].u));
}
/* paranoia / a bit assumptive. */
if ( pCpu
&& (off & 7)
{
{
LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPML4->a[iShw2].u));
}
}
}
break;
}
#endif /* IN_RING0 */
default:
}
/* next */
return;
}
}
# ifndef IN_RING3
/**
* Checks if a access could be a fork operation in progress.
*
* Meaning, that the guest is setuping up the parent process for Copy-On-Write.
*
* @returns true if it's likly that we're forking, otherwise false.
* @param pPool The pool.
* @param pCpu The disassembled instruction.
* @param offFault The access offset.
*/
{
/*
* i386 linux is using btr to clear X86_PTE_RW.
* The functions involved are (2.6.16 source inspection):
* clear_bit
* ptep_set_wrprotect
* copy_one_pte
* copy_pte_range
* copy_pmd_range
* copy_pud_range
* copy_page_range
* dup_mmap
* dup_mm
* copy_mm
* copy_process
* do_fork
*/
&& !(offFault & 4)
/** @todo Validate that the bit index is X86_PTE_RW. */
)
{
return true;
}
return false;
}
/**
* Determine whether the page is likely to have been reused.
*
* @returns true if we consider the page as being reused for a different purpose.
* @returns false if we consider it to still be a paging page.
* @param pVM VM Handle.
* @param pPage The page in question.
* @param pRegFrame Trap register frame.
* @param pCpu The disassembly info for the faulting instruction.
* @param pvFault The fault address.
*
* @remark The REP prefix check is left to the caller because of STOSD/W.
*/
DECLINLINE(bool) pgmPoolMonitorIsReused(PVM pVM, PPGMPOOLPAGE pPage, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu, RTGCPTR pvFault)
{
#ifndef IN_RC
/** @todo could make this general, faulting close to rsp should be safe reuse heuristic. */
if ( HWACCMHasPendingIrq(pVM)
{
/* Fault caused by stack writes while trying to inject an interrupt event. */
Log(("pgmPoolMonitorIsReused: reused %RGv for interrupt stack (rsp=%RGv).\n", pvFault, pRegFrame->rsp));
return true;
}
#else
#endif
{
/* call implies the actual push of the return address faulted */
case OP_CALL:
Log4(("pgmPoolMonitorIsReused: CALL\n"));
return true;
case OP_PUSH:
Log4(("pgmPoolMonitorIsReused: PUSH\n"));
return true;
case OP_PUSHF:
Log4(("pgmPoolMonitorIsReused: PUSHF\n"));
return true;
case OP_PUSHA:
Log4(("pgmPoolMonitorIsReused: PUSHA\n"));
return true;
case OP_FXSAVE:
Log4(("pgmPoolMonitorIsReused: FXSAVE\n"));
return true;
case OP_MOVNTI: /* solaris - block_zero_no_xmm */
Log4(("pgmPoolMonitorIsReused: MOVNTI\n"));
return true;
case OP_MOVNTDQ: /* solaris - hwblkclr & hwblkpagecopy */
Log4(("pgmPoolMonitorIsReused: MOVNTDQ\n"));
return true;
case OP_MOVSWD:
case OP_STOSWD:
)
{
Log(("pgmPoolMonitorIsReused: OP_STOSQ\n"));
return true;
}
return false;
}
{
Log4(("pgmPoolMonitorIsReused: ESP\n"));
return true;
}
//if (pPage->fCR3Mix)
// return false;
return false;
}
/**
* Flushes the page being accessed.
*
* @returns VBox status code suitable for scheduling.
* @param pVM The VM handle.
* @param pPool The pool.
* @param pPage The pool page (head).
* @param pCpu The disassembly of the write instruction.
* @param pRegFrame The trap register frame.
* @param GCPhysFault The fault address as guest physical address.
* @param pvFault The fault address.
*/
static int pgmPoolAccessHandlerFlush(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
{
/*
* First, do the flushing.
*/
/*
*/
if (RT_SUCCESS(rc2))
else if (rc2 == VERR_EM_INTERPRETER)
{
#ifdef IN_RC
{
LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for patch code %04x:%RGv, ignoring.\n",
rc = VINF_SUCCESS;
}
else
#endif
{
}
}
else
/* See use in pgmPoolAccessHandlerSimple(). */
return rc;
}
/**
* Handles the STOSD write accesses.
*
* @returns VBox status code suitable for scheduling.
* @param pVM The VM handle.
* @param pPool The pool.
* @param pPage The pool page (head).
* @param pCpu The disassembly of the write instruction.
* @param pRegFrame The trap register frame.
* @param GCPhysFault The fault address as guest physical address.
* @param pvFault The fault address.
*/
DECLINLINE(int) pgmPoolAccessHandlerSTOSD(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
{
Log3(("pgmPoolAccessHandlerSTOSD\n"));
/*
* Increment the modification counter and insert it into the list
* of modified pages the first time.
*/
if (!pPage->cModifications++)
/*
* Execute REP STOSD.
*
* This ASSUMES that we're not invoked by Trap0e on in a out-of-sync
* write situation, meaning that it's safe to write here.
*/
#endif
{
#else
#endif
#ifdef IN_RC
#else
#endif
pu32 += 4;
GCPhysFault += 4;
}
#ifdef IN_RC
/* See use in pgmPoolAccessHandlerSimple(). */
#endif
LogFlow(("pgmPoolAccessHandlerSTOSD: returns\n"));
return VINF_SUCCESS;
}
/**
* Handles the simple write accesses.
*
* @returns VBox status code suitable for scheduling.
* @param pVM The VM handle.
* @param pPool The pool.
* @param pPage The pool page (head).
* @param pCpu The disassembly of the write instruction.
* @param pRegFrame The trap register frame.
* @param GCPhysFault The fault address as guest physical address.
* @param pvFault The fault address.
*/
DECLINLINE(int) pgmPoolAccessHandlerSimple(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
{
Log3(("pgmPoolAccessHandlerSimple\n"));
/*
* Increment the modification counter and insert it into the list
* of modified pages the first time.
*/
if (!pPage->cModifications++)
/*
* Clear all the pages. ASSUMES that pvFault is readable.
*/
#else
#endif
/*
* Interpret the instruction.
*/
if (RT_SUCCESS(rc))
else if (rc == VERR_EM_INTERPRETER)
{
LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for %04x:%RGv - opcode=%d\n",
}
#ifdef IN_RC
/*
* Quick hack, with logging enabled we're getting stale
* code TLBs but no data TLB for EIP and crash in EMInterpretDisasOne.
* Flushing here is BAD and expensive, I think EMInterpretDisasOne will
* have to be fixed to support this. But that'll have to wait till next week.
*
* An alternative is to keep track of the changed PTEs together with the
* GCPhys from the guest PT. This may proove expensive though.
*
* At the moment, it's VITAL that it's done AFTER the instruction interpreting
* because we need the stale TLBs in some cases (XP boot). This MUST be fixed properly!
*/
#endif
return rc;
}
/**
* \#PF Handler callback for PT write accesses.
*
* @returns VBox status code (appropriate for GC return).
* @param pVM VM Handle.
* @param uErrorCode CPU Error code.
* @param pRegFrame Trap register frame.
* NULL on DMA and other non CPU access.
* @param pvFault The fault address (cr2).
* @param GCPhysFault The GC physical address corresponding to pvFault.
* @param pvUser User argument.
*/
DECLEXPORT(int) pgmPoolAccessHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser)
{
LogFlow(("pgmPoolAccessHandler: pvFault=%RGv pPage=%p:{.idx=%d} GCPhysFault=%RGp\n", pvFault, pPage, pPage->idx, GCPhysFault));
/*
* We should ALWAYS have the list head as user parameter. This
* is because we use that page to record the changes.
*/
/*
* Disassemble the faulting instruction.
*/
/*
* Check if it's worth dealing with.
*/
bool fReused = false;
if ( ( pPage->cModifications < 48 /** @todo #define */ /** @todo need to check that it's not mapping EIP. */ /** @todo adjust this! */
#else
#endif
)
{
/*
* Simple instructions, no REP prefix.
*/
{
STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,Handled), a);
return rc;
}
/*
* Windows is frequently doing small memset() operations (netio test 4k+).
* We have to deal with these or we'll kill the cache and performance.
*/
)
{
STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,RepStosd), a);
return rc;
}
/* REP prefix, don't bother. */
Log4(("pgmPoolAccessHandler: eax=%#x ecx=%#x edi=%#x esi=%#x rip=%RGv opcode=%d prefix=%#x\n",
pRegFrame->eax, pRegFrame->ecx, pRegFrame->edi, pRegFrame->esi, (RTGCPTR)pRegFrame->rip, Cpu.pCurInstr->opcode, Cpu.prefix));
}
/*
* Not worth it, so flush it.
*
* If we considered it to be reused, don't to back to ring-3
* to emulate failed instructions since we usually cannot
* interpret then. This may be a bit risky, in which case
* the reuse detection must be fixed.
*/
rc = VINF_SUCCESS;
STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,FlushPage), a);
return rc;
}
# endif /* !IN_RING3 */
#endif /* PGMPOOL_WITH_MONITORING */
#ifdef PGMPOOL_WITH_CACHE
/**
* Inserts a page into the GCPhys hash table.
*
* @param pPool The pool.
* @param pPage The page.
*/
{
}
/**
* Removes a page from the GCPhys hash table.
*
* @param pPool The pool.
* @param pPage The page.
*/
{
else
{
for (;;)
{
{
break;
}
if (i == NIL_PGMPOOL_IDX)
{
break;
}
iPrev = i;
}
}
}
/**
* Frees up one cache page.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @retval VERR_PGM_POOL_CLEARED if the deregistration of a physical handler will cause a light weight pool flush.
* @param pPool The pool.
* @param iUser The user index.
*/
{
#ifndef IN_RC
#endif
/*
* Select one page from the tail of the age list.
*/
/* This is the alternative to the SyncCR3 pgmPoolCacheUsed calls.
if (pPool->aPages[iToFree].iUserHead != NIL_PGMPOOL_USER_INDEX)
{
uint16_t i = pPool->aPages[iToFree].iAgePrev;
for (unsigned j = 0; j < 10 && i != NIL_PGMPOOL_USER_INDEX; j++, i = pPool->aPages[i].iAgePrev)
{
if (pPool->aPages[iToFree].iUserHead == NIL_PGMPOOL_USER_INDEX)
continue;
iToFree = i;
break;
}
}
*/
/*
* Reject any attempts at flushing the currently active shadow CR3 mapping
*/
#else
#endif
{
/* Refresh the cr3 mapping by putting it at the head of the age list. */
LogFlow(("pgmPoolCacheFreeOne refuse CR3 mapping\n"));
}
if (rc == VINF_SUCCESS)
PGM_INVL_GUEST_TLBS(); /* see PT handler. */
return rc;
}
/**
* Checks if a kind mismatch is really a page being reused
* or if it's just normal remappings.
*
* @returns true if reused and the cached page (enmKind1) should be flushed
* @returns false if not reused.
* @param enmKind1 The kind of the cached page.
* @param enmKind2 The kind of the requested page.
*/
{
switch (enmKind1)
{
/*
* Never reuse them. There is no remapping in non-paging mode.
*/
case PGMPOOLKIND_PAE_PD_PHYS:
case PGMPOOLKIND_PAE_PDPT_FOR_32BIT: /* never reuse them for other types */
return false;
#else
return true;
#endif
/*
* It's perfectly fine to reuse these, except for PAE and non-paging stuff.
*/
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_PAE_PDPT:
switch (enmKind2)
{
case PGMPOOLKIND_64BIT_PML4:
return true;
default:
return false;
}
/*
* It's perfectly fine to reuse these, except for PAE and non-paging stuff.
*/
case PGMPOOLKIND_64BIT_PML4:
switch (enmKind2)
{
return true;
default:
return false;
}
/*
* These cannot be flushed, and it's common to reuse the PDs as PTs.
*/
#ifndef VBOX_WITH_PGMPOOL_PAGING_ONLY
case PGMPOOLKIND_ROOT_PAE_PD:
case PGMPOOLKIND_ROOT_PDPT:
#endif
case PGMPOOLKIND_ROOT_NESTED:
return false;
default:
}
}
/**
* Attempts to satisfy a pgmPoolAlloc request from the cache.
*
* @returns VBox status code.
* @retval VINF_PGM_CACHED_PAGE on success.
* @retval VERR_FILE_NOT_FOUND if not found.
* @param pPool The pool.
* @param GCPhys The GC physical address of the page we're gonna shadow.
* @param enmKind The kind of mapping.
* @param iUser The shadow page pool index of the user table.
* @param iUserTable The index into the user table (shadowed).
* @param ppPage Where to store the pointer to the page.
*/
static int pgmPoolCacheAlloc(PPGMPOOL pPool, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
{
#ifndef IN_RC
#endif
/*
* Look up the GCPhys in the hash.
*/
Log3(("pgmPoolCacheAlloc: %RGp kind %s iUser=%x iUserTable=%x SLOT=%d\n", GCPhys, pgmPoolPoolKindToStr(enmKind), iUser, iUserTable, i));
if (i != NIL_PGMPOOL_IDX)
{
do
{
{
{
/* Put it at the start of the use list to make sure pgmPoolTrackAddUser
* doesn't flush it in case there are no more free use records.
*/
if (RT_SUCCESS(rc))
{
return VINF_PGM_CACHED_PAGE;
}
return rc;
}
/*
* The kind is different. In some cases we should now flush the page
* as it has been reused, but in most cases this is normal remapping
* of PDs as PT or big pages using the GCPhys field in a slightly
* different way than the other kinds.
*/
{
pgmPoolFlushPage(pPool, pPage); /* ASSUMES that VERR_PGM_POOL_CLEARED will be returned by pgmPoolTracInsert. */
PGM_INVL_GUEST_TLBS(); /* see PT handler. */
break;
}
}
/* next */
} while (i != NIL_PGMPOOL_IDX);
}
Log3(("pgmPoolCacheAlloc: Missed GCPhys=%RGp enmKind=%s\n", GCPhys, pgmPoolPoolKindToStr(enmKind)));
return VERR_FILE_NOT_FOUND;
}
/**
* Inserts a page into the cache.
*
* @param pPool The pool.
* @param pPage The cached page.
* @param fCanBeCached Set if the page is fit for caching from the caller's point of view.
*/
{
/*
* Insert into the GCPhys hash if the page is fit for that.
*/
if (fCanBeCached)
{
Log3(("pgmPoolCacheInsert: Caching %p:{.Core=%RHp, .idx=%d, .enmKind=%s, GCPhys=%RGp}\n",
}
else
{
Log3(("pgmPoolCacheInsert: Not caching %p:{.Core=%RHp, .idx=%d, .enmKind=%s, GCPhys=%RGp}\n",
}
/*
* Insert at the head of the age list.
*/
else
}
/**
* Flushes a cached page.
*
* @param pPool The pool.
* @param pPage The cached page.
*/
{
/*
* Remove the page from the hash.
*/
{
}
else
/*
* Remove it from the age list.
*/
else
else
}
#endif /* PGMPOOL_WITH_CACHE */
#ifdef PGMPOOL_WITH_MONITORING
/**
* Looks for pages sharing the monitor.
*
* @returns Pointer to the head page.
* @returns NULL if not found.
* @param pPool The Pool
* @param pNewPage The page which is going to be monitored.
*/
{
#ifdef PGMPOOL_WITH_CACHE
/*
* Look up the GCPhys in the hash.
*/
if (i == NIL_PGMPOOL_IDX)
return NULL;
do
{
{
{
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_PAE_PDPT:
#else
case PGMPOOLKIND_ROOT_PAE_PD:
case PGMPOOLKIND_ROOT_PDPT:
#endif
{
/* find the head */
{
}
return pPage;
}
/* ignore, no monitoring. */
case PGMPOOLKIND_ROOT_NESTED:
case PGMPOOLKIND_PAE_PD_PHYS:
#endif
break;
default:
}
}
/* next */
} while (i != NIL_PGMPOOL_IDX);
#endif
return NULL;
}
/**
* Enabled write monitoring of a guest page.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @retval VERR_PGM_POOL_CLEARED if the registration of the physical handler will cause a light weight pool flush.
* @param pPool The pool.
* @param pPage The cached page.
*/
{
/*
* Filter out the relevant kinds.
*/
{
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_PAE_PDPT:
#else
case PGMPOOLKIND_ROOT_PDPT:
#endif
break;
case PGMPOOLKIND_ROOT_NESTED:
/* Nothing to monitor here. */
return VINF_SUCCESS;
case PGMPOOLKIND_PAE_PD_PHYS:
/* Nothing to monitor here. */
return VINF_SUCCESS;
#else
case PGMPOOLKIND_ROOT_PAE_PD:
#endif
#ifdef PGMPOOL_WITH_MIXED_PT_CR3
break;
#else
#endif
default:
}
/*
* Install handler.
*/
int rc;
if (pPageHead)
{
rc = VINF_SUCCESS;
}
else
{
/** @todo we should probably deal with out-of-memory conditions here, but for now increasing
* the heap size should suffice. */
}
pPage->fMonitored = true;
return rc;
}
/**
* Disables write monitoring of a guest page.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @retval VERR_PGM_POOL_CLEARED if the deregistration of the physical handler will cause a light weight pool flush.
* @param pPool The pool.
* @param pPage The cached page.
*/
{
/*
* Filter out the relevant kinds.
*/
{
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_PAE_PDPT:
#else
case PGMPOOLKIND_ROOT_PDPT:
#endif
break;
case PGMPOOLKIND_ROOT_NESTED:
case PGMPOOLKIND_PAE_PD_PHYS:
/* Nothing to monitor here. */
return VINF_SUCCESS;
#ifndef VBOX_WITH_PGMPOOL_PAGING_ONLY
case PGMPOOLKIND_ROOT_PAE_PD:
#endif
#ifdef PGMPOOL_WITH_MIXED_PT_CR3
break;
#endif
#ifndef VBOX_WITH_PGMPOOL_PAGING_ONLY
#endif
default:
}
/*
* Remove the page from the monitored list or uninstall it if last.
*/
int rc;
{
{
#ifndef VBOX_WITH_PGMPOOL_PAGING_ONLY
#endif
}
else
{
{
}
rc = VINF_SUCCESS;
}
}
else
{
}
pPage->fMonitored = false;
/*
* Remove it from the list of modified pages (if in it).
*/
return rc;
}
# if defined(PGMPOOL_WITH_MIXED_PT_CR3) && !defined(VBOX_WITH_PGMPOOL_PAGING_ONLY)
/**
* Set or clear the fCR3Mix attribute in a chain of monitored pages.
*
* @param pPool The Pool.
* @param pPage A page in the chain.
* @param fCR3Mix The new fCR3Mix value.
*/
{
/* current */
/* before */
while (idx != NIL_PGMPOOL_IDX)
{
}
/* after */
while (idx != NIL_PGMPOOL_IDX)
{
}
}
/**
* Installs or modifies monitoring of a CR3 page (special).
*
* We're pretending the CR3 page is shadowed by the pool so we can use the
* generic mechanisms in detecting chained monitoring. (This also gives us a
* tast of what code changes are required to really pool CR3 shadow pages.)
*
* @returns VBox status code.
* @param pPool The pool.
* @param idxRoot The CR3 (root) page index.
* @param GCPhysCR3 The (new) CR3 value.
*/
{
LogFlow(("pgmPoolMonitorMonitorCR3: idxRoot=%d pPage=%p:{.GCPhys=%RGp, .fMonitored=%d} GCPhysCR3=%RGp\n",
/*
* The unlikely case where it already matches.
*/
{
return VINF_SUCCESS;
}
/*
* Flush the current monitoring and remove it from the hash.
*/
int rc = VINF_SUCCESS;
if (pPage->fMonitored)
{
if (rc == VERR_PGM_POOL_CLEARED)
rc = VINF_SUCCESS;
else
}
/*
* Monitor the page at the new location and insert it into the hash.
*/
if (rc2 != VERR_PGM_POOL_CLEARED)
{
}
return rc;
}
/**
* Removes the monitoring of a CR3 page (special).
*
* @returns VBox status code.
* @param pPool The pool.
* @param idxRoot The CR3 (root) page index.
*/
{
LogFlow(("pgmPoolMonitorUnmonitorCR3: idxRoot=%d pPage=%p:{.GCPhys=%RGp, .fMonitored=%d}\n",
if (!pPage->fMonitored)
return VINF_SUCCESS;
if (rc != VERR_PGM_POOL_CLEARED)
else
rc = VINF_SUCCESS;
return rc;
}
# endif /* PGMPOOL_WITH_MIXED_PT_CR3 && !VBOX_WITH_PGMPOOL_PAGING_ONLY*/
/**
* Inserts the page into the list of modified pages.
*
* @param pPool The pool.
* @param pPage The page.
*/
{
("Next=%d Prev=%d idx=%d cModifications=%d Head=%d cModifiedPages=%d\n",
pPool->cModifiedPages++;
#ifdef VBOX_WITH_STATISTICS
#endif
}
/**
* Removes the page from the list of modified pages and resets the
* moficiation counter.
*
* @param pPool The pool.
* @param pPage The page which is believed to be in the list of modified pages.
*/
{
Log3(("pgmPoolMonitorModifiedRemove: idx=%d cModifications=%d\n", pPage->idx, pPage->cModifications));
{
{
}
pPool->cModifiedPages--;
}
{
{
}
pPool->cModifiedPages--;
}
else
pPage->cModifications = 0;
}
/**
* Zaps the list of modified pages, resetting their modification counters in the process.
*
* @param pVM The VM handle.
*/
{
while (idx != NIL_PGMPOOL_IDX)
{
pPage->cModifications = 0;
}
pPool->cModifiedPages = 0;
}
#ifdef IN_RING3
/**
* Clear all shadow pages and clear all modification counters.
*
* @param pVM The VM handle.
* @remark Should only be used when monitoring is available, thus placed in
* the PGMPOOL_WITH_MONITORING #ifdef.
*/
{
/*
* Iterate all the pages until we've encountered all that in use.
* This is simple but not quite optimal solution.
*/
while (--iPage >= PGMPOOL_IDX_FIRST)
{
{
{
/*
* We only care about shadow page tables.
*/
{
#ifdef PGMPOOL_WITH_USER_TRACKING
#endif
{
#ifdef PGMPOOL_WITH_USER_TRACKING
pPage->iFirstPresent = ~0;
#endif
}
}
/* fall thru */
default:
pPage->cModifications = 0;
break;
}
if (!--cLeft)
break;
}
}
/* swipe the special pages too. */
{
{
pPage->cModifications = 0;
}
}
#ifndef DEBUG_michael
AssertMsg(cModifiedPages == pPool->cModifiedPages, ("%d != %d\n", cModifiedPages, pPool->cModifiedPages));
#endif
pPool->cModifiedPages = 0;
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
/*
* Clear all the GCPhys links and rebuild the phys ext free list.
*/
pRam;
{
while (iPage-- > 0)
}
pPool->iPhysExtFreeHead = 0;
for (unsigned i = 0; i < cMaxPhysExts; i++)
{
}
#endif
}
#endif /* IN_RING3 */
/**
* Handle SyncCR3 pool tasks
*
* @returns VBox status code.
* @retval VINF_SUCCESS if successfully added.
* @retval VINF_PGM_SYNC_CR3 is it needs to be deferred to ring 3 (GC only)
* @param pVM The VM handle.
* @remark Should only be used when monitoring is available, thus placed in
* the PGMPOOL_WITH_MONITORING #ifdef.
*/
{
LogFlow(("pgmPoolSyncCR3\n"));
/*
* When monitoring shadowed pages, we reset the modification counters on CR3 sync.
* Occasionally we will have to clear all the shadow page tables because we wanted
* to monitor a page which was mapped by too many shadowed page tables. This operation
* sometimes refered to as a 'lightweight flush'.
*/
else
{
# ifdef IN_RING3 /* Don't flush in ring-0 or raw mode, it's taking too long. */
# else /* !IN_RING3 */
LogFlow(("SyncCR3: PGM_SYNC_CLEAR_PGM_POOL is set -> VINF_PGM_SYNC_CR3\n"));
return VINF_PGM_SYNC_CR3;
# endif /* !IN_RING3 */
}
return VINF_SUCCESS;
}
#endif /* PGMPOOL_WITH_MONITORING */
#ifdef PGMPOOL_WITH_USER_TRACKING
/**
* Frees up at least one user entry.
*
* @returns VBox status code.
* @retval VINF_SUCCESS if successfully added.
* @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
* @param pPool The pool.
* @param iUser The user index.
*/
{
#ifdef PGMPOOL_WITH_CACHE
/*
* Just free cached pages in a braindead fashion.
*/
/** @todo walk the age list backwards and free the first with usage. */
int rc = VINF_SUCCESS;
do
{
return rc;
#else
/*
* Lazy approach.
*/
/* @todo incompatible with long mode paging (cr3 root will be flushed) */
return VERR_PGM_POOL_FLUSHED;
#endif
}
/**
* Inserts a page into the cache.
*
* This will create user node for the page, insert it into the GCPhys
* hash, and insert it into the age list.
*
* @returns VBox status code.
* @retval VINF_SUCCESS if successfully added.
* @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
* @retval VERR_PGM_POOL_CLEARED if the deregistration of the physical handler will cause a light weight pool flush.
* @param pPool The pool.
* @param pPage The cached page.
* @param GCPhys The GC physical address of the page we're gonna shadow.
* @param iUser The user index.
* @param iUserTable The user table index.
*/
DECLINLINE(int) pgmPoolTrackInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhys, uint16_t iUser, uint32_t iUserTable)
{
int rc = VINF_SUCCESS;
#ifdef VBOX_STRICT
/*
* Check that the entry doesn't already exists.
*/
{
do
{
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);
}
#endif
/*
* Find free a user node.
*/
if (i == NIL_PGMPOOL_USER_INDEX)
{
if (RT_FAILURE(rc))
return rc;
i = pPool->iUserFreeHead;
}
/*
* Unlink the user node from the free list,
* initialize and insert it into the user list.
*/
/*
* Insert into cache and enable monitoring of the guest page if enabled.
*
* Until we implement caching of all levels, including the CR3 one, we'll
* have to make sure we don't try monitor & cache any recursive reuse of
* a monitored CR3 page. Because all windows versions are doing this we'll
* have to be able to do combined access monitoring, CR3 + PT and
* PD + PT (guest PAE).
*
* Update:
* We're now cooperating with the CR3 monitor if an uncachable page is found.
*/
#if defined(PGMPOOL_WITH_MONITORING) || defined(PGMPOOL_WITH_CACHE)
# ifdef PGMPOOL_WITH_MIXED_PT_CR3
const bool fCanBeMonitored = true;
# else
|| (GCPhys & X86_PTE_PAE_PG_MASK) != (pPool->CTX_SUFF(pVM)->pgm.s.GCPhysGstCR3Monitored & X86_PTE_PAE_PG_MASK)
# endif
# ifdef PGMPOOL_WITH_CACHE
# endif
if (fCanBeMonitored)
{
# ifdef PGMPOOL_WITH_MONITORING
if (rc == VERR_PGM_POOL_CLEARED)
{
/* 'Failed' - free the usage, and keep it in the cache (if enabled). */
# ifndef PGMPOOL_WITH_CACHE
# endif
pPool->iUserFreeHead = i;
}
}
# endif
#endif /* PGMPOOL_WITH_MONITORING */
return rc;
}
# ifdef PGMPOOL_WITH_CACHE /* (only used when the cache is enabled.) */
/**
* Adds a user reference to a page.
*
* This will move the page to the head of the
*
* @returns VBox status code.
* @retval VINF_SUCCESS if successfully added.
* @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
* @param pPool The pool.
* @param pPage The cached page.
* @param iUser The user index.
* @param iUserTable The user table.
*/
static int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
{
Log3(("pgmPoolTrackAddUser GCPhys = %RGp iUser %x iUserTable %x\n", pPage->GCPhys, iUser, iUserTable));
# ifdef VBOX_STRICT
/*
* Check that the entry doesn't already exists.
*/
{
do
{
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);
}
# endif
/*
* Allocate a user node.
*/
if (i == NIL_PGMPOOL_USER_INDEX)
{
if (RT_FAILURE(rc))
return rc;
i = pPool->iUserFreeHead;
}
/*
* Initialize the user node and insert it.
*/
# ifdef PGMPOOL_WITH_CACHE
/*
* Tell the cache to update its replacement stats for this page.
*/
# endif
return VINF_SUCCESS;
}
# endif /* PGMPOOL_WITH_CACHE */
/**
* Frees a user record associated with a page.
*
* This does not clear the entry in the user table, it simply replaces the
* user record to the chain of free records.
*
* @param pPool The pool.
* @param HCPhys The HC physical address of the shadow page.
* @param iUser The shadow page pool index of the user table.
* @param iUserTable The index into the user table (shadowed).
*/
static void pgmPoolTrackFreeUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
{
/*
* Unlink and free the specified user entry.
*/
/* Special: For PAE and 32-bit paging, there is usually no more than one user. */
if ( i != NIL_PGMPOOL_USER_INDEX
{
pPool->iUserFreeHead = i;
return;
}
/* General: Linear search. */
while (i != NIL_PGMPOOL_USER_INDEX)
{
{
if (iPrev != NIL_PGMPOOL_USER_INDEX)
else
pPool->iUserFreeHead = i;
return;
}
iPrev = i;
}
/* Fatal: didn't find it */
AssertFatalMsgFailed(("Didn't find the user entry! iUser=%#x iUserTable=%#x GCPhys=%RGp\n",
}
/**
* Gets the entry size of a shadow table.
*
* @param enmKind The kind of page.
*
* @returns The size of the entry in bytes. That is, 4 or 8.
* @returns If the kind is not for a table, an assertion is raised and 0 is
* returned.
*/
{
switch (enmKind)
{
case PGMPOOLKIND_32BIT_PD:
#else
#endif
return 4;
case PGMPOOLKIND_64BIT_PML4:
#ifndef VBOX_WITH_PGMPOOL_PAGING_ONLY
case PGMPOOLKIND_ROOT_PAE_PD:
case PGMPOOLKIND_ROOT_PDPT:
#endif
case PGMPOOLKIND_PAE_PDPT:
case PGMPOOLKIND_ROOT_NESTED:
case PGMPOOLKIND_PAE_PD_PHYS:
return 8;
default:
}
}
/**
* Gets the entry size of a guest table.
*
* @param enmKind The kind of page.
*
* @returns The size of the entry in bytes. That is, 0, 4 or 8.
* @returns If the kind is not for a table, an assertion is raised and 0 is
* returned.
*/
{
switch (enmKind)
{
case PGMPOOLKIND_32BIT_PD:
#else
#endif
return 4;
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_PAE_PDPT:
#else
case PGMPOOLKIND_ROOT_PAE_PD:
case PGMPOOLKIND_ROOT_PDPT:
#endif
return 8;
case PGMPOOLKIND_ROOT_NESTED:
case PGMPOOLKIND_PAE_PD_PHYS:
/** @todo can we return 0? (nobody is calling this...) */
AssertFailed();
return 0;
default:
}
}
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
/**
* Scans one shadow page table for mappings of a physical page.
*
* @param pVM The VM handle.
* @param pPhysPage The guest page in question.
* @param iShw The shadow page table.
* @param cRefs The number of references made in that PT.
*/
static void pgmPoolTrackFlushGCPhysPTInt(PVM pVM, PCPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs)
{
LogFlow(("pgmPoolTrackFlushGCPhysPT: pPhysPage=%R[pgmpage] iShw=%d cRefs=%d\n", pPhysPage, iShw, cRefs));
/*
* Assert sanity.
*/
/*
* Then, clear the actual mappings to the page in the shadow PT.
*/
{
{
{
pPT->a[i].u = 0;
cRefs--;
if (!cRefs)
return;
}
#ifdef LOG_ENABLED
RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
for (unsigned i = 0; i < RT_ELEMENTS(pPT->a); i++)
{
pPT->a[i].u = 0;
}
#endif
AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
break;
}
{
{
pPT->a[i].u = 0;
cRefs--;
if (!cRefs)
return;
}
#ifdef LOG_ENABLED
RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
for (unsigned i = 0; i < RT_ELEMENTS(pPT->a); i++)
{
pPT->a[i].u = 0;
}
#endif
AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d u64=%RX64\n", cRefs, pPage->iFirstPresent, pPage->cPresent, u64));
break;
}
{
{
pPT->a[i].u = 0;
cRefs--;
if (!cRefs)
return;
}
#ifdef LOG_ENABLED
RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
for (unsigned i = 0; i < RT_ELEMENTS(pPT->a); i++)
{
pPT->a[i].u = 0;
}
#endif
AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
break;
}
default:
}
}
/**
* Scans one shadow page table for mappings of a physical page.
*
* @param pVM The VM handle.
* @param pPhysPage The guest page in question.
* @param iShw The shadow page table.
* @param cRefs The number of references made in that PT.
*/
{
LogFlow(("pgmPoolTrackFlushGCPhysPT: pPhysPage=%R[pgmpage] iShw=%d cRefs=%d\n", pPhysPage, iShw, cRefs));
}
/**
* Flushes a list of shadow page tables mapping the same physical page.
*
* @param pVM The VM handle.
* @param pPhysPage The guest page in question.
* @param iPhysExt The physical cross reference extent list to flush.
*/
{
do
{
{
}
/* next */
} while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
/* insert the list into the free list and clear the ram range entry. */
}
#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
/**
* Flushes all shadow page table mappings of the given guest page.
*
* This is typically called when the host page backing the guest one has been
* replaced or when the page protection was changed due to an access handler.
*
* @returns VBox status code.
* @retval VINF_SUCCESS if all references has been successfully cleared.
* @retval VINF_PGM_GCPHYS_ALIASED if we're better off with a CR3 sync and
* a page pool cleaning.
*
* @param pVM The VM handle.
* @param pPhysPage The guest page in question.
* @param pfFlushTLBs This is set to @a true if the shadow TLBs should be
* flushed, it is NOT touched if this isn't necessary.
* The caller MUST initialized this to @a false.
*/
{
int rc = VINF_SUCCESS;
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
if (u16)
{
# ifdef VBOX_WITH_NEW_PHYS_CODE
/*
* The zero page is currently screwing up the tracking and we'll
* have to flush the whole shebang. Unless VBOX_WITH_NEW_LAZY_PAGE_ALLOC
* is defined, zero pages won't normally be mapped. Some kind of solution
* will be needed for this problem of course, but it will have to wait...
*/
if (PGM_PAGE_IS_ZERO(pPhysPage))
else
# endif
{
/* Start a subset here because pgmPoolTrackFlushGCPhysPTsSlow and
# endif
else
*pfFlushTLBs = true;
# endif
}
}
#elif defined(PGMPOOL_WITH_CACHE)
# ifdef VBOX_WITH_NEW_PHYS_CODE
if (PGM_PAGE_IS_ZERO(pPhysPage))
else
# endif
{
/* Start a subset here because pgmPoolTrackFlushGCPhysPTsSlow kill the pool otherwise. */
# endif
if (rc == VINF_SUCCESS)
*pfFlushTLBs = true;
}
# endif
#else
#endif
return rc;
}
/**
* Scans all shadow page tables for mappings of a physical page.
*
* This may be slow, but it's most likely more efficient than cleaning
* out the entire page pool / cache.
*
* @returns VBox status code.
* @retval VINF_SUCCESS if all references has been successfully cleared.
* @retval VINF_PGM_GCPHYS_ALIASED if we're better off with a CR3 sync and
* a page pool cleaning.
*
* @param pVM The VM handle.
* @param pPhysPage The guest page in question.
*/
{
LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: cUsedPages=%d cPresent=%d pPhysPage=%R[pgmpage]\n",
#if 1
/*
* There is a limit to what makes sense.
*/
{
return VINF_PGM_GCPHYS_ALIASED;
}
#endif
/*
* Iterate all the pages until we've encountered all that in use.
* This is simple but not quite optimal solution.
*/
while (--iPage >= PGMPOOL_IDX_FIRST)
{
{
{
/*
* We only care about shadow page tables.
*/
{
{
{
//Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX32\n", iPage, i, pPT->a[i]));
pPT->a[i].u = 0;
}
if (!--cPresent)
break;
}
break;
}
{
{
{
//Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX64\n", iPage, i, pPT->a[i]));
pPT->a[i].u = 0;
}
if (!--cPresent)
break;
}
break;
}
}
if (!--cLeft)
break;
}
}
return VINF_SUCCESS;
}
/**
* Clears the user entry in a user table.
*
* This is used to remove all references to a page when flushing it.
*/
{
/*
* Map the user page.
*/
#if defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0) && !defined(VBOX_WITH_PGMPOOL_PAGING_ONLY)
{
/* Must translate the fake 2048 entry PD to a 512 PD one since the R0 mapping is not linear. */
}
#endif
union
{
} u;
LogFlow(("pgmPoolTrackClearPageUser: clear %x in %s (%RGp) (flushing %s)\n", iUserTable, pgmPoolPoolKindToStr(pUserPage->enmKind), pUserPage->Core.Key, pgmPoolPoolKindToStr(pPage->enmKind)));
/* Safety precaution in case we change the paging for other modes too in the future. */
#else
#endif
#ifdef VBOX_STRICT
/*
* Some sanity checks.
*/
{
# ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
case PGMPOOLKIND_32BIT_PD:
break;
# else
break;
# endif
# if !defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0) && !defined(VBOX_WITH_PGMPOOL_PAGING_ONLY)
case PGMPOOLKIND_ROOT_PAE_PD:
AssertMsg(!(u.pau64[iUserTable] & PGM_PDFLAGS_MAPPING), ("%llx %d\n", u.pau64[iUserTable], iUserTable));
break;
# endif
# ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
case PGMPOOLKIND_PAE_PDPT:
# else
case PGMPOOLKIND_ROOT_PDPT:
# endif
break;
case PGMPOOLKIND_PAE_PD_PHYS:
break;
break;
break;
case PGMPOOLKIND_64BIT_PML4:
/* GCPhys >> PAGE_SHIFT is the index here */
break;
break;
break;
case PGMPOOLKIND_ROOT_NESTED:
break;
default:
break;
}
#endif /* VBOX_STRICT */
/*
* Clear the entry in the user page.
*/
{
/* 32-bit entries */
case PGMPOOLKIND_32BIT_PD:
#else
#endif
u.pau32[iUserTable] = 0;
break;
/* 64-bit entries */
#if defined(IN_RC) && defined(VBOX_WITH_PGMPOOL_PAGING_ONLY)
/* In 32 bits PAE mode we *must* invalidate the TLB when changing a PDPT entry; the CPU fetches them only during cr3 load, so any
* non-present PDPT will continue to cause page faults.
*/
ASMReloadCR3();
#endif
/* no break */
case PGMPOOLKIND_PAE_PD_PHYS:
case PGMPOOLKIND_64BIT_PML4:
#if !defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0) && !defined(VBOX_WITH_PGMPOOL_PAGING_ONLY)
case PGMPOOLKIND_ROOT_PAE_PD:
#endif
case PGMPOOLKIND_PAE_PDPT:
#else
case PGMPOOLKIND_ROOT_PDPT:
#endif
case PGMPOOLKIND_ROOT_NESTED:
u.pau64[iUserTable] = 0;
break;
default:
AssertFatalMsgFailed(("enmKind=%d iUser=%#x iUserTable=%#x\n", pUserPage->enmKind, pUser->iUser, pUser->iUserTable));
}
}
/**
* Clears all users of a page.
*/
{
/*
* Free all the user records.
*/
while (i != NIL_PGMPOOL_USER_INDEX)
{
/* Clear enter in user table. */
/* Free it. */
pPool->iUserFreeHead = i;
/* Next. */
i = iNext;
}
}
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
/**
* Allocates a new physical cross reference extent.
*
* @returns Pointer to the allocated extent on success. NULL if we're out of them.
* @param pVM The VM handle.
* @param piPhysExt Where to store the phys ext index.
*/
{
if (iPhysExt == NIL_PGMPOOL_PHYSEXT_INDEX)
{
return NULL;
}
return pPhysExt;
}
/**
* Frees a physical cross reference extent.
*
* @param pVM The VM handle.
* @param iPhysExt The extent to free.
*/
{
}
/**
* Frees a physical cross reference extent.
*
* @param pVM The VM handle.
* @param iPhysExt The extent to free.
*/
{
do
{
/* next */
} while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
}
/**
* Insert a reference into a list of physical cross reference extents.
*
* @returns The new tracking data for PGMPAGE.
*
* @param pVM The VM handle.
* @param iPhysExt The physical extent index of the list head.
* @param iShwPT The shadow page table index.
*
*/
{
/* special common case. */
{
}
/* general treatment. */
unsigned cMax = 15;
for (;;)
{
{
}
if (!--cMax)
{
}
}
/* add another extent to the list. */
if (!pNew)
{
}
LogFlow(("pgmPoolTrackPhysExtAddref: added new extent %d:{%d}->%d\n", iPhysExt, iShwPT, iPhysExtStart));
}
/**
* Add a reference to guest physical page where extents are in use.
*
* @returns The new tracking data for PGMPAGE.
*
* @param pVM The VM handle.
* @param u16 The ram range flags (top 16-bits).
* @param iShwPT The shadow page table index.
*/
{
{
/*
* Convert to extent list.
*/
if (pPhysExt)
{
LogFlow(("pgmPoolTrackPhysExtAddref: new extent: %d:{%d, %d}\n", iPhysExt, PGMPOOL_TD_GET_IDX(u16), iShwPT));
}
else
}
{
/*
* Insert into the extent list.
*/
}
else
return u16;
}
/**
* Clear references to guest physical memory.
*
* @param pPool The pool.
* @param pPage The page.
* @param pPhysPage Pointer to the aPages entry in the ram range.
*/
{
AssertFatalMsg(cRefs == PGMPOOL_TD_CREFS_PHYSEXT, ("cRefs=%d pPhysPage=%R[pgmpage] pPage=%p:{.idx=%d}\n", cRefs, pPhysPage, pPage, pPage->idx));
if (iPhysExt != PGMPOOL_TD_IDX_OVERFLOWED)
{
do
{
/*
* Look for the shadow page and check if it's all freed.
*/
{
{
{
return;
}
/* we can free the node. */
{
/* lonely node */
Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d lonely\n", pPhysPage, pPage->idx));
}
else if (iPhysExtPrev == NIL_PGMPOOL_PHYSEXT_INDEX)
{
/* head */
Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d head\n", pPhysPage, pPage->idx));
}
else
{
/* in list */
}
return;
}
}
/* next */
} while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
AssertFatalMsgFailed(("not-found! cRefs=%d pPhysPage=%R[pgmpage] pPage=%p:{.idx=%d}\n", cRefs, pPhysPage, pPage, pPage->idx));
}
else /* nothing to do */
}
/**
* Clear references to guest physical memory.
*
* This is the same as pgmPoolTracDerefGCPhys except that the guest physical address
* is assumed to be correct, so the linear search can be skipped and we can assert
* at an earlier point.
*
* @param pPool The pool.
* @param pPage The page.
* @param HCPhys The host physical address corresponding to the guest page.
* @param GCPhys The guest physical address corresponding to HCPhys.
*/
static void pgmPoolTracDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhys)
{
/*
* Walk range list.
*/
while (pRam)
{
{
/* does it match? */
#ifdef LOG_ENABLED
#endif
{
return;
}
break;
}
}
}
/**
* Clear references to guest physical memory.
*
* @param pPool The pool.
* @param pPage The page.
* @param HCPhys The host physical address corresponding to the guest page.
* @param GCPhysHint The guest physical address which may corresponding to HCPhys.
*/
static void pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint)
{
/*
* Walk range list.
*/
while (pRam)
{
{
/* does it match? */
{
return;
}
break;
}
}
/*
* Damn, the hint didn't work. We'll have to do an expensive linear search.
*/
while (pRam)
{
while (iPage-- > 0)
{
{
Log4(("pgmPoolTracDerefGCPhysHint: Linear HCPhys=%RHp GCPhysHint=%RGp GCPhysReal=%RGp\n",
return;
}
}
}
}
/**
* Clear references to guest physical memory in a 32-bit / 32-bit page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
* @param pGstPT The guest page table.
*/
DECLINLINE(void) pgmPoolTrackDerefPT32Bit32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT, PCX86PT pGstPT)
{
{
Log4(("pgmPoolTrackDerefPT32Bit32Bit: i=%d pte=%RX32 hint=%RX32\n",
pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
break;
}
}
/**
* Clear references to guest physical memory in a PAE / 32-bit page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
* @param pGstPT The guest page table (just a half one).
*/
DECLINLINE(void) pgmPoolTrackDerefPTPae32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PT pGstPT)
{
for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++)
{
Log4(("pgmPoolTrackDerefPTPae32Bit: i=%d pte=%RX64 hint=%RX32\n",
pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
}
}
/**
* Clear references to guest physical memory in a PAE / PAE page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
* @param pGstPT The guest page table.
*/
DECLINLINE(void) pgmPoolTrackDerefPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PTPAE pGstPT)
{
for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++)
{
Log4(("pgmPoolTrackDerefPTPaePae: i=%d pte=%RX32 hint=%RX32\n",
pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PAE_PG_MASK);
}
}
/**
* Clear references to guest physical memory in a 32-bit / 4MB page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
*/
{
{
Log4(("pgmPoolTrackDerefPT32Bit4MB: i=%d pte=%RX32 GCPhys=%RGp\n",
}
}
/**
* Clear references to guest physical memory in a PAE / 2/4MB page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
*/
{
{
Log4(("pgmPoolTrackDerefPTPaeBig: i=%d pte=%RX64 hint=%RGp\n",
}
}
#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
/**
* Clear references to shadowed pages in a 32 bits page directory.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPD The shadow page directory (mapping of the page).
*/
{
for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
{
&& !(pShwPD->a[i].u & PGM_PDFLAGS_MAPPING)
)
{
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PG_MASK);
if (pSubPage)
else
}
}
}
#endif
/**
* Clear references to shadowed pages in a PAE (legacy or 64 bits) page directory.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPD The shadow page directory (mapping of the page).
*/
{
for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
{
&& !(pShwPD->a[i].u & PGM_PDFLAGS_MAPPING)
#endif
)
{
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PAE_PG_MASK);
if (pSubPage)
else
/** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
}
}
}
/**
* Clear references to shadowed pages in a 64-bit page directory pointer table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPDPT The shadow page directory pointer table (mapping of the page).
*/
{
for (unsigned i = 0; i < RT_ELEMENTS(pShwPDPT->a); i++)
{
&& !(pShwPDPT->a[i].u & PGM_PLXFLAGS_MAPPING)
#endif
)
{
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & X86_PDPE_PG_MASK);
if (pSubPage)
else
/** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
}
}
}
/**
* Clear references to shadowed pages in a 64-bit level 4 page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPML4 The shadow page directory pointer table (mapping of the page).
*/
{
for (unsigned i = 0; i < RT_ELEMENTS(pShwPML4->a); i++)
{
{
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPML4->a[i].u & X86_PDPE_PG_MASK);
if (pSubPage)
else
/** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
}
}
}
/**
* Clear references to shadowed pages in an EPT page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPML4 The shadow page directory pointer table (mapping of the page).
*/
{
{
Log4(("pgmPoolTrackDerefPTEPT: i=%d pte=%RX64 GCPhys=%RX64\n",
}
}
/**
* Clear references to shadowed pages in an EPT page directory.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPD The shadow page directory (mapping of the page).
*/
{
for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
{
{
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & EPT_PDE_PG_MASK);
if (pSubPage)
else
/** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
}
}
}
/**
* Clear references to shadowed pages in an EPT page directory pointer table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPDPT The shadow page directory pointer table (mapping of the page).
*/
{
for (unsigned i = 0; i < RT_ELEMENTS(pShwPDPT->a); i++)
{
{
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & EPT_PDPTE_PG_MASK);
if (pSubPage)
else
/** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
}
}
}
/**
* Clears all references made by this page.
*
* This includes other shadow pages and GC physical addresses.
*
* @param pPool The pool.
* @param pPage The page.
*/
{
/*
* Map the shadow page and take action according to the page kind.
*/
{
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
{
void *pvGst;
break;
}
{
void *pvGst;
break;
}
{
void *pvGst;
break;
}
case PGMPOOLKIND_32BIT_PT_FOR_PHYS: /* treat it like a 4 MB page */
{
break;
}
case PGMPOOLKIND_PAE_PT_FOR_PHYS: /* treat it like a 2 MB page */
{
break;
}
#else /* !PGMPOOL_WITH_GCPHYS_TRACKING */
break;
#endif /* !PGMPOOL_WITH_GCPHYS_TRACKING */
case PGMPOOLKIND_PAE_PD_PHYS:
break;
case PGMPOOLKIND_32BIT_PD:
break;
case PGMPOOLKIND_PAE_PDPT:
#endif
break;
case PGMPOOLKIND_64BIT_PML4:
break;
break;
break;
break;
default:
}
/* paranoia, clear the shadow page. Remove this laser (i.e. let Alloc and ClearAll do it). */
}
#endif /* PGMPOOL_WITH_USER_TRACKING */
#ifdef IN_RING3
/**
* Flushes all the special root pages as part of a pgmPoolFlushAllInt operation.
*
* @param pPool The pool.
*/
{
/* Start a subset so we won't run out of mapping space. */
#endif
/*
* These special pages are all mapped into the indexes 1..PGMPOOL_IDX_FIRST.
*/
Assert(NIL_PGMPOOL_IDX == 0);
#ifndef VBOX_WITH_PGMPOOL_PAGING_ONLY
for (unsigned i = 1; i < PGMPOOL_IDX_FIRST; i++)
{
/*
* Get the page address.
*/
union
{
} u;
/*
* Mark stuff not present.
*/
{
break;
break;
case PGMPOOLKIND_ROOT_PDPT:
/* Not root of shadowed pages currently, ignore it. */
break;
case PGMPOOLKIND_ROOT_NESTED:
break;
}
}
#endif
/*
* Paranoia (to be removed), flag a global CR3 sync.
*/
/* Pop the subset. */
#endif
}
/**
* Flushes the entire cache.
*
* It will assert a global CR3 flush (FF) and assumes the caller is aware of this
* and execute this CR3 flush.
*
* @param pPool The pool.
*/
{
LogFlow(("pgmPoolFlushAllInt:\n"));
/*
* If there are no pages in the pool, there is nothing to do.
*/
{
return;
}
/* Unmap the old CR3 value before flushing everything. */
/* Exit the current shadow paging mode as well; nested paging and EPT use a root CR3 which will get flushed here. */
#endif
/*
* Nuke the free list and reinsert all pages into it.
*/
{
#ifdef IN_RING3
#endif
#ifdef PGMPOOL_WITH_MONITORING
if (pPage->fMonitored)
pPage->cModifications = 0;
#endif
pPage->fSeenNonGlobal = false;
pPage->fMonitored= false;
pPage->fReusedFlushPending = false;
#ifdef PGMPOOL_WITH_USER_TRACKING
#else
#endif
#ifdef PGMPOOL_WITH_CACHE
#endif
#endif
}
pPool->cUsedPages = 0;
#ifdef PGMPOOL_WITH_USER_TRACKING
/*
* Zap and reinitialize the user records.
*/
pPool->iUserFreeHead = 0;
for (unsigned i = 0; i < cMaxUsers; i++)
{
}
#endif
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
/*
* Clear all the GCPhys links and rebuild the phys ext free list.
*/
pRam;
{
while (iPage-- > 0)
}
pPool->iPhysExtFreeHead = 0;
for (unsigned i = 0; i < cMaxPhysExts; i++)
{
}
#endif
#ifdef PGMPOOL_WITH_MONITORING
/*
* Just zap the modified list.
*/
pPool->cModifiedPages = 0;
#endif
#ifdef PGMPOOL_WITH_CACHE
/*
* Clear the GCPhys hash and the age list.
*/
#endif
/*
* Flush all the special root pages.
* Reinsert active pages into the hash and ensure monitoring chains are correct.
*/
for (unsigned i = PGMPOOL_IDX_FIRST_SPECIAL; i < PGMPOOL_IDX_FIRST; i++)
{
#ifdef PGMPOOL_WITH_MONITORING
pPage->cModifications = 0;
/* ASSUMES that we're not sharing with any of the other special pages (safe for now). */
if (pPage->fMonitored)
{
# ifdef PGMPOOL_WITH_CACHE
# endif
}
#endif
#ifdef PGMPOOL_WITH_USER_TRACKING
#endif
#ifdef PGMPOOL_WITH_CACHE
#endif
}
/* Force a shadow mode reinit (necessary for nested paging and ept). */
/* Reinit the current shadow paging mode as well; nested paging and EPT use a root CR3 which will get flushed here. */
#endif
/*
* Finally, assert the FF.
*/
}
#endif /* IN_RING3 */
/**
* Flushes a pool page.
*
* This moves the page to the free list after removing all user references to it.
* In GC this will cause a CR3 reload if the page is traced back to an active root page.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @retval VERR_PGM_POOL_CLEARED if the deregistration of the physical handler will cause a light weight pool flush.
* @param pPool The pool.
* @param HCPhys The HC physical address of the shadow page.
*/
{
int rc = VINF_SUCCESS;
LogFlow(("pgmPoolFlushPage: pPage=%p:{.Key=%RHp, .idx=%d, .enmKind=%s, .GCPhys=%RGp}\n",
/*
* Quietly reject any attempts at flushing any of the special root pages.
*/
{
Log(("pgmPoolFlushPage: special root page, rejected. enmKind=%s idx=%d\n", pgmPoolPoolKindToStr(pPage->enmKind), pPage->idx));
return VINF_SUCCESS;
}
/*
* Quietly reject any attempts at flushing the currently active shadow CR3 mapping
*/
{
("Can't free the shadow CR3! (%RHp vs %RHp kind=%d\n", PGMGetHyperCR3(pPool->CTX_SUFF(pVM)), pPage->Core.Key, pPage->enmKind));
#else
{
("Can't free the shadow CR3! (%RHp vs %RHp kind=%d\n", PGMGetHyperCR3(pPool->CTX_SUFF(pVM)), pPage->Core.Key, pPage->enmKind));
#endif
Log(("pgmPoolFlushPage: current active shadow CR3, rejected. enmKind=%s idx=%d\n", pgmPoolPoolKindToStr(pPage->enmKind), pPage->idx));
return VINF_SUCCESS;
}
/* Start a subset so we won't run out of mapping space. */
#endif
/*
* Mark the page as being in need of a ASMMemZeroPage().
*/
#ifdef PGMPOOL_WITH_USER_TRACKING
/*
* Clear the page.
*/
#endif
#ifdef PGMPOOL_WITH_CACHE
/*
* Flush it from the cache.
*/
#endif /* PGMPOOL_WITH_CACHE */
/* Heavy stuff done. */
#endif
#ifdef PGMPOOL_WITH_MONITORING
/*
* Deregistering the monitoring.
*/
if (pPage->fMonitored)
#endif
/*
* Free the page.
*/
pPage->fReusedFlushPending = false;
pPool->cUsedPages--;
return rc;
}
/**
* Frees a usage of a pool page.
*
* The caller is responsible to updating the user table so that it no longer
* references the shadow page.
*
* @param pPool The pool.
* @param HCPhys The HC physical address of the shadow page.
* @param iUser The shadow page pool index of the user table.
* @param iUserTable The index into the user table (shadowed).
*/
{
LogFlow(("pgmPoolFreeByPage: pPage=%p:{.Key=%RHp, .idx=%d, enmKind=%s} iUser=%#x iUserTable=%#x\n",
#ifdef PGMPOOL_WITH_USER_TRACKING
#endif
#ifdef PGMPOOL_WITH_CACHE
#endif
}
/**
* Makes one or more free page free.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
*
* @param pPool The pool.
* @param enmKind Page table kind
* @param iUser The user of the page.
*/
{
/*
* If the pool isn't full grown yet, expand it.
*/
#if defined(VBOX_WITH_PGMPOOL_PAGING_ONLY) && defined(IN_RC)
/* Hack alert: we can't deal with jumps to ring 3 when called from MapCR3 and allocating pages for PAE PDs. */
#endif
)
{
#ifdef IN_RING3
#else
#endif
if (RT_FAILURE(rc))
return rc;
return VINF_SUCCESS;
}
#ifdef PGMPOOL_WITH_CACHE
/*
* Free one cached page.
*/
#else
/*
* Flush the pool.
*
* If we have tracking enabled, it should be possible to come up with
* a cheap replacement strategy...
*/
/* @todo incompatible with long mode paging (cr3 root will be flushed) */
return VERR_PGM_POOL_FLUSHED;
#endif
}
/**
* Allocates a page from the pool.
*
* This page may actually be a cached page and not in need of any processing
* on the callers part.
*
* @returns VBox status code.
* @retval VINF_SUCCESS if a NEW page was allocated.
* @retval VINF_PGM_CACHED_PAGE if a CACHED page was returned.
* @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
* @param pVM The VM handle.
* @param GCPhys The GC physical address of the page we're gonna shadow.
* For 4MB and 2MB PD entries, it's the first address the
* shadow PT is covering.
* @param enmKind The kind of mapping.
* @param iUser The shadow page pool index of the user table.
* @param iUserTable The index into the user table (shadowed).
* @param ppPage Where to store the pointer to the page. NULL is stored here on failure.
*/
int pgmPoolAlloc(PVM pVM, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
{
LogFlow(("pgmPoolAlloc: GCPhys=%RGp enmKind=%s iUser=%#x iUserTable=%#x\n", GCPhys, pgmPoolPoolKindToStr(enmKind), iUser, iUserTable));
/** @todo CSAM/PGMPrefetchPage messes up here during CSAMR3CheckGates
* (TRPMR3SyncIDT) because of FF priority. Try fix that?
* Assert(!(pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)); */
#ifdef PGMPOOL_WITH_CACHE
if (pPool->fCacheEnabled)
{
if (RT_SUCCESS(rc2))
{
LogFlow(("pgmPoolAlloc: cached returns %Rrc *ppPage=%p:{.Key=%RHp, .idx=%d}\n", rc2, *ppPage, (*ppPage)->Core.Key, (*ppPage)->idx));
return rc2;
}
}
#endif
/*
* Allocate a new one.
*/
int rc = VINF_SUCCESS;
if (iNew == NIL_PGMPOOL_IDX)
{
if (RT_FAILURE(rc))
{
if (rc != VERR_PGM_POOL_CLEARED)
{
return rc;
}
}
}
/* unlink the free head */
/*
* Initialize it.
*/
pPool->cUsedPages++; /* physical handler registration / pgmPoolTrackFlushGCPhysPTsSlow requirement. */
pPage->fMonitored = false;
pPage->fReusedFlushPending = false;
#ifdef PGMPOOL_WITH_MONITORING
pPage->cModifications = 0;
#else
#endif
#ifdef PGMPOOL_WITH_USER_TRACKING
pPage->iFirstPresent = ~0;
/*
* Insert into the tracking and cache. If this fails, free the page.
*/
if (RT_FAILURE(rc3))
{
if (rc3 != VERR_PGM_POOL_CLEARED)
{
pPool->cUsedPages--;
return rc3;
}
}
#endif /* PGMPOOL_WITH_USER_TRACKING */
/*
* Commit the allocation, clear the page and return.
*/
#ifdef VBOX_WITH_STATISTICS
#endif
{
}
LogFlow(("pgmPoolAlloc: returns %Rrc *ppPage=%p:{.Key=%RHp, .idx=%d, .fCached=%RTbool, .fMonitored=%RTbool}\n",
return rc;
}
/**
* Frees a usage of a pool page.
*
* @param pVM The VM handle.
* @param HCPhys The HC physical address of the shadow page.
* @param iUser The shadow page pool index of the user table.
* @param iUserTable The index into the user table (shadowed).
*/
{
}
/**
* Gets a in-use page in the pool by it's physical address.
*
* @returns Pointer to the page.
* @param pVM The VM handle.
* @param HCPhys The HC physical address of the shadow page.
* @remark This function will NEVER return NULL. It will assert if HCPhys is invalid.
*/
{
/** @todo profile this! */
Log4(("pgmPoolGetPageByHCPhys: HCPhys=%RHp -> %p:{.idx=%d .GCPhys=%RGp .enmKind=%s}\n",
return pPage;
}
#ifdef IN_RING3
/**
* Flushes the entire cache.
*
* It will assert a global CR3 flush (FF) and assumes the caller is aware of this
* and execute this CR3 flush.
*
* @param pPool The pool.
*/
{
LogFlow(("pgmPoolFlushAll:\n"));
}
#endif
#ifdef LOG_ENABLED
{
switch(enmKind)
{
case PGMPOOLKIND_INVALID:
return "PGMPOOLKIND_INVALID";
case PGMPOOLKIND_FREE:
return "PGMPOOLKIND_FREE";
return "PGMPOOLKIND_32BIT_PT_FOR_PHYS";
return "PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT";
return "PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB";
return "PGMPOOLKIND_PAE_PT_FOR_PHYS";
return "PGMPOOLKIND_PAE_PT_FOR_32BIT_PT";
return "PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB";
return "PGMPOOLKIND_PAE_PT_FOR_PAE_PT";
return "PGMPOOLKIND_PAE_PT_FOR_PAE_2MB";
case PGMPOOLKIND_32BIT_PD:
return "PGMPOOLKIND_32BIT_PD";
return "PGMPOOLKIND_32BIT_PD_PHYS";
return "PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD";
return "PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD";
return "PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD";
return "PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD";
return "PGMPOOLKIND_PAE_PD_FOR_PAE_PD";
case PGMPOOLKIND_PAE_PD_PHYS:
return "PGMPOOLKIND_PAE_PD_PHYS";
return "PGMPOOLKIND_PAE_PDPT_FOR_32BIT";
case PGMPOOLKIND_PAE_PDPT:
return "PGMPOOLKIND_PAE_PDPT";
return "PGMPOOLKIND_PAE_PDPT_PHYS";
return "PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT";
return "PGMPOOLKIND_64BIT_PDPT_FOR_PHYS";
return "PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD";
return "PGMPOOLKIND_64BIT_PD_FOR_PHYS";
case PGMPOOLKIND_64BIT_PML4:
return "PGMPOOLKIND_64BIT_PML4";
return "PGMPOOLKIND_EPT_PDPT_FOR_PHYS";
return "PGMPOOLKIND_EPT_PD_FOR_PHYS";
return "PGMPOOLKIND_EPT_PT_FOR_PHYS";
#ifndef VBOX_WITH_PGMPOOL_PAGING_ONLY
return "PGMPOOLKIND_ROOT_32BIT_PD";
case PGMPOOLKIND_ROOT_PAE_PD:
return "PGMPOOLKIND_ROOT_PAE_PD";
case PGMPOOLKIND_ROOT_PDPT:
return "PGMPOOLKIND_ROOT_PDPT";
#endif
case PGMPOOLKIND_ROOT_NESTED:
return "PGMPOOLKIND_ROOT_NESTED";
}
return "Unknown kind!";
}
#endif