VirtualBox

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

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

Automated rebranding to Oracle copyright/license strings via filemuncher

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

© 2023 Oracle
ContactPrivacy policyTerms of Use