VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/VM.cpp@ 74795

Last change on this file since 74795 was 74786, checked in by vboxsync, 6 years ago

vm.h,VMM: Made VMCPU_FF_CLEAR work on a single flag, introducing VMCPU_FF_CLEAR_MASK for the two cases we clear more than one. bugref:9180

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 170.6 KB
RevLine 
[23]1/* $Id: VM.cpp 74786 2018-10-12 10:20:59Z vboxsync $ */
[1]2/** @file
3 * VM - Virtual Machine
4 */
5
6/*
[69111]7 * Copyright (C) 2006-2017 Oracle Corporation
[1]8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
[5999]12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
[1]16 */
17
[13667]18/** @page pg_vm VM API
19 *
20 * This is the encapsulating bit. It provides the APIs that Main and VBoxBFE
21 * use to create a VMM instance for running a guest in. It also provides
22 * facilities for queuing request for execution in EMT (serialization purposes
23 * mostly) and for reporting error back to the VMM user (Main/VBoxBFE).
24 *
25 *
26 * @section sec_vm_design Design Critique / Things To Do
27 *
28 * In hindsight this component is a big design mistake, all this stuff really
29 * belongs in the VMM component. It just seemed like a kind of ok idea at a
[33540]30 * time when the VMM bit was a kind of vague. 'VM' also happened to be the name
[22986]31 * of the per-VM instance structure (see vm.h), so it kind of made sense.
32 * However as it turned out, VMM(.cpp) is almost empty all it provides in ring-3
33 * is some minor functionally and some "routing" services.
[13667]34 *
35 * Fixing this is just a matter of some more or less straight forward
[22986]36 * refactoring, the question is just when someone will get to it. Moving the EMT
37 * would be a good start.
[13667]38 *
39 */
[1]40
[57358]41
42/*********************************************************************************************************************************
43* Header Files *
44*********************************************************************************************************************************/
[1]45#define LOG_GROUP LOG_GROUP_VM
[35346]46#include <VBox/vmm/cfgm.h>
47#include <VBox/vmm/vmm.h>
48#include <VBox/vmm/gvmm.h>
49#include <VBox/vmm/mm.h>
50#include <VBox/vmm/cpum.h>
51#include <VBox/vmm/selm.h>
52#include <VBox/vmm/trpm.h>
53#include <VBox/vmm/dbgf.h>
54#include <VBox/vmm/pgm.h>
55#include <VBox/vmm/pdmapi.h>
[60404]56#include <VBox/vmm/pdmdev.h>
[35346]57#include <VBox/vmm/pdmcritsect.h>
58#include <VBox/vmm/em.h>
[36768]59#include <VBox/vmm/iem.h>
[40274]60#ifdef VBOX_WITH_REM
61# include <VBox/vmm/rem.h>
62#endif
[70918]63#include <VBox/vmm/nem.h>
[64626]64#include <VBox/vmm/apic.h>
[35346]65#include <VBox/vmm/tm.h>
66#include <VBox/vmm/stam.h>
67#include <VBox/vmm/patm.h>
68#include <VBox/vmm/csam.h>
69#include <VBox/vmm/iom.h>
70#include <VBox/vmm/ssm.h>
71#include <VBox/vmm/ftm.h>
[43387]72#include <VBox/vmm/hm.h>
[50953]73#include <VBox/vmm/gim.h>
[1]74#include "VMInternal.h"
[35346]75#include <VBox/vmm/vm.h>
76#include <VBox/vmm/uvm.h>
[1]77
78#include <VBox/sup.h>
[40975]79#if defined(VBOX_WITH_DTRACE_R3) && !defined(VBOX_WITH_NATIVE_DTRACE)
80# include <VBox/VBoxTpG.h>
81#endif
[1]82#include <VBox/dbg.h>
83#include <VBox/err.h>
84#include <VBox/param.h>
85#include <VBox/log.h>
86#include <iprt/assert.h>
87#include <iprt/alloc.h>
88#include <iprt/asm.h>
[5099]89#include <iprt/env.h>
[1]90#include <iprt/string.h>
91#include <iprt/time.h>
92#include <iprt/semaphore.h>
93#include <iprt/thread.h>
[36041]94#include <iprt/uuid.h>
[1]95
96
[57358]97/*********************************************************************************************************************************
98* Internal Functions *
99*********************************************************************************************************************************/
[32190]100static int vmR3CreateUVM(uint32_t cCpus, PCVMM2USERMETHODS pVmm2UserMethods, PUVM *ppUVM);
101static int vmR3CreateU(PUVM pUVM, uint32_t cCpus, PFNCFGMCONSTRUCTOR pfnCFGMConstructor, void *pvUserCFGM);
[45618]102static int vmR3ReadBaseConfig(PVM pVM, PUVM pUVM, uint32_t cCpus);
[32190]103static int vmR3InitRing3(PVM pVM, PUVM pUVM);
104static int vmR3InitRing0(PVM pVM);
[45525]105#ifdef VBOX_WITH_RAW_MODE
106static int vmR3InitRC(PVM pVM);
107#endif
[32190]108static int vmR3InitDoCompleted(PVM pVM, VMINITCOMPLETED enmWhat);
109static void vmR3DestroyUVM(PUVM pUVM, uint32_t cMilliesEMTWait);
110static bool vmR3ValidateStateTransition(VMSTATE enmStateOld, VMSTATE enmStateNew);
111static void vmR3DoAtState(PVM pVM, PUVM pUVM, VMSTATE enmStateNew, VMSTATE enmStateOld);
112static int vmR3TrySetState(PVM pVM, const char *pszWho, unsigned cTransitions, ...);
[61643]113static void vmR3SetStateLocked(PVM pVM, PUVM pUVM, VMSTATE enmStateNew, VMSTATE enmStateOld, bool fSetRatherThanClearFF);
[32190]114static void vmR3SetState(PVM pVM, VMSTATE enmStateNew, VMSTATE enmStateOld);
[61122]115static int vmR3SetErrorU(PUVM pUVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(6, 7);
[1]116
117
118/**
119 * Do global VMM init.
120 *
121 * @returns VBox status code.
122 */
[12989]123VMMR3DECL(int) VMR3GlobalInit(void)
[1]124{
125 /*
126 * Only once.
127 */
[6796]128 static bool volatile s_fDone = false;
129 if (s_fDone)
[1]130 return VINF_SUCCESS;
131
[40975]132#if defined(VBOX_WITH_DTRACE_R3) && !defined(VBOX_WITH_NATIVE_DTRACE)
[41117]133 SUPR3TracerRegisterModule(~(uintptr_t)0, "VBoxVMM", &g_VTGObjHeader, (uintptr_t)&g_VTGObjHeader,
134 SUP_TRACER_UMOD_FLAGS_SHARED);
[40975]135#endif
136
[1]137 /*
138 * We're done.
139 */
[6796]140 s_fDone = true;
[1]141 return VINF_SUCCESS;
142}
143
144
145/**
146 * Creates a virtual machine by calling the supplied configuration constructor.
147 *
148 * On successful returned the VM is powered, i.e. VMR3PowerOn() should be
149 * called to start the execution.
150 *
151 * @returns 0 on success.
152 * @returns VBox error code on failure.
[22890]153 * @param cCpus Number of virtual CPUs for the new VM.
[36437]154 * @param pVmm2UserMethods An optional method table that the VMM can use
155 * to make the user perform various action, like
156 * for instance state saving.
[18757]157 * @param pfnVMAtError Pointer to callback function for setting VM
[36437]158 * errors. This was added as an implicit call to
[18757]159 * VMR3AtErrorRegister() since there is no way the
160 * caller can get to the VM handle early enough to
[18767]161 * do this on its own.
[18757]162 * This is called in the context of an EMT.
[1]163 * @param pvUserVM The user argument passed to pfnVMAtError.
164 * @param pfnCFGMConstructor Pointer to callback function for constructing the VM configuration tree.
[18757]165 * This is called in the context of an EMT0.
[1]166 * @param pvUserCFGM The user argument passed to pfnCFGMConstructor.
[44340]167 * @param ppVM Where to optionally store the 'handle' of the
168 * created VM.
169 * @param ppUVM Where to optionally store the user 'handle' of
170 * the created VM, this includes one reference as
171 * if VMR3RetainUVM() was called. The caller
172 * *MUST* remember to pass the returned value to
173 * VMR3ReleaseUVM() once done with the handle.
[1]174 */
[32190]175VMMR3DECL(int) VMR3Create(uint32_t cCpus, PCVMM2USERMETHODS pVmm2UserMethods,
176 PFNVMATERROR pfnVMAtError, void *pvUserVM,
177 PFNCFGMCONSTRUCTOR pfnCFGMConstructor, void *pvUserCFGM,
[44340]178 PVM *ppVM, PUVM *ppUVM)
[1]179{
[44340]180 LogFlow(("VMR3Create: cCpus=%RU32 pVmm2UserMethods=%p pfnVMAtError=%p pvUserVM=%p pfnCFGMConstructor=%p pvUserCFGM=%p ppVM=%p ppUVM=%p\n",
181 cCpus, pVmm2UserMethods, pfnVMAtError, pvUserVM, pfnCFGMConstructor, pvUserCFGM, ppVM, ppUVM));
[1]182
[32190]183 if (pVmm2UserMethods)
184 {
185 AssertPtrReturn(pVmm2UserMethods, VERR_INVALID_POINTER);
186 AssertReturn(pVmm2UserMethods->u32Magic == VMM2USERMETHODS_MAGIC, VERR_INVALID_PARAMETER);
187 AssertReturn(pVmm2UserMethods->u32Version == VMM2USERMETHODS_VERSION, VERR_INVALID_PARAMETER);
[36437]188 AssertPtrNullReturn(pVmm2UserMethods->pfnSaveState, VERR_INVALID_POINTER);
189 AssertPtrNullReturn(pVmm2UserMethods->pfnNotifyEmtInit, VERR_INVALID_POINTER);
190 AssertPtrNullReturn(pVmm2UserMethods->pfnNotifyEmtTerm, VERR_INVALID_POINTER);
191 AssertPtrNullReturn(pVmm2UserMethods->pfnNotifyPdmtInit, VERR_INVALID_POINTER);
192 AssertPtrNullReturn(pVmm2UserMethods->pfnNotifyPdmtTerm, VERR_INVALID_POINTER);
[48528]193 AssertPtrNullReturn(pVmm2UserMethods->pfnNotifyResetTurnedIntoPowerOff, VERR_INVALID_POINTER);
[32190]194 AssertReturn(pVmm2UserMethods->u32EndMagic == VMM2USERMETHODS_MAGIC, VERR_INVALID_PARAMETER);
195 }
196 AssertPtrNullReturn(pfnVMAtError, VERR_INVALID_POINTER);
197 AssertPtrNullReturn(pfnCFGMConstructor, VERR_INVALID_POINTER);
[44340]198 AssertPtrNullReturn(ppVM, VERR_INVALID_POINTER);
199 AssertPtrNullReturn(ppUVM, VERR_INVALID_POINTER);
200 AssertReturn(ppVM || ppUVM, VERR_INVALID_PARAMETER);
[32190]201
[1]202 /*
203 * Because of the current hackiness of the applications
204 * we'll have to initialize global stuff from here.
205 * Later the applications will take care of this in a proper way.
206 */
207 static bool fGlobalInitDone = false;
208 if (!fGlobalInitDone)
209 {
210 int rc = VMR3GlobalInit();
[13816]211 if (RT_FAILURE(rc))
[1]212 return rc;
213 fGlobalInitDone = true;
214 }
215
216 /*
[13789]217 * Validate input.
218 */
[22890]219 AssertLogRelMsgReturn(cCpus > 0 && cCpus <= VMM_MAX_CPU_COUNT, ("%RU32\n", cCpus), VERR_TOO_MANY_CPUS);
[13789]220
221 /*
[6796]222 * Create the UVM so we can register the at-error callback
[33540]223 * and consolidate a bit of cleanup code.
[1]224 */
[22122]225 PUVM pUVM = NULL; /* shuts up gcc */
[32190]226 int rc = vmR3CreateUVM(cCpus, pVmm2UserMethods, &pUVM);
[6796]227 if (RT_FAILURE(rc))
228 return rc;
229 if (pfnVMAtError)
[44340]230 rc = VMR3AtErrorRegister(pUVM, pfnVMAtError, pvUserVM);
[6796]231 if (RT_SUCCESS(rc))
[1]232 {
[6796]233 /*
[13749]234 * Initialize the support library creating the session for this VM.
[6796]235 */
[11794]236 rc = SUPR3Init(&pUVM->vm.s.pSession);
[5031]237 if (RT_SUCCESS(rc))
[1]238 {
239 /*
[6796]240 * Call vmR3CreateU in the EMT thread and wait for it to finish.
[19395]241 *
242 * Note! VMCPUID_ANY is used here because VMR3ReqQueueU would have trouble
243 * submitting a request to a specific VCPU without a pVM. So, to make
244 * sure init is running on EMT(0), vmR3EmulationThreadWithId makes sure
245 * that only EMT(0) is servicing VMCPUID_ANY requests when pVM is NULL.
[1]246 */
[6796]247 PVMREQ pReq;
[23009]248 rc = VMR3ReqCallU(pUVM, VMCPUID_ANY, &pReq, RT_INDEFINITE_WAIT, VMREQFLAGS_VBOX_STATUS,
249 (PFNRT)vmR3CreateU, 4, pUVM, cCpus, pfnCFGMConstructor, pvUserCFGM);
[5031]250 if (RT_SUCCESS(rc))
251 {
[6796]252 rc = pReq->iStatus;
253 VMR3ReqFree(pReq);
[6633]254 if (RT_SUCCESS(rc))
[1]255 {
256 /*
[6796]257 * Success!
[1]258 */
[44340]259 if (ppVM)
260 *ppVM = pUVM->pVM;
261 if (ppUVM)
262 {
263 VMR3RetainUVM(pUVM);
264 *ppUVM = pUVM;
265 }
266 LogFlow(("VMR3Create: returns VINF_SUCCESS (pVM=%p, pUVM=%p\n", pUVM->pVM, pUVM));
[6796]267 return VINF_SUCCESS;
268 }
269 }
270 else
[23009]271 AssertMsgFailed(("VMR3ReqCallU failed rc=%Rrc\n", rc));
[6633]272
[6796]273 /*
[35810]274 * An error occurred during VM creation. Set the error message directly
275 * using the initial callback, as the callback list might not exist yet.
[6796]276 */
[35810]277 const char *pszError;
[6796]278 switch (rc)
279 {
280 case VERR_VMX_IN_VMX_ROOT_MODE:
[4085]281#ifdef RT_OS_LINUX
[6796]282 pszError = N_("VirtualBox can't operate in VMX root mode. "
283 "Please disable the KVM kernel extension, recompile your kernel and reboot");
[4078]284#else
[12497]285 pszError = N_("VirtualBox can't operate in VMX root mode. Please close all other virtualization programs.");
[4078]286#endif
[6796]287 break;
[6633]288
[31854]289#ifndef RT_OS_DARWIN
[43387]290 case VERR_HM_CONFIG_MISMATCH:
[31854]291 pszError = N_("VT-x/AMD-V is either not available on your host or disabled. "
292 "This hardware extension is required by the VM configuration");
293 break;
294#endif
295
[24030]296 case VERR_SVM_IN_USE:
297#ifdef RT_OS_LINUX
298 pszError = N_("VirtualBox can't enable the AMD-V extension. "
299 "Please disable the KVM kernel extension, recompile your kernel and reboot");
300#else
301 pszError = N_("VirtualBox can't enable the AMD-V extension. Please close all other virtualization programs.");
302#endif
[24037]303 break;
[24030]304
[20432]305#ifdef RT_OS_LINUX
306 case VERR_SUPDRV_COMPONENT_NOT_FOUND:
307 pszError = N_("One of the kernel modules was not successfully loaded. Make sure "
308 "that no kernel modules from an older version of VirtualBox exist. "
309 "Then try to recompile and reload the kernel modules by executing "
[58090]310 "'/sbin/vboxconfig' as root");
[20432]311 break;
312#endif
313
[22635]314 case VERR_RAW_MODE_INVALID_SMP:
315 pszError = N_("VT-x/AMD-V is either not available on your host or disabled. "
316 "VirtualBox requires this hardware extension to emulate more than one "
[22636]317 "guest CPU");
[22635]318 break;
319
[22609]320 case VERR_SUPDRV_KERNEL_TOO_OLD_FOR_VTX:
[22635]321#ifdef RT_OS_LINUX
322 pszError = N_("Because the host kernel is too old, VirtualBox cannot enable the VT-x "
323 "extension. Either upgrade your kernel to Linux 2.6.13 or later or disable "
324 "the VT-x extension in the VM settings. Note that without VT-x you have "
325 "to reduce the number of guest CPUs to one");
326#else
327 pszError = N_("Because the host kernel is too old, VirtualBox cannot enable the VT-x "
328 "extension. Either upgrade your kernel or disable the VT-x extension in the "
329 "VM settings. Note that without VT-x you have to reduce the number of guest "
330 "CPUs to one");
331#endif
[22609]332 break;
333
[34683]334 case VERR_PDM_DEVICE_NOT_FOUND:
335 pszError = N_("A virtual device is configured in the VM settings but the device "
336 "implementation is missing.\n"
337 "A possible reason for this error is a missing extension pack. Note "
338 "that as of VirtualBox 4.0, certain features (for example USB 2.0 "
339 "support and remote desktop) are only available from an 'extension "
340 "pack' which must be downloaded and installed separately");
341 break;
342
[43387]343 case VERR_PCI_PASSTHROUGH_NO_HM:
[37794]344 pszError = N_("PCI passthrough requires VT-x/AMD-V");
345 break;
346
347 case VERR_PCI_PASSTHROUGH_NO_NESTED_PAGING:
348 pszError = N_("PCI passthrough requires nested paging");
349 break;
350
[6796]351 default:
[44340]352 if (VMR3GetErrorCount(pUVM) == 0)
[35810]353 pszError = RTErrGetFull(rc);
354 else
355 pszError = NULL; /* already set. */
[6796]356 break;
[1]357 }
[35810]358 if (pszError)
359 vmR3SetErrorU(pUVM, rc, RT_SRC_POS, pszError, rc);
[1]360 }
361 else
362 {
[6796]363 /*
364 * An error occurred at support library initialization time (before the
365 * VM could be created). Set the error message directly using the
366 * initial callback, as the callback list doesn't exist yet.
367 */
368 const char *pszError;
369 switch (rc)
370 {
371 case VERR_VM_DRIVER_LOAD_ERROR:
[4086]372#ifdef RT_OS_LINUX
[6796]373 pszError = N_("VirtualBox kernel driver not loaded. The vboxdrv kernel module "
374 "was either not loaded or /dev/vboxdrv is not set up properly. "
375 "Re-setup the kernel module by executing "
[58090]376 "'/sbin/vboxconfig' as root");
[1187]377#else
[6796]378 pszError = N_("VirtualBox kernel driver not loaded");
[1187]379#endif
[6796]380 break;
381 case VERR_VM_DRIVER_OPEN_ERROR:
382 pszError = N_("VirtualBox kernel driver cannot be opened");
383 break;
384 case VERR_VM_DRIVER_NOT_ACCESSIBLE:
[11883]385#ifdef VBOX_WITH_HARDENING
386 /* This should only happen if the executable wasn't hardened - bad code/build. */
387 pszError = N_("VirtualBox kernel driver not accessible, permission problem. "
388 "Re-install VirtualBox. If you are building it yourself, you "
389 "should make sure it installed correctly and that the setuid "
390 "bit is set on the executables calling VMR3Create.");
[1187]391#else
[11883]392 /* This should only happen when mixing builds or with the usual /dev/vboxdrv access issues. */
393# if defined(RT_OS_DARWIN)
394 pszError = N_("VirtualBox KEXT is not accessible, permission problem. "
395 "If you have built VirtualBox yourself, make sure that you do not "
396 "have the vboxdrv KEXT from a different build or installation loaded.");
397# elif defined(RT_OS_LINUX)
398 pszError = N_("VirtualBox kernel driver is not accessible, permission problem. "
399 "If you have built VirtualBox yourself, make sure that you do "
400 "not have the vboxdrv kernel module from a different build or "
401 "installation loaded. Also, make sure the vboxdrv udev rule gives "
402 "you the permission you need to access the device.");
403# elif defined(RT_OS_WINDOWS)
404 pszError = N_("VirtualBox kernel driver is not accessible, permission problem.");
405# else /* solaris, freebsd, ++. */
406 pszError = N_("VirtualBox kernel module is not accessible, permission problem. "
407 "If you have built VirtualBox yourself, make sure that you do "
408 "not have the vboxdrv kernel module from a different install loaded.");
[12579]409# endif
[1187]410#endif
[6796]411 break;
[11883]412 case VERR_INVALID_HANDLE: /** @todo track down and fix this error. */
[6796]413 case VERR_VM_DRIVER_NOT_INSTALLED:
[3696]414#ifdef RT_OS_LINUX
[6796]415 pszError = N_("VirtualBox kernel driver not installed. The vboxdrv kernel module "
416 "was either not loaded or /dev/vboxdrv was not created for some "
417 "reason. Re-setup the kernel module by executing "
[58090]418 "'/sbin/vboxconfig' as root");
[1187]419#else
[6796]420 pszError = N_("VirtualBox kernel driver not installed");
[1187]421#endif
[6796]422 break;
423 case VERR_NO_MEMORY:
424 pszError = N_("VirtualBox support library out of memory");
425 break;
426 case VERR_VERSION_MISMATCH:
427 case VERR_VM_DRIVER_VERSION_MISMATCH:
428 pszError = N_("The VirtualBox support driver which is running is from a different "
429 "version of VirtualBox. You can correct this by stopping all "
430 "running instances of VirtualBox and reinstalling the software.");
431 break;
432 default:
433 pszError = N_("Unknown error initializing kernel driver");
[13818]434 AssertMsgFailed(("Add error message for rc=%d (%Rrc)\n", rc, rc));
[6796]435 }
436 vmR3SetErrorU(pUVM, rc, RT_SRC_POS, pszError, rc);
[1]437 }
438 }
439
[6796]440 /* cleanup */
[19101]441 vmR3DestroyUVM(pUVM, 2000);
[13818]442 LogFlow(("VMR3Create: returns %Rrc\n", rc));
[1]443 return rc;
444}
445
[47]446
[1]447/**
[6796]448 * Creates the UVM.
449 *
450 * This will not initialize the support library even if vmR3DestroyUVM
451 * will terminate that.
452 *
453 * @returns VBox status code.
[32190]454 * @param cCpus Number of virtual CPUs
455 * @param pVmm2UserMethods Pointer to the optional VMM -> User method
456 * table.
457 * @param ppUVM Where to store the UVM pointer.
[47]458 */
[32190]459static int vmR3CreateUVM(uint32_t cCpus, PCVMM2USERMETHODS pVmm2UserMethods, PUVM *ppUVM)
[47]460{
[13796]461 uint32_t i;
462
[6796]463 /*
[13796]464 * Create and initialize the UVM.
[6796]465 */
[73097]466 PUVM pUVM = (PUVM)RTMemPageAllocZ(RT_UOFFSETOF_DYN(UVM, aCpus[cCpus]));
[6796]467 AssertReturn(pUVM, VERR_NO_MEMORY);
[32190]468 pUVM->u32Magic = UVM_MAGIC;
469 pUVM->cCpus = cCpus;
470 pUVM->pVmm2UserMethods = pVmm2UserMethods;
[6796]471
472 AssertCompile(sizeof(pUVM->vm.s) <= sizeof(pUVM->vm.padding));
473
[36041]474 pUVM->vm.s.cUvmRefs = 1;
[6796]475 pUVM->vm.s.ppAtStateNext = &pUVM->vm.s.pAtState;
476 pUVM->vm.s.ppAtErrorNext = &pUVM->vm.s.pAtError;
477 pUVM->vm.s.ppAtRuntimeErrorNext = &pUVM->vm.s.pAtRuntimeError;
[19217]478
[6796]479 pUVM->vm.s.enmHaltMethod = VMHALTMETHOD_BOOTSTRAP;
[36041]480 RTUuidClear(&pUVM->vm.s.Uuid);
[13782]481
482 /* Initialize the VMCPU array in the UVM. */
[19395]483 for (i = 0; i < cCpus; i++)
[13782]484 {
[19217]485 pUVM->aCpus[i].pUVM = pUVM;
486 pUVM->aCpus[i].idCpu = i;
[13782]487 }
488
[13796]489 /* Allocate a TLS entry to store the VMINTUSERPERVMCPU pointer. */
490 int rc = RTTlsAllocEx(&pUVM->vm.s.idxTLS, NULL);
491 AssertRC(rc);
[6796]492 if (RT_SUCCESS(rc))
493 {
[19217]494 /* Allocate a halt method event semaphore for each VCPU. */
[19395]495 for (i = 0; i < cCpus; i++)
[22980]496 pUVM->aCpus[i].vm.s.EventSemWait = NIL_RTSEMEVENT;
497 for (i = 0; i < cCpus; i++)
[19217]498 {
499 rc = RTSemEventCreate(&pUVM->aCpus[i].vm.s.EventSemWait);
500 if (RT_FAILURE(rc))
501 break;
502 }
[6796]503 if (RT_SUCCESS(rc))
504 {
[22980]505 rc = RTCritSectInit(&pUVM->vm.s.AtStateCritSect);
[6796]506 if (RT_SUCCESS(rc))
507 {
[22980]508 rc = RTCritSectInit(&pUVM->vm.s.AtErrorCritSect);
[6796]509 if (RT_SUCCESS(rc))
510 {
[22980]511 /*
512 * Init fundamental (sub-)components - STAM, MMR3Heap and PDMLdr.
513 */
[45189]514 rc = PDMR3InitUVM(pUVM);
[13796]515 if (RT_SUCCESS(rc))
[13782]516 {
[45189]517 rc = STAMR3InitUVM(pUVM);
[22980]518 if (RT_SUCCESS(rc))
[13796]519 {
[45189]520 rc = MMR3InitUVM(pUVM);
[22980]521 if (RT_SUCCESS(rc))
522 {
523 /*
524 * Start the emulation threads for all VMCPUs.
525 */
526 for (i = 0; i < cCpus; i++)
527 {
[45189]528 rc = RTThreadCreateF(&pUVM->aCpus[i].vm.s.ThreadEMT, vmR3EmulationThread, &pUVM->aCpus[i],
529 _1M, RTTHREADTYPE_EMULATION, RTTHREADFLAGS_WAITABLE,
[22980]530 cCpus > 1 ? "EMT-%u" : "EMT", i);
531 if (RT_FAILURE(rc))
532 break;
[13791]533
[22980]534 pUVM->aCpus[i].vm.s.NativeThreadEMT = RTThreadGetNative(pUVM->aCpus[i].vm.s.ThreadEMT);
535 }
[13782]536
[22980]537 if (RT_SUCCESS(rc))
538 {
539 *ppUVM = pUVM;
540 return VINF_SUCCESS;
541 }
[13796]542
[22980]543 /* bail out. */
544 while (i-- > 0)
545 {
546 /** @todo rainy day: terminate the EMTs. */
547 }
[45189]548 MMR3TermUVM(pUVM);
[22980]549 }
[45189]550 STAMR3TermUVM(pUVM);
[13796]551 }
[45189]552 PDMR3TermUVM(pUVM);
[6796]553 }
[22980]554 RTCritSectDelete(&pUVM->vm.s.AtErrorCritSect);
[6796]555 }
[22980]556 RTCritSectDelete(&pUVM->vm.s.AtStateCritSect);
[6796]557 }
558 }
[22980]559 for (i = 0; i < cCpus; i++)
560 {
561 RTSemEventDestroy(pUVM->aCpus[i].vm.s.EventSemWait);
562 pUVM->aCpus[i].vm.s.EventSemWait = NIL_RTSEMEVENT;
563 }
[13796]564 RTTlsFree(pUVM->vm.s.idxTLS);
[6796]565 }
[73097]566 RTMemPageFree(pUVM, RT_UOFFSETOF_DYN(UVM, aCpus[pUVM->cCpus]));
[6796]567 return rc;
[47]568}
569
570
571/**
[6796]572 * Creates and initializes the VM.
573 *
574 * @thread EMT
[1]575 */
[19395]576static int vmR3CreateU(PUVM pUVM, uint32_t cCpus, PFNCFGMCONSTRUCTOR pfnCFGMConstructor, void *pvUserCFGM)
[1]577{
[71699]578#if (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)) && !defined(VBOX_WITH_OLD_CPU_SUPPORT)
[6796]579 /*
[71699]580 * Require SSE2 to be present (already checked for in supdrv, so we
581 * shouldn't ever really get here).
582 */
583 if (!(ASMCpuId_EDX(1) & X86_CPUID_FEATURE_EDX_SSE2))
584 {
585 LogRel(("vboxdrv: Requires SSE2 (cpuid(0).EDX=%#x)\n", ASMCpuId_EDX(1)));
586 return VERR_UNSUPPORTED_CPU;
587 }
588#endif
589
590 /*
[6796]591 * Load the VMMR0.r0 module so that we can call GVMMR0CreateVM.
592 */
[36041]593 int rc = PDMR3LdrLoadVMMR0U(pUVM);
[6796]594 if (RT_FAILURE(rc))
[8604]595 {
[8608]596 /** @todo we need a cleaner solution for this (VERR_VMX_IN_VMX_ROOT_MODE).
597 * bird: what about moving the message down here? Main picks the first message, right? */
598 if (rc == VERR_VMX_IN_VMX_ROOT_MODE)
599 return rc; /* proper error message set later on */
600 return vmR3SetErrorU(pUVM, rc, RT_SRC_POS, N_("Failed to load VMMR0.r0"));
[8604]601 }
[6796]602
603 /*
604 * Request GVMM to create a new VM for us.
605 */
606 GVMMCREATEVMREQ CreateVMReq;
[13785]607 CreateVMReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
608 CreateVMReq.Hdr.cbReq = sizeof(CreateVMReq);
609 CreateVMReq.pSession = pUVM->vm.s.pSession;
610 CreateVMReq.pVMR0 = NIL_RTR0PTR;
611 CreateVMReq.pVMR3 = NULL;
[19395]612 CreateVMReq.cCpus = cCpus;
[20864]613 rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_GVMM_CREATE_VM, 0, &CreateVMReq.Hdr);
[6796]614 if (RT_SUCCESS(rc))
[1]615 {
[6796]616 PVM pVM = pUVM->pVM = CreateVMReq.pVMR3;
617 AssertRelease(VALID_PTR(pVM));
618 AssertRelease(pVM->pVMR0 == CreateVMReq.pVMR0);
619 AssertRelease(pVM->pSession == pUVM->vm.s.pSession);
[22890]620 AssertRelease(pVM->cCpus == cCpus);
[32885]621 AssertRelease(pVM->uCpuExecutionCap == 100);
[13796]622 AssertRelease(pVM->offVMCPU == RT_UOFFSETOF(VM, aCpus));
[37462]623 AssertCompileMemberAlignment(VM, cpum, 64);
[37465]624 AssertCompileMemberAlignment(VM, tm, 64);
[37462]625 AssertCompileMemberAlignment(VM, aCpus, PAGE_SIZE);
[6796]626
[22890]627 Log(("VMR3Create: Created pUVM=%p pVM=%p pVMR0=%p hSelf=%#x cCpus=%RU32\n",
628 pUVM, pVM, pVM->pVMR0, pVM->hSelf, pVM->cCpus));
[6796]629
[1]630 /*
[6796]631 * Initialize the VM structure and our internal data (VMINT).
632 */
633 pVM->pUVM = pUVM;
634
[22890]635 for (VMCPUID i = 0; i < pVM->cCpus; i++)
[13785]636 {
[31362]637 pVM->aCpus[i].pUVCpu = &pUVM->aCpus[i];
638 pVM->aCpus[i].idCpu = i;
639 pVM->aCpus[i].hNativeThread = pUVM->aCpus[i].vm.s.NativeThreadEMT;
[13796]640 Assert(pVM->aCpus[i].hNativeThread != NIL_RTNATIVETHREAD);
[31410]641 /* hNativeThreadR0 is initialized on EMT registration. */
[31362]642 pUVM->aCpus[i].pVCpu = &pVM->aCpus[i];
643 pUVM->aCpus[i].pVM = pVM;
[13785]644 }
645
[13789]646
[6796]647 /*
[1]648 * Init the configuration.
649 */
650 rc = CFGMR3Init(pVM, pfnCFGMConstructor, pvUserCFGM);
[13816]651 if (RT_SUCCESS(rc))
[1]652 {
[45618]653 rc = vmR3ReadBaseConfig(pVM, pUVM, cCpus);
[14543]654 if (RT_SUCCESS(rc))
[13789]655 {
[1]656 /*
[16184]657 * Init the ring-3 components and ring-3 per cpu data, finishing it off
658 * by a relocation round (intermediate context finalization will do this).
[1]659 */
[13742]660 rc = vmR3InitRing3(pVM, pUVM);
[13816]661 if (RT_SUCCESS(rc))
[1]662 {
[34326]663 rc = PGMR3FinalizeMappings(pVM);
[13816]664 if (RT_SUCCESS(rc))
[1]665 {
[16184]666
667 LogFlow(("Ring-3 init succeeded\n"));
668
[1]669 /*
[13778]670 * Init the Ring-0 components.
[1]671 */
[13778]672 rc = vmR3InitRing0(pVM);
[13816]673 if (RT_SUCCESS(rc))
[1]674 {
[13778]675 /* Relocate again, because some switcher fixups depends on R0 init results. */
[60364]676 VMR3Relocate(pVM, 0 /* offDelta */);
[13778]677
678#ifdef VBOX_WITH_DEBUGGER
[1]679 /*
[13778]680 * Init the tcp debugger console if we're building
681 * with debugger support.
682 */
683 void *pvUser = NULL;
[44399]684 rc = DBGCTcpCreate(pUVM, &pvUser);
[13816]685 if ( RT_SUCCESS(rc)
[13778]686 || rc == VERR_NET_ADDRESS_IN_USE)
[1]687 {
[13778]688 pUVM->vm.s.pvDBGC = pvUser;
689#endif
[1]690 /*
[45525]691 * Init the Raw-Mode Context components.
[1]692 */
[45525]693#ifdef VBOX_WITH_RAW_MODE
694 rc = vmR3InitRC(pVM);
[13816]695 if (RT_SUCCESS(rc))
[45525]696#endif
[13742]697 {
698 /*
[20564]699 * Now we can safely set the VM halt method to default.
700 */
[13778]701 rc = vmR3SetHaltMethodU(pUVM, VMHALTMETHOD_DEFAULT);
702 if (RT_SUCCESS(rc))
703 {
704 /*
[44340]705 * Set the state and we're done.
[20854]706 */
[23009]707 vmR3SetState(pVM, VMSTATE_CREATED, VMSTATE_CREATING);
[13778]708 return VINF_SUCCESS;
709 }
[13742]710 }
[13778]711#ifdef VBOX_WITH_DEBUGGER
[44399]712 DBGCTcpTerminate(pUVM, pUVM->vm.s.pvDBGC);
[13778]713 pUVM->vm.s.pvDBGC = NULL;
[1]714 }
[13778]715#endif
716 //..
[6796]717 }
718 }
[13742]719 vmR3Destroy(pVM);
[1]720 }
721 }
[6796]722 //..
[1]723
724 /* Clean CFGM. */
725 int rc2 = CFGMR3Term(pVM);
726 AssertRC(rc2);
727 }
[6796]728
[20564]729 /*
[28258]730 * Do automatic cleanups while the VM structure is still alive and all
731 * references to it are still working.
732 */
[45152]733 PDMR3CritSectBothTerm(pVM);
[28258]734
735 /*
[20564]736 * Drop all references to VM and the VMCPU structures, then
737 * tell GVMM to destroy the VM.
738 */
739 pUVM->pVM = NULL;
740 for (VMCPUID i = 0; i < pUVM->cCpus; i++)
741 {
742 pUVM->aCpus[i].pVM = NULL;
743 pUVM->aCpus[i].pVCpu = NULL;
744 }
745 Assert(pUVM->vm.s.enmHaltMethod == VMHALTMETHOD_BOOTSTRAP);
746
747 if (pUVM->cCpus > 1)
748 {
749 /* Poke the other EMTs since they may have stale pVM and pVCpu references
750 on the stack (see VMR3WaitU for instance) if they've been awakened after
751 VM creation. */
752 for (VMCPUID i = 1; i < pUVM->cCpus; i++)
753 VMR3NotifyCpuFFU(&pUVM->aCpus[i], 0);
754 RTThreadSleep(RT_MIN(100 + 25 *(pUVM->cCpus - 1), 500)); /* very sophisticated */
755 }
756
[20864]757 int rc2 = SUPR3CallVMMR0Ex(CreateVMReq.pVMR0, 0 /*idCpu*/, VMMR0_DO_GVMM_DESTROY_VM, 0, NULL);
[6796]758 AssertRC(rc2);
[1]759 }
[6796]760 else
761 vmR3SetErrorU(pUVM, rc, RT_SRC_POS, N_("VM creation failed (GVMM)"));
[1]762
[20564]763 LogFlow(("vmR3CreateU: returns %Rrc\n", rc));
[1]764 return rc;
765}
766
[19435]767
[19395]768/**
[45618]769 * Reads the base configuation from CFGM.
770 *
771 * @returns VBox status code.
[58122]772 * @param pVM The cross context VM structure.
[45618]773 * @param pUVM The user mode VM structure.
774 * @param cCpus The CPU count given to VMR3Create.
775 */
776static int vmR3ReadBaseConfig(PVM pVM, PUVM pUVM, uint32_t cCpus)
777{
778 int rc;
779 PCFGMNODE pRoot = CFGMR3GetRoot(pVM);
780
781 /*
782 * If executing in fake suplib mode disable RR3 and RR0 in the config.
783 */
784 const char *psz = RTEnvGet("VBOX_SUPLIB_FAKE");
785 if (psz && !strcmp(psz, "fake"))
786 {
787 CFGMR3RemoveValue(pRoot, "RawR3Enabled");
788 CFGMR3InsertInteger(pRoot, "RawR3Enabled", 0);
789 CFGMR3RemoveValue(pRoot, "RawR0Enabled");
790 CFGMR3InsertInteger(pRoot, "RawR0Enabled", 0);
791 }
792
793 /*
794 * Base EM and HM config properties.
795 */
[70953]796 /** @todo We don't need to read any of this here. The relevant modules reads
797 * them again and will be in a better position to set them correctly. */
[45618]798 Assert(pVM->fRecompileUser == false); /* ASSUMES all zeros at this point */
[45786]799 bool fEnabled;
[45618]800 rc = CFGMR3QueryBoolDef(pRoot, "RawR3Enabled", &fEnabled, false); AssertRCReturn(rc, rc);
801 pVM->fRecompileUser = !fEnabled;
802 rc = CFGMR3QueryBoolDef(pRoot, "RawR0Enabled", &fEnabled, false); AssertRCReturn(rc, rc);
803 pVM->fRecompileSupervisor = !fEnabled;
[70953]804#ifdef VBOX_WITH_RAW_MODE
[45618]805# ifdef VBOX_WITH_RAW_RING1
806 rc = CFGMR3QueryBoolDef(pRoot, "RawR1Enabled", &pVM->fRawRing1Enabled, false);
807# endif
808 rc = CFGMR3QueryBoolDef(pRoot, "PATMEnabled", &pVM->fPATMEnabled, true); AssertRCReturn(rc, rc);
809 rc = CFGMR3QueryBoolDef(pRoot, "CSAMEnabled", &pVM->fCSAMEnabled, true); AssertRCReturn(rc, rc);
810 rc = CFGMR3QueryBoolDef(pRoot, "HMEnabled", &pVM->fHMEnabled, true); AssertRCReturn(rc, rc);
811#else
812 pVM->fHMEnabled = true;
813#endif
814 LogRel(("VM: fHMEnabled=%RTbool (configured) fRecompileUser=%RTbool fRecompileSupervisor=%RTbool\n"
815 "VM: fRawRing1Enabled=%RTbool CSAM=%RTbool PATM=%RTbool\n",
816 pVM->fHMEnabled, pVM->fRecompileUser, pVM->fRecompileSupervisor,
817 pVM->fRawRing1Enabled, pVM->fCSAMEnabled, pVM->fPATMEnabled));
818
819 /*
820 * Make sure the CPU count in the config data matches.
821 */
822 uint32_t cCPUsCfg;
823 rc = CFGMR3QueryU32Def(pRoot, "NumCPUs", &cCPUsCfg, 1);
824 AssertLogRelMsgRCReturn(rc, ("Configuration error: Querying \"NumCPUs\" as integer failed, rc=%Rrc\n", rc), rc);
825 AssertLogRelMsgReturn(cCPUsCfg == cCpus,
826 ("Configuration error: \"NumCPUs\"=%RU32 and VMR3Create::cCpus=%RU32 does not match!\n",
827 cCPUsCfg, cCpus),
828 VERR_INVALID_PARAMETER);
829
830 /*
831 * Get the CPU execution cap.
832 */
833 rc = CFGMR3QueryU32Def(pRoot, "CpuExecutionCap", &pVM->uCpuExecutionCap, 100);
834 AssertLogRelMsgRCReturn(rc, ("Configuration error: Querying \"CpuExecutionCap\" as integer failed, rc=%Rrc\n", rc), rc);
835
836 /*
837 * Get the VM name and UUID.
838 */
839 rc = CFGMR3QueryStringAllocDef(pRoot, "Name", &pUVM->vm.s.pszName, "<unknown>");
840 AssertLogRelMsgRCReturn(rc, ("Configuration error: Querying \"Name\" failed, rc=%Rrc\n", rc), rc);
841
842 rc = CFGMR3QueryBytes(pRoot, "UUID", &pUVM->vm.s.Uuid, sizeof(pUVM->vm.s.Uuid));
843 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
844 rc = VINF_SUCCESS;
845 AssertLogRelMsgRCReturn(rc, ("Configuration error: Querying \"UUID\" failed, rc=%Rrc\n", rc), rc);
846
[48528]847 rc = CFGMR3QueryBoolDef(pRoot, "PowerOffInsteadOfReset", &pVM->vm.s.fPowerOffInsteadOfReset, false);
848 AssertLogRelMsgRCReturn(rc, ("Configuration error: Querying \"PowerOffInsteadOfReset\" failed, rc=%Rrc\n", rc), rc);
849
[45618]850 return VINF_SUCCESS;
851}
852
853
854/**
[19395]855 * Register the calling EMT with GVM.
856 *
857 * @returns VBox status code.
[58122]858 * @param pVM The cross context VM structure.
[19395]859 * @param idCpu The Virtual CPU ID.
860 */
861static DECLCALLBACK(int) vmR3RegisterEMT(PVM pVM, VMCPUID idCpu)
862{
863 Assert(VMMGetCpuId(pVM) == idCpu);
[20864]864 int rc = SUPR3CallVMMR0Ex(pVM->pVMR0, idCpu, VMMR0_DO_GVMM_REGISTER_VMCPU, 0, NULL);
[19395]865 if (RT_FAILURE(rc))
866 LogRel(("idCpu=%u rc=%Rrc\n", idCpu, rc));
867 return rc;
868}
[1]869
[19395]870
[1]871/**
872 * Initializes all R3 components of the VM
873 */
[6796]874static int vmR3InitRing3(PVM pVM, PUVM pUVM)
[1]875{
876 int rc;
877
878 /*
[19395]879 * Register the other EMTs with GVM.
880 */
[22890]881 for (VMCPUID idCpu = 1; idCpu < pVM->cCpus; idCpu++)
[19395]882 {
[38838]883 rc = VMR3ReqCallWait(pVM, idCpu, (PFNRT)vmR3RegisterEMT, 2, pVM, idCpu);
[19395]884 if (RT_FAILURE(rc))
885 return rc;
886 }
887
888 /*
[45618]889 * Register statistics.
890 */
891 STAM_REG(pVM, &pVM->StatTotalInGC, STAMTYPE_PROFILE_ADV, "/PROF/VM/InGC", STAMUNIT_TICKS_PER_CALL, "Profiling the total time spent in GC.");
892 STAM_REG(pVM, &pVM->StatSwitcherToGC, STAMTYPE_PROFILE_ADV, "/PROF/VM/SwitchToGC", STAMUNIT_TICKS_PER_CALL, "Profiling switching to GC.");
893 STAM_REG(pVM, &pVM->StatSwitcherToHC, STAMTYPE_PROFILE_ADV, "/PROF/VM/SwitchToHC", STAMUNIT_TICKS_PER_CALL, "Profiling switching to HC.");
894 STAM_REG(pVM, &pVM->StatSwitcherSaveRegs, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/SaveRegs", STAMUNIT_TICKS_PER_CALL,"Profiling switching to GC.");
895 STAM_REG(pVM, &pVM->StatSwitcherSysEnter, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/SysEnter", STAMUNIT_TICKS_PER_CALL,"Profiling switching to GC.");
896 STAM_REG(pVM, &pVM->StatSwitcherDebug, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/Debug", STAMUNIT_TICKS_PER_CALL,"Profiling switching to GC.");
897 STAM_REG(pVM, &pVM->StatSwitcherCR0, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/CR0", STAMUNIT_TICKS_PER_CALL, "Profiling switching to GC.");
898 STAM_REG(pVM, &pVM->StatSwitcherCR4, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/CR4", STAMUNIT_TICKS_PER_CALL, "Profiling switching to GC.");
899 STAM_REG(pVM, &pVM->StatSwitcherLgdt, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/Lgdt", STAMUNIT_TICKS_PER_CALL, "Profiling switching to GC.");
900 STAM_REG(pVM, &pVM->StatSwitcherLidt, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/Lidt", STAMUNIT_TICKS_PER_CALL, "Profiling switching to GC.");
901 STAM_REG(pVM, &pVM->StatSwitcherLldt, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/Lldt", STAMUNIT_TICKS_PER_CALL, "Profiling switching to GC.");
902 STAM_REG(pVM, &pVM->StatSwitcherTSS, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/TSS", STAMUNIT_TICKS_PER_CALL, "Profiling switching to GC.");
903 STAM_REG(pVM, &pVM->StatSwitcherJmpCR3, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/JmpCR3", STAMUNIT_TICKS_PER_CALL,"Profiling switching to GC.");
904 STAM_REG(pVM, &pVM->StatSwitcherRstrRegs, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/RstrRegs", STAMUNIT_TICKS_PER_CALL,"Profiling switching to GC.");
905
906 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
907 {
908 rc = STAMR3RegisterF(pVM, &pUVM->aCpus[idCpu].vm.s.StatHaltYield, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_NS_PER_CALL, "Profiling halted state yielding.", "/PROF/CPU%d/VM/Halt/Yield", idCpu);
909 AssertRC(rc);
910 rc = STAMR3RegisterF(pVM, &pUVM->aCpus[idCpu].vm.s.StatHaltBlock, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_NS_PER_CALL, "Profiling halted state blocking.", "/PROF/CPU%d/VM/Halt/Block", idCpu);
911 AssertRC(rc);
912 rc = STAMR3RegisterF(pVM, &pUVM->aCpus[idCpu].vm.s.StatHaltBlockOverslept, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_NS_PER_CALL, "Time wasted by blocking too long.", "/PROF/CPU%d/VM/Halt/BlockOverslept", idCpu);
913 AssertRC(rc);
914 rc = STAMR3RegisterF(pVM, &pUVM->aCpus[idCpu].vm.s.StatHaltBlockInsomnia, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_NS_PER_CALL, "Time slept when returning to early.","/PROF/CPU%d/VM/Halt/BlockInsomnia", idCpu);
915 AssertRC(rc);
916 rc = STAMR3RegisterF(pVM, &pUVM->aCpus[idCpu].vm.s.StatHaltBlockOnTime, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_NS_PER_CALL, "Time slept on time.", "/PROF/CPU%d/VM/Halt/BlockOnTime", idCpu);
917 AssertRC(rc);
918 rc = STAMR3RegisterF(pVM, &pUVM->aCpus[idCpu].vm.s.StatHaltTimers, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_NS_PER_CALL, "Profiling halted state timer tasks.", "/PROF/CPU%d/VM/Halt/Timers", idCpu);
919 AssertRC(rc);
920 }
921
922 STAM_REG(pVM, &pUVM->vm.s.StatReqAllocNew, STAMTYPE_COUNTER, "/VM/Req/AllocNew", STAMUNIT_OCCURENCES, "Number of VMR3ReqAlloc returning a new packet.");
923 STAM_REG(pVM, &pUVM->vm.s.StatReqAllocRaces, STAMTYPE_COUNTER, "/VM/Req/AllocRaces", STAMUNIT_OCCURENCES, "Number of VMR3ReqAlloc causing races.");
924 STAM_REG(pVM, &pUVM->vm.s.StatReqAllocRecycled, STAMTYPE_COUNTER, "/VM/Req/AllocRecycled", STAMUNIT_OCCURENCES, "Number of VMR3ReqAlloc returning a recycled packet.");
925 STAM_REG(pVM, &pUVM->vm.s.StatReqFree, STAMTYPE_COUNTER, "/VM/Req/Free", STAMUNIT_OCCURENCES, "Number of VMR3ReqFree calls.");
926 STAM_REG(pVM, &pUVM->vm.s.StatReqFreeOverflow, STAMTYPE_COUNTER, "/VM/Req/FreeOverflow", STAMUNIT_OCCURENCES, "Number of times the request was actually freed.");
927 STAM_REG(pVM, &pUVM->vm.s.StatReqProcessed, STAMTYPE_COUNTER, "/VM/Req/Processed", STAMUNIT_OCCURENCES, "Number of processed requests (any queue).");
928 STAM_REG(pVM, &pUVM->vm.s.StatReqMoreThan1, STAMTYPE_COUNTER, "/VM/Req/MoreThan1", STAMUNIT_OCCURENCES, "Number of times there are more than one request on the queue when processing it.");
929 STAM_REG(pVM, &pUVM->vm.s.StatReqPushBackRaces, STAMTYPE_COUNTER, "/VM/Req/PushBackRaces", STAMUNIT_OCCURENCES, "Number of push back races.");
930
931 /*
[1]932 * Init all R3 components, the order here might be important.
[70918]933 * NEM and HM shall be initialized first!
[1]934 */
[70948]935 Assert(pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NOT_SET);
[70918]936 rc = NEMR3InitConfig(pVM);
[13816]937 if (RT_SUCCESS(rc))
[70918]938 rc = HMR3Init(pVM);
939 if (RT_SUCCESS(rc))
[1]940 {
[70948]941 ASMCompilerBarrier(); /* HMR3Init will have modified bMainExecutionEngine */
942#ifdef VBOX_WITH_RAW_MODE
943 Assert( pVM->bMainExecutionEngine == VM_EXEC_ENGINE_HW_VIRT
944 || pVM->bMainExecutionEngine == VM_EXEC_ENGINE_RAW_MODE
945 || pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API);
946#else
947 Assert( pVM->bMainExecutionEngine == VM_EXEC_ENGINE_HW_VIRT
948 || pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API);
949#endif
[45618]950 rc = MMR3Init(pVM);
[13816]951 if (RT_SUCCESS(rc))
[1]952 {
[45618]953 rc = CPUMR3Init(pVM);
[13816]954 if (RT_SUCCESS(rc))
[1]955 {
[70945]956 rc = NEMR3InitAfterCPUM(pVM);
[13816]957 if (RT_SUCCESS(rc))
[70945]958 rc = PGMR3Init(pVM);
959 if (RT_SUCCESS(rc))
[1]960 {
[40274]961#ifdef VBOX_WITH_REM
[1]962 rc = REMR3Init(pVM);
[40274]963#endif
[13816]964 if (RT_SUCCESS(rc))
[1]965 {
966 rc = MMR3InitPaging(pVM);
[13816]967 if (RT_SUCCESS(rc))
[1]968 rc = TMR3Init(pVM);
[13816]969 if (RT_SUCCESS(rc))
[1]970 {
[31737]971 rc = FTMR3Init(pVM);
[13816]972 if (RT_SUCCESS(rc))
[1]973 {
[31737]974 rc = VMMR3Init(pVM);
[13816]975 if (RT_SUCCESS(rc))
[1]976 {
[31737]977 rc = SELMR3Init(pVM);
[13816]978 if (RT_SUCCESS(rc))
[1]979 {
[31737]980 rc = TRPMR3Init(pVM);
[13816]981 if (RT_SUCCESS(rc))
[1]982 {
[45528]983#ifdef VBOX_WITH_RAW_MODE
[31737]984 rc = CSAMR3Init(pVM);
[13816]985 if (RT_SUCCESS(rc))
[1]986 {
[31737]987 rc = PATMR3Init(pVM);
[13816]988 if (RT_SUCCESS(rc))
[1]989 {
[45528]990#endif
[31737]991 rc = IOMR3Init(pVM);
[13816]992 if (RT_SUCCESS(rc))
[1]993 {
[31737]994 rc = EMR3Init(pVM);
[13816]995 if (RT_SUCCESS(rc))
[1]996 {
[36768]997 rc = IEMR3Init(pVM);
[13816]998 if (RT_SUCCESS(rc))
[1]999 {
[36768]1000 rc = DBGFR3Init(pVM);
[13816]1001 if (RT_SUCCESS(rc))
[1]1002 {
[52009]1003 /* GIM must be init'd before PDM, gimdevR3Construct()
1004 requires GIM provider to be setup. */
[51560]1005 rc = GIMR3Init(pVM);
[31737]1006 if (RT_SUCCESS(rc))
1007 {
[51560]1008 rc = PDMR3Init(pVM);
[36768]1009 if (RT_SUCCESS(rc))
[50953]1010 {
1011 rc = PGMR3InitDynMap(pVM);
1012 if (RT_SUCCESS(rc))
1013 rc = MMR3HyperInitFinalize(pVM);
[45528]1014#ifdef VBOX_WITH_RAW_MODE
[50953]1015 if (RT_SUCCESS(rc))
1016 rc = PATMR3InitFinalize(pVM);
[45528]1017#endif
[50953]1018 if (RT_SUCCESS(rc))
1019 rc = PGMR3InitFinalize(pVM);
1020 if (RT_SUCCESS(rc))
1021 rc = SELMR3InitFinalize(pVM);
1022 if (RT_SUCCESS(rc))
1023 rc = TMR3InitFinalize(pVM);
[40274]1024#ifdef VBOX_WITH_REM
[50953]1025 if (RT_SUCCESS(rc))
1026 rc = REMR3InitFinalize(pVM);
[40274]1027#endif
[50953]1028 if (RT_SUCCESS(rc))
1029 {
1030 PGMR3MemSetup(pVM, false /*fAtReset*/);
1031 PDMR3MemSetup(pVM, false /*fAtReset*/);
1032 }
1033 if (RT_SUCCESS(rc))
1034 rc = vmR3InitDoCompleted(pVM, VMINITCOMPLETED_RING3);
1035 if (RT_SUCCESS(rc))
1036 {
1037 LogFlow(("vmR3InitRing3: returns %Rrc\n", VINF_SUCCESS));
1038 return VINF_SUCCESS;
1039 }
1040
[51560]1041 int rc2 = PDMR3Term(pVM);
[50953]1042 AssertRC(rc2);
[45024]1043 }
[51560]1044 int rc2 = GIMR3Term(pVM);
[36768]1045 AssertRC(rc2);
[31737]1046 }
[36768]1047 int rc2 = DBGFR3Term(pVM);
[31737]1048 AssertRC(rc2);
[1]1049 }
[36768]1050 int rc2 = IEMR3Term(pVM);
[1]1051 AssertRC(rc2);
1052 }
[31737]1053 int rc2 = EMR3Term(pVM);
[1]1054 AssertRC(rc2);
1055 }
[31737]1056 int rc2 = IOMR3Term(pVM);
[1]1057 AssertRC(rc2);
1058 }
[45528]1059#ifdef VBOX_WITH_RAW_MODE
[31737]1060 int rc2 = PATMR3Term(pVM);
[1]1061 AssertRC(rc2);
1062 }
[31737]1063 int rc2 = CSAMR3Term(pVM);
[1]1064 AssertRC(rc2);
1065 }
[45528]1066#endif
[31737]1067 int rc2 = TRPMR3Term(pVM);
[1]1068 AssertRC(rc2);
1069 }
[31737]1070 int rc2 = SELMR3Term(pVM);
[1]1071 AssertRC(rc2);
1072 }
[31737]1073 int rc2 = VMMR3Term(pVM);
[1]1074 AssertRC(rc2);
1075 }
[31737]1076 int rc2 = FTMR3Term(pVM);
[1]1077 AssertRC(rc2);
1078 }
1079 int rc2 = TMR3Term(pVM);
1080 AssertRC(rc2);
1081 }
[40274]1082#ifdef VBOX_WITH_REM
[1]1083 int rc2 = REMR3Term(pVM);
1084 AssertRC(rc2);
[40274]1085#endif
[1]1086 }
1087 int rc2 = PGMR3Term(pVM);
1088 AssertRC(rc2);
1089 }
[45618]1090 //int rc2 = CPUMR3Term(pVM);
1091 //AssertRC(rc2);
[1]1092 }
[45618]1093 /* MMR3Term is not called here because it'll kill the heap. */
[1]1094 }
[45618]1095 int rc2 = HMR3Term(pVM);
1096 AssertRC(rc2);
[1]1097 }
[70918]1098 NEMR3Term(pVM);
[1]1099
[13818]1100 LogFlow(("vmR3InitRing3: returns %Rrc\n", rc));
[1]1101 return rc;
1102}
1103
[13796]1104
[13778]1105/**
[1]1106 * Initializes all R0 components of the VM
1107 */
1108static int vmR3InitRing0(PVM pVM)
1109{
1110 LogFlow(("vmR3InitRing0:\n"));
1111
1112 /*
1113 * Check for FAKE suplib mode.
1114 */
1115 int rc = VINF_SUCCESS;
[5099]1116 const char *psz = RTEnvGet("VBOX_SUPLIB_FAKE");
[1]1117 if (!psz || strcmp(psz, "fake"))
1118 {
1119 /*
1120 * Call the VMMR0 component and let it do the init.
1121 */
1122 rc = VMMR3InitR0(pVM);
1123 }
1124 else
1125 Log(("vmR3InitRing0: skipping because of VBOX_SUPLIB_FAKE=fake\n"));
1126
1127 /*
1128 * Do notifications and return.
1129 */
[13816]1130 if (RT_SUCCESS(rc))
[1]1131 rc = vmR3InitDoCompleted(pVM, VMINITCOMPLETED_RING0);
[36441]1132 if (RT_SUCCESS(rc))
[43387]1133 rc = vmR3InitDoCompleted(pVM, VMINITCOMPLETED_HM);
[7471]1134
[13818]1135 LogFlow(("vmR3InitRing0: returns %Rrc\n", rc));
[1]1136 return rc;
1137}
1138
1139
[45525]1140#ifdef VBOX_WITH_RAW_MODE
[1]1141/**
[45525]1142 * Initializes all RC components of the VM
[1]1143 */
[45525]1144static int vmR3InitRC(PVM pVM)
[1]1145{
[45525]1146 LogFlow(("vmR3InitRC:\n"));
[1]1147
1148 /*
1149 * Check for FAKE suplib mode.
1150 */
1151 int rc = VINF_SUCCESS;
[5099]1152 const char *psz = RTEnvGet("VBOX_SUPLIB_FAKE");
[1]1153 if (!psz || strcmp(psz, "fake"))
1154 {
1155 /*
1156 * Call the VMMR0 component and let it do the init.
1157 */
[13697]1158 rc = VMMR3InitRC(pVM);
[1]1159 }
1160 else
[45525]1161 Log(("vmR3InitRC: skipping because of VBOX_SUPLIB_FAKE=fake\n"));
[1]1162
1163 /*
1164 * Do notifications and return.
1165 */
[13816]1166 if (RT_SUCCESS(rc))
[45618]1167 rc = vmR3InitDoCompleted(pVM, VMINITCOMPLETED_RC);
[45525]1168 LogFlow(("vmR3InitRC: returns %Rrc\n", rc));
[1]1169 return rc;
1170}
[45525]1171#endif /* VBOX_WITH_RAW_MODE */
[1]1172
1173
1174/**
1175 * Do init completed notifications.
1176 *
[34320]1177 * @returns VBox status code.
[58122]1178 * @param pVM The cross context VM structure.
[1]1179 * @param enmWhat What's completed.
1180 */
1181static int vmR3InitDoCompleted(PVM pVM, VMINITCOMPLETED enmWhat)
1182{
[34320]1183 int rc = VMMR3InitCompleted(pVM, enmWhat);
[34326]1184 if (RT_SUCCESS(rc))
[43387]1185 rc = HMR3InitCompleted(pVM, enmWhat);
[36441]1186 if (RT_SUCCESS(rc))
[70918]1187 rc = NEMR3InitCompleted(pVM, enmWhat);
1188 if (RT_SUCCESS(rc))
[60307]1189 rc = PGMR3InitCompleted(pVM, enmWhat);
1190 if (RT_SUCCESS(rc))
[60377]1191 rc = CPUMR3InitCompleted(pVM, enmWhat);
[72642]1192 if (RT_SUCCESS(rc))
1193 rc = EMR3InitCompleted(pVM, enmWhat);
[50596]1194 if (enmWhat == VMINITCOMPLETED_RING3)
1195 {
[61397]1196#ifndef VBOX_WITH_RAW_MODE
[50596]1197 if (RT_SUCCESS(rc))
1198 rc = SSMR3RegisterStub(pVM, "CSAM", 0);
1199 if (RT_SUCCESS(rc))
1200 rc = SSMR3RegisterStub(pVM, "PATM", 0);
[61397]1201#endif
1202#ifndef VBOX_WITH_REM
1203 if (RT_SUCCESS(rc))
1204 rc = SSMR3RegisterStub(pVM, "rem", 1);
1205#endif
[50596]1206 }
[60387]1207 if (RT_SUCCESS(rc))
1208 rc = PDMR3InitCompleted(pVM, enmWhat);
[34320]1209 return rc;
[1]1210}
1211
1212
1213/**
1214 * Calls the relocation functions for all VMM components so they can update
1215 * any GC pointers. When this function is called all the basic VM members
1216 * have been updated and the actual memory relocation have been done
1217 * by the PGM/MM.
1218 *
1219 * This is used both on init and on runtime relocations.
1220 *
[58122]1221 * @param pVM The cross context VM structure.
[1]1222 * @param offDelta Relocation delta relative to old location.
1223 */
[44340]1224VMMR3_INT_DECL(void) VMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
[1]1225{
[13823]1226 LogFlow(("VMR3Relocate: offDelta=%RGv\n", offDelta));
[1]1227
1228 /*
1229 * The order here is very important!
1230 */
1231 PGMR3Relocate(pVM, offDelta);
[6796]1232 PDMR3LdrRelocateU(pVM->pUVM, offDelta);
[1]1233 PGMR3Relocate(pVM, 0); /* Repeat after PDM relocation. */
1234 CPUMR3Relocate(pVM);
[43387]1235 HMR3Relocate(pVM);
[1]1236 SELMR3Relocate(pVM);
1237 VMMR3Relocate(pVM, offDelta);
1238 SELMR3Relocate(pVM); /* !hack! fix stack! */
1239 TRPMR3Relocate(pVM, offDelta);
[45528]1240#ifdef VBOX_WITH_RAW_MODE
[54763]1241 PATMR3Relocate(pVM, (RTRCINTPTR)offDelta);
[1]1242 CSAMR3Relocate(pVM, offDelta);
[45528]1243#endif
[1]1244 IOMR3Relocate(pVM, offDelta);
1245 EMR3Relocate(pVM);
1246 TMR3Relocate(pVM, offDelta);
[36768]1247 IEMR3Relocate(pVM);
[1]1248 DBGFR3Relocate(pVM, offDelta);
1249 PDMR3Relocate(pVM, offDelta);
[63648]1250 GIMR3Relocate(pVM, offDelta);
[1]1251}
1252
1253
1254/**
[23148]1255 * EMT rendezvous worker for VMR3PowerOn.
[1]1256 *
[23148]1257 * @returns VERR_VM_INVALID_VM_STATE or VINF_SUCCESS. (This is a strict return
1258 * code, see FNVMMEMTRENDEZVOUS.)
1259 *
[58122]1260 * @param pVM The cross context VM structure.
[58123]1261 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
[23148]1262 * @param pvUser Ignored.
[1]1263 */
[23148]1264static DECLCALLBACK(VBOXSTRICTRC) vmR3PowerOn(PVM pVM, PVMCPU pVCpu, void *pvUser)
[1]1265{
[23148]1266 LogFlow(("vmR3PowerOn: pVM=%p pVCpu=%p/#%u\n", pVM, pVCpu, pVCpu->idCpu));
1267 Assert(!pvUser); NOREF(pvUser);
[1]1268
[19435]1269 /*
[23148]1270 * The first thread thru here tries to change the state. We shouldn't be
1271 * called again if this fails.
[19435]1272 */
[23148]1273 if (pVCpu->idCpu == pVM->cCpus - 1)
1274 {
1275 int rc = vmR3TrySetState(pVM, "VMR3PowerOn", 1, VMSTATE_POWERING_ON, VMSTATE_CREATED);
1276 if (RT_FAILURE(rc))
1277 return rc;
1278 }
[19428]1279
[23148]1280 VMSTATE enmVMState = VMR3GetState(pVM);
1281 AssertMsgReturn(enmVMState == VMSTATE_POWERING_ON,
1282 ("%s\n", VMR3GetStateName(enmVMState)),
[39402]1283 VERR_VM_UNEXPECTED_UNSTABLE_STATE);
[23148]1284
[1]1285 /*
[23148]1286 * All EMTs changes their state to started.
[1]1287 */
[23148]1288 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED);
[1]1289
1290 /*
[23148]1291 * EMT(0) is last thru here and it will make the notification calls
1292 * and advance the state.
[1]1293 */
[23148]1294 if (pVCpu->idCpu == 0)
1295 {
1296 PDMR3PowerOn(pVM);
1297 vmR3SetState(pVM, VMSTATE_RUNNING, VMSTATE_POWERING_ON);
1298 }
[1]1299
1300 return VINF_SUCCESS;
1301}
1302
1303
1304/**
[23009]1305 * Powers on the virtual machine.
[1]1306 *
[23009]1307 * @returns VBox status code.
1308 *
[44340]1309 * @param pUVM The VM to power on.
[23009]1310 *
[1]1311 * @thread Any thread.
[23009]1312 * @vmstate Created
[23148]1313 * @vmstateto PoweringOn+Running
[1]1314 */
[44340]1315VMMR3DECL(int) VMR3PowerOn(PUVM pUVM)
[1]1316{
[44340]1317 LogFlow(("VMR3PowerOn: pUVM=%p\n", pUVM));
1318 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1319 PVM pVM = pUVM->pVM;
[23009]1320 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
[1]1321
1322 /*
[23152]1323 * Gather all the EMTs to reduce the init TSC drift and keep
[23148]1324 * the state changing APIs a bit uniform.
[1]1325 */
[23148]1326 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING | VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR,
1327 vmR3PowerOn, NULL);
[24730]1328 LogFlow(("VMR3PowerOn: returns %Rrc\n", rc));
[22915]1329 return rc;
1330}
1331
1332
1333/**
[23146]1334 * Does the suspend notifications.
[23009]1335 *
[58122]1336 * @param pVM The cross context VM structure.
[23146]1337 * @thread EMT(0)
1338 */
1339static void vmR3SuspendDoWork(PVM pVM)
1340{
1341 PDMR3Suspend(pVM);
1342}
1343
1344
1345/**
[23148]1346 * EMT rendezvous worker for VMR3Suspend.
[23146]1347 *
[23148]1348 * @returns VERR_VM_INVALID_VM_STATE or VINF_EM_SUSPEND. (This is a strict
1349 * return code, see FNVMMEMTRENDEZVOUS.)
[23009]1350 *
[58122]1351 * @param pVM The cross context VM structure.
[58123]1352 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
[23148]1353 * @param pvUser Ignored.
[23009]1354 */
[23146]1355static DECLCALLBACK(VBOXSTRICTRC) vmR3Suspend(PVM pVM, PVMCPU pVCpu, void *pvUser)
[23009]1356{
[46788]1357 VMSUSPENDREASON enmReason = (VMSUSPENDREASON)(uintptr_t)pvUser;
1358 LogFlow(("vmR3Suspend: pVM=%p pVCpu=%p/#%u enmReason=%d\n", pVM, pVCpu, pVCpu->idCpu, enmReason));
[23009]1359
1360 /*
[23146]1361 * The first EMT switches the state to suspending. If this fails because
1362 * something was racing us in one way or the other, there will be no more
1363 * calls and thus the state assertion below is not going to annoy anyone.
[23009]1364 */
1365 if (pVCpu->idCpu == pVM->cCpus - 1)
[23018]1366 {
[23023]1367 int rc = vmR3TrySetState(pVM, "VMR3Suspend", 2,
[23146]1368 VMSTATE_SUSPENDING, VMSTATE_RUNNING,
1369 VMSTATE_SUSPENDING_EXT_LS, VMSTATE_RUNNING_LS);
[23018]1370 if (RT_FAILURE(rc))
1371 return rc;
[46788]1372 pVM->pUVM->vm.s.enmSuspendReason = enmReason;
[23018]1373 }
[23009]1374
[23024]1375 VMSTATE enmVMState = VMR3GetState(pVM);
[23009]1376 AssertMsgReturn( enmVMState == VMSTATE_SUSPENDING
[23146]1377 || enmVMState == VMSTATE_SUSPENDING_EXT_LS,
[23009]1378 ("%s\n", VMR3GetStateName(enmVMState)),
[39402]1379 VERR_VM_UNEXPECTED_UNSTABLE_STATE);
[23009]1380
1381 /*
[23146]1382 * EMT(0) does the actually suspending *after* all the other CPUs have
[23009]1383 * been thru here.
1384 */
1385 if (pVCpu->idCpu == 0)
1386 {
[23146]1387 vmR3SuspendDoWork(pVM);
[23009]1388
[23146]1389 int rc = vmR3TrySetState(pVM, "VMR3Suspend", 2,
1390 VMSTATE_SUSPENDED, VMSTATE_SUSPENDING,
1391 VMSTATE_SUSPENDED_EXT_LS, VMSTATE_SUSPENDING_EXT_LS);
1392 if (RT_FAILURE(rc))
[39402]1393 return VERR_VM_UNEXPECTED_UNSTABLE_STATE;
[23009]1394 }
1395
1396 return VINF_EM_SUSPEND;
1397}
1398
1399
1400/**
[1]1401 * Suspends a running VM.
1402 *
[23009]1403 * @returns VBox status code. When called on EMT, this will be a strict status
1404 * code that has to be propagated up the call stack.
1405 *
[46788]1406 * @param pUVM The VM to suspend.
1407 * @param enmReason The reason for suspending.
[23009]1408 *
1409 * @thread Any thread.
1410 * @vmstate Running or RunningLS
[23146]1411 * @vmstateto Suspending + Suspended or SuspendingExtLS + SuspendedExtLS
[23009]1412 */
[46788]1413VMMR3DECL(int) VMR3Suspend(PUVM pUVM, VMSUSPENDREASON enmReason)
[23009]1414{
[44340]1415 LogFlow(("VMR3Suspend: pUVM=%p\n", pUVM));
1416 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
[46788]1417 AssertReturn(enmReason > VMSUSPENDREASON_INVALID && enmReason < VMSUSPENDREASON_END, VERR_INVALID_PARAMETER);
[23146]1418
1419 /*
1420 * Gather all the EMTs to make sure there are no races before
1421 * changing the VM state.
1422 */
[44340]1423 int rc = VMMR3EmtRendezvous(pUVM->pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING | VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR,
[46788]1424 vmR3Suspend, (void *)(uintptr_t)enmReason);
[23009]1425 LogFlow(("VMR3Suspend: returns %Rrc\n", rc));
1426 return rc;
1427}
1428
1429
1430/**
[46788]1431 * Retrieves the reason for the most recent suspend.
1432 *
1433 * @returns Suspend reason. VMSUSPENDREASON_INVALID if no suspend has been done
1434 * or the handle is invalid.
1435 * @param pUVM The user mode VM handle.
1436 */
1437VMMR3DECL(VMSUSPENDREASON) VMR3GetSuspendReason(PUVM pUVM)
1438{
1439 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VMSUSPENDREASON_INVALID);
1440 return pUVM->vm.s.enmSuspendReason;
1441}
1442
1443
1444/**
[23148]1445 * EMT rendezvous worker for VMR3Resume.
[23009]1446 *
[23148]1447 * @returns VERR_VM_INVALID_VM_STATE or VINF_EM_RESUME. (This is a strict
1448 * return code, see FNVMMEMTRENDEZVOUS.)
1449 *
[58122]1450 * @param pVM The cross context VM structure.
[58123]1451 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
[46788]1452 * @param pvUser Reason.
[1]1453 */
[23148]1454static DECLCALLBACK(VBOXSTRICTRC) vmR3Resume(PVM pVM, PVMCPU pVCpu, void *pvUser)
[1]1455{
[46788]1456 VMRESUMEREASON enmReason = (VMRESUMEREASON)(uintptr_t)pvUser;
1457 LogFlow(("vmR3Resume: pVM=%p pVCpu=%p/#%u enmReason=%d\n", pVM, pVCpu, pVCpu->idCpu, enmReason));
[1]1458
1459 /*
[23148]1460 * The first thread thru here tries to change the state. We shouldn't be
1461 * called again if this fails.
[1]1462 */
[23148]1463 if (pVCpu->idCpu == pVM->cCpus - 1)
[1]1464 {
[23023]1465 int rc = vmR3TrySetState(pVM, "VMR3Resume", 1, VMSTATE_RESUMING, VMSTATE_SUSPENDED);
[23018]1466 if (RT_FAILURE(rc))
1467 return rc;
[46788]1468 pVM->pUVM->vm.s.enmResumeReason = enmReason;
[23148]1469 }
[1]1470
[23148]1471 VMSTATE enmVMState = VMR3GetState(pVM);
1472 AssertMsgReturn(enmVMState == VMSTATE_RESUMING,
1473 ("%s\n", VMR3GetStateName(enmVMState)),
[39402]1474 VERR_VM_UNEXPECTED_UNSTABLE_STATE);
[23148]1475
1476#if 0
1477 /*
1478 * All EMTs changes their state to started.
1479 */
1480 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED);
1481#endif
1482
1483 /*
1484 * EMT(0) is last thru here and it will make the notification calls
1485 * and advance the state.
1486 */
1487 if (pVCpu->idCpu == 0)
1488 {
[23009]1489 PDMR3Resume(pVM);
1490 vmR3SetState(pVM, VMSTATE_RUNNING, VMSTATE_RESUMING);
[23915]1491 pVM->vm.s.fTeleportedAndNotFullyResumedYet = false;
[23009]1492 }
[1]1493
[23009]1494 return VINF_EM_RESUME;
[1]1495}
1496
1497
1498/**
1499 * Resume VM execution.
1500 *
[23009]1501 * @returns VBox status code. When called on EMT, this will be a strict status
1502 * code that has to be propagated up the call stack.
1503 *
[58122]1504 * @param pUVM The user mode VM handle.
[46788]1505 * @param enmReason The reason we're resuming.
[23009]1506 *
[1]1507 * @thread Any thread.
1508 * @vmstate Suspended
1509 * @vmstateto Running
1510 */
[46788]1511VMMR3DECL(int) VMR3Resume(PUVM pUVM, VMRESUMEREASON enmReason)
[1]1512{
[44340]1513 LogFlow(("VMR3Resume: pUVM=%p\n", pUVM));
1514 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1515 PVM pVM = pUVM->pVM;
[23009]1516 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
[46788]1517 AssertReturn(enmReason > VMRESUMEREASON_INVALID && enmReason < VMRESUMEREASON_END, VERR_INVALID_PARAMETER);
[1]1518
1519 /*
[23148]1520 * Gather all the EMTs to make sure there are no races before
1521 * changing the VM state.
[1]1522 */
[23148]1523 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING | VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR,
[46788]1524 vmR3Resume, (void *)(uintptr_t)enmReason);
[13818]1525 LogFlow(("VMR3Resume: returns %Rrc\n", rc));
[1]1526 return rc;
1527}
1528
1529
1530/**
[46788]1531 * Retrieves the reason for the most recent resume.
1532 *
1533 * @returns Resume reason. VMRESUMEREASON_INVALID if no suspend has been
1534 * done or the handle is invalid.
1535 * @param pUVM The user mode VM handle.
1536 */
1537VMMR3DECL(VMRESUMEREASON) VMR3GetResumeReason(PUVM pUVM)
1538{
1539 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VMRESUMEREASON_INVALID);
1540 return pUVM->vm.s.enmResumeReason;
1541}
1542
1543
1544/**
[23801]1545 * EMT rendezvous worker for VMR3Save and VMR3Teleport that suspends the VM
1546 * after the live step has been completed.
[1]1547 *
[23151]1548 * @returns VERR_VM_INVALID_VM_STATE or VINF_EM_RESUME. (This is a strict
1549 * return code, see FNVMMEMTRENDEZVOUS.)
[23009]1550 *
[58122]1551 * @param pVM The cross context VM structure.
[58123]1552 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
[24353]1553 * @param pvUser The pfSuspended argument of vmR3SaveTeleport.
[1]1554 */
[23151]1555static DECLCALLBACK(VBOXSTRICTRC) vmR3LiveDoSuspend(PVM pVM, PVMCPU pVCpu, void *pvUser)
[1]1556{
[23151]1557 LogFlow(("vmR3LiveDoSuspend: pVM=%p pVCpu=%p/#%u\n", pVM, pVCpu, pVCpu->idCpu));
[24353]1558 bool *pfSuspended = (bool *)pvUser;
[1]1559
1560 /*
[23151]1561 * The first thread thru here tries to change the state. We shouldn't be
1562 * called again if this fails.
[1]1563 */
[23151]1564 if (pVCpu->idCpu == pVM->cCpus - 1U)
1565 {
1566 PUVM pUVM = pVM->pUVM;
1567 int rc;
[1]1568
[23151]1569 RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
1570 VMSTATE enmVMState = pVM->enmVMState;
1571 switch (enmVMState)
1572 {
1573 case VMSTATE_RUNNING_LS:
[61643]1574 vmR3SetStateLocked(pVM, pUVM, VMSTATE_SUSPENDING_LS, VMSTATE_RUNNING_LS, false /*fSetRatherThanClearFF*/);
[23151]1575 rc = VINF_SUCCESS;
1576 break;
1577
1578 case VMSTATE_SUSPENDED_EXT_LS:
1579 case VMSTATE_SUSPENDED_LS: /* (via reset) */
1580 rc = VINF_SUCCESS;
1581 break;
1582
1583 case VMSTATE_DEBUGGING_LS:
1584 rc = VERR_TRY_AGAIN;
1585 break;
1586
1587 case VMSTATE_OFF_LS:
[61643]1588 vmR3SetStateLocked(pVM, pUVM, VMSTATE_OFF, VMSTATE_OFF_LS, false /*fSetRatherThanClearFF*/);
[23151]1589 rc = VERR_SSM_LIVE_POWERED_OFF;
1590 break;
1591
1592 case VMSTATE_FATAL_ERROR_LS:
[61643]1593 vmR3SetStateLocked(pVM, pUVM, VMSTATE_FATAL_ERROR, VMSTATE_FATAL_ERROR_LS, false /*fSetRatherThanClearFF*/);
[23151]1594 rc = VERR_SSM_LIVE_FATAL_ERROR;
1595 break;
1596
1597 case VMSTATE_GURU_MEDITATION_LS:
[61643]1598 vmR3SetStateLocked(pVM, pUVM, VMSTATE_GURU_MEDITATION, VMSTATE_GURU_MEDITATION_LS, false /*fSetRatherThanClearFF*/);
[23151]1599 rc = VERR_SSM_LIVE_GURU_MEDITATION;
1600 break;
1601
1602 case VMSTATE_POWERING_OFF_LS:
1603 case VMSTATE_SUSPENDING_EXT_LS:
1604 case VMSTATE_RESETTING_LS:
1605 default:
1606 AssertMsgFailed(("%s\n", VMR3GetStateName(enmVMState)));
[39402]1607 rc = VERR_VM_UNEXPECTED_VM_STATE;
[23151]1608 break;
1609 }
1610 RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
1611 if (RT_FAILURE(rc))
1612 {
1613 LogFlow(("vmR3LiveDoSuspend: returns %Rrc (state was %s)\n", rc, VMR3GetStateName(enmVMState)));
1614 return rc;
1615 }
1616 }
1617
1618 VMSTATE enmVMState = VMR3GetState(pVM);
1619 AssertMsgReturn(enmVMState == VMSTATE_SUSPENDING_LS,
1620 ("%s\n", VMR3GetStateName(enmVMState)),
[39402]1621 VERR_VM_UNEXPECTED_UNSTABLE_STATE);
[23151]1622
[1]1623 /*
[23151]1624 * Only EMT(0) have work to do since it's last thru here.
[1]1625 */
[23151]1626 if (pVCpu->idCpu == 0)
[23009]1627 {
[23151]1628 vmR3SuspendDoWork(pVM);
[23153]1629 int rc = vmR3TrySetState(pVM, "VMR3Suspend", 1,
[23151]1630 VMSTATE_SUSPENDED_LS, VMSTATE_SUSPENDING_LS);
1631 if (RT_FAILURE(rc))
[39402]1632 return VERR_VM_UNEXPECTED_UNSTABLE_STATE;
[24353]1633
1634 *pfSuspended = true;
[23009]1635 }
[23151]1636
1637 return VINF_EM_SUSPEND;
1638}
1639
1640
1641/**
[23801]1642 * EMT rendezvous worker that VMR3Save and VMR3Teleport uses to clean up a
[23151]1643 * SSMR3LiveDoStep1 failure.
1644 *
1645 * Doing this as a rendezvous operation avoids all annoying transition
1646 * states.
1647 *
1648 * @returns VERR_VM_INVALID_VM_STATE, VINF_SUCCESS or some specific VERR_SSM_*
1649 * status code. (This is a strict return code, see FNVMMEMTRENDEZVOUS.)
1650 *
[58122]1651 * @param pVM The cross context VM structure.
[58123]1652 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
[24918]1653 * @param pvUser The pfSuspended argument of vmR3SaveTeleport.
[23151]1654 */
1655static DECLCALLBACK(VBOXSTRICTRC) vmR3LiveDoStep1Cleanup(PVM pVM, PVMCPU pVCpu, void *pvUser)
1656{
1657 LogFlow(("vmR3LiveDoStep1Cleanup: pVM=%p pVCpu=%p/#%u\n", pVM, pVCpu, pVCpu->idCpu));
[24918]1658 bool *pfSuspended = (bool *)pvUser;
1659 NOREF(pVCpu);
[23151]1660
[27789]1661 int rc = vmR3TrySetState(pVM, "vmR3LiveDoStep1Cleanup", 8,
[24918]1662 VMSTATE_OFF, VMSTATE_OFF_LS, /* 1 */
1663 VMSTATE_FATAL_ERROR, VMSTATE_FATAL_ERROR_LS, /* 2 */
1664 VMSTATE_GURU_MEDITATION, VMSTATE_GURU_MEDITATION_LS, /* 3 */
1665 VMSTATE_SUSPENDED, VMSTATE_SUSPENDED_LS, /* 4 */
[25075]1666 VMSTATE_SUSPENDED, VMSTATE_SAVING,
[24918]1667 VMSTATE_SUSPENDED, VMSTATE_SUSPENDED_EXT_LS,
[23151]1668 VMSTATE_RUNNING, VMSTATE_RUNNING_LS,
1669 VMSTATE_DEBUGGING, VMSTATE_DEBUGGING_LS);
1670 if (rc == 1)
1671 rc = VERR_SSM_LIVE_POWERED_OFF;
[23018]1672 else if (rc == 2)
[23151]1673 rc = VERR_SSM_LIVE_FATAL_ERROR;
1674 else if (rc == 3)
1675 rc = VERR_SSM_LIVE_GURU_MEDITATION;
[24918]1676 else if (rc == 4)
1677 {
1678 *pfSuspended = true;
1679 rc = VINF_SUCCESS;
1680 }
[23151]1681 else if (rc > 0)
1682 rc = VINF_SUCCESS;
[23009]1683 return rc;
[1]1684}
1685
1686
1687/**
[23801]1688 * EMT(0) worker for VMR3Save and VMR3Teleport that completes the live save.
[23037]1689 *
1690 * @returns VBox status code.
[23151]1691 * @retval VINF_SSM_LIVE_SUSPENDED if VMR3Suspend was called.
[23037]1692 *
[58122]1693 * @param pVM The cross context VM structure.
[23151]1694 * @param pSSM The handle of saved state operation.
1695 *
[23037]1696 * @thread EMT(0)
1697 */
[23151]1698static DECLCALLBACK(int) vmR3LiveDoStep2(PVM pVM, PSSMHANDLE pSSM)
[23037]1699{
[23151]1700 LogFlow(("vmR3LiveDoStep2: pVM=%p pSSM=%p\n", pVM, pSSM));
[23037]1701 VM_ASSERT_EMT0(pVM);
1702
1703 /*
[23151]1704 * Advance the state and mark if VMR3Suspend was called.
[23037]1705 */
[23151]1706 int rc = VINF_SUCCESS;
[23904]1707 VMSTATE enmVMState = VMR3GetState(pVM);
1708 if (enmVMState == VMSTATE_SUSPENDED_LS)
[23151]1709 vmR3SetState(pVM, VMSTATE_SAVING, VMSTATE_SUSPENDED_LS);
1710 else
[23037]1711 {
[23904]1712 if (enmVMState != VMSTATE_SAVING)
1713 vmR3SetState(pVM, VMSTATE_SAVING, VMSTATE_SUSPENDED_EXT_LS);
[23151]1714 rc = VINF_SSM_LIVE_SUSPENDED;
[23037]1715 }
[23151]1716
1717 /*
1718 * Finish up and release the handle. Careful with the status codes.
1719 */
1720 int rc2 = SSMR3LiveDoStep2(pSSM);
1721 if (rc == VINF_SUCCESS || (RT_FAILURE(rc2) && RT_SUCCESS(rc)))
1722 rc = rc2;
1723
1724 rc2 = SSMR3LiveDone(pSSM);
1725 if (rc == VINF_SUCCESS || (RT_FAILURE(rc2) && RT_SUCCESS(rc)))
1726 rc = rc2;
1727
1728 /*
1729 * Advance to the final state and return.
1730 */
1731 vmR3SetState(pVM, VMSTATE_SUSPENDED, VMSTATE_SAVING);
1732 Assert(rc > VINF_EM_LAST || rc < VINF_EM_FIRST);
[23037]1733 return rc;
1734}
1735
1736
1737/**
[23801]1738 * Worker for vmR3SaveTeleport that validates the state and calls SSMR3Save or
[23598]1739 * SSMR3LiveSave.
[23009]1740 *
1741 * @returns VBox status code.
1742 *
[58122]1743 * @param pVM The cross context VM structure.
[32193]1744 * @param cMsMaxDowntime The maximum downtime given as milliseconds.
1745 * @param pszFilename The name of the file. NULL if pStreamOps is used.
1746 * @param pStreamOps The stream methods. NULL if pszFilename is used.
1747 * @param pvStreamOpsUser The user argument to the stream methods.
1748 * @param enmAfter What to do afterwards.
1749 * @param pfnProgress Progress callback. Optional.
1750 * @param pvProgressUser User argument for the progress callback.
1751 * @param ppSSM Where to return the saved state handle in case of a
1752 * live snapshot scenario.
1753 * @param fSkipStateChanges Set if we're supposed to skip state changes (FTM delta case)
1754 *
[23151]1755 * @thread EMT
[23009]1756 */
[24874]1757static DECLCALLBACK(int) vmR3Save(PVM pVM, uint32_t cMsMaxDowntime, const char *pszFilename, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser,
[32193]1758 SSMAFTER enmAfter, PFNVMPROGRESS pfnProgress, void *pvProgressUser, PSSMHANDLE *ppSSM,
1759 bool fSkipStateChanges)
[23009]1760{
[32193]1761 int rc = VINF_SUCCESS;
1762
[24874]1763 LogFlow(("vmR3Save: pVM=%p cMsMaxDowntime=%u pszFilename=%p:{%s} pStreamOps=%p pvStreamOpsUser=%p enmAfter=%d pfnProgress=%p pvProgressUser=%p ppSSM=%p\n",
1764 pVM, cMsMaxDowntime, pszFilename, pszFilename, pStreamOps, pvStreamOpsUser, enmAfter, pfnProgress, pvProgressUser, ppSSM));
[23009]1765
[23151]1766 /*
1767 * Validate input.
1768 */
[23668]1769 AssertPtrNull(pszFilename);
1770 AssertPtrNull(pStreamOps);
[23151]1771 AssertPtr(pVM);
[23668]1772 Assert( enmAfter == SSMAFTER_DESTROY
1773 || enmAfter == SSMAFTER_CONTINUE
[23801]1774 || enmAfter == SSMAFTER_TELEPORT);
[23151]1775 AssertPtr(ppSSM);
1776 *ppSSM = NULL;
[23009]1777
[23151]1778 /*
1779 * Change the state and perform/start the saving.
1780 */
[32193]1781 if (!fSkipStateChanges)
1782 {
1783 rc = vmR3TrySetState(pVM, "VMR3Save", 2,
[23153]1784 VMSTATE_SAVING, VMSTATE_SUSPENDED,
1785 VMSTATE_RUNNING_LS, VMSTATE_RUNNING);
[32193]1786 }
1787 else
1788 {
1789 Assert(enmAfter != SSMAFTER_TELEPORT);
1790 rc = 1;
1791 }
1792
[23904]1793 if (rc == 1 && enmAfter != SSMAFTER_TELEPORT)
[23151]1794 {
[31895]1795 rc = SSMR3Save(pVM, pszFilename, pStreamOps, pvStreamOpsUser, enmAfter, pfnProgress, pvProgressUser);
[32193]1796 if (!fSkipStateChanges)
1797 vmR3SetState(pVM, VMSTATE_SUSPENDED, VMSTATE_SAVING);
[23151]1798 }
[23904]1799 else if (rc == 2 || enmAfter == SSMAFTER_TELEPORT)
[23151]1800 {
[32193]1801 Assert(!fSkipStateChanges);
[23915]1802 if (enmAfter == SSMAFTER_TELEPORT)
1803 pVM->vm.s.fTeleportedAndNotFullyResumedYet = true;
[24874]1804 rc = SSMR3LiveSave(pVM, cMsMaxDowntime, pszFilename, pStreamOps, pvStreamOpsUser,
[23598]1805 enmAfter, pfnProgress, pvProgressUser, ppSSM);
[23151]1806 /* (We're not subject to cancellation just yet.) */
1807 }
1808 else
1809 Assert(RT_FAILURE(rc));
[23009]1810 return rc;
1811}
1812
1813
1814/**
[33540]1815 * Common worker for VMR3Save and VMR3Teleport.
[1]1816 *
[22784]1817 * @returns VBox status code.
1818 *
[58122]1819 * @param pVM The cross context VM structure.
[24874]1820 * @param cMsMaxDowntime The maximum downtime given as milliseconds.
[32193]1821 * @param pszFilename The name of the file. NULL if pStreamOps is used.
1822 * @param pStreamOps The stream methods. NULL if pszFilename is used.
1823 * @param pvStreamOpsUser The user argument to the stream methods.
1824 * @param enmAfter What to do afterwards.
1825 * @param pfnProgress Progress callback. Optional.
1826 * @param pvProgressUser User argument for the progress callback.
1827 * @param pfSuspended Set if we suspended the VM.
1828 * @param fSkipStateChanges Set if we're supposed to skip state changes (FTM delta case)
[22784]1829 *
[23598]1830 * @thread Non-EMT
[1]1831 */
[24874]1832static int vmR3SaveTeleport(PVM pVM, uint32_t cMsMaxDowntime,
1833 const char *pszFilename, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser,
[32193]1834 SSMAFTER enmAfter, PFNVMPROGRESS pfnProgress, void *pvProgressUser, bool *pfSuspended,
1835 bool fSkipStateChanges)
[1]1836{
1837 /*
[22784]1838 * Request the operation in EMT(0).
[1]1839 */
[23598]1840 PSSMHANDLE pSSM;
[38838]1841 int rc = VMR3ReqCallWait(pVM, 0 /*idDstCpu*/,
1842 (PFNRT)vmR3Save, 10, pVM, cMsMaxDowntime, pszFilename, pStreamOps, pvStreamOpsUser,
1843 enmAfter, pfnProgress, pvProgressUser, &pSSM, fSkipStateChanges);
[22915]1844 if ( RT_SUCCESS(rc)
1845 && pSSM)
1846 {
[32193]1847 Assert(!fSkipStateChanges);
1848
[22915]1849 /*
1850 * Live snapshot.
[23037]1851 *
[23146]1852 * The state handling here is kind of tricky, doing it on EMT(0) helps
[23151]1853 * a bit. See the VMSTATE diagram for details.
[22915]1854 */
1855 rc = SSMR3LiveDoStep1(pSSM);
1856 if (RT_SUCCESS(rc))
[23146]1857 {
[23904]1858 if (VMR3GetState(pVM) != VMSTATE_SAVING)
1859 for (;;)
1860 {
1861 /* Try suspend the VM. */
1862 rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING | VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR,
[24353]1863 vmR3LiveDoSuspend, pfSuspended);
[23904]1864 if (rc != VERR_TRY_AGAIN)
1865 break;
[23146]1866
[23904]1867 /* Wait for the state to change. */
[24353]1868 RTThreadSleep(250); /** @todo Live Migration: fix this polling wait by some smart use of multiple release event semaphores.. */
[23904]1869 }
[23151]1870 if (RT_SUCCESS(rc))
[38838]1871 rc = VMR3ReqCallWait(pVM, 0 /*idDstCpu*/, (PFNRT)vmR3LiveDoStep2, 2, pVM, pSSM);
[23151]1872 else
[24199]1873 {
[38838]1874 int rc2 = VMR3ReqCallWait(pVM, 0 /*idDstCpu*/, (PFNRT)SSMR3LiveDone, 1, pSSM);
[39034]1875 AssertMsg(rc2 == rc, ("%Rrc != %Rrc\n", rc2, rc)); NOREF(rc2);
[24199]1876 }
[23146]1877 }
[23009]1878 else
1879 {
[38838]1880 int rc2 = VMR3ReqCallWait(pVM, 0 /*idDstCpu*/, (PFNRT)SSMR3LiveDone, 1, pSSM);
[23151]1881 AssertMsg(rc2 == rc, ("%Rrc != %Rrc\n", rc2, rc));
1882
[24918]1883 rc2 = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, vmR3LiveDoStep1Cleanup, pfSuspended);
[23151]1884 if (RT_FAILURE(rc2) && rc == VERR_SSM_CANCELLED)
1885 rc = rc2;
[23009]1886 }
[22915]1887 }
[1]1888
[23598]1889 return rc;
1890}
1891
1892
1893/**
1894 * Save current VM state.
1895 *
1896 * Can be used for both saving the state and creating snapshots.
1897 *
1898 * When called for a VM in the Running state, the saved state is created live
1899 * and the VM is only suspended when the final part of the saving is preformed.
1900 * The VM state will not be restored to Running in this case and it's up to the
1901 * caller to call VMR3Resume if this is desirable. (The rational is that the
1902 * caller probably wish to reconfigure the disks before resuming the VM.)
1903 *
1904 * @returns VBox status code.
1905 *
[44340]1906 * @param pUVM The VM which state should be saved.
[23598]1907 * @param pszFilename The name of the save state file.
1908 * @param fContinueAfterwards Whether continue execution afterwards or not.
1909 * When in doubt, set this to true.
1910 * @param pfnProgress Progress callback. Optional.
1911 * @param pvUser User argument for the progress callback.
[24922]1912 * @param pfSuspended Set if we suspended the VM.
[23598]1913 *
1914 * @thread Non-EMT.
1915 * @vmstate Suspended or Running
1916 * @vmstateto Saving+Suspended or
[33540]1917 * RunningLS+SuspendingLS+SuspendedLS+Saving+Suspended.
[23598]1918 */
[58126]1919VMMR3DECL(int) VMR3Save(PUVM pUVM, const char *pszFilename, bool fContinueAfterwards, PFNVMPROGRESS pfnProgress, void *pvUser,
1920 bool *pfSuspended)
[23598]1921{
[44340]1922 LogFlow(("VMR3Save: pUVM=%p pszFilename=%p:{%s} fContinueAfterwards=%RTbool pfnProgress=%p pvUser=%p pfSuspended=%p\n",
1923 pUVM, pszFilename, pszFilename, fContinueAfterwards, pfnProgress, pvUser, pfSuspended));
[23598]1924
1925 /*
1926 * Validate input.
1927 */
[24922]1928 AssertPtr(pfSuspended);
1929 *pfSuspended = false;
[44340]1930 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1931 PVM pVM = pUVM->pVM;
[23598]1932 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1933 VM_ASSERT_OTHER_THREAD(pVM);
[32169]1934 AssertReturn(VALID_PTR(pszFilename), VERR_INVALID_POINTER);
1935 AssertReturn(*pszFilename, VERR_INVALID_PARAMETER);
[23598]1936 AssertPtrNullReturn(pfnProgress, VERR_INVALID_POINTER);
1937
1938 /*
[23801]1939 * Join paths with VMR3Teleport.
[23598]1940 */
1941 SSMAFTER enmAfter = fContinueAfterwards ? SSMAFTER_CONTINUE : SSMAFTER_DESTROY;
[24874]1942 int rc = vmR3SaveTeleport(pVM, 250 /*cMsMaxDowntime*/,
[32169]1943 pszFilename, NULL /* pStreamOps */, NULL /* pvStreamOpsUser */,
[32193]1944 enmAfter, pfnProgress, pvUser, pfSuspended,
1945 false /* fSkipStateChanges */);
[24922]1946 LogFlow(("VMR3Save: returns %Rrc (*pfSuspended=%RTbool)\n", rc, *pfSuspended));
[1]1947 return rc;
1948}
1949
[32169]1950/**
1951 * Save current VM state (used by FTM)
1952 *
1953 *
1954 * @returns VBox status code.
1955 *
[58122]1956 * @param pUVM The user mode VM handle.
[32169]1957 * @param pStreamOps The stream methods.
1958 * @param pvStreamOpsUser The user argument to the stream methods.
1959 * @param pfSuspended Set if we suspended the VM.
[32193]1960 * @param fSkipStateChanges Set if we're supposed to skip state changes (FTM delta case)
[32169]1961 *
1962 * @thread Any
1963 * @vmstate Suspended or Running
1964 * @vmstateto Saving+Suspended or
[33540]1965 * RunningLS+SuspendingLS+SuspendedLS+Saving+Suspended.
[32169]1966 */
[44340]1967VMMR3_INT_DECL(int) VMR3SaveFT(PUVM pUVM, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser, bool *pfSuspended, bool fSkipStateChanges)
[32169]1968{
[44340]1969 LogFlow(("VMR3SaveFT: pUVM=%p pStreamOps=%p pvSteamOpsUser=%p pfSuspended=%p\n",
1970 pUVM, pStreamOps, pvStreamOpsUser, pfSuspended));
[1]1971
[32169]1972 /*
1973 * Validate input.
1974 */
1975 AssertPtr(pfSuspended);
1976 *pfSuspended = false;
[44340]1977 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1978 PVM pVM = pUVM->pVM;
[32169]1979 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1980 AssertReturn(pStreamOps, VERR_INVALID_PARAMETER);
1981
1982 /*
1983 * Join paths with VMR3Teleport.
1984 */
1985 int rc = vmR3SaveTeleport(pVM, 250 /*cMsMaxDowntime*/,
1986 NULL, pStreamOps, pvStreamOpsUser,
[32193]1987 SSMAFTER_CONTINUE, NULL, NULL, pfSuspended,
1988 fSkipStateChanges);
[32171]1989 LogFlow(("VMR3SaveFT: returns %Rrc (*pfSuspended=%RTbool)\n", rc, *pfSuspended));
[32169]1990 return rc;
1991}
1992
1993
[1]1994/**
[23801]1995 * Teleport the VM (aka live migration).
[1]1996 *
[23598]1997 * @returns VBox status code.
[23009]1998 *
[44340]1999 * @param pUVM The VM which state should be saved.
[24874]2000 * @param cMsMaxDowntime The maximum downtime given as milliseconds.
[23598]2001 * @param pStreamOps The stream methods.
2002 * @param pvStreamOpsUser The user argument to the stream methods.
2003 * @param pfnProgress Progress callback. Optional.
2004 * @param pvProgressUser User argument for the progress callback.
[24353]2005 * @param pfSuspended Set if we suspended the VM.
[23598]2006 *
2007 * @thread Non-EMT.
2008 * @vmstate Suspended or Running
2009 * @vmstateto Saving+Suspended or
[33540]2010 * RunningLS+SuspendingLS+SuspendedLS+Saving+Suspended.
[23598]2011 */
[44340]2012VMMR3DECL(int) VMR3Teleport(PUVM pUVM, uint32_t cMsMaxDowntime, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser,
[24353]2013 PFNVMPROGRESS pfnProgress, void *pvProgressUser, bool *pfSuspended)
[23598]2014{
[44340]2015 LogFlow(("VMR3Teleport: pUVM=%p cMsMaxDowntime=%u pStreamOps=%p pvStreamOps=%p pfnProgress=%p pvProgressUser=%p\n",
2016 pUVM, cMsMaxDowntime, pStreamOps, pvStreamOpsUser, pfnProgress, pvProgressUser));
[23598]2017
2018 /*
2019 * Validate input.
2020 */
[24353]2021 AssertPtr(pfSuspended);
2022 *pfSuspended = false;
[44340]2023 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2024 PVM pVM = pUVM->pVM;
[23598]2025 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
2026 VM_ASSERT_OTHER_THREAD(pVM);
2027 AssertPtrReturn(pStreamOps, VERR_INVALID_POINTER);
2028 AssertPtrNullReturn(pfnProgress, VERR_INVALID_POINTER);
2029
2030 /*
2031 * Join paths with VMR3Save.
2032 */
[24874]2033 int rc = vmR3SaveTeleport(pVM, cMsMaxDowntime,
2034 NULL /*pszFilename*/, pStreamOps, pvStreamOpsUser,
[32193]2035 SSMAFTER_TELEPORT, pfnProgress, pvProgressUser, pfSuspended,
2036 false /* fSkipStateChanges */);
[24353]2037 LogFlow(("VMR3Teleport: returns %Rrc (*pfSuspended=%RTbool)\n", rc, *pfSuspended));
[23598]2038 return rc;
2039}
2040
2041
2042
2043/**
2044 * EMT(0) worker for VMR3LoadFromFile and VMR3LoadFromStream.
2045 *
[23009]2046 * @returns VBox status code.
[23598]2047 *
[44340]2048 * @param pUVM Pointer to the VM.
[32206]2049 * @param pszFilename The name of the file. NULL if pStreamOps is used.
2050 * @param pStreamOps The stream methods. NULL if pszFilename is used.
2051 * @param pvStreamOpsUser The user argument to the stream methods.
2052 * @param pfnProgress Progress callback. Optional.
[58126]2053 * @param pvProgressUser User argument for the progress callback.
[32206]2054 * @param fTeleporting Indicates whether we're teleporting or not.
2055 * @param fSkipStateChanges Set if we're supposed to skip state changes (FTM delta case)
[23598]2056 *
[23009]2057 * @thread EMT.
[1]2058 */
[44340]2059static DECLCALLBACK(int) vmR3Load(PUVM pUVM, const char *pszFilename, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser,
[32206]2060 PFNVMPROGRESS pfnProgress, void *pvProgressUser, bool fTeleporting,
2061 bool fSkipStateChanges)
[1]2062{
[32206]2063 int rc = VINF_SUCCESS;
2064
[44340]2065 LogFlow(("vmR3Load: pUVM=%p pszFilename=%p:{%s} pStreamOps=%p pvStreamOpsUser=%p pfnProgress=%p pvProgressUser=%p fTeleporting=%RTbool\n",
2066 pUVM, pszFilename, pszFilename, pStreamOps, pvStreamOpsUser, pfnProgress, pvProgressUser, fTeleporting));
[1]2067
2068 /*
[23009]2069 * Validate input (paranoia).
[1]2070 */
[44340]2071 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2072 PVM pVM = pUVM->pVM;
2073 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
[23598]2074 AssertPtrNull(pszFilename);
2075 AssertPtrNull(pStreamOps);
2076 AssertPtrNull(pfnProgress);
[1]2077
[32206]2078 if (!fSkipStateChanges)
2079 {
2080 /*
2081 * Change the state and perform the load.
2082 *
2083 * Always perform a relocation round afterwards to make sure hypervisor
2084 * selectors and such are correct.
2085 */
2086 rc = vmR3TrySetState(pVM, "VMR3Load", 2,
[44340]2087 VMSTATE_LOADING, VMSTATE_CREATED,
2088 VMSTATE_LOADING, VMSTATE_SUSPENDED);
[32206]2089 if (RT_FAILURE(rc))
2090 return rc;
2091 }
[23917]2092 pVM->vm.s.fTeleportedAndNotFullyResumedYet = fTeleporting;
[23009]2093
[44340]2094 uint32_t cErrorsPriorToSave = VMR3GetErrorCount(pUVM);
[23598]2095 rc = SSMR3Load(pVM, pszFilename, pStreamOps, pvStreamOpsUser, SSMAFTER_RESUME, pfnProgress, pvProgressUser);
[23009]2096 if (RT_SUCCESS(rc))
[22915]2097 {
[23009]2098 VMR3Relocate(pVM, 0 /*offDelta*/);
[32206]2099 if (!fSkipStateChanges)
2100 vmR3SetState(pVM, VMSTATE_SUSPENDED, VMSTATE_LOADING);
[22915]2101 }
2102 else
2103 {
[23917]2104 pVM->vm.s.fTeleportedAndNotFullyResumedYet = false;
[32206]2105 if (!fSkipStateChanges)
2106 vmR3SetState(pVM, VMSTATE_LOAD_FAILURE, VMSTATE_LOADING);
2107
[44340]2108 if (cErrorsPriorToSave == VMR3GetErrorCount(pUVM))
[24508]2109 rc = VMSetError(pVM, rc, RT_SRC_POS,
2110 N_("Unable to restore the virtual machine's saved state from '%s'. "
2111 "It may be damaged or from an older version of VirtualBox. "
2112 "Please discard the saved state before starting the virtual machine"),
2113 pszFilename);
[22915]2114 }
[1]2115
2116 return rc;
2117}
2118
2119
2120/**
[23009]2121 * Loads a VM state into a newly created VM or a one that is suspended.
[1]2122 *
[23009]2123 * To restore a saved state on VM startup, call this function and then resume
2124 * the VM instead of powering it on.
[1]2125 *
[23009]2126 * @returns VBox status code.
2127 *
[58126]2128 * @param pUVM The user mode VM structure.
[23009]2129 * @param pszFilename The name of the save state file.
[1]2130 * @param pfnProgress Progress callback. Optional.
2131 * @param pvUser User argument for the progress callback.
[23009]2132 *
[1]2133 * @thread Any thread.
2134 * @vmstate Created, Suspended
[23009]2135 * @vmstateto Loading+Suspended
[1]2136 */
[44340]2137VMMR3DECL(int) VMR3LoadFromFile(PUVM pUVM, const char *pszFilename, PFNVMPROGRESS pfnProgress, void *pvUser)
[1]2138{
[44340]2139 LogFlow(("VMR3LoadFromFile: pUVM=%p pszFilename=%p:{%s} pfnProgress=%p pvUser=%p\n",
2140 pUVM, pszFilename, pszFilename, pfnProgress, pvUser));
[1]2141
2142 /*
2143 * Validate input.
2144 */
[44340]2145 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
[23009]2146 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
[1]2147
2148 /*
[23148]2149 * Forward the request to EMT(0). No need to setup a rendezvous here
2150 * since there is no execution taking place when this call is allowed.
[1]2151 */
[44340]2152 int rc = VMR3ReqCallWaitU(pUVM, 0 /*idDstCpu*/, (PFNRT)vmR3Load, 8,
2153 pUVM, pszFilename, (uintptr_t)NULL /*pStreamOps*/, (uintptr_t)NULL /*pvStreamOpsUser*/, pfnProgress, pvUser,
2154 false /*fTeleporting*/, false /* fSkipStateChanges */);
[23598]2155 LogFlow(("VMR3LoadFromFile: returns %Rrc\n", rc));
[1]2156 return rc;
2157}
2158
2159
2160/**
[33540]2161 * VMR3LoadFromFile for arbitrary file streams.
[23598]2162 *
2163 * @returns VBox status code.
2164 *
[44340]2165 * @param pUVM Pointer to the VM.
[23598]2166 * @param pStreamOps The stream methods.
2167 * @param pvStreamOpsUser The user argument to the stream methods.
2168 * @param pfnProgress Progress callback. Optional.
2169 * @param pvProgressUser User argument for the progress callback.
2170 *
2171 * @thread Any thread.
2172 * @vmstate Created, Suspended
2173 * @vmstateto Loading+Suspended
2174 */
[44340]2175VMMR3DECL(int) VMR3LoadFromStream(PUVM pUVM, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser,
[23598]2176 PFNVMPROGRESS pfnProgress, void *pvProgressUser)
2177{
[44340]2178 LogFlow(("VMR3LoadFromStream: pUVM=%p pStreamOps=%p pvStreamOpsUser=%p pfnProgress=%p pvProgressUser=%p\n",
2179 pUVM, pStreamOps, pvStreamOpsUser, pfnProgress, pvProgressUser));
[23598]2180
2181 /*
2182 * Validate input.
2183 */
[44340]2184 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
[23598]2185 AssertPtrReturn(pStreamOps, VERR_INVALID_POINTER);
2186
2187 /*
2188 * Forward the request to EMT(0). No need to setup a rendezvous here
2189 * since there is no execution taking place when this call is allowed.
2190 */
[44340]2191 int rc = VMR3ReqCallWaitU(pUVM, 0 /*idDstCpu*/, (PFNRT)vmR3Load, 8,
2192 pUVM, (uintptr_t)NULL /*pszFilename*/, pStreamOps, pvStreamOpsUser, pfnProgress, pvProgressUser,
2193 true /*fTeleporting*/, false /* fSkipStateChanges */);
[23598]2194 LogFlow(("VMR3LoadFromStream: returns %Rrc\n", rc));
2195 return rc;
2196}
2197
2198
2199/**
[44340]2200 * Special version for the FT component, it skips state changes.
[32206]2201 *
2202 * @returns VBox status code.
2203 *
[44340]2204 * @param pUVM The VM handle.
[32206]2205 * @param pStreamOps The stream methods.
2206 * @param pvStreamOpsUser The user argument to the stream methods.
2207 *
2208 * @thread Any thread.
2209 * @vmstate Created, Suspended
2210 * @vmstateto Loading+Suspended
2211 */
[44340]2212VMMR3_INT_DECL(int) VMR3LoadFromStreamFT(PUVM pUVM, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser)
[32206]2213{
[44340]2214 LogFlow(("VMR3LoadFromStreamFT: pUVM=%p pStreamOps=%p pvStreamOpsUser=%p\n", pUVM, pStreamOps, pvStreamOpsUser));
[32206]2215
2216 /*
2217 * Validate input.
2218 */
[44340]2219 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
[32206]2220 AssertPtrReturn(pStreamOps, VERR_INVALID_POINTER);
2221
2222 /*
2223 * Forward the request to EMT(0). No need to setup a rendezvous here
2224 * since there is no execution taking place when this call is allowed.
2225 */
[44340]2226 int rc = VMR3ReqCallWaitU(pUVM, 0 /*idDstCpu*/, (PFNRT)vmR3Load, 8,
2227 pUVM, (uintptr_t)NULL /*pszFilename*/, pStreamOps, pvStreamOpsUser, NULL, NULL,
2228 true /*fTeleporting*/, true /* fSkipStateChanges */);
[32206]2229 LogFlow(("VMR3LoadFromStream: returns %Rrc\n", rc));
2230 return rc;
2231}
2232
2233/**
[23148]2234 * EMT rendezvous worker for VMR3PowerOff.
[1]2235 *
[23148]2236 * @returns VERR_VM_INVALID_VM_STATE or VINF_EM_OFF. (This is a strict
2237 * return code, see FNVMMEMTRENDEZVOUS.)
[1]2238 *
[58122]2239 * @param pVM The cross context VM structure.
[58123]2240 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
[23148]2241 * @param pvUser Ignored.
[1]2242 */
[23146]2243static DECLCALLBACK(VBOXSTRICTRC) vmR3PowerOff(PVM pVM, PVMCPU pVCpu, void *pvUser)
[1]2244{
[23146]2245 LogFlow(("vmR3PowerOff: pVM=%p pVCpu=%p/#%u\n", pVM, pVCpu, pVCpu->idCpu));
[23148]2246 Assert(!pvUser); NOREF(pvUser);
[1]2247
2248 /*
[23009]2249 * The first EMT thru here will change the state to PoweringOff.
[1]2250 */
[23009]2251 if (pVCpu->idCpu == pVM->cCpus - 1)
[1]2252 {
[25238]2253 int rc = vmR3TrySetState(pVM, "VMR3PowerOff", 11,
2254 VMSTATE_POWERING_OFF, VMSTATE_RUNNING, /* 1 */
2255 VMSTATE_POWERING_OFF, VMSTATE_SUSPENDED, /* 2 */
2256 VMSTATE_POWERING_OFF, VMSTATE_DEBUGGING, /* 3 */
2257 VMSTATE_POWERING_OFF, VMSTATE_LOAD_FAILURE, /* 4 */
2258 VMSTATE_POWERING_OFF, VMSTATE_GURU_MEDITATION, /* 5 */
2259 VMSTATE_POWERING_OFF, VMSTATE_FATAL_ERROR, /* 6 */
2260 VMSTATE_POWERING_OFF, VMSTATE_CREATED, /* 7 */ /** @todo update the diagram! */
2261 VMSTATE_POWERING_OFF_LS, VMSTATE_RUNNING_LS, /* 8 */
2262 VMSTATE_POWERING_OFF_LS, VMSTATE_DEBUGGING_LS, /* 9 */
2263 VMSTATE_POWERING_OFF_LS, VMSTATE_GURU_MEDITATION_LS,/* 10 */
2264 VMSTATE_POWERING_OFF_LS, VMSTATE_FATAL_ERROR_LS); /* 11 */
[23024]2265 if (RT_FAILURE(rc))
2266 return rc;
[23151]2267 if (rc >= 7)
[44347]2268 SSMR3Cancel(pVM->pUVM);
[1]2269 }
2270
2271 /*
[23009]2272 * Check the state.
[1]2273 */
[23024]2274 VMSTATE enmVMState = VMR3GetState(pVM);
[23009]2275 AssertMsgReturn( enmVMState == VMSTATE_POWERING_OFF
2276 || enmVMState == VMSTATE_POWERING_OFF_LS,
2277 ("%s\n", VMR3GetStateName(enmVMState)),
2278 VERR_VM_INVALID_VM_STATE);
2279
2280 /*
2281 * EMT(0) does the actual power off work here *after* all the other EMTs
2282 * have been thru and entered the STOPPED state.
2283 */
2284 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STOPPED);
2285 if (pVCpu->idCpu == 0)
[1]2286 {
[23009]2287 /*
2288 * For debugging purposes, we will log a summary of the guest state at this point.
2289 */
2290 if (enmVMState != VMSTATE_GURU_MEDITATION)
2291 {
2292 /** @todo make the state dumping at VMR3PowerOff optional. */
[32356]2293 bool fOldBuffered = RTLogRelSetBuffering(true /*fBuffered*/);
[61657]2294 RTLogRelPrintf("****************** Guest state at power off for VCpu %u ******************\n", pVCpu->idCpu);
2295 DBGFR3InfoEx(pVM->pUVM, pVCpu->idCpu, "cpumguest", "verbose", DBGFR3InfoLogRelHlp());
[23009]2296 RTLogRelPrintf("***\n");
[67529]2297 DBGFR3InfoEx(pVM->pUVM, pVCpu->idCpu, "cpumguesthwvirt", "verbose", DBGFR3InfoLogRelHlp());
2298 RTLogRelPrintf("***\n");
[61657]2299 DBGFR3InfoEx(pVM->pUVM, pVCpu->idCpu, "mode", NULL, DBGFR3InfoLogRelHlp());
[23009]2300 RTLogRelPrintf("***\n");
[44399]2301 DBGFR3Info(pVM->pUVM, "activetimers", NULL, DBGFR3InfoLogRelHlp());
[23009]2302 RTLogRelPrintf("***\n");
[44399]2303 DBGFR3Info(pVM->pUVM, "gdt", NULL, DBGFR3InfoLogRelHlp());
[23009]2304 /** @todo dump guest call stack. */
[32356]2305 RTLogRelSetBuffering(fOldBuffered);
[23009]2306 RTLogRelPrintf("************** End of Guest state at power off ***************\n");
2307 }
2308
2309 /*
2310 * Perform the power off notifications and advance the state to
2311 * Off or OffLS.
2312 */
2313 PDMR3PowerOff(pVM);
[45006]2314 DBGFR3PowerOff(pVM);
[23023]2315
2316 PUVM pUVM = pVM->pUVM;
2317 RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
[23024]2318 enmVMState = pVM->enmVMState;
[23023]2319 if (enmVMState == VMSTATE_POWERING_OFF_LS)
[61643]2320 vmR3SetStateLocked(pVM, pUVM, VMSTATE_OFF_LS, VMSTATE_POWERING_OFF_LS, false /*fSetRatherThanClearFF*/);
[23023]2321 else
[61643]2322 vmR3SetStateLocked(pVM, pUVM, VMSTATE_OFF, VMSTATE_POWERING_OFF, false /*fSetRatherThanClearFF*/);
[23023]2323 RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
[1]2324 }
[61657]2325 else if (enmVMState != VMSTATE_GURU_MEDITATION)
2326 {
2327 /** @todo make the state dumping at VMR3PowerOff optional. */
2328 bool fOldBuffered = RTLogRelSetBuffering(true /*fBuffered*/);
2329 RTLogRelPrintf("****************** Guest state at power off for VCpu %u ******************\n", pVCpu->idCpu);
2330 DBGFR3InfoEx(pVM->pUVM, pVCpu->idCpu, "cpumguest", "verbose", DBGFR3InfoLogRelHlp());
2331 RTLogRelPrintf("***\n");
[67529]2332 DBGFR3InfoEx(pVM->pUVM, pVCpu->idCpu, "cpumguesthwvirt", "verbose", DBGFR3InfoLogRelHlp());
2333 RTLogRelPrintf("***\n");
[61657]2334 DBGFR3InfoEx(pVM->pUVM, pVCpu->idCpu, "mode", NULL, DBGFR3InfoLogRelHlp());
2335 RTLogRelPrintf("***\n");
2336 RTLogRelSetBuffering(fOldBuffered);
2337 RTLogRelPrintf("************** End of Guest state at power off for VCpu %u ***************\n", pVCpu->idCpu);
2338 }
2339
[23009]2340 return VINF_EM_OFF;
[1]2341}
2342
2343
2344/**
[23024]2345 * Power off the VM.
[1]2346 *
[23024]2347 * @returns VBox status code. When called on EMT, this will be a strict status
2348 * code that has to be propagated up the call stack.
2349 *
[44340]2350 * @param pUVM The handle of the VM to be powered off.
[23024]2351 *
[1]2352 * @thread Any thread.
[11473]2353 * @vmstate Suspended, Running, Guru Meditation, Load Failure
[23024]2354 * @vmstateto Off or OffLS
[1]2355 */
[44340]2356VMMR3DECL(int) VMR3PowerOff(PUVM pUVM)
[1]2357{
[44340]2358 LogFlow(("VMR3PowerOff: pUVM=%p\n", pUVM));
2359 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2360 PVM pVM = pUVM->pVM;
[23024]2361 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
[1]2362
2363 /*
[23146]2364 * Gather all the EMTs to make sure there are no races before
2365 * changing the VM state.
2366 */
2367 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING | VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR,
2368 vmR3PowerOff, NULL);
[13818]2369 LogFlow(("VMR3PowerOff: returns %Rrc\n", rc));
[1]2370 return rc;
2371}
2372
2373
2374/**
2375 * Destroys the VM.
2376 *
[23009]2377 * The VM must be powered off (or never really powered on) to call this
2378 * function. The VM handle is destroyed and can no longer be used up successful
2379 * return.
2380 *
[6796]2381 * @returns VBox status code.
[23009]2382 *
[58122]2383 * @param pUVM The user mode VM handle.
[23009]2384 *
[30473]2385 * @thread Any none emulation thread.
[1]2386 * @vmstate Off, Created
2387 * @vmstateto N/A
2388 */
[44340]2389VMMR3DECL(int) VMR3Destroy(PUVM pUVM)
[1]2390{
[44340]2391 LogFlow(("VMR3Destroy: pUVM=%p\n", pUVM));
[1]2392
2393 /*
2394 * Validate input.
2395 */
[44340]2396 if (!pUVM)
[36041]2397 return VERR_INVALID_VM_HANDLE;
[44340]2398 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2399 PVM pVM = pUVM->pVM;
[23009]2400 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
[30473]2401 AssertLogRelReturn(!VM_IS_EMT(pVM), VERR_VM_THREAD_IS_EMT);
[1]2402
2403 /*
[44340]2404 * Change VM state to destroying and aall vmR3Destroy on each of the EMTs
2405 * ending with EMT(0) doing the bulk of the cleanup.
[1]2406 */
[23023]2407 int rc = vmR3TrySetState(pVM, "VMR3Destroy", 1, VMSTATE_DESTROYING, VMSTATE_OFF);
[23018]2408 if (RT_FAILURE(rc))
2409 return rc;
[6796]2410
[44340]2411 rc = VMR3ReqCallWait(pVM, VMCPUID_ALL_REVERSE, (PFNRT)vmR3Destroy, 1, pVM);
2412 AssertLogRelRC(rc);
[6796]2413
[1]2414 /*
[44340]2415 * Wait for EMTs to quit and destroy the UVM.
[1]2416 */
[30473]2417 vmR3DestroyUVM(pUVM, 30000);
[1]2418
2419 LogFlow(("VMR3Destroy: returns VINF_SUCCESS\n"));
2420 return VINF_SUCCESS;
2421}
2422
2423
2424/**
[6796]2425 * Internal destruction worker.
[1]2426 *
[23009]2427 * This is either called from VMR3Destroy via VMR3ReqCallU or from
2428 * vmR3EmulationThreadWithId when EMT(0) terminates after having called
2429 * VMR3Destroy().
[6796]2430 *
[23009]2431 * When called on EMT(0), it will performed the great bulk of the destruction.
2432 * When called on the other EMTs, they will do nothing and the whole purpose is
2433 * to return VINF_EM_TERMINATE so they break out of their run loops.
2434 *
2435 * @returns VINF_EM_TERMINATE.
[58122]2436 * @param pVM The cross context VM structure.
[1]2437 */
2438DECLCALLBACK(int) vmR3Destroy(PVM pVM)
2439{
[23009]2440 PUVM pUVM = pVM->pUVM;
[19265]2441 PVMCPU pVCpu = VMMGetCpu(pVM);
[23009]2442 Assert(pVCpu);
2443 LogFlow(("vmR3Destroy: pVM=%p pUVM=%p pVCpu=%p idCpu=%u\n", pVM, pUVM, pVCpu, pVCpu->idCpu));
[19265]2444
[1]2445 /*
[30473]2446 * Only VCPU 0 does the full cleanup (last).
[1]2447 */
[23009]2448 if (pVCpu->idCpu == 0)
2449 {
2450 /*
2451 * Dump statistics to the log.
2452 */
[1]2453#if defined(VBOX_WITH_STATISTICS) || defined(LOG_ENABLED)
[23009]2454 RTLogFlags(NULL, "nodisabled nobuffered");
[1]2455#endif
[55881]2456//#ifdef VBOX_WITH_STATISTICS
2457// STAMR3Dump(pUVM, "*");
2458//#else
[23009]2459 LogRel(("************************* Statistics *************************\n"));
[44347]2460 STAMR3DumpToReleaseLog(pUVM, "*");
[23009]2461 LogRel(("********************* End of statistics **********************\n"));
[55881]2462//#endif
[1]2463
[23009]2464 /*
2465 * Destroy the VM components.
2466 */
2467 int rc = TMR3Term(pVM);
2468 AssertRC(rc);
[5684]2469#ifdef VBOX_WITH_DEBUGGER
[44399]2470 rc = DBGCTcpTerminate(pUVM, pUVM->vm.s.pvDBGC);
[23009]2471 pUVM->vm.s.pvDBGC = NULL;
[5684]2472#endif
[23009]2473 AssertRC(rc);
[31737]2474 rc = FTMR3Term(pVM);
2475 AssertRC(rc);
[44401]2476 rc = PDMR3Term(pVM);
2477 AssertRC(rc);
[57863]2478 rc = GIMR3Term(pVM);
2479 AssertRC(rc);
[23009]2480 rc = DBGFR3Term(pVM);
2481 AssertRC(rc);
[36768]2482 rc = IEMR3Term(pVM);
2483 AssertRC(rc);
[23009]2484 rc = EMR3Term(pVM);
2485 AssertRC(rc);
2486 rc = IOMR3Term(pVM);
2487 AssertRC(rc);
[45528]2488#ifdef VBOX_WITH_RAW_MODE
[23009]2489 rc = CSAMR3Term(pVM);
2490 AssertRC(rc);
2491 rc = PATMR3Term(pVM);
2492 AssertRC(rc);
[45528]2493#endif
[23009]2494 rc = TRPMR3Term(pVM);
2495 AssertRC(rc);
2496 rc = SELMR3Term(pVM);
2497 AssertRC(rc);
[40274]2498#ifdef VBOX_WITH_REM
[23009]2499 rc = REMR3Term(pVM);
2500 AssertRC(rc);
[40274]2501#endif
[43387]2502 rc = HMR3Term(pVM);
[23009]2503 AssertRC(rc);
[70918]2504 rc = NEMR3Term(pVM);
2505 AssertRC(rc);
[23009]2506 rc = PGMR3Term(pVM);
2507 AssertRC(rc);
2508 rc = VMMR3Term(pVM); /* Terminates the ring-0 code! */
2509 AssertRC(rc);
2510 rc = CPUMR3Term(pVM);
2511 AssertRC(rc);
2512 SSMR3Term(pVM);
[45152]2513 rc = PDMR3CritSectBothTerm(pVM);
[23009]2514 AssertRC(rc);
2515 rc = MMR3Term(pVM);
2516 AssertRC(rc);
[1]2517
[23009]2518 /*
[30473]2519 * We're done, tell the other EMTs to quit.
[23009]2520 */
2521 ASMAtomicUoWriteBool(&pUVM->vm.s.fTerminateEMT, true);
[30473]2522 ASMAtomicWriteU32(&pVM->fGlobalForcedActions, VM_FF_CHECK_VM_STATE); /* Can't hurt... */
[23009]2523 LogFlow(("vmR3Destroy: returning %Rrc\n", VINF_EM_TERMINATE));
2524 }
[68853]2525
2526 /*
2527 * Decrement the active EMT count here.
2528 */
2529 PUVMCPU pUVCpu = &pUVM->aCpus[pVCpu->idCpu];
2530 if (!pUVCpu->vm.s.fBeenThruVmDestroy)
2531 {
2532 pUVCpu->vm.s.fBeenThruVmDestroy = true;
2533 ASMAtomicDecU32(&pUVM->vm.s.cActiveEmts);
2534 }
2535 else
2536 AssertFailed();
2537
[1]2538 return VINF_EM_TERMINATE;
2539}
2540
2541
2542/**
[6796]2543 * Destroys the UVM portion.
2544 *
2545 * This is called as the final step in the VM destruction or as the cleanup
[30473]2546 * in case of a creation failure.
[6796]2547 *
[58126]2548 * @param pUVM The user mode VM structure.
[19101]2549 * @param cMilliesEMTWait The number of milliseconds to wait for the emulation
2550 * threads.
[1]2551 */
[19101]2552static void vmR3DestroyUVM(PUVM pUVM, uint32_t cMilliesEMTWait)
[1]2553{
2554 /*
[19101]2555 * Signal termination of each the emulation threads and
2556 * wait for them to complete.
[6796]2557 */
[66096]2558 /* Signal them - in reverse order since EMT(0) waits for the others. */
[19101]2559 ASMAtomicUoWriteBool(&pUVM->vm.s.fTerminateEMT, true);
[30473]2560 if (pUVM->pVM)
2561 VM_FF_SET(pUVM->pVM, VM_FF_CHECK_VM_STATE); /* Can't hurt... */
[66096]2562 VMCPUID iCpu = pUVM->cCpus;
2563 while (iCpu-- > 0)
[6796]2564 {
[19400]2565 VMR3NotifyGlobalFFU(pUVM, VMNOTIFYFF_FLAGS_DONE_REM);
[66096]2566 RTSemEventSignal(pUVM->aCpus[iCpu].vm.s.EventSemWait);
[19101]2567 }
2568
[66096]2569 /* Wait for EMT(0), it in turn waits for the rest. */
[19101]2570 ASMAtomicUoWriteBool(&pUVM->vm.s.fTerminateEMT, true);
[66096]2571
2572 RTTHREAD const hSelf = RTThreadSelf();
2573 RTTHREAD hThread = pUVM->aCpus[0].vm.s.ThreadEMT;
2574 if ( hThread != NIL_RTTHREAD
2575 && hThread != hSelf)
[19101]2576 {
[66096]2577 int rc2 = RTThreadWait(hThread, RT_MAX(cMilliesEMTWait, 2000), NULL);
2578 if (rc2 == VERR_TIMEOUT) /* avoid the assertion when debugging. */
2579 rc2 = RTThreadWait(hThread, 1000, NULL);
2580 AssertLogRelMsgRC(rc2, ("iCpu=0 rc=%Rrc\n", rc2));
2581 if (RT_SUCCESS(rc2))
2582 pUVM->aCpus[0].vm.s.ThreadEMT = NIL_RTTHREAD;
2583 }
2584
2585 /* Just in case we're in a weird failure situation w/o EMT(0) to do the
2586 waiting, wait the other EMTs too. */
2587 for (iCpu = 1; iCpu < pUVM->cCpus; iCpu++)
2588 {
2589 ASMAtomicXchgHandle(&pUVM->aCpus[iCpu].vm.s.ThreadEMT, NIL_RTTHREAD, &hThread);
2590 if (hThread != NIL_RTTHREAD)
[19095]2591 {
[66096]2592 if (hThread != hSelf)
2593 {
2594 int rc2 = RTThreadWait(hThread, 250 /*ms*/, NULL);
2595 AssertLogRelMsgRC(rc2, ("iCpu=%u rc=%Rrc\n", iCpu, rc2));
2596 if (RT_SUCCESS(rc2))
2597 continue;
2598 }
2599 pUVM->aCpus[iCpu].vm.s.ThreadEMT = hThread;
[19095]2600 }
[19101]2601 }
[19089]2602
[19101]2603 /* Cleanup the semaphores. */
[66096]2604 iCpu = pUVM->cCpus;
2605 while (iCpu-- > 0)
[19217]2606 {
[66096]2607 RTSemEventDestroy(pUVM->aCpus[iCpu].vm.s.EventSemWait);
2608 pUVM->aCpus[iCpu].vm.s.EventSemWait = NIL_RTSEMEVENT;
[19217]2609 }
[6796]2610
2611 /*
[5031]2612 * Free the event semaphores associated with the request packets.
[1]2613 */
2614 unsigned cReqs = 0;
[6796]2615 for (unsigned i = 0; i < RT_ELEMENTS(pUVM->vm.s.apReqFree); i++)
[1]2616 {
[6796]2617 PVMREQ pReq = pUVM->vm.s.apReqFree[i];
2618 pUVM->vm.s.apReqFree[i] = NULL;
[1]2619 for (; pReq; pReq = pReq->pNext, cReqs++)
2620 {
2621 pReq->enmState = VMREQSTATE_INVALID;
2622 RTSemEventDestroy(pReq->EventSem);
2623 }
2624 }
[6796]2625 Assert(cReqs == pUVM->vm.s.cReqFree); NOREF(cReqs);
[1]2626
2627 /*
2628 * Kill all queued requests. (There really shouldn't be any!)
2629 */
2630 for (unsigned i = 0; i < 10; i++)
2631 {
[38838]2632 PVMREQ pReqHead = ASMAtomicXchgPtrT(&pUVM->vm.s.pPriorityReqs, NULL, PVMREQ);
[1]2633 if (!pReqHead)
[38838]2634 {
2635 pReqHead = ASMAtomicXchgPtrT(&pUVM->vm.s.pNormalReqs, NULL, PVMREQ);
2636 if (!pReqHead)
2637 break;
2638 }
2639 AssertLogRelMsgFailed(("Requests pending! VMR3Destroy caller has to serialize this.\n"));
2640
[1]2641 for (PVMREQ pReq = pReqHead; pReq; pReq = pReq->pNext)
2642 {
[39402]2643 ASMAtomicUoWriteS32(&pReq->iStatus, VERR_VM_REQUEST_KILLED);
[6796]2644 ASMAtomicWriteSize(&pReq->enmState, VMREQSTATE_INVALID);
[1]2645 RTSemEventSignal(pReq->EventSem);
2646 RTThreadSleep(2);
2647 RTSemEventDestroy(pReq->EventSem);
2648 }
2649 /* give them a chance to respond before we free the request memory. */
2650 RTThreadSleep(32);
2651 }
2652
[19293]2653 /*
[19265]2654 * Now all queued VCPU requests (again, there shouldn't be any).
2655 */
[25238]2656 for (VMCPUID idCpu = 0; idCpu < pUVM->cCpus; idCpu++)
[19265]2657 {
[25238]2658 PUVMCPU pUVCpu = &pUVM->aCpus[idCpu];
[19265]2659
2660 for (unsigned i = 0; i < 10; i++)
2661 {
[38838]2662 PVMREQ pReqHead = ASMAtomicXchgPtrT(&pUVCpu->vm.s.pPriorityReqs, NULL, PVMREQ);
[19265]2663 if (!pReqHead)
[38838]2664 {
2665 pReqHead = ASMAtomicXchgPtrT(&pUVCpu->vm.s.pNormalReqs, NULL, PVMREQ);
2666 if (!pReqHead)
2667 break;
2668 }
2669 AssertLogRelMsgFailed(("Requests pending! VMR3Destroy caller has to serialize this.\n"));
2670
[19265]2671 for (PVMREQ pReq = pReqHead; pReq; pReq = pReq->pNext)
2672 {
[39402]2673 ASMAtomicUoWriteS32(&pReq->iStatus, VERR_VM_REQUEST_KILLED);
[19265]2674 ASMAtomicWriteSize(&pReq->enmState, VMREQSTATE_INVALID);
2675 RTSemEventSignal(pReq->EventSem);
2676 RTThreadSleep(2);
2677 RTSemEventDestroy(pReq->EventSem);
2678 }
2679 /* give them a chance to respond before we free the request memory. */
2680 RTThreadSleep(32);
2681 }
2682 }
2683
[1]2684 /*
[6796]2685 * Make sure the VMMR0.r0 module and whatever else is unloaded.
[1]2686 */
[6796]2687 PDMR3TermUVM(pUVM);
[1]2688
[65718]2689 RTCritSectDelete(&pUVM->vm.s.AtErrorCritSect);
2690 RTCritSectDelete(&pUVM->vm.s.AtStateCritSect);
2691
[1]2692 /*
[6796]2693 * Terminate the support library if initialized.
[1]2694 */
[6796]2695 if (pUVM->vm.s.pSession)
2696 {
[20864]2697 int rc = SUPR3Term(false /*fForced*/);
[6796]2698 AssertRC(rc);
2699 pUVM->vm.s.pSession = NIL_RTR0PTR;
2700 }
[1]2701
[6796]2702 /*
[36041]2703 * Release the UVM structure reference.
[6796]2704 */
[36041]2705 VMR3ReleaseUVM(pUVM);
[6796]2706
[36041]2707 /*
2708 * Clean up and flush logs.
2709 */
[1]2710 RTLogFlush(NULL);
2711}
2712
2713
2714/**
2715 * Worker which checks integrity of some internal structures.
2716 * This is yet another attempt to track down that AVL tree crash.
2717 */
2718static void vmR3CheckIntegrity(PVM pVM)
2719{
2720#ifdef VBOX_STRICT
2721 int rc = PGMR3CheckIntegrity(pVM);
2722 AssertReleaseRC(rc);
[62647]2723#else
2724 RT_NOREF_PV(pVM);
[1]2725#endif
2726}
2727
2728
2729/**
[60404]2730 * EMT rendezvous worker for VMR3ResetFF for doing soft/warm reset.
[1]2731 *
[60404]2732 * @returns VERR_VM_INVALID_VM_STATE, VINF_EM_RESCHEDULE.
2733 * (This is a strict return code, see FNVMMEMTRENDEZVOUS.)
2734 *
2735 * @param pVM The cross context VM structure.
2736 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2737 * @param pvUser The reset flags.
2738 */
2739static DECLCALLBACK(VBOXSTRICTRC) vmR3SoftReset(PVM pVM, PVMCPU pVCpu, void *pvUser)
2740{
2741 uint32_t fResetFlags = *(uint32_t *)pvUser;
2742
2743
2744 /*
2745 * The first EMT will try change the state to resetting. If this fails,
2746 * we won't get called for the other EMTs.
2747 */
2748 if (pVCpu->idCpu == pVM->cCpus - 1)
2749 {
2750 int rc = vmR3TrySetState(pVM, "vmR3ResetSoft", 3,
2751 VMSTATE_SOFT_RESETTING, VMSTATE_RUNNING,
2752 VMSTATE_SOFT_RESETTING, VMSTATE_SUSPENDED,
2753 VMSTATE_SOFT_RESETTING_LS, VMSTATE_RUNNING_LS);
2754 if (RT_FAILURE(rc))
2755 return rc;
[73351]2756 pVM->vm.s.cResets++;
2757 pVM->vm.s.cSoftResets++;
[60404]2758 }
2759
2760 /*
2761 * Check the state.
2762 */
2763 VMSTATE enmVMState = VMR3GetState(pVM);
2764 AssertLogRelMsgReturn( enmVMState == VMSTATE_SOFT_RESETTING
2765 || enmVMState == VMSTATE_SOFT_RESETTING_LS,
2766 ("%s\n", VMR3GetStateName(enmVMState)),
2767 VERR_VM_UNEXPECTED_UNSTABLE_STATE);
2768
2769 /*
2770 * EMT(0) does the full cleanup *after* all the other EMTs has been
2771 * thru here and been told to enter the EMSTATE_WAIT_SIPI state.
2772 *
2773 * Because there are per-cpu reset routines and order may/is important,
2774 * the following sequence looks a bit ugly...
2775 */
2776
2777 /* Reset the VCpu state. */
2778 VMCPU_ASSERT_STATE(pVCpu, VMCPUSTATE_STARTED);
2779
2780 /*
2781 * Soft reset the VM components.
2782 */
2783 if (pVCpu->idCpu == 0)
2784 {
2785#ifdef VBOX_WITH_REM
2786 REMR3Reset(pVM);
2787#endif
2788 PDMR3SoftReset(pVM, fResetFlags);
2789 TRPMR3Reset(pVM);
2790 CPUMR3Reset(pVM); /* This must come *after* PDM (due to APIC base MSR caching). */
2791 EMR3Reset(pVM);
2792 HMR3Reset(pVM); /* This must come *after* PATM, CSAM, CPUM, SELM and TRPM. */
[70918]2793 NEMR3Reset(pVM);
[60404]2794
2795 /*
2796 * Since EMT(0) is the last to go thru here, it will advance the state.
2797 * (Unlike vmR3HardReset we won't be doing any suspending of live
2798 * migration VMs here since memory is unchanged.)
2799 */
2800 PUVM pUVM = pVM->pUVM;
2801 RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
2802 enmVMState = pVM->enmVMState;
2803 if (enmVMState == VMSTATE_SOFT_RESETTING)
2804 {
2805 if (pUVM->vm.s.enmPrevVMState == VMSTATE_SUSPENDED)
[61643]2806 vmR3SetStateLocked(pVM, pUVM, VMSTATE_SUSPENDED, VMSTATE_SOFT_RESETTING, false /*fSetRatherThanClearFF*/);
[60404]2807 else
[61643]2808 vmR3SetStateLocked(pVM, pUVM, VMSTATE_RUNNING, VMSTATE_SOFT_RESETTING, false /*fSetRatherThanClearFF*/);
[60404]2809 }
2810 else
[61643]2811 vmR3SetStateLocked(pVM, pUVM, VMSTATE_RUNNING_LS, VMSTATE_SOFT_RESETTING_LS, false /*fSetRatherThanClearFF*/);
[60404]2812 RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
2813 }
2814
2815 return VINF_EM_RESCHEDULE;
2816}
2817
2818
2819/**
2820 * EMT rendezvous worker for VMR3Reset and VMR3ResetFF.
2821 *
[23148]2822 * This is called by the emulation threads as a response to the reset request
2823 * issued by VMR3Reset().
[1]2824 *
[23148]2825 * @returns VERR_VM_INVALID_VM_STATE, VINF_EM_RESET or VINF_EM_SUSPEND. (This
2826 * is a strict return code, see FNVMMEMTRENDEZVOUS.)
2827 *
[58122]2828 * @param pVM The cross context VM structure.
[58123]2829 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
[23148]2830 * @param pvUser Ignored.
[1]2831 */
[60404]2832static DECLCALLBACK(VBOXSTRICTRC) vmR3HardReset(PVM pVM, PVMCPU pVCpu, void *pvUser)
[1]2833{
[23148]2834 Assert(!pvUser); NOREF(pvUser);
[19322]2835
[19435]2836 /*
[23146]2837 * The first EMT will try change the state to resetting. If this fails,
2838 * we won't get called for the other EMTs.
[19435]2839 */
[23009]2840 if (pVCpu->idCpu == pVM->cCpus - 1)
2841 {
[60404]2842 int rc = vmR3TrySetState(pVM, "vmR3HardReset", 3,
[23024]2843 VMSTATE_RESETTING, VMSTATE_RUNNING,
2844 VMSTATE_RESETTING, VMSTATE_SUSPENDED,
2845 VMSTATE_RESETTING_LS, VMSTATE_RUNNING_LS);
2846 if (RT_FAILURE(rc))
2847 return rc;
[73351]2848 pVM->vm.s.cResets++;
2849 pVM->vm.s.cHardResets++;
[20199]2850 }
[19322]2851
[1]2852 /*
[23009]2853 * Check the state.
[1]2854 */
[23024]2855 VMSTATE enmVMState = VMR3GetState(pVM);
[23009]2856 AssertLogRelMsgReturn( enmVMState == VMSTATE_RESETTING
2857 || enmVMState == VMSTATE_RESETTING_LS,
2858 ("%s\n", VMR3GetStateName(enmVMState)),
[39402]2859 VERR_VM_UNEXPECTED_UNSTABLE_STATE);
[1]2860
[23009]2861 /*
2862 * EMT(0) does the full cleanup *after* all the other EMTs has been
2863 * thru here and been told to enter the EMSTATE_WAIT_SIPI state.
2864 *
2865 * Because there are per-cpu reset routines and order may/is important,
2866 * the following sequence looks a bit ugly...
2867 */
2868 if (pVCpu->idCpu == 0)
2869 vmR3CheckIntegrity(pVM);
[1]2870
[23009]2871 /* Reset the VCpu state. */
2872 VMCPU_ASSERT_STATE(pVCpu, VMCPUSTATE_STARTED);
2873
2874 /* Clear all pending forced actions. */
[74786]2875 VMCPU_FF_CLEAR_MASK(pVCpu, VMCPU_FF_ALL_MASK & ~VMCPU_FF_REQUEST);
[23009]2876
[1]2877 /*
2878 * Reset the VM components.
2879 */
[23009]2880 if (pVCpu->idCpu == 0)
2881 {
[45528]2882#ifdef VBOX_WITH_RAW_MODE
[23009]2883 PATMR3Reset(pVM);
2884 CSAMR3Reset(pVM);
[45528]2885#endif
[54065]2886 GIMR3Reset(pVM); /* This must come *before* PDM and TM. */
[23009]2887 PDMR3Reset(pVM);
[45024]2888 PGMR3Reset(pVM);
[23009]2889 SELMR3Reset(pVM);
2890 TRPMR3Reset(pVM);
[40274]2891#ifdef VBOX_WITH_REM
[23009]2892 REMR3Reset(pVM);
[40274]2893#endif
[23009]2894 IOMR3Reset(pVM);
[60377]2895 CPUMR3Reset(pVM); /* This must come *after* PDM (due to APIC base MSR caching). */
[23009]2896 TMR3Reset(pVM);
2897 EMR3Reset(pVM);
[43387]2898 HMR3Reset(pVM); /* This must come *after* PATM, CSAM, CPUM, SELM and TRPM. */
[70918]2899 NEMR3Reset(pVM);
[1]2900
[23009]2901 /*
[45024]2902 * Do memory setup.
2903 */
2904 PGMR3MemSetup(pVM, true /*fAtReset*/);
2905 PDMR3MemSetup(pVM, true /*fAtReset*/);
2906
2907 /*
[23009]2908 * Since EMT(0) is the last to go thru here, it will advance the state.
[23146]2909 * When a live save is active, we will move on to SuspendingLS but
2910 * leave it for VMR3Reset to do the actual suspending due to deadlock risks.
[23009]2911 */
2912 PUVM pUVM = pVM->pUVM;
2913 RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
[23037]2914 enmVMState = pVM->enmVMState;
2915 if (enmVMState == VMSTATE_RESETTING)
2916 {
2917 if (pUVM->vm.s.enmPrevVMState == VMSTATE_SUSPENDED)
[61643]2918 vmR3SetStateLocked(pVM, pUVM, VMSTATE_SUSPENDED, VMSTATE_RESETTING, false /*fSetRatherThanClearFF*/);
[23037]2919 else
[61643]2920 vmR3SetStateLocked(pVM, pUVM, VMSTATE_RUNNING, VMSTATE_RESETTING, false /*fSetRatherThanClearFF*/);
[23037]2921 }
[23009]2922 else
[61643]2923 vmR3SetStateLocked(pVM, pUVM, VMSTATE_SUSPENDING_LS, VMSTATE_RESETTING_LS, false /*fSetRatherThanClearFF*/);
[23009]2924 RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
2925
2926 vmR3CheckIntegrity(pVM);
[23146]2927
2928 /*
2929 * Do the suspend bit as well.
2930 * It only requires some EMT(0) work at present.
2931 */
2932 if (enmVMState != VMSTATE_RESETTING)
2933 {
2934 vmR3SuspendDoWork(pVM);
2935 vmR3SetState(pVM, VMSTATE_SUSPENDED_LS, VMSTATE_SUSPENDING_LS);
2936 }
[23009]2937 }
2938
[23146]2939 return enmVMState == VMSTATE_RESETTING
2940 ? VINF_EM_RESET
2941 : VINF_EM_SUSPEND; /** @todo VINF_EM_SUSPEND has lower priority than VINF_EM_RESET, so fix races. Perhaps add a new code for this combined case. */
[23009]2942}
2943
2944
2945/**
[60404]2946 * Internal worker for VMR3Reset, VMR3ResetFF, VMR3TripleFault.
2947 *
2948 * @returns VBox status code.
2949 * @param pVM The cross context VM structure.
2950 * @param fHardReset Whether it's a hard reset or not.
2951 * @param fResetFlags The reset flags (PDMVMRESET_F_XXX).
2952 */
2953static VBOXSTRICTRC vmR3ResetCommon(PVM pVM, bool fHardReset, uint32_t fResetFlags)
2954{
2955 LogFlow(("vmR3ResetCommon: fHardReset=%RTbool fResetFlags=%#x\n", fHardReset, fResetFlags));
2956 int rc;
2957 if (fHardReset)
2958 {
2959 /*
2960 * Hard reset.
2961 */
2962 /* Check whether we're supposed to power off instead of resetting. */
2963 if (pVM->vm.s.fPowerOffInsteadOfReset)
2964 {
2965 PUVM pUVM = pVM->pUVM;
2966 if ( pUVM->pVmm2UserMethods
2967 && pUVM->pVmm2UserMethods->pfnNotifyResetTurnedIntoPowerOff)
2968 pUVM->pVmm2UserMethods->pfnNotifyResetTurnedIntoPowerOff(pUVM->pVmm2UserMethods, pUVM);
2969 return VMR3PowerOff(pUVM);
2970 }
2971
2972 /* Gather all the EMTs to make sure there are no races before changing
2973 the VM state. */
2974 rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING | VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR,
2975 vmR3HardReset, NULL);
2976 }
2977 else
2978 {
2979 /*
2980 * Soft reset. Since we only support this with a single CPU active,
2981 * we must be on EMT #0 here.
2982 */
2983 VM_ASSERT_EMT0(pVM);
2984 rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING | VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR,
2985 vmR3SoftReset, &fResetFlags);
2986 }
2987
2988 LogFlow(("vmR3ResetCommon: returns %Rrc\n", rc));
2989 return rc;
2990}
2991
2992
2993
2994/**
[23009]2995 * Reset the current VM.
2996 *
2997 * @returns VBox status code.
[44340]2998 * @param pUVM The VM to reset.
[23009]2999 */
[44340]3000VMMR3DECL(int) VMR3Reset(PUVM pUVM)
[23009]3001{
3002 LogFlow(("VMR3Reset:\n"));
[44340]3003 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
3004 PVM pVM = pUVM->pVM;
[23009]3005 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
3006
[60404]3007 return VBOXSTRICTRC_VAL(vmR3ResetCommon(pVM, true, 0));
3008}
[48528]3009
[60404]3010
3011/**
3012 * Handle the reset force flag or triple fault.
3013 *
3014 * This handles both soft and hard resets (see PDMVMRESET_F_XXX).
3015 *
3016 * @returns VBox status code.
[60406]3017 * @param pVM The cross context VM structure.
[60404]3018 * @thread EMT
3019 *
[60406]3020 * @remarks Caller is expected to clear the VM_FF_RESET force flag.
[60404]3021 */
3022VMMR3_INT_DECL(VBOXSTRICTRC) VMR3ResetFF(PVM pVM)
3023{
3024 LogFlow(("VMR3ResetFF:\n"));
3025
[1]3026 /*
[60404]3027 * First consult the firmware on whether this is a hard or soft reset.
[23146]3028 */
[60404]3029 uint32_t fResetFlags;
3030 bool fHardReset = PDMR3GetResetInfo(pVM, 0 /*fOverride*/, &fResetFlags);
3031 return vmR3ResetCommon(pVM, fHardReset, fResetFlags);
[1]3032}
3033
3034
3035/**
[60404]3036 * For handling a CPU reset on triple fault.
3037 *
3038 * According to one mainboard manual, a CPU triple fault causes the 286 CPU to
3039 * send a SHUTDOWN signal to the chipset. The chipset responds by sending a
3040 * RESET signal to the CPU. So, it should be very similar to a soft/warm reset.
3041 *
3042 * @returns VBox status code.
[60406]3043 * @param pVM The cross context VM structure.
[60404]3044 * @thread EMT
3045 */
3046VMMR3_INT_DECL(VBOXSTRICTRC) VMR3ResetTripleFault(PVM pVM)
3047{
3048 LogFlow(("VMR3ResetTripleFault:\n"));
3049
3050 /*
3051 * First consult the firmware on whether this is a hard or soft reset.
3052 */
3053 uint32_t fResetFlags;
3054 bool fHardReset = PDMR3GetResetInfo(pVM, PDMVMRESET_F_TRIPLE_FAULT, &fResetFlags);
3055 return vmR3ResetCommon(pVM, fHardReset, fResetFlags);
3056}
3057
3058
3059/**
[41783]3060 * Gets the user mode VM structure pointer given Pointer to the VM.
[36041]3061 *
3062 * @returns Pointer to the user mode VM structure on success. NULL if @a pVM is
3063 * invalid (asserted).
[58122]3064 * @param pVM The cross context VM structure.
[36041]3065 * @sa VMR3GetVM, VMR3RetainUVM
3066 */
3067VMMR3DECL(PUVM) VMR3GetUVM(PVM pVM)
3068{
3069 VM_ASSERT_VALID_EXT_RETURN(pVM, NULL);
3070 return pVM->pUVM;
3071}
3072
3073
3074/**
3075 * Gets the shared VM structure pointer given the pointer to the user mode VM
3076 * structure.
3077 *
[41777]3078 * @returns Pointer to the VM.
[36041]3079 * NULL if @a pUVM is invalid (asserted) or if no shared VM structure
3080 * is currently associated with it.
3081 * @param pUVM The user mode VM handle.
3082 * @sa VMR3GetUVM
3083 */
3084VMMR3DECL(PVM) VMR3GetVM(PUVM pUVM)
3085{
3086 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NULL);
3087 return pUVM->pVM;
3088}
3089
3090
3091/**
3092 * Retain the user mode VM handle.
3093 *
3094 * @returns Reference count.
3095 * UINT32_MAX if @a pUVM is invalid.
3096 *
3097 * @param pUVM The user mode VM handle.
3098 * @sa VMR3ReleaseUVM
3099 */
3100VMMR3DECL(uint32_t) VMR3RetainUVM(PUVM pUVM)
3101{
3102 UVM_ASSERT_VALID_EXT_RETURN(pUVM, UINT32_MAX);
3103 uint32_t cRefs = ASMAtomicIncU32(&pUVM->vm.s.cUvmRefs);
3104 AssertMsg(cRefs > 0 && cRefs < _64K, ("%u\n", cRefs));
3105 return cRefs;
3106}
3107
3108
3109/**
3110 * Does the final release of the UVM structure.
3111 *
3112 * @param pUVM The user mode VM handle.
3113 */
3114static void vmR3DoReleaseUVM(PUVM pUVM)
3115{
3116 /*
3117 * Free the UVM.
3118 */
3119 Assert(!pUVM->pVM);
3120
[65718]3121 MMR3HeapFree(pUVM->vm.s.pszName);
3122 pUVM->vm.s.pszName = NULL;
3123
[36041]3124 MMR3TermUVM(pUVM);
3125 STAMR3TermUVM(pUVM);
3126
3127 ASMAtomicUoWriteU32(&pUVM->u32Magic, UINT32_MAX);
3128 RTTlsFree(pUVM->vm.s.idxTLS);
[73097]3129 RTMemPageFree(pUVM, RT_UOFFSETOF_DYN(UVM, aCpus[pUVM->cCpus]));
[36041]3130}
3131
3132
3133/**
3134 * Releases a refernece to the mode VM handle.
3135 *
3136 * @returns The new reference count, 0 if destroyed.
3137 * UINT32_MAX if @a pUVM is invalid.
3138 *
3139 * @param pUVM The user mode VM handle.
3140 * @sa VMR3RetainUVM
3141 */
3142VMMR3DECL(uint32_t) VMR3ReleaseUVM(PUVM pUVM)
3143{
3144 if (!pUVM)
3145 return 0;
3146 UVM_ASSERT_VALID_EXT_RETURN(pUVM, UINT32_MAX);
3147 uint32_t cRefs = ASMAtomicDecU32(&pUVM->vm.s.cUvmRefs);
3148 if (!cRefs)
3149 vmR3DoReleaseUVM(pUVM);
3150 else
3151 AssertMsg(cRefs < _64K, ("%u\n", cRefs));
3152 return cRefs;
3153}
3154
3155
3156/**
3157 * Gets the VM name.
3158 *
3159 * @returns Pointer to a read-only string containing the name. NULL if called
3160 * too early.
3161 * @param pUVM The user mode VM handle.
3162 */
3163VMMR3DECL(const char *) VMR3GetName(PUVM pUVM)
3164{
3165 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NULL);
3166 return pUVM->vm.s.pszName;
3167}
3168
3169
3170/**
3171 * Gets the VM UUID.
3172 *
3173 * @returns pUuid on success, NULL on failure.
3174 * @param pUVM The user mode VM handle.
3175 * @param pUuid Where to store the UUID.
3176 */
3177VMMR3DECL(PRTUUID) VMR3GetUuid(PUVM pUVM, PRTUUID pUuid)
3178{
3179 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NULL);
3180 AssertPtrReturn(pUuid, NULL);
3181
3182 *pUuid = pUVM->vm.s.Uuid;
3183 return pUuid;
3184}
3185
3186
3187/**
[1]3188 * Gets the current VM state.
3189 *
3190 * @returns The current VM state.
[58122]3191 * @param pVM The cross context VM structure.
[1]3192 * @thread Any
3193 */
[12989]3194VMMR3DECL(VMSTATE) VMR3GetState(PVM pVM)
[1]3195{
[39916]3196 AssertMsgReturn(RT_VALID_ALIGNED_PTR(pVM, PAGE_SIZE), ("%p\n", pVM), VMSTATE_TERMINATED);
3197 VMSTATE enmVMState = pVM->enmVMState;
3198 return enmVMState >= VMSTATE_CREATING && enmVMState <= VMSTATE_TERMINATED ? enmVMState : VMSTATE_TERMINATED;
[1]3199}
3200
3201
3202/**
[36041]3203 * Gets the current VM state.
3204 *
3205 * @returns The current VM state.
3206 * @param pUVM The user-mode VM handle.
3207 * @thread Any
3208 */
3209VMMR3DECL(VMSTATE) VMR3GetStateU(PUVM pUVM)
3210{
3211 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VMSTATE_TERMINATED);
3212 if (RT_UNLIKELY(!pUVM->pVM))
3213 return VMSTATE_TERMINATED;
3214 return pUVM->pVM->enmVMState;
3215}
3216
3217
3218/**
[1]3219 * Gets the state name string for a VM state.
3220 *
3221 * @returns Pointer to the state name. (readonly)
3222 * @param enmState The state.
3223 */
[12989]3224VMMR3DECL(const char *) VMR3GetStateName(VMSTATE enmState)
[1]3225{
3226 switch (enmState)
3227 {
3228 case VMSTATE_CREATING: return "CREATING";
3229 case VMSTATE_CREATED: return "CREATED";
[22915]3230 case VMSTATE_LOADING: return "LOADING";
[22961]3231 case VMSTATE_POWERING_ON: return "POWERING_ON";
[23009]3232 case VMSTATE_RESUMING: return "RESUMING";
[1]3233 case VMSTATE_RUNNING: return "RUNNING";
[22961]3234 case VMSTATE_RUNNING_LS: return "RUNNING_LS";
[31695]3235 case VMSTATE_RUNNING_FT: return "RUNNING_FT";
[22915]3236 case VMSTATE_RESETTING: return "RESETTING";
[22961]3237 case VMSTATE_RESETTING_LS: return "RESETTING_LS";
[60404]3238 case VMSTATE_SOFT_RESETTING: return "SOFT_RESETTING";
3239 case VMSTATE_SOFT_RESETTING_LS: return "SOFT_RESETTING_LS";
[22915]3240 case VMSTATE_SUSPENDED: return "SUSPENDED";
[23009]3241 case VMSTATE_SUSPENDED_LS: return "SUSPENDED_LS";
[23146]3242 case VMSTATE_SUSPENDED_EXT_LS: return "SUSPENDED_EXT_LS";
[23009]3243 case VMSTATE_SUSPENDING: return "SUSPENDING";
[22961]3244 case VMSTATE_SUSPENDING_LS: return "SUSPENDING_LS";
[23146]3245 case VMSTATE_SUSPENDING_EXT_LS: return "SUSPENDING_EXT_LS";
[1]3246 case VMSTATE_SAVING: return "SAVING";
[22975]3247 case VMSTATE_DEBUGGING: return "DEBUGGING";
3248 case VMSTATE_DEBUGGING_LS: return "DEBUGGING_LS";
[22961]3249 case VMSTATE_POWERING_OFF: return "POWERING_OFF";
3250 case VMSTATE_POWERING_OFF_LS: return "POWERING_OFF_LS";
[22915]3251 case VMSTATE_FATAL_ERROR: return "FATAL_ERROR";
[22961]3252 case VMSTATE_FATAL_ERROR_LS: return "FATAL_ERROR_LS";
[11473]3253 case VMSTATE_GURU_MEDITATION: return "GURU_MEDITATION";
[22961]3254 case VMSTATE_GURU_MEDITATION_LS:return "GURU_MEDITATION_LS";
[22915]3255 case VMSTATE_LOAD_FAILURE: return "LOAD_FAILURE";
[1]3256 case VMSTATE_OFF: return "OFF";
[24199]3257 case VMSTATE_OFF_LS: return "OFF_LS";
[1]3258 case VMSTATE_DESTROYING: return "DESTROYING";
3259 case VMSTATE_TERMINATED: return "TERMINATED";
[22915]3260
[1]3261 default:
3262 AssertMsgFailed(("Unknown state %d\n", enmState));
3263 return "Unknown!\n";
3264 }
3265}
3266
3267
3268/**
[23009]3269 * Validates the state transition in strict builds.
3270 *
3271 * @returns true if valid, false if not.
3272 *
3273 * @param enmStateOld The old (current) state.
3274 * @param enmStateNew The proposed new state.
[23024]3275 *
3276 * @remarks The reference for this is found in doc/vp/VMM.vpp, the VMSTATE
3277 * diagram (under State Machine Diagram).
[22975]3278 */
3279static bool vmR3ValidateStateTransition(VMSTATE enmStateOld, VMSTATE enmStateNew)
3280{
[62647]3281#ifndef VBOX_STRICT
3282 RT_NOREF2(enmStateOld, enmStateNew);
3283#else
[22975]3284 switch (enmStateOld)
3285 {
3286 case VMSTATE_CREATING:
3287 AssertMsgReturn(enmStateNew == VMSTATE_CREATED, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3288 break;
3289
3290 case VMSTATE_CREATED:
3291 AssertMsgReturn( enmStateNew == VMSTATE_LOADING
3292 || enmStateNew == VMSTATE_POWERING_ON
3293 || enmStateNew == VMSTATE_POWERING_OFF
3294 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3295 break;
3296
3297 case VMSTATE_LOADING:
3298 AssertMsgReturn( enmStateNew == VMSTATE_SUSPENDED
3299 || enmStateNew == VMSTATE_LOAD_FAILURE
3300 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3301 break;
3302
3303 case VMSTATE_POWERING_ON:
3304 AssertMsgReturn( enmStateNew == VMSTATE_RUNNING
[23148]3305 /*|| enmStateNew == VMSTATE_FATAL_ERROR ?*/
[22975]3306 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3307 break;
3308
[23009]3309 case VMSTATE_RESUMING:
3310 AssertMsgReturn( enmStateNew == VMSTATE_RUNNING
[23148]3311 /*|| enmStateNew == VMSTATE_FATAL_ERROR ?*/
[23009]3312 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3313 break;
3314
[22975]3315 case VMSTATE_RUNNING:
3316 AssertMsgReturn( enmStateNew == VMSTATE_POWERING_OFF
3317 || enmStateNew == VMSTATE_SUSPENDING
3318 || enmStateNew == VMSTATE_RESETTING
[60404]3319 || enmStateNew == VMSTATE_SOFT_RESETTING
[22975]3320 || enmStateNew == VMSTATE_RUNNING_LS
[31695]3321 || enmStateNew == VMSTATE_RUNNING_FT
[22975]3322 || enmStateNew == VMSTATE_DEBUGGING
3323 || enmStateNew == VMSTATE_FATAL_ERROR
3324 || enmStateNew == VMSTATE_GURU_MEDITATION
3325 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3326 break;
3327
3328 case VMSTATE_RUNNING_LS:
3329 AssertMsgReturn( enmStateNew == VMSTATE_POWERING_OFF_LS
3330 || enmStateNew == VMSTATE_SUSPENDING_LS
[23146]3331 || enmStateNew == VMSTATE_SUSPENDING_EXT_LS
[22975]3332 || enmStateNew == VMSTATE_RESETTING_LS
[60404]3333 || enmStateNew == VMSTATE_SOFT_RESETTING_LS
[22975]3334 || enmStateNew == VMSTATE_RUNNING
3335 || enmStateNew == VMSTATE_DEBUGGING_LS
3336 || enmStateNew == VMSTATE_FATAL_ERROR_LS
3337 || enmStateNew == VMSTATE_GURU_MEDITATION_LS
3338 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3339 break;
3340
[31695]3341 case VMSTATE_RUNNING_FT:
3342 AssertMsgReturn( enmStateNew == VMSTATE_POWERING_OFF
3343 || enmStateNew == VMSTATE_FATAL_ERROR
3344 || enmStateNew == VMSTATE_GURU_MEDITATION
3345 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3346 break;
3347
[22975]3348 case VMSTATE_RESETTING:
3349 AssertMsgReturn(enmStateNew == VMSTATE_RUNNING, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3350 break;
3351
[60404]3352 case VMSTATE_SOFT_RESETTING:
3353 AssertMsgReturn(enmStateNew == VMSTATE_RUNNING, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3354 break;
3355
[22975]3356 case VMSTATE_RESETTING_LS:
[23146]3357 AssertMsgReturn( enmStateNew == VMSTATE_SUSPENDING_LS
[23037]3358 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3359 break;
3360
[60404]3361 case VMSTATE_SOFT_RESETTING_LS:
3362 AssertMsgReturn( enmStateNew == VMSTATE_RUNNING_LS
3363 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3364 break;
3365
[22975]3366 case VMSTATE_SUSPENDING:
3367 AssertMsgReturn(enmStateNew == VMSTATE_SUSPENDED, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3368 break;
3369
3370 case VMSTATE_SUSPENDING_LS:
[23037]3371 AssertMsgReturn( enmStateNew == VMSTATE_SUSPENDING
3372 || enmStateNew == VMSTATE_SUSPENDED_LS
3373 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
[22975]3374 break;
3375
[23146]3376 case VMSTATE_SUSPENDING_EXT_LS:
3377 AssertMsgReturn( enmStateNew == VMSTATE_SUSPENDING
3378 || enmStateNew == VMSTATE_SUSPENDED_EXT_LS
3379 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3380 break;
3381
[22975]3382 case VMSTATE_SUSPENDED:
3383 AssertMsgReturn( enmStateNew == VMSTATE_POWERING_OFF
3384 || enmStateNew == VMSTATE_SAVING
3385 || enmStateNew == VMSTATE_RESETTING
[60404]3386 || enmStateNew == VMSTATE_SOFT_RESETTING
[23009]3387 || enmStateNew == VMSTATE_RESUMING
3388 || enmStateNew == VMSTATE_LOADING
[22975]3389 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3390 break;
3391
3392 case VMSTATE_SUSPENDED_LS:
[23146]3393 AssertMsgReturn( enmStateNew == VMSTATE_SUSPENDED
3394 || enmStateNew == VMSTATE_SAVING
[22975]3395 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3396 break;
3397
[23146]3398 case VMSTATE_SUSPENDED_EXT_LS:
3399 AssertMsgReturn( enmStateNew == VMSTATE_SUSPENDED
3400 || enmStateNew == VMSTATE_SAVING
3401 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3402 break;
3403
[22975]3404 case VMSTATE_SAVING:
3405 AssertMsgReturn(enmStateNew == VMSTATE_SUSPENDED, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3406 break;
3407
3408 case VMSTATE_DEBUGGING:
[23009]3409 AssertMsgReturn( enmStateNew == VMSTATE_RUNNING
3410 || enmStateNew == VMSTATE_POWERING_OFF
3411 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3412 break;
3413
3414 case VMSTATE_DEBUGGING_LS:
[23037]3415 AssertMsgReturn( enmStateNew == VMSTATE_DEBUGGING
3416 || enmStateNew == VMSTATE_RUNNING_LS
[22975]3417 || enmStateNew == VMSTATE_POWERING_OFF_LS
3418 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3419 break;
3420
3421 case VMSTATE_POWERING_OFF:
3422 AssertMsgReturn(enmStateNew == VMSTATE_OFF, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3423 break;
3424
3425 case VMSTATE_POWERING_OFF_LS:
[23009]3426 AssertMsgReturn( enmStateNew == VMSTATE_POWERING_OFF
3427 || enmStateNew == VMSTATE_OFF_LS
3428 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
[22975]3429 break;
3430
3431 case VMSTATE_OFF:
3432 AssertMsgReturn(enmStateNew == VMSTATE_DESTROYING, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3433 break;
3434
[23009]3435 case VMSTATE_OFF_LS:
3436 AssertMsgReturn(enmStateNew == VMSTATE_OFF, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3437 break;
3438
[22975]3439 case VMSTATE_FATAL_ERROR:
3440 AssertMsgReturn(enmStateNew == VMSTATE_POWERING_OFF, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3441 break;
3442
3443 case VMSTATE_FATAL_ERROR_LS:
[23009]3444 AssertMsgReturn( enmStateNew == VMSTATE_FATAL_ERROR
3445 || enmStateNew == VMSTATE_POWERING_OFF_LS
3446 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
[22975]3447 break;
3448
3449 case VMSTATE_GURU_MEDITATION:
3450 AssertMsgReturn( enmStateNew == VMSTATE_DEBUGGING
3451 || enmStateNew == VMSTATE_POWERING_OFF
3452 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3453 break;
3454
3455 case VMSTATE_GURU_MEDITATION_LS:
[23009]3456 AssertMsgReturn( enmStateNew == VMSTATE_GURU_MEDITATION
[23146]3457 || enmStateNew == VMSTATE_DEBUGGING_LS
[23009]3458 || enmStateNew == VMSTATE_POWERING_OFF_LS
3459 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
[22975]3460 break;
3461
3462 case VMSTATE_LOAD_FAILURE:
3463 AssertMsgReturn(enmStateNew == VMSTATE_POWERING_OFF, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3464 break;
3465
3466 case VMSTATE_DESTROYING:
3467 AssertMsgReturn(enmStateNew == VMSTATE_TERMINATED, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3468 break;
3469
3470 case VMSTATE_TERMINATED:
3471 default:
3472 AssertMsgFailedReturn(("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3473 break;
3474 }
3475#endif /* VBOX_STRICT */
3476 return true;
3477}
3478
3479
3480/**
[23009]3481 * Does the state change callouts.
[1]3482 *
[23009]3483 * The caller owns the AtStateCritSect.
3484 *
[58122]3485 * @param pVM The cross context VM structure.
[23009]3486 * @param pUVM The UVM handle.
3487 * @param enmStateNew The New state.
3488 * @param enmStateOld The old state.
[1]3489 */
[23009]3490static void vmR3DoAtState(PVM pVM, PUVM pUVM, VMSTATE enmStateNew, VMSTATE enmStateOld)
[1]3491{
[51640]3492 LogRel(("Changing the VM state from '%s' to '%s'\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)));
[1]3493
[22980]3494 for (PVMATSTATE pCur = pUVM->vm.s.pAtState; pCur; pCur = pCur->pNext)
[1]3495 {
[44393]3496 pCur->pfnAtState(pUVM, enmStateNew, enmStateOld, pCur->pvUser);
[23009]3497 if ( enmStateNew != VMSTATE_DESTROYING
[18752]3498 && pVM->enmVMState == VMSTATE_DESTROYING)
[1]3499 break;
3500 AssertMsg(pVM->enmVMState == enmStateNew,
3501 ("You are not allowed to change the state while in the change callback, except "
3502 "from destroying the VM. There are restrictions in the way the state changes "
3503 "are propagated up to the EM execution loop and it makes the program flow very "
[23024]3504 "difficult to follow. (%s, expected %s, old %s)\n",
3505 VMR3GetStateName(pVM->enmVMState), VMR3GetStateName(enmStateNew),
3506 VMR3GetStateName(enmStateOld)));
[1]3507 }
[23009]3508}
3509
3510
3511/**
3512 * Sets the current VM state, with the AtStatCritSect already entered.
3513 *
[61643]3514 * @param pVM The cross context VM structure.
3515 * @param pUVM The UVM handle.
3516 * @param enmStateNew The new state.
3517 * @param enmStateOld The old state.
3518 * @param fSetRatherThanClearFF The usual behavior is to clear the
3519 * VM_FF_CHECK_VM_STATE force flag, but for
3520 * some transitions (-> guru) we need to kick
3521 * the other EMTs to stop what they're doing.
[23009]3522 */
[61643]3523static void vmR3SetStateLocked(PVM pVM, PUVM pUVM, VMSTATE enmStateNew, VMSTATE enmStateOld, bool fSetRatherThanClearFF)
[23009]3524{
3525 vmR3ValidateStateTransition(enmStateOld, enmStateNew);
3526
[23024]3527 AssertMsg(pVM->enmVMState == enmStateOld,
3528 ("%s != %s\n", VMR3GetStateName(pVM->enmVMState), VMR3GetStateName(enmStateOld)));
[61643]3529
[23009]3530 pUVM->vm.s.enmPrevVMState = enmStateOld;
3531 pVM->enmVMState = enmStateNew;
3532
[61643]3533 if (!fSetRatherThanClearFF)
3534 VM_FF_CLEAR(pVM, VM_FF_CHECK_VM_STATE);
3535 else if (pVM->cCpus > 0)
3536 VM_FF_SET(pVM, VM_FF_CHECK_VM_STATE);
3537
[23009]3538 vmR3DoAtState(pVM, pUVM, enmStateNew, enmStateOld);
3539}
3540
3541
3542/**
[23024]3543 * Sets the current VM state.
3544 *
[58122]3545 * @param pVM The cross context VM structure.
[23024]3546 * @param enmStateNew The new state.
3547 * @param enmStateOld The old state (for asserting only).
3548 */
3549static void vmR3SetState(PVM pVM, VMSTATE enmStateNew, VMSTATE enmStateOld)
3550{
3551 PUVM pUVM = pVM->pUVM;
3552 RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
3553
[62647]3554 RT_NOREF_PV(enmStateOld);
[23024]3555 AssertMsg(pVM->enmVMState == enmStateOld,
3556 ("%s != %s\n", VMR3GetStateName(pVM->enmVMState), VMR3GetStateName(enmStateOld)));
[61643]3557 vmR3SetStateLocked(pVM, pUVM, enmStateNew, pVM->enmVMState, false /*fSetRatherThanClearFF*/);
[23024]3558
3559 RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
3560}
3561
3562
3563/**
[23018]3564 * Tries to perform a state transition.
[23009]3565 *
[23018]3566 * @returns The 1-based ordinal of the succeeding transition.
3567 * VERR_VM_INVALID_VM_STATE and Assert+LogRel on failure.
3568 *
[58122]3569 * @param pVM The cross context VM structure.
[23018]3570 * @param pszWho Who is trying to change it.
3571 * @param cTransitions The number of transitions in the ellipsis.
3572 * @param ... Transition pairs; new, old.
[23009]3573 */
[23023]3574static int vmR3TrySetState(PVM pVM, const char *pszWho, unsigned cTransitions, ...)
[23009]3575{
[23018]3576 va_list va;
3577 VMSTATE enmStateNew = VMSTATE_CREATED;
3578 VMSTATE enmStateOld = VMSTATE_CREATED;
[23009]3579
[23018]3580#ifdef VBOX_STRICT
3581 /*
3582 * Validate the input first.
3583 */
3584 va_start(va, cTransitions);
3585 for (unsigned i = 0; i < cTransitions; i++)
[23009]3586 {
[23018]3587 enmStateNew = (VMSTATE)va_arg(va, /*VMSTATE*/int);
3588 enmStateOld = (VMSTATE)va_arg(va, /*VMSTATE*/int);
3589 vmR3ValidateStateTransition(enmStateOld, enmStateNew);
[23009]3590 }
[23018]3591 va_end(va);
3592#endif
[1]3593
[23018]3594 /*
[33540]3595 * Grab the lock and see if any of the proposed transitions works out.
[23018]3596 */
3597 va_start(va, cTransitions);
3598 int rc = VERR_VM_INVALID_VM_STATE;
3599 PUVM pUVM = pVM->pUVM;
3600 RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
[1]3601
[23018]3602 VMSTATE enmStateCur = pVM->enmVMState;
[23009]3603
[23018]3604 for (unsigned i = 0; i < cTransitions; i++)
[23009]3605 {
[23018]3606 enmStateNew = (VMSTATE)va_arg(va, /*VMSTATE*/int);
3607 enmStateOld = (VMSTATE)va_arg(va, /*VMSTATE*/int);
3608 if (enmStateCur == enmStateOld)
3609 {
[61643]3610 vmR3SetStateLocked(pVM, pUVM, enmStateNew, enmStateOld, false /*fSetRatherThanClearFF*/);
[23018]3611 rc = i + 1;
3612 break;
3613 }
[23009]3614 }
[23018]3615
3616 if (RT_FAILURE(rc))
[23009]3617 {
[23024]3618 /*
3619 * Complain about it.
3620 */
[23018]3621 if (cTransitions == 1)
[27789]3622 {
[23018]3623 LogRel(("%s: %s -> %s failed, because the VM state is actually %s\n",
3624 pszWho, VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew), VMR3GetStateName(enmStateCur)));
[27789]3625 VMSetError(pVM, VERR_VM_INVALID_VM_STATE, RT_SRC_POS,
3626 N_("%s failed because the VM state is %s instead of %s"),
3627 pszWho, VMR3GetStateName(enmStateCur), VMR3GetStateName(enmStateOld));
3628 AssertMsgFailed(("%s: %s -> %s failed, because the VM state is actually %s\n",
3629 pszWho, VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew), VMR3GetStateName(enmStateCur)));
3630 }
[23018]3631 else
3632 {
3633 va_end(va);
3634 va_start(va, cTransitions);
3635 LogRel(("%s:\n", pszWho));
3636 for (unsigned i = 0; i < cTransitions; i++)
3637 {
3638 enmStateNew = (VMSTATE)va_arg(va, /*VMSTATE*/int);
3639 enmStateOld = (VMSTATE)va_arg(va, /*VMSTATE*/int);
3640 LogRel(("%s%s -> %s",
3641 i ? ", " : " ", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)));
3642 }
3643 LogRel((" failed, because the VM state is actually %s\n", VMR3GetStateName(enmStateCur)));
[27789]3644 VMSetError(pVM, VERR_VM_INVALID_VM_STATE, RT_SRC_POS,
[57006]3645 N_("%s failed because the current VM state, %s, was not found in the state transition table (old state %s)"),
[27789]3646 pszWho, VMR3GetStateName(enmStateCur), VMR3GetStateName(enmStateOld));
3647 AssertMsgFailed(("%s - state=%s, see release log for full details. Check the cTransitions passed us.\n",
3648 pszWho, VMR3GetStateName(enmStateCur)));
[23018]3649 }
[23009]3650 }
3651
3652 RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
[23018]3653 va_end(va);
3654 Assert(rc > 0 || rc < 0);
[23009]3655 return rc;
3656}
3657
3658
3659/**
[61643]3660 * Interface used by EM to signal that it's entering the guru meditation state.
[23009]3661 *
[61643]3662 * This will notifying other threads.
3663 *
3664 * @returns true if the state changed to Guru, false if no state change.
[58122]3665 * @param pVM The cross context VM structure.
[23009]3666 */
[61643]3667VMMR3_INT_DECL(bool) VMR3SetGuruMeditation(PVM pVM)
[23009]3668{
[23018]3669 PUVM pUVM = pVM->pUVM;
3670 RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
3671
3672 VMSTATE enmStateCur = pVM->enmVMState;
[61643]3673 bool fRc = true;
[23018]3674 if (enmStateCur == VMSTATE_RUNNING)
[61643]3675 vmR3SetStateLocked(pVM, pUVM, VMSTATE_GURU_MEDITATION, VMSTATE_RUNNING, true /*fSetRatherThanClearFF*/);
[23018]3676 else if (enmStateCur == VMSTATE_RUNNING_LS)
3677 {
[61643]3678 vmR3SetStateLocked(pVM, pUVM, VMSTATE_GURU_MEDITATION_LS, VMSTATE_RUNNING_LS, true /*fSetRatherThanClearFF*/);
[44347]3679 SSMR3Cancel(pUVM);
[23018]3680 }
[61643]3681 else
3682 fRc = false;
[23018]3683
3684 RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
[61643]3685 return fRc;
[23009]3686}
3687
3688
3689/**
[30473]3690 * Called by vmR3EmulationThreadWithId just before the VM structure is freed.
3691 *
[58122]3692 * @param pVM The cross context VM structure.
[30473]3693 */
3694void vmR3SetTerminated(PVM pVM)
3695{
3696 vmR3SetState(pVM, VMSTATE_TERMINATED, VMSTATE_DESTROYING);
3697}
3698
3699
3700/**
[23915]3701 * Checks if the VM was teleported and hasn't been fully resumed yet.
3702 *
[23917]3703 * This applies to both sides of the teleportation since we may leave a working
3704 * clone behind and the user is allowed to resume this...
3705 *
[23915]3706 * @returns true / false.
[58122]3707 * @param pVM The cross context VM structure.
[23915]3708 * @thread Any thread.
3709 */
[44340]3710VMMR3_INT_DECL(bool) VMR3TeleportedAndNotFullyResumedYet(PVM pVM)
[23915]3711{
3712 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
3713 return pVM->vm.s.fTeleportedAndNotFullyResumedYet;
3714}
3715
3716
3717/**
[1]3718 * Registers a VM state change callback.
3719 *
3720 * You are not allowed to call any function which changes the VM state from a
[30473]3721 * state callback.
[1]3722 *
3723 * @returns VBox status code.
[44340]3724 * @param pUVM The VM handle.
[1]3725 * @param pfnAtState Pointer to callback.
3726 * @param pvUser User argument.
3727 * @thread Any.
3728 */
[44340]3729VMMR3DECL(int) VMR3AtStateRegister(PUVM pUVM, PFNVMATSTATE pfnAtState, void *pvUser)
[1]3730{
3731 LogFlow(("VMR3AtStateRegister: pfnAtState=%p pvUser=%p\n", pfnAtState, pvUser));
3732
3733 /*
3734 * Validate input.
3735 */
[22986]3736 AssertPtrReturn(pfnAtState, VERR_INVALID_PARAMETER);
[44340]3737 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
[1]3738
3739 /*
3740 * Allocate a new record.
3741 */
[6796]3742 PVMATSTATE pNew = (PVMATSTATE)MMR3HeapAllocU(pUVM, MM_TAG_VM, sizeof(*pNew));
[1]3743 if (!pNew)
3744 return VERR_NO_MEMORY;
3745
3746 /* fill */
3747 pNew->pfnAtState = pfnAtState;
3748 pNew->pvUser = pvUser;
3749
3750 /* insert */
[22980]3751 RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
[15349]3752 pNew->pNext = *pUVM->vm.s.ppAtStateNext;
[6796]3753 *pUVM->vm.s.ppAtStateNext = pNew;
3754 pUVM->vm.s.ppAtStateNext = &pNew->pNext;
[22980]3755 RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
[1]3756
3757 return VINF_SUCCESS;
3758}
3759
3760
3761/**
3762 * Deregisters a VM state change callback.
3763 *
3764 * @returns VBox status code.
[44340]3765 * @param pUVM The VM handle.
[1]3766 * @param pfnAtState Pointer to callback.
3767 * @param pvUser User argument.
3768 * @thread Any.
3769 */
[44340]3770VMMR3DECL(int) VMR3AtStateDeregister(PUVM pUVM, PFNVMATSTATE pfnAtState, void *pvUser)
[1]3771{
3772 LogFlow(("VMR3AtStateDeregister: pfnAtState=%p pvUser=%p\n", pfnAtState, pvUser));
3773
3774 /*
3775 * Validate input.
3776 */
[22986]3777 AssertPtrReturn(pfnAtState, VERR_INVALID_PARAMETER);
[44340]3778 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
[1]3779
[22980]3780 RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
[1]3781
3782 /*
3783 * Search the list for the entry.
3784 */
3785 PVMATSTATE pPrev = NULL;
[6796]3786 PVMATSTATE pCur = pUVM->vm.s.pAtState;
[1]3787 while ( pCur
[17900]3788 && ( pCur->pfnAtState != pfnAtState
3789 || pCur->pvUser != pvUser))
[1]3790 {
3791 pPrev = pCur;
3792 pCur = pCur->pNext;
3793 }
3794 if (!pCur)
3795 {
3796 AssertMsgFailed(("pfnAtState=%p was not found\n", pfnAtState));
[22980]3797 RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
[1]3798 return VERR_FILE_NOT_FOUND;
3799 }
3800
3801 /*
3802 * Unlink it.
3803 */
3804 if (pPrev)
3805 {
3806 pPrev->pNext = pCur->pNext;
3807 if (!pCur->pNext)
[6796]3808 pUVM->vm.s.ppAtStateNext = &pPrev->pNext;
[1]3809 }
3810 else
3811 {
[6796]3812 pUVM->vm.s.pAtState = pCur->pNext;
[1]3813 if (!pCur->pNext)
[6796]3814 pUVM->vm.s.ppAtStateNext = &pUVM->vm.s.pAtState;
[1]3815 }
3816
[22980]3817 RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
3818
[1]3819 /*
3820 * Free it.
3821 */
3822 pCur->pfnAtState = NULL;
3823 pCur->pNext = NULL;
3824 MMR3HeapFree(pCur);
3825
3826 return VINF_SUCCESS;
3827}
3828
3829
3830/**
3831 * Registers a VM error callback.
3832 *
3833 * @returns VBox status code.
[44340]3834 * @param pUVM The VM handle.
[1]3835 * @param pfnAtError Pointer to callback.
3836 * @param pvUser User argument.
3837 * @thread Any.
3838 */
[44340]3839VMMR3DECL(int) VMR3AtErrorRegister(PUVM pUVM, PFNVMATERROR pfnAtError, void *pvUser)
[1]3840{
3841 LogFlow(("VMR3AtErrorRegister: pfnAtError=%p pvUser=%p\n", pfnAtError, pvUser));
3842
3843 /*
[22988]3844 * Validate input.
[1]3845 */
[22988]3846 AssertPtrReturn(pfnAtError, VERR_INVALID_PARAMETER);
3847 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
[1]3848
3849 /*
3850 * Allocate a new record.
3851 */
[6796]3852 PVMATERROR pNew = (PVMATERROR)MMR3HeapAllocU(pUVM, MM_TAG_VM, sizeof(*pNew));
[1]3853 if (!pNew)
3854 return VERR_NO_MEMORY;
3855
3856 /* fill */
3857 pNew->pfnAtError = pfnAtError;
3858 pNew->pvUser = pvUser;
3859
3860 /* insert */
[22980]3861 RTCritSectEnter(&pUVM->vm.s.AtErrorCritSect);
[15349]3862 pNew->pNext = *pUVM->vm.s.ppAtErrorNext;
[6796]3863 *pUVM->vm.s.ppAtErrorNext = pNew;
3864 pUVM->vm.s.ppAtErrorNext = &pNew->pNext;
[22980]3865 RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
[1]3866
3867 return VINF_SUCCESS;
3868}
3869
3870
3871/**
3872 * Deregisters a VM error callback.
3873 *
3874 * @returns VBox status code.
[44340]3875 * @param pUVM The VM handle.
[1]3876 * @param pfnAtError Pointer to callback.
3877 * @param pvUser User argument.
3878 * @thread Any.
3879 */
[44340]3880VMMR3DECL(int) VMR3AtErrorDeregister(PUVM pUVM, PFNVMATERROR pfnAtError, void *pvUser)
[1]3881{
3882 LogFlow(("VMR3AtErrorDeregister: pfnAtError=%p pvUser=%p\n", pfnAtError, pvUser));
3883
3884 /*
3885 * Validate input.
3886 */
[22988]3887 AssertPtrReturn(pfnAtError, VERR_INVALID_PARAMETER);
[44340]3888 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
[1]3889
[22980]3890 RTCritSectEnter(&pUVM->vm.s.AtErrorCritSect);
[1]3891
3892 /*
3893 * Search the list for the entry.
3894 */
3895 PVMATERROR pPrev = NULL;
[6796]3896 PVMATERROR pCur = pUVM->vm.s.pAtError;
[1]3897 while ( pCur
[17900]3898 && ( pCur->pfnAtError != pfnAtError
3899 || pCur->pvUser != pvUser))
[1]3900 {
3901 pPrev = pCur;
3902 pCur = pCur->pNext;
3903 }
3904 if (!pCur)
3905 {
3906 AssertMsgFailed(("pfnAtError=%p was not found\n", pfnAtError));
[22980]3907 RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
[1]3908 return VERR_FILE_NOT_FOUND;
3909 }
3910
3911 /*
3912 * Unlink it.
3913 */
3914 if (pPrev)
3915 {
3916 pPrev->pNext = pCur->pNext;
3917 if (!pCur->pNext)
[6796]3918 pUVM->vm.s.ppAtErrorNext = &pPrev->pNext;
[1]3919 }
3920 else
3921 {
[6796]3922 pUVM->vm.s.pAtError = pCur->pNext;
[1]3923 if (!pCur->pNext)
[6796]3924 pUVM->vm.s.ppAtErrorNext = &pUVM->vm.s.pAtError;
[1]3925 }
3926
[22980]3927 RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
3928
[1]3929 /*
3930 * Free it.
3931 */
3932 pCur->pfnAtError = NULL;
3933 pCur->pNext = NULL;
3934 MMR3HeapFree(pCur);
3935
3936 return VINF_SUCCESS;
3937}
3938
3939
3940/**
3941 * Ellipsis to va_list wrapper for calling pfnAtError.
3942 */
3943static void vmR3SetErrorWorkerDoCall(PVM pVM, PVMATERROR pCur, int rc, RT_SRC_POS_DECL, const char *pszFormat, ...)
3944{
3945 va_list va;
3946 va_start(va, pszFormat);
[44393]3947 pCur->pfnAtError(pVM->pUVM, pCur->pvUser, rc, RT_SRC_POS_ARGS, pszFormat, va);
[1]3948 va_end(va);
3949}
3950
3951
3952/**
3953 * This is a worker function for GC and Ring-0 calls to VMSetError and VMSetErrorV.
3954 * The message is found in VMINT.
3955 *
[58122]3956 * @param pVM The cross context VM structure.
[1]3957 * @thread EMT.
3958 */
[44340]3959VMMR3_INT_DECL(void) VMR3SetErrorWorker(PVM pVM)
[1]3960{
3961 VM_ASSERT_EMT(pVM);
[41532]3962 AssertReleaseMsgFailed(("And we have a winner! You get to implement Ring-0 and GC VMSetErrorV! Congrats!\n"));
[1]3963
3964 /*
3965 * Unpack the error (if we managed to format one).
3966 */
3967 PVMERROR pErr = pVM->vm.s.pErrorR3;
3968 const char *pszFile = NULL;
3969 const char *pszFunction = NULL;
3970 uint32_t iLine = 0;
3971 const char *pszMessage;
3972 int32_t rc = VERR_MM_HYPER_NO_MEMORY;
3973 if (pErr)
3974 {
3975 AssertCompile(sizeof(const char) == sizeof(uint8_t));
3976 if (pErr->offFile)
3977 pszFile = (const char *)pErr + pErr->offFile;
[234]3978 iLine = pErr->iLine;
[1]3979 if (pErr->offFunction)
3980 pszFunction = (const char *)pErr + pErr->offFunction;
3981 if (pErr->offMessage)
3982 pszMessage = (const char *)pErr + pErr->offMessage;
3983 else
3984 pszMessage = "No message!";
3985 }
3986 else
3987 pszMessage = "No message! (Failed to allocate memory to put the error message in!)";
3988
3989 /*
3990 * Call the at error callbacks.
3991 */
[22980]3992 PUVM pUVM = pVM->pUVM;
3993 RTCritSectEnter(&pUVM->vm.s.AtErrorCritSect);
[24508]3994 ASMAtomicIncU32(&pUVM->vm.s.cRuntimeErrors);
[22980]3995 for (PVMATERROR pCur = pUVM->vm.s.pAtError; pCur; pCur = pCur->pNext)
[1]3996 vmR3SetErrorWorkerDoCall(pVM, pCur, rc, RT_SRC_POS_ARGS, "%s", pszMessage);
[22980]3997 RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
[1]3998}
3999
4000
4001/**
[24508]4002 * Gets the number of errors raised via VMSetError.
4003 *
4004 * This can be used avoid double error messages.
4005 *
4006 * @returns The error count.
[44340]4007 * @param pUVM The VM handle.
[24508]4008 */
[44340]4009VMMR3_INT_DECL(uint32_t) VMR3GetErrorCount(PUVM pUVM)
[24508]4010{
[35810]4011 AssertPtrReturn(pUVM, 0);
4012 AssertReturn(pUVM->u32Magic == UVM_MAGIC, 0);
4013 return pUVM->vm.s.cErrors;
4014}
4015
4016
4017/**
[6796]4018 * Creation time wrapper for vmR3SetErrorUV.
4019 *
4020 * @returns rc.
4021 * @param pUVM Pointer to the user mode VM structure.
4022 * @param rc The VBox status code.
[58116]4023 * @param SRC_POS The source position of this error.
[6796]4024 * @param pszFormat Format string.
4025 * @param ... The arguments.
4026 * @thread Any thread.
4027 */
[61122]4028static int vmR3SetErrorU(PUVM pUVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, ...)
[6796]4029{
4030 va_list va;
4031 va_start(va, pszFormat);
4032 vmR3SetErrorUV(pUVM, rc, pszFile, iLine, pszFunction, pszFormat, &va);
4033 va_end(va);
4034 return rc;
4035}
4036
4037
4038/**
[1]4039 * Worker which calls everyone listening to the VM error messages.
4040 *
[6796]4041 * @param pUVM Pointer to the user mode VM structure.
[1]4042 * @param rc The VBox status code.
[58116]4043 * @param SRC_POS The source position of this error.
[1]4044 * @param pszFormat Format string.
4045 * @param pArgs Pointer to the format arguments.
4046 * @thread EMT
4047 */
[6796]4048DECLCALLBACK(void) vmR3SetErrorUV(PUVM pUVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list *pArgs)
[1]4049{
4050 /*
[2283]4051 * Log the error.
4052 */
4053 va_list va3;
4054 va_copy(va3, *pArgs);
[26365]4055 RTLogRelPrintf("VMSetError: %s(%d) %s; rc=%Rrc\n"
4056 "VMSetError: %N\n",
4057 pszFile, iLine, pszFunction, rc,
4058 pszFormat, &va3);
[24567]4059 va_end(va3);
4060
4061#ifdef LOG_ENABLED
4062 va_copy(va3, *pArgs);
[26365]4063 RTLogPrintf("VMSetError: %s(%d) %s; rc=%Rrc\n"
4064 "%N\n",
4065 pszFile, iLine, pszFunction, rc,
4066 pszFormat, &va3);
[2283]4067 va_end(va3);
[2858]4068#endif
[2283]4069
4070 /*
[1]4071 * Make a copy of the message.
4072 */
[6796]4073 if (pUVM->pVM)
4074 vmSetErrorCopy(pUVM->pVM, rc, RT_SRC_POS_ARGS, pszFormat, *pArgs);
[1]4075
4076 /*
4077 * Call the at error callbacks.
4078 */
[24267]4079 bool fCalledSomeone = false;
[22980]4080 RTCritSectEnter(&pUVM->vm.s.AtErrorCritSect);
[24508]4081 ASMAtomicIncU32(&pUVM->vm.s.cErrors);
[6796]4082 for (PVMATERROR pCur = pUVM->vm.s.pAtError; pCur; pCur = pCur->pNext)
[1]4083 {
4084 va_list va2;
4085 va_copy(va2, *pArgs);
[44393]4086 pCur->pfnAtError(pUVM, pCur->pvUser, rc, RT_SRC_POS_ARGS, pszFormat, va2);
[1]4087 va_end(va2);
[24267]4088 fCalledSomeone = true;
[1]4089 }
[22980]4090 RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
[1]4091}
4092
[234]4093
4094/**
[44399]4095 * Sets the error message.
4096 *
4097 * @returns rc. Meaning you can do:
4098 * @code
4099 * return VM_SET_ERROR_U(pUVM, VERR_OF_YOUR_CHOICE, "descriptive message");
4100 * @endcode
4101 * @param pUVM The user mode VM handle.
4102 * @param rc VBox status code.
[58116]4103 * @param SRC_POS Use RT_SRC_POS.
[44399]4104 * @param pszFormat Error message format string.
4105 * @param ... Error message arguments.
4106 * @thread Any
4107 */
4108VMMR3DECL(int) VMR3SetError(PUVM pUVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, ...)
4109{
4110 va_list va;
4111 va_start(va, pszFormat);
4112 int rcRet = VMR3SetErrorV(pUVM, rc, pszFile, iLine, pszFunction, pszFormat, va);
4113 va_end(va);
4114 return rcRet;
4115}
4116
4117
4118/**
4119 * Sets the error message.
4120 *
4121 * @returns rc. Meaning you can do:
4122 * @code
4123 * return VM_SET_ERROR_U(pUVM, VERR_OF_YOUR_CHOICE, "descriptive message");
4124 * @endcode
4125 * @param pUVM The user mode VM handle.
4126 * @param rc VBox status code.
[58116]4127 * @param SRC_POS Use RT_SRC_POS.
[44399]4128 * @param pszFormat Error message format string.
4129 * @param va Error message arguments.
4130 * @thread Any
4131 */
4132VMMR3DECL(int) VMR3SetErrorV(PUVM pUVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
4133{
4134 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
[57376]4135
4136 /* Take shortcut when called on EMT, skipping VM handle requirement + validation. */
4137 if (VMR3GetVMCPUThread(pUVM) != NIL_RTTHREAD)
4138 {
[57380]4139 va_list vaCopy;
[57381]4140 va_copy(vaCopy, va);
[57380]4141 vmR3SetErrorUV(pUVM, rc, RT_SRC_POS_ARGS, pszFormat, &vaCopy);
4142 va_end(vaCopy);
[57376]4143 return rc;
4144 }
4145
[44399]4146 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
4147 return VMSetErrorV(pUVM->pVM, rc, pszFile, iLine, pszFunction, pszFormat, va);
4148}
4149
4150
4151
4152/**
[234]4153 * Registers a VM runtime error callback.
4154 *
4155 * @returns VBox status code.
[58126]4156 * @param pUVM The user mode VM structure.
[234]4157 * @param pfnAtRuntimeError Pointer to callback.
4158 * @param pvUser User argument.
4159 * @thread Any.
4160 */
[44340]4161VMMR3DECL(int) VMR3AtRuntimeErrorRegister(PUVM pUVM, PFNVMATRUNTIMEERROR pfnAtRuntimeError, void *pvUser)
[234]4162{
4163 LogFlow(("VMR3AtRuntimeErrorRegister: pfnAtRuntimeError=%p pvUser=%p\n", pfnAtRuntimeError, pvUser));
4164
4165 /*
4166 * Validate input.
4167 */
[22988]4168 AssertPtrReturn(pfnAtRuntimeError, VERR_INVALID_PARAMETER);
[44340]4169 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
[234]4170
4171 /*
4172 * Allocate a new record.
4173 */
[6796]4174 PVMATRUNTIMEERROR pNew = (PVMATRUNTIMEERROR)MMR3HeapAllocU(pUVM, MM_TAG_VM, sizeof(*pNew));
[234]4175 if (!pNew)
4176 return VERR_NO_MEMORY;
4177
4178 /* fill */
4179 pNew->pfnAtRuntimeError = pfnAtRuntimeError;
4180 pNew->pvUser = pvUser;
4181
4182 /* insert */
[22980]4183 RTCritSectEnter(&pUVM->vm.s.AtErrorCritSect);
[15349]4184 pNew->pNext = *pUVM->vm.s.ppAtRuntimeErrorNext;
[6796]4185 *pUVM->vm.s.ppAtRuntimeErrorNext = pNew;
4186 pUVM->vm.s.ppAtRuntimeErrorNext = &pNew->pNext;
[22980]4187 RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
[234]4188
4189 return VINF_SUCCESS;
4190}
4191
4192
4193/**
4194 * Deregisters a VM runtime error callback.
4195 *
4196 * @returns VBox status code.
[44340]4197 * @param pUVM The user mode VM handle.
[234]4198 * @param pfnAtRuntimeError Pointer to callback.
4199 * @param pvUser User argument.
4200 * @thread Any.
4201 */
[44340]4202VMMR3DECL(int) VMR3AtRuntimeErrorDeregister(PUVM pUVM, PFNVMATRUNTIMEERROR pfnAtRuntimeError, void *pvUser)
[234]4203{
4204 LogFlow(("VMR3AtRuntimeErrorDeregister: pfnAtRuntimeError=%p pvUser=%p\n", pfnAtRuntimeError, pvUser));
4205
4206 /*
4207 * Validate input.
4208 */
[22988]4209 AssertPtrReturn(pfnAtRuntimeError, VERR_INVALID_PARAMETER);
[44340]4210 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
[234]4211
[22980]4212 RTCritSectEnter(&pUVM->vm.s.AtErrorCritSect);
[234]4213
4214 /*
4215 * Search the list for the entry.
4216 */
4217 PVMATRUNTIMEERROR pPrev = NULL;
[6796]4218 PVMATRUNTIMEERROR pCur = pUVM->vm.s.pAtRuntimeError;
[234]4219 while ( pCur
[17900]4220 && ( pCur->pfnAtRuntimeError != pfnAtRuntimeError
4221 || pCur->pvUser != pvUser))
[234]4222 {
4223 pPrev = pCur;
4224 pCur = pCur->pNext;
4225 }
4226 if (!pCur)
4227 {
4228 AssertMsgFailed(("pfnAtRuntimeError=%p was not found\n", pfnAtRuntimeError));
[22980]4229 RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
[234]4230 return VERR_FILE_NOT_FOUND;
4231 }
4232
4233 /*
4234 * Unlink it.
4235 */
4236 if (pPrev)
4237 {
4238 pPrev->pNext = pCur->pNext;
4239 if (!pCur->pNext)
[6796]4240 pUVM->vm.s.ppAtRuntimeErrorNext = &pPrev->pNext;
[234]4241 }
4242 else
4243 {
[6796]4244 pUVM->vm.s.pAtRuntimeError = pCur->pNext;
[234]4245 if (!pCur->pNext)
[6796]4246 pUVM->vm.s.ppAtRuntimeErrorNext = &pUVM->vm.s.pAtRuntimeError;
[234]4247 }
4248
[22980]4249 RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
4250
[234]4251 /*
4252 * Free it.
4253 */
4254 pCur->pfnAtRuntimeError = NULL;
4255 pCur->pNext = NULL;
4256 MMR3HeapFree(pCur);
4257
4258 return VINF_SUCCESS;
4259}
4260
4261
4262/**
[23146]4263 * EMT rendezvous worker that vmR3SetRuntimeErrorCommon uses to safely change
4264 * the state to FatalError(LS).
4265 *
[33540]4266 * @returns VERR_VM_INVALID_VM_STATE or VINF_EM_SUSPEND. (This is a strict
[30473]4267 * return code, see FNVMMEMTRENDEZVOUS.)
[23146]4268 *
[58122]4269 * @param pVM The cross context VM structure.
[58123]4270 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
[23148]4271 * @param pvUser Ignored.
[23146]4272 */
4273static DECLCALLBACK(VBOXSTRICTRC) vmR3SetRuntimeErrorChangeState(PVM pVM, PVMCPU pVCpu, void *pvUser)
4274{
4275 NOREF(pVCpu);
[23148]4276 Assert(!pvUser); NOREF(pvUser);
4277
[30473]4278 /*
4279 * The first EMT thru here changes the state.
4280 */
4281 if (pVCpu->idCpu == pVM->cCpus - 1)
4282 {
4283 int rc = vmR3TrySetState(pVM, "VMSetRuntimeError", 2,
4284 VMSTATE_FATAL_ERROR, VMSTATE_RUNNING,
4285 VMSTATE_FATAL_ERROR_LS, VMSTATE_RUNNING_LS);
4286 if (RT_FAILURE(rc))
4287 return rc;
4288 if (rc == 2)
[44347]4289 SSMR3Cancel(pVM->pUVM);
[30473]4290
4291 VM_FF_SET(pVM, VM_FF_CHECK_VM_STATE);
4292 }
4293
4294 /* This'll make sure we get out of whereever we are (e.g. REM). */
4295 return VINF_EM_SUSPEND;
[23146]4296}
4297
4298
4299/**
[18645]4300 * Worker for VMR3SetRuntimeErrorWorker and vmR3SetRuntimeErrorV.
4301 *
4302 * This does the common parts after the error has been saved / retrieved.
4303 *
4304 * @returns VBox status code with modifications, see VMSetRuntimeErrorV.
4305 *
[58122]4306 * @param pVM The cross context VM structure.
[18645]4307 * @param fFlags The error flags.
4308 * @param pszErrorId Error ID string.
4309 * @param pszFormat Format string.
4310 * @param pVa Pointer to the format arguments.
[234]4311 */
[18645]4312static int vmR3SetRuntimeErrorCommon(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list *pVa)
[234]4313{
[18645]4314 LogRel(("VM: Raising runtime error '%s' (fFlags=%#x)\n", pszErrorId, fFlags));
[44393]4315 PUVM pUVM = pVM->pUVM;
[18645]4316
4317 /*
4318 * Take actions before the call.
4319 */
[23146]4320 int rc;
[18645]4321 if (fFlags & VMSETRTERR_FLAGS_FATAL)
[30473]4322 rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING | VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR,
4323 vmR3SetRuntimeErrorChangeState, NULL);
[18645]4324 else if (fFlags & VMSETRTERR_FLAGS_SUSPEND)
[46788]4325 rc = VMR3Suspend(pUVM, VMSUSPENDREASON_RUNTIME_ERROR);
[23146]4326 else
4327 rc = VINF_SUCCESS;
[18645]4328
4329 /*
4330 * Do the callback round.
4331 */
[22980]4332 RTCritSectEnter(&pUVM->vm.s.AtErrorCritSect);
[24508]4333 ASMAtomicIncU32(&pUVM->vm.s.cRuntimeErrors);
[22980]4334 for (PVMATRUNTIMEERROR pCur = pUVM->vm.s.pAtRuntimeError; pCur; pCur = pCur->pNext)
[18645]4335 {
4336 va_list va;
4337 va_copy(va, *pVa);
[44393]4338 pCur->pfnAtRuntimeError(pUVM, pCur->pvUser, fFlags, pszErrorId, pszFormat, va);
[18645]4339 va_end(va);
4340 }
[22980]4341 RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
[18645]4342
4343 return rc;
4344}
4345
4346
4347/**
4348 * Ellipsis to va_list wrapper for calling vmR3SetRuntimeErrorCommon.
4349 */
4350static int vmR3SetRuntimeErrorCommonF(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, ...)
4351{
[234]4352 va_list va;
4353 va_start(va, pszFormat);
[18645]4354 int rc = vmR3SetRuntimeErrorCommon(pVM, fFlags, pszErrorId, pszFormat, &va);
[234]4355 va_end(va);
[18645]4356 return rc;
[234]4357}
4358
4359
4360/**
[18645]4361 * This is a worker function for RC and Ring-0 calls to VMSetError and
4362 * VMSetErrorV.
4363 *
[234]4364 * The message is found in VMINT.
4365 *
[18645]4366 * @returns VBox status code, see VMSetRuntimeError.
[58122]4367 * @param pVM The cross context VM structure.
[234]4368 * @thread EMT.
4369 */
[44340]4370VMMR3_INT_DECL(int) VMR3SetRuntimeErrorWorker(PVM pVM)
[234]4371{
4372 VM_ASSERT_EMT(pVM);
[16584]4373 AssertReleaseMsgFailed(("And we have a winner! You get to implement Ring-0 and GC VMSetRuntimeErrorV! Congrats!\n"));
[234]4374
4375 /*
4376 * Unpack the error (if we managed to format one).
4377 */
[18645]4378 const char *pszErrorId = "SetRuntimeError";
4379 const char *pszMessage = "No message!";
4380 uint32_t fFlags = VMSETRTERR_FLAGS_FATAL;
4381 PVMRUNTIMEERROR pErr = pVM->vm.s.pRuntimeErrorR3;
[234]4382 if (pErr)
4383 {
4384 AssertCompile(sizeof(const char) == sizeof(uint8_t));
[18645]4385 if (pErr->offErrorId)
4386 pszErrorId = (const char *)pErr + pErr->offErrorId;
[234]4387 if (pErr->offMessage)
4388 pszMessage = (const char *)pErr + pErr->offMessage;
[18645]4389 fFlags = pErr->fFlags;
[234]4390 }
4391
4392 /*
[18645]4393 * Join cause with vmR3SetRuntimeErrorV.
[234]4394 */
[18645]4395 return vmR3SetRuntimeErrorCommonF(pVM, fFlags, pszErrorId, "%s", pszMessage);
[234]4396}
4397
4398
4399/**
[18645]4400 * Worker for VMSetRuntimeErrorV for doing the job on EMT in ring-3.
[234]4401 *
[18645]4402 * @returns VBox status code with modifications, see VMSetRuntimeErrorV.
4403 *
[58122]4404 * @param pVM The cross context VM structure.
[18645]4405 * @param fFlags The error flags.
4406 * @param pszErrorId Error ID string.
[22915]4407 * @param pszMessage The error message residing the MM heap.
4408 *
4409 * @thread EMT
4410 */
4411DECLCALLBACK(int) vmR3SetRuntimeError(PVM pVM, uint32_t fFlags, const char *pszErrorId, char *pszMessage)
4412{
4413#if 0 /** @todo make copy of the error msg. */
4414 /*
4415 * Make a copy of the message.
4416 */
4417 va_list va2;
4418 va_copy(va2, *pVa);
4419 vmSetRuntimeErrorCopy(pVM, fFlags, pszErrorId, pszFormat, va2);
4420 va_end(va2);
4421#endif
4422
4423 /*
4424 * Join paths with VMR3SetRuntimeErrorWorker.
4425 */
4426 int rc = vmR3SetRuntimeErrorCommonF(pVM, fFlags, pszErrorId, "%s", pszMessage);
4427 MMR3HeapFree(pszMessage);
4428 return rc;
4429}
4430
4431
4432/**
4433 * Worker for VMSetRuntimeErrorV for doing the job on EMT in ring-3.
4434 *
4435 * @returns VBox status code with modifications, see VMSetRuntimeErrorV.
4436 *
[58122]4437 * @param pVM The cross context VM structure.
[22915]4438 * @param fFlags The error flags.
4439 * @param pszErrorId Error ID string.
[234]4440 * @param pszFormat Format string.
[18645]4441 * @param pVa Pointer to the format arguments.
4442 *
[234]4443 * @thread EMT
4444 */
[18645]4445DECLCALLBACK(int) vmR3SetRuntimeErrorV(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list *pVa)
[234]4446{
4447 /*
4448 * Make a copy of the message.
4449 */
[18645]4450 va_list va2;
4451 va_copy(va2, *pVa);
4452 vmSetRuntimeErrorCopy(pVM, fFlags, pszErrorId, pszFormat, va2);
4453 va_end(va2);
[234]4454
4455 /*
[18645]4456 * Join paths with VMR3SetRuntimeErrorWorker.
[234]4457 */
[18645]4458 return vmR3SetRuntimeErrorCommon(pVM, fFlags, pszErrorId, pszFormat, pVa);
[234]4459}
4460
[13796]4461
[13791]4462/**
[24508]4463 * Gets the number of runtime errors raised via VMR3SetRuntimeError.
4464 *
4465 * This can be used avoid double error messages.
4466 *
4467 * @returns The runtime error count.
[44340]4468 * @param pUVM The user mode VM handle.
[24508]4469 */
[44340]4470VMMR3_INT_DECL(uint32_t) VMR3GetRuntimeErrorCount(PUVM pUVM)
[24508]4471{
[44340]4472 return pUVM->vm.s.cRuntimeErrors;
[24508]4473}
4474
4475
4476/**
[33540]4477 * Gets the ID virtual of the virtual CPU associated with the calling thread.
[13791]4478 *
[19293]4479 * @returns The CPU ID. NIL_VMCPUID if the thread isn't an EMT.
4480 *
[58122]4481 * @param pVM The cross context VM structure.
[13791]4482 */
[44340]4483VMMR3_INT_DECL(RTCPUID) VMR3GetVMCPUId(PVM pVM)
[13791]4484{
[19217]4485 PUVMCPU pUVCpu = (PUVMCPU)RTTlsGet(pVM->pUVM->vm.s.idxTLS);
[19293]4486 return pUVCpu
4487 ? pUVCpu->idCpu
4488 : NIL_VMCPUID;
[13791]4489}
4490
[13796]4491
[13791]4492/**
[52419]4493 * Checks if the VM is long-mode (64-bit) capable or not.
4494 *
[72343]4495 * @returns true if VM can operate in long-mode, false otherwise.
[58122]4496 * @param pVM The cross context VM structure.
[52419]4497 */
4498VMMR3_INT_DECL(bool) VMR3IsLongModeAllowed(PVM pVM)
4499{
[72343]4500 switch (pVM->bMainExecutionEngine)
4501 {
4502 case VM_EXEC_ENGINE_HW_VIRT:
4503 return HMIsLongModeAllowed(pVM);
4504
4505 case VM_EXEC_ENGINE_NATIVE_API:
4506#ifndef IN_RC
4507 return NEMHCIsLongModeAllowed(pVM);
4508#else
4509 return false;
4510#endif
4511
4512 case VM_EXEC_ENGINE_NOT_SET:
4513 AssertFailed();
4514 RT_FALL_THRU();
4515 default:
4516 return false;
4517 }
[52419]4518}
4519
4520
4521/**
[71129]4522 * Returns the native ID of the current EMT VMCPU thread.
[13791]4523 *
4524 * @returns Handle if this is an EMT thread; NIL_RTNATIVETHREAD otherwise
[58122]4525 * @param pVM The cross context VM structure.
[13791]4526 * @thread EMT
4527 */
4528VMMR3DECL(RTNATIVETHREAD) VMR3GetVMCPUNativeThread(PVM pVM)
4529{
[19217]4530 PUVMCPU pUVCpu = (PUVMCPU)RTTlsGet(pVM->pUVM->vm.s.idxTLS);
[13791]4531
[19217]4532 if (!pUVCpu)
[13791]4533 return NIL_RTNATIVETHREAD;
4534
[19217]4535 return pUVCpu->vm.s.NativeThreadEMT;
[13791]4536}
4537
[13796]4538
[13791]4539/**
[71129]4540 * Returns the native ID of the current EMT VMCPU thread.
[13791]4541 *
4542 * @returns Handle if this is an EMT thread; NIL_RTNATIVETHREAD otherwise
[58126]4543 * @param pUVM The user mode VM structure.
[13791]4544 * @thread EMT
4545 */
4546VMMR3DECL(RTNATIVETHREAD) VMR3GetVMCPUNativeThreadU(PUVM pUVM)
4547{
[19217]4548 PUVMCPU pUVCpu = (PUVMCPU)RTTlsGet(pUVM->vm.s.idxTLS);
[13791]4549
[19217]4550 if (!pUVCpu)
[13791]4551 return NIL_RTNATIVETHREAD;
4552
[19217]4553 return pUVCpu->vm.s.NativeThreadEMT;
[13791]4554}
4555
[13796]4556
[13791]4557/**
4558 * Returns the handle of the current EMT VMCPU thread.
4559 *
4560 * @returns Handle if this is an EMT thread; NIL_RTNATIVETHREAD otherwise
[44340]4561 * @param pUVM The user mode VM handle.
[13791]4562 * @thread EMT
4563 */
[44340]4564VMMR3DECL(RTTHREAD) VMR3GetVMCPUThread(PUVM pUVM)
[13791]4565{
[19217]4566 PUVMCPU pUVCpu = (PUVMCPU)RTTlsGet(pUVM->vm.s.idxTLS);
[13791]4567
[19217]4568 if (!pUVCpu)
[13791]4569 return NIL_RTTHREAD;
4570
[19217]4571 return pUVCpu->vm.s.ThreadEMT;
[13791]4572}
[23024]4573
[25816]4574
4575/**
[71129]4576 * Returns the handle of the current EMT VMCPU thread.
4577 *
4578 * @returns The IPRT thread handle.
4579 * @param pUVCpu The user mode CPU handle.
4580 * @thread EMT
4581 */
4582VMMR3_INT_DECL(RTTHREAD) VMR3GetThreadHandle(PUVMCPU pUVCpu)
4583{
4584 return pUVCpu->vm.s.ThreadEMT;
4585}
4586
4587
4588/**
[44340]4589 * Return the package and core ID of a CPU.
[25816]4590 *
4591 * @returns VBOX status code.
[44340]4592 * @param pUVM The user mode VM handle.
[25816]4593 * @param idCpu Virtual CPU to get the ID from.
4594 * @param pidCpuCore Where to store the core ID of the virtual CPU.
4595 * @param pidCpuPackage Where to store the package ID of the virtual CPU.
[25825]4596 *
[25816]4597 */
[44340]4598VMMR3DECL(int) VMR3GetCpuCoreAndPackageIdFromCpuId(PUVM pUVM, VMCPUID idCpu, uint32_t *pidCpuCore, uint32_t *pidCpuPackage)
[25816]4599{
[36041]4600 /*
4601 * Validate input.
4602 */
[44340]4603 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
4604 PVM pVM = pUVM->pVM;
[36041]4605 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
4606 AssertPtrReturn(pidCpuCore, VERR_INVALID_POINTER);
4607 AssertPtrReturn(pidCpuPackage, VERR_INVALID_POINTER);
[25816]4608 if (idCpu >= pVM->cCpus)
4609 return VERR_INVALID_CPU_ID;
4610
[36041]4611 /*
4612 * Set return values.
4613 */
[25816]4614#ifdef VBOX_WITH_MULTI_CORE
4615 *pidCpuCore = idCpu;
4616 *pidCpuPackage = 0;
4617#else
4618 *pidCpuCore = 0;
4619 *pidCpuPackage = idCpu;
4620#endif
4621
4622 return VINF_SUCCESS;
4623}
4624
[25825]4625
[25816]4626/**
[25825]4627 * Worker for VMR3HotUnplugCpu.
[25816]4628 *
[25825]4629 * @returns VINF_EM_WAIT_SPIP (strict status code).
[58122]4630 * @param pVM The cross context VM structure.
[25825]4631 * @param idCpu The current CPU.
4632 */
4633static DECLCALLBACK(int) vmR3HotUnplugCpu(PVM pVM, VMCPUID idCpu)
4634{
4635 PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
4636 VMCPU_ASSERT_EMT(pVCpu);
4637
4638 /*
4639 * Reset per CPU resources.
4640 *
4641 * Actually only needed for VT-x because the CPU seems to be still in some
4642 * paged mode and startup fails after a new hot plug event. SVM works fine
4643 * even without this.
4644 */
4645 Log(("vmR3HotUnplugCpu for VCPU %u\n", idCpu));
[44730]4646 PGMR3ResetCpu(pVM, pVCpu);
[25825]4647 PDMR3ResetCpu(pVCpu);
4648 TRPMR3ResetCpu(pVCpu);
[49893]4649 CPUMR3ResetCpu(pVM, pVCpu);
[25825]4650 EMR3ResetCpu(pVCpu);
[43387]4651 HMR3ResetCpu(pVCpu);
[71040]4652 NEMR3ResetCpu(pVCpu, false /*fInitIpi*/);
[25825]4653 return VINF_EM_WAIT_SIPI;
4654}
4655
4656
4657/**
4658 * Hot-unplugs a CPU from the guest.
4659 *
[25816]4660 * @returns VBox status code.
[44340]4661 * @param pUVM The user mode VM handle.
[25825]4662 * @param idCpu Virtual CPU to perform the hot unplugging operation on.
[25816]4663 */
[44340]4664VMMR3DECL(int) VMR3HotUnplugCpu(PUVM pUVM, VMCPUID idCpu)
[25816]4665{
[44340]4666 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
4667 PVM pVM = pUVM->pVM;
[36041]4668 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
[25816]4669 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
4670
[25825]4671 /** @todo r=bird: Don't destroy the EMT, it'll break VMMR3EmtRendezvous and
4672 * broadcast requests. Just note down somewhere that the CPU is
4673 * offline and send it to SPIP wait. Maybe modify VMCPUSTATE and push
4674 * it out of the EM loops when offline. */
[44340]4675 return VMR3ReqCallNoWaitU(pUVM, idCpu, (PFNRT)vmR3HotUnplugCpu, 2, pVM, idCpu);
[25816]4676}
4677
[25825]4678
[25816]4679/**
[25825]4680 * Hot-plugs a CPU on the guest.
[25816]4681 *
4682 * @returns VBox status code.
[44340]4683 * @param pUVM The user mode VM handle.
[25825]4684 * @param idCpu Virtual CPU to perform the hot plugging operation on.
[25816]4685 */
[44340]4686VMMR3DECL(int) VMR3HotPlugCpu(PUVM pUVM, VMCPUID idCpu)
[25816]4687{
[44340]4688 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
4689 PVM pVM = pUVM->pVM;
[36041]4690 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
[25816]4691 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
4692
[25825]4693 /** @todo r-bird: Just mark it online and make sure it waits on SPIP. */
[25816]4694 return VINF_SUCCESS;
4695}
4696
[31331]4697
4698/**
[32885]4699 * Changes the VMM execution cap.
[31331]4700 *
4701 * @returns VBox status code.
[58126]4702 * @param pUVM The user mode VM structure.
[36041]4703 * @param uCpuExecutionCap New CPU execution cap in precent, 1-100. Where
4704 * 100 is max performance (default).
[31331]4705 */
[44340]4706VMMR3DECL(int) VMR3SetCpuExecutionCap(PUVM pUVM, uint32_t uCpuExecutionCap)
[31331]4707{
[44340]4708 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
4709 PVM pVM = pUVM->pVM;
[36041]4710 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
4711 AssertReturn(uCpuExecutionCap > 0 && uCpuExecutionCap <= 100, VERR_INVALID_PARAMETER);
[31339]4712
[36041]4713 Log(("VMR3SetCpuExecutionCap: new priority = %d\n", uCpuExecutionCap));
[31331]4714 /* Note: not called from EMT. */
[36041]4715 pVM->uCpuExecutionCap = uCpuExecutionCap;
[31331]4716 return VINF_SUCCESS;
4717}
[34326]4718
[48528]4719
4720/**
4721 * Control whether the VM should power off when resetting.
4722 *
4723 * @returns VBox status code.
4724 * @param pUVM The user mode VM handle.
4725 * @param fPowerOffInsteadOfReset Flag whether the VM should power off when
4726 * resetting.
4727 */
4728VMMR3DECL(int) VMR3SetPowerOffInsteadOfReset(PUVM pUVM, bool fPowerOffInsteadOfReset)
4729{
4730 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
4731 PVM pVM = pUVM->pVM;
4732 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
4733
4734 /* Note: not called from EMT. */
4735 pVM->vm.s.fPowerOffInsteadOfReset = fPowerOffInsteadOfReset;
4736 return VINF_SUCCESS;
4737}
4738
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use