VirtualBox

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

Last change on this file since 41777 was 41777, checked in by vboxsync, 12 years ago

Doxygen.

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