VirtualBox

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

Last change on this file since 24141 was 24037, checked in by vboxsync, 15 years ago

Missing break

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette