VirtualBox

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

Last change on this file since 90785 was 90785, checked in by vboxsync, 3 years ago

*: AssertReturn(VALID_PTR(),...) -> AssertPtrReturn

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