VirtualBox

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

Last change on this file since 12071 was 11883, checked in by vboxsync, 16 years ago

#3076: better error messages for when using the wrong driver.

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