VirtualBox

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

Last change on this file since 31854 was 31854, checked in by vboxsync, 14 years ago

VM: proper error message for VERR_HWACCM_CONFIG_MISMATCH

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