VirtualBox

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

Last change on this file since 23011 was 23011, checked in by vboxsync, 15 years ago

VMM,VMMDev: Some VMMR3ReqCall refactoring.

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