GVMMR0.cpp revision 5c318078b230f9867de2e1cc64a8407411a89f83
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/* $Id$ */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/** @file
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * GVMM - Global VM Manager.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/*
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * Copyright (C) 2007 Sun Microsystems, Inc.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * available from http://www.virtualbox.org. This file is free software;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * you can redistribute it and/or modify it under the terms of the GNU
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * General Public License (GPL) as published by the Free Software
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * additional information or have any questions.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/** @page pg_GVMM GVMM - The Global VM Manager
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * The Global VM Manager lives in ring-0. It's main function at the moment
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * is to manage a list of all running VMs, keep a ring-0 only structure (GVM)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * for each of them, and assign them unique identifiers (so GMM can track
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * page owners). The idea for the future is to add an idle priority kernel
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * thread that can take care of tasks like page sharing.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * The GVMM will create a ring-0 object for each VM when it's registered,
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * this is both for session cleanup purposes and for having a point where
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * it's possible to implement usage polices later (in SUPR0ObjRegister).
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/*******************************************************************************
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync* Header Files *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync*******************************************************************************/
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#define LOG_GROUP LOG_GROUP_GVMM
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include <VBox/gvmm.h>
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync#include "GVMMR0Internal.h"
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync#include <VBox/gvm.h>
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync#include <VBox/vm.h>
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync#include <VBox/err.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include <iprt/alloc.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include <iprt/semaphore.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include <iprt/time.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include <VBox/log.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include <iprt/thread.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include <iprt/param.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include <iprt/string.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include <iprt/assert.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include <iprt/mem.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include <iprt/memobj.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/*******************************************************************************
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync* Structures and Typedefs *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync*******************************************************************************/
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/**
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Global VM handle.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsynctypedef struct GVMHANDLE
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** The index of the next handle in the list (free or used). (0 is nil.) */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync uint16_t volatile iNext;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Our own index / handle value. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync uint16_t iSelf;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** The pointer to the ring-0 only (aka global) VM structure. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync PGVM pGVM;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** The ring-0 mapping of the shared VM instance data. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PVM pVM;
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync /** The virtual machine object. */
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync void *pvObj;
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync /** The session this VM is associated with. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PSUPDRVSESSION pSession;
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync /** The ring-0 handle of the EMT thread.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * This is used for assertions and similar cases where we need to find the VM handle. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync RTNATIVETHREAD hEMT;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync} GVMHANDLE;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync/** Pointer to a global VM handle. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsynctypedef GVMHANDLE *PGVMHANDLE;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync/**
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * The GVMM instance data.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsynctypedef struct GVMM
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Eyecatcher / magic. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync uint32_t u32Magic;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync /** The index of the head of the free handle chain. (0 is nil.) */
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync uint16_t volatile iFreeHead;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync /** The index of the head of the active handle chain. (0 is nil.) */
0c4004948fca34f2db87e7b38013137e9472c306vboxsync uint16_t volatile iUsedHead;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync /** The number of VMs. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync uint16_t volatile cVMs;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync// /** The number of halted EMT threads. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync// uint16_t volatile cHaltedEMTs;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** The lock used to serialize VM creation, destruction and associated events that
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync * isn't performance critical. Owners may acquire the list lock. */
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync RTSEMFASTMUTEX CreateDestroyLock;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** The lock used to serialize used list updates and accesses.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * This indirectly includes scheduling since the scheduler will have to walk the
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * used list to examin running VMs. Owners may not acquire any other locks. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync RTSEMFASTMUTEX UsedLock;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /** The handle array.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * The size of this array defines the maximum number of currently running VMs.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * The first entry is unused as it represents the NIL handle. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync GVMHANDLE aHandles[128];
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /** @gcfgm{/GVMM/cVMsMeansCompany, 32-bit, 0, UINT32_MAX, 1}
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * The number of VMs that means we no longer consider ourselves alone on a CPU/Core.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync uint32_t cVMsMeansCompany;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** @gcfgm{/GVMM/MinSleepAlone,32-bit, 0, 100000000, 750000, ns}
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * The minimum sleep time for when we're alone, in nano seconds.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync uint32_t nsMinSleepAlone;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /** @gcfgm{/GVMM/MinSleepCompany,32-bit,0, 100000000, 15000, ns}
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * The minimum sleep time for when we've got company, in nano seconds.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync uint32_t nsMinSleepCompany;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /** @gcfgm{/GVMM/EarlyWakeUp1, 32-bit, 0, 100000000, 25000, ns}
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * The limit for the first round of early wakeups, given in nano seconds.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync */
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync uint32_t nsEarlyWakeUp1;
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync /** @gcfgm{/GVMM/EarlyWakeUp2, 32-bit, 0, 100000000, 50000, ns}
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync * The limit for the second round of early wakeups, given in nano seconds.
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync */
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync uint32_t nsEarlyWakeUp2;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync} GVMM;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync/** Pointer to the GVMM instance data. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsynctypedef GVMM *PGVMM;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/** The GVMM::u32Magic value (Charlie Haden). */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync#define GVMM_MAGIC 0x19370806
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync/*******************************************************************************
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync* Global Variables *
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync*******************************************************************************/
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync/** Pointer to the GVMM instance data.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * (Just my general dislike for global variables.) */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsyncstatic PGVMM g_pGVMM = NULL;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync/** Macro for obtaining and validating the g_pGVMM pointer.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * On failure it will return from the invoking function with the specified return value.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync *
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param pGVMM The name of the pGVMM variable.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param rc The return value on failure. Use VERR_INTERNAL_ERROR for
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * VBox status codes.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync#define GVMM_GET_VALID_INSTANCE(pGVMM, rc) \
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync do { \
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync (pGVMM) = g_pGVMM;\
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertPtrReturn((pGVMM), (rc)); \
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertMsgReturn((pGVMM)->u32Magic == GVMM_MAGIC, ("%p - %#x\n", (pGVMM), (pGVMM)->u32Magic), (rc)); \
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync } while (0)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync/** Macro for obtaining and validating the g_pGVMM pointer, void function variant.
93f91841f87620d1cb6d0238b3d0d5e52cd3b9a4vboxsync * On failure it will return from the invoking function.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync *
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param pGVMM The name of the pGVMM variable.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync#define GVMM_GET_VALID_INSTANCE_VOID(pGVMM) \
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync do { \
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync (pGVMM) = g_pGVMM;\
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertPtrReturnVoid((pGVMM)); \
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertMsgReturnVoid((pGVMM)->u32Magic == GVMM_MAGIC, ("%p - %#x\n", (pGVMM), (pGVMM)->u32Magic)); \
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync } while (0)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync/*******************************************************************************
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync* Internal Functions *
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync*******************************************************************************/
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsyncstatic void gvmmR0InitPerVMData(PGVM pGVM);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic DECLCALLBACK(void) gvmmR0HandleObjDestructor(void *pvObj, void *pvGVMM, void *pvHandle);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsyncstatic int gvmmR0ByVM(PVM pVM, PGVM *ppGVM, PGVMM *ppGVMM, bool fTakeUsedLock);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic int gvmmR0ByVMAndEMT(PVM pVM, PGVM *ppGVM, PGVMM *ppGVMM);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/**
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * Initializes the GVMM.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync *
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * This is called while owninng the loader sempahore (see supdrvIOCtl_LdrLoad()).
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync *
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @returns VBox status code.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncGVMMR0DECL(int) GVMMR0Init(void)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync{
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync LogFlow(("GVMMR0Init:\n"));
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /*
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * Allocate and initialize the instance data.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync */
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync PGVMM pGVMM = (PGVMM)RTMemAllocZ(sizeof(*pGVMM));
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync if (!pGVMM)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return VERR_NO_MEMORY;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync int rc = RTSemFastMutexCreate(&pGVMM->CreateDestroyLock);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (RT_SUCCESS(rc))
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = RTSemFastMutexCreate(&pGVMM->UsedLock);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (RT_SUCCESS(rc))
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync pGVMM->u32Magic = GVMM_MAGIC;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pGVMM->iUsedHead = 0;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pGVMM->iFreeHead = 1;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* the nil handle */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pGVMM->aHandles[0].iSelf = 0;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pGVMM->aHandles[0].iNext = 0;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* the tail */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync unsigned i = RT_ELEMENTS(pGVMM->aHandles) - 1;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pGVMM->aHandles[i].iSelf = i;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pGVMM->aHandles[i].iNext = 0; /* nil */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync /* the rest */
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync while (i-- > 1)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pGVMM->aHandles[i].iSelf = i;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pGVMM->aHandles[i].iNext = i + 1;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync /* The default configuration values. */
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync pGVMM->cVMsMeansCompany = 1; /** @todo should be adjusted to relative to the cpu count or something... */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pGVMM->nsMinSleepAlone = 750000 /* ns (0.750 ms) */; /** @todo this should be adjusted to be 75% (or something) of the scheduler granularity... */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pGVMM->nsMinSleepCompany = 15000 /* ns (0.015 ms) */;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pGVMM->nsEarlyWakeUp1 = 25000 /* ns (0.025 ms) */;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pGVMM->nsEarlyWakeUp2 = 50000 /* ns (0.050 ms) */;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync g_pGVMM = pGVMM;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync LogFlow(("GVMMR0Init: pGVMM=%p\n", pGVMM));
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync return VINF_SUCCESS;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync RTSemFastMutexDestroy(pGVMM->CreateDestroyLock);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync RTMemFree(pGVMM);
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync return rc;
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync}
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync/**
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Terminates the GVM.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync *
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * This is called while owning the loader semaphore (see supdrvLdrFree()).
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * And unless something is wrong, there should be absolutely no VMs
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * registered at this point.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncGVMMR0DECL(void) GVMMR0Term(void)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync{
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync LogFlow(("GVMMR0Term:\n"));
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync PGVMM pGVMM = g_pGVMM;
d5ea45cc92d7f1d3ade8189944531f665bfe8ed5vboxsync g_pGVMM = NULL;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (RT_UNLIKELY(!VALID_PTR(pGVMM)))
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync SUPR0Printf("GVMMR0Term: pGVMM=%p\n", pGVMM);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pGVMM->u32Magic++;
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync RTSemFastMutexDestroy(pGVMM->UsedLock);
d5ea45cc92d7f1d3ade8189944531f665bfe8ed5vboxsync pGVMM->UsedLock = NIL_RTSEMFASTMUTEX;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync RTSemFastMutexDestroy(pGVMM->CreateDestroyLock);
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync pGVMM->CreateDestroyLock = NIL_RTSEMFASTMUTEX;
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync pGVMM->iFreeHead = 0;
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync if (pGVMM->iUsedHead)
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync {
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync SUPR0Printf("GVMMR0Term: iUsedHead=%#x! (cVMs=%#x)\n", pGVMM->iUsedHead, pGVMM->cVMs);
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync pGVMM->iUsedHead = 0;
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync }
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync RTMemFree(pGVMM);
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync}
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync/**
d5ea45cc92d7f1d3ade8189944531f665bfe8ed5vboxsync * A quick hack for setting global config values.
d5ea45cc92d7f1d3ade8189944531f665bfe8ed5vboxsync *
d5ea45cc92d7f1d3ade8189944531f665bfe8ed5vboxsync * @returns VBox status code.
d5ea45cc92d7f1d3ade8189944531f665bfe8ed5vboxsync *
d5ea45cc92d7f1d3ade8189944531f665bfe8ed5vboxsync * @param pSession The session handle. Used for authentication.
d5ea45cc92d7f1d3ade8189944531f665bfe8ed5vboxsync * @param pszName The variable name.
d5ea45cc92d7f1d3ade8189944531f665bfe8ed5vboxsync * @param u64Value The new value.
d5ea45cc92d7f1d3ade8189944531f665bfe8ed5vboxsync */
d5ea45cc92d7f1d3ade8189944531f665bfe8ed5vboxsyncGVMMR0DECL(int) GVMMR0SetConfig(PSUPDRVSESSION pSession, const char *pszName, uint64_t u64Value)
d5ea45cc92d7f1d3ade8189944531f665bfe8ed5vboxsync{
d5ea45cc92d7f1d3ade8189944531f665bfe8ed5vboxsync /*
d5ea45cc92d7f1d3ade8189944531f665bfe8ed5vboxsync * Validate input.
d5ea45cc92d7f1d3ade8189944531f665bfe8ed5vboxsync */
d5ea45cc92d7f1d3ade8189944531f665bfe8ed5vboxsync PGVMM pGVMM;
d5ea45cc92d7f1d3ade8189944531f665bfe8ed5vboxsync GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertPtrReturn(pSession, VERR_INVALID_HANDLE);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertPtrReturn(pszName, VERR_INVALID_POINTER);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /*
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * String switch time!
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (strncmp(pszName, "/GVMM/", sizeof("/GVMM/") - 1))
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return VERR_CFGM_VALUE_NOT_FOUND; /* borrow status codes from CFGM... */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync int rc = VINF_SUCCESS;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pszName += sizeof("/GVMM/") - 1;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (!strcmp(pszName, "cVMsMeansCompany"))
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (u64Value <= UINT32_MAX)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pGVMM->cVMsMeansCompany = u64Value;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync else
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = VERR_OUT_OF_RANGE;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync else if (!strcmp(pszName, "MinSleepAlone"))
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (u64Value <= 100000000)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pGVMM->nsMinSleepAlone = u64Value;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync else
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VERR_OUT_OF_RANGE;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync else if (!strcmp(pszName, "MinSleepCompany"))
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync if (u64Value <= 100000000)
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync pGVMM->nsMinSleepCompany = u64Value;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync else
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync rc = VERR_OUT_OF_RANGE;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync }
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync else if (!strcmp(pszName, "EarlyWakeUp1"))
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync {
0c4004948fca34f2db87e7b38013137e9472c306vboxsync if (u64Value <= 100000000)
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync pGVMM->nsEarlyWakeUp1 = u64Value;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync else
0c4004948fca34f2db87e7b38013137e9472c306vboxsync rc = VERR_OUT_OF_RANGE;
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync }
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync else if (!strcmp(pszName, "EarlyWakeUp2"))
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (u64Value <= 100000000)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pGVMM->nsEarlyWakeUp2 = u64Value;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync else
0c4004948fca34f2db87e7b38013137e9472c306vboxsync rc = VERR_OUT_OF_RANGE;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync else
0c4004948fca34f2db87e7b38013137e9472c306vboxsync rc = VERR_CFGM_VALUE_NOT_FOUND;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return rc;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync}
0c4004948fca34f2db87e7b38013137e9472c306vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/**
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * A quick hack for getting global config values.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @returns VBox status code.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param pSession The session handle. Used for authentication.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * @param pszName The variable name.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param u64Value The new value.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncGVMMR0DECL(int) GVMMR0QueryConfig(PSUPDRVSESSION pSession, const char *pszName, uint64_t *pu64Value)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /*
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Validate input.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PGVMM pGVMM;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertPtrReturn(pSession, VERR_INVALID_HANDLE);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertPtrReturn(pszName, VERR_INVALID_POINTER);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertPtrReturn(pu64Value, VERR_INVALID_POINTER);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /*
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * String switch time!
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (strncmp(pszName, "/GVMM/", sizeof("/GVMM/") - 1))
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return VERR_CFGM_VALUE_NOT_FOUND; /* borrow status codes from CFGM... */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = VINF_SUCCESS;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pszName += sizeof("/GVMM/") - 1;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (!strcmp(pszName, "cVMsMeansCompany"))
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync *pu64Value = pGVMM->cVMsMeansCompany;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync else if (!strcmp(pszName, "MinSleepAlone"))
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync *pu64Value = pGVMM->nsMinSleepAlone;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync else if (!strcmp(pszName, "MinSleepCompany"))
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync *pu64Value = pGVMM->nsMinSleepCompany;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync else if (!strcmp(pszName, "EarlyWakeUp1"))
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync *pu64Value = pGVMM->nsEarlyWakeUp1;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync else if (!strcmp(pszName, "EarlyWakeUp2"))
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync *pu64Value = pGVMM->nsEarlyWakeUp2;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync else
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync rc = VERR_CFGM_VALUE_NOT_FOUND;
8a339f91959bb7a3315b51a23461b68c7b0cb50evboxsync return rc;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync}
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync/**
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * Try acquire the 'used' lock.
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync *
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * @returns IPRT status code, see RTSemFastMutexRequest.
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * @param pGVMM The GVMM instance data.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncDECLINLINE(int) gvmmR0UsedLock(PGVMM pGVMM)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync LogFlow(("++gvmmR0UsedLock(%p)\n", pGVMM));
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync int rc = RTSemFastMutexRequest(pGVMM->UsedLock);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync LogFlow(("gvmmR0UsedLock(%p)->%Rrc\n", pGVMM, rc));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return rc;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync/**
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * Release the 'used' lock.
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync *
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * @returns IPRT status code, see RTSemFastMutexRelease.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * @param pGVMM The GVMM instance data.
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync */
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsyncDECLINLINE(int) gvmmR0UsedUnlock(PGVMM pGVMM)
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync{
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync LogFlow(("--gvmmR0UsedUnlock(%p)\n", pGVMM));
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync int rc = RTSemFastMutexRelease(pGVMM->UsedLock);
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync AssertRC(rc);
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync return rc;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/**
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Try acquire the 'create & destroy' lock.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @returns IPRT status code, see RTSemFastMutexRequest.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param pGVMM The GVMM instance data.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncDECLINLINE(int) gvmmR0CreateDestroyLock(PGVMM pGVMM)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync LogFlow(("++gvmmR0CreateDestroyLock(%p)\n", pGVMM));
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync int rc = RTSemFastMutexRequest(pGVMM->CreateDestroyLock);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync LogFlow(("gvmmR0CreateDestroyLock(%p)->%Rrc\n", pGVMM, rc));
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return rc;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync}
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync/**
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * Release the 'create & destroy' lock.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync *
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @returns IPRT status code, see RTSemFastMutexRequest.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param pGVMM The GVMM instance data.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsyncDECLINLINE(int) gvmmR0CreateDestroyUnlock(PGVMM pGVMM)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync LogFlow(("--gvmmR0CreateDestroyUnlock(%p)\n", pGVMM));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertRC(rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return rc;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync/**
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * Request wrapper for the GVMMR0CreateVM API.
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync *
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * @returns VBox status code.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param pReq The request buffer.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsyncGVMMR0DECL(int) GVMMR0CreateVMReq(PGVMMCREATEVMREQ pReq)
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync{
a39ea3668b7019c23a68936259545f9b71bce1aavboxsync /*
0db6a029780d9f9b347500e117320a8d5661efe5vboxsync * Validate the request.
da3503c04ce76e653401396fe2795a9bc2427a1dvboxsync */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (!VALID_PTR(pReq))
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync return VERR_INVALID_POINTER;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (pReq->Hdr.cbReq != sizeof(*pReq))
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return VERR_INVALID_PARAMETER;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync if (!VALID_PTR(pReq->pSession))
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync return VERR_INVALID_POINTER;
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync
0c4004948fca34f2db87e7b38013137e9472c306vboxsync /*
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * Execute it.
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync */
0c4004948fca34f2db87e7b38013137e9472c306vboxsync PVM pVM;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync pReq->pVMR0 = NULL;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync pReq->pVMR3 = NIL_RTR3PTR;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync int rc = GVMMR0CreateVM(pReq->pSession, &pVM);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync if (RT_SUCCESS(rc))
0c4004948fca34f2db87e7b38013137e9472c306vboxsync {
0c4004948fca34f2db87e7b38013137e9472c306vboxsync pReq->pVMR0 = pVM;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync pReq->pVMR3 = pVM->pVMR3;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync }
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync return rc;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync}
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync
0c4004948fca34f2db87e7b38013137e9472c306vboxsync
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync/**
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * Allocates the VM structure and registers it with GVM.
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync *
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * The caller will become the VM owner and there by the EMT.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync *
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * @returns VBox status code.
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * @param pSession The support driver session.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * @param ppVM Where to store the pointer to the VM structure.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync *
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * @thread EMT.
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync */
0c4004948fca34f2db87e7b38013137e9472c306vboxsyncGVMMR0DECL(int) GVMMR0CreateVM(PSUPDRVSESSION pSession, PVM *ppVM)
0c4004948fca34f2db87e7b38013137e9472c306vboxsync{
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync LogFlow(("GVMMR0CreateVM: pSession=%p\n", pSession));
0c4004948fca34f2db87e7b38013137e9472c306vboxsync PGVMM pGVMM;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync
0c4004948fca34f2db87e7b38013137e9472c306vboxsync AssertPtrReturn(ppVM, VERR_INVALID_POINTER);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync *ppVM = NULL;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync
0c4004948fca34f2db87e7b38013137e9472c306vboxsync RTNATIVETHREAD hEMT = RTThreadNativeSelf();
0c4004948fca34f2db87e7b38013137e9472c306vboxsync AssertReturn(hEMT != NIL_RTNATIVETHREAD, VERR_INTERNAL_ERROR);
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync
0c4004948fca34f2db87e7b38013137e9472c306vboxsync /*
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * The whole allocation process is protected by the lock.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync */
0c4004948fca34f2db87e7b38013137e9472c306vboxsync int rc = gvmmR0CreateDestroyLock(pGVMM);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync AssertRCReturn(rc, rc);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync
0c4004948fca34f2db87e7b38013137e9472c306vboxsync /*
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * Allocate a handle first so we don't waste resources unnecessarily.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync */
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync uint16_t iHandle = pGVMM->iFreeHead;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync if (iHandle)
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync {
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync PGVMHANDLE pHandle = &pGVMM->aHandles[iHandle];
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync /* consistency checks, a bit paranoid as always. */
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync if ( !pHandle->pVM
0c4004948fca34f2db87e7b38013137e9472c306vboxsync && !pHandle->pGVM
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync && !pHandle->pvObj
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync && pHandle->iSelf == iHandle)
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync {
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync pHandle->pvObj = SUPR0ObjRegister(pSession, SUPDRVOBJTYPE_VM, gvmmR0HandleObjDestructor, pGVMM, pHandle);
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync if (pHandle->pvObj)
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync {
0c4004948fca34f2db87e7b38013137e9472c306vboxsync /*
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * Move the handle from the free to used list and perform permission checks.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync */
0c4004948fca34f2db87e7b38013137e9472c306vboxsync rc = gvmmR0UsedLock(pGVMM);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync AssertRC(rc);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync
0c4004948fca34f2db87e7b38013137e9472c306vboxsync pGVMM->iFreeHead = pHandle->iNext;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync pHandle->iNext = pGVMM->iUsedHead;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync pGVMM->iUsedHead = iHandle;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync pGVMM->cVMs++;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync pHandle->pVM = NULL;
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync pHandle->pGVM = NULL;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync pHandle->pSession = pSession;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync pHandle->hEMT = NIL_RTNATIVETHREAD;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync
0c4004948fca34f2db87e7b38013137e9472c306vboxsync gvmmR0UsedUnlock(pGVMM);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync rc = SUPR0ObjVerifyAccess(pHandle->pvObj, pSession, NULL);
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync if (RT_SUCCESS(rc))
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync {
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync /*
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync * Allocate the global VM structure (GVM) and initialize it.
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync */
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync PGVM pGVM = (PGVM)RTMemAllocZ(sizeof(*pGVM));
0c4004948fca34f2db87e7b38013137e9472c306vboxsync if (pGVM)
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync {
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync pGVM->u32Magic = GVM_MAGIC;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pGVM->hSelf = iHandle;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pGVM->hEMT = NIL_RTNATIVETHREAD;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pGVM->pVM = NULL;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync gvmmR0InitPerVMData(pGVM);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* GMMR0InitPerVMData(pGVM); - later */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /*
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Allocate the shared VM structure and associated page array.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync const size_t cPages = RT_ALIGN(sizeof(VM), PAGE_SIZE) >> PAGE_SHIFT;
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync rc = RTR0MemObjAllocLow(&pGVM->gvmm.s.VMMemObj, cPages << PAGE_SHIFT, false /* fExecutable */);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (RT_SUCCESS(rc))
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PVM pVM = (PVM)RTR0MemObjAddress(pGVM->gvmm.s.VMMemObj); AssertPtr(pVM);
8cd2f2e64725096acb682f34a5568b7fb816eda7vboxsync memset(pVM, 0, cPages << PAGE_SHIFT);
8cd2f2e64725096acb682f34a5568b7fb816eda7vboxsync pVM->enmVMState = VMSTATE_CREATING;
8cd2f2e64725096acb682f34a5568b7fb816eda7vboxsync pVM->pVMR0 = pVM;
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync pVM->pSession = pSession;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pVM->hSelf = iHandle;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync rc = RTR0MemObjAllocPage(&pGVM->gvmm.s.VMPagesMemObj, cPages * sizeof(SUPPAGE), false /* fExecutable */);
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync if (RT_SUCCESS(rc))
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync {
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync PSUPPAGE paPages = (PSUPPAGE)RTR0MemObjAddress(pGVM->gvmm.s.VMPagesMemObj); AssertPtr(paPages);
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync for (size_t iPage = 0; iPage < cPages; iPage++)
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync {
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync paPages[iPage].uReserved = 0;
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync paPages[iPage].Phys = RTR0MemObjGetPagePhysAddr(pGVM->gvmm.s.VMMemObj, iPage);
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync Assert(paPages[iPage].Phys != NIL_RTHCPHYS);
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync }
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /*
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Map them into ring-3.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = RTR0MemObjMapUser(&pGVM->gvmm.s.VMMapObj, pGVM->gvmm.s.VMMemObj, (RTR3PTR)-1, 0,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
cba6719bd64ec749967bbe931230452664109857vboxsync if (RT_SUCCESS(rc))
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsync pVM->pVMR3 = RTR0MemObjAddressR3(pGVM->gvmm.s.VMMapObj);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertPtr((void *)pVM->pVMR3);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsync /** @todo SMP - need to get the cpu count and initialize all the VM pointers. */
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync pVM->aCpus[0].pVMR0 = pVM;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pVM->aCpus[0].pVMR3 = pVM->pVMR3;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
a8139954a84d6e9090dd3a8371aa788351d45bc3vboxsync rc = RTR0MemObjMapUser(&pGVM->gvmm.s.VMPagesMapObj, pGVM->gvmm.s.VMPagesMemObj, (RTR3PTR)-1, 0,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
a8139954a84d6e9090dd3a8371aa788351d45bc3vboxsync if (RT_SUCCESS(rc))
a8139954a84d6e9090dd3a8371aa788351d45bc3vboxsync {
a8139954a84d6e9090dd3a8371aa788351d45bc3vboxsync pVM->paVMPagesR3 = RTR0MemObjAddressR3(pGVM->gvmm.s.VMPagesMapObj);
a8139954a84d6e9090dd3a8371aa788351d45bc3vboxsync AssertPtr((void *)pVM->paVMPagesR3);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* complete the handle - take the UsedLock sem just to be careful. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = gvmmR0UsedLock(pGVMM);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertRC(rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pHandle->pVM = pVM;
cba6719bd64ec749967bbe931230452664109857vboxsync pHandle->pGVM = pGVM;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pHandle->hEMT = hEMT;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pGVM->pVM = pVM;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pGVM->hEMT = hEMT;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync gvmmR0UsedUnlock(pGVMM);
da3503c04ce76e653401396fe2795a9bc2427a1dvboxsync gvmmR0CreateDestroyUnlock(pGVMM);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync *ppVM = pVM;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync Log(("GVMMR0CreateVM: pVM=%p pVMR3=%p pGVM=%p hGVM=%d\n", pVM, pVM->pVMR3, pGVM, iHandle));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return VINF_SUCCESS;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
da3503c04ce76e653401396fe2795a9bc2427a1dvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync RTR0MemObjFree(pGVM->gvmm.s.VMMapObj, false /* fFreeMappings */);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pGVM->gvmm.s.VMMapObj = NIL_RTR0MEMOBJ;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync RTR0MemObjFree(pGVM->gvmm.s.VMPagesMemObj, false /* fFreeMappings */);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pGVM->gvmm.s.VMPagesMemObj = NIL_RTR0MEMOBJ;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync RTR0MemObjFree(pGVM->gvmm.s.VMMemObj, false /* fFreeMappings */);
0db6a029780d9f9b347500e117320a8d5661efe5vboxsync pGVM->gvmm.s.VMMemObj = NIL_RTR0MEMOBJ;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
da3503c04ce76e653401396fe2795a9bc2427a1dvboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* else: The user wasn't permitted to create this VM. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
0db6a029780d9f9b347500e117320a8d5661efe5vboxsync /*
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * The handle will be freed by gvmmR0HandleObjDestructor as we release the
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * object reference here. A little extra mess because of non-recursive lock.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync void *pvObj = pHandle->pvObj;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pHandle->pvObj = NULL;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync gvmmR0CreateDestroyUnlock(pGVMM);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync SUPR0ObjRelease(pvObj, pSession);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync SUPR0Printf("GVMMR0CreateVM: failed, rc=%d\n", rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return rc;
da3503c04ce76e653401396fe2795a9bc2427a1dvboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
cba6719bd64ec749967bbe931230452664109857vboxsync rc = VERR_NO_MEMORY;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
9b789c281103a2489742bf32f6ab500e38b2ecd5vboxsync else
9b789c281103a2489742bf32f6ab500e38b2ecd5vboxsync rc = VERR_INTERNAL_ERROR;
9b789c281103a2489742bf32f6ab500e38b2ecd5vboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync else
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VERR_GVM_TOO_MANY_VMS;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync gvmmR0CreateDestroyUnlock(pGVMM);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return rc;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync}
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync/**
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * Initializes the per VM data belonging to GVMM.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync *
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param pGVM Pointer to the global VM structure.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic void gvmmR0InitPerVMData(PGVM pGVM)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertCompile(RT_SIZEOFMEMB(GVM,gvmm.s) <= RT_SIZEOFMEMB(GVM,gvmm.padding));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync Assert(RT_SIZEOFMEMB(GVM,gvmm.s) <= RT_SIZEOFMEMB(GVM,gvmm.padding));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pGVM->gvmm.s.VMMemObj = NIL_RTR0MEMOBJ;
9b789c281103a2489742bf32f6ab500e38b2ecd5vboxsync pGVM->gvmm.s.VMMapObj = NIL_RTR0MEMOBJ;
9b789c281103a2489742bf32f6ab500e38b2ecd5vboxsync pGVM->gvmm.s.VMPagesMemObj = NIL_RTR0MEMOBJ;
9b789c281103a2489742bf32f6ab500e38b2ecd5vboxsync pGVM->gvmm.s.VMPagesMapObj = NIL_RTR0MEMOBJ;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pGVM->gvmm.s.HaltEventMulti = NIL_RTSEMEVENTMULTI;
c91345f92b829d3fba05ce7a97206d83c5183ce0vboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
c91345f92b829d3fba05ce7a97206d83c5183ce0vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/**
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync * Does the VM initialization.
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync *
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync * @returns VBox status code.
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync * @param pVM Pointer to the shared VM structure.
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync */
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsyncGVMMR0DECL(int) GVMMR0InitVM(PVM pVM)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync LogFlow(("GVMMR0InitVM: pVM=%p\n", pVM));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /*
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Validate the VM structure, state and handle.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PGVM pGVM;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PGVMM pGVMM;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = gvmmR0ByVMAndEMT(pVM, &pGVM, &pGVMM);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (RT_SUCCESS(rc))
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (pGVM->gvmm.s.HaltEventMulti == NIL_RTSEMEVENTMULTI)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = RTSemEventMultiCreate(&pGVM->gvmm.s.HaltEventMulti);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (RT_FAILURE(rc))
5b465a7c1237993faf8bb50120d247f3f0319adavboxsync pGVM->gvmm.s.HaltEventMulti = NIL_RTSEMEVENTMULTI;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
ad77e3ec3cde24263bc7537575f5cae442bee3b1vboxsync else
ad77e3ec3cde24263bc7537575f5cae442bee3b1vboxsync rc = VERR_WRONG_ORDER;
ad77e3ec3cde24263bc7537575f5cae442bee3b1vboxsync }
ad77e3ec3cde24263bc7537575f5cae442bee3b1vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync LogFlow(("GVMMR0InitVM: returns %Rrc\n", rc));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return rc;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/**
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Destroys the VM, freeing all associated resources (the ring-0 ones anyway).
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * This is call from the vmR3DestroyFinalBit and from a error path in VMR3Create,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * and the caller is not the EMT thread, unfortunately. For security reasons, it
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * would've been nice if the caller was actually the EMT thread or that we somehow
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * could've associated the calling thread with the VM up front.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @returns VBox status code.
ad77e3ec3cde24263bc7537575f5cae442bee3b1vboxsync * @param pVM Where to store the pointer to the VM structure.
ad77e3ec3cde24263bc7537575f5cae442bee3b1vboxsync *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @thread EMT if it's associated with the VM, otherwise any thread.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncGVMMR0DECL(int) GVMMR0DestroyVM(PVM pVM)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync LogFlow(("GVMMR0DestroyVM: pVM=%p\n", pVM));
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync PGVMM pGVMM;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /*
cba6719bd64ec749967bbe931230452664109857vboxsync * Validate the VM structure, state and caller.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
cba6719bd64ec749967bbe931230452664109857vboxsync AssertPtrReturn(pVM, VERR_INVALID_POINTER);
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync AssertReturn(!((uintptr_t)pVM & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertMsgReturn(pVM->enmVMState >= VMSTATE_CREATING && pVM->enmVMState <= VMSTATE_TERMINATED, ("%d\n", pVM->enmVMState), VERR_WRONG_ORDER);
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync
cba6719bd64ec749967bbe931230452664109857vboxsync uint32_t hGVM = pVM->hSelf;
cba6719bd64ec749967bbe931230452664109857vboxsync AssertReturn(hGVM != NIL_GVM_HANDLE, VERR_INVALID_HANDLE);
cba6719bd64ec749967bbe931230452664109857vboxsync AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), VERR_INVALID_HANDLE);
cba6719bd64ec749967bbe931230452664109857vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync AssertReturn(pHandle->pVM == pVM, VERR_NOT_OWNER);
RTNATIVETHREAD hSelf = RTThreadNativeSelf();
AssertReturn(pHandle->hEMT == hSelf || pHandle->hEMT == NIL_RTNATIVETHREAD, VERR_NOT_OWNER);
/*
* Lookup the handle and destroy the object.
* Since the lock isn't recursive and we'll have to leave it before dereferencing the
* object, we take some precautions against racing callers just in case...
*/
int rc = gvmmR0CreateDestroyLock(pGVMM);
AssertRC(rc);
/* be careful here because we might theoretically be racing someone else cleaning up. */
if ( pHandle->pVM == pVM
&& ( pHandle->hEMT == hSelf
|| pHandle->hEMT == NIL_RTNATIVETHREAD)
&& VALID_PTR(pHandle->pvObj)
&& VALID_PTR(pHandle->pSession)
&& VALID_PTR(pHandle->pGVM)
&& pHandle->pGVM->u32Magic == GVM_MAGIC)
{
void *pvObj = pHandle->pvObj;
pHandle->pvObj = NULL;
gvmmR0CreateDestroyUnlock(pGVMM);
SUPR0ObjRelease(pvObj, pHandle->pSession);
}
else
{
SUPR0Printf("GVMMR0DestroyVM: pHandle=%p:{.pVM=%p, hEMT=%p, .pvObj=%p} pVM=%p hSelf=%p\n",
pHandle, pHandle->pVM, pHandle->hEMT, pHandle->pvObj, pVM, hSelf);
gvmmR0CreateDestroyUnlock(pGVMM);
rc = VERR_INTERNAL_ERROR;
}
return rc;
}
/**
* Handle destructor.
*
* @param pvGVMM The GVM instance pointer.
* @param pvHandle The handle pointer.
*/
static DECLCALLBACK(void) gvmmR0HandleObjDestructor(void *pvObj, void *pvGVMM, void *pvHandle)
{
LogFlow(("gvmmR0HandleObjDestructor: %p %p %p\n", pvObj, pvGVMM, pvHandle));
/*
* Some quick, paranoid, input validation.
*/
PGVMHANDLE pHandle = (PGVMHANDLE)pvHandle;
AssertPtr(pHandle);
PGVMM pGVMM = (PGVMM)pvGVMM;
Assert(pGVMM == g_pGVMM);
const uint16_t iHandle = pHandle - &pGVMM->aHandles[0];
if ( !iHandle
|| iHandle >= RT_ELEMENTS(pGVMM->aHandles)
|| iHandle != pHandle->iSelf)
{
SUPR0Printf("GVM: handle %d is out of range or corrupt (iSelf=%d)!\n", iHandle, pHandle->iSelf);
return;
}
int rc = gvmmR0CreateDestroyLock(pGVMM);
AssertRC(rc);
rc = gvmmR0UsedLock(pGVMM);
AssertRC(rc);
/*
* This is a tad slow but a doubly linked list is too much hazzle.
*/
if (RT_UNLIKELY(pHandle->iNext >= RT_ELEMENTS(pGVMM->aHandles)))
{
SUPR0Printf("GVM: used list index %d is out of range!\n", pHandle->iNext);
gvmmR0UsedUnlock(pGVMM);
gvmmR0CreateDestroyUnlock(pGVMM);
return;
}
if (pGVMM->iUsedHead == iHandle)
pGVMM->iUsedHead = pHandle->iNext;
else
{
uint16_t iPrev = pGVMM->iUsedHead;
int c = RT_ELEMENTS(pGVMM->aHandles) + 2;
while (iPrev)
{
if (RT_UNLIKELY(iPrev >= RT_ELEMENTS(pGVMM->aHandles)))
{
SUPR0Printf("GVM: used list index %d is out of range!\n");
gvmmR0UsedUnlock(pGVMM);
gvmmR0CreateDestroyUnlock(pGVMM);
return;
}
if (RT_UNLIKELY(c-- <= 0))
{
iPrev = 0;
break;
}
if (pGVMM->aHandles[iPrev].iNext == iHandle)
break;
iPrev = pGVMM->aHandles[iPrev].iNext;
}
if (!iPrev)
{
SUPR0Printf("GVM: can't find the handle previous previous of %d!\n", pHandle->iSelf);
gvmmR0UsedUnlock(pGVMM);
gvmmR0CreateDestroyUnlock(pGVMM);
return;
}
Assert(pGVMM->aHandles[iPrev].iNext == iHandle);
pGVMM->aHandles[iPrev].iNext = pHandle->iNext;
}
pHandle->iNext = 0;
pGVMM->cVMs--;
gvmmR0UsedUnlock(pGVMM);
/*
* Do the global cleanup round.
*/
PGVM pGVM = pHandle->pGVM;
if ( VALID_PTR(pGVM)
&& pGVM->u32Magic == GVM_MAGIC)
{
/// @todo GMMR0CleanupVM(pGVM);
/*
* Do the GVMM cleanup - must be done last.
*/
/* The VM and VM pages mappings/allocations. */
if (pGVM->gvmm.s.VMPagesMapObj != NIL_RTR0MEMOBJ)
{
rc = RTR0MemObjFree(pGVM->gvmm.s.VMPagesMapObj, false /* fFreeMappings */); AssertRC(rc);
pGVM->gvmm.s.VMPagesMapObj = NIL_RTR0MEMOBJ;
}
if (pGVM->gvmm.s.VMMapObj != NIL_RTR0MEMOBJ)
{
rc = RTR0MemObjFree(pGVM->gvmm.s.VMMapObj, false /* fFreeMappings */); AssertRC(rc);
pGVM->gvmm.s.VMMapObj = NIL_RTR0MEMOBJ;
}
if (pGVM->gvmm.s.VMPagesMemObj != NIL_RTR0MEMOBJ)
{
rc = RTR0MemObjFree(pGVM->gvmm.s.VMPagesMemObj, false /* fFreeMappings */); AssertRC(rc);
pGVM->gvmm.s.VMPagesMemObj = NIL_RTR0MEMOBJ;
}
if (pGVM->gvmm.s.VMMemObj != NIL_RTR0MEMOBJ)
{
rc = RTR0MemObjFree(pGVM->gvmm.s.VMMemObj, false /* fFreeMappings */); AssertRC(rc);
pGVM->gvmm.s.VMMemObj = NIL_RTR0MEMOBJ;
}
if (pGVM->gvmm.s.HaltEventMulti != NIL_RTSEMEVENTMULTI)
{
rc = RTSemEventMultiDestroy(pGVM->gvmm.s.HaltEventMulti); AssertRC(rc);
pGVM->gvmm.s.HaltEventMulti = NIL_RTSEMEVENTMULTI;
}
/* the GVM structure itself. */
pGVM->u32Magic++;
RTMemFree(pGVM);
}
/* else: GVMMR0CreateVM cleanup. */
/*
* Free the handle.
* Reacquire the UsedLock here to since we're updating handle fields.
*/
rc = gvmmR0UsedLock(pGVMM);
AssertRC(rc);
pHandle->iNext = pGVMM->iFreeHead;
pGVMM->iFreeHead = iHandle;
ASMAtomicXchgPtr((void * volatile *)&pHandle->pGVM, NULL);
ASMAtomicXchgPtr((void * volatile *)&pHandle->pVM, NULL);
ASMAtomicXchgPtr((void * volatile *)&pHandle->pvObj, NULL);
ASMAtomicXchgPtr((void * volatile *)&pHandle->pSession, NULL);
ASMAtomicXchgSize(&pHandle->hEMT, NIL_RTNATIVETHREAD);
gvmmR0UsedUnlock(pGVMM);
gvmmR0CreateDestroyUnlock(pGVMM);
LogFlow(("gvmmR0HandleObjDestructor: returns\n"));
}
/**
* Lookup a GVM structure by its handle.
*
* @returns The GVM pointer on success, NULL on failure.
* @param hGVM The global VM handle. Asserts on bad handle.
*/
GVMMR0DECL(PGVM) GVMMR0ByHandle(uint32_t hGVM)
{
PGVMM pGVMM;
GVMM_GET_VALID_INSTANCE(pGVMM, NULL);
/*
* Validate.
*/
AssertReturn(hGVM != NIL_GVM_HANDLE, NULL);
AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), NULL);
/*
* Look it up.
*/
PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
AssertPtrReturn(pHandle->pVM, NULL);
AssertPtrReturn(pHandle->pvObj, NULL);
PGVM pGVM = pHandle->pGVM;
AssertPtrReturn(pGVM, NULL);
AssertReturn(pGVM->pVM == pHandle->pVM, NULL);
return pHandle->pGVM;
}
/**
* Lookup a GVM structure by the shared VM structure.
*
* @returns VBox status code.
* @param pVM The shared VM structure (the ring-0 mapping).
* @param ppGVM Where to store the GVM pointer.
* @param ppGVMM Where to store the pointer to the GVMM instance data.
* @param fTakeUsedLock Whether to take the used lock or not.
* Be very careful if not taking the lock as it's possible that
* the VM will disappear then.
*
* @remark This will not assert on an invalid pVM but try return sliently.
*/
static int gvmmR0ByVM(PVM pVM, PGVM *ppGVM, PGVMM *ppGVMM, bool fTakeUsedLock)
{
PGVMM pGVMM;
GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
/*
* Validate.
*/
if (RT_UNLIKELY( !VALID_PTR(pVM)
|| ((uintptr_t)pVM & PAGE_OFFSET_MASK)))
return VERR_INVALID_POINTER;
if (RT_UNLIKELY( pVM->enmVMState < VMSTATE_CREATING
|| pVM->enmVMState >= VMSTATE_TERMINATED))
return VERR_INVALID_POINTER;
uint16_t hGVM = pVM->hSelf;
if (RT_UNLIKELY( hGVM == NIL_GVM_HANDLE
|| hGVM >= RT_ELEMENTS(pGVMM->aHandles)))
return VERR_INVALID_HANDLE;
/*
* Look it up.
*/
PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
PGVM pGVM;
if (fTakeUsedLock)
{
int rc = gvmmR0UsedLock(pGVMM);
AssertRCReturn(rc, rc);
pGVM = pHandle->pGVM;
if (RT_UNLIKELY( pHandle->pVM != pVM
|| !VALID_PTR(pHandle->pvObj)
|| !VALID_PTR(pGVM)
|| pGVM->pVM != pVM))
{
gvmmR0UsedUnlock(pGVMM);
return VERR_INVALID_HANDLE;
}
}
else
{
if (RT_UNLIKELY(pHandle->pVM != pVM))
return VERR_INVALID_HANDLE;
if (RT_UNLIKELY(!VALID_PTR(pHandle->pvObj)))
return VERR_INVALID_HANDLE;
pGVM = pHandle->pGVM;
if (RT_UNLIKELY(!VALID_PTR(pGVM)))
return VERR_INVALID_HANDLE;
if (RT_UNLIKELY(pGVM->pVM != pVM))
return VERR_INVALID_HANDLE;
}
*ppGVM = pGVM;
*ppGVMM = pGVMM;
return VINF_SUCCESS;
}
/**
* Lookup a GVM structure by the shared VM structure.
*
* @returns The GVM pointer on success, NULL on failure.
* @param pVM The shared VM structure (the ring-0 mapping).
*
* @remark This will not take the 'used'-lock because it doesn't do
* nesting and this function will be used from under the lock.
*/
GVMMR0DECL(PGVM) GVMMR0ByVM(PVM pVM)
{
PGVMM pGVMM;
PGVM pGVM;
int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, false /* fTakeUsedLock */);
if (RT_SUCCESS(rc))
return pGVM;
AssertRC(rc);
return NULL;
}
/**
* Lookup a GVM structure by the shared VM structure
* and ensuring that the caller is the EMT thread.
*
* @returns VBox status code.
* @param pVM The shared VM structure (the ring-0 mapping).
* @param ppGVM Where to store the GVM pointer.
* @param ppGVMM Where to store the pointer to the GVMM instance data.
* @thread EMT
*
* @remark This will assert in failure paths.
*/
static int gvmmR0ByVMAndEMT(PVM pVM, PGVM *ppGVM, PGVMM *ppGVMM)
{
PGVMM pGVMM;
GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
/*
* Validate.
*/
AssertPtrReturn(pVM, VERR_INVALID_POINTER);
AssertReturn(!((uintptr_t)pVM & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
uint16_t hGVM = pVM->hSelf;
AssertReturn(hGVM != NIL_GVM_HANDLE, VERR_INVALID_HANDLE);
AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), VERR_INVALID_HANDLE);
/*
* Look it up.
*/
PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
RTNATIVETHREAD hAllegedEMT = RTThreadNativeSelf();
AssertMsgReturn(pHandle->hEMT == hAllegedEMT, ("hEMT %x hAllegedEMT %x\n", pHandle->hEMT, hAllegedEMT), VERR_NOT_OWNER);
AssertReturn(pHandle->pVM == pVM, VERR_NOT_OWNER);
AssertPtrReturn(pHandle->pvObj, VERR_INTERNAL_ERROR);
PGVM pGVM = pHandle->pGVM;
AssertPtrReturn(pGVM, VERR_INTERNAL_ERROR);
AssertReturn(pGVM->pVM == pVM, VERR_INTERNAL_ERROR);
AssertReturn(pGVM->hEMT == hAllegedEMT, VERR_INTERNAL_ERROR);
*ppGVM = pGVM;
*ppGVMM = pGVMM;
return VINF_SUCCESS;
}
/**
* Lookup a GVM structure by the shared VM structure
* and ensuring that the caller is the EMT thread.
*
* @returns VBox status code.
* @param pVM The shared VM structure (the ring-0 mapping).
* @param ppGVM Where to store the GVM pointer.
* @thread EMT
*/
GVMMR0DECL(int) GVMMR0ByVMAndEMT(PVM pVM, PGVM *ppGVM)
{
AssertPtrReturn(ppGVM, VERR_INVALID_POINTER);
PGVMM pGVMM;
return gvmmR0ByVMAndEMT(pVM, ppGVM, &pGVMM);
}
/**
* Lookup a VM by its global handle.
*
* @returns The VM handle on success, NULL on failure.
* @param hGVM The global VM handle. Asserts on bad handle.
*/
GVMMR0DECL(PVM) GVMMR0GetVMByHandle(uint32_t hGVM)
{
PGVM pGVM = GVMMR0ByHandle(hGVM);
return pGVM ? pGVM->pVM : NULL;
}
/**
* Looks up the VM belonging to the specified EMT thread.
*
* This is used by the assertion machinery in VMMR0.cpp to avoid causing
* unnecessary kernel panics when the EMT thread hits an assertion. The
* call may or not be an EMT thread.
*
* @returns The VM handle on success, NULL on failure.
* @param hEMT The native thread handle of the EMT.
* NIL_RTNATIVETHREAD means the current thread
*/
GVMMR0DECL(PVM) GVMMR0GetVMByEMT(RTNATIVETHREAD hEMT)
{
/*
* No Assertions here as we're usually called in a AssertMsgN or
* RTAssert* context.
*/
PGVMM pGVMM = g_pGVMM;
if ( !VALID_PTR(pGVMM)
|| pGVMM->u32Magic != GVMM_MAGIC)
return NULL;
if (hEMT == NIL_RTNATIVETHREAD)
hEMT = RTThreadNativeSelf();
/*
* Search the handles in a linear fashion as we don't dare take the lock (assert).
*/
for (unsigned i = 1; i < RT_ELEMENTS(pGVMM->aHandles); i++)
if ( pGVMM->aHandles[i].hEMT == hEMT
&& pGVMM->aHandles[i].iSelf == i
&& VALID_PTR(pGVMM->aHandles[i].pvObj)
&& VALID_PTR(pGVMM->aHandles[i].pVM))
return pGVMM->aHandles[i].pVM;
return NULL;
}
/**
* This is will wake up expired and soon-to-be expired VMs.
*
* @returns Number of VMs that has been woken up.
* @param pGVMM Pointer to the GVMM instance data.
* @param u64Now The current time.
*/
static unsigned gvmmR0SchedDoWakeUps(PGVMM pGVMM, uint64_t u64Now)
{
/*
* The first pass will wake up VMs which has actually expired
* and look for VMs that should be woken up in the 2nd and 3rd passes.
*/
unsigned cWoken = 0;
unsigned cHalted = 0;
unsigned cTodo2nd = 0;
unsigned cTodo3rd = 0;
for (unsigned i = pGVMM->iUsedHead, cGuard = 0;
i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
i = pGVMM->aHandles[i].iNext)
{
PGVM pCurGVM = pGVMM->aHandles[i].pGVM;
if ( VALID_PTR(pCurGVM)
&& pCurGVM->u32Magic == GVM_MAGIC)
{
uint64_t u64 = pCurGVM->gvmm.s.u64HaltExpire;
if (u64)
{
if (u64 <= u64Now)
{
if (ASMAtomicXchgU64(&pCurGVM->gvmm.s.u64HaltExpire, 0))
{
int rc = RTSemEventMultiSignal(pCurGVM->gvmm.s.HaltEventMulti);
AssertRC(rc);
cWoken++;
}
}
else
{
cHalted++;
if (u64 <= u64Now + pGVMM->nsEarlyWakeUp1)
cTodo2nd++;
else if (u64 <= u64Now + pGVMM->nsEarlyWakeUp2)
cTodo3rd++;
}
}
}
AssertLogRelBreak(cGuard++ < RT_ELEMENTS(pGVMM->aHandles));
}
if (cTodo2nd)
{
for (unsigned i = pGVMM->iUsedHead, cGuard = 0;
i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
i = pGVMM->aHandles[i].iNext)
{
PGVM pCurGVM = pGVMM->aHandles[i].pGVM;
if ( VALID_PTR(pCurGVM)
&& pCurGVM->u32Magic == GVM_MAGIC
&& pCurGVM->gvmm.s.u64HaltExpire
&& pCurGVM->gvmm.s.u64HaltExpire <= u64Now + pGVMM->nsEarlyWakeUp1)
{
if (ASMAtomicXchgU64(&pCurGVM->gvmm.s.u64HaltExpire, 0))
{
int rc = RTSemEventMultiSignal(pCurGVM->gvmm.s.HaltEventMulti);
AssertRC(rc);
cWoken++;
}
}
AssertLogRelBreak(cGuard++ < RT_ELEMENTS(pGVMM->aHandles));
}
}
if (cTodo3rd)
{
for (unsigned i = pGVMM->iUsedHead, cGuard = 0;
i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
i = pGVMM->aHandles[i].iNext)
{
PGVM pCurGVM = pGVMM->aHandles[i].pGVM;
if ( VALID_PTR(pCurGVM)
&& pCurGVM->u32Magic == GVM_MAGIC
&& pCurGVM->gvmm.s.u64HaltExpire
&& pCurGVM->gvmm.s.u64HaltExpire <= u64Now + pGVMM->nsEarlyWakeUp2)
{
if (ASMAtomicXchgU64(&pCurGVM->gvmm.s.u64HaltExpire, 0))
{
int rc = RTSemEventMultiSignal(pCurGVM->gvmm.s.HaltEventMulti);
AssertRC(rc);
cWoken++;
}
}
AssertLogRelBreak(cGuard++ < RT_ELEMENTS(pGVMM->aHandles));
}
}
return cWoken;
}
/**
* Halt the EMT thread.
*
* @returns VINF_SUCCESS normal wakeup (timeout or kicked by other thread).
* VERR_INTERRUPTED if a signal was scheduled for the thread.
* @param pVM Pointer to the shared VM structure.
* @param u64ExpireGipTime The time for the sleep to expire expressed as GIP time.
* @thread EMT.
*/
GVMMR0DECL(int) GVMMR0SchedHalt(PVM pVM, uint64_t u64ExpireGipTime)
{
LogFlow(("GVMMR0SchedHalt: pVM=%p\n", pVM));
/*
* Validate the VM structure, state and handle.
*/
PGVMM pGVMM;
PGVM pGVM;
int rc = gvmmR0ByVMAndEMT(pVM, &pGVM, &pGVMM);
if (RT_FAILURE(rc))
return rc;
pGVM->gvmm.s.StatsSched.cHaltCalls++;
Assert(!pGVM->gvmm.s.u64HaltExpire);
/*
* Take the UsedList semaphore, get the current time
* and check if anyone needs waking up.
* Interrupts must NOT be disabled at this point because we ask for GIP time!
*/
rc = gvmmR0UsedLock(pGVMM);
AssertRC(rc);
pGVM->gvmm.s.iCpuEmt = ASMGetApicId();
Assert(ASMGetFlags() & X86_EFL_IF);
const uint64_t u64Now = RTTimeNanoTS(); /* (GIP time) */
pGVM->gvmm.s.StatsSched.cHaltWakeUps += gvmmR0SchedDoWakeUps(pGVMM, u64Now);
/*
* Go to sleep if we must...
*/
if ( u64Now < u64ExpireGipTime
&& u64ExpireGipTime - u64Now > (pGVMM->cVMs > pGVMM->cVMsMeansCompany
? pGVMM->nsMinSleepCompany
: pGVMM->nsMinSleepAlone))
{
pGVM->gvmm.s.StatsSched.cHaltBlocking++;
ASMAtomicXchgU64(&pGVM->gvmm.s.u64HaltExpire, u64ExpireGipTime);
gvmmR0UsedUnlock(pGVMM);
uint32_t cMillies = (u64ExpireGipTime - u64Now) / 1000000;
rc = RTSemEventMultiWaitNoResume(pGVM->gvmm.s.HaltEventMulti, cMillies ? cMillies : 1);
ASMAtomicXchgU64(&pGVM->gvmm.s.u64HaltExpire, 0);
if (rc == VERR_TIMEOUT)
{
pGVM->gvmm.s.StatsSched.cHaltTimeouts++;
rc = VINF_SUCCESS;
}
}
else
{
pGVM->gvmm.s.StatsSched.cHaltNotBlocking++;
gvmmR0UsedUnlock(pGVMM);
}
/* Make sure false wake up calls (gvmmR0SchedDoWakeUps) cause us to spin. */
RTSemEventMultiReset(pGVM->gvmm.s.HaltEventMulti);
return rc;
}
/**
* Wakes up the halted EMT thread so it can service a pending request.
*
* @returns VINF_SUCCESS if not yielded.
* VINF_GVM_NOT_BLOCKED if the EMT thread wasn't blocked.
* @param pVM Pointer to the shared VM structure.
* @thread Any but EMT.
*/
GVMMR0DECL(int) GVMMR0SchedWakeUp(PVM pVM)
{
/*
* Validate input and take the UsedLock.
*/
PGVM pGVM;
PGVMM pGVMM;
int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, true /* fTakeUsedLock */);
if (RT_SUCCESS(rc))
{
pGVM->gvmm.s.StatsSched.cWakeUpCalls++;
/*
* Signal the semaphore regardless of whether it's current blocked on it.
*
* The reason for this is that there is absolutely no way we can be 100%
* certain that it isn't *about* go to go to sleep on it and just got
* delayed a bit en route. So, we will always signal the semaphore when
* the it is flagged as halted in the VMM.
*/
if (pGVM->gvmm.s.u64HaltExpire)
{
rc = VINF_SUCCESS;
ASMAtomicXchgU64(&pGVM->gvmm.s.u64HaltExpire, 0);
}
else
{
rc = VINF_GVM_NOT_BLOCKED;
pGVM->gvmm.s.StatsSched.cWakeUpNotHalted++;
}
int rc2 = RTSemEventMultiSignal(pGVM->gvmm.s.HaltEventMulti);
AssertRC(rc2);
/*
* While we're here, do a round of scheduling.
*/
Assert(ASMGetFlags() & X86_EFL_IF);
const uint64_t u64Now = RTTimeNanoTS(); /* (GIP time) */
pGVM->gvmm.s.StatsSched.cWakeUpWakeUps += gvmmR0SchedDoWakeUps(pGVMM, u64Now);
rc2 = gvmmR0UsedUnlock(pGVMM);
AssertRC(rc2);
}
LogFlow(("GVMMR0SchedWakeUp: returns %Rrc\n", rc));
return rc;
}
/**
* Poll the schedule to see if someone else should get a chance to run.
*
* This is a bit hackish and will not work too well if the machine is
* under heavy load from non-VM processes.
*
* @returns VINF_SUCCESS if not yielded.
* VINF_GVM_YIELDED if an attempt to switch to a different VM task was made.
* @param pVM Pointer to the shared VM structure.
* @param u64ExpireGipTime The time for the sleep to expire expressed as GIP time.
* @param fYield Whether to yield or not.
* This is for when we're spinning in the halt loop.
* @thread EMT.
*/
GVMMR0DECL(int) GVMMR0SchedPoll(PVM pVM, bool fYield)
{
/*
* Validate input.
*/
PGVM pGVM;
PGVMM pGVMM;
int rc = gvmmR0ByVMAndEMT(pVM, &pGVM, &pGVMM);
if (RT_SUCCESS(rc))
{
rc = gvmmR0UsedLock(pGVMM);
AssertRC(rc);
pGVM->gvmm.s.StatsSched.cPollCalls++;
Assert(ASMGetFlags() & X86_EFL_IF);
const uint64_t u64Now = RTTimeNanoTS(); /* (GIP time) */
if (!fYield)
pGVM->gvmm.s.StatsSched.cPollWakeUps += gvmmR0SchedDoWakeUps(pGVMM, u64Now);
else
{
/** @todo implement this... */
rc = VERR_NOT_IMPLEMENTED;
}
gvmmR0UsedUnlock(pGVMM);
}
LogFlow(("GVMMR0SchedWakeUp: returns %Rrc\n", rc));
return rc;
}
/**
* Retrieves the GVMM statistics visible to the caller.
*
* @returns VBox status code.
*
* @param pStats Where to put the statistics.
* @param pSession The current session.
* @param pVM The VM to obtain statistics for. Optional.
*/
GVMMR0DECL(int) GVMMR0QueryStatistics(PGVMMSTATS pStats, PSUPDRVSESSION pSession, PVM pVM)
{
LogFlow(("GVMMR0QueryStatistics: pStats=%p pSession=%p pVM=%p\n", pStats, pSession, pVM));
/*
* Validate input.
*/
AssertPtrReturn(pSession, VERR_INVALID_POINTER);
AssertPtrReturn(pStats, VERR_INVALID_POINTER);
pStats->cVMs = 0; /* (crash before taking the sem...) */
/*
* Take the lock and get the VM statistics.
*/
PGVMM pGVMM;
if (pVM)
{
PGVM pGVM;
int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, true /*fTakeUsedLock*/);
if (RT_FAILURE(rc))
return rc;
pStats->SchedVM = pGVM->gvmm.s.StatsSched;
}
else
{
GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
memset(&pStats->SchedVM, 0, sizeof(pStats->SchedVM));
int rc = gvmmR0UsedLock(pGVMM);
AssertRCReturn(rc, rc);
}
/*
* Enumerate the VMs and add the ones visibile to the statistics.
*/
pStats->cVMs = 0;
memset(&pStats->SchedSum, 0, sizeof(pStats->SchedSum));
for (unsigned i = pGVMM->iUsedHead;
i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
i = pGVMM->aHandles[i].iNext)
{
PGVM pGVM = pGVMM->aHandles[i].pGVM;
void *pvObj = pGVMM->aHandles[i].pvObj;
if ( VALID_PTR(pvObj)
&& VALID_PTR(pGVM)
&& pGVM->u32Magic == GVM_MAGIC
&& RT_SUCCESS(SUPR0ObjVerifyAccess(pvObj, pSession, NULL)))
{
pStats->cVMs++;
pStats->SchedSum.cHaltCalls += pGVM->gvmm.s.StatsSched.cHaltCalls;
pStats->SchedSum.cHaltBlocking += pGVM->gvmm.s.StatsSched.cHaltBlocking;
pStats->SchedSum.cHaltTimeouts += pGVM->gvmm.s.StatsSched.cHaltTimeouts;
pStats->SchedSum.cHaltNotBlocking += pGVM->gvmm.s.StatsSched.cHaltNotBlocking;
pStats->SchedSum.cHaltWakeUps += pGVM->gvmm.s.StatsSched.cHaltWakeUps;
pStats->SchedSum.cWakeUpCalls += pGVM->gvmm.s.StatsSched.cWakeUpCalls;
pStats->SchedSum.cWakeUpNotHalted += pGVM->gvmm.s.StatsSched.cWakeUpNotHalted;
pStats->SchedSum.cWakeUpWakeUps += pGVM->gvmm.s.StatsSched.cWakeUpWakeUps;
pStats->SchedSum.cPollCalls += pGVM->gvmm.s.StatsSched.cPollCalls;
pStats->SchedSum.cPollHalts += pGVM->gvmm.s.StatsSched.cPollHalts;
pStats->SchedSum.cPollWakeUps += pGVM->gvmm.s.StatsSched.cPollWakeUps;
}
}
gvmmR0UsedUnlock(pGVMM);
return VINF_SUCCESS;
}
/**
* VMMR0 request wrapper for GVMMR0QueryStatistics.
*
* @returns see GVMMR0QueryStatistics.
* @param pVM Pointer to the shared VM structure. Optional.
* @param pReq The request packet.
*/
GVMMR0DECL(int) GVMMR0QueryStatisticsReq(PVM pVM, PGVMMQUERYSTATISTICSSREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
return GVMMR0QueryStatistics(&pReq->Stats, pReq->pSession, pVM);
}
/**
* Resets the specified GVMM statistics.
*
* @returns VBox status code.
*
* @param pStats Which statistics to reset, that is, non-zero fields indicates which to reset.
* @param pSession The current session.
* @param pVM The VM to reset statistics for. Optional.
*/
GVMMR0DECL(int) GVMMR0ResetStatistics(PCGVMMSTATS pStats, PSUPDRVSESSION pSession, PVM pVM)
{
LogFlow(("GVMMR0ResetStatistics: pStats=%p pSession=%p pVM=%p\n", pStats, pSession, pVM));
/*
* Validate input.
*/
AssertPtrReturn(pSession, VERR_INVALID_POINTER);
AssertPtrReturn(pStats, VERR_INVALID_POINTER);
/*
* Take the lock and get the VM statistics.
*/
PGVMM pGVMM;
if (pVM)
{
PGVM pGVM;
int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, true /*fTakeUsedLock*/);
if (RT_FAILURE(rc))
return rc;
# define MAYBE_RESET_FIELD(field) \
do { if (pStats->SchedVM. field ) { pGVM->gvmm.s.StatsSched. field = 0; } } while (0)
MAYBE_RESET_FIELD(cHaltCalls);
MAYBE_RESET_FIELD(cHaltBlocking);
MAYBE_RESET_FIELD(cHaltTimeouts);
MAYBE_RESET_FIELD(cHaltNotBlocking);
MAYBE_RESET_FIELD(cHaltWakeUps);
MAYBE_RESET_FIELD(cWakeUpCalls);
MAYBE_RESET_FIELD(cWakeUpNotHalted);
MAYBE_RESET_FIELD(cWakeUpWakeUps);
MAYBE_RESET_FIELD(cPollCalls);
MAYBE_RESET_FIELD(cPollHalts);
MAYBE_RESET_FIELD(cPollWakeUps);
# undef MAYBE_RESET_FIELD
}
else
{
GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
int rc = gvmmR0UsedLock(pGVMM);
AssertRCReturn(rc, rc);
}
/*
* Enumerate the VMs and add the ones visibile to the statistics.
*/
if (ASMMemIsAll8(&pStats->SchedSum, sizeof(pStats->SchedSum), 0))
{
for (unsigned i = pGVMM->iUsedHead;
i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
i = pGVMM->aHandles[i].iNext)
{
PGVM pGVM = pGVMM->aHandles[i].pGVM;
void *pvObj = pGVMM->aHandles[i].pvObj;
if ( VALID_PTR(pvObj)
&& VALID_PTR(pGVM)
&& pGVM->u32Magic == GVM_MAGIC
&& RT_SUCCESS(SUPR0ObjVerifyAccess(pvObj, pSession, NULL)))
{
# define MAYBE_RESET_FIELD(field) \
do { if (pStats->SchedSum. field ) { pGVM->gvmm.s.StatsSched. field = 0; } } while (0)
MAYBE_RESET_FIELD(cHaltCalls);
MAYBE_RESET_FIELD(cHaltBlocking);
MAYBE_RESET_FIELD(cHaltTimeouts);
MAYBE_RESET_FIELD(cHaltNotBlocking);
MAYBE_RESET_FIELD(cHaltWakeUps);
MAYBE_RESET_FIELD(cWakeUpCalls);
MAYBE_RESET_FIELD(cWakeUpNotHalted);
MAYBE_RESET_FIELD(cWakeUpWakeUps);
MAYBE_RESET_FIELD(cPollCalls);
MAYBE_RESET_FIELD(cPollHalts);
MAYBE_RESET_FIELD(cPollWakeUps);
# undef MAYBE_RESET_FIELD
}
}
}
gvmmR0UsedUnlock(pGVMM);
return VINF_SUCCESS;
}
/**
* VMMR0 request wrapper for GVMMR0ResetStatistics.
*
* @returns see GVMMR0ResetStatistics.
* @param pVM Pointer to the shared VM structure. Optional.
* @param pReq The request packet.
*/
GVMMR0DECL(int) GVMMR0ResetStatisticsReq(PVM pVM, PGVMMRESETSTATISTICSSREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
return GVMMR0ResetStatistics(&pReq->Stats, pReq->pSession, pVM);
}