VirtualBox

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

Last change on this file since 27149 was 26653, checked in by vboxsync, 15 years ago

Rewrote cpuid workaround

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