[23] | 1 | /* $Id: VMEmt.cpp 46420 2013-06-06 16:27:25Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
| 3 | * VM - Virtual Machine, The Emulation Thread.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[44528] | 7 | * Copyright (C) 2006-2013 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
|
---|
[35346] | 23 | #include <VBox/vmm/tm.h>
|
---|
| 24 | #include <VBox/vmm/dbgf.h>
|
---|
| 25 | #include <VBox/vmm/em.h>
|
---|
| 26 | #include <VBox/vmm/pdmapi.h>
|
---|
[40274] | 27 | #ifdef VBOX_WITH_REM
|
---|
| 28 | # include <VBox/vmm/rem.h>
|
---|
| 29 | #endif
|
---|
[35346] | 30 | #include <VBox/vmm/tm.h>
|
---|
[1] | 31 | #include "VMInternal.h"
|
---|
[35346] | 32 | #include <VBox/vmm/vm.h>
|
---|
| 33 | #include <VBox/vmm/uvm.h>
|
---|
[1] | 34 |
|
---|
| 35 | #include <VBox/err.h>
|
---|
| 36 | #include <VBox/log.h>
|
---|
| 37 | #include <iprt/assert.h>
|
---|
| 38 | #include <iprt/asm.h>
|
---|
[29250] | 39 | #include <iprt/asm-math.h>
|
---|
[1] | 40 | #include <iprt/semaphore.h>
|
---|
[2984] | 41 | #include <iprt/string.h>
|
---|
[1] | 42 | #include <iprt/thread.h>
|
---|
[2283] | 43 | #include <iprt/time.h>
|
---|
[1] | 44 |
|
---|
[19400] | 45 |
|
---|
[19395] | 46 | /*******************************************************************************
|
---|
| 47 | * Internal Functions *
|
---|
| 48 | *******************************************************************************/
|
---|
| 49 | int vmR3EmulationThreadWithId(RTTHREAD ThreadSelf, PUVMCPU pUVCpu, VMCPUID idCpu);
|
---|
[1] | 50 |
|
---|
[19395] | 51 |
|
---|
[1] | 52 | /**
|
---|
[19395] | 53 | * The emulation thread main function.
|
---|
[1] | 54 | *
|
---|
| 55 | * @returns Thread exit code.
|
---|
| 56 | * @param ThreadSelf The handle to the executing thread.
|
---|
[19395] | 57 | * @param pvArgs Pointer to the user mode per-VCpu structure (UVMPCU).
|
---|
[1] | 58 | */
|
---|
| 59 | DECLCALLBACK(int) vmR3EmulationThread(RTTHREAD ThreadSelf, void *pvArgs)
|
---|
| 60 | {
|
---|
[19217] | 61 | PUVMCPU pUVCpu = (PUVMCPU)pvArgs;
|
---|
[19395] | 62 | return vmR3EmulationThreadWithId(ThreadSelf, pUVCpu, pUVCpu->idCpu);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
| 67 | * The emulation thread main function, with Virtual CPU ID for debugging.
|
---|
| 68 | *
|
---|
| 69 | * @returns Thread exit code.
|
---|
| 70 | * @param ThreadSelf The handle to the executing thread.
|
---|
| 71 | * @param pUVCpu Pointer to the user mode per-VCpu structure.
|
---|
| 72 | * @param idCpu The virtual CPU ID, for backtrace purposes.
|
---|
| 73 | */
|
---|
| 74 | int vmR3EmulationThreadWithId(RTTHREAD ThreadSelf, PUVMCPU pUVCpu, VMCPUID idCpu)
|
---|
| 75 | {
|
---|
| 76 | PUVM pUVM = pUVCpu->pUVM;
|
---|
[13796] | 77 | int rc;
|
---|
[13782] | 78 |
|
---|
[6796] | 79 | AssertReleaseMsg(VALID_PTR(pUVM) && pUVM->u32Magic == UVM_MAGIC,
|
---|
| 80 | ("Invalid arguments to the emulation thread!\n"));
|
---|
[1] | 81 |
|
---|
[19217] | 82 | rc = RTTlsSet(pUVM->vm.s.idxTLS, pUVCpu);
|
---|
[13796] | 83 | AssertReleaseMsgRCReturn(rc, ("RTTlsSet %x failed with %Rrc\n", pUVM->vm.s.idxTLS, rc), rc);
|
---|
[13782] | 84 |
|
---|
[36437] | 85 | if ( pUVM->pVmm2UserMethods
|
---|
| 86 | && pUVM->pVmm2UserMethods->pfnNotifyEmtInit)
|
---|
| 87 | pUVM->pVmm2UserMethods->pfnNotifyEmtInit(pUVM->pVmm2UserMethods, pUVM, pUVCpu);
|
---|
| 88 |
|
---|
[1] | 89 | /*
|
---|
| 90 | * The request loop.
|
---|
| 91 | */
|
---|
[13796] | 92 | rc = VINF_SUCCESS;
|
---|
[6796] | 93 | Log(("vmR3EmulationThread: Emulation thread starting the days work... Thread=%#x pUVM=%p\n", ThreadSelf, pUVM));
|
---|
[19435] | 94 | VMSTATE enmBefore = VMSTATE_CREATED; /* (only used for logging atm.) */
|
---|
[1] | 95 | for (;;)
|
---|
| 96 | {
|
---|
| 97 | /*
|
---|
[6796] | 98 | * During early init there is no pVM, so make a special path
|
---|
| 99 | * for that to keep things clearly separate.
|
---|
[1] | 100 | */
|
---|
[6796] | 101 | if (!pUVM->pVM)
|
---|
[1] | 102 | {
|
---|
| 103 | /*
|
---|
[6796] | 104 | * Check for termination first.
|
---|
[1] | 105 | */
|
---|
[6796] | 106 | if (pUVM->vm.s.fTerminateEMT)
|
---|
| 107 | {
|
---|
| 108 | rc = VINF_EM_TERMINATE;
|
---|
| 109 | break;
|
---|
| 110 | }
|
---|
[19217] | 111 |
|
---|
[19395] | 112 | /*
|
---|
| 113 | * Only the first VCPU may initialize the VM during early init
|
---|
| 114 | * and must therefore service all VMCPUID_ANY requests.
|
---|
| 115 | * See also VMR3Create
|
---|
| 116 | */
|
---|
[38838] | 117 | if ( (pUVM->vm.s.pNormalReqs || pUVM->vm.s.pPriorityReqs)
|
---|
[19395] | 118 | && pUVCpu->idCpu == 0)
|
---|
[6796] | 119 | {
|
---|
| 120 | /*
|
---|
[19395] | 121 | * Service execute in any EMT request.
|
---|
[6796] | 122 | */
|
---|
[38838] | 123 | rc = VMR3ReqProcessU(pUVM, VMCPUID_ANY, false /*fPriorityOnly*/);
|
---|
[23190] | 124 | Log(("vmR3EmulationThread: Req rc=%Rrc, VM state %s -> %s\n", rc, VMR3GetStateName(enmBefore), pUVM->pVM ? VMR3GetStateName(pUVM->pVM->enmVMState) : "CREATING"));
|
---|
[6796] | 125 | }
|
---|
[38838] | 126 | else if (pUVCpu->vm.s.pNormalReqs || pUVCpu->vm.s.pPriorityReqs)
|
---|
[6796] | 127 | {
|
---|
| 128 | /*
|
---|
[19395] | 129 | * Service execute in specific EMT request.
|
---|
[19217] | 130 | */
|
---|
[38838] | 131 | rc = VMR3ReqProcessU(pUVM, pUVCpu->idCpu, false /*fPriorityOnly*/);
|
---|
[23190] | 132 | Log(("vmR3EmulationThread: Req (cpu=%u) rc=%Rrc, VM state %s -> %s\n", pUVCpu->idCpu, rc, VMR3GetStateName(enmBefore), pUVM->pVM ? VMR3GetStateName(pUVM->pVM->enmVMState) : "CREATING"));
|
---|
[19217] | 133 | }
|
---|
| 134 | else
|
---|
| 135 | {
|
---|
| 136 | /*
|
---|
[6796] | 137 | * Nothing important is pending, so wait for something.
|
---|
| 138 | */
|
---|
[19217] | 139 | rc = VMR3WaitU(pUVCpu);
|
---|
[13816] | 140 | if (RT_FAILURE(rc))
|
---|
[20855] | 141 | {
|
---|
[20901] | 142 | AssertLogRelMsgFailed(("VMR3WaitU failed with %Rrc\n", rc));
|
---|
[6796] | 143 | break;
|
---|
[20855] | 144 | }
|
---|
[6796] | 145 | }
|
---|
[1] | 146 | }
|
---|
[6796] | 147 | else
|
---|
[1] | 148 | {
|
---|
| 149 | /*
|
---|
[6796] | 150 | * Pending requests which needs servicing?
|
---|
| 151 | *
|
---|
| 152 | * We check for state changes in addition to status codes when
|
---|
| 153 | * servicing requests. (Look after the ifs.)
|
---|
[1] | 154 | */
|
---|
[6796] | 155 | PVM pVM = pUVM->pVM;
|
---|
| 156 | enmBefore = pVM->enmVMState;
|
---|
[30473] | 157 | if (pUVM->vm.s.fTerminateEMT)
|
---|
[6796] | 158 | {
|
---|
| 159 | rc = VINF_EM_TERMINATE;
|
---|
| 160 | break;
|
---|
| 161 | }
|
---|
[23145] | 162 |
|
---|
[46420] | 163 | if (VM_FF_IS_PENDING(pVM, VM_FF_EMT_RENDEZVOUS))
|
---|
[6796] | 164 | {
|
---|
[23145] | 165 | rc = VMMR3EmtRendezvousFF(pVM, &pVM->aCpus[idCpu]);
|
---|
[23190] | 166 | Log(("vmR3EmulationThread: Rendezvous rc=%Rrc, VM state %s -> %s\n", rc, VMR3GetStateName(enmBefore), VMR3GetStateName(pVM->enmVMState)));
|
---|
[23145] | 167 | }
|
---|
[38838] | 168 | else if (pUVM->vm.s.pNormalReqs || pUVM->vm.s.pPriorityReqs)
|
---|
[23145] | 169 | {
|
---|
[6796] | 170 | /*
|
---|
[19395] | 171 | * Service execute in any EMT request.
|
---|
[6796] | 172 | */
|
---|
[38838] | 173 | rc = VMR3ReqProcessU(pUVM, VMCPUID_ANY, false /*fPriorityOnly*/);
|
---|
[23190] | 174 | Log(("vmR3EmulationThread: Req rc=%Rrc, VM state %s -> %s\n", rc, VMR3GetStateName(enmBefore), VMR3GetStateName(pVM->enmVMState)));
|
---|
[6796] | 175 | }
|
---|
[38838] | 176 | else if (pUVCpu->vm.s.pNormalReqs || pUVCpu->vm.s.pPriorityReqs)
|
---|
[19217] | 177 | {
|
---|
| 178 | /*
|
---|
[19395] | 179 | * Service execute in specific EMT request.
|
---|
[19217] | 180 | */
|
---|
[38838] | 181 | rc = VMR3ReqProcessU(pUVM, pUVCpu->idCpu, false /*fPriorityOnly*/);
|
---|
[23190] | 182 | Log(("vmR3EmulationThread: Req (cpu=%u) rc=%Rrc, VM state %s -> %s\n", pUVCpu->idCpu, rc, VMR3GetStateName(enmBefore), VMR3GetStateName(pVM->enmVMState)));
|
---|
[19217] | 183 | }
|
---|
[46420] | 184 | else if (VM_FF_IS_SET(pVM, VM_FF_DBGF))
|
---|
[6796] | 185 | {
|
---|
| 186 | /*
|
---|
| 187 | * Service the debugger request.
|
---|
| 188 | */
|
---|
| 189 | rc = DBGFR3VMMForcedAction(pVM);
|
---|
[23190] | 190 | Log(("vmR3EmulationThread: Dbg rc=%Rrc, VM state %s -> %s\n", rc, VMR3GetStateName(enmBefore), VMR3GetStateName(pVM->enmVMState)));
|
---|
[6796] | 191 | }
|
---|
[46420] | 192 | else if (VM_FF_TEST_AND_CLEAR(pVM, VM_FF_RESET))
|
---|
[6796] | 193 | {
|
---|
| 194 | /*
|
---|
| 195 | * Service a delayed reset request.
|
---|
| 196 | */
|
---|
[44340] | 197 | rc = VMR3Reset(pVM->pUVM);
|
---|
[6796] | 198 | VM_FF_CLEAR(pVM, VM_FF_RESET);
|
---|
[23190] | 199 | Log(("vmR3EmulationThread: Reset rc=%Rrc, VM state %s -> %s\n", rc, VMR3GetStateName(enmBefore), VMR3GetStateName(pVM->enmVMState)));
|
---|
[6796] | 200 | }
|
---|
| 201 | else
|
---|
| 202 | {
|
---|
| 203 | /*
|
---|
| 204 | * Nothing important is pending, so wait for something.
|
---|
| 205 | */
|
---|
[19217] | 206 | rc = VMR3WaitU(pUVCpu);
|
---|
[13816] | 207 | if (RT_FAILURE(rc))
|
---|
[20856] | 208 | {
|
---|
[20901] | 209 | AssertLogRelMsgFailed(("VMR3WaitU failed with %Rrc\n", rc));
|
---|
[6796] | 210 | break;
|
---|
[20856] | 211 | }
|
---|
[6796] | 212 | }
|
---|
| 213 |
|
---|
[1] | 214 | /*
|
---|
[6796] | 215 | * Check for termination requests, these have extremely high priority.
|
---|
[1] | 216 | */
|
---|
[6796] | 217 | if ( rc == VINF_EM_TERMINATE
|
---|
[30473] | 218 | || pUVM->vm.s.fTerminateEMT)
|
---|
[6796] | 219 | break;
|
---|
[6804] | 220 | }
|
---|
[6796] | 221 |
|
---|
[6804] | 222 | /*
|
---|
[19435] | 223 | * Some requests (both VMR3Req* and the DBGF) can potentially resume
|
---|
| 224 | * or start the VM, in that case we'll get a change in VM status
|
---|
| 225 | * indicating that we're now running.
|
---|
[6804] | 226 | */
|
---|
[13816] | 227 | if ( RT_SUCCESS(rc)
|
---|
[19435] | 228 | && pUVM->pVM)
|
---|
[6804] | 229 | {
|
---|
[23009] | 230 | PVM pVM = pUVM->pVM;
|
---|
| 231 | PVMCPU pVCpu = &pVM->aCpus[idCpu];
|
---|
[19435] | 232 | if ( pVM->enmVMState == VMSTATE_RUNNING
|
---|
| 233 | && VMCPUSTATE_IS_STARTED(VMCPU_GET_STATE(pVCpu)))
|
---|
| 234 | {
|
---|
| 235 | rc = EMR3ExecuteVM(pVM, pVCpu);
|
---|
| 236 | Log(("vmR3EmulationThread: EMR3ExecuteVM() -> rc=%Rrc, enmVMState=%d\n", rc, pVM->enmVMState));
|
---|
[23009] | 237 | if (EMGetState(pVCpu) == EMSTATE_GURU_MEDITATION)
|
---|
| 238 | vmR3SetGuruMeditation(pVM);
|
---|
[19435] | 239 | }
|
---|
[1] | 240 | }
|
---|
[6804] | 241 |
|
---|
[1] | 242 | } /* forever */
|
---|
| 243 |
|
---|
| 244 |
|
---|
| 245 | /*
|
---|
[23009] | 246 | * Cleanup and exit.
|
---|
[1] | 247 | */
|
---|
[13818] | 248 | Log(("vmR3EmulationThread: Terminating emulation thread! Thread=%#x pUVM=%p rc=%Rrc enmBefore=%d enmVMState=%d\n",
|
---|
[6796] | 249 | ThreadSelf, pUVM, rc, enmBefore, pUVM->pVM ? pUVM->pVM->enmVMState : VMSTATE_TERMINATED));
|
---|
[30473] | 250 | if ( idCpu == 0
|
---|
| 251 | && pUVM->pVM)
|
---|
[1] | 252 | {
|
---|
[30473] | 253 | PVM pVM = pUVM->pVM;
|
---|
| 254 | vmR3SetTerminated(pVM);
|
---|
| 255 | pUVM->pVM = NULL;
|
---|
| 256 |
|
---|
| 257 | /** @todo SMP: This isn't 100% safe. We should wait for the other
|
---|
| 258 | * threads to finish before destroy the VM. */
|
---|
| 259 | int rc2 = SUPR3CallVMMR0Ex(pVM->pVMR0, 0 /*idCpu*/, VMMR0_DO_GVMM_DESTROY_VM, 0, NULL);
|
---|
| 260 | AssertLogRelRC(rc2);
|
---|
[1] | 261 | }
|
---|
[30473] | 262 |
|
---|
[36437] | 263 | if ( pUVM->pVmm2UserMethods
|
---|
| 264 | && pUVM->pVmm2UserMethods->pfnNotifyEmtTerm)
|
---|
| 265 | pUVM->pVmm2UserMethods->pfnNotifyEmtTerm(pUVM->pVmm2UserMethods, pUVM, pUVCpu);
|
---|
| 266 |
|
---|
[30473] | 267 | pUVCpu->vm.s.NativeThreadEMT = NIL_RTNATIVETHREAD;
|
---|
[6796] | 268 | Log(("vmR3EmulationThread: EMT is terminated.\n"));
|
---|
[1] | 269 | return rc;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
[2984] | 272 |
|
---|
[247] | 273 | /**
|
---|
[5167] | 274 | * Gets the name of a halt method.
|
---|
| 275 | *
|
---|
| 276 | * @returns Pointer to a read only string.
|
---|
| 277 | * @param enmMethod The method.
|
---|
| 278 | */
|
---|
| 279 | static const char *vmR3GetHaltMethodName(VMHALTMETHOD enmMethod)
|
---|
| 280 | {
|
---|
| 281 | switch (enmMethod)
|
---|
| 282 | {
|
---|
[6796] | 283 | case VMHALTMETHOD_BOOTSTRAP: return "bootstrap";
|
---|
| 284 | case VMHALTMETHOD_DEFAULT: return "default";
|
---|
| 285 | case VMHALTMETHOD_OLD: return "old";
|
---|
| 286 | case VMHALTMETHOD_1: return "method1";
|
---|
| 287 | //case VMHALTMETHOD_2: return "method2";
|
---|
| 288 | case VMHALTMETHOD_GLOBAL_1: return "global1";
|
---|
| 289 | default: return "unknown";
|
---|
[5167] | 290 | }
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 |
|
---|
| 294 | /**
|
---|
[30473] | 295 | * Signal a fatal wait error.
|
---|
| 296 | *
|
---|
| 297 | * @returns Fatal error code to be propagated up the call stack.
|
---|
| 298 | * @param pUVCpu The user mode per CPU structure of the calling
|
---|
| 299 | * EMT.
|
---|
| 300 | * @param pszFmt The error format with a single %Rrc in it.
|
---|
| 301 | * @param rcFmt The status code to format.
|
---|
| 302 | */
|
---|
| 303 | static int vmR3FatalWaitError(PUVMCPU pUVCpu, const char *pszFmt, int rcFmt)
|
---|
| 304 | {
|
---|
| 305 | /** @todo This is wrong ... raise a fatal error / guru meditation
|
---|
| 306 | * instead. */
|
---|
| 307 | AssertLogRelMsgFailed((pszFmt, rcFmt));
|
---|
| 308 | ASMAtomicUoWriteBool(&pUVCpu->pUVM->vm.s.fTerminateEMT, true);
|
---|
| 309 | if (pUVCpu->pVM)
|
---|
| 310 | VM_FF_SET(pUVCpu->pVM, VM_FF_CHECK_VM_STATE);
|
---|
[39402] | 311 | return VERR_VM_FATAL_WAIT_ERROR;
|
---|
[30473] | 312 | }
|
---|
| 313 |
|
---|
| 314 |
|
---|
| 315 | /**
|
---|
[2830] | 316 | * The old halt loop.
|
---|
[1] | 317 | */
|
---|
[19217] | 318 | static DECLCALLBACK(int) vmR3HaltOldDoHalt(PUVMCPU pUVCpu, const uint32_t fMask, uint64_t /* u64Now*/)
|
---|
[1] | 319 | {
|
---|
| 320 | /*
|
---|
| 321 | * Halt loop.
|
---|
| 322 | */
|
---|
[19217] | 323 | PVM pVM = pUVCpu->pVM;
|
---|
| 324 | PVMCPU pVCpu = pUVCpu->pVCpu;
|
---|
| 325 |
|
---|
[1] | 326 | int rc = VINF_SUCCESS;
|
---|
[19217] | 327 | ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
|
---|
[2284] | 328 | //unsigned cLoops = 0;
|
---|
[1] | 329 | for (;;)
|
---|
| 330 | {
|
---|
| 331 | /*
|
---|
| 332 | * Work the timers and check if we can exit.
|
---|
| 333 | * The poll call gives us the ticks left to the next event in
|
---|
| 334 | * addition to perhaps set an FF.
|
---|
| 335 | */
|
---|
[33170] | 336 | uint64_t const u64StartTimers = RTTimeNanoTS();
|
---|
[1] | 337 | TMR3TimerQueuesDo(pVM);
|
---|
[33170] | 338 | uint64_t const cNsElapsedTimers = RTTimeNanoTS() - u64StartTimers;
|
---|
[33172] | 339 | STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltTimers, cNsElapsedTimers);
|
---|
[46420] | 340 | if ( VM_FF_IS_PENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
|
---|
| 341 | || VMCPU_FF_IS_PENDING(pVCpu, fMask))
|
---|
[1] | 342 | break;
|
---|
[19821] | 343 | uint64_t u64NanoTS;
|
---|
| 344 | TMTimerPollGIP(pVM, pVCpu, &u64NanoTS);
|
---|
[46420] | 345 | if ( VM_FF_IS_PENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
|
---|
| 346 | || VMCPU_FF_IS_PENDING(pVCpu, fMask))
|
---|
[1] | 347 | break;
|
---|
| 348 |
|
---|
| 349 | /*
|
---|
| 350 | * Wait for a while. Someone will wake us up or interrupt the call if
|
---|
| 351 | * anything needs our attention.
|
---|
| 352 | */
|
---|
| 353 | if (u64NanoTS < 50000)
|
---|
| 354 | {
|
---|
[2284] | 355 | //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d spin\n", u64NanoTS, cLoops++);
|
---|
[1] | 356 | /* spin */;
|
---|
| 357 | }
|
---|
| 358 | else
|
---|
| 359 | {
|
---|
| 360 | VMMR3YieldStop(pVM);
|
---|
[2284] | 361 | //uint64_t u64Start = RTTimeNanoTS();
|
---|
[1] | 362 | if (u64NanoTS < 870000) /* this is a bit speculative... works fine on linux. */
|
---|
| 363 | {
|
---|
[2284] | 364 | //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d yield", u64NanoTS, cLoops++);
|
---|
[33170] | 365 | uint64_t const u64StartSchedYield = RTTimeNanoTS();
|
---|
[1] | 366 | RTThreadYield(); /* this is the best we can do here */
|
---|
[33170] | 367 | uint64_t const cNsElapsedSchedYield = RTTimeNanoTS() - u64StartSchedYield;
|
---|
| 368 | STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltYield, cNsElapsedSchedYield);
|
---|
[1] | 369 | }
|
---|
| 370 | else if (u64NanoTS < 2000000)
|
---|
| 371 | {
|
---|
[2284] | 372 | //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d sleep 1ms", u64NanoTS, cLoops++);
|
---|
[33170] | 373 | uint64_t const u64StartSchedHalt = RTTimeNanoTS();
|
---|
[19217] | 374 | rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, 1);
|
---|
[33170] | 375 | uint64_t const cNsElapsedSchedHalt = RTTimeNanoTS() - u64StartSchedHalt;
|
---|
| 376 | STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltBlock, cNsElapsedSchedHalt);
|
---|
[1] | 377 | }
|
---|
| 378 | else
|
---|
| 379 | {
|
---|
[2284] | 380 | //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d sleep %dms", u64NanoTS, cLoops++, (uint32_t)RT_MIN((u64NanoTS - 500000) / 1000000, 15));
|
---|
[33170] | 381 | uint64_t const u64StartSchedHalt = RTTimeNanoTS();
|
---|
[19217] | 382 | rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, RT_MIN((u64NanoTS - 1000000) / 1000000, 15));
|
---|
[33170] | 383 | uint64_t const cNsElapsedSchedHalt = RTTimeNanoTS() - u64StartSchedHalt;
|
---|
| 384 | STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltBlock, cNsElapsedSchedHalt);
|
---|
[1] | 385 | }
|
---|
[2284] | 386 | //uint64_t u64Slept = RTTimeNanoTS() - u64Start;
|
---|
[13818] | 387 | //RTLogPrintf(" -> rc=%Rrc in %RU64 ns / %RI64 ns delta\n", rc, u64Slept, u64NanoTS - u64Slept);
|
---|
[1] | 388 | }
|
---|
| 389 | if (rc == VERR_TIMEOUT)
|
---|
| 390 | rc = VINF_SUCCESS;
|
---|
[13816] | 391 | else if (RT_FAILURE(rc))
|
---|
[1] | 392 | {
|
---|
[30473] | 393 | rc = vmR3FatalWaitError(pUVCpu, "RTSemEventWait->%Rrc\n", rc);
|
---|
[1] | 394 | break;
|
---|
| 395 | }
|
---|
| 396 | }
|
---|
| 397 |
|
---|
[19217] | 398 | ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
|
---|
[2830] | 399 | return rc;
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 |
|
---|
| 403 | /**
|
---|
[2984] | 404 | * Initialize the configuration of halt method 1 & 2.
|
---|
| 405 | *
|
---|
| 406 | * @return VBox status code. Failure on invalid CFGM data.
|
---|
[41783] | 407 | * @param pVM Pointer to the VM.
|
---|
[2984] | 408 | */
|
---|
[6796] | 409 | static int vmR3HaltMethod12ReadConfigU(PUVM pUVM)
|
---|
[2984] | 410 | {
|
---|
| 411 | /*
|
---|
| 412 | * The defaults.
|
---|
| 413 | */
|
---|
[5080] | 414 | #if 1 /* DEBUGGING STUFF - REMOVE LATER */
|
---|
[6796] | 415 | pUVM->vm.s.Halt.Method12.u32LagBlockIntervalDivisorCfg = 4;
|
---|
| 416 | pUVM->vm.s.Halt.Method12.u32MinBlockIntervalCfg = 2*1000000;
|
---|
| 417 | pUVM->vm.s.Halt.Method12.u32MaxBlockIntervalCfg = 75*1000000;
|
---|
| 418 | pUVM->vm.s.Halt.Method12.u32StartSpinningCfg = 30*1000000;
|
---|
| 419 | pUVM->vm.s.Halt.Method12.u32StopSpinningCfg = 20*1000000;
|
---|
[5080] | 420 | #else
|
---|
[6796] | 421 | pUVM->vm.s.Halt.Method12.u32LagBlockIntervalDivisorCfg = 4;
|
---|
| 422 | pUVM->vm.s.Halt.Method12.u32MinBlockIntervalCfg = 5*1000000;
|
---|
| 423 | pUVM->vm.s.Halt.Method12.u32MaxBlockIntervalCfg = 200*1000000;
|
---|
| 424 | pUVM->vm.s.Halt.Method12.u32StartSpinningCfg = 20*1000000;
|
---|
| 425 | pUVM->vm.s.Halt.Method12.u32StopSpinningCfg = 2*1000000;
|
---|
[5080] | 426 | #endif
|
---|
[2984] | 427 |
|
---|
| 428 | /*
|
---|
| 429 | * Query overrides.
|
---|
[5080] | 430 | *
|
---|
[33540] | 431 | * I don't have time to bother with niceties such as invalid value checks
|
---|
[5080] | 432 | * here right now. sorry.
|
---|
[2984] | 433 | */
|
---|
[6796] | 434 | PCFGMNODE pCfg = CFGMR3GetChild(CFGMR3GetRoot(pUVM->pVM), "/VMM/HaltedMethod1");
|
---|
[2984] | 435 | if (pCfg)
|
---|
| 436 | {
|
---|
[5080] | 437 | uint32_t u32;
|
---|
| 438 | if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "LagBlockIntervalDivisor", &u32)))
|
---|
[6796] | 439 | pUVM->vm.s.Halt.Method12.u32LagBlockIntervalDivisorCfg = u32;
|
---|
[5080] | 440 | if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "MinBlockInterval", &u32)))
|
---|
[6796] | 441 | pUVM->vm.s.Halt.Method12.u32MinBlockIntervalCfg = u32;
|
---|
[5080] | 442 | if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "MaxBlockInterval", &u32)))
|
---|
[6796] | 443 | pUVM->vm.s.Halt.Method12.u32MaxBlockIntervalCfg = u32;
|
---|
[5080] | 444 | if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "StartSpinning", &u32)))
|
---|
[6796] | 445 | pUVM->vm.s.Halt.Method12.u32StartSpinningCfg = u32;
|
---|
[5080] | 446 | if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "StopSpinning", &u32)))
|
---|
[6796] | 447 | pUVM->vm.s.Halt.Method12.u32StopSpinningCfg = u32;
|
---|
[5080] | 448 | LogRel(("HaltedMethod1 config: %d/%d/%d/%d/%d\n",
|
---|
[6796] | 449 | pUVM->vm.s.Halt.Method12.u32LagBlockIntervalDivisorCfg,
|
---|
| 450 | pUVM->vm.s.Halt.Method12.u32MinBlockIntervalCfg,
|
---|
| 451 | pUVM->vm.s.Halt.Method12.u32MaxBlockIntervalCfg,
|
---|
| 452 | pUVM->vm.s.Halt.Method12.u32StartSpinningCfg,
|
---|
| 453 | pUVM->vm.s.Halt.Method12.u32StopSpinningCfg));
|
---|
[2984] | 454 | }
|
---|
| 455 |
|
---|
| 456 | return VINF_SUCCESS;
|
---|
| 457 | }
|
---|
| 458 |
|
---|
| 459 |
|
---|
| 460 | /**
|
---|
| 461 | * Initialize halt method 1.
|
---|
| 462 | *
|
---|
| 463 | * @return VBox status code.
|
---|
[6796] | 464 | * @param pUVM Pointer to the user mode VM structure.
|
---|
[2984] | 465 | */
|
---|
[6796] | 466 | static DECLCALLBACK(int) vmR3HaltMethod1Init(PUVM pUVM)
|
---|
[2984] | 467 | {
|
---|
[6796] | 468 | return vmR3HaltMethod12ReadConfigU(pUVM);
|
---|
[2984] | 469 | }
|
---|
| 470 |
|
---|
| 471 |
|
---|
| 472 | /**
|
---|
[2965] | 473 | * Method 1 - Block whenever possible, and when lagging behind
|
---|
[33540] | 474 | * switch to spinning for 10-30ms with occasional blocking until
|
---|
[2830] | 475 | * the lag has been eliminated.
|
---|
| 476 | */
|
---|
[19217] | 477 | static DECLCALLBACK(int) vmR3HaltMethod1Halt(PUVMCPU pUVCpu, const uint32_t fMask, uint64_t u64Now)
|
---|
[2830] | 478 | {
|
---|
[19217] | 479 | PUVM pUVM = pUVCpu->pUVM;
|
---|
| 480 | PVMCPU pVCpu = pUVCpu->pVCpu;
|
---|
| 481 | PVM pVM = pUVCpu->pVM;
|
---|
[6796] | 482 |
|
---|
[1] | 483 | /*
|
---|
[2984] | 484 | * To simplify things, we decide up-front whether we should switch to spinning or
|
---|
| 485 | * not. This makes some ASSUMPTIONS about the cause of the spinning (PIT/RTC/PCNet)
|
---|
| 486 | * and that it will generate interrupts or other events that will cause us to exit
|
---|
| 487 | * the halt loop.
|
---|
[1] | 488 | */
|
---|
[2830] | 489 | bool fBlockOnce = false;
|
---|
| 490 | bool fSpinning = false;
|
---|
[2965] | 491 | uint32_t u32CatchUpPct = TMVirtualSyncGetCatchUpPct(pVM);
|
---|
| 492 | if (u32CatchUpPct /* non-zero if catching up */)
|
---|
[2830] | 493 | {
|
---|
[19217] | 494 | if (pUVCpu->vm.s.Halt.Method12.u64StartSpinTS)
|
---|
[2830] | 495 | {
|
---|
[6796] | 496 | fSpinning = TMVirtualSyncGetLag(pVM) >= pUVM->vm.s.Halt.Method12.u32StopSpinningCfg;
|
---|
[2965] | 497 | if (fSpinning)
|
---|
| 498 | {
|
---|
| 499 | uint64_t u64Lag = TMVirtualSyncGetLag(pVM);
|
---|
[19217] | 500 | fBlockOnce = u64Now - pUVCpu->vm.s.Halt.Method12.u64LastBlockTS
|
---|
[6796] | 501 | > RT_MAX(pUVM->vm.s.Halt.Method12.u32MinBlockIntervalCfg,
|
---|
| 502 | RT_MIN(u64Lag / pUVM->vm.s.Halt.Method12.u32LagBlockIntervalDivisorCfg,
|
---|
| 503 | pUVM->vm.s.Halt.Method12.u32MaxBlockIntervalCfg));
|
---|
[2965] | 504 | }
|
---|
| 505 | else
|
---|
| 506 | {
|
---|
[19217] | 507 | //RTLogRelPrintf("Stopped spinning (%u ms)\n", (u64Now - pUVCpu->vm.s.Halt.Method12.u64StartSpinTS) / 1000000);
|
---|
| 508 | pUVCpu->vm.s.Halt.Method12.u64StartSpinTS = 0;
|
---|
[2965] | 509 | }
|
---|
| 510 | }
|
---|
| 511 | else
|
---|
| 512 | {
|
---|
[6796] | 513 | fSpinning = TMVirtualSyncGetLag(pVM) >= pUVM->vm.s.Halt.Method12.u32StartSpinningCfg;
|
---|
[2965] | 514 | if (fSpinning)
|
---|
[19217] | 515 | pUVCpu->vm.s.Halt.Method12.u64StartSpinTS = u64Now;
|
---|
[2830] | 516 | }
|
---|
| 517 | }
|
---|
[19217] | 518 | else if (pUVCpu->vm.s.Halt.Method12.u64StartSpinTS)
|
---|
[2965] | 519 | {
|
---|
[19217] | 520 | //RTLogRelPrintf("Stopped spinning (%u ms)\n", (u64Now - pUVCpu->vm.s.Halt.Method12.u64StartSpinTS) / 1000000);
|
---|
| 521 | pUVCpu->vm.s.Halt.Method12.u64StartSpinTS = 0;
|
---|
[2965] | 522 | }
|
---|
[1] | 523 |
|
---|
[2830] | 524 | /*
|
---|
| 525 | * Halt loop.
|
---|
| 526 | */
|
---|
| 527 | int rc = VINF_SUCCESS;
|
---|
[19217] | 528 | ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
|
---|
[2830] | 529 | unsigned cLoops = 0;
|
---|
| 530 | for (;; cLoops++)
|
---|
| 531 | {
|
---|
| 532 | /*
|
---|
[2965] | 533 | * Work the timers and check if we can exit.
|
---|
[2830] | 534 | */
|
---|
[33170] | 535 | uint64_t const u64StartTimers = RTTimeNanoTS();
|
---|
[2830] | 536 | TMR3TimerQueuesDo(pVM);
|
---|
[33170] | 537 | uint64_t const cNsElapsedTimers = RTTimeNanoTS() - u64StartTimers;
|
---|
[33172] | 538 | STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltTimers, cNsElapsedTimers);
|
---|
[46420] | 539 | if ( VM_FF_IS_PENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
|
---|
| 540 | || VMCPU_FF_IS_PENDING(pVCpu, fMask))
|
---|
[2830] | 541 | break;
|
---|
[1] | 542 |
|
---|
[2830] | 543 | /*
|
---|
| 544 | * Estimate time left to the next event.
|
---|
| 545 | */
|
---|
[19821] | 546 | uint64_t u64NanoTS;
|
---|
| 547 | TMTimerPollGIP(pVM, pVCpu, &u64NanoTS);
|
---|
[46420] | 548 | if ( VM_FF_IS_PENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
|
---|
| 549 | || VMCPU_FF_IS_PENDING(pVCpu, fMask))
|
---|
[2830] | 550 | break;
|
---|
| 551 |
|
---|
| 552 | /*
|
---|
| 553 | * Block if we're not spinning and the interval isn't all that small.
|
---|
| 554 | */
|
---|
[2965] | 555 | if ( ( !fSpinning
|
---|
[2830] | 556 | || fBlockOnce)
|
---|
[5080] | 557 | #if 1 /* DEBUGGING STUFF - REMOVE LATER */
|
---|
| 558 | && u64NanoTS >= 100000) /* 0.100 ms */
|
---|
| 559 | #else
|
---|
[2830] | 560 | && u64NanoTS >= 250000) /* 0.250 ms */
|
---|
[5080] | 561 | #endif
|
---|
[2830] | 562 | {
|
---|
[19217] | 563 | const uint64_t Start = pUVCpu->vm.s.Halt.Method12.u64LastBlockTS = RTTimeNanoTS();
|
---|
[2830] | 564 | VMMR3YieldStop(pVM);
|
---|
| 565 |
|
---|
| 566 | uint32_t cMilliSecs = RT_MIN(u64NanoTS / 1000000, 15);
|
---|
[19217] | 567 | if (cMilliSecs <= pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLongAvg)
|
---|
[2830] | 568 | cMilliSecs = 1;
|
---|
[2965] | 569 | else
|
---|
[19217] | 570 | cMilliSecs -= pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLongAvg;
|
---|
[33170] | 571 |
|
---|
[2984] | 572 | //RTLogRelPrintf("u64NanoTS=%RI64 cLoops=%3d sleep %02dms (%7RU64) ", u64NanoTS, cLoops, cMilliSecs, u64NanoTS);
|
---|
[33170] | 573 | uint64_t const u64StartSchedHalt = RTTimeNanoTS();
|
---|
[19217] | 574 | rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, cMilliSecs);
|
---|
[33170] | 575 | uint64_t const cNsElapsedSchedHalt = RTTimeNanoTS() - u64StartSchedHalt;
|
---|
| 576 | STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltBlock, cNsElapsedSchedHalt);
|
---|
| 577 |
|
---|
[2830] | 578 | if (rc == VERR_TIMEOUT)
|
---|
| 579 | rc = VINF_SUCCESS;
|
---|
[13816] | 580 | else if (RT_FAILURE(rc))
|
---|
[2830] | 581 | {
|
---|
[30473] | 582 | rc = vmR3FatalWaitError(pUVCpu, "RTSemEventWait->%Rrc\n", rc);
|
---|
[2830] | 583 | break;
|
---|
| 584 | }
|
---|
| 585 |
|
---|
[2965] | 586 | /*
|
---|
| 587 | * Calc the statistics.
|
---|
[2830] | 588 | * Update averages every 16th time, and flush parts of the history every 64th time.
|
---|
| 589 | */
|
---|
| 590 | const uint64_t Elapsed = RTTimeNanoTS() - Start;
|
---|
[19217] | 591 | pUVCpu->vm.s.Halt.Method12.cNSBlocked += Elapsed;
|
---|
[2830] | 592 | if (Elapsed > u64NanoTS)
|
---|
[19217] | 593 | pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLong += Elapsed - u64NanoTS;
|
---|
| 594 | pUVCpu->vm.s.Halt.Method12.cBlocks++;
|
---|
| 595 | if (!(pUVCpu->vm.s.Halt.Method12.cBlocks & 0xf))
|
---|
[2830] | 596 | {
|
---|
[19217] | 597 | pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLongAvg = pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLong / pUVCpu->vm.s.Halt.Method12.cBlocks;
|
---|
| 598 | if (!(pUVCpu->vm.s.Halt.Method12.cBlocks & 0x3f))
|
---|
[2830] | 599 | {
|
---|
[19217] | 600 | pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLong = pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLongAvg * 0x40;
|
---|
| 601 | pUVCpu->vm.s.Halt.Method12.cBlocks = 0x40;
|
---|
[2830] | 602 | }
|
---|
| 603 | }
|
---|
[2984] | 604 | //RTLogRelPrintf(" -> %7RU64 ns / %7RI64 ns delta%s\n", Elapsed, Elapsed - u64NanoTS, fBlockOnce ? " (block once)" : "");
|
---|
[2830] | 605 |
|
---|
| 606 | /*
|
---|
| 607 | * Clear the block once flag if we actually blocked.
|
---|
| 608 | */
|
---|
| 609 | if ( fBlockOnce
|
---|
| 610 | && Elapsed > 100000 /* 0.1 ms */)
|
---|
| 611 | fBlockOnce = false;
|
---|
| 612 | }
|
---|
| 613 | }
|
---|
[2984] | 614 | //if (fSpinning) RTLogRelPrintf("spun for %RU64 ns %u loops; lag=%RU64 pct=%d\n", RTTimeNanoTS() - u64Now, cLoops, TMVirtualSyncGetLag(pVM), u32CatchUpPct);
|
---|
[2830] | 615 |
|
---|
[19217] | 616 | ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
|
---|
[1] | 617 | return rc;
|
---|
| 618 | }
|
---|
| 619 |
|
---|
| 620 |
|
---|
| 621 | /**
|
---|
[5167] | 622 | * Initialize the global 1 halt method.
|
---|
| 623 | *
|
---|
| 624 | * @return VBox status code.
|
---|
[6796] | 625 | * @param pUVM Pointer to the user mode VM structure.
|
---|
[5167] | 626 | */
|
---|
[6796] | 627 | static DECLCALLBACK(int) vmR3HaltGlobal1Init(PUVM pUVM)
|
---|
[5167] | 628 | {
|
---|
[33170] | 629 | /*
|
---|
| 630 | * The defaults.
|
---|
| 631 | */
|
---|
| 632 | uint32_t cNsResolution = SUPSemEventMultiGetResolution(pUVM->vm.s.pSession);
|
---|
[33172] | 633 | if (cNsResolution > 5*RT_NS_100US)
|
---|
| 634 | pUVM->vm.s.Halt.Global1.cNsSpinBlockThresholdCfg = 50000;
|
---|
| 635 | else if (cNsResolution > RT_NS_100US)
|
---|
| 636 | pUVM->vm.s.Halt.Global1.cNsSpinBlockThresholdCfg = cNsResolution / 4;
|
---|
| 637 | else
|
---|
| 638 | pUVM->vm.s.Halt.Global1.cNsSpinBlockThresholdCfg = 2000;
|
---|
[33170] | 639 |
|
---|
| 640 | /*
|
---|
| 641 | * Query overrides.
|
---|
| 642 | *
|
---|
[33540] | 643 | * I don't have time to bother with niceties such as invalid value checks
|
---|
[33170] | 644 | * here right now. sorry.
|
---|
| 645 | */
|
---|
| 646 | PCFGMNODE pCfg = CFGMR3GetChild(CFGMR3GetRoot(pUVM->pVM), "/VMM/HaltedGlobal1");
|
---|
| 647 | if (pCfg)
|
---|
| 648 | {
|
---|
| 649 | uint32_t u32;
|
---|
| 650 | if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "SpinBlockThreshold", &u32)))
|
---|
| 651 | pUVM->vm.s.Halt.Global1.cNsSpinBlockThresholdCfg = u32;
|
---|
| 652 | }
|
---|
| 653 | LogRel(("HaltedGlobal1 config: cNsSpinBlockThresholdCfg=%u\n",
|
---|
| 654 | pUVM->vm.s.Halt.Global1.cNsSpinBlockThresholdCfg));
|
---|
[5167] | 655 | return VINF_SUCCESS;
|
---|
| 656 | }
|
---|
| 657 |
|
---|
| 658 |
|
---|
| 659 | /**
|
---|
| 660 | * The global 1 halt method - Block in GMM (ring-0) and let it
|
---|
| 661 | * try take care of the global scheduling of EMT threads.
|
---|
| 662 | */
|
---|
[19217] | 663 | static DECLCALLBACK(int) vmR3HaltGlobal1Halt(PUVMCPU pUVCpu, const uint32_t fMask, uint64_t u64Now)
|
---|
[5167] | 664 | {
|
---|
[19217] | 665 | PUVM pUVM = pUVCpu->pUVM;
|
---|
| 666 | PVMCPU pVCpu = pUVCpu->pVCpu;
|
---|
| 667 | PVM pVM = pUVCpu->pVM;
|
---|
[19395] | 668 | Assert(VMMGetCpu(pVM) == pVCpu);
|
---|
[39078] | 669 | NOREF(u64Now);
|
---|
[6796] | 670 |
|
---|
[5167] | 671 | /*
|
---|
| 672 | * Halt loop.
|
---|
| 673 | */
|
---|
[33170] | 674 | //uint64_t u64NowLog, u64Start;
|
---|
| 675 | //u64Start = u64NowLog = RTTimeNanoTS();
|
---|
[5167] | 676 | int rc = VINF_SUCCESS;
|
---|
[19217] | 677 | ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
|
---|
[5167] | 678 | unsigned cLoops = 0;
|
---|
| 679 | for (;; cLoops++)
|
---|
| 680 | {
|
---|
| 681 | /*
|
---|
| 682 | * Work the timers and check if we can exit.
|
---|
| 683 | */
|
---|
[33170] | 684 | uint64_t const u64StartTimers = RTTimeNanoTS();
|
---|
[5167] | 685 | TMR3TimerQueuesDo(pVM);
|
---|
[33170] | 686 | uint64_t const cNsElapsedTimers = RTTimeNanoTS() - u64StartTimers;
|
---|
[33172] | 687 | STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltTimers, cNsElapsedTimers);
|
---|
[46420] | 688 | if ( VM_FF_IS_PENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
|
---|
| 689 | || VMCPU_FF_IS_PENDING(pVCpu, fMask))
|
---|
[5167] | 690 | break;
|
---|
| 691 |
|
---|
| 692 | /*
|
---|
| 693 | * Estimate time left to the next event.
|
---|
| 694 | */
|
---|
[33170] | 695 | //u64NowLog = RTTimeNanoTS();
|
---|
[5167] | 696 | uint64_t u64Delta;
|
---|
[19660] | 697 | uint64_t u64GipTime = TMTimerPollGIP(pVM, pVCpu, &u64Delta);
|
---|
[46420] | 698 | if ( VM_FF_IS_PENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
|
---|
| 699 | || VMCPU_FF_IS_PENDING(pVCpu, fMask))
|
---|
[5167] | 700 | break;
|
---|
| 701 |
|
---|
| 702 | /*
|
---|
| 703 | * Block if we're not spinning and the interval isn't all that small.
|
---|
| 704 | */
|
---|
[33170] | 705 | if (u64Delta >= pUVM->vm.s.Halt.Global1.cNsSpinBlockThresholdCfg)
|
---|
[5167] | 706 | {
|
---|
| 707 | VMMR3YieldStop(pVM);
|
---|
[46420] | 708 | if ( VM_FF_IS_PENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
|
---|
| 709 | || VMCPU_FF_IS_PENDING(pVCpu, fMask))
|
---|
[33170] | 710 | break;
|
---|
[5167] | 711 |
|
---|
[33170] | 712 | //RTLogPrintf("loop=%-3d u64GipTime=%'llu / %'llu now=%'llu / %'llu\n", cLoops, u64GipTime, u64Delta, u64NowLog, u64GipTime - u64NowLog);
|
---|
| 713 | uint64_t const u64StartSchedHalt = RTTimeNanoTS();
|
---|
[20864] | 714 | rc = SUPR3CallVMMR0Ex(pVM->pVMR0, pVCpu->idCpu, VMMR0_DO_GVMM_SCHED_HALT, u64GipTime, NULL);
|
---|
[33170] | 715 | uint64_t const u64EndSchedHalt = RTTimeNanoTS();
|
---|
| 716 | uint64_t const cNsElapsedSchedHalt = u64EndSchedHalt - u64StartSchedHalt;
|
---|
| 717 | STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltBlock, cNsElapsedSchedHalt);
|
---|
| 718 |
|
---|
[5167] | 719 | if (rc == VERR_INTERRUPTED)
|
---|
| 720 | rc = VINF_SUCCESS;
|
---|
[13816] | 721 | else if (RT_FAILURE(rc))
|
---|
[5167] | 722 | {
|
---|
[30473] | 723 | rc = vmR3FatalWaitError(pUVCpu, "VMMR0_DO_GVMM_SCHED_HALT->%Rrc\n", rc);
|
---|
[5167] | 724 | break;
|
---|
| 725 | }
|
---|
[33170] | 726 | else
|
---|
| 727 | {
|
---|
| 728 | int64_t const cNsOverslept = u64EndSchedHalt - u64GipTime;
|
---|
| 729 | if (cNsOverslept > 50000)
|
---|
| 730 | STAM_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltBlockOverslept, cNsOverslept);
|
---|
| 731 | else if (cNsOverslept < -50000)
|
---|
| 732 | STAM_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltBlockInsomnia, cNsElapsedSchedHalt);
|
---|
| 733 | else
|
---|
| 734 | STAM_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltBlockOnTime, cNsElapsedSchedHalt);
|
---|
| 735 | }
|
---|
[5167] | 736 | }
|
---|
| 737 | /*
|
---|
| 738 | * When spinning call upon the GVMM and do some wakups once
|
---|
| 739 | * in a while, it's not like we're actually busy or anything.
|
---|
| 740 | */
|
---|
| 741 | else if (!(cLoops & 0x1fff))
|
---|
| 742 | {
|
---|
[33170] | 743 | uint64_t const u64StartSchedYield = RTTimeNanoTS();
|
---|
[20864] | 744 | rc = SUPR3CallVMMR0Ex(pVM->pVMR0, pVCpu->idCpu, VMMR0_DO_GVMM_SCHED_POLL, false /* don't yield */, NULL);
|
---|
[33170] | 745 | uint64_t const cNsElapsedSchedYield = RTTimeNanoTS() - u64StartSchedYield;
|
---|
| 746 | STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltYield, cNsElapsedSchedYield);
|
---|
[5167] | 747 | }
|
---|
| 748 | }
|
---|
[33170] | 749 | //RTLogPrintf("*** %u loops %'llu; lag=%RU64\n", cLoops, u64NowLog - u64Start, TMVirtualSyncGetLag(pVM));
|
---|
[5167] | 750 |
|
---|
[19217] | 751 | ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
|
---|
[5167] | 752 | return rc;
|
---|
| 753 | }
|
---|
| 754 |
|
---|
| 755 |
|
---|
| 756 | /**
|
---|
| 757 | * The global 1 halt method - VMR3Wait() worker.
|
---|
| 758 | *
|
---|
| 759 | * @returns VBox status code.
|
---|
[19217] | 760 | * @param pUVCpu Pointer to the user mode VMCPU structure.
|
---|
[5167] | 761 | */
|
---|
[19217] | 762 | static DECLCALLBACK(int) vmR3HaltGlobal1Wait(PUVMCPU pUVCpu)
|
---|
[5167] | 763 | {
|
---|
[19217] | 764 | ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
|
---|
[6796] | 765 |
|
---|
[19217] | 766 | PVM pVM = pUVCpu->pUVM->pVM;
|
---|
[19141] | 767 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
[19395] | 768 | Assert(pVCpu->idCpu == pUVCpu->idCpu);
|
---|
[19141] | 769 |
|
---|
[5167] | 770 | int rc = VINF_SUCCESS;
|
---|
| 771 | for (;;)
|
---|
| 772 | {
|
---|
| 773 | /*
|
---|
| 774 | * Check Relevant FFs.
|
---|
| 775 | */
|
---|
[46420] | 776 | if ( VM_FF_IS_PENDING(pVM, VM_FF_EXTERNAL_SUSPENDED_MASK)
|
---|
| 777 | || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_EXTERNAL_SUSPENDED_MASK))
|
---|
[5167] | 778 | break;
|
---|
| 779 |
|
---|
| 780 | /*
|
---|
| 781 | * Wait for a while. Someone will wake us up or interrupt the call if
|
---|
| 782 | * anything needs our attention.
|
---|
| 783 | */
|
---|
[20864] | 784 | rc = SUPR3CallVMMR0Ex(pVM->pVMR0, pVCpu->idCpu, VMMR0_DO_GVMM_SCHED_HALT, RTTimeNanoTS() + 1000000000 /* +1s */, NULL);
|
---|
[5167] | 785 | if (rc == VERR_INTERRUPTED)
|
---|
| 786 | rc = VINF_SUCCESS;
|
---|
[13816] | 787 | else if (RT_FAILURE(rc))
|
---|
[5167] | 788 | {
|
---|
[30473] | 789 | rc = vmR3FatalWaitError(pUVCpu, "VMMR0_DO_GVMM_SCHED_HALT->%Rrc\n", rc);
|
---|
[5167] | 790 | break;
|
---|
| 791 | }
|
---|
| 792 | }
|
---|
[6796] | 793 |
|
---|
[19217] | 794 | ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
|
---|
[5167] | 795 | return rc;
|
---|
| 796 | }
|
---|
| 797 |
|
---|
| 798 |
|
---|
| 799 | /**
|
---|
| 800 | * The global 1 halt method - VMR3NotifyFF() worker.
|
---|
| 801 | *
|
---|
[19400] | 802 | * @param pUVCpu Pointer to the user mode VMCPU structure.
|
---|
| 803 | * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
|
---|
[5167] | 804 | */
|
---|
[19400] | 805 | static DECLCALLBACK(void) vmR3HaltGlobal1NotifyCpuFF(PUVMCPU pUVCpu, uint32_t fFlags)
|
---|
[5167] | 806 | {
|
---|
[19217] | 807 | if (pUVCpu->vm.s.fWait)
|
---|
[5167] | 808 | {
|
---|
[20864] | 809 | int rc = SUPR3CallVMMR0Ex(pUVCpu->pVM->pVMR0, pUVCpu->idCpu, VMMR0_DO_GVMM_SCHED_WAKE_UP, 0, NULL);
|
---|
[5167] | 810 | AssertRC(rc);
|
---|
| 811 | }
|
---|
[19461] | 812 | else if ( ( (fFlags & VMNOTIFYFF_FLAGS_POKE)
|
---|
| 813 | || !(fFlags & VMNOTIFYFF_FLAGS_DONE_REM))
|
---|
| 814 | && pUVCpu->pVCpu)
|
---|
[19407] | 815 | {
|
---|
[19461] | 816 | VMCPUSTATE enmState = VMCPU_GET_STATE(pUVCpu->pVCpu);
|
---|
| 817 | if (enmState == VMCPUSTATE_STARTED_EXEC)
|
---|
| 818 | {
|
---|
| 819 | if (fFlags & VMNOTIFYFF_FLAGS_POKE)
|
---|
| 820 | {
|
---|
[20864] | 821 | int rc = SUPR3CallVMMR0Ex(pUVCpu->pVM->pVMR0, pUVCpu->idCpu, VMMR0_DO_GVMM_SCHED_POKE, 0, NULL);
|
---|
[19461] | 822 | AssertRC(rc);
|
---|
| 823 | }
|
---|
| 824 | }
|
---|
[40274] | 825 | #ifdef VBOX_WITH_REM
|
---|
[19461] | 826 | else if (enmState == VMCPUSTATE_STARTED_EXEC_REM)
|
---|
| 827 | {
|
---|
| 828 | if (!(fFlags & VMNOTIFYFF_FLAGS_DONE_REM))
|
---|
| 829 | REMR3NotifyFF(pUVCpu->pVM);
|
---|
| 830 | }
|
---|
[40274] | 831 | #endif
|
---|
[19407] | 832 | }
|
---|
[5167] | 833 | }
|
---|
| 834 |
|
---|
| 835 |
|
---|
| 836 | /**
|
---|
[6796] | 837 | * Bootstrap VMR3Wait() worker.
|
---|
| 838 | *
|
---|
| 839 | * @returns VBox status code.
|
---|
[19217] | 840 | * @param pUVMCPU Pointer to the user mode VMCPU structure.
|
---|
[6796] | 841 | */
|
---|
[19217] | 842 | static DECLCALLBACK(int) vmR3BootstrapWait(PUVMCPU pUVCpu)
|
---|
[6796] | 843 | {
|
---|
[19217] | 844 | PUVM pUVM = pUVCpu->pUVM;
|
---|
[6796] | 845 |
|
---|
[19217] | 846 | ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
|
---|
| 847 |
|
---|
[6796] | 848 | int rc = VINF_SUCCESS;
|
---|
| 849 | for (;;)
|
---|
| 850 | {
|
---|
| 851 | /*
|
---|
| 852 | * Check Relevant FFs.
|
---|
| 853 | */
|
---|
[38838] | 854 | if (pUVM->vm.s.pNormalReqs || pUVM->vm.s.pPriorityReqs) /* global requests pending? */
|
---|
[6796] | 855 | break;
|
---|
[38838] | 856 | if (pUVCpu->vm.s.pNormalReqs || pUVCpu->vm.s.pPriorityReqs) /* local requests pending? */
|
---|
[19217] | 857 | break;
|
---|
| 858 |
|
---|
| 859 | if ( pUVCpu->pVM
|
---|
[46420] | 860 | && ( VM_FF_IS_PENDING(pUVCpu->pVM, VM_FF_EXTERNAL_SUSPENDED_MASK)
|
---|
| 861 | || VMCPU_FF_IS_PENDING(VMMGetCpu(pUVCpu->pVM), VMCPU_FF_EXTERNAL_SUSPENDED_MASK)
|
---|
[19141] | 862 | )
|
---|
| 863 | )
|
---|
[6796] | 864 | break;
|
---|
[30473] | 865 | if (pUVM->vm.s.fTerminateEMT)
|
---|
[6799] | 866 | break;
|
---|
[6796] | 867 |
|
---|
| 868 | /*
|
---|
| 869 | * Wait for a while. Someone will wake us up or interrupt the call if
|
---|
| 870 | * anything needs our attention.
|
---|
| 871 | */
|
---|
[19217] | 872 | rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, 1000);
|
---|
[6796] | 873 | if (rc == VERR_TIMEOUT)
|
---|
| 874 | rc = VINF_SUCCESS;
|
---|
[13816] | 875 | else if (RT_FAILURE(rc))
|
---|
[6796] | 876 | {
|
---|
[30473] | 877 | rc = vmR3FatalWaitError(pUVCpu, "RTSemEventWait->%Rrc\n", rc);
|
---|
[6796] | 878 | break;
|
---|
| 879 | }
|
---|
| 880 | }
|
---|
| 881 |
|
---|
[19217] | 882 | ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
|
---|
[6796] | 883 | return rc;
|
---|
| 884 | }
|
---|
| 885 |
|
---|
| 886 |
|
---|
| 887 | /**
|
---|
| 888 | * Bootstrap VMR3NotifyFF() worker.
|
---|
| 889 | *
|
---|
[19400] | 890 | * @param pUVCpu Pointer to the user mode VMCPU structure.
|
---|
| 891 | * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
|
---|
[6796] | 892 | */
|
---|
[19400] | 893 | static DECLCALLBACK(void) vmR3BootstrapNotifyCpuFF(PUVMCPU pUVCpu, uint32_t fFlags)
|
---|
[6796] | 894 | {
|
---|
[19217] | 895 | if (pUVCpu->vm.s.fWait)
|
---|
[6796] | 896 | {
|
---|
[19217] | 897 | int rc = RTSemEventSignal(pUVCpu->vm.s.EventSemWait);
|
---|
[6796] | 898 | AssertRC(rc);
|
---|
| 899 | }
|
---|
[19400] | 900 | NOREF(fFlags);
|
---|
[6796] | 901 | }
|
---|
| 902 |
|
---|
| 903 |
|
---|
| 904 | /**
|
---|
[2984] | 905 | * Default VMR3Wait() worker.
|
---|
| 906 | *
|
---|
| 907 | * @returns VBox status code.
|
---|
[19217] | 908 | * @param pUVMCPU Pointer to the user mode VMCPU structure.
|
---|
[2984] | 909 | */
|
---|
[19217] | 910 | static DECLCALLBACK(int) vmR3DefaultWait(PUVMCPU pUVCpu)
|
---|
[2984] | 911 | {
|
---|
[19217] | 912 | ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
|
---|
[6796] | 913 |
|
---|
[19217] | 914 | PVM pVM = pUVCpu->pVM;
|
---|
| 915 | PVMCPU pVCpu = pUVCpu->pVCpu;
|
---|
[19141] | 916 | int rc = VINF_SUCCESS;
|
---|
[2984] | 917 | for (;;)
|
---|
| 918 | {
|
---|
| 919 | /*
|
---|
| 920 | * Check Relevant FFs.
|
---|
| 921 | */
|
---|
[46420] | 922 | if ( VM_FF_IS_PENDING(pVM, VM_FF_EXTERNAL_SUSPENDED_MASK)
|
---|
| 923 | || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_EXTERNAL_SUSPENDED_MASK))
|
---|
[2984] | 924 | break;
|
---|
| 925 |
|
---|
| 926 | /*
|
---|
| 927 | * Wait for a while. Someone will wake us up or interrupt the call if
|
---|
| 928 | * anything needs our attention.
|
---|
| 929 | */
|
---|
[19217] | 930 | rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, 1000);
|
---|
[2984] | 931 | if (rc == VERR_TIMEOUT)
|
---|
| 932 | rc = VINF_SUCCESS;
|
---|
[13816] | 933 | else if (RT_FAILURE(rc))
|
---|
[2984] | 934 | {
|
---|
[30473] | 935 | rc = vmR3FatalWaitError(pUVCpu, "RTSemEventWait->%Rrc", rc);
|
---|
[2984] | 936 | break;
|
---|
| 937 | }
|
---|
| 938 | }
|
---|
[6796] | 939 |
|
---|
[19217] | 940 | ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
|
---|
[2984] | 941 | return rc;
|
---|
| 942 | }
|
---|
| 943 |
|
---|
| 944 |
|
---|
| 945 | /**
|
---|
| 946 | * Default VMR3NotifyFF() worker.
|
---|
| 947 | *
|
---|
[19400] | 948 | * @param pUVCpu Pointer to the user mode VMCPU structure.
|
---|
| 949 | * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
|
---|
[2984] | 950 | */
|
---|
[19400] | 951 | static DECLCALLBACK(void) vmR3DefaultNotifyCpuFF(PUVMCPU pUVCpu, uint32_t fFlags)
|
---|
[2984] | 952 | {
|
---|
[19217] | 953 | if (pUVCpu->vm.s.fWait)
|
---|
[2984] | 954 | {
|
---|
[19217] | 955 | int rc = RTSemEventSignal(pUVCpu->vm.s.EventSemWait);
|
---|
[2984] | 956 | AssertRC(rc);
|
---|
| 957 | }
|
---|
[40274] | 958 | #ifdef VBOX_WITH_REM
|
---|
[19461] | 959 | else if ( !(fFlags & VMNOTIFYFF_FLAGS_DONE_REM)
|
---|
| 960 | && pUVCpu->pVCpu
|
---|
| 961 | && pUVCpu->pVCpu->enmState == VMCPUSTATE_STARTED_EXEC_REM)
|
---|
[19217] | 962 | REMR3NotifyFF(pUVCpu->pVM);
|
---|
[40274] | 963 | #endif
|
---|
[2984] | 964 | }
|
---|
| 965 |
|
---|
| 966 |
|
---|
| 967 | /**
|
---|
| 968 | * Array with halt method descriptors.
|
---|
| 969 | * VMINT::iHaltMethod contains an index into this array.
|
---|
| 970 | */
|
---|
| 971 | static const struct VMHALTMETHODDESC
|
---|
| 972 | {
|
---|
| 973 | /** The halt method id. */
|
---|
| 974 | VMHALTMETHOD enmHaltMethod;
|
---|
| 975 | /** The init function for loading config and initialize variables. */
|
---|
[6796] | 976 | DECLR3CALLBACKMEMBER(int, pfnInit,(PUVM pUVM));
|
---|
[2984] | 977 | /** The term function. */
|
---|
[6796] | 978 | DECLR3CALLBACKMEMBER(void, pfnTerm,(PUVM pUVM));
|
---|
[19401] | 979 | /** The VMR3WaitHaltedU function. */
|
---|
[19217] | 980 | DECLR3CALLBACKMEMBER(int, pfnHalt,(PUVMCPU pUVCpu, const uint32_t fMask, uint64_t u64Now));
|
---|
[19401] | 981 | /** The VMR3WaitU function. */
|
---|
[19217] | 982 | DECLR3CALLBACKMEMBER(int, pfnWait,(PUVMCPU pUVCpu));
|
---|
[19401] | 983 | /** The VMR3NotifyCpuFFU function. */
|
---|
[19400] | 984 | DECLR3CALLBACKMEMBER(void, pfnNotifyCpuFF,(PUVMCPU pUVCpu, uint32_t fFlags));
|
---|
[19401] | 985 | /** The VMR3NotifyGlobalFFU function. */
|
---|
[19400] | 986 | DECLR3CALLBACKMEMBER(void, pfnNotifyGlobalFF,(PUVM pUVM, uint32_t fFlags));
|
---|
[2984] | 987 | } g_aHaltMethods[] =
|
---|
| 988 | {
|
---|
[19400] | 989 | { VMHALTMETHOD_BOOTSTRAP, NULL, NULL, NULL, vmR3BootstrapWait, vmR3BootstrapNotifyCpuFF, NULL },
|
---|
| 990 | { VMHALTMETHOD_OLD, NULL, NULL, vmR3HaltOldDoHalt, vmR3DefaultWait, vmR3DefaultNotifyCpuFF, NULL },
|
---|
| 991 | { VMHALTMETHOD_1, vmR3HaltMethod1Init, NULL, vmR3HaltMethod1Halt, vmR3DefaultWait, vmR3DefaultNotifyCpuFF, NULL },
|
---|
| 992 | { VMHALTMETHOD_GLOBAL_1, vmR3HaltGlobal1Init, NULL, vmR3HaltGlobal1Halt, vmR3HaltGlobal1Wait, vmR3HaltGlobal1NotifyCpuFF, NULL },
|
---|
[2984] | 993 | };
|
---|
| 994 |
|
---|
| 995 |
|
---|
| 996 | /**
|
---|
| 997 | * Notify the emulation thread (EMT) about pending Forced Action (FF).
|
---|
| 998 | *
|
---|
| 999 | * This function is called by thread other than EMT to make
|
---|
| 1000 | * sure EMT wakes up and promptly service an FF request.
|
---|
| 1001 | *
|
---|
[19400] | 1002 | * @param pUVM Pointer to the user mode VM structure.
|
---|
| 1003 | * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
|
---|
[44340] | 1004 | * @internal
|
---|
[2984] | 1005 | */
|
---|
[44340] | 1006 | VMMR3_INT_DECL(void) VMR3NotifyGlobalFFU(PUVM pUVM, uint32_t fFlags)
|
---|
[2984] | 1007 | {
|
---|
[19400] | 1008 | LogFlow(("VMR3NotifyGlobalFFU:\n"));
|
---|
| 1009 | uint32_t iHaldMethod = pUVM->vm.s.iHaltMethod;
|
---|
[19217] | 1010 |
|
---|
[19400] | 1011 | if (g_aHaltMethods[iHaldMethod].pfnNotifyGlobalFF) /** @todo make mandatory. */
|
---|
| 1012 | g_aHaltMethods[iHaldMethod].pfnNotifyGlobalFF(pUVM, fFlags);
|
---|
| 1013 | else
|
---|
| 1014 | for (VMCPUID iCpu = 0; iCpu < pUVM->cCpus; iCpu++)
|
---|
| 1015 | g_aHaltMethods[iHaldMethod].pfnNotifyCpuFF(&pUVM->aCpus[iCpu], fFlags);
|
---|
[2984] | 1016 | }
|
---|
| 1017 |
|
---|
| 1018 |
|
---|
| 1019 | /**
|
---|
[6796] | 1020 | * Notify the emulation thread (EMT) about pending Forced Action (FF).
|
---|
| 1021 | *
|
---|
| 1022 | * This function is called by thread other than EMT to make
|
---|
| 1023 | * sure EMT wakes up and promptly service an FF request.
|
---|
| 1024 | *
|
---|
| 1025 | * @param pUVM Pointer to the user mode VM structure.
|
---|
[19400] | 1026 | * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
|
---|
[44340] | 1027 | * @internal
|
---|
[6796] | 1028 | */
|
---|
[44340] | 1029 | VMMR3_INT_DECL(void) VMR3NotifyCpuFFU(PUVMCPU pUVCpu, uint32_t fFlags)
|
---|
[6796] | 1030 | {
|
---|
[19217] | 1031 | PUVM pUVM = pUVCpu->pUVM;
|
---|
[6796] | 1032 |
|
---|
[19217] | 1033 | LogFlow(("VMR3NotifyCpuFFU:\n"));
|
---|
[19400] | 1034 | g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnNotifyCpuFF(pUVCpu, fFlags);
|
---|
[19217] | 1035 | }
|
---|
| 1036 |
|
---|
| 1037 |
|
---|
[6796] | 1038 | /**
|
---|
[2830] | 1039 | * Halted VM Wait.
|
---|
| 1040 | * Any external event will unblock the thread.
|
---|
| 1041 | *
|
---|
[20961] | 1042 | * @returns VINF_SUCCESS unless a fatal error occurred. In the latter
|
---|
[2830] | 1043 | * case an appropriate status code is returned.
|
---|
[41800] | 1044 | * @param pVM Pointer to the VM.
|
---|
[41802] | 1045 | * @param pVCpu Pointer to the VMCPU.
|
---|
[2830] | 1046 | * @param fIgnoreInterrupts If set the VM_FF_INTERRUPT flags is ignored.
|
---|
| 1047 | * @thread The emulation thread.
|
---|
[44340] | 1048 | * @internal
|
---|
[2830] | 1049 | */
|
---|
[44340] | 1050 | VMMR3_INT_DECL(int) VMR3WaitHalted(PVM pVM, PVMCPU pVCpu, bool fIgnoreInterrupts)
|
---|
[2830] | 1051 | {
|
---|
| 1052 | LogFlow(("VMR3WaitHalted: fIgnoreInterrupts=%d\n", fIgnoreInterrupts));
|
---|
| 1053 |
|
---|
| 1054 | /*
|
---|
| 1055 | * Check Relevant FFs.
|
---|
| 1056 | */
|
---|
| 1057 | const uint32_t fMask = !fIgnoreInterrupts
|
---|
[19141] | 1058 | ? VMCPU_FF_EXTERNAL_HALTED_MASK
|
---|
| 1059 | : VMCPU_FF_EXTERNAL_HALTED_MASK & ~(VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC);
|
---|
[46420] | 1060 | if ( VM_FF_IS_PENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
|
---|
| 1061 | || VMCPU_FF_IS_PENDING(pVCpu, fMask))
|
---|
[2830] | 1062 | {
|
---|
[19141] | 1063 | LogFlow(("VMR3WaitHalted: returns VINF_SUCCESS (FF %#x FFCPU %#x)\n", pVM->fGlobalForcedActions, pVCpu->fLocalForcedActions));
|
---|
[2830] | 1064 | return VINF_SUCCESS;
|
---|
| 1065 | }
|
---|
| 1066 |
|
---|
| 1067 | /*
|
---|
[12549] | 1068 | * The yielder is suspended while we're halting, while TM might have clock(s) running
|
---|
| 1069 | * only at certain times and need to be notified..
|
---|
[2830] | 1070 | */
|
---|
[19539] | 1071 | if (pVCpu->idCpu == 0)
|
---|
| 1072 | VMMR3YieldSuspend(pVM);
|
---|
[19032] | 1073 | TMNotifyStartOfHalt(pVCpu);
|
---|
[2830] | 1074 |
|
---|
| 1075 | /*
|
---|
| 1076 | * Record halt averages for the last second.
|
---|
| 1077 | */
|
---|
[19299] | 1078 | PUVMCPU pUVCpu = pVCpu->pUVCpu;
|
---|
[2830] | 1079 | uint64_t u64Now = RTTimeNanoTS();
|
---|
[19217] | 1080 | int64_t off = u64Now - pUVCpu->vm.s.u64HaltsStartTS;
|
---|
[2830] | 1081 | if (off > 1000000000)
|
---|
| 1082 | {
|
---|
[19217] | 1083 | if (off > _4G || !pUVCpu->vm.s.cHalts)
|
---|
[2830] | 1084 | {
|
---|
[19217] | 1085 | pUVCpu->vm.s.HaltInterval = 1000000000 /* 1 sec */;
|
---|
| 1086 | pUVCpu->vm.s.HaltFrequency = 1;
|
---|
[2830] | 1087 | }
|
---|
| 1088 | else
|
---|
| 1089 | {
|
---|
[19217] | 1090 | pUVCpu->vm.s.HaltInterval = (uint32_t)off / pUVCpu->vm.s.cHalts;
|
---|
| 1091 | pUVCpu->vm.s.HaltFrequency = ASMMultU64ByU32DivByU32(pUVCpu->vm.s.cHalts, 1000000000, (uint32_t)off);
|
---|
[2830] | 1092 | }
|
---|
[19217] | 1093 | pUVCpu->vm.s.u64HaltsStartTS = u64Now;
|
---|
| 1094 | pUVCpu->vm.s.cHalts = 0;
|
---|
[2830] | 1095 | }
|
---|
[19217] | 1096 | pUVCpu->vm.s.cHalts++;
|
---|
[2830] | 1097 |
|
---|
| 1098 | /*
|
---|
| 1099 | * Do the halt.
|
---|
| 1100 | */
|
---|
[45749] | 1101 | VMCPU_ASSERT_STATE(pVCpu, VMCPUSTATE_STARTED);
|
---|
[19435] | 1102 | VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED_HALTED);
|
---|
[19217] | 1103 | PUVM pUVM = pUVCpu->pUVM;
|
---|
| 1104 | int rc = g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnHalt(pUVCpu, fMask, u64Now);
|
---|
[19435] | 1105 | VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED);
|
---|
[2830] | 1106 |
|
---|
| 1107 | /*
|
---|
[12549] | 1108 | * Notify TM and resume the yielder
|
---|
[2830] | 1109 | */
|
---|
[19032] | 1110 | TMNotifyEndOfHalt(pVCpu);
|
---|
[19539] | 1111 | if (pVCpu->idCpu == 0)
|
---|
| 1112 | VMMR3YieldResume(pVM);
|
---|
[2830] | 1113 |
|
---|
[19141] | 1114 | LogFlow(("VMR3WaitHalted: returns %Rrc (FF %#x)\n", rc, pVM->fGlobalForcedActions));
|
---|
[2830] | 1115 | return rc;
|
---|
| 1116 | }
|
---|
| 1117 |
|
---|
| 1118 |
|
---|
| 1119 | /**
|
---|
[1] | 1120 | * Suspended VM Wait.
|
---|
| 1121 | * Only a handful of forced actions will cause the function to
|
---|
| 1122 | * return to the caller.
|
---|
| 1123 | *
|
---|
[20961] | 1124 | * @returns VINF_SUCCESS unless a fatal error occurred. In the latter
|
---|
[1] | 1125 | * case an appropriate status code is returned.
|
---|
[19217] | 1126 | * @param pUVCpu Pointer to the user mode VMCPU structure.
|
---|
[1] | 1127 | * @thread The emulation thread.
|
---|
[44340] | 1128 | * @internal
|
---|
[1] | 1129 | */
|
---|
[44340] | 1130 | VMMR3_INT_DECL(int) VMR3WaitU(PUVMCPU pUVCpu)
|
---|
[1] | 1131 | {
|
---|
[6796] | 1132 | LogFlow(("VMR3WaitU:\n"));
|
---|
[1] | 1133 |
|
---|
| 1134 | /*
|
---|
| 1135 | * Check Relevant FFs.
|
---|
| 1136 | */
|
---|
[19217] | 1137 | PVM pVM = pUVCpu->pVM;
|
---|
| 1138 | PVMCPU pVCpu = pUVCpu->pVCpu;
|
---|
[19141] | 1139 |
|
---|
[6796] | 1140 | if ( pVM
|
---|
[46420] | 1141 | && ( VM_FF_IS_PENDING(pVM, VM_FF_EXTERNAL_SUSPENDED_MASK)
|
---|
| 1142 | || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_EXTERNAL_SUSPENDED_MASK)
|
---|
[19141] | 1143 | )
|
---|
| 1144 | )
|
---|
[1] | 1145 | {
|
---|
[19141] | 1146 | LogFlow(("VMR3Wait: returns VINF_SUCCESS (FF %#x)\n", pVM->fGlobalForcedActions));
|
---|
[1] | 1147 | return VINF_SUCCESS;
|
---|
| 1148 | }
|
---|
| 1149 |
|
---|
[2984] | 1150 | /*
|
---|
| 1151 | * Do waiting according to the halt method (so VMR3NotifyFF
|
---|
| 1152 | * doesn't have to special case anything).
|
---|
| 1153 | */
|
---|
[19217] | 1154 | PUVM pUVM = pUVCpu->pUVM;
|
---|
| 1155 | int rc = g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnWait(pUVCpu);
|
---|
[36255] | 1156 | LogFlow(("VMR3WaitU: returns %Rrc (FF %#x)\n", rc, pUVM->pVM ? pUVM->pVM->fGlobalForcedActions : 0));
|
---|
[2984] | 1157 | return rc;
|
---|
| 1158 | }
|
---|
| 1159 |
|
---|
| 1160 |
|
---|
| 1161 | /**
|
---|
[24740] | 1162 | * Interface that PDMR3Suspend, PDMR3PowerOff and PDMR3Reset uses when they wait
|
---|
| 1163 | * for the handling of asynchronous notifications to complete.
|
---|
| 1164 | *
|
---|
| 1165 | * @returns VINF_SUCCESS unless a fatal error occurred. In the latter
|
---|
| 1166 | * case an appropriate status code is returned.
|
---|
| 1167 | * @param pUVCpu Pointer to the user mode VMCPU structure.
|
---|
| 1168 | * @thread The emulation thread.
|
---|
| 1169 | */
|
---|
| 1170 | VMMR3_INT_DECL(int) VMR3AsyncPdmNotificationWaitU(PUVMCPU pUVCpu)
|
---|
| 1171 | {
|
---|
| 1172 | LogFlow(("VMR3AsyncPdmNotificationWaitU:\n"));
|
---|
| 1173 | return VMR3WaitU(pUVCpu);
|
---|
| 1174 | }
|
---|
| 1175 |
|
---|
| 1176 |
|
---|
| 1177 | /**
|
---|
| 1178 | * Interface that PDM the helper asynchronous notification completed methods
|
---|
| 1179 | * uses for EMT0 when it is waiting inside VMR3AsyncPdmNotificationWaitU().
|
---|
| 1180 | *
|
---|
| 1181 | * @param pUVM Pointer to the user mode VM structure.
|
---|
| 1182 | */
|
---|
| 1183 | VMMR3_INT_DECL(void) VMR3AsyncPdmNotificationWakeupU(PUVM pUVM)
|
---|
| 1184 | {
|
---|
| 1185 | LogFlow(("VMR3AsyncPdmNotificationWakeupU:\n"));
|
---|
| 1186 | VM_FF_SET(pUVM->pVM, VM_FF_REQUEST); /* this will have to do for now. */
|
---|
| 1187 | g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnNotifyCpuFF(&pUVM->aCpus[0], 0 /*fFlags*/);
|
---|
| 1188 | }
|
---|
| 1189 |
|
---|
| 1190 |
|
---|
| 1191 | /**
|
---|
[20663] | 1192 | * Rendezvous callback that will be called once.
|
---|
| 1193 | *
|
---|
[23145] | 1194 | * @returns VBox strict status code.
|
---|
[41800] | 1195 | * @param pVM Pointer to the VM.
|
---|
[41803] | 1196 | * @param pVCpu Pointer to the VMCPU of the calling EMT.
|
---|
[20663] | 1197 | * @param pvUser The new g_aHaltMethods index.
|
---|
| 1198 | */
|
---|
[23145] | 1199 | static DECLCALLBACK(VBOXSTRICTRC) vmR3SetHaltMethodCallback(PVM pVM, PVMCPU pVCpu, void *pvUser)
|
---|
[20663] | 1200 | {
|
---|
| 1201 | PUVM pUVM = pVM->pUVM;
|
---|
| 1202 | uintptr_t i = (uintptr_t)pvUser;
|
---|
| 1203 | Assert(i < RT_ELEMENTS(g_aHaltMethods));
|
---|
| 1204 | NOREF(pVCpu);
|
---|
| 1205 |
|
---|
| 1206 | /*
|
---|
| 1207 | * Terminate the old one.
|
---|
| 1208 | */
|
---|
| 1209 | if ( pUVM->vm.s.enmHaltMethod != VMHALTMETHOD_INVALID
|
---|
| 1210 | && g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnTerm)
|
---|
| 1211 | {
|
---|
| 1212 | g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnTerm(pUVM);
|
---|
| 1213 | pUVM->vm.s.enmHaltMethod = VMHALTMETHOD_INVALID;
|
---|
| 1214 | }
|
---|
| 1215 |
|
---|
| 1216 | /* Assert that the failure fallback is where we expect. */
|
---|
| 1217 | Assert(g_aHaltMethods[0].enmHaltMethod == VMHALTMETHOD_BOOTSTRAP);
|
---|
| 1218 | Assert(!g_aHaltMethods[0].pfnTerm && !g_aHaltMethods[0].pfnInit);
|
---|
| 1219 |
|
---|
| 1220 | /*
|
---|
| 1221 | * Init the new one.
|
---|
| 1222 | */
|
---|
| 1223 | int rc = VINF_SUCCESS;
|
---|
| 1224 | memset(&pUVM->vm.s.Halt, 0, sizeof(pUVM->vm.s.Halt));
|
---|
| 1225 | if (g_aHaltMethods[i].pfnInit)
|
---|
| 1226 | {
|
---|
| 1227 | rc = g_aHaltMethods[i].pfnInit(pUVM);
|
---|
| 1228 | if (RT_FAILURE(rc))
|
---|
| 1229 | {
|
---|
| 1230 | /* Fall back on the bootstrap method. This requires no
|
---|
| 1231 | init/term (see assertion above), and will always work. */
|
---|
| 1232 | AssertLogRelRC(rc);
|
---|
| 1233 | i = 0;
|
---|
| 1234 | }
|
---|
| 1235 | }
|
---|
| 1236 |
|
---|
| 1237 | /*
|
---|
| 1238 | * Commit it.
|
---|
| 1239 | */
|
---|
| 1240 | pUVM->vm.s.enmHaltMethod = g_aHaltMethods[i].enmHaltMethod;
|
---|
| 1241 | ASMAtomicWriteU32(&pUVM->vm.s.iHaltMethod, i);
|
---|
| 1242 |
|
---|
| 1243 | return rc;
|
---|
| 1244 | }
|
---|
| 1245 |
|
---|
| 1246 |
|
---|
| 1247 | /**
|
---|
[2984] | 1248 | * Changes the halt method.
|
---|
| 1249 | *
|
---|
| 1250 | * @returns VBox status code.
|
---|
[6796] | 1251 | * @param pUVM Pointer to the user mode VM structure.
|
---|
[2984] | 1252 | * @param enmHaltMethod The new halt method.
|
---|
| 1253 | * @thread EMT.
|
---|
| 1254 | */
|
---|
[6796] | 1255 | int vmR3SetHaltMethodU(PUVM pUVM, VMHALTMETHOD enmHaltMethod)
|
---|
[2984] | 1256 | {
|
---|
[6796] | 1257 | PVM pVM = pUVM->pVM; Assert(pVM);
|
---|
[2984] | 1258 | VM_ASSERT_EMT(pVM);
|
---|
| 1259 | AssertReturn(enmHaltMethod > VMHALTMETHOD_INVALID && enmHaltMethod < VMHALTMETHOD_END, VERR_INVALID_PARAMETER);
|
---|
| 1260 |
|
---|
| 1261 | /*
|
---|
| 1262 | * Resolve default (can be overridden in the configuration).
|
---|
| 1263 | */
|
---|
| 1264 | if (enmHaltMethod == VMHALTMETHOD_DEFAULT)
|
---|
[1] | 1265 | {
|
---|
[2984] | 1266 | uint32_t u32;
|
---|
| 1267 | int rc = CFGMR3QueryU32(CFGMR3GetChild(CFGMR3GetRoot(pVM), "VM"), "HaltMethod", &u32);
|
---|
[13816] | 1268 | if (RT_SUCCESS(rc))
|
---|
[1] | 1269 | {
|
---|
[2984] | 1270 | enmHaltMethod = (VMHALTMETHOD)u32;
|
---|
| 1271 | if (enmHaltMethod <= VMHALTMETHOD_INVALID || enmHaltMethod >= VMHALTMETHOD_END)
|
---|
[6300] | 1272 | return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, N_("Invalid VM/HaltMethod value %d"), enmHaltMethod);
|
---|
[1] | 1273 | }
|
---|
[2984] | 1274 | else if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_CHILD_NOT_FOUND)
|
---|
[6300] | 1275 | return VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to Query VM/HaltMethod as uint32_t"));
|
---|
[2984] | 1276 | else
|
---|
[5167] | 1277 | enmHaltMethod = VMHALTMETHOD_GLOBAL_1;
|
---|
| 1278 | //enmHaltMethod = VMHALTMETHOD_1;
|
---|
[3273] | 1279 | //enmHaltMethod = VMHALTMETHOD_OLD;
|
---|
[2984] | 1280 | }
|
---|
[5167] | 1281 | LogRel(("VM: Halt method %s (%d)\n", vmR3GetHaltMethodName(enmHaltMethod), enmHaltMethod));
|
---|
[1] | 1282 |
|
---|
[2984] | 1283 | /*
|
---|
| 1284 | * Find the descriptor.
|
---|
| 1285 | */
|
---|
| 1286 | unsigned i = 0;
|
---|
| 1287 | while ( i < RT_ELEMENTS(g_aHaltMethods)
|
---|
| 1288 | && g_aHaltMethods[i].enmHaltMethod != enmHaltMethod)
|
---|
| 1289 | i++;
|
---|
| 1290 | AssertReturn(i < RT_ELEMENTS(g_aHaltMethods), VERR_INVALID_PARAMETER);
|
---|
| 1291 |
|
---|
| 1292 | /*
|
---|
[20663] | 1293 | * This needs to be done while the other EMTs are not sleeping or otherwise messing around.
|
---|
[2984] | 1294 | */
|
---|
[20663] | 1295 | return VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, vmR3SetHaltMethodCallback, (void *)(uintptr_t)i);
|
---|
[1] | 1296 | }
|
---|
| 1297 |
|
---|