VirtualBox

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

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

The Big Sun Rebranding Header Change

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