VirtualBox

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

Last change on this file since 80268 was 80268, checked in by vboxsync, 6 years ago

VMM: Refactoring VMMAll/* to use VMCC & VMMCPUCC. bugref:9217

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette