1 | /* $Id: VMAll.cpp 80333 2019-08-16 20:28:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VM - Virtual Machine All Contexts.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2019 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 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_VM
|
---|
23 | #include "VMInternal.h"
|
---|
24 | #include <VBox/vmm/vmm.h>
|
---|
25 | #include <VBox/vmm/mm.h>
|
---|
26 | #include <VBox/vmm/vmcc.h>
|
---|
27 | #include <VBox/err.h>
|
---|
28 | #include <VBox/log.h>
|
---|
29 |
|
---|
30 | #include <iprt/assert.h>
|
---|
31 | #include <iprt/string.h>
|
---|
32 | #include <iprt/thread.h>
|
---|
33 |
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Sets the error message.
|
---|
37 | *
|
---|
38 | * @returns rc. Meaning you can do:
|
---|
39 | * @code
|
---|
40 | * return VM_SET_ERROR(pVM, VERR_OF_YOUR_CHOICE, "descriptive message");
|
---|
41 | * @endcode
|
---|
42 | * @param pVM The cross context VM structure.
|
---|
43 | * @param rc VBox status code.
|
---|
44 | * @param SRC_POS Use RT_SRC_POS.
|
---|
45 | * @param pszFormat Error message format string.
|
---|
46 | * @param ... Error message arguments.
|
---|
47 | * @thread Any
|
---|
48 | */
|
---|
49 | VMMDECL(int) VMSetError(PVMCC pVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, ...)
|
---|
50 | {
|
---|
51 | va_list args;
|
---|
52 | va_start(args, pszFormat);
|
---|
53 | int rc2 = VMSetErrorV(pVM, rc, RT_SRC_POS_ARGS, pszFormat, args); Assert(rc == rc2); NOREF(rc2);
|
---|
54 | va_end(args);
|
---|
55 | return rc;
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Sets the error message.
|
---|
61 | *
|
---|
62 | * @returns rc. Meaning you can do:
|
---|
63 | * @code
|
---|
64 | * return VM_SET_ERROR(pVM, VERR_OF_YOUR_CHOICE, "descriptive message");
|
---|
65 | * @endcode
|
---|
66 | * @param pVM The cross context VM structure.
|
---|
67 | * @param rc VBox status code.
|
---|
68 | * @param SRC_POS Use RT_SRC_POS.
|
---|
69 | * @param pszFormat Error message format string.
|
---|
70 | * @param args Error message arguments.
|
---|
71 | * @thread Any
|
---|
72 | */
|
---|
73 | VMMDECL(int) VMSetErrorV(PVMCC pVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list args)
|
---|
74 | {
|
---|
75 | #ifdef IN_RING3
|
---|
76 | /*
|
---|
77 | * Switch to EMT.
|
---|
78 | */
|
---|
79 | va_list va2;
|
---|
80 | va_copy(va2, args); /* Have to make a copy here or GCC will break. */
|
---|
81 | VMR3ReqPriorityCallWait(pVM, VMCPUID_ANY, (PFNRT)vmR3SetErrorUV, 7, /* ASSUMES 3 source pos args! */
|
---|
82 | pVM->pUVM, rc, RT_SRC_POS_ARGS, pszFormat, &va2);
|
---|
83 | va_end(va2);
|
---|
84 |
|
---|
85 | #else
|
---|
86 | /*
|
---|
87 | * We're already on the EMT thread and can safely create a VMERROR chunk.
|
---|
88 | */
|
---|
89 | vmSetErrorCopy(pVM, rc, RT_SRC_POS_ARGS, pszFormat, args);
|
---|
90 | VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_VM_SET_ERROR, 0);
|
---|
91 | #endif
|
---|
92 | return rc;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Copies the error to a VMERROR structure.
|
---|
98 | *
|
---|
99 | * This is mainly intended for Ring-0 and GC where the error must be copied to
|
---|
100 | * memory accessible from ring-3. But it's just possible that we might add
|
---|
101 | * APIs for retrieving the VMERROR copy later.
|
---|
102 | *
|
---|
103 | * @param pVM The cross context VM structure.
|
---|
104 | * @param rc VBox status code.
|
---|
105 | * @param SRC_POS Use RT_SRC_POS.
|
---|
106 | * @param pszFormat Error message format string.
|
---|
107 | * @param args Error message arguments.
|
---|
108 | * @thread EMT
|
---|
109 | */
|
---|
110 | void vmSetErrorCopy(PVM pVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list args)
|
---|
111 | {
|
---|
112 | NOREF(pVM); NOREF(rc); RT_SRC_POS_NOREF(); NOREF(pszFormat); NOREF(args);
|
---|
113 | #if 0 /// @todo implement Ring-0 and GC VMSetError
|
---|
114 | /*
|
---|
115 | * Create the untranslated message copy.
|
---|
116 | */
|
---|
117 | /* free any old message. */
|
---|
118 | MMHyperFree(pVM, MMHyperR32Ctx(pVM, pVM->vm.s.pError));
|
---|
119 | pVM->vm.s.pError = NULL;
|
---|
120 |
|
---|
121 | /* calc reasonable start size. */
|
---|
122 | size_t cchFile = pszFile ? strlen(pszFile) : 0;
|
---|
123 | size_t cchFunction = pszFunction ? strlen(pszFunction) : 0;
|
---|
124 | size_t cchFormat = strlen(pszFormat);
|
---|
125 | size_t cb = sizeof(VMERROR)
|
---|
126 | + cchFile + 1
|
---|
127 | + cchFunction + 1
|
---|
128 | + cchFormat + 32;
|
---|
129 |
|
---|
130 | /* allocate it */
|
---|
131 | void *pv;
|
---|
132 | int rc2 = MMHyperAlloc(pVM, cb, 0, MM_TAG_VM, &pv);
|
---|
133 | if (RT_SUCCESS(rc2))
|
---|
134 | {
|
---|
135 | /* initialize it. */
|
---|
136 | PVMERROR pErr = (PVMERROR)pv;
|
---|
137 | pErr->cbAllocated = cb;
|
---|
138 | pErr->iLine = iLine;
|
---|
139 | pErr->off = sizeof(VMERROR);
|
---|
140 | pErr->offFile = pErr->offFunction = 0;
|
---|
141 |
|
---|
142 | if (cchFile)
|
---|
143 | {
|
---|
144 | pErr->offFile = pErr->off;
|
---|
145 | memcpy((uint8_t *)pErr + pErr->off, pszFile, cchFile + 1);
|
---|
146 | pErr->off += cchFile + 1;
|
---|
147 | }
|
---|
148 |
|
---|
149 | if (cchFunction)
|
---|
150 | {
|
---|
151 | pErr->offFunction = pErr->off;
|
---|
152 | memcpy((uint8_t *)pErr + pErr->off, pszFunction, cchFunction + 1);
|
---|
153 | pErr->off += cchFunction + 1;
|
---|
154 | }
|
---|
155 |
|
---|
156 | pErr->offMessage = pErr->off;
|
---|
157 |
|
---|
158 | /* format the message (pErr might be reallocated) */
|
---|
159 | VMSETERRORFMTARGS Args;
|
---|
160 | Args.pVM = pVM;
|
---|
161 | Args.pErr = pErr;
|
---|
162 |
|
---|
163 | va_list va2;
|
---|
164 | va_copy(va2, args);
|
---|
165 | RTStrFormatV(vmSetErrorFmtOut, &pErr, NULL, NULL, &pszFormatTmp, args);
|
---|
166 | va_end(va2);
|
---|
167 |
|
---|
168 | /* done. */
|
---|
169 | pVM->vm.s.pErrorR3 = MMHyper2HC(pVM, (uintptr_t)pArgs.pErr);
|
---|
170 | }
|
---|
171 | #endif
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Sets the runtime error message.
|
---|
177 | *
|
---|
178 | * As opposed VMSetError(), this method is intended to inform the VM user about
|
---|
179 | * errors and error-like conditions that happen at an arbitrary point during VM
|
---|
180 | * execution (like "host memory low" or "out of host disk space").
|
---|
181 | *
|
---|
182 | * @returns VBox status code. For some flags the status code <b>must</b> be
|
---|
183 | * propagated up the stack.
|
---|
184 | *
|
---|
185 | * @param pVM The cross context VM structure.
|
---|
186 | *
|
---|
187 | * @param fFlags Flags indicating which actions to take.
|
---|
188 | * See VMSETRTERR_FLAGS_* for details on each flag.
|
---|
189 | *
|
---|
190 | * @param pszErrorId Unique error identification string. This is used by
|
---|
191 | * the frontends and maybe other devices or drivers, so
|
---|
192 | * once an ID has been selected it's essentially
|
---|
193 | * unchangable. Employ camelcase when constructing the
|
---|
194 | * string, leave out spaces.
|
---|
195 | *
|
---|
196 | * The registered runtime error callbacks should string
|
---|
197 | * switch on this and handle the ones it knows
|
---|
198 | * specifically and the unknown ones generically.
|
---|
199 | *
|
---|
200 | * @param pszFormat Error message format string.
|
---|
201 | * @param ... Error message arguments.
|
---|
202 | *
|
---|
203 | * @thread Any
|
---|
204 | */
|
---|
205 | VMMDECL(int) VMSetRuntimeError(PVMCC pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, ...)
|
---|
206 | {
|
---|
207 | va_list va;
|
---|
208 | va_start(va, pszFormat);
|
---|
209 | int rc = VMSetRuntimeErrorV(pVM, fFlags, pszErrorId, pszFormat, va);
|
---|
210 | va_end(va);
|
---|
211 | return rc;
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * va_list version of VMSetRuntimeError.
|
---|
217 | *
|
---|
218 | * @returns VBox status code. For some flags the status code <b>must</b> be
|
---|
219 | * propagated up the stack.
|
---|
220 | *
|
---|
221 | * @param pVM The cross context VM structure.
|
---|
222 | * @param fFlags Flags indicating which actions to take. See
|
---|
223 | * VMSETRTERR_FLAGS_*.
|
---|
224 | * @param pszErrorId Error ID string.
|
---|
225 | * @param pszFormat Error message format string.
|
---|
226 | * @param va Error message arguments.
|
---|
227 | *
|
---|
228 | * @thread Any
|
---|
229 | */
|
---|
230 | VMMDECL(int) VMSetRuntimeErrorV(PVMCC pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list va)
|
---|
231 | {
|
---|
232 | Log(("VMSetRuntimeErrorV: fFlags=%#x pszErrorId=%s\n", fFlags, pszErrorId));
|
---|
233 |
|
---|
234 | /*
|
---|
235 | * Relaxed parameter validation.
|
---|
236 | */
|
---|
237 | AssertPtr(pVM);
|
---|
238 | AssertMsg(!(fFlags & ~(VMSETRTERR_FLAGS_NO_WAIT | VMSETRTERR_FLAGS_SUSPEND | VMSETRTERR_FLAGS_FATAL)), ("%#x\n", fFlags));
|
---|
239 | Assert(!(fFlags & VMSETRTERR_FLAGS_NO_WAIT) || !VM_IS_EMT(pVM));
|
---|
240 | Assert(!(fFlags & VMSETRTERR_FLAGS_SUSPEND) || !(fFlags & VMSETRTERR_FLAGS_FATAL));
|
---|
241 | AssertPtr(pszErrorId);
|
---|
242 | Assert(*pszErrorId);
|
---|
243 | Assert(RTStrEnd(pszErrorId, 128) != NULL);
|
---|
244 | AssertPtr(pszFormat);
|
---|
245 | Assert(RTStrEnd(pszFormat, 512) != NULL);
|
---|
246 |
|
---|
247 | #ifdef IN_RING3
|
---|
248 | /*
|
---|
249 | * Switch to EMT.
|
---|
250 | *
|
---|
251 | * If it's a no-wait request, we have to format the message into a buffer
|
---|
252 | * here since the variable arguments list will become invalid once we call
|
---|
253 | * va_end and return.
|
---|
254 | */
|
---|
255 | int rc;
|
---|
256 | if ( !(fFlags & VMSETRTERR_FLAGS_NO_WAIT)
|
---|
257 | || VM_IS_EMT(pVM))
|
---|
258 | {
|
---|
259 | fFlags &= ~VMSETRTERR_FLAGS_NO_WAIT;
|
---|
260 |
|
---|
261 | va_list va2;
|
---|
262 | va_copy(va2, va); /* Have to make a copy here or GCC will break. */
|
---|
263 | rc = VMR3ReqPriorityCallWait(pVM, VMCPUID_ANY,
|
---|
264 | (PFNRT)vmR3SetRuntimeErrorV, 5, pVM, fFlags, pszErrorId, pszFormat, &va2);
|
---|
265 | va_end(va2);
|
---|
266 | }
|
---|
267 | else
|
---|
268 | {
|
---|
269 | char *pszMessage = MMR3HeapAPrintfV(pVM, MM_TAG_VM, pszFormat, va);
|
---|
270 | rc = VMR3ReqCallNoWait(pVM, VMCPUID_ANY,
|
---|
271 | (PFNRT)vmR3SetRuntimeError, 4, pVM, fFlags, pszErrorId, pszMessage);
|
---|
272 | if (RT_FAILURE(rc))
|
---|
273 | MMR3HeapFree(pszMessage);
|
---|
274 | }
|
---|
275 |
|
---|
276 | #else
|
---|
277 | /*
|
---|
278 | * We're already on the EMT and can safely create a VMRUNTIMEERROR chunk.
|
---|
279 | */
|
---|
280 | AssertReleaseMsgFailed(("Congratulations! You will have the pleasure of debugging the RC/R0 path.\n"));
|
---|
281 | vmSetRuntimeErrorCopy(pVM, fFlags, pszErrorId, pszFormat, va);
|
---|
282 |
|
---|
283 | int rc = VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_VM_SET_RUNTIME_ERROR, 0);
|
---|
284 | #endif
|
---|
285 |
|
---|
286 | Log(("VMSetRuntimeErrorV: returns %Rrc (pszErrorId=%s)\n", rc, pszErrorId));
|
---|
287 | return rc;
|
---|
288 | }
|
---|
289 |
|
---|
290 |
|
---|
291 | /**
|
---|
292 | * Copies the error to a VMRUNTIMEERROR structure.
|
---|
293 | *
|
---|
294 | * This is mainly intended for Ring-0 and RC where the error must be copied to
|
---|
295 | * memory accessible from ring-3. But it's just possible that we might add
|
---|
296 | * APIs for retrieving the VMRUNTIMEERROR copy later.
|
---|
297 | *
|
---|
298 | * @param pVM The cross context VM structure.
|
---|
299 | * @param fFlags The error flags.
|
---|
300 | * @param pszErrorId Error ID string.
|
---|
301 | * @param pszFormat Error message format string.
|
---|
302 | * @param va Error message arguments. This is of course spoiled
|
---|
303 | * by this call.
|
---|
304 | * @thread EMT
|
---|
305 | */
|
---|
306 | void vmSetRuntimeErrorCopy(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list va)
|
---|
307 | {
|
---|
308 | NOREF(pVM); NOREF(fFlags); NOREF(pszErrorId); NOREF(pszFormat); NOREF(va);
|
---|
309 | #if 0 /// @todo implement Ring-0 and GC VMSetError
|
---|
310 | /*
|
---|
311 | * Create the untranslated message copy.
|
---|
312 | */
|
---|
313 | /* free any old message. */
|
---|
314 | MMHyperFree(pVM, MMHyperR32Ctx(pVM, pVM->vm.s.pRuntimeErrorR3));
|
---|
315 | pVM->vm.s.pRuntimeErrorR3 = NULL;
|
---|
316 |
|
---|
317 | /* calc reasonable start size. */
|
---|
318 | size_t cchErrorID = pszErrorId ? strlen(pszErrorId) : 0;
|
---|
319 | size_t cchFormat = strlen(pszFormat);
|
---|
320 | size_t cb = sizeof(VMRUNTIMEERROR)
|
---|
321 | + cchErrorID + 1
|
---|
322 | + cchFormat + 32;
|
---|
323 |
|
---|
324 | /* allocate it */
|
---|
325 | void *pv;
|
---|
326 | int rc2 = MMHyperAlloc(pVM, cb, 0, MM_TAG_VM, &pv);
|
---|
327 | if (RT_SUCCESS(rc2))
|
---|
328 | {
|
---|
329 | /* initialize it. */
|
---|
330 | PVMRUNTIMEERROR pErr = (PVMRUNTIMEERROR)pv;
|
---|
331 | pErr->cbAllocated = cb;
|
---|
332 | pErr->fFlags = fFlags;
|
---|
333 | pErr->off = sizeof(PVMRUNTIMEERROR);
|
---|
334 | pErr->offErrorID = 0;
|
---|
335 |
|
---|
336 | if (cchErrorID)
|
---|
337 | {
|
---|
338 | pErr->offErrorID = pErr->off;
|
---|
339 | memcpy((uint8_t *)pErr + pErr->off, pszErrorId, cchErrorID + 1);
|
---|
340 | pErr->off += cchErrorID + 1;
|
---|
341 | }
|
---|
342 |
|
---|
343 | pErr->offMessage = pErr->off;
|
---|
344 |
|
---|
345 | /* format the message (pErr might be reallocated) */
|
---|
346 | VMSETRUNTIMEERRORFMTARGS Args;
|
---|
347 | Args.pVM = pVM;
|
---|
348 | Args.pErr = pErr;
|
---|
349 |
|
---|
350 | va_list va2;
|
---|
351 | va_copy(va2, args);
|
---|
352 | RTStrFormatV(vmSetRuntimeErrorFmtOut, &pErr, NULL, NULL, &pszFormatTmp, args);
|
---|
353 | va_end(va2);
|
---|
354 |
|
---|
355 | /* done. */
|
---|
356 | pVM->vm.s.pErrorRuntimeR3 = MMHyper2HC(pVM, (uintptr_t)pArgs.pErr);
|
---|
357 | }
|
---|
358 | #endif
|
---|
359 | }
|
---|
360 |
|
---|
361 |
|
---|
362 | /**
|
---|
363 | * Gets the name of VM state.
|
---|
364 | *
|
---|
365 | * @returns Pointer to a read-only string with the state name.
|
---|
366 | * @param enmState The state.
|
---|
367 | */
|
---|
368 | VMMDECL(const char *) VMGetStateName(VMSTATE enmState)
|
---|
369 | {
|
---|
370 | switch (enmState)
|
---|
371 | {
|
---|
372 | #define MY_CASE(enm) case VMSTATE_##enm: return #enm;
|
---|
373 | MY_CASE(CREATING);
|
---|
374 | MY_CASE(CREATED);
|
---|
375 | MY_CASE(RUNNING);
|
---|
376 | MY_CASE(LOADING);
|
---|
377 | MY_CASE(LOAD_FAILURE);
|
---|
378 | MY_CASE(SAVING);
|
---|
379 | MY_CASE(SUSPENDED);
|
---|
380 | MY_CASE(RESETTING);
|
---|
381 | MY_CASE(GURU_MEDITATION);
|
---|
382 | MY_CASE(OFF);
|
---|
383 | MY_CASE(DESTROYING);
|
---|
384 | MY_CASE(TERMINATED);
|
---|
385 | #undef MY_CASE
|
---|
386 | default:
|
---|
387 | return "Unknown";
|
---|
388 | }
|
---|
389 | }
|
---|
390 |
|
---|
391 |
|
---|
392 | /**
|
---|
393 | * Gets the total reset count.
|
---|
394 | *
|
---|
395 | * @returns Reset count. UINT32_MAX if @a pVM is invalid.
|
---|
396 | * @param pVM The VM handle.
|
---|
397 | */
|
---|
398 | VMMDECL(uint32_t) VMGetResetCount(PVMCC pVM)
|
---|
399 | {
|
---|
400 | VM_ASSERT_VALID_EXT_RETURN(pVM, UINT32_MAX);
|
---|
401 | return pVM->vm.s.cResets;
|
---|
402 | }
|
---|
403 |
|
---|
404 |
|
---|
405 | /**
|
---|
406 | * Gets the soft reset count.
|
---|
407 | *
|
---|
408 | * @returns Soft reset count. UINT32_MAX if @a pVM is invalid.
|
---|
409 | * @param pVM The VM handle.
|
---|
410 | */
|
---|
411 | VMMDECL(uint32_t) VMGetSoftResetCount(PVMCC pVM)
|
---|
412 | {
|
---|
413 | VM_ASSERT_VALID_EXT_RETURN(pVM, UINT32_MAX);
|
---|
414 | return pVM->vm.s.cSoftResets;
|
---|
415 | }
|
---|
416 |
|
---|
417 |
|
---|
418 | /**
|
---|
419 | * Gets the hard reset count.
|
---|
420 | *
|
---|
421 | * @returns Hard reset count. UINT32_MAX if @a pVM is invalid.
|
---|
422 | * @param pVM The VM handle.
|
---|
423 | */
|
---|
424 | VMMDECL(uint32_t) VMGetHardResetCount(PVMCC pVM)
|
---|
425 | {
|
---|
426 | VM_ASSERT_VALID_EXT_RETURN(pVM, UINT32_MAX);
|
---|
427 | return pVM->vm.s.cHardResets;
|
---|
428 | }
|
---|
429 |
|
---|