1 | /* $Id: VMAll.cpp 18646 2009-04-02 15:53:31Z 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 | */
|
---|
55 | VMMDECL(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 | */
|
---|
79 | VMMDECL(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, VMREQDEST_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 |
|
---|
99 | # ifdef IN_RC
|
---|
100 | VMMGCCallHost(pVM, VMMCALLHOST_VM_SET_ERROR, 0);
|
---|
101 | # elif defined(IN_RING0)
|
---|
102 | VMMR0CallHost(pVM, VMMCALLHOST_VM_SET_ERROR, 0);
|
---|
103 | # else
|
---|
104 | # endif
|
---|
105 | #endif
|
---|
106 | return rc;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Copies the error to a VMERROR structure.
|
---|
112 | *
|
---|
113 | * This is mainly intended for Ring-0 and GC where the error must be copied to
|
---|
114 | * memory accessible from ring-3. But it's just possible that we might add
|
---|
115 | * APIs for retrieving the VMERROR copy later.
|
---|
116 | *
|
---|
117 | * @param pVM VM handle. Must be non-NULL.
|
---|
118 | * @param rc VBox status code.
|
---|
119 | * @param RT_SRC_POS_DECL Use RT_SRC_POS.
|
---|
120 | * @param pszFormat Error message format string.
|
---|
121 | * @param args Error message arguments.
|
---|
122 | * @thread EMT
|
---|
123 | */
|
---|
124 | void vmSetErrorCopy(PVM pVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list args)
|
---|
125 | {
|
---|
126 | #if 0 /// @todo implement Ring-0 and GC VMSetError
|
---|
127 | /*
|
---|
128 | * Create the untranslated message copy.
|
---|
129 | */
|
---|
130 | /* free any old message. */
|
---|
131 | MMHyperFree(pVM, MMHyperR32Ctx(pVM, pVM->vm.s.pError));
|
---|
132 | pVM->vm.s.pError = NULL;
|
---|
133 |
|
---|
134 | /* calc reasonable start size. */
|
---|
135 | size_t cchFile = pszFile ? strlen(pszFile) : 0;
|
---|
136 | size_t cchFunction = pszFunction ? strlen(pszFunction) : 0;
|
---|
137 | size_t cchFormat = strlen(pszFormat);
|
---|
138 | size_t cb = sizeof(VMERROR)
|
---|
139 | + cchFile + 1
|
---|
140 | + cchFunction + 1
|
---|
141 | + cchFormat + 32;
|
---|
142 |
|
---|
143 | /* allocate it */
|
---|
144 | void *pv;
|
---|
145 | int rc2 = MMHyperAlloc(pVM, cb, 0, MM_TAG_VM, &pv);
|
---|
146 | if (RT_SUCCESS(rc2))
|
---|
147 | {
|
---|
148 | /* initialize it. */
|
---|
149 | PVMERROR pErr = (PVMERROR)pv;
|
---|
150 | pErr->cbAllocated = cb;
|
---|
151 | pErr->iLine = iLine;
|
---|
152 | pErr->off = sizeof(VMERROR);
|
---|
153 | pErr->offFile = pErr->offFunction = 0;
|
---|
154 |
|
---|
155 | if (cchFile)
|
---|
156 | {
|
---|
157 | pErr->offFile = pErr->off;
|
---|
158 | memcpy((uint8_t *)pErr + pErr->off, pszFile, cchFile + 1);
|
---|
159 | pErr->off += cchFile + 1;
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (cchFunction)
|
---|
163 | {
|
---|
164 | pErr->offFunction = pErr->off;
|
---|
165 | memcpy((uint8_t *)pErr + pErr->off, pszFunction, cchFunction + 1);
|
---|
166 | pErr->off += cchFunction + 1;
|
---|
167 | }
|
---|
168 |
|
---|
169 | pErr->offMessage = pErr->off;
|
---|
170 |
|
---|
171 | /* format the message (pErr might be reallocated) */
|
---|
172 | VMSETERRORFMTARGS Args;
|
---|
173 | Args.pVM = pVM;
|
---|
174 | Args.pErr = pErr;
|
---|
175 |
|
---|
176 | va_list va2;
|
---|
177 | va_copy(va2, args);
|
---|
178 | RTStrFormatV(vmSetErrorFmtOut, &pErr, NULL, NULL, &pszFormatTmp, args);
|
---|
179 | va_end(va2);
|
---|
180 |
|
---|
181 | /* done. */
|
---|
182 | pVM->vm.s.pErrorR3 = MMHyper2HC(pVM, (uintptr_t)pArgs.pErr);
|
---|
183 | }
|
---|
184 | #endif
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Sets the runtime error message.
|
---|
190 | *
|
---|
191 | * As opposed VMSetError(), this method is intended to inform the VM user about
|
---|
192 | * errors and error-like conditions that happen at an arbitrary point during VM
|
---|
193 | * execution (like "host memory low" or "out of host disk space").
|
---|
194 | *
|
---|
195 | * @returns VBox status code. For some flags the status code needs to be
|
---|
196 | * propagated up the stack, but this may depend on where the call was
|
---|
197 | * made.
|
---|
198 | *
|
---|
199 | * @param pVM The VM handle.
|
---|
200 | *
|
---|
201 | * @param fFlags Flags indicating which actions to take.
|
---|
202 | * See VMSETRTERR_FLAGS_* for details on each flag.
|
---|
203 | *
|
---|
204 | * @param pszErrorId Unique error identificator string. This is used by
|
---|
205 | * the frontends and maybe other devices or drivers, so
|
---|
206 | * once an ID has been selected it's essentially
|
---|
207 | * unchangable. Employ camelcase when constructing the
|
---|
208 | * string, leave out spaces.
|
---|
209 | *
|
---|
210 | * The registered runtime error callbacks should string
|
---|
211 | * switch on this and handle the ones it knows
|
---|
212 | * specifically and the unknown ones generically.
|
---|
213 | *
|
---|
214 | * @param pszFormat Error message format string.
|
---|
215 | * @param ... Error message arguments.
|
---|
216 | *
|
---|
217 | * @thread Any
|
---|
218 | */
|
---|
219 | VMMDECL(int) VMSetRuntimeError(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, ...)
|
---|
220 | {
|
---|
221 | va_list va;
|
---|
222 | va_start(va, pszFormat);
|
---|
223 | int rc = VMSetRuntimeErrorV(pVM, fFlags, pszErrorId, pszFormat, va);
|
---|
224 | va_end(va);
|
---|
225 | return rc;
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * va_list version of VMSetRuntimeError.
|
---|
231 | *
|
---|
232 | * @returns VBox status code. For some flags the status code needs to be
|
---|
233 | * propagated up the stack, but this may depend on where the call was
|
---|
234 | * made. For most actions, there is a force action flag mopping up if
|
---|
235 | * the status code can't be propagated.
|
---|
236 | *
|
---|
237 | * @param pVM The VM handle.
|
---|
238 | * @param fFlags Flags indicating which actions to take. See
|
---|
239 | * VMSETRTERR_FLAGS_*.
|
---|
240 | * @param pszErrorId Error ID string.
|
---|
241 | * @param pszFormat Error message format string.
|
---|
242 | * @param va Error message arguments.
|
---|
243 | *
|
---|
244 | * @thread Any
|
---|
245 | */
|
---|
246 | VMMDECL(int) VMSetRuntimeErrorV(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list va)
|
---|
247 | {
|
---|
248 | Log(("VMSetRuntimeErrorV: fFlags=%#x pszErrorId=%s\n", fFlags, pszErrorId));
|
---|
249 |
|
---|
250 | /*
|
---|
251 | * Relaxed parameter validation.
|
---|
252 | */
|
---|
253 | AssertPtr(pVM);
|
---|
254 | AssertMsg(!(fFlags & ~(VMSETRTERR_FLAGS_NO_WAIT | VMSETRTERR_FLAGS_SUSPEND | VMSETRTERR_FLAGS_FATAL)), ("%#x\n", fFlags));
|
---|
255 | Assert(!(fFlags & VMSETRTERR_FLAGS_NO_WAIT) || !VM_IS_EMT(pVM));
|
---|
256 | Assert(!(fFlags & VMSETRTERR_FLAGS_SUSPEND) || !(fFlags & VMSETRTERR_FLAGS_FATAL));
|
---|
257 | AssertPtr(pszErrorId);
|
---|
258 | Assert(*pszErrorId);
|
---|
259 | Assert(memchr(pszErrorId, '\0', 128) != NULL);
|
---|
260 | AssertPtr(pszFormat);
|
---|
261 | Assert(memchr(pszFormat, '\0', 512) != NULL);
|
---|
262 |
|
---|
263 | #ifdef IN_RING3
|
---|
264 | /*
|
---|
265 | * Switch to EMT.
|
---|
266 | */
|
---|
267 | va_list va2;
|
---|
268 | va_copy(va2, va); /* Have to make a copy here or GCC will break. */
|
---|
269 | int rc;
|
---|
270 | PVMREQ pReq;
|
---|
271 | if ( !(fFlags & VMSETRTERR_FLAGS_NO_WAIT)
|
---|
272 | || VM_IS_EMT(pVM))
|
---|
273 | {
|
---|
274 | rc = VMR3ReqCallU(pVM->pUVM, VMREQDEST_ANY, &pReq, RT_INDEFINITE_WAIT, VMREQFLAGS_VBOX_STATUS,
|
---|
275 | (PFNRT)vmR3SetRuntimeErrorV, 5, pVM, fFlags, pszErrorId, pszFormat, &va2);
|
---|
276 | if (RT_SUCCESS(rc))
|
---|
277 | rc = pReq->iStatus;
|
---|
278 | }
|
---|
279 | else
|
---|
280 | rc = VMR3ReqCallU(pVM->pUVM, VMREQDEST_ANY, &pReq, 0, VMREQFLAGS_VBOX_STATUS | VMREQFLAGS_NO_WAIT,
|
---|
281 | (PFNRT)vmR3SetRuntimeErrorV, 5, pVM, fFlags, pszErrorId, pszFormat, &va2);
|
---|
282 | VMR3ReqFree(pReq);
|
---|
283 | va_end(va2);
|
---|
284 |
|
---|
285 | #else
|
---|
286 | /*
|
---|
287 | * We're already on the EMT and can safely create a VMRUNTIMEERROR chunk.
|
---|
288 | */
|
---|
289 | AssertReleaseMsgFailed(("Congratulations! You will have the pleasure of debugging the RC/R0 path.\n"));
|
---|
290 | vmSetRuntimeErrorCopy(pVM, fFlags, pszErrorId, pszFormat, va);
|
---|
291 |
|
---|
292 | # ifdef IN_RC
|
---|
293 | int rc = VMMGCCallHost(pVM, VMMCALLHOST_VM_SET_RUNTIME_ERROR, 0);
|
---|
294 | # else
|
---|
295 | int rc = VMMR0CallHost(pVM, VMMCALLHOST_VM_SET_RUNTIME_ERROR, 0);
|
---|
296 | # endif
|
---|
297 | #endif
|
---|
298 |
|
---|
299 | Log(("VMSetRuntimeErrorV: returns %Rrc (pszErrorId=%s)\n", rc, pszErrorId));
|
---|
300 | return rc;
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * Copies the error to a VMRUNTIMEERROR structure.
|
---|
306 | *
|
---|
307 | * This is mainly intended for Ring-0 and RC where the error must be copied to
|
---|
308 | * memory accessible from ring-3. But it's just possible that we might add
|
---|
309 | * APIs for retrieving the VMRUNTIMEERROR copy later.
|
---|
310 | *
|
---|
311 | * @param pVM VM handle. Must be non-NULL.
|
---|
312 | * @param fFlags The error flags.
|
---|
313 | * @param pszErrorId Error ID string.
|
---|
314 | * @param pszFormat Error message format string.
|
---|
315 | * @param va Error message arguments. This is of course spoiled
|
---|
316 | * by this call.
|
---|
317 | * @thread EMT
|
---|
318 | */
|
---|
319 | void vmSetRuntimeErrorCopy(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list va)
|
---|
320 | {
|
---|
321 | #if 0 /// @todo implement Ring-0 and GC VMSetError
|
---|
322 | /*
|
---|
323 | * Create the untranslated message copy.
|
---|
324 | */
|
---|
325 | /* free any old message. */
|
---|
326 | MMHyperFree(pVM, MMHyperR32Ctx(pVM, pVM->vm.s.pRuntimeErrorR3));
|
---|
327 | pVM->vm.s.pRuntimeErrorR3 = NULL;
|
---|
328 |
|
---|
329 | /* calc reasonable start size. */
|
---|
330 | size_t cchErrorID = pszErrorId ? strlen(pszErrorId) : 0;
|
---|
331 | size_t cchFormat = strlen(pszFormat);
|
---|
332 | size_t cb = sizeof(VMRUNTIMEERROR)
|
---|
333 | + cchErrorID + 1
|
---|
334 | + cchFormat + 32;
|
---|
335 |
|
---|
336 | /* allocate it */
|
---|
337 | void *pv;
|
---|
338 | int rc2 = MMHyperAlloc(pVM, cb, 0, MM_TAG_VM, &pv);
|
---|
339 | if (RT_SUCCESS(rc2))
|
---|
340 | {
|
---|
341 | /* initialize it. */
|
---|
342 | PVMRUNTIMEERROR pErr = (PVMRUNTIMEERROR)pv;
|
---|
343 | pErr->cbAllocated = cb;
|
---|
344 | pErr->fFlags = fFlags;
|
---|
345 | pErr->off = sizeof(PVMRUNTIMEERROR);
|
---|
346 | pErr->offErrorID = 0;
|
---|
347 |
|
---|
348 | if (cchErrorID)
|
---|
349 | {
|
---|
350 | pErr->offErrorID = pErr->off;
|
---|
351 | memcpy((uint8_t *)pErr + pErr->off, pszErrorId, cchErrorID + 1);
|
---|
352 | pErr->off += cchErrorID + 1;
|
---|
353 | }
|
---|
354 |
|
---|
355 | pErr->offMessage = pErr->off;
|
---|
356 |
|
---|
357 | /* format the message (pErr might be reallocated) */
|
---|
358 | VMSETRUNTIMEERRORFMTARGS Args;
|
---|
359 | Args.pVM = pVM;
|
---|
360 | Args.pErr = pErr;
|
---|
361 |
|
---|
362 | va_list va2;
|
---|
363 | va_copy(va2, args);
|
---|
364 | RTStrFormatV(vmSetRuntimeErrorFmtOut, &pErr, NULL, NULL, &pszFormatTmp, args);
|
---|
365 | va_end(va2);
|
---|
366 |
|
---|
367 | /* done. */
|
---|
368 | pVM->vm.s.pErrorRuntimeR3 = MMHyper2HC(pVM, (uintptr_t)pArgs.pErr);
|
---|
369 | }
|
---|
370 | #endif
|
---|
371 | }
|
---|
372 |
|
---|
373 |
|
---|
374 | /**
|
---|
375 | * Gets the name of VM state.
|
---|
376 | *
|
---|
377 | * @returns Pointer to a read-only string with the state name.
|
---|
378 | * @param enmState The state.
|
---|
379 | */
|
---|
380 | VMMDECL(const char *) VMGetStateName(VMSTATE enmState)
|
---|
381 | {
|
---|
382 | switch (enmState)
|
---|
383 | {
|
---|
384 | #define MY_CASE(enm) case VMSTATE_##enm: return #enm;
|
---|
385 | MY_CASE(CREATING);
|
---|
386 | MY_CASE(CREATED);
|
---|
387 | MY_CASE(RUNNING);
|
---|
388 | MY_CASE(LOADING);
|
---|
389 | MY_CASE(LOAD_FAILURE);
|
---|
390 | MY_CASE(SAVING);
|
---|
391 | MY_CASE(SUSPENDED);
|
---|
392 | MY_CASE(RESETTING);
|
---|
393 | MY_CASE(GURU_MEDITATION);
|
---|
394 | MY_CASE(OFF);
|
---|
395 | MY_CASE(DESTROYING);
|
---|
396 | MY_CASE(TERMINATED);
|
---|
397 | #undef MY_CASE
|
---|
398 | default:
|
---|
399 | return "Unknown";
|
---|
400 | }
|
---|
401 | }
|
---|
402 |
|
---|