VirtualBox

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

Last change on this file since 71289 was 71129, checked in by vboxsync, 7 years ago

VMM/NEM/win: Reimplemented virtual process API, optimizing the cancel case and prepping for doing this from ring-0. bugref:9044

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette