PGMPool.cpp revision ec588a4ac8429a8b6c744544818b3ce3b2c75690
/* $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.
*/
/** @page pg_pgm_pool PGM Shadow Page Pool
*
* Motivations:
* -# Relationship between shadow page tables and physical guest pages. This
* should allow us to skip most of the global flushes now following access
* handler changes. The main expense is flushing shadow pages.
* -# Limit the pool size (currently it's kind of limitless IIRC).
* -# Allocate shadow pages from GC. Currently we're allocating at SyncCR3 time.
* -# Required for 64-bit guests.
* -# Combining the PD cache and page pool in order to simplify caching.
*
*
* @section sec_pgm_pool_outline Design Outline
*
* The shadow page pool tracks pages used for shadowing paging structures (i.e. page
* tables, page directory, page directory pointer table and page map level-4). Each
* page in the pool has an unique identifier. This identifier is used to link a guest
* physical page to a shadow PT. The identifier is a non-zero value and has a
* relativly low max value - say 14 bits. This makes it possible to fit it into the
* upper bits of the of the aHCPhys entries in the ram range.
*
* By restricting host physical memory to the first 48 bits (which is the announced
* physical memory range of the K8L chip (scheduled for 2008)), we can safely use the
* upper 16 bits for shadow page ID and reference counting.
*
* Now, it's possible for a page to be aliased, i.e. mapped by more than one PT or
* PD. This is solved by creating a list of physical cross reference extents when
* ever this happens. Each node in the list (extent) is can contain 3 page pool
* indexes. The list it self is chained using indexes into the paPhysExt array.
*
*
* @section sec_pgm_pool_life Life Cycle of a Shadow Page
*
* -# The SyncPT function requests a page from the pool.
* address of the page it's shadowing, and more.
* -# The pool responds to the request by allocating a new page.
* When the cache is enabled, it will first check if it's in the cache.
* Should the pool be exhausted, one of two things can be done:
* -# Flush the whole pool and current CR3.
* -# Use the cache to find a page which can be flushed (~age).
* -# The SyncPT function will sync one or more pages and insert it into the
* shadow PD.
* -# The SyncPage function may sync more pages on a later \#PFs.
* -# The page is freed / flushed in SyncCR3 (perhaps) and some other cases.
* When caching is enabled, the page isn't flush but remains in the cache.
*
*
* @section sec_pgm_pool_impl Monitoring
*
* We always monitor PAGE_SIZE chunks of memory. When we've got multiple shadow
* sharing the monitor get linked using the iMonitoredNext/Prev. The head page
* is the pvUser to the access handlers.
*
*
* @section sec_pgm_pool_impl Implementation
*
* The pool will take pages from the MM page pool. The tracking data (attributes,
* bitmaps and so on) are allocated from the hypervisor heap. The pool content can
* be accessed both by using the page id and the physical address (HC). The former
* is managed by means of an array, the latter by an offset based AVL tree.
*
* Flushing of a pool page means that we iterate the content (we know what kind
* it is) and updates the link information in the ram range.
*
* ...
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_PGM_POOL
#include "PGMInternal.h"
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
#ifdef PGMPOOL_WITH_MONITORING
static DECLCALLBACK(int) pgmR3PoolAccessHandler(PVM pVM, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser);
#endif /* PGMPOOL_WITH_MONITORING */
/**
* Initalizes the pool
*
* @returns VBox status code.
* @param pVM The VM handle.
*/
{
/*
* Query Pool config.
*/
else if (VBOX_FAILURE(rc))
else
else if (VBOX_FAILURE(rc))
else
else if (VBOX_FAILURE(rc))
else
bool fCacheEnabled;
fCacheEnabled = true;
else if (VBOX_FAILURE(rc))
Log(("pgmR3PoolInit: cMaxPages=%#RX16 cMaxUsers=%#RX16 cMaxPhysExts=%#RX16 fCacheEnable=%RTbool\n",
/*
* Allocate the data structures.
*/
#ifdef PGMPOOL_WITH_USER_TRACKING
#endif
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
#endif
if (VBOX_FAILURE(rc))
return rc;
/*
* Initialize it.
*/
#ifdef PGMPOOL_WITH_USER_TRACKING
pPool->iUserFreeHead = 0;
for (unsigned i = 0; i < cMaxUsers; i++)
{
}
#endif
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
pPool->iPhysExtFreeHead = 0;
for (unsigned i = 0; i < cMaxPhysExts; i++)
{
}
#endif
#ifdef PGMPOOL_WITH_CACHE
#endif
#ifdef PGMPOOL_WITH_MONITORING
#endif
pPool->HCPhysTree = 0;
/* The NIL entry. */
Assert(NIL_PGMPOOL_IDX == 0);
/* The Shadow 32-bit PD. (32 bits guest paging) */
/* The Shadow PAE PDs. This is actually 4 pages! (32 bits guest paging) */
/* The Shadow PAE PDs for PAE guest mode. */
for (unsigned i = 0; i < X86_PG_PAE_PDPE_ENTRIES; i++)
{
}
/* The Shadow PDPT. */
/* The Shadow AMD64 CR3. */
/* The Shadow AMD64 CR3. */
/*
* Set common stuff.
*/
{
#ifdef PGMPOOL_WITH_USER_TRACKING
#endif
#ifdef PGMPOOL_WITH_MONITORING
#endif
#ifdef PGMPOOL_WITH_CACHE
#endif
}
#ifdef VBOX_WITH_STATISTICS
/*
* Register statistics.
*/
STAM_REG(pVM, &pPool->cCurPages, STAMTYPE_U16, "/PGM/Pool/cCurPages", STAMUNIT_PAGES, "Current pool size.");
STAM_REG(pVM, &pPool->cMaxPages, STAMTYPE_U16, "/PGM/Pool/cMaxPages", STAMUNIT_PAGES, "Max pool size.");
STAM_REG(pVM, &pPool->cUsedPages, STAMTYPE_U16, "/PGM/Pool/cUsedPages", STAMUNIT_PAGES, "The number of pages currently in use.");
STAM_REG(pVM, &pPool->cUsedPagesHigh, STAMTYPE_U16_RESET, "/PGM/Pool/cUsedPagesHigh", STAMUNIT_PAGES, "The high watermark for cUsedPages.");
STAM_REG(pVM, &pPool->StatAlloc, STAMTYPE_PROFILE_ADV, "/PGM/Pool/Alloc", STAMUNIT_TICKS_PER_CALL, "Profiling of pgmPoolAlloc.");
STAM_REG(pVM, &pPool->StatClearAll, STAMTYPE_PROFILE, "/PGM/Pool/ClearAll", STAMUNIT_TICKS_PER_CALL, "Profiling of pgmPoolClearAll.");
STAM_REG(pVM, &pPool->StatFlushAllInt, STAMTYPE_PROFILE, "/PGM/Pool/FlushAllInt", STAMUNIT_TICKS_PER_CALL, "Profiling of pgmPoolFlushAllInt.");
STAM_REG(pVM, &pPool->StatFlushPage, STAMTYPE_PROFILE, "/PGM/Pool/FlushPage", STAMUNIT_TICKS_PER_CALL, "Profiling of pgmPoolFlushPage.");
STAM_REG(pVM, &pPool->StatFree, STAMTYPE_PROFILE, "/PGM/Pool/Free", STAMUNIT_TICKS_PER_CALL, "Profiling of pgmPoolFree.");
STAM_REG(pVM, &pPool->StatZeroPage, STAMTYPE_PROFILE, "/PGM/Pool/ZeroPage", STAMUNIT_TICKS_PER_CALL, "Profiling time spend zeroing pages. Overlaps with Alloc.");
# ifdef PGMPOOL_WITH_USER_TRACKING
STAM_REG(pVM, &pPool->cMaxUsers, STAMTYPE_U16, "/PGM/Pool/Track/cMaxUsers", STAMUNIT_COUNT, "Max user tracking records.");
STAM_REG(pVM, &pPool->cPresent, STAMTYPE_U32, "/PGM/Pool/Track/cPresent", STAMUNIT_COUNT, "Number of present page table entries.");
STAM_REG(pVM, &pPool->StatTrackDeref, STAMTYPE_PROFILE, "/PGM/Pool/Track/Deref", STAMUNIT_OCCURENCES, "Profiling of pgmPoolTrackDeref.");
STAM_REG(pVM, &pPool->StatTrackFlushGCPhysPT, STAMTYPE_PROFILE, "/PGM/Pool/Track/FlushGCPhysPT", STAMUNIT_OCCURENCES, "Profiling of pgmPoolTrackFlushGCPhysPT.");
STAM_REG(pVM, &pPool->StatTrackFlushGCPhysPTs, STAMTYPE_PROFILE, "/PGM/Pool/Track/FlushGCPhysPTs", STAMUNIT_OCCURENCES, "Profiling of pgmPoolTrackFlushGCPhysPTs.");
STAM_REG(pVM, &pPool->StatTrackFlushGCPhysPTsSlow, STAMTYPE_PROFILE, "/PGM/Pool/Track/FlushGCPhysPTsSlow", STAMUNIT_OCCURENCES, "Profiling of pgmPoolTrackFlushGCPhysPTsSlow.");
STAM_REG(pVM, &pPool->StatTrackFreeUpOneUser, STAMTYPE_COUNTER, "/PGM/Pool/Track/FreeUpOneUser", STAMUNIT_OCCURENCES, "The number of times we were out of user tracking records.");
# endif
# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
STAM_REG(pVM, &pPool->StatTrackDerefGCPhys, STAMTYPE_PROFILE, "/PGM/Pool/Track/DrefGCPhys", STAMUNIT_OCCURENCES, "Profiling deref activity related tracking GC physical pages.");
STAM_REG(pVM, &pPool->StatTrackLinearRamSearches, STAMTYPE_COUNTER, "/PGM/Pool/Track/LinearRamSearches", STAMUNIT_OCCURENCES, "The number of times we had to do linear ram searches.");
STAM_REG(pVM, &pPool->StamTrackPhysExtAllocFailures,STAMTYPE_COUNTER, "/PGM/Pool/Track/PhysExtAllocFailures", STAMUNIT_OCCURENCES, "The number of failing pgmPoolTrackPhysExtAlloc calls.");
# endif
# ifdef PGMPOOL_WITH_MONITORING
STAM_REG(pVM, &pPool->StatMonitorGC, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/GC", STAMUNIT_TICKS_PER_CALL, "Profiling the GC PT access handler.");
STAM_REG(pVM, &pPool->StatMonitorGCEmulateInstr, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/GCEmulateInstr", STAMUNIT_OCCURENCES, "Times we've failed interpreting the instruction.");
STAM_REG(pVM, &pPool->StatMonitorGCFlushPage, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/GCFlushPage", STAMUNIT_TICKS_PER_CALL, "Profiling the pgmPoolFlushPage calls made from the GC PT access handler.");
STAM_REG(pVM, &pPool->StatMonitorGCFork, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/GCFork", STAMUNIT_OCCURENCES, "Times we've detected fork().");
STAM_REG(pVM, &pPool->StatMonitorGCHandled, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/GCHandled", STAMUNIT_TICKS_PER_CALL, "Profiling the GC access we've handled (except REP STOSD).");
STAM_REG(pVM, &pPool->StatMonitorGCIntrFailPatch1, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/GCIntrFailPatch1", STAMUNIT_OCCURENCES, "Times we've failed interpreting a patch code instruction.");
STAM_REG(pVM, &pPool->StatMonitorGCIntrFailPatch2, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/GCIntrFailPatch2", STAMUNIT_OCCURENCES, "Times we've failed interpreting a patch code instruction during flushing.");
STAM_REG(pVM, &pPool->StatMonitorGCRepPrefix, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/GCRepPrefix", STAMUNIT_OCCURENCES, "The number of times we've seen rep prefixes we can't handle.");
STAM_REG(pVM, &pPool->StatMonitorGCRepStosd, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/GCRepStosd", STAMUNIT_TICKS_PER_CALL, "Profiling the REP STOSD cases we've handled.");
STAM_REG(pVM, &pPool->StatMonitorHC, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/HC", STAMUNIT_TICKS_PER_CALL, "Profiling the HC PT access handler.");
STAM_REG(pVM, &pPool->StatMonitorHCEmulateInstr, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/HCEmulateInstr", STAMUNIT_OCCURENCES, "Times we've failed interpreting the instruction.");
STAM_REG(pVM, &pPool->StatMonitorHCFlushPage, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/HCFlushPage", STAMUNIT_TICKS_PER_CALL, "Profiling the pgmPoolFlushPage calls made from the HC PT access handler.");
STAM_REG(pVM, &pPool->StatMonitorHCFork, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/HCFork", STAMUNIT_OCCURENCES, "Times we've detected fork().");
STAM_REG(pVM, &pPool->StatMonitorHCHandled, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/HCHandled", STAMUNIT_TICKS_PER_CALL, "Profiling the HC access we've handled (except REP STOSD).");
STAM_REG(pVM, &pPool->StatMonitorHCRepPrefix, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/HCRepPrefix", STAMUNIT_OCCURENCES, "The number of times we've seen rep prefixes we can't handle.");
STAM_REG(pVM, &pPool->StatMonitorHCRepStosd, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/HCRepStosd", STAMUNIT_TICKS_PER_CALL, "Profiling the REP STOSD cases we've handled.");
STAM_REG(pVM, &pPool->StatMonitorHCAsync, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/HCAsync", STAMUNIT_OCCURENCES, "Times we're called in an async thread and need to flush.");
STAM_REG(pVM, &pPool->cModifiedPages, STAMTYPE_U16, "/PGM/Pool/Monitor/cModifiedPages", STAMUNIT_PAGES, "The current cModifiedPages value.");
STAM_REG(pVM, &pPool->cModifiedPagesHigh, STAMTYPE_U16_RESET, "/PGM/Pool/Monitor/cModifiedPagesHigh", STAMUNIT_PAGES, "The high watermark for cModifiedPages.");
# endif
# ifdef PGMPOOL_WITH_CACHE
STAM_REG(pVM, &pPool->StatCacheHits, STAMTYPE_COUNTER, "/PGM/Pool/Cache/Hits", STAMUNIT_OCCURENCES, "The number of pgmPoolAlloc calls satisfied by the cache.");
STAM_REG(pVM, &pPool->StatCacheMisses, STAMTYPE_COUNTER, "/PGM/Pool/Cache/Misses", STAMUNIT_OCCURENCES, "The number of pgmPoolAlloc calls not statisfied by the cache.");
STAM_REG(pVM, &pPool->StatCacheKindMismatches, STAMTYPE_COUNTER, "/PGM/Pool/Cache/KindMismatches", STAMUNIT_OCCURENCES, "The number of shadow page kind mismatches. (Better be low, preferably 0!)");
STAM_REG(pVM, &pPool->StatCacheFreeUpOne, STAMTYPE_COUNTER, "/PGM/Pool/Cache/FreeUpOne", STAMUNIT_OCCURENCES, "The number of times the cache was asked to free up a page.");
STAM_REG(pVM, &pPool->StatCacheCacheable, STAMTYPE_COUNTER, "/PGM/Pool/Cache/Cacheable", STAMUNIT_OCCURENCES, "The number of cacheable allocations.");
STAM_REG(pVM, &pPool->StatCacheUncacheable, STAMTYPE_COUNTER, "/PGM/Pool/Cache/Uncacheable", STAMUNIT_OCCURENCES, "The number of uncacheable allocations.");
# endif
#endif /* VBOX_WITH_STATISTICS */
return VINF_SUCCESS;
}
/**
* Relocate the page pool data.
*
* @param pVM The VM handle.
*/
{
#ifdef PGMPOOL_WITH_USER_TRACKING
#endif
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
#endif
#ifdef PGMPOOL_WITH_MONITORING
int rc = PDMR3GetSymbolGC(pVM, NULL, "pgmPoolAccessHandler", &pVM->pgm.s.pPoolHC->pfnAccessHandlerGC);
/* init order hack. */
{
}
#endif
}
/**
* Reset notification.
*
* This will flush the pool.
* @param pVM The VM handle.
*/
{
}
/**
* Grows the shadow page pool.
*
* I.e. adds more pages to it, assuming that hasn't reached cMaxPages yet.
*
* @returns VBox status code.
* @param pVM The VM handle.
*/
{
/*
* How much to grow it by?
*/
{
{
Log(("We're out of memory!! i=%d\n", i));
return i ? VINF_SUCCESS : VERR_NO_PAGE_MEMORY;
}
#ifdef PGMPOOL_WITH_USER_TRACKING
#endif
#ifdef PGMPOOL_WITH_MONITORING
#endif
#ifdef PGMPOOL_WITH_CACHE
#endif
/* commit it */
}
return VINF_SUCCESS;
}
#ifdef PGMPOOL_WITH_MONITORING
/**
* Worker used by pgmR3PoolAccessHandler when it's invoked by an async thread.
*
* @param pPool The pool.
* @param pPage The page.
*/
{
/* for the present this should be safe enough I think... */
if ( pPage->fReusedFlushPending
}
/**
* \#PF Handler callback for PT write accesses.
*
* The handler can not raise any faults, it's mainly for monitoring write access
* to certain pages.
*
* @returns VINF_SUCCESS if the handler have carried out the operation.
* @returns VINF_PGM_HANDLER_DO_DEFAULT if the caller should carry out the access operation.
* @param pVM VM Handle.
* @param GCPhys The physical address the guest is writing to.
* @param pvPhys The HC mapping of that address.
* @param enmAccessType The access type.
* @param pvUser User argument.
*/
static DECLCALLBACK(int) pgmR3PoolAccessHandler(PVM pVM, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser)
{
LogFlow(("pgmR3PoolAccessHandler: GCPhys=%VGp %p:{.Core=%RHp, .idx=%d, .GCPhys=%RGp, .enmType=%d}\n",
/*
* We don't have to be very sophisiticated about this since there are relativly few calls here.
* However, we must try our best to detect any non-cpu accesses (disk / networking).
*
* Just to make life more interesting, we'll have to deal with the async threads too.
* We cannot flush a page if we're in an async thread because of REM notifications.
*/
{
Log(("pgmR3PoolAccessHandler: async thread, requesting EMT to flush the page: %p:{.Core=%RHp, .idx=%d, .GCPhys=%RGp, .enmType=%d}\n",
if (!pPage->fReusedFlushPending)
{
int rc = VMR3ReqCallEx(pPool->pVMHC, NULL, 0, VMREQFLAGS_NO_WAIT | VMREQFLAGS_VOID, (PFNRT)pgmR3PoolFlushReusedPage, 2, pPool, pPage);
pPage->fReusedFlushPending = true;
}
/** @todo r=bird: making unsafe assumption about not crossing entries here! */
while (cbBuf > 4)
{
cbBuf -= 4;
GCPhys += 4;
}
}
&& cbBuf <= 4)
{
/* Clear the shadow entry. */
if (!pPage->cModifications++)
/** @todo r=bird: making unsafe assumption about not crossing entries here! */
}
else
{
pgmPoolMonitorChainFlush(pPool, pPage); /* ASSUME that VERR_PGM_POOL_CLEARED can be ignored here and that FFs will deal with it in due time. */
}
return VINF_PGM_HANDLER_DO_DEFAULT;
}
#endif /* PGMPOOL_WITH_MONITORING */