VirtualBox

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

Last change on this file since 4284 was 4228, checked in by vboxsync, 17 years ago

corrected error message for SUPLowAlloc failure.

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