MMAll.cpp revision 3ef7379d968d951ab9154926b5f6d51856c586e6
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/* $Id$ */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** @file
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * MM - Memory Manager - Any Context.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/*
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * available from http://www.virtualbox.org. This file is free software;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * you can redistribute it and/or modify it under the terms of the GNU
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * General Public License (GPL) as published by the Free Software
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * additional information or have any questions.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/*******************************************************************************
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync* Header Files *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync*******************************************************************************/
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#define LOG_GROUP LOG_GROUP_MM_HYPER
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#include <VBox/mm.h>
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#include "MMInternal.h"
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#include <VBox/vm.h>
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#include <VBox/log.h>
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#include <iprt/assert.h>
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/**
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Lookup a host context ring-3 address.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @returns Pointer to the corresponding lookup record.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @returns NULL on failure.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pVM The VM handle.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param R3Ptr The host context ring-3 address to lookup.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param poff Where to store the offset into the HMA memory chunk.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncDECLINLINE(PMMLOOKUPHYPER) mmHyperLookupR3(PVM pVM, RTR3PTR R3Ptr, uint32_t *poff)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync{
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /** @todo cache last lookup this stuff ain't cheap! */
PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.CTX_SUFF(pHyperHeap) + pVM->mm.s.offLookupHyper);
for (;;)
{
switch (pLookup->enmType)
{
case MMLOOKUPHYPERTYPE_LOCKED:
{
const uint32_t off = (RTR3UINTPTR)R3Ptr - (RTR3UINTPTR)pLookup->u.Locked.pvR3;
if (off < pLookup->cb)
{
*poff = off;
return pLookup;
}
break;
}
case MMLOOKUPHYPERTYPE_HCPHYS:
{
const uint32_t off = (RTR3UINTPTR)R3Ptr - (RTR3UINTPTR)pLookup->u.HCPhys.pvR3;
if (off < pLookup->cb)
{
*poff = off;
return pLookup;
}
break;
}
case MMLOOKUPHYPERTYPE_GCPHYS: /* (for now we'll not allow these kind of conversions) */
case MMLOOKUPHYPERTYPE_MMIO2:
case MMLOOKUPHYPERTYPE_DYNAMIC:
break;
default:
AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
break;
}
/* next */
if (pLookup->offNext == (int32_t)NIL_OFFSET)
break;
pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
}
AssertMsgFailed(("R3Ptr=%p is not inside the hypervisor memory area!\n", R3Ptr));
return NULL;
}
/**
* Lookup a host context ring-0 address.
*
* @returns Pointer to the corresponding lookup record.
* @returns NULL on failure.
* @param pVM The VM handle.
* @param R0Ptr The host context ring-0 address to lookup.
* @param poff Where to store the offset into the HMA memory chunk.
*/
DECLINLINE(PMMLOOKUPHYPER) mmHyperLookupR0(PVM pVM, RTR0PTR R0Ptr, uint32_t *poff)
{
AssertCompile(sizeof(RTR0PTR) == sizeof(RTR3PTR));
/*
* Translate Ring-0 VM addresses into Ring-3 VM addresses before feeding it to mmHyperLookupR3.
*/
/** @todo fix this properly; the ring 0 pVM address differs from the R3 one. (#1865) */
RTR0UINTPTR offVM = (RTR0UINTPTR)R0Ptr - (RTR0UINTPTR)pVM->pVMR0;
RTR3PTR R3Ptr = offVM < sizeof(*pVM)
? (RTR3PTR)((RTR3UINTPTR)pVM->pVMR3 + offVM)
: (RTR3PTR)R0Ptr;
return mmHyperLookupR3(pVM, R3Ptr, poff);
}
/**
* Lookup a raw-mode context address.
*
* @returns Pointer to the corresponding lookup record.
* @returns NULL on failure.
* @param pVM The VM handle.
* @param RCPtr The raw-mode context address to lookup.
* @param poff Where to store the offset into the HMA memory chunk.
*/
DECLINLINE(PMMLOOKUPHYPER) mmHyperLookupRC(PVM pVM, RTRCPTR RCPtr, uint32_t *poff)
{
/** @todo cache last lookup this stuff ain't cheap! */
unsigned offRC = (RTRCUINTPTR)RCPtr - (RTGCUINTPTR)pVM->mm.s.pvHyperAreaGC;
PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.CTX_SUFF(pHyperHeap) + pVM->mm.s.offLookupHyper);
for (;;)
{
const uint32_t off = offRC - pLookup->off;
if (off < pLookup->cb)
{
switch (pLookup->enmType)
{
case MMLOOKUPHYPERTYPE_LOCKED:
case MMLOOKUPHYPERTYPE_HCPHYS:
*poff = off;
return pLookup;
default:
break;
}
AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
return NULL;
}
/* next */
if (pLookup->offNext == (int32_t)NIL_OFFSET)
break;
pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
}
AssertMsgFailed(("GCPtr=%p is not inside the hypervisor memory area!\n", RCPtr));
return NULL;
}
/**
* Lookup a current context address.
*
* @returns Pointer to the corresponding lookup record.
* @returns NULL on failure.
* @param pVM The VM handle.
* @param pv The current context address to lookup.
* @param poff Where to store the offset into the HMA memory chunk.
*/
DECLINLINE(PMMLOOKUPHYPER) mmHyperLookupCC(PVM pVM, void *pv, uint32_t *poff)
{
#ifdef IN_GC
return mmHyperLookupRC(pVM, (RTRCPTR)pv, poff);
#elif defined(IN_RING0)
return mmHyperLookupR0(pVM, pv, poff);
#else
return mmHyperLookupR3(pVM, pv, poff);
#endif
}
/**
* Calculate the host context ring-3 address of an offset into the HMA memory chunk.
*
* @returns the host context ring-3 address.
* @param pLookup The HMA lookup record.
* @param off The offset into the HMA memory chunk.
*/
DECLINLINE(RTR3PTR) mmHyperLookupCalcR3(PMMLOOKUPHYPER pLookup, uint32_t off)
{
switch (pLookup->enmType)
{
case MMLOOKUPHYPERTYPE_LOCKED:
return (RTR3PTR)((RTR3UINTPTR)pLookup->u.Locked.pvR3 + off);
case MMLOOKUPHYPERTYPE_HCPHYS:
return (RTR3PTR)((RTR3UINTPTR)pLookup->u.HCPhys.pvR3 + off);
default:
AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
return NIL_RTR3PTR;
}
}
/**
* Calculate the host context ring-0 address of an offset into the HMA memory chunk.
*
* @returns the host context ring-0 address.
* @param pLookup The HMA lookup record.
* @param off The offset into the HMA memory chunk.
*/
DECLINLINE(RTR0PTR) mmHyperLookupCalcR0(PMMLOOKUPHYPER pLookup, uint32_t off)
{
switch (pLookup->enmType)
{
case MMLOOKUPHYPERTYPE_LOCKED:
if (pLookup->u.Locked.pvR0)
return (RTR0PTR)((RTR0UINTPTR)pLookup->u.Locked.pvR0 + off);
/** @todo #1865: accessing ring-3 memory (LOCKED)! */
return (RTR0PTR)((RTR3UINTPTR)pLookup->u.Locked.pvR3 + off);
case MMLOOKUPHYPERTYPE_HCPHYS:
/** @todo #1865: accessing ring-3 memory (HCPHYS)! */
return (RTR0PTR)((RTR3UINTPTR)pLookup->u.HCPhys.pvR3 + off);
default:
AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
return NIL_RTR0PTR;
}
}
/**
* Calculate the raw-mode context address of an offset into the HMA memory chunk.
*
* @returns the raw-mode context base address.
* @param pVM The the VM handle.
* @param pLookup The HMA lookup record.
* @param off The offset into the HMA memory chunk.
*/
DECLINLINE(RTRCPTR) mmHyperLookupCalcRC(PVM pVM, PMMLOOKUPHYPER pLookup, uint32_t off)
{
return (RTRCPTR)((RTGCUINTPTR)pVM->mm.s.pvHyperAreaGC + pLookup->off + off);
}
/**
* Calculate the guest context address of an offset into the HMA memory chunk.
*
* @returns the guest context base address.
* @param pVM The the VM handle.
* @param pLookup The HMA lookup record.
* @param off The offset into the HMA memory chunk.
*/
DECLINLINE(void *) mmHyperLookupCalcCC(PVM pVM, PMMLOOKUPHYPER pLookup, uint32_t off)
{
#ifdef IN_GC
return (void *)mmHyperLookupCalcRC(pVM, pLookup, off);
#elif defined(IN_RING0)
return mmHyperLookupCalcR0(pLookup, off);
#else
return mmHyperLookupCalcR3(pLookup, off);
#endif
}
/**
* Converts a ring-0 host context address in the Hypervisor memory region to a ring-3 host context address.
*
* @returns ring-3 host context address.
* @param pVM The VM to operate on.
* @param R0Ptr The ring-0 host context address.
* You'll be damned if this is not in the HMA! :-)
* @thread The Emulation Thread.
*/
MMDECL(RTR3PTR) MMHyperR0ToR3(PVM pVM, RTR0PTR R0Ptr)
{
uint32_t off;
PMMLOOKUPHYPER pLookup = mmHyperLookupR0(pVM, R0Ptr, &off);
if (pLookup)
return mmHyperLookupCalcR3(pLookup, off);
return NIL_RTR3PTR;
}
/**
* Converts a ring-0 host context address in the Hypervisor memory region to a raw-mode context address.
*
* @returns raw-mode context address.
* @param pVM The VM to operate on.
* @param R0Ptr The ring-0 host context address.
* You'll be damned if this is not in the HMA! :-)
* @thread The Emulation Thread.
*/
MMDECL(RTRCPTR) MMHyperR0ToRC(PVM pVM, RTR0PTR R0Ptr)
{
uint32_t off;
PMMLOOKUPHYPER pLookup = mmHyperLookupR0(pVM, R0Ptr, &off);
if (pLookup)
return mmHyperLookupCalcRC(pVM, pLookup, off);
return NIL_RTRCPTR;
}
#ifndef IN_RING0
/**
* Converts a ring-0 host context address in the Hypervisor memory region to a current context address.
*
* @returns current context address.
* @param pVM The VM to operate on.
* @param R0Ptr The ring-0 host context address.
* You'll be damned if this is not in the HMA! :-)
* @thread The Emulation Thread.
*/
MMDECL(void *) MMHyperR0ToCC(PVM pVM, RTR0PTR R0Ptr)
{
uint32_t off;
PMMLOOKUPHYPER pLookup = mmHyperLookupR0(pVM, R0Ptr, &off);
if (pLookup)
return mmHyperLookupCalcCC(pVM, pLookup, off);
return NULL;
}
#endif
/**
* Converts a ring-3 host context address in the Hypervisor memory region to a ring-0 host context address.
*
* @returns ring-0 host context address.
* @param pVM The VM to operate on.
* @param R3Ptr The ring-3 host context address.
* You'll be damned if this is not in the HMA! :-)
* @thread The Emulation Thread.
*/
MMDECL(RTR0PTR) MMHyperR3ToR0(PVM pVM, RTR3PTR R3Ptr)
{
uint32_t off;
PMMLOOKUPHYPER pLookup = mmHyperLookupR3(pVM, R3Ptr, &off);
if (pLookup)
return mmHyperLookupCalcR0(pLookup, off);
AssertMsgFailed(("R3Ptr=%p is not inside the hypervisor memory area!\n", R3Ptr));
return NIL_RTR0PTR;
}
/**
* Converts a ring-3 host context address in the Hypervisor memory region to a guest context address.
*
* @returns guest context address.
* @param pVM The VM to operate on.
* @param R3Ptr The ring-3 host context address.
* You'll be damned if this is not in the HMA! :-)
* @thread The Emulation Thread.
*/
MMDECL(RTRCPTR) MMHyperR3ToRC(PVM pVM, RTR3PTR R3Ptr)
{
uint32_t off;
PMMLOOKUPHYPER pLookup = mmHyperLookupR3(pVM, R3Ptr, &off);
if (pLookup)
return mmHyperLookupCalcRC(pVM, pLookup, off);
AssertMsgFailed(("R3Ptr=%p is not inside the hypervisor memory area!\n", R3Ptr));
return NIL_RTRCPTR;
}
/**
* Converts a ring-3 host context address in the Hypervisor memory region to a current context address.
*
* @returns current context address.
* @param pVM The VM to operate on.
* @param R3Ptr The ring-3 host context address.
* You'll be damned if this is not in the HMA! :-)
* @thread The Emulation Thread.
*/
#ifndef IN_RING3
MMDECL(void *) MMHyperR3ToCC(PVM pVM, RTR3PTR R3Ptr)
{
uint32_t off;
PMMLOOKUPHYPER pLookup = mmHyperLookupR3(pVM, R3Ptr, &off);
if (pLookup)
return mmHyperLookupCalcCC(pVM, pLookup, off);
return NULL;
}
#endif
/**
* Converts a raw-mode context address in the Hypervisor memory region to a ring-3 context address.
*
* @returns ring-3 host context address.
* @param pVM The VM to operate on.
* @param GCPtr The raw-mode context address.
* You'll be damned if this is not in the HMA! :-)
* @thread The Emulation Thread.
*/
MMDECL(RTR3PTR) MMHyperRCToR3(PVM pVM, RTRCPTR RCPtr)
{
uint32_t off;
PMMLOOKUPHYPER pLookup = mmHyperLookupRC(pVM, RCPtr, &off);
if (pLookup)
return mmHyperLookupCalcR3(pLookup, off);
return NIL_RTR3PTR;
}
/**
* Converts a raw-mode context address in the Hypervisor memory region to a ring-0 host context address.
*
* @returns ring-0 host context address.
* @param pVM The VM to operate on.
* @param RCPtr The raw-mode context address.
* You'll be damned if this is not in the HMA! :-)
* @thread The Emulation Thread.
*/
MMDECL(RTR0PTR) MMHyperRCToR0(PVM pVM, RTRCPTR RCPtr)
{
uint32_t off;
PMMLOOKUPHYPER pLookup = mmHyperLookupRC(pVM, RCPtr, &off);
if (pLookup)
return mmHyperLookupCalcR0(pLookup, off);
return NIL_RTR0PTR;
}
/**
* Converts a raw-mode context address in the Hypervisor memory region to a current context address.
*
* @returns current context address.
* @param pVM The VM to operate on.
* @param RCPtr The raw-mode host context address.
* You'll be damned if this is not in the HMA! :-)
* @thread The Emulation Thread.
*/
#ifndef IN_GC
MMDECL(void *) MMHyperRCToCC(PVM pVM, RTRCPTR RCPtr)
{
uint32_t off;
PMMLOOKUPHYPER pLookup = mmHyperLookupRC(pVM, RCPtr, &off);
if (pLookup)
return mmHyperLookupCalcCC(pVM, pLookup, off);
return NULL;
}
#endif
/**
* Converts a current context address in the Hypervisor memory region to a ring-3 host context address.
*
* @returns ring-3 host context address.
* @param pVM The VM to operate on.
* @param pv The current context address.
* You'll be damned if this is not in the HMA! :-)
* @thread The Emulation Thread.
*/
#ifndef IN_RING3
MMDECL(RTR3PTR) MMHyperCCToR3(PVM pVM, void *pv)
{
uint32_t off;
PMMLOOKUPHYPER pLookup = mmHyperLookupCC(pVM, pv, &off);
if (pLookup)
return mmHyperLookupCalcR3(pLookup, off);
return NIL_RTR3PTR;
}
#endif
/**
* Converts a current context address in the Hypervisor memory region to a ring-0 host context address.
*
* @returns ring-0 host context address.
* @param pVM The VM to operate on.
* @param pv The current context address.
* You'll be damned if this is not in the HMA! :-)
* @thread The Emulation Thread.
*/
#ifndef IN_RING0
MMDECL(RTR0PTR) MMHyperCCToR0(PVM pVM, void *pv)
{
uint32_t off;
PMMLOOKUPHYPER pLookup = mmHyperLookupCC(pVM, pv, &off);
if (pLookup)
return mmHyperLookupCalcR0(pLookup, off);
return NIL_RTR0PTR;
}
#endif
/**
* Converts a current context address in the Hypervisor memory region to a raw-mode context address.
*
* @returns guest context address.
* @param pVM The VM to operate on.
* @param pv The current context address.
* You'll be damned if this is not in the HMA! :-)
* @thread The Emulation Thread.
*/
#ifndef IN_GC
MMDECL(RTRCPTR) MMHyperCCToRC(PVM pVM, void *pv)
{
uint32_t off;
PMMLOOKUPHYPER pLookup = mmHyperLookupCC(pVM, pv, &off);
if (pLookup)
return mmHyperLookupCalcRC(pVM, pLookup, off);
return NIL_RTRCPTR;
}
#endif