VMMR0.cpp revision 52079e6b4668b5688cdd52891721f322fa721643
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * VMM - Host Context Ring 0.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * available from http://www.virtualbox.org. This file is free software;
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * you can redistribute it and/or modify it under the terms of the GNU
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * General Public License (GPL) as published by the Free Software
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * additional information or have any questions.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync/*******************************************************************************
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync* Header Files *
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync*******************************************************************************/
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync#if defined(_MSC_VER) && defined(RT_ARCH_AMD64) /** @todo check this with with VC7! */
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync/*******************************************************************************
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync* Internal Functions *
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync*******************************************************************************/
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync/*******************************************************************************
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync* Global Variables *
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync*******************************************************************************/
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync/** Pointer to the internal networking service instance. */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Initialize the module.
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync * This is called when we're first loaded.
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync * @returns 0 on success.
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync * @returns VBox status on failure.
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync * Initialize the GVMM, GMM, HWACCM, PGM (Darwin) and INTNET.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogFlow(("ModuleInit: g_pIntNet=%p\n", g_pIntNet));
ffb50166c9adb4ae583b914d405197035cf890advboxsync LogFlow(("ModuleInit: g_pIntNet=%p should be NULL now...\n", g_pIntNet));
ffb50166c9adb4ae583b914d405197035cf890advboxsync LogFlow(("ModuleInit: returns success. g_pIntNet=%p\n", g_pIntNet));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* bail out */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Terminate the module.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * This is called when we're finally unloaded.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Destroy the internal networking instance.
b4d7b4dbcc45b8bde7502aa129440d92d7ffd038vboxsync * PGM (Darwin) and HWACCM global cleanup.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Destroy the GMM and GVMM instances.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Initaties the R0 driver for a particular VM instance.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @returns VBox status code.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pVM The VM instance in question.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param uSvnRev The SVN revision of the ring-3 part.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @thread EMT.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Match the SVN revisions.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogRel(("VMMR0InitVM: Revision mismatch, r3=%d r0=%d\n", uSvnRev, VMMGetSvnRev()));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync SUPR0Printf("VMMR0InitVM: Revision mismatch, r3=%d r0=%d\n", uSvnRev, VMMGetSvnRev());
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Register the EMT R0 logger instance for VCPU 0.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync# if 0 /* testing of the logger. */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogCom(("vmmR0InitVM: before %p\n", RTLogDefaultInstance()));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogCom(("vmmR0InitVM: pfnFlush=%p actual=%p\n", pR0Logger->Logger.pfnFlush, vmmR0LoggerFlush));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogCom(("vmmR0InitVM: pfnLogger=%p actual=%p\n", pR0Logger->Logger.pfnLogger, vmmR0LoggerWrapper));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogCom(("vmmR0InitVM: offScratch=%d fFlags=%#x fDestFlags=%#x\n", pR0Logger->Logger.offScratch, pR0Logger->Logger.fFlags, pR0Logger->Logger.fDestFlags));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogCom(("vmmR0InitVM: after %p reg\n", RTLogDefaultInstance()));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync RTLogSetDefaultInstanceThread(NULL, pVM->pSession);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogCom(("vmmR0InitVM: after %p dereg\n", RTLogDefaultInstance()));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync pR0Logger->Logger.pfnLogger("hello ring-0 logger\n");
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogCom(("vmmR0InitVM: returned succesfully from direct logger call.\n"));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogCom(("vmmR0InitVM: returned succesfully from direct flush call.\n"));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogCom(("vmmR0InitVM: after %p reg2\n", RTLogDefaultInstance()));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync pR0Logger->Logger.pfnLogger("hello ring-0 logger\n");
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogCom(("vmmR0InitVM: returned succesfully from direct logger call (2). offScratch=%d\n", pR0Logger->Logger.offScratch));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync RTLogSetDefaultInstanceThread(NULL, pVM->pSession);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogCom(("vmmR0InitVM: after %p dereg2\n", RTLogDefaultInstance()));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync RTLogLoggerEx(&pR0Logger->Logger, 0, ~0U, "hello ring-0 logger (RTLogLoggerEx)\n");
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogCom(("vmmR0InitVM: RTLogLoggerEx returned fine offScratch=%d\n", pR0Logger->Logger.offScratch));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync RTLogPrintf("hello ring-0 logger (RTLogPrintf)\n");
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync LogCom(("vmmR0InitVM: RTLogPrintf returned fine offScratch=%d\n", pR0Logger->Logger.offScratch));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync Log(("Switching to per-thread logging instance %p (key=%p)\n", &pR0Logger->Logger, pVM->pSession));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync#endif /* LOG_ENABLED */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Initialize the per VM data for GVMM and GMM.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync// if (RT_SUCCESS(rc))
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync// rc = GMMR0InitPerVMData(pVM);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Init HWACCM, CPUM and PGM (Darwin only).
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync rc = CPUMR0Init(pVM); /** @todo rename to CPUMR0InitVM */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* bail out */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync RTLogSetDefaultInstanceThread(NULL, (uintptr_t)pVM->pSession);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Terminates the R0 driver for a particular VM instance.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * This is normally called by ring-3 as part of the VM termination process, but
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * may alternatively be called during the support driver session cleanup when
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * the VM object is destroyed (see GVMM).
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @returns VBox status code.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pVM The VM instance in question.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pGVM Pointer to the global VM structure. Optional.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @thread EMT or session clean up thread.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Tell GVMM what we're up to and check that we only do this once.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Deregister the logger.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync RTLogSetDefaultInstanceThread(NULL, (uintptr_t)pVM->pSession);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Record return code statistics
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pVM The VM handle.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pVCpu The VMCPU handle.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param rc The status code.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsyncstatic void vmmR0RecordRC(PVM pVM, PVMCPU pVCpu, int rc)
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Collect statistics.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetInterruptHyper);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetRingSwitchInt);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetStaleSelector);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOReadWrite);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOPatchRead);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOPatchWrite);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIOBlockEmulate);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchEmulate);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchIretIRQ);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetRescheduleREM);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetTimerPending);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetInterruptPending);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPDMQueueFlush);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMPoolGrow);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMMapChunk);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMAllocHandy);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync case VMMCALLRING3_REM_REPLAY_HANDLER_NOTIFICATIONS:
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallVMSetError);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallVMSetRuntimeError);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPATMDuplicateFn);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPGMChangeMode);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPGMFlushPending);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPendingRequest);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync#endif /* VBOX_WITH_STATISTICS */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Unused ring-0 entry point that used to be called from the interrupt gate.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Will be removed one of the next times we do a major SUPDrv version bump.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @returns VBox status code.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pVM The VM to operate on.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param enmOperation Which operation to execute.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pvArg Argument to the operation.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @remarks Assume called with interrupts disabled.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsyncVMMR0DECL(int) VMMR0EntryInt(PVM pVM, VMMR0OPERATION enmOperation, void *pvArg)
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * We're returning VERR_NOT_SUPPORT here so we've got something else
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * than -1 which the interrupt gate glue code might return.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync Log(("operation %#x is not supported\n", enmOperation));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * The Ring 0 entry point, called by the fast-ioctl path.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pVM The VM to operate on.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * The return code is stored in pVM->vmm.s.iLastGZRc.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param idCpu The Virtual CPU ID of the calling EMT.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param enmOperation Which operation to execute.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @remarks Assume called with interrupts _enabled_.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsyncVMMR0DECL(void) VMMR0EntryFast(PVM pVM, VMCPUID idCpu, VMMR0OPERATION enmOperation)
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Switch to GC and run guest raw mode code.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Disable interrupts before doing the world switch.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* Safety precaution as hwaccm disables the switcher. */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync pVCpu->vmm.s.iLastGZRc = VERR_RAW_MODE_INVALID_SMP;
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync pVCpu->vmm.s.iLastGZRc = VERR_PGM_NO_CR3_SHADOW_ROOT;
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* We might need to disable VT-x if the active switcher turns off paging. */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* Re-enable VT-x if previously turned off. */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Run guest code using the available hardware acceleration technology.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Disable interrupts before we do anything interesting. On Windows we avoid
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * this by having the support driver raise the IRQL before calling us, this way
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * we hope to get away with page faults and later calling into the kernel.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* Lazy registration of ring 0 loggers. */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync rc = vmmR0CallRing3SetJmp(&pVCpu->vmm.s.CallRing3JmpBufR0, HWACCMR0RunGuestCode, pVM, pVCpu); /* this may resume code. */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* System is about to go into suspend mode; go back to ring 3. */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* No special action required for external interrupts, just return. */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * For profiling.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Impossible.
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync * Validates a session or VM session argument.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @returns true / false accordingly.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pVM The VM argument.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pSession The session argument.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsyncDECLINLINE(bool) vmmR0IsValidSession(PVM pVM, PSUPDRVSESSION pClaimedSession, PSUPDRVSESSION pSession)
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* This must be set! */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync return false;
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* Only one out of the two. */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync return false;
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * VMMR0EntryEx worker function, either called directly or when ever possible
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * called thru a longjmp so we can exit safely on failure.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @returns VBox status code.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pVM The VM to operate on.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param idCpu Virtual CPU ID argument. Must be NIL_VMCPUID if pVM
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * is NIL_RTR0PTR, and may be NIL_VMCPUID if it isn't
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param enmOperation Which operation to execute.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pReqHdr This points to a SUPVMMR0REQHDR packet. Optional.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * The support driver validates this if it's present.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param u64Arg Some simple constant argument.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pSession The session of the caller.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @remarks Assume called with interrupts _enabled_.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsyncstatic int vmmR0EntryExWorker(PVM pVM, VMCPUID idCpu, VMMR0OPERATION enmOperation, PSUPVMMR0REQHDR pReqHdr, uint64_t u64Arg, PSUPDRVSESSION pSession)
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Common VM pointer validation.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync SUPR0Printf("vmmR0EntryExWorker: Invalid pVM=%p! (op=%d)\n", pVM, enmOperation);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync if (RT_UNLIKELY( pVM->enmVMState < VMSTATE_CREATING
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync SUPR0Printf("vmmR0EntryExWorker: Invalid pVM=%p:{enmVMState=%d, .pVMR0=%p}! (op=%d)\n",
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync if (RT_UNLIKELY(idCpu >= pVM->cCpus && idCpu != NIL_VMCPUID))
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync SUPR0Printf("vmmR0EntryExWorker: Invalid idCpu (%u vs cCpus=%u)\n", idCpu, pVM->cCpus);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync SUPR0Printf("vmmR0EntryExWorker: Invalid idCpu=%u\n", idCpu);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * GVM requests
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync return GVMMR0CreateVMReq((PGVMMCREATEVMREQ)pReqHdr);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync return GVMMR0SchedWakeUpAndPokeCpusReq(pVM, (PGVMMSCHEDWAKEUPANDPOKECPUSREQ)pReqHdr);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync return GVMMR0QueryStatisticsReq(pVM, (PGVMMQUERYSTATISTICSSREQ)pReqHdr);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync return GVMMR0ResetStatisticsReq(pVM, (PGVMMRESETSTATISTICSSREQ)pReqHdr);
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync * Initialize the R0 part of a VM instance.
ffb50166c9adb4ae583b914d405197035cf890advboxsync * Terminate the R0 part of a VM instance.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Attempt to enable hwacc mode and check the current setting.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Setup the hardware accelerated session.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Switch to RC to execute Hypervisor function.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* Safety precaution as HWACCM can disable the switcher. */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* We might need to disable VT-x if the active switcher turns off paging. */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /* Re-enable VT-x if previously turned off. */
b4d7b4dbcc45b8bde7502aa129440d92d7ffd038vboxsync /** @todo dispatch interrupts? */
b4d7b4dbcc45b8bde7502aa129440d92d7ffd038vboxsync * PGM wrappers.
b4d7b4dbcc45b8bde7502aa129440d92d7ffd038vboxsync return PGMR0PhysAllocateHandyPages(pVM, &pVM->aCpus[idCpu]);
b4d7b4dbcc45b8bde7502aa129440d92d7ffd038vboxsync return PGMR0PhysAllocateLargeHandyPage(pVM, &pVM->aCpus[idCpu]);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * GMM wrappers.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync return GMMR0InitialReservationReq(pVM, idCpu, (PGMMINITIALRESERVATIONREQ)pReqHdr);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync return GMMR0UpdateReservationReq(pVM, idCpu, (PGMMUPDATERESERVATIONREQ)pReqHdr);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync return GMMR0AllocatePagesReq(pVM, idCpu, (PGMMALLOCATEPAGESREQ)pReqHdr);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync return GMMR0FreePagesReq(pVM, idCpu, (PGMMFREEPAGESREQ)pReqHdr);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync return GMMR0FreeLargePageReq(pVM, idCpu, (PGMMFREELARGEPAGEREQ)pReqHdr);
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync return GMMR0BalloonedPagesReq(pVM, idCpu, (PGMMBALLOONEDPAGESREQ)pReqHdr);
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync return GMMR0MapUnmapChunkReq(pVM, idCpu, (PGMMMAPUNMAPCHUNKREQ)pReqHdr);
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync return GMMR0SeedChunk(pVM, idCpu, (RTR3PTR)u64Arg);
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * A quick GCFGM mock-up.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync /** @todo GCFGM with proper access control, ring-3 management interface and all that. */
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync if (pVM || !pReqHdr || u64Arg || idCpu != NIL_VMCPUID)
6475559a7e0e52892efbab4fbdedc879f6866109vboxsync rc = GVMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
0dd3967035b8a02985920baa57f948dc542b9388vboxsync //if (rc == VERR_CFGM_VALUE_NOT_FOUND)
0dd3967035b8a02985920baa57f948dc542b9388vboxsync // rc = GMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
6475559a7e0e52892efbab4fbdedc879f6866109vboxsync rc = GVMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
6475559a7e0e52892efbab4fbdedc879f6866109vboxsync //if (rc == VERR_CFGM_VALUE_NOT_FOUND)
6475559a7e0e52892efbab4fbdedc879f6866109vboxsync // rc = GMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync * Requests to the internal networking service.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync if (u64Arg || !pReq || !vmmR0IsValidSession(pVM, pReq->pSession, pSession) || idCpu != NIL_VMCPUID)
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFCLOSEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
f75c6db919d277952ca03b7acf643e5e3ac96cafvboxsync return INTNETR0IfCloseReq(g_pIntNet, pSession, (PINTNETIFCLOSEREQ)pReqHdr);
0dd3967035b8a02985920baa57f948dc542b9388vboxsync if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFGETRING3BUFFERREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync return INTNETR0IfGetRing3BufferReq(g_pIntNet, pSession, (PINTNETIFGETRING3BUFFERREQ)pReqHdr);
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETPROMISCUOUSMODEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
3c6306a66deef467e3c13483dd6529e1e1c6b822vboxsync return INTNETR0IfSetPromiscuousModeReq(g_pIntNet, pSession, (PINTNETIFSETPROMISCUOUSMODEREQ)pReqHdr);
f75c6db919d277952ca03b7acf643e5e3ac96cafvboxsync if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETMACADDRESSREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
f75c6db919d277952ca03b7acf643e5e3ac96cafvboxsync return INTNETR0IfSetMacAddressReq(g_pIntNet, pSession, (PINTNETIFSETMACADDRESSREQ)pReqHdr);
f75c6db919d277952ca03b7acf643e5e3ac96cafvboxsync if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETACTIVEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
f75c6db919d277952ca03b7acf643e5e3ac96cafvboxsync return INTNETR0IfSetActiveReq(g_pIntNet, pSession, (PINTNETIFSETACTIVEREQ)pReqHdr);
f75c6db919d277952ca03b7acf643e5e3ac96cafvboxsync if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSENDREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
f75c6db919d277952ca03b7acf643e5e3ac96cafvboxsync return INTNETR0IfSendReq(g_pIntNet, pSession, (PINTNETIFSENDREQ)pReqHdr);
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFWAITREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync return INTNETR0IfWaitReq(g_pIntNet, pSession, (PINTNETIFWAITREQ)pReqHdr);
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync * For profiling.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * For testing Ring-0 APIs invoked in this environment.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync /** @todo make new test */
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync#if HC_ARCH_BITS == 32 && defined(VBOX_WITH_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * We're returning VERR_NOT_SUPPORT here so we've got something else
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * than -1 which the interrupt gate glue code might return.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync Log(("operation %#x is not supported\n", enmOperation));
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Argument for vmmR0EntryExWrapper containing the arguments for VMMR0EntryEx.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync/** Pointer to a vmmR0EntryExWrapper argument package. */
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * This is just a longjmp wrapper function for VMMR0EntryEx calls.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @returns VBox status code.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @param pvArgs The argument package
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync return vmmR0EntryExWorker(((PVMMR0ENTRYEXARGS)pvArgs)->pVM,
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * The Ring 0 entry point, called by the support library (SUP).
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @returns VBox status code.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @param pVM The VM to operate on.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param idCpu Virtual CPU ID argument. Must be NIL_VMCPUID if pVM
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * is NIL_RTR0PTR, and may be NIL_VMCPUID if it isn't
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param enmOperation Which operation to execute.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @param pReq This points to a SUPVMMR0REQHDR packet. Optional.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @param u64Arg Some simple constant argument.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @param pSession The session of the caller.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @remarks Assume called with interrupts _enabled_.
ffb50166c9adb4ae583b914d405197035cf890advboxsyncVMMR0DECL(int) VMMR0EntryEx(PVM pVM, VMCPUID idCpu, VMMR0OPERATION enmOperation, PSUPVMMR0REQHDR pReq, uint64_t u64Arg, PSUPDRVSESSION pSession)
ffb50166c9adb4ae583b914d405197035cf890advboxsync * Requests that should only happen on the EMT thread will be
ffb50166c9adb4ae583b914d405197035cf890advboxsync * wrapped in a setjmp so we can assert without causing trouble.
ffb50166c9adb4ae583b914d405197035cf890advboxsync /* These might/will be called before VMMR3Init. */
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync /* On the mac we might not have a valid jmp buf, so check these as well. */
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync /** @todo validate this EMT claim... GVM knows. */
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync return vmmR0CallRing3SetJmpEx(&pVCpu->vmm.s.CallRing3JmpBufR0, vmmR0EntryExWrapper, &Args);
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync return vmmR0EntryExWorker(pVM, idCpu, enmOperation, pReq, u64Arg, pSession);
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * Internal R0 logger worker: Flush logger.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @param pLogger The logger instance to flush.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * @remark This function must be exported!
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * Convert the pLogger into a VM handle and 'call' back to Ring-3.
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync * (This is a bit paranoid code.)
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync PVMMR0LOGGER pR0Logger = (PVMMR0LOGGER)((uintptr_t)pLogger - RT_OFFSETOF(VMMR0LOGGER, Logger));
ffb50166c9adb4ae583b914d405197035cf890advboxsync SUPR0Printf("vmmR0LoggerFlush: pLogger=%p!\n", pLogger);
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync return; /* quietly */
ffb50166c9adb4ae583b914d405197035cf890advboxsync SUPR0Printf("vmmR0LoggerFlush: pVM=%p! pVMR0=%p! pLogger=%p\n", pVM, pVM->pVMR0, pLogger);
ffb50166c9adb4ae583b914d405197035cf890advboxsync * Check that the jump buffer is armed.
ffb50166c9adb4ae583b914d405197035cf890advboxsync SUPR0Printf("vmmR0LoggerFlush: Jump buffer isn't armed!\n");
ffb50166c9adb4ae583b914d405197035cf890advboxsync VMMRZCallRing3(pVM, pVCpu, VMMCALLRING3_VMM_LOGGER_FLUSH, 0);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Interal R0 logger worker: Custom prefix.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * @returns Number of chars written.
ffb50166c9adb4ae583b914d405197035cf890advboxsync * @param pLogger The logger instance.
ffb50166c9adb4ae583b914d405197035cf890advboxsync * @param pchBuf The output buffer.
ffb50166c9adb4ae583b914d405197035cf890advboxsync * @param cchBuf The size of the buffer.
ffb50166c9adb4ae583b914d405197035cf890advboxsync * @param pvUser User argument (ignored).
ffb50166c9adb4ae583b914d405197035cf890advboxsyncVMMR0DECL(size_t) vmmR0LoggerPrefix(PRTLOGGER pLogger, char *pchBuf, size_t cchBuf, void *pvUser)
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync PVMMR0LOGGER pR0Logger = (PVMMR0LOGGER)((uintptr_t)pLogger - RT_OFFSETOF(VMMR0LOGGER, Logger));
c89333d3e41e439ed9e74768000edc399d3e72e6vboxsync static const char s_szHex[17] = "0123456789abcdef";
ffb50166c9adb4ae583b914d405197035cf890advboxsync * Disables flushing of the ring-0 debug log.
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync * @param pVCpu The shared virtual cpu structure.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled = true;
ffb50166c9adb4ae583b914d405197035cf890advboxsync * Enables flushing of the ring-0 debug log.
ffb50166c9adb4ae583b914d405197035cf890advboxsync * @param pVCpu The shared virtual cpu structure.
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled = false;
ffb50166c9adb4ae583b914d405197035cf890advboxsync * Jump back to ring-3 if we're the EMT and the longjmp is armed.
ffb50166c9adb4ae583b914d405197035cf890advboxsync * @returns true if the breakpoint should be hit, false if it should be ignored.
750df3fe104e01cadbc3d5bd20243055d283d4e5vboxsync return true;
6475559a7e0e52892efbab4fbdedc879f6866109vboxsync int rc = VMMRZCallRing3(pVM, pVCpu, VMMCALLRING3_VM_R0_ASSERTION, 0);
6475559a7e0e52892efbab4fbdedc879f6866109vboxsync return true;
6475559a7e0e52892efbab4fbdedc879f6866109vboxsync return false;
6475559a7e0e52892efbab4fbdedc879f6866109vboxsync * Override this so we can push it up to ring-3.
6475559a7e0e52892efbab4fbdedc879f6866109vboxsync * @param pszExpr Expression. Can be NULL.
ffb50166c9adb4ae583b914d405197035cf890advboxsync * @param uLine Location line number.
ffb50166c9adb4ae583b914d405197035cf890advboxsync * @param pszFile Location file name.
6475559a7e0e52892efbab4fbdedc879f6866109vboxsync * @param pszFunction Location function name.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsyncDECLEXPORT(void) RTCALL RTAssertMsg1Weak(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * To the log.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync "Expression: %s\n"
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync "Location : %s(%d) %s\n",
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * To the global VMM buffer.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync RTStrPrintf(pVM->vmm.s.szRing0AssertMsg1, sizeof(pVM->vmm.s.szRing0AssertMsg1),
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync "\n!!R0-Assertion Failed!!\n"
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync "Expression: %s\n"
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync "Location : %s(%d) %s\n",
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Continue the normal way.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync RTAssertMsg1(pszExpr, uLine, pszFile, pszFunction);
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * Callback for RTLogFormatV which writes to the ring-3 log port.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsync * See PFNLOGOUTPUT() for details.
553a2f0d8ef91a6dad8de4eef206ff093af53a5dvboxsyncstatic DECLCALLBACK(size_t) rtLogOutput(void *pv, const char *pachChars, size_t cbChars)
return cbChars;
if (pLog)
if (pVM)
RTStrPrintfV(pVM->vmm.s.szRing0AssertMsg2, sizeof(pVM->vmm.s.szRing0AssertMsg2), pszFormat, vaCopy);