PGMHandler.cpp revision d6b5539478863f176bfffefc1ada2b9489cfa092
/* $Id$ */
/** @file
* PGM - Page Manager / Monitor, Access Handlers.
*/
/*
* 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
#include "PGMInternal.h"
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/**
* Register a access handler for a physical range.
*
* @returns VBox status code.
* @param pVM VM handle.
* @param enmType Handler type. Any of the PGMPHYSHANDLERTYPE_PHYSICAL* enums.
* @param GCPhys Start physical address.
* @param GCPhysLast Last physical address. (inclusive)
* @param pfnHandlerR3 The R3 handler.
* @param pvUserR3 User argument to the R3 handler.
* @param pszModR0 The R0 handler module. NULL means the default R0 module.
* @param pszHandlerR0 The R0 handler symbol name.
* @param pvUserR0 User argument to the R0 handler.
* @param pszModRC The RC handler module. NULL means the default RC
* module.
* @param pszHandlerRC The RC handler symbol name.
* @param pvUserRC User argument to the RC handler. Values less than
* 0x10000 will not be relocated.
* @param pszDesc Pointer to description string. This must not be freed.
*/
VMMR3DECL(int) PGMR3HandlerPhysicalRegister(PVM pVM, PGMPHYSHANDLERTYPE enmType, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast,
{
LogFlow(("PGMR3HandlerPhysicalRegister: enmType=%d GCPhys=%RGp GCPhysLast=%RGp pfnHandlerR3=%RHv pvUserHC=%RHv pszModR0=%s pszHandlerR0=%s pvUserR0=%RHv pszModRC=%s pszHandlerRC=%s pvUser=%RRv pszDesc=%s\n",
enmType, GCPhys, GCPhysLast, pfnHandlerR3, pvUserR3, pszModR0, pszHandlerR0, pvUserR0, pszHandlerRC, pszModRC, pvUserRC, pszDesc));
/*
* Validate input.
*/
if (!pszModRC)
if (!pszModR0)
/*
* Resolve the R0 handler.
*/
int rc = VINF_SUCCESS;
if (RT_SUCCESS(rc))
{
/*
* Resolve the GC handler.
*/
if (RT_SUCCESS(rc))
}
else
return rc;
}
/**
* Updates the physical page access handlers.
*
* @param pVM VM handle.
* @remark Only used when restoring a saved state.
*/
{
LogFlow(("pgmHandlerPhysicalUpdateAll:\n"));
/*
* Clear and set.
* (the right -> left on the setting pass is just bird speculating on cache hits)
*/
RTAvlroGCPhysDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, true, pgmR3HandlerPhysicalOneClear, pVM);
RTAvlroGCPhysDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, false, pgmR3HandlerPhysicalOneSet, pVM);
}
/**
* Clears all the page level flags for one physical handler range.
*
* @returns 0
* @param pNode Pointer to a PGMPHYSHANDLER.
* @param pvUser VM handle.
*/
{
for (;;)
{
if (RT_SUCCESS(rc))
else
if (--cPages == 0)
return 0;
}
}
/**
* Sets all the page level flags for one physical handler range.
*
* @returns 0
* @param pNode Pointer to a PGMPHYSHANDLER.
* @param pvUser VM handle.
*/
{
for (;;)
{
if (RT_SUCCESS(rc))
else
if (--cPages == 0)
return 0;
}
}
/**
* Register a access handler for a virtual range.
*
* @returns VBox status code.
* @param pVM VM handle.
* @param enmType Handler type. Any of the PGMVIRTHANDLERTYPE_* enums.
* @param GCPtr Start address.
* @param GCPtrLast Last address (inclusive).
* @param pfnInvalidateR3 The R3 invalidate callback (can be 0)
* @param pfnHandlerR3 The R3 handler.
* @param pszHandlerRC The RC handler symbol name.
* @param pszModRC The RC handler module.
* @param pszDesc Pointer to description string. This must not be freed.
*/
VMMR3DECL(int) PGMR3HandlerVirtualRegister(PVM pVM, PGMVIRTHANDLERTYPE enmType, RTGCPTR GCPtr, RTGCPTR GCPtrLast,
const char *pszHandlerRC, const char *pszModRC,
const char *pszDesc)
{
LogFlow(("PGMR3HandlerVirtualRegisterEx: enmType=%d GCPtr=%RGv GCPtrLast=%RGv pszHandlerRC=%p:{%s} pszModRC=%p:{%s} pszDesc=%s\n",
/*
* Validate input.
*/
if (!pszModRC)
{
AssertMsgFailed(("pfnHandlerGC or/and pszModRC is missing\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Resolve the GC handler.
*/
if (RT_SUCCESS(rc))
return PGMR3HandlerVirtualRegisterEx(pVM, enmType, GCPtr, GCPtrLast, pfnInvalidateR3, pfnHandlerR3, pfnHandlerRC, pszDesc);
return rc;
}
/**
* Register an access handler for a virtual range.
*
* @returns VBox status code.
* @param pVM VM handle.
* @param enmType Handler type. Any of the PGMVIRTHANDLERTYPE_* enums.
* @param GCPtr Start address.
* @param GCPtrLast Last address (inclusive).
* @param pfnInvalidateR3 The R3 invalidate callback (can be 0)
* @param pfnHandlerR3 The R3 handler.
* @param pfnHandlerRC The RC handler.
* @param pszDesc Pointer to description string. This must not be freed.
* @thread EMT
*/
/** @todo create a template for virtual handlers (see async i/o), we're wasting space
* duplicating the function pointers now. (Or we will once we add the missing callbacks.) */
VMMDECL(int) PGMR3HandlerVirtualRegisterEx(PVM pVM, PGMVIRTHANDLERTYPE enmType, RTGCPTR GCPtr, RTGCPTR GCPtrLast,
{
Log(("PGMR3HandlerVirtualRegister: enmType=%d GCPtr=%RGv GCPtrLast=%RGv pfnInvalidateR3=%RHv pfnHandlerR3=%RHv pfnHandlerRC=%RRv pszDesc=%s\n",
/*
* Validate input.
*/
switch (enmType)
{
case PGMVIRTHANDLERTYPE_ALL:
break;
case PGMVIRTHANDLERTYPE_WRITE:
if (!pfnHandlerR3)
{
AssertMsgFailed(("No HC handler specified!!\n"));
return VERR_INVALID_PARAMETER;
}
break;
if (pfnHandlerR3)
{
AssertMsgFailed(("R3 handler specified for hypervisor range!?!\n"));
return VERR_INVALID_PARAMETER;
}
break;
default:
return VERR_INVALID_PARAMETER;
}
{
return VERR_INVALID_PARAMETER;
}
if (!pfnHandlerRC)
{
AssertMsgFailed(("pfnHandlerRC is missing\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Allocate and initialize a new entry.
*/
int rc = MMHyperAlloc(pVM, RT_OFFSETOF(PGMVIRTHANDLER, aPhysToVirt[cPages]), 0, MM_TAG_PGM_HANDLERS, (void **)&pNew); /** @todo r=bird: incorrect member name PhysToVirt? */
if (RT_FAILURE(rc))
return rc;
/* Will be synced at next guest execution attempt. */
while (cPages-- > 0)
{
}
/*
* Try to insert it into the tree.
*
* The current implementation doesn't allow multiple handlers for
* the same range this makes everything much simpler and faster.
*/
if (*pRoot != 0)
{
if ( !pCur
if ( pCur
{
/*
* The LDT sometimes conflicts with the IDT and LDT ranges while being
* updated on linux. So, we don't assert simply log it.
*/
Log(("PGMR3HandlerVirtualRegister: Conflict with existing range %RGv-%RGv (%s), req. %RGv-%RGv (%s)\n",
}
}
{
if (enmType != PGMVIRTHANDLERTYPE_HYPERVISOR)
{
}
#ifdef VBOX_WITH_STATISTICS
char szPath[256];
RTStrPrintf(szPath, sizeof(szPath), "/PGM/VirtHandler/Calls/%RGv-%RGv", pNew->Core.Key, pNew->Core.KeyLast);
rc = STAMR3Register(pVM, &pNew->Stat, STAMTYPE_PROFILE, STAMVISIBILITY_USED, szPath, STAMUNIT_TICKS_PER_CALL, pszDesc);
#endif
return VINF_SUCCESS;
}
AssertFailed();
}
/**
* Modify the page invalidation callback handler for a registered virtual range.
* (add more when needed)
*
* @returns VBox status code.
* @param pVM VM handle.
* @param GCPtr Start address.
* @param pfnInvalidateR3 The R3 invalidate callback (can be 0)
* @remarks Doesn't work with the hypervisor access handler type.
*/
VMMDECL(int) PGMHandlerVirtualChangeInvalidateCallback(PVM pVM, RTGCPTR GCPtr, PFNPGMR3VIRTINVALIDATE pfnInvalidateR3)
{
if (pCur)
{
return VINF_SUCCESS;
}
return VERR_INVALID_PARAMETER;
}
/**
* Deregister an access handler for a virtual range.
*
* @returns VBox status code.
* @param pVM VM handle.
* @param GCPtr Start address.
* @thread EMT
*/
{
/*
* Find the handler.
* We naturally assume GCPtr is a unique specification.
*/
PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers, GCPtr);
{
/*
* Reset the flags and remove phys2virt nodes.
*/
/*
* Schedule CR3 sync.
*/
}
else
{
/* must be a hypervisor one then. */
if (RT_UNLIKELY(!pCur))
{
return VERR_INVALID_PARAMETER;
}
Log(("PGMHandlerVirtualDeregister: Removing Hyper Virtual (%d) Range %RGv-%RGv %s\n", pCur->enmType,
}
return VINF_SUCCESS;
}
/**
* Arguments for pgmR3InfoHandlersPhysicalOne and pgmR3InfoHandlersVirtualOne.
*/
typedef struct PGMHANDLERINFOARG
{
/** The output helpers.*/
/** Set if statistics should be dumped. */
bool fStats;
/**
* Info callback for 'pgmhandlers'.
*
* @param pHlp The output helpers.
* @param pszArgs The arguments. phys or virt.
*/
{
/*
* Test input.
*/
if (!fPhysical)
{
}
/*
* Dump the handlers.
*/
if (fPhysical)
{
"Physical handlers: (PhysHandlers=%d (%#x))\n"
"From - To (incl) HandlerHC UserHC HandlerGC UserGC Type Description\n",
RTAvlroGCPhysDoWithAll(&pVM->pgm.s.pTreesR3->PhysHandlers, true, pgmR3InfoHandlersPhysicalOne, &Args);
}
if (fVirtual)
{
"Virtual handlers:\n"
"From - To (excl) HandlerHC HandlerGC Type Description\n");
RTAvlroGCPtrDoWithAll(&pVM->pgm.s.pTreesR3->VirtHandlers, true, pgmR3InfoHandlersVirtualOne, &Args);
}
if (fHyper)
{
"Hypervisor Virtual handlers:\n"
"From - To (excl) HandlerHC HandlerGC Type Description\n");
RTAvlroGCPtrDoWithAll(&pVM->pgm.s.pTreesR3->HyperVirtHandlers, true, pgmR3InfoHandlersVirtualOne, &Args);
}
}
/**
* Displays one physical handler range.
*
* @returns 0
* @param pNode Pointer to a PGMPHYSHANDLER.
* @param pvUser Pointer to command helper functions.
*/
{
const char *pszType;
{
default: pszType = "????"; break;
}
"%RGp - %RGp %RHv %RHv %RRv %RRv %s %s\n",
pCur->Core.Key, pCur->Core.KeyLast, pCur->pfnHandlerR3, pCur->pvUserR3, pCur->pfnHandlerRC, pCur->pvUserRC, pszType, pCur->pszDesc);
#ifdef VBOX_WITH_STATISTICS
#endif
return 0;
}
/**
* Displays one virtual handler range.
*
* @returns 0
* @param pNode Pointer to a PGMVIRTHANDLER.
* @param pvUser Pointer to command helper functions.
*/
{
const char *pszType;
{
default: pszType = "????"; break;
}
pCur->Core.Key, pCur->Core.KeyLast, pCur->pfnHandlerR3, pCur->pfnHandlerRC, pszType, pCur->pszDesc);
#ifdef VBOX_WITH_STATISTICS
#endif
return 0;
}