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