VM.cpp revision 65bee08da2ae66d098423e06e27ad06f69e4aa63
05afe08870681beb0792f384475077c988916762vboxsync/* $Id$ */
05afe08870681beb0792f384475077c988916762vboxsync/** @file
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * VM - Virtual Machine
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync/*
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
05afe08870681beb0792f384475077c988916762vboxsync *
05afe08870681beb0792f384475077c988916762vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
05afe08870681beb0792f384475077c988916762vboxsync * available from http://www.virtualbox.org. This file is free software;
05afe08870681beb0792f384475077c988916762vboxsync * you can redistribute it and/or modify it under the terms of the GNU
05afe08870681beb0792f384475077c988916762vboxsync * General Public License (GPL) as published by the Free Software
05afe08870681beb0792f384475077c988916762vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
05afe08870681beb0792f384475077c988916762vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
05afe08870681beb0792f384475077c988916762vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
05afe08870681beb0792f384475077c988916762vboxsync *
05afe08870681beb0792f384475077c988916762vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
05afe08870681beb0792f384475077c988916762vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
05afe08870681beb0792f384475077c988916762vboxsync * additional information or have any questions.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync/** @page pg_vm VM API
05afe08870681beb0792f384475077c988916762vboxsync *
05afe08870681beb0792f384475077c988916762vboxsync * This is the encapsulating bit. It provides the APIs that Main and VBoxBFE
05afe08870681beb0792f384475077c988916762vboxsync * use to create a VMM instance for running a guest in. It also provides
05afe08870681beb0792f384475077c988916762vboxsync * facilities for queuing request for execution in EMT (serialization purposes
05afe08870681beb0792f384475077c988916762vboxsync * mostly) and for reporting error back to the VMM user (Main/VBoxBFE).
05afe08870681beb0792f384475077c988916762vboxsync *
05afe08870681beb0792f384475077c988916762vboxsync *
05afe08870681beb0792f384475077c988916762vboxsync * @section sec_vm_design Design Critique / Things To Do
05afe08870681beb0792f384475077c988916762vboxsync *
05afe08870681beb0792f384475077c988916762vboxsync * In hindsight this component is a big design mistake, all this stuff really
05afe08870681beb0792f384475077c988916762vboxsync * belongs in the VMM component. It just seemed like a kind of ok idea at a
05afe08870681beb0792f384475077c988916762vboxsync * time when the VMM bit was a bit vague. 'VM' also happend to be the name of
05afe08870681beb0792f384475077c988916762vboxsync * the per-VM instance structure (see vm.h), so it kind of made sense. However
05afe08870681beb0792f384475077c988916762vboxsync * as it turned out, VMM(.cpp) is almost empty all it provides in ring-3 is some
a8f65e585466d1267633cea76b4f97a69b7f1cc0vboxsync * minor functionally and some "routing" services.
a8f65e585466d1267633cea76b4f97a69b7f1cc0vboxsync *
a8f65e585466d1267633cea76b4f97a69b7f1cc0vboxsync * Fixing this is just a matter of some more or less straight forward
05afe08870681beb0792f384475077c988916762vboxsync * refactoring, the question is just when someone will get to it.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync *
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
a8f65e585466d1267633cea76b4f97a69b7f1cc0vboxsync
05afe08870681beb0792f384475077c988916762vboxsync/*******************************************************************************
05afe08870681beb0792f384475077c988916762vboxsync* Header Files *
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync*******************************************************************************/
05afe08870681beb0792f384475077c988916762vboxsync#define LOG_GROUP LOG_GROUP_VM
05afe08870681beb0792f384475077c988916762vboxsync#include <VBox/cfgm.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <VBox/vmm.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <VBox/gvmm.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <VBox/mm.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <VBox/cpum.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <VBox/selm.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <VBox/trpm.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <VBox/dbgf.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <VBox/pgm.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <VBox/pdmapi.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <VBox/pdmcritsect.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <VBox/em.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <VBox/rem.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <VBox/tm.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <VBox/stam.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <VBox/patm.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#ifdef VBOX_WITH_VMI
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync# include <VBox/parav.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#endif
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <VBox/csam.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <VBox/iom.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <VBox/ssm.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <VBox/hwaccm.h>
05afe08870681beb0792f384475077c988916762vboxsync#include "VMInternal.h"
05afe08870681beb0792f384475077c988916762vboxsync#include <VBox/vm.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <VBox/uvm.h>
05afe08870681beb0792f384475077c988916762vboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <VBox/sup.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <VBox/dbg.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <VBox/err.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <VBox/param.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <VBox/log.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <iprt/assert.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <iprt/alloc.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <iprt/asm.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <iprt/env.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <iprt/string.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <iprt/time.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <iprt/semaphore.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <iprt/thread.h>
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync/*******************************************************************************
05afe08870681beb0792f384475077c988916762vboxsync* Structures and Typedefs *
05afe08870681beb0792f384475077c988916762vboxsync*******************************************************************************/
05afe08870681beb0792f384475077c988916762vboxsync/**
05afe08870681beb0792f384475077c988916762vboxsync * VM destruction callback registration record.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsynctypedef struct VMATDTOR
05afe08870681beb0792f384475077c988916762vboxsync{
05afe08870681beb0792f384475077c988916762vboxsync /** Pointer to the next record in the list. */
05afe08870681beb0792f384475077c988916762vboxsync struct VMATDTOR *pNext;
05afe08870681beb0792f384475077c988916762vboxsync /** Pointer to the callback function. */
05afe08870681beb0792f384475077c988916762vboxsync PFNVMATDTOR pfnAtDtor;
05afe08870681beb0792f384475077c988916762vboxsync /** The user argument. */
05afe08870681beb0792f384475077c988916762vboxsync void *pvUser;
05afe08870681beb0792f384475077c988916762vboxsync} VMATDTOR;
05afe08870681beb0792f384475077c988916762vboxsync/** Pointer to a VM destruction callback registration record. */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsynctypedef VMATDTOR *PVMATDTOR;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync/*******************************************************************************
05afe08870681beb0792f384475077c988916762vboxsync* Global Variables *
05afe08870681beb0792f384475077c988916762vboxsync*******************************************************************************/
05afe08870681beb0792f384475077c988916762vboxsync/** Pointer to the list of VMs. */
05afe08870681beb0792f384475077c988916762vboxsyncstatic PUVM g_pUVMsHead = NULL;
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync/** Pointer to the list of at VM destruction callbacks. */
05afe08870681beb0792f384475077c988916762vboxsyncstatic PVMATDTOR g_pVMAtDtorHead = NULL;
05afe08870681beb0792f384475077c988916762vboxsync/** Lock the g_pVMAtDtorHead list. */
05afe08870681beb0792f384475077c988916762vboxsync#define VM_ATDTOR_LOCK() do { } while (0)
05afe08870681beb0792f384475077c988916762vboxsync/** Unlock the g_pVMAtDtorHead list. */
05afe08870681beb0792f384475077c988916762vboxsync#define VM_ATDTOR_UNLOCK() do { } while (0)
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync/*******************************************************************************
05afe08870681beb0792f384475077c988916762vboxsync* Internal Functions *
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync*******************************************************************************/
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic int vmR3CreateUVM(uint32_t cCpus, PUVM *ppUVM);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic int vmR3CreateU(PUVM pUVM, uint32_t cCpus, PFNCFGMCONSTRUCTOR pfnCFGMConstructor, void *pvUserCFGM);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic int vmR3InitRing3(PVM pVM, PUVM pUVM);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic int vmR3InitVMCpu(PVM pVM);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic int vmR3InitRing0(PVM pVM);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic int vmR3InitGC(PVM pVM);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic int vmR3InitDoCompleted(PVM pVM, VMINITCOMPLETED enmWhat);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic DECLCALLBACK(size_t) vmR3LogPrefixCallback(PRTLOGGER pLogger, char *pchBuf, size_t cchBuf, void *pvUser);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic DECLCALLBACK(int) vmR3PowerOn(PVM pVM);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic int vmR3SuspendCommon(PVM pVM, bool fFatal);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic DECLCALLBACK(int) vmR3Suspend(PVM pVM, bool fFatal);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic DECLCALLBACK(int) vmR3Resume(PVM pVM);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic DECLCALLBACK(int) vmR3Save(PVM pVM, const char *pszFilename, SSMAFTER enmAfter, PFNVMPROGRESS pfnProgress, void *pvUser, PSSMHANDLE *ppSSM);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic DECLCALLBACK(int) vmR3Load(PVM pVM, const char *pszFilename, PFNVMPROGRESS pfnProgress, void *pvUser);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic DECLCALLBACK(int) vmR3PowerOff(PVM pVM);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic void vmR3DestroyUVM(PUVM pUVM, uint32_t cMilliesEMTWait);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic void vmR3AtDtor(PVM pVM);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic int vmR3AtResetU(PUVM pUVM);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic DECLCALLBACK(int) vmR3Reset(PVM pVM);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic DECLCALLBACK(int) vmR3AtStateRegisterU(PUVM pUVM, PFNVMATSTATE pfnAtState, void *pvUser);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic DECLCALLBACK(int) vmR3AtStateDeregisterU(PUVM pUVM, PFNVMATSTATE pfnAtState, void *pvUser);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic DECLCALLBACK(int) vmR3AtErrorRegisterU(PUVM pUVM, PFNVMATERROR pfnAtError, void *pvUser);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic DECLCALLBACK(int) vmR3AtErrorDeregisterU(PUVM pUVM, PFNVMATERROR pfnAtError, void *pvUser);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic int vmR3SetErrorU(PUVM pUVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, ...);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic DECLCALLBACK(int) vmR3AtRuntimeErrorRegisterU(PUVM pUVM, PFNVMATRUNTIMEERROR pfnAtRuntimeError, void *pvUser);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic DECLCALLBACK(int) vmR3AtRuntimeErrorDeregisterU(PUVM pUVM, PFNVMATRUNTIMEERROR pfnAtRuntimeError, void *pvUser);
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync/**
05afe08870681beb0792f384475077c988916762vboxsync * Do global VMM init.
05afe08870681beb0792f384475077c988916762vboxsync *
05afe08870681beb0792f384475077c988916762vboxsync * @returns VBox status code.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsyncVMMR3DECL(int) VMR3GlobalInit(void)
05afe08870681beb0792f384475077c988916762vboxsync{
05afe08870681beb0792f384475077c988916762vboxsync /*
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Only once.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync static bool volatile s_fDone = false;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (s_fDone)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync return VINF_SUCCESS;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /*
05afe08870681beb0792f384475077c988916762vboxsync * We're done.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync s_fDone = true;
05afe08870681beb0792f384475077c988916762vboxsync return VINF_SUCCESS;
05afe08870681beb0792f384475077c988916762vboxsync}
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
05afe08870681beb0792f384475077c988916762vboxsync/**
05afe08870681beb0792f384475077c988916762vboxsync * Creates a virtual machine by calling the supplied configuration constructor.
05afe08870681beb0792f384475077c988916762vboxsync *
05afe08870681beb0792f384475077c988916762vboxsync * On successful returned the VM is powered, i.e. VMR3PowerOn() should be
05afe08870681beb0792f384475077c988916762vboxsync * called to start the execution.
05afe08870681beb0792f384475077c988916762vboxsync *
05afe08870681beb0792f384475077c988916762vboxsync * @returns 0 on success.
05afe08870681beb0792f384475077c988916762vboxsync * @returns VBox error code on failure.
05afe08870681beb0792f384475077c988916762vboxsync * @param cCpus Number of virtual CPUs for the new VM.
05afe08870681beb0792f384475077c988916762vboxsync * @param pfnVMAtError Pointer to callback function for setting VM
05afe08870681beb0792f384475077c988916762vboxsync * errors. This was added as an implicit call to
05afe08870681beb0792f384475077c988916762vboxsync * VMR3AtErrorRegister() since there is no way the
05afe08870681beb0792f384475077c988916762vboxsync * caller can get to the VM handle early enough to
05afe08870681beb0792f384475077c988916762vboxsync * do this on its own.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * This is called in the context of an EMT.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * @param pvUserVM The user argument passed to pfnVMAtError.
05afe08870681beb0792f384475077c988916762vboxsync * @param pfnCFGMConstructor Pointer to callback function for constructing the VM configuration tree.
05afe08870681beb0792f384475077c988916762vboxsync * This is called in the context of an EMT0.
05afe08870681beb0792f384475077c988916762vboxsync * @param pvUserCFGM The user argument passed to pfnCFGMConstructor.
05afe08870681beb0792f384475077c988916762vboxsync * @param ppVM Where to store the 'handle' of the created VM.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncVMMR3DECL(int) VMR3Create(uint32_t cCpus, PFNVMATERROR pfnVMAtError, void *pvUserVM, PFNCFGMCONSTRUCTOR pfnCFGMConstructor, void *pvUserCFGM, PVM *ppVM)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync{
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync LogFlow(("VMR3Create: cCpus=%RU32 pfnVMAtError=%p pvUserVM=%p pfnCFGMConstructor=%p pvUserCFGM=%p ppVM=%p\n",
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync cCpus, pfnVMAtError, pvUserVM, pfnCFGMConstructor, pvUserCFGM, ppVM));
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /*
05afe08870681beb0792f384475077c988916762vboxsync * Because of the current hackiness of the applications
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * we'll have to initialize global stuff from here.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Later the applications will take care of this in a proper way.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync static bool fGlobalInitDone = false;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (!fGlobalInitDone)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync int rc = VMR3GlobalInit();
05afe08870681beb0792f384475077c988916762vboxsync if (RT_FAILURE(rc))
05afe08870681beb0792f384475077c988916762vboxsync return rc;
05afe08870681beb0792f384475077c988916762vboxsync fGlobalInitDone = true;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync /*
05afe08870681beb0792f384475077c988916762vboxsync * Validate input.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsync AssertLogRelMsgReturn(cCpus > 0 && cCpus <= VMM_MAX_CPU_COUNT, ("%RU32\n", cCpus), VERR_TOO_MANY_CPUS);
05afe08870681beb0792f384475077c988916762vboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /*
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Create the UVM so we can register the at-error callback
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * and consoliate a bit of cleanup code.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync PUVM pUVM = NULL; /* shuts up gcc */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync int rc = vmR3CreateUVM(cCpus, &pUVM);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (RT_FAILURE(rc))
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync return rc;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (pfnVMAtError)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rc = VMR3AtErrorRegisterU(pUVM, pfnVMAtError, pvUserVM);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (RT_SUCCESS(rc))
05afe08870681beb0792f384475077c988916762vboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /*
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Initialize the support library creating the session for this VM.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rc = SUPR3Init(&pUVM->vm.s.pSession);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (RT_SUCCESS(rc))
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /*
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Call vmR3CreateU in the EMT thread and wait for it to finish.
05afe08870681beb0792f384475077c988916762vboxsync *
05afe08870681beb0792f384475077c988916762vboxsync * Note! VMCPUID_ANY is used here because VMR3ReqQueueU would have trouble
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * submitting a request to a specific VCPU without a pVM. So, to make
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * sure init is running on EMT(0), vmR3EmulationThreadWithId makes sure
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * that only EMT(0) is servicing VMCPUID_ANY requests when pVM is NULL.
05afe08870681beb0792f384475077c988916762vboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync PVMREQ pReq;
05afe08870681beb0792f384475077c988916762vboxsync rc = VMR3ReqCallU(pUVM, VMCPUID_ANY, &pReq, RT_INDEFINITE_WAIT, 0, (PFNRT)vmR3CreateU, 4,
05afe08870681beb0792f384475077c988916762vboxsync pUVM, cCpus, pfnCFGMConstructor, pvUserCFGM);
05afe08870681beb0792f384475077c988916762vboxsync if (RT_SUCCESS(rc))
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rc = pReq->iStatus;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync VMR3ReqFree(pReq);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (RT_SUCCESS(rc))
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /*
05afe08870681beb0792f384475077c988916762vboxsync * Success!
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync *ppVM = pUVM->pVM;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync LogFlow(("VMR3Create: returns VINF_SUCCESS *ppVM=%p\n", *ppVM));
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync return VINF_SUCCESS;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync else
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync AssertMsgFailed(("VMR3ReqCall failed rc=%Rrc\n", rc));
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /*
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * An error occurred during VM creation. Set the error message directly
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * using the initial callback, as the callback list doesn't exist yet.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync const char *pszError = NULL;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync switch (rc)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync case VERR_VMX_IN_VMX_ROOT_MODE:
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#ifdef RT_OS_LINUX
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pszError = N_("VirtualBox can't operate in VMX root mode. "
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync "Please disable the KVM kernel extension, recompile your kernel and reboot");
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#else
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pszError = N_("VirtualBox can't operate in VMX root mode. Please close all other virtualization programs.");
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#endif
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync break;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync case VERR_VERSION_MISMATCH:
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pszError = N_("VMMR0 driver version mismatch. Please terminate all VMs, make sure that "
05afe08870681beb0792f384475077c988916762vboxsync "VBoxNetDHCP is not running and try again. If you still get this error, "
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync "re-install VirtualBox");
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync break;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#ifdef RT_OS_LINUX
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync case VERR_SUPDRV_COMPONENT_NOT_FOUND:
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pszError = N_("One of the kernel modules was not successfully loaded. Make sure "
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync "that no kernel modules from an older version of VirtualBox exist. "
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync "Then try to recompile and reload the kernel modules by executing "
05afe08870681beb0792f384475077c988916762vboxsync "'/etc/init.d/vboxdrv setup' as root");
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync break;
05afe08870681beb0792f384475077c988916762vboxsync#endif
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync case VERR_RAW_MODE_INVALID_SMP:
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pszError = N_("VT-x/AMD-V is either not available on your host or disabled. "
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync "VirtualBox requires this hardware extension to emulate more than one "
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync "guest CPU");
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync break;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync case VERR_SUPDRV_KERNEL_TOO_OLD_FOR_VTX:
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#ifdef RT_OS_LINUX
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pszError = N_("Because the host kernel is too old, VirtualBox cannot enable the VT-x "
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync "extension. Either upgrade your kernel to Linux 2.6.13 or later or disable "
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync "the VT-x extension in the VM settings. Note that without VT-x you have "
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync "to reduce the number of guest CPUs to one");
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#else
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pszError = N_("Because the host kernel is too old, VirtualBox cannot enable the VT-x "
05afe08870681beb0792f384475077c988916762vboxsync "extension. Either upgrade your kernel or disable the VT-x extension in the "
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync "VM settings. Note that without VT-x you have to reduce the number of guest "
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync "CPUs to one");
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#endif
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync break;
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync default:
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pszError = N_("Unknown error creating VM");
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync break;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync vmR3SetErrorU(pUVM, rc, RT_SRC_POS, pszError, rc);
05afe08870681beb0792f384475077c988916762vboxsync }
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync else
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /*
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * An error occurred at support library initialization time (before the
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * VM could be created). Set the error message directly using the
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * initial callback, as the callback list doesn't exist yet.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync const char *pszError;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync switch (rc)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync case VERR_VM_DRIVER_LOAD_ERROR:
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#ifdef RT_OS_LINUX
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pszError = N_("VirtualBox kernel driver not loaded. The vboxdrv kernel module "
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync "was either not loaded or /dev/vboxdrv is not set up properly. "
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync "Re-setup the kernel module by executing "
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync "'/etc/init.d/vboxdrv setup' as root");
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#else
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pszError = N_("VirtualBox kernel driver not loaded");
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#endif
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync break;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync case VERR_VM_DRIVER_OPEN_ERROR:
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pszError = N_("VirtualBox kernel driver cannot be opened");
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync break;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync case VERR_VM_DRIVER_NOT_ACCESSIBLE:
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#ifdef VBOX_WITH_HARDENING
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /* This should only happen if the executable wasn't hardened - bad code/build. */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pszError = N_("VirtualBox kernel driver not accessible, permission problem. "
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync "Re-install VirtualBox. If you are building it yourself, you "
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync "should make sure it installed correctly and that the setuid "
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync "bit is set on the executables calling VMR3Create.");
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#else
05afe08870681beb0792f384475077c988916762vboxsync /* This should only happen when mixing builds or with the usual /dev/vboxdrv access issues. */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync# if defined(RT_OS_DARWIN)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pszError = N_("VirtualBox KEXT is not accessible, permission problem. "
05afe08870681beb0792f384475077c988916762vboxsync "If you have built VirtualBox yourself, make sure that you do not "
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync "have the vboxdrv KEXT from a different build or installation loaded.");
05afe08870681beb0792f384475077c988916762vboxsync# elif defined(RT_OS_LINUX)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pszError = N_("VirtualBox kernel driver is not accessible, permission problem. "
05afe08870681beb0792f384475077c988916762vboxsync "If you have built VirtualBox yourself, make sure that you do "
05afe08870681beb0792f384475077c988916762vboxsync "not have the vboxdrv kernel module from a different build or "
05afe08870681beb0792f384475077c988916762vboxsync "installation loaded. Also, make sure the vboxdrv udev rule gives "
05afe08870681beb0792f384475077c988916762vboxsync "you the permission you need to access the device.");
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync# elif defined(RT_OS_WINDOWS)
05afe08870681beb0792f384475077c988916762vboxsync pszError = N_("VirtualBox kernel driver is not accessible, permission problem.");
05afe08870681beb0792f384475077c988916762vboxsync# else /* solaris, freebsd, ++. */
05afe08870681beb0792f384475077c988916762vboxsync pszError = N_("VirtualBox kernel module is not accessible, permission problem. "
05afe08870681beb0792f384475077c988916762vboxsync "If you have built VirtualBox yourself, make sure that you do "
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync "not have the vboxdrv kernel module from a different install loaded.");
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync# endif
05afe08870681beb0792f384475077c988916762vboxsync#endif
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync break;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync case VERR_INVALID_HANDLE: /** @todo track down and fix this error. */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync case VERR_VM_DRIVER_NOT_INSTALLED:
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#ifdef RT_OS_LINUX
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pszError = N_("VirtualBox kernel driver not installed. The vboxdrv kernel module "
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync "was either not loaded or /dev/vboxdrv was not created for some "
05afe08870681beb0792f384475077c988916762vboxsync "reason. Re-setup the kernel module by executing "
05afe08870681beb0792f384475077c988916762vboxsync "'/etc/init.d/vboxdrv setup' as root");
05afe08870681beb0792f384475077c988916762vboxsync#else
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pszError = N_("VirtualBox kernel driver not installed");
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#endif
05afe08870681beb0792f384475077c988916762vboxsync break;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync case VERR_NO_MEMORY:
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pszError = N_("VirtualBox support library out of memory");
05afe08870681beb0792f384475077c988916762vboxsync break;
05afe08870681beb0792f384475077c988916762vboxsync case VERR_VERSION_MISMATCH:
e0e7da0420be1398d23ffa9953686d3a43619abdvboxsync case VERR_VM_DRIVER_VERSION_MISMATCH:
e0e7da0420be1398d23ffa9953686d3a43619abdvboxsync pszError = N_("The VirtualBox support driver which is running is from a different "
e0e7da0420be1398d23ffa9953686d3a43619abdvboxsync "version of VirtualBox. You can correct this by stopping all "
e0e7da0420be1398d23ffa9953686d3a43619abdvboxsync "running instances of VirtualBox and reinstalling the software.");
e0e7da0420be1398d23ffa9953686d3a43619abdvboxsync break;
e0e7da0420be1398d23ffa9953686d3a43619abdvboxsync default:
e0e7da0420be1398d23ffa9953686d3a43619abdvboxsync pszError = N_("Unknown error initializing kernel driver");
e0e7da0420be1398d23ffa9953686d3a43619abdvboxsync AssertMsgFailed(("Add error message for rc=%d (%Rrc)\n", rc, rc));
e0e7da0420be1398d23ffa9953686d3a43619abdvboxsync }
vmR3SetErrorU(pUVM, rc, RT_SRC_POS, pszError, rc);
}
}
/* cleanup */
vmR3DestroyUVM(pUVM, 2000);
LogFlow(("VMR3Create: returns %Rrc\n", rc));
return rc;
}
/**
* Creates the UVM.
*
* This will not initialize the support library even if vmR3DestroyUVM
* will terminate that.
*
* @returns VBox status code.
* @param cCpus Number of virtual CPUs
* @param ppUVM Where to store the UVM pointer.
*/
static int vmR3CreateUVM(uint32_t cCpus, PUVM *ppUVM)
{
uint32_t i;
/*
* Create and initialize the UVM.
*/
PUVM pUVM = (PUVM)RTMemAllocZ(RT_OFFSETOF(UVM, aCpus[cCpus]));
AssertReturn(pUVM, VERR_NO_MEMORY);
pUVM->u32Magic = UVM_MAGIC;
pUVM->cCpus = cCpus;
AssertCompile(sizeof(pUVM->vm.s) <= sizeof(pUVM->vm.padding));
pUVM->vm.s.ppAtResetNext = &pUVM->vm.s.pAtReset;
pUVM->vm.s.ppAtStateNext = &pUVM->vm.s.pAtState;
pUVM->vm.s.ppAtErrorNext = &pUVM->vm.s.pAtError;
pUVM->vm.s.ppAtRuntimeErrorNext = &pUVM->vm.s.pAtRuntimeError;
pUVM->vm.s.enmHaltMethod = VMHALTMETHOD_BOOTSTRAP;
/* Initialize the VMCPU array in the UVM. */
for (i = 0; i < cCpus; i++)
{
pUVM->aCpus[i].pUVM = pUVM;
pUVM->aCpus[i].idCpu = i;
}
/* Allocate a TLS entry to store the VMINTUSERPERVMCPU pointer. */
int rc = RTTlsAllocEx(&pUVM->vm.s.idxTLS, NULL);
AssertRC(rc);
if (RT_SUCCESS(rc))
{
/* Allocate a halt method event semaphore for each VCPU. */
for (i = 0; i < cCpus; i++)
{
rc = RTSemEventCreate(&pUVM->aCpus[i].vm.s.EventSemWait);
if (RT_FAILURE(rc))
break;
}
if (RT_SUCCESS(rc))
{
/*
* Init fundamental (sub-)components - STAM, MMR3Heap and PDMLdr.
*/
rc = STAMR3InitUVM(pUVM);
if (RT_SUCCESS(rc))
{
rc = MMR3InitUVM(pUVM);
if (RT_SUCCESS(rc))
{
rc = PDMR3InitUVM(pUVM);
if (RT_SUCCESS(rc))
{
/*
* Start the emulation threads for all VMCPUs.
*/
for (i = 0; i < cCpus; i++)
{
rc = RTThreadCreateF(&pUVM->aCpus[i].vm.s.ThreadEMT, vmR3EmulationThread, &pUVM->aCpus[i], _1M,
RTTHREADTYPE_EMULATION, RTTHREADFLAGS_WAITABLE,
cCpus > 1 ? "EMT-%u" : "EMT", i);
if (RT_FAILURE(rc))
break;
pUVM->aCpus[i].vm.s.NativeThreadEMT = RTThreadGetNative(pUVM->aCpus[i].vm.s.ThreadEMT);
}
if (RT_SUCCESS(rc))
{
*ppUVM = pUVM;
return VINF_SUCCESS;
}
/* bail out. */
while (i-- > 0)
{
/** @todo rainy day: terminate the EMTs. */
}
PDMR3TermUVM(pUVM);
}
MMR3TermUVM(pUVM);
}
STAMR3TermUVM(pUVM);
}
for (i = 0; i < cCpus; i++)
{
RTSemEventDestroy(pUVM->aCpus[i].vm.s.EventSemWait);
pUVM->aCpus[i].vm.s.EventSemWait = NIL_RTSEMEVENT;
}
}
RTTlsFree(pUVM->vm.s.idxTLS);
}
RTMemFree(pUVM);
return rc;
}
/**
* Creates and initializes the VM.
*
* @thread EMT
*/
static int vmR3CreateU(PUVM pUVM, uint32_t cCpus, PFNCFGMCONSTRUCTOR pfnCFGMConstructor, void *pvUserCFGM)
{
int rc = VINF_SUCCESS;
/*
* Load the VMMR0.r0 module so that we can call GVMMR0CreateVM.
*/
rc = PDMR3LdrLoadVMMR0U(pUVM);
if (RT_FAILURE(rc))
{
/** @todo we need a cleaner solution for this (VERR_VMX_IN_VMX_ROOT_MODE).
* bird: what about moving the message down here? Main picks the first message, right? */
if (rc == VERR_VMX_IN_VMX_ROOT_MODE)
return rc; /* proper error message set later on */
return vmR3SetErrorU(pUVM, rc, RT_SRC_POS, N_("Failed to load VMMR0.r0"));
}
/*
* Request GVMM to create a new VM for us.
*/
GVMMCREATEVMREQ CreateVMReq;
CreateVMReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
CreateVMReq.Hdr.cbReq = sizeof(CreateVMReq);
CreateVMReq.pSession = pUVM->vm.s.pSession;
CreateVMReq.pVMR0 = NIL_RTR0PTR;
CreateVMReq.pVMR3 = NULL;
CreateVMReq.cCpus = cCpus;
rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_GVMM_CREATE_VM, 0, &CreateVMReq.Hdr);
if (RT_SUCCESS(rc))
{
PVM pVM = pUVM->pVM = CreateVMReq.pVMR3;
AssertRelease(VALID_PTR(pVM));
AssertRelease(pVM->pVMR0 == CreateVMReq.pVMR0);
AssertRelease(pVM->pSession == pUVM->vm.s.pSession);
AssertRelease(pVM->cCpus == cCpus);
AssertRelease(pVM->offVMCPU == RT_UOFFSETOF(VM, aCpus));
Log(("VMR3Create: Created pUVM=%p pVM=%p pVMR0=%p hSelf=%#x cCpus=%RU32\n",
pUVM, pVM, pVM->pVMR0, pVM->hSelf, pVM->cCpus));
/*
* Initialize the VM structure and our internal data (VMINT).
*/
pVM->pUVM = pUVM;
for (VMCPUID i = 0; i < pVM->cCpus; i++)
{
pVM->aCpus[i].pUVCpu = &pUVM->aCpus[i];
pVM->aCpus[i].idCpu = i;
pVM->aCpus[i].hNativeThread = pUVM->aCpus[i].vm.s.NativeThreadEMT;
Assert(pVM->aCpus[i].hNativeThread != NIL_RTNATIVETHREAD);
pUVM->aCpus[i].pVCpu = &pVM->aCpus[i];
pUVM->aCpus[i].pVM = pVM;
}
/*
* Init the configuration.
*/
rc = CFGMR3Init(pVM, pfnCFGMConstructor, pvUserCFGM);
if (RT_SUCCESS(rc))
{
rc = CFGMR3QueryBoolDef(CFGMR3GetRoot(pVM), "HwVirtExtForced", &pVM->fHwVirtExtForced, false);
if (RT_SUCCESS(rc) && pVM->fHwVirtExtForced)
pVM->fHWACCMEnabled = true;
/*
* If executing in fake suplib mode disable RR3 and RR0 in the config.
*/
const char *psz = RTEnvGet("VBOX_SUPLIB_FAKE");
if (psz && !strcmp(psz, "fake"))
{
CFGMR3RemoveValue(CFGMR3GetRoot(pVM), "RawR3Enabled");
CFGMR3InsertInteger(CFGMR3GetRoot(pVM), "RawR3Enabled", 0);
CFGMR3RemoveValue(CFGMR3GetRoot(pVM), "RawR0Enabled");
CFGMR3InsertInteger(CFGMR3GetRoot(pVM), "RawR0Enabled", 0);
}
/*
* Make sure the CPU count in the config data matches.
*/
if (RT_SUCCESS(rc))
{
uint32_t cCPUsCfg;
rc = CFGMR3QueryU32Def(CFGMR3GetRoot(pVM), "NumCPUs", &cCPUsCfg, 1);
AssertLogRelMsgRC(rc, ("Configuration error: Querying \"NumCPUs\" as integer failed, rc=%Rrc\n", rc));
if (RT_SUCCESS(rc) && cCPUsCfg != cCpus)
{
AssertLogRelMsgFailed(("Configuration error: \"NumCPUs\"=%RU32 and VMR3CreateVM::cCpus=%RU32 does not match!\n",
cCPUsCfg, cCpus));
rc = VERR_INVALID_PARAMETER;
}
}
if (RT_SUCCESS(rc))
{
/*
* Init the ring-3 components and ring-3 per cpu data, finishing it off
* by a relocation round (intermediate context finalization will do this).
*/
rc = vmR3InitRing3(pVM, pUVM);
if (RT_SUCCESS(rc))
{
rc = vmR3InitVMCpu(pVM);
if (RT_SUCCESS(rc))
rc = PGMR3FinalizeMappings(pVM);
if (RT_SUCCESS(rc))
{
LogFlow(("Ring-3 init succeeded\n"));
/*
* Init the Ring-0 components.
*/
rc = vmR3InitRing0(pVM);
if (RT_SUCCESS(rc))
{
/* Relocate again, because some switcher fixups depends on R0 init results. */
VMR3Relocate(pVM, 0);
#ifdef VBOX_WITH_DEBUGGER
/*
* Init the tcp debugger console if we're building
* with debugger support.
*/
void *pvUser = NULL;
rc = DBGCTcpCreate(pVM, &pvUser);
if ( RT_SUCCESS(rc)
|| rc == VERR_NET_ADDRESS_IN_USE)
{
pUVM->vm.s.pvDBGC = pvUser;
#endif
/*
* Init the Guest Context components.
*/
rc = vmR3InitGC(pVM);
if (RT_SUCCESS(rc))
{
/*
* Now we can safely set the VM halt method to default.
*/
rc = vmR3SetHaltMethodU(pUVM, VMHALTMETHOD_DEFAULT);
if (RT_SUCCESS(rc))
{
/*
* Set the state and link into the global list.
*/
vmR3SetState(pVM, VMSTATE_CREATED);
pUVM->pNext = g_pUVMsHead;
g_pUVMsHead = pUVM;
#ifdef LOG_ENABLED
RTLogSetCustomPrefixCallback(NULL, vmR3LogPrefixCallback, pUVM);
#endif
return VINF_SUCCESS;
}
}
#ifdef VBOX_WITH_DEBUGGER
DBGCTcpTerminate(pVM, pUVM->vm.s.pvDBGC);
pUVM->vm.s.pvDBGC = NULL;
}
#endif
//..
}
}
vmR3Destroy(pVM);
}
}
//..
/* Clean CFGM. */
int rc2 = CFGMR3Term(pVM);
AssertRC(rc2);
}
/*
* Drop all references to VM and the VMCPU structures, then
* tell GVMM to destroy the VM.
*/
pUVM->pVM = NULL;
for (VMCPUID i = 0; i < pUVM->cCpus; i++)
{
pUVM->aCpus[i].pVM = NULL;
pUVM->aCpus[i].pVCpu = NULL;
}
Assert(pUVM->vm.s.enmHaltMethod == VMHALTMETHOD_BOOTSTRAP);
if (pUVM->cCpus > 1)
{
/* Poke the other EMTs since they may have stale pVM and pVCpu references
on the stack (see VMR3WaitU for instance) if they've been awakened after
VM creation. */
for (VMCPUID i = 1; i < pUVM->cCpus; i++)
VMR3NotifyCpuFFU(&pUVM->aCpus[i], 0);
RTThreadSleep(RT_MIN(100 + 25 *(pUVM->cCpus - 1), 500)); /* very sophisticated */
}
int rc2 = SUPR3CallVMMR0Ex(CreateVMReq.pVMR0, 0 /*idCpu*/, VMMR0_DO_GVMM_DESTROY_VM, 0, NULL);
AssertRC(rc2);
}
else
vmR3SetErrorU(pUVM, rc, RT_SRC_POS, N_("VM creation failed (GVMM)"));
LogFlow(("vmR3CreateU: returns %Rrc\n", rc));
return rc;
}
/**
* Register the calling EMT with GVM.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param idCpu The Virtual CPU ID.
*/
static DECLCALLBACK(int) vmR3RegisterEMT(PVM pVM, VMCPUID idCpu)
{
Assert(VMMGetCpuId(pVM) == idCpu);
int rc = SUPR3CallVMMR0Ex(pVM->pVMR0, idCpu, VMMR0_DO_GVMM_REGISTER_VMCPU, 0, NULL);
if (RT_FAILURE(rc))
LogRel(("idCpu=%u rc=%Rrc\n", idCpu, rc));
return rc;
}
/**
* Initializes all R3 components of the VM
*/
static int vmR3InitRing3(PVM pVM, PUVM pUVM)
{
int rc;
/*
* Register the other EMTs with GVM.
*/
for (VMCPUID idCpu = 1; idCpu < pVM->cCpus; idCpu++)
{
PVMREQ pReq;
rc = VMR3ReqCallU(pUVM, idCpu, &pReq, RT_INDEFINITE_WAIT, 0 /*fFlags*/,
(PFNRT)vmR3RegisterEMT, 2, pVM, idCpu);
if (RT_SUCCESS(rc))
rc = pReq->iStatus;
VMR3ReqFree(pReq);
if (RT_FAILURE(rc))
return rc;
}
/*
* Init all R3 components, the order here might be important.
*/
rc = MMR3Init(pVM);
if (RT_SUCCESS(rc))
{
STAM_REG(pVM, &pVM->StatTotalInGC, STAMTYPE_PROFILE_ADV, "/PROF/VM/InGC", STAMUNIT_TICKS_PER_CALL, "Profiling the total time spent in GC.");
STAM_REG(pVM, &pVM->StatSwitcherToGC, STAMTYPE_PROFILE_ADV, "/PROF/VM/SwitchToGC", STAMUNIT_TICKS_PER_CALL, "Profiling switching to GC.");
STAM_REG(pVM, &pVM->StatSwitcherToHC, STAMTYPE_PROFILE_ADV, "/PROF/VM/SwitchToHC", STAMUNIT_TICKS_PER_CALL, "Profiling switching to HC.");
STAM_REG(pVM, &pVM->StatSwitcherSaveRegs, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/SaveRegs", STAMUNIT_TICKS_PER_CALL,"Profiling switching to GC.");
STAM_REG(pVM, &pVM->StatSwitcherSysEnter, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/SysEnter", STAMUNIT_TICKS_PER_CALL,"Profiling switching to GC.");
STAM_REG(pVM, &pVM->StatSwitcherDebug, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/Debug", STAMUNIT_TICKS_PER_CALL,"Profiling switching to GC.");
STAM_REG(pVM, &pVM->StatSwitcherCR0, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/CR0", STAMUNIT_TICKS_PER_CALL, "Profiling switching to GC.");
STAM_REG(pVM, &pVM->StatSwitcherCR4, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/CR4", STAMUNIT_TICKS_PER_CALL, "Profiling switching to GC.");
STAM_REG(pVM, &pVM->StatSwitcherLgdt, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/Lgdt", STAMUNIT_TICKS_PER_CALL, "Profiling switching to GC.");
STAM_REG(pVM, &pVM->StatSwitcherLidt, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/Lidt", STAMUNIT_TICKS_PER_CALL, "Profiling switching to GC.");
STAM_REG(pVM, &pVM->StatSwitcherLldt, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/Lldt", STAMUNIT_TICKS_PER_CALL, "Profiling switching to GC.");
STAM_REG(pVM, &pVM->StatSwitcherTSS, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/TSS", STAMUNIT_TICKS_PER_CALL, "Profiling switching to GC.");
STAM_REG(pVM, &pVM->StatSwitcherJmpCR3, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/JmpCR3", STAMUNIT_TICKS_PER_CALL,"Profiling switching to GC.");
STAM_REG(pVM, &pVM->StatSwitcherRstrRegs, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/RstrRegs", STAMUNIT_TICKS_PER_CALL,"Profiling switching to GC.");
for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
{
rc = STAMR3RegisterF(pVM, &pUVM->aCpus[idCpu].vm.s.StatHaltYield, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling halted state yielding.", "/PROF/VM/CPU%d/Halt/Yield", idCpu);
AssertRC(rc);
rc = STAMR3RegisterF(pVM, &pUVM->aCpus[idCpu].vm.s.StatHaltBlock, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling halted state blocking.", "/PROF/VM/CPU%d/Halt/Block", idCpu);
AssertRC(rc);
rc = STAMR3RegisterF(pVM, &pUVM->aCpus[idCpu].vm.s.StatHaltTimers, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling halted state timer tasks.", "/PROF/VM/CPU%d/Halt/Timers", idCpu);
AssertRC(rc);
}
STAM_REG(pVM, &pUVM->vm.s.StatReqAllocNew, STAMTYPE_COUNTER, "/VM/Req/AllocNew", STAMUNIT_OCCURENCES, "Number of VMR3ReqAlloc returning a new packet.");
STAM_REG(pVM, &pUVM->vm.s.StatReqAllocRaces, STAMTYPE_COUNTER, "/VM/Req/AllocRaces", STAMUNIT_OCCURENCES, "Number of VMR3ReqAlloc causing races.");
STAM_REG(pVM, &pUVM->vm.s.StatReqAllocRecycled, STAMTYPE_COUNTER, "/VM/Req/AllocRecycled", STAMUNIT_OCCURENCES, "Number of VMR3ReqAlloc returning a recycled packet.");
STAM_REG(pVM, &pUVM->vm.s.StatReqFree, STAMTYPE_COUNTER, "/VM/Req/Free", STAMUNIT_OCCURENCES, "Number of VMR3ReqFree calls.");
STAM_REG(pVM, &pUVM->vm.s.StatReqFreeOverflow, STAMTYPE_COUNTER, "/VM/Req/FreeOverflow", STAMUNIT_OCCURENCES, "Number of times the request was actually freed.");
rc = CPUMR3Init(pVM);
if (RT_SUCCESS(rc))
{
rc = HWACCMR3Init(pVM);
if (RT_SUCCESS(rc))
{
rc = PGMR3Init(pVM);
if (RT_SUCCESS(rc))
{
rc = REMR3Init(pVM);
if (RT_SUCCESS(rc))
{
rc = MMR3InitPaging(pVM);
if (RT_SUCCESS(rc))
rc = TMR3Init(pVM);
if (RT_SUCCESS(rc))
{
rc = VMMR3Init(pVM);
if (RT_SUCCESS(rc))
{
rc = SELMR3Init(pVM);
if (RT_SUCCESS(rc))
{
rc = TRPMR3Init(pVM);
if (RT_SUCCESS(rc))
{
rc = CSAMR3Init(pVM);
if (RT_SUCCESS(rc))
{
rc = PATMR3Init(pVM);
if (RT_SUCCESS(rc))
{
#ifdef VBOX_WITH_VMI
rc = PARAVR3Init(pVM);
if (RT_SUCCESS(rc))
{
#endif
rc = IOMR3Init(pVM);
if (RT_SUCCESS(rc))
{
rc = EMR3Init(pVM);
if (RT_SUCCESS(rc))
{
rc = DBGFR3Init(pVM);
if (RT_SUCCESS(rc))
{
rc = PDMR3Init(pVM);
if (RT_SUCCESS(rc))
{
rc = PGMR3InitDynMap(pVM);
if (RT_SUCCESS(rc))
rc = MMR3HyperInitFinalize(pVM);
if (RT_SUCCESS(rc))
rc = PATMR3InitFinalize(pVM);
if (RT_SUCCESS(rc))
rc = PGMR3InitFinalize(pVM);
if (RT_SUCCESS(rc))
rc = SELMR3InitFinalize(pVM);
if (RT_SUCCESS(rc))
rc = TMR3InitFinalize(pVM);
if (RT_SUCCESS(rc))
rc = VMMR3InitFinalize(pVM);
if (RT_SUCCESS(rc))
rc = REMR3InitFinalize(pVM);
if (RT_SUCCESS(rc))
rc = vmR3InitDoCompleted(pVM, VMINITCOMPLETED_RING3);
if (RT_SUCCESS(rc))
{
LogFlow(("vmR3InitRing3: returns %Rrc\n", VINF_SUCCESS));
return VINF_SUCCESS;
}
int rc2 = PDMR3Term(pVM);
AssertRC(rc2);
}
int rc2 = DBGFR3Term(pVM);
AssertRC(rc2);
}
int rc2 = EMR3Term(pVM);
AssertRC(rc2);
}
int rc2 = IOMR3Term(pVM);
AssertRC(rc2);
}
#ifdef VBOX_WITH_VMI
int rc2 = PARAVR3Term(pVM);
AssertRC(rc2);
}
#endif
int rc2 = PATMR3Term(pVM);
AssertRC(rc2);
}
int rc2 = CSAMR3Term(pVM);
AssertRC(rc2);
}
int rc2 = TRPMR3Term(pVM);
AssertRC(rc2);
}
int rc2 = SELMR3Term(pVM);
AssertRC(rc2);
}
int rc2 = VMMR3Term(pVM);
AssertRC(rc2);
}
int rc2 = TMR3Term(pVM);
AssertRC(rc2);
}
int rc2 = REMR3Term(pVM);
AssertRC(rc2);
}
int rc2 = PGMR3Term(pVM);
AssertRC(rc2);
}
int rc2 = HWACCMR3Term(pVM);
AssertRC(rc2);
}
//int rc2 = CPUMR3Term(pVM);
//AssertRC(rc2);
}
/* MMR3Term is not called here because it'll kill the heap. */
}
LogFlow(("vmR3InitRing3: returns %Rrc\n", rc));
return rc;
}
/**
* Initializes all VM CPU components of the VM
*/
static int vmR3InitVMCpu(PVM pVM)
{
int rc = VINF_SUCCESS;
int rc2;
rc = CPUMR3InitCPU(pVM);
if (RT_SUCCESS(rc))
{
rc = HWACCMR3InitCPU(pVM);
if (RT_SUCCESS(rc))
{
rc = PGMR3InitCPU(pVM);
if (RT_SUCCESS(rc))
{
rc = TMR3InitCPU(pVM);
if (RT_SUCCESS(rc))
{
rc = VMMR3InitCPU(pVM);
if (RT_SUCCESS(rc))
{
rc = EMR3InitCPU(pVM);
if (RT_SUCCESS(rc))
{
LogFlow(("vmR3InitVMCpu: returns %Rrc\n", VINF_SUCCESS));
return VINF_SUCCESS;
}
rc2 = VMMR3TermCPU(pVM);
AssertRC(rc2);
}
rc2 = TMR3TermCPU(pVM);
AssertRC(rc2);
}
rc2 = PGMR3TermCPU(pVM);
AssertRC(rc2);
}
rc2 = HWACCMR3TermCPU(pVM);
AssertRC(rc2);
}
rc2 = CPUMR3TermCPU(pVM);
AssertRC(rc2);
}
LogFlow(("vmR3InitVMCpu: returns %Rrc\n", rc));
return rc;
}
/**
* Initializes all R0 components of the VM
*/
static int vmR3InitRing0(PVM pVM)
{
LogFlow(("vmR3InitRing0:\n"));
/*
* Check for FAKE suplib mode.
*/
int rc = VINF_SUCCESS;
const char *psz = RTEnvGet("VBOX_SUPLIB_FAKE");
if (!psz || strcmp(psz, "fake"))
{
/*
* Call the VMMR0 component and let it do the init.
*/
rc = VMMR3InitR0(pVM);
}
else
Log(("vmR3InitRing0: skipping because of VBOX_SUPLIB_FAKE=fake\n"));
/*
* Do notifications and return.
*/
if (RT_SUCCESS(rc))
rc = vmR3InitDoCompleted(pVM, VMINITCOMPLETED_RING0);
/** todo: move this to the VMINITCOMPLETED_RING0 notification handler once implemented */
if (RT_SUCCESS(rc))
rc = HWACCMR3InitFinalizeR0(pVM);
LogFlow(("vmR3InitRing0: returns %Rrc\n", rc));
return rc;
}
/**
* Initializes all GC components of the VM
*/
static int vmR3InitGC(PVM pVM)
{
LogFlow(("vmR3InitGC:\n"));
/*
* Check for FAKE suplib mode.
*/
int rc = VINF_SUCCESS;
const char *psz = RTEnvGet("VBOX_SUPLIB_FAKE");
if (!psz || strcmp(psz, "fake"))
{
/*
* Call the VMMR0 component and let it do the init.
*/
rc = VMMR3InitRC(pVM);
}
else
Log(("vmR3InitGC: skipping because of VBOX_SUPLIB_FAKE=fake\n"));
/*
* Do notifications and return.
*/
if (RT_SUCCESS(rc))
rc = vmR3InitDoCompleted(pVM, VMINITCOMPLETED_GC);
LogFlow(("vmR3InitGC: returns %Rrc\n", rc));
return rc;
}
/**
* Do init completed notifications.
* This notifications can fail.
*
* @param pVM The VM handle.
* @param enmWhat What's completed.
*/
static int vmR3InitDoCompleted(PVM pVM, VMINITCOMPLETED enmWhat)
{
return VINF_SUCCESS;
}
/**
* Logger callback for inserting a custom prefix.
*
* @returns Number of chars written.
* @param pLogger The logger.
* @param pchBuf The output buffer.
* @param cchBuf The output buffer size.
* @param pvUser Pointer to the UVM structure.
*/
static DECLCALLBACK(size_t) vmR3LogPrefixCallback(PRTLOGGER pLogger, char *pchBuf, size_t cchBuf, void *pvUser)
{
AssertReturn(cchBuf >= 2, 0);
PUVM pUVM = (PUVM)pvUser;
PUVMCPU pUVCpu = (PUVMCPU)RTTlsGet(pUVM->vm.s.idxTLS);
if (pUVCpu)
{
static const char s_szHex[17] = "0123456789abcdef";
VMCPUID const idCpu = pUVCpu->idCpu;
pchBuf[1] = s_szHex[ idCpu & 15];
pchBuf[0] = s_szHex[(idCpu >> 4) & 15];
}
else
{
pchBuf[0] = 'x';
pchBuf[1] = 'y';
}
return 2;
}
/**
* Calls the relocation functions for all VMM components so they can update
* any GC pointers. When this function is called all the basic VM members
* have been updated and the actual memory relocation have been done
* by the PGM/MM.
*
* This is used both on init and on runtime relocations.
*
* @param pVM VM handle.
* @param offDelta Relocation delta relative to old location.
*/
VMMR3DECL(void) VMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
{
LogFlow(("VMR3Relocate: offDelta=%RGv\n", offDelta));
/*
* The order here is very important!
*/
PGMR3Relocate(pVM, offDelta);
PDMR3LdrRelocateU(pVM->pUVM, offDelta);
PGMR3Relocate(pVM, 0); /* Repeat after PDM relocation. */
CPUMR3Relocate(pVM);
HWACCMR3Relocate(pVM);
SELMR3Relocate(pVM);
VMMR3Relocate(pVM, offDelta);
SELMR3Relocate(pVM); /* !hack! fix stack! */
TRPMR3Relocate(pVM, offDelta);
PATMR3Relocate(pVM);
CSAMR3Relocate(pVM, offDelta);
IOMR3Relocate(pVM, offDelta);
EMR3Relocate(pVM);
TMR3Relocate(pVM, offDelta);
DBGFR3Relocate(pVM, offDelta);
PDMR3Relocate(pVM, offDelta);
}
/**
* Power on the virtual machine.
*
* @returns 0 on success.
* @returns VBox error code on failure.
* @param pVM VM to power on.
* @thread Any thread.
* @vmstate Created
* @vmstateto Running
*/
VMMR3DECL(int) VMR3PowerOn(PVM pVM)
{
LogFlow(("VMR3PowerOn: pVM=%p\n", pVM));
/*
* Validate input.
*/
if (!pVM)
{
AssertMsgFailed(("Invalid VM pointer\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Request the operation in EMT (in order as VCPU 0 does all the work)
*/
PVMREQ pReq;
int rc = VMR3ReqCall(pVM, VMCPUID_ALL, &pReq, RT_INDEFINITE_WAIT, (PFNRT)vmR3PowerOn, 1, pVM);
if (RT_SUCCESS(rc))
{
rc = pReq->iStatus;
VMR3ReqFree(pReq);
}
LogFlow(("VMR3PowerOn: returns %Rrc\n", rc));
return rc;
}
/**
* Power on the virtual machine.
*
* @returns 0 on success.
* @returns VBox error code on failure.
* @param pVM VM to power on.
* @thread EMT
*/
static DECLCALLBACK(int) vmR3PowerOn(PVM pVM)
{
LogFlow(("vmR3PowerOn: pVM=%p\n", pVM));
/*
* EMT(0) does the actual power on work *before* the other EMTs
* get here, they just need to set their state to STARTED so they
* get out of the EMT loop and into EM.
*/
PVMCPU pVCpu = VMMGetCpu(pVM);
VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED);
if (pVCpu->idCpu != 0)
return VINF_SUCCESS;
/*
* Validate input.
*/
if (pVM->enmVMState != VMSTATE_CREATED)
{
AssertMsgFailed(("Invalid VM state %d\n", pVM->enmVMState));
return VERR_VM_INVALID_VM_STATE;
}
/*
* Change the state, notify the components and resume the execution.
*/
vmR3SetState(pVM, VMSTATE_RUNNING);
PDMR3PowerOn(pVM);
return VINF_SUCCESS;
}
/**
* Suspends a running VM.
*
* @returns 0 on success.
* @returns VBox error code on failure.
* @param pVM VM to suspend.
* @thread Any thread.
* @vmstate Running
* @vmstateto Suspended
*/
VMMR3DECL(int) VMR3Suspend(PVM pVM)
{
LogFlow(("VMR3Suspend: pVM=%p\n", pVM));
/*
* Validate input and pass it on to the internal worker.
*/
VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
int rc = vmR3SuspendCommon(pVM, false /*fFatal*/);
LogFlow(("VMR3Suspend: returns %Rrc\n", rc));
return rc;
}
/**
* Common worker for VMR3Suspend and vmR3SetRuntimeErrorCommon.
*
* They both suspends the VM, but the latter ends up in the VMSTATE_FATAL_ERROR
* instead of VMSTATE_SUSPENDED.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param fFatal Whether it's a fatal error or not.
*
* @thread Any thread.
* @vmstate Running
* @vmstateto Suspended, FatalError
*/
static int vmR3SuspendCommon(PVM pVM, bool fFatal)
{
/*
* Request the operation in EMT. (in reverse order as VCPU 0 does the actual work)
*/
PVMREQ pReq = NULL;
int rc = VMR3ReqCall(pVM, VMCPUID_ALL_REVERSE, &pReq, RT_INDEFINITE_WAIT,
(PFNRT)vmR3Suspend, 2, pVM, fFatal);
if (RT_SUCCESS(rc))
{
rc = pReq->iStatus;
VMR3ReqFree(pReq);
}
else
Assert(pReq == NULL);
return rc;
}
/**
* Suspends a running VM.
*
* @returns 0 on success.
* @returns VBox error code on failure.
* @param pVM VM to suspend.
* @param fFatal Whether it's a fatal error or normal suspend.
* @thread EMT
*/
static DECLCALLBACK(int) vmR3Suspend(PVM pVM, bool fFatal)
{
LogFlow(("vmR3Suspend: pVM=%p\n", pVM));
/*
* Validate input.
*/
if (pVM->enmVMState != VMSTATE_RUNNING)
{
AssertMsgFailed(("Invalid VM state %d\n", pVM->enmVMState));
return VERR_VM_INVALID_VM_STATE;
}
PVMCPU pVCpu = VMMGetCpu(pVM);
/* Only VCPU 0 does the actual work (*after* all the other CPUs has been here). */
if (pVCpu->idCpu != 0)
return VINF_EM_SUSPEND;
/*
* Change the state, notify the components and resume the execution.
*/
vmR3SetState(pVM, fFatal ? VMSTATE_FATAL_ERROR : VMSTATE_SUSPENDED);
PDMR3Suspend(pVM);
return VINF_EM_SUSPEND;
}
/**
* Resume VM execution.
*
* @returns 0 on success.
* @returns VBox error code on failure.
* @param pVM The VM to resume.
* @thread Any thread.
* @vmstate Suspended
* @vmstateto Running
*/
VMMR3DECL(int) VMR3Resume(PVM pVM)
{
LogFlow(("VMR3Resume: pVM=%p\n", pVM));
/*
* Validate input.
*/
if (!pVM)
{
AssertMsgFailed(("Invalid VM pointer\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Request the operation in EMT. (in VCPU order as VCPU 0 does the actual work)
*/
PVMREQ pReq = NULL;
int rc = VMR3ReqCall(pVM, VMCPUID_ALL, &pReq, RT_INDEFINITE_WAIT, (PFNRT)vmR3Resume, 1, pVM);
if (RT_SUCCESS(rc))
{
rc = pReq->iStatus;
VMR3ReqFree(pReq);
}
else
Assert(pReq == NULL);
LogFlow(("VMR3Resume: returns %Rrc\n", rc));
return rc;
}
/**
* Resume VM execution.
*
* @returns 0 on success.
* @returns VBox error code on failure.
* @param pVM The VM to resume.
* @thread EMT
*/
static DECLCALLBACK(int) vmR3Resume(PVM pVM)
{
LogFlow(("vmR3Resume: pVM=%p\n", pVM));
PVMCPU pVCpu = VMMGetCpu(pVM);
/* Only VCPU 0 does the actual work (*before* the others wake up). */
if (pVCpu->idCpu != 0)
return VINF_EM_RESUME;
/*
* Validate input.
*/
if (pVM->enmVMState != VMSTATE_SUSPENDED)
{
AssertMsgFailed(("Invalid VM state %d\n", pVM->enmVMState));
return VERR_VM_INVALID_VM_STATE;
}
/*
* Change the state, notify the components and resume the execution.
*/
pVM->vm.s.fPreventSaveState = false;
vmR3SetState(pVM, VMSTATE_RUNNING);
PDMR3Resume(pVM);
return VINF_EM_RESUME;
}
/**
* Save current VM state.
*
* Can be used for both saving the state and creating snapshots.
*
* When called for a VM in the RUNNING state, the saved state is created live
* and the VM is only suspended when the final part of the saving is preformed.
* The VM state will not be restored to RUNNING in this case and it's up to the
* caller to call VMR3Resume if this is desirable. (The rational is that the
* caller probably wish to reconfigure the disks before resuming the VM.)
*
* @returns VBox status code.
*
* @param pVM VM which state should be saved.
* @param pszFilename Name of the save state file.
* @param fContinueAfterwards Continue execution afterwards. When in doubt,
* set this to true.
* @param pfnProgress Progress callback. Optional.
* @param pvUser User argument for the progress callback.
*
* @thread Any thread. Will forward the request to EMT(0).
* @vmstate Suspended, Running
* @vmstateto Suspended.
*/
VMMR3DECL(int) VMR3Save(PVM pVM, const char *pszFilename, bool fContinueAfterwards, PFNVMPROGRESS pfnProgress, void *pvUser)
{
LogFlow(("VMR3Save: pVM=%p pszFilename=%p:{%s} fContinueAfterwards=%RTbool pfnProgress=%p pvUser=%p\n",
pVM, pszFilename, pszFilename, fContinueAfterwards, pfnProgress, pvUser));
/*
* Validate input.
*/
VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
AssertReturn(*pszFilename, VERR_INVALID_PARAMETER);
AssertPtrNullReturn(pfnProgress, VERR_INVALID_POINTER);
/*
* Request the operation in EMT(0).
*/
SSMAFTER enmAfter = fContinueAfterwards ? SSMAFTER_CONTINUE : SSMAFTER_DESTROY;
PSSMHANDLE pSSM;
PVMREQ pReq;
int rc = VMR3ReqCall(pVM, 0 /* VCPU 0 */, &pReq, RT_INDEFINITE_WAIT,
(PFNRT)vmR3Save, 6, pVM, pszFilename, enmAfter, pfnProgress, pvUser, &pSSM);
if (RT_SUCCESS(rc))
{
rc = pReq->iStatus;
VMR3ReqFree(pReq);
}
if ( RT_SUCCESS(rc)
&& pSSM)
{
#if 1
/**@todo*/ rc = VERR_NOT_IMPLEMENTED;
#else
/*
* Live snapshot.
*/
rc = SSMR3LiveDoStep1(pSSM);
if (RT_SUCCESS(rc))
{
rc = VMR3ReqCall(pVM, 0 /* VCPU 0 */, &pReq, RT_INDEFINITE_WAIT,
(PFNRT)vmR3SaveLive, 2, pVM, pSSM);
if (RT_SUCCESS(rc))
{
rc = pReq->iStatus;
VMR3ReqFree(pReq);
}
}
#endif
}
LogFlow(("VMR3Save: returns %Rrc\n", rc));
return rc;
}
/**
* Worker for VMR3Save that validates the state and calls SSMR3Save.
*
* @returns 0 on success.
* @returns VBox error code on failure.
* @param pVM VM which state should be saved.
* @param pszFilename Name of the save state file.
* @param enmAfter What to do afterwards.
* @param pfnProgress Progress callback. Optional.
* @param pvUser User argument for the progress callback.
* @param ppSSM Where to return the saved state handle in case of a
* live snapshot scenario.
* @thread EMT
*/
static DECLCALLBACK(int) vmR3Save(PVM pVM, const char *pszFilename, SSMAFTER enmAfter, PFNVMPROGRESS pfnProgress, void *pvUser, PSSMHANDLE *ppSSM)
{
LogFlow(("vmR3Save: pVM=%p pszFilename=%p:{%s} enmAfter=%d pfnProgress=%p pvUser=%p ppSSM=%p\n", pVM, pszFilename, pszFilename, enmAfter, pfnProgress, pvUser, ppSSM));
/*
* Validate input.
*/
/** @todo SMP: Check that vmR3SetState always done by EMT(0). If not add a vmR3TrySetState(). */
*ppSSM = NULL;
AssertMsgReturn( pVM->enmVMState == VMSTATE_SUSPENDED
|| pVM->enmVMState == VMSTATE_RUNNING,
("%d\n", pVM->enmVMState),
VERR_VM_INVALID_VM_STATE);
/* If we are in an inconsistent state, then we don't allow state saving. */
if (pVM->vm.s.fPreventSaveState) /** @todo Incorporate VMINT::fPreventSaveState into the VMSTATE. There is a defect for this. */
{
LogRel(("VMM: vmR3Save: saving the VM state is not allowed at this moment\n"));
return VERR_VM_SAVE_STATE_NOT_ALLOWED;
}
/*
* Change the state and perform/start the saveing.
*/
int rc;
if (pVM->enmVMState == VMSTATE_RUNNING)
{
/** @todo state mess. */
rc = SSMR3LiveToFile(pVM, pszFilename, enmAfter, pfnProgress, pvUser, ppSSM);
}
else
{
vmR3SetState(pVM, VMSTATE_SAVING); /** @todo Should probably use a different state for live snapshots and/or live migration. Will fix the state machine later. */
rc = SSMR3Save(pVM, pszFilename, enmAfter, pfnProgress, pvUser);
vmR3SetState(pVM, VMSTATE_SUSPENDED);
}
return rc;
}
/**
* Loads a new VM state.
*
* To restore a saved state on VM startup, call this function and then
* resume the VM instead of powering it on.
*
* @returns 0 on success.
* @returns VBox error code on failure.
* @param pVM VM which state should be saved.
* @param pszFilename Name of the save state file.
* @param pfnProgress Progress callback. Optional.
* @param pvUser User argument for the progress callback.
* @thread Any thread.
* @vmstate Created, Suspended
* @vmstateto Suspended
*/
VMMR3DECL(int) VMR3Load(PVM pVM, const char *pszFilename, PFNVMPROGRESS pfnProgress, void *pvUser)
{
LogFlow(("VMR3Load: pVM=%p pszFilename=%p:{%s} pfnProgress=%p pvUser=%p\n", pVM, pszFilename, pszFilename, pfnProgress, pvUser));
/*
* Validate input.
*/
if (!pVM)
{
AssertMsgFailed(("Invalid VM pointer\n"));
return VERR_INVALID_PARAMETER;
}
if (!pszFilename)
{
AssertMsgFailed(("Must specify a filename to load the state from, wise guy!\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Request the operation in EMT.
*/
PVMREQ pReq;
int rc = VMR3ReqCall(pVM, 0 /* VCPU 0 */, &pReq, RT_INDEFINITE_WAIT, (PFNRT)vmR3Load, 4, pVM, pszFilename, pfnProgress, pvUser);
if (RT_SUCCESS(rc))
{
rc = pReq->iStatus;
VMR3ReqFree(pReq);
}
LogFlow(("VMR3Load: returns %Rrc\n", rc));
return rc;
}
/**
* Loads a new VM state.
*
* To restore a saved state on VM startup, call this function and then
* resume the VM instead of powering it on.
*
* @returns 0 on success.
* @returns VBox error code on failure.
* @param pVM VM which state should be saved.
* @param pszFilename Name of the save state file.
* @param pfnProgress Progress callback. Optional.
* @param pvUser User argument for the progress callback.
* @thread EMT.
*/
static DECLCALLBACK(int) vmR3Load(PVM pVM, const char *pszFilename, PFNVMPROGRESS pfnProgress, void *pvUser)
{
LogFlow(("vmR3Load: pVM=%p pszFilename=%p:{%s} pfnProgress=%p pvUser=%p\n", pVM, pszFilename, pszFilename, pfnProgress, pvUser));
/*
* Validate input.
*/
if ( pVM->enmVMState != VMSTATE_SUSPENDED
&& pVM->enmVMState != VMSTATE_CREATED)
{
AssertMsgFailed(("Invalid VM state %d\n", pVM->enmVMState));
return VMSetError(pVM, VERR_VM_INVALID_VM_STATE, RT_SRC_POS, N_("Invalid VM state (%s) for restoring state from '%s'"),
VMR3GetStateName(pVM->enmVMState), pszFilename);
}
/*
* Change the state and perform the load.
*/
vmR3SetState(pVM, VMSTATE_LOADING);
int rc = SSMR3Load(pVM, pszFilename, SSMAFTER_RESUME, pfnProgress, pvUser);
if (RT_SUCCESS(rc))
{
/* Not paranoia anymore; the saved guest might use different hypervisor selectors. We must call VMR3Relocate. */
VMR3Relocate(pVM, 0);
vmR3SetState(pVM, VMSTATE_SUSPENDED);
}
else
{
vmR3SetState(pVM, VMSTATE_LOAD_FAILURE);
rc = VMSetError(pVM, rc, RT_SRC_POS, N_("Unable to restore the virtual machine's saved state from '%s'. It may be damaged or from an older version of VirtualBox. Please discard the saved state before starting the virtual machine"), pszFilename);
}
return rc;
}
/**
* Power Off the VM.
*
* @returns 0 on success.
* @returns VBox error code on failure.
* @param pVM VM which should be destroyed.
* @thread Any thread.
* @vmstate Suspended, Running, Guru Meditation, Load Failure
* @vmstateto Off
*/
VMMR3DECL(int) VMR3PowerOff(PVM pVM)
{
LogFlow(("VMR3PowerOff: pVM=%p\n", pVM));
/*
* Validate input.
*/
if (!pVM)
{
AssertMsgFailed(("Invalid VM pointer\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Request the operation in EMT. (in reverse order as VCPU 0 does the actual work)
*/
PVMREQ pReq = NULL;
int rc = VMR3ReqCall(pVM, VMCPUID_ALL_REVERSE, &pReq, RT_INDEFINITE_WAIT, (PFNRT)vmR3PowerOff, 1, pVM);
if (RT_SUCCESS(rc))
{
rc = pReq->iStatus;
VMR3ReqFree(pReq);
}
else
Assert(pReq == NULL);
LogFlow(("VMR3PowerOff: returns %Rrc\n", rc));
return rc;
}
/**
* Power Off the VM.
*
* @returns 0 on success.
* @returns VBox error code on failure.
* @param pVM VM which should be destroyed.
* @thread EMT.
*/
static DECLCALLBACK(int) vmR3PowerOff(PVM pVM)
{
LogFlow(("vmR3PowerOff: pVM=%p\n", pVM));
/*
* Validate input.
*/
if ( pVM->enmVMState != VMSTATE_RUNNING
&& pVM->enmVMState != VMSTATE_SUSPENDED
&& pVM->enmVMState != VMSTATE_LOAD_FAILURE
&& pVM->enmVMState != VMSTATE_FATAL_ERROR
&& pVM->enmVMState != VMSTATE_GURU_MEDITATION)
{
AssertMsgFailed(("Invalid VM state %d\n", pVM->enmVMState));
return VERR_VM_INVALID_VM_STATE;
}
/*
* EMT(0) does the actual power off work here *after* all the other EMTs
* have been thru and entered the STOPPED state.
*/
PVMCPU pVCpu = VMMGetCpu(pVM);
VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STOPPED);
if (pVCpu->idCpu != 0)
return VINF_EM_OFF;
/*
* For debugging purposes, we will log a summary of the guest state at this point.
*/
if (pVM->enmVMState != VMSTATE_GURU_MEDITATION)
{
/* @todo SMP support? */
PVMCPU pVCpu = VMMGetCpu(pVM);
/** @todo make the state dumping at VMR3PowerOff optional. */
RTLogRelPrintf("****************** Guest state at power off ******************\n");
DBGFR3Info(pVM, "cpumguest", "verbose", DBGFR3InfoLogRelHlp());
RTLogRelPrintf("***\n");
DBGFR3Info(pVM, "mode", NULL, DBGFR3InfoLogRelHlp());
RTLogRelPrintf("***\n");
DBGFR3Info(pVM, "activetimers", NULL, DBGFR3InfoLogRelHlp());
RTLogRelPrintf("***\n");
DBGFR3Info(pVM, "gdt", NULL, DBGFR3InfoLogRelHlp());
/** @todo dump guest call stack. */
#if 1 // temporary while debugging #1589
RTLogRelPrintf("***\n");
uint32_t esp = CPUMGetGuestESP(pVCpu);
if ( CPUMGetGuestSS(pVCpu) == 0
&& esp < _64K)
{
uint8_t abBuf[PAGE_SIZE];
RTLogRelPrintf("***\n"
"ss:sp=0000:%04x ", esp);
uint32_t Start = esp & ~(uint32_t)63;
int rc = PGMPhysSimpleReadGCPhys(pVM, abBuf, Start, 0x100);
if (RT_SUCCESS(rc))
RTLogRelPrintf("0000:%04x TO 0000:%04x:\n"
"%.*Rhxd\n",
Start, Start + 0x100 - 1,
0x100, abBuf);
else
RTLogRelPrintf("rc=%Rrc\n", rc);
/* grub ... */
if (esp < 0x2000 && esp > 0x1fc0)
{
rc = PGMPhysSimpleReadGCPhys(pVM, abBuf, 0x8000, 0x800);
if (RT_SUCCESS(rc))
RTLogRelPrintf("0000:8000 TO 0000:87ff:\n"
"%.*Rhxd\n",
0x800, abBuf);
}
/* microsoft cdrom hang ... */
if (true)
{
rc = PGMPhysSimpleReadGCPhys(pVM, abBuf, 0x8000, 0x200);
if (RT_SUCCESS(rc))
RTLogRelPrintf("2000:0000 TO 2000:01ff:\n"
"%.*Rhxd\n",
0x200, abBuf);
}
}
#endif
RTLogRelPrintf("************** End of Guest state at power off ***************\n");
}
/*
* Change the state to OFF and notify the components.
*/
vmR3SetState(pVM, VMSTATE_OFF);
PDMR3PowerOff(pVM);
return VINF_EM_OFF;
}
/**
* Destroys the VM.
* The VM must be powered off (or never really powered on) to call this function.
* The VM handle is destroyed and can no longer be used up successful return.
*
* @returns VBox status code.
* @param pVM VM which should be destroyed.
* @thread Any thread but the emulation thread.
* @vmstate Off, Created
* @vmstateto N/A
*/
VMMR3DECL(int) VMR3Destroy(PVM pVM)
{
LogFlow(("VMR3Destroy: pVM=%p\n", pVM));
/*
* Validate input.
*/
if (!pVM)
return VERR_INVALID_PARAMETER;
AssertPtrReturn(pVM, VERR_INVALID_POINTER);
AssertMsgReturn( pVM->enmVMState == VMSTATE_OFF
|| pVM->enmVMState == VMSTATE_CREATED,
("Invalid VM state %d\n", pVM->enmVMState),
VERR_VM_INVALID_VM_STATE);
/*
* Change VM state to destroying and unlink the VM.
*/
vmR3SetState(pVM, VMSTATE_DESTROYING);
/** @todo lock this when we start having multiple machines in a process... */
PUVM pUVM = pVM->pUVM; AssertPtr(pUVM);
if (g_pUVMsHead == pUVM)
g_pUVMsHead = pUVM->pNext;
else
{
PUVM pPrev = g_pUVMsHead;
while (pPrev && pPrev->pNext != pUVM)
pPrev = pPrev->pNext;
AssertMsgReturn(pPrev, ("pUVM=%p / pVM=%p is INVALID!\n", pUVM, pVM), VERR_INVALID_PARAMETER);
pPrev->pNext = pUVM->pNext;
}
pUVM->pNext = NULL;
/*
* Notify registered at destruction listeners.
* (That's the debugger console.)
*/
vmR3AtDtor(pVM);
/*
* If we are the EMT of VCPU 0, then we'll delay the cleanup till later.
*/
if (VMMGetCpuId(pVM) == 0)
{
pUVM->vm.s.fEMTDoesTheCleanup = true;
pUVM->vm.s.fTerminateEMT = true;
VM_FF_SET(pVM, VM_FF_TERMINATE);
/* Inform all other VCPUs too. */
for (VMCPUID idCpu = 1; idCpu < pVM->cCpus; idCpu++)
{
/*
* Request EMT to do the larger part of the destruction.
*/
PVMREQ pReq = NULL;
int rc = VMR3ReqCallU(pUVM, idCpu, &pReq, RT_INDEFINITE_WAIT, 0, (PFNRT)vmR3Destroy, 1, pVM);
if (RT_SUCCESS(rc))
rc = pReq->iStatus;
AssertRC(rc);
VMR3ReqFree(pReq);
}
}
else
{
/*
* Request EMT to do the larger part of the destruction. (in reverse order as VCPU 0 does the real cleanup)
*/
PVMREQ pReq = NULL;
int rc = VMR3ReqCallU(pUVM, VMCPUID_ALL_REVERSE, &pReq, RT_INDEFINITE_WAIT, 0, (PFNRT)vmR3Destroy, 1, pVM);
if (RT_SUCCESS(rc))
rc = pReq->iStatus;
AssertRC(rc);
VMR3ReqFree(pReq);
/*
* Now do the final bit where the heap and VM structures are freed up.
* This will also wait 30 secs for the emulation threads to terminate.
*/
vmR3DestroyUVM(pUVM, 30000);
}
LogFlow(("VMR3Destroy: returns VINF_SUCCESS\n"));
return VINF_SUCCESS;
}
/**
* Internal destruction worker.
*
* This will do nearly all of the job, including sacking the EMT.
*
* @returns VBox status.
* @param pVM VM handle.
*/
DECLCALLBACK(int) vmR3Destroy(PVM pVM)
{
PUVM pUVM = pVM->pUVM;
PVMCPU pVCpu = VMMGetCpu(pVM);
NOREF(pUVM);
LogFlow(("vmR3Destroy: pVM=%p pUVM=%p\n", pVM, pUVM));
VM_ASSERT_EMT(pVM);
/* Only VCPU 0 does the full cleanup. */
if (pVCpu->idCpu != 0)
return VINF_EM_TERMINATE;
/*
* Dump statistics to the log.
*/
#if defined(VBOX_WITH_STATISTICS) || defined(LOG_ENABLED)
RTLogFlags(NULL, "nodisabled nobuffered");
#endif
#ifdef VBOX_WITH_STATISTICS
STAMR3Dump(pVM, "*");
#else
LogRel(("************************* Statistics *************************\n"));
STAMR3DumpToReleaseLog(pVM, "*");
LogRel(("********************* End of statistics **********************\n"));
#endif
/*
* Destroy the VM components.
*/
int rc = TMR3Term(pVM);
AssertRC(rc);
#ifdef VBOX_WITH_DEBUGGER
rc = DBGCTcpTerminate(pVM, pUVM->vm.s.pvDBGC);
pVM->pUVM->vm.s.pvDBGC = NULL;
#endif
AssertRC(rc);
rc = DBGFR3Term(pVM);
AssertRC(rc);
rc = PDMR3Term(pVM);
AssertRC(rc);
rc = EMR3Term(pVM);
AssertRC(rc);
rc = IOMR3Term(pVM);
AssertRC(rc);
rc = CSAMR3Term(pVM);
AssertRC(rc);
rc = PATMR3Term(pVM);
AssertRC(rc);
rc = TRPMR3Term(pVM);
AssertRC(rc);
rc = SELMR3Term(pVM);
AssertRC(rc);
rc = REMR3Term(pVM);
AssertRC(rc);
rc = HWACCMR3Term(pVM);
AssertRC(rc);
rc = PGMR3Term(pVM);
AssertRC(rc);
rc = VMMR3Term(pVM); /* Terminates the ring-0 code! */
AssertRC(rc);
rc = CPUMR3Term(pVM);
AssertRC(rc);
SSMR3Term(pVM);
rc = PDMR3CritSectTerm(pVM);
AssertRC(rc);
rc = MMR3Term(pVM);
AssertRC(rc);
/*
* We're done in this thread (EMT).
*/
ASMAtomicUoWriteBool(&pVM->pUVM->vm.s.fTerminateEMT, true);
ASMAtomicWriteU32(&pVM->fGlobalForcedActions, VM_FF_TERMINATE);
LogFlow(("vmR3Destroy: returning %Rrc\n", VINF_EM_TERMINATE));
return VINF_EM_TERMINATE;
}
/**
* Destroys the shared VM structure, leaving only UVM behind.
*
* This is called by EMT right before terminating.
*
* @param pUVM The UVM handle.
*/
void vmR3DestroyFinalBitFromEMT(PUVM pUVM)
{
if (pUVM->pVM)
{
PVMCPU pVCpu = VMMGetCpu(pUVM->pVM);
/* VCPU 0 does all the cleanup work. */
if (pVCpu->idCpu != 0)
return;
/*
* Modify state and then terminate MM.
* (MM must be delayed until this point so we don't destroy the callbacks and the request packet.)
*/
vmR3SetState(pUVM->pVM, VMSTATE_TERMINATED);
/*
* Tell GVMM to destroy the VM and free its resources.
*/
int rc = SUPR3CallVMMR0Ex(pUVM->pVM->pVMR0, 0 /*idCpu*/, VMMR0_DO_GVMM_DESTROY_VM, 0, NULL);
AssertRC(rc);
pUVM->pVM = NULL;
}
/*
* Did an EMT call VMR3Destroy and end up having to do all the work?
*/
if (pUVM->vm.s.fEMTDoesTheCleanup)
vmR3DestroyUVM(pUVM, 30000);
}
/**
* Destroys the UVM portion.
*
* This is called as the final step in the VM destruction or as the cleanup
* in case of a creation failure. If EMT called VMR3Destroy, meaning
* VMINTUSERPERVM::fEMTDoesTheCleanup is true, it will call this as
* vmR3DestroyFinalBitFromEMT completes.
*
* @param pVM VM Handle.
* @param cMilliesEMTWait The number of milliseconds to wait for the emulation
* threads.
*/
static void vmR3DestroyUVM(PUVM pUVM, uint32_t cMilliesEMTWait)
{
/*
* Signal termination of each the emulation threads and
* wait for them to complete.
*/
/* Signal them. */
ASMAtomicUoWriteBool(&pUVM->vm.s.fTerminateEMT, true);
for (VMCPUID i = 0; i < pUVM->cCpus; i++)
{
ASMAtomicUoWriteBool(&pUVM->aCpus[i].vm.s.fTerminateEMT, true);
if (pUVM->pVM)
VM_FF_SET(pUVM->pVM, VM_FF_TERMINATE);
VMR3NotifyGlobalFFU(pUVM, VMNOTIFYFF_FLAGS_DONE_REM);
RTSemEventSignal(pUVM->aCpus[i].vm.s.EventSemWait);
}
/* Wait for them. */
uint64_t NanoTS = RTTimeNanoTS();
RTTHREAD hSelf = RTThreadSelf();
ASMAtomicUoWriteBool(&pUVM->vm.s.fTerminateEMT, true);
for (VMCPUID i = 0; i < pUVM->cCpus; i++)
{
RTTHREAD hThread = pUVM->aCpus[i].vm.s.ThreadEMT;
if ( hThread != NIL_RTTHREAD
&& hThread != hSelf)
{
uint64_t cMilliesElapsed = (RTTimeNanoTS() - NanoTS) / 1000000;
int rc2 = RTThreadWait(hThread,
cMilliesElapsed < cMilliesEMTWait
? RT_MAX(cMilliesEMTWait - cMilliesElapsed, 2000)
: 2000,
NULL);
if (rc2 == VERR_TIMEOUT) /* avoid the assertion when debugging. */
rc2 = RTThreadWait(hThread, 1000, NULL);
AssertLogRelMsgRC(rc2, ("i=%u rc=%Rrc\n", i, rc2));
if (RT_SUCCESS(rc2))
pUVM->aCpus[0].vm.s.ThreadEMT = NIL_RTTHREAD;
}
}
/* Cleanup the semaphores. */
for (VMCPUID i = 0; i < pUVM->cCpus; i++)
{
RTSemEventDestroy(pUVM->aCpus[i].vm.s.EventSemWait);
pUVM->aCpus[i].vm.s.EventSemWait = NIL_RTSEMEVENT;
}
/*
* Free the event semaphores associated with the request packets.
*/
unsigned cReqs = 0;
for (unsigned i = 0; i < RT_ELEMENTS(pUVM->vm.s.apReqFree); i++)
{
PVMREQ pReq = pUVM->vm.s.apReqFree[i];
pUVM->vm.s.apReqFree[i] = NULL;
for (; pReq; pReq = pReq->pNext, cReqs++)
{
pReq->enmState = VMREQSTATE_INVALID;
RTSemEventDestroy(pReq->EventSem);
}
}
Assert(cReqs == pUVM->vm.s.cReqFree); NOREF(cReqs);
/*
* Kill all queued requests. (There really shouldn't be any!)
*/
for (unsigned i = 0; i < 10; i++)
{
PVMREQ pReqHead = (PVMREQ)ASMAtomicXchgPtr((void *volatile *)&pUVM->vm.s.pReqs, NULL);
AssertMsg(!pReqHead, ("This isn't supposed to happen! VMR3Destroy caller has to serialize this.\n"));
if (!pReqHead)
break;
for (PVMREQ pReq = pReqHead; pReq; pReq = pReq->pNext)
{
ASMAtomicUoWriteSize(&pReq->iStatus, VERR_INTERNAL_ERROR);
ASMAtomicWriteSize(&pReq->enmState, VMREQSTATE_INVALID);
RTSemEventSignal(pReq->EventSem);
RTThreadSleep(2);
RTSemEventDestroy(pReq->EventSem);
}
/* give them a chance to respond before we free the request memory. */
RTThreadSleep(32);
}
/*
* Now all queued VCPU requests (again, there shouldn't be any).
*/
for (VMCPUID i = 0; i < pUVM->cCpus; i++)
{
PUVMCPU pUVCpu = &pUVM->aCpus[i];
for (unsigned i = 0; i < 10; i++)
{
PVMREQ pReqHead = (PVMREQ)ASMAtomicXchgPtr((void *volatile *)&pUVCpu->vm.s.pReqs, NULL);
AssertMsg(!pReqHead, ("This isn't supposed to happen! VMR3Destroy caller has to serialize this.\n"));
if (!pReqHead)
break;
for (PVMREQ pReq = pReqHead; pReq; pReq = pReq->pNext)
{
ASMAtomicUoWriteSize(&pReq->iStatus, VERR_INTERNAL_ERROR);
ASMAtomicWriteSize(&pReq->enmState, VMREQSTATE_INVALID);
RTSemEventSignal(pReq->EventSem);
RTThreadSleep(2);
RTSemEventDestroy(pReq->EventSem);
}
/* give them a chance to respond before we free the request memory. */
RTThreadSleep(32);
}
}
/*
* Make sure the VMMR0.r0 module and whatever else is unloaded.
*/
PDMR3TermUVM(pUVM);
/*
* Terminate the support library if initialized.
*/
if (pUVM->vm.s.pSession)
{
int rc = SUPR3Term(false /*fForced*/);
AssertRC(rc);
pUVM->vm.s.pSession = NIL_RTR0PTR;
}
/*
* Destroy the MM heap and free the UVM structure.
*/
MMR3TermUVM(pUVM);
STAMR3TermUVM(pUVM);
#ifdef LOG_ENABLED
RTLogSetCustomPrefixCallback(NULL, NULL, NULL);
#endif
RTTlsFree(pUVM->vm.s.idxTLS);
ASMAtomicUoWriteU32(&pUVM->u32Magic, UINT32_MAX);
RTMemFree(pUVM);
RTLogFlush(NULL);
}
/**
* Enumerates the VMs in this process.
*
* @returns Pointer to the next VM.
* @returns NULL when no more VMs.
* @param pVMPrev The previous VM
* Use NULL to start the enumeration.
*/
VMMR3DECL(PVM) VMR3EnumVMs(PVM pVMPrev)
{
/*
* This is quick and dirty. It has issues with VM being
* destroyed during the enumeration.
*/
PUVM pNext;
if (pVMPrev)
pNext = pVMPrev->pUVM->pNext;
else
pNext = g_pUVMsHead;
return pNext ? pNext->pVM : NULL;
}
/**
* Registers an at VM destruction callback.
*
* @returns VBox status code.
* @param pfnAtDtor Pointer to callback.
* @param pvUser User argument.
*/
VMMR3DECL(int) VMR3AtDtorRegister(PFNVMATDTOR pfnAtDtor, void *pvUser)
{
/*
* Check if already registered.
*/
VM_ATDTOR_LOCK();
PVMATDTOR pCur = g_pVMAtDtorHead;
while (pCur)
{
if (pfnAtDtor == pCur->pfnAtDtor)
{
VM_ATDTOR_UNLOCK();
AssertMsgFailed(("Already registered at destruction callback %p!\n", pfnAtDtor));
return VERR_INVALID_PARAMETER;
}
/* next */
pCur = pCur->pNext;
}
VM_ATDTOR_UNLOCK();
/*
* Allocate new entry.
*/
PVMATDTOR pVMAtDtor = (PVMATDTOR)RTMemAlloc(sizeof(*pVMAtDtor));
if (!pVMAtDtor)
return VERR_NO_MEMORY;
VM_ATDTOR_LOCK();
pVMAtDtor->pfnAtDtor = pfnAtDtor;
pVMAtDtor->pvUser = pvUser;
pVMAtDtor->pNext = g_pVMAtDtorHead;
g_pVMAtDtorHead = pVMAtDtor;
VM_ATDTOR_UNLOCK();
return VINF_SUCCESS;
}
/**
* Deregisters an at VM destruction callback.
*
* @returns VBox status code.
* @param pfnAtDtor Pointer to callback.
*/
VMMR3DECL(int) VMR3AtDtorDeregister(PFNVMATDTOR pfnAtDtor)
{
/*
* Find it, unlink it and free it.
*/
VM_ATDTOR_LOCK();
PVMATDTOR pPrev = NULL;
PVMATDTOR pCur = g_pVMAtDtorHead;
while (pCur)
{
if (pfnAtDtor == pCur->pfnAtDtor)
{
if (pPrev)
pPrev->pNext = pCur->pNext;
else
g_pVMAtDtorHead = pCur->pNext;
pCur->pNext = NULL;
VM_ATDTOR_UNLOCK();
RTMemFree(pCur);
return VINF_SUCCESS;
}
/* next */
pPrev = pCur;
pCur = pCur->pNext;
}
VM_ATDTOR_UNLOCK();
return VERR_INVALID_PARAMETER;
}
/**
* Walks the list of at VM destructor callbacks.
* @param pVM The VM which is about to be destroyed.
*/
static void vmR3AtDtor(PVM pVM)
{
/*
* Find it, unlink it and free it.
*/
VM_ATDTOR_LOCK();
for (PVMATDTOR pCur = g_pVMAtDtorHead; pCur; pCur = pCur->pNext)
pCur->pfnAtDtor(pVM, pCur->pvUser);
VM_ATDTOR_UNLOCK();
}
/**
* Reset the current VM.
*
* @returns VBox status code.
* @param pVM VM to reset.
*/
VMMR3DECL(int) VMR3Reset(PVM pVM)
{
int rc = VINF_SUCCESS;
/*
* Check the state.
*/
if (!pVM)
return VERR_INVALID_PARAMETER;
if ( pVM->enmVMState != VMSTATE_RUNNING
&& pVM->enmVMState != VMSTATE_SUSPENDED)
{
AssertMsgFailed(("Invalid VM state %d\n", pVM->enmVMState));
return VERR_VM_INVALID_VM_STATE;
}
/*
* Queue reset request to the emulation thread
* and wait for it to be processed. (in reverse order as VCPU 0 does the real cleanup)
*/
PVMREQ pReq = NULL;
rc = VMR3ReqCall(pVM, VMCPUID_ALL_REVERSE, &pReq, RT_INDEFINITE_WAIT, (PFNRT)vmR3Reset, 1, pVM);
if (RT_SUCCESS(rc))
rc = pReq->iStatus;
AssertRC(rc);
VMR3ReqFree(pReq);
return rc;
}
/**
* Worker which checks integrity of some internal structures.
* This is yet another attempt to track down that AVL tree crash.
*/
static void vmR3CheckIntegrity(PVM pVM)
{
#ifdef VBOX_STRICT
int rc = PGMR3CheckIntegrity(pVM);
AssertReleaseRC(rc);
#endif
}
/**
* Reset request processor.
*
* This is called by the emulation threads as a response to the
* reset request issued by VMR3Reset().
*
* @returns VBox status code.
* @param pVM VM to reset.
*/
static DECLCALLBACK(int) vmR3Reset(PVM pVM)
{
PVMCPU pVCpu = VMMGetCpu(pVM);
/*
* EMT(0) does the full cleanup *after* all the other EMTs has been
* thru here and been told to enter the EMSTATE_WAIT_SIPI state.
*/
VMCPU_ASSERT_STATE(pVCpu, VMCPUSTATE_STARTED);
/* Clear all pending forced actions. */
VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_ALL_MASK & ~VMCPU_FF_REQUEST);
if (pVCpu->idCpu != 0)
{
CPUMR3ResetCpu(pVCpu);
return VINF_EM_RESET;
}
/*
* As a safety precaution we temporarily change the state while resetting.
* (If VMR3Reset was not called from EMT we might have change state... let's ignore that fact for now.)
*/
VMSTATE enmVMState = pVM->enmVMState;
Assert(enmVMState == VMSTATE_SUSPENDED || enmVMState == VMSTATE_RUNNING);
vmR3SetState(pVM, VMSTATE_RESETTING);
vmR3CheckIntegrity(pVM);
/*
* Reset the VM components.
*/
PATMR3Reset(pVM);
CSAMR3Reset(pVM);
PGMR3Reset(pVM); /* We clear VM RAM in PGMR3Reset. It's vital PDMR3Reset is executed
* _afterwards_. E.g. ACPI sets up RAM tables during init/reset. */
MMR3Reset(pVM);
PDMR3Reset(pVM);
SELMR3Reset(pVM);
TRPMR3Reset(pVM);
vmR3AtResetU(pVM->pUVM);
REMR3Reset(pVM);
IOMR3Reset(pVM);
CPUMR3Reset(pVM);
TMR3Reset(pVM);
EMR3Reset(pVM);
HWACCMR3Reset(pVM); /* This must come *after* PATM, CSAM, CPUM, SELM and TRPM. */
#ifdef LOG_ENABLED
/*
* Debug logging.
*/
RTLogPrintf("\n\nThe VM was reset:\n");
DBGFR3Info(pVM, "cpum", "verbose", NULL);
#endif
/*
* Restore the state.
*/
vmR3CheckIntegrity(pVM);
Assert(pVM->enmVMState == VMSTATE_RESETTING);
vmR3SetState(pVM, enmVMState);
return VINF_EM_RESET;
}
/**
* Walks the list of at VM reset callbacks and calls them
*
* @returns VBox status code.
* Any failure is fatal.
* @param pUVM Pointe to the user mode VM structure.
*/
static int vmR3AtResetU(PUVM pUVM)
{
/*
* Walk the list and call them all.
*/
int rc = VINF_SUCCESS;
for (PVMATRESET pCur = pUVM->vm.s.pAtReset; pCur; pCur = pCur->pNext)
{
/* do the call */
switch (pCur->enmType)
{
case VMATRESETTYPE_DEV:
rc = pCur->u.Dev.pfnCallback(pCur->u.Dev.pDevIns, pCur->pvUser);
break;
case VMATRESETTYPE_INTERNAL:
rc = pCur->u.Internal.pfnCallback(pUVM->pVM, pCur->pvUser);
break;
case VMATRESETTYPE_EXTERNAL:
pCur->u.External.pfnCallback(pCur->pvUser);
break;
default:
AssertMsgFailed(("Invalid at-reset type %d!\n", pCur->enmType));
return VERR_INTERNAL_ERROR;
}
if (RT_FAILURE(rc))
{
AssertMsgFailed(("At-reset handler %s failed with rc=%d\n", pCur->pszDesc, rc));
return rc;
}
}
return VINF_SUCCESS;
}
/**
* Internal registration function
*/
static int vmr3AtResetRegisterU(PUVM pUVM, void *pvUser, const char *pszDesc, PVMATRESET *ppNew)
{
/*
* Allocate restration structure.
*/
PVMATRESET pNew = (PVMATRESET)MMR3HeapAllocU(pUVM, MM_TAG_VM, sizeof(*pNew));
if (pNew)
{
/* fill data. */
pNew->pszDesc = pszDesc;
pNew->pvUser = pvUser;
/* insert */
pNew->pNext = *pUVM->vm.s.ppAtResetNext;
*pUVM->vm.s.ppAtResetNext = pNew;
pUVM->vm.s.ppAtResetNext = &pNew->pNext;
*ppNew = pNew;
return VINF_SUCCESS;
}
return VERR_NO_MEMORY;
}
/**
* Registers an at VM reset callback.
*
* @returns VBox status code.
* @param pVM The VM.
* @param pDevInst Device instance.
* @param pfnCallback Callback function.
* @param pvUser User argument.
* @param pszDesc Description (optional).
*/
VMMR3DECL(int) VMR3AtResetRegister(PVM pVM, PPDMDEVINS pDevInst, PFNVMATRESET pfnCallback, void *pvUser, const char *pszDesc)
{
/*
* Validate.
*/
if (!pDevInst)
{
AssertMsgFailed(("pDevIns is NULL!\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Create the new entry.
*/
PVMATRESET pNew;
int rc = vmr3AtResetRegisterU(pVM->pUVM, pvUser, pszDesc, &pNew);
if (RT_SUCCESS(rc))
{
/*
* Fill in type data.
*/
pNew->enmType = VMATRESETTYPE_DEV;
pNew->u.Dev.pfnCallback = pfnCallback;
pNew->u.Dev.pDevIns = pDevInst;
}
return rc;
}
/**
* Registers an at VM reset internal callback.
*
* @returns VBox status code.
* @param pVM The VM.
* @param pfnCallback Callback function.
* @param pvUser User argument.
* @param pszDesc Description (optional).
*/
VMMR3DECL(int) VMR3AtResetRegisterInternal(PVM pVM, PFNVMATRESETINT pfnCallback, void *pvUser, const char *pszDesc)
{
/*
* Validate.
*/
if (!pfnCallback)
{
AssertMsgFailed(("pfnCallback is NULL!\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Create the new entry.
*/
PVMATRESET pNew;
int rc = vmr3AtResetRegisterU(pVM->pUVM, pvUser, pszDesc, &pNew);
if (RT_SUCCESS(rc))
{
/*
* Fill in type data.
*/
pNew->enmType = VMATRESETTYPE_INTERNAL;
pNew->u.Internal.pfnCallback = pfnCallback;
}
return rc;
}
/**
* Registers an at VM reset external callback.
*
* @returns VBox status code.
* @param pVM The VM.
* @param pfnCallback Callback function.
* @param pvUser User argument.
* @param pszDesc Description (optional).
*/
VMMR3DECL(int) VMR3AtResetRegisterExternal(PVM pVM, PFNVMATRESETEXT pfnCallback, void *pvUser, const char *pszDesc)
{
/*
* Validate.
*/
if (!pfnCallback)
{
AssertMsgFailed(("pfnCallback is NULL!\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Create the new entry.
*/
PVMATRESET pNew;
int rc = vmr3AtResetRegisterU(pVM->pUVM, pvUser, pszDesc, &pNew);
if (RT_SUCCESS(rc))
{
/*
* Fill in type data.
*/
pNew->enmType = VMATRESETTYPE_EXTERNAL;
pNew->u.External.pfnCallback = pfnCallback;
}
return rc;
}
/**
* Unlinks and frees a callback.
*
* @returns Pointer to the next callback structure.
* @param pUVM Pointer to the user mode VM structure.
* @param pCur The one to free.
* @param pPrev The one before pCur.
*/
static PVMATRESET vmr3AtResetFreeU(PUVM pUVM, PVMATRESET pCur, PVMATRESET pPrev)
{
/*
* Unlink it.
*/
PVMATRESET pNext = pCur->pNext;
if (pPrev)
{
pPrev->pNext = pNext;
if (!pNext)
pUVM->vm.s.ppAtResetNext = &pPrev->pNext;
}
else
{
pUVM->vm.s.pAtReset = pNext;
if (!pNext)
pUVM->vm.s.ppAtResetNext = &pUVM->vm.s.pAtReset;
}
/*
* Free it.
*/
MMR3HeapFree(pCur);
return pNext;
}
/**
* Deregisters an at VM reset callback.
*
* @returns VBox status code.
* @param pVM The VM.
* @param pDevIns Device instance.
* @param pfnCallback Callback function.
*/
VMMR3DECL(int) VMR3AtResetDeregister(PVM pVM, PPDMDEVINS pDevIns, PFNVMATRESET pfnCallback)
{
int rc = VERR_VM_ATRESET_NOT_FOUND;
PVMATRESET pPrev = NULL;
PVMATRESET pCur = pVM->pUVM->vm.s.pAtReset;
while (pCur)
{
if ( pCur->enmType == VMATRESETTYPE_DEV
&& pCur->u.Dev.pDevIns == pDevIns
&& ( !pfnCallback
|| pCur->u.Dev.pfnCallback == pfnCallback))
{
pCur = vmr3AtResetFreeU(pVM->pUVM, pCur, pPrev);
rc = VINF_SUCCESS;
}
else
{
pPrev = pCur;
pCur = pCur->pNext;
}
}
AssertRC(rc);
return rc;
}
/**
* Deregisters an at VM reset internal callback.
*
* @returns VBox status code.
* @param pVM The VM.
* @param pfnCallback Callback function.
*/
VMMR3DECL(int) VMR3AtResetDeregisterInternal(PVM pVM, PFNVMATRESETINT pfnCallback)
{
int rc = VERR_VM_ATRESET_NOT_FOUND;
PVMATRESET pPrev = NULL;
PVMATRESET pCur = pVM->pUVM->vm.s.pAtReset;
while (pCur)
{
if ( pCur->enmType == VMATRESETTYPE_INTERNAL
&& pCur->u.Internal.pfnCallback == pfnCallback)
{
pCur = vmr3AtResetFreeU(pVM->pUVM, pCur, pPrev);
rc = VINF_SUCCESS;
}
else
{
pPrev = pCur;
pCur = pCur->pNext;
}
}
AssertRC(rc);
return rc;
}
/**
* Deregisters an at VM reset external callback.
*
* @returns VBox status code.
* @param pVM The VM.
* @param pfnCallback Callback function.
*/
VMMR3DECL(int) VMR3AtResetDeregisterExternal(PVM pVM, PFNVMATRESETEXT pfnCallback)
{
int rc = VERR_VM_ATRESET_NOT_FOUND;
PVMATRESET pPrev = NULL;
PVMATRESET pCur = pVM->pUVM->vm.s.pAtReset;
while (pCur)
{
if ( pCur->enmType == VMATRESETTYPE_INTERNAL
&& pCur->u.External.pfnCallback == pfnCallback)
{
pCur = vmr3AtResetFreeU(pVM->pUVM, pCur, pPrev);
rc = VINF_SUCCESS;
}
else
{
pPrev = pCur;
pCur = pCur->pNext;
}
}
AssertRC(rc);
return rc;
}
/**
* Gets the current VM state.
*
* @returns The current VM state.
* @param pVM VM handle.
* @thread Any
*/
VMMR3DECL(VMSTATE) VMR3GetState(PVM pVM)
{
return pVM->enmVMState;
}
/**
* Gets the state name string for a VM state.
*
* @returns Pointer to the state name. (readonly)
* @param enmState The state.
*/
VMMR3DECL(const char *) VMR3GetStateName(VMSTATE enmState)
{
switch (enmState)
{
case VMSTATE_CREATING: return "CREATING";
case VMSTATE_CREATED: return "CREATED";
case VMSTATE_LOADING: return "LOADING";
case VMSTATE_RUNNING: return "RUNNING";
case VMSTATE_LS_RUNNING: return "LS_RUNNING";
case VMSTATE_RESETTING: return "RESETTING";
case VMSTATE_LS_RESETTING: return "LS_RESETTING";
case VMSTATE_SUSPENDED: return "SUSPENDED";
case VMSTATE_LS_SUSPENDING: return "LS_SUSPENDING";
case VMSTATE_SAVING: return "SAVING";
case VMSTATE_LS_SAVING: return "LS_SAVING";
case VMSTATE_LS_POWERING_OFF: return "LS_POWERING_OFF";
case VMSTATE_FATAL_ERROR: return "FATAL_ERROR";
case VMSTATE_LS_FATAL_ERROR: return "LS_FATAL_ERROR";
case VMSTATE_GURU_MEDITATION: return "GURU_MEDITATION";
case VMSTATE_LS_GURU_MEDIATION: return "LS_GURU_MEDIATION";
case VMSTATE_LOAD_FAILURE: return "LOAD_FAILURE";
case VMSTATE_OFF: return "OFF";
case VMSTATE_DESTROYING: return "DESTROYING";
case VMSTATE_TERMINATED: return "TERMINATED";
default:
AssertMsgFailed(("Unknown state %d\n", enmState));
return "Unknown!\n";
}
}
/**
* Sets the current VM state.
*
* @returns The current VM state.
* @param pVM VM handle.
* @param enmStateNew The new state.
*/
void vmR3SetState(PVM pVM, VMSTATE enmStateNew)
{
#ifdef DEBUG_bird /** @todo remove when sorted out. */
VM_ASSERT_EMT0(pVM);
#endif
/*
* Validate state machine transitions before doing the actual change.
*/
VMSTATE enmStateOld = pVM->enmVMState;
switch (enmStateOld)
{
case VMSTATE_OFF:
Assert(enmStateNew != VMSTATE_GURU_MEDITATION);
break;
default:
/** @todo full validation. */
break;
}
pVM->enmVMState = enmStateNew;
LogRel(("Changing the VM state from '%s' to '%s'.\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)));
/*
* Call the at state change callbacks.
*/
for (PVMATSTATE pCur = pVM->pUVM->vm.s.pAtState; pCur; pCur = pCur->pNext)
{
pCur->pfnAtState(pVM, enmStateNew, enmStateOld, pCur->pvUser);
if ( pVM->enmVMState != enmStateNew
&& pVM->enmVMState == VMSTATE_DESTROYING)
break;
AssertMsg(pVM->enmVMState == enmStateNew,
("You are not allowed to change the state while in the change callback, except "
"from destroying the VM. There are restrictions in the way the state changes "
"are propagated up to the EM execution loop and it makes the program flow very "
"difficult to follow.\n"));
}
}
/**
* Registers a VM state change callback.
*
* You are not allowed to call any function which changes the VM state from a
* state callback, except VMR3Destroy().
*
* @returns VBox status code.
* @param pVM VM handle.
* @param pfnAtState Pointer to callback.
* @param pvUser User argument.
* @thread Any.
*/
VMMR3DECL(int) VMR3AtStateRegister(PVM pVM, PFNVMATSTATE pfnAtState, void *pvUser)
{
LogFlow(("VMR3AtStateRegister: pfnAtState=%p pvUser=%p\n", pfnAtState, pvUser));
/*
* Validate input.
*/
if (!pfnAtState)
{
AssertMsgFailed(("callback is required\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Make sure we're in EMT (to avoid the logging).
*/
PVMREQ pReq;
int rc = VMR3ReqCall(pVM, VMCPUID_ANY, &pReq, RT_INDEFINITE_WAIT, (PFNRT)vmR3AtStateRegisterU, 3, pVM->pUVM, pfnAtState, pvUser);
if (RT_FAILURE(rc))
return rc;
rc = pReq->iStatus;
VMR3ReqFree(pReq);
LogFlow(("VMR3AtStateRegister: returns %Rrc\n", rc));
return rc;
}
/**
* Registers a VM state change callback.
*
* @returns VBox status code.
* @param pUVM Pointer to the user mode VM structure.
* @param pfnAtState Pointer to callback.
* @param pvUser User argument.
* @thread EMT
*/
static DECLCALLBACK(int) vmR3AtStateRegisterU(PUVM pUVM, PFNVMATSTATE pfnAtState, void *pvUser)
{
/*
* Allocate a new record.
*/
PVMATSTATE pNew = (PVMATSTATE)MMR3HeapAllocU(pUVM, MM_TAG_VM, sizeof(*pNew));
if (!pNew)
return VERR_NO_MEMORY;
/* fill */
pNew->pfnAtState = pfnAtState;
pNew->pvUser = pvUser;
/* insert */
pNew->pNext = *pUVM->vm.s.ppAtStateNext;
*pUVM->vm.s.ppAtStateNext = pNew;
pUVM->vm.s.ppAtStateNext = &pNew->pNext;
return VINF_SUCCESS;
}
/**
* Deregisters a VM state change callback.
*
* @returns VBox status code.
* @param pVM VM handle.
* @param pfnAtState Pointer to callback.
* @param pvUser User argument.
* @thread Any.
*/
VMMR3DECL(int) VMR3AtStateDeregister(PVM pVM, PFNVMATSTATE pfnAtState, void *pvUser)
{
LogFlow(("VMR3AtStateDeregister: pfnAtState=%p pvUser=%p\n", pfnAtState, pvUser));
/*
* Validate input.
*/
if (!pfnAtState)
{
AssertMsgFailed(("callback is required\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Make sure we're in EMT (to avoid the logging).
*/
PVMREQ pReq;
int rc = VMR3ReqCall(pVM, VMCPUID_ANY, &pReq, RT_INDEFINITE_WAIT, (PFNRT)vmR3AtStateDeregisterU, 3, pVM->pUVM, pfnAtState, pvUser);
if (RT_FAILURE(rc))
return rc;
rc = pReq->iStatus;
VMR3ReqFree(pReq);
LogFlow(("VMR3AtStateDeregister: returns %Rrc\n", rc));
return rc;
}
/**
* Deregisters a VM state change callback.
*
* @returns VBox status code.
* @param pUVM Pointer to the user mode VM structure.
* @param pfnAtState Pointer to callback.
* @param pvUser User argument.
* @thread EMT
*/
static DECLCALLBACK(int) vmR3AtStateDeregisterU(PUVM pUVM, PFNVMATSTATE pfnAtState, void *pvUser)
{
LogFlow(("vmR3AtStateDeregisterU: pfnAtState=%p pvUser=%p\n", pfnAtState, pvUser));
/*
* Search the list for the entry.
*/
PVMATSTATE pPrev = NULL;
PVMATSTATE pCur = pUVM->vm.s.pAtState;
while ( pCur
&& ( pCur->pfnAtState != pfnAtState
|| pCur->pvUser != pvUser))
{
pPrev = pCur;
pCur = pCur->pNext;
}
if (!pCur)
{
AssertMsgFailed(("pfnAtState=%p was not found\n", pfnAtState));
return VERR_FILE_NOT_FOUND;
}
/*
* Unlink it.
*/
if (pPrev)
{
pPrev->pNext = pCur->pNext;
if (!pCur->pNext)
pUVM->vm.s.ppAtStateNext = &pPrev->pNext;
}
else
{
pUVM->vm.s.pAtState = pCur->pNext;
if (!pCur->pNext)
pUVM->vm.s.ppAtStateNext = &pUVM->vm.s.pAtState;
}
/*
* Free it.
*/
pCur->pfnAtState = NULL;
pCur->pNext = NULL;
MMR3HeapFree(pCur);
return VINF_SUCCESS;
}
/**
* Registers a VM error callback.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pfnAtError Pointer to callback.
* @param pvUser User argument.
* @thread Any.
*/
VMMR3DECL(int) VMR3AtErrorRegister(PVM pVM, PFNVMATERROR pfnAtError, void *pvUser)
{
return VMR3AtErrorRegisterU(pVM->pUVM, pfnAtError, pvUser);
}
/**
* Registers a VM error callback.
*
* @returns VBox status code.
* @param pUVM The VM handle.
* @param pfnAtError Pointer to callback.
* @param pvUser User argument.
* @thread Any.
*/
VMMR3DECL(int) VMR3AtErrorRegisterU(PUVM pUVM, PFNVMATERROR pfnAtError, void *pvUser)
{
LogFlow(("VMR3AtErrorRegister: pfnAtError=%p pvUser=%p\n", pfnAtError, pvUser));
AssertPtrReturn(pfnAtError, VERR_INVALID_PARAMETER);
/*
* Make sure we're in EMT (to avoid the logging).
*/
PVMREQ pReq;
int rc = VMR3ReqCallU(pUVM, VMCPUID_ANY, &pReq, RT_INDEFINITE_WAIT, 0, (PFNRT)vmR3AtErrorRegisterU, 3, pUVM, pfnAtError, pvUser);
if (RT_FAILURE(rc))
return rc;
rc = pReq->iStatus;
VMR3ReqFree(pReq);
LogFlow(("VMR3AtErrorRegister: returns %Rrc\n", rc));
return rc;
}
/**
* Registers a VM error callback.
*
* @returns VBox status code.
* @param pUVM Pointer to the user mode VM structure.
* @param pfnAtError Pointer to callback.
* @param pvUser User argument.
* @thread EMT
*/
static DECLCALLBACK(int) vmR3AtErrorRegisterU(PUVM pUVM, PFNVMATERROR pfnAtError, void *pvUser)
{
/*
* Allocate a new record.
*/
PVMATERROR pNew = (PVMATERROR)MMR3HeapAllocU(pUVM, MM_TAG_VM, sizeof(*pNew));
if (!pNew)
return VERR_NO_MEMORY;
/* fill */
pNew->pfnAtError = pfnAtError;
pNew->pvUser = pvUser;
/* insert */
pNew->pNext = *pUVM->vm.s.ppAtErrorNext;
*pUVM->vm.s.ppAtErrorNext = pNew;
pUVM->vm.s.ppAtErrorNext = &pNew->pNext;
return VINF_SUCCESS;
}
/**
* Deregisters a VM error callback.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pfnAtError Pointer to callback.
* @param pvUser User argument.
* @thread Any.
*/
VMMR3DECL(int) VMR3AtErrorDeregister(PVM pVM, PFNVMATERROR pfnAtError, void *pvUser)
{
LogFlow(("VMR3AtErrorDeregister: pfnAtError=%p pvUser=%p\n", pfnAtError, pvUser));
/*
* Validate input.
*/
if (!pfnAtError)
{
AssertMsgFailed(("callback is required\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Make sure we're in EMT (to avoid the logging).
*/
PVMREQ pReq;
int rc = VMR3ReqCall(pVM, VMCPUID_ANY, &pReq, RT_INDEFINITE_WAIT, (PFNRT)vmR3AtErrorDeregisterU, 3, pVM->pUVM, pfnAtError, pvUser);
if (RT_FAILURE(rc))
return rc;
rc = pReq->iStatus;
VMR3ReqFree(pReq);
LogFlow(("VMR3AtErrorDeregister: returns %Rrc\n", rc));
return rc;
}
/**
* Deregisters a VM error callback.
*
* @returns VBox status code.
* @param pUVM Pointer to the user mode VM structure.
* @param pfnAtError Pointer to callback.
* @param pvUser User argument.
* @thread EMT
*/
static DECLCALLBACK(int) vmR3AtErrorDeregisterU(PUVM pUVM, PFNVMATERROR pfnAtError, void *pvUser)
{
LogFlow(("vmR3AtErrorDeregisterU: pfnAtError=%p pvUser=%p\n", pfnAtError, pvUser));
/*
* Search the list for the entry.
*/
PVMATERROR pPrev = NULL;
PVMATERROR pCur = pUVM->vm.s.pAtError;
while ( pCur
&& ( pCur->pfnAtError != pfnAtError
|| pCur->pvUser != pvUser))
{
pPrev = pCur;
pCur = pCur->pNext;
}
if (!pCur)
{
AssertMsgFailed(("pfnAtError=%p was not found\n", pfnAtError));
return VERR_FILE_NOT_FOUND;
}
/*
* Unlink it.
*/
if (pPrev)
{
pPrev->pNext = pCur->pNext;
if (!pCur->pNext)
pUVM->vm.s.ppAtErrorNext = &pPrev->pNext;
}
else
{
pUVM->vm.s.pAtError = pCur->pNext;
if (!pCur->pNext)
pUVM->vm.s.ppAtErrorNext = &pUVM->vm.s.pAtError;
}
/*
* Free it.
*/
pCur->pfnAtError = NULL;
pCur->pNext = NULL;
MMR3HeapFree(pCur);
return VINF_SUCCESS;
}
/**
* Ellipsis to va_list wrapper for calling pfnAtError.
*/
static void vmR3SetErrorWorkerDoCall(PVM pVM, PVMATERROR pCur, int rc, RT_SRC_POS_DECL, const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
pCur->pfnAtError(pVM, pCur->pvUser, rc, RT_SRC_POS_ARGS, pszFormat, va);
va_end(va);
}
/**
* This is a worker function for GC and Ring-0 calls to VMSetError and VMSetErrorV.
* The message is found in VMINT.
*
* @param pVM The VM handle.
* @thread EMT.
*/
VMMR3DECL(void) VMR3SetErrorWorker(PVM pVM)
{
VM_ASSERT_EMT(pVM);
AssertReleaseMsgFailed(("And we have a winner! You get to implement Ring-0 and GC VMSetErrorV! Contrats!\n"));
/*
* Unpack the error (if we managed to format one).
*/
PVMERROR pErr = pVM->vm.s.pErrorR3;
const char *pszFile = NULL;
const char *pszFunction = NULL;
uint32_t iLine = 0;
const char *pszMessage;
int32_t rc = VERR_MM_HYPER_NO_MEMORY;
if (pErr)
{
AssertCompile(sizeof(const char) == sizeof(uint8_t));
if (pErr->offFile)
pszFile = (const char *)pErr + pErr->offFile;
iLine = pErr->iLine;
if (pErr->offFunction)
pszFunction = (const char *)pErr + pErr->offFunction;
if (pErr->offMessage)
pszMessage = (const char *)pErr + pErr->offMessage;
else
pszMessage = "No message!";
}
else
pszMessage = "No message! (Failed to allocate memory to put the error message in!)";
/*
* Call the at error callbacks.
*/
for (PVMATERROR pCur = pVM->pUVM->vm.s.pAtError; pCur; pCur = pCur->pNext)
vmR3SetErrorWorkerDoCall(pVM, pCur, rc, RT_SRC_POS_ARGS, "%s", pszMessage);
}
/**
* Creation time wrapper for vmR3SetErrorUV.
*
* @returns rc.
* @param pUVM Pointer to the user mode VM structure.
* @param rc The VBox status code.
* @param RT_SRC_POS_DECL The source position of this error.
* @param pszFormat Format string.
* @param ... The arguments.
* @thread Any thread.
*/
static int vmR3SetErrorU(PUVM pUVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
vmR3SetErrorUV(pUVM, rc, pszFile, iLine, pszFunction, pszFormat, &va);
va_end(va);
return rc;
}
/**
* Worker which calls everyone listening to the VM error messages.
*
* @param pUVM Pointer to the user mode VM structure.
* @param rc The VBox status code.
* @param RT_SRC_POS_DECL The source position of this error.
* @param pszFormat Format string.
* @param pArgs Pointer to the format arguments.
* @thread EMT
*/
DECLCALLBACK(void) vmR3SetErrorUV(PUVM pUVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list *pArgs)
{
#ifdef LOG_ENABLED
/*
* Log the error.
*/
RTLogPrintf("VMSetError: %s(%d) %s\n", pszFile, iLine, pszFunction);
va_list va3;
va_copy(va3, *pArgs);
RTLogPrintfV(pszFormat, va3);
va_end(va3);
RTLogPrintf("\n");
#endif
/*
* Make a copy of the message.
*/
if (pUVM->pVM)
vmSetErrorCopy(pUVM->pVM, rc, RT_SRC_POS_ARGS, pszFormat, *pArgs);
/*
* Call the at error callbacks.
*/
for (PVMATERROR pCur = pUVM->vm.s.pAtError; pCur; pCur = pCur->pNext)
{
va_list va2;
va_copy(va2, *pArgs);
pCur->pfnAtError(pUVM->pVM, pCur->pvUser, rc, RT_SRC_POS_ARGS, pszFormat, va2);
va_end(va2);
}
}
/**
* Registers a VM runtime error callback.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pfnAtRuntimeError Pointer to callback.
* @param pvUser User argument.
* @thread Any.
*/
VMMR3DECL(int) VMR3AtRuntimeErrorRegister(PVM pVM, PFNVMATRUNTIMEERROR pfnAtRuntimeError, void *pvUser)
{
LogFlow(("VMR3AtRuntimeErrorRegister: pfnAtRuntimeError=%p pvUser=%p\n", pfnAtRuntimeError, pvUser));
/*
* Validate input.
*/
if (!pfnAtRuntimeError)
{
AssertMsgFailed(("callback is required\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Make sure we're in EMT (to avoid the logging).
*/
PVMREQ pReq;
int rc = VMR3ReqCall(pVM, VMCPUID_ANY, &pReq, RT_INDEFINITE_WAIT, (PFNRT)vmR3AtRuntimeErrorRegisterU, 3, pVM->pUVM, pfnAtRuntimeError, pvUser);
if (RT_FAILURE(rc))
return rc;
rc = pReq->iStatus;
VMR3ReqFree(pReq);
LogFlow(("VMR3AtRuntimeErrorRegister: returns %Rrc\n", rc));
return rc;
}
/**
* Registers a VM runtime error callback.
*
* @returns VBox status code.
* @param pUVM Pointer to the user mode VM structure.
* @param pfnAtRuntimeError Pointer to callback.
* @param pvUser User argument.
* @thread EMT
*/
static DECLCALLBACK(int) vmR3AtRuntimeErrorRegisterU(PUVM pUVM, PFNVMATRUNTIMEERROR pfnAtRuntimeError, void *pvUser)
{
/*
* Allocate a new record.
*/
PVMATRUNTIMEERROR pNew = (PVMATRUNTIMEERROR)MMR3HeapAllocU(pUVM, MM_TAG_VM, sizeof(*pNew));
if (!pNew)
return VERR_NO_MEMORY;
/* fill */
pNew->pfnAtRuntimeError = pfnAtRuntimeError;
pNew->pvUser = pvUser;
/* insert */
pNew->pNext = *pUVM->vm.s.ppAtRuntimeErrorNext;
*pUVM->vm.s.ppAtRuntimeErrorNext = pNew;
pUVM->vm.s.ppAtRuntimeErrorNext = &pNew->pNext;
return VINF_SUCCESS;
}
/**
* Deregisters a VM runtime error callback.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pfnAtRuntimeError Pointer to callback.
* @param pvUser User argument.
* @thread Any.
*/
VMMR3DECL(int) VMR3AtRuntimeErrorDeregister(PVM pVM, PFNVMATRUNTIMEERROR pfnAtRuntimeError, void *pvUser)
{
LogFlow(("VMR3AtRuntimeErrorDeregister: pfnAtRuntimeError=%p pvUser=%p\n", pfnAtRuntimeError, pvUser));
/*
* Validate input.
*/
if (!pfnAtRuntimeError)
{
AssertMsgFailed(("callback is required\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Make sure we're in EMT (to avoid the logging).
*/
PVMREQ pReq;
int rc = VMR3ReqCall(pVM, VMCPUID_ANY, &pReq, RT_INDEFINITE_WAIT, (PFNRT)vmR3AtRuntimeErrorDeregisterU, 3, pVM->pUVM, pfnAtRuntimeError, pvUser);
if (RT_FAILURE(rc))
return rc;
rc = pReq->iStatus;
VMR3ReqFree(pReq);
LogFlow(("VMR3AtRuntimeErrorDeregister: returns %Rrc\n", rc));
return rc;
}
/**
* Deregisters a VM runtime error callback.
*
* @returns VBox status code.
* @param pUVM Pointer to the user mode VM structure.
* @param pfnAtRuntimeError Pointer to callback.
* @param pvUser User argument.
* @thread EMT
*/
static DECLCALLBACK(int) vmR3AtRuntimeErrorDeregisterU(PUVM pUVM, PFNVMATRUNTIMEERROR pfnAtRuntimeError, void *pvUser)
{
LogFlow(("vmR3AtRuntimeErrorDeregisterU: pfnAtRuntimeError=%p pvUser=%p\n", pfnAtRuntimeError, pvUser));
/*
* Search the list for the entry.
*/
PVMATRUNTIMEERROR pPrev = NULL;
PVMATRUNTIMEERROR pCur = pUVM->vm.s.pAtRuntimeError;
while ( pCur
&& ( pCur->pfnAtRuntimeError != pfnAtRuntimeError
|| pCur->pvUser != pvUser))
{
pPrev = pCur;
pCur = pCur->pNext;
}
if (!pCur)
{
AssertMsgFailed(("pfnAtRuntimeError=%p was not found\n", pfnAtRuntimeError));
return VERR_FILE_NOT_FOUND;
}
/*
* Unlink it.
*/
if (pPrev)
{
pPrev->pNext = pCur->pNext;
if (!pCur->pNext)
pUVM->vm.s.ppAtRuntimeErrorNext = &pPrev->pNext;
}
else
{
pUVM->vm.s.pAtRuntimeError = pCur->pNext;
if (!pCur->pNext)
pUVM->vm.s.ppAtRuntimeErrorNext = &pUVM->vm.s.pAtRuntimeError;
}
/*
* Free it.
*/
pCur->pfnAtRuntimeError = NULL;
pCur->pNext = NULL;
MMR3HeapFree(pCur);
return VINF_SUCCESS;
}
/**
* Worker for VMR3SetRuntimeErrorWorker and vmR3SetRuntimeErrorV.
*
* This does the common parts after the error has been saved / retrieved.
*
* @returns VBox status code with modifications, see VMSetRuntimeErrorV.
*
* @param pVM The VM handle.
* @param fFlags The error flags.
* @param pszErrorId Error ID string.
* @param pszFormat Format string.
* @param pVa Pointer to the format arguments.
*/
static int vmR3SetRuntimeErrorCommon(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list *pVa)
{
LogRel(("VM: Raising runtime error '%s' (fFlags=%#x)\n", pszErrorId, fFlags));
/*
* Take actions before the call.
*/
int rc = VINF_SUCCESS;
if (fFlags & VMSETRTERR_FLAGS_FATAL)
rc = vmR3SuspendCommon(pVM, true /*fFatal*/);
else if (fFlags & VMSETRTERR_FLAGS_SUSPEND)
rc = VMR3Suspend(pVM);
/*
* Do the callback round.
*/
for (PVMATRUNTIMEERROR pCur = pVM->pUVM->vm.s.pAtRuntimeError; pCur; pCur = pCur->pNext)
{
va_list va;
va_copy(va, *pVa);
pCur->pfnAtRuntimeError(pVM, pCur->pvUser, fFlags, pszErrorId, pszFormat, va);
va_end(va);
}
return rc;
}
/**
* Ellipsis to va_list wrapper for calling vmR3SetRuntimeErrorCommon.
*/
static int vmR3SetRuntimeErrorCommonF(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
int rc = vmR3SetRuntimeErrorCommon(pVM, fFlags, pszErrorId, pszFormat, &va);
va_end(va);
return rc;
}
/**
* This is a worker function for RC and Ring-0 calls to VMSetError and
* VMSetErrorV.
*
* The message is found in VMINT.
*
* @returns VBox status code, see VMSetRuntimeError.
* @param pVM The VM handle.
* @thread EMT.
*/
VMMR3DECL(int) VMR3SetRuntimeErrorWorker(PVM pVM)
{
VM_ASSERT_EMT(pVM);
AssertReleaseMsgFailed(("And we have a winner! You get to implement Ring-0 and GC VMSetRuntimeErrorV! Congrats!\n"));
/*
* Unpack the error (if we managed to format one).
*/
const char *pszErrorId = "SetRuntimeError";
const char *pszMessage = "No message!";
uint32_t fFlags = VMSETRTERR_FLAGS_FATAL;
PVMRUNTIMEERROR pErr = pVM->vm.s.pRuntimeErrorR3;
if (pErr)
{
AssertCompile(sizeof(const char) == sizeof(uint8_t));
if (pErr->offErrorId)
pszErrorId = (const char *)pErr + pErr->offErrorId;
if (pErr->offMessage)
pszMessage = (const char *)pErr + pErr->offMessage;
fFlags = pErr->fFlags;
}
/*
* Join cause with vmR3SetRuntimeErrorV.
*/
return vmR3SetRuntimeErrorCommonF(pVM, fFlags, pszErrorId, "%s", pszMessage);
}
/**
* Worker for VMSetRuntimeErrorV for doing the job on EMT in ring-3.
*
* @returns VBox status code with modifications, see VMSetRuntimeErrorV.
*
* @param pVM The VM handle.
* @param fFlags The error flags.
* @param pszErrorId Error ID string.
* @param pszMessage The error message residing the MM heap.
*
* @thread EMT
*/
DECLCALLBACK(int) vmR3SetRuntimeError(PVM pVM, uint32_t fFlags, const char *pszErrorId, char *pszMessage)
{
#if 0 /** @todo make copy of the error msg. */
/*
* Make a copy of the message.
*/
va_list va2;
va_copy(va2, *pVa);
vmSetRuntimeErrorCopy(pVM, fFlags, pszErrorId, pszFormat, va2);
va_end(va2);
#endif
/*
* Join paths with VMR3SetRuntimeErrorWorker.
*/
int rc = vmR3SetRuntimeErrorCommonF(pVM, fFlags, pszErrorId, "%s", pszMessage);
MMR3HeapFree(pszMessage);
return rc;
}
/**
* Worker for VMSetRuntimeErrorV for doing the job on EMT in ring-3.
*
* @returns VBox status code with modifications, see VMSetRuntimeErrorV.
*
* @param pVM The VM handle.
* @param fFlags The error flags.
* @param pszErrorId Error ID string.
* @param pszFormat Format string.
* @param pVa Pointer to the format arguments.
*
* @thread EMT
*/
DECLCALLBACK(int) vmR3SetRuntimeErrorV(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list *pVa)
{
/*
* Make a copy of the message.
*/
va_list va2;
va_copy(va2, *pVa);
vmSetRuntimeErrorCopy(pVM, fFlags, pszErrorId, pszFormat, va2);
va_end(va2);
/*
* Join paths with VMR3SetRuntimeErrorWorker.
*/
return vmR3SetRuntimeErrorCommon(pVM, fFlags, pszErrorId, pszFormat, pVa);
}
/**
* Gets the ID virtual of the virtual CPU assoicated with the calling thread.
*
* @returns The CPU ID. NIL_VMCPUID if the thread isn't an EMT.
*
* @param pVM The VM handle.
*/
VMMR3DECL(RTCPUID) VMR3GetVMCPUId(PVM pVM)
{
PUVMCPU pUVCpu = (PUVMCPU)RTTlsGet(pVM->pUVM->vm.s.idxTLS);
return pUVCpu
? pUVCpu->idCpu
: NIL_VMCPUID;
}
/**
* Returns the native handle of the current EMT VMCPU thread.
*
* @returns Handle if this is an EMT thread; NIL_RTNATIVETHREAD otherwise
* @param pVM The VM handle.
* @thread EMT
*/
VMMR3DECL(RTNATIVETHREAD) VMR3GetVMCPUNativeThread(PVM pVM)
{
PUVMCPU pUVCpu = (PUVMCPU)RTTlsGet(pVM->pUVM->vm.s.idxTLS);
if (!pUVCpu)
return NIL_RTNATIVETHREAD;
return pUVCpu->vm.s.NativeThreadEMT;
}
/**
* Returns the native handle of the current EMT VMCPU thread.
*
* @returns Handle if this is an EMT thread; NIL_RTNATIVETHREAD otherwise
* @param pVM The VM handle.
* @thread EMT
*/
VMMR3DECL(RTNATIVETHREAD) VMR3GetVMCPUNativeThreadU(PUVM pUVM)
{
PUVMCPU pUVCpu = (PUVMCPU)RTTlsGet(pUVM->vm.s.idxTLS);
if (!pUVCpu)
return NIL_RTNATIVETHREAD;
return pUVCpu->vm.s.NativeThreadEMT;
}
/**
* Returns the handle of the current EMT VMCPU thread.
*
* @returns Handle if this is an EMT thread; NIL_RTNATIVETHREAD otherwise
* @param pVM The VM handle.
* @thread EMT
*/
VMMR3DECL(RTTHREAD) VMR3GetVMCPUThread(PVM pVM)
{
PUVMCPU pUVCpu = (PUVMCPU)RTTlsGet(pVM->pUVM->vm.s.idxTLS);
if (!pUVCpu)
return NIL_RTTHREAD;
return pUVCpu->vm.s.ThreadEMT;
}
/**
* Returns the handle of the current EMT VMCPU thread.
*
* @returns Handle if this is an EMT thread; NIL_RTNATIVETHREAD otherwise
* @param pVM The VM handle.
* @thread EMT
*/
VMMR3DECL(RTTHREAD) VMR3GetVMCPUThreadU(PUVM pUVM)
{
PUVMCPU pUVCpu = (PUVMCPU)RTTlsGet(pUVM->vm.s.idxTLS);
if (!pUVCpu)
return NIL_RTTHREAD;
return pUVCpu->vm.s.ThreadEMT;
}