GVMMR0.cpp revision eb3d4bdcfedc1f41576ce8a3a53da32380002272
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync/* $Id$ */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync/** @file
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * GVMM - Global VM Manager.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync/*
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * Copyright (C) 2007 InnoTek Systemberatung GmbH
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * available from http://www.virtualbox.org. This file is free software;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * you can redistribute it and/or modify it under the terms of the GNU
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * General Public License as published by the Free Software Foundation,
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * distribution. VirtualBox OSE is distributed in the hope that it will
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * be useful, but WITHOUT ANY WARRANTY of any kind.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync/** @page pg_GVMM GVMM - The Global VM Manager
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * The Global VM Manager lives in ring-0. It's main function at the moment
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * is to manage a list of all running VMs, keep a ring-0 only structure (GVM)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * for each of them, and assign them unique identifiers (so GMM can track
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * page owners). The idea for the future is to add an idle priority kernel
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * thread that can take care of tasks like page sharing.
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsync *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * The GVMM will create a ring-0 object for each VM when it's registered,
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * this is both for session cleanup purposes and for having a point where
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * it's possible to implement usage polices later (in SUPR0ObjRegister).
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync/*******************************************************************************
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync* Header Files *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync*******************************************************************************/
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync#define LOG_GROUP LOG_GROUP_GVMM
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync#include <VBox/gvmm.h>
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync#include "GVMMR0Internal.h"
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync#include <VBox/gvm.h>
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync#include <VBox/vm.h>
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync#include <VBox/err.h>
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync#include <iprt/alloc.h>
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync#include <iprt/semaphore.h>
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync#include <iprt/time.h>
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsync#include <VBox/log.h>
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync#include <iprt/thread.h>
e204adaca3d185da35ecbb638f81535c7e95b47dvboxsync#include <iprt/param.h>
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync#include <iprt/string.h>
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync#include <iprt/assert.h>
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync#include <iprt/mem.h>
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync#include <iprt/memobj.h>
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync/*******************************************************************************
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync* Structures and Typedefs *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync*******************************************************************************/
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync/**
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * Global VM handle.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsynctypedef struct GVMHANDLE
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync{
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /** The index of the next handle in the list (free or used). (0 is nil.) */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync uint16_t volatile iNext;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /** Our own index / handle value. */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync uint16_t iSelf;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /** The pointer to the ring-0 only (aka global) VM structure. */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync PGVM pGVM;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /** The ring-0 mapping of the shared VM instance data. */
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync PVM pVM;
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync /** The virtual machine object. */
787c7e6ef15f993c455c374f5158e7bb753b1c33vboxsync void *pvObj;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /** The session this VM is associated with. */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync PSUPDRVSESSION pSession;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /** The ring-0 handle of the EMT thread.
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * This is used for assertions and similar cases where we need to find the VM handle. */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync RTNATIVETHREAD hEMT;
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync} GVMHANDLE;
787c7e6ef15f993c455c374f5158e7bb753b1c33vboxsync/** Pointer to a global VM handle. */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsynctypedef GVMHANDLE *PGVMHANDLE;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync/**
787c7e6ef15f993c455c374f5158e7bb753b1c33vboxsync * The GVMM instance data.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsynctypedef struct GVMM
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync{
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /** Eyecatcher / magic. */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync uint32_t u32Magic;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /** The index of the head of the free handle chain. (0 is nil.) */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync uint16_t volatile iFreeHead;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /** The index of the head of the active handle chain. (0 is nil.) */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync uint16_t volatile iUsedHead;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /** The number of VMs. */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync uint16_t volatile cVMs;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync// /** The number of halted EMT threads. */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync// uint16_t volatile cHaltedEMTs;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /** The lock used to serialize VM creation, destruction and associated events that
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * isn't performance critical. Owners may acquire the list lock. */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync RTSEMFASTMUTEX CreateDestroyLock;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /** The lock used to serialize used list updates and accesses.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * This indirectly includes scheduling since the scheduler will have to walk the
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * used list to examin running VMs. Owners may not acquire any other locks. */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync RTSEMFASTMUTEX UsedLock;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /** The handle array.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * The size of this array defines the maximum number of currently running VMs.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * The first entry is unused as it represents the NIL handle. */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync GVMHANDLE aHandles[128];
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /** The number of VMs that means we no longer considers ourselves along on a CPU/Core.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @gcfgm /GVMM/cVMsMeansCompany 32-bit 0..UINT32_MAX
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync uint32_t cVMsMeansCompany;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /** The minimum sleep time for when we're alone, in nano seconds.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @gcfgm /GVMM/MinSleepAlone 32-bit 0..100000000
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync uint32_t nsMinSleepAlone;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /** The minimum sleep time for when we've got company, in nano seconds.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @gcfgm /GVMM/MinSleepCompany 32-bit 0..100000000
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync uint32_t nsMinSleepCompany;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /** The limit for the first round of early wakeups, given in nano seconds.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @gcfgm /GVMM/EarlyWakeUp1 32-bit 0..100000000
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync uint32_t nsEarlyWakeUp1;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /** The limit for the second round of early wakeups, given in nano seconds.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @gcfgm /GVMM/EarlyWakeUp2 32-bit 0..100000000
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync uint32_t nsEarlyWakeUp2;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync} GVMM;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync/** Pointer to the GVMM instance data. */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsynctypedef GVMM *PGVMM;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync/** The GVMM::u32Magic value (Charlie Haden). */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync#define GVMM_MAGIC 0x19370806
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync/*******************************************************************************
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync* Global Variables *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync*******************************************************************************/
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync/** Pointer to the GVMM instance data.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * (Just my general dislike for global variables.) */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsyncstatic PGVMM g_pGVMM = NULL;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync/** Macro for obtaining and validating the g_pGVMM pointer.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * On failure it will return from the invoking function with the specified return value.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @param pGVMM The name of the pGVMM variable.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @param rc The return value on failure. Use VERR_INTERNAL_ERROR for
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * VBox status codes.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
25747178cb66800d8386c20b8ffd87f78f24f4e5vboxsync#define GVMM_GET_VALID_INSTANCE(pGVMM, rc) \
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync do { \
25747178cb66800d8386c20b8ffd87f78f24f4e5vboxsync (pGVMM) = g_pGVMM;\
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertPtrReturn((pGVMM), (rc)); \
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertMsgReturn((pGVMM)->u32Magic == GVMM_MAGIC, ("%p - %#x\n", (pGVMM), (pGVMM)->u32Magic), (rc)); \
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync } while (0)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync/** Macro for obtaining and validating the g_pGVMM pointer, void function variant.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * On failure it will return from the invoking function.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @param pGVMM The name of the pGVMM variable.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync#define GVMM_GET_VALID_INSTANCE_VOID(pGVMM) \
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync do { \
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync (pGVMM) = g_pGVMM;\
25747178cb66800d8386c20b8ffd87f78f24f4e5vboxsync AssertPtrReturnVoid((pGVMM)); \
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertMsgReturnVoid((pGVMM)->u32Magic == GVMM_MAGIC, ("%p - %#x\n", (pGVMM), (pGVMM)->u32Magic)); \
25747178cb66800d8386c20b8ffd87f78f24f4e5vboxsync } while (0)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync/*******************************************************************************
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync* Internal Functions *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync*******************************************************************************/
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsyncstatic void gvmmR0InitPerVMData(PGVM pGVM);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsyncstatic DECLCALLBACK(void) gvmmR0HandleObjDestructor(void *pvObj, void *pvGVMM, void *pvHandle);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsyncstatic int gvmmR0ByVM(PVM pVM, PGVM *ppGVM, PGVMM *ppGVMM, bool fTakeUsedLock);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsyncstatic int gvmmR0ByVMAndEMT(PVM pVM, PGVM *ppGVM, PGVMM *ppGVMM);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync/**
25747178cb66800d8386c20b8ffd87f78f24f4e5vboxsync * Initializes the GVMM.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *
25747178cb66800d8386c20b8ffd87f78f24f4e5vboxsync * This is called while owninng the loader sempahore (see supdrvIOCtl_LdrLoad()).
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @returns VBox status code.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsyncGVMMR0DECL(int) GVMMR0Init(void)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync{
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync LogFlow(("GVMMR0Init:\n"));
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /*
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * Allocate and initialize the instance data.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync PGVMM pGVMM = (PGVMM)RTMemAllocZ(sizeof(*pGVMM));
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (!pGVMM)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync return VERR_NO_MEMORY;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync int rc = RTSemFastMutexCreate(&pGVMM->CreateDestroyLock);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (RT_SUCCESS(rc))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync {
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync rc = RTSemFastMutexCreate(&pGVMM->UsedLock);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (RT_SUCCESS(rc))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync {
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->u32Magic = GVMM_MAGIC;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->iUsedHead = 0;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->iFreeHead = 1;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /* the nil handle */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->aHandles[0].iSelf = 0;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->aHandles[0].iNext = 0;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /* the tail */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync unsigned i = RT_ELEMENTS(pGVMM->aHandles);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->aHandles[i].iSelf = i;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->aHandles[i].iNext = 0; /* nil */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /* the rest */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync while (i-- > 1)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync {
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->aHandles[i].iSelf = i;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->aHandles[i].iNext = i + 1;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /* The default configuration values. */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->cVMsMeansCompany = 1; /** @todo should be adjusted to relative to the cpu count or something... */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->nsMinSleepAlone = 750000 /* ns (0.750 ms) */; /** @todo this should be adjusted to be 75% (or something) of the scheduler granularity... */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->nsMinSleepCompany = 15000 /* ns (0.015 ms) */;
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync pGVMM->nsEarlyWakeUp1 = 25000 /* ns (0.025 ms) */;
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync pGVMM->nsEarlyWakeUp2 = 50000 /* ns (0.050 ms) */;
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync g_pGVMM = pGVMM;
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync LogFlow(("GVMMR0Init: pGVMM=%p\n", pGVMM));
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync return VINF_SUCCESS;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync RTSemFastMutexDestroy(pGVMM->CreateDestroyLock);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync RTMemFree(pGVMM);
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync return rc;
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync}
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync/**
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync * Terminates the GVM.
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync *
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync * This is called while owning the loader semaphore (see supdrvLdrFree()).
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync * And unless something is wrong, there should be absolutely no VMs
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync * registered at this point.
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync */
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsyncGVMMR0DECL(void) GVMMR0Term(void)
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync{
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync LogFlow(("GVMMR0Term:\n"));
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync PGVMM pGVMM = g_pGVMM;
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync g_pGVMM = NULL;
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync if (RT_UNLIKELY(!VALID_PTR(pGVMM)))
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync {
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync SUPR0Printf("GVMMR0Term: pGVMM=%p\n", pGVMM);
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync return;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync pGVMM->u32Magic++;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync RTSemFastMutexDestroy(pGVMM->UsedLock);
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync pGVMM->UsedLock = NIL_RTSEMFASTMUTEX;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync RTSemFastMutexDestroy(pGVMM->CreateDestroyLock);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->CreateDestroyLock = NIL_RTSEMFASTMUTEX;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->iFreeHead = 0;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (pGVMM->iUsedHead)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync {
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync SUPR0Printf("GVMMR0Term: iUsedHead=%#x! (cVMs=%#x)\n", pGVMM->iUsedHead, pGVMM->cVMs);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->iUsedHead = 0;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync RTMemFree(pGVMM);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync}
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync/**
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * A quick hack for setting global config values.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @returns VBox status code.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @param pSession The session handle. Used for authentication.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @param pszName The variable name.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @param u64Value The new value.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsyncGVMMR0DECL(int) GVMMR0SetConfig(PSUPDRVSESSION pSession, const char *pszName, uint64_t u64Value)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync{
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /*
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * Validate input.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync PGVMM pGVMM;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertPtrReturn(pSession, VERR_INVALID_HANDLE);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertPtrReturn(pszName, VERR_INVALID_POINTER);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /*
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * String switch time!
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (strncmp(pszName, "/GVMM/", sizeof("/GVMM/") - 1))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync return VERR_CFGM_VALUE_NOT_FOUND; /* borrow status codes from CFGM... */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync int rc = VINF_SUCCESS;
fbf08fabb4c4b383d6aa2830c2bd5b943a26f10cvboxsync pszName += sizeof("/GVMM/") - 1;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (!strcmp(pszName, "cVMsMeansCompany"))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync {
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (u64Value <= UINT32_MAX)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->cVMsMeansCompany = u64Value;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync else
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync rc = VERR_OUT_OF_RANGE;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync else if (!strcmp(pszName, "MinSleepAlone"))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync {
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (u64Value <= 100000000)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->nsMinSleepAlone = u64Value;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync else
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync rc = VERR_OUT_OF_RANGE;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
fbf08fabb4c4b383d6aa2830c2bd5b943a26f10cvboxsync else if (!strcmp(pszName, "MinSleepCompany"))
fbf08fabb4c4b383d6aa2830c2bd5b943a26f10cvboxsync {
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (u64Value <= 100000000)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->nsMinSleepCompany = u64Value;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync else
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync rc = VERR_OUT_OF_RANGE;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync else if (!strcmp(pszName, "EarlyWakeUp1"))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync {
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (u64Value <= 100000000)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->nsEarlyWakeUp1 = u64Value;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync else
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync rc = VERR_OUT_OF_RANGE;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync else if (!strcmp(pszName, "EarlyWakeUp2"))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync {
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (u64Value <= 100000000)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->nsEarlyWakeUp2 = u64Value;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync else
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync rc = VERR_OUT_OF_RANGE;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync else
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync rc = VERR_CFGM_VALUE_NOT_FOUND;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync return rc;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync}
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync/**
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * A quick hack for getting global config values.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @returns VBox status code.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @param pSession The session handle. Used for authentication.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @param pszName The variable name.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @param u64Value The new value.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsyncGVMMR0DECL(int) GVMMR0QueryConfig(PSUPDRVSESSION pSession, const char *pszName, uint64_t *pu64Value)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync{
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /*
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * Validate input.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync PGVMM pGVMM;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertPtrReturn(pSession, VERR_INVALID_HANDLE);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertPtrReturn(pszName, VERR_INVALID_POINTER);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertPtrReturn(pu64Value, VERR_INVALID_POINTER);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /*
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * String switch time!
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (strncmp(pszName, "/GVMM/", sizeof("/GVMM/") - 1))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync return VERR_CFGM_VALUE_NOT_FOUND; /* borrow status codes from CFGM... */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync int rc = VINF_SUCCESS;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pszName += sizeof("/GVMM/") - 1;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (!strcmp(pszName, "cVMsMeansCompany"))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *pu64Value = pGVMM->cVMsMeansCompany;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync else if (!strcmp(pszName, "MinSleepAlone"))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *pu64Value = pGVMM->nsMinSleepAlone;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync else if (!strcmp(pszName, "MinSleepCompany"))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *pu64Value = pGVMM->nsMinSleepCompany;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync else if (!strcmp(pszName, "EarlyWakeUp1"))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *pu64Value = pGVMM->nsEarlyWakeUp1;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync else if (!strcmp(pszName, "EarlyWakeUp2"))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *pu64Value = pGVMM->nsEarlyWakeUp2;
fbf08fabb4c4b383d6aa2830c2bd5b943a26f10cvboxsync else
a7d402dcc23137e7b9527be6de80400043a5fbf4vboxsync rc = VERR_CFGM_VALUE_NOT_FOUND;
fbf08fabb4c4b383d6aa2830c2bd5b943a26f10cvboxsync return rc;
a7d402dcc23137e7b9527be6de80400043a5fbf4vboxsync}
fbf08fabb4c4b383d6aa2830c2bd5b943a26f10cvboxsync
fbf08fabb4c4b383d6aa2830c2bd5b943a26f10cvboxsync
fbf08fabb4c4b383d6aa2830c2bd5b943a26f10cvboxsync/**
fbf08fabb4c4b383d6aa2830c2bd5b943a26f10cvboxsync * Request wrapper for the GVMMR0CreateVM API.
fbf08fabb4c4b383d6aa2830c2bd5b943a26f10cvboxsync *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @returns VBox status code.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @param pReq The request buffer.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsyncGVMMR0DECL(int) GVMMR0CreateVMReq(PGVMMCREATEVMREQ pReq)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync{
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /*
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * Validate the request.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (!VALID_PTR(pReq))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync return VERR_INVALID_POINTER;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (pReq->Hdr.cbReq != sizeof(*pReq))
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync return VERR_INVALID_PARAMETER;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (!VALID_PTR(pReq->pSession))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync return VERR_INVALID_POINTER;
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /*
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * Execute it.
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync PVM pVM;
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync pReq->pVMR0 = NULL;
a39ea3668b7019c23a68936259545f9b71bce1aavboxsync pReq->pVMR3 = NIL_RTR3PTR;
787c7e6ef15f993c455c374f5158e7bb753b1c33vboxsync int rc = GVMMR0CreateVM(pReq->pSession, &pVM);
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync if (RT_SUCCESS(rc))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync {
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pReq->pVMR0 = pVM;
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync pReq->pVMR3 = pVM->pVMR3;
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync }
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync return rc;
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync}
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync/**
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync * Allocates the VM structure and registers it with GVM.
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync *
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync * @returns VBox status code.
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync * @param pSession The support driver session.
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync * @param ppVM Where to store the pointer to the VM structure.
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync *
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync * @thread Any thread.
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync */
e74eef731a813e4e06680c587a6759b9974b29c9vboxsyncGVMMR0DECL(int) GVMMR0CreateVM(PSUPDRVSESSION pSession, PVM *ppVM)
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync{
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync LogFlow(("GVMMR0CreateVM: pSession=%p\n", pSession));
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync PGVMM pGVMM;
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync AssertPtrReturn(ppVM, VERR_INVALID_POINTER);
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync *ppVM = NULL;
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync AssertReturn(RTThreadNativeSelf() != NIL_RTNATIVETHREAD, VERR_INTERNAL_ERROR);
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync /*
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync * The whole allocation process is protected by the lock.
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync */
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync int rc = RTSemFastMutexRequest(pGVMM->CreateDestroyLock);
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync AssertRCReturn(rc, rc);
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync /*
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync * Allocate a handle first so we don't waste resources unnecessarily.
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync */
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync uint16_t iHandle = pGVMM->iFreeHead;
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync if (iHandle)
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync {
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync PGVMHANDLE pHandle = &pGVMM->aHandles[iHandle];
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
cad8876b46f9e366c4a1007a40c27ca1df078950vboxsync /* consistency checks, a bit paranoid as always. */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if ( !pHandle->pVM
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync && !pHandle->pGVM
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsync && !pHandle->pvObj
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync && pHandle->iSelf == iHandle)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync {
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pHandle->pvObj = SUPR0ObjRegister(pSession, SUPDRVOBJTYPE_VM, gvmmR0HandleObjDestructor, pGVMM, pHandle);
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsync if (pHandle->pvObj)
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsync {
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /*
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * Move the handle from the free to used list and perform permission checks.
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync rc = RTSemFastMutexRequest(pGVMM->UsedLock);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertRC(rc);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->iFreeHead = pHandle->iNext;
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsync pHandle->iNext = pGVMM->iUsedHead;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->iUsedHead = iHandle;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVMM->cVMs++;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pHandle->pVM = NULL;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pHandle->pGVM = NULL;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pHandle->pSession = pSession;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pHandle->hEMT = NIL_RTNATIVETHREAD;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync RTSemFastMutexRelease(pGVMM->UsedLock);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync rc = SUPR0ObjVerifyAccess(pHandle->pvObj, pSession, NULL);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (RT_SUCCESS(rc))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync {
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /*
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * Allocate the global VM structure (GVM) and initialize it.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
787c7e6ef15f993c455c374f5158e7bb753b1c33vboxsync PGVM pGVM = (PGVM)RTMemAllocZ(sizeof(*pGVM));
fbf08fabb4c4b383d6aa2830c2bd5b943a26f10cvboxsync if (pGVM)
fbf08fabb4c4b383d6aa2830c2bd5b943a26f10cvboxsync {
a7d402dcc23137e7b9527be6de80400043a5fbf4vboxsync pGVM->u32Magic = GVM_MAGIC;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVM->hSelf = iHandle;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVM->hEMT = NIL_RTNATIVETHREAD;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVM->pVM = NULL;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsync gvmmR0InitPerVMData(pGVM);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /* GMMR0InitPerVMData(pGVM); - later */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /*
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsync * Allocate the shared VM structure and associated page array.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync const size_t cPages = RT_ALIGN(sizeof(VM), PAGE_SIZE) >> PAGE_SHIFT;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync rc = RTR0MemObjAllocLow(&pGVM->gvmm.s.VMMemObj, cPages << PAGE_SHIFT, false /* fExecutable */);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (RT_SUCCESS(rc))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync {
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync PVM pVM = (PVM)RTR0MemObjAddress(pGVM->gvmm.s.VMMemObj); AssertPtr(pVM);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync memset(pVM, 0, cPages << PAGE_SHIFT);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pVM->enmVMState = VMSTATE_CREATING;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pVM->pVMR0 = pVM;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pVM->pSession = pSession;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pVM->hSelf = iHandle;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync rc = RTR0MemObjAllocPage(&pGVM->gvmm.s.VMPagesMemObj, cPages * sizeof(SUPPAGE), false /* fExecutable */);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (RT_SUCCESS(rc))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync {
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync PSUPPAGE paPages = (PSUPPAGE)RTR0MemObjAddress(pGVM->gvmm.s.VMPagesMemObj); AssertPtr(paPages);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync for (size_t iPage = 0; iPage < cPages; iPage++)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync {
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync paPages[iPage].uReserved = 0;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync paPages[iPage].Phys = RTR0MemObjGetPagePhysAddr(pGVM->gvmm.s.VMMemObj, iPage);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync Assert(paPages[iPage].Phys != NIL_RTHCPHYS);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /*
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * Map them into ring-3.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync rc = RTR0MemObjMapUser(&pGVM->gvmm.s.VMMapObj, pGVM->gvmm.s.VMMemObj, (RTR3PTR)-1, 0,
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (RT_SUCCESS(rc))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync {
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pVM->pVMR3 = RTR0MemObjAddressR3(pGVM->gvmm.s.VMMapObj);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertPtr((void *)pVM->pVMR3);
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync rc = RTR0MemObjMapUser(&pGVM->gvmm.s.VMPagesMapObj, pGVM->gvmm.s.VMPagesMemObj, (RTR3PTR)-1, 0,
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (RT_SUCCESS(rc))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync {
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pVM->paVMPagesR3 = RTR0MemObjAddressR3(pGVM->gvmm.s.VMPagesMapObj);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertPtr((void *)pVM->paVMPagesR3);
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /* complete the handle - take the UsedLock sem just to be careful. */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync rc = RTSemFastMutexRequest(pGVMM->UsedLock);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertRC(rc);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pHandle->pVM = pVM;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pHandle->pGVM = pGVM;
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsync pGVM->pVM = pVM;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync RTSemFastMutexRelease(pGVMM->UsedLock);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *ppVM = pVM;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync Log(("GVMMR0CreateVM: pVM=%p pVMR3=%p pGVM=%p hGVM=%d\n", pVM, pVM->pVMR3, pGVM, iHandle));
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync return VINF_SUCCESS;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync RTR0MemObjFree(pGVM->gvmm.s.VMMapObj, false /* fFreeMappings */);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVM->gvmm.s.VMMapObj = NIL_RTR0MEMOBJ;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync RTR0MemObjFree(pGVM->gvmm.s.VMPagesMemObj, false /* fFreeMappings */);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVM->gvmm.s.VMPagesMemObj = NIL_RTR0MEMOBJ;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync RTR0MemObjFree(pGVM->gvmm.s.VMMemObj, false /* fFreeMappings */);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVM->gvmm.s.VMMemObj = NIL_RTR0MEMOBJ;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /* else: The user wasn't permitted to create this VM. */
787c7e6ef15f993c455c374f5158e7bb753b1c33vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /*
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * The handle will be freed by gvmmR0HandleObjDestructor as we release the
787c7e6ef15f993c455c374f5158e7bb753b1c33vboxsync * object reference here. A little extra mess because of non-recursive lock.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync void *pvObj = pHandle->pvObj;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pHandle->pvObj = NULL;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync SUPR0ObjRelease(pvObj, pSession);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync SUPR0Printf("GVMMR0CreateVM: failed, rc=%d\n", rc);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync return rc;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync rc = VERR_NO_MEMORY;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync else
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync rc = VERR_INTERNAL_ERROR;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync else
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync rc = VERR_GVM_TOO_MANY_VMS;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync return rc;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync}
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync/**
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * Initializes the per VM data belonging to GVMM.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @param pGVM Pointer to the global VM structure.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsyncstatic void gvmmR0InitPerVMData(PGVM pGVM)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync{
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertCompile(RT_SIZEOFMEMB(GVM,gvmm.s) <= RT_SIZEOFMEMB(GVM,gvmm.padding));
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync Assert(RT_SIZEOFMEMB(GVM,gvmm.s) <= RT_SIZEOFMEMB(GVM,gvmm.padding));
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVM->gvmm.s.VMMemObj = NIL_RTR0MEMOBJ;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVM->gvmm.s.VMMapObj = NIL_RTR0MEMOBJ;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVM->gvmm.s.VMPagesMemObj = NIL_RTR0MEMOBJ;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVM->gvmm.s.VMPagesMapObj = NIL_RTR0MEMOBJ;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVM->gvmm.s.HaltEventMulti = NIL_RTSEMEVENTMULTI;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync}
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync/**
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * Associates an EMT thread with a VM.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * This is called early during the ring-0 VM initialization so assertions later in
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * the process can be handled gracefully.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @returns VBox status code.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @param pVM The VM instance data (aka handle), ring-0 mapping of course.
25747178cb66800d8386c20b8ffd87f78f24f4e5vboxsync * @thread EMT.
25747178cb66800d8386c20b8ffd87f78f24f4e5vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsyncGVMMR0DECL(int) GVMMR0AssociateEMTWithVM(PVM pVM)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync{
25747178cb66800d8386c20b8ffd87f78f24f4e5vboxsync LogFlow(("GVMMR0AssociateEMTWithVM: pVM=%p\n", pVM));
25747178cb66800d8386c20b8ffd87f78f24f4e5vboxsync PGVMM pGVMM;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /*
25747178cb66800d8386c20b8ffd87f78f24f4e5vboxsync * Validate the VM structure, state and handle.
25747178cb66800d8386c20b8ffd87f78f24f4e5vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertPtrReturn(pVM, VERR_INVALID_POINTER);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertReturn(!((uintptr_t)pVM & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertMsgReturn(pVM->enmVMState == VMSTATE_CREATING, ("%d\n", pVM->enmVMState), VERR_WRONG_ORDER);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync RTNATIVETHREAD hEMT = RTThreadNativeSelf();
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertReturn(hEMT != NIL_RTNATIVETHREAD, VERR_NOT_SUPPORTED);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync const uint16_t hGVM = pVM->hSelf;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertReturn(hGVM != NIL_GVM_HANDLE, VERR_INVALID_HANDLE);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), VERR_INVALID_HANDLE);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertReturn(pHandle->pVM == pVM, VERR_NOT_OWNER);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /*
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * Take the lock, validate the handle and update the structure members.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync int rc = RTSemFastMutexRequest(pGVMM->CreateDestroyLock);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertRCReturn(rc, rc);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync rc = RTSemFastMutexRequest(pGVMM->UsedLock);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertRC(rc);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if ( pHandle->pVM == pVM
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync && VALID_PTR(pHandle->pvObj)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync && VALID_PTR(pHandle->pSession)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync && VALID_PTR(pHandle->pGVM)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync && pHandle->pGVM->u32Magic == GVM_MAGIC)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync {
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pHandle->hEMT = hEMT;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pHandle->pGVM->hEMT = hEMT;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync else
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync rc = VERR_INTERNAL_ERROR;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync RTSemFastMutexRelease(pGVMM->UsedLock);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync LogFlow(("GVMMR0AssociateEMTWithVM: returns %Vrc (hEMT=%RTnthrd)\n", rc, hEMT));
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync return rc;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync}
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync/**
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * Does the VM initialization.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @returns VBox status code.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @param pVM Pointer to the shared VM structure.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsyncGVMMR0DECL(int) GVMMR0InitVM(PVM pVM)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync{
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync LogFlow(("GVMMR0InitVM: pVM=%p\n", pVM));
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /*
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * Validate the VM structure, state and handle.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync PGVM pGVM;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync PGVMM pGVMM;
2c8ee291fb75c4a6f05df160f5d67f4e9ef1cabcvboxsync int rc = gvmmR0ByVMAndEMT(pVM, &pGVM, &pGVMM);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (RT_SUCCESS(rc))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync {
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (pGVM->gvmm.s.HaltEventMulti == NIL_RTSEMEVENTMULTI)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync {
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync rc = RTSemEventMultiCreate(&pGVM->gvmm.s.HaltEventMulti);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (RT_FAILURE(rc))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync pGVM->gvmm.s.HaltEventMulti = NIL_RTSEMEVENTMULTI;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync else
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync rc = VERR_WRONG_ORDER;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync }
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync LogFlow(("GVMMR0InitVM: returns %Rrc\n", rc));
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync return rc;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync}
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync/**
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * Disassociates the EMT thread from a VM.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * This is called last in the ring-0 VM termination. After this point anyone is
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * allowed to destroy the VM. Ideally, we should associate the VM with the thread
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * that's going to call GVMMR0DestroyVM for optimal security, but that's impractical
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * at present.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @returns VBox status code.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync *
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @param pVM The VM instance data (aka handle), ring-0 mapping of course.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * @thread EMT.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsyncGVMMR0DECL(int) GVMMR0DisassociateEMTFromVM(PVM pVM)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync{
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync LogFlow(("GVMMR0DisassociateEMTFromVM: pVM=%p\n", pVM));
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync PGVMM pGVMM;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /*
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * Validate the VM structure, state and handle.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertPtrReturn(pVM, VERR_INVALID_POINTER);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertReturn(!((uintptr_t)pVM & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertMsgReturn(pVM->enmVMState >= VMSTATE_CREATING && pVM->enmVMState <= VMSTATE_DESTROYING, ("%d\n", pVM->enmVMState), VERR_WRONG_ORDER);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync RTNATIVETHREAD hEMT = RTThreadNativeSelf();
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync AssertReturn(hEMT != NIL_RTNATIVETHREAD, VERR_NOT_SUPPORTED);
const uint16_t hGVM = pVM->hSelf;
AssertReturn(hGVM != NIL_GVM_HANDLE, VERR_INVALID_HANDLE);
AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), VERR_INVALID_HANDLE);
PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
AssertReturn(pHandle->pVM == pVM, VERR_NOT_OWNER);
/*
* Take the lock, validate the handle and update the structure members.
*/
int rc = RTSemFastMutexRequest(pGVMM->CreateDestroyLock);
AssertRCReturn(rc, rc);
rc = RTSemFastMutexRequest(pGVMM->UsedLock);
AssertRC(rc);
if ( VALID_PTR(pHandle->pvObj)
&& VALID_PTR(pHandle->pSession)
&& VALID_PTR(pHandle->pGVM)
&& pHandle->pGVM->u32Magic == GVM_MAGIC)
{
if ( pHandle->pVM == pVM
&& pHandle->hEMT == hEMT)
{
pHandle->hEMT = NIL_RTNATIVETHREAD;
pHandle->pGVM->hEMT = NIL_RTNATIVETHREAD;
}
else
rc = VERR_NOT_OWNER;
}
else
rc = VERR_INVALID_HANDLE;
RTSemFastMutexRelease(pGVMM->UsedLock);
RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
LogFlow(("GVMMR0DisassociateEMTFromVM: returns %Vrc (hEMT=%RTnthrd)\n", rc, hEMT));
return rc;
}
/**
* Destroys the VM, freeing all associated resources (the ring-0 ones anyway).
*
* This is call from the vmR3DestroyFinalBit and from a error path in VMR3Create,
* and the caller is not the EMT thread, unfortunately. For security reasons, it
* would've been nice if the caller was actually the EMT thread or that we somehow
* could've associated the calling thread with the VM up front.
*
* @returns VBox status code.
* @param pVM Where to store the pointer to the VM structure.
*
* @thread EMT if it's associated with the VM, otherwise any thread.
*/
GVMMR0DECL(int) GVMMR0DestroyVM(PVM pVM)
{
LogFlow(("GVMMR0DestroyVM: pVM=%p\n", pVM));
PGVMM pGVMM;
GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
/*
* Validate the VM structure, state and caller.
*/
AssertPtrReturn(pVM, VERR_INVALID_POINTER);
AssertReturn(!((uintptr_t)pVM & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
AssertMsgReturn(pVM->enmVMState >= VMSTATE_CREATING && pVM->enmVMState <= VMSTATE_TERMINATED, ("%d\n", pVM->enmVMState), VERR_WRONG_ORDER);
uint32_t hGVM = pVM->hSelf;
AssertReturn(hGVM != NIL_GVM_HANDLE, VERR_INVALID_HANDLE);
AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), VERR_INVALID_HANDLE);
PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
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 = RTSemFastMutexRequest(pGVMM->CreateDestroyLock);
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;
RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
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);
RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
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 = RTSemFastMutexRequest(pGVMM->CreateDestroyLock);
AssertRC(rc);
rc = RTSemFastMutexRequest(pGVMM->UsedLock);
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);
RTSemFastMutexRelease(pGVMM->UsedLock);
RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
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");
RTSemFastMutexRelease(pGVMM->UsedLock);
RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
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);
RTSemFastMutexRelease(pGVMM->UsedLock);
RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
return;
}
pGVMM->aHandles[iPrev].iNext = pHandle->iNext;
}
pHandle->iNext = 0;
pGVMM->cVMs--;
RTSemFastMutexRelease(pGVMM->UsedLock);
/*
* 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;
}
/* 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 = RTSemFastMutexRequest(pGVMM->UsedLock);
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);
RTSemFastMutexRelease(pGVMM->UsedLock);
RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
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 = RTSemFastMutexRequest(pGVMM->UsedLock);
AssertRCReturn(rc, rc);
pGVM = pHandle->pGVM;
if (RT_UNLIKELY( pHandle->pVM != pVM
|| !VALID_PTR(pHandle->pvObj)
|| !VALID_PTR(pGVM)
|| pGVM->pVM != pVM))
{
RTSemFastMutexRelease(pGVMM->UsedLock);
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).
*/
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;
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++;
}
}
}
}
if (cTodo2nd)
{
for (unsigned i = pGVMM->iUsedHead;
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++;
}
}
}
}
if (cTodo3rd)
{
for (unsigned i = pGVMM->iUsedHead;
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++;
}
}
}
}
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(("GVMMR0DisassociateEMTFromVM: 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 = RTSemFastMutexRequest(pGVMM->UsedLock);
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);
RTSemFastMutexRelease(pGVMM->UsedLock);
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++;
RTSemFastMutexRelease(pGVMM->UsedLock);
}
/* 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 = RTSemFastMutexRelease(pGVMM->UsedLock);
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 = RTSemFastMutexRequest(pGVMM->UsedLock);
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;
}
RTSemFastMutexRelease(pGVMM->UsedLock);
}
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 = RTSemFastMutexRequest(pGVMM->UsedLock);
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;
}
}
RTSemFastMutexRelease(pGVMM->UsedLock);
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 = RTSemFastMutexRequest(pGVMM->UsedLock);
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
}
}
}
RTSemFastMutexRelease(pGVMM->UsedLock);
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);
}