VirtualBox

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

Last change on this file since 4520 was 4520, checked in by vboxsync, 17 years ago

Don't take the address of a va_list parameter. must va_copy it first or GCC/AMD64 won't work.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.2 KB
Line 
1/* $Id: VMAll.cpp 4520 2007-09-05 07:17:14Z vboxsync $ */
2/** @file
3 * VM - Virtual Machine All Contexts.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * 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.h>
25#include <VBox/mm.h>
26#include <VBox/vm.h>
27#include <VBox/err.h>
28
29#include <iprt/assert.h>
30#include <iprt/string.h>
31
32
33/**
34 * Sets the error message.
35 *
36 * @returns rc. Meaning you can do:
37 * @code
38 * return VM_SET_ERROR(pVM, VERR_OF_YOUR_CHOICE, "descriptive message");
39 * @endcode
40 * @param pVM VM handle. Must be non-NULL.
41 * @param rc VBox status code.
42 * @param RT_SRC_POS_DECL Use RT_SRC_POS.
43 * @param pszFormat Error message format string.
44 * @param ... Error message arguments.
45 * @thread Any
46 */
47VMDECL(int) VMSetError(PVM pVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, ...)
48{
49 va_list args;
50 va_start(args, pszFormat);
51 int rc2 = VMSetErrorV(pVM, rc, RT_SRC_POS_ARGS, pszFormat, args); Assert(rc == rc2); NOREF(rc2);
52 va_end(args);
53 return rc;
54}
55
56
57/**
58 * Sets the error message.
59 *
60 * @returns rc. Meaning you can do:
61 * @code
62 * return VM_SET_ERROR(pVM, VERR_OF_YOUR_CHOICE, "descriptive message");
63 * @endcode
64 * @param pVM VM handle. Must be non-NULL.
65 * @param rc VBox status code.
66 * @param RT_SRC_POS_DECL Use RT_SRC_POS.
67 * @param pszFormat Error message format string.
68 * @param args Error message arguments.
69 * @thread Any
70 */
71VMDECL(int) VMSetErrorV(PVM pVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list args)
72{
73#ifdef IN_RING3
74 /*
75 * Switch to EMT.
76 */
77 PVMREQ pReq;
78 VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT, (PFNRT)vmR3SetErrorV, 7, /* ASSUMES 3 source pos args! */
79 pVM, rc, RT_SRC_POS_ARGS, pszFormat, &args);
80 VMR3ReqFree(pReq);
81
82#else
83 /*
84 * We're already on the EMT thread and can safely create a VMERROR chunk.
85 */
86 vmSetErrorCopy(pVM, rc, RT_SRC_POS_ARGS, pszFormat, args);
87
88# ifdef IN_GC
89 VMMGCCallHost(pVM, VMMCALLHOST_VM_SET_ERROR, 0);
90# elif defined(IN_RING0)
91 VMMR0CallHost(pVM, VMMCALLHOST_VM_SET_ERROR, 0);
92# else
93# endif
94#endif
95 return rc;
96}
97
98
99/**
100 * Copies the error to a VMERROR structure.
101 *
102 * This is mainly intended for Ring-0 and GC where the error must be copied to
103 * memory accessible from ring-3. But it's just possible that we might add
104 * APIs for retrieving the VMERROR copy later.
105 *
106 * @param pVM VM handle. Must be non-NULL.
107 * @param rc VBox status code.
108 * @param RT_SRC_POS_DECL Use RT_SRC_POS.
109 * @param pszFormat Error message format string.
110 * @param args Error message arguments.
111 * @thread EMT
112 */
113void vmSetErrorCopy(PVM pVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list args)
114{
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 (VBOX_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 * 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 * The @a fFatal parameter defines whether the error is fatal or not. If it is
184 * true, then it is expected that the caller has already paused the VM execution
185 * before calling this method. The VM user is supposed to power off the VM
186 * immediately after it has received the runtime error notification via the
187 * FNVMATRUNTIMEERROR callback.
188 *
189 * If @a fFatal is false, then the paused state of the VM defines the kind of
190 * the error. If the VM is paused before calling this method, it means that
191 * the VM user may try to fix the error condition (i.e. free more host memory)
192 * and then resume the VM execution. If the VM is not paused before calling
193 * this method, it means that the given error is a warning about an error
194 * condition that may happen soon but that doesn't directly affect the
195 * VM execution by the time of the call.
196 *
197 * The @a pszErrorID parameter defines an unique error identificator.
198 * It is used by the front-ends to show a proper message to the end user
199 * containig possible actions (for example, Retry/Ignore). For this reason,
200 * an error ID assigned once to some particular error condition should not
201 * change in the future. The format of this parameter is "someErrorCondition".
202 *
203 * @param pVM VM handle. Must be non-NULL.
204 * @param fFatal Whether it is a fatal error or not.
205 * @param pszErrorID Error ID string.
206 * @param pszFormat Error message format string.
207 * @param ... Error message arguments.
208 *
209 * @return VBox status code (whether the error has been successfully set
210 * and delivered to callbacks or not).
211 *
212 * @thread Any
213 * @todo r=bird: The pausing/suspending of the VM should be done here, we'll just end
214 * up duplicating code all over the place otherwise. In the case of
215 * devices/drivers/etc they might not be trusted to pause/suspend the
216 * vm even. Change fFatal to fFlags and define action flags and a fatal flag.
217 *
218 * Also, why a string ID and not an enum?
219 */
220VMDECL(int) VMSetRuntimeError(PVM pVM, bool fFatal, const char *pszErrorID,
221 const char *pszFormat, ...)
222{
223 va_list args;
224 va_start(args, pszFormat);
225 int rc = VMSetRuntimeErrorV(pVM, fFatal, pszErrorID, pszFormat, args);
226 va_end(args);
227 return rc;
228}
229
230
231/**
232 * va_list version of VMSetRuntimeError.
233 *
234 * @param pVM VM handle. Must be non-NULL.
235 * @param fFatal Whether it is a fatal error or not.
236 * @param pszErrorID Error ID string.
237 * @param pszFormat Error message format string.
238 * @param args Error message arguments.
239 *
240 * @return VBox status code (whether the error has been successfully set
241 * and delivered to callbacks or not).
242 *
243 * @thread Any
244 */
245VMDECL(int) VMSetRuntimeErrorV(PVM pVM, bool fFatal, const char *pszErrorID,
246 const char *pszFormat, va_list args)
247{
248#ifdef IN_RING3
249 /*
250 * Switch to EMT.
251 */
252 va_list WorkaroundVA;
253 va_copy(WorkaroundVA, args); /* Have to make a copy here or GCC will break. */
254 PVMREQ pReq;
255 VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT, (PFNRT)vmR3SetRuntimeErrorV, 5,
256 pVM, fFatal, pszErrorID, pszFormat, &WorkaroundVA);
257 VMR3ReqFree(pReq);
258 va_end(WorkaroundVA);
259
260#else
261 /*
262 * We're already on the EMT thread and can safely create a VMRUNTIMEERROR chunk.
263 */
264 vmSetRuntimeErrorCopy(pVM, fFatal, pszErrorID, pszFormat, args);
265
266# ifdef IN_GC
267 VMMGCCallHost(pVM, VMMCALLHOST_VM_SET_RUNTIME_ERROR, 0);
268# elif defined(IN_RING0)
269 VMMR0CallHost(pVM, VMMCALLHOST_VM_SET_RUNTIME_ERROR, 0);
270# else
271# endif
272#endif
273 return VINF_SUCCESS;
274}
275
276
277/**
278 * Copies the error to a VMRUNTIMEERROR structure.
279 *
280 * This is mainly intended for Ring-0 and GC where the error must be copied to
281 * memory accessible from ring-3. But it's just possible that we might add
282 * APIs for retrieving the VMRUNTIMEERROR copy later.
283 *
284 * @param pVM VM handle. Must be non-NULL.
285 * @param fFatal Whether it is a fatal error or not.
286 * @param pszErrorID Error ID string.
287 * @param pszFormat Error message format string.
288 * @param args Error message arguments.
289 * @thread EMT
290 */
291void vmSetRuntimeErrorCopy(PVM pVM, bool fFatal, const char *pszErrorID,
292 const char *pszFormat, va_list args)
293{
294#if 0 /// @todo implement Ring-0 and GC VMSetError
295 /*
296 * Create the untranslated message copy.
297 */
298 /* free any old message. */
299 MMHyperFree(pVM, MMHyperR32Ctx(pVM, pVM->vm.s.pRuntimeErrorR3));
300 pVM->vm.s.pRuntimeErrorR3 = NULL;
301
302 /* calc reasonable start size. */
303 size_t cchErrorID = pszErrorID ? strlen(pszErrorID) : 0;
304 size_t cchFormat = strlen(pszFormat);
305 size_t cb = sizeof(VMRUNTIMEERROR)
306 + cchErrorID + 1
307 + cchFormat + 32;
308
309 /* allocate it */
310 void *pv;
311 int rc2 = MMHyperAlloc(pVM, cb, 0, MM_TAG_VM, &pv);
312 if (VBOX_SUCCESS(rc2))
313 {
314 /* initialize it. */
315 PVMRUNTIMEERROR pErr = (PVMRUNTIMEERROR)pv;
316 pErr->cbAllocated = cb;
317 pErr->off = sizeof(PVMRUNTIMEERROR);
318 pErr->offErrorID = = 0;
319
320 if (cchErrorID)
321 {
322 pErr->offErrorID = pErr->off;
323 memcpy((uint8_t *)pErr + pErr->off, pszErrorID, cchErrorID + 1);
324 pErr->off += cchErrorID + 1;
325 }
326
327 pErr->offMessage = pErr->off;
328
329 /* format the message (pErr might be reallocated) */
330 VMSETRUNTIMEERRORFMTARGS Args;
331 Args.pVM = pVM;
332 Args.pErr = pErr;
333
334 va_list va2;
335 va_copy(va2, args);
336 RTStrFormatV(vmSetRuntimeErrorFmtOut, &pErr, NULL, NULL, &pszFormatTmp, args);
337 va_end(va2);
338
339 /* done. */
340 pVM->vm.s.pErrorRuntimeR3 = MMHyper2HC(pVM, (uintptr_t)pArgs.pErr);
341 }
342#endif
343}
344
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