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