VirtualBox

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

Last change on this file since 84044 was 82990, checked in by vboxsync, 4 years ago

VMM/GMMR0: Added TLB statistics for GMMR0PageIdToVirt. bugref:9627

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

© 2023 Oracle
ContactPrivacy policyTerms of Use