VirtualBox

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

Last change on this file since 26234 was 24740, checked in by vboxsync, 15 years ago

VMM, PDM: Need a AsyncNotificationCompleted helper so we can skip the poll mess.

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