1 | /* $Id: VMAll.cpp 13816 2008-11-04 22:52:12Z 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 |
|
---|
33 | #include <iprt/assert.h>
|
---|
34 | #include <iprt/string.h>
|
---|
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 VM handle. Must be non-NULL.
|
---|
45 | * @param rc VBox status code.
|
---|
46 | * @param RT_SRC_POS_DECL Use RT_SRC_POS.
|
---|
47 | * @param pszFormat Error message format string.
|
---|
48 | * @param ... Error message arguments.
|
---|
49 | * @thread Any
|
---|
50 | */
|
---|
51 | VMMDECL(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 VM handle. Must be non-NULL.
|
---|
69 | * @param rc VBox status code.
|
---|
70 | * @param RT_SRC_POS_DECL Use RT_SRC_POS.
|
---|
71 | * @param pszFormat Error message format string.
|
---|
72 | * @param args Error message arguments.
|
---|
73 | * @thread Any
|
---|
74 | */
|
---|
75 | VMMDECL(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 | PVMREQ pReq;
|
---|
84 | VMR3ReqCall(pVM, VMREQDEST_ANY, &pReq, RT_INDEFINITE_WAIT, (PFNRT)vmR3SetErrorUV, 7, /* ASSUMES 3 source pos args! */
|
---|
85 | pVM->pUVM, rc, RT_SRC_POS_ARGS, pszFormat, &va2);
|
---|
86 | VMR3ReqFree(pReq);
|
---|
87 | va_end(va2);
|
---|
88 |
|
---|
89 | #else
|
---|
90 | /*
|
---|
91 | * We're already on the EMT thread and can safely create a VMERROR chunk.
|
---|
92 | */
|
---|
93 | vmSetErrorCopy(pVM, rc, RT_SRC_POS_ARGS, pszFormat, args);
|
---|
94 |
|
---|
95 | # ifdef IN_GC
|
---|
96 | VMMGCCallHost(pVM, VMMCALLHOST_VM_SET_ERROR, 0);
|
---|
97 | # elif defined(IN_RING0)
|
---|
98 | VMMR0CallHost(pVM, VMMCALLHOST_VM_SET_ERROR, 0);
|
---|
99 | # else
|
---|
100 | # endif
|
---|
101 | #endif
|
---|
102 | return rc;
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Copies the error to a VMERROR structure.
|
---|
108 | *
|
---|
109 | * This is mainly intended for Ring-0 and GC where the error must be copied to
|
---|
110 | * memory accessible from ring-3. But it's just possible that we might add
|
---|
111 | * APIs for retrieving the VMERROR copy later.
|
---|
112 | *
|
---|
113 | * @param pVM VM handle. Must be non-NULL.
|
---|
114 | * @param rc VBox status code.
|
---|
115 | * @param RT_SRC_POS_DECL Use RT_SRC_POS.
|
---|
116 | * @param pszFormat Error message format string.
|
---|
117 | * @param args Error message arguments.
|
---|
118 | * @thread EMT
|
---|
119 | */
|
---|
120 | void vmSetErrorCopy(PVM pVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list args)
|
---|
121 | {
|
---|
122 | #if 0 /// @todo implement Ring-0 and GC VMSetError
|
---|
123 | /*
|
---|
124 | * Create the untranslated message copy.
|
---|
125 | */
|
---|
126 | /* free any old message. */
|
---|
127 | MMHyperFree(pVM, MMHyperR32Ctx(pVM, pVM->vm.s.pError));
|
---|
128 | pVM->vm.s.pError = NULL;
|
---|
129 |
|
---|
130 | /* calc reasonable start size. */
|
---|
131 | size_t cchFile = pszFile ? strlen(pszFile) : 0;
|
---|
132 | size_t cchFunction = pszFunction ? strlen(pszFunction) : 0;
|
---|
133 | size_t cchFormat = strlen(pszFormat);
|
---|
134 | size_t cb = sizeof(VMERROR)
|
---|
135 | + cchFile + 1
|
---|
136 | + cchFunction + 1
|
---|
137 | + cchFormat + 32;
|
---|
138 |
|
---|
139 | /* allocate it */
|
---|
140 | void *pv;
|
---|
141 | int rc2 = MMHyperAlloc(pVM, cb, 0, MM_TAG_VM, &pv);
|
---|
142 | if (RT_SUCCESS(rc2))
|
---|
143 | {
|
---|
144 | /* initialize it. */
|
---|
145 | PVMERROR pErr = (PVMERROR)pv;
|
---|
146 | pErr->cbAllocated = cb;
|
---|
147 | pErr->iLine = iLine;
|
---|
148 | pErr->off = sizeof(VMERROR);
|
---|
149 | pErr->offFile = pErr->offFunction = 0;
|
---|
150 |
|
---|
151 | if (cchFile)
|
---|
152 | {
|
---|
153 | pErr->offFile = pErr->off;
|
---|
154 | memcpy((uint8_t *)pErr + pErr->off, pszFile, cchFile + 1);
|
---|
155 | pErr->off += cchFile + 1;
|
---|
156 | }
|
---|
157 |
|
---|
158 | if (cchFunction)
|
---|
159 | {
|
---|
160 | pErr->offFunction = pErr->off;
|
---|
161 | memcpy((uint8_t *)pErr + pErr->off, pszFunction, cchFunction + 1);
|
---|
162 | pErr->off += cchFunction + 1;
|
---|
163 | }
|
---|
164 |
|
---|
165 | pErr->offMessage = pErr->off;
|
---|
166 |
|
---|
167 | /* format the message (pErr might be reallocated) */
|
---|
168 | VMSETERRORFMTARGS Args;
|
---|
169 | Args.pVM = pVM;
|
---|
170 | Args.pErr = pErr;
|
---|
171 |
|
---|
172 | va_list va2;
|
---|
173 | va_copy(va2, args);
|
---|
174 | RTStrFormatV(vmSetErrorFmtOut, &pErr, NULL, NULL, &pszFormatTmp, args);
|
---|
175 | va_end(va2);
|
---|
176 |
|
---|
177 | /* done. */
|
---|
178 | pVM->vm.s.pErrorR3 = MMHyper2HC(pVM, (uintptr_t)pArgs.pErr);
|
---|
179 | }
|
---|
180 | #endif
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Sets the runtime error message.
|
---|
186 | * As opposed VMSetError(), this method is intended to inform the VM user about
|
---|
187 | * errors and error-like conditions that happen at an arbitrary point during VM
|
---|
188 | * execution (like "host memory low" or "out of host disk space").
|
---|
189 | *
|
---|
190 | * The @a fFatal parameter defines whether the error is fatal or not. If it is
|
---|
191 | * true, then it is expected that the caller has already paused the VM execution
|
---|
192 | * before calling this method. The VM user is supposed to power off the VM
|
---|
193 | * immediately after it has received the runtime error notification via the
|
---|
194 | * FNVMATRUNTIMEERROR callback.
|
---|
195 | *
|
---|
196 | * If @a fFatal is false, then the paused state of the VM defines the kind of
|
---|
197 | * the error. If the VM is paused before calling this method, it means that
|
---|
198 | * the VM user may try to fix the error condition (i.e. free more host memory)
|
---|
199 | * and then resume the VM execution. If the VM is not paused before calling
|
---|
200 | * this method, it means that the given error is a warning about an error
|
---|
201 | * condition that may happen soon but that doesn't directly affect the
|
---|
202 | * VM execution by the time of the call.
|
---|
203 | *
|
---|
204 | * The @a pszErrorID parameter defines an unique error identificator.
|
---|
205 | * It is used by the front-ends to show a proper message to the end user
|
---|
206 | * containig possible actions (for example, Retry/Ignore). For this reason,
|
---|
207 | * an error ID assigned once to some particular error condition should not
|
---|
208 | * change in the future. The format of this parameter is "someErrorCondition".
|
---|
209 | *
|
---|
210 | * @param pVM VM handle. Must be non-NULL.
|
---|
211 | * @param fFatal Whether it is a fatal error or not.
|
---|
212 | * @param pszErrorID Error ID string.
|
---|
213 | * @param pszFormat Error message format string.
|
---|
214 | * @param ... Error message arguments.
|
---|
215 | *
|
---|
216 | * @return VBox status code (whether the error has been successfully set
|
---|
217 | * and delivered to callbacks or not).
|
---|
218 | *
|
---|
219 | * @thread Any
|
---|
220 | * @todo r=bird: The pausing/suspending of the VM should be done here, we'll just end
|
---|
221 | * up duplicating code all over the place otherwise. In the case of
|
---|
222 | * devices/drivers/etc they might not be trusted to pause/suspend the
|
---|
223 | * vm even. Change fFatal to fFlags and define action flags and a fatal flag.
|
---|
224 | *
|
---|
225 | * Also, why a string ID and not an enum?
|
---|
226 | */
|
---|
227 | VMMDECL(int) VMSetRuntimeError(PVM pVM, bool fFatal, const char *pszErrorID,
|
---|
228 | const char *pszFormat, ...)
|
---|
229 | {
|
---|
230 | va_list args;
|
---|
231 | va_start(args, pszFormat);
|
---|
232 | int rc = VMSetRuntimeErrorV(pVM, fFatal, pszErrorID, pszFormat, args);
|
---|
233 | va_end(args);
|
---|
234 | return rc;
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * va_list version of VMSetRuntimeError.
|
---|
240 | *
|
---|
241 | * @param pVM VM handle. Must be non-NULL.
|
---|
242 | * @param fFatal Whether it is a fatal error or not.
|
---|
243 | * @param pszErrorID Error ID string.
|
---|
244 | * @param pszFormat Error message format string.
|
---|
245 | * @param args Error message arguments.
|
---|
246 | *
|
---|
247 | * @return VBox status code (whether the error has been successfully set
|
---|
248 | * and delivered to callbacks or not).
|
---|
249 | *
|
---|
250 | * @thread Any
|
---|
251 | */
|
---|
252 | VMMDECL(int) VMSetRuntimeErrorV(PVM pVM, bool fFatal, const char *pszErrorID,
|
---|
253 | const char *pszFormat, va_list args)
|
---|
254 | {
|
---|
255 | #ifdef IN_RING3
|
---|
256 | /*
|
---|
257 | * Switch to EMT.
|
---|
258 | */
|
---|
259 | va_list va2;
|
---|
260 | va_copy(va2, args); /* Have to make a copy here or GCC will break. */
|
---|
261 | PVMREQ pReq;
|
---|
262 | VMR3ReqCall(pVM, VMREQDEST_ANY, &pReq, RT_INDEFINITE_WAIT, (PFNRT)vmR3SetRuntimeErrorV, 5,
|
---|
263 | pVM, fFatal, pszErrorID, pszFormat, &va2);
|
---|
264 | VMR3ReqFree(pReq);
|
---|
265 | va_end(va2);
|
---|
266 |
|
---|
267 | #else
|
---|
268 | /*
|
---|
269 | * We're already on the EMT thread and can safely create a VMRUNTIMEERROR chunk.
|
---|
270 | */
|
---|
271 | vmSetRuntimeErrorCopy(pVM, fFatal, pszErrorID, pszFormat, args);
|
---|
272 |
|
---|
273 | # ifdef IN_GC
|
---|
274 | VMMGCCallHost(pVM, VMMCALLHOST_VM_SET_RUNTIME_ERROR, 0);
|
---|
275 | # elif defined(IN_RING0)
|
---|
276 | VMMR0CallHost(pVM, VMMCALLHOST_VM_SET_RUNTIME_ERROR, 0);
|
---|
277 | # else
|
---|
278 | # endif
|
---|
279 | #endif
|
---|
280 | return VINF_SUCCESS;
|
---|
281 | }
|
---|
282 |
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * Copies the error to a VMRUNTIMEERROR structure.
|
---|
286 | *
|
---|
287 | * This is mainly intended for Ring-0 and GC where the error must be copied to
|
---|
288 | * memory accessible from ring-3. But it's just possible that we might add
|
---|
289 | * APIs for retrieving the VMRUNTIMEERROR copy later.
|
---|
290 | *
|
---|
291 | * @param pVM VM handle. Must be non-NULL.
|
---|
292 | * @param fFatal Whether it is a fatal error or not.
|
---|
293 | * @param pszErrorID Error ID string.
|
---|
294 | * @param pszFormat Error message format string.
|
---|
295 | * @param args Error message arguments.
|
---|
296 | * @thread EMT
|
---|
297 | */
|
---|
298 | void vmSetRuntimeErrorCopy(PVM pVM, bool fFatal, const char *pszErrorID,
|
---|
299 | const char *pszFormat, va_list args)
|
---|
300 | {
|
---|
301 | #if 0 /// @todo implement Ring-0 and GC VMSetError
|
---|
302 | /*
|
---|
303 | * Create the untranslated message copy.
|
---|
304 | */
|
---|
305 | /* free any old message. */
|
---|
306 | MMHyperFree(pVM, MMHyperR32Ctx(pVM, pVM->vm.s.pRuntimeErrorR3));
|
---|
307 | pVM->vm.s.pRuntimeErrorR3 = NULL;
|
---|
308 |
|
---|
309 | /* calc reasonable start size. */
|
---|
310 | size_t cchErrorID = pszErrorID ? strlen(pszErrorID) : 0;
|
---|
311 | size_t cchFormat = strlen(pszFormat);
|
---|
312 | size_t cb = sizeof(VMRUNTIMEERROR)
|
---|
313 | + cchErrorID + 1
|
---|
314 | + cchFormat + 32;
|
---|
315 |
|
---|
316 | /* allocate it */
|
---|
317 | void *pv;
|
---|
318 | int rc2 = MMHyperAlloc(pVM, cb, 0, MM_TAG_VM, &pv);
|
---|
319 | if (RT_SUCCESS(rc2))
|
---|
320 | {
|
---|
321 | /* initialize it. */
|
---|
322 | PVMRUNTIMEERROR pErr = (PVMRUNTIMEERROR)pv;
|
---|
323 | pErr->cbAllocated = cb;
|
---|
324 | pErr->off = sizeof(PVMRUNTIMEERROR);
|
---|
325 | pErr->offErrorID = = 0;
|
---|
326 |
|
---|
327 | if (cchErrorID)
|
---|
328 | {
|
---|
329 | pErr->offErrorID = pErr->off;
|
---|
330 | memcpy((uint8_t *)pErr + pErr->off, pszErrorID, cchErrorID + 1);
|
---|
331 | pErr->off += cchErrorID + 1;
|
---|
332 | }
|
---|
333 |
|
---|
334 | pErr->offMessage = pErr->off;
|
---|
335 |
|
---|
336 | /* format the message (pErr might be reallocated) */
|
---|
337 | VMSETRUNTIMEERRORFMTARGS Args;
|
---|
338 | Args.pVM = pVM;
|
---|
339 | Args.pErr = pErr;
|
---|
340 |
|
---|
341 | va_list va2;
|
---|
342 | va_copy(va2, args);
|
---|
343 | RTStrFormatV(vmSetRuntimeErrorFmtOut, &pErr, NULL, NULL, &pszFormatTmp, args);
|
---|
344 | va_end(va2);
|
---|
345 |
|
---|
346 | /* done. */
|
---|
347 | pVM->vm.s.pErrorRuntimeR3 = MMHyper2HC(pVM, (uintptr_t)pArgs.pErr);
|
---|
348 | }
|
---|
349 | #endif
|
---|
350 | }
|
---|
351 |
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Gets the name of VM state.
|
---|
355 | *
|
---|
356 | * @returns Pointer to a read-only string with the state name.
|
---|
357 | * @param enmState The state.
|
---|
358 | */
|
---|
359 | VMMDECL(const char *) VMGetStateName(VMSTATE enmState)
|
---|
360 | {
|
---|
361 | switch (enmState)
|
---|
362 | {
|
---|
363 | #define MY_CASE(enm) case VMSTATE_##enm: return #enm;
|
---|
364 | MY_CASE(CREATING);
|
---|
365 | MY_CASE(CREATED);
|
---|
366 | MY_CASE(RUNNING);
|
---|
367 | MY_CASE(LOADING);
|
---|
368 | MY_CASE(LOAD_FAILURE);
|
---|
369 | MY_CASE(SAVING);
|
---|
370 | MY_CASE(SUSPENDED);
|
---|
371 | MY_CASE(RESETTING);
|
---|
372 | MY_CASE(GURU_MEDITATION);
|
---|
373 | MY_CASE(OFF);
|
---|
374 | MY_CASE(DESTROYING);
|
---|
375 | MY_CASE(TERMINATED);
|
---|
376 | #undef MY_CASE
|
---|
377 | default:
|
---|
378 | return "Unknown";
|
---|
379 | }
|
---|
380 | }
|
---|
381 |
|
---|