1 | /* $Id: VMAll.cpp 4071 2007-08-07 17:07:59Z 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 | */
|
---|
47 | VMDECL(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 | */
|
---|
71 | VMDECL(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 | */
|
---|
113 | void 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 | */
|
---|
220 | VMDECL(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 | */
|
---|
245 | VMDECL(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 | PVMREQ pReq;
|
---|
253 | VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT, (PFNRT)vmR3SetRuntimeErrorV, 5,
|
---|
254 | pVM, fFatal, pszErrorID, pszFormat, &args);
|
---|
255 | VMR3ReqFree(pReq);
|
---|
256 |
|
---|
257 | #else
|
---|
258 | /*
|
---|
259 | * We're already on the EMT thread and can safely create a VMRUNTIMEERROR chunk.
|
---|
260 | */
|
---|
261 | vmSetRuntimeErrorCopy(pVM, fFatal, pszErrorID, pszFormat, args);
|
---|
262 |
|
---|
263 | # ifdef IN_GC
|
---|
264 | VMMGCCallHost(pVM, VMMCALLHOST_VM_SET_RUNTIME_ERROR, 0);
|
---|
265 | # elif defined(IN_RING0)
|
---|
266 | VMMR0CallHost(pVM, VMMCALLHOST_VM_SET_RUNTIME_ERROR, 0);
|
---|
267 | # else
|
---|
268 | # endif
|
---|
269 | #endif
|
---|
270 | return VINF_SUCCESS;
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Copies the error to a VMRUNTIMEERROR structure.
|
---|
276 | *
|
---|
277 | * This is mainly intended for Ring-0 and GC where the error must be copied to
|
---|
278 | * memory accessible from ring-3. But it's just possible that we might add
|
---|
279 | * APIs for retrieving the VMRUNTIMEERROR copy later.
|
---|
280 | *
|
---|
281 | * @param pVM VM handle. Must be non-NULL.
|
---|
282 | * @param fFatal Whether it is a fatal error or not.
|
---|
283 | * @param pszErrorID Error ID string.
|
---|
284 | * @param pszFormat Error message format string.
|
---|
285 | * @param args Error message arguments.
|
---|
286 | * @thread EMT
|
---|
287 | */
|
---|
288 | void vmSetRuntimeErrorCopy(PVM pVM, bool fFatal, const char *pszErrorID,
|
---|
289 | const char *pszFormat, va_list args)
|
---|
290 | {
|
---|
291 | #if 0 /// @todo implement Ring-0 and GC VMSetError
|
---|
292 | /*
|
---|
293 | * Create the untranslated message copy.
|
---|
294 | */
|
---|
295 | /* free any old message. */
|
---|
296 | MMHyperFree(pVM, MMHyperR32Ctx(pVM, pVM->vm.s.pRuntimeErrorR3));
|
---|
297 | pVM->vm.s.pRuntimeErrorR3 = NULL;
|
---|
298 |
|
---|
299 | /* calc reasonable start size. */
|
---|
300 | size_t cchErrorID = pszErrorID ? strlen(pszErrorID) : 0;
|
---|
301 | size_t cchFormat = strlen(pszFormat);
|
---|
302 | size_t cb = sizeof(VMRUNTIMEERROR)
|
---|
303 | + cchErrorID + 1
|
---|
304 | + cchFormat + 32;
|
---|
305 |
|
---|
306 | /* allocate it */
|
---|
307 | void *pv;
|
---|
308 | int rc2 = MMHyperAlloc(pVM, cb, 0, MM_TAG_VM, &pv);
|
---|
309 | if (VBOX_SUCCESS(rc2))
|
---|
310 | {
|
---|
311 | /* initialize it. */
|
---|
312 | PVMRUNTIMEERROR pErr = (PVMRUNTIMEERROR)pv;
|
---|
313 | pErr->cbAllocated = cb;
|
---|
314 | pErr->off = sizeof(PVMRUNTIMEERROR);
|
---|
315 | pErr->offErrorID = = 0;
|
---|
316 |
|
---|
317 | if (cchErrorID)
|
---|
318 | {
|
---|
319 | pErr->offErrorID = pErr->off;
|
---|
320 | memcpy((uint8_t *)pErr + pErr->off, pszErrorID, cchErrorID + 1);
|
---|
321 | pErr->off += cchErrorID + 1;
|
---|
322 | }
|
---|
323 |
|
---|
324 | pErr->offMessage = pErr->off;
|
---|
325 |
|
---|
326 | /* format the message (pErr might be reallocated) */
|
---|
327 | VMSETRUNTIMEERRORFMTARGS Args;
|
---|
328 | Args.pVM = pVM;
|
---|
329 | Args.pErr = pErr;
|
---|
330 |
|
---|
331 | va_list va2;
|
---|
332 | va_copy(va2, args);
|
---|
333 | RTStrFormatV(vmSetRuntimeErrorFmtOut, &pErr, NULL, NULL, &pszFormatTmp, args);
|
---|
334 | va_end(va2);
|
---|
335 |
|
---|
336 | /* done. */
|
---|
337 | pVM->vm.s.pErrorRuntimeR3 = MMHyper2HC(pVM, (uintptr_t)pArgs.pErr);
|
---|
338 | }
|
---|
339 | #endif
|
---|
340 | }
|
---|
341 |
|
---|