VirtualBox

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

Last change on this file since 33980 was 33540, checked in by vboxsync, 14 years ago

*: spelling fixes, thanks Timeless!

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