VirtualBox

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

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

PDMDevHlpVMSuspendSaveAndPowerOff: More code.

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

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