[23] | 1 | /* $Id: VMAll.cpp 41965 2012-06-29 02:52:49Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
| 3 | * VM - Virtual Machine All Contexts.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[28800] | 7 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
[1] | 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
|
---|
[5999] | 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.
|
---|
[1] | 16 | */
|
---|
| 17 |
|
---|
| 18 |
|
---|
| 19 | /*******************************************************************************
|
---|
| 20 | * Header Files *
|
---|
| 21 | *******************************************************************************/
|
---|
| 22 | #define LOG_GROUP LOG_GROUP_VM
|
---|
| 23 | #include "VMInternal.h"
|
---|
[35346] | 24 | #include <VBox/vmm/vmm.h>
|
---|
| 25 | #include <VBox/vmm/mm.h>
|
---|
| 26 | #include <VBox/vmm/vm.h>
|
---|
[1] | 27 | #include <VBox/err.h>
|
---|
[18645] | 28 | #include <VBox/log.h>
|
---|
[1] | 29 |
|
---|
| 30 | #include <iprt/assert.h>
|
---|
| 31 | #include <iprt/string.h>
|
---|
[18645] | 32 | #ifndef IN_RC
|
---|
| 33 | # include <iprt/thread.h>
|
---|
| 34 | #endif
|
---|
[1] | 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");
|
---|
[234] | 43 | * @endcode
|
---|
[41800] | 44 | * @param pVM Pointer to the VM. Must be non-NULL.
|
---|
[1] | 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 | */
|
---|
[12989] | 51 | VMMDECL(int) VMSetError(PVM pVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, ...)
|
---|
[1] | 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");
|
---|
[234] | 67 | * @endcode
|
---|
[41800] | 68 | * @param pVM Pointer to the VM. Must be non-NULL.
|
---|
[1] | 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 | */
|
---|
[12989] | 75 | VMMDECL(int) VMSetErrorV(PVM pVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list args)
|
---|
[1] | 76 | {
|
---|
| 77 | #ifdef IN_RING3
|
---|
| 78 | /*
|
---|
| 79 | * Switch to EMT.
|
---|
| 80 | */
|
---|
[4521] | 81 | va_list va2;
|
---|
| 82 | va_copy(va2, args); /* Have to make a copy here or GCC will break. */
|
---|
[38838] | 83 | VMR3ReqPriorityCallWait(pVM, VMCPUID_ANY, (PFNRT)vmR3SetErrorUV, 7, /* ASSUMES 3 source pos args! */
|
---|
| 84 | pVM->pUVM, rc, RT_SRC_POS_ARGS, pszFormat, &va2);
|
---|
[4521] | 85 | va_end(va2);
|
---|
[1] | 86 |
|
---|
| 87 | #else
|
---|
| 88 | /*
|
---|
| 89 | * We're already on the EMT thread and can safely create a VMERROR chunk.
|
---|
| 90 | */
|
---|
| 91 | vmSetErrorCopy(pVM, rc, RT_SRC_POS_ARGS, pszFormat, args);
|
---|
[20874] | 92 | VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_VM_SET_ERROR, 0);
|
---|
[1] | 93 | #endif
|
---|
| 94 | return rc;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | /**
|
---|
| 99 | * Copies the error to a VMERROR structure.
|
---|
| 100 | *
|
---|
| 101 | * This is mainly intended for Ring-0 and GC where the error must be copied to
|
---|
| 102 | * memory accessible from ring-3. But it's just possible that we might add
|
---|
| 103 | * APIs for retrieving the VMERROR copy later.
|
---|
| 104 | *
|
---|
[41800] | 105 | * @param pVM Pointer to the VM. Must be non-NULL.
|
---|
[1] | 106 | * @param rc VBox status code.
|
---|
| 107 | * @param RT_SRC_POS_DECL Use RT_SRC_POS.
|
---|
| 108 | * @param pszFormat Error message format string.
|
---|
| 109 | * @param args Error message arguments.
|
---|
| 110 | * @thread EMT
|
---|
| 111 | */
|
---|
| 112 | void vmSetErrorCopy(PVM pVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list args)
|
---|
| 113 | {
|
---|
[39078] | 114 | NOREF(pVM); NOREF(rc); RT_SRC_POS_NOREF(); NOREF(pszFormat); NOREF(args);
|
---|
[1] | 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);
|
---|
[13816] | 135 | if (RT_SUCCESS(rc2))
|
---|
[1] | 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 |
|
---|
[234] | 176 |
|
---|
| 177 | /**
|
---|
| 178 | * Sets the runtime error message.
|
---|
[18645] | 179 | *
|
---|
[234] | 180 | * As opposed VMSetError(), this method is intended to inform the VM user about
|
---|
| 181 | * errors and error-like conditions that happen at an arbitrary point during VM
|
---|
| 182 | * execution (like "host memory low" or "out of host disk space").
|
---|
| 183 | *
|
---|
[18649] | 184 | * @returns VBox status code. For some flags the status code <b>must</b> be
|
---|
| 185 | * propagated up the stack.
|
---|
[234] | 186 | *
|
---|
[41783] | 187 | * @param pVM Pointer to the VM.
|
---|
[234] | 188 | *
|
---|
[18645] | 189 | * @param fFlags Flags indicating which actions to take.
|
---|
| 190 | * See VMSETRTERR_FLAGS_* for details on each flag.
|
---|
[234] | 191 | *
|
---|
[33540] | 192 | * @param pszErrorId Unique error identification string. This is used by
|
---|
[18645] | 193 | * the frontends and maybe other devices or drivers, so
|
---|
| 194 | * once an ID has been selected it's essentially
|
---|
| 195 | * unchangable. Employ camelcase when constructing the
|
---|
| 196 | * string, leave out spaces.
|
---|
| 197 | *
|
---|
| 198 | * The registered runtime error callbacks should string
|
---|
| 199 | * switch on this and handle the ones it knows
|
---|
| 200 | * specifically and the unknown ones generically.
|
---|
| 201 | *
|
---|
[234] | 202 | * @param pszFormat Error message format string.
|
---|
| 203 | * @param ... Error message arguments.
|
---|
| 204 | *
|
---|
| 205 | * @thread Any
|
---|
| 206 | */
|
---|
[18645] | 207 | VMMDECL(int) VMSetRuntimeError(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, ...)
|
---|
[234] | 208 | {
|
---|
[18645] | 209 | va_list va;
|
---|
| 210 | va_start(va, pszFormat);
|
---|
| 211 | int rc = VMSetRuntimeErrorV(pVM, fFlags, pszErrorId, pszFormat, va);
|
---|
| 212 | va_end(va);
|
---|
[234] | 213 | return rc;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 |
|
---|
| 217 | /**
|
---|
| 218 | * va_list version of VMSetRuntimeError.
|
---|
| 219 | *
|
---|
[18649] | 220 | * @returns VBox status code. For some flags the status code <b>must</b> be
|
---|
| 221 | * propagated up the stack.
|
---|
[18645] | 222 | *
|
---|
[41783] | 223 | * @param pVM Pointer to the VM.
|
---|
[18645] | 224 | * @param fFlags Flags indicating which actions to take. See
|
---|
| 225 | * VMSETRTERR_FLAGS_*.
|
---|
| 226 | * @param pszErrorId Error ID string.
|
---|
[234] | 227 | * @param pszFormat Error message format string.
|
---|
[18645] | 228 | * @param va Error message arguments.
|
---|
[234] | 229 | *
|
---|
| 230 | * @thread Any
|
---|
| 231 | */
|
---|
[18645] | 232 | VMMDECL(int) VMSetRuntimeErrorV(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list va)
|
---|
[234] | 233 | {
|
---|
[18645] | 234 | Log(("VMSetRuntimeErrorV: fFlags=%#x pszErrorId=%s\n", fFlags, pszErrorId));
|
---|
| 235 |
|
---|
| 236 | /*
|
---|
| 237 | * Relaxed parameter validation.
|
---|
| 238 | */
|
---|
| 239 | AssertPtr(pVM);
|
---|
[18646] | 240 | AssertMsg(!(fFlags & ~(VMSETRTERR_FLAGS_NO_WAIT | VMSETRTERR_FLAGS_SUSPEND | VMSETRTERR_FLAGS_FATAL)), ("%#x\n", fFlags));
|
---|
[18645] | 241 | Assert(!(fFlags & VMSETRTERR_FLAGS_NO_WAIT) || !VM_IS_EMT(pVM));
|
---|
| 242 | Assert(!(fFlags & VMSETRTERR_FLAGS_SUSPEND) || !(fFlags & VMSETRTERR_FLAGS_FATAL));
|
---|
| 243 | AssertPtr(pszErrorId);
|
---|
| 244 | Assert(*pszErrorId);
|
---|
[30320] | 245 | Assert(RTStrEnd(pszErrorId, 128) != NULL);
|
---|
[18645] | 246 | AssertPtr(pszFormat);
|
---|
[30320] | 247 | Assert(RTStrEnd(pszFormat, 512) != NULL);
|
---|
[18645] | 248 |
|
---|
[234] | 249 | #ifdef IN_RING3
|
---|
| 250 | /*
|
---|
| 251 | * Switch to EMT.
|
---|
[22915] | 252 | *
|
---|
| 253 | * If it's a no-wait request, we have to format the message into a buffer
|
---|
| 254 | * here since the variable arguments list will become invalid once we call
|
---|
| 255 | * va_end and return.
|
---|
[234] | 256 | */
|
---|
[18645] | 257 | int rc;
|
---|
| 258 | if ( !(fFlags & VMSETRTERR_FLAGS_NO_WAIT)
|
---|
| 259 | || VM_IS_EMT(pVM))
|
---|
| 260 | {
|
---|
[22915] | 261 | fFlags &= ~VMSETRTERR_FLAGS_NO_WAIT;
|
---|
| 262 |
|
---|
| 263 | va_list va2;
|
---|
| 264 | va_copy(va2, va); /* Have to make a copy here or GCC will break. */
|
---|
[38838] | 265 | rc = VMR3ReqPriorityCallWait(pVM, VMCPUID_ANY,
|
---|
| 266 | (PFNRT)vmR3SetRuntimeErrorV, 5, pVM, fFlags, pszErrorId, pszFormat, &va2);
|
---|
[22915] | 267 | va_end(va2);
|
---|
[18645] | 268 | }
|
---|
| 269 | else
|
---|
[22915] | 270 | {
|
---|
| 271 | char *pszMessage = MMR3HeapAPrintfV(pVM, MM_TAG_VM, pszFormat, va);
|
---|
[38838] | 272 | rc = VMR3ReqCallNoWait(pVM, VMCPUID_ANY,
|
---|
| 273 | (PFNRT)vmR3SetRuntimeError, 4, pVM, fFlags, pszErrorId, pszMessage);
|
---|
[22915] | 274 | if (RT_FAILURE(rc))
|
---|
| 275 | MMR3HeapFree(pszMessage);
|
---|
| 276 | }
|
---|
[234] | 277 |
|
---|
| 278 | #else
|
---|
| 279 | /*
|
---|
[18645] | 280 | * We're already on the EMT and can safely create a VMRUNTIMEERROR chunk.
|
---|
[234] | 281 | */
|
---|
[18645] | 282 | AssertReleaseMsgFailed(("Congratulations! You will have the pleasure of debugging the RC/R0 path.\n"));
|
---|
| 283 | vmSetRuntimeErrorCopy(pVM, fFlags, pszErrorId, pszFormat, va);
|
---|
[234] | 284 |
|
---|
[20874] | 285 | int rc = VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_VM_SET_RUNTIME_ERROR, 0);
|
---|
[234] | 286 | #endif
|
---|
[18645] | 287 |
|
---|
| 288 | Log(("VMSetRuntimeErrorV: returns %Rrc (pszErrorId=%s)\n", rc, pszErrorId));
|
---|
| 289 | return rc;
|
---|
[234] | 290 | }
|
---|
| 291 |
|
---|
| 292 |
|
---|
| 293 | /**
|
---|
| 294 | * Copies the error to a VMRUNTIMEERROR structure.
|
---|
| 295 | *
|
---|
[18645] | 296 | * This is mainly intended for Ring-0 and RC where the error must be copied to
|
---|
[234] | 297 | * memory accessible from ring-3. But it's just possible that we might add
|
---|
| 298 | * APIs for retrieving the VMRUNTIMEERROR copy later.
|
---|
| 299 | *
|
---|
[41800] | 300 | * @param pVM Pointer to the VM. Must be non-NULL.
|
---|
[18645] | 301 | * @param fFlags The error flags.
|
---|
| 302 | * @param pszErrorId Error ID string.
|
---|
[234] | 303 | * @param pszFormat Error message format string.
|
---|
[18645] | 304 | * @param va Error message arguments. This is of course spoiled
|
---|
| 305 | * by this call.
|
---|
[234] | 306 | * @thread EMT
|
---|
| 307 | */
|
---|
[18645] | 308 | void vmSetRuntimeErrorCopy(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list va)
|
---|
[234] | 309 | {
|
---|
[39078] | 310 | NOREF(pVM); NOREF(fFlags); NOREF(pszErrorId); NOREF(pszFormat); NOREF(va);
|
---|
[234] | 311 | #if 0 /// @todo implement Ring-0 and GC VMSetError
|
---|
| 312 | /*
|
---|
| 313 | * Create the untranslated message copy.
|
---|
| 314 | */
|
---|
| 315 | /* free any old message. */
|
---|
| 316 | MMHyperFree(pVM, MMHyperR32Ctx(pVM, pVM->vm.s.pRuntimeErrorR3));
|
---|
| 317 | pVM->vm.s.pRuntimeErrorR3 = NULL;
|
---|
| 318 |
|
---|
| 319 | /* calc reasonable start size. */
|
---|
[18645] | 320 | size_t cchErrorID = pszErrorId ? strlen(pszErrorId) : 0;
|
---|
[234] | 321 | size_t cchFormat = strlen(pszFormat);
|
---|
| 322 | size_t cb = sizeof(VMRUNTIMEERROR)
|
---|
| 323 | + cchErrorID + 1
|
---|
| 324 | + cchFormat + 32;
|
---|
| 325 |
|
---|
| 326 | /* allocate it */
|
---|
| 327 | void *pv;
|
---|
| 328 | int rc2 = MMHyperAlloc(pVM, cb, 0, MM_TAG_VM, &pv);
|
---|
[13816] | 329 | if (RT_SUCCESS(rc2))
|
---|
[234] | 330 | {
|
---|
| 331 | /* initialize it. */
|
---|
| 332 | PVMRUNTIMEERROR pErr = (PVMRUNTIMEERROR)pv;
|
---|
| 333 | pErr->cbAllocated = cb;
|
---|
[18645] | 334 | pErr->fFlags = fFlags;
|
---|
[234] | 335 | pErr->off = sizeof(PVMRUNTIMEERROR);
|
---|
[18645] | 336 | pErr->offErrorID = 0;
|
---|
[234] | 337 |
|
---|
| 338 | if (cchErrorID)
|
---|
| 339 | {
|
---|
| 340 | pErr->offErrorID = pErr->off;
|
---|
[18645] | 341 | memcpy((uint8_t *)pErr + pErr->off, pszErrorId, cchErrorID + 1);
|
---|
[234] | 342 | pErr->off += cchErrorID + 1;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | pErr->offMessage = pErr->off;
|
---|
| 346 |
|
---|
| 347 | /* format the message (pErr might be reallocated) */
|
---|
| 348 | VMSETRUNTIMEERRORFMTARGS Args;
|
---|
| 349 | Args.pVM = pVM;
|
---|
| 350 | Args.pErr = pErr;
|
---|
| 351 |
|
---|
| 352 | va_list va2;
|
---|
| 353 | va_copy(va2, args);
|
---|
| 354 | RTStrFormatV(vmSetRuntimeErrorFmtOut, &pErr, NULL, NULL, &pszFormatTmp, args);
|
---|
| 355 | va_end(va2);
|
---|
| 356 |
|
---|
| 357 | /* done. */
|
---|
| 358 | pVM->vm.s.pErrorRuntimeR3 = MMHyper2HC(pVM, (uintptr_t)pArgs.pErr);
|
---|
| 359 | }
|
---|
| 360 | #endif
|
---|
| 361 | }
|
---|
| 362 |
|
---|
[6835] | 363 |
|
---|
| 364 | /**
|
---|
| 365 | * Gets the name of VM state.
|
---|
| 366 | *
|
---|
| 367 | * @returns Pointer to a read-only string with the state name.
|
---|
| 368 | * @param enmState The state.
|
---|
| 369 | */
|
---|
[12989] | 370 | VMMDECL(const char *) VMGetStateName(VMSTATE enmState)
|
---|
[6835] | 371 | {
|
---|
| 372 | switch (enmState)
|
---|
| 373 | {
|
---|
| 374 | #define MY_CASE(enm) case VMSTATE_##enm: return #enm;
|
---|
| 375 | MY_CASE(CREATING);
|
---|
| 376 | MY_CASE(CREATED);
|
---|
| 377 | MY_CASE(RUNNING);
|
---|
| 378 | MY_CASE(LOADING);
|
---|
| 379 | MY_CASE(LOAD_FAILURE);
|
---|
| 380 | MY_CASE(SAVING);
|
---|
| 381 | MY_CASE(SUSPENDED);
|
---|
| 382 | MY_CASE(RESETTING);
|
---|
| 383 | MY_CASE(GURU_MEDITATION);
|
---|
| 384 | MY_CASE(OFF);
|
---|
| 385 | MY_CASE(DESTROYING);
|
---|
| 386 | MY_CASE(TERMINATED);
|
---|
| 387 | #undef MY_CASE
|
---|
| 388 | default:
|
---|
| 389 | return "Unknown";
|
---|
| 390 | }
|
---|
| 391 | }
|
---|
[13667] | 392 |
|
---|