VirtualBox

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

Last change on this file since 29287 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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