VirtualBox

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

Last change on this file since 95638 was 95244, checked in by vboxsync, 2 years ago

VMM/vmR3SetRuntimeErrorCommon: Don't call VMR3Suspend unless the VM is running. [fix] bugref:10111

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