VirtualBox

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

Last change on this file since 5734 was 5684, checked in by vboxsync, 17 years ago

Build fix.

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