VirtualBox

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

Last change on this file since 42062 was 41965, checked in by vboxsync, 13 years ago

VMM: ran scm. Mostly svn:keywords changes (adding Revision).

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