VirtualBox

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

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

Workaround for the multiple error messages set in the VERR_VMX_IN_VMX_ROOT_MODE case. Needs to be redone properly.

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