VirtualBox

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

Last change on this file since 32319 was 30473, checked in by vboxsync, 14 years ago

VMM: First shot at the fatal error misbehavior (PAE).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 41.8 KB
Line 
1/* $Id: VMEmt.cpp 30473 2010-06-28 15:45:22Z 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 STAM_REL_PROFILE_START(&pUVCpu->vm.s.StatHaltTimers, b);
327 TMR3TimerQueuesDo(pVM);
328 STAM_REL_PROFILE_STOP(&pUVCpu->vm.s.StatHaltTimers, b);
329 if ( VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
330 || VMCPU_FF_ISPENDING(pVCpu, fMask))
331 break;
332 uint64_t u64NanoTS;
333 TMTimerPollGIP(pVM, pVCpu, &u64NanoTS);
334 if ( VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
335 || VMCPU_FF_ISPENDING(pVCpu, fMask))
336 break;
337
338 /*
339 * Wait for a while. Someone will wake us up or interrupt the call if
340 * anything needs our attention.
341 */
342 if (u64NanoTS < 50000)
343 {
344 //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d spin\n", u64NanoTS, cLoops++);
345 /* spin */;
346 }
347 else
348 {
349 VMMR3YieldStop(pVM);
350 //uint64_t u64Start = RTTimeNanoTS();
351 if (u64NanoTS < 870000) /* this is a bit speculative... works fine on linux. */
352 {
353 //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d yield", u64NanoTS, cLoops++);
354 STAM_REL_PROFILE_START(&pUVCpu->vm.s.StatHaltYield, a);
355 RTThreadYield(); /* this is the best we can do here */
356 STAM_REL_PROFILE_STOP(&pUVCpu->vm.s.StatHaltYield, a);
357 }
358 else if (u64NanoTS < 2000000)
359 {
360 //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d sleep 1ms", u64NanoTS, cLoops++);
361 STAM_REL_PROFILE_START(&pUVCpu->vm.s.StatHaltBlock, a);
362 rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, 1);
363 STAM_REL_PROFILE_STOP(&pUVCpu->vm.s.StatHaltBlock, a);
364 }
365 else
366 {
367 //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d sleep %dms", u64NanoTS, cLoops++, (uint32_t)RT_MIN((u64NanoTS - 500000) / 1000000, 15));
368 STAM_REL_PROFILE_START(&pUVCpu->vm.s.StatHaltBlock, a);
369 rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, RT_MIN((u64NanoTS - 1000000) / 1000000, 15));
370 STAM_REL_PROFILE_STOP(&pUVCpu->vm.s.StatHaltBlock, a);
371 }
372 //uint64_t u64Slept = RTTimeNanoTS() - u64Start;
373 //RTLogPrintf(" -> rc=%Rrc in %RU64 ns / %RI64 ns delta\n", rc, u64Slept, u64NanoTS - u64Slept);
374 }
375 if (rc == VERR_TIMEOUT)
376 rc = VINF_SUCCESS;
377 else if (RT_FAILURE(rc))
378 {
379 rc = vmR3FatalWaitError(pUVCpu, "RTSemEventWait->%Rrc\n", rc);
380 break;
381 }
382 }
383
384 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
385 return rc;
386}
387
388
389/**
390 * Initialize the configuration of halt method 1 & 2.
391 *
392 * @return VBox status code. Failure on invalid CFGM data.
393 * @param pVM The VM handle.
394 */
395static int vmR3HaltMethod12ReadConfigU(PUVM pUVM)
396{
397 /*
398 * The defaults.
399 */
400#if 1 /* DEBUGGING STUFF - REMOVE LATER */
401 pUVM->vm.s.Halt.Method12.u32LagBlockIntervalDivisorCfg = 4;
402 pUVM->vm.s.Halt.Method12.u32MinBlockIntervalCfg = 2*1000000;
403 pUVM->vm.s.Halt.Method12.u32MaxBlockIntervalCfg = 75*1000000;
404 pUVM->vm.s.Halt.Method12.u32StartSpinningCfg = 30*1000000;
405 pUVM->vm.s.Halt.Method12.u32StopSpinningCfg = 20*1000000;
406#else
407 pUVM->vm.s.Halt.Method12.u32LagBlockIntervalDivisorCfg = 4;
408 pUVM->vm.s.Halt.Method12.u32MinBlockIntervalCfg = 5*1000000;
409 pUVM->vm.s.Halt.Method12.u32MaxBlockIntervalCfg = 200*1000000;
410 pUVM->vm.s.Halt.Method12.u32StartSpinningCfg = 20*1000000;
411 pUVM->vm.s.Halt.Method12.u32StopSpinningCfg = 2*1000000;
412#endif
413
414 /*
415 * Query overrides.
416 *
417 * I don't have time to bother with niceities such as invalid value checks
418 * here right now. sorry.
419 */
420 PCFGMNODE pCfg = CFGMR3GetChild(CFGMR3GetRoot(pUVM->pVM), "/VMM/HaltedMethod1");
421 if (pCfg)
422 {
423 uint32_t u32;
424 if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "LagBlockIntervalDivisor", &u32)))
425 pUVM->vm.s.Halt.Method12.u32LagBlockIntervalDivisorCfg = u32;
426 if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "MinBlockInterval", &u32)))
427 pUVM->vm.s.Halt.Method12.u32MinBlockIntervalCfg = u32;
428 if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "MaxBlockInterval", &u32)))
429 pUVM->vm.s.Halt.Method12.u32MaxBlockIntervalCfg = u32;
430 if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "StartSpinning", &u32)))
431 pUVM->vm.s.Halt.Method12.u32StartSpinningCfg = u32;
432 if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "StopSpinning", &u32)))
433 pUVM->vm.s.Halt.Method12.u32StopSpinningCfg = u32;
434 LogRel(("HaltedMethod1 config: %d/%d/%d/%d/%d\n",
435 pUVM->vm.s.Halt.Method12.u32LagBlockIntervalDivisorCfg,
436 pUVM->vm.s.Halt.Method12.u32MinBlockIntervalCfg,
437 pUVM->vm.s.Halt.Method12.u32MaxBlockIntervalCfg,
438 pUVM->vm.s.Halt.Method12.u32StartSpinningCfg,
439 pUVM->vm.s.Halt.Method12.u32StopSpinningCfg));
440 }
441
442 return VINF_SUCCESS;
443}
444
445
446/**
447 * Initialize halt method 1.
448 *
449 * @return VBox status code.
450 * @param pUVM Pointer to the user mode VM structure.
451 */
452static DECLCALLBACK(int) vmR3HaltMethod1Init(PUVM pUVM)
453{
454 return vmR3HaltMethod12ReadConfigU(pUVM);
455}
456
457
458/**
459 * Method 1 - Block whenever possible, and when lagging behind
460 * switch to spinning for 10-30ms with occational blocking until
461 * the lag has been eliminated.
462 */
463static DECLCALLBACK(int) vmR3HaltMethod1Halt(PUVMCPU pUVCpu, const uint32_t fMask, uint64_t u64Now)
464{
465 PUVM pUVM = pUVCpu->pUVM;
466 PVMCPU pVCpu = pUVCpu->pVCpu;
467 PVM pVM = pUVCpu->pVM;
468
469 /*
470 * To simplify things, we decide up-front whether we should switch to spinning or
471 * not. This makes some ASSUMPTIONS about the cause of the spinning (PIT/RTC/PCNet)
472 * and that it will generate interrupts or other events that will cause us to exit
473 * the halt loop.
474 */
475 bool fBlockOnce = false;
476 bool fSpinning = false;
477 uint32_t u32CatchUpPct = TMVirtualSyncGetCatchUpPct(pVM);
478 if (u32CatchUpPct /* non-zero if catching up */)
479 {
480 if (pUVCpu->vm.s.Halt.Method12.u64StartSpinTS)
481 {
482 fSpinning = TMVirtualSyncGetLag(pVM) >= pUVM->vm.s.Halt.Method12.u32StopSpinningCfg;
483 if (fSpinning)
484 {
485 uint64_t u64Lag = TMVirtualSyncGetLag(pVM);
486 fBlockOnce = u64Now - pUVCpu->vm.s.Halt.Method12.u64LastBlockTS
487 > RT_MAX(pUVM->vm.s.Halt.Method12.u32MinBlockIntervalCfg,
488 RT_MIN(u64Lag / pUVM->vm.s.Halt.Method12.u32LagBlockIntervalDivisorCfg,
489 pUVM->vm.s.Halt.Method12.u32MaxBlockIntervalCfg));
490 }
491 else
492 {
493 //RTLogRelPrintf("Stopped spinning (%u ms)\n", (u64Now - pUVCpu->vm.s.Halt.Method12.u64StartSpinTS) / 1000000);
494 pUVCpu->vm.s.Halt.Method12.u64StartSpinTS = 0;
495 }
496 }
497 else
498 {
499 fSpinning = TMVirtualSyncGetLag(pVM) >= pUVM->vm.s.Halt.Method12.u32StartSpinningCfg;
500 if (fSpinning)
501 pUVCpu->vm.s.Halt.Method12.u64StartSpinTS = u64Now;
502 }
503 }
504 else if (pUVCpu->vm.s.Halt.Method12.u64StartSpinTS)
505 {
506 //RTLogRelPrintf("Stopped spinning (%u ms)\n", (u64Now - pUVCpu->vm.s.Halt.Method12.u64StartSpinTS) / 1000000);
507 pUVCpu->vm.s.Halt.Method12.u64StartSpinTS = 0;
508 }
509
510 /*
511 * Halt loop.
512 */
513 int rc = VINF_SUCCESS;
514 ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
515 unsigned cLoops = 0;
516 for (;; cLoops++)
517 {
518 /*
519 * Work the timers and check if we can exit.
520 */
521 STAM_REL_PROFILE_START(&pUVCpu->vm.s.StatHaltTimers, b);
522 TMR3TimerQueuesDo(pVM);
523 STAM_REL_PROFILE_STOP(&pUVCpu->vm.s.StatHaltTimers, b);
524 if ( VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
525 || VMCPU_FF_ISPENDING(pVCpu, fMask))
526 break;
527
528 /*
529 * Estimate time left to the next event.
530 */
531 uint64_t u64NanoTS;
532 TMTimerPollGIP(pVM, pVCpu, &u64NanoTS);
533 if ( VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
534 || VMCPU_FF_ISPENDING(pVCpu, fMask))
535 break;
536
537 /*
538 * Block if we're not spinning and the interval isn't all that small.
539 */
540 if ( ( !fSpinning
541 || fBlockOnce)
542#if 1 /* DEBUGGING STUFF - REMOVE LATER */
543 && u64NanoTS >= 100000) /* 0.100 ms */
544#else
545 && u64NanoTS >= 250000) /* 0.250 ms */
546#endif
547 {
548 const uint64_t Start = pUVCpu->vm.s.Halt.Method12.u64LastBlockTS = RTTimeNanoTS();
549 VMMR3YieldStop(pVM);
550
551 uint32_t cMilliSecs = RT_MIN(u64NanoTS / 1000000, 15);
552 if (cMilliSecs <= pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLongAvg)
553 cMilliSecs = 1;
554 else
555 cMilliSecs -= pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLongAvg;
556 //RTLogRelPrintf("u64NanoTS=%RI64 cLoops=%3d sleep %02dms (%7RU64) ", u64NanoTS, cLoops, cMilliSecs, u64NanoTS);
557 STAM_REL_PROFILE_START(&pUVCpu->vm.s.StatHaltBlock, a);
558 rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, cMilliSecs);
559 STAM_REL_PROFILE_STOP(&pUVCpu->vm.s.StatHaltBlock, a);
560 if (rc == VERR_TIMEOUT)
561 rc = VINF_SUCCESS;
562 else if (RT_FAILURE(rc))
563 {
564 rc = vmR3FatalWaitError(pUVCpu, "RTSemEventWait->%Rrc\n", rc);
565 break;
566 }
567
568 /*
569 * Calc the statistics.
570 * Update averages every 16th time, and flush parts of the history every 64th time.
571 */
572 const uint64_t Elapsed = RTTimeNanoTS() - Start;
573 pUVCpu->vm.s.Halt.Method12.cNSBlocked += Elapsed;
574 if (Elapsed > u64NanoTS)
575 pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLong += Elapsed - u64NanoTS;
576 pUVCpu->vm.s.Halt.Method12.cBlocks++;
577 if (!(pUVCpu->vm.s.Halt.Method12.cBlocks & 0xf))
578 {
579 pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLongAvg = pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLong / pUVCpu->vm.s.Halt.Method12.cBlocks;
580 if (!(pUVCpu->vm.s.Halt.Method12.cBlocks & 0x3f))
581 {
582 pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLong = pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLongAvg * 0x40;
583 pUVCpu->vm.s.Halt.Method12.cBlocks = 0x40;
584 }
585 }
586 //RTLogRelPrintf(" -> %7RU64 ns / %7RI64 ns delta%s\n", Elapsed, Elapsed - u64NanoTS, fBlockOnce ? " (block once)" : "");
587
588 /*
589 * Clear the block once flag if we actually blocked.
590 */
591 if ( fBlockOnce
592 && Elapsed > 100000 /* 0.1 ms */)
593 fBlockOnce = false;
594 }
595 }
596 //if (fSpinning) RTLogRelPrintf("spun for %RU64 ns %u loops; lag=%RU64 pct=%d\n", RTTimeNanoTS() - u64Now, cLoops, TMVirtualSyncGetLag(pVM), u32CatchUpPct);
597
598 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
599 return rc;
600}
601
602
603/**
604 * Initialize the global 1 halt method.
605 *
606 * @return VBox status code.
607 * @param pUVM Pointer to the user mode VM structure.
608 */
609static DECLCALLBACK(int) vmR3HaltGlobal1Init(PUVM pUVM)
610{
611 return VINF_SUCCESS;
612}
613
614
615/**
616 * The global 1 halt method - Block in GMM (ring-0) and let it
617 * try take care of the global scheduling of EMT threads.
618 */
619static DECLCALLBACK(int) vmR3HaltGlobal1Halt(PUVMCPU pUVCpu, const uint32_t fMask, uint64_t u64Now)
620{
621 PUVM pUVM = pUVCpu->pUVM;
622 PVMCPU pVCpu = pUVCpu->pVCpu;
623 PVM pVM = pUVCpu->pVM;
624 Assert(VMMGetCpu(pVM) == pVCpu);
625
626 /*
627 * Halt loop.
628 */
629 int rc = VINF_SUCCESS;
630 ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
631 unsigned cLoops = 0;
632 for (;; cLoops++)
633 {
634 /*
635 * Work the timers and check if we can exit.
636 */
637 STAM_REL_PROFILE_START(&pUVCpu->vm.s.StatHaltTimers, b);
638 TMR3TimerQueuesDo(pVM);
639 STAM_REL_PROFILE_STOP(&pUVCpu->vm.s.StatHaltTimers, b);
640 if ( VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
641 || VMCPU_FF_ISPENDING(pVCpu, fMask))
642 break;
643
644 /*
645 * Estimate time left to the next event.
646 */
647 uint64_t u64Delta;
648 uint64_t u64GipTime = TMTimerPollGIP(pVM, pVCpu, &u64Delta);
649 if ( VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
650 || VMCPU_FF_ISPENDING(pVCpu, fMask))
651 break;
652
653 /*
654 * Block if we're not spinning and the interval isn't all that small.
655 */
656 if (u64Delta > 50000 /* 0.050ms */)
657 {
658 VMMR3YieldStop(pVM);
659 if ( VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
660 || VMCPU_FF_ISPENDING(pVCpu, fMask))
661 break;
662
663 //RTLogRelPrintf("u64NanoTS=%RI64 cLoops=%3d sleep %02dms (%7RU64) ", u64NanoTS, cLoops, cMilliSecs, u64NanoTS);
664 STAM_REL_PROFILE_START(&pUVCpu->vm.s.StatHaltBlock, c);
665 rc = SUPR3CallVMMR0Ex(pVM->pVMR0, pVCpu->idCpu, VMMR0_DO_GVMM_SCHED_HALT, u64GipTime, NULL);
666 STAM_REL_PROFILE_STOP(&pUVCpu->vm.s.StatHaltBlock, c);
667 if (rc == VERR_INTERRUPTED)
668 rc = VINF_SUCCESS;
669 else if (RT_FAILURE(rc))
670 {
671 rc = vmR3FatalWaitError(pUVCpu, "VMMR0_DO_GVMM_SCHED_HALT->%Rrc\n", rc);
672 break;
673 }
674 }
675 /*
676 * When spinning call upon the GVMM and do some wakups once
677 * in a while, it's not like we're actually busy or anything.
678 */
679 else if (!(cLoops & 0x1fff))
680 {
681 STAM_REL_PROFILE_START(&pUVCpu->vm.s.StatHaltYield, d);
682 rc = SUPR3CallVMMR0Ex(pVM->pVMR0, pVCpu->idCpu, VMMR0_DO_GVMM_SCHED_POLL, false /* don't yield */, NULL);
683 STAM_REL_PROFILE_STOP(&pUVCpu->vm.s.StatHaltYield, d);
684 }
685 }
686 //if (fSpinning) RTLogRelPrintf("spun for %RU64 ns %u loops; lag=%RU64 pct=%d\n", RTTimeNanoTS() - u64Now, cLoops, TMVirtualSyncGetLag(pVM), u32CatchUpPct);
687
688 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
689 return rc;
690}
691
692
693/**
694 * The global 1 halt method - VMR3Wait() worker.
695 *
696 * @returns VBox status code.
697 * @param pUVCpu Pointer to the user mode VMCPU structure.
698 */
699static DECLCALLBACK(int) vmR3HaltGlobal1Wait(PUVMCPU pUVCpu)
700{
701 ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
702
703 PVM pVM = pUVCpu->pUVM->pVM;
704 PVMCPU pVCpu = VMMGetCpu(pVM);
705 Assert(pVCpu->idCpu == pUVCpu->idCpu);
706
707 int rc = VINF_SUCCESS;
708 for (;;)
709 {
710 /*
711 * Check Relevant FFs.
712 */
713 if ( VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_SUSPENDED_MASK)
714 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_EXTERNAL_SUSPENDED_MASK))
715 break;
716
717 /*
718 * Wait for a while. Someone will wake us up or interrupt the call if
719 * anything needs our attention.
720 */
721 rc = SUPR3CallVMMR0Ex(pVM->pVMR0, pVCpu->idCpu, VMMR0_DO_GVMM_SCHED_HALT, RTTimeNanoTS() + 1000000000 /* +1s */, NULL);
722 if (rc == VERR_INTERRUPTED)
723 rc = VINF_SUCCESS;
724 else if (RT_FAILURE(rc))
725 {
726 rc = vmR3FatalWaitError(pUVCpu, "VMMR0_DO_GVMM_SCHED_HALT->%Rrc\n", rc);
727 break;
728 }
729 }
730
731 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
732 return rc;
733}
734
735
736/**
737 * The global 1 halt method - VMR3NotifyFF() worker.
738 *
739 * @param pUVCpu Pointer to the user mode VMCPU structure.
740 * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
741 */
742static DECLCALLBACK(void) vmR3HaltGlobal1NotifyCpuFF(PUVMCPU pUVCpu, uint32_t fFlags)
743{
744 if (pUVCpu->vm.s.fWait)
745 {
746 int rc = SUPR3CallVMMR0Ex(pUVCpu->pVM->pVMR0, pUVCpu->idCpu, VMMR0_DO_GVMM_SCHED_WAKE_UP, 0, NULL);
747 AssertRC(rc);
748 }
749 else if ( ( (fFlags & VMNOTIFYFF_FLAGS_POKE)
750 || !(fFlags & VMNOTIFYFF_FLAGS_DONE_REM))
751 && pUVCpu->pVCpu)
752 {
753 VMCPUSTATE enmState = VMCPU_GET_STATE(pUVCpu->pVCpu);
754 if (enmState == VMCPUSTATE_STARTED_EXEC)
755 {
756 if (fFlags & VMNOTIFYFF_FLAGS_POKE)
757 {
758 int rc = SUPR3CallVMMR0Ex(pUVCpu->pVM->pVMR0, pUVCpu->idCpu, VMMR0_DO_GVMM_SCHED_POKE, 0, NULL);
759 AssertRC(rc);
760 }
761 }
762 else if (enmState == VMCPUSTATE_STARTED_EXEC_REM)
763 {
764 if (!(fFlags & VMNOTIFYFF_FLAGS_DONE_REM))
765 REMR3NotifyFF(pUVCpu->pVM);
766 }
767 }
768}
769
770
771/**
772 * Bootstrap VMR3Wait() worker.
773 *
774 * @returns VBox status code.
775 * @param pUVMCPU Pointer to the user mode VMCPU structure.
776 */
777static DECLCALLBACK(int) vmR3BootstrapWait(PUVMCPU pUVCpu)
778{
779 PUVM pUVM = pUVCpu->pUVM;
780
781 ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
782
783 int rc = VINF_SUCCESS;
784 for (;;)
785 {
786 /*
787 * Check Relevant FFs.
788 */
789 if (pUVM->vm.s.pReqs) /* global requests pending? */
790 break;
791 if (pUVCpu->vm.s.pReqs) /* local requests pending? */
792 break;
793
794 if ( pUVCpu->pVM
795 && ( VM_FF_ISPENDING(pUVCpu->pVM, VM_FF_EXTERNAL_SUSPENDED_MASK)
796 || VMCPU_FF_ISPENDING(VMMGetCpu(pUVCpu->pVM), VMCPU_FF_EXTERNAL_SUSPENDED_MASK)
797 )
798 )
799 break;
800 if (pUVM->vm.s.fTerminateEMT)
801 break;
802
803 /*
804 * Wait for a while. Someone will wake us up or interrupt the call if
805 * anything needs our attention.
806 */
807 rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, 1000);
808 if (rc == VERR_TIMEOUT)
809 rc = VINF_SUCCESS;
810 else if (RT_FAILURE(rc))
811 {
812 rc = vmR3FatalWaitError(pUVCpu, "RTSemEventWait->%Rrc\n", rc);
813 break;
814 }
815 }
816
817 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
818 return rc;
819}
820
821
822/**
823 * Bootstrap VMR3NotifyFF() worker.
824 *
825 * @param pUVCpu Pointer to the user mode VMCPU structure.
826 * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
827 */
828static DECLCALLBACK(void) vmR3BootstrapNotifyCpuFF(PUVMCPU pUVCpu, uint32_t fFlags)
829{
830 if (pUVCpu->vm.s.fWait)
831 {
832 int rc = RTSemEventSignal(pUVCpu->vm.s.EventSemWait);
833 AssertRC(rc);
834 }
835 NOREF(fFlags);
836}
837
838
839/**
840 * Default VMR3Wait() worker.
841 *
842 * @returns VBox status code.
843 * @param pUVMCPU Pointer to the user mode VMCPU structure.
844 */
845static DECLCALLBACK(int) vmR3DefaultWait(PUVMCPU pUVCpu)
846{
847 ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
848
849 PVM pVM = pUVCpu->pVM;
850 PVMCPU pVCpu = pUVCpu->pVCpu;
851 int rc = VINF_SUCCESS;
852 for (;;)
853 {
854 /*
855 * Check Relevant FFs.
856 */
857 if ( VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_SUSPENDED_MASK)
858 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_EXTERNAL_SUSPENDED_MASK))
859 break;
860
861 /*
862 * Wait for a while. Someone will wake us up or interrupt the call if
863 * anything needs our attention.
864 */
865 rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, 1000);
866 if (rc == VERR_TIMEOUT)
867 rc = VINF_SUCCESS;
868 else if (RT_FAILURE(rc))
869 {
870 rc = vmR3FatalWaitError(pUVCpu, "RTSemEventWait->%Rrc", rc);
871 break;
872 }
873 }
874
875 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
876 return rc;
877}
878
879
880/**
881 * Default VMR3NotifyFF() worker.
882 *
883 * @param pUVCpu Pointer to the user mode VMCPU structure.
884 * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
885 */
886static DECLCALLBACK(void) vmR3DefaultNotifyCpuFF(PUVMCPU pUVCpu, uint32_t fFlags)
887{
888 if (pUVCpu->vm.s.fWait)
889 {
890 int rc = RTSemEventSignal(pUVCpu->vm.s.EventSemWait);
891 AssertRC(rc);
892 }
893 else if ( !(fFlags & VMNOTIFYFF_FLAGS_DONE_REM)
894 && pUVCpu->pVCpu
895 && pUVCpu->pVCpu->enmState == VMCPUSTATE_STARTED_EXEC_REM)
896 REMR3NotifyFF(pUVCpu->pVM);
897}
898
899
900/**
901 * Array with halt method descriptors.
902 * VMINT::iHaltMethod contains an index into this array.
903 */
904static const struct VMHALTMETHODDESC
905{
906 /** The halt method id. */
907 VMHALTMETHOD enmHaltMethod;
908 /** The init function for loading config and initialize variables. */
909 DECLR3CALLBACKMEMBER(int, pfnInit,(PUVM pUVM));
910 /** The term function. */
911 DECLR3CALLBACKMEMBER(void, pfnTerm,(PUVM pUVM));
912 /** The VMR3WaitHaltedU function. */
913 DECLR3CALLBACKMEMBER(int, pfnHalt,(PUVMCPU pUVCpu, const uint32_t fMask, uint64_t u64Now));
914 /** The VMR3WaitU function. */
915 DECLR3CALLBACKMEMBER(int, pfnWait,(PUVMCPU pUVCpu));
916 /** The VMR3NotifyCpuFFU function. */
917 DECLR3CALLBACKMEMBER(void, pfnNotifyCpuFF,(PUVMCPU pUVCpu, uint32_t fFlags));
918 /** The VMR3NotifyGlobalFFU function. */
919 DECLR3CALLBACKMEMBER(void, pfnNotifyGlobalFF,(PUVM pUVM, uint32_t fFlags));
920} g_aHaltMethods[] =
921{
922 { VMHALTMETHOD_BOOTSTRAP, NULL, NULL, NULL, vmR3BootstrapWait, vmR3BootstrapNotifyCpuFF, NULL },
923 { VMHALTMETHOD_OLD, NULL, NULL, vmR3HaltOldDoHalt, vmR3DefaultWait, vmR3DefaultNotifyCpuFF, NULL },
924 { VMHALTMETHOD_1, vmR3HaltMethod1Init, NULL, vmR3HaltMethod1Halt, vmR3DefaultWait, vmR3DefaultNotifyCpuFF, NULL },
925 { VMHALTMETHOD_GLOBAL_1, vmR3HaltGlobal1Init, NULL, vmR3HaltGlobal1Halt, vmR3HaltGlobal1Wait, vmR3HaltGlobal1NotifyCpuFF, NULL },
926};
927
928
929/**
930 * Notify the emulation thread (EMT) about pending Forced Action (FF).
931 *
932 * This function is called by thread other than EMT to make
933 * sure EMT wakes up and promptly service an FF request.
934 *
935 * @param pUVM Pointer to the user mode VM structure.
936 * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
937 */
938VMMR3DECL(void) VMR3NotifyGlobalFFU(PUVM pUVM, uint32_t fFlags)
939{
940 LogFlow(("VMR3NotifyGlobalFFU:\n"));
941 uint32_t iHaldMethod = pUVM->vm.s.iHaltMethod;
942
943 if (g_aHaltMethods[iHaldMethod].pfnNotifyGlobalFF) /** @todo make mandatory. */
944 g_aHaltMethods[iHaldMethod].pfnNotifyGlobalFF(pUVM, fFlags);
945 else
946 for (VMCPUID iCpu = 0; iCpu < pUVM->cCpus; iCpu++)
947 g_aHaltMethods[iHaldMethod].pfnNotifyCpuFF(&pUVM->aCpus[iCpu], fFlags);
948}
949
950
951/**
952 * Notify the emulation thread (EMT) about pending Forced Action (FF).
953 *
954 * This function is called by thread other than EMT to make
955 * sure EMT wakes up and promptly service an FF request.
956 *
957 * @param pUVM Pointer to the user mode VM structure.
958 * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
959 */
960VMMR3DECL(void) VMR3NotifyCpuFFU(PUVMCPU pUVCpu, uint32_t fFlags)
961{
962 PUVM pUVM = pUVCpu->pUVM;
963
964 LogFlow(("VMR3NotifyCpuFFU:\n"));
965 g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnNotifyCpuFF(pUVCpu, fFlags);
966}
967
968
969/**
970 * Halted VM Wait.
971 * Any external event will unblock the thread.
972 *
973 * @returns VINF_SUCCESS unless a fatal error occurred. In the latter
974 * case an appropriate status code is returned.
975 * @param pVM VM handle.
976 * @param pVCpu VMCPU handle.
977 * @param fIgnoreInterrupts If set the VM_FF_INTERRUPT flags is ignored.
978 * @thread The emulation thread.
979 */
980VMMR3DECL(int) VMR3WaitHalted(PVM pVM, PVMCPU pVCpu, bool fIgnoreInterrupts)
981{
982 LogFlow(("VMR3WaitHalted: fIgnoreInterrupts=%d\n", fIgnoreInterrupts));
983
984 /*
985 * Check Relevant FFs.
986 */
987 const uint32_t fMask = !fIgnoreInterrupts
988 ? VMCPU_FF_EXTERNAL_HALTED_MASK
989 : VMCPU_FF_EXTERNAL_HALTED_MASK & ~(VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC);
990 if ( VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
991 || VMCPU_FF_ISPENDING(pVCpu, fMask))
992 {
993 LogFlow(("VMR3WaitHalted: returns VINF_SUCCESS (FF %#x FFCPU %#x)\n", pVM->fGlobalForcedActions, pVCpu->fLocalForcedActions));
994 return VINF_SUCCESS;
995 }
996
997 /*
998 * The yielder is suspended while we're halting, while TM might have clock(s) running
999 * only at certain times and need to be notified..
1000 */
1001 if (pVCpu->idCpu == 0)
1002 VMMR3YieldSuspend(pVM);
1003 TMNotifyStartOfHalt(pVCpu);
1004
1005 /*
1006 * Record halt averages for the last second.
1007 */
1008 PUVMCPU pUVCpu = pVCpu->pUVCpu;
1009 uint64_t u64Now = RTTimeNanoTS();
1010 int64_t off = u64Now - pUVCpu->vm.s.u64HaltsStartTS;
1011 if (off > 1000000000)
1012 {
1013 if (off > _4G || !pUVCpu->vm.s.cHalts)
1014 {
1015 pUVCpu->vm.s.HaltInterval = 1000000000 /* 1 sec */;
1016 pUVCpu->vm.s.HaltFrequency = 1;
1017 }
1018 else
1019 {
1020 pUVCpu->vm.s.HaltInterval = (uint32_t)off / pUVCpu->vm.s.cHalts;
1021 pUVCpu->vm.s.HaltFrequency = ASMMultU64ByU32DivByU32(pUVCpu->vm.s.cHalts, 1000000000, (uint32_t)off);
1022 }
1023 pUVCpu->vm.s.u64HaltsStartTS = u64Now;
1024 pUVCpu->vm.s.cHalts = 0;
1025 }
1026 pUVCpu->vm.s.cHalts++;
1027
1028 /*
1029 * Do the halt.
1030 */
1031 Assert(VMCPU_GET_STATE(pVCpu) == VMCPUSTATE_STARTED);
1032 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED_HALTED);
1033 PUVM pUVM = pUVCpu->pUVM;
1034 int rc = g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnHalt(pUVCpu, fMask, u64Now);
1035 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED);
1036
1037 /*
1038 * Notify TM and resume the yielder
1039 */
1040 TMNotifyEndOfHalt(pVCpu);
1041 if (pVCpu->idCpu == 0)
1042 VMMR3YieldResume(pVM);
1043
1044 LogFlow(("VMR3WaitHalted: returns %Rrc (FF %#x)\n", rc, pVM->fGlobalForcedActions));
1045 return rc;
1046}
1047
1048
1049/**
1050 * Suspended VM Wait.
1051 * Only a handful of forced actions will cause the function to
1052 * return to the caller.
1053 *
1054 * @returns VINF_SUCCESS unless a fatal error occurred. In the latter
1055 * case an appropriate status code is returned.
1056 * @param pUVCpu Pointer to the user mode VMCPU structure.
1057 * @thread The emulation thread.
1058 */
1059VMMR3DECL(int) VMR3WaitU(PUVMCPU pUVCpu)
1060{
1061 LogFlow(("VMR3WaitU:\n"));
1062
1063 /*
1064 * Check Relevant FFs.
1065 */
1066 PVM pVM = pUVCpu->pVM;
1067 PVMCPU pVCpu = pUVCpu->pVCpu;
1068
1069 if ( pVM
1070 && ( VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_SUSPENDED_MASK)
1071 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_EXTERNAL_SUSPENDED_MASK)
1072 )
1073 )
1074 {
1075 LogFlow(("VMR3Wait: returns VINF_SUCCESS (FF %#x)\n", pVM->fGlobalForcedActions));
1076 return VINF_SUCCESS;
1077 }
1078
1079 /*
1080 * Do waiting according to the halt method (so VMR3NotifyFF
1081 * doesn't have to special case anything).
1082 */
1083 PUVM pUVM = pUVCpu->pUVM;
1084 int rc = g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnWait(pUVCpu);
1085 LogFlow(("VMR3WaitU: returns %Rrc (FF %#x)\n", rc, pVM ? pVM->fGlobalForcedActions : 0));
1086 return rc;
1087}
1088
1089
1090/**
1091 * Interface that PDMR3Suspend, PDMR3PowerOff and PDMR3Reset uses when they wait
1092 * for the handling of asynchronous notifications to complete.
1093 *
1094 * @returns VINF_SUCCESS unless a fatal error occurred. In the latter
1095 * case an appropriate status code is returned.
1096 * @param pUVCpu Pointer to the user mode VMCPU structure.
1097 * @thread The emulation thread.
1098 */
1099VMMR3_INT_DECL(int) VMR3AsyncPdmNotificationWaitU(PUVMCPU pUVCpu)
1100{
1101 LogFlow(("VMR3AsyncPdmNotificationWaitU:\n"));
1102 return VMR3WaitU(pUVCpu);
1103}
1104
1105
1106/**
1107 * Interface that PDM the helper asynchronous notification completed methods
1108 * uses for EMT0 when it is waiting inside VMR3AsyncPdmNotificationWaitU().
1109 *
1110 * @param pUVM Pointer to the user mode VM structure.
1111 */
1112VMMR3_INT_DECL(void) VMR3AsyncPdmNotificationWakeupU(PUVM pUVM)
1113{
1114 LogFlow(("VMR3AsyncPdmNotificationWakeupU:\n"));
1115 VM_FF_SET(pUVM->pVM, VM_FF_REQUEST); /* this will have to do for now. */
1116 g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnNotifyCpuFF(&pUVM->aCpus[0], 0 /*fFlags*/);
1117}
1118
1119
1120/**
1121 * Rendezvous callback that will be called once.
1122 *
1123 * @returns VBox strict status code.
1124 * @param pVM VM handle.
1125 * @param pVCpu The VMCPU handle for the calling EMT.
1126 * @param pvUser The new g_aHaltMethods index.
1127 */
1128static DECLCALLBACK(VBOXSTRICTRC) vmR3SetHaltMethodCallback(PVM pVM, PVMCPU pVCpu, void *pvUser)
1129{
1130 PUVM pUVM = pVM->pUVM;
1131 uintptr_t i = (uintptr_t)pvUser;
1132 Assert(i < RT_ELEMENTS(g_aHaltMethods));
1133 NOREF(pVCpu);
1134
1135 /*
1136 * Terminate the old one.
1137 */
1138 if ( pUVM->vm.s.enmHaltMethod != VMHALTMETHOD_INVALID
1139 && g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnTerm)
1140 {
1141 g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnTerm(pUVM);
1142 pUVM->vm.s.enmHaltMethod = VMHALTMETHOD_INVALID;
1143 }
1144
1145 /* Assert that the failure fallback is where we expect. */
1146 Assert(g_aHaltMethods[0].enmHaltMethod == VMHALTMETHOD_BOOTSTRAP);
1147 Assert(!g_aHaltMethods[0].pfnTerm && !g_aHaltMethods[0].pfnInit);
1148
1149 /*
1150 * Init the new one.
1151 */
1152 int rc = VINF_SUCCESS;
1153 memset(&pUVM->vm.s.Halt, 0, sizeof(pUVM->vm.s.Halt));
1154 if (g_aHaltMethods[i].pfnInit)
1155 {
1156 rc = g_aHaltMethods[i].pfnInit(pUVM);
1157 if (RT_FAILURE(rc))
1158 {
1159 /* Fall back on the bootstrap method. This requires no
1160 init/term (see assertion above), and will always work. */
1161 AssertLogRelRC(rc);
1162 i = 0;
1163 }
1164 }
1165
1166 /*
1167 * Commit it.
1168 */
1169 pUVM->vm.s.enmHaltMethod = g_aHaltMethods[i].enmHaltMethod;
1170 ASMAtomicWriteU32(&pUVM->vm.s.iHaltMethod, i);
1171
1172 return rc;
1173}
1174
1175
1176/**
1177 * Changes the halt method.
1178 *
1179 * @returns VBox status code.
1180 * @param pUVM Pointer to the user mode VM structure.
1181 * @param enmHaltMethod The new halt method.
1182 * @thread EMT.
1183 */
1184int vmR3SetHaltMethodU(PUVM pUVM, VMHALTMETHOD enmHaltMethod)
1185{
1186 PVM pVM = pUVM->pVM; Assert(pVM);
1187 VM_ASSERT_EMT(pVM);
1188 AssertReturn(enmHaltMethod > VMHALTMETHOD_INVALID && enmHaltMethod < VMHALTMETHOD_END, VERR_INVALID_PARAMETER);
1189
1190 /*
1191 * Resolve default (can be overridden in the configuration).
1192 */
1193 if (enmHaltMethod == VMHALTMETHOD_DEFAULT)
1194 {
1195 uint32_t u32;
1196 int rc = CFGMR3QueryU32(CFGMR3GetChild(CFGMR3GetRoot(pVM), "VM"), "HaltMethod", &u32);
1197 if (RT_SUCCESS(rc))
1198 {
1199 enmHaltMethod = (VMHALTMETHOD)u32;
1200 if (enmHaltMethod <= VMHALTMETHOD_INVALID || enmHaltMethod >= VMHALTMETHOD_END)
1201 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, N_("Invalid VM/HaltMethod value %d"), enmHaltMethod);
1202 }
1203 else if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_CHILD_NOT_FOUND)
1204 return VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to Query VM/HaltMethod as uint32_t"));
1205 else
1206 enmHaltMethod = VMHALTMETHOD_GLOBAL_1;
1207 //enmHaltMethod = VMHALTMETHOD_1;
1208 //enmHaltMethod = VMHALTMETHOD_OLD;
1209 }
1210 LogRel(("VM: Halt method %s (%d)\n", vmR3GetHaltMethodName(enmHaltMethod), enmHaltMethod));
1211
1212 /*
1213 * Find the descriptor.
1214 */
1215 unsigned i = 0;
1216 while ( i < RT_ELEMENTS(g_aHaltMethods)
1217 && g_aHaltMethods[i].enmHaltMethod != enmHaltMethod)
1218 i++;
1219 AssertReturn(i < RT_ELEMENTS(g_aHaltMethods), VERR_INVALID_PARAMETER);
1220
1221 /*
1222 * This needs to be done while the other EMTs are not sleeping or otherwise messing around.
1223 */
1224 return VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, vmR3SetHaltMethodCallback, (void *)(uintptr_t)i);
1225}
1226
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