VirtualBox

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

Last change on this file since 20041 was 19495, checked in by vboxsync, 16 years ago

VM: Name the EMTs differently if there are more than one.

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