VirtualBox

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

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

VMM: Removed PGM_WITHOUT_MAPPINGS and associated mapping code. bugref:9517

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