VirtualBox

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

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

VMM: First stab at Guest Compatibility Manager, fixing up things like division overflows caused by fast CPUs (see bugref:9735).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 158.8 KB
Line 
1/* $Id: VM.cpp 94882 2022-05-06 06:33:54Z 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 if (pVCpu->idCpu == pVM->cCpus - 1)
1185 {
1186 int rc = vmR3TrySetState(pVM, "VMR3Suspend", 2,
1187 VMSTATE_SUSPENDING, VMSTATE_RUNNING,
1188 VMSTATE_SUSPENDING_EXT_LS, VMSTATE_RUNNING_LS);
1189 if (RT_FAILURE(rc))
1190 return rc;
1191 pVM->pUVM->vm.s.enmSuspendReason = enmReason;
1192 }
1193
1194 VMSTATE enmVMState = VMR3GetState(pVM);
1195 AssertMsgReturn( enmVMState == VMSTATE_SUSPENDING
1196 || enmVMState == VMSTATE_SUSPENDING_EXT_LS,
1197 ("%s\n", VMR3GetStateName(enmVMState)),
1198 VERR_VM_UNEXPECTED_UNSTABLE_STATE);
1199
1200 /*
1201 * EMT(0) does the actually suspending *after* all the other CPUs have
1202 * been thru here.
1203 */
1204 if (pVCpu->idCpu == 0)
1205 {
1206 vmR3SuspendDoWork(pVM);
1207
1208 int rc = vmR3TrySetState(pVM, "VMR3Suspend", 2,
1209 VMSTATE_SUSPENDED, VMSTATE_SUSPENDING,
1210 VMSTATE_SUSPENDED_EXT_LS, VMSTATE_SUSPENDING_EXT_LS);
1211 if (RT_FAILURE(rc))
1212 return VERR_VM_UNEXPECTED_UNSTABLE_STATE;
1213 }
1214
1215 return VINF_EM_SUSPEND;
1216}
1217
1218
1219/**
1220 * Suspends a running VM.
1221 *
1222 * @returns VBox status code. When called on EMT, this will be a strict status
1223 * code that has to be propagated up the call stack.
1224 *
1225 * @param pUVM The VM to suspend.
1226 * @param enmReason The reason for suspending.
1227 *
1228 * @thread Any thread.
1229 * @vmstate Running or RunningLS
1230 * @vmstateto Suspending + Suspended or SuspendingExtLS + SuspendedExtLS
1231 */
1232VMMR3DECL(int) VMR3Suspend(PUVM pUVM, VMSUSPENDREASON enmReason)
1233{
1234 LogFlow(("VMR3Suspend: pUVM=%p\n", pUVM));
1235 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1236 AssertReturn(enmReason > VMSUSPENDREASON_INVALID && enmReason < VMSUSPENDREASON_END, VERR_INVALID_PARAMETER);
1237
1238 /*
1239 * Gather all the EMTs to make sure there are no races before
1240 * changing the VM state.
1241 */
1242 int rc = VMMR3EmtRendezvous(pUVM->pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING | VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR,
1243 vmR3Suspend, (void *)(uintptr_t)enmReason);
1244 LogFlow(("VMR3Suspend: returns %Rrc\n", rc));
1245 return rc;
1246}
1247
1248
1249/**
1250 * Retrieves the reason for the most recent suspend.
1251 *
1252 * @returns Suspend reason. VMSUSPENDREASON_INVALID if no suspend has been done
1253 * or the handle is invalid.
1254 * @param pUVM The user mode VM handle.
1255 */
1256VMMR3DECL(VMSUSPENDREASON) VMR3GetSuspendReason(PUVM pUVM)
1257{
1258 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VMSUSPENDREASON_INVALID);
1259 return pUVM->vm.s.enmSuspendReason;
1260}
1261
1262
1263/**
1264 * EMT rendezvous worker for VMR3Resume.
1265 *
1266 * @returns VERR_VM_INVALID_VM_STATE or VINF_EM_RESUME. (This is a strict
1267 * return code, see FNVMMEMTRENDEZVOUS.)
1268 *
1269 * @param pVM The cross context VM structure.
1270 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1271 * @param pvUser Reason.
1272 */
1273static DECLCALLBACK(VBOXSTRICTRC) vmR3Resume(PVM pVM, PVMCPU pVCpu, void *pvUser)
1274{
1275 VMRESUMEREASON enmReason = (VMRESUMEREASON)(uintptr_t)pvUser;
1276 LogFlow(("vmR3Resume: pVM=%p pVCpu=%p/#%u enmReason=%d\n", pVM, pVCpu, pVCpu->idCpu, enmReason));
1277
1278 /*
1279 * The first thread thru here tries to change the state. We shouldn't be
1280 * called again if this fails.
1281 */
1282 if (pVCpu->idCpu == pVM->cCpus - 1)
1283 {
1284 int rc = vmR3TrySetState(pVM, "VMR3Resume", 1, VMSTATE_RESUMING, VMSTATE_SUSPENDED);
1285 if (RT_FAILURE(rc))
1286 return rc;
1287 pVM->pUVM->vm.s.enmResumeReason = enmReason;
1288 }
1289
1290 VMSTATE enmVMState = VMR3GetState(pVM);
1291 AssertMsgReturn(enmVMState == VMSTATE_RESUMING,
1292 ("%s\n", VMR3GetStateName(enmVMState)),
1293 VERR_VM_UNEXPECTED_UNSTABLE_STATE);
1294
1295#if 0
1296 /*
1297 * All EMTs changes their state to started.
1298 */
1299 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED);
1300#endif
1301
1302 /*
1303 * EMT(0) is last thru here and it will make the notification calls
1304 * and advance the state.
1305 */
1306 if (pVCpu->idCpu == 0)
1307 {
1308 PDMR3Resume(pVM);
1309 vmR3SetState(pVM, VMSTATE_RUNNING, VMSTATE_RESUMING);
1310 pVM->vm.s.fTeleportedAndNotFullyResumedYet = false;
1311 }
1312
1313 return VINF_EM_RESUME;
1314}
1315
1316
1317/**
1318 * Resume VM execution.
1319 *
1320 * @returns VBox status code. When called on EMT, this will be a strict status
1321 * code that has to be propagated up the call stack.
1322 *
1323 * @param pUVM The user mode VM handle.
1324 * @param enmReason The reason we're resuming.
1325 *
1326 * @thread Any thread.
1327 * @vmstate Suspended
1328 * @vmstateto Running
1329 */
1330VMMR3DECL(int) VMR3Resume(PUVM pUVM, VMRESUMEREASON enmReason)
1331{
1332 LogFlow(("VMR3Resume: pUVM=%p\n", pUVM));
1333 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1334 PVM pVM = pUVM->pVM;
1335 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1336 AssertReturn(enmReason > VMRESUMEREASON_INVALID && enmReason < VMRESUMEREASON_END, VERR_INVALID_PARAMETER);
1337
1338 /*
1339 * Gather all the EMTs to make sure there are no races before
1340 * changing the VM state.
1341 */
1342 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING | VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR,
1343 vmR3Resume, (void *)(uintptr_t)enmReason);
1344 LogFlow(("VMR3Resume: returns %Rrc\n", rc));
1345 return rc;
1346}
1347
1348
1349/**
1350 * Retrieves the reason for the most recent resume.
1351 *
1352 * @returns Resume reason. VMRESUMEREASON_INVALID if no suspend has been
1353 * done or the handle is invalid.
1354 * @param pUVM The user mode VM handle.
1355 */
1356VMMR3DECL(VMRESUMEREASON) VMR3GetResumeReason(PUVM pUVM)
1357{
1358 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VMRESUMEREASON_INVALID);
1359 return pUVM->vm.s.enmResumeReason;
1360}
1361
1362
1363/**
1364 * EMT rendezvous worker for VMR3Save and VMR3Teleport that suspends the VM
1365 * after the live step has been completed.
1366 *
1367 * @returns VERR_VM_INVALID_VM_STATE or VINF_EM_RESUME. (This is a strict
1368 * return code, see FNVMMEMTRENDEZVOUS.)
1369 *
1370 * @param pVM The cross context VM structure.
1371 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1372 * @param pvUser The pfSuspended argument of vmR3SaveTeleport.
1373 */
1374static DECLCALLBACK(VBOXSTRICTRC) vmR3LiveDoSuspend(PVM pVM, PVMCPU pVCpu, void *pvUser)
1375{
1376 LogFlow(("vmR3LiveDoSuspend: pVM=%p pVCpu=%p/#%u\n", pVM, pVCpu, pVCpu->idCpu));
1377 bool *pfSuspended = (bool *)pvUser;
1378
1379 /*
1380 * The first thread thru here tries to change the state. We shouldn't be
1381 * called again if this fails.
1382 */
1383 if (pVCpu->idCpu == pVM->cCpus - 1U)
1384 {
1385 PUVM pUVM = pVM->pUVM;
1386 int rc;
1387
1388 RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
1389 VMSTATE enmVMState = pVM->enmVMState;
1390 switch (enmVMState)
1391 {
1392 case VMSTATE_RUNNING_LS:
1393 vmR3SetStateLocked(pVM, pUVM, VMSTATE_SUSPENDING_LS, VMSTATE_RUNNING_LS, false /*fSetRatherThanClearFF*/);
1394 rc = VINF_SUCCESS;
1395 break;
1396
1397 case VMSTATE_SUSPENDED_EXT_LS:
1398 case VMSTATE_SUSPENDED_LS: /* (via reset) */
1399 rc = VINF_SUCCESS;
1400 break;
1401
1402 case VMSTATE_DEBUGGING_LS:
1403 rc = VERR_TRY_AGAIN;
1404 break;
1405
1406 case VMSTATE_OFF_LS:
1407 vmR3SetStateLocked(pVM, pUVM, VMSTATE_OFF, VMSTATE_OFF_LS, false /*fSetRatherThanClearFF*/);
1408 rc = VERR_SSM_LIVE_POWERED_OFF;
1409 break;
1410
1411 case VMSTATE_FATAL_ERROR_LS:
1412 vmR3SetStateLocked(pVM, pUVM, VMSTATE_FATAL_ERROR, VMSTATE_FATAL_ERROR_LS, false /*fSetRatherThanClearFF*/);
1413 rc = VERR_SSM_LIVE_FATAL_ERROR;
1414 break;
1415
1416 case VMSTATE_GURU_MEDITATION_LS:
1417 vmR3SetStateLocked(pVM, pUVM, VMSTATE_GURU_MEDITATION, VMSTATE_GURU_MEDITATION_LS, false /*fSetRatherThanClearFF*/);
1418 rc = VERR_SSM_LIVE_GURU_MEDITATION;
1419 break;
1420
1421 case VMSTATE_POWERING_OFF_LS:
1422 case VMSTATE_SUSPENDING_EXT_LS:
1423 case VMSTATE_RESETTING_LS:
1424 default:
1425 AssertMsgFailed(("%s\n", VMR3GetStateName(enmVMState)));
1426 rc = VERR_VM_UNEXPECTED_VM_STATE;
1427 break;
1428 }
1429 RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
1430 if (RT_FAILURE(rc))
1431 {
1432 LogFlow(("vmR3LiveDoSuspend: returns %Rrc (state was %s)\n", rc, VMR3GetStateName(enmVMState)));
1433 return rc;
1434 }
1435 }
1436
1437 VMSTATE enmVMState = VMR3GetState(pVM);
1438 AssertMsgReturn(enmVMState == VMSTATE_SUSPENDING_LS,
1439 ("%s\n", VMR3GetStateName(enmVMState)),
1440 VERR_VM_UNEXPECTED_UNSTABLE_STATE);
1441
1442 /*
1443 * Only EMT(0) have work to do since it's last thru here.
1444 */
1445 if (pVCpu->idCpu == 0)
1446 {
1447 vmR3SuspendDoWork(pVM);
1448 int rc = vmR3TrySetState(pVM, "VMR3Suspend", 1,
1449 VMSTATE_SUSPENDED_LS, VMSTATE_SUSPENDING_LS);
1450 if (RT_FAILURE(rc))
1451 return VERR_VM_UNEXPECTED_UNSTABLE_STATE;
1452
1453 *pfSuspended = true;
1454 }
1455
1456 return VINF_EM_SUSPEND;
1457}
1458
1459
1460/**
1461 * EMT rendezvous worker that VMR3Save and VMR3Teleport uses to clean up a
1462 * SSMR3LiveDoStep1 failure.
1463 *
1464 * Doing this as a rendezvous operation avoids all annoying transition
1465 * states.
1466 *
1467 * @returns VERR_VM_INVALID_VM_STATE, VINF_SUCCESS or some specific VERR_SSM_*
1468 * status code. (This is a strict return code, see FNVMMEMTRENDEZVOUS.)
1469 *
1470 * @param pVM The cross context VM structure.
1471 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1472 * @param pvUser The pfSuspended argument of vmR3SaveTeleport.
1473 */
1474static DECLCALLBACK(VBOXSTRICTRC) vmR3LiveDoStep1Cleanup(PVM pVM, PVMCPU pVCpu, void *pvUser)
1475{
1476 LogFlow(("vmR3LiveDoStep1Cleanup: pVM=%p pVCpu=%p/#%u\n", pVM, pVCpu, pVCpu->idCpu));
1477 bool *pfSuspended = (bool *)pvUser;
1478 NOREF(pVCpu);
1479
1480 int rc = vmR3TrySetState(pVM, "vmR3LiveDoStep1Cleanup", 8,
1481 VMSTATE_OFF, VMSTATE_OFF_LS, /* 1 */
1482 VMSTATE_FATAL_ERROR, VMSTATE_FATAL_ERROR_LS, /* 2 */
1483 VMSTATE_GURU_MEDITATION, VMSTATE_GURU_MEDITATION_LS, /* 3 */
1484 VMSTATE_SUSPENDED, VMSTATE_SUSPENDED_LS, /* 4 */
1485 VMSTATE_SUSPENDED, VMSTATE_SAVING,
1486 VMSTATE_SUSPENDED, VMSTATE_SUSPENDED_EXT_LS,
1487 VMSTATE_RUNNING, VMSTATE_RUNNING_LS,
1488 VMSTATE_DEBUGGING, VMSTATE_DEBUGGING_LS);
1489 if (rc == 1)
1490 rc = VERR_SSM_LIVE_POWERED_OFF;
1491 else if (rc == 2)
1492 rc = VERR_SSM_LIVE_FATAL_ERROR;
1493 else if (rc == 3)
1494 rc = VERR_SSM_LIVE_GURU_MEDITATION;
1495 else if (rc == 4)
1496 {
1497 *pfSuspended = true;
1498 rc = VINF_SUCCESS;
1499 }
1500 else if (rc > 0)
1501 rc = VINF_SUCCESS;
1502 return rc;
1503}
1504
1505
1506/**
1507 * EMT(0) worker for VMR3Save and VMR3Teleport that completes the live save.
1508 *
1509 * @returns VBox status code.
1510 * @retval VINF_SSM_LIVE_SUSPENDED if VMR3Suspend was called.
1511 *
1512 * @param pVM The cross context VM structure.
1513 * @param pSSM The handle of saved state operation.
1514 *
1515 * @thread EMT(0)
1516 */
1517static DECLCALLBACK(int) vmR3LiveDoStep2(PVM pVM, PSSMHANDLE pSSM)
1518{
1519 LogFlow(("vmR3LiveDoStep2: pVM=%p pSSM=%p\n", pVM, pSSM));
1520 VM_ASSERT_EMT0(pVM);
1521
1522 /*
1523 * Advance the state and mark if VMR3Suspend was called.
1524 */
1525 int rc = VINF_SUCCESS;
1526 VMSTATE enmVMState = VMR3GetState(pVM);
1527 if (enmVMState == VMSTATE_SUSPENDED_LS)
1528 vmR3SetState(pVM, VMSTATE_SAVING, VMSTATE_SUSPENDED_LS);
1529 else
1530 {
1531 if (enmVMState != VMSTATE_SAVING)
1532 vmR3SetState(pVM, VMSTATE_SAVING, VMSTATE_SUSPENDED_EXT_LS);
1533 rc = VINF_SSM_LIVE_SUSPENDED;
1534 }
1535
1536 /*
1537 * Finish up and release the handle. Careful with the status codes.
1538 */
1539 int rc2 = SSMR3LiveDoStep2(pSSM);
1540 if (rc == VINF_SUCCESS || (RT_FAILURE(rc2) && RT_SUCCESS(rc)))
1541 rc = rc2;
1542
1543 rc2 = SSMR3LiveDone(pSSM);
1544 if (rc == VINF_SUCCESS || (RT_FAILURE(rc2) && RT_SUCCESS(rc)))
1545 rc = rc2;
1546
1547 /*
1548 * Advance to the final state and return.
1549 */
1550 vmR3SetState(pVM, VMSTATE_SUSPENDED, VMSTATE_SAVING);
1551 Assert(rc > VINF_EM_LAST || rc < VINF_EM_FIRST);
1552 return rc;
1553}
1554
1555
1556/**
1557 * Worker for vmR3SaveTeleport that validates the state and calls SSMR3Save or
1558 * SSMR3LiveSave.
1559 *
1560 * @returns VBox status code.
1561 *
1562 * @param pVM The cross context VM structure.
1563 * @param cMsMaxDowntime The maximum downtime given as milliseconds.
1564 * @param pszFilename The name of the file. NULL if pStreamOps is used.
1565 * @param pStreamOps The stream methods. NULL if pszFilename is used.
1566 * @param pvStreamOpsUser The user argument to the stream methods.
1567 * @param enmAfter What to do afterwards.
1568 * @param pfnProgress Progress callback. Optional.
1569 * @param pvProgressUser User argument for the progress callback.
1570 * @param ppSSM Where to return the saved state handle in case of a
1571 * live snapshot scenario.
1572 *
1573 * @thread EMT
1574 */
1575static DECLCALLBACK(int) vmR3Save(PVM pVM, uint32_t cMsMaxDowntime, const char *pszFilename, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser,
1576 SSMAFTER enmAfter, PFNVMPROGRESS pfnProgress, void *pvProgressUser, PSSMHANDLE *ppSSM)
1577{
1578 int rc = VINF_SUCCESS;
1579
1580 LogFlow(("vmR3Save: pVM=%p cMsMaxDowntime=%u pszFilename=%p:{%s} pStreamOps=%p pvStreamOpsUser=%p enmAfter=%d pfnProgress=%p pvProgressUser=%p ppSSM=%p\n",
1581 pVM, cMsMaxDowntime, pszFilename, pszFilename, pStreamOps, pvStreamOpsUser, enmAfter, pfnProgress, pvProgressUser, ppSSM));
1582
1583 /*
1584 * Validate input.
1585 */
1586 AssertPtrNull(pszFilename);
1587 AssertPtrNull(pStreamOps);
1588 AssertPtr(pVM);
1589 Assert( enmAfter == SSMAFTER_DESTROY
1590 || enmAfter == SSMAFTER_CONTINUE
1591 || enmAfter == SSMAFTER_TELEPORT);
1592 AssertPtr(ppSSM);
1593 *ppSSM = NULL;
1594
1595 /*
1596 * Change the state and perform/start the saving.
1597 */
1598 rc = vmR3TrySetState(pVM, "VMR3Save", 2,
1599 VMSTATE_SAVING, VMSTATE_SUSPENDED,
1600 VMSTATE_RUNNING_LS, VMSTATE_RUNNING);
1601 if (rc == 1 && enmAfter != SSMAFTER_TELEPORT)
1602 {
1603 rc = SSMR3Save(pVM, pszFilename, pStreamOps, pvStreamOpsUser, enmAfter, pfnProgress, pvProgressUser);
1604 vmR3SetState(pVM, VMSTATE_SUSPENDED, VMSTATE_SAVING);
1605 }
1606 else if (rc == 2 || enmAfter == SSMAFTER_TELEPORT)
1607 {
1608 if (enmAfter == SSMAFTER_TELEPORT)
1609 pVM->vm.s.fTeleportedAndNotFullyResumedYet = true;
1610 rc = SSMR3LiveSave(pVM, cMsMaxDowntime, pszFilename, pStreamOps, pvStreamOpsUser,
1611 enmAfter, pfnProgress, pvProgressUser, ppSSM);
1612 /* (We're not subject to cancellation just yet.) */
1613 }
1614 else
1615 Assert(RT_FAILURE(rc));
1616 return rc;
1617}
1618
1619
1620/**
1621 * Common worker for VMR3Save and VMR3Teleport.
1622 *
1623 * @returns VBox status code.
1624 *
1625 * @param pVM The cross context VM structure.
1626 * @param cMsMaxDowntime The maximum downtime given as milliseconds.
1627 * @param pszFilename The name of the file. NULL if pStreamOps is used.
1628 * @param pStreamOps The stream methods. NULL if pszFilename is used.
1629 * @param pvStreamOpsUser The user argument to the stream methods.
1630 * @param enmAfter What to do afterwards.
1631 * @param pfnProgress Progress callback. Optional.
1632 * @param pvProgressUser User argument for the progress callback.
1633 * @param pfSuspended Set if we suspended the VM.
1634 *
1635 * @thread Non-EMT
1636 */
1637static int vmR3SaveTeleport(PVM pVM, uint32_t cMsMaxDowntime,
1638 const char *pszFilename, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser,
1639 SSMAFTER enmAfter, PFNVMPROGRESS pfnProgress, void *pvProgressUser, bool *pfSuspended)
1640{
1641 /*
1642 * Request the operation in EMT(0).
1643 */
1644 PSSMHANDLE pSSM;
1645 int rc = VMR3ReqCallWait(pVM, 0 /*idDstCpu*/,
1646 (PFNRT)vmR3Save, 9, pVM, cMsMaxDowntime, pszFilename, pStreamOps, pvStreamOpsUser,
1647 enmAfter, pfnProgress, pvProgressUser, &pSSM);
1648 if ( RT_SUCCESS(rc)
1649 && pSSM)
1650 {
1651 /*
1652 * Live snapshot.
1653 *
1654 * The state handling here is kind of tricky, doing it on EMT(0) helps
1655 * a bit. See the VMSTATE diagram for details.
1656 */
1657 rc = SSMR3LiveDoStep1(pSSM);
1658 if (RT_SUCCESS(rc))
1659 {
1660 if (VMR3GetState(pVM) != VMSTATE_SAVING)
1661 for (;;)
1662 {
1663 /* Try suspend the VM. */
1664 rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING | VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR,
1665 vmR3LiveDoSuspend, pfSuspended);
1666 if (rc != VERR_TRY_AGAIN)
1667 break;
1668
1669 /* Wait for the state to change. */
1670 RTThreadSleep(250); /** @todo Live Migration: fix this polling wait by some smart use of multiple release event semaphores.. */
1671 }
1672 if (RT_SUCCESS(rc))
1673 rc = VMR3ReqCallWait(pVM, 0 /*idDstCpu*/, (PFNRT)vmR3LiveDoStep2, 2, pVM, pSSM);
1674 else
1675 {
1676 int rc2 = VMR3ReqCallWait(pVM, 0 /*idDstCpu*/, (PFNRT)SSMR3LiveDone, 1, pSSM);
1677 AssertMsg(rc2 == rc, ("%Rrc != %Rrc\n", rc2, rc)); NOREF(rc2);
1678 }
1679 }
1680 else
1681 {
1682 int rc2 = VMR3ReqCallWait(pVM, 0 /*idDstCpu*/, (PFNRT)SSMR3LiveDone, 1, pSSM);
1683 AssertMsg(rc2 == rc, ("%Rrc != %Rrc\n", rc2, rc));
1684
1685 rc2 = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, vmR3LiveDoStep1Cleanup, pfSuspended);
1686 if (RT_FAILURE(rc2) && rc == VERR_SSM_CANCELLED)
1687 rc = rc2;
1688 }
1689 }
1690
1691 return rc;
1692}
1693
1694
1695/**
1696 * Save current VM state.
1697 *
1698 * Can be used for both saving the state and creating snapshots.
1699 *
1700 * When called for a VM in the Running state, the saved state is created live
1701 * and the VM is only suspended when the final part of the saving is preformed.
1702 * The VM state will not be restored to Running in this case and it's up to the
1703 * caller to call VMR3Resume if this is desirable. (The rational is that the
1704 * caller probably wish to reconfigure the disks before resuming the VM.)
1705 *
1706 * @returns VBox status code.
1707 *
1708 * @param pUVM The VM which state should be saved.
1709 * @param pszFilename The name of the save state file.
1710 * @param pStreamOps The stream methods. NULL if pszFilename is used.
1711 * @param pvStreamOpsUser The user argument to the stream methods.
1712 * @param fContinueAfterwards Whether continue execution afterwards or not.
1713 * When in doubt, set this to true.
1714 * @param pfnProgress Progress callback. Optional.
1715 * @param pvUser User argument for the progress callback.
1716 * @param pfSuspended Set if we suspended the VM.
1717 *
1718 * @thread Non-EMT.
1719 * @vmstate Suspended or Running
1720 * @vmstateto Saving+Suspended or
1721 * RunningLS+SuspendingLS+SuspendedLS+Saving+Suspended.
1722 */
1723VMMR3DECL(int) VMR3Save(PUVM pUVM, const char *pszFilename, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser,
1724 bool fContinueAfterwards, PFNVMPROGRESS pfnProgress, void *pvUser,
1725 bool *pfSuspended)
1726{
1727 LogFlow(("VMR3Save: pUVM=%p pszFilename=%p:{%s} fContinueAfterwards=%RTbool pfnProgress=%p pvUser=%p pfSuspended=%p\n",
1728 pUVM, pszFilename, pszFilename, fContinueAfterwards, pfnProgress, pvUser, pfSuspended));
1729
1730 /*
1731 * Validate input.
1732 */
1733 AssertPtr(pfSuspended);
1734 *pfSuspended = false;
1735 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1736 PVM pVM = pUVM->pVM;
1737 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1738 VM_ASSERT_OTHER_THREAD(pVM);
1739 AssertReturn(pszFilename || pStreamOps, VERR_INVALID_POINTER);
1740 AssertReturn( (!pStreamOps && *pszFilename)
1741 || pStreamOps,
1742 VERR_INVALID_PARAMETER);
1743 AssertPtrNullReturn(pfnProgress, VERR_INVALID_POINTER);
1744
1745 /*
1746 * Join paths with VMR3Teleport.
1747 */
1748 SSMAFTER enmAfter = fContinueAfterwards ? SSMAFTER_CONTINUE : SSMAFTER_DESTROY;
1749 int rc = vmR3SaveTeleport(pVM, 250 /*cMsMaxDowntime*/,
1750 pszFilename, pStreamOps, pvStreamOpsUser,
1751 enmAfter, pfnProgress, pvUser, pfSuspended);
1752 LogFlow(("VMR3Save: returns %Rrc (*pfSuspended=%RTbool)\n", rc, *pfSuspended));
1753 return rc;
1754}
1755
1756
1757/**
1758 * Teleport the VM (aka live migration).
1759 *
1760 * @returns VBox status code.
1761 *
1762 * @param pUVM The VM which state should be saved.
1763 * @param cMsMaxDowntime The maximum downtime given as milliseconds.
1764 * @param pStreamOps The stream methods.
1765 * @param pvStreamOpsUser The user argument to the stream methods.
1766 * @param pfnProgress Progress callback. Optional.
1767 * @param pvProgressUser User argument for the progress callback.
1768 * @param pfSuspended Set if we suspended the VM.
1769 *
1770 * @thread Non-EMT.
1771 * @vmstate Suspended or Running
1772 * @vmstateto Saving+Suspended or
1773 * RunningLS+SuspendingLS+SuspendedLS+Saving+Suspended.
1774 */
1775VMMR3DECL(int) VMR3Teleport(PUVM pUVM, uint32_t cMsMaxDowntime, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser,
1776 PFNVMPROGRESS pfnProgress, void *pvProgressUser, bool *pfSuspended)
1777{
1778 LogFlow(("VMR3Teleport: pUVM=%p cMsMaxDowntime=%u pStreamOps=%p pvStreamOps=%p pfnProgress=%p pvProgressUser=%p\n",
1779 pUVM, cMsMaxDowntime, pStreamOps, pvStreamOpsUser, pfnProgress, pvProgressUser));
1780
1781 /*
1782 * Validate input.
1783 */
1784 AssertPtr(pfSuspended);
1785 *pfSuspended = false;
1786 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1787 PVM pVM = pUVM->pVM;
1788 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1789 VM_ASSERT_OTHER_THREAD(pVM);
1790 AssertPtrReturn(pStreamOps, VERR_INVALID_POINTER);
1791 AssertPtrNullReturn(pfnProgress, VERR_INVALID_POINTER);
1792
1793 /*
1794 * Join paths with VMR3Save.
1795 */
1796 int rc = vmR3SaveTeleport(pVM, cMsMaxDowntime, NULL /*pszFilename*/, pStreamOps, pvStreamOpsUser,
1797 SSMAFTER_TELEPORT, pfnProgress, pvProgressUser, pfSuspended);
1798 LogFlow(("VMR3Teleport: returns %Rrc (*pfSuspended=%RTbool)\n", rc, *pfSuspended));
1799 return rc;
1800}
1801
1802
1803
1804/**
1805 * EMT(0) worker for VMR3LoadFromFile and VMR3LoadFromStream.
1806 *
1807 * @returns VBox status code.
1808 *
1809 * @param pUVM Pointer to the VM.
1810 * @param pszFilename The name of the file. NULL if pStreamOps is used.
1811 * @param pStreamOps The stream methods. NULL if pszFilename is used.
1812 * @param pvStreamOpsUser The user argument to the stream methods.
1813 * @param pfnProgress Progress callback. Optional.
1814 * @param pvProgressUser User argument for the progress callback.
1815 * @param fTeleporting Indicates whether we're teleporting or not.
1816 *
1817 * @thread EMT.
1818 */
1819static DECLCALLBACK(int) vmR3Load(PUVM pUVM, const char *pszFilename, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser,
1820 PFNVMPROGRESS pfnProgress, void *pvProgressUser, bool fTeleporting)
1821{
1822 LogFlow(("vmR3Load: pUVM=%p pszFilename=%p:{%s} pStreamOps=%p pvStreamOpsUser=%p pfnProgress=%p pvProgressUser=%p fTeleporting=%RTbool\n",
1823 pUVM, pszFilename, pszFilename, pStreamOps, pvStreamOpsUser, pfnProgress, pvProgressUser, fTeleporting));
1824
1825 /*
1826 * Validate input (paranoia).
1827 */
1828 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1829 PVM pVM = pUVM->pVM;
1830 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1831 AssertPtrNull(pszFilename);
1832 AssertPtrNull(pStreamOps);
1833 AssertPtrNull(pfnProgress);
1834
1835 /*
1836 * Change the state and perform the load.
1837 *
1838 * Always perform a relocation round afterwards to make sure hypervisor
1839 * selectors and such are correct.
1840 */
1841 int rc = vmR3TrySetState(pVM, "VMR3Load", 2,
1842 VMSTATE_LOADING, VMSTATE_CREATED,
1843 VMSTATE_LOADING, VMSTATE_SUSPENDED);
1844 if (RT_FAILURE(rc))
1845 return rc;
1846
1847 pVM->vm.s.fTeleportedAndNotFullyResumedYet = fTeleporting;
1848
1849 uint32_t cErrorsPriorToSave = VMR3GetErrorCount(pUVM);
1850 rc = SSMR3Load(pVM, pszFilename, pStreamOps, pvStreamOpsUser, SSMAFTER_RESUME, pfnProgress, pvProgressUser);
1851 if (RT_SUCCESS(rc))
1852 {
1853 VMR3Relocate(pVM, 0 /*offDelta*/);
1854 vmR3SetState(pVM, VMSTATE_SUSPENDED, VMSTATE_LOADING);
1855 }
1856 else
1857 {
1858 pVM->vm.s.fTeleportedAndNotFullyResumedYet = false;
1859 vmR3SetState(pVM, VMSTATE_LOAD_FAILURE, VMSTATE_LOADING);
1860
1861 if (cErrorsPriorToSave == VMR3GetErrorCount(pUVM))
1862 rc = VMSetError(pVM, rc, RT_SRC_POS,
1863 N_("Unable to restore the virtual machine's saved state from '%s'. "
1864 "It may be damaged or from an older version of VirtualBox. "
1865 "Please discard the saved state before starting the virtual machine"),
1866 pszFilename);
1867 }
1868
1869 return rc;
1870}
1871
1872
1873/**
1874 * Loads a VM state into a newly created VM or a one that is suspended.
1875 *
1876 * To restore a saved state on VM startup, call this function and then resume
1877 * the VM instead of powering it on.
1878 *
1879 * @returns VBox status code.
1880 *
1881 * @param pUVM The user mode VM structure.
1882 * @param pszFilename The name of the save state file.
1883 * @param pfnProgress Progress callback. Optional.
1884 * @param pvUser User argument for the progress callback.
1885 *
1886 * @thread Any thread.
1887 * @vmstate Created, Suspended
1888 * @vmstateto Loading+Suspended
1889 */
1890VMMR3DECL(int) VMR3LoadFromFile(PUVM pUVM, const char *pszFilename, PFNVMPROGRESS pfnProgress, void *pvUser)
1891{
1892 LogFlow(("VMR3LoadFromFile: pUVM=%p pszFilename=%p:{%s} pfnProgress=%p pvUser=%p\n",
1893 pUVM, pszFilename, pszFilename, pfnProgress, pvUser));
1894
1895 /*
1896 * Validate input.
1897 */
1898 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1899 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
1900
1901 /*
1902 * Forward the request to EMT(0). No need to setup a rendezvous here
1903 * since there is no execution taking place when this call is allowed.
1904 */
1905 int rc = VMR3ReqCallWaitU(pUVM, 0 /*idDstCpu*/, (PFNRT)vmR3Load, 7,
1906 pUVM, pszFilename, (uintptr_t)NULL /*pStreamOps*/, (uintptr_t)NULL /*pvStreamOpsUser*/,
1907 pfnProgress, pvUser, false /*fTeleporting*/);
1908 LogFlow(("VMR3LoadFromFile: returns %Rrc\n", rc));
1909 return rc;
1910}
1911
1912
1913/**
1914 * VMR3LoadFromFile for arbitrary file streams.
1915 *
1916 * @returns VBox status code.
1917 *
1918 * @param pUVM Pointer to the VM.
1919 * @param pStreamOps The stream methods.
1920 * @param pvStreamOpsUser The user argument to the stream methods.
1921 * @param pfnProgress Progress callback. Optional.
1922 * @param pvProgressUser User argument for the progress callback.
1923 *
1924 * @thread Any thread.
1925 * @vmstate Created, Suspended
1926 * @vmstateto Loading+Suspended
1927 */
1928VMMR3DECL(int) VMR3LoadFromStream(PUVM pUVM, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser,
1929 PFNVMPROGRESS pfnProgress, void *pvProgressUser)
1930{
1931 LogFlow(("VMR3LoadFromStream: pUVM=%p pStreamOps=%p pvStreamOpsUser=%p pfnProgress=%p pvProgressUser=%p\n",
1932 pUVM, pStreamOps, pvStreamOpsUser, pfnProgress, pvProgressUser));
1933
1934 /*
1935 * Validate input.
1936 */
1937 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1938 AssertPtrReturn(pStreamOps, VERR_INVALID_POINTER);
1939
1940 /*
1941 * Forward the request to EMT(0). No need to setup a rendezvous here
1942 * since there is no execution taking place when this call is allowed.
1943 */
1944 int rc = VMR3ReqCallWaitU(pUVM, 0 /*idDstCpu*/, (PFNRT)vmR3Load, 7,
1945 pUVM, (uintptr_t)NULL /*pszFilename*/, pStreamOps, pvStreamOpsUser, pfnProgress,
1946 pvProgressUser, true /*fTeleporting*/);
1947 LogFlow(("VMR3LoadFromStream: returns %Rrc\n", rc));
1948 return rc;
1949}
1950
1951
1952/**
1953 * EMT rendezvous worker for VMR3PowerOff.
1954 *
1955 * @returns VERR_VM_INVALID_VM_STATE or VINF_EM_OFF. (This is a strict
1956 * return code, see FNVMMEMTRENDEZVOUS.)
1957 *
1958 * @param pVM The cross context VM structure.
1959 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1960 * @param pvUser Ignored.
1961 */
1962static DECLCALLBACK(VBOXSTRICTRC) vmR3PowerOff(PVM pVM, PVMCPU pVCpu, void *pvUser)
1963{
1964 LogFlow(("vmR3PowerOff: pVM=%p pVCpu=%p/#%u\n", pVM, pVCpu, pVCpu->idCpu));
1965 Assert(!pvUser); NOREF(pvUser);
1966
1967 /*
1968 * The first EMT thru here will change the state to PoweringOff.
1969 */
1970 if (pVCpu->idCpu == pVM->cCpus - 1)
1971 {
1972 int rc = vmR3TrySetState(pVM, "VMR3PowerOff", 11,
1973 VMSTATE_POWERING_OFF, VMSTATE_RUNNING, /* 1 */
1974 VMSTATE_POWERING_OFF, VMSTATE_SUSPENDED, /* 2 */
1975 VMSTATE_POWERING_OFF, VMSTATE_DEBUGGING, /* 3 */
1976 VMSTATE_POWERING_OFF, VMSTATE_LOAD_FAILURE, /* 4 */
1977 VMSTATE_POWERING_OFF, VMSTATE_GURU_MEDITATION, /* 5 */
1978 VMSTATE_POWERING_OFF, VMSTATE_FATAL_ERROR, /* 6 */
1979 VMSTATE_POWERING_OFF, VMSTATE_CREATED, /* 7 */ /** @todo update the diagram! */
1980 VMSTATE_POWERING_OFF_LS, VMSTATE_RUNNING_LS, /* 8 */
1981 VMSTATE_POWERING_OFF_LS, VMSTATE_DEBUGGING_LS, /* 9 */
1982 VMSTATE_POWERING_OFF_LS, VMSTATE_GURU_MEDITATION_LS,/* 10 */
1983 VMSTATE_POWERING_OFF_LS, VMSTATE_FATAL_ERROR_LS); /* 11 */
1984 if (RT_FAILURE(rc))
1985 return rc;
1986 if (rc >= 7)
1987 SSMR3Cancel(pVM->pUVM);
1988 }
1989
1990 /*
1991 * Check the state.
1992 */
1993 VMSTATE enmVMState = VMR3GetState(pVM);
1994 AssertMsgReturn( enmVMState == VMSTATE_POWERING_OFF
1995 || enmVMState == VMSTATE_POWERING_OFF_LS,
1996 ("%s\n", VMR3GetStateName(enmVMState)),
1997 VERR_VM_INVALID_VM_STATE);
1998
1999 /*
2000 * EMT(0) does the actual power off work here *after* all the other EMTs
2001 * have been thru and entered the STOPPED state.
2002 */
2003 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STOPPED);
2004 if (pVCpu->idCpu == 0)
2005 {
2006 /*
2007 * For debugging purposes, we will log a summary of the guest state at this point.
2008 */
2009 if (enmVMState != VMSTATE_GURU_MEDITATION)
2010 {
2011 /** @todo make the state dumping at VMR3PowerOff optional. */
2012 bool fOldBuffered = RTLogRelSetBuffering(true /*fBuffered*/);
2013 RTLogRelPrintf("****************** Guest state at power off for VCpu %u ******************\n", pVCpu->idCpu);
2014 DBGFR3InfoEx(pVM->pUVM, pVCpu->idCpu, "cpumguest", "verbose", DBGFR3InfoLogRelHlp());
2015 RTLogRelPrintf("***\n");
2016 DBGFR3InfoEx(pVM->pUVM, pVCpu->idCpu, "cpumguesthwvirt", "verbose", DBGFR3InfoLogRelHlp());
2017 RTLogRelPrintf("***\n");
2018 DBGFR3InfoEx(pVM->pUVM, pVCpu->idCpu, "mode", NULL, DBGFR3InfoLogRelHlp());
2019 RTLogRelPrintf("***\n");
2020 DBGFR3Info(pVM->pUVM, "activetimers", NULL, DBGFR3InfoLogRelHlp());
2021 RTLogRelPrintf("***\n");
2022 DBGFR3Info(pVM->pUVM, "gdt", NULL, DBGFR3InfoLogRelHlp());
2023 /** @todo dump guest call stack. */
2024 RTLogRelSetBuffering(fOldBuffered);
2025 RTLogRelPrintf("************** End of Guest state at power off ***************\n");
2026 }
2027
2028 /*
2029 * Perform the power off notifications and advance the state to
2030 * Off or OffLS.
2031 */
2032 PDMR3PowerOff(pVM);
2033 DBGFR3PowerOff(pVM);
2034
2035 PUVM pUVM = pVM->pUVM;
2036 RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
2037 enmVMState = pVM->enmVMState;
2038 if (enmVMState == VMSTATE_POWERING_OFF_LS)
2039 vmR3SetStateLocked(pVM, pUVM, VMSTATE_OFF_LS, VMSTATE_POWERING_OFF_LS, false /*fSetRatherThanClearFF*/);
2040 else
2041 vmR3SetStateLocked(pVM, pUVM, VMSTATE_OFF, VMSTATE_POWERING_OFF, false /*fSetRatherThanClearFF*/);
2042 RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
2043 }
2044 else if (enmVMState != VMSTATE_GURU_MEDITATION)
2045 {
2046 /** @todo make the state dumping at VMR3PowerOff optional. */
2047 bool fOldBuffered = RTLogRelSetBuffering(true /*fBuffered*/);
2048 RTLogRelPrintf("****************** Guest state at power off for VCpu %u ******************\n", pVCpu->idCpu);
2049 DBGFR3InfoEx(pVM->pUVM, pVCpu->idCpu, "cpumguest", "verbose", DBGFR3InfoLogRelHlp());
2050 RTLogRelPrintf("***\n");
2051 DBGFR3InfoEx(pVM->pUVM, pVCpu->idCpu, "cpumguesthwvirt", "verbose", DBGFR3InfoLogRelHlp());
2052 RTLogRelPrintf("***\n");
2053 DBGFR3InfoEx(pVM->pUVM, pVCpu->idCpu, "mode", NULL, DBGFR3InfoLogRelHlp());
2054 RTLogRelPrintf("***\n");
2055 RTLogRelSetBuffering(fOldBuffered);
2056 RTLogRelPrintf("************** End of Guest state at power off for VCpu %u ***************\n", pVCpu->idCpu);
2057 }
2058
2059 return VINF_EM_OFF;
2060}
2061
2062
2063/**
2064 * Power off the VM.
2065 *
2066 * @returns VBox status code. When called on EMT, this will be a strict status
2067 * code that has to be propagated up the call stack.
2068 *
2069 * @param pUVM The handle of the VM to be powered off.
2070 *
2071 * @thread Any thread.
2072 * @vmstate Suspended, Running, Guru Meditation, Load Failure
2073 * @vmstateto Off or OffLS
2074 */
2075VMMR3DECL(int) VMR3PowerOff(PUVM pUVM)
2076{
2077 LogFlow(("VMR3PowerOff: pUVM=%p\n", pUVM));
2078 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2079 PVM pVM = pUVM->pVM;
2080 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
2081
2082 /*
2083 * Gather all the EMTs to make sure there are no races before
2084 * changing the VM state.
2085 */
2086 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING | VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR,
2087 vmR3PowerOff, NULL);
2088 LogFlow(("VMR3PowerOff: returns %Rrc\n", rc));
2089 return rc;
2090}
2091
2092
2093/**
2094 * Destroys the VM.
2095 *
2096 * The VM must be powered off (or never really powered on) to call this
2097 * function. The VM handle is destroyed and can no longer be used up successful
2098 * return.
2099 *
2100 * @returns VBox status code.
2101 *
2102 * @param pUVM The user mode VM handle.
2103 *
2104 * @thread Any none emulation thread.
2105 * @vmstate Off, Created
2106 * @vmstateto N/A
2107 */
2108VMMR3DECL(int) VMR3Destroy(PUVM pUVM)
2109{
2110 LogFlow(("VMR3Destroy: pUVM=%p\n", pUVM));
2111
2112 /*
2113 * Validate input.
2114 */
2115 if (!pUVM)
2116 return VERR_INVALID_VM_HANDLE;
2117 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2118 PVM pVM = pUVM->pVM;
2119 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
2120 AssertLogRelReturn(!VM_IS_EMT(pVM), VERR_VM_THREAD_IS_EMT);
2121
2122 /*
2123 * Change VM state to destroying and aall vmR3Destroy on each of the EMTs
2124 * ending with EMT(0) doing the bulk of the cleanup.
2125 */
2126 int rc = vmR3TrySetState(pVM, "VMR3Destroy", 1, VMSTATE_DESTROYING, VMSTATE_OFF);
2127 if (RT_FAILURE(rc))
2128 return rc;
2129
2130 rc = VMR3ReqCallWait(pVM, VMCPUID_ALL_REVERSE, (PFNRT)vmR3Destroy, 1, pVM);
2131 AssertLogRelRC(rc);
2132
2133 /*
2134 * Wait for EMTs to quit and destroy the UVM.
2135 */
2136 vmR3DestroyUVM(pUVM, 30000);
2137
2138 LogFlow(("VMR3Destroy: returns VINF_SUCCESS\n"));
2139 return VINF_SUCCESS;
2140}
2141
2142
2143/**
2144 * Internal destruction worker.
2145 *
2146 * This is either called from VMR3Destroy via VMR3ReqCallU or from
2147 * vmR3EmulationThreadWithId when EMT(0) terminates after having called
2148 * VMR3Destroy().
2149 *
2150 * When called on EMT(0), it will performed the great bulk of the destruction.
2151 * When called on the other EMTs, they will do nothing and the whole purpose is
2152 * to return VINF_EM_TERMINATE so they break out of their run loops.
2153 *
2154 * @returns VINF_EM_TERMINATE.
2155 * @param pVM The cross context VM structure.
2156 */
2157DECLCALLBACK(int) vmR3Destroy(PVM pVM)
2158{
2159 PUVM pUVM = pVM->pUVM;
2160 PVMCPU pVCpu = VMMGetCpu(pVM);
2161 Assert(pVCpu);
2162 LogFlow(("vmR3Destroy: pVM=%p pUVM=%p pVCpu=%p idCpu=%u\n", pVM, pUVM, pVCpu, pVCpu->idCpu));
2163
2164 /*
2165 * Only VCPU 0 does the full cleanup (last).
2166 */
2167 if (pVCpu->idCpu == 0)
2168 {
2169 /*
2170 * Dump statistics to the log.
2171 */
2172#if defined(VBOX_WITH_STATISTICS) || defined(LOG_ENABLED)
2173 RTLogFlags(NULL, "nodisabled nobuffered");
2174#endif
2175//#ifdef VBOX_WITH_STATISTICS
2176// STAMR3Dump(pUVM, "*");
2177//#else
2178 LogRel(("************************* Statistics *************************\n"));
2179 STAMR3DumpToReleaseLog(pUVM, "*");
2180 LogRel(("********************* End of statistics **********************\n"));
2181//#endif
2182
2183 /*
2184 * Destroy the VM components.
2185 */
2186 int rc = TMR3Term(pVM);
2187 AssertRC(rc);
2188#ifdef VBOX_WITH_DEBUGGER
2189 rc = DBGCIoTerminate(pUVM, pUVM->vm.s.pvDBGC);
2190 pUVM->vm.s.pvDBGC = NULL;
2191#endif
2192 AssertRC(rc);
2193 rc = PDMR3Term(pVM);
2194 AssertRC(rc);
2195 rc = GIMR3Term(pVM);
2196 AssertRC(rc);
2197 rc = DBGFR3Term(pVM);
2198 AssertRC(rc);
2199 rc = IEMR3Term(pVM);
2200 AssertRC(rc);
2201 rc = EMR3Term(pVM);
2202 AssertRC(rc);
2203 rc = IOMR3Term(pVM);
2204 AssertRC(rc);
2205 rc = TRPMR3Term(pVM);
2206 AssertRC(rc);
2207 rc = SELMR3Term(pVM);
2208 AssertRC(rc);
2209 rc = HMR3Term(pVM);
2210 AssertRC(rc);
2211 rc = NEMR3Term(pVM);
2212 AssertRC(rc);
2213 rc = PGMR3Term(pVM);
2214 AssertRC(rc);
2215 rc = VMMR3Term(pVM); /* Terminates the ring-0 code! */
2216 AssertRC(rc);
2217 rc = CPUMR3Term(pVM);
2218 AssertRC(rc);
2219 SSMR3Term(pVM);
2220 rc = PDMR3CritSectBothTerm(pVM);
2221 AssertRC(rc);
2222 rc = MMR3Term(pVM);
2223 AssertRC(rc);
2224
2225 /*
2226 * We're done, tell the other EMTs to quit.
2227 */
2228 ASMAtomicUoWriteBool(&pUVM->vm.s.fTerminateEMT, true);
2229 ASMAtomicWriteU32(&pVM->fGlobalForcedActions, VM_FF_CHECK_VM_STATE); /* Can't hurt... */
2230 LogFlow(("vmR3Destroy: returning %Rrc\n", VINF_EM_TERMINATE));
2231 }
2232
2233 /*
2234 * Decrement the active EMT count here.
2235 */
2236 PUVMCPU pUVCpu = &pUVM->aCpus[pVCpu->idCpu];
2237 if (!pUVCpu->vm.s.fBeenThruVmDestroy)
2238 {
2239 pUVCpu->vm.s.fBeenThruVmDestroy = true;
2240 ASMAtomicDecU32(&pUVM->vm.s.cActiveEmts);
2241 }
2242 else
2243 AssertFailed();
2244
2245 return VINF_EM_TERMINATE;
2246}
2247
2248
2249/**
2250 * Destroys the UVM portion.
2251 *
2252 * This is called as the final step in the VM destruction or as the cleanup
2253 * in case of a creation failure.
2254 *
2255 * @param pUVM The user mode VM structure.
2256 * @param cMilliesEMTWait The number of milliseconds to wait for the emulation
2257 * threads.
2258 */
2259static void vmR3DestroyUVM(PUVM pUVM, uint32_t cMilliesEMTWait)
2260{
2261 /*
2262 * Signal termination of each the emulation threads and
2263 * wait for them to complete.
2264 */
2265 /* Signal them - in reverse order since EMT(0) waits for the others. */
2266 ASMAtomicUoWriteBool(&pUVM->vm.s.fTerminateEMT, true);
2267 if (pUVM->pVM)
2268 VM_FF_SET(pUVM->pVM, VM_FF_CHECK_VM_STATE); /* Can't hurt... */
2269 VMCPUID iCpu = pUVM->cCpus;
2270 while (iCpu-- > 0)
2271 {
2272 VMR3NotifyGlobalFFU(pUVM, VMNOTIFYFF_FLAGS_DONE_REM);
2273 RTSemEventSignal(pUVM->aCpus[iCpu].vm.s.EventSemWait);
2274 }
2275
2276 /* Wait for EMT(0), it in turn waits for the rest. */
2277 ASMAtomicUoWriteBool(&pUVM->vm.s.fTerminateEMT, true);
2278
2279 RTTHREAD const hSelf = RTThreadSelf();
2280 RTTHREAD hThread = pUVM->aCpus[0].vm.s.ThreadEMT;
2281 if ( hThread != NIL_RTTHREAD
2282 && hThread != hSelf)
2283 {
2284 int rc2 = RTThreadWait(hThread, RT_MAX(cMilliesEMTWait, 2000), NULL);
2285 if (rc2 == VERR_TIMEOUT) /* avoid the assertion when debugging. */
2286 rc2 = RTThreadWait(hThread, 1000, NULL);
2287 AssertLogRelMsgRC(rc2, ("iCpu=0 rc=%Rrc\n", rc2));
2288 if (RT_SUCCESS(rc2))
2289 pUVM->aCpus[0].vm.s.ThreadEMT = NIL_RTTHREAD;
2290 }
2291
2292 /* Just in case we're in a weird failure situation w/o EMT(0) to do the
2293 waiting, wait the other EMTs too. */
2294 for (iCpu = 1; iCpu < pUVM->cCpus; iCpu++)
2295 {
2296 ASMAtomicXchgHandle(&pUVM->aCpus[iCpu].vm.s.ThreadEMT, NIL_RTTHREAD, &hThread);
2297 if (hThread != NIL_RTTHREAD)
2298 {
2299 if (hThread != hSelf)
2300 {
2301 int rc2 = RTThreadWait(hThread, 250 /*ms*/, NULL);
2302 AssertLogRelMsgRC(rc2, ("iCpu=%u rc=%Rrc\n", iCpu, rc2));
2303 if (RT_SUCCESS(rc2))
2304 continue;
2305 }
2306 pUVM->aCpus[iCpu].vm.s.ThreadEMT = hThread;
2307 }
2308 }
2309
2310 /* Cleanup the semaphores. */
2311 iCpu = pUVM->cCpus;
2312 while (iCpu-- > 0)
2313 {
2314 RTSemEventDestroy(pUVM->aCpus[iCpu].vm.s.EventSemWait);
2315 pUVM->aCpus[iCpu].vm.s.EventSemWait = NIL_RTSEMEVENT;
2316 }
2317
2318 /*
2319 * Free the event semaphores associated with the request packets.
2320 */
2321 unsigned cReqs = 0;
2322 for (unsigned i = 0; i < RT_ELEMENTS(pUVM->vm.s.apReqFree); i++)
2323 {
2324 PVMREQ pReq = pUVM->vm.s.apReqFree[i];
2325 pUVM->vm.s.apReqFree[i] = NULL;
2326 for (; pReq; pReq = pReq->pNext, cReqs++)
2327 {
2328 pReq->enmState = VMREQSTATE_INVALID;
2329 RTSemEventDestroy(pReq->EventSem);
2330 }
2331 }
2332 Assert(cReqs == pUVM->vm.s.cReqFree); NOREF(cReqs);
2333
2334 /*
2335 * Kill all queued requests. (There really shouldn't be any!)
2336 */
2337 for (unsigned i = 0; i < 10; i++)
2338 {
2339 PVMREQ pReqHead = ASMAtomicXchgPtrT(&pUVM->vm.s.pPriorityReqs, NULL, PVMREQ);
2340 if (!pReqHead)
2341 {
2342 pReqHead = ASMAtomicXchgPtrT(&pUVM->vm.s.pNormalReqs, NULL, PVMREQ);
2343 if (!pReqHead)
2344 break;
2345 }
2346 AssertLogRelMsgFailed(("Requests pending! VMR3Destroy caller has to serialize this.\n"));
2347
2348 for (PVMREQ pReq = pReqHead; pReq; pReq = pReq->pNext)
2349 {
2350 ASMAtomicUoWriteS32(&pReq->iStatus, VERR_VM_REQUEST_KILLED);
2351 ASMAtomicWriteSize(&pReq->enmState, VMREQSTATE_INVALID);
2352 RTSemEventSignal(pReq->EventSem);
2353 RTThreadSleep(2);
2354 RTSemEventDestroy(pReq->EventSem);
2355 }
2356 /* give them a chance to respond before we free the request memory. */
2357 RTThreadSleep(32);
2358 }
2359
2360 /*
2361 * Now all queued VCPU requests (again, there shouldn't be any).
2362 */
2363 for (VMCPUID idCpu = 0; idCpu < pUVM->cCpus; idCpu++)
2364 {
2365 PUVMCPU pUVCpu = &pUVM->aCpus[idCpu];
2366
2367 for (unsigned i = 0; i < 10; i++)
2368 {
2369 PVMREQ pReqHead = ASMAtomicXchgPtrT(&pUVCpu->vm.s.pPriorityReqs, NULL, PVMREQ);
2370 if (!pReqHead)
2371 {
2372 pReqHead = ASMAtomicXchgPtrT(&pUVCpu->vm.s.pNormalReqs, NULL, PVMREQ);
2373 if (!pReqHead)
2374 break;
2375 }
2376 AssertLogRelMsgFailed(("Requests pending! VMR3Destroy caller has to serialize this.\n"));
2377
2378 for (PVMREQ pReq = pReqHead; pReq; pReq = pReq->pNext)
2379 {
2380 ASMAtomicUoWriteS32(&pReq->iStatus, VERR_VM_REQUEST_KILLED);
2381 ASMAtomicWriteSize(&pReq->enmState, VMREQSTATE_INVALID);
2382 RTSemEventSignal(pReq->EventSem);
2383 RTThreadSleep(2);
2384 RTSemEventDestroy(pReq->EventSem);
2385 }
2386 /* give them a chance to respond before we free the request memory. */
2387 RTThreadSleep(32);
2388 }
2389 }
2390
2391 /*
2392 * Make sure the VMMR0.r0 module and whatever else is unloaded.
2393 */
2394 PDMR3TermUVM(pUVM);
2395
2396 RTCritSectDelete(&pUVM->vm.s.AtErrorCritSect);
2397 RTCritSectDelete(&pUVM->vm.s.AtStateCritSect);
2398
2399 /*
2400 * Terminate the support library if initialized.
2401 */
2402 if (pUVM->vm.s.pSession)
2403 {
2404 int rc = SUPR3Term(false /*fForced*/);
2405 AssertRC(rc);
2406 pUVM->vm.s.pSession = NIL_RTR0PTR;
2407 }
2408
2409 /*
2410 * Release the UVM structure reference.
2411 */
2412 VMR3ReleaseUVM(pUVM);
2413
2414 /*
2415 * Clean up and flush logs.
2416 */
2417 RTLogFlush(NULL);
2418}
2419
2420
2421/**
2422 * Worker which checks integrity of some internal structures.
2423 * This is yet another attempt to track down that AVL tree crash.
2424 */
2425static void vmR3CheckIntegrity(PVM pVM)
2426{
2427#ifdef VBOX_STRICT
2428 int rc = PGMR3CheckIntegrity(pVM);
2429 AssertReleaseRC(rc);
2430#else
2431 RT_NOREF_PV(pVM);
2432#endif
2433}
2434
2435
2436/**
2437 * EMT rendezvous worker for VMR3ResetFF for doing soft/warm reset.
2438 *
2439 * @returns VERR_VM_INVALID_VM_STATE, VINF_EM_RESCHEDULE.
2440 * (This is a strict return code, see FNVMMEMTRENDEZVOUS.)
2441 *
2442 * @param pVM The cross context VM structure.
2443 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2444 * @param pvUser The reset flags.
2445 */
2446static DECLCALLBACK(VBOXSTRICTRC) vmR3SoftReset(PVM pVM, PVMCPU pVCpu, void *pvUser)
2447{
2448 uint32_t fResetFlags = *(uint32_t *)pvUser;
2449
2450
2451 /*
2452 * The first EMT will try change the state to resetting. If this fails,
2453 * we won't get called for the other EMTs.
2454 */
2455 if (pVCpu->idCpu == pVM->cCpus - 1)
2456 {
2457 int rc = vmR3TrySetState(pVM, "vmR3ResetSoft", 3,
2458 VMSTATE_SOFT_RESETTING, VMSTATE_RUNNING,
2459 VMSTATE_SOFT_RESETTING, VMSTATE_SUSPENDED,
2460 VMSTATE_SOFT_RESETTING_LS, VMSTATE_RUNNING_LS);
2461 if (RT_FAILURE(rc))
2462 return rc;
2463 pVM->vm.s.cResets++;
2464 pVM->vm.s.cSoftResets++;
2465 }
2466
2467 /*
2468 * Check the state.
2469 */
2470 VMSTATE enmVMState = VMR3GetState(pVM);
2471 AssertLogRelMsgReturn( enmVMState == VMSTATE_SOFT_RESETTING
2472 || enmVMState == VMSTATE_SOFT_RESETTING_LS,
2473 ("%s\n", VMR3GetStateName(enmVMState)),
2474 VERR_VM_UNEXPECTED_UNSTABLE_STATE);
2475
2476 /*
2477 * EMT(0) does the full cleanup *after* all the other EMTs has been
2478 * thru here and been told to enter the EMSTATE_WAIT_SIPI state.
2479 *
2480 * Because there are per-cpu reset routines and order may/is important,
2481 * the following sequence looks a bit ugly...
2482 */
2483
2484 /* Reset the VCpu state. */
2485 VMCPU_ASSERT_STATE(pVCpu, VMCPUSTATE_STARTED);
2486
2487 /*
2488 * Soft reset the VM components.
2489 */
2490 if (pVCpu->idCpu == 0)
2491 {
2492 PDMR3SoftReset(pVM, fResetFlags);
2493 TRPMR3Reset(pVM);
2494 CPUMR3Reset(pVM); /* This must come *after* PDM (due to APIC base MSR caching). */
2495 EMR3Reset(pVM);
2496 HMR3Reset(pVM); /* This must come *after* PATM, CSAM, CPUM, SELM and TRPM. */
2497 NEMR3Reset(pVM);
2498
2499 /*
2500 * Since EMT(0) is the last to go thru here, it will advance the state.
2501 * (Unlike vmR3HardReset we won't be doing any suspending of live
2502 * migration VMs here since memory is unchanged.)
2503 */
2504 PUVM pUVM = pVM->pUVM;
2505 RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
2506 enmVMState = pVM->enmVMState;
2507 if (enmVMState == VMSTATE_SOFT_RESETTING)
2508 {
2509 if (pUVM->vm.s.enmPrevVMState == VMSTATE_SUSPENDED)
2510 vmR3SetStateLocked(pVM, pUVM, VMSTATE_SUSPENDED, VMSTATE_SOFT_RESETTING, false /*fSetRatherThanClearFF*/);
2511 else
2512 vmR3SetStateLocked(pVM, pUVM, VMSTATE_RUNNING, VMSTATE_SOFT_RESETTING, false /*fSetRatherThanClearFF*/);
2513 }
2514 else
2515 vmR3SetStateLocked(pVM, pUVM, VMSTATE_RUNNING_LS, VMSTATE_SOFT_RESETTING_LS, false /*fSetRatherThanClearFF*/);
2516 RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
2517 }
2518
2519 return VINF_EM_RESCHEDULE;
2520}
2521
2522
2523/**
2524 * EMT rendezvous worker for VMR3Reset and VMR3ResetFF.
2525 *
2526 * This is called by the emulation threads as a response to the reset request
2527 * issued by VMR3Reset().
2528 *
2529 * @returns VERR_VM_INVALID_VM_STATE, VINF_EM_RESET or VINF_EM_SUSPEND. (This
2530 * is a strict return code, see FNVMMEMTRENDEZVOUS.)
2531 *
2532 * @param pVM The cross context VM structure.
2533 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2534 * @param pvUser Ignored.
2535 */
2536static DECLCALLBACK(VBOXSTRICTRC) vmR3HardReset(PVM pVM, PVMCPU pVCpu, void *pvUser)
2537{
2538 Assert(!pvUser); NOREF(pvUser);
2539
2540 /*
2541 * The first EMT will try change the state to resetting. If this fails,
2542 * we won't get called for the other EMTs.
2543 */
2544 if (pVCpu->idCpu == pVM->cCpus - 1)
2545 {
2546 int rc = vmR3TrySetState(pVM, "vmR3HardReset", 3,
2547 VMSTATE_RESETTING, VMSTATE_RUNNING,
2548 VMSTATE_RESETTING, VMSTATE_SUSPENDED,
2549 VMSTATE_RESETTING_LS, VMSTATE_RUNNING_LS);
2550 if (RT_FAILURE(rc))
2551 return rc;
2552 pVM->vm.s.cResets++;
2553 pVM->vm.s.cHardResets++;
2554 }
2555
2556 /*
2557 * Check the state.
2558 */
2559 VMSTATE enmVMState = VMR3GetState(pVM);
2560 AssertLogRelMsgReturn( enmVMState == VMSTATE_RESETTING
2561 || enmVMState == VMSTATE_RESETTING_LS,
2562 ("%s\n", VMR3GetStateName(enmVMState)),
2563 VERR_VM_UNEXPECTED_UNSTABLE_STATE);
2564
2565 /*
2566 * EMT(0) does the full cleanup *after* all the other EMTs has been
2567 * thru here and been told to enter the EMSTATE_WAIT_SIPI state.
2568 *
2569 * Because there are per-cpu reset routines and order may/is important,
2570 * the following sequence looks a bit ugly...
2571 */
2572 if (pVCpu->idCpu == 0)
2573 vmR3CheckIntegrity(pVM);
2574
2575 /* Reset the VCpu state. */
2576 VMCPU_ASSERT_STATE(pVCpu, VMCPUSTATE_STARTED);
2577
2578 /* Clear all pending forced actions. */
2579 VMCPU_FF_CLEAR_MASK(pVCpu, VMCPU_FF_ALL_MASK & ~VMCPU_FF_REQUEST);
2580
2581 /*
2582 * Reset the VM components.
2583 */
2584 if (pVCpu->idCpu == 0)
2585 {
2586 GIMR3Reset(pVM); /* This must come *before* PDM and TM. */
2587 PDMR3Reset(pVM);
2588 PGMR3Reset(pVM);
2589 SELMR3Reset(pVM);
2590 TRPMR3Reset(pVM);
2591 IOMR3Reset(pVM);
2592 CPUMR3Reset(pVM); /* This must come *after* PDM (due to APIC base MSR caching). */
2593 TMR3Reset(pVM);
2594 EMR3Reset(pVM);
2595 HMR3Reset(pVM); /* This must come *after* PATM, CSAM, CPUM, SELM and TRPM. */
2596 NEMR3Reset(pVM);
2597
2598 /*
2599 * Do memory setup.
2600 */
2601 PGMR3MemSetup(pVM, true /*fAtReset*/);
2602 PDMR3MemSetup(pVM, true /*fAtReset*/);
2603
2604 /*
2605 * Since EMT(0) is the last to go thru here, it will advance the state.
2606 * When a live save is active, we will move on to SuspendingLS but
2607 * leave it for VMR3Reset to do the actual suspending due to deadlock risks.
2608 */
2609 PUVM pUVM = pVM->pUVM;
2610 RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
2611 enmVMState = pVM->enmVMState;
2612 if (enmVMState == VMSTATE_RESETTING)
2613 {
2614 if (pUVM->vm.s.enmPrevVMState == VMSTATE_SUSPENDED)
2615 vmR3SetStateLocked(pVM, pUVM, VMSTATE_SUSPENDED, VMSTATE_RESETTING, false /*fSetRatherThanClearFF*/);
2616 else
2617 vmR3SetStateLocked(pVM, pUVM, VMSTATE_RUNNING, VMSTATE_RESETTING, false /*fSetRatherThanClearFF*/);
2618 }
2619 else
2620 vmR3SetStateLocked(pVM, pUVM, VMSTATE_SUSPENDING_LS, VMSTATE_RESETTING_LS, false /*fSetRatherThanClearFF*/);
2621 RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
2622
2623 vmR3CheckIntegrity(pVM);
2624
2625 /*
2626 * Do the suspend bit as well.
2627 * It only requires some EMT(0) work at present.
2628 */
2629 if (enmVMState != VMSTATE_RESETTING)
2630 {
2631 vmR3SuspendDoWork(pVM);
2632 vmR3SetState(pVM, VMSTATE_SUSPENDED_LS, VMSTATE_SUSPENDING_LS);
2633 }
2634 }
2635
2636 return enmVMState == VMSTATE_RESETTING
2637 ? VINF_EM_RESET
2638 : 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. */
2639}
2640
2641
2642/**
2643 * Internal worker for VMR3Reset, VMR3ResetFF, VMR3TripleFault.
2644 *
2645 * @returns VBox status code.
2646 * @param pVM The cross context VM structure.
2647 * @param fHardReset Whether it's a hard reset or not.
2648 * @param fResetFlags The reset flags (PDMVMRESET_F_XXX).
2649 */
2650static VBOXSTRICTRC vmR3ResetCommon(PVM pVM, bool fHardReset, uint32_t fResetFlags)
2651{
2652 LogFlow(("vmR3ResetCommon: fHardReset=%RTbool fResetFlags=%#x\n", fHardReset, fResetFlags));
2653 int rc;
2654 if (fHardReset)
2655 {
2656 /*
2657 * Hard reset.
2658 */
2659 /* Check whether we're supposed to power off instead of resetting. */
2660 if (pVM->vm.s.fPowerOffInsteadOfReset)
2661 {
2662 PUVM pUVM = pVM->pUVM;
2663 if ( pUVM->pVmm2UserMethods
2664 && pUVM->pVmm2UserMethods->pfnNotifyResetTurnedIntoPowerOff)
2665 pUVM->pVmm2UserMethods->pfnNotifyResetTurnedIntoPowerOff(pUVM->pVmm2UserMethods, pUVM);
2666 return VMR3PowerOff(pUVM);
2667 }
2668
2669 /* Gather all the EMTs to make sure there are no races before changing
2670 the VM state. */
2671 rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING | VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR,
2672 vmR3HardReset, NULL);
2673 }
2674 else
2675 {
2676 /*
2677 * Soft reset. Since we only support this with a single CPU active,
2678 * we must be on EMT #0 here.
2679 */
2680 VM_ASSERT_EMT0(pVM);
2681 rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING | VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR,
2682 vmR3SoftReset, &fResetFlags);
2683 }
2684
2685 LogFlow(("vmR3ResetCommon: returns %Rrc\n", rc));
2686 return rc;
2687}
2688
2689
2690
2691/**
2692 * Reset the current VM.
2693 *
2694 * @returns VBox status code.
2695 * @param pUVM The VM to reset.
2696 */
2697VMMR3DECL(int) VMR3Reset(PUVM pUVM)
2698{
2699 LogFlow(("VMR3Reset:\n"));
2700 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2701 PVM pVM = pUVM->pVM;
2702 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
2703
2704 return VBOXSTRICTRC_VAL(vmR3ResetCommon(pVM, true, 0));
2705}
2706
2707
2708/**
2709 * Handle the reset force flag or triple fault.
2710 *
2711 * This handles both soft and hard resets (see PDMVMRESET_F_XXX).
2712 *
2713 * @returns VBox status code.
2714 * @param pVM The cross context VM structure.
2715 * @thread EMT
2716 *
2717 * @remarks Caller is expected to clear the VM_FF_RESET force flag.
2718 */
2719VMMR3_INT_DECL(VBOXSTRICTRC) VMR3ResetFF(PVM pVM)
2720{
2721 LogFlow(("VMR3ResetFF:\n"));
2722
2723 /*
2724 * First consult the firmware on whether this is a hard or soft reset.
2725 */
2726 uint32_t fResetFlags;
2727 bool fHardReset = PDMR3GetResetInfo(pVM, 0 /*fOverride*/, &fResetFlags);
2728 return vmR3ResetCommon(pVM, fHardReset, fResetFlags);
2729}
2730
2731
2732/**
2733 * For handling a CPU reset on triple fault.
2734 *
2735 * According to one mainboard manual, a CPU triple fault causes the 286 CPU to
2736 * send a SHUTDOWN signal to the chipset. The chipset responds by sending a
2737 * RESET signal to the CPU. So, it should be very similar to a soft/warm reset.
2738 *
2739 * @returns VBox status code.
2740 * @param pVM The cross context VM structure.
2741 * @thread EMT
2742 */
2743VMMR3_INT_DECL(VBOXSTRICTRC) VMR3ResetTripleFault(PVM pVM)
2744{
2745 LogFlow(("VMR3ResetTripleFault:\n"));
2746
2747 /*
2748 * First consult the firmware on whether this is a hard or soft reset.
2749 */
2750 uint32_t fResetFlags;
2751 bool fHardReset = PDMR3GetResetInfo(pVM, PDMVMRESET_F_TRIPLE_FAULT, &fResetFlags);
2752 return vmR3ResetCommon(pVM, fHardReset, fResetFlags);
2753}
2754
2755
2756/**
2757 * Gets the user mode VM structure pointer given Pointer to the VM.
2758 *
2759 * @returns Pointer to the user mode VM structure on success. NULL if @a pVM is
2760 * invalid (asserted).
2761 * @param pVM The cross context VM structure.
2762 * @sa VMR3GetVM, VMR3RetainUVM
2763 */
2764VMMR3DECL(PUVM) VMR3GetUVM(PVM pVM)
2765{
2766 VM_ASSERT_VALID_EXT_RETURN(pVM, NULL);
2767 return pVM->pUVM;
2768}
2769
2770
2771/**
2772 * Gets the shared VM structure pointer given the pointer to the user mode VM
2773 * structure.
2774 *
2775 * @returns Pointer to the VM.
2776 * NULL if @a pUVM is invalid (asserted) or if no shared VM structure
2777 * is currently associated with it.
2778 * @param pUVM The user mode VM handle.
2779 * @sa VMR3GetUVM
2780 */
2781VMMR3DECL(PVM) VMR3GetVM(PUVM pUVM)
2782{
2783 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NULL);
2784 return pUVM->pVM;
2785}
2786
2787
2788/**
2789 * Retain the user mode VM handle.
2790 *
2791 * @returns Reference count.
2792 * UINT32_MAX if @a pUVM is invalid.
2793 *
2794 * @param pUVM The user mode VM handle.
2795 * @sa VMR3ReleaseUVM
2796 */
2797VMMR3DECL(uint32_t) VMR3RetainUVM(PUVM pUVM)
2798{
2799 UVM_ASSERT_VALID_EXT_RETURN(pUVM, UINT32_MAX);
2800 uint32_t cRefs = ASMAtomicIncU32(&pUVM->vm.s.cUvmRefs);
2801 AssertMsg(cRefs > 0 && cRefs < _64K, ("%u\n", cRefs));
2802 return cRefs;
2803}
2804
2805
2806/**
2807 * Does the final release of the UVM structure.
2808 *
2809 * @param pUVM The user mode VM handle.
2810 */
2811static void vmR3DoReleaseUVM(PUVM pUVM)
2812{
2813 /*
2814 * Free the UVM.
2815 */
2816 Assert(!pUVM->pVM);
2817
2818 MMR3HeapFree(pUVM->vm.s.pszName);
2819 pUVM->vm.s.pszName = NULL;
2820
2821 MMR3TermUVM(pUVM);
2822 STAMR3TermUVM(pUVM);
2823
2824 ASMAtomicUoWriteU32(&pUVM->u32Magic, UINT32_MAX);
2825 RTTlsFree(pUVM->vm.s.idxTLS);
2826 RTMemPageFree(pUVM, RT_UOFFSETOF_DYN(UVM, aCpus[pUVM->cCpus]));
2827}
2828
2829
2830/**
2831 * Releases a refernece to the mode VM handle.
2832 *
2833 * @returns The new reference count, 0 if destroyed.
2834 * UINT32_MAX if @a pUVM is invalid.
2835 *
2836 * @param pUVM The user mode VM handle.
2837 * @sa VMR3RetainUVM
2838 */
2839VMMR3DECL(uint32_t) VMR3ReleaseUVM(PUVM pUVM)
2840{
2841 if (!pUVM)
2842 return 0;
2843 UVM_ASSERT_VALID_EXT_RETURN(pUVM, UINT32_MAX);
2844 uint32_t cRefs = ASMAtomicDecU32(&pUVM->vm.s.cUvmRefs);
2845 if (!cRefs)
2846 vmR3DoReleaseUVM(pUVM);
2847 else
2848 AssertMsg(cRefs < _64K, ("%u\n", cRefs));
2849 return cRefs;
2850}
2851
2852
2853/**
2854 * Gets the VM name.
2855 *
2856 * @returns Pointer to a read-only string containing the name. NULL if called
2857 * too early.
2858 * @param pUVM The user mode VM handle.
2859 */
2860VMMR3DECL(const char *) VMR3GetName(PUVM pUVM)
2861{
2862 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NULL);
2863 return pUVM->vm.s.pszName;
2864}
2865
2866
2867/**
2868 * Gets the VM UUID.
2869 *
2870 * @returns pUuid on success, NULL on failure.
2871 * @param pUVM The user mode VM handle.
2872 * @param pUuid Where to store the UUID.
2873 */
2874VMMR3DECL(PRTUUID) VMR3GetUuid(PUVM pUVM, PRTUUID pUuid)
2875{
2876 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NULL);
2877 AssertPtrReturn(pUuid, NULL);
2878
2879 *pUuid = pUVM->vm.s.Uuid;
2880 return pUuid;
2881}
2882
2883
2884/**
2885 * Gets the current VM state.
2886 *
2887 * @returns The current VM state.
2888 * @param pVM The cross context VM structure.
2889 * @thread Any
2890 */
2891VMMR3DECL(VMSTATE) VMR3GetState(PVM pVM)
2892{
2893 AssertMsgReturn(RT_VALID_ALIGNED_PTR(pVM, HOST_PAGE_SIZE), ("%p\n", pVM), VMSTATE_TERMINATED);
2894 VMSTATE enmVMState = pVM->enmVMState;
2895 return enmVMState >= VMSTATE_CREATING && enmVMState <= VMSTATE_TERMINATED ? enmVMState : VMSTATE_TERMINATED;
2896}
2897
2898
2899/**
2900 * Gets the current VM state.
2901 *
2902 * @returns The current VM state.
2903 * @param pUVM The user-mode VM handle.
2904 * @thread Any
2905 */
2906VMMR3DECL(VMSTATE) VMR3GetStateU(PUVM pUVM)
2907{
2908 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VMSTATE_TERMINATED);
2909 if (RT_UNLIKELY(!pUVM->pVM))
2910 return VMSTATE_TERMINATED;
2911 return pUVM->pVM->enmVMState;
2912}
2913
2914
2915/**
2916 * Gets the state name string for a VM state.
2917 *
2918 * @returns Pointer to the state name. (readonly)
2919 * @param enmState The state.
2920 */
2921VMMR3DECL(const char *) VMR3GetStateName(VMSTATE enmState)
2922{
2923 switch (enmState)
2924 {
2925 case VMSTATE_CREATING: return "CREATING";
2926 case VMSTATE_CREATED: return "CREATED";
2927 case VMSTATE_LOADING: return "LOADING";
2928 case VMSTATE_POWERING_ON: return "POWERING_ON";
2929 case VMSTATE_RESUMING: return "RESUMING";
2930 case VMSTATE_RUNNING: return "RUNNING";
2931 case VMSTATE_RUNNING_LS: return "RUNNING_LS";
2932 case VMSTATE_RESETTING: return "RESETTING";
2933 case VMSTATE_RESETTING_LS: return "RESETTING_LS";
2934 case VMSTATE_SOFT_RESETTING: return "SOFT_RESETTING";
2935 case VMSTATE_SOFT_RESETTING_LS: return "SOFT_RESETTING_LS";
2936 case VMSTATE_SUSPENDED: return "SUSPENDED";
2937 case VMSTATE_SUSPENDED_LS: return "SUSPENDED_LS";
2938 case VMSTATE_SUSPENDED_EXT_LS: return "SUSPENDED_EXT_LS";
2939 case VMSTATE_SUSPENDING: return "SUSPENDING";
2940 case VMSTATE_SUSPENDING_LS: return "SUSPENDING_LS";
2941 case VMSTATE_SUSPENDING_EXT_LS: return "SUSPENDING_EXT_LS";
2942 case VMSTATE_SAVING: return "SAVING";
2943 case VMSTATE_DEBUGGING: return "DEBUGGING";
2944 case VMSTATE_DEBUGGING_LS: return "DEBUGGING_LS";
2945 case VMSTATE_POWERING_OFF: return "POWERING_OFF";
2946 case VMSTATE_POWERING_OFF_LS: return "POWERING_OFF_LS";
2947 case VMSTATE_FATAL_ERROR: return "FATAL_ERROR";
2948 case VMSTATE_FATAL_ERROR_LS: return "FATAL_ERROR_LS";
2949 case VMSTATE_GURU_MEDITATION: return "GURU_MEDITATION";
2950 case VMSTATE_GURU_MEDITATION_LS:return "GURU_MEDITATION_LS";
2951 case VMSTATE_LOAD_FAILURE: return "LOAD_FAILURE";
2952 case VMSTATE_OFF: return "OFF";
2953 case VMSTATE_OFF_LS: return "OFF_LS";
2954 case VMSTATE_DESTROYING: return "DESTROYING";
2955 case VMSTATE_TERMINATED: return "TERMINATED";
2956
2957 default:
2958 AssertMsgFailed(("Unknown state %d\n", enmState));
2959 return "Unknown!\n";
2960 }
2961}
2962
2963
2964/**
2965 * Validates the state transition in strict builds.
2966 *
2967 * @returns true if valid, false if not.
2968 *
2969 * @param enmStateOld The old (current) state.
2970 * @param enmStateNew The proposed new state.
2971 *
2972 * @remarks The reference for this is found in doc/vp/VMM.vpp, the VMSTATE
2973 * diagram (under State Machine Diagram).
2974 */
2975static bool vmR3ValidateStateTransition(VMSTATE enmStateOld, VMSTATE enmStateNew)
2976{
2977#ifndef VBOX_STRICT
2978 RT_NOREF2(enmStateOld, enmStateNew);
2979#else
2980 switch (enmStateOld)
2981 {
2982 case VMSTATE_CREATING:
2983 AssertMsgReturn(enmStateNew == VMSTATE_CREATED, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
2984 break;
2985
2986 case VMSTATE_CREATED:
2987 AssertMsgReturn( enmStateNew == VMSTATE_LOADING
2988 || enmStateNew == VMSTATE_POWERING_ON
2989 || enmStateNew == VMSTATE_POWERING_OFF
2990 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
2991 break;
2992
2993 case VMSTATE_LOADING:
2994 AssertMsgReturn( enmStateNew == VMSTATE_SUSPENDED
2995 || enmStateNew == VMSTATE_LOAD_FAILURE
2996 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
2997 break;
2998
2999 case VMSTATE_POWERING_ON:
3000 AssertMsgReturn( enmStateNew == VMSTATE_RUNNING
3001 /*|| enmStateNew == VMSTATE_FATAL_ERROR ?*/
3002 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3003 break;
3004
3005 case VMSTATE_RESUMING:
3006 AssertMsgReturn( enmStateNew == VMSTATE_RUNNING
3007 /*|| enmStateNew == VMSTATE_FATAL_ERROR ?*/
3008 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3009 break;
3010
3011 case VMSTATE_RUNNING:
3012 AssertMsgReturn( enmStateNew == VMSTATE_POWERING_OFF
3013 || enmStateNew == VMSTATE_SUSPENDING
3014 || enmStateNew == VMSTATE_RESETTING
3015 || enmStateNew == VMSTATE_SOFT_RESETTING
3016 || enmStateNew == VMSTATE_RUNNING_LS
3017 || enmStateNew == VMSTATE_DEBUGGING
3018 || enmStateNew == VMSTATE_FATAL_ERROR
3019 || enmStateNew == VMSTATE_GURU_MEDITATION
3020 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3021 break;
3022
3023 case VMSTATE_RUNNING_LS:
3024 AssertMsgReturn( enmStateNew == VMSTATE_POWERING_OFF_LS
3025 || enmStateNew == VMSTATE_SUSPENDING_LS
3026 || enmStateNew == VMSTATE_SUSPENDING_EXT_LS
3027 || enmStateNew == VMSTATE_RESETTING_LS
3028 || enmStateNew == VMSTATE_SOFT_RESETTING_LS
3029 || enmStateNew == VMSTATE_RUNNING
3030 || enmStateNew == VMSTATE_DEBUGGING_LS
3031 || enmStateNew == VMSTATE_FATAL_ERROR_LS
3032 || enmStateNew == VMSTATE_GURU_MEDITATION_LS
3033 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3034 break;
3035
3036 case VMSTATE_RESETTING:
3037 AssertMsgReturn(enmStateNew == VMSTATE_RUNNING, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3038 break;
3039
3040 case VMSTATE_SOFT_RESETTING:
3041 AssertMsgReturn(enmStateNew == VMSTATE_RUNNING, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3042 break;
3043
3044 case VMSTATE_RESETTING_LS:
3045 AssertMsgReturn( enmStateNew == VMSTATE_SUSPENDING_LS
3046 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3047 break;
3048
3049 case VMSTATE_SOFT_RESETTING_LS:
3050 AssertMsgReturn( enmStateNew == VMSTATE_RUNNING_LS
3051 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3052 break;
3053
3054 case VMSTATE_SUSPENDING:
3055 AssertMsgReturn(enmStateNew == VMSTATE_SUSPENDED, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3056 break;
3057
3058 case VMSTATE_SUSPENDING_LS:
3059 AssertMsgReturn( enmStateNew == VMSTATE_SUSPENDING
3060 || enmStateNew == VMSTATE_SUSPENDED_LS
3061 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3062 break;
3063
3064 case VMSTATE_SUSPENDING_EXT_LS:
3065 AssertMsgReturn( enmStateNew == VMSTATE_SUSPENDING
3066 || enmStateNew == VMSTATE_SUSPENDED_EXT_LS
3067 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3068 break;
3069
3070 case VMSTATE_SUSPENDED:
3071 AssertMsgReturn( enmStateNew == VMSTATE_POWERING_OFF
3072 || enmStateNew == VMSTATE_SAVING
3073 || enmStateNew == VMSTATE_RESETTING
3074 || enmStateNew == VMSTATE_SOFT_RESETTING
3075 || enmStateNew == VMSTATE_RESUMING
3076 || enmStateNew == VMSTATE_LOADING
3077 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3078 break;
3079
3080 case VMSTATE_SUSPENDED_LS:
3081 AssertMsgReturn( enmStateNew == VMSTATE_SUSPENDED
3082 || enmStateNew == VMSTATE_SAVING
3083 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3084 break;
3085
3086 case VMSTATE_SUSPENDED_EXT_LS:
3087 AssertMsgReturn( enmStateNew == VMSTATE_SUSPENDED
3088 || enmStateNew == VMSTATE_SAVING
3089 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3090 break;
3091
3092 case VMSTATE_SAVING:
3093 AssertMsgReturn(enmStateNew == VMSTATE_SUSPENDED, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3094 break;
3095
3096 case VMSTATE_DEBUGGING:
3097 AssertMsgReturn( enmStateNew == VMSTATE_RUNNING
3098 || enmStateNew == VMSTATE_POWERING_OFF
3099 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3100 break;
3101
3102 case VMSTATE_DEBUGGING_LS:
3103 AssertMsgReturn( enmStateNew == VMSTATE_DEBUGGING
3104 || enmStateNew == VMSTATE_RUNNING_LS
3105 || enmStateNew == VMSTATE_POWERING_OFF_LS
3106 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3107 break;
3108
3109 case VMSTATE_POWERING_OFF:
3110 AssertMsgReturn(enmStateNew == VMSTATE_OFF, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3111 break;
3112
3113 case VMSTATE_POWERING_OFF_LS:
3114 AssertMsgReturn( enmStateNew == VMSTATE_POWERING_OFF
3115 || enmStateNew == VMSTATE_OFF_LS
3116 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3117 break;
3118
3119 case VMSTATE_OFF:
3120 AssertMsgReturn(enmStateNew == VMSTATE_DESTROYING, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3121 break;
3122
3123 case VMSTATE_OFF_LS:
3124 AssertMsgReturn(enmStateNew == VMSTATE_OFF, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3125 break;
3126
3127 case VMSTATE_FATAL_ERROR:
3128 AssertMsgReturn(enmStateNew == VMSTATE_POWERING_OFF, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3129 break;
3130
3131 case VMSTATE_FATAL_ERROR_LS:
3132 AssertMsgReturn( enmStateNew == VMSTATE_FATAL_ERROR
3133 || enmStateNew == VMSTATE_POWERING_OFF_LS
3134 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3135 break;
3136
3137 case VMSTATE_GURU_MEDITATION:
3138 AssertMsgReturn( enmStateNew == VMSTATE_DEBUGGING
3139 || enmStateNew == VMSTATE_POWERING_OFF
3140 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3141 break;
3142
3143 case VMSTATE_GURU_MEDITATION_LS:
3144 AssertMsgReturn( enmStateNew == VMSTATE_GURU_MEDITATION
3145 || enmStateNew == VMSTATE_DEBUGGING_LS
3146 || enmStateNew == VMSTATE_POWERING_OFF_LS
3147 , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3148 break;
3149
3150 case VMSTATE_LOAD_FAILURE:
3151 AssertMsgReturn(enmStateNew == VMSTATE_POWERING_OFF, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3152 break;
3153
3154 case VMSTATE_DESTROYING:
3155 AssertMsgReturn(enmStateNew == VMSTATE_TERMINATED, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3156 break;
3157
3158 case VMSTATE_TERMINATED:
3159 default:
3160 AssertMsgFailedReturn(("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
3161 break;
3162 }
3163#endif /* VBOX_STRICT */
3164 return true;
3165}
3166
3167
3168/**
3169 * Does the state change callouts.
3170 *
3171 * The caller owns the AtStateCritSect.
3172 *
3173 * @param pVM The cross context VM structure.
3174 * @param pUVM The UVM handle.
3175 * @param enmStateNew The New state.
3176 * @param enmStateOld The old state.
3177 */
3178static void vmR3DoAtState(PVM pVM, PUVM pUVM, VMSTATE enmStateNew, VMSTATE enmStateOld)
3179{
3180 LogRel(("Changing the VM state from '%s' to '%s'\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)));
3181
3182 for (PVMATSTATE pCur = pUVM->vm.s.pAtState; pCur; pCur = pCur->pNext)
3183 {
3184 pCur->pfnAtState(pUVM, VMMR3GetVTable(), enmStateNew, enmStateOld, pCur->pvUser);
3185 if ( enmStateNew != VMSTATE_DESTROYING
3186 && pVM->enmVMState == VMSTATE_DESTROYING)
3187 break;
3188 AssertMsg(pVM->enmVMState == enmStateNew,
3189 ("You are not allowed to change the state while in the change callback, except "
3190 "from destroying the VM. There are restrictions in the way the state changes "
3191 "are propagated up to the EM execution loop and it makes the program flow very "
3192 "difficult to follow. (%s, expected %s, old %s)\n",
3193 VMR3GetStateName(pVM->enmVMState), VMR3GetStateName(enmStateNew),
3194 VMR3GetStateName(enmStateOld)));
3195 }
3196}
3197
3198
3199/**
3200 * Sets the current VM state, with the AtStatCritSect already entered.
3201 *
3202 * @param pVM The cross context VM structure.
3203 * @param pUVM The UVM handle.
3204 * @param enmStateNew The new state.
3205 * @param enmStateOld The old state.
3206 * @param fSetRatherThanClearFF The usual behavior is to clear the
3207 * VM_FF_CHECK_VM_STATE force flag, but for
3208 * some transitions (-> guru) we need to kick
3209 * the other EMTs to stop what they're doing.
3210 */
3211static void vmR3SetStateLocked(PVM pVM, PUVM pUVM, VMSTATE enmStateNew, VMSTATE enmStateOld, bool fSetRatherThanClearFF)
3212{
3213 vmR3ValidateStateTransition(enmStateOld, enmStateNew);
3214
3215 AssertMsg(pVM->enmVMState == enmStateOld,
3216 ("%s != %s\n", VMR3GetStateName(pVM->enmVMState), VMR3GetStateName(enmStateOld)));
3217
3218 pUVM->vm.s.enmPrevVMState = enmStateOld;
3219 pVM->enmVMState = enmStateNew;
3220
3221 if (!fSetRatherThanClearFF)
3222 VM_FF_CLEAR(pVM, VM_FF_CHECK_VM_STATE);
3223 else if (pVM->cCpus > 0)
3224 VM_FF_SET(pVM, VM_FF_CHECK_VM_STATE);
3225
3226 vmR3DoAtState(pVM, pUVM, enmStateNew, enmStateOld);
3227}
3228
3229
3230/**
3231 * Sets the current VM state.
3232 *
3233 * @param pVM The cross context VM structure.
3234 * @param enmStateNew The new state.
3235 * @param enmStateOld The old state (for asserting only).
3236 */
3237static void vmR3SetState(PVM pVM, VMSTATE enmStateNew, VMSTATE enmStateOld)
3238{
3239 PUVM pUVM = pVM->pUVM;
3240 RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
3241
3242 RT_NOREF_PV(enmStateOld);
3243 AssertMsg(pVM->enmVMState == enmStateOld,
3244 ("%s != %s\n", VMR3GetStateName(pVM->enmVMState), VMR3GetStateName(enmStateOld)));
3245 vmR3SetStateLocked(pVM, pUVM, enmStateNew, pVM->enmVMState, false /*fSetRatherThanClearFF*/);
3246
3247 RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
3248}
3249
3250
3251/**
3252 * Tries to perform a state transition.
3253 *
3254 * @returns The 1-based ordinal of the succeeding transition.
3255 * VERR_VM_INVALID_VM_STATE and Assert+LogRel on failure.
3256 *
3257 * @param pVM The cross context VM structure.
3258 * @param pszWho Who is trying to change it.
3259 * @param cTransitions The number of transitions in the ellipsis.
3260 * @param ... Transition pairs; new, old.
3261 */
3262static int vmR3TrySetState(PVM pVM, const char *pszWho, unsigned cTransitions, ...)
3263{
3264 va_list va;
3265 VMSTATE enmStateNew = VMSTATE_CREATED;
3266 VMSTATE enmStateOld = VMSTATE_CREATED;
3267
3268#ifdef VBOX_STRICT
3269 /*
3270 * Validate the input first.
3271 */
3272 va_start(va, cTransitions);
3273 for (unsigned i = 0; i < cTransitions; i++)
3274 {
3275 enmStateNew = (VMSTATE)va_arg(va, /*VMSTATE*/int);
3276 enmStateOld = (VMSTATE)va_arg(va, /*VMSTATE*/int);
3277 vmR3ValidateStateTransition(enmStateOld, enmStateNew);
3278 }
3279 va_end(va);
3280#endif
3281
3282 /*
3283 * Grab the lock and see if any of the proposed transitions works out.
3284 */
3285 va_start(va, cTransitions);
3286 int rc = VERR_VM_INVALID_VM_STATE;
3287 PUVM pUVM = pVM->pUVM;
3288 RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
3289
3290 VMSTATE enmStateCur = pVM->enmVMState;
3291
3292 for (unsigned i = 0; i < cTransitions; i++)
3293 {
3294 enmStateNew = (VMSTATE)va_arg(va, /*VMSTATE*/int);
3295 enmStateOld = (VMSTATE)va_arg(va, /*VMSTATE*/int);
3296 if (enmStateCur == enmStateOld)
3297 {
3298 vmR3SetStateLocked(pVM, pUVM, enmStateNew, enmStateOld, false /*fSetRatherThanClearFF*/);
3299 rc = i + 1;
3300 break;
3301 }
3302 }
3303
3304 if (RT_FAILURE(rc))
3305 {
3306 /*
3307 * Complain about it.
3308 */
3309 const char * const pszStateCur = VMR3GetStateName(enmStateCur);
3310 if (cTransitions == 1)
3311 {
3312 LogRel(("%s: %s -> %s failed, because the VM state is actually %s!\n",
3313 pszWho, VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew), pszStateCur));
3314 VMSetError(pVM, VERR_VM_INVALID_VM_STATE, RT_SRC_POS, N_("%s failed because the VM state is %s instead of %s"),
3315 pszWho, pszStateCur, VMR3GetStateName(enmStateOld));
3316 AssertMsgFailed(("%s: %s -> %s failed, because the VM state is actually %s\n",
3317 pszWho, VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew), pszStateCur));
3318 }
3319 else
3320 {
3321 char szTransitions[4096];
3322 size_t cchTransitions = 0;
3323 szTransitions[0] = '\0';
3324 va_end(va);
3325 va_start(va, cTransitions);
3326 for (unsigned i = 0; i < cTransitions; i++)
3327 {
3328 enmStateNew = (VMSTATE)va_arg(va, /*VMSTATE*/int);
3329 enmStateOld = (VMSTATE)va_arg(va, /*VMSTATE*/int);
3330 const char * const pszStateNew = VMR3GetStateName(enmStateNew);
3331 const char * const pszStateOld = VMR3GetStateName(enmStateOld);
3332 LogRel(("%s%s -> %s", i ? ", " : " ", pszStateOld, pszStateNew));
3333 cchTransitions += RTStrPrintf(&szTransitions[cchTransitions], sizeof(szTransitions) - cchTransitions,
3334 "%s%s -> %s", i ? ", " : " ", pszStateOld, pszStateNew);
3335 }
3336 Assert(cchTransitions < sizeof(szTransitions) - 64);
3337
3338 LogRel(("%s: %s failed, because the VM state is actually %s!\n", pszWho, szTransitions, pszStateCur));
3339 VMSetError(pVM, VERR_VM_INVALID_VM_STATE, RT_SRC_POS,
3340 N_("%s failed because the current VM state, %s, was not found in the state transition table (%s)"),
3341 pszWho, pszStateCur, szTransitions);
3342 AssertMsgFailed(("%s - state=%s, transitions: %s. Check the cTransitions passed us.\n",
3343 pszWho, pszStateCur, szTransitions));
3344 }
3345 }
3346
3347 RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
3348 va_end(va);
3349 Assert(rc > 0 || rc < 0);
3350 return rc;
3351}
3352
3353
3354/**
3355 * Interface used by EM to signal that it's entering the guru meditation state.
3356 *
3357 * This will notifying other threads.
3358 *
3359 * @returns true if the state changed to Guru, false if no state change.
3360 * @param pVM The cross context VM structure.
3361 */
3362VMMR3_INT_DECL(bool) VMR3SetGuruMeditation(PVM pVM)
3363{
3364 PUVM pUVM = pVM->pUVM;
3365 RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
3366
3367 VMSTATE enmStateCur = pVM->enmVMState;
3368 bool fRc = true;
3369 if (enmStateCur == VMSTATE_RUNNING)
3370 vmR3SetStateLocked(pVM, pUVM, VMSTATE_GURU_MEDITATION, VMSTATE_RUNNING, true /*fSetRatherThanClearFF*/);
3371 else if (enmStateCur == VMSTATE_RUNNING_LS)
3372 {
3373 vmR3SetStateLocked(pVM, pUVM, VMSTATE_GURU_MEDITATION_LS, VMSTATE_RUNNING_LS, true /*fSetRatherThanClearFF*/);
3374 SSMR3Cancel(pUVM);
3375 }
3376 else
3377 fRc = false;
3378
3379 RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
3380 return fRc;
3381}
3382
3383
3384/**
3385 * Called by vmR3EmulationThreadWithId just before the VM structure is freed.
3386 *
3387 * @param pVM The cross context VM structure.
3388 */
3389void vmR3SetTerminated(PVM pVM)
3390{
3391 vmR3SetState(pVM, VMSTATE_TERMINATED, VMSTATE_DESTROYING);
3392}
3393
3394
3395/**
3396 * Checks if the VM was teleported and hasn't been fully resumed yet.
3397 *
3398 * This applies to both sides of the teleportation since we may leave a working
3399 * clone behind and the user is allowed to resume this...
3400 *
3401 * @returns true / false.
3402 * @param pVM The cross context VM structure.
3403 * @thread Any thread.
3404 */
3405VMMR3_INT_DECL(bool) VMR3TeleportedAndNotFullyResumedYet(PVM pVM)
3406{
3407 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
3408 return pVM->vm.s.fTeleportedAndNotFullyResumedYet;
3409}
3410
3411
3412/**
3413 * Registers a VM state change callback.
3414 *
3415 * You are not allowed to call any function which changes the VM state from a
3416 * state callback.
3417 *
3418 * @returns VBox status code.
3419 * @param pUVM The VM handle.
3420 * @param pfnAtState Pointer to callback.
3421 * @param pvUser User argument.
3422 * @thread Any.
3423 */
3424VMMR3DECL(int) VMR3AtStateRegister(PUVM pUVM, PFNVMATSTATE pfnAtState, void *pvUser)
3425{
3426 LogFlow(("VMR3AtStateRegister: pfnAtState=%p pvUser=%p\n", pfnAtState, pvUser));
3427
3428 /*
3429 * Validate input.
3430 */
3431 AssertPtrReturn(pfnAtState, VERR_INVALID_PARAMETER);
3432 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
3433
3434 /*
3435 * Allocate a new record.
3436 */
3437 PVMATSTATE pNew = (PVMATSTATE)MMR3HeapAllocU(pUVM, MM_TAG_VM, sizeof(*pNew));
3438 if (!pNew)
3439 return VERR_NO_MEMORY;
3440
3441 /* fill */
3442 pNew->pfnAtState = pfnAtState;
3443 pNew->pvUser = pvUser;
3444
3445 /* insert */
3446 RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
3447 pNew->pNext = *pUVM->vm.s.ppAtStateNext;
3448 *pUVM->vm.s.ppAtStateNext = pNew;
3449 pUVM->vm.s.ppAtStateNext = &pNew->pNext;
3450 RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
3451
3452 return VINF_SUCCESS;
3453}
3454
3455
3456/**
3457 * Deregisters a VM state change callback.
3458 *
3459 * @returns VBox status code.
3460 * @param pUVM The VM handle.
3461 * @param pfnAtState Pointer to callback.
3462 * @param pvUser User argument.
3463 * @thread Any.
3464 */
3465VMMR3DECL(int) VMR3AtStateDeregister(PUVM pUVM, PFNVMATSTATE pfnAtState, void *pvUser)
3466{
3467 LogFlow(("VMR3AtStateDeregister: pfnAtState=%p pvUser=%p\n", pfnAtState, pvUser));
3468
3469 /*
3470 * Validate input.
3471 */
3472 AssertPtrReturn(pfnAtState, VERR_INVALID_PARAMETER);
3473 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
3474
3475 RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
3476
3477 /*
3478 * Search the list for the entry.
3479 */
3480 PVMATSTATE pPrev = NULL;
3481 PVMATSTATE pCur = pUVM->vm.s.pAtState;
3482 while ( pCur
3483 && ( pCur->pfnAtState != pfnAtState
3484 || pCur->pvUser != pvUser))
3485 {
3486 pPrev = pCur;
3487 pCur = pCur->pNext;
3488 }
3489 if (!pCur)
3490 {
3491 AssertMsgFailed(("pfnAtState=%p was not found\n", pfnAtState));
3492 RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
3493 return VERR_FILE_NOT_FOUND;
3494 }
3495
3496 /*
3497 * Unlink it.
3498 */
3499 if (pPrev)
3500 {
3501 pPrev->pNext = pCur->pNext;
3502 if (!pCur->pNext)
3503 pUVM->vm.s.ppAtStateNext = &pPrev->pNext;
3504 }
3505 else
3506 {
3507 pUVM->vm.s.pAtState = pCur->pNext;
3508 if (!pCur->pNext)
3509 pUVM->vm.s.ppAtStateNext = &pUVM->vm.s.pAtState;
3510 }
3511
3512 RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
3513
3514 /*
3515 * Free it.
3516 */
3517 pCur->pfnAtState = NULL;
3518 pCur->pNext = NULL;
3519 MMR3HeapFree(pCur);
3520
3521 return VINF_SUCCESS;
3522}
3523
3524
3525/**
3526 * Registers a VM error callback.
3527 *
3528 * @returns VBox status code.
3529 * @param pUVM The VM handle.
3530 * @param pfnAtError Pointer to callback.
3531 * @param pvUser User argument.
3532 * @thread Any.
3533 */
3534VMMR3DECL(int) VMR3AtErrorRegister(PUVM pUVM, PFNVMATERROR pfnAtError, void *pvUser)
3535{
3536 LogFlow(("VMR3AtErrorRegister: pfnAtError=%p pvUser=%p\n", pfnAtError, pvUser));
3537
3538 /*
3539 * Validate input.
3540 */
3541 AssertPtrReturn(pfnAtError, VERR_INVALID_PARAMETER);
3542 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
3543
3544 /*
3545 * Allocate a new record.
3546 */
3547 PVMATERROR pNew = (PVMATERROR)MMR3HeapAllocU(pUVM, MM_TAG_VM, sizeof(*pNew));
3548 if (!pNew)
3549 return VERR_NO_MEMORY;
3550
3551 /* fill */
3552 pNew->pfnAtError = pfnAtError;
3553 pNew->pvUser = pvUser;
3554
3555 /* insert */
3556 RTCritSectEnter(&pUVM->vm.s.AtErrorCritSect);
3557 pNew->pNext = *pUVM->vm.s.ppAtErrorNext;
3558 *pUVM->vm.s.ppAtErrorNext = pNew;
3559 pUVM->vm.s.ppAtErrorNext = &pNew->pNext;
3560 RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
3561
3562 return VINF_SUCCESS;
3563}
3564
3565
3566/**
3567 * Deregisters a VM error callback.
3568 *
3569 * @returns VBox status code.
3570 * @param pUVM The VM handle.
3571 * @param pfnAtError Pointer to callback.
3572 * @param pvUser User argument.
3573 * @thread Any.
3574 */
3575VMMR3DECL(int) VMR3AtErrorDeregister(PUVM pUVM, PFNVMATERROR pfnAtError, void *pvUser)
3576{
3577 LogFlow(("VMR3AtErrorDeregister: pfnAtError=%p pvUser=%p\n", pfnAtError, pvUser));
3578
3579 /*
3580 * Validate input.
3581 */
3582 AssertPtrReturn(pfnAtError, VERR_INVALID_PARAMETER);
3583 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
3584
3585 RTCritSectEnter(&pUVM->vm.s.AtErrorCritSect);
3586
3587 /*
3588 * Search the list for the entry.
3589 */
3590 PVMATERROR pPrev = NULL;
3591 PVMATERROR pCur = pUVM->vm.s.pAtError;
3592 while ( pCur
3593 && ( pCur->pfnAtError != pfnAtError
3594 || pCur->pvUser != pvUser))
3595 {
3596 pPrev = pCur;
3597 pCur = pCur->pNext;
3598 }
3599 if (!pCur)
3600 {
3601 AssertMsgFailed(("pfnAtError=%p was not found\n", pfnAtError));
3602 RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
3603 return VERR_FILE_NOT_FOUND;
3604 }
3605
3606 /*
3607 * Unlink it.
3608 */
3609 if (pPrev)
3610 {
3611 pPrev->pNext = pCur->pNext;
3612 if (!pCur->pNext)
3613 pUVM->vm.s.ppAtErrorNext = &pPrev->pNext;
3614 }
3615 else
3616 {
3617 pUVM->vm.s.pAtError = pCur->pNext;
3618 if (!pCur->pNext)
3619 pUVM->vm.s.ppAtErrorNext = &pUVM->vm.s.pAtError;
3620 }
3621
3622 RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
3623
3624 /*
3625 * Free it.
3626 */
3627 pCur->pfnAtError = NULL;
3628 pCur->pNext = NULL;
3629 MMR3HeapFree(pCur);
3630
3631 return VINF_SUCCESS;
3632}
3633
3634
3635/**
3636 * Ellipsis to va_list wrapper for calling pfnAtError.
3637 */
3638static void vmR3SetErrorWorkerDoCall(PVM pVM, PVMATERROR pCur, int rc, RT_SRC_POS_DECL, const char *pszFormat, ...)
3639{
3640 va_list va;
3641 va_start(va, pszFormat);
3642 pCur->pfnAtError(pVM->pUVM, pCur->pvUser, rc, RT_SRC_POS_ARGS, pszFormat, va);
3643 va_end(va);
3644}
3645
3646
3647/**
3648 * This is a worker function for GC and Ring-0 calls to VMSetError and VMSetErrorV.
3649 * The message is found in VMINT.
3650 *
3651 * @param pVM The cross context VM structure.
3652 * @thread EMT.
3653 */
3654VMMR3_INT_DECL(void) VMR3SetErrorWorker(PVM pVM)
3655{
3656 VM_ASSERT_EMT(pVM);
3657 AssertReleaseMsgFailed(("And we have a winner! You get to implement Ring-0 and GC VMSetErrorV! Congrats!\n"));
3658
3659 /*
3660 * Unpack the error (if we managed to format one).
3661 */
3662 PVMERROR pErr = pVM->vm.s.pErrorR3;
3663 const char *pszFile = NULL;
3664 const char *pszFunction = NULL;
3665 uint32_t iLine = 0;
3666 const char *pszMessage;
3667 int32_t rc = VERR_MM_HYPER_NO_MEMORY;
3668 if (pErr)
3669 {
3670 AssertCompile(sizeof(const char) == sizeof(uint8_t));
3671 if (pErr->offFile)
3672 pszFile = (const char *)pErr + pErr->offFile;
3673 iLine = pErr->iLine;
3674 if (pErr->offFunction)
3675 pszFunction = (const char *)pErr + pErr->offFunction;
3676 if (pErr->offMessage)
3677 pszMessage = (const char *)pErr + pErr->offMessage;
3678 else
3679 pszMessage = "No message!";
3680 }
3681 else
3682 pszMessage = "No message! (Failed to allocate memory to put the error message in!)";
3683
3684 /*
3685 * Call the at error callbacks.
3686 */
3687 PUVM pUVM = pVM->pUVM;
3688 RTCritSectEnter(&pUVM->vm.s.AtErrorCritSect);
3689 ASMAtomicIncU32(&pUVM->vm.s.cRuntimeErrors);
3690 for (PVMATERROR pCur = pUVM->vm.s.pAtError; pCur; pCur = pCur->pNext)
3691 vmR3SetErrorWorkerDoCall(pVM, pCur, rc, RT_SRC_POS_ARGS, "%s", pszMessage);
3692 RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
3693}
3694
3695
3696/**
3697 * Gets the number of errors raised via VMSetError.
3698 *
3699 * This can be used avoid double error messages.
3700 *
3701 * @returns The error count.
3702 * @param pUVM The VM handle.
3703 */
3704VMMR3_INT_DECL(uint32_t) VMR3GetErrorCount(PUVM pUVM)
3705{
3706 AssertPtrReturn(pUVM, 0);
3707 AssertReturn(pUVM->u32Magic == UVM_MAGIC, 0);
3708 return pUVM->vm.s.cErrors;
3709}
3710
3711
3712/**
3713 * Creation time wrapper for vmR3SetErrorUV.
3714 *
3715 * @returns rc.
3716 * @param pUVM Pointer to the user mode VM structure.
3717 * @param rc The VBox status code.
3718 * @param SRC_POS The source position of this error.
3719 * @param pszFormat Format string.
3720 * @param ... The arguments.
3721 * @thread Any thread.
3722 */
3723static int vmR3SetErrorU(PUVM pUVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, ...)
3724{
3725 va_list va;
3726 va_start(va, pszFormat);
3727 vmR3SetErrorUV(pUVM, rc, pszFile, iLine, pszFunction, pszFormat, &va);
3728 va_end(va);
3729 return rc;
3730}
3731
3732
3733/**
3734 * Worker which calls everyone listening to the VM error messages.
3735 *
3736 * @param pUVM Pointer to the user mode VM structure.
3737 * @param rc The VBox status code.
3738 * @param SRC_POS The source position of this error.
3739 * @param pszFormat Format string.
3740 * @param pArgs Pointer to the format arguments.
3741 * @thread EMT
3742 */
3743DECLCALLBACK(void) vmR3SetErrorUV(PUVM pUVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list *pArgs)
3744{
3745 /*
3746 * Log the error.
3747 */
3748 va_list va3;
3749 va_copy(va3, *pArgs);
3750 RTLogRelPrintf("VMSetError: %s(%d) %s; rc=%Rrc\n"
3751 "VMSetError: %N\n",
3752 pszFile, iLine, pszFunction, rc,
3753 pszFormat, &va3);
3754 va_end(va3);
3755
3756#ifdef LOG_ENABLED
3757 va_copy(va3, *pArgs);
3758 RTLogPrintf("VMSetError: %s(%d) %s; rc=%Rrc\n"
3759 "%N\n",
3760 pszFile, iLine, pszFunction, rc,
3761 pszFormat, &va3);
3762 va_end(va3);
3763#endif
3764
3765 /*
3766 * Make a copy of the message.
3767 */
3768 if (pUVM->pVM)
3769 vmSetErrorCopy(pUVM->pVM, rc, RT_SRC_POS_ARGS, pszFormat, *pArgs);
3770
3771 /*
3772 * Call the at error callbacks.
3773 */
3774 bool fCalledSomeone = false;
3775 RTCritSectEnter(&pUVM->vm.s.AtErrorCritSect);
3776 ASMAtomicIncU32(&pUVM->vm.s.cErrors);
3777 for (PVMATERROR pCur = pUVM->vm.s.pAtError; pCur; pCur = pCur->pNext)
3778 {
3779 va_list va2;
3780 va_copy(va2, *pArgs);
3781 pCur->pfnAtError(pUVM, pCur->pvUser, rc, RT_SRC_POS_ARGS, pszFormat, va2);
3782 va_end(va2);
3783 fCalledSomeone = true;
3784 }
3785 RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
3786}
3787
3788
3789/**
3790 * Sets the error message.
3791 *
3792 * @returns rc. Meaning you can do:
3793 * @code
3794 * return VM_SET_ERROR_U(pUVM, VERR_OF_YOUR_CHOICE, "descriptive message");
3795 * @endcode
3796 * @param pUVM The user mode VM handle.
3797 * @param rc VBox status code.
3798 * @param SRC_POS Use RT_SRC_POS.
3799 * @param pszFormat Error message format string.
3800 * @param ... Error message arguments.
3801 * @thread Any
3802 */
3803VMMR3DECL(int) VMR3SetError(PUVM pUVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, ...)
3804{
3805 va_list va;
3806 va_start(va, pszFormat);
3807 int rcRet = VMR3SetErrorV(pUVM, rc, pszFile, iLine, pszFunction, pszFormat, va);
3808 va_end(va);
3809 return rcRet;
3810}
3811
3812
3813/**
3814 * Sets the error message.
3815 *
3816 * @returns rc. Meaning you can do:
3817 * @code
3818 * return VM_SET_ERROR_U(pUVM, VERR_OF_YOUR_CHOICE, "descriptive message");
3819 * @endcode
3820 * @param pUVM The user mode VM handle.
3821 * @param rc VBox status code.
3822 * @param SRC_POS Use RT_SRC_POS.
3823 * @param pszFormat Error message format string.
3824 * @param va Error message arguments.
3825 * @thread Any
3826 */
3827VMMR3DECL(int) VMR3SetErrorV(PUVM pUVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
3828{
3829 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
3830
3831 /* Take shortcut when called on EMT, skipping VM handle requirement + validation. */
3832 if (VMR3GetVMCPUThread(pUVM) != NIL_RTTHREAD)
3833 {
3834 va_list vaCopy;
3835 va_copy(vaCopy, va);
3836 vmR3SetErrorUV(pUVM, rc, RT_SRC_POS_ARGS, pszFormat, &vaCopy);
3837 va_end(vaCopy);
3838 return rc;
3839 }
3840
3841 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
3842 return VMSetErrorV(pUVM->pVM, rc, pszFile, iLine, pszFunction, pszFormat, va);
3843}
3844
3845
3846
3847/**
3848 * Registers a VM runtime error callback.
3849 *
3850 * @returns VBox status code.
3851 * @param pUVM The user mode VM structure.
3852 * @param pfnAtRuntimeError Pointer to callback.
3853 * @param pvUser User argument.
3854 * @thread Any.
3855 */
3856VMMR3DECL(int) VMR3AtRuntimeErrorRegister(PUVM pUVM, PFNVMATRUNTIMEERROR pfnAtRuntimeError, void *pvUser)
3857{
3858 LogFlow(("VMR3AtRuntimeErrorRegister: pfnAtRuntimeError=%p pvUser=%p\n", pfnAtRuntimeError, pvUser));
3859
3860 /*
3861 * Validate input.
3862 */
3863 AssertPtrReturn(pfnAtRuntimeError, VERR_INVALID_PARAMETER);
3864 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
3865
3866 /*
3867 * Allocate a new record.
3868 */
3869 PVMATRUNTIMEERROR pNew = (PVMATRUNTIMEERROR)MMR3HeapAllocU(pUVM, MM_TAG_VM, sizeof(*pNew));
3870 if (!pNew)
3871 return VERR_NO_MEMORY;
3872
3873 /* fill */
3874 pNew->pfnAtRuntimeError = pfnAtRuntimeError;
3875 pNew->pvUser = pvUser;
3876
3877 /* insert */
3878 RTCritSectEnter(&pUVM->vm.s.AtErrorCritSect);
3879 pNew->pNext = *pUVM->vm.s.ppAtRuntimeErrorNext;
3880 *pUVM->vm.s.ppAtRuntimeErrorNext = pNew;
3881 pUVM->vm.s.ppAtRuntimeErrorNext = &pNew->pNext;
3882 RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
3883
3884 return VINF_SUCCESS;
3885}
3886
3887
3888/**
3889 * Deregisters a VM runtime error callback.
3890 *
3891 * @returns VBox status code.
3892 * @param pUVM The user mode VM handle.
3893 * @param pfnAtRuntimeError Pointer to callback.
3894 * @param pvUser User argument.
3895 * @thread Any.
3896 */
3897VMMR3DECL(int) VMR3AtRuntimeErrorDeregister(PUVM pUVM, PFNVMATRUNTIMEERROR pfnAtRuntimeError, void *pvUser)
3898{
3899 LogFlow(("VMR3AtRuntimeErrorDeregister: pfnAtRuntimeError=%p pvUser=%p\n", pfnAtRuntimeError, pvUser));
3900
3901 /*
3902 * Validate input.
3903 */
3904 AssertPtrReturn(pfnAtRuntimeError, VERR_INVALID_PARAMETER);
3905 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
3906
3907 RTCritSectEnter(&pUVM->vm.s.AtErrorCritSect);
3908
3909 /*
3910 * Search the list for the entry.
3911 */
3912 PVMATRUNTIMEERROR pPrev = NULL;
3913 PVMATRUNTIMEERROR pCur = pUVM->vm.s.pAtRuntimeError;
3914 while ( pCur
3915 && ( pCur->pfnAtRuntimeError != pfnAtRuntimeError
3916 || pCur->pvUser != pvUser))
3917 {
3918 pPrev = pCur;
3919 pCur = pCur->pNext;
3920 }
3921 if (!pCur)
3922 {
3923 AssertMsgFailed(("pfnAtRuntimeError=%p was not found\n", pfnAtRuntimeError));
3924 RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
3925 return VERR_FILE_NOT_FOUND;
3926 }
3927
3928 /*
3929 * Unlink it.
3930 */
3931 if (pPrev)
3932 {
3933 pPrev->pNext = pCur->pNext;
3934 if (!pCur->pNext)
3935 pUVM->vm.s.ppAtRuntimeErrorNext = &pPrev->pNext;
3936 }
3937 else
3938 {
3939 pUVM->vm.s.pAtRuntimeError = pCur->pNext;
3940 if (!pCur->pNext)
3941 pUVM->vm.s.ppAtRuntimeErrorNext = &pUVM->vm.s.pAtRuntimeError;
3942 }
3943
3944 RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
3945
3946 /*
3947 * Free it.
3948 */
3949 pCur->pfnAtRuntimeError = NULL;
3950 pCur->pNext = NULL;
3951 MMR3HeapFree(pCur);
3952
3953 return VINF_SUCCESS;
3954}
3955
3956
3957/**
3958 * EMT rendezvous worker that vmR3SetRuntimeErrorCommon uses to safely change
3959 * the state to FatalError(LS).
3960 *
3961 * @returns VERR_VM_INVALID_VM_STATE or VINF_EM_SUSPEND. (This is a strict
3962 * return code, see FNVMMEMTRENDEZVOUS.)
3963 *
3964 * @param pVM The cross context VM structure.
3965 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
3966 * @param pvUser Ignored.
3967 */
3968static DECLCALLBACK(VBOXSTRICTRC) vmR3SetRuntimeErrorChangeState(PVM pVM, PVMCPU pVCpu, void *pvUser)
3969{
3970 NOREF(pVCpu);
3971 Assert(!pvUser); NOREF(pvUser);
3972
3973 /*
3974 * The first EMT thru here changes the state.
3975 */
3976 if (pVCpu->idCpu == pVM->cCpus - 1)
3977 {
3978 int rc = vmR3TrySetState(pVM, "VMSetRuntimeError", 2,
3979 VMSTATE_FATAL_ERROR, VMSTATE_RUNNING,
3980 VMSTATE_FATAL_ERROR_LS, VMSTATE_RUNNING_LS);
3981 if (RT_FAILURE(rc))
3982 return rc;
3983 if (rc == 2)
3984 SSMR3Cancel(pVM->pUVM);
3985
3986 VM_FF_SET(pVM, VM_FF_CHECK_VM_STATE);
3987 }
3988
3989 /* This'll make sure we get out of whereever we are (e.g. REM). */
3990 return VINF_EM_SUSPEND;
3991}
3992
3993
3994/**
3995 * Worker for VMR3SetRuntimeErrorWorker and vmR3SetRuntimeErrorV.
3996 *
3997 * This does the common parts after the error has been saved / retrieved.
3998 *
3999 * @returns VBox status code with modifications, see VMSetRuntimeErrorV.
4000 *
4001 * @param pVM The cross context VM structure.
4002 * @param fFlags The error flags.
4003 * @param pszErrorId Error ID string.
4004 * @param pszFormat Format string.
4005 * @param pVa Pointer to the format arguments.
4006 */
4007static int vmR3SetRuntimeErrorCommon(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list *pVa)
4008{
4009 LogRel(("VM: Raising runtime error '%s' (fFlags=%#x)\n", pszErrorId, fFlags));
4010 PUVM pUVM = pVM->pUVM;
4011
4012 /*
4013 * Take actions before the call.
4014 */
4015 int rc;
4016 if (fFlags & VMSETRTERR_FLAGS_FATAL)
4017 rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING | VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR,
4018 vmR3SetRuntimeErrorChangeState, NULL);
4019 else if (fFlags & VMSETRTERR_FLAGS_SUSPEND)
4020 rc = VMR3Suspend(pUVM, VMSUSPENDREASON_RUNTIME_ERROR);
4021 else
4022 rc = VINF_SUCCESS;
4023
4024 /*
4025 * Do the callback round.
4026 */
4027 RTCritSectEnter(&pUVM->vm.s.AtErrorCritSect);
4028 ASMAtomicIncU32(&pUVM->vm.s.cRuntimeErrors);
4029 for (PVMATRUNTIMEERROR pCur = pUVM->vm.s.pAtRuntimeError; pCur; pCur = pCur->pNext)
4030 {
4031 va_list va;
4032 va_copy(va, *pVa);
4033 pCur->pfnAtRuntimeError(pUVM, pCur->pvUser, fFlags, pszErrorId, pszFormat, va);
4034 va_end(va);
4035 }
4036 RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
4037
4038 return rc;
4039}
4040
4041
4042/**
4043 * Ellipsis to va_list wrapper for calling vmR3SetRuntimeErrorCommon.
4044 */
4045static int vmR3SetRuntimeErrorCommonF(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, ...)
4046{
4047 va_list va;
4048 va_start(va, pszFormat);
4049 int rc = vmR3SetRuntimeErrorCommon(pVM, fFlags, pszErrorId, pszFormat, &va);
4050 va_end(va);
4051 return rc;
4052}
4053
4054
4055/**
4056 * This is a worker function for RC and Ring-0 calls to VMSetError and
4057 * VMSetErrorV.
4058 *
4059 * The message is found in VMINT.
4060 *
4061 * @returns VBox status code, see VMSetRuntimeError.
4062 * @param pVM The cross context VM structure.
4063 * @thread EMT.
4064 */
4065VMMR3_INT_DECL(int) VMR3SetRuntimeErrorWorker(PVM pVM)
4066{
4067 VM_ASSERT_EMT(pVM);
4068 AssertReleaseMsgFailed(("And we have a winner! You get to implement Ring-0 and GC VMSetRuntimeErrorV! Congrats!\n"));
4069
4070 /*
4071 * Unpack the error (if we managed to format one).
4072 */
4073 const char *pszErrorId = "SetRuntimeError";
4074 const char *pszMessage = "No message!";
4075 uint32_t fFlags = VMSETRTERR_FLAGS_FATAL;
4076 PVMRUNTIMEERROR pErr = pVM->vm.s.pRuntimeErrorR3;
4077 if (pErr)
4078 {
4079 AssertCompile(sizeof(const char) == sizeof(uint8_t));
4080 if (pErr->offErrorId)
4081 pszErrorId = (const char *)pErr + pErr->offErrorId;
4082 if (pErr->offMessage)
4083 pszMessage = (const char *)pErr + pErr->offMessage;
4084 fFlags = pErr->fFlags;
4085 }
4086
4087 /*
4088 * Join cause with vmR3SetRuntimeErrorV.
4089 */
4090 return vmR3SetRuntimeErrorCommonF(pVM, fFlags, pszErrorId, "%s", pszMessage);
4091}
4092
4093
4094/**
4095 * Worker for VMSetRuntimeErrorV for doing the job on EMT in ring-3.
4096 *
4097 * @returns VBox status code with modifications, see VMSetRuntimeErrorV.
4098 *
4099 * @param pVM The cross context VM structure.
4100 * @param fFlags The error flags.
4101 * @param pszErrorId Error ID string.
4102 * @param pszMessage The error message residing the MM heap.
4103 *
4104 * @thread EMT
4105 */
4106DECLCALLBACK(int) vmR3SetRuntimeError(PVM pVM, uint32_t fFlags, const char *pszErrorId, char *pszMessage)
4107{
4108#if 0 /** @todo make copy of the error msg. */
4109 /*
4110 * Make a copy of the message.
4111 */
4112 va_list va2;
4113 va_copy(va2, *pVa);
4114 vmSetRuntimeErrorCopy(pVM, fFlags, pszErrorId, pszFormat, va2);
4115 va_end(va2);
4116#endif
4117
4118 /*
4119 * Join paths with VMR3SetRuntimeErrorWorker.
4120 */
4121 int rc = vmR3SetRuntimeErrorCommonF(pVM, fFlags, pszErrorId, "%s", pszMessage);
4122 MMR3HeapFree(pszMessage);
4123 return rc;
4124}
4125
4126
4127/**
4128 * Worker for VMSetRuntimeErrorV for doing the job on EMT in ring-3.
4129 *
4130 * @returns VBox status code with modifications, see VMSetRuntimeErrorV.
4131 *
4132 * @param pVM The cross context VM structure.
4133 * @param fFlags The error flags.
4134 * @param pszErrorId Error ID string.
4135 * @param pszFormat Format string.
4136 * @param pVa Pointer to the format arguments.
4137 *
4138 * @thread EMT
4139 */
4140DECLCALLBACK(int) vmR3SetRuntimeErrorV(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list *pVa)
4141{
4142 /*
4143 * Make a copy of the message.
4144 */
4145 va_list va2;
4146 va_copy(va2, *pVa);
4147 vmSetRuntimeErrorCopy(pVM, fFlags, pszErrorId, pszFormat, va2);
4148 va_end(va2);
4149
4150 /*
4151 * Join paths with VMR3SetRuntimeErrorWorker.
4152 */
4153 return vmR3SetRuntimeErrorCommon(pVM, fFlags, pszErrorId, pszFormat, pVa);
4154}
4155
4156
4157/**
4158 * Gets the number of runtime errors raised via VMR3SetRuntimeError.
4159 *
4160 * This can be used avoid double error messages.
4161 *
4162 * @returns The runtime error count.
4163 * @param pUVM The user mode VM handle.
4164 */
4165VMMR3_INT_DECL(uint32_t) VMR3GetRuntimeErrorCount(PUVM pUVM)
4166{
4167 return pUVM->vm.s.cRuntimeErrors;
4168}
4169
4170
4171/**
4172 * Gets the ID virtual of the virtual CPU associated with the calling thread.
4173 *
4174 * @returns The CPU ID. NIL_VMCPUID if the thread isn't an EMT.
4175 *
4176 * @param pVM The cross context VM structure.
4177 */
4178VMMR3_INT_DECL(RTCPUID) VMR3GetVMCPUId(PVM pVM)
4179{
4180 PUVMCPU pUVCpu = (PUVMCPU)RTTlsGet(pVM->pUVM->vm.s.idxTLS);
4181 return pUVCpu
4182 ? pUVCpu->idCpu
4183 : NIL_VMCPUID;
4184}
4185
4186
4187/**
4188 * Checks if the VM is long-mode (64-bit) capable or not.
4189 *
4190 * @returns true if VM can operate in long-mode, false otherwise.
4191 * @param pVM The cross context VM structure.
4192 */
4193VMMR3_INT_DECL(bool) VMR3IsLongModeAllowed(PVM pVM)
4194{
4195 switch (pVM->bMainExecutionEngine)
4196 {
4197 case VM_EXEC_ENGINE_HW_VIRT:
4198 return HMIsLongModeAllowed(pVM);
4199
4200 case VM_EXEC_ENGINE_NATIVE_API:
4201 return NEMHCIsLongModeAllowed(pVM);
4202
4203 case VM_EXEC_ENGINE_NOT_SET:
4204 AssertFailed();
4205 RT_FALL_THRU();
4206 default:
4207 return false;
4208 }
4209}
4210
4211
4212/**
4213 * Returns the native ID of the current EMT VMCPU thread.
4214 *
4215 * @returns Handle if this is an EMT thread; NIL_RTNATIVETHREAD otherwise
4216 * @param pVM The cross context VM structure.
4217 * @thread EMT
4218 */
4219VMMR3DECL(RTNATIVETHREAD) VMR3GetVMCPUNativeThread(PVM pVM)
4220{
4221 PUVMCPU pUVCpu = (PUVMCPU)RTTlsGet(pVM->pUVM->vm.s.idxTLS);
4222
4223 if (!pUVCpu)
4224 return NIL_RTNATIVETHREAD;
4225
4226 return pUVCpu->vm.s.NativeThreadEMT;
4227}
4228
4229
4230/**
4231 * Returns the native ID of the current EMT VMCPU thread.
4232 *
4233 * @returns Handle if this is an EMT thread; NIL_RTNATIVETHREAD otherwise
4234 * @param pUVM The user mode VM structure.
4235 * @thread EMT
4236 */
4237VMMR3DECL(RTNATIVETHREAD) VMR3GetVMCPUNativeThreadU(PUVM pUVM)
4238{
4239 PUVMCPU pUVCpu = (PUVMCPU)RTTlsGet(pUVM->vm.s.idxTLS);
4240
4241 if (!pUVCpu)
4242 return NIL_RTNATIVETHREAD;
4243
4244 return pUVCpu->vm.s.NativeThreadEMT;
4245}
4246
4247
4248/**
4249 * Returns the handle of the current EMT VMCPU thread.
4250 *
4251 * @returns Handle if this is an EMT thread; NIL_RTNATIVETHREAD otherwise
4252 * @param pUVM The user mode VM handle.
4253 * @thread EMT
4254 */
4255VMMR3DECL(RTTHREAD) VMR3GetVMCPUThread(PUVM pUVM)
4256{
4257 PUVMCPU pUVCpu = (PUVMCPU)RTTlsGet(pUVM->vm.s.idxTLS);
4258
4259 if (!pUVCpu)
4260 return NIL_RTTHREAD;
4261
4262 return pUVCpu->vm.s.ThreadEMT;
4263}
4264
4265
4266/**
4267 * Returns the handle of the current EMT VMCPU thread.
4268 *
4269 * @returns The IPRT thread handle.
4270 * @param pUVCpu The user mode CPU handle.
4271 * @thread EMT
4272 */
4273VMMR3_INT_DECL(RTTHREAD) VMR3GetThreadHandle(PUVMCPU pUVCpu)
4274{
4275 return pUVCpu->vm.s.ThreadEMT;
4276}
4277
4278
4279/**
4280 * Return the package and core ID of a CPU.
4281 *
4282 * @returns VBOX status code.
4283 * @param pUVM The user mode VM handle.
4284 * @param idCpu Virtual CPU to get the ID from.
4285 * @param pidCpuCore Where to store the core ID of the virtual CPU.
4286 * @param pidCpuPackage Where to store the package ID of the virtual CPU.
4287 *
4288 */
4289VMMR3DECL(int) VMR3GetCpuCoreAndPackageIdFromCpuId(PUVM pUVM, VMCPUID idCpu, uint32_t *pidCpuCore, uint32_t *pidCpuPackage)
4290{
4291 /*
4292 * Validate input.
4293 */
4294 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
4295 PVM pVM = pUVM->pVM;
4296 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
4297 AssertPtrReturn(pidCpuCore, VERR_INVALID_POINTER);
4298 AssertPtrReturn(pidCpuPackage, VERR_INVALID_POINTER);
4299 if (idCpu >= pVM->cCpus)
4300 return VERR_INVALID_CPU_ID;
4301
4302 /*
4303 * Set return values.
4304 */
4305#ifdef VBOX_WITH_MULTI_CORE
4306 *pidCpuCore = idCpu;
4307 *pidCpuPackage = 0;
4308#else
4309 *pidCpuCore = 0;
4310 *pidCpuPackage = idCpu;
4311#endif
4312
4313 return VINF_SUCCESS;
4314}
4315
4316
4317/**
4318 * Worker for VMR3HotUnplugCpu.
4319 *
4320 * @returns VINF_EM_WAIT_SPIP (strict status code).
4321 * @param pVM The cross context VM structure.
4322 * @param idCpu The current CPU.
4323 */
4324static DECLCALLBACK(int) vmR3HotUnplugCpu(PVM pVM, VMCPUID idCpu)
4325{
4326 PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
4327 VMCPU_ASSERT_EMT(pVCpu);
4328
4329 /*
4330 * Reset per CPU resources.
4331 *
4332 * Actually only needed for VT-x because the CPU seems to be still in some
4333 * paged mode and startup fails after a new hot plug event. SVM works fine
4334 * even without this.
4335 */
4336 Log(("vmR3HotUnplugCpu for VCPU %u\n", idCpu));
4337 PGMR3ResetCpu(pVM, pVCpu);
4338 PDMR3ResetCpu(pVCpu);
4339 TRPMR3ResetCpu(pVCpu);
4340 CPUMR3ResetCpu(pVM, pVCpu);
4341 EMR3ResetCpu(pVCpu);
4342 HMR3ResetCpu(pVCpu);
4343 NEMR3ResetCpu(pVCpu, false /*fInitIpi*/);
4344 return VINF_EM_WAIT_SIPI;
4345}
4346
4347
4348/**
4349 * Hot-unplugs a CPU from the guest.
4350 *
4351 * @returns VBox status code.
4352 * @param pUVM The user mode VM handle.
4353 * @param idCpu Virtual CPU to perform the hot unplugging operation on.
4354 */
4355VMMR3DECL(int) VMR3HotUnplugCpu(PUVM pUVM, VMCPUID idCpu)
4356{
4357 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
4358 PVM pVM = pUVM->pVM;
4359 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
4360 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
4361
4362 /** @todo r=bird: Don't destroy the EMT, it'll break VMMR3EmtRendezvous and
4363 * broadcast requests. Just note down somewhere that the CPU is
4364 * offline and send it to SPIP wait. Maybe modify VMCPUSTATE and push
4365 * it out of the EM loops when offline. */
4366 return VMR3ReqCallNoWaitU(pUVM, idCpu, (PFNRT)vmR3HotUnplugCpu, 2, pVM, idCpu);
4367}
4368
4369
4370/**
4371 * Hot-plugs a CPU on the guest.
4372 *
4373 * @returns VBox status code.
4374 * @param pUVM The user mode VM handle.
4375 * @param idCpu Virtual CPU to perform the hot plugging operation on.
4376 */
4377VMMR3DECL(int) VMR3HotPlugCpu(PUVM pUVM, VMCPUID idCpu)
4378{
4379 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
4380 PVM pVM = pUVM->pVM;
4381 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
4382 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
4383
4384 /** @todo r-bird: Just mark it online and make sure it waits on SPIP. */
4385 return VINF_SUCCESS;
4386}
4387
4388
4389/**
4390 * Changes the VMM execution cap.
4391 *
4392 * @returns VBox status code.
4393 * @param pUVM The user mode VM structure.
4394 * @param uCpuExecutionCap New CPU execution cap in precent, 1-100. Where
4395 * 100 is max performance (default).
4396 */
4397VMMR3DECL(int) VMR3SetCpuExecutionCap(PUVM pUVM, uint32_t uCpuExecutionCap)
4398{
4399 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
4400 PVM pVM = pUVM->pVM;
4401 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
4402 AssertReturn(uCpuExecutionCap > 0 && uCpuExecutionCap <= 100, VERR_INVALID_PARAMETER);
4403
4404 Log(("VMR3SetCpuExecutionCap: new priority = %d\n", uCpuExecutionCap));
4405 /* Note: not called from EMT. */
4406 pVM->uCpuExecutionCap = uCpuExecutionCap;
4407 return VINF_SUCCESS;
4408}
4409
4410
4411/**
4412 * Control whether the VM should power off when resetting.
4413 *
4414 * @returns VBox status code.
4415 * @param pUVM The user mode VM handle.
4416 * @param fPowerOffInsteadOfReset Flag whether the VM should power off when
4417 * resetting.
4418 */
4419VMMR3DECL(int) VMR3SetPowerOffInsteadOfReset(PUVM pUVM, bool fPowerOffInsteadOfReset)
4420{
4421 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
4422 PVM pVM = pUVM->pVM;
4423 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
4424
4425 /* Note: not called from EMT. */
4426 pVM->vm.s.fPowerOffInsteadOfReset = fPowerOffInsteadOfReset;
4427 return VINF_SUCCESS;
4428}
4429
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