VirtualBox

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

Last change on this file since 66250 was 62478, checked in by vboxsync, 8 years ago

(C) 2016

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

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