1 | /* $Id: VM.cpp 24922 2009-11-24 19:33:31Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VM - Virtual Machine
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /** @page pg_vm VM API
|
---|
23 | *
|
---|
24 | * This is the encapsulating bit. It provides the APIs that Main and VBoxBFE
|
---|
25 | * use to create a VMM instance for running a guest in. It also provides
|
---|
26 | * facilities for queuing request for execution in EMT (serialization purposes
|
---|
27 | * mostly) and for reporting error back to the VMM user (Main/VBoxBFE).
|
---|
28 | *
|
---|
29 | *
|
---|
30 | * @section sec_vm_design Design Critique / Things To Do
|
---|
31 | *
|
---|
32 | * In hindsight this component is a big design mistake, all this stuff really
|
---|
33 | * belongs in the VMM component. It just seemed like a kind of ok idea at a
|
---|
34 | * time when the VMM bit was a kind of vague. 'VM' also happend to be the name
|
---|
35 | * of the per-VM instance structure (see vm.h), so it kind of made sense.
|
---|
36 | * However as it turned out, VMM(.cpp) is almost empty all it provides in ring-3
|
---|
37 | * is some minor functionally and some "routing" services.
|
---|
38 | *
|
---|
39 | * Fixing this is just a matter of some more or less straight forward
|
---|
40 | * refactoring, the question is just when someone will get to it. Moving the EMT
|
---|
41 | * would be a good start.
|
---|
42 | *
|
---|
43 | */
|
---|
44 |
|
---|
45 | /*******************************************************************************
|
---|
46 | * Header Files *
|
---|
47 | *******************************************************************************/
|
---|
48 | #define LOG_GROUP LOG_GROUP_VM
|
---|
49 | #include <VBox/cfgm.h>
|
---|
50 | #include <VBox/vmm.h>
|
---|
51 | #include <VBox/gvmm.h>
|
---|
52 | #include <VBox/mm.h>
|
---|
53 | #include <VBox/cpum.h>
|
---|
54 | #include <VBox/selm.h>
|
---|
55 | #include <VBox/trpm.h>
|
---|
56 | #include <VBox/dbgf.h>
|
---|
57 | #include <VBox/pgm.h>
|
---|
58 | #include <VBox/pdmapi.h>
|
---|
59 | #include <VBox/pdmcritsect.h>
|
---|
60 | #include <VBox/em.h>
|
---|
61 | #include <VBox/rem.h>
|
---|
62 | #include <VBox/tm.h>
|
---|
63 | #include <VBox/stam.h>
|
---|
64 | #include <VBox/patm.h>
|
---|
65 | #ifdef VBOX_WITH_VMI
|
---|
66 | # include <VBox/parav.h>
|
---|
67 | #endif
|
---|
68 | #include <VBox/csam.h>
|
---|
69 | #include <VBox/iom.h>
|
---|
70 | #include <VBox/ssm.h>
|
---|
71 | #include <VBox/hwaccm.h>
|
---|
72 | #include "VMInternal.h"
|
---|
73 | #include <VBox/vm.h>
|
---|
74 | #include <VBox/uvm.h>
|
---|
75 |
|
---|
76 | #include <VBox/sup.h>
|
---|
77 | #include <VBox/dbg.h>
|
---|
78 | #include <VBox/err.h>
|
---|
79 | #include <VBox/param.h>
|
---|
80 | #include <VBox/log.h>
|
---|
81 | #include <iprt/assert.h>
|
---|
82 | #include <iprt/alloc.h>
|
---|
83 | #include <iprt/asm.h>
|
---|
84 | #include <iprt/env.h>
|
---|
85 | #include <iprt/string.h>
|
---|
86 | #include <iprt/time.h>
|
---|
87 | #include <iprt/semaphore.h>
|
---|
88 | #include <iprt/thread.h>
|
---|
89 |
|
---|
90 |
|
---|
91 | /*******************************************************************************
|
---|
92 | * Structures and Typedefs *
|
---|
93 | *******************************************************************************/
|
---|
94 | /**
|
---|
95 | * VM destruction callback registration record.
|
---|
96 | */
|
---|
97 | typedef struct VMATDTOR
|
---|
98 | {
|
---|
99 | /** Pointer to the next record in the list. */
|
---|
100 | struct VMATDTOR *pNext;
|
---|
101 | /** Pointer to the callback function. */
|
---|
102 | PFNVMATDTOR pfnAtDtor;
|
---|
103 | /** The user argument. */
|
---|
104 | void *pvUser;
|
---|
105 | } VMATDTOR;
|
---|
106 | /** Pointer to a VM destruction callback registration record. */
|
---|
107 | typedef VMATDTOR *PVMATDTOR;
|
---|
108 |
|
---|
109 |
|
---|
110 | /*******************************************************************************
|
---|
111 | * Global Variables *
|
---|
112 | *******************************************************************************/
|
---|
113 | /** Pointer to the list of VMs. */
|
---|
114 | static PUVM g_pUVMsHead = NULL;
|
---|
115 |
|
---|
116 | /** Pointer to the list of at VM destruction callbacks. */
|
---|
117 | static PVMATDTOR g_pVMAtDtorHead = NULL;
|
---|
118 | /** Lock the g_pVMAtDtorHead list. */
|
---|
119 | #define VM_ATDTOR_LOCK() do { } while (0)
|
---|
120 | /** Unlock the g_pVMAtDtorHead list. */
|
---|
121 | #define VM_ATDTOR_UNLOCK() do { } while (0)
|
---|
122 |
|
---|
123 |
|
---|
124 | /*******************************************************************************
|
---|
125 | * Internal Functions *
|
---|
126 | *******************************************************************************/
|
---|
127 | static int vmR3CreateUVM(uint32_t cCpus, PUVM *ppUVM);
|
---|
128 | static int vmR3CreateU(PUVM pUVM, uint32_t cCpus, PFNCFGMCONSTRUCTOR pfnCFGMConstructor, void *pvUserCFGM);
|
---|
129 | static int vmR3InitRing3(PVM pVM, PUVM pUVM);
|
---|
130 | static int vmR3InitVMCpu(PVM pVM);
|
---|
131 | static int vmR3InitRing0(PVM pVM);
|
---|
132 | static int vmR3InitGC(PVM pVM);
|
---|
133 | static int vmR3InitDoCompleted(PVM pVM, VMINITCOMPLETED enmWhat);
|
---|
134 | static DECLCALLBACK(size_t) vmR3LogPrefixCallback(PRTLOGGER pLogger, char *pchBuf, size_t cchBuf, void *pvUser);
|
---|
135 | static void vmR3DestroyUVM(PUVM pUVM, uint32_t cMilliesEMTWait);
|
---|
136 | static void vmR3AtDtor(PVM pVM);
|
---|
137 | static bool vmR3ValidateStateTransition(VMSTATE enmStateOld, VMSTATE enmStateNew);
|
---|
138 | static void vmR3DoAtState(PVM pVM, PUVM pUVM, VMSTATE enmStateNew, VMSTATE enmStateOld);
|
---|
139 | static int vmR3TrySetState(PVM pVM, const char *pszWho, unsigned cTransitions, ...);
|
---|
140 | static void vmR3SetStateLocked(PVM pVM, PUVM pUVM, VMSTATE enmStateNew, VMSTATE enmStateOld);
|
---|
141 | static void vmR3SetState(PVM pVM, VMSTATE enmStateNew, VMSTATE enmStateOld);
|
---|
142 | static int vmR3SetErrorU(PUVM pUVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, ...);
|
---|
143 |
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Do global VMM init.
|
---|
147 | *
|
---|
148 | * @returns VBox status code.
|
---|
149 | */
|
---|
150 | VMMR3DECL(int) VMR3GlobalInit(void)
|
---|
151 | {
|
---|
152 | /*
|
---|
153 | * Only once.
|
---|
154 | */
|
---|
155 | static bool volatile s_fDone = false;
|
---|
156 | if (s_fDone)
|
---|
157 | return VINF_SUCCESS;
|
---|
158 |
|
---|
159 | /*
|
---|
160 | * We're done.
|
---|
161 | */
|
---|
162 | s_fDone = true;
|
---|
163 | return VINF_SUCCESS;
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Creates a virtual machine by calling the supplied configuration constructor.
|
---|
170 | *
|
---|
171 | * On successful returned the VM is powered, i.e. VMR3PowerOn() should be
|
---|
172 | * called to start the execution.
|
---|
173 | *
|
---|
174 | * @returns 0 on success.
|
---|
175 | * @returns VBox error code on failure.
|
---|
176 | * @param cCpus Number of virtual CPUs for the new VM.
|
---|
177 | * @param pfnVMAtError Pointer to callback function for setting VM
|
---|
178 | * errors. This was added as an implicit call to
|
---|
179 | * VMR3AtErrorRegister() since there is no way the
|
---|
180 | * caller can get to the VM handle early enough to
|
---|
181 | * do this on its own.
|
---|
182 | * This is called in the context of an EMT.
|
---|
183 | * @param pvUserVM The user argument passed to pfnVMAtError.
|
---|
184 | * @param pfnCFGMConstructor Pointer to callback function for constructing the VM configuration tree.
|
---|
185 | * This is called in the context of an EMT0.
|
---|
186 | * @param pvUserCFGM The user argument passed to pfnCFGMConstructor.
|
---|
187 | * @param ppVM Where to store the 'handle' of the created VM.
|
---|
188 | */
|
---|
189 | VMMR3DECL(int) VMR3Create(uint32_t cCpus, PFNVMATERROR pfnVMAtError, void *pvUserVM, PFNCFGMCONSTRUCTOR pfnCFGMConstructor, void *pvUserCFGM, PVM *ppVM)
|
---|
190 | {
|
---|
191 | LogFlow(("VMR3Create: cCpus=%RU32 pfnVMAtError=%p pvUserVM=%p pfnCFGMConstructor=%p pvUserCFGM=%p ppVM=%p\n",
|
---|
192 | cCpus, pfnVMAtError, pvUserVM, pfnCFGMConstructor, pvUserCFGM, ppVM));
|
---|
193 |
|
---|
194 | /*
|
---|
195 | * Because of the current hackiness of the applications
|
---|
196 | * we'll have to initialize global stuff from here.
|
---|
197 | * Later the applications will take care of this in a proper way.
|
---|
198 | */
|
---|
199 | static bool fGlobalInitDone = false;
|
---|
200 | if (!fGlobalInitDone)
|
---|
201 | {
|
---|
202 | int rc = VMR3GlobalInit();
|
---|
203 | if (RT_FAILURE(rc))
|
---|
204 | return rc;
|
---|
205 | fGlobalInitDone = true;
|
---|
206 | }
|
---|
207 |
|
---|
208 | /*
|
---|
209 | * Validate input.
|
---|
210 | */
|
---|
211 | AssertLogRelMsgReturn(cCpus > 0 && cCpus <= VMM_MAX_CPU_COUNT, ("%RU32\n", cCpus), VERR_TOO_MANY_CPUS);
|
---|
212 |
|
---|
213 | /*
|
---|
214 | * Create the UVM so we can register the at-error callback
|
---|
215 | * and consoliate a bit of cleanup code.
|
---|
216 | */
|
---|
217 | PUVM pUVM = NULL; /* shuts up gcc */
|
---|
218 | int rc = vmR3CreateUVM(cCpus, &pUVM);
|
---|
219 | if (RT_FAILURE(rc))
|
---|
220 | return rc;
|
---|
221 | if (pfnVMAtError)
|
---|
222 | rc = VMR3AtErrorRegisterU(pUVM, pfnVMAtError, pvUserVM);
|
---|
223 | if (RT_SUCCESS(rc))
|
---|
224 | {
|
---|
225 | /*
|
---|
226 | * Initialize the support library creating the session for this VM.
|
---|
227 | */
|
---|
228 | rc = SUPR3Init(&pUVM->vm.s.pSession);
|
---|
229 | if (RT_SUCCESS(rc))
|
---|
230 | {
|
---|
231 | /*
|
---|
232 | * Call vmR3CreateU in the EMT thread and wait for it to finish.
|
---|
233 | *
|
---|
234 | * Note! VMCPUID_ANY is used here because VMR3ReqQueueU would have trouble
|
---|
235 | * submitting a request to a specific VCPU without a pVM. So, to make
|
---|
236 | * sure init is running on EMT(0), vmR3EmulationThreadWithId makes sure
|
---|
237 | * that only EMT(0) is servicing VMCPUID_ANY requests when pVM is NULL.
|
---|
238 | */
|
---|
239 | PVMREQ pReq;
|
---|
240 | rc = VMR3ReqCallU(pUVM, VMCPUID_ANY, &pReq, RT_INDEFINITE_WAIT, VMREQFLAGS_VBOX_STATUS,
|
---|
241 | (PFNRT)vmR3CreateU, 4, pUVM, cCpus, pfnCFGMConstructor, pvUserCFGM);
|
---|
242 | if (RT_SUCCESS(rc))
|
---|
243 | {
|
---|
244 | rc = pReq->iStatus;
|
---|
245 | VMR3ReqFree(pReq);
|
---|
246 | if (RT_SUCCESS(rc))
|
---|
247 | {
|
---|
248 | /*
|
---|
249 | * Success!
|
---|
250 | */
|
---|
251 | *ppVM = pUVM->pVM;
|
---|
252 | LogFlow(("VMR3Create: returns VINF_SUCCESS *ppVM=%p\n", *ppVM));
|
---|
253 | return VINF_SUCCESS;
|
---|
254 | }
|
---|
255 | }
|
---|
256 | else
|
---|
257 | AssertMsgFailed(("VMR3ReqCallU failed rc=%Rrc\n", rc));
|
---|
258 |
|
---|
259 | /*
|
---|
260 | * An error occurred during VM creation. Set the error message directly
|
---|
261 | * using the initial callback, as the callback list doesn't exist yet.
|
---|
262 | */
|
---|
263 | const char *pszError = NULL;
|
---|
264 | switch (rc)
|
---|
265 | {
|
---|
266 | case VERR_VMX_IN_VMX_ROOT_MODE:
|
---|
267 | #ifdef RT_OS_LINUX
|
---|
268 | pszError = N_("VirtualBox can't operate in VMX root mode. "
|
---|
269 | "Please disable the KVM kernel extension, recompile your kernel and reboot");
|
---|
270 | #else
|
---|
271 | pszError = N_("VirtualBox can't operate in VMX root mode. Please close all other virtualization programs.");
|
---|
272 | #endif
|
---|
273 | break;
|
---|
274 |
|
---|
275 | case VERR_SVM_IN_USE:
|
---|
276 | #ifdef RT_OS_LINUX
|
---|
277 | pszError = N_("VirtualBox can't enable the AMD-V extension. "
|
---|
278 | "Please disable the KVM kernel extension, recompile your kernel and reboot");
|
---|
279 | #else
|
---|
280 | pszError = N_("VirtualBox can't enable the AMD-V extension. Please close all other virtualization programs.");
|
---|
281 | #endif
|
---|
282 | break;
|
---|
283 |
|
---|
284 | case VERR_VERSION_MISMATCH:
|
---|
285 | pszError = N_("VMMR0 driver version mismatch. Please terminate all VMs, make sure that "
|
---|
286 | "VBoxNetDHCP is not running and try again. If you still get this error, "
|
---|
287 | "re-install VirtualBox");
|
---|
288 | break;
|
---|
289 |
|
---|
290 | #ifdef RT_OS_LINUX
|
---|
291 | case VERR_SUPDRV_COMPONENT_NOT_FOUND:
|
---|
292 | pszError = N_("One of the kernel modules was not successfully loaded. Make sure "
|
---|
293 | "that no kernel modules from an older version of VirtualBox exist. "
|
---|
294 | "Then try to recompile and reload the kernel modules by executing "
|
---|
295 | "'/etc/init.d/vboxdrv setup' as root");
|
---|
296 | break;
|
---|
297 | #endif
|
---|
298 |
|
---|
299 | case VERR_RAW_MODE_INVALID_SMP:
|
---|
300 | pszError = N_("VT-x/AMD-V is either not available on your host or disabled. "
|
---|
301 | "VirtualBox requires this hardware extension to emulate more than one "
|
---|
302 | "guest CPU");
|
---|
303 | break;
|
---|
304 |
|
---|
305 | case VERR_SUPDRV_KERNEL_TOO_OLD_FOR_VTX:
|
---|
306 | #ifdef RT_OS_LINUX
|
---|
307 | pszError = N_("Because the host kernel is too old, VirtualBox cannot enable the VT-x "
|
---|
308 | "extension. Either upgrade your kernel to Linux 2.6.13 or later or disable "
|
---|
309 | "the VT-x extension in the VM settings. Note that without VT-x you have "
|
---|
310 | "to reduce the number of guest CPUs to one");
|
---|
311 | #else
|
---|
312 | pszError = N_("Because the host kernel is too old, VirtualBox cannot enable the VT-x "
|
---|
313 | "extension. Either upgrade your kernel or disable the VT-x extension in the "
|
---|
314 | "VM settings. Note that without VT-x you have to reduce the number of guest "
|
---|
315 | "CPUs to one");
|
---|
316 | #endif
|
---|
317 | break;
|
---|
318 |
|
---|
319 | default:
|
---|
320 | pszError = N_("Unknown error creating VM");
|
---|
321 | break;
|
---|
322 | }
|
---|
323 | vmR3SetErrorU(pUVM, rc, RT_SRC_POS, pszError, rc);
|
---|
324 | }
|
---|
325 | else
|
---|
326 | {
|
---|
327 | /*
|
---|
328 | * An error occurred at support library initialization time (before the
|
---|
329 | * VM could be created). Set the error message directly using the
|
---|
330 | * initial callback, as the callback list doesn't exist yet.
|
---|
331 | */
|
---|
332 | const char *pszError;
|
---|
333 | switch (rc)
|
---|
334 | {
|
---|
335 | case VERR_VM_DRIVER_LOAD_ERROR:
|
---|
336 | #ifdef RT_OS_LINUX
|
---|
337 | pszError = N_("VirtualBox kernel driver not loaded. The vboxdrv kernel module "
|
---|
338 | "was either not loaded or /dev/vboxdrv is not set up properly. "
|
---|
339 | "Re-setup the kernel module by executing "
|
---|
340 | "'/etc/init.d/vboxdrv setup' as root");
|
---|
341 | #else
|
---|
342 | pszError = N_("VirtualBox kernel driver not loaded");
|
---|
343 | #endif
|
---|
344 | break;
|
---|
345 | case VERR_VM_DRIVER_OPEN_ERROR:
|
---|
346 | pszError = N_("VirtualBox kernel driver cannot be opened");
|
---|
347 | break;
|
---|
348 | case VERR_VM_DRIVER_NOT_ACCESSIBLE:
|
---|
349 | #ifdef VBOX_WITH_HARDENING
|
---|
350 | /* This should only happen if the executable wasn't hardened - bad code/build. */
|
---|
351 | pszError = N_("VirtualBox kernel driver not accessible, permission problem. "
|
---|
352 | "Re-install VirtualBox. If you are building it yourself, you "
|
---|
353 | "should make sure it installed correctly and that the setuid "
|
---|
354 | "bit is set on the executables calling VMR3Create.");
|
---|
355 | #else
|
---|
356 | /* This should only happen when mixing builds or with the usual /dev/vboxdrv access issues. */
|
---|
357 | # if defined(RT_OS_DARWIN)
|
---|
358 | pszError = N_("VirtualBox KEXT is not accessible, permission problem. "
|
---|
359 | "If you have built VirtualBox yourself, make sure that you do not "
|
---|
360 | "have the vboxdrv KEXT from a different build or installation loaded.");
|
---|
361 | # elif defined(RT_OS_LINUX)
|
---|
362 | pszError = N_("VirtualBox kernel driver is not accessible, permission problem. "
|
---|
363 | "If you have built VirtualBox yourself, make sure that you do "
|
---|
364 | "not have the vboxdrv kernel module from a different build or "
|
---|
365 | "installation loaded. Also, make sure the vboxdrv udev rule gives "
|
---|
366 | "you the permission you need to access the device.");
|
---|
367 | # elif defined(RT_OS_WINDOWS)
|
---|
368 | pszError = N_("VirtualBox kernel driver is not accessible, permission problem.");
|
---|
369 | # else /* solaris, freebsd, ++. */
|
---|
370 | pszError = N_("VirtualBox kernel module is not accessible, permission problem. "
|
---|
371 | "If you have built VirtualBox yourself, make sure that you do "
|
---|
372 | "not have the vboxdrv kernel module from a different install loaded.");
|
---|
373 | # endif
|
---|
374 | #endif
|
---|
375 | break;
|
---|
376 | case VERR_INVALID_HANDLE: /** @todo track down and fix this error. */
|
---|
377 | case VERR_VM_DRIVER_NOT_INSTALLED:
|
---|
378 | #ifdef RT_OS_LINUX
|
---|
379 | pszError = N_("VirtualBox kernel driver not installed. The vboxdrv kernel module "
|
---|
380 | "was either not loaded or /dev/vboxdrv was not created for some "
|
---|
381 | "reason. Re-setup the kernel module by executing "
|
---|
382 | "'/etc/init.d/vboxdrv setup' as root");
|
---|
383 | #else
|
---|
384 | pszError = N_("VirtualBox kernel driver not installed");
|
---|
385 | #endif
|
---|
386 | break;
|
---|
387 | case VERR_NO_MEMORY:
|
---|
388 | pszError = N_("VirtualBox support library out of memory");
|
---|
389 | break;
|
---|
390 | case VERR_VERSION_MISMATCH:
|
---|
391 | case VERR_VM_DRIVER_VERSION_MISMATCH:
|
---|
392 | pszError = N_("The VirtualBox support driver which is running is from a different "
|
---|
393 | "version of VirtualBox. You can correct this by stopping all "
|
---|
394 | "running instances of VirtualBox and reinstalling the software.");
|
---|
395 | break;
|
---|
396 | default:
|
---|
397 | pszError = N_("Unknown error initializing kernel driver");
|
---|
398 | AssertMsgFailed(("Add error message for rc=%d (%Rrc)\n", rc, rc));
|
---|
399 | }
|
---|
400 | vmR3SetErrorU(pUVM, rc, RT_SRC_POS, pszError, rc);
|
---|
401 | }
|
---|
402 | }
|
---|
403 |
|
---|
404 | /* cleanup */
|
---|
405 | vmR3DestroyUVM(pUVM, 2000);
|
---|
406 | LogFlow(("VMR3Create: returns %Rrc\n", rc));
|
---|
407 | return rc;
|
---|
408 | }
|
---|
409 |
|
---|
410 |
|
---|
411 | /**
|
---|
412 | * Creates the UVM.
|
---|
413 | *
|
---|
414 | * This will not initialize the support library even if vmR3DestroyUVM
|
---|
415 | * will terminate that.
|
---|
416 | *
|
---|
417 | * @returns VBox status code.
|
---|
418 | * @param cCpus Number of virtual CPUs
|
---|
419 | * @param ppUVM Where to store the UVM pointer.
|
---|
420 | */
|
---|
421 | static int vmR3CreateUVM(uint32_t cCpus, PUVM *ppUVM)
|
---|
422 | {
|
---|
423 | uint32_t i;
|
---|
424 |
|
---|
425 | /*
|
---|
426 | * Create and initialize the UVM.
|
---|
427 | */
|
---|
428 | PUVM pUVM = (PUVM)RTMemPageAllocZ(RT_OFFSETOF(UVM, aCpus[cCpus]));
|
---|
429 | AssertReturn(pUVM, VERR_NO_MEMORY);
|
---|
430 | pUVM->u32Magic = UVM_MAGIC;
|
---|
431 | pUVM->cCpus = cCpus;
|
---|
432 |
|
---|
433 | AssertCompile(sizeof(pUVM->vm.s) <= sizeof(pUVM->vm.padding));
|
---|
434 |
|
---|
435 | pUVM->vm.s.ppAtStateNext = &pUVM->vm.s.pAtState;
|
---|
436 | pUVM->vm.s.ppAtErrorNext = &pUVM->vm.s.pAtError;
|
---|
437 | pUVM->vm.s.ppAtRuntimeErrorNext = &pUVM->vm.s.pAtRuntimeError;
|
---|
438 |
|
---|
439 | pUVM->vm.s.enmHaltMethod = VMHALTMETHOD_BOOTSTRAP;
|
---|
440 |
|
---|
441 | /* Initialize the VMCPU array in the UVM. */
|
---|
442 | for (i = 0; i < cCpus; i++)
|
---|
443 | {
|
---|
444 | pUVM->aCpus[i].pUVM = pUVM;
|
---|
445 | pUVM->aCpus[i].idCpu = i;
|
---|
446 | }
|
---|
447 |
|
---|
448 | /* Allocate a TLS entry to store the VMINTUSERPERVMCPU pointer. */
|
---|
449 | int rc = RTTlsAllocEx(&pUVM->vm.s.idxTLS, NULL);
|
---|
450 | AssertRC(rc);
|
---|
451 | if (RT_SUCCESS(rc))
|
---|
452 | {
|
---|
453 | /* Allocate a halt method event semaphore for each VCPU. */
|
---|
454 | for (i = 0; i < cCpus; i++)
|
---|
455 | pUVM->aCpus[i].vm.s.EventSemWait = NIL_RTSEMEVENT;
|
---|
456 | for (i = 0; i < cCpus; i++)
|
---|
457 | {
|
---|
458 | rc = RTSemEventCreate(&pUVM->aCpus[i].vm.s.EventSemWait);
|
---|
459 | if (RT_FAILURE(rc))
|
---|
460 | break;
|
---|
461 | }
|
---|
462 | if (RT_SUCCESS(rc))
|
---|
463 | {
|
---|
464 | rc = RTCritSectInit(&pUVM->vm.s.AtStateCritSect);
|
---|
465 | if (RT_SUCCESS(rc))
|
---|
466 | {
|
---|
467 | rc = RTCritSectInit(&pUVM->vm.s.AtErrorCritSect);
|
---|
468 | if (RT_SUCCESS(rc))
|
---|
469 | {
|
---|
470 | /*
|
---|
471 | * Init fundamental (sub-)components - STAM, MMR3Heap and PDMLdr.
|
---|
472 | */
|
---|
473 | rc = STAMR3InitUVM(pUVM);
|
---|
474 | if (RT_SUCCESS(rc))
|
---|
475 | {
|
---|
476 | rc = MMR3InitUVM(pUVM);
|
---|
477 | if (RT_SUCCESS(rc))
|
---|
478 | {
|
---|
479 | rc = PDMR3InitUVM(pUVM);
|
---|
480 | if (RT_SUCCESS(rc))
|
---|
481 | {
|
---|
482 | /*
|
---|
483 | * Start the emulation threads for all VMCPUs.
|
---|
484 | */
|
---|
485 | for (i = 0; i < cCpus; i++)
|
---|
486 | {
|
---|
487 | rc = RTThreadCreateF(&pUVM->aCpus[i].vm.s.ThreadEMT, vmR3EmulationThread, &pUVM->aCpus[i], _1M,
|
---|
488 | RTTHREADTYPE_EMULATION, RTTHREADFLAGS_WAITABLE,
|
---|
489 | cCpus > 1 ? "EMT-%u" : "EMT", i);
|
---|
490 | if (RT_FAILURE(rc))
|
---|
491 | break;
|
---|
492 |
|
---|
493 | pUVM->aCpus[i].vm.s.NativeThreadEMT = RTThreadGetNative(pUVM->aCpus[i].vm.s.ThreadEMT);
|
---|
494 | }
|
---|
495 |
|
---|
496 | if (RT_SUCCESS(rc))
|
---|
497 | {
|
---|
498 | *ppUVM = pUVM;
|
---|
499 | return VINF_SUCCESS;
|
---|
500 | }
|
---|
501 |
|
---|
502 | /* bail out. */
|
---|
503 | while (i-- > 0)
|
---|
504 | {
|
---|
505 | /** @todo rainy day: terminate the EMTs. */
|
---|
506 | }
|
---|
507 | PDMR3TermUVM(pUVM);
|
---|
508 | }
|
---|
509 | MMR3TermUVM(pUVM);
|
---|
510 | }
|
---|
511 | STAMR3TermUVM(pUVM);
|
---|
512 | }
|
---|
513 | RTCritSectDelete(&pUVM->vm.s.AtErrorCritSect);
|
---|
514 | }
|
---|
515 | RTCritSectDelete(&pUVM->vm.s.AtStateCritSect);
|
---|
516 | }
|
---|
517 | }
|
---|
518 | for (i = 0; i < cCpus; i++)
|
---|
519 | {
|
---|
520 | RTSemEventDestroy(pUVM->aCpus[i].vm.s.EventSemWait);
|
---|
521 | pUVM->aCpus[i].vm.s.EventSemWait = NIL_RTSEMEVENT;
|
---|
522 | }
|
---|
523 | RTTlsFree(pUVM->vm.s.idxTLS);
|
---|
524 | }
|
---|
525 | RTMemPageFree(pUVM);
|
---|
526 | return rc;
|
---|
527 | }
|
---|
528 |
|
---|
529 |
|
---|
530 | /**
|
---|
531 | * Creates and initializes the VM.
|
---|
532 | *
|
---|
533 | * @thread EMT
|
---|
534 | */
|
---|
535 | static int vmR3CreateU(PUVM pUVM, uint32_t cCpus, PFNCFGMCONSTRUCTOR pfnCFGMConstructor, void *pvUserCFGM)
|
---|
536 | {
|
---|
537 | int rc = VINF_SUCCESS;
|
---|
538 |
|
---|
539 | /*
|
---|
540 | * Load the VMMR0.r0 module so that we can call GVMMR0CreateVM.
|
---|
541 | */
|
---|
542 | rc = PDMR3LdrLoadVMMR0U(pUVM);
|
---|
543 | if (RT_FAILURE(rc))
|
---|
544 | {
|
---|
545 | /** @todo we need a cleaner solution for this (VERR_VMX_IN_VMX_ROOT_MODE).
|
---|
546 | * bird: what about moving the message down here? Main picks the first message, right? */
|
---|
547 | if (rc == VERR_VMX_IN_VMX_ROOT_MODE)
|
---|
548 | return rc; /* proper error message set later on */
|
---|
549 | return vmR3SetErrorU(pUVM, rc, RT_SRC_POS, N_("Failed to load VMMR0.r0"));
|
---|
550 | }
|
---|
551 |
|
---|
552 | /*
|
---|
553 | * Request GVMM to create a new VM for us.
|
---|
554 | */
|
---|
555 | GVMMCREATEVMREQ CreateVMReq;
|
---|
556 | CreateVMReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
557 | CreateVMReq.Hdr.cbReq = sizeof(CreateVMReq);
|
---|
558 | CreateVMReq.pSession = pUVM->vm.s.pSession;
|
---|
559 | CreateVMReq.pVMR0 = NIL_RTR0PTR;
|
---|
560 | CreateVMReq.pVMR3 = NULL;
|
---|
561 | CreateVMReq.cCpus = cCpus;
|
---|
562 | rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_GVMM_CREATE_VM, 0, &CreateVMReq.Hdr);
|
---|
563 | if (RT_SUCCESS(rc))
|
---|
564 | {
|
---|
565 | PVM pVM = pUVM->pVM = CreateVMReq.pVMR3;
|
---|
566 | AssertRelease(VALID_PTR(pVM));
|
---|
567 | AssertRelease(pVM->pVMR0 == CreateVMReq.pVMR0);
|
---|
568 | AssertRelease(pVM->pSession == pUVM->vm.s.pSession);
|
---|
569 | AssertRelease(pVM->cCpus == cCpus);
|
---|
570 | AssertRelease(pVM->offVMCPU == RT_UOFFSETOF(VM, aCpus));
|
---|
571 |
|
---|
572 | Log(("VMR3Create: Created pUVM=%p pVM=%p pVMR0=%p hSelf=%#x cCpus=%RU32\n",
|
---|
573 | pUVM, pVM, pVM->pVMR0, pVM->hSelf, pVM->cCpus));
|
---|
574 |
|
---|
575 | /*
|
---|
576 | * Initialize the VM structure and our internal data (VMINT).
|
---|
577 | */
|
---|
578 | pVM->pUVM = pUVM;
|
---|
579 |
|
---|
580 | for (VMCPUID i = 0; i < pVM->cCpus; i++)
|
---|
581 | {
|
---|
582 | pVM->aCpus[i].pUVCpu = &pUVM->aCpus[i];
|
---|
583 | pVM->aCpus[i].idCpu = i;
|
---|
584 | pVM->aCpus[i].hNativeThread = pUVM->aCpus[i].vm.s.NativeThreadEMT;
|
---|
585 | Assert(pVM->aCpus[i].hNativeThread != NIL_RTNATIVETHREAD);
|
---|
586 |
|
---|
587 | pUVM->aCpus[i].pVCpu = &pVM->aCpus[i];
|
---|
588 | pUVM->aCpus[i].pVM = pVM;
|
---|
589 | }
|
---|
590 |
|
---|
591 |
|
---|
592 | /*
|
---|
593 | * Init the configuration.
|
---|
594 | */
|
---|
595 | rc = CFGMR3Init(pVM, pfnCFGMConstructor, pvUserCFGM);
|
---|
596 | if (RT_SUCCESS(rc))
|
---|
597 | {
|
---|
598 | rc = CFGMR3QueryBoolDef(CFGMR3GetRoot(pVM), "HwVirtExtForced", &pVM->fHwVirtExtForced, false);
|
---|
599 | if (RT_SUCCESS(rc) && pVM->fHwVirtExtForced)
|
---|
600 | pVM->fHWACCMEnabled = true;
|
---|
601 |
|
---|
602 | /*
|
---|
603 | * If executing in fake suplib mode disable RR3 and RR0 in the config.
|
---|
604 | */
|
---|
605 | const char *psz = RTEnvGet("VBOX_SUPLIB_FAKE");
|
---|
606 | if (psz && !strcmp(psz, "fake"))
|
---|
607 | {
|
---|
608 | CFGMR3RemoveValue(CFGMR3GetRoot(pVM), "RawR3Enabled");
|
---|
609 | CFGMR3InsertInteger(CFGMR3GetRoot(pVM), "RawR3Enabled", 0);
|
---|
610 | CFGMR3RemoveValue(CFGMR3GetRoot(pVM), "RawR0Enabled");
|
---|
611 | CFGMR3InsertInteger(CFGMR3GetRoot(pVM), "RawR0Enabled", 0);
|
---|
612 | }
|
---|
613 |
|
---|
614 | /*
|
---|
615 | * Make sure the CPU count in the config data matches.
|
---|
616 | */
|
---|
617 | if (RT_SUCCESS(rc))
|
---|
618 | {
|
---|
619 | uint32_t cCPUsCfg;
|
---|
620 | rc = CFGMR3QueryU32Def(CFGMR3GetRoot(pVM), "NumCPUs", &cCPUsCfg, 1);
|
---|
621 | AssertLogRelMsgRC(rc, ("Configuration error: Querying \"NumCPUs\" as integer failed, rc=%Rrc\n", rc));
|
---|
622 | if (RT_SUCCESS(rc) && cCPUsCfg != cCpus)
|
---|
623 | {
|
---|
624 | AssertLogRelMsgFailed(("Configuration error: \"NumCPUs\"=%RU32 and VMR3CreateVM::cCpus=%RU32 does not match!\n",
|
---|
625 | cCPUsCfg, cCpus));
|
---|
626 | rc = VERR_INVALID_PARAMETER;
|
---|
627 | }
|
---|
628 | }
|
---|
629 | if (RT_SUCCESS(rc))
|
---|
630 | {
|
---|
631 | /*
|
---|
632 | * Init the ring-3 components and ring-3 per cpu data, finishing it off
|
---|
633 | * by a relocation round (intermediate context finalization will do this).
|
---|
634 | */
|
---|
635 | rc = vmR3InitRing3(pVM, pUVM);
|
---|
636 | if (RT_SUCCESS(rc))
|
---|
637 | {
|
---|
638 | rc = vmR3InitVMCpu(pVM);
|
---|
639 | if (RT_SUCCESS(rc))
|
---|
640 | rc = PGMR3FinalizeMappings(pVM);
|
---|
641 | if (RT_SUCCESS(rc))
|
---|
642 | {
|
---|
643 |
|
---|
644 | LogFlow(("Ring-3 init succeeded\n"));
|
---|
645 |
|
---|
646 | /*
|
---|
647 | * Init the Ring-0 components.
|
---|
648 | */
|
---|
649 | rc = vmR3InitRing0(pVM);
|
---|
650 | if (RT_SUCCESS(rc))
|
---|
651 | {
|
---|
652 | /* Relocate again, because some switcher fixups depends on R0 init results. */
|
---|
653 | VMR3Relocate(pVM, 0);
|
---|
654 |
|
---|
655 | #ifdef VBOX_WITH_DEBUGGER
|
---|
656 | /*
|
---|
657 | * Init the tcp debugger console if we're building
|
---|
658 | * with debugger support.
|
---|
659 | */
|
---|
660 | void *pvUser = NULL;
|
---|
661 | rc = DBGCTcpCreate(pVM, &pvUser);
|
---|
662 | if ( RT_SUCCESS(rc)
|
---|
663 | || rc == VERR_NET_ADDRESS_IN_USE)
|
---|
664 | {
|
---|
665 | pUVM->vm.s.pvDBGC = pvUser;
|
---|
666 | #endif
|
---|
667 | /*
|
---|
668 | * Init the Guest Context components.
|
---|
669 | */
|
---|
670 | rc = vmR3InitGC(pVM);
|
---|
671 | if (RT_SUCCESS(rc))
|
---|
672 | {
|
---|
673 | /*
|
---|
674 | * Now we can safely set the VM halt method to default.
|
---|
675 | */
|
---|
676 | rc = vmR3SetHaltMethodU(pUVM, VMHALTMETHOD_DEFAULT);
|
---|
677 | if (RT_SUCCESS(rc))
|
---|
678 | {
|
---|
679 | /*
|
---|
680 | * Set the state and link into the global list.
|
---|
681 | */
|
---|
682 | vmR3SetState(pVM, VMSTATE_CREATED, VMSTATE_CREATING);
|
---|
683 | pUVM->pNext = g_pUVMsHead;
|
---|
684 | g_pUVMsHead = pUVM;
|
---|
685 |
|
---|
686 | #ifdef LOG_ENABLED
|
---|
687 | RTLogSetCustomPrefixCallback(NULL, vmR3LogPrefixCallback, pUVM);
|
---|
688 | #endif
|
---|
689 | return VINF_SUCCESS;
|
---|
690 | }
|
---|
691 | }
|
---|
692 | #ifdef VBOX_WITH_DEBUGGER
|
---|
693 | DBGCTcpTerminate(pVM, pUVM->vm.s.pvDBGC);
|
---|
694 | pUVM->vm.s.pvDBGC = NULL;
|
---|
695 | }
|
---|
696 | #endif
|
---|
697 | //..
|
---|
698 | }
|
---|
699 | }
|
---|
700 | vmR3Destroy(pVM);
|
---|
701 | }
|
---|
702 | }
|
---|
703 | //..
|
---|
704 |
|
---|
705 | /* Clean CFGM. */
|
---|
706 | int rc2 = CFGMR3Term(pVM);
|
---|
707 | AssertRC(rc2);
|
---|
708 | }
|
---|
709 |
|
---|
710 | /*
|
---|
711 | * Drop all references to VM and the VMCPU structures, then
|
---|
712 | * tell GVMM to destroy the VM.
|
---|
713 | */
|
---|
714 | pUVM->pVM = NULL;
|
---|
715 | for (VMCPUID i = 0; i < pUVM->cCpus; i++)
|
---|
716 | {
|
---|
717 | pUVM->aCpus[i].pVM = NULL;
|
---|
718 | pUVM->aCpus[i].pVCpu = NULL;
|
---|
719 | }
|
---|
720 | Assert(pUVM->vm.s.enmHaltMethod == VMHALTMETHOD_BOOTSTRAP);
|
---|
721 |
|
---|
722 | if (pUVM->cCpus > 1)
|
---|
723 | {
|
---|
724 | /* Poke the other EMTs since they may have stale pVM and pVCpu references
|
---|
725 | on the stack (see VMR3WaitU for instance) if they've been awakened after
|
---|
726 | VM creation. */
|
---|
727 | for (VMCPUID i = 1; i < pUVM->cCpus; i++)
|
---|
728 | VMR3NotifyCpuFFU(&pUVM->aCpus[i], 0);
|
---|
729 | RTThreadSleep(RT_MIN(100 + 25 *(pUVM->cCpus - 1), 500)); /* very sophisticated */
|
---|
730 | }
|
---|
731 |
|
---|
732 | int rc2 = SUPR3CallVMMR0Ex(CreateVMReq.pVMR0, 0 /*idCpu*/, VMMR0_DO_GVMM_DESTROY_VM, 0, NULL);
|
---|
733 | AssertRC(rc2);
|
---|
734 | }
|
---|
735 | else
|
---|
736 | vmR3SetErrorU(pUVM, rc, RT_SRC_POS, N_("VM creation failed (GVMM)"));
|
---|
737 |
|
---|
738 | LogFlow(("vmR3CreateU: returns %Rrc\n", rc));
|
---|
739 | return rc;
|
---|
740 | }
|
---|
741 |
|
---|
742 |
|
---|
743 | /**
|
---|
744 | * Register the calling EMT with GVM.
|
---|
745 | *
|
---|
746 | * @returns VBox status code.
|
---|
747 | * @param pVM The VM handle.
|
---|
748 | * @param idCpu The Virtual CPU ID.
|
---|
749 | */
|
---|
750 | static DECLCALLBACK(int) vmR3RegisterEMT(PVM pVM, VMCPUID idCpu)
|
---|
751 | {
|
---|
752 | Assert(VMMGetCpuId(pVM) == idCpu);
|
---|
753 | int rc = SUPR3CallVMMR0Ex(pVM->pVMR0, idCpu, VMMR0_DO_GVMM_REGISTER_VMCPU, 0, NULL);
|
---|
754 | if (RT_FAILURE(rc))
|
---|
755 | LogRel(("idCpu=%u rc=%Rrc\n", idCpu, rc));
|
---|
756 | return rc;
|
---|
757 | }
|
---|
758 |
|
---|
759 |
|
---|
760 | /**
|
---|
761 | * Initializes all R3 components of the VM
|
---|
762 | */
|
---|
763 | static int vmR3InitRing3(PVM pVM, PUVM pUVM)
|
---|
764 | {
|
---|
765 | int rc;
|
---|
766 |
|
---|
767 | /*
|
---|
768 | * Register the other EMTs with GVM.
|
---|
769 | */
|
---|
770 | for (VMCPUID idCpu = 1; idCpu < pVM->cCpus; idCpu++)
|
---|
771 | {
|
---|
772 | rc = VMR3ReqCallWaitU(pUVM, idCpu, (PFNRT)vmR3RegisterEMT, 2, pVM, idCpu);
|
---|
773 | if (RT_FAILURE(rc))
|
---|
774 | return rc;
|
---|
775 | }
|
---|
776 |
|
---|
777 | /*
|
---|
778 | * Init all R3 components, the order here might be important.
|
---|
779 | */
|
---|
780 | rc = MMR3Init(pVM);
|
---|
781 | if (RT_SUCCESS(rc))
|
---|
782 | {
|
---|
783 | STAM_REG(pVM, &pVM->StatTotalInGC, STAMTYPE_PROFILE_ADV, "/PROF/VM/InGC", STAMUNIT_TICKS_PER_CALL, "Profiling the total time spent in GC.");
|
---|
784 | STAM_REG(pVM, &pVM->StatSwitcherToGC, STAMTYPE_PROFILE_ADV, "/PROF/VM/SwitchToGC", STAMUNIT_TICKS_PER_CALL, "Profiling switching to GC.");
|
---|
785 | STAM_REG(pVM, &pVM->StatSwitcherToHC, STAMTYPE_PROFILE_ADV, "/PROF/VM/SwitchToHC", STAMUNIT_TICKS_PER_CALL, "Profiling switching to HC.");
|
---|
786 | STAM_REG(pVM, &pVM->StatSwitcherSaveRegs, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/SaveRegs", STAMUNIT_TICKS_PER_CALL,"Profiling switching to GC.");
|
---|
787 | STAM_REG(pVM, &pVM->StatSwitcherSysEnter, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/SysEnter", STAMUNIT_TICKS_PER_CALL,"Profiling switching to GC.");
|
---|
788 | STAM_REG(pVM, &pVM->StatSwitcherDebug, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/Debug", STAMUNIT_TICKS_PER_CALL,"Profiling switching to GC.");
|
---|
789 | STAM_REG(pVM, &pVM->StatSwitcherCR0, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/CR0", STAMUNIT_TICKS_PER_CALL, "Profiling switching to GC.");
|
---|
790 | STAM_REG(pVM, &pVM->StatSwitcherCR4, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/CR4", STAMUNIT_TICKS_PER_CALL, "Profiling switching to GC.");
|
---|
791 | STAM_REG(pVM, &pVM->StatSwitcherLgdt, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/Lgdt", STAMUNIT_TICKS_PER_CALL, "Profiling switching to GC.");
|
---|
792 | STAM_REG(pVM, &pVM->StatSwitcherLidt, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/Lidt", STAMUNIT_TICKS_PER_CALL, "Profiling switching to GC.");
|
---|
793 | STAM_REG(pVM, &pVM->StatSwitcherLldt, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/Lldt", STAMUNIT_TICKS_PER_CALL, "Profiling switching to GC.");
|
---|
794 | STAM_REG(pVM, &pVM->StatSwitcherTSS, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/TSS", STAMUNIT_TICKS_PER_CALL, "Profiling switching to GC.");
|
---|
795 | STAM_REG(pVM, &pVM->StatSwitcherJmpCR3, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/JmpCR3", STAMUNIT_TICKS_PER_CALL,"Profiling switching to GC.");
|
---|
796 | STAM_REG(pVM, &pVM->StatSwitcherRstrRegs, STAMTYPE_PROFILE_ADV, "/VM/Switcher/ToGC/RstrRegs", STAMUNIT_TICKS_PER_CALL,"Profiling switching to GC.");
|
---|
797 |
|
---|
798 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
799 | {
|
---|
800 | rc = STAMR3RegisterF(pVM, &pUVM->aCpus[idCpu].vm.s.StatHaltYield, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling halted state yielding.", "/PROF/VM/CPU%d/Halt/Yield", idCpu);
|
---|
801 | AssertRC(rc);
|
---|
802 | rc = STAMR3RegisterF(pVM, &pUVM->aCpus[idCpu].vm.s.StatHaltBlock, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling halted state blocking.", "/PROF/VM/CPU%d/Halt/Block", idCpu);
|
---|
803 | AssertRC(rc);
|
---|
804 | rc = STAMR3RegisterF(pVM, &pUVM->aCpus[idCpu].vm.s.StatHaltTimers, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling halted state timer tasks.", "/PROF/VM/CPU%d/Halt/Timers", idCpu);
|
---|
805 | AssertRC(rc);
|
---|
806 | }
|
---|
807 |
|
---|
808 | STAM_REG(pVM, &pUVM->vm.s.StatReqAllocNew, STAMTYPE_COUNTER, "/VM/Req/AllocNew", STAMUNIT_OCCURENCES, "Number of VMR3ReqAlloc returning a new packet.");
|
---|
809 | STAM_REG(pVM, &pUVM->vm.s.StatReqAllocRaces, STAMTYPE_COUNTER, "/VM/Req/AllocRaces", STAMUNIT_OCCURENCES, "Number of VMR3ReqAlloc causing races.");
|
---|
810 | STAM_REG(pVM, &pUVM->vm.s.StatReqAllocRecycled, STAMTYPE_COUNTER, "/VM/Req/AllocRecycled", STAMUNIT_OCCURENCES, "Number of VMR3ReqAlloc returning a recycled packet.");
|
---|
811 | STAM_REG(pVM, &pUVM->vm.s.StatReqFree, STAMTYPE_COUNTER, "/VM/Req/Free", STAMUNIT_OCCURENCES, "Number of VMR3ReqFree calls.");
|
---|
812 | STAM_REG(pVM, &pUVM->vm.s.StatReqFreeOverflow, STAMTYPE_COUNTER, "/VM/Req/FreeOverflow", STAMUNIT_OCCURENCES, "Number of times the request was actually freed.");
|
---|
813 | STAM_REG(pVM, &pUVM->vm.s.StatReqProcessed, STAMTYPE_COUNTER, "/VM/Req/Processed", STAMUNIT_OCCURENCES, "Number of processed requests (any queue).");
|
---|
814 | STAM_REG(pVM, &pUVM->vm.s.StatReqMoreThan1, STAMTYPE_COUNTER, "/VM/Req/MoreThan1", STAMUNIT_OCCURENCES, "Number of times there are more than one request on the queue when processing it.");
|
---|
815 | STAM_REG(pVM, &pUVM->vm.s.StatReqPushBackRaces, STAMTYPE_COUNTER, "/VM/Req/PushBackRaces", STAMUNIT_OCCURENCES, "Number of push back races.");
|
---|
816 |
|
---|
817 | rc = CPUMR3Init(pVM);
|
---|
818 | if (RT_SUCCESS(rc))
|
---|
819 | {
|
---|
820 | rc = HWACCMR3Init(pVM);
|
---|
821 | if (RT_SUCCESS(rc))
|
---|
822 | {
|
---|
823 | rc = PGMR3Init(pVM);
|
---|
824 | if (RT_SUCCESS(rc))
|
---|
825 | {
|
---|
826 | rc = REMR3Init(pVM);
|
---|
827 | if (RT_SUCCESS(rc))
|
---|
828 | {
|
---|
829 | rc = MMR3InitPaging(pVM);
|
---|
830 | if (RT_SUCCESS(rc))
|
---|
831 | rc = TMR3Init(pVM);
|
---|
832 | if (RT_SUCCESS(rc))
|
---|
833 | {
|
---|
834 | rc = VMMR3Init(pVM);
|
---|
835 | if (RT_SUCCESS(rc))
|
---|
836 | {
|
---|
837 | rc = SELMR3Init(pVM);
|
---|
838 | if (RT_SUCCESS(rc))
|
---|
839 | {
|
---|
840 | rc = TRPMR3Init(pVM);
|
---|
841 | if (RT_SUCCESS(rc))
|
---|
842 | {
|
---|
843 | rc = CSAMR3Init(pVM);
|
---|
844 | if (RT_SUCCESS(rc))
|
---|
845 | {
|
---|
846 | rc = PATMR3Init(pVM);
|
---|
847 | if (RT_SUCCESS(rc))
|
---|
848 | {
|
---|
849 | #ifdef VBOX_WITH_VMI
|
---|
850 | rc = PARAVR3Init(pVM);
|
---|
851 | if (RT_SUCCESS(rc))
|
---|
852 | {
|
---|
853 | #endif
|
---|
854 | rc = IOMR3Init(pVM);
|
---|
855 | if (RT_SUCCESS(rc))
|
---|
856 | {
|
---|
857 | rc = EMR3Init(pVM);
|
---|
858 | if (RT_SUCCESS(rc))
|
---|
859 | {
|
---|
860 | rc = DBGFR3Init(pVM);
|
---|
861 | if (RT_SUCCESS(rc))
|
---|
862 | {
|
---|
863 | rc = PDMR3Init(pVM);
|
---|
864 | if (RT_SUCCESS(rc))
|
---|
865 | {
|
---|
866 | rc = PGMR3InitDynMap(pVM);
|
---|
867 | if (RT_SUCCESS(rc))
|
---|
868 | rc = MMR3HyperInitFinalize(pVM);
|
---|
869 | if (RT_SUCCESS(rc))
|
---|
870 | rc = PATMR3InitFinalize(pVM);
|
---|
871 | if (RT_SUCCESS(rc))
|
---|
872 | rc = PGMR3InitFinalize(pVM);
|
---|
873 | if (RT_SUCCESS(rc))
|
---|
874 | rc = SELMR3InitFinalize(pVM);
|
---|
875 | if (RT_SUCCESS(rc))
|
---|
876 | rc = TMR3InitFinalize(pVM);
|
---|
877 | if (RT_SUCCESS(rc))
|
---|
878 | rc = VMMR3InitFinalize(pVM);
|
---|
879 | if (RT_SUCCESS(rc))
|
---|
880 | rc = REMR3InitFinalize(pVM);
|
---|
881 | if (RT_SUCCESS(rc))
|
---|
882 | rc = vmR3InitDoCompleted(pVM, VMINITCOMPLETED_RING3);
|
---|
883 | if (RT_SUCCESS(rc))
|
---|
884 | {
|
---|
885 | LogFlow(("vmR3InitRing3: returns %Rrc\n", VINF_SUCCESS));
|
---|
886 | return VINF_SUCCESS;
|
---|
887 | }
|
---|
888 | int rc2 = PDMR3Term(pVM);
|
---|
889 | AssertRC(rc2);
|
---|
890 | }
|
---|
891 | int rc2 = DBGFR3Term(pVM);
|
---|
892 | AssertRC(rc2);
|
---|
893 | }
|
---|
894 | int rc2 = EMR3Term(pVM);
|
---|
895 | AssertRC(rc2);
|
---|
896 | }
|
---|
897 | int rc2 = IOMR3Term(pVM);
|
---|
898 | AssertRC(rc2);
|
---|
899 | }
|
---|
900 | #ifdef VBOX_WITH_VMI
|
---|
901 | int rc2 = PARAVR3Term(pVM);
|
---|
902 | AssertRC(rc2);
|
---|
903 | }
|
---|
904 | #endif
|
---|
905 | int rc2 = PATMR3Term(pVM);
|
---|
906 | AssertRC(rc2);
|
---|
907 | }
|
---|
908 | int rc2 = CSAMR3Term(pVM);
|
---|
909 | AssertRC(rc2);
|
---|
910 | }
|
---|
911 | int rc2 = TRPMR3Term(pVM);
|
---|
912 | AssertRC(rc2);
|
---|
913 | }
|
---|
914 | int rc2 = SELMR3Term(pVM);
|
---|
915 | AssertRC(rc2);
|
---|
916 | }
|
---|
917 | int rc2 = VMMR3Term(pVM);
|
---|
918 | AssertRC(rc2);
|
---|
919 | }
|
---|
920 | int rc2 = TMR3Term(pVM);
|
---|
921 | AssertRC(rc2);
|
---|
922 | }
|
---|
923 | int rc2 = REMR3Term(pVM);
|
---|
924 | AssertRC(rc2);
|
---|
925 | }
|
---|
926 | int rc2 = PGMR3Term(pVM);
|
---|
927 | AssertRC(rc2);
|
---|
928 | }
|
---|
929 | int rc2 = HWACCMR3Term(pVM);
|
---|
930 | AssertRC(rc2);
|
---|
931 | }
|
---|
932 | //int rc2 = CPUMR3Term(pVM);
|
---|
933 | //AssertRC(rc2);
|
---|
934 | }
|
---|
935 | /* MMR3Term is not called here because it'll kill the heap. */
|
---|
936 | }
|
---|
937 |
|
---|
938 | LogFlow(("vmR3InitRing3: returns %Rrc\n", rc));
|
---|
939 | return rc;
|
---|
940 | }
|
---|
941 |
|
---|
942 |
|
---|
943 | /**
|
---|
944 | * Initializes all VM CPU components of the VM
|
---|
945 | */
|
---|
946 | static int vmR3InitVMCpu(PVM pVM)
|
---|
947 | {
|
---|
948 | int rc = VINF_SUCCESS;
|
---|
949 | int rc2;
|
---|
950 |
|
---|
951 | rc = CPUMR3InitCPU(pVM);
|
---|
952 | if (RT_SUCCESS(rc))
|
---|
953 | {
|
---|
954 | rc = HWACCMR3InitCPU(pVM);
|
---|
955 | if (RT_SUCCESS(rc))
|
---|
956 | {
|
---|
957 | rc = PGMR3InitCPU(pVM);
|
---|
958 | if (RT_SUCCESS(rc))
|
---|
959 | {
|
---|
960 | rc = TMR3InitCPU(pVM);
|
---|
961 | if (RT_SUCCESS(rc))
|
---|
962 | {
|
---|
963 | rc = VMMR3InitCPU(pVM);
|
---|
964 | if (RT_SUCCESS(rc))
|
---|
965 | {
|
---|
966 | rc = EMR3InitCPU(pVM);
|
---|
967 | if (RT_SUCCESS(rc))
|
---|
968 | {
|
---|
969 | LogFlow(("vmR3InitVMCpu: returns %Rrc\n", VINF_SUCCESS));
|
---|
970 | return VINF_SUCCESS;
|
---|
971 | }
|
---|
972 |
|
---|
973 | rc2 = VMMR3TermCPU(pVM);
|
---|
974 | AssertRC(rc2);
|
---|
975 | }
|
---|
976 | rc2 = TMR3TermCPU(pVM);
|
---|
977 | AssertRC(rc2);
|
---|
978 | }
|
---|
979 | rc2 = PGMR3TermCPU(pVM);
|
---|
980 | AssertRC(rc2);
|
---|
981 | }
|
---|
982 | rc2 = HWACCMR3TermCPU(pVM);
|
---|
983 | AssertRC(rc2);
|
---|
984 | }
|
---|
985 | rc2 = CPUMR3TermCPU(pVM);
|
---|
986 | AssertRC(rc2);
|
---|
987 | }
|
---|
988 | LogFlow(("vmR3InitVMCpu: returns %Rrc\n", rc));
|
---|
989 | return rc;
|
---|
990 | }
|
---|
991 |
|
---|
992 |
|
---|
993 | /**
|
---|
994 | * Initializes all R0 components of the VM
|
---|
995 | */
|
---|
996 | static int vmR3InitRing0(PVM pVM)
|
---|
997 | {
|
---|
998 | LogFlow(("vmR3InitRing0:\n"));
|
---|
999 |
|
---|
1000 | /*
|
---|
1001 | * Check for FAKE suplib mode.
|
---|
1002 | */
|
---|
1003 | int rc = VINF_SUCCESS;
|
---|
1004 | const char *psz = RTEnvGet("VBOX_SUPLIB_FAKE");
|
---|
1005 | if (!psz || strcmp(psz, "fake"))
|
---|
1006 | {
|
---|
1007 | /*
|
---|
1008 | * Call the VMMR0 component and let it do the init.
|
---|
1009 | */
|
---|
1010 | rc = VMMR3InitR0(pVM);
|
---|
1011 | }
|
---|
1012 | else
|
---|
1013 | Log(("vmR3InitRing0: skipping because of VBOX_SUPLIB_FAKE=fake\n"));
|
---|
1014 |
|
---|
1015 | /*
|
---|
1016 | * Do notifications and return.
|
---|
1017 | */
|
---|
1018 | if (RT_SUCCESS(rc))
|
---|
1019 | rc = vmR3InitDoCompleted(pVM, VMINITCOMPLETED_RING0);
|
---|
1020 |
|
---|
1021 | /** todo: move this to the VMINITCOMPLETED_RING0 notification handler once implemented */
|
---|
1022 | if (RT_SUCCESS(rc))
|
---|
1023 | rc = HWACCMR3InitFinalizeR0(pVM);
|
---|
1024 |
|
---|
1025 | LogFlow(("vmR3InitRing0: returns %Rrc\n", rc));
|
---|
1026 | return rc;
|
---|
1027 | }
|
---|
1028 |
|
---|
1029 |
|
---|
1030 | /**
|
---|
1031 | * Initializes all GC components of the VM
|
---|
1032 | */
|
---|
1033 | static int vmR3InitGC(PVM pVM)
|
---|
1034 | {
|
---|
1035 | LogFlow(("vmR3InitGC:\n"));
|
---|
1036 |
|
---|
1037 | /*
|
---|
1038 | * Check for FAKE suplib mode.
|
---|
1039 | */
|
---|
1040 | int rc = VINF_SUCCESS;
|
---|
1041 | const char *psz = RTEnvGet("VBOX_SUPLIB_FAKE");
|
---|
1042 | if (!psz || strcmp(psz, "fake"))
|
---|
1043 | {
|
---|
1044 | /*
|
---|
1045 | * Call the VMMR0 component and let it do the init.
|
---|
1046 | */
|
---|
1047 | rc = VMMR3InitRC(pVM);
|
---|
1048 | }
|
---|
1049 | else
|
---|
1050 | Log(("vmR3InitGC: skipping because of VBOX_SUPLIB_FAKE=fake\n"));
|
---|
1051 |
|
---|
1052 | /*
|
---|
1053 | * Do notifications and return.
|
---|
1054 | */
|
---|
1055 | if (RT_SUCCESS(rc))
|
---|
1056 | rc = vmR3InitDoCompleted(pVM, VMINITCOMPLETED_GC);
|
---|
1057 | LogFlow(("vmR3InitGC: returns %Rrc\n", rc));
|
---|
1058 | return rc;
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 |
|
---|
1062 | /**
|
---|
1063 | * Do init completed notifications.
|
---|
1064 | * This notifications can fail.
|
---|
1065 | *
|
---|
1066 | * @param pVM The VM handle.
|
---|
1067 | * @param enmWhat What's completed.
|
---|
1068 | */
|
---|
1069 | static int vmR3InitDoCompleted(PVM pVM, VMINITCOMPLETED enmWhat)
|
---|
1070 | {
|
---|
1071 | return VINF_SUCCESS;
|
---|
1072 | }
|
---|
1073 |
|
---|
1074 |
|
---|
1075 | /**
|
---|
1076 | * Logger callback for inserting a custom prefix.
|
---|
1077 | *
|
---|
1078 | * @returns Number of chars written.
|
---|
1079 | * @param pLogger The logger.
|
---|
1080 | * @param pchBuf The output buffer.
|
---|
1081 | * @param cchBuf The output buffer size.
|
---|
1082 | * @param pvUser Pointer to the UVM structure.
|
---|
1083 | */
|
---|
1084 | static DECLCALLBACK(size_t) vmR3LogPrefixCallback(PRTLOGGER pLogger, char *pchBuf, size_t cchBuf, void *pvUser)
|
---|
1085 | {
|
---|
1086 | AssertReturn(cchBuf >= 2, 0);
|
---|
1087 | PUVM pUVM = (PUVM)pvUser;
|
---|
1088 | PUVMCPU pUVCpu = (PUVMCPU)RTTlsGet(pUVM->vm.s.idxTLS);
|
---|
1089 | if (pUVCpu)
|
---|
1090 | {
|
---|
1091 | static const char s_szHex[17] = "0123456789abcdef";
|
---|
1092 | VMCPUID const idCpu = pUVCpu->idCpu;
|
---|
1093 | pchBuf[1] = s_szHex[ idCpu & 15];
|
---|
1094 | pchBuf[0] = s_szHex[(idCpu >> 4) & 15];
|
---|
1095 | }
|
---|
1096 | else
|
---|
1097 | {
|
---|
1098 | pchBuf[0] = 'x';
|
---|
1099 | pchBuf[1] = 'y';
|
---|
1100 | }
|
---|
1101 |
|
---|
1102 | return 2;
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 |
|
---|
1106 | /**
|
---|
1107 | * Calls the relocation functions for all VMM components so they can update
|
---|
1108 | * any GC pointers. When this function is called all the basic VM members
|
---|
1109 | * have been updated and the actual memory relocation have been done
|
---|
1110 | * by the PGM/MM.
|
---|
1111 | *
|
---|
1112 | * This is used both on init and on runtime relocations.
|
---|
1113 | *
|
---|
1114 | * @param pVM VM handle.
|
---|
1115 | * @param offDelta Relocation delta relative to old location.
|
---|
1116 | */
|
---|
1117 | VMMR3DECL(void) VMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
|
---|
1118 | {
|
---|
1119 | LogFlow(("VMR3Relocate: offDelta=%RGv\n", offDelta));
|
---|
1120 |
|
---|
1121 | /*
|
---|
1122 | * The order here is very important!
|
---|
1123 | */
|
---|
1124 | PGMR3Relocate(pVM, offDelta);
|
---|
1125 | PDMR3LdrRelocateU(pVM->pUVM, offDelta);
|
---|
1126 | PGMR3Relocate(pVM, 0); /* Repeat after PDM relocation. */
|
---|
1127 | CPUMR3Relocate(pVM);
|
---|
1128 | HWACCMR3Relocate(pVM);
|
---|
1129 | SELMR3Relocate(pVM);
|
---|
1130 | VMMR3Relocate(pVM, offDelta);
|
---|
1131 | SELMR3Relocate(pVM); /* !hack! fix stack! */
|
---|
1132 | TRPMR3Relocate(pVM, offDelta);
|
---|
1133 | PATMR3Relocate(pVM);
|
---|
1134 | CSAMR3Relocate(pVM, offDelta);
|
---|
1135 | IOMR3Relocate(pVM, offDelta);
|
---|
1136 | EMR3Relocate(pVM);
|
---|
1137 | TMR3Relocate(pVM, offDelta);
|
---|
1138 | DBGFR3Relocate(pVM, offDelta);
|
---|
1139 | PDMR3Relocate(pVM, offDelta);
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 |
|
---|
1143 | /**
|
---|
1144 | * EMT rendezvous worker for VMR3PowerOn.
|
---|
1145 | *
|
---|
1146 | * @returns VERR_VM_INVALID_VM_STATE or VINF_SUCCESS. (This is a strict return
|
---|
1147 | * code, see FNVMMEMTRENDEZVOUS.)
|
---|
1148 | *
|
---|
1149 | * @param pVM The VM handle.
|
---|
1150 | * @param pVCpu The VMCPU handle of the EMT.
|
---|
1151 | * @param pvUser Ignored.
|
---|
1152 | */
|
---|
1153 | static DECLCALLBACK(VBOXSTRICTRC) vmR3PowerOn(PVM pVM, PVMCPU pVCpu, void *pvUser)
|
---|
1154 | {
|
---|
1155 | LogFlow(("vmR3PowerOn: pVM=%p pVCpu=%p/#%u\n", pVM, pVCpu, pVCpu->idCpu));
|
---|
1156 | Assert(!pvUser); NOREF(pvUser);
|
---|
1157 |
|
---|
1158 | /*
|
---|
1159 | * The first thread thru here tries to change the state. We shouldn't be
|
---|
1160 | * called again if this fails.
|
---|
1161 | */
|
---|
1162 | if (pVCpu->idCpu == pVM->cCpus - 1)
|
---|
1163 | {
|
---|
1164 | int rc = vmR3TrySetState(pVM, "VMR3PowerOn", 1, VMSTATE_POWERING_ON, VMSTATE_CREATED);
|
---|
1165 | if (RT_FAILURE(rc))
|
---|
1166 | return rc;
|
---|
1167 | }
|
---|
1168 |
|
---|
1169 | VMSTATE enmVMState = VMR3GetState(pVM);
|
---|
1170 | AssertMsgReturn(enmVMState == VMSTATE_POWERING_ON,
|
---|
1171 | ("%s\n", VMR3GetStateName(enmVMState)),
|
---|
1172 | VERR_INTERNAL_ERROR_4);
|
---|
1173 |
|
---|
1174 | /*
|
---|
1175 | * All EMTs changes their state to started.
|
---|
1176 | */
|
---|
1177 | VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED);
|
---|
1178 |
|
---|
1179 | /*
|
---|
1180 | * EMT(0) is last thru here and it will make the notification calls
|
---|
1181 | * and advance the state.
|
---|
1182 | */
|
---|
1183 | if (pVCpu->idCpu == 0)
|
---|
1184 | {
|
---|
1185 | PDMR3PowerOn(pVM);
|
---|
1186 | vmR3SetState(pVM, VMSTATE_RUNNING, VMSTATE_POWERING_ON);
|
---|
1187 | }
|
---|
1188 |
|
---|
1189 | return VINF_SUCCESS;
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 |
|
---|
1193 | /**
|
---|
1194 | * Powers on the virtual machine.
|
---|
1195 | *
|
---|
1196 | * @returns VBox status code.
|
---|
1197 | *
|
---|
1198 | * @param pVM The VM to power on.
|
---|
1199 | *
|
---|
1200 | * @thread Any thread.
|
---|
1201 | * @vmstate Created
|
---|
1202 | * @vmstateto PoweringOn+Running
|
---|
1203 | */
|
---|
1204 | VMMR3DECL(int) VMR3PowerOn(PVM pVM)
|
---|
1205 | {
|
---|
1206 | LogFlow(("VMR3PowerOn: pVM=%p\n", pVM));
|
---|
1207 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
1208 |
|
---|
1209 | /*
|
---|
1210 | * Gather all the EMTs to reduce the init TSC drift and keep
|
---|
1211 | * the state changing APIs a bit uniform.
|
---|
1212 | */
|
---|
1213 | int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING | VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR,
|
---|
1214 | vmR3PowerOn, NULL);
|
---|
1215 | LogFlow(("VMR3PowerOn: returns %Rrc\n", rc));
|
---|
1216 | return rc;
|
---|
1217 | }
|
---|
1218 |
|
---|
1219 |
|
---|
1220 | /**
|
---|
1221 | * Does the suspend notifications.
|
---|
1222 | *
|
---|
1223 | * @param pVM The VM handle.
|
---|
1224 | * @thread EMT(0)
|
---|
1225 | */
|
---|
1226 | static void vmR3SuspendDoWork(PVM pVM)
|
---|
1227 | {
|
---|
1228 | PDMR3Suspend(pVM);
|
---|
1229 | }
|
---|
1230 |
|
---|
1231 |
|
---|
1232 | /**
|
---|
1233 | * EMT rendezvous worker for VMR3Suspend.
|
---|
1234 | *
|
---|
1235 | * @returns VERR_VM_INVALID_VM_STATE or VINF_EM_SUSPEND. (This is a strict
|
---|
1236 | * return code, see FNVMMEMTRENDEZVOUS.)
|
---|
1237 | *
|
---|
1238 | * @param pVM The VM handle.
|
---|
1239 | * @param pVCpu The VMCPU handle of the EMT.
|
---|
1240 | * @param pvUser Ignored.
|
---|
1241 | */
|
---|
1242 | static DECLCALLBACK(VBOXSTRICTRC) vmR3Suspend(PVM pVM, PVMCPU pVCpu, void *pvUser)
|
---|
1243 | {
|
---|
1244 | LogFlow(("vmR3Suspend: pVM=%p pVCpu=%p/#%u\n", pVM, pVCpu, pVCpu->idCpu));
|
---|
1245 | Assert(!pvUser); NOREF(pvUser);
|
---|
1246 |
|
---|
1247 | /*
|
---|
1248 | * The first EMT switches the state to suspending. If this fails because
|
---|
1249 | * something was racing us in one way or the other, there will be no more
|
---|
1250 | * calls and thus the state assertion below is not going to annoy anyone.
|
---|
1251 | */
|
---|
1252 | if (pVCpu->idCpu == pVM->cCpus - 1)
|
---|
1253 | {
|
---|
1254 | int rc = vmR3TrySetState(pVM, "VMR3Suspend", 2,
|
---|
1255 | VMSTATE_SUSPENDING, VMSTATE_RUNNING,
|
---|
1256 | VMSTATE_SUSPENDING_EXT_LS, VMSTATE_RUNNING_LS);
|
---|
1257 | if (RT_FAILURE(rc))
|
---|
1258 | return rc;
|
---|
1259 | }
|
---|
1260 |
|
---|
1261 | VMSTATE enmVMState = VMR3GetState(pVM);
|
---|
1262 | AssertMsgReturn( enmVMState == VMSTATE_SUSPENDING
|
---|
1263 | || enmVMState == VMSTATE_SUSPENDING_EXT_LS,
|
---|
1264 | ("%s\n", VMR3GetStateName(enmVMState)),
|
---|
1265 | VERR_INTERNAL_ERROR_4);
|
---|
1266 |
|
---|
1267 | /*
|
---|
1268 | * EMT(0) does the actually suspending *after* all the other CPUs have
|
---|
1269 | * been thru here.
|
---|
1270 | */
|
---|
1271 | if (pVCpu->idCpu == 0)
|
---|
1272 | {
|
---|
1273 | vmR3SuspendDoWork(pVM);
|
---|
1274 |
|
---|
1275 | int rc = vmR3TrySetState(pVM, "VMR3Suspend", 2,
|
---|
1276 | VMSTATE_SUSPENDED, VMSTATE_SUSPENDING,
|
---|
1277 | VMSTATE_SUSPENDED_EXT_LS, VMSTATE_SUSPENDING_EXT_LS);
|
---|
1278 | if (RT_FAILURE(rc))
|
---|
1279 | return VERR_INTERNAL_ERROR_3;
|
---|
1280 | }
|
---|
1281 |
|
---|
1282 | return VINF_EM_SUSPEND;
|
---|
1283 | }
|
---|
1284 |
|
---|
1285 |
|
---|
1286 | /**
|
---|
1287 | * Suspends a running VM.
|
---|
1288 | *
|
---|
1289 | * @returns VBox status code. When called on EMT, this will be a strict status
|
---|
1290 | * code that has to be propagated up the call stack.
|
---|
1291 | *
|
---|
1292 | * @param pVM The VM to suspend.
|
---|
1293 | *
|
---|
1294 | * @thread Any thread.
|
---|
1295 | * @vmstate Running or RunningLS
|
---|
1296 | * @vmstateto Suspending + Suspended or SuspendingExtLS + SuspendedExtLS
|
---|
1297 | */
|
---|
1298 | VMMR3DECL(int) VMR3Suspend(PVM pVM)
|
---|
1299 | {
|
---|
1300 | LogFlow(("VMR3Suspend: pVM=%p\n", pVM));
|
---|
1301 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
1302 |
|
---|
1303 | /*
|
---|
1304 | * Gather all the EMTs to make sure there are no races before
|
---|
1305 | * changing the VM state.
|
---|
1306 | */
|
---|
1307 | int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING | VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR,
|
---|
1308 | vmR3Suspend, NULL);
|
---|
1309 | LogFlow(("VMR3Suspend: returns %Rrc\n", rc));
|
---|
1310 | return rc;
|
---|
1311 | }
|
---|
1312 |
|
---|
1313 |
|
---|
1314 | /**
|
---|
1315 | * EMT rendezvous worker for VMR3Resume.
|
---|
1316 | *
|
---|
1317 | * @returns VERR_VM_INVALID_VM_STATE or VINF_EM_RESUME. (This is a strict
|
---|
1318 | * return code, see FNVMMEMTRENDEZVOUS.)
|
---|
1319 | *
|
---|
1320 | * @param pVM The VM handle.
|
---|
1321 | * @param pVCpu The VMCPU handle of the EMT.
|
---|
1322 | * @param pvUser Ignored.
|
---|
1323 | */
|
---|
1324 | static DECLCALLBACK(VBOXSTRICTRC) vmR3Resume(PVM pVM, PVMCPU pVCpu, void *pvUser)
|
---|
1325 | {
|
---|
1326 | LogFlow(("vmR3Resume: pVM=%p pVCpu=%p/#%u\n", pVM, pVCpu, pVCpu->idCpu));
|
---|
1327 | Assert(!pvUser); NOREF(pvUser);
|
---|
1328 |
|
---|
1329 | /*
|
---|
1330 | * The first thread thru here tries to change the state. We shouldn't be
|
---|
1331 | * called again if this fails.
|
---|
1332 | */
|
---|
1333 | if (pVCpu->idCpu == pVM->cCpus - 1)
|
---|
1334 | {
|
---|
1335 | int rc = vmR3TrySetState(pVM, "VMR3Resume", 1, VMSTATE_RESUMING, VMSTATE_SUSPENDED);
|
---|
1336 | if (RT_FAILURE(rc))
|
---|
1337 | return rc;
|
---|
1338 | }
|
---|
1339 |
|
---|
1340 | VMSTATE enmVMState = VMR3GetState(pVM);
|
---|
1341 | AssertMsgReturn(enmVMState == VMSTATE_RESUMING,
|
---|
1342 | ("%s\n", VMR3GetStateName(enmVMState)),
|
---|
1343 | VERR_INTERNAL_ERROR_4);
|
---|
1344 |
|
---|
1345 | #if 0
|
---|
1346 | /*
|
---|
1347 | * All EMTs changes their state to started.
|
---|
1348 | */
|
---|
1349 | VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED);
|
---|
1350 | #endif
|
---|
1351 |
|
---|
1352 | /*
|
---|
1353 | * EMT(0) is last thru here and it will make the notification calls
|
---|
1354 | * and advance the state.
|
---|
1355 | */
|
---|
1356 | if (pVCpu->idCpu == 0)
|
---|
1357 | {
|
---|
1358 | PDMR3Resume(pVM);
|
---|
1359 | vmR3SetState(pVM, VMSTATE_RUNNING, VMSTATE_RESUMING);
|
---|
1360 | pVM->vm.s.fTeleportedAndNotFullyResumedYet = false;
|
---|
1361 | }
|
---|
1362 |
|
---|
1363 | return VINF_EM_RESUME;
|
---|
1364 | }
|
---|
1365 |
|
---|
1366 |
|
---|
1367 | /**
|
---|
1368 | * Resume VM execution.
|
---|
1369 | *
|
---|
1370 | * @returns VBox status code. When called on EMT, this will be a strict status
|
---|
1371 | * code that has to be propagated up the call stack.
|
---|
1372 | *
|
---|
1373 | * @param pVM The VM to resume.
|
---|
1374 | *
|
---|
1375 | * @thread Any thread.
|
---|
1376 | * @vmstate Suspended
|
---|
1377 | * @vmstateto Running
|
---|
1378 | */
|
---|
1379 | VMMR3DECL(int) VMR3Resume(PVM pVM)
|
---|
1380 | {
|
---|
1381 | LogFlow(("VMR3Resume: pVM=%p\n", pVM));
|
---|
1382 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
1383 |
|
---|
1384 | /*
|
---|
1385 | * Gather all the EMTs to make sure there are no races before
|
---|
1386 | * changing the VM state.
|
---|
1387 | */
|
---|
1388 | int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING | VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR,
|
---|
1389 | vmR3Resume, NULL);
|
---|
1390 | LogFlow(("VMR3Resume: returns %Rrc\n", rc));
|
---|
1391 | return rc;
|
---|
1392 | }
|
---|
1393 |
|
---|
1394 |
|
---|
1395 | /**
|
---|
1396 | * EMT rendezvous worker for VMR3Save and VMR3Teleport that suspends the VM
|
---|
1397 | * after the live step has been completed.
|
---|
1398 | *
|
---|
1399 | * @returns VERR_VM_INVALID_VM_STATE or VINF_EM_RESUME. (This is a strict
|
---|
1400 | * return code, see FNVMMEMTRENDEZVOUS.)
|
---|
1401 | *
|
---|
1402 | * @param pVM The VM handle.
|
---|
1403 | * @param pVCpu The VMCPU handle of the EMT.
|
---|
1404 | * @param pvUser The pfSuspended argument of vmR3SaveTeleport.
|
---|
1405 | */
|
---|
1406 | static DECLCALLBACK(VBOXSTRICTRC) vmR3LiveDoSuspend(PVM pVM, PVMCPU pVCpu, void *pvUser)
|
---|
1407 | {
|
---|
1408 | LogFlow(("vmR3LiveDoSuspend: pVM=%p pVCpu=%p/#%u\n", pVM, pVCpu, pVCpu->idCpu));
|
---|
1409 | bool *pfSuspended = (bool *)pvUser;
|
---|
1410 |
|
---|
1411 | /*
|
---|
1412 | * The first thread thru here tries to change the state. We shouldn't be
|
---|
1413 | * called again if this fails.
|
---|
1414 | */
|
---|
1415 | if (pVCpu->idCpu == pVM->cCpus - 1U)
|
---|
1416 | {
|
---|
1417 | PUVM pUVM = pVM->pUVM;
|
---|
1418 | int rc;
|
---|
1419 |
|
---|
1420 | RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
|
---|
1421 | VMSTATE enmVMState = pVM->enmVMState;
|
---|
1422 | switch (enmVMState)
|
---|
1423 | {
|
---|
1424 | case VMSTATE_RUNNING_LS:
|
---|
1425 | vmR3SetStateLocked(pVM, pUVM, VMSTATE_SUSPENDING_LS, VMSTATE_RUNNING_LS);
|
---|
1426 | rc = VINF_SUCCESS;
|
---|
1427 | break;
|
---|
1428 |
|
---|
1429 | case VMSTATE_SUSPENDED_EXT_LS:
|
---|
1430 | case VMSTATE_SUSPENDED_LS: /* (via reset) */
|
---|
1431 | rc = VINF_SUCCESS;
|
---|
1432 | break;
|
---|
1433 |
|
---|
1434 | case VMSTATE_DEBUGGING_LS:
|
---|
1435 | rc = VERR_TRY_AGAIN;
|
---|
1436 | break;
|
---|
1437 |
|
---|
1438 | case VMSTATE_OFF_LS:
|
---|
1439 | vmR3SetStateLocked(pVM, pUVM, VMSTATE_OFF, VMSTATE_OFF_LS);
|
---|
1440 | rc = VERR_SSM_LIVE_POWERED_OFF;
|
---|
1441 | break;
|
---|
1442 |
|
---|
1443 | case VMSTATE_FATAL_ERROR_LS:
|
---|
1444 | vmR3SetStateLocked(pVM, pUVM, VMSTATE_FATAL_ERROR, VMSTATE_FATAL_ERROR_LS);
|
---|
1445 | rc = VERR_SSM_LIVE_FATAL_ERROR;
|
---|
1446 | break;
|
---|
1447 |
|
---|
1448 | case VMSTATE_GURU_MEDITATION_LS:
|
---|
1449 | vmR3SetStateLocked(pVM, pUVM, VMSTATE_GURU_MEDITATION, VMSTATE_GURU_MEDITATION_LS);
|
---|
1450 | rc = VERR_SSM_LIVE_GURU_MEDITATION;
|
---|
1451 | break;
|
---|
1452 |
|
---|
1453 | case VMSTATE_POWERING_OFF_LS:
|
---|
1454 | case VMSTATE_SUSPENDING_EXT_LS:
|
---|
1455 | case VMSTATE_RESETTING_LS:
|
---|
1456 | default:
|
---|
1457 | AssertMsgFailed(("%s\n", VMR3GetStateName(enmVMState)));
|
---|
1458 | rc = VERR_INTERNAL_ERROR_3;
|
---|
1459 | break;
|
---|
1460 | }
|
---|
1461 | RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
|
---|
1462 | if (RT_FAILURE(rc))
|
---|
1463 | {
|
---|
1464 | LogFlow(("vmR3LiveDoSuspend: returns %Rrc (state was %s)\n", rc, VMR3GetStateName(enmVMState)));
|
---|
1465 | return rc;
|
---|
1466 | }
|
---|
1467 | }
|
---|
1468 |
|
---|
1469 | VMSTATE enmVMState = VMR3GetState(pVM);
|
---|
1470 | AssertMsgReturn(enmVMState == VMSTATE_SUSPENDING_LS,
|
---|
1471 | ("%s\n", VMR3GetStateName(enmVMState)),
|
---|
1472 | VERR_INTERNAL_ERROR_4);
|
---|
1473 |
|
---|
1474 | /*
|
---|
1475 | * Only EMT(0) have work to do since it's last thru here.
|
---|
1476 | */
|
---|
1477 | if (pVCpu->idCpu == 0)
|
---|
1478 | {
|
---|
1479 | vmR3SuspendDoWork(pVM);
|
---|
1480 | int rc = vmR3TrySetState(pVM, "VMR3Suspend", 1,
|
---|
1481 | VMSTATE_SUSPENDED_LS, VMSTATE_SUSPENDING_LS);
|
---|
1482 | if (RT_FAILURE(rc))
|
---|
1483 | return VERR_INTERNAL_ERROR_3;
|
---|
1484 |
|
---|
1485 | *pfSuspended = true;
|
---|
1486 | }
|
---|
1487 |
|
---|
1488 | return VINF_EM_SUSPEND;
|
---|
1489 | }
|
---|
1490 |
|
---|
1491 |
|
---|
1492 | /**
|
---|
1493 | * EMT rendezvous worker that VMR3Save and VMR3Teleport uses to clean up a
|
---|
1494 | * SSMR3LiveDoStep1 failure.
|
---|
1495 | *
|
---|
1496 | * Doing this as a rendezvous operation avoids all annoying transition
|
---|
1497 | * states.
|
---|
1498 | *
|
---|
1499 | * @returns VERR_VM_INVALID_VM_STATE, VINF_SUCCESS or some specific VERR_SSM_*
|
---|
1500 | * status code. (This is a strict return code, see FNVMMEMTRENDEZVOUS.)
|
---|
1501 | *
|
---|
1502 | * @param pVM The VM handle.
|
---|
1503 | * @param pVCpu The VMCPU handle of the EMT.
|
---|
1504 | * @param pvUser The pfSuspended argument of vmR3SaveTeleport.
|
---|
1505 | */
|
---|
1506 | static DECLCALLBACK(VBOXSTRICTRC) vmR3LiveDoStep1Cleanup(PVM pVM, PVMCPU pVCpu, void *pvUser)
|
---|
1507 | {
|
---|
1508 | LogFlow(("vmR3LiveDoStep1Cleanup: pVM=%p pVCpu=%p/#%u\n", pVM, pVCpu, pVCpu->idCpu));
|
---|
1509 | bool *pfSuspended = (bool *)pvUser;
|
---|
1510 | NOREF(pVCpu);
|
---|
1511 |
|
---|
1512 | int rc = vmR3TrySetState(pVM, "vmR3LiveDoStep1Cleanup", 6,
|
---|
1513 | VMSTATE_OFF, VMSTATE_OFF_LS, /* 1 */
|
---|
1514 | VMSTATE_FATAL_ERROR, VMSTATE_FATAL_ERROR_LS, /* 2 */
|
---|
1515 | VMSTATE_GURU_MEDITATION, VMSTATE_GURU_MEDITATION_LS, /* 3 */
|
---|
1516 | VMSTATE_SUSPENDED, VMSTATE_SUSPENDED_LS, /* 4 */
|
---|
1517 | VMSTATE_SUSPENDED, VMSTATE_SUSPENDED_EXT_LS,
|
---|
1518 | VMSTATE_RUNNING, VMSTATE_RUNNING_LS,
|
---|
1519 | VMSTATE_DEBUGGING, VMSTATE_DEBUGGING_LS);
|
---|
1520 | if (rc == 1)
|
---|
1521 | rc = VERR_SSM_LIVE_POWERED_OFF;
|
---|
1522 | else if (rc == 2)
|
---|
1523 | rc = VERR_SSM_LIVE_FATAL_ERROR;
|
---|
1524 | else if (rc == 3)
|
---|
1525 | rc = VERR_SSM_LIVE_GURU_MEDITATION;
|
---|
1526 | else if (rc == 4)
|
---|
1527 | {
|
---|
1528 | *pfSuspended = true;
|
---|
1529 | rc = VINF_SUCCESS;
|
---|
1530 | }
|
---|
1531 | else if (rc > 0)
|
---|
1532 | rc = VINF_SUCCESS;
|
---|
1533 | return rc;
|
---|
1534 | }
|
---|
1535 |
|
---|
1536 |
|
---|
1537 | /**
|
---|
1538 | * EMT(0) worker for VMR3Save and VMR3Teleport that completes the live save.
|
---|
1539 | *
|
---|
1540 | * @returns VBox status code.
|
---|
1541 | * @retval VINF_SSM_LIVE_SUSPENDED if VMR3Suspend was called.
|
---|
1542 | *
|
---|
1543 | * @param pVM The VM handle.
|
---|
1544 | * @param pSSM The handle of saved state operation.
|
---|
1545 | *
|
---|
1546 | * @thread EMT(0)
|
---|
1547 | */
|
---|
1548 | static DECLCALLBACK(int) vmR3LiveDoStep2(PVM pVM, PSSMHANDLE pSSM)
|
---|
1549 | {
|
---|
1550 | LogFlow(("vmR3LiveDoStep2: pVM=%p pSSM=%p\n", pVM, pSSM));
|
---|
1551 | VM_ASSERT_EMT0(pVM);
|
---|
1552 |
|
---|
1553 | /*
|
---|
1554 | * Advance the state and mark if VMR3Suspend was called.
|
---|
1555 | */
|
---|
1556 | int rc = VINF_SUCCESS;
|
---|
1557 | VMSTATE enmVMState = VMR3GetState(pVM);
|
---|
1558 | if (enmVMState == VMSTATE_SUSPENDED_LS)
|
---|
1559 | vmR3SetState(pVM, VMSTATE_SAVING, VMSTATE_SUSPENDED_LS);
|
---|
1560 | else
|
---|
1561 | {
|
---|
1562 | if (enmVMState != VMSTATE_SAVING)
|
---|
1563 | vmR3SetState(pVM, VMSTATE_SAVING, VMSTATE_SUSPENDED_EXT_LS);
|
---|
1564 | rc = VINF_SSM_LIVE_SUSPENDED;
|
---|
1565 | }
|
---|
1566 |
|
---|
1567 | /*
|
---|
1568 | * Finish up and release the handle. Careful with the status codes.
|
---|
1569 | */
|
---|
1570 | int rc2 = SSMR3LiveDoStep2(pSSM);
|
---|
1571 | if (rc == VINF_SUCCESS || (RT_FAILURE(rc2) && RT_SUCCESS(rc)))
|
---|
1572 | rc = rc2;
|
---|
1573 |
|
---|
1574 | rc2 = SSMR3LiveDone(pSSM);
|
---|
1575 | if (rc == VINF_SUCCESS || (RT_FAILURE(rc2) && RT_SUCCESS(rc)))
|
---|
1576 | rc = rc2;
|
---|
1577 |
|
---|
1578 | /*
|
---|
1579 | * Advance to the final state and return.
|
---|
1580 | */
|
---|
1581 | vmR3SetState(pVM, VMSTATE_SUSPENDED, VMSTATE_SAVING);
|
---|
1582 | Assert(rc > VINF_EM_LAST || rc < VINF_EM_FIRST);
|
---|
1583 | return rc;
|
---|
1584 | }
|
---|
1585 |
|
---|
1586 |
|
---|
1587 | /**
|
---|
1588 | * Worker for vmR3SaveTeleport that validates the state and calls SSMR3Save or
|
---|
1589 | * SSMR3LiveSave.
|
---|
1590 | *
|
---|
1591 | * @returns VBox status code.
|
---|
1592 | *
|
---|
1593 | * @param pVM The VM handle.
|
---|
1594 | * @param cMsMaxDowntime The maximum downtime given as milliseconds.
|
---|
1595 | * @param pszFilename The name of the file. NULL if pStreamOps is used.
|
---|
1596 | * @param pStreamOps The stream methods. NULL if pszFilename is used.
|
---|
1597 | * @param pvStreamOpsUser The user argument to the stream methods.
|
---|
1598 | * @param enmAfter What to do afterwards.
|
---|
1599 | * @param pfnProgress Progress callback. Optional.
|
---|
1600 | * @param pvProgressUser User argument for the progress callback.
|
---|
1601 | * @param ppSSM Where to return the saved state handle in case of a
|
---|
1602 | * live snapshot scenario.
|
---|
1603 | * @thread EMT
|
---|
1604 | */
|
---|
1605 | static DECLCALLBACK(int) vmR3Save(PVM pVM, uint32_t cMsMaxDowntime, const char *pszFilename, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser,
|
---|
1606 | SSMAFTER enmAfter, PFNVMPROGRESS pfnProgress, void *pvProgressUser, PSSMHANDLE *ppSSM)
|
---|
1607 | {
|
---|
1608 | LogFlow(("vmR3Save: pVM=%p cMsMaxDowntime=%u pszFilename=%p:{%s} pStreamOps=%p pvStreamOpsUser=%p enmAfter=%d pfnProgress=%p pvProgressUser=%p ppSSM=%p\n",
|
---|
1609 | pVM, cMsMaxDowntime, pszFilename, pszFilename, pStreamOps, pvStreamOpsUser, enmAfter, pfnProgress, pvProgressUser, ppSSM));
|
---|
1610 |
|
---|
1611 | /*
|
---|
1612 | * Validate input.
|
---|
1613 | */
|
---|
1614 | AssertPtrNull(pszFilename);
|
---|
1615 | AssertPtrNull(pStreamOps);
|
---|
1616 | AssertPtr(pVM);
|
---|
1617 | Assert( enmAfter == SSMAFTER_DESTROY
|
---|
1618 | || enmAfter == SSMAFTER_CONTINUE
|
---|
1619 | || enmAfter == SSMAFTER_TELEPORT);
|
---|
1620 | AssertPtr(ppSSM);
|
---|
1621 | *ppSSM = NULL;
|
---|
1622 |
|
---|
1623 | /*
|
---|
1624 | * Change the state and perform/start the saving.
|
---|
1625 | */
|
---|
1626 | int rc = vmR3TrySetState(pVM, "VMR3Save", 2,
|
---|
1627 | VMSTATE_SAVING, VMSTATE_SUSPENDED,
|
---|
1628 | VMSTATE_RUNNING_LS, VMSTATE_RUNNING);
|
---|
1629 | if (rc == 1 && enmAfter != SSMAFTER_TELEPORT)
|
---|
1630 | {
|
---|
1631 | Assert(!pStreamOps);
|
---|
1632 | rc = SSMR3Save(pVM, pszFilename, enmAfter, pfnProgress, pvProgressUser);
|
---|
1633 | vmR3SetState(pVM, VMSTATE_SUSPENDED, VMSTATE_SAVING);
|
---|
1634 | }
|
---|
1635 | else if (rc == 2 || enmAfter == SSMAFTER_TELEPORT)
|
---|
1636 | {
|
---|
1637 | if (enmAfter == SSMAFTER_TELEPORT)
|
---|
1638 | pVM->vm.s.fTeleportedAndNotFullyResumedYet = true;
|
---|
1639 | rc = SSMR3LiveSave(pVM, cMsMaxDowntime, pszFilename, pStreamOps, pvStreamOpsUser,
|
---|
1640 | enmAfter, pfnProgress, pvProgressUser, ppSSM);
|
---|
1641 | /* (We're not subject to cancellation just yet.) */
|
---|
1642 | }
|
---|
1643 | else
|
---|
1644 | Assert(RT_FAILURE(rc));
|
---|
1645 | return rc;
|
---|
1646 | }
|
---|
1647 |
|
---|
1648 |
|
---|
1649 | /**
|
---|
1650 | * Commmon worker for VMR3Save and VMR3Teleport.
|
---|
1651 | *
|
---|
1652 | * @returns VBox status code.
|
---|
1653 | *
|
---|
1654 | * @param pVM The VM handle.
|
---|
1655 | * @param cMsMaxDowntime The maximum downtime given as milliseconds.
|
---|
1656 | * @param pszFilename The name of the file. NULL if pStreamOps is used.
|
---|
1657 | * @param pStreamOps The stream methods. NULL if pszFilename is used.
|
---|
1658 | * @param pvStreamOpsUser The user argument to the stream methods.
|
---|
1659 | * @param enmAfter What to do afterwards.
|
---|
1660 | * @param pfnProgress Progress callback. Optional.
|
---|
1661 | * @param pvProgressUser User argument for the progress callback.
|
---|
1662 | * @param pfSuspended Set if we suspended the VM.
|
---|
1663 | *
|
---|
1664 | * @thread Non-EMT
|
---|
1665 | */
|
---|
1666 | static int vmR3SaveTeleport(PVM pVM, uint32_t cMsMaxDowntime,
|
---|
1667 | const char *pszFilename, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser,
|
---|
1668 | SSMAFTER enmAfter, PFNVMPROGRESS pfnProgress, void *pvProgressUser, bool *pfSuspended)
|
---|
1669 | {
|
---|
1670 | /*
|
---|
1671 | * Request the operation in EMT(0).
|
---|
1672 | */
|
---|
1673 | PSSMHANDLE pSSM;
|
---|
1674 | int rc = VMR3ReqCallWaitU(pVM->pUVM, 0 /*idDstCpu*/,
|
---|
1675 | (PFNRT)vmR3Save, 9, pVM, cMsMaxDowntime, pszFilename, pStreamOps, pvStreamOpsUser,
|
---|
1676 | enmAfter, pfnProgress, pvProgressUser, &pSSM);
|
---|
1677 | if ( RT_SUCCESS(rc)
|
---|
1678 | && pSSM)
|
---|
1679 | {
|
---|
1680 | /*
|
---|
1681 | * Live snapshot.
|
---|
1682 | *
|
---|
1683 | * The state handling here is kind of tricky, doing it on EMT(0) helps
|
---|
1684 | * a bit. See the VMSTATE diagram for details.
|
---|
1685 | */
|
---|
1686 | rc = SSMR3LiveDoStep1(pSSM);
|
---|
1687 | if (RT_SUCCESS(rc))
|
---|
1688 | {
|
---|
1689 | if (VMR3GetState(pVM) != VMSTATE_SAVING)
|
---|
1690 | for (;;)
|
---|
1691 | {
|
---|
1692 | /* Try suspend the VM. */
|
---|
1693 | rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING | VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR,
|
---|
1694 | vmR3LiveDoSuspend, pfSuspended);
|
---|
1695 | if (rc != VERR_TRY_AGAIN)
|
---|
1696 | break;
|
---|
1697 |
|
---|
1698 | /* Wait for the state to change. */
|
---|
1699 | RTThreadSleep(250); /** @todo Live Migration: fix this polling wait by some smart use of multiple release event semaphores.. */
|
---|
1700 | }
|
---|
1701 | if (RT_SUCCESS(rc))
|
---|
1702 | rc = VMR3ReqCallWaitU(pVM->pUVM, 0 /*idDstCpu*/, (PFNRT)vmR3LiveDoStep2, 2, pVM, pSSM);
|
---|
1703 | else
|
---|
1704 | {
|
---|
1705 | int rc2 = VMR3ReqCallWaitU(pVM->pUVM, 0 /*idDstCpu*/, (PFNRT)SSMR3LiveDone, 1, pSSM);
|
---|
1706 | AssertMsg(rc2 == rc, ("%Rrc != %Rrc\n", rc2, rc));
|
---|
1707 | }
|
---|
1708 | }
|
---|
1709 | else
|
---|
1710 | {
|
---|
1711 | int rc2 = VMR3ReqCallWaitU(pVM->pUVM, 0 /*idDstCpu*/, (PFNRT)SSMR3LiveDone, 1, pSSM);
|
---|
1712 | AssertMsg(rc2 == rc, ("%Rrc != %Rrc\n", rc2, rc));
|
---|
1713 |
|
---|
1714 | rc2 = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, vmR3LiveDoStep1Cleanup, pfSuspended);
|
---|
1715 | if (RT_FAILURE(rc2) && rc == VERR_SSM_CANCELLED)
|
---|
1716 | rc = rc2;
|
---|
1717 | }
|
---|
1718 | }
|
---|
1719 |
|
---|
1720 | return rc;
|
---|
1721 | }
|
---|
1722 |
|
---|
1723 |
|
---|
1724 | /**
|
---|
1725 | * Save current VM state.
|
---|
1726 | *
|
---|
1727 | * Can be used for both saving the state and creating snapshots.
|
---|
1728 | *
|
---|
1729 | * When called for a VM in the Running state, the saved state is created live
|
---|
1730 | * and the VM is only suspended when the final part of the saving is preformed.
|
---|
1731 | * The VM state will not be restored to Running in this case and it's up to the
|
---|
1732 | * caller to call VMR3Resume if this is desirable. (The rational is that the
|
---|
1733 | * caller probably wish to reconfigure the disks before resuming the VM.)
|
---|
1734 | *
|
---|
1735 | * @returns VBox status code.
|
---|
1736 | *
|
---|
1737 | * @param pVM The VM which state should be saved.
|
---|
1738 | * @param pszFilename The name of the save state file.
|
---|
1739 | * @param fContinueAfterwards Whether continue execution afterwards or not.
|
---|
1740 | * When in doubt, set this to true.
|
---|
1741 | * @param pfnProgress Progress callback. Optional.
|
---|
1742 | * @param pvUser User argument for the progress callback.
|
---|
1743 | * @param pfSuspended Set if we suspended the VM.
|
---|
1744 | *
|
---|
1745 | * @thread Non-EMT.
|
---|
1746 | * @vmstate Suspended or Running
|
---|
1747 | * @vmstateto Saving+Suspended or
|
---|
1748 | * RunningLS+SuspeningLS+SuspendedLS+Saving+Suspended.
|
---|
1749 | */
|
---|
1750 | VMMR3DECL(int) VMR3Save(PVM pVM, const char *pszFilename, bool fContinueAfterwards,
|
---|
1751 | PFNVMPROGRESS pfnProgress, void *pvUser, bool *pfSuspended)
|
---|
1752 | {
|
---|
1753 | LogFlow(("VMR3Save: pVM=%p pszFilename=%p:{%s} fContinueAfterwards=%RTbool pfnProgress=%p pvUser=%p pfSuspended=%p\n",
|
---|
1754 | pVM, pszFilename, pszFilename, fContinueAfterwards, pfnProgress, pvUser, pfSuspended));
|
---|
1755 |
|
---|
1756 | /*
|
---|
1757 | * Validate input.
|
---|
1758 | */
|
---|
1759 | AssertPtr(pfSuspended);
|
---|
1760 | *pfSuspended = false;
|
---|
1761 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
1762 | VM_ASSERT_OTHER_THREAD(pVM);
|
---|
1763 | AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
|
---|
1764 | AssertReturn(*pszFilename, VERR_INVALID_PARAMETER);
|
---|
1765 | AssertPtrNullReturn(pfnProgress, VERR_INVALID_POINTER);
|
---|
1766 |
|
---|
1767 | /*
|
---|
1768 | * Join paths with VMR3Teleport.
|
---|
1769 | */
|
---|
1770 | SSMAFTER enmAfter = fContinueAfterwards ? SSMAFTER_CONTINUE : SSMAFTER_DESTROY;
|
---|
1771 | int rc = vmR3SaveTeleport(pVM, 250 /*cMsMaxDowntime*/,
|
---|
1772 | pszFilename, NULL /*pStreamOps*/, NULL /*pvStreamOpsUser*/,
|
---|
1773 | enmAfter, pfnProgress, pvUser, pfSuspended);
|
---|
1774 | LogFlow(("VMR3Save: returns %Rrc (*pfSuspended=%RTbool)\n", rc, *pfSuspended));
|
---|
1775 | return rc;
|
---|
1776 | }
|
---|
1777 |
|
---|
1778 |
|
---|
1779 | /**
|
---|
1780 | * Teleport the VM (aka live migration).
|
---|
1781 | *
|
---|
1782 | * @returns VBox status code.
|
---|
1783 | *
|
---|
1784 | * @param pVM The VM which state should be saved.
|
---|
1785 | * @param cMsMaxDowntime The maximum downtime given as milliseconds.
|
---|
1786 | * @param pStreamOps The stream methods.
|
---|
1787 | * @param pvStreamOpsUser The user argument to the stream methods.
|
---|
1788 | * @param pfnProgress Progress callback. Optional.
|
---|
1789 | * @param pvProgressUser User argument for the progress callback.
|
---|
1790 | * @param pfSuspended Set if we suspended the VM.
|
---|
1791 | *
|
---|
1792 | * @thread Non-EMT.
|
---|
1793 | * @vmstate Suspended or Running
|
---|
1794 | * @vmstateto Saving+Suspended or
|
---|
1795 | * RunningLS+SuspeningLS+SuspendedLS+Saving+Suspended.
|
---|
1796 | */
|
---|
1797 | VMMR3DECL(int) VMR3Teleport(PVM pVM, uint32_t cMsMaxDowntime, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser,
|
---|
1798 | PFNVMPROGRESS pfnProgress, void *pvProgressUser, bool *pfSuspended)
|
---|
1799 | {
|
---|
1800 | LogFlow(("VMR3Teleport: pVM=%p cMsMaxDowntime=%u pStreamOps=%p pvStreamOps=%p pfnProgress=%p pvProgressUser=%p\n",
|
---|
1801 | pVM, cMsMaxDowntime, pStreamOps, pvStreamOpsUser, pfnProgress, pvProgressUser));
|
---|
1802 |
|
---|
1803 | /*
|
---|
1804 | * Validate input.
|
---|
1805 | */
|
---|
1806 | AssertPtr(pfSuspended);
|
---|
1807 | *pfSuspended = false;
|
---|
1808 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
1809 | VM_ASSERT_OTHER_THREAD(pVM);
|
---|
1810 | AssertPtrReturn(pStreamOps, VERR_INVALID_POINTER);
|
---|
1811 | AssertPtrNullReturn(pfnProgress, VERR_INVALID_POINTER);
|
---|
1812 |
|
---|
1813 | /*
|
---|
1814 | * Join paths with VMR3Save.
|
---|
1815 | */
|
---|
1816 | int rc = vmR3SaveTeleport(pVM, cMsMaxDowntime,
|
---|
1817 | NULL /*pszFilename*/, pStreamOps, pvStreamOpsUser,
|
---|
1818 | SSMAFTER_TELEPORT, pfnProgress, pvProgressUser, pfSuspended);
|
---|
1819 | LogFlow(("VMR3Teleport: returns %Rrc (*pfSuspended=%RTbool)\n", rc, *pfSuspended));
|
---|
1820 | return rc;
|
---|
1821 | }
|
---|
1822 |
|
---|
1823 |
|
---|
1824 |
|
---|
1825 | /**
|
---|
1826 | * EMT(0) worker for VMR3LoadFromFile and VMR3LoadFromStream.
|
---|
1827 | *
|
---|
1828 | * @returns VBox status code.
|
---|
1829 | *
|
---|
1830 | * @param pVM The VM handle.
|
---|
1831 | * @param pszFilename The name of the file. NULL if pStreamOps is used.
|
---|
1832 | * @param pStreamOps The stream methods. NULL if pszFilename is used.
|
---|
1833 | * @param pvStreamOpsUser The user argument to the stream methods.
|
---|
1834 | * @param pfnProgress Progress callback. Optional.
|
---|
1835 | * @param pvUser User argument for the progress callback.
|
---|
1836 | * @param fTeleporting Indicates whether we're teleporting or not.
|
---|
1837 | *
|
---|
1838 | * @thread EMT.
|
---|
1839 | */
|
---|
1840 | static DECLCALLBACK(int) vmR3Load(PVM pVM, const char *pszFilename, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser,
|
---|
1841 | PFNVMPROGRESS pfnProgress, void *pvProgressUser, bool fTeleporting)
|
---|
1842 | {
|
---|
1843 | LogFlow(("vmR3Load: pVM=%p pszFilename=%p:{%s} pStreamOps=%p pvStreamOpsUser=%p pfnProgress=%p pvProgressUser=%p fTeleporting=%RTbool\n",
|
---|
1844 | pVM, pszFilename, pszFilename, pStreamOps, pvStreamOpsUser, pfnProgress, pvProgressUser, fTeleporting));
|
---|
1845 |
|
---|
1846 | /*
|
---|
1847 | * Validate input (paranoia).
|
---|
1848 | */
|
---|
1849 | AssertPtr(pVM);
|
---|
1850 | AssertPtrNull(pszFilename);
|
---|
1851 | AssertPtrNull(pStreamOps);
|
---|
1852 | AssertPtrNull(pfnProgress);
|
---|
1853 |
|
---|
1854 | /*
|
---|
1855 | * Change the state and perform the load.
|
---|
1856 | *
|
---|
1857 | * Always perform a relocation round afterwards to make sure hypervisor
|
---|
1858 | * selectors and such are correct.
|
---|
1859 | */
|
---|
1860 | int rc = vmR3TrySetState(pVM, "VMR3Load", 2,
|
---|
1861 | VMSTATE_LOADING, VMSTATE_CREATED,
|
---|
1862 | VMSTATE_LOADING, VMSTATE_SUSPENDED);
|
---|
1863 | if (RT_FAILURE(rc))
|
---|
1864 | return rc;
|
---|
1865 | pVM->vm.s.fTeleportedAndNotFullyResumedYet = fTeleporting;
|
---|
1866 |
|
---|
1867 | uint32_t cErrorsPriorToSave = VMR3GetErrorCount(pVM);
|
---|
1868 | rc = SSMR3Load(pVM, pszFilename, pStreamOps, pvStreamOpsUser, SSMAFTER_RESUME, pfnProgress, pvProgressUser);
|
---|
1869 | if (RT_SUCCESS(rc))
|
---|
1870 | {
|
---|
1871 | VMR3Relocate(pVM, 0 /*offDelta*/);
|
---|
1872 | vmR3SetState(pVM, VMSTATE_SUSPENDED, VMSTATE_LOADING);
|
---|
1873 | }
|
---|
1874 | else
|
---|
1875 | {
|
---|
1876 | pVM->vm.s.fTeleportedAndNotFullyResumedYet = false;
|
---|
1877 | vmR3SetState(pVM, VMSTATE_LOAD_FAILURE, VMSTATE_LOADING);
|
---|
1878 | if (cErrorsPriorToSave == VMR3GetErrorCount(pVM))
|
---|
1879 | rc = VMSetError(pVM, rc, RT_SRC_POS,
|
---|
1880 | N_("Unable to restore the virtual machine's saved state from '%s'. "
|
---|
1881 | "It may be damaged or from an older version of VirtualBox. "
|
---|
1882 | "Please discard the saved state before starting the virtual machine"),
|
---|
1883 | pszFilename);
|
---|
1884 | }
|
---|
1885 |
|
---|
1886 | return rc;
|
---|
1887 | }
|
---|
1888 |
|
---|
1889 |
|
---|
1890 | /**
|
---|
1891 | * Loads a VM state into a newly created VM or a one that is suspended.
|
---|
1892 | *
|
---|
1893 | * To restore a saved state on VM startup, call this function and then resume
|
---|
1894 | * the VM instead of powering it on.
|
---|
1895 | *
|
---|
1896 | * @returns VBox status code.
|
---|
1897 | *
|
---|
1898 | * @param pVM The VM handle.
|
---|
1899 | * @param pszFilename The name of the save state file.
|
---|
1900 | * @param pfnProgress Progress callback. Optional.
|
---|
1901 | * @param pvUser User argument for the progress callback.
|
---|
1902 | *
|
---|
1903 | * @thread Any thread.
|
---|
1904 | * @vmstate Created, Suspended
|
---|
1905 | * @vmstateto Loading+Suspended
|
---|
1906 | */
|
---|
1907 | VMMR3DECL(int) VMR3LoadFromFile(PVM pVM, const char *pszFilename, PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
1908 | {
|
---|
1909 | LogFlow(("VMR3LoadFromFile: pVM=%p pszFilename=%p:{%s} pfnProgress=%p pvUser=%p\n",
|
---|
1910 | pVM, pszFilename, pszFilename, pfnProgress, pvUser));
|
---|
1911 |
|
---|
1912 | /*
|
---|
1913 | * Validate input.
|
---|
1914 | */
|
---|
1915 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
1916 | AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
|
---|
1917 |
|
---|
1918 | /*
|
---|
1919 | * Forward the request to EMT(0). No need to setup a rendezvous here
|
---|
1920 | * since there is no execution taking place when this call is allowed.
|
---|
1921 | */
|
---|
1922 | int rc = VMR3ReqCallWaitU(pVM->pUVM, 0 /*idDstCpu*/, (PFNRT)vmR3Load, 7,
|
---|
1923 | pVM, pszFilename, (uintptr_t)NULL /*pStreamOps*/, (uintptr_t)NULL /*pvStreamOpsUser*/, pfnProgress, pvUser,
|
---|
1924 | false /*fTeleporting*/);
|
---|
1925 | LogFlow(("VMR3LoadFromFile: returns %Rrc\n", rc));
|
---|
1926 | return rc;
|
---|
1927 | }
|
---|
1928 |
|
---|
1929 |
|
---|
1930 | /**
|
---|
1931 | * VMR3LoadFromFile for arbritrary file streams.
|
---|
1932 | *
|
---|
1933 | * @returns VBox status code.
|
---|
1934 | *
|
---|
1935 | * @param pVM The VM handle.
|
---|
1936 | * @param pStreamOps The stream methods.
|
---|
1937 | * @param pvStreamOpsUser The user argument to the stream methods.
|
---|
1938 | * @param pfnProgress Progress callback. Optional.
|
---|
1939 | * @param pvProgressUser User argument for the progress callback.
|
---|
1940 | *
|
---|
1941 | * @thread Any thread.
|
---|
1942 | * @vmstate Created, Suspended
|
---|
1943 | * @vmstateto Loading+Suspended
|
---|
1944 | */
|
---|
1945 | VMMR3DECL(int) VMR3LoadFromStream(PVM pVM, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser,
|
---|
1946 | PFNVMPROGRESS pfnProgress, void *pvProgressUser)
|
---|
1947 | {
|
---|
1948 | LogFlow(("VMR3LoadFromStream: pVM=%p pStreamOps=%p pvStreamOpsUser=%p pfnProgress=%p pvProgressUser=%p\n",
|
---|
1949 | pVM, pStreamOps, pvStreamOpsUser, pfnProgress, pvProgressUser));
|
---|
1950 |
|
---|
1951 | /*
|
---|
1952 | * Validate input.
|
---|
1953 | */
|
---|
1954 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
1955 | AssertPtrReturn(pStreamOps, VERR_INVALID_POINTER);
|
---|
1956 |
|
---|
1957 | /*
|
---|
1958 | * Forward the request to EMT(0). No need to setup a rendezvous here
|
---|
1959 | * since there is no execution taking place when this call is allowed.
|
---|
1960 | */
|
---|
1961 | int rc = VMR3ReqCallWaitU(pVM->pUVM, 0 /*idDstCpu*/, (PFNRT)vmR3Load, 7,
|
---|
1962 | pVM, (uintptr_t)NULL /*pszFilename*/, pStreamOps, pvStreamOpsUser, pfnProgress, pvProgressUser,
|
---|
1963 | true /*fTeleporting*/);
|
---|
1964 | LogFlow(("VMR3LoadFromStream: returns %Rrc\n", rc));
|
---|
1965 | return rc;
|
---|
1966 | }
|
---|
1967 |
|
---|
1968 |
|
---|
1969 | /**
|
---|
1970 | * EMT rendezvous worker for VMR3PowerOff.
|
---|
1971 | *
|
---|
1972 | * @returns VERR_VM_INVALID_VM_STATE or VINF_EM_OFF. (This is a strict
|
---|
1973 | * return code, see FNVMMEMTRENDEZVOUS.)
|
---|
1974 | *
|
---|
1975 | * @param pVM The VM handle.
|
---|
1976 | * @param pVCpu The VMCPU handle of the EMT.
|
---|
1977 | * @param pvUser Ignored.
|
---|
1978 | */
|
---|
1979 | static DECLCALLBACK(VBOXSTRICTRC) vmR3PowerOff(PVM pVM, PVMCPU pVCpu, void *pvUser)
|
---|
1980 | {
|
---|
1981 | LogFlow(("vmR3PowerOff: pVM=%p pVCpu=%p/#%u\n", pVM, pVCpu, pVCpu->idCpu));
|
---|
1982 | Assert(!pvUser); NOREF(pvUser);
|
---|
1983 |
|
---|
1984 | /*
|
---|
1985 | * The first EMT thru here will change the state to PoweringOff.
|
---|
1986 | */
|
---|
1987 | if (pVCpu->idCpu == pVM->cCpus - 1)
|
---|
1988 | {
|
---|
1989 | int rc = vmR3TrySetState(pVM, "VMR3PowerOff", 10,
|
---|
1990 | VMSTATE_POWERING_OFF, VMSTATE_RUNNING,
|
---|
1991 | VMSTATE_POWERING_OFF, VMSTATE_SUSPENDED,
|
---|
1992 | VMSTATE_POWERING_OFF, VMSTATE_DEBUGGING,
|
---|
1993 | VMSTATE_POWERING_OFF, VMSTATE_LOAD_FAILURE,
|
---|
1994 | VMSTATE_POWERING_OFF, VMSTATE_GURU_MEDITATION,
|
---|
1995 | VMSTATE_POWERING_OFF, VMSTATE_FATAL_ERROR,
|
---|
1996 | VMSTATE_POWERING_OFF, VMSTATE_CREATED, /** @todo update the diagram! */
|
---|
1997 | VMSTATE_POWERING_OFF_LS, VMSTATE_RUNNING_LS,
|
---|
1998 | VMSTATE_POWERING_OFF_LS, VMSTATE_DEBUGGING_LS,
|
---|
1999 | VMSTATE_POWERING_OFF_LS, VMSTATE_GURU_MEDITATION_LS,
|
---|
2000 | VMSTATE_POWERING_OFF_LS, VMSTATE_FATAL_ERROR_LS);
|
---|
2001 | if (RT_FAILURE(rc))
|
---|
2002 | return rc;
|
---|
2003 | if (rc >= 7)
|
---|
2004 | SSMR3Cancel(pVM);
|
---|
2005 | }
|
---|
2006 |
|
---|
2007 | /*
|
---|
2008 | * Check the state.
|
---|
2009 | */
|
---|
2010 | VMSTATE enmVMState = VMR3GetState(pVM);
|
---|
2011 | AssertMsgReturn( enmVMState == VMSTATE_POWERING_OFF
|
---|
2012 | || enmVMState == VMSTATE_POWERING_OFF_LS,
|
---|
2013 | ("%s\n", VMR3GetStateName(enmVMState)),
|
---|
2014 | VERR_VM_INVALID_VM_STATE);
|
---|
2015 |
|
---|
2016 | /*
|
---|
2017 | * EMT(0) does the actual power off work here *after* all the other EMTs
|
---|
2018 | * have been thru and entered the STOPPED state.
|
---|
2019 | */
|
---|
2020 | VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STOPPED);
|
---|
2021 | if (pVCpu->idCpu == 0)
|
---|
2022 | {
|
---|
2023 | /*
|
---|
2024 | * For debugging purposes, we will log a summary of the guest state at this point.
|
---|
2025 | */
|
---|
2026 | if (enmVMState != VMSTATE_GURU_MEDITATION)
|
---|
2027 | {
|
---|
2028 | /** @todo SMP support? */
|
---|
2029 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
2030 |
|
---|
2031 | /** @todo make the state dumping at VMR3PowerOff optional. */
|
---|
2032 | RTLogRelPrintf("****************** Guest state at power off ******************\n");
|
---|
2033 | DBGFR3Info(pVM, "cpumguest", "verbose", DBGFR3InfoLogRelHlp());
|
---|
2034 | RTLogRelPrintf("***\n");
|
---|
2035 | DBGFR3Info(pVM, "mode", NULL, DBGFR3InfoLogRelHlp());
|
---|
2036 | RTLogRelPrintf("***\n");
|
---|
2037 | DBGFR3Info(pVM, "activetimers", NULL, DBGFR3InfoLogRelHlp());
|
---|
2038 | RTLogRelPrintf("***\n");
|
---|
2039 | DBGFR3Info(pVM, "gdt", NULL, DBGFR3InfoLogRelHlp());
|
---|
2040 | /** @todo dump guest call stack. */
|
---|
2041 | #if 1 // "temporary" while debugging #1589
|
---|
2042 | RTLogRelPrintf("***\n");
|
---|
2043 | uint32_t esp = CPUMGetGuestESP(pVCpu);
|
---|
2044 | if ( CPUMGetGuestSS(pVCpu) == 0
|
---|
2045 | && esp < _64K)
|
---|
2046 | {
|
---|
2047 | uint8_t abBuf[PAGE_SIZE];
|
---|
2048 | RTLogRelPrintf("***\n"
|
---|
2049 | "ss:sp=0000:%04x ", esp);
|
---|
2050 | uint32_t Start = esp & ~(uint32_t)63;
|
---|
2051 | int rc = PGMPhysSimpleReadGCPhys(pVM, abBuf, Start, 0x100);
|
---|
2052 | if (RT_SUCCESS(rc))
|
---|
2053 | RTLogRelPrintf("0000:%04x TO 0000:%04x:\n"
|
---|
2054 | "%.*Rhxd\n",
|
---|
2055 | Start, Start + 0x100 - 1,
|
---|
2056 | 0x100, abBuf);
|
---|
2057 | else
|
---|
2058 | RTLogRelPrintf("rc=%Rrc\n", rc);
|
---|
2059 |
|
---|
2060 | /* grub ... */
|
---|
2061 | if (esp < 0x2000 && esp > 0x1fc0)
|
---|
2062 | {
|
---|
2063 | rc = PGMPhysSimpleReadGCPhys(pVM, abBuf, 0x8000, 0x800);
|
---|
2064 | if (RT_SUCCESS(rc))
|
---|
2065 | RTLogRelPrintf("0000:8000 TO 0000:87ff:\n"
|
---|
2066 | "%.*Rhxd\n",
|
---|
2067 | 0x800, abBuf);
|
---|
2068 | }
|
---|
2069 | /* microsoft cdrom hang ... */
|
---|
2070 | if (true)
|
---|
2071 | {
|
---|
2072 | rc = PGMPhysSimpleReadGCPhys(pVM, abBuf, 0x8000, 0x200);
|
---|
2073 | if (RT_SUCCESS(rc))
|
---|
2074 | RTLogRelPrintf("2000:0000 TO 2000:01ff:\n"
|
---|
2075 | "%.*Rhxd\n",
|
---|
2076 | 0x200, abBuf);
|
---|
2077 | }
|
---|
2078 | }
|
---|
2079 | #endif
|
---|
2080 | RTLogRelPrintf("************** End of Guest state at power off ***************\n");
|
---|
2081 | }
|
---|
2082 |
|
---|
2083 | /*
|
---|
2084 | * Perform the power off notifications and advance the state to
|
---|
2085 | * Off or OffLS.
|
---|
2086 | */
|
---|
2087 | PDMR3PowerOff(pVM);
|
---|
2088 |
|
---|
2089 | PUVM pUVM = pVM->pUVM;
|
---|
2090 | RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
|
---|
2091 | enmVMState = pVM->enmVMState;
|
---|
2092 | if (enmVMState == VMSTATE_POWERING_OFF_LS)
|
---|
2093 | vmR3SetStateLocked(pVM, pUVM, VMSTATE_OFF_LS, VMSTATE_POWERING_OFF_LS);
|
---|
2094 | else
|
---|
2095 | vmR3SetStateLocked(pVM, pUVM, VMSTATE_OFF, VMSTATE_POWERING_OFF);
|
---|
2096 | RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
|
---|
2097 | }
|
---|
2098 | return VINF_EM_OFF;
|
---|
2099 | }
|
---|
2100 |
|
---|
2101 |
|
---|
2102 | /**
|
---|
2103 | * Power off the VM.
|
---|
2104 | *
|
---|
2105 | * @returns VBox status code. When called on EMT, this will be a strict status
|
---|
2106 | * code that has to be propagated up the call stack.
|
---|
2107 | *
|
---|
2108 | * @param pVM The handle of the VM to be powered off.
|
---|
2109 | *
|
---|
2110 | * @thread Any thread.
|
---|
2111 | * @vmstate Suspended, Running, Guru Meditation, Load Failure
|
---|
2112 | * @vmstateto Off or OffLS
|
---|
2113 | */
|
---|
2114 | VMMR3DECL(int) VMR3PowerOff(PVM pVM)
|
---|
2115 | {
|
---|
2116 | LogFlow(("VMR3PowerOff: pVM=%p\n", pVM));
|
---|
2117 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
2118 |
|
---|
2119 | /*
|
---|
2120 | * Gather all the EMTs to make sure there are no races before
|
---|
2121 | * changing the VM state.
|
---|
2122 | */
|
---|
2123 | int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING | VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR,
|
---|
2124 | vmR3PowerOff, NULL);
|
---|
2125 | LogFlow(("VMR3PowerOff: returns %Rrc\n", rc));
|
---|
2126 | return rc;
|
---|
2127 | }
|
---|
2128 |
|
---|
2129 |
|
---|
2130 | /**
|
---|
2131 | * Destroys the VM.
|
---|
2132 | *
|
---|
2133 | * The VM must be powered off (or never really powered on) to call this
|
---|
2134 | * function. The VM handle is destroyed and can no longer be used up successful
|
---|
2135 | * return.
|
---|
2136 | *
|
---|
2137 | * @returns VBox status code.
|
---|
2138 | *
|
---|
2139 | * @param pVM The handle of the VM which should be destroyed.
|
---|
2140 | *
|
---|
2141 | * @thread EMT(0) or any none emulation thread.
|
---|
2142 | * @vmstate Off, Created
|
---|
2143 | * @vmstateto N/A
|
---|
2144 | */
|
---|
2145 | VMMR3DECL(int) VMR3Destroy(PVM pVM)
|
---|
2146 | {
|
---|
2147 | LogFlow(("VMR3Destroy: pVM=%p\n", pVM));
|
---|
2148 |
|
---|
2149 | /*
|
---|
2150 | * Validate input.
|
---|
2151 | */
|
---|
2152 | if (!pVM)
|
---|
2153 | return VERR_INVALID_PARAMETER;
|
---|
2154 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
2155 | Assert(VMMGetCpuId(pVM) == 0 || VMMGetCpuId(pVM) == NIL_VMCPUID);
|
---|
2156 |
|
---|
2157 | /*
|
---|
2158 | * Change VM state to destroying and unlink the VM.
|
---|
2159 | */
|
---|
2160 | int rc = vmR3TrySetState(pVM, "VMR3Destroy", 1, VMSTATE_DESTROYING, VMSTATE_OFF);
|
---|
2161 | if (RT_FAILURE(rc))
|
---|
2162 | return rc;
|
---|
2163 |
|
---|
2164 | /** @todo lock this when we start having multiple machines in a process... */
|
---|
2165 | PUVM pUVM = pVM->pUVM; AssertPtr(pUVM);
|
---|
2166 | if (g_pUVMsHead == pUVM)
|
---|
2167 | g_pUVMsHead = pUVM->pNext;
|
---|
2168 | else
|
---|
2169 | {
|
---|
2170 | PUVM pPrev = g_pUVMsHead;
|
---|
2171 | while (pPrev && pPrev->pNext != pUVM)
|
---|
2172 | pPrev = pPrev->pNext;
|
---|
2173 | AssertMsgReturn(pPrev, ("pUVM=%p / pVM=%p is INVALID!\n", pUVM, pVM), VERR_INVALID_PARAMETER);
|
---|
2174 |
|
---|
2175 | pPrev->pNext = pUVM->pNext;
|
---|
2176 | }
|
---|
2177 | pUVM->pNext = NULL;
|
---|
2178 |
|
---|
2179 | /*
|
---|
2180 | * Notify registered at destruction listeners.
|
---|
2181 | */
|
---|
2182 | vmR3AtDtor(pVM);
|
---|
2183 |
|
---|
2184 | /*
|
---|
2185 | * EMT(0) does the final cleanup, so if we're it calling VMR3Destroy then
|
---|
2186 | * we'll have to postpone parts of it till later. Otherwise, call
|
---|
2187 | * vmR3Destroy on each of the EMTs in ending with EMT(0) doing the bulk
|
---|
2188 | * of the cleanup.
|
---|
2189 | */
|
---|
2190 | if (VMMGetCpuId(pVM) == 0)
|
---|
2191 | {
|
---|
2192 | pUVM->vm.s.fEMTDoesTheCleanup = true;
|
---|
2193 | pUVM->vm.s.fTerminateEMT = true;
|
---|
2194 | VM_FF_SET(pVM, VM_FF_TERMINATE);
|
---|
2195 |
|
---|
2196 | /* Terminate the other EMTs. */
|
---|
2197 | for (VMCPUID idCpu = 1; idCpu < pVM->cCpus; idCpu++)
|
---|
2198 | {
|
---|
2199 | int rc = VMR3ReqCallWaitU(pUVM, idCpu, (PFNRT)vmR3Destroy, 1, pVM);
|
---|
2200 | AssertLogRelRC(rc);
|
---|
2201 | }
|
---|
2202 | }
|
---|
2203 | else
|
---|
2204 | {
|
---|
2205 | /* vmR3Destroy on all EMTs, ending with EMT(0). */
|
---|
2206 | int rc = VMR3ReqCallWaitU(pUVM, VMCPUID_ALL_REVERSE, (PFNRT)vmR3Destroy, 1, pVM);
|
---|
2207 | AssertLogRelRC(rc);
|
---|
2208 |
|
---|
2209 | /* Wait for EMTs and destroy the UVM. */
|
---|
2210 | vmR3DestroyUVM(pUVM, 30000);
|
---|
2211 | }
|
---|
2212 |
|
---|
2213 | LogFlow(("VMR3Destroy: returns VINF_SUCCESS\n"));
|
---|
2214 | return VINF_SUCCESS;
|
---|
2215 | }
|
---|
2216 |
|
---|
2217 |
|
---|
2218 | /**
|
---|
2219 | * Internal destruction worker.
|
---|
2220 | *
|
---|
2221 | * This is either called from VMR3Destroy via VMR3ReqCallU or from
|
---|
2222 | * vmR3EmulationThreadWithId when EMT(0) terminates after having called
|
---|
2223 | * VMR3Destroy().
|
---|
2224 | *
|
---|
2225 | * When called on EMT(0), it will performed the great bulk of the destruction.
|
---|
2226 | * When called on the other EMTs, they will do nothing and the whole purpose is
|
---|
2227 | * to return VINF_EM_TERMINATE so they break out of their run loops.
|
---|
2228 | *
|
---|
2229 | * @returns VINF_EM_TERMINATE.
|
---|
2230 | * @param pVM The VM handle.
|
---|
2231 | */
|
---|
2232 | DECLCALLBACK(int) vmR3Destroy(PVM pVM)
|
---|
2233 | {
|
---|
2234 | PUVM pUVM = pVM->pUVM;
|
---|
2235 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
2236 | Assert(pVCpu);
|
---|
2237 | LogFlow(("vmR3Destroy: pVM=%p pUVM=%p pVCpu=%p idCpu=%u\n", pVM, pUVM, pVCpu, pVCpu->idCpu));
|
---|
2238 |
|
---|
2239 | /*
|
---|
2240 | * Only VCPU 0 does the full cleanup.
|
---|
2241 | */
|
---|
2242 | if (pVCpu->idCpu == 0)
|
---|
2243 | {
|
---|
2244 |
|
---|
2245 | /*
|
---|
2246 | * Dump statistics to the log.
|
---|
2247 | */
|
---|
2248 | #if defined(VBOX_WITH_STATISTICS) || defined(LOG_ENABLED)
|
---|
2249 | RTLogFlags(NULL, "nodisabled nobuffered");
|
---|
2250 | #endif
|
---|
2251 | #ifdef VBOX_WITH_STATISTICS
|
---|
2252 | STAMR3Dump(pVM, "*");
|
---|
2253 | #else
|
---|
2254 | LogRel(("************************* Statistics *************************\n"));
|
---|
2255 | STAMR3DumpToReleaseLog(pVM, "*");
|
---|
2256 | LogRel(("********************* End of statistics **********************\n"));
|
---|
2257 | #endif
|
---|
2258 |
|
---|
2259 | /*
|
---|
2260 | * Destroy the VM components.
|
---|
2261 | */
|
---|
2262 | int rc = TMR3Term(pVM);
|
---|
2263 | AssertRC(rc);
|
---|
2264 | #ifdef VBOX_WITH_DEBUGGER
|
---|
2265 | rc = DBGCTcpTerminate(pVM, pUVM->vm.s.pvDBGC);
|
---|
2266 | pUVM->vm.s.pvDBGC = NULL;
|
---|
2267 | #endif
|
---|
2268 | AssertRC(rc);
|
---|
2269 | rc = DBGFR3Term(pVM);
|
---|
2270 | AssertRC(rc);
|
---|
2271 | rc = PDMR3Term(pVM);
|
---|
2272 | AssertRC(rc);
|
---|
2273 | rc = EMR3Term(pVM);
|
---|
2274 | AssertRC(rc);
|
---|
2275 | rc = IOMR3Term(pVM);
|
---|
2276 | AssertRC(rc);
|
---|
2277 | rc = CSAMR3Term(pVM);
|
---|
2278 | AssertRC(rc);
|
---|
2279 | rc = PATMR3Term(pVM);
|
---|
2280 | AssertRC(rc);
|
---|
2281 | rc = TRPMR3Term(pVM);
|
---|
2282 | AssertRC(rc);
|
---|
2283 | rc = SELMR3Term(pVM);
|
---|
2284 | AssertRC(rc);
|
---|
2285 | rc = REMR3Term(pVM);
|
---|
2286 | AssertRC(rc);
|
---|
2287 | rc = HWACCMR3Term(pVM);
|
---|
2288 | AssertRC(rc);
|
---|
2289 | rc = PGMR3Term(pVM);
|
---|
2290 | AssertRC(rc);
|
---|
2291 | rc = VMMR3Term(pVM); /* Terminates the ring-0 code! */
|
---|
2292 | AssertRC(rc);
|
---|
2293 | rc = CPUMR3Term(pVM);
|
---|
2294 | AssertRC(rc);
|
---|
2295 | SSMR3Term(pVM);
|
---|
2296 | rc = PDMR3CritSectTerm(pVM);
|
---|
2297 | AssertRC(rc);
|
---|
2298 | rc = MMR3Term(pVM);
|
---|
2299 | AssertRC(rc);
|
---|
2300 |
|
---|
2301 | /*
|
---|
2302 | * We're done in this thread (EMT).
|
---|
2303 | */
|
---|
2304 | ASMAtomicUoWriteBool(&pUVM->vm.s.fTerminateEMT, true);
|
---|
2305 | ASMAtomicWriteU32(&pVM->fGlobalForcedActions, VM_FF_TERMINATE);
|
---|
2306 | LogFlow(("vmR3Destroy: returning %Rrc\n", VINF_EM_TERMINATE));
|
---|
2307 | }
|
---|
2308 | return VINF_EM_TERMINATE;
|
---|
2309 | }
|
---|
2310 |
|
---|
2311 |
|
---|
2312 | /**
|
---|
2313 | * Called at the end of the EMT procedure to take care of the final cleanup.
|
---|
2314 | *
|
---|
2315 | * Currently only EMT(0) will do work here. It will destroy the shared VM
|
---|
2316 | * structure if it is still around. If EMT(0) was the caller of VMR3Destroy it
|
---|
2317 | * will destroy UVM and nothing will be left behind upon exit. But if some
|
---|
2318 | * other thread is calling VMR3Destroy, they will clean up UVM after all EMTs
|
---|
2319 | * has exitted.
|
---|
2320 | *
|
---|
2321 | * @param pUVM The UVM handle.
|
---|
2322 | * @param idCpu The virtual CPU id.
|
---|
2323 | */
|
---|
2324 | void vmR3DestroyFinalBitFromEMT(PUVM pUVM, VMCPUID idCpu)
|
---|
2325 | {
|
---|
2326 | /*
|
---|
2327 | * Only EMT(0) has work to do here.
|
---|
2328 | */
|
---|
2329 | if (idCpu != 0)
|
---|
2330 | return;
|
---|
2331 | Assert( !pUVM->pVM
|
---|
2332 | || VMMGetCpuId(pUVM->pVM) == 0);
|
---|
2333 |
|
---|
2334 | /*
|
---|
2335 | * If we have a shared VM structure, change its state to Terminated and
|
---|
2336 | * tell GVMM to destroy it.
|
---|
2337 | */
|
---|
2338 | if (pUVM->pVM)
|
---|
2339 | {
|
---|
2340 | vmR3SetState(pUVM->pVM, VMSTATE_TERMINATED, VMSTATE_DESTROYING);
|
---|
2341 | int rc = SUPR3CallVMMR0Ex(pUVM->pVM->pVMR0, 0 /*idCpu*/, VMMR0_DO_GVMM_DESTROY_VM, 0, NULL);
|
---|
2342 | AssertLogRelRC(rc);
|
---|
2343 | pUVM->pVM = NULL;
|
---|
2344 | }
|
---|
2345 |
|
---|
2346 | /*
|
---|
2347 | * If EMT(0) called VMR3Destroy, then it will destroy UVM as well.
|
---|
2348 | */
|
---|
2349 | if (pUVM->vm.s.fEMTDoesTheCleanup)
|
---|
2350 | vmR3DestroyUVM(pUVM, 30000);
|
---|
2351 | }
|
---|
2352 |
|
---|
2353 |
|
---|
2354 | /**
|
---|
2355 | * Destroys the UVM portion.
|
---|
2356 | *
|
---|
2357 | * This is called as the final step in the VM destruction or as the cleanup
|
---|
2358 | * in case of a creation failure. If EMT(0) called VMR3Destroy, meaning
|
---|
2359 | * VMINTUSERPERVM::fEMTDoesTheCleanup is true, it will call this as
|
---|
2360 | * vmR3DestroyFinalBitFromEMT completes.
|
---|
2361 | *
|
---|
2362 | * @param pVM VM Handle.
|
---|
2363 | * @param cMilliesEMTWait The number of milliseconds to wait for the emulation
|
---|
2364 | * threads.
|
---|
2365 | */
|
---|
2366 | static void vmR3DestroyUVM(PUVM pUVM, uint32_t cMilliesEMTWait)
|
---|
2367 | {
|
---|
2368 | /*
|
---|
2369 | * Signal termination of each the emulation threads and
|
---|
2370 | * wait for them to complete.
|
---|
2371 | */
|
---|
2372 | /* Signal them. */
|
---|
2373 | ASMAtomicUoWriteBool(&pUVM->vm.s.fTerminateEMT, true);
|
---|
2374 | for (VMCPUID i = 0; i < pUVM->cCpus; i++)
|
---|
2375 | {
|
---|
2376 | ASMAtomicUoWriteBool(&pUVM->aCpus[i].vm.s.fTerminateEMT, true);
|
---|
2377 | if (pUVM->pVM)
|
---|
2378 | VM_FF_SET(pUVM->pVM, VM_FF_TERMINATE);
|
---|
2379 | VMR3NotifyGlobalFFU(pUVM, VMNOTIFYFF_FLAGS_DONE_REM);
|
---|
2380 | RTSemEventSignal(pUVM->aCpus[i].vm.s.EventSemWait);
|
---|
2381 | }
|
---|
2382 |
|
---|
2383 | /* Wait for them. */
|
---|
2384 | uint64_t NanoTS = RTTimeNanoTS();
|
---|
2385 | RTTHREAD hSelf = RTThreadSelf();
|
---|
2386 | ASMAtomicUoWriteBool(&pUVM->vm.s.fTerminateEMT, true);
|
---|
2387 | for (VMCPUID i = 0; i < pUVM->cCpus; i++)
|
---|
2388 | {
|
---|
2389 | RTTHREAD hThread = pUVM->aCpus[i].vm.s.ThreadEMT;
|
---|
2390 | if ( hThread != NIL_RTTHREAD
|
---|
2391 | && hThread != hSelf)
|
---|
2392 | {
|
---|
2393 | uint64_t cMilliesElapsed = (RTTimeNanoTS() - NanoTS) / 1000000;
|
---|
2394 | int rc2 = RTThreadWait(hThread,
|
---|
2395 | cMilliesElapsed < cMilliesEMTWait
|
---|
2396 | ? RT_MAX(cMilliesEMTWait - cMilliesElapsed, 2000)
|
---|
2397 | : 2000,
|
---|
2398 | NULL);
|
---|
2399 | if (rc2 == VERR_TIMEOUT) /* avoid the assertion when debugging. */
|
---|
2400 | rc2 = RTThreadWait(hThread, 1000, NULL);
|
---|
2401 | AssertLogRelMsgRC(rc2, ("i=%u rc=%Rrc\n", i, rc2));
|
---|
2402 | if (RT_SUCCESS(rc2))
|
---|
2403 | pUVM->aCpus[0].vm.s.ThreadEMT = NIL_RTTHREAD;
|
---|
2404 | }
|
---|
2405 | }
|
---|
2406 |
|
---|
2407 | /* Cleanup the semaphores. */
|
---|
2408 | for (VMCPUID i = 0; i < pUVM->cCpus; i++)
|
---|
2409 | {
|
---|
2410 | RTSemEventDestroy(pUVM->aCpus[i].vm.s.EventSemWait);
|
---|
2411 | pUVM->aCpus[i].vm.s.EventSemWait = NIL_RTSEMEVENT;
|
---|
2412 | }
|
---|
2413 |
|
---|
2414 | /*
|
---|
2415 | * Free the event semaphores associated with the request packets.
|
---|
2416 | */
|
---|
2417 | unsigned cReqs = 0;
|
---|
2418 | for (unsigned i = 0; i < RT_ELEMENTS(pUVM->vm.s.apReqFree); i++)
|
---|
2419 | {
|
---|
2420 | PVMREQ pReq = pUVM->vm.s.apReqFree[i];
|
---|
2421 | pUVM->vm.s.apReqFree[i] = NULL;
|
---|
2422 | for (; pReq; pReq = pReq->pNext, cReqs++)
|
---|
2423 | {
|
---|
2424 | pReq->enmState = VMREQSTATE_INVALID;
|
---|
2425 | RTSemEventDestroy(pReq->EventSem);
|
---|
2426 | }
|
---|
2427 | }
|
---|
2428 | Assert(cReqs == pUVM->vm.s.cReqFree); NOREF(cReqs);
|
---|
2429 |
|
---|
2430 | /*
|
---|
2431 | * Kill all queued requests. (There really shouldn't be any!)
|
---|
2432 | */
|
---|
2433 | for (unsigned i = 0; i < 10; i++)
|
---|
2434 | {
|
---|
2435 | PVMREQ pReqHead = (PVMREQ)ASMAtomicXchgPtr((void *volatile *)&pUVM->vm.s.pReqs, NULL);
|
---|
2436 | AssertMsg(!pReqHead, ("This isn't supposed to happen! VMR3Destroy caller has to serialize this.\n"));
|
---|
2437 | if (!pReqHead)
|
---|
2438 | break;
|
---|
2439 | for (PVMREQ pReq = pReqHead; pReq; pReq = pReq->pNext)
|
---|
2440 | {
|
---|
2441 | ASMAtomicUoWriteSize(&pReq->iStatus, VERR_INTERNAL_ERROR);
|
---|
2442 | ASMAtomicWriteSize(&pReq->enmState, VMREQSTATE_INVALID);
|
---|
2443 | RTSemEventSignal(pReq->EventSem);
|
---|
2444 | RTThreadSleep(2);
|
---|
2445 | RTSemEventDestroy(pReq->EventSem);
|
---|
2446 | }
|
---|
2447 | /* give them a chance to respond before we free the request memory. */
|
---|
2448 | RTThreadSleep(32);
|
---|
2449 | }
|
---|
2450 |
|
---|
2451 | /*
|
---|
2452 | * Now all queued VCPU requests (again, there shouldn't be any).
|
---|
2453 | */
|
---|
2454 | for (VMCPUID i = 0; i < pUVM->cCpus; i++)
|
---|
2455 | {
|
---|
2456 | PUVMCPU pUVCpu = &pUVM->aCpus[i];
|
---|
2457 |
|
---|
2458 | for (unsigned i = 0; i < 10; i++)
|
---|
2459 | {
|
---|
2460 | PVMREQ pReqHead = (PVMREQ)ASMAtomicXchgPtr((void *volatile *)&pUVCpu->vm.s.pReqs, NULL);
|
---|
2461 | AssertMsg(!pReqHead, ("This isn't supposed to happen! VMR3Destroy caller has to serialize this.\n"));
|
---|
2462 | if (!pReqHead)
|
---|
2463 | break;
|
---|
2464 | for (PVMREQ pReq = pReqHead; pReq; pReq = pReq->pNext)
|
---|
2465 | {
|
---|
2466 | ASMAtomicUoWriteSize(&pReq->iStatus, VERR_INTERNAL_ERROR);
|
---|
2467 | ASMAtomicWriteSize(&pReq->enmState, VMREQSTATE_INVALID);
|
---|
2468 | RTSemEventSignal(pReq->EventSem);
|
---|
2469 | RTThreadSleep(2);
|
---|
2470 | RTSemEventDestroy(pReq->EventSem);
|
---|
2471 | }
|
---|
2472 | /* give them a chance to respond before we free the request memory. */
|
---|
2473 | RTThreadSleep(32);
|
---|
2474 | }
|
---|
2475 | }
|
---|
2476 |
|
---|
2477 | /*
|
---|
2478 | * Make sure the VMMR0.r0 module and whatever else is unloaded.
|
---|
2479 | */
|
---|
2480 | PDMR3TermUVM(pUVM);
|
---|
2481 |
|
---|
2482 | /*
|
---|
2483 | * Terminate the support library if initialized.
|
---|
2484 | */
|
---|
2485 | if (pUVM->vm.s.pSession)
|
---|
2486 | {
|
---|
2487 | int rc = SUPR3Term(false /*fForced*/);
|
---|
2488 | AssertRC(rc);
|
---|
2489 | pUVM->vm.s.pSession = NIL_RTR0PTR;
|
---|
2490 | }
|
---|
2491 |
|
---|
2492 | /*
|
---|
2493 | * Destroy the MM heap and free the UVM structure.
|
---|
2494 | */
|
---|
2495 | MMR3TermUVM(pUVM);
|
---|
2496 | STAMR3TermUVM(pUVM);
|
---|
2497 |
|
---|
2498 | #ifdef LOG_ENABLED
|
---|
2499 | RTLogSetCustomPrefixCallback(NULL, NULL, NULL);
|
---|
2500 | #endif
|
---|
2501 | RTTlsFree(pUVM->vm.s.idxTLS);
|
---|
2502 |
|
---|
2503 | ASMAtomicUoWriteU32(&pUVM->u32Magic, UINT32_MAX);
|
---|
2504 | RTMemPageFree(pUVM);
|
---|
2505 |
|
---|
2506 | RTLogFlush(NULL);
|
---|
2507 | }
|
---|
2508 |
|
---|
2509 |
|
---|
2510 | /**
|
---|
2511 | * Enumerates the VMs in this process.
|
---|
2512 | *
|
---|
2513 | * @returns Pointer to the next VM.
|
---|
2514 | * @returns NULL when no more VMs.
|
---|
2515 | * @param pVMPrev The previous VM
|
---|
2516 | * Use NULL to start the enumeration.
|
---|
2517 | */
|
---|
2518 | VMMR3DECL(PVM) VMR3EnumVMs(PVM pVMPrev)
|
---|
2519 | {
|
---|
2520 | /*
|
---|
2521 | * This is quick and dirty. It has issues with VM being
|
---|
2522 | * destroyed during the enumeration.
|
---|
2523 | */
|
---|
2524 | PUVM pNext;
|
---|
2525 | if (pVMPrev)
|
---|
2526 | pNext = pVMPrev->pUVM->pNext;
|
---|
2527 | else
|
---|
2528 | pNext = g_pUVMsHead;
|
---|
2529 | return pNext ? pNext->pVM : NULL;
|
---|
2530 | }
|
---|
2531 |
|
---|
2532 |
|
---|
2533 | /**
|
---|
2534 | * Registers an at VM destruction callback.
|
---|
2535 | *
|
---|
2536 | * @returns VBox status code.
|
---|
2537 | * @param pfnAtDtor Pointer to callback.
|
---|
2538 | * @param pvUser User argument.
|
---|
2539 | */
|
---|
2540 | VMMR3DECL(int) VMR3AtDtorRegister(PFNVMATDTOR pfnAtDtor, void *pvUser)
|
---|
2541 | {
|
---|
2542 | /*
|
---|
2543 | * Check if already registered.
|
---|
2544 | */
|
---|
2545 | VM_ATDTOR_LOCK();
|
---|
2546 | PVMATDTOR pCur = g_pVMAtDtorHead;
|
---|
2547 | while (pCur)
|
---|
2548 | {
|
---|
2549 | if (pfnAtDtor == pCur->pfnAtDtor)
|
---|
2550 | {
|
---|
2551 | VM_ATDTOR_UNLOCK();
|
---|
2552 | AssertMsgFailed(("Already registered at destruction callback %p!\n", pfnAtDtor));
|
---|
2553 | return VERR_INVALID_PARAMETER;
|
---|
2554 | }
|
---|
2555 |
|
---|
2556 | /* next */
|
---|
2557 | pCur = pCur->pNext;
|
---|
2558 | }
|
---|
2559 | VM_ATDTOR_UNLOCK();
|
---|
2560 |
|
---|
2561 | /*
|
---|
2562 | * Allocate new entry.
|
---|
2563 | */
|
---|
2564 | PVMATDTOR pVMAtDtor = (PVMATDTOR)RTMemAlloc(sizeof(*pVMAtDtor));
|
---|
2565 | if (!pVMAtDtor)
|
---|
2566 | return VERR_NO_MEMORY;
|
---|
2567 |
|
---|
2568 | VM_ATDTOR_LOCK();
|
---|
2569 | pVMAtDtor->pfnAtDtor = pfnAtDtor;
|
---|
2570 | pVMAtDtor->pvUser = pvUser;
|
---|
2571 | pVMAtDtor->pNext = g_pVMAtDtorHead;
|
---|
2572 | g_pVMAtDtorHead = pVMAtDtor;
|
---|
2573 | VM_ATDTOR_UNLOCK();
|
---|
2574 |
|
---|
2575 | return VINF_SUCCESS;
|
---|
2576 | }
|
---|
2577 |
|
---|
2578 |
|
---|
2579 | /**
|
---|
2580 | * Deregisters an at VM destruction callback.
|
---|
2581 | *
|
---|
2582 | * @returns VBox status code.
|
---|
2583 | * @param pfnAtDtor Pointer to callback.
|
---|
2584 | */
|
---|
2585 | VMMR3DECL(int) VMR3AtDtorDeregister(PFNVMATDTOR pfnAtDtor)
|
---|
2586 | {
|
---|
2587 | /*
|
---|
2588 | * Find it, unlink it and free it.
|
---|
2589 | */
|
---|
2590 | VM_ATDTOR_LOCK();
|
---|
2591 | PVMATDTOR pPrev = NULL;
|
---|
2592 | PVMATDTOR pCur = g_pVMAtDtorHead;
|
---|
2593 | while (pCur)
|
---|
2594 | {
|
---|
2595 | if (pfnAtDtor == pCur->pfnAtDtor)
|
---|
2596 | {
|
---|
2597 | if (pPrev)
|
---|
2598 | pPrev->pNext = pCur->pNext;
|
---|
2599 | else
|
---|
2600 | g_pVMAtDtorHead = pCur->pNext;
|
---|
2601 | pCur->pNext = NULL;
|
---|
2602 | VM_ATDTOR_UNLOCK();
|
---|
2603 |
|
---|
2604 | RTMemFree(pCur);
|
---|
2605 | return VINF_SUCCESS;
|
---|
2606 | }
|
---|
2607 |
|
---|
2608 | /* next */
|
---|
2609 | pPrev = pCur;
|
---|
2610 | pCur = pCur->pNext;
|
---|
2611 | }
|
---|
2612 | VM_ATDTOR_UNLOCK();
|
---|
2613 |
|
---|
2614 | return VERR_INVALID_PARAMETER;
|
---|
2615 | }
|
---|
2616 |
|
---|
2617 |
|
---|
2618 | /**
|
---|
2619 | * Walks the list of at VM destructor callbacks.
|
---|
2620 | * @param pVM The VM which is about to be destroyed.
|
---|
2621 | */
|
---|
2622 | static void vmR3AtDtor(PVM pVM)
|
---|
2623 | {
|
---|
2624 | /*
|
---|
2625 | * Find it, unlink it and free it.
|
---|
2626 | */
|
---|
2627 | VM_ATDTOR_LOCK();
|
---|
2628 | for (PVMATDTOR pCur = g_pVMAtDtorHead; pCur; pCur = pCur->pNext)
|
---|
2629 | pCur->pfnAtDtor(pVM, pCur->pvUser);
|
---|
2630 | VM_ATDTOR_UNLOCK();
|
---|
2631 | }
|
---|
2632 |
|
---|
2633 |
|
---|
2634 | /**
|
---|
2635 | * Worker which checks integrity of some internal structures.
|
---|
2636 | * This is yet another attempt to track down that AVL tree crash.
|
---|
2637 | */
|
---|
2638 | static void vmR3CheckIntegrity(PVM pVM)
|
---|
2639 | {
|
---|
2640 | #ifdef VBOX_STRICT
|
---|
2641 | int rc = PGMR3CheckIntegrity(pVM);
|
---|
2642 | AssertReleaseRC(rc);
|
---|
2643 | #endif
|
---|
2644 | }
|
---|
2645 |
|
---|
2646 |
|
---|
2647 | /**
|
---|
2648 | * EMT rendezvous worker for VMR3Reset.
|
---|
2649 | *
|
---|
2650 | * This is called by the emulation threads as a response to the reset request
|
---|
2651 | * issued by VMR3Reset().
|
---|
2652 | *
|
---|
2653 | * @returns VERR_VM_INVALID_VM_STATE, VINF_EM_RESET or VINF_EM_SUSPEND. (This
|
---|
2654 | * is a strict return code, see FNVMMEMTRENDEZVOUS.)
|
---|
2655 | *
|
---|
2656 | * @param pVM The VM handle.
|
---|
2657 | * @param pVCpu The VMCPU handle of the EMT.
|
---|
2658 | * @param pvUser Ignored.
|
---|
2659 | */
|
---|
2660 | static DECLCALLBACK(VBOXSTRICTRC) vmR3Reset(PVM pVM, PVMCPU pVCpu, void *pvUser)
|
---|
2661 | {
|
---|
2662 | Assert(!pvUser); NOREF(pvUser);
|
---|
2663 |
|
---|
2664 | /*
|
---|
2665 | * The first EMT will try change the state to resetting. If this fails,
|
---|
2666 | * we won't get called for the other EMTs.
|
---|
2667 | */
|
---|
2668 | if (pVCpu->idCpu == pVM->cCpus - 1)
|
---|
2669 | {
|
---|
2670 | int rc = vmR3TrySetState(pVM, "VMR3Reset", 3,
|
---|
2671 | VMSTATE_RESETTING, VMSTATE_RUNNING,
|
---|
2672 | VMSTATE_RESETTING, VMSTATE_SUSPENDED,
|
---|
2673 | VMSTATE_RESETTING_LS, VMSTATE_RUNNING_LS);
|
---|
2674 | if (RT_FAILURE(rc))
|
---|
2675 | return rc;
|
---|
2676 | }
|
---|
2677 |
|
---|
2678 | /*
|
---|
2679 | * Check the state.
|
---|
2680 | */
|
---|
2681 | VMSTATE enmVMState = VMR3GetState(pVM);
|
---|
2682 | AssertLogRelMsgReturn( enmVMState == VMSTATE_RESETTING
|
---|
2683 | || enmVMState == VMSTATE_RESETTING_LS,
|
---|
2684 | ("%s\n", VMR3GetStateName(enmVMState)),
|
---|
2685 | VERR_INTERNAL_ERROR_4);
|
---|
2686 |
|
---|
2687 | /*
|
---|
2688 | * EMT(0) does the full cleanup *after* all the other EMTs has been
|
---|
2689 | * thru here and been told to enter the EMSTATE_WAIT_SIPI state.
|
---|
2690 | *
|
---|
2691 | * Because there are per-cpu reset routines and order may/is important,
|
---|
2692 | * the following sequence looks a bit ugly...
|
---|
2693 | */
|
---|
2694 | if (pVCpu->idCpu == 0)
|
---|
2695 | vmR3CheckIntegrity(pVM);
|
---|
2696 |
|
---|
2697 | /* Reset the VCpu state. */
|
---|
2698 | VMCPU_ASSERT_STATE(pVCpu, VMCPUSTATE_STARTED);
|
---|
2699 |
|
---|
2700 | /* Clear all pending forced actions. */
|
---|
2701 | VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_ALL_MASK & ~VMCPU_FF_REQUEST);
|
---|
2702 |
|
---|
2703 | /*
|
---|
2704 | * Reset the VM components.
|
---|
2705 | */
|
---|
2706 | if (pVCpu->idCpu == 0)
|
---|
2707 | {
|
---|
2708 | PATMR3Reset(pVM);
|
---|
2709 | CSAMR3Reset(pVM);
|
---|
2710 | PGMR3Reset(pVM); /* We clear VM RAM in PGMR3Reset. It's vital PDMR3Reset is executed
|
---|
2711 | * _afterwards_. E.g. ACPI sets up RAM tables during init/reset. */
|
---|
2712 | /** @todo PGMR3Reset should be called after PDMR3Reset really, because we'll trash OS <-> hardware
|
---|
2713 | * communication structures residing in RAM when done in the other order. I.e. the device must be
|
---|
2714 | * quiesced first, then we clear the memory and plan tables. Probably have to make these things
|
---|
2715 | * explicit in some way, some memory setup pass or something.
|
---|
2716 | * (Example: DevAHCI may assert if memory is zeroed before it've read the FIS.)
|
---|
2717 | *
|
---|
2718 | * @bugref{4467}
|
---|
2719 | */
|
---|
2720 | MMR3Reset(pVM);
|
---|
2721 | PDMR3Reset(pVM);
|
---|
2722 | SELMR3Reset(pVM);
|
---|
2723 | TRPMR3Reset(pVM);
|
---|
2724 | REMR3Reset(pVM);
|
---|
2725 | IOMR3Reset(pVM);
|
---|
2726 | CPUMR3Reset(pVM);
|
---|
2727 | }
|
---|
2728 | CPUMR3ResetCpu(pVCpu);
|
---|
2729 | if (pVCpu->idCpu == 0)
|
---|
2730 | {
|
---|
2731 | TMR3Reset(pVM);
|
---|
2732 | EMR3Reset(pVM);
|
---|
2733 | HWACCMR3Reset(pVM); /* This must come *after* PATM, CSAM, CPUM, SELM and TRPM. */
|
---|
2734 |
|
---|
2735 | #ifdef LOG_ENABLED
|
---|
2736 | /*
|
---|
2737 | * Debug logging.
|
---|
2738 | */
|
---|
2739 | RTLogPrintf("\n\nThe VM was reset:\n");
|
---|
2740 | DBGFR3Info(pVM, "cpum", "verbose", NULL);
|
---|
2741 | #endif
|
---|
2742 |
|
---|
2743 | /*
|
---|
2744 | * Since EMT(0) is the last to go thru here, it will advance the state.
|
---|
2745 | * When a live save is active, we will move on to SuspendingLS but
|
---|
2746 | * leave it for VMR3Reset to do the actual suspending due to deadlock risks.
|
---|
2747 | */
|
---|
2748 | PUVM pUVM = pVM->pUVM;
|
---|
2749 | RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
|
---|
2750 | enmVMState = pVM->enmVMState;
|
---|
2751 | if (enmVMState == VMSTATE_RESETTING)
|
---|
2752 | {
|
---|
2753 | if (pUVM->vm.s.enmPrevVMState == VMSTATE_SUSPENDED)
|
---|
2754 | vmR3SetStateLocked(pVM, pUVM, VMSTATE_SUSPENDED, VMSTATE_RESETTING);
|
---|
2755 | else
|
---|
2756 | vmR3SetStateLocked(pVM, pUVM, VMSTATE_RUNNING, VMSTATE_RESETTING);
|
---|
2757 | }
|
---|
2758 | else
|
---|
2759 | vmR3SetStateLocked(pVM, pUVM, VMSTATE_SUSPENDING_LS, VMSTATE_RESETTING_LS);
|
---|
2760 | RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
|
---|
2761 |
|
---|
2762 | vmR3CheckIntegrity(pVM);
|
---|
2763 |
|
---|
2764 | /*
|
---|
2765 | * Do the suspend bit as well.
|
---|
2766 | * It only requires some EMT(0) work at present.
|
---|
2767 | */
|
---|
2768 | if (enmVMState != VMSTATE_RESETTING)
|
---|
2769 | {
|
---|
2770 | vmR3SuspendDoWork(pVM);
|
---|
2771 | vmR3SetState(pVM, VMSTATE_SUSPENDED_LS, VMSTATE_SUSPENDING_LS);
|
---|
2772 | }
|
---|
2773 | }
|
---|
2774 |
|
---|
2775 | return enmVMState == VMSTATE_RESETTING
|
---|
2776 | ? VINF_EM_RESET
|
---|
2777 | : VINF_EM_SUSPEND; /** @todo VINF_EM_SUSPEND has lower priority than VINF_EM_RESET, so fix races. Perhaps add a new code for this combined case. */
|
---|
2778 | }
|
---|
2779 |
|
---|
2780 |
|
---|
2781 | /**
|
---|
2782 | * Reset the current VM.
|
---|
2783 | *
|
---|
2784 | * @returns VBox status code.
|
---|
2785 | * @param pVM VM to reset.
|
---|
2786 | */
|
---|
2787 | VMMR3DECL(int) VMR3Reset(PVM pVM)
|
---|
2788 | {
|
---|
2789 | LogFlow(("VMR3Reset:\n"));
|
---|
2790 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
2791 |
|
---|
2792 | /*
|
---|
2793 | * Gather all the EMTs to make sure there are no races before
|
---|
2794 | * changing the VM state.
|
---|
2795 | */
|
---|
2796 | int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING | VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR,
|
---|
2797 | vmR3Reset, NULL);
|
---|
2798 | LogFlow(("VMR3Reset: returns %Rrc\n", rc));
|
---|
2799 | return rc;
|
---|
2800 | }
|
---|
2801 |
|
---|
2802 |
|
---|
2803 | /**
|
---|
2804 | * Gets the current VM state.
|
---|
2805 | *
|
---|
2806 | * @returns The current VM state.
|
---|
2807 | * @param pVM VM handle.
|
---|
2808 | * @thread Any
|
---|
2809 | */
|
---|
2810 | VMMR3DECL(VMSTATE) VMR3GetState(PVM pVM)
|
---|
2811 | {
|
---|
2812 | return pVM->enmVMState;
|
---|
2813 | }
|
---|
2814 |
|
---|
2815 |
|
---|
2816 | /**
|
---|
2817 | * Gets the state name string for a VM state.
|
---|
2818 | *
|
---|
2819 | * @returns Pointer to the state name. (readonly)
|
---|
2820 | * @param enmState The state.
|
---|
2821 | */
|
---|
2822 | VMMR3DECL(const char *) VMR3GetStateName(VMSTATE enmState)
|
---|
2823 | {
|
---|
2824 | switch (enmState)
|
---|
2825 | {
|
---|
2826 | case VMSTATE_CREATING: return "CREATING";
|
---|
2827 | case VMSTATE_CREATED: return "CREATED";
|
---|
2828 | case VMSTATE_LOADING: return "LOADING";
|
---|
2829 | case VMSTATE_POWERING_ON: return "POWERING_ON";
|
---|
2830 | case VMSTATE_RESUMING: return "RESUMING";
|
---|
2831 | case VMSTATE_RUNNING: return "RUNNING";
|
---|
2832 | case VMSTATE_RUNNING_LS: return "RUNNING_LS";
|
---|
2833 | case VMSTATE_RESETTING: return "RESETTING";
|
---|
2834 | case VMSTATE_RESETTING_LS: return "RESETTING_LS";
|
---|
2835 | case VMSTATE_SUSPENDED: return "SUSPENDED";
|
---|
2836 | case VMSTATE_SUSPENDED_LS: return "SUSPENDED_LS";
|
---|
2837 | case VMSTATE_SUSPENDED_EXT_LS: return "SUSPENDED_EXT_LS";
|
---|
2838 | case VMSTATE_SUSPENDING: return "SUSPENDING";
|
---|
2839 | case VMSTATE_SUSPENDING_LS: return "SUSPENDING_LS";
|
---|
2840 | case VMSTATE_SUSPENDING_EXT_LS: return "SUSPENDING_EXT_LS";
|
---|
2841 | case VMSTATE_SAVING: return "SAVING";
|
---|
2842 | case VMSTATE_DEBUGGING: return "DEBUGGING";
|
---|
2843 | case VMSTATE_DEBUGGING_LS: return "DEBUGGING_LS";
|
---|
2844 | case VMSTATE_POWERING_OFF: return "POWERING_OFF";
|
---|
2845 | case VMSTATE_POWERING_OFF_LS: return "POWERING_OFF_LS";
|
---|
2846 | case VMSTATE_FATAL_ERROR: return "FATAL_ERROR";
|
---|
2847 | case VMSTATE_FATAL_ERROR_LS: return "FATAL_ERROR_LS";
|
---|
2848 | case VMSTATE_GURU_MEDITATION: return "GURU_MEDITATION";
|
---|
2849 | case VMSTATE_GURU_MEDITATION_LS:return "GURU_MEDITATION_LS";
|
---|
2850 | case VMSTATE_LOAD_FAILURE: return "LOAD_FAILURE";
|
---|
2851 | case VMSTATE_OFF: return "OFF";
|
---|
2852 | case VMSTATE_OFF_LS: return "OFF_LS";
|
---|
2853 | case VMSTATE_DESTROYING: return "DESTROYING";
|
---|
2854 | case VMSTATE_TERMINATED: return "TERMINATED";
|
---|
2855 |
|
---|
2856 | default:
|
---|
2857 | AssertMsgFailed(("Unknown state %d\n", enmState));
|
---|
2858 | return "Unknown!\n";
|
---|
2859 | }
|
---|
2860 | }
|
---|
2861 |
|
---|
2862 |
|
---|
2863 | /**
|
---|
2864 | * Validates the state transition in strict builds.
|
---|
2865 | *
|
---|
2866 | * @returns true if valid, false if not.
|
---|
2867 | *
|
---|
2868 | * @param enmStateOld The old (current) state.
|
---|
2869 | * @param enmStateNew The proposed new state.
|
---|
2870 | *
|
---|
2871 | * @remarks The reference for this is found in doc/vp/VMM.vpp, the VMSTATE
|
---|
2872 | * diagram (under State Machine Diagram).
|
---|
2873 | */
|
---|
2874 | static bool vmR3ValidateStateTransition(VMSTATE enmStateOld, VMSTATE enmStateNew)
|
---|
2875 | {
|
---|
2876 | #ifdef VBOX_STRICT
|
---|
2877 | switch (enmStateOld)
|
---|
2878 | {
|
---|
2879 | case VMSTATE_CREATING:
|
---|
2880 | AssertMsgReturn(enmStateNew == VMSTATE_CREATED, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
2881 | break;
|
---|
2882 |
|
---|
2883 | case VMSTATE_CREATED:
|
---|
2884 | AssertMsgReturn( enmStateNew == VMSTATE_LOADING
|
---|
2885 | || enmStateNew == VMSTATE_POWERING_ON
|
---|
2886 | || enmStateNew == VMSTATE_POWERING_OFF
|
---|
2887 | , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
2888 | break;
|
---|
2889 |
|
---|
2890 | case VMSTATE_LOADING:
|
---|
2891 | AssertMsgReturn( enmStateNew == VMSTATE_SUSPENDED
|
---|
2892 | || enmStateNew == VMSTATE_LOAD_FAILURE
|
---|
2893 | , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
2894 | break;
|
---|
2895 |
|
---|
2896 | case VMSTATE_POWERING_ON:
|
---|
2897 | AssertMsgReturn( enmStateNew == VMSTATE_RUNNING
|
---|
2898 | /*|| enmStateNew == VMSTATE_FATAL_ERROR ?*/
|
---|
2899 | , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
2900 | break;
|
---|
2901 |
|
---|
2902 | case VMSTATE_RESUMING:
|
---|
2903 | AssertMsgReturn( enmStateNew == VMSTATE_RUNNING
|
---|
2904 | /*|| enmStateNew == VMSTATE_FATAL_ERROR ?*/
|
---|
2905 | , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
2906 | break;
|
---|
2907 |
|
---|
2908 | case VMSTATE_RUNNING:
|
---|
2909 | AssertMsgReturn( enmStateNew == VMSTATE_POWERING_OFF
|
---|
2910 | || enmStateNew == VMSTATE_SUSPENDING
|
---|
2911 | || enmStateNew == VMSTATE_RESETTING
|
---|
2912 | || enmStateNew == VMSTATE_RUNNING_LS
|
---|
2913 | || enmStateNew == VMSTATE_DEBUGGING
|
---|
2914 | || enmStateNew == VMSTATE_FATAL_ERROR
|
---|
2915 | || enmStateNew == VMSTATE_GURU_MEDITATION
|
---|
2916 | , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
2917 | break;
|
---|
2918 |
|
---|
2919 | case VMSTATE_RUNNING_LS:
|
---|
2920 | AssertMsgReturn( enmStateNew == VMSTATE_POWERING_OFF_LS
|
---|
2921 | || enmStateNew == VMSTATE_SUSPENDING_LS
|
---|
2922 | || enmStateNew == VMSTATE_SUSPENDING_EXT_LS
|
---|
2923 | || enmStateNew == VMSTATE_RESETTING_LS
|
---|
2924 | || enmStateNew == VMSTATE_RUNNING
|
---|
2925 | || enmStateNew == VMSTATE_DEBUGGING_LS
|
---|
2926 | || enmStateNew == VMSTATE_FATAL_ERROR_LS
|
---|
2927 | || enmStateNew == VMSTATE_GURU_MEDITATION_LS
|
---|
2928 | , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
2929 | break;
|
---|
2930 |
|
---|
2931 | case VMSTATE_RESETTING:
|
---|
2932 | AssertMsgReturn(enmStateNew == VMSTATE_RUNNING, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
2933 | break;
|
---|
2934 |
|
---|
2935 | case VMSTATE_RESETTING_LS:
|
---|
2936 | AssertMsgReturn( enmStateNew == VMSTATE_SUSPENDING_LS
|
---|
2937 | , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
2938 | break;
|
---|
2939 |
|
---|
2940 | case VMSTATE_SUSPENDING:
|
---|
2941 | AssertMsgReturn(enmStateNew == VMSTATE_SUSPENDED, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
2942 | break;
|
---|
2943 |
|
---|
2944 | case VMSTATE_SUSPENDING_LS:
|
---|
2945 | AssertMsgReturn( enmStateNew == VMSTATE_SUSPENDING
|
---|
2946 | || enmStateNew == VMSTATE_SUSPENDED_LS
|
---|
2947 | , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
2948 | break;
|
---|
2949 |
|
---|
2950 | case VMSTATE_SUSPENDING_EXT_LS:
|
---|
2951 | AssertMsgReturn( enmStateNew == VMSTATE_SUSPENDING
|
---|
2952 | || enmStateNew == VMSTATE_SUSPENDED_EXT_LS
|
---|
2953 | , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
2954 | break;
|
---|
2955 |
|
---|
2956 | case VMSTATE_SUSPENDED:
|
---|
2957 | AssertMsgReturn( enmStateNew == VMSTATE_POWERING_OFF
|
---|
2958 | || enmStateNew == VMSTATE_SAVING
|
---|
2959 | || enmStateNew == VMSTATE_RESETTING
|
---|
2960 | || enmStateNew == VMSTATE_RESUMING
|
---|
2961 | || enmStateNew == VMSTATE_LOADING
|
---|
2962 | , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
2963 | break;
|
---|
2964 |
|
---|
2965 | case VMSTATE_SUSPENDED_LS:
|
---|
2966 | AssertMsgReturn( enmStateNew == VMSTATE_SUSPENDED
|
---|
2967 | || enmStateNew == VMSTATE_SAVING
|
---|
2968 | , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
2969 | break;
|
---|
2970 |
|
---|
2971 | case VMSTATE_SUSPENDED_EXT_LS:
|
---|
2972 | AssertMsgReturn( enmStateNew == VMSTATE_SUSPENDED
|
---|
2973 | || enmStateNew == VMSTATE_SAVING
|
---|
2974 | , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
2975 | break;
|
---|
2976 |
|
---|
2977 | case VMSTATE_SAVING:
|
---|
2978 | AssertMsgReturn(enmStateNew == VMSTATE_SUSPENDED, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
2979 | break;
|
---|
2980 |
|
---|
2981 | case VMSTATE_DEBUGGING:
|
---|
2982 | AssertMsgReturn( enmStateNew == VMSTATE_RUNNING
|
---|
2983 | || enmStateNew == VMSTATE_POWERING_OFF
|
---|
2984 | , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
2985 | break;
|
---|
2986 |
|
---|
2987 | case VMSTATE_DEBUGGING_LS:
|
---|
2988 | AssertMsgReturn( enmStateNew == VMSTATE_DEBUGGING
|
---|
2989 | || enmStateNew == VMSTATE_RUNNING_LS
|
---|
2990 | || enmStateNew == VMSTATE_POWERING_OFF_LS
|
---|
2991 | , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
2992 | break;
|
---|
2993 |
|
---|
2994 | case VMSTATE_POWERING_OFF:
|
---|
2995 | AssertMsgReturn(enmStateNew == VMSTATE_OFF, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
2996 | break;
|
---|
2997 |
|
---|
2998 | case VMSTATE_POWERING_OFF_LS:
|
---|
2999 | AssertMsgReturn( enmStateNew == VMSTATE_POWERING_OFF
|
---|
3000 | || enmStateNew == VMSTATE_OFF_LS
|
---|
3001 | , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
3002 | break;
|
---|
3003 |
|
---|
3004 | case VMSTATE_OFF:
|
---|
3005 | AssertMsgReturn(enmStateNew == VMSTATE_DESTROYING, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
3006 | break;
|
---|
3007 |
|
---|
3008 | case VMSTATE_OFF_LS:
|
---|
3009 | AssertMsgReturn(enmStateNew == VMSTATE_OFF, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
3010 | break;
|
---|
3011 |
|
---|
3012 | case VMSTATE_FATAL_ERROR:
|
---|
3013 | AssertMsgReturn(enmStateNew == VMSTATE_POWERING_OFF, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
3014 | break;
|
---|
3015 |
|
---|
3016 | case VMSTATE_FATAL_ERROR_LS:
|
---|
3017 | AssertMsgReturn( enmStateNew == VMSTATE_FATAL_ERROR
|
---|
3018 | || enmStateNew == VMSTATE_POWERING_OFF_LS
|
---|
3019 | , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
3020 | break;
|
---|
3021 |
|
---|
3022 | case VMSTATE_GURU_MEDITATION:
|
---|
3023 | AssertMsgReturn( enmStateNew == VMSTATE_DEBUGGING
|
---|
3024 | || enmStateNew == VMSTATE_POWERING_OFF
|
---|
3025 | , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
3026 | break;
|
---|
3027 |
|
---|
3028 | case VMSTATE_GURU_MEDITATION_LS:
|
---|
3029 | AssertMsgReturn( enmStateNew == VMSTATE_GURU_MEDITATION
|
---|
3030 | || enmStateNew == VMSTATE_DEBUGGING_LS
|
---|
3031 | || enmStateNew == VMSTATE_POWERING_OFF_LS
|
---|
3032 | , ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
3033 | break;
|
---|
3034 |
|
---|
3035 | case VMSTATE_LOAD_FAILURE:
|
---|
3036 | AssertMsgReturn(enmStateNew == VMSTATE_POWERING_OFF, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
3037 | break;
|
---|
3038 |
|
---|
3039 | case VMSTATE_DESTROYING:
|
---|
3040 | AssertMsgReturn(enmStateNew == VMSTATE_TERMINATED, ("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
3041 | break;
|
---|
3042 |
|
---|
3043 | case VMSTATE_TERMINATED:
|
---|
3044 | default:
|
---|
3045 | AssertMsgFailedReturn(("%s -> %s\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)), false);
|
---|
3046 | break;
|
---|
3047 | }
|
---|
3048 | #endif /* VBOX_STRICT */
|
---|
3049 | return true;
|
---|
3050 | }
|
---|
3051 |
|
---|
3052 |
|
---|
3053 | /**
|
---|
3054 | * Does the state change callouts.
|
---|
3055 | *
|
---|
3056 | * The caller owns the AtStateCritSect.
|
---|
3057 | *
|
---|
3058 | * @param pVM The VM handle.
|
---|
3059 | * @param pUVM The UVM handle.
|
---|
3060 | * @param enmStateNew The New state.
|
---|
3061 | * @param enmStateOld The old state.
|
---|
3062 | */
|
---|
3063 | static void vmR3DoAtState(PVM pVM, PUVM pUVM, VMSTATE enmStateNew, VMSTATE enmStateOld)
|
---|
3064 | {
|
---|
3065 | LogRel(("Changing the VM state from '%s' to '%s'.\n", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)));
|
---|
3066 |
|
---|
3067 | for (PVMATSTATE pCur = pUVM->vm.s.pAtState; pCur; pCur = pCur->pNext)
|
---|
3068 | {
|
---|
3069 | pCur->pfnAtState(pVM, enmStateNew, enmStateOld, pCur->pvUser);
|
---|
3070 | if ( enmStateNew != VMSTATE_DESTROYING
|
---|
3071 | && pVM->enmVMState == VMSTATE_DESTROYING)
|
---|
3072 | break;
|
---|
3073 | AssertMsg(pVM->enmVMState == enmStateNew,
|
---|
3074 | ("You are not allowed to change the state while in the change callback, except "
|
---|
3075 | "from destroying the VM. There are restrictions in the way the state changes "
|
---|
3076 | "are propagated up to the EM execution loop and it makes the program flow very "
|
---|
3077 | "difficult to follow. (%s, expected %s, old %s)\n",
|
---|
3078 | VMR3GetStateName(pVM->enmVMState), VMR3GetStateName(enmStateNew),
|
---|
3079 | VMR3GetStateName(enmStateOld)));
|
---|
3080 | }
|
---|
3081 | }
|
---|
3082 |
|
---|
3083 |
|
---|
3084 | /**
|
---|
3085 | * Sets the current VM state, with the AtStatCritSect already entered.
|
---|
3086 | *
|
---|
3087 | * @param pVM The VM handle.
|
---|
3088 | * @param pUVM The UVM handle.
|
---|
3089 | * @param enmStateNew The new state.
|
---|
3090 | * @param enmStateOld The old state.
|
---|
3091 | */
|
---|
3092 | static void vmR3SetStateLocked(PVM pVM, PUVM pUVM, VMSTATE enmStateNew, VMSTATE enmStateOld)
|
---|
3093 | {
|
---|
3094 | vmR3ValidateStateTransition(enmStateOld, enmStateNew);
|
---|
3095 |
|
---|
3096 | AssertMsg(pVM->enmVMState == enmStateOld,
|
---|
3097 | ("%s != %s\n", VMR3GetStateName(pVM->enmVMState), VMR3GetStateName(enmStateOld)));
|
---|
3098 | pUVM->vm.s.enmPrevVMState = enmStateOld;
|
---|
3099 | pVM->enmVMState = enmStateNew;
|
---|
3100 |
|
---|
3101 | vmR3DoAtState(pVM, pUVM, enmStateNew, enmStateOld);
|
---|
3102 | }
|
---|
3103 |
|
---|
3104 |
|
---|
3105 | /**
|
---|
3106 | * Sets the current VM state.
|
---|
3107 | *
|
---|
3108 | * @param pVM VM handle.
|
---|
3109 | * @param enmStateNew The new state.
|
---|
3110 | * @param enmStateOld The old state (for asserting only).
|
---|
3111 | */
|
---|
3112 | static void vmR3SetState(PVM pVM, VMSTATE enmStateNew, VMSTATE enmStateOld)
|
---|
3113 | {
|
---|
3114 | PUVM pUVM = pVM->pUVM;
|
---|
3115 | RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
|
---|
3116 |
|
---|
3117 | AssertMsg(pVM->enmVMState == enmStateOld,
|
---|
3118 | ("%s != %s\n", VMR3GetStateName(pVM->enmVMState), VMR3GetStateName(enmStateOld)));
|
---|
3119 | vmR3SetStateLocked(pVM, pUVM, enmStateNew, pVM->enmVMState);
|
---|
3120 |
|
---|
3121 | RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
|
---|
3122 | }
|
---|
3123 |
|
---|
3124 |
|
---|
3125 | /**
|
---|
3126 | * Tries to perform a state transition.
|
---|
3127 | *
|
---|
3128 | * @returns The 1-based ordinal of the succeeding transition.
|
---|
3129 | * VERR_VM_INVALID_VM_STATE and Assert+LogRel on failure.
|
---|
3130 | *
|
---|
3131 | * @param pVM The VM handle.
|
---|
3132 | * @param pszWho Who is trying to change it.
|
---|
3133 | * @param cTransitions The number of transitions in the ellipsis.
|
---|
3134 | * @param ... Transition pairs; new, old.
|
---|
3135 | */
|
---|
3136 | static int vmR3TrySetState(PVM pVM, const char *pszWho, unsigned cTransitions, ...)
|
---|
3137 | {
|
---|
3138 | va_list va;
|
---|
3139 | VMSTATE enmStateNew = VMSTATE_CREATED;
|
---|
3140 | VMSTATE enmStateOld = VMSTATE_CREATED;
|
---|
3141 |
|
---|
3142 | #ifdef VBOX_STRICT
|
---|
3143 | /*
|
---|
3144 | * Validate the input first.
|
---|
3145 | */
|
---|
3146 | va_start(va, cTransitions);
|
---|
3147 | for (unsigned i = 0; i < cTransitions; i++)
|
---|
3148 | {
|
---|
3149 | enmStateNew = (VMSTATE)va_arg(va, /*VMSTATE*/int);
|
---|
3150 | enmStateOld = (VMSTATE)va_arg(va, /*VMSTATE*/int);
|
---|
3151 | vmR3ValidateStateTransition(enmStateOld, enmStateNew);
|
---|
3152 | }
|
---|
3153 | va_end(va);
|
---|
3154 | #endif
|
---|
3155 |
|
---|
3156 | /*
|
---|
3157 | * Grab the lock and see if any of the proposed transisions works out.
|
---|
3158 | */
|
---|
3159 | va_start(va, cTransitions);
|
---|
3160 | int rc = VERR_VM_INVALID_VM_STATE;
|
---|
3161 | PUVM pUVM = pVM->pUVM;
|
---|
3162 | RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
|
---|
3163 |
|
---|
3164 | VMSTATE enmStateCur = pVM->enmVMState;
|
---|
3165 |
|
---|
3166 | for (unsigned i = 0; i < cTransitions; i++)
|
---|
3167 | {
|
---|
3168 | enmStateNew = (VMSTATE)va_arg(va, /*VMSTATE*/int);
|
---|
3169 | enmStateOld = (VMSTATE)va_arg(va, /*VMSTATE*/int);
|
---|
3170 | if (enmStateCur == enmStateOld)
|
---|
3171 | {
|
---|
3172 | vmR3SetStateLocked(pVM, pUVM, enmStateNew, enmStateOld);
|
---|
3173 | rc = i + 1;
|
---|
3174 | break;
|
---|
3175 | }
|
---|
3176 | }
|
---|
3177 |
|
---|
3178 | if (RT_FAILURE(rc))
|
---|
3179 | {
|
---|
3180 | /*
|
---|
3181 | * Complain about it.
|
---|
3182 | */
|
---|
3183 | if (cTransitions == 1)
|
---|
3184 | LogRel(("%s: %s -> %s failed, because the VM state is actually %s\n",
|
---|
3185 | pszWho, VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew), VMR3GetStateName(enmStateCur)));
|
---|
3186 | else
|
---|
3187 | {
|
---|
3188 | va_end(va);
|
---|
3189 | va_start(va, cTransitions);
|
---|
3190 | LogRel(("%s:\n", pszWho));
|
---|
3191 | for (unsigned i = 0; i < cTransitions; i++)
|
---|
3192 | {
|
---|
3193 | enmStateNew = (VMSTATE)va_arg(va, /*VMSTATE*/int);
|
---|
3194 | enmStateOld = (VMSTATE)va_arg(va, /*VMSTATE*/int);
|
---|
3195 | LogRel(("%s%s -> %s",
|
---|
3196 | i ? ", " : " ", VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew)));
|
---|
3197 | }
|
---|
3198 | LogRel((" failed, because the VM state is actually %s\n", VMR3GetStateName(enmStateCur)));
|
---|
3199 | }
|
---|
3200 |
|
---|
3201 | VMSetError(pVM, VERR_VM_INVALID_VM_STATE, RT_SRC_POS,
|
---|
3202 | N_("%s failed because the VM state is %s instead of %s"),
|
---|
3203 | VMR3GetStateName(enmStateCur), VMR3GetStateName(enmStateOld));
|
---|
3204 | AssertMsgFailed(("%s: %s -> %s failed, state is actually %s\n",
|
---|
3205 | pszWho, VMR3GetStateName(enmStateOld), VMR3GetStateName(enmStateNew), VMR3GetStateName(enmStateCur)));
|
---|
3206 | }
|
---|
3207 |
|
---|
3208 | RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
|
---|
3209 | va_end(va);
|
---|
3210 | Assert(rc > 0 || rc < 0);
|
---|
3211 | return rc;
|
---|
3212 | }
|
---|
3213 |
|
---|
3214 |
|
---|
3215 | /**
|
---|
3216 | * Flag a guru meditation ... a hack.
|
---|
3217 | *
|
---|
3218 | * @param pVM The VM handle
|
---|
3219 | *
|
---|
3220 | * @todo Rewrite this part. The guru meditation should be flagged
|
---|
3221 | * immediately by the VMM and not by VMEmt.cpp when it's all over.
|
---|
3222 | */
|
---|
3223 | void vmR3SetGuruMeditation(PVM pVM)
|
---|
3224 | {
|
---|
3225 | PUVM pUVM = pVM->pUVM;
|
---|
3226 | RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
|
---|
3227 |
|
---|
3228 | VMSTATE enmStateCur = pVM->enmVMState;
|
---|
3229 | if (enmStateCur == VMSTATE_RUNNING)
|
---|
3230 | vmR3SetStateLocked(pVM, pUVM, VMSTATE_GURU_MEDITATION, VMSTATE_RUNNING);
|
---|
3231 | else if (enmStateCur == VMSTATE_RUNNING_LS)
|
---|
3232 | {
|
---|
3233 | vmR3SetStateLocked(pVM, pUVM, VMSTATE_GURU_MEDITATION_LS, VMSTATE_RUNNING_LS);
|
---|
3234 | SSMR3Cancel(pVM);
|
---|
3235 | }
|
---|
3236 |
|
---|
3237 | RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
|
---|
3238 | }
|
---|
3239 |
|
---|
3240 |
|
---|
3241 | /**
|
---|
3242 | * Checks if the VM was teleported and hasn't been fully resumed yet.
|
---|
3243 | *
|
---|
3244 | * This applies to both sides of the teleportation since we may leave a working
|
---|
3245 | * clone behind and the user is allowed to resume this...
|
---|
3246 | *
|
---|
3247 | * @returns true / false.
|
---|
3248 | * @param pVM The VM handle.
|
---|
3249 | * @thread Any thread.
|
---|
3250 | */
|
---|
3251 | VMMR3DECL(bool) VMR3TeleportedAndNotFullyResumedYet(PVM pVM)
|
---|
3252 | {
|
---|
3253 | VM_ASSERT_VALID_EXT_RETURN(pVM, false);
|
---|
3254 | return pVM->vm.s.fTeleportedAndNotFullyResumedYet;
|
---|
3255 | }
|
---|
3256 |
|
---|
3257 |
|
---|
3258 | /**
|
---|
3259 | * Registers a VM state change callback.
|
---|
3260 | *
|
---|
3261 | * You are not allowed to call any function which changes the VM state from a
|
---|
3262 | * state callback, except VMR3Destroy().
|
---|
3263 | *
|
---|
3264 | * @returns VBox status code.
|
---|
3265 | * @param pVM VM handle.
|
---|
3266 | * @param pfnAtState Pointer to callback.
|
---|
3267 | * @param pvUser User argument.
|
---|
3268 | * @thread Any.
|
---|
3269 | */
|
---|
3270 | VMMR3DECL(int) VMR3AtStateRegister(PVM pVM, PFNVMATSTATE pfnAtState, void *pvUser)
|
---|
3271 | {
|
---|
3272 | LogFlow(("VMR3AtStateRegister: pfnAtState=%p pvUser=%p\n", pfnAtState, pvUser));
|
---|
3273 |
|
---|
3274 | /*
|
---|
3275 | * Validate input.
|
---|
3276 | */
|
---|
3277 | AssertPtrReturn(pfnAtState, VERR_INVALID_PARAMETER);
|
---|
3278 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
3279 |
|
---|
3280 | /*
|
---|
3281 | * Allocate a new record.
|
---|
3282 | */
|
---|
3283 | PUVM pUVM = pVM->pUVM;
|
---|
3284 | PVMATSTATE pNew = (PVMATSTATE)MMR3HeapAllocU(pUVM, MM_TAG_VM, sizeof(*pNew));
|
---|
3285 | if (!pNew)
|
---|
3286 | return VERR_NO_MEMORY;
|
---|
3287 |
|
---|
3288 | /* fill */
|
---|
3289 | pNew->pfnAtState = pfnAtState;
|
---|
3290 | pNew->pvUser = pvUser;
|
---|
3291 |
|
---|
3292 | /* insert */
|
---|
3293 | RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
|
---|
3294 | pNew->pNext = *pUVM->vm.s.ppAtStateNext;
|
---|
3295 | *pUVM->vm.s.ppAtStateNext = pNew;
|
---|
3296 | pUVM->vm.s.ppAtStateNext = &pNew->pNext;
|
---|
3297 | RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
|
---|
3298 |
|
---|
3299 | return VINF_SUCCESS;
|
---|
3300 | }
|
---|
3301 |
|
---|
3302 |
|
---|
3303 | /**
|
---|
3304 | * Deregisters a VM state change callback.
|
---|
3305 | *
|
---|
3306 | * @returns VBox status code.
|
---|
3307 | * @param pVM VM handle.
|
---|
3308 | * @param pfnAtState Pointer to callback.
|
---|
3309 | * @param pvUser User argument.
|
---|
3310 | * @thread Any.
|
---|
3311 | */
|
---|
3312 | VMMR3DECL(int) VMR3AtStateDeregister(PVM pVM, PFNVMATSTATE pfnAtState, void *pvUser)
|
---|
3313 | {
|
---|
3314 | LogFlow(("VMR3AtStateDeregister: pfnAtState=%p pvUser=%p\n", pfnAtState, pvUser));
|
---|
3315 |
|
---|
3316 | /*
|
---|
3317 | * Validate input.
|
---|
3318 | */
|
---|
3319 | AssertPtrReturn(pfnAtState, VERR_INVALID_PARAMETER);
|
---|
3320 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
3321 |
|
---|
3322 | PUVM pUVM = pVM->pUVM;
|
---|
3323 | RTCritSectEnter(&pUVM->vm.s.AtStateCritSect);
|
---|
3324 |
|
---|
3325 | /*
|
---|
3326 | * Search the list for the entry.
|
---|
3327 | */
|
---|
3328 | PVMATSTATE pPrev = NULL;
|
---|
3329 | PVMATSTATE pCur = pUVM->vm.s.pAtState;
|
---|
3330 | while ( pCur
|
---|
3331 | && ( pCur->pfnAtState != pfnAtState
|
---|
3332 | || pCur->pvUser != pvUser))
|
---|
3333 | {
|
---|
3334 | pPrev = pCur;
|
---|
3335 | pCur = pCur->pNext;
|
---|
3336 | }
|
---|
3337 | if (!pCur)
|
---|
3338 | {
|
---|
3339 | AssertMsgFailed(("pfnAtState=%p was not found\n", pfnAtState));
|
---|
3340 | RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
|
---|
3341 | return VERR_FILE_NOT_FOUND;
|
---|
3342 | }
|
---|
3343 |
|
---|
3344 | /*
|
---|
3345 | * Unlink it.
|
---|
3346 | */
|
---|
3347 | if (pPrev)
|
---|
3348 | {
|
---|
3349 | pPrev->pNext = pCur->pNext;
|
---|
3350 | if (!pCur->pNext)
|
---|
3351 | pUVM->vm.s.ppAtStateNext = &pPrev->pNext;
|
---|
3352 | }
|
---|
3353 | else
|
---|
3354 | {
|
---|
3355 | pUVM->vm.s.pAtState = pCur->pNext;
|
---|
3356 | if (!pCur->pNext)
|
---|
3357 | pUVM->vm.s.ppAtStateNext = &pUVM->vm.s.pAtState;
|
---|
3358 | }
|
---|
3359 |
|
---|
3360 | RTCritSectLeave(&pUVM->vm.s.AtStateCritSect);
|
---|
3361 |
|
---|
3362 | /*
|
---|
3363 | * Free it.
|
---|
3364 | */
|
---|
3365 | pCur->pfnAtState = NULL;
|
---|
3366 | pCur->pNext = NULL;
|
---|
3367 | MMR3HeapFree(pCur);
|
---|
3368 |
|
---|
3369 | return VINF_SUCCESS;
|
---|
3370 | }
|
---|
3371 |
|
---|
3372 |
|
---|
3373 | /**
|
---|
3374 | * Registers a VM error callback.
|
---|
3375 | *
|
---|
3376 | * @returns VBox status code.
|
---|
3377 | * @param pVM The VM handle.
|
---|
3378 | * @param pfnAtError Pointer to callback.
|
---|
3379 | * @param pvUser User argument.
|
---|
3380 | * @thread Any.
|
---|
3381 | */
|
---|
3382 | VMMR3DECL(int) VMR3AtErrorRegister(PVM pVM, PFNVMATERROR pfnAtError, void *pvUser)
|
---|
3383 | {
|
---|
3384 | return VMR3AtErrorRegisterU(pVM->pUVM, pfnAtError, pvUser);
|
---|
3385 | }
|
---|
3386 |
|
---|
3387 |
|
---|
3388 | /**
|
---|
3389 | * Registers a VM error callback.
|
---|
3390 | *
|
---|
3391 | * @returns VBox status code.
|
---|
3392 | * @param pUVM The VM handle.
|
---|
3393 | * @param pfnAtError Pointer to callback.
|
---|
3394 | * @param pvUser User argument.
|
---|
3395 | * @thread Any.
|
---|
3396 | */
|
---|
3397 | VMMR3DECL(int) VMR3AtErrorRegisterU(PUVM pUVM, PFNVMATERROR pfnAtError, void *pvUser)
|
---|
3398 | {
|
---|
3399 | LogFlow(("VMR3AtErrorRegister: pfnAtError=%p pvUser=%p\n", pfnAtError, pvUser));
|
---|
3400 |
|
---|
3401 | /*
|
---|
3402 | * Validate input.
|
---|
3403 | */
|
---|
3404 | AssertPtrReturn(pfnAtError, VERR_INVALID_PARAMETER);
|
---|
3405 | UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
|
---|
3406 |
|
---|
3407 | /*
|
---|
3408 | * Allocate a new record.
|
---|
3409 | */
|
---|
3410 | PVMATERROR pNew = (PVMATERROR)MMR3HeapAllocU(pUVM, MM_TAG_VM, sizeof(*pNew));
|
---|
3411 | if (!pNew)
|
---|
3412 | return VERR_NO_MEMORY;
|
---|
3413 |
|
---|
3414 | /* fill */
|
---|
3415 | pNew->pfnAtError = pfnAtError;
|
---|
3416 | pNew->pvUser = pvUser;
|
---|
3417 |
|
---|
3418 | /* insert */
|
---|
3419 | RTCritSectEnter(&pUVM->vm.s.AtErrorCritSect);
|
---|
3420 | pNew->pNext = *pUVM->vm.s.ppAtErrorNext;
|
---|
3421 | *pUVM->vm.s.ppAtErrorNext = pNew;
|
---|
3422 | pUVM->vm.s.ppAtErrorNext = &pNew->pNext;
|
---|
3423 | RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
|
---|
3424 |
|
---|
3425 | return VINF_SUCCESS;
|
---|
3426 | }
|
---|
3427 |
|
---|
3428 |
|
---|
3429 | /**
|
---|
3430 | * Deregisters a VM error callback.
|
---|
3431 | *
|
---|
3432 | * @returns VBox status code.
|
---|
3433 | * @param pVM The VM handle.
|
---|
3434 | * @param pfnAtError Pointer to callback.
|
---|
3435 | * @param pvUser User argument.
|
---|
3436 | * @thread Any.
|
---|
3437 | */
|
---|
3438 | VMMR3DECL(int) VMR3AtErrorDeregister(PVM pVM, PFNVMATERROR pfnAtError, void *pvUser)
|
---|
3439 | {
|
---|
3440 | LogFlow(("VMR3AtErrorDeregister: pfnAtError=%p pvUser=%p\n", pfnAtError, pvUser));
|
---|
3441 |
|
---|
3442 | /*
|
---|
3443 | * Validate input.
|
---|
3444 | */
|
---|
3445 | AssertPtrReturn(pfnAtError, VERR_INVALID_PARAMETER);
|
---|
3446 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
3447 |
|
---|
3448 | PUVM pUVM = pVM->pUVM;
|
---|
3449 | RTCritSectEnter(&pUVM->vm.s.AtErrorCritSect);
|
---|
3450 |
|
---|
3451 | /*
|
---|
3452 | * Search the list for the entry.
|
---|
3453 | */
|
---|
3454 | PVMATERROR pPrev = NULL;
|
---|
3455 | PVMATERROR pCur = pUVM->vm.s.pAtError;
|
---|
3456 | while ( pCur
|
---|
3457 | && ( pCur->pfnAtError != pfnAtError
|
---|
3458 | || pCur->pvUser != pvUser))
|
---|
3459 | {
|
---|
3460 | pPrev = pCur;
|
---|
3461 | pCur = pCur->pNext;
|
---|
3462 | }
|
---|
3463 | if (!pCur)
|
---|
3464 | {
|
---|
3465 | AssertMsgFailed(("pfnAtError=%p was not found\n", pfnAtError));
|
---|
3466 | RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
|
---|
3467 | return VERR_FILE_NOT_FOUND;
|
---|
3468 | }
|
---|
3469 |
|
---|
3470 | /*
|
---|
3471 | * Unlink it.
|
---|
3472 | */
|
---|
3473 | if (pPrev)
|
---|
3474 | {
|
---|
3475 | pPrev->pNext = pCur->pNext;
|
---|
3476 | if (!pCur->pNext)
|
---|
3477 | pUVM->vm.s.ppAtErrorNext = &pPrev->pNext;
|
---|
3478 | }
|
---|
3479 | else
|
---|
3480 | {
|
---|
3481 | pUVM->vm.s.pAtError = pCur->pNext;
|
---|
3482 | if (!pCur->pNext)
|
---|
3483 | pUVM->vm.s.ppAtErrorNext = &pUVM->vm.s.pAtError;
|
---|
3484 | }
|
---|
3485 |
|
---|
3486 | RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
|
---|
3487 |
|
---|
3488 | /*
|
---|
3489 | * Free it.
|
---|
3490 | */
|
---|
3491 | pCur->pfnAtError = NULL;
|
---|
3492 | pCur->pNext = NULL;
|
---|
3493 | MMR3HeapFree(pCur);
|
---|
3494 |
|
---|
3495 | return VINF_SUCCESS;
|
---|
3496 | }
|
---|
3497 |
|
---|
3498 |
|
---|
3499 | /**
|
---|
3500 | * Ellipsis to va_list wrapper for calling pfnAtError.
|
---|
3501 | */
|
---|
3502 | static void vmR3SetErrorWorkerDoCall(PVM pVM, PVMATERROR pCur, int rc, RT_SRC_POS_DECL, const char *pszFormat, ...)
|
---|
3503 | {
|
---|
3504 | va_list va;
|
---|
3505 | va_start(va, pszFormat);
|
---|
3506 | pCur->pfnAtError(pVM, pCur->pvUser, rc, RT_SRC_POS_ARGS, pszFormat, va);
|
---|
3507 | va_end(va);
|
---|
3508 | }
|
---|
3509 |
|
---|
3510 |
|
---|
3511 | /**
|
---|
3512 | * This is a worker function for GC and Ring-0 calls to VMSetError and VMSetErrorV.
|
---|
3513 | * The message is found in VMINT.
|
---|
3514 | *
|
---|
3515 | * @param pVM The VM handle.
|
---|
3516 | * @thread EMT.
|
---|
3517 | */
|
---|
3518 | VMMR3DECL(void) VMR3SetErrorWorker(PVM pVM)
|
---|
3519 | {
|
---|
3520 | VM_ASSERT_EMT(pVM);
|
---|
3521 | AssertReleaseMsgFailed(("And we have a winner! You get to implement Ring-0 and GC VMSetErrorV! Contrats!\n"));
|
---|
3522 |
|
---|
3523 | /*
|
---|
3524 | * Unpack the error (if we managed to format one).
|
---|
3525 | */
|
---|
3526 | PVMERROR pErr = pVM->vm.s.pErrorR3;
|
---|
3527 | const char *pszFile = NULL;
|
---|
3528 | const char *pszFunction = NULL;
|
---|
3529 | uint32_t iLine = 0;
|
---|
3530 | const char *pszMessage;
|
---|
3531 | int32_t rc = VERR_MM_HYPER_NO_MEMORY;
|
---|
3532 | if (pErr)
|
---|
3533 | {
|
---|
3534 | AssertCompile(sizeof(const char) == sizeof(uint8_t));
|
---|
3535 | if (pErr->offFile)
|
---|
3536 | pszFile = (const char *)pErr + pErr->offFile;
|
---|
3537 | iLine = pErr->iLine;
|
---|
3538 | if (pErr->offFunction)
|
---|
3539 | pszFunction = (const char *)pErr + pErr->offFunction;
|
---|
3540 | if (pErr->offMessage)
|
---|
3541 | pszMessage = (const char *)pErr + pErr->offMessage;
|
---|
3542 | else
|
---|
3543 | pszMessage = "No message!";
|
---|
3544 | }
|
---|
3545 | else
|
---|
3546 | pszMessage = "No message! (Failed to allocate memory to put the error message in!)";
|
---|
3547 |
|
---|
3548 | /*
|
---|
3549 | * Call the at error callbacks.
|
---|
3550 | */
|
---|
3551 | PUVM pUVM = pVM->pUVM;
|
---|
3552 | RTCritSectEnter(&pUVM->vm.s.AtErrorCritSect);
|
---|
3553 | ASMAtomicIncU32(&pUVM->vm.s.cRuntimeErrors);
|
---|
3554 | for (PVMATERROR pCur = pUVM->vm.s.pAtError; pCur; pCur = pCur->pNext)
|
---|
3555 | vmR3SetErrorWorkerDoCall(pVM, pCur, rc, RT_SRC_POS_ARGS, "%s", pszMessage);
|
---|
3556 | RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
|
---|
3557 | }
|
---|
3558 |
|
---|
3559 |
|
---|
3560 | /**
|
---|
3561 | * Gets the number of errors raised via VMSetError.
|
---|
3562 | *
|
---|
3563 | * This can be used avoid double error messages.
|
---|
3564 | *
|
---|
3565 | * @returns The error count.
|
---|
3566 | * @param pVM The VM handle.
|
---|
3567 | */
|
---|
3568 | VMMR3DECL(uint32_t) VMR3GetErrorCount(PVM pVM)
|
---|
3569 | {
|
---|
3570 | return pVM->pUVM->vm.s.cErrors;
|
---|
3571 | }
|
---|
3572 |
|
---|
3573 |
|
---|
3574 | /**
|
---|
3575 | * Creation time wrapper for vmR3SetErrorUV.
|
---|
3576 | *
|
---|
3577 | * @returns rc.
|
---|
3578 | * @param pUVM Pointer to the user mode VM structure.
|
---|
3579 | * @param rc The VBox status code.
|
---|
3580 | * @param RT_SRC_POS_DECL The source position of this error.
|
---|
3581 | * @param pszFormat Format string.
|
---|
3582 | * @param ... The arguments.
|
---|
3583 | * @thread Any thread.
|
---|
3584 | */
|
---|
3585 | static int vmR3SetErrorU(PUVM pUVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, ...)
|
---|
3586 | {
|
---|
3587 | va_list va;
|
---|
3588 | va_start(va, pszFormat);
|
---|
3589 | vmR3SetErrorUV(pUVM, rc, pszFile, iLine, pszFunction, pszFormat, &va);
|
---|
3590 | va_end(va);
|
---|
3591 | return rc;
|
---|
3592 | }
|
---|
3593 |
|
---|
3594 |
|
---|
3595 | /**
|
---|
3596 | * Worker which calls everyone listening to the VM error messages.
|
---|
3597 | *
|
---|
3598 | * @param pUVM Pointer to the user mode VM structure.
|
---|
3599 | * @param rc The VBox status code.
|
---|
3600 | * @param RT_SRC_POS_DECL The source position of this error.
|
---|
3601 | * @param pszFormat Format string.
|
---|
3602 | * @param pArgs Pointer to the format arguments.
|
---|
3603 | * @thread EMT
|
---|
3604 | */
|
---|
3605 | DECLCALLBACK(void) vmR3SetErrorUV(PUVM pUVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list *pArgs)
|
---|
3606 | {
|
---|
3607 | /*
|
---|
3608 | * Log the error.
|
---|
3609 | */
|
---|
3610 | va_list va3;
|
---|
3611 | va_copy(va3, *pArgs);
|
---|
3612 | RTLogRelPrintf("VMSetError: %s(%d) %s\nVMSetError: %N\n", pszFile, iLine, pszFunction, pszFormat, &va3);
|
---|
3613 | va_end(va3);
|
---|
3614 |
|
---|
3615 | #ifdef LOG_ENABLED
|
---|
3616 | va_copy(va3, *pArgs);
|
---|
3617 | RTLogPrintf("VMSetError: %s(%d) %s\n%N\n", pszFile, iLine, pszFunction, pszFormat, &va3);
|
---|
3618 | va_end(va3);
|
---|
3619 | #endif
|
---|
3620 |
|
---|
3621 | /*
|
---|
3622 | * Make a copy of the message.
|
---|
3623 | */
|
---|
3624 | if (pUVM->pVM)
|
---|
3625 | vmSetErrorCopy(pUVM->pVM, rc, RT_SRC_POS_ARGS, pszFormat, *pArgs);
|
---|
3626 |
|
---|
3627 | /*
|
---|
3628 | * Call the at error callbacks.
|
---|
3629 | */
|
---|
3630 | bool fCalledSomeone = false;
|
---|
3631 | RTCritSectEnter(&pUVM->vm.s.AtErrorCritSect);
|
---|
3632 | ASMAtomicIncU32(&pUVM->vm.s.cErrors);
|
---|
3633 | for (PVMATERROR pCur = pUVM->vm.s.pAtError; pCur; pCur = pCur->pNext)
|
---|
3634 | {
|
---|
3635 | va_list va2;
|
---|
3636 | va_copy(va2, *pArgs);
|
---|
3637 | pCur->pfnAtError(pUVM->pVM, pCur->pvUser, rc, RT_SRC_POS_ARGS, pszFormat, va2);
|
---|
3638 | va_end(va2);
|
---|
3639 | fCalledSomeone = true;
|
---|
3640 | }
|
---|
3641 | RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
|
---|
3642 | }
|
---|
3643 |
|
---|
3644 |
|
---|
3645 | /**
|
---|
3646 | * Registers a VM runtime error callback.
|
---|
3647 | *
|
---|
3648 | * @returns VBox status code.
|
---|
3649 | * @param pVM The VM handle.
|
---|
3650 | * @param pfnAtRuntimeError Pointer to callback.
|
---|
3651 | * @param pvUser User argument.
|
---|
3652 | * @thread Any.
|
---|
3653 | */
|
---|
3654 | VMMR3DECL(int) VMR3AtRuntimeErrorRegister(PVM pVM, PFNVMATRUNTIMEERROR pfnAtRuntimeError, void *pvUser)
|
---|
3655 | {
|
---|
3656 | LogFlow(("VMR3AtRuntimeErrorRegister: pfnAtRuntimeError=%p pvUser=%p\n", pfnAtRuntimeError, pvUser));
|
---|
3657 |
|
---|
3658 | /*
|
---|
3659 | * Validate input.
|
---|
3660 | */
|
---|
3661 | AssertPtrReturn(pfnAtRuntimeError, VERR_INVALID_PARAMETER);
|
---|
3662 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
3663 |
|
---|
3664 | /*
|
---|
3665 | * Allocate a new record.
|
---|
3666 | */
|
---|
3667 | PUVM pUVM = pVM->pUVM;
|
---|
3668 | PVMATRUNTIMEERROR pNew = (PVMATRUNTIMEERROR)MMR3HeapAllocU(pUVM, MM_TAG_VM, sizeof(*pNew));
|
---|
3669 | if (!pNew)
|
---|
3670 | return VERR_NO_MEMORY;
|
---|
3671 |
|
---|
3672 | /* fill */
|
---|
3673 | pNew->pfnAtRuntimeError = pfnAtRuntimeError;
|
---|
3674 | pNew->pvUser = pvUser;
|
---|
3675 |
|
---|
3676 | /* insert */
|
---|
3677 | RTCritSectEnter(&pUVM->vm.s.AtErrorCritSect);
|
---|
3678 | pNew->pNext = *pUVM->vm.s.ppAtRuntimeErrorNext;
|
---|
3679 | *pUVM->vm.s.ppAtRuntimeErrorNext = pNew;
|
---|
3680 | pUVM->vm.s.ppAtRuntimeErrorNext = &pNew->pNext;
|
---|
3681 | RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
|
---|
3682 |
|
---|
3683 | return VINF_SUCCESS;
|
---|
3684 | }
|
---|
3685 |
|
---|
3686 |
|
---|
3687 | /**
|
---|
3688 | * Deregisters a VM runtime error callback.
|
---|
3689 | *
|
---|
3690 | * @returns VBox status code.
|
---|
3691 | * @param pVM The VM handle.
|
---|
3692 | * @param pfnAtRuntimeError Pointer to callback.
|
---|
3693 | * @param pvUser User argument.
|
---|
3694 | * @thread Any.
|
---|
3695 | */
|
---|
3696 | VMMR3DECL(int) VMR3AtRuntimeErrorDeregister(PVM pVM, PFNVMATRUNTIMEERROR pfnAtRuntimeError, void *pvUser)
|
---|
3697 | {
|
---|
3698 | LogFlow(("VMR3AtRuntimeErrorDeregister: pfnAtRuntimeError=%p pvUser=%p\n", pfnAtRuntimeError, pvUser));
|
---|
3699 |
|
---|
3700 | /*
|
---|
3701 | * Validate input.
|
---|
3702 | */
|
---|
3703 | AssertPtrReturn(pfnAtRuntimeError, VERR_INVALID_PARAMETER);
|
---|
3704 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
3705 |
|
---|
3706 | PUVM pUVM = pVM->pUVM;
|
---|
3707 | RTCritSectEnter(&pUVM->vm.s.AtErrorCritSect);
|
---|
3708 |
|
---|
3709 | /*
|
---|
3710 | * Search the list for the entry.
|
---|
3711 | */
|
---|
3712 | PVMATRUNTIMEERROR pPrev = NULL;
|
---|
3713 | PVMATRUNTIMEERROR pCur = pUVM->vm.s.pAtRuntimeError;
|
---|
3714 | while ( pCur
|
---|
3715 | && ( pCur->pfnAtRuntimeError != pfnAtRuntimeError
|
---|
3716 | || pCur->pvUser != pvUser))
|
---|
3717 | {
|
---|
3718 | pPrev = pCur;
|
---|
3719 | pCur = pCur->pNext;
|
---|
3720 | }
|
---|
3721 | if (!pCur)
|
---|
3722 | {
|
---|
3723 | AssertMsgFailed(("pfnAtRuntimeError=%p was not found\n", pfnAtRuntimeError));
|
---|
3724 | RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
|
---|
3725 | return VERR_FILE_NOT_FOUND;
|
---|
3726 | }
|
---|
3727 |
|
---|
3728 | /*
|
---|
3729 | * Unlink it.
|
---|
3730 | */
|
---|
3731 | if (pPrev)
|
---|
3732 | {
|
---|
3733 | pPrev->pNext = pCur->pNext;
|
---|
3734 | if (!pCur->pNext)
|
---|
3735 | pUVM->vm.s.ppAtRuntimeErrorNext = &pPrev->pNext;
|
---|
3736 | }
|
---|
3737 | else
|
---|
3738 | {
|
---|
3739 | pUVM->vm.s.pAtRuntimeError = pCur->pNext;
|
---|
3740 | if (!pCur->pNext)
|
---|
3741 | pUVM->vm.s.ppAtRuntimeErrorNext = &pUVM->vm.s.pAtRuntimeError;
|
---|
3742 | }
|
---|
3743 |
|
---|
3744 | RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
|
---|
3745 |
|
---|
3746 | /*
|
---|
3747 | * Free it.
|
---|
3748 | */
|
---|
3749 | pCur->pfnAtRuntimeError = NULL;
|
---|
3750 | pCur->pNext = NULL;
|
---|
3751 | MMR3HeapFree(pCur);
|
---|
3752 |
|
---|
3753 | return VINF_SUCCESS;
|
---|
3754 | }
|
---|
3755 |
|
---|
3756 |
|
---|
3757 | /**
|
---|
3758 | * EMT rendezvous worker that vmR3SetRuntimeErrorCommon uses to safely change
|
---|
3759 | * the state to FatalError(LS).
|
---|
3760 | *
|
---|
3761 | * @returns VERR_VM_INVALID_VM_STATE or VINF_SUCCESS. (This is a strict return
|
---|
3762 | * code, see FNVMMEMTRENDEZVOUS.)
|
---|
3763 | *
|
---|
3764 | * @param pVM The VM handle.
|
---|
3765 | * @param pVCpu The VMCPU handle of the EMT.
|
---|
3766 | * @param pvUser Ignored.
|
---|
3767 | */
|
---|
3768 | static DECLCALLBACK(VBOXSTRICTRC) vmR3SetRuntimeErrorChangeState(PVM pVM, PVMCPU pVCpu, void *pvUser)
|
---|
3769 | {
|
---|
3770 | NOREF(pVCpu);
|
---|
3771 | Assert(!pvUser); NOREF(pvUser);
|
---|
3772 |
|
---|
3773 | int rc = vmR3TrySetState(pVM, "VMSetRuntimeError", 2,
|
---|
3774 | VMSTATE_FATAL_ERROR, VMSTATE_RUNNING,
|
---|
3775 | VMSTATE_FATAL_ERROR_LS, VMSTATE_RUNNING_LS);
|
---|
3776 | if (rc == 2)
|
---|
3777 | SSMR3Cancel(pVM);
|
---|
3778 | return RT_SUCCESS(rc) ? VINF_SUCCESS : rc;
|
---|
3779 | }
|
---|
3780 |
|
---|
3781 |
|
---|
3782 | /**
|
---|
3783 | * Worker for VMR3SetRuntimeErrorWorker and vmR3SetRuntimeErrorV.
|
---|
3784 | *
|
---|
3785 | * This does the common parts after the error has been saved / retrieved.
|
---|
3786 | *
|
---|
3787 | * @returns VBox status code with modifications, see VMSetRuntimeErrorV.
|
---|
3788 | *
|
---|
3789 | * @param pVM The VM handle.
|
---|
3790 | * @param fFlags The error flags.
|
---|
3791 | * @param pszErrorId Error ID string.
|
---|
3792 | * @param pszFormat Format string.
|
---|
3793 | * @param pVa Pointer to the format arguments.
|
---|
3794 | */
|
---|
3795 | static int vmR3SetRuntimeErrorCommon(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list *pVa)
|
---|
3796 | {
|
---|
3797 | LogRel(("VM: Raising runtime error '%s' (fFlags=%#x)\n", pszErrorId, fFlags));
|
---|
3798 |
|
---|
3799 | /*
|
---|
3800 | * Take actions before the call.
|
---|
3801 | */
|
---|
3802 | int rc;
|
---|
3803 | if (fFlags & VMSETRTERR_FLAGS_FATAL)
|
---|
3804 | rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, vmR3SetRuntimeErrorChangeState, NULL);
|
---|
3805 | else if (fFlags & VMSETRTERR_FLAGS_SUSPEND)
|
---|
3806 | rc = VMR3Suspend(pVM);
|
---|
3807 | else
|
---|
3808 | rc = VINF_SUCCESS;
|
---|
3809 |
|
---|
3810 | /*
|
---|
3811 | * Do the callback round.
|
---|
3812 | */
|
---|
3813 | PUVM pUVM = pVM->pUVM;
|
---|
3814 | RTCritSectEnter(&pUVM->vm.s.AtErrorCritSect);
|
---|
3815 | ASMAtomicIncU32(&pUVM->vm.s.cRuntimeErrors);
|
---|
3816 | for (PVMATRUNTIMEERROR pCur = pUVM->vm.s.pAtRuntimeError; pCur; pCur = pCur->pNext)
|
---|
3817 | {
|
---|
3818 | va_list va;
|
---|
3819 | va_copy(va, *pVa);
|
---|
3820 | pCur->pfnAtRuntimeError(pVM, pCur->pvUser, fFlags, pszErrorId, pszFormat, va);
|
---|
3821 | va_end(va);
|
---|
3822 | }
|
---|
3823 | RTCritSectLeave(&pUVM->vm.s.AtErrorCritSect);
|
---|
3824 |
|
---|
3825 | return rc;
|
---|
3826 | }
|
---|
3827 |
|
---|
3828 |
|
---|
3829 | /**
|
---|
3830 | * Ellipsis to va_list wrapper for calling vmR3SetRuntimeErrorCommon.
|
---|
3831 | */
|
---|
3832 | static int vmR3SetRuntimeErrorCommonF(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, ...)
|
---|
3833 | {
|
---|
3834 | va_list va;
|
---|
3835 | va_start(va, pszFormat);
|
---|
3836 | int rc = vmR3SetRuntimeErrorCommon(pVM, fFlags, pszErrorId, pszFormat, &va);
|
---|
3837 | va_end(va);
|
---|
3838 | return rc;
|
---|
3839 | }
|
---|
3840 |
|
---|
3841 |
|
---|
3842 | /**
|
---|
3843 | * This is a worker function for RC and Ring-0 calls to VMSetError and
|
---|
3844 | * VMSetErrorV.
|
---|
3845 | *
|
---|
3846 | * The message is found in VMINT.
|
---|
3847 | *
|
---|
3848 | * @returns VBox status code, see VMSetRuntimeError.
|
---|
3849 | * @param pVM The VM handle.
|
---|
3850 | * @thread EMT.
|
---|
3851 | */
|
---|
3852 | VMMR3DECL(int) VMR3SetRuntimeErrorWorker(PVM pVM)
|
---|
3853 | {
|
---|
3854 | VM_ASSERT_EMT(pVM);
|
---|
3855 | AssertReleaseMsgFailed(("And we have a winner! You get to implement Ring-0 and GC VMSetRuntimeErrorV! Congrats!\n"));
|
---|
3856 |
|
---|
3857 | /*
|
---|
3858 | * Unpack the error (if we managed to format one).
|
---|
3859 | */
|
---|
3860 | const char *pszErrorId = "SetRuntimeError";
|
---|
3861 | const char *pszMessage = "No message!";
|
---|
3862 | uint32_t fFlags = VMSETRTERR_FLAGS_FATAL;
|
---|
3863 | PVMRUNTIMEERROR pErr = pVM->vm.s.pRuntimeErrorR3;
|
---|
3864 | if (pErr)
|
---|
3865 | {
|
---|
3866 | AssertCompile(sizeof(const char) == sizeof(uint8_t));
|
---|
3867 | if (pErr->offErrorId)
|
---|
3868 | pszErrorId = (const char *)pErr + pErr->offErrorId;
|
---|
3869 | if (pErr->offMessage)
|
---|
3870 | pszMessage = (const char *)pErr + pErr->offMessage;
|
---|
3871 | fFlags = pErr->fFlags;
|
---|
3872 | }
|
---|
3873 |
|
---|
3874 | /*
|
---|
3875 | * Join cause with vmR3SetRuntimeErrorV.
|
---|
3876 | */
|
---|
3877 | return vmR3SetRuntimeErrorCommonF(pVM, fFlags, pszErrorId, "%s", pszMessage);
|
---|
3878 | }
|
---|
3879 |
|
---|
3880 |
|
---|
3881 | /**
|
---|
3882 | * Worker for VMSetRuntimeErrorV for doing the job on EMT in ring-3.
|
---|
3883 | *
|
---|
3884 | * @returns VBox status code with modifications, see VMSetRuntimeErrorV.
|
---|
3885 | *
|
---|
3886 | * @param pVM The VM handle.
|
---|
3887 | * @param fFlags The error flags.
|
---|
3888 | * @param pszErrorId Error ID string.
|
---|
3889 | * @param pszMessage The error message residing the MM heap.
|
---|
3890 | *
|
---|
3891 | * @thread EMT
|
---|
3892 | */
|
---|
3893 | DECLCALLBACK(int) vmR3SetRuntimeError(PVM pVM, uint32_t fFlags, const char *pszErrorId, char *pszMessage)
|
---|
3894 | {
|
---|
3895 | #if 0 /** @todo make copy of the error msg. */
|
---|
3896 | /*
|
---|
3897 | * Make a copy of the message.
|
---|
3898 | */
|
---|
3899 | va_list va2;
|
---|
3900 | va_copy(va2, *pVa);
|
---|
3901 | vmSetRuntimeErrorCopy(pVM, fFlags, pszErrorId, pszFormat, va2);
|
---|
3902 | va_end(va2);
|
---|
3903 | #endif
|
---|
3904 |
|
---|
3905 | /*
|
---|
3906 | * Join paths with VMR3SetRuntimeErrorWorker.
|
---|
3907 | */
|
---|
3908 | int rc = vmR3SetRuntimeErrorCommonF(pVM, fFlags, pszErrorId, "%s", pszMessage);
|
---|
3909 | MMR3HeapFree(pszMessage);
|
---|
3910 | return rc;
|
---|
3911 | }
|
---|
3912 |
|
---|
3913 |
|
---|
3914 | /**
|
---|
3915 | * Worker for VMSetRuntimeErrorV for doing the job on EMT in ring-3.
|
---|
3916 | *
|
---|
3917 | * @returns VBox status code with modifications, see VMSetRuntimeErrorV.
|
---|
3918 | *
|
---|
3919 | * @param pVM The VM handle.
|
---|
3920 | * @param fFlags The error flags.
|
---|
3921 | * @param pszErrorId Error ID string.
|
---|
3922 | * @param pszFormat Format string.
|
---|
3923 | * @param pVa Pointer to the format arguments.
|
---|
3924 | *
|
---|
3925 | * @thread EMT
|
---|
3926 | */
|
---|
3927 | DECLCALLBACK(int) vmR3SetRuntimeErrorV(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list *pVa)
|
---|
3928 | {
|
---|
3929 | /*
|
---|
3930 | * Make a copy of the message.
|
---|
3931 | */
|
---|
3932 | va_list va2;
|
---|
3933 | va_copy(va2, *pVa);
|
---|
3934 | vmSetRuntimeErrorCopy(pVM, fFlags, pszErrorId, pszFormat, va2);
|
---|
3935 | va_end(va2);
|
---|
3936 |
|
---|
3937 | /*
|
---|
3938 | * Join paths with VMR3SetRuntimeErrorWorker.
|
---|
3939 | */
|
---|
3940 | return vmR3SetRuntimeErrorCommon(pVM, fFlags, pszErrorId, pszFormat, pVa);
|
---|
3941 | }
|
---|
3942 |
|
---|
3943 |
|
---|
3944 | /**
|
---|
3945 | * Gets the number of runtime errors raised via VMR3SetRuntimeError.
|
---|
3946 | *
|
---|
3947 | * This can be used avoid double error messages.
|
---|
3948 | *
|
---|
3949 | * @returns The runtime error count.
|
---|
3950 | * @param pVM The VM handle.
|
---|
3951 | */
|
---|
3952 | VMMR3DECL(uint32_t) VMR3GetRuntimeErrorCount(PVM pVM)
|
---|
3953 | {
|
---|
3954 | return pVM->pUVM->vm.s.cRuntimeErrors;
|
---|
3955 | }
|
---|
3956 |
|
---|
3957 |
|
---|
3958 | /**
|
---|
3959 | * Gets the ID virtual of the virtual CPU assoicated with the calling thread.
|
---|
3960 | *
|
---|
3961 | * @returns The CPU ID. NIL_VMCPUID if the thread isn't an EMT.
|
---|
3962 | *
|
---|
3963 | * @param pVM The VM handle.
|
---|
3964 | */
|
---|
3965 | VMMR3DECL(RTCPUID) VMR3GetVMCPUId(PVM pVM)
|
---|
3966 | {
|
---|
3967 | PUVMCPU pUVCpu = (PUVMCPU)RTTlsGet(pVM->pUVM->vm.s.idxTLS);
|
---|
3968 | return pUVCpu
|
---|
3969 | ? pUVCpu->idCpu
|
---|
3970 | : NIL_VMCPUID;
|
---|
3971 | }
|
---|
3972 |
|
---|
3973 |
|
---|
3974 | /**
|
---|
3975 | * Returns the native handle of the current EMT VMCPU thread.
|
---|
3976 | *
|
---|
3977 | * @returns Handle if this is an EMT thread; NIL_RTNATIVETHREAD otherwise
|
---|
3978 | * @param pVM The VM handle.
|
---|
3979 | * @thread EMT
|
---|
3980 | */
|
---|
3981 | VMMR3DECL(RTNATIVETHREAD) VMR3GetVMCPUNativeThread(PVM pVM)
|
---|
3982 | {
|
---|
3983 | PUVMCPU pUVCpu = (PUVMCPU)RTTlsGet(pVM->pUVM->vm.s.idxTLS);
|
---|
3984 |
|
---|
3985 | if (!pUVCpu)
|
---|
3986 | return NIL_RTNATIVETHREAD;
|
---|
3987 |
|
---|
3988 | return pUVCpu->vm.s.NativeThreadEMT;
|
---|
3989 | }
|
---|
3990 |
|
---|
3991 |
|
---|
3992 | /**
|
---|
3993 | * Returns the native handle of the current EMT VMCPU thread.
|
---|
3994 | *
|
---|
3995 | * @returns Handle if this is an EMT thread; NIL_RTNATIVETHREAD otherwise
|
---|
3996 | * @param pVM The VM handle.
|
---|
3997 | * @thread EMT
|
---|
3998 | */
|
---|
3999 | VMMR3DECL(RTNATIVETHREAD) VMR3GetVMCPUNativeThreadU(PUVM pUVM)
|
---|
4000 | {
|
---|
4001 | PUVMCPU pUVCpu = (PUVMCPU)RTTlsGet(pUVM->vm.s.idxTLS);
|
---|
4002 |
|
---|
4003 | if (!pUVCpu)
|
---|
4004 | return NIL_RTNATIVETHREAD;
|
---|
4005 |
|
---|
4006 | return pUVCpu->vm.s.NativeThreadEMT;
|
---|
4007 | }
|
---|
4008 |
|
---|
4009 |
|
---|
4010 | /**
|
---|
4011 | * Returns the handle of the current EMT VMCPU thread.
|
---|
4012 | *
|
---|
4013 | * @returns Handle if this is an EMT thread; NIL_RTNATIVETHREAD otherwise
|
---|
4014 | * @param pVM The VM handle.
|
---|
4015 | * @thread EMT
|
---|
4016 | */
|
---|
4017 | VMMR3DECL(RTTHREAD) VMR3GetVMCPUThread(PVM pVM)
|
---|
4018 | {
|
---|
4019 | PUVMCPU pUVCpu = (PUVMCPU)RTTlsGet(pVM->pUVM->vm.s.idxTLS);
|
---|
4020 |
|
---|
4021 | if (!pUVCpu)
|
---|
4022 | return NIL_RTTHREAD;
|
---|
4023 |
|
---|
4024 | return pUVCpu->vm.s.ThreadEMT;
|
---|
4025 | }
|
---|
4026 |
|
---|
4027 |
|
---|
4028 | /**
|
---|
4029 | * Returns the handle of the current EMT VMCPU thread.
|
---|
4030 | *
|
---|
4031 | * @returns Handle if this is an EMT thread; NIL_RTNATIVETHREAD otherwise
|
---|
4032 | * @param pVM The VM handle.
|
---|
4033 | * @thread EMT
|
---|
4034 | */
|
---|
4035 | VMMR3DECL(RTTHREAD) VMR3GetVMCPUThreadU(PUVM pUVM)
|
---|
4036 | {
|
---|
4037 | PUVMCPU pUVCpu = (PUVMCPU)RTTlsGet(pUVM->vm.s.idxTLS);
|
---|
4038 |
|
---|
4039 | if (!pUVCpu)
|
---|
4040 | return NIL_RTTHREAD;
|
---|
4041 |
|
---|
4042 | return pUVCpu->vm.s.ThreadEMT;
|
---|
4043 | }
|
---|
4044 |
|
---|