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