VirtualBox

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

Last change on this file since 17049 was 16584, checked in by vboxsync, 16 years ago

VMM: message typo fix

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