VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/VMAll.cpp

Last change on this file was 98103, checked in by vboxsync, 17 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 13.7 KB
RevLine 
[23]1/* $Id: VMAll.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
[1]2/** @file
3 * VM - Virtual Machine All Contexts.
4 */
5
6/*
[98103]7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
[1]8 *
[96407]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
[1]26 */
27
28
[57358]29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
[1]32#define LOG_GROUP LOG_GROUP_VM
33#include "VMInternal.h"
[35346]34#include <VBox/vmm/vmm.h>
35#include <VBox/vmm/mm.h>
[80268]36#include <VBox/vmm/vmcc.h>
[1]37#include <VBox/err.h>
[18645]38#include <VBox/log.h>
[1]39
40#include <iprt/assert.h>
41#include <iprt/string.h>
[80161]42#include <iprt/thread.h>
[1]43
44
[90997]45#ifdef IN_RING3
46
[1]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");
[234]53 * @endcode
[58122]54 * @param pVM The cross context VM structure.
[1]55 * @param rc VBox status code.
[58116]56 * @param SRC_POS Use RT_SRC_POS.
[1]57 * @param pszFormat Error message format string.
58 * @param ... Error message arguments.
59 * @thread Any
60 */
[80281]61VMMDECL(int) VMSetError(PVMCC pVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, ...)
[1]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");
[234]77 * @endcode
[58122]78 * @param pVM The cross context VM structure.
[1]79 * @param rc VBox status code.
[58116]80 * @param SRC_POS Use RT_SRC_POS.
[1]81 * @param pszFormat Error message format string.
82 * @param args Error message arguments.
83 * @thread Any
84 */
[80281]85VMMDECL(int) VMSetErrorV(PVMCC pVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list args)
[1]86{
[90997]87# ifdef IN_RING3
[1]88 /*
89 * Switch to EMT.
90 */
[4521]91 va_list va2;
92 va_copy(va2, args); /* Have to make a copy here or GCC will break. */
[38838]93 VMR3ReqPriorityCallWait(pVM, VMCPUID_ANY, (PFNRT)vmR3SetErrorUV, 7, /* ASSUMES 3 source pos args! */
94 pVM->pUVM, rc, RT_SRC_POS_ARGS, pszFormat, &va2);
[4521]95 va_end(va2);
[1]96
[90997]97# else
[1]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);
[20874]102 VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_VM_SET_ERROR, 0);
[90997]103# endif
[1]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 *
[58122]115 * @param pVM The cross context VM structure.
[1]116 * @param rc VBox status code.
[58116]117 * @param SRC_POS Use RT_SRC_POS.
[1]118 * @param pszFormat Error message format string.
119 * @param args Error message arguments.
120 * @thread EMT
121 */
122void vmSetErrorCopy(PVM pVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list args)
123{
[39078]124 NOREF(pVM); NOREF(rc); RT_SRC_POS_NOREF(); NOREF(pszFormat); NOREF(args);
[90997]125# if 0 /// @todo implement Ring-0 and GC VMSetError
[1]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);
[13816]145 if (RT_SUCCESS(rc2))
[1]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 }
[90997]183# endif
[1]184}
185
[90997]186#endif /* IN_RING3 */
187#ifdef IN_RING3
[234]188
189/**
190 * Sets the runtime error message.
[18645]191 *
[234]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 *
[18649]196 * @returns VBox status code. For some flags the status code <b>must</b> be
197 * propagated up the stack.
[234]198 *
[58122]199 * @param pVM The cross context VM structure.
[234]200 *
[18645]201 * @param fFlags Flags indicating which actions to take.
202 * See VMSETRTERR_FLAGS_* for details on each flag.
[234]203 *
[33540]204 * @param pszErrorId Unique error identification string. This is used by
[18645]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 *
[234]214 * @param pszFormat Error message format string.
215 * @param ... Error message arguments.
216 *
217 * @thread Any
218 */
[80281]219VMMDECL(int) VMSetRuntimeError(PVMCC pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, ...)
[234]220{
[18645]221 va_list va;
222 va_start(va, pszFormat);
223 int rc = VMSetRuntimeErrorV(pVM, fFlags, pszErrorId, pszFormat, va);
224 va_end(va);
[234]225 return rc;
226}
227
228
229/**
230 * va_list version of VMSetRuntimeError.
231 *
[18649]232 * @returns VBox status code. For some flags the status code <b>must</b> be
233 * propagated up the stack.
[18645]234 *
[58122]235 * @param pVM The cross context VM structure.
[18645]236 * @param fFlags Flags indicating which actions to take. See
237 * VMSETRTERR_FLAGS_*.
238 * @param pszErrorId Error ID string.
[234]239 * @param pszFormat Error message format string.
[18645]240 * @param va Error message arguments.
[234]241 *
242 * @thread Any
243 */
[80281]244VMMDECL(int) VMSetRuntimeErrorV(PVMCC pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list va)
[234]245{
[18645]246 Log(("VMSetRuntimeErrorV: fFlags=%#x pszErrorId=%s\n", fFlags, pszErrorId));
247
248 /*
249 * Relaxed parameter validation.
250 */
251 AssertPtr(pVM);
[18646]252 AssertMsg(!(fFlags & ~(VMSETRTERR_FLAGS_NO_WAIT | VMSETRTERR_FLAGS_SUSPEND | VMSETRTERR_FLAGS_FATAL)), ("%#x\n", fFlags));
[18645]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);
[30320]257 Assert(RTStrEnd(pszErrorId, 128) != NULL);
[18645]258 AssertPtr(pszFormat);
[30320]259 Assert(RTStrEnd(pszFormat, 512) != NULL);
[18645]260
[90997]261# ifdef IN_RING3
[234]262 /*
263 * Switch to EMT.
[22915]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.
[234]268 */
[18645]269 int rc;
270 if ( !(fFlags & VMSETRTERR_FLAGS_NO_WAIT)
271 || VM_IS_EMT(pVM))
272 {
[22915]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. */
[38838]277 rc = VMR3ReqPriorityCallWait(pVM, VMCPUID_ANY,
278 (PFNRT)vmR3SetRuntimeErrorV, 5, pVM, fFlags, pszErrorId, pszFormat, &va2);
[22915]279 va_end(va2);
[18645]280 }
281 else
[22915]282 {
283 char *pszMessage = MMR3HeapAPrintfV(pVM, MM_TAG_VM, pszFormat, va);
[38838]284 rc = VMR3ReqCallNoWait(pVM, VMCPUID_ANY,
285 (PFNRT)vmR3SetRuntimeError, 4, pVM, fFlags, pszErrorId, pszMessage);
[22915]286 if (RT_FAILURE(rc))
287 MMR3HeapFree(pszMessage);
288 }
[234]289
[90997]290# else
[234]291 /*
[18645]292 * We're already on the EMT and can safely create a VMRUNTIMEERROR chunk.
[234]293 */
[18645]294 AssertReleaseMsgFailed(("Congratulations! You will have the pleasure of debugging the RC/R0 path.\n"));
295 vmSetRuntimeErrorCopy(pVM, fFlags, pszErrorId, pszFormat, va);
[234]296
[20874]297 int rc = VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_VM_SET_RUNTIME_ERROR, 0);
[90997]298# endif
[18645]299
300 Log(("VMSetRuntimeErrorV: returns %Rrc (pszErrorId=%s)\n", rc, pszErrorId));
301 return rc;
[234]302}
303
304
305/**
306 * Copies the error to a VMRUNTIMEERROR structure.
307 *
[18645]308 * This is mainly intended for Ring-0 and RC where the error must be copied to
[234]309 * memory accessible from ring-3. But it's just possible that we might add
310 * APIs for retrieving the VMRUNTIMEERROR copy later.
311 *
[58122]312 * @param pVM The cross context VM structure.
[18645]313 * @param fFlags The error flags.
314 * @param pszErrorId Error ID string.
[234]315 * @param pszFormat Error message format string.
[18645]316 * @param va Error message arguments. This is of course spoiled
317 * by this call.
[234]318 * @thread EMT
319 */
[18645]320void vmSetRuntimeErrorCopy(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list va)
[234]321{
[39078]322 NOREF(pVM); NOREF(fFlags); NOREF(pszErrorId); NOREF(pszFormat); NOREF(va);
[90997]323# if 0 /// @todo implement Ring-0 and GC VMSetError
[234]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. */
[18645]332 size_t cchErrorID = pszErrorId ? strlen(pszErrorId) : 0;
[234]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);
[13816]341 if (RT_SUCCESS(rc2))
[234]342 {
343 /* initialize it. */
344 PVMRUNTIMEERROR pErr = (PVMRUNTIMEERROR)pv;
345 pErr->cbAllocated = cb;
[18645]346 pErr->fFlags = fFlags;
[234]347 pErr->off = sizeof(PVMRUNTIMEERROR);
[18645]348 pErr->offErrorID = 0;
[234]349
350 if (cchErrorID)
351 {
352 pErr->offErrorID = pErr->off;
[18645]353 memcpy((uint8_t *)pErr + pErr->off, pszErrorId, cchErrorID + 1);
[234]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 }
[90997]372# endif
[234]373}
374
[90997]375#endif /* IN_RING3 */
[6835]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 */
[12989]383VMMDECL(const char *) VMGetStateName(VMSTATE enmState)
[6835]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}
[13667]405
[73351]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 */
[80281]413VMMDECL(uint32_t) VMGetResetCount(PVMCC pVM)
[73351]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 */
[80281]426VMMDECL(uint32_t) VMGetSoftResetCount(PVMCC pVM)
[73351]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 */
[80281]439VMMDECL(uint32_t) VMGetHardResetCount(PVMCC pVM)
[73351]440{
441 VM_ASSERT_VALID_EXT_RETURN(pVM, UINT32_MAX);
442 return pVM->vm.s.cHardResets;
443}
444
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use