VirtualBox

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

Last change on this file since 14672 was 14543, checked in by vboxsync, 16 years ago

#1865/VT-x: Attacking the heap allocation, introducing VMMIsHwVirtExtForced which will only return true on darwin (for now at least).

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