VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/VMEmt.cpp@ 78126

Last change on this file since 78126 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette