VirtualBox

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

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

FT updates

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

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