VMReq.cpp revision 3dc55354143de06ead357136a00f175dada2e549
/* $Id$ */
/** @file
* VM - Virtual Machine
*/
/*
* 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_VM
#include "VMInternal.h"
#include <iprt/semaphore.h>
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/**
* Allocate and queue a call request.
*
* If it's desired to poll on the completion of the request set cMillies
* to 0 and use VMR3ReqWait() to check for completation. In the other case
* use RT_INDEFINITE_WAIT.
* The returned request packet must be freed using VMR3ReqFree().
*
* @returns VBox status code.
* Will not return VERR_INTERRUPTED.
* @returns VERR_TIMEOUT if cMillies was reached without the packet being completed.
*
* @param pVM The VM handle.
* @param enmDest Destination of the request packet (global or per VCPU).
* @param ppReq Where to store the pointer to the request.
* This will be NULL or a valid request pointer not matter what happends.
* @param cMillies Number of milliseconds to wait for the request to
* be completed. Use RT_INDEFINITE_WAIT to only
* wait till it's completed.
* @param pfnFunction Pointer to the function to call.
* @param cArgs Number of arguments following in the ellipsis.
* Not possible to pass 64-bit arguments!
* @param ... Function arguments.
*/
VMMR3DECL(int) VMR3ReqCall(PVM pVM, VMREQDEST enmDest, PVMREQ *ppReq, unsigned cMillies, PFNRT pfnFunction, unsigned cArgs, ...)
{
int rc = VMR3ReqCallVU(pVM->pUVM, enmDest, ppReq, cMillies, VMREQFLAGS_VBOX_STATUS, pfnFunction, cArgs, va);
return rc;
}
/**
* Allocate and queue a call request to a void function.
*
* If it's desired to poll on the completion of the request set cMillies
* to 0 and use VMR3ReqWait() to check for completation. In the other case
* use RT_INDEFINITE_WAIT.
* The returned request packet must be freed using VMR3ReqFree().
*
* @returns VBox status code.
* Will not return VERR_INTERRUPTED.
* @returns VERR_TIMEOUT if cMillies was reached without the packet being completed.
*
* @param pUVM Pointer to the user mode VM structure.
* @param enmDest Destination of the request packet (global or per VCPU).
* @param ppReq Where to store the pointer to the request.
* This will be NULL or a valid request pointer not matter what happends.
* @param cMillies Number of milliseconds to wait for the request to
* be completed. Use RT_INDEFINITE_WAIT to only
* wait till it's completed.
* @param pfnFunction Pointer to the function to call.
* @param cArgs Number of arguments following in the ellipsis.
* Not possible to pass 64-bit arguments!
* @param ... Function arguments.
*/
VMMR3DECL(int) VMR3ReqCallVoidU(PUVM pUVM, VMREQDEST enmDest, PVMREQ *ppReq, unsigned cMillies, PFNRT pfnFunction, unsigned cArgs, ...)
{
return rc;
}
/**
* Allocate and queue a call request to a void function.
*
* If it's desired to poll on the completion of the request set cMillies
* to 0 and use VMR3ReqWait() to check for completation. In the other case
* use RT_INDEFINITE_WAIT.
* The returned request packet must be freed using VMR3ReqFree().
*
* @returns VBox status code.
* Will not return VERR_INTERRUPTED.
* @returns VERR_TIMEOUT if cMillies was reached without the packet being completed.
*
* @param pVM The VM handle.
* @param enmDest Destination of the request packet (global or per VCPU).
* @param ppReq Where to store the pointer to the request.
* This will be NULL or a valid request pointer not matter what happends.
* @param cMillies Number of milliseconds to wait for the request to
* be completed. Use RT_INDEFINITE_WAIT to only
* wait till it's completed.
* @param pfnFunction Pointer to the function to call.
* @param cArgs Number of arguments following in the ellipsis.
* Not possible to pass 64-bit arguments!
* @param ... Function arguments.
*/
VMMR3DECL(int) VMR3ReqCallVoid(PVM pVM, VMREQDEST enmDest, PVMREQ *ppReq, unsigned cMillies, PFNRT pfnFunction, unsigned cArgs, ...)
{
int rc = VMR3ReqCallVU(pVM->pUVM, enmDest, ppReq, cMillies, VMREQFLAGS_VOID, pfnFunction, cArgs, va);
return rc;
}
/**
* Allocate and queue a call request to a void function.
*
* If it's desired to poll on the completion of the request set cMillies
* to 0 and use VMR3ReqWait() to check for completation. In the other case
* use RT_INDEFINITE_WAIT.
* The returned request packet must be freed using VMR3ReqFree().
*
* @returns VBox status code.
* Will not return VERR_INTERRUPTED.
* @returns VERR_TIMEOUT if cMillies was reached without the packet being completed.
*
* @param pVM The VM handle.
* @param enmDest Destination of the request packet (global or per VCPU).
* @param ppReq Where to store the pointer to the request.
* This will be NULL or a valid request pointer not matter what happends, unless fFlags
* contains VMREQFLAGS_NO_WAIT when it will be optional and always NULL.
* @param cMillies Number of milliseconds to wait for the request to
* be completed. Use RT_INDEFINITE_WAIT to only
* wait till it's completed.
* @param fFlags A combination of the VMREQFLAGS values.
* @param pfnFunction Pointer to the function to call.
* @param cArgs Number of arguments following in the ellipsis.
* Not possible to pass 64-bit arguments!
* @param ... Function arguments.
*/
VMMR3DECL(int) VMR3ReqCallEx(PVM pVM, VMREQDEST enmDest, PVMREQ *ppReq, unsigned cMillies, unsigned fFlags, PFNRT pfnFunction, unsigned cArgs, ...)
{
return rc;
}
/**
* Allocate and queue a call request to a void function.
*
* If it's desired to poll on the completion of the request set cMillies
* to 0 and use VMR3ReqWait() to check for completation. In the other case
* use RT_INDEFINITE_WAIT.
* The returned request packet must be freed using VMR3ReqFree().
*
* @returns VBox status code.
* Will not return VERR_INTERRUPTED.
* @returns VERR_TIMEOUT if cMillies was reached without the packet being completed.
*
* @param pUVM Pointer to the user mode VM structure.
* @param enmDest Destination of the request packet (global or per VCPU).
* @param ppReq Where to store the pointer to the request.
* This will be NULL or a valid request pointer not matter what happends, unless fFlags
* contains VMREQFLAGS_NO_WAIT when it will be optional and always NULL.
* @param cMillies Number of milliseconds to wait for the request to
* be completed. Use RT_INDEFINITE_WAIT to only
* wait till it's completed.
* @param fFlags A combination of the VMREQFLAGS values.
* @param pfnFunction Pointer to the function to call.
* @param cArgs Number of arguments following in the ellipsis.
* Not possible to pass 64-bit arguments!
* @param ... Function arguments.
*/
VMMR3DECL(int) VMR3ReqCallU(PUVM pUVM, VMREQDEST enmDest, PVMREQ *ppReq, unsigned cMillies, unsigned fFlags, PFNRT pfnFunction, unsigned cArgs, ...)
{
return rc;
}
/**
* Allocate and queue a call request.
*
* If it's desired to poll on the completion of the request set cMillies
* to 0 and use VMR3ReqWait() to check for completation. In the other case
* use RT_INDEFINITE_WAIT.
* The returned request packet must be freed using VMR3ReqFree().
*
* @returns VBox status code.
* Will not return VERR_INTERRUPTED.
* @returns VERR_TIMEOUT if cMillies was reached without the packet being completed.
*
* @param pUVM Pointer to the user mode VM structure.
* @param enmDest Destination of the request packet (global or per VCPU).
* @param ppReq Where to store the pointer to the request.
* This will be NULL or a valid request pointer not matter what happends, unless fFlags
* contains VMREQFLAGS_NO_WAIT when it will be optional and always NULL.
* @param cMillies Number of milliseconds to wait for the request to
* be completed. Use RT_INDEFINITE_WAIT to only
* wait till it's completed.
* @param pfnFunction Pointer to the function to call.
* @param fFlags A combination of the VMREQFLAGS values.
* @param cArgs Number of arguments following in the ellipsis.
* Stuff which differs in size from uintptr_t is gonna make trouble, so don't try!
* @param Args Argument vector.
*/
VMMR3DECL(int) VMR3ReqCallVU(PUVM pUVM, VMREQDEST enmDest, PVMREQ *ppReq, unsigned cMillies, unsigned fFlags, PFNRT pfnFunction, unsigned cArgs, va_list Args)
{
LogFlow(("VMR3ReqCallV: cMillies=%d fFlags=%#x pfnFunction=%p cArgs=%d\n", cMillies, fFlags, pfnFunction, cArgs));
/*
* Validate input.
*/
{
}
("cArg=%d\n", cArgs),
/*
* Allocate request
*/
if (RT_FAILURE(rc))
return rc;
/*
* Initialize the request data.
*/
/*
* Queue the request and return.
*/
if ( RT_FAILURE(rc)
&& rc != VERR_TIMEOUT)
{
}
if (!(fFlags & VMREQFLAGS_NO_WAIT))
{
}
else
return rc;
}
/**
* Joins the list pList with whatever is linked up at *pHead.
*/
{
for (unsigned cIterations = 0;; cIterations++)
{
if (!pHead)
return;
return;
return;
}
}
/**
* Joins the list pList with whatever is linked up at *pHead.
*/
{
/*
* Split the list if it's too long.
*/
unsigned cReqs = 1;
{
if (cReqs++ > 25)
{
vmr3ReqJoinFreeSub(&pVMInt->apReqFree[(i + 2 + (i == pVMInt->iReqFree)) % RT_ELEMENTS(pVMInt->apReqFree)], pTail->pNext);
return;
}
}
vmr3ReqJoinFreeSub(&pVMInt->apReqFree[(pVMInt->iReqFree + 2) % RT_ELEMENTS(pVMInt->apReqFree)], pList);
}
/**
* Allocates a request packet.
*
* The caller allocates a request packet, fills in the request data
* union and queues the request.
*
* @returns VBox status code.
*
* @param pVM VM handle.
* @param ppReq Where to store the pointer to the allocated packet.
* @param enmType Package type.
* @param enmDest Destination of the request packet (global or per VCPU).
*/
{
}
/**
* Allocates a request packet.
*
* The caller allocates a request packet, fills in the request data
* union and queues the request.
*
* @returns VBox status code.
*
* @param pUVM Pointer to the user mode VM structure.
* @param ppReq Where to store the pointer to the allocated packet.
* @param enmType Package type.
* @param enmDest Destination of the request packet (global or per VCPU).
*/
{
/*
* Validate input.
*/
("Invalid package type %d valid range %d-%d inclusivly.\n",
AssertMsgReturn(enmDest == VMREQDEST_ANY || enmDest == VMREQDEST_BROADCAST || (unsigned)enmDest < pUVM->pVM->cCPUs, ("Invalid destination %d (max=%d)\n", enmDest, pUVM->pVM->cCPUs), VERR_INVALID_PARAMETER);
/*
* Try get a recycled packet.
* While this could all be solved with a single list with a lock, it's a sport
* of mine to avoid locks.
*/
while (--cTries >= 0)
{
PVMREQ volatile *ppHead = &pUVM->vm.s.apReqFree[ASMAtomicIncU32(&pUVM->vm.s.iReqFree) % RT_ELEMENTS(pUVM->vm.s.apReqFree)];
#if 0 /* sad, but this won't work safely because the reading of pReq->pNext. */
if ( pReq
if (pReq)
{
#else
if (pReq)
{
if ( pNext
{
}
#endif
/*
* Make sure the event sem is not signaled.
*/
if (!pReq->fEventSemClear)
{
{
/*
* This shall not happen, but if it does we'll just destroy
* the semaphore and create a new one.
*/
if (RT_FAILURE(rc))
return rc;
}
pReq->fEventSemClear = true;
}
else
/*
* Initialize the packet and return it.
*/
return VINF_SUCCESS;
}
}
/*
* Ok allocate one.
*/
if (!pReq)
return VERR_NO_MEMORY;
/*
* Create the semaphore.
*/
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Initialize the packet and return it.
*/
pReq->fEventSemClear = true;
return VINF_SUCCESS;
}
/**
* Free a request packet.
*
* @returns VBox status code.
*
* @param pReq Package to free.
* @remark The request packet must be in allocated or completed state!
*/
{
/*
* Ignore NULL (all free functions should do this imho).
*/
if (!pReq)
return VINF_SUCCESS;
/*
* Check packet state.
*/
{
case VMREQSTATE_ALLOCATED:
case VMREQSTATE_COMPLETED:
break;
default:
return VERR_VM_REQUEST_STATE;
}
/*
* Make it a free packet and put it into one of the free packet lists.
*/
{
PVMREQ volatile *ppHead = &pUVM->vm.s.apReqFree[ASMAtomicIncU32(&pUVM->vm.s.iReqFree) % RT_ELEMENTS(pUVM->vm.s.apReqFree)];
do
{
}
else
{
}
return VINF_SUCCESS;
}
/**
* Queue a request.
*
* The quest must be allocated using VMR3ReqAlloc() and contain
* all the required data.
* If it's desired to poll on the completion of the request set cMillies
* to 0 and use VMR3ReqWait() to check for completation. In the other case
* use RT_INDEFINITE_WAIT.
*
* @returns VBox status code.
* Will not return VERR_INTERRUPTED.
* @returns VERR_TIMEOUT if cMillies was reached without the packet being completed.
*
* @param pReq The request to queue.
* @param cMillies Number of milliseconds to wait for the request to
* be completed. Use RT_INDEFINITE_WAIT to only
* wait till it's completed.
*/
{
/*
* Verify the supplied package.
*/
AssertMsgReturn(pReq->enmState == VMREQSTATE_ALLOCATED, ("%d\n", pReq->enmState), VERR_VM_REQUEST_STATE);
("Invalid request package! Anyone cooking their own packages???\n"),
("Invalid package type %d valid range %d-%d inclusivly. This was verified on alloc too...\n",
/*
* Are we the EMT or not?
* Also, store pVM (and fFlags) locally since pReq may be invalid after queuing it.
*/
int rc = VINF_SUCCESS;
{
{
if ( !pUVMCPU
{
/*
* Insert it.
*/
do
{
} while (!ASMAtomicCmpXchgPtr((void * volatile *)&pUVM->aCpus[i].vm.s.pReqs, (void *)pReq, (void *)pNext));
/*
* Notify EMT.
*/
/* @todo: VMR3NotifyFFU*/
AssertFailed();
VMR3NotifyFFU(pUVM, false);
/*
* Wait and return.
*/
if (!(fFlags & VMREQFLAGS_NO_WAIT))
}
else
{
/*
* The requester was EMT, just execute it.
*/
}
} /* for each VMCPU */
}
else
{
/*
* Insert it.
*/
do
{
} while (!ASMAtomicCmpXchgPtr((void * volatile *)&pUVM->aCpus[idTarget].vm.s.pReqs, (void *)pReq, (void *)pNext));
/*
* Notify EMT.
*/
/* @todo: VMR3NotifyFFU*/
AssertFailed();
VMR3NotifyFFU(pUVM, false);
/*
* Wait and return.
*/
if (!(fFlags & VMREQFLAGS_NO_WAIT))
}
&& !pUVMCPU /* only EMT threads have a valid pointer stored in the TLS slot. */)
{
/*
* Insert it.
*/
do
{
/*
* Notify EMT.
*/
VMR3NotifyFFU(pUVM, false);
/*
* Wait and return.
*/
if (!(fFlags & VMREQFLAGS_NO_WAIT))
}
else
{
/*
* The requester was EMT, just execute it.
*/
}
return rc;
}
/**
* Wait for a request to be completed.
*
* @returns VBox status code.
* @returns VERR_TIMEOUT if cMillies was reached without the packet being completed.
*
* @param pReq The request to wait for.
* @param cMillies Number of milliseconds to wait.
* Use RT_INDEFINITE_WAIT to only wait till it's completed.
*/
{
/*
* Verify the supplied package.
*/
("Invalid request package! Anyone cooking their own packages???\n"),
("Invalid package type %d valid range %d-%d inclusivly. This was verified on alloc too...\n",
/*
* Check for deadlock condition
*/
("Waiting for EMT to process a request, but we own the global VM lock!?!?!?!\n"));
/*
* Wait on the package.
*/
int rc;
if (cMillies != RT_INDEFINITE_WAIT)
else
{
do
{
}
if (RT_SUCCESS(rc))
rc = VINF_SUCCESS;
return rc;
}
/**
* Process pending request(s).
*
* This function is called from a forced action handler in the EMT
* or from one of the EMT loops.
*
* @returns VBox status code.
*
* @param pUVM Pointer to the user mode VM structure.
* @param enmDest Destination of the request packet (global or per VCPU).
*/
{
LogFlow(("VMR3ReqProcessU: (enmVMState=%d) enmDest=%d\n", pUVM->pVM ? pUVM->pVM->enmVMState : VMSTATE_CREATING, enmDest));
/*
* Process loop.
*
* We do not repeat the outer loop if we've got an informationtional status code
* since that code needs processing by our caller.
*/
int rc = VINF_SUCCESS;
while (rc <= VINF_SUCCESS)
{
/*
* Get pending requests.
*/
void *volatile *ppReqs;
if (enmDest == VMREQDEST_ANY)
{
}
else
{
}
if (!pReqs)
break;
/*
* Reverse the list to process it in FIFO order.
*/
while (pReq)
{
}
/*
* Process the requests.
*
* Since this is a FF worker certain rules applies to the
*/
while (pReqs)
{
/* Unchain the first request and advance the list. */
/* Process the request */
/*
* The status code handling extremely important yet very fragile. Should probably
* look for a better way of communicating status changes to EM...
*/
if ( rc2 >= VINF_EM_FIRST
&& rc2 <= VINF_EM_LAST
&& ( rc == VINF_SUCCESS
}
}
LogFlow(("VMR3ReqProcess: returns %Rrc (enmVMState=%d)\n", rc, pUVM->pVM ? pUVM->pVM->enmVMState : VMSTATE_CREATING));
return rc;
}
/**
* Process one request.
*
* @returns VBox status code.
*
* @param pVM VM handle.
* @param pReq Request packet to process.
*/
{
/*
* Process the request.
*/
{
/*
* A packed down call frame.
*/
case VMREQTYPE_INTERNAL:
{
union
{
DECLCALLBACKMEMBER(int, pfn00)(void);
DECLCALLBACKMEMBER(int, pfn07)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
DECLCALLBACKMEMBER(int, pfn08)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
DECLCALLBACKMEMBER(int, pfn09)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
DECLCALLBACKMEMBER(int, pfn10)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
DECLCALLBACKMEMBER(int, pfn11)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
DECLCALLBACKMEMBER(int, pfn12)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
} u;
#ifdef RT_ARCH_AMD64
{
case 6: rcRet = u.pfn06(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5]); break;
case 7: rcRet = u.pfn07(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5], pauArgs[6]); break;
case 8: rcRet = u.pfn08(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5], pauArgs[6], pauArgs[7]); break;
case 9: rcRet = u.pfn09(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5], pauArgs[6], pauArgs[7], pauArgs[8]); break;
case 10: rcRet = u.pfn10(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5], pauArgs[6], pauArgs[7], pauArgs[8], pauArgs[9]); break;
case 11: rcRet = u.pfn11(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5], pauArgs[6], pauArgs[7], pauArgs[8], pauArgs[9], pauArgs[10]); break;
case 12: rcRet = u.pfn12(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5], pauArgs[6], pauArgs[7], pauArgs[8], pauArgs[9], pauArgs[10], pauArgs[11]); break;
default:
break;
}
#else /* x86: */
# ifdef __GNUC__
"subl %2, %%esp\n\t"
"andl $0xfffffff0, %%esp\n\t"
"shrl $2, %2\n\t"
"movl %%esp, %%edi\n\t"
"rep movsl\n\t"
"movl %%edx, %%edi\n\t"
"call *%%eax\n\t"
"mov %%edi, %%esp\n\t"
: "=a" (rcRet),
"=S" (pauArgs),
"=c" (cbArgs)
: "0" (u.pfn),
"1" (pauArgs),
"2" (cbArgs)
: "edi", "edx");
# else
{
}
# endif
#endif /* x86 */
break;
}
default:
break;
}
/*
* Complete the request.
*/
{
/* Free the packet, nobody is waiting. */
LogFlow(("vmR3ReqProcessOne: Completed request %p: rcReq=%Rrc rcRet=%Rrc - freeing it\n",
}
else
{
/* Notify the waiter and him free up the packet. */
LogFlow(("vmR3ReqProcessOne: Completed request %p: rcReq=%Rrc rcRet=%Rrc - notifying waiting thread\n",
if (RT_FAILURE(rc2))
{
}
}
return rcRet;
}