PGMAllHandler.cpp revision d1011b3f539ad819b5e2c1c3d8152e7113725bf6
/* $Id$ */
/** @file
* PGM - Page Manager / Monitor, Access Handlers.
*/
/*
* Copyright (C) 2006 InnoTek Systemberatung GmbH
*
* 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 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.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_PGM
#include "PGMInternal.h"
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static int pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(PVM pVM, PPGMPHYSHANDLER pCur, PPGMRAMRANGE pRam);
/**
* Register a access handler for a physical range.
*
* @returns VBox status code.
* @retval VINF_SUCCESS when successfully installed.
* @retval VINF_PGM_GCPHYS_ALIASED when the shadow PTs could be updated because
* flagged together with a pool clearing.
* @retval VERR_PGM_HANDLER_PHYSICAL_CONFLICT if the range conflicts with an existing
* one. A debug assertion is raised.
*
* @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 pfnHandlerR0 The R0 handler.
* @param pvUserR0 User argument to the R0 handler.
* @param pfnHandlerGC The GC handler.
* @param pvUserGC User argument to the GC handler.
* This must be a GC pointer because it will be relocated!
* @param pszDesc Pointer to description string. This must not be freed.
*/
PGMDECL(int) PGMHandlerPhysicalRegisterEx(PVM pVM, PGMPHYSHANDLERTYPE enmType, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast,
{
Log(("PGMHandlerPhysicalRegisterEx: enmType=%d GCPhys=%VGp GCPhysLast=%VGp pfnHandlerR3=%VHv pvUserR3=%VHv pfnHandlerR0=%VHv pvUserR0=%VHv pfnHandlerGC=%VGv pvUserGC=%VGv pszDesc=%s\n",
enmType, GCPhys, GCPhysLast, pfnHandlerR3, pvUserR3, pfnHandlerR0, pvUserR0, pfnHandlerGC, pvUserGC, HCSTRING(pszDesc)));
/*
* Validate input.
*/
if (GCPhys >= GCPhysLast)
{
return VERR_INVALID_PARAMETER;
}
switch (enmType)
{
case PGMPHYSHANDLERTYPE_MMIO:
break;
default:
return VERR_INVALID_PARAMETER;
}
if (!pfnHandlerGC)
{
AssertMsgFailed(("!pfnHandlerGC\n"));
return VERR_INVALID_PARAMETER;
}
{
return VERR_INVALID_PARAMETER;
}
/*
* We require the range to be within registered ram.
* There is no apparent need to support ranges which cover more than one ram range.
*/
if ( !pRam
{
#ifdef IN_RING3
/*
* If this is an MMIO registration, we'll just add a range for it.
*/
if ( enmType == PGMPHYSHANDLERTYPE_MMIO
&& ( !pRam
)
{
int rc = PGMR3PhysRegister(pVM, NULL, GCPhys, cb, MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO, NULL, pszDesc);
if (VBOX_FAILURE(rc))
return rc;
/* search again. */
}
if ( !pRam
#endif /* IN_RING3 */
{
#ifdef IN_RING3
#endif
}
}
/*
* Allocate and initialize the new entry.
*/
if (VBOX_FAILURE(rc))
return rc;
/*
* Try insert into list.
*/
{
if (rc == VINF_PGM_GCPHYS_ALIASED)
{
}
#ifndef IN_RING3
#else
#endif
if (rc != VINF_SUCCESS)
return rc;
}
#if defined(IN_RING3) && defined(VBOX_STRICT)
#endif
AssertMsgFailed(("Conflict! GCPhys=%VGp GCPhysLast=%VGp pszDesc=%s\n", GCPhys, GCPhysLast, pszDesc));
}
/**
* Sets ram range flags and attempts updating shadow PTs.
*
* @returns VBox status code.
* @retval VINF_SUCCESS when shadow PTs was successfully updated.
* @retval VINF_PGM_GCPHYS_ALIASED when the shadow PTs could be updated because
* @param pVM The VM handle.
* @param pCur The physical handler.
*/
static int pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(PVM pVM, PPGMPHYSHANDLER pCur, PPGMRAMRANGE pRam)
{
/*
* Iterate the guest ram pages updating the flags and flushing PT entries
* mapping the page.
*/
bool fFlushTLBs = false;
#if defined(PGMPOOL_WITH_GCPHYS_TRACKING) || defined(PGMPOOL_WITH_CACHE)
int rc = VINF_SUCCESS;
#else
const int rc = VINF_PGM_GCPHYS_ALIASED;
#endif
for (;;)
{
/* Physical chunk in dynamically allocated range not present? */
{
#ifdef IN_RING3
#else
#endif
if (rc2 != VINF_SUCCESS)
return rc2;
}
{
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
/* This code also makes ASSUMPTIONS about the cRefs and stuff. */
if (u16)
{
else if (u16 != ((MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT)) | MM_RAM_FLAGS_IDX_OVERFLOWED))
else
fFlushTLBs = true;
}
#elif defined(PGMPOOL_WITH_CACHE)
fFlushTLBs = true;
#endif
}
/* next */
if (--cPages == 0)
break;
i++;
}
{
Log(("pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs: flushing guest TLBs\n"));
}
else
return rc;
}
/**
* Register a physical page access handler.
*
* @returns VBox status code.
* @param pVM VM Handle.
* @param GCPhys Start physical address.
*/
{
/*
* Find the handler.
*/
PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys);
if (pCur)
{
LogFlow(("PGMHandlerPhysicalDeregister: Removing Range %#VGp-%#VGp %s\n",
/*
* Clear the page bits and notify the REM about this change.
*/
return VINF_SUCCESS;
}
return VERR_PGM_HANDLER_NOT_FOUND;
}
/**
* Shared code with modify.
*/
{
/*
* Page align the range.
*/
{
if (GCPhysStart & PAGE_OFFSET_MASK)
{
if (PGMRamTestFlags(&pVM->pgm.s, GCPhysStart, MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_PHYSICAL_HANDLER | MM_RAM_FLAGS_PHYSICAL_ALL | MM_RAM_FLAGS_PHYSICAL_WRITE | MM_RAM_FLAGS_PHYSICAL_TEMP_OFF))
{
if ( GCPhys > GCPhysLast
|| GCPhys < GCPhysStart)
return;
}
else
}
if (GCPhysLast & PAGE_OFFSET_MASK)
{
if (PGMRamTestFlags(&pVM->pgm.s, GCPhysLast, MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_PHYSICAL_HANDLER | MM_RAM_FLAGS_PHYSICAL_ALL | MM_RAM_FLAGS_PHYSICAL_WRITE | MM_RAM_FLAGS_PHYSICAL_TEMP_OFF))
{
if ( GCPhys < GCPhysStart
|| GCPhys > GCPhysLast)
return;
GCPhysLast = GCPhys;
}
else
}
}
/*
* Tell REM.
*/
PGMRamGCPhys2HCPtr(&pVM->pgm.s, GCPhysStart, &pvRange); /* ASSUMES it doesn't change pvRange on failure. */
#ifndef IN_RING3
REMNotifyHandlerPhysicalDeregister(pVM, pCur->enmType, GCPhysStart, GCPhysLast - GCPhysStart + 1, !!pCur->pfnHandlerR3, pvRange);
#else
REMR3NotifyHandlerPhysicalDeregister(pVM, pCur->enmType, GCPhysStart, GCPhysLast - GCPhysStart + 1, !!pCur->pfnHandlerR3, pvRange);
#endif
}
/**
* Resets ram range flags.
*
* @returns VBox status code.
* @retval VINF_SUCCESS when shadow PTs was successfully updated.
* @param pVM The VM handle.
* @param pCur The physical handler.
*
* @remark We don't start messing with the shadow page tables, as we've already got code
* in Trap0e which deals with out of sync handler flags (originally conceived for
* global pages).
*/
{
/*
* Iterate the guest ram pages updating the flags and flushing PT entries
* mapping the page.
*/
for (;;)
{
&pRamHint);
/* next */
if (--cPages == 0)
break;
}
/*
* Check for partial start page.
*/
{
for (;;)
{
PPGMPHYSHANDLER pBelow = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys, false);
if ( !pBelow
break;
/* next? */
break;
}
}
/*
* Check for partial end page.
*/
{
for (;;)
{
PPGMPHYSHANDLER pAbove = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys, true);
if ( !pAbove
break;
/* next? */
break;
}
}
}
/**
* Modify a physical page access handler.
*
* Modification can only be done to the range it self, not the type or anything else.
*
* @returns VBox status code.
* For all return codes other than VERR_PGM_HANDLER_NOT_FOUND and VINF_SUCCESS the range is deregistered
* and a new registration must be performed!
* @param pVM VM handle.
* @param GCPhysCurrent Current location.
* @param GCPhys New location.
* @param GCPhysLast New last location.
*/
PGMDECL(int) PGMHandlerPhysicalModify(PVM pVM, RTGCPHYS GCPhysCurrent, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast)
{
/*
* Remove it.
*/
int rc;
PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhysCurrent);
if (pCur)
{
/*
* Clear the ram flags. (We're gonna move or free it!)
*/
PGMRamGCPhys2HCPtr(&pVM->pgm.s, GCPhysCurrent, &pvRange); /* ASSUMES it doesn't change pvRange on failure. */
/*
* Validate the new range, modify and reinsert.
*/
if (GCPhysLast >= GCPhys)
{
/*
* We require the range to be within registered ram.
* There is no apparent need to support ranges which cover more than one ram range.
*/
if ( pRam
{
{
/*
* Set ram flags, flush shadow PT entries and finally tell REM about this.
*/
if (rc == VINF_PGM_GCPHYS_ALIASED)
{
}
#ifndef IN_RING3
#else
#endif
Log(("PGMHandlerPhysicalModify: GCPhysCurrent=%VGp -> GCPhys=%VGp GCPhysLast=%VGp\n",
return VINF_SUCCESS;
}
}
else
{
}
}
else
{
}
/*
* Invalid new location, free it.
* We've only gotta notify REM and free the memory.
*/
}
else
{
}
return rc;
}
/**
* Changes the callbacks associated with a physical access handler.
*
* @returns VBox status code.
* @param pVM VM Handle.
* @param GCPhys Start physical address.
* @param pfnHandlerR3 The R3 handler.
* @param pvUserR3 User argument to the R3 handler.
* @param pfnHandlerR0 The R0 handler.
* @param pvUserR0 User argument to the R0 handler.
* @param pfnHandlerGC The GC handler.
* @param pvUserGC User argument to the GC handler.
* This must be a GC pointer because it will be relocated!
* @param pszDesc Pointer to description string. This must not be freed.
*/
{
/*
* Get the handler.
*/
int rc = VINF_SUCCESS;
PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys);
if (pCur)
{
/*
* Change callbacks.
*/
}
else
{
}
return rc;
}
/**
* Splitts a physical access handler in two.
*
* @returns VBox status code.
* @param pVM VM Handle.
* @param GCPhys Start physical address of the handler.
* @param GCPhysSplit The split address.
*/
{
/*
* Do the allocation without owning the lock.
*/
if (VBOX_FAILURE(rc))
return rc;
/*
* Get the handler.
*/
PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys);
if (pCur)
{
{
/*
* Create new handler node for the 2nd half.
*/
pNew->cPages = (pNew->Core.KeyLast - (pNew->Core.Key & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
pCur->cPages = (pCur->Core.KeyLast - (pCur->Core.Key & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
{
LogFlow(("PGMHandlerPhysicalSplit: %VGp-%VGp and %VGp-%VGp\n",
return VINF_SUCCESS;
}
AssertMsgFailed(("whu?\n"));
}
else
{
AssertMsgFailed(("outside range: %VGp-%VGp split %VGp\n", pCur->Core.Key, pCur->Core.KeyLast, GCPhysSplit));
}
}
else
{
}
return rc;
}
/**
* Joins up two adjacent physical access handlers which has the same callbacks.
*
* @returns VBox status code.
* @param pVM VM Handle.
* @param GCPhys1 Start physical address of the first handler.
* @param GCPhys2 Start physical address of the second handler.
*/
{
/*
* Get the handlers.
*/
int rc;
PPGMPHYSHANDLER pCur1 = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys1);
if (pCur1)
{
PPGMPHYSHANDLER pCur2 = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys2);
if (pCur2)
{
/*
* Make sure that they are adjacent, and that they've got the same callbacks.
*/
{
{
PPGMPHYSHANDLER pCur3 = (PPGMPHYSHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys2);
{
pCur1->cPages = (pCur1->Core.KeyLast - (pCur1->Core.Key & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
LogFlow(("PGMHandlerPhysicalJoin: %VGp-%VGp %VGp-%VGp\n",
return VINF_SUCCESS;
}
}
else
{
AssertMsgFailed(("mismatching handlers\n"));
}
}
else
{
AssertMsgFailed(("not adjacent: %VGp-%VGp %VGp-%VGp\n",
}
}
else
{
}
}
else
{
}
return rc;
}
/**
* Resets any modifications to individual pages in a physical
* page access handler region.
*
* This is used in pair with PGMHandlerPhysicalModify().
*
* @returns VBox status code.
* @param pVM VM Handle
* @param GCPhys Start physical address earlier passed to PGMR3HandlerPhysicalRegister().
*/
{
/*
* Find the handler.
*/
PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys);
if (pCur)
{
/*
* Validate type.
*/
{
{
/*
* Set the flags and flush shadow PT entries.
*/
if (rc == VINF_PGM_GCPHYS_ALIASED)
{
}
return VINF_SUCCESS;
}
/*
* Invalid.
*/
case PGMPHYSHANDLERTYPE_MMIO:
return VERR_INTERNAL_ERROR;
default:
return VERR_INTERNAL_ERROR;
}
}
return VERR_PGM_HANDLER_NOT_FOUND;
}
/**
* Search for virtual handler with matching physical address
*
* @returns VBox status code
* @param pVM The VM handle.
* @param GCPhys GC physical address to search for.
* @param ppVirt Where to store the pointer to the virtual handler structure.
* @param piPage Where to store the pointer to the index of the cached physical page.
*/
int pgmHandlerVirtualFindByPhysAddr(PVM pVM, RTGCPHYS GCPhys, PPGMVIRTHANDLER *ppVirt, unsigned *piPage)
{
pCur = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysRangeGet(&CTXSUFF(pVM->pgm.s.pTrees)->PhysToVirtHandlers, GCPhys);
if (pCur)
{
/* found a match! */
#endif
LogFlow(("PHYS2VIRT: found match for %VGp -> %VGv *piPage=%#x\n",
return VINF_SUCCESS;
}
return VERR_PGM_HANDLER_NOT_FOUND;
}
/**
* Deal with aliases in phys2virt.
*
* @param pVM The VM handle.
* @param pPhys2Virt The node we failed insert.
*/
{
/*
* First find the node which is conflicting with us.
*/
/** @todo Deal with partial overlapping. (Unlikly situation, so I'm too lazy to do anything about it now.) */
PPGMPHYS2VIRTHANDLER pHead = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTXSUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key);
if (!pHead)
{
/** @todo do something clever here... */
#ifdef IN_RING3
LogRel(("pgmHandlerVirtualInsertAliased: %VGp-%VGp\n", pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast));
#endif
pPhys2Virt->offNextAlias = 0;
return;
}
#endif
/** @todo check if the current head node covers the ground we do. This is highly unlikely
* and I'm too lazy to implement this now as it will require sorting the list and stuff like that. */
/*
* Insert ourselves as the next node.
*/
else
{
PPGMPHYS2VIRTHANDLER pNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pHead + (pHead->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
}
Log(("pgmHandlerVirtualInsertAliased: %VGp-%VGp offNextAlias=%#RX32\n", pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias));
}
/**
* Resets one virtual handler range.
*
* @returns 0
* @param pNode Pointer to a PGMVIRTHANDLER.
* @param pvUser The VM handle.
*/
{
/*
* Calc flags.
*/
unsigned fFlags;
{
case PGMVIRTHANDLERTYPE_EIP:
case PGMVIRTHANDLERTYPE_WRITE: fFlags = MM_RAM_FLAGS_VIRTUAL_HANDLER | MM_RAM_FLAGS_VIRTUAL_WRITE; break;
case PGMVIRTHANDLERTYPE_ALL: fFlags = MM_RAM_FLAGS_VIRTUAL_HANDLER | MM_RAM_FLAGS_VIRTUAL_ALL; break;
/* hypervisor handlers need no flags and wouldn't have nowhere to put them in any case. */
return 0;
default:
return 0;
}
/*
* Iterate the pages and apply the flags.
*/
{
{
/* Update the flags. */
/* Need to insert the page in the Phys2Virt lookup tree? */
{
#endif
else
else
AssertReleaseMsg(RTAvlroGCPhysGet(&pVM->pgm.s.CTXSUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key) == &pPhys2Virt->Core,
("%VGp-%VGp offNextAlias=%#RX32\n",
#endif
Log2(("PHYS2VIRT: Insert physical range %VGp-%VGp offNextAlias=%#RX32 %s\n",
pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias, R3STRING(pCur->pszDesc)));
}
}
offPage = 0;
}
return 0;
}
#ifndef IN_RING3
# ifdef IN_RING0
/** @todo try combine this with iom and em. */
/**
* Read callback for disassembly function; supports reading bytes that cross a page boundary
*
* @returns VBox status code.
* @param pSrc GC source pointer
* @param pDest HC destination pointer
* @param size Number of bytes to read
* @param dwUserdata Callback specific user data (pCpu)
*
*/
DECLCALLBACK(int32_t) pgmReadBytes(RTHCUINTPTR pSrc, uint8_t *pDest, uint32_t size, RTHCUINTPTR dwUserdata)
{
return rc;
}
{
}
# else /* !IN_RING0 (i.e. in IN_GC) */
{
}
#endif /* !IN_RING0 (i.e. in IN_GC) */
/**
* \#PF Handler callback for Guest ROM range write access.
* We simply ignore the writes or fall back to the recompiler if we don't support the instruction.
*
* @returns VBox status code (appropritate for trap handling and GC return).
* @param pVM VM Handle.
* @param uErrorCode CPU Error code.
* @param pRegFrame Trap register frame.
* @param pvFault The fault address (cr2).
* @param GCPhysFault The GC physical address corresponding to pvFault.
* @param pvUser User argument.
*/
PGMDECL(int) pgmGuestROMWriteHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, void *pvFault, RTGCPHYS GCPhysFault, void *pvUser)
{
Cpu.mode = SELMIsSelector32Bit(pVM, pRegFrame->eflags, pRegFrame->cs, &pRegFrame->csHid) ? CPUMODE_32BIT : CPUMODE_16BIT;
{
int rc = SELMValidateAndConvertCSAddr(pVM, pRegFrame->eflags, pRegFrame->ss, pRegFrame->cs, &pRegFrame->csHid, (RTGCPTR)pRegFrame->eip, &GCPtrCode);
if (VBOX_SUCCESS(rc))
{
if (VBOX_SUCCESS(rc))
{
/* ASSUMES simple instructions.
* For instance 'pop [ROM_ADDRESS]' or 'and [ROM_ADDRESS], eax' better
* not occure or we'll screw up the cpu state.
*/
/** @todo We're assuming too much here I think. */
{
/*
* Move on to the next instruction.
*/
return VINF_SUCCESS;
}
LogFlow(("pgmGuestROMWriteHandler: wrong prefix!!\n"));
}
}
}
return VINF_EM_RAW_EMULATE_INSTR;
}
#endif /* !IN_RING3 */