VirtualBox

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

Last change on this file since 13538 was 13413, checked in by vboxsync, 16 years ago

VMM: spaces mostly.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use