1 | /* $Id: VMAll.cpp 18649 2009-04-02 16:24:49Z 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 <b>must</b> be
|
---|
196 | * propagated up the stack.
|
---|
197 | *
|
---|
198 | * @param pVM The VM handle.
|
---|
199 | *
|
---|
200 | * @param fFlags Flags indicating which actions to take.
|
---|
201 | * See VMSETRTERR_FLAGS_* for details on each flag.
|
---|
202 | *
|
---|
203 | * @param pszErrorId Unique error identificator string. This is used by
|
---|
204 | * the frontends and maybe other devices or drivers, so
|
---|
205 | * once an ID has been selected it's essentially
|
---|
206 | * unchangable. Employ camelcase when constructing the
|
---|
207 | * string, leave out spaces.
|
---|
208 | *
|
---|
209 | * The registered runtime error callbacks should string
|
---|
210 | * switch on this and handle the ones it knows
|
---|
211 | * specifically and the unknown ones generically.
|
---|
212 | *
|
---|
213 | * @param pszFormat Error message format string.
|
---|
214 | * @param ... Error message arguments.
|
---|
215 | *
|
---|
216 | * @thread Any
|
---|
217 | */
|
---|
218 | VMMDECL(int) VMSetRuntimeError(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, ...)
|
---|
219 | {
|
---|
220 | va_list va;
|
---|
221 | va_start(va, pszFormat);
|
---|
222 | int rc = VMSetRuntimeErrorV(pVM, fFlags, pszErrorId, pszFormat, va);
|
---|
223 | va_end(va);
|
---|
224 | return rc;
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * va_list version of VMSetRuntimeError.
|
---|
230 | *
|
---|
231 | * @returns VBox status code. For some flags the status code <b>must</b> be
|
---|
232 | * propagated up the stack.
|
---|
233 | *
|
---|
234 | * @param pVM The VM handle.
|
---|
235 | * @param fFlags Flags indicating which actions to take. See
|
---|
236 | * VMSETRTERR_FLAGS_*.
|
---|
237 | * @param pszErrorId Error ID string.
|
---|
238 | * @param pszFormat Error message format string.
|
---|
239 | * @param va Error message arguments.
|
---|
240 | *
|
---|
241 | * @thread Any
|
---|
242 | */
|
---|
243 | VMMDECL(int) VMSetRuntimeErrorV(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list va)
|
---|
244 | {
|
---|
245 | Log(("VMSetRuntimeErrorV: fFlags=%#x pszErrorId=%s\n", fFlags, pszErrorId));
|
---|
246 |
|
---|
247 | /*
|
---|
248 | * Relaxed parameter validation.
|
---|
249 | */
|
---|
250 | AssertPtr(pVM);
|
---|
251 | AssertMsg(!(fFlags & ~(VMSETRTERR_FLAGS_NO_WAIT | VMSETRTERR_FLAGS_SUSPEND | VMSETRTERR_FLAGS_FATAL)), ("%#x\n", fFlags));
|
---|
252 | Assert(!(fFlags & VMSETRTERR_FLAGS_NO_WAIT) || !VM_IS_EMT(pVM));
|
---|
253 | Assert(!(fFlags & VMSETRTERR_FLAGS_SUSPEND) || !(fFlags & VMSETRTERR_FLAGS_FATAL));
|
---|
254 | AssertPtr(pszErrorId);
|
---|
255 | Assert(*pszErrorId);
|
---|
256 | Assert(memchr(pszErrorId, '\0', 128) != NULL);
|
---|
257 | AssertPtr(pszFormat);
|
---|
258 | Assert(memchr(pszFormat, '\0', 512) != NULL);
|
---|
259 |
|
---|
260 | #ifdef IN_RING3
|
---|
261 | /*
|
---|
262 | * Switch to EMT.
|
---|
263 | */
|
---|
264 | va_list va2;
|
---|
265 | va_copy(va2, va); /* Have to make a copy here or GCC will break. */
|
---|
266 | int rc;
|
---|
267 | PVMREQ pReq;
|
---|
268 | if ( !(fFlags & VMSETRTERR_FLAGS_NO_WAIT)
|
---|
269 | || VM_IS_EMT(pVM))
|
---|
270 | {
|
---|
271 | rc = VMR3ReqCallU(pVM->pUVM, VMREQDEST_ANY, &pReq, RT_INDEFINITE_WAIT, VMREQFLAGS_VBOX_STATUS,
|
---|
272 | (PFNRT)vmR3SetRuntimeErrorV, 5, pVM, fFlags, pszErrorId, pszFormat, &va2);
|
---|
273 | if (RT_SUCCESS(rc))
|
---|
274 | rc = pReq->iStatus;
|
---|
275 | }
|
---|
276 | else
|
---|
277 | rc = VMR3ReqCallU(pVM->pUVM, VMREQDEST_ANY, &pReq, 0, VMREQFLAGS_VBOX_STATUS | VMREQFLAGS_NO_WAIT,
|
---|
278 | (PFNRT)vmR3SetRuntimeErrorV, 5, pVM, fFlags, pszErrorId, pszFormat, &va2);
|
---|
279 | VMR3ReqFree(pReq);
|
---|
280 | va_end(va2);
|
---|
281 |
|
---|
282 | #else
|
---|
283 | /*
|
---|
284 | * We're already on the EMT and can safely create a VMRUNTIMEERROR chunk.
|
---|
285 | */
|
---|
286 | AssertReleaseMsgFailed(("Congratulations! You will have the pleasure of debugging the RC/R0 path.\n"));
|
---|
287 | vmSetRuntimeErrorCopy(pVM, fFlags, pszErrorId, pszFormat, va);
|
---|
288 |
|
---|
289 | # ifdef IN_RC
|
---|
290 | int rc = VMMGCCallHost(pVM, VMMCALLHOST_VM_SET_RUNTIME_ERROR, 0);
|
---|
291 | # else
|
---|
292 | int rc = VMMR0CallHost(pVM, VMMCALLHOST_VM_SET_RUNTIME_ERROR, 0);
|
---|
293 | # endif
|
---|
294 | #endif
|
---|
295 |
|
---|
296 | Log(("VMSetRuntimeErrorV: returns %Rrc (pszErrorId=%s)\n", rc, pszErrorId));
|
---|
297 | return rc;
|
---|
298 | }
|
---|
299 |
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * Copies the error to a VMRUNTIMEERROR structure.
|
---|
303 | *
|
---|
304 | * This is mainly intended for Ring-0 and RC where the error must be copied to
|
---|
305 | * memory accessible from ring-3. But it's just possible that we might add
|
---|
306 | * APIs for retrieving the VMRUNTIMEERROR copy later.
|
---|
307 | *
|
---|
308 | * @param pVM VM handle. Must be non-NULL.
|
---|
309 | * @param fFlags The error flags.
|
---|
310 | * @param pszErrorId Error ID string.
|
---|
311 | * @param pszFormat Error message format string.
|
---|
312 | * @param va Error message arguments. This is of course spoiled
|
---|
313 | * by this call.
|
---|
314 | * @thread EMT
|
---|
315 | */
|
---|
316 | void vmSetRuntimeErrorCopy(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list va)
|
---|
317 | {
|
---|
318 | #if 0 /// @todo implement Ring-0 and GC VMSetError
|
---|
319 | /*
|
---|
320 | * Create the untranslated message copy.
|
---|
321 | */
|
---|
322 | /* free any old message. */
|
---|
323 | MMHyperFree(pVM, MMHyperR32Ctx(pVM, pVM->vm.s.pRuntimeErrorR3));
|
---|
324 | pVM->vm.s.pRuntimeErrorR3 = NULL;
|
---|
325 |
|
---|
326 | /* calc reasonable start size. */
|
---|
327 | size_t cchErrorID = pszErrorId ? strlen(pszErrorId) : 0;
|
---|
328 | size_t cchFormat = strlen(pszFormat);
|
---|
329 | size_t cb = sizeof(VMRUNTIMEERROR)
|
---|
330 | + cchErrorID + 1
|
---|
331 | + cchFormat + 32;
|
---|
332 |
|
---|
333 | /* allocate it */
|
---|
334 | void *pv;
|
---|
335 | int rc2 = MMHyperAlloc(pVM, cb, 0, MM_TAG_VM, &pv);
|
---|
336 | if (RT_SUCCESS(rc2))
|
---|
337 | {
|
---|
338 | /* initialize it. */
|
---|
339 | PVMRUNTIMEERROR pErr = (PVMRUNTIMEERROR)pv;
|
---|
340 | pErr->cbAllocated = cb;
|
---|
341 | pErr->fFlags = fFlags;
|
---|
342 | pErr->off = sizeof(PVMRUNTIMEERROR);
|
---|
343 | pErr->offErrorID = 0;
|
---|
344 |
|
---|
345 | if (cchErrorID)
|
---|
346 | {
|
---|
347 | pErr->offErrorID = pErr->off;
|
---|
348 | memcpy((uint8_t *)pErr + pErr->off, pszErrorId, cchErrorID + 1);
|
---|
349 | pErr->off += cchErrorID + 1;
|
---|
350 | }
|
---|
351 |
|
---|
352 | pErr->offMessage = pErr->off;
|
---|
353 |
|
---|
354 | /* format the message (pErr might be reallocated) */
|
---|
355 | VMSETRUNTIMEERRORFMTARGS Args;
|
---|
356 | Args.pVM = pVM;
|
---|
357 | Args.pErr = pErr;
|
---|
358 |
|
---|
359 | va_list va2;
|
---|
360 | va_copy(va2, args);
|
---|
361 | RTStrFormatV(vmSetRuntimeErrorFmtOut, &pErr, NULL, NULL, &pszFormatTmp, args);
|
---|
362 | va_end(va2);
|
---|
363 |
|
---|
364 | /* done. */
|
---|
365 | pVM->vm.s.pErrorRuntimeR3 = MMHyper2HC(pVM, (uintptr_t)pArgs.pErr);
|
---|
366 | }
|
---|
367 | #endif
|
---|
368 | }
|
---|
369 |
|
---|
370 |
|
---|
371 | /**
|
---|
372 | * Gets the name of VM state.
|
---|
373 | *
|
---|
374 | * @returns Pointer to a read-only string with the state name.
|
---|
375 | * @param enmState The state.
|
---|
376 | */
|
---|
377 | VMMDECL(const char *) VMGetStateName(VMSTATE enmState)
|
---|
378 | {
|
---|
379 | switch (enmState)
|
---|
380 | {
|
---|
381 | #define MY_CASE(enm) case VMSTATE_##enm: return #enm;
|
---|
382 | MY_CASE(CREATING);
|
---|
383 | MY_CASE(CREATED);
|
---|
384 | MY_CASE(RUNNING);
|
---|
385 | MY_CASE(LOADING);
|
---|
386 | MY_CASE(LOAD_FAILURE);
|
---|
387 | MY_CASE(SAVING);
|
---|
388 | MY_CASE(SUSPENDED);
|
---|
389 | MY_CASE(RESETTING);
|
---|
390 | MY_CASE(GURU_MEDITATION);
|
---|
391 | MY_CASE(OFF);
|
---|
392 | MY_CASE(DESTROYING);
|
---|
393 | MY_CASE(TERMINATED);
|
---|
394 | #undef MY_CASE
|
---|
395 | default:
|
---|
396 | return "Unknown";
|
---|
397 | }
|
---|
398 | }
|
---|
399 |
|
---|