VirtualBox

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

Last change on this file since 104507 was 104507, checked in by vboxsync, 6 months ago

VMM,Main: Some GCM adjustments. bugref:9735 bugref:10683

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