VirtualBox

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

Last change on this file since 19435 was 19435, checked in by vboxsync, 16 years ago

VMM: VMCPU::enmState.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 39.0 KB
Line 
1/* $Id: VMEmt.cpp 19435 2009-05-06 14:01:15Z 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 %d -> %d\n", rc, enmBefore, pUVM->pVM ? pUVM->pVM->enmVMState : VMSTATE_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 %d -> %d\n", pUVCpu->idCpu, rc, enmBefore, pUVM->pVM ? pUVM->pVM->enmVMState : VMSTATE_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 break;
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 ( VM_FF_ISSET(pVM, VM_FF_TERMINATE)
152 || pUVM->vm.s.fTerminateEMT)
153 {
154 rc = VINF_EM_TERMINATE;
155 break;
156 }
157 if (pUVM->vm.s.pReqs)
158 {
159 /*
160 * Service execute in any EMT request.
161 */
162 rc = VMR3ReqProcessU(pUVM, VMCPUID_ANY);
163 Log(("vmR3EmulationThread: Req rc=%Rrc, VM state %d -> %d\n", rc, enmBefore, pVM->enmVMState));
164 }
165 else if (pUVCpu->vm.s.pReqs)
166 {
167 /*
168 * Service execute in specific EMT request.
169 */
170 rc = VMR3ReqProcessU(pUVM, pUVCpu->idCpu);
171 Log(("vmR3EmulationThread: Req (cpu=%u) rc=%Rrc, VM state %d -> %d\n", pUVCpu->idCpu, rc, enmBefore, pVM->enmVMState));
172 }
173 else if (VM_FF_ISSET(pVM, VM_FF_DBGF))
174 {
175 /*
176 * Service the debugger request.
177 */
178 rc = DBGFR3VMMForcedAction(pVM);
179 Log(("vmR3EmulationThread: Dbg rc=%Rrc, VM state %d -> %d\n", rc, enmBefore, pVM->enmVMState));
180 }
181 else if (VM_FF_TESTANDCLEAR(pVM, VM_FF_RESET_BIT))
182 {
183 /*
184 * Service a delayed reset request.
185 */
186 rc = VMR3Reset(pVM);
187 VM_FF_CLEAR(pVM, VM_FF_RESET);
188 Log(("vmR3EmulationThread: Reset rc=%Rrc, VM state %d -> %d\n", rc, enmBefore, pVM->enmVMState));
189 }
190 else
191 {
192 /*
193 * Nothing important is pending, so wait for something.
194 */
195 rc = VMR3WaitU(pUVCpu);
196 if (RT_FAILURE(rc))
197 break;
198 }
199
200 /*
201 * Check for termination requests, these have extremely high priority.
202 */
203 if ( rc == VINF_EM_TERMINATE
204 || VM_FF_ISSET(pVM, VM_FF_TERMINATE)
205 || pUVM->vm.s.fTerminateEMT)
206 break;
207 }
208
209 /*
210 * Some requests (both VMR3Req* and the DBGF) can potentially resume
211 * or start the VM, in that case we'll get a change in VM status
212 * indicating that we're now running.
213 */
214 if ( RT_SUCCESS(rc)
215 && pUVM->pVM)
216 {
217 PVM pVM = pUVM->pVM;
218 PVMCPU pVCpu = &pVM->aCpus[idCpu];
219 if ( pVM->enmVMState == VMSTATE_RUNNING
220 && VMCPUSTATE_IS_STARTED(VMCPU_GET_STATE(pVCpu)))
221 {
222 rc = EMR3ExecuteVM(pVM, pVCpu);
223 Log(("vmR3EmulationThread: EMR3ExecuteVM() -> rc=%Rrc, enmVMState=%d\n", rc, pVM->enmVMState));
224 if ( EMGetState(pVCpu) == EMSTATE_GURU_MEDITATION
225 && pVM->enmVMState == VMSTATE_RUNNING)
226 vmR3SetState(pVM, VMSTATE_GURU_MEDITATION);
227 }
228 }
229
230 } /* forever */
231
232
233 /*
234 * Exiting.
235 */
236 Log(("vmR3EmulationThread: Terminating emulation thread! Thread=%#x pUVM=%p rc=%Rrc enmBefore=%d enmVMState=%d\n",
237 ThreadSelf, pUVM, rc, enmBefore, pUVM->pVM ? pUVM->pVM->enmVMState : VMSTATE_TERMINATED));
238 if (pUVM->vm.s.fEMTDoesTheCleanup)
239 {
240 Log(("vmR3EmulationThread: executing delayed Destroy\n"));
241 Assert(pUVM->pVM);
242 vmR3Destroy(pUVM->pVM);
243 vmR3DestroyFinalBitFromEMT(pUVM);
244 }
245 else
246 {
247 vmR3DestroyFinalBitFromEMT(pUVM);
248
249 pUVCpu->vm.s.NativeThreadEMT = NIL_RTNATIVETHREAD;
250 }
251 Log(("vmR3EmulationThread: EMT is terminated.\n"));
252 return rc;
253}
254
255
256/**
257 * Gets the name of a halt method.
258 *
259 * @returns Pointer to a read only string.
260 * @param enmMethod The method.
261 */
262static const char *vmR3GetHaltMethodName(VMHALTMETHOD enmMethod)
263{
264 switch (enmMethod)
265 {
266 case VMHALTMETHOD_BOOTSTRAP: return "bootstrap";
267 case VMHALTMETHOD_DEFAULT: return "default";
268 case VMHALTMETHOD_OLD: return "old";
269 case VMHALTMETHOD_1: return "method1";
270 //case VMHALTMETHOD_2: return "method2";
271 case VMHALTMETHOD_GLOBAL_1: return "global1";
272 default: return "unknown";
273 }
274}
275
276
277/**
278 * The old halt loop.
279 */
280static DECLCALLBACK(int) vmR3HaltOldDoHalt(PUVMCPU pUVCpu, const uint32_t fMask, uint64_t /* u64Now*/)
281{
282 /*
283 * Halt loop.
284 */
285 PVM pVM = pUVCpu->pVM;
286 PVMCPU pVCpu = pUVCpu->pVCpu;
287
288 int rc = VINF_SUCCESS;
289 ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
290 //unsigned cLoops = 0;
291 for (;;)
292 {
293 /*
294 * Work the timers and check if we can exit.
295 * The poll call gives us the ticks left to the next event in
296 * addition to perhaps set an FF.
297 */
298 STAM_REL_PROFILE_START(&pUVCpu->vm.s.StatHaltTimers, b);
299 TMR3TimerQueuesDo(pVM);
300 STAM_REL_PROFILE_STOP(&pUVCpu->vm.s.StatHaltTimers, b);
301 if ( VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
302 || VMCPU_FF_ISPENDING(pVCpu, fMask))
303 break;
304 uint64_t u64NanoTS = TMVirtualToNano(pVM, TMTimerPoll(pVM));
305 if ( VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
306 || VMCPU_FF_ISPENDING(pVCpu, fMask))
307 break;
308
309 /*
310 * Wait for a while. Someone will wake us up or interrupt the call if
311 * anything needs our attention.
312 */
313 if (u64NanoTS < 50000)
314 {
315 //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d spin\n", u64NanoTS, cLoops++);
316 /* spin */;
317 }
318 else
319 {
320 VMMR3YieldStop(pVM);
321 //uint64_t u64Start = RTTimeNanoTS();
322 if (u64NanoTS < 870000) /* this is a bit speculative... works fine on linux. */
323 {
324 //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d yield", u64NanoTS, cLoops++);
325 STAM_REL_PROFILE_START(&pUVCpu->vm.s.StatHaltYield, a);
326 RTThreadYield(); /* this is the best we can do here */
327 STAM_REL_PROFILE_STOP(&pUVCpu->vm.s.StatHaltYield, a);
328 }
329 else if (u64NanoTS < 2000000)
330 {
331 //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d sleep 1ms", u64NanoTS, cLoops++);
332 STAM_REL_PROFILE_START(&pUVCpu->vm.s.StatHaltBlock, a);
333 rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, 1);
334 STAM_REL_PROFILE_STOP(&pUVCpu->vm.s.StatHaltBlock, a);
335 }
336 else
337 {
338 //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d sleep %dms", u64NanoTS, cLoops++, (uint32_t)RT_MIN((u64NanoTS - 500000) / 1000000, 15));
339 STAM_REL_PROFILE_START(&pUVCpu->vm.s.StatHaltBlock, a);
340 rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, RT_MIN((u64NanoTS - 1000000) / 1000000, 15));
341 STAM_REL_PROFILE_STOP(&pUVCpu->vm.s.StatHaltBlock, a);
342 }
343 //uint64_t u64Slept = RTTimeNanoTS() - u64Start;
344 //RTLogPrintf(" -> rc=%Rrc in %RU64 ns / %RI64 ns delta\n", rc, u64Slept, u64NanoTS - u64Slept);
345 }
346 if (rc == VERR_TIMEOUT)
347 rc = VINF_SUCCESS;
348 else if (RT_FAILURE(rc))
349 {
350 AssertRC(rc != VERR_INTERRUPTED);
351 AssertMsgFailed(("RTSemEventWait->%Rrc\n", rc));
352 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fTerminateEMT, true);
353 VM_FF_SET(pVM, VM_FF_TERMINATE);
354 rc = VERR_INTERNAL_ERROR;
355 break;
356 }
357 }
358
359 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
360 return rc;
361}
362
363
364/**
365 * Initialize the configuration of halt method 1 & 2.
366 *
367 * @return VBox status code. Failure on invalid CFGM data.
368 * @param pVM The VM handle.
369 */
370static int vmR3HaltMethod12ReadConfigU(PUVM pUVM)
371{
372 /*
373 * The defaults.
374 */
375#if 1 /* DEBUGGING STUFF - REMOVE LATER */
376 pUVM->vm.s.Halt.Method12.u32LagBlockIntervalDivisorCfg = 4;
377 pUVM->vm.s.Halt.Method12.u32MinBlockIntervalCfg = 2*1000000;
378 pUVM->vm.s.Halt.Method12.u32MaxBlockIntervalCfg = 75*1000000;
379 pUVM->vm.s.Halt.Method12.u32StartSpinningCfg = 30*1000000;
380 pUVM->vm.s.Halt.Method12.u32StopSpinningCfg = 20*1000000;
381#else
382 pUVM->vm.s.Halt.Method12.u32LagBlockIntervalDivisorCfg = 4;
383 pUVM->vm.s.Halt.Method12.u32MinBlockIntervalCfg = 5*1000000;
384 pUVM->vm.s.Halt.Method12.u32MaxBlockIntervalCfg = 200*1000000;
385 pUVM->vm.s.Halt.Method12.u32StartSpinningCfg = 20*1000000;
386 pUVM->vm.s.Halt.Method12.u32StopSpinningCfg = 2*1000000;
387#endif
388
389 /*
390 * Query overrides.
391 *
392 * I don't have time to bother with niceities such as invalid value checks
393 * here right now. sorry.
394 */
395 PCFGMNODE pCfg = CFGMR3GetChild(CFGMR3GetRoot(pUVM->pVM), "/VMM/HaltedMethod1");
396 if (pCfg)
397 {
398 uint32_t u32;
399 if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "LagBlockIntervalDivisor", &u32)))
400 pUVM->vm.s.Halt.Method12.u32LagBlockIntervalDivisorCfg = u32;
401 if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "MinBlockInterval", &u32)))
402 pUVM->vm.s.Halt.Method12.u32MinBlockIntervalCfg = u32;
403 if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "MaxBlockInterval", &u32)))
404 pUVM->vm.s.Halt.Method12.u32MaxBlockIntervalCfg = u32;
405 if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "StartSpinning", &u32)))
406 pUVM->vm.s.Halt.Method12.u32StartSpinningCfg = u32;
407 if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "StopSpinning", &u32)))
408 pUVM->vm.s.Halt.Method12.u32StopSpinningCfg = u32;
409 LogRel(("HaltedMethod1 config: %d/%d/%d/%d/%d\n",
410 pUVM->vm.s.Halt.Method12.u32LagBlockIntervalDivisorCfg,
411 pUVM->vm.s.Halt.Method12.u32MinBlockIntervalCfg,
412 pUVM->vm.s.Halt.Method12.u32MaxBlockIntervalCfg,
413 pUVM->vm.s.Halt.Method12.u32StartSpinningCfg,
414 pUVM->vm.s.Halt.Method12.u32StopSpinningCfg));
415 }
416
417 return VINF_SUCCESS;
418}
419
420
421/**
422 * Initialize halt method 1.
423 *
424 * @return VBox status code.
425 * @param pUVM Pointer to the user mode VM structure.
426 */
427static DECLCALLBACK(int) vmR3HaltMethod1Init(PUVM pUVM)
428{
429 return vmR3HaltMethod12ReadConfigU(pUVM);
430}
431
432
433/**
434 * Method 1 - Block whenever possible, and when lagging behind
435 * switch to spinning for 10-30ms with occational blocking until
436 * the lag has been eliminated.
437 */
438static DECLCALLBACK(int) vmR3HaltMethod1Halt(PUVMCPU pUVCpu, const uint32_t fMask, uint64_t u64Now)
439{
440 PUVM pUVM = pUVCpu->pUVM;
441 PVMCPU pVCpu = pUVCpu->pVCpu;
442 PVM pVM = pUVCpu->pVM;
443
444 /*
445 * To simplify things, we decide up-front whether we should switch to spinning or
446 * not. This makes some ASSUMPTIONS about the cause of the spinning (PIT/RTC/PCNet)
447 * and that it will generate interrupts or other events that will cause us to exit
448 * the halt loop.
449 */
450 bool fBlockOnce = false;
451 bool fSpinning = false;
452 uint32_t u32CatchUpPct = TMVirtualSyncGetCatchUpPct(pVM);
453 if (u32CatchUpPct /* non-zero if catching up */)
454 {
455 if (pUVCpu->vm.s.Halt.Method12.u64StartSpinTS)
456 {
457 fSpinning = TMVirtualSyncGetLag(pVM) >= pUVM->vm.s.Halt.Method12.u32StopSpinningCfg;
458 if (fSpinning)
459 {
460 uint64_t u64Lag = TMVirtualSyncGetLag(pVM);
461 fBlockOnce = u64Now - pUVCpu->vm.s.Halt.Method12.u64LastBlockTS
462 > RT_MAX(pUVM->vm.s.Halt.Method12.u32MinBlockIntervalCfg,
463 RT_MIN(u64Lag / pUVM->vm.s.Halt.Method12.u32LagBlockIntervalDivisorCfg,
464 pUVM->vm.s.Halt.Method12.u32MaxBlockIntervalCfg));
465 }
466 else
467 {
468 //RTLogRelPrintf("Stopped spinning (%u ms)\n", (u64Now - pUVCpu->vm.s.Halt.Method12.u64StartSpinTS) / 1000000);
469 pUVCpu->vm.s.Halt.Method12.u64StartSpinTS = 0;
470 }
471 }
472 else
473 {
474 fSpinning = TMVirtualSyncGetLag(pVM) >= pUVM->vm.s.Halt.Method12.u32StartSpinningCfg;
475 if (fSpinning)
476 pUVCpu->vm.s.Halt.Method12.u64StartSpinTS = u64Now;
477 }
478 }
479 else if (pUVCpu->vm.s.Halt.Method12.u64StartSpinTS)
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 /*
486 * Halt loop.
487 */
488 int rc = VINF_SUCCESS;
489 ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
490 unsigned cLoops = 0;
491 for (;; cLoops++)
492 {
493 /*
494 * Work the timers and check if we can exit.
495 */
496 STAM_REL_PROFILE_START(&pUVCpu->vm.s.StatHaltTimers, b);
497 TMR3TimerQueuesDo(pVM);
498 STAM_REL_PROFILE_STOP(&pUVCpu->vm.s.StatHaltTimers, b);
499 if ( VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
500 || VMCPU_FF_ISPENDING(pVCpu, fMask))
501 break;
502
503 /*
504 * Estimate time left to the next event.
505 */
506 uint64_t u64NanoTS = TMVirtualToNano(pVM, TMTimerPoll(pVM));
507 if ( VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
508 || VMCPU_FF_ISPENDING(pVCpu, fMask))
509 break;
510
511 /*
512 * Block if we're not spinning and the interval isn't all that small.
513 */
514 if ( ( !fSpinning
515 || fBlockOnce)
516#if 1 /* DEBUGGING STUFF - REMOVE LATER */
517 && u64NanoTS >= 100000) /* 0.100 ms */
518#else
519 && u64NanoTS >= 250000) /* 0.250 ms */
520#endif
521 {
522 const uint64_t Start = pUVCpu->vm.s.Halt.Method12.u64LastBlockTS = RTTimeNanoTS();
523 VMMR3YieldStop(pVM);
524
525 uint32_t cMilliSecs = RT_MIN(u64NanoTS / 1000000, 15);
526 if (cMilliSecs <= pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLongAvg)
527 cMilliSecs = 1;
528 else
529 cMilliSecs -= pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLongAvg;
530 //RTLogRelPrintf("u64NanoTS=%RI64 cLoops=%3d sleep %02dms (%7RU64) ", u64NanoTS, cLoops, cMilliSecs, u64NanoTS);
531 STAM_REL_PROFILE_START(&pUVCpu->vm.s.StatHaltBlock, a);
532 rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, cMilliSecs);
533 STAM_REL_PROFILE_STOP(&pUVCpu->vm.s.StatHaltBlock, a);
534 if (rc == VERR_TIMEOUT)
535 rc = VINF_SUCCESS;
536 else if (RT_FAILURE(rc))
537 {
538 AssertRC(rc != VERR_INTERRUPTED);
539 AssertMsgFailed(("RTSemEventWait->%Rrc\n", rc));
540 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fTerminateEMT, true);
541 VM_FF_SET(pVM, VM_FF_TERMINATE);
542 rc = VERR_INTERNAL_ERROR;
543 break;
544 }
545
546 /*
547 * Calc the statistics.
548 * Update averages every 16th time, and flush parts of the history every 64th time.
549 */
550 const uint64_t Elapsed = RTTimeNanoTS() - Start;
551 pUVCpu->vm.s.Halt.Method12.cNSBlocked += Elapsed;
552 if (Elapsed > u64NanoTS)
553 pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLong += Elapsed - u64NanoTS;
554 pUVCpu->vm.s.Halt.Method12.cBlocks++;
555 if (!(pUVCpu->vm.s.Halt.Method12.cBlocks & 0xf))
556 {
557 pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLongAvg = pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLong / pUVCpu->vm.s.Halt.Method12.cBlocks;
558 if (!(pUVCpu->vm.s.Halt.Method12.cBlocks & 0x3f))
559 {
560 pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLong = pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLongAvg * 0x40;
561 pUVCpu->vm.s.Halt.Method12.cBlocks = 0x40;
562 }
563 }
564 //RTLogRelPrintf(" -> %7RU64 ns / %7RI64 ns delta%s\n", Elapsed, Elapsed - u64NanoTS, fBlockOnce ? " (block once)" : "");
565
566 /*
567 * Clear the block once flag if we actually blocked.
568 */
569 if ( fBlockOnce
570 && Elapsed > 100000 /* 0.1 ms */)
571 fBlockOnce = false;
572 }
573 }
574 //if (fSpinning) RTLogRelPrintf("spun for %RU64 ns %u loops; lag=%RU64 pct=%d\n", RTTimeNanoTS() - u64Now, cLoops, TMVirtualSyncGetLag(pVM), u32CatchUpPct);
575
576 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
577 return rc;
578}
579
580
581/**
582 * Initialize the global 1 halt method.
583 *
584 * @return VBox status code.
585 * @param pUVM Pointer to the user mode VM structure.
586 */
587static DECLCALLBACK(int) vmR3HaltGlobal1Init(PUVM pUVM)
588{
589 return VINF_SUCCESS;
590}
591
592
593/**
594 * The global 1 halt method - Block in GMM (ring-0) and let it
595 * try take care of the global scheduling of EMT threads.
596 */
597static DECLCALLBACK(int) vmR3HaltGlobal1Halt(PUVMCPU pUVCpu, const uint32_t fMask, uint64_t u64Now)
598{
599 PUVM pUVM = pUVCpu->pUVM;
600 PVMCPU pVCpu = pUVCpu->pVCpu;
601 PVM pVM = pUVCpu->pVM;
602 Assert(VMMGetCpu(pVM) == pVCpu);
603
604 /*
605 * Halt loop.
606 */
607 int rc = VINF_SUCCESS;
608 ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
609 unsigned cLoops = 0;
610 for (;; cLoops++)
611 {
612 /*
613 * Work the timers and check if we can exit.
614 */
615 STAM_REL_PROFILE_START(&pUVCpu->vm.s.StatHaltTimers, b);
616 TMR3TimerQueuesDo(pVM);
617 STAM_REL_PROFILE_STOP(&pUVCpu->vm.s.StatHaltTimers, b);
618 if ( VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
619 || VMCPU_FF_ISPENDING(pVCpu, fMask))
620 break;
621
622 /*
623 * Estimate time left to the next event.
624 */
625 uint64_t u64Delta;
626 uint64_t u64GipTime = TMTimerPollGIP(pVM, &u64Delta);
627 if ( VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
628 || VMCPU_FF_ISPENDING(pVCpu, fMask))
629 break;
630
631 /*
632 * Block if we're not spinning and the interval isn't all that small.
633 */
634 if (u64Delta > 50000 /* 0.050ms */)
635 {
636 VMMR3YieldStop(pVM);
637 if ( VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
638 || VMCPU_FF_ISPENDING(pVCpu, fMask))
639 break;
640
641 //RTLogRelPrintf("u64NanoTS=%RI64 cLoops=%3d sleep %02dms (%7RU64) ", u64NanoTS, cLoops, cMilliSecs, u64NanoTS);
642 STAM_REL_PROFILE_START(&pUVCpu->vm.s.StatHaltBlock, c);
643 rc = SUPCallVMMR0Ex(pVM->pVMR0, pVCpu->idCpu, VMMR0_DO_GVMM_SCHED_HALT, u64GipTime, NULL);
644 STAM_REL_PROFILE_STOP(&pUVCpu->vm.s.StatHaltBlock, c);
645 if (rc == VERR_INTERRUPTED)
646 rc = VINF_SUCCESS;
647 else if (RT_FAILURE(rc))
648 {
649 AssertMsgFailed(("VMMR0_DO_GVMM_SCHED_HALT->%Rrc\n", rc));
650 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fTerminateEMT, true);
651 VM_FF_SET(pVM, VM_FF_TERMINATE);
652 rc = VERR_INTERNAL_ERROR;
653 break;
654 }
655 }
656 /*
657 * When spinning call upon the GVMM and do some wakups once
658 * in a while, it's not like we're actually busy or anything.
659 */
660 else if (!(cLoops & 0x1fff))
661 {
662 STAM_REL_PROFILE_START(&pUVCpu->vm.s.StatHaltYield, d);
663 rc = SUPCallVMMR0Ex(pVM->pVMR0, pVCpu->idCpu, VMMR0_DO_GVMM_SCHED_POLL, false /* don't yield */, NULL);
664 STAM_REL_PROFILE_STOP(&pUVCpu->vm.s.StatHaltYield, d);
665 }
666 }
667 //if (fSpinning) RTLogRelPrintf("spun for %RU64 ns %u loops; lag=%RU64 pct=%d\n", RTTimeNanoTS() - u64Now, cLoops, TMVirtualSyncGetLag(pVM), u32CatchUpPct);
668
669 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
670 return rc;
671}
672
673
674/**
675 * The global 1 halt method - VMR3Wait() worker.
676 *
677 * @returns VBox status code.
678 * @param pUVCpu Pointer to the user mode VMCPU structure.
679 */
680static DECLCALLBACK(int) vmR3HaltGlobal1Wait(PUVMCPU pUVCpu)
681{
682 ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
683
684 PVM pVM = pUVCpu->pUVM->pVM;
685 PVMCPU pVCpu = VMMGetCpu(pVM);
686 Assert(pVCpu->idCpu == pUVCpu->idCpu);
687
688 int rc = VINF_SUCCESS;
689 for (;;)
690 {
691 /*
692 * Check Relevant FFs.
693 */
694 if ( VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_SUSPENDED_MASK)
695 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_EXTERNAL_SUSPENDED_MASK))
696 break;
697
698 /*
699 * Wait for a while. Someone will wake us up or interrupt the call if
700 * anything needs our attention.
701 */
702 rc = SUPCallVMMR0Ex(pVM->pVMR0, pVCpu->idCpu, VMMR0_DO_GVMM_SCHED_HALT, RTTimeNanoTS() + 1000000000 /* +1s */, NULL);
703 if (rc == VERR_INTERRUPTED)
704 rc = VINF_SUCCESS;
705 else if (RT_FAILURE(rc))
706 {
707 AssertMsgFailed(("RTSemEventWait->%Rrc\n", rc));
708 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fTerminateEMT, true);
709 VM_FF_SET(pVM, VM_FF_TERMINATE);
710 rc = VERR_INTERNAL_ERROR;
711 break;
712 }
713
714 }
715
716 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
717 return rc;
718}
719
720
721/**
722 * The global 1 halt method - VMR3NotifyFF() worker.
723 *
724 * @param pUVCpu Pointer to the user mode VMCPU structure.
725 * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
726 */
727static DECLCALLBACK(void) vmR3HaltGlobal1NotifyCpuFF(PUVMCPU pUVCpu, uint32_t fFlags)
728{
729 if (pUVCpu->vm.s.fWait)
730 {
731 int rc = SUPCallVMMR0Ex(pUVCpu->pVM->pVMR0, pUVCpu->idCpu, VMMR0_DO_GVMM_SCHED_WAKE_UP, 0, NULL);
732 AssertRC(rc);
733 }
734 else if ( (fFlags & VMNOTIFYFF_FLAGS_POKE)
735 && pUVCpu->pVCpu
736 && pUVCpu->pVCpu->enmState == VMCPUSTATE_STARTED_EXEC)
737 {
738 int rc = SUPCallVMMR0Ex(pUVCpu->pVM->pVMR0, pUVCpu->idCpu, VMMR0_DO_GVMM_SCHED_POKE, 0, NULL);
739 AssertRC(rc);
740 }
741 else if (!(fFlags & VMNOTIFYFF_FLAGS_DONE_REM)) /** @todo use VMCPUSTATE_RUN_EXEC_REM */
742 REMR3NotifyFF(pUVCpu->pVM);
743}
744
745
746/**
747 * Bootstrap VMR3Wait() worker.
748 *
749 * @returns VBox status code.
750 * @param pUVMCPU Pointer to the user mode VMCPU structure.
751 */
752static DECLCALLBACK(int) vmR3BootstrapWait(PUVMCPU pUVCpu)
753{
754 PUVM pUVM = pUVCpu->pUVM;
755
756 ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
757
758 int rc = VINF_SUCCESS;
759 for (;;)
760 {
761 /*
762 * Check Relevant FFs.
763 */
764 if (pUVM->vm.s.pReqs) /* global requests pending? */
765 break;
766 if (pUVCpu->vm.s.pReqs) /* local requests pending? */
767 break;
768
769 if ( pUVCpu->pVM
770 && ( VM_FF_ISPENDING(pUVCpu->pVM, VM_FF_EXTERNAL_SUSPENDED_MASK)
771 || VMCPU_FF_ISPENDING(VMMGetCpu(pUVCpu->pVM), VMCPU_FF_EXTERNAL_SUSPENDED_MASK)
772 )
773 )
774 break;
775 if (pUVCpu->vm.s.fTerminateEMT)
776 break;
777
778 /*
779 * Wait for a while. Someone will wake us up or interrupt the call if
780 * anything needs our attention.
781 */
782 rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, 1000);
783 if (rc == VERR_TIMEOUT)
784 rc = VINF_SUCCESS;
785 else if (RT_FAILURE(rc))
786 {
787 AssertMsgFailed(("RTSemEventWait->%Rrc\n", rc));
788 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fTerminateEMT, true);
789 if (pUVCpu->pVM)
790 VM_FF_SET(pUVCpu->pVM, VM_FF_TERMINATE);
791 rc = VERR_INTERNAL_ERROR;
792 break;
793 }
794
795 }
796
797 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
798 return rc;
799}
800
801
802/**
803 * Bootstrap VMR3NotifyFF() worker.
804 *
805 * @param pUVCpu Pointer to the user mode VMCPU structure.
806 * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
807 */
808static DECLCALLBACK(void) vmR3BootstrapNotifyCpuFF(PUVMCPU pUVCpu, uint32_t fFlags)
809{
810 if (pUVCpu->vm.s.fWait)
811 {
812 int rc = RTSemEventSignal(pUVCpu->vm.s.EventSemWait);
813 AssertRC(rc);
814 }
815 NOREF(fFlags);
816}
817
818
819/**
820 * Default VMR3Wait() worker.
821 *
822 * @returns VBox status code.
823 * @param pUVMCPU Pointer to the user mode VMCPU structure.
824 */
825static DECLCALLBACK(int) vmR3DefaultWait(PUVMCPU pUVCpu)
826{
827 ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
828
829 PVM pVM = pUVCpu->pVM;
830 PVMCPU pVCpu = pUVCpu->pVCpu;
831 int rc = VINF_SUCCESS;
832 for (;;)
833 {
834 /*
835 * Check Relevant FFs.
836 */
837 if ( VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_SUSPENDED_MASK)
838 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_EXTERNAL_SUSPENDED_MASK))
839 break;
840
841 /*
842 * Wait for a while. Someone will wake us up or interrupt the call if
843 * anything needs our attention.
844 */
845 rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, 1000);
846 if (rc == VERR_TIMEOUT)
847 rc = VINF_SUCCESS;
848 else if (RT_FAILURE(rc))
849 {
850 AssertMsgFailed(("RTSemEventWait->%Rrc\n", rc));
851 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fTerminateEMT, true);
852 VM_FF_SET(pVM, VM_FF_TERMINATE);
853 rc = VERR_INTERNAL_ERROR;
854 break;
855 }
856
857 }
858
859 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
860 return rc;
861}
862
863
864/**
865 * Default VMR3NotifyFF() worker.
866 *
867 * @param pUVCpu Pointer to the user mode VMCPU structure.
868 * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
869 */
870static DECLCALLBACK(void) vmR3DefaultNotifyCpuFF(PUVMCPU pUVCpu, uint32_t fFlags)
871{
872 if (pUVCpu->vm.s.fWait)
873 {
874 int rc = RTSemEventSignal(pUVCpu->vm.s.EventSemWait);
875 AssertRC(rc);
876 }
877 else if (!(fFlags & VMNOTIFYFF_FLAGS_DONE_REM))
878 REMR3NotifyFF(pUVCpu->pVM);
879}
880
881
882/**
883 * Array with halt method descriptors.
884 * VMINT::iHaltMethod contains an index into this array.
885 */
886static const struct VMHALTMETHODDESC
887{
888 /** The halt method id. */
889 VMHALTMETHOD enmHaltMethod;
890 /** The init function for loading config and initialize variables. */
891 DECLR3CALLBACKMEMBER(int, pfnInit,(PUVM pUVM));
892 /** The term function. */
893 DECLR3CALLBACKMEMBER(void, pfnTerm,(PUVM pUVM));
894 /** The VMR3WaitHaltedU function. */
895 DECLR3CALLBACKMEMBER(int, pfnHalt,(PUVMCPU pUVCpu, const uint32_t fMask, uint64_t u64Now));
896 /** The VMR3WaitU function. */
897 DECLR3CALLBACKMEMBER(int, pfnWait,(PUVMCPU pUVCpu));
898 /** The VMR3NotifyCpuFFU function. */
899 DECLR3CALLBACKMEMBER(void, pfnNotifyCpuFF,(PUVMCPU pUVCpu, uint32_t fFlags));
900 /** The VMR3NotifyGlobalFFU function. */
901 DECLR3CALLBACKMEMBER(void, pfnNotifyGlobalFF,(PUVM pUVM, uint32_t fFlags));
902} g_aHaltMethods[] =
903{
904 { VMHALTMETHOD_BOOTSTRAP, NULL, NULL, NULL, vmR3BootstrapWait, vmR3BootstrapNotifyCpuFF, NULL },
905 { VMHALTMETHOD_OLD, NULL, NULL, vmR3HaltOldDoHalt, vmR3DefaultWait, vmR3DefaultNotifyCpuFF, NULL },
906 { VMHALTMETHOD_1, vmR3HaltMethod1Init, NULL, vmR3HaltMethod1Halt, vmR3DefaultWait, vmR3DefaultNotifyCpuFF, NULL },
907 { VMHALTMETHOD_GLOBAL_1, vmR3HaltGlobal1Init, NULL, vmR3HaltGlobal1Halt, vmR3HaltGlobal1Wait, vmR3HaltGlobal1NotifyCpuFF, NULL },
908};
909
910
911/**
912 * Notify the emulation thread (EMT) about pending Forced Action (FF).
913 *
914 * This function is called by thread other than EMT to make
915 * sure EMT wakes up and promptly service an FF request.
916 *
917 * @param pUVM Pointer to the user mode VM structure.
918 * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
919 */
920VMMR3DECL(void) VMR3NotifyGlobalFFU(PUVM pUVM, uint32_t fFlags)
921{
922 LogFlow(("VMR3NotifyGlobalFFU:\n"));
923 uint32_t iHaldMethod = pUVM->vm.s.iHaltMethod;
924
925 if (g_aHaltMethods[iHaldMethod].pfnNotifyGlobalFF) /** @todo make mandatory. */
926 g_aHaltMethods[iHaldMethod].pfnNotifyGlobalFF(pUVM, fFlags);
927 else
928 for (VMCPUID iCpu = 0; iCpu < pUVM->cCpus; iCpu++)
929 g_aHaltMethods[iHaldMethod].pfnNotifyCpuFF(&pUVM->aCpus[iCpu], fFlags);
930}
931
932
933/**
934 * Notify the emulation thread (EMT) about pending Forced Action (FF).
935 *
936 * This function is called by thread other than EMT to make
937 * sure EMT wakes up and promptly service an FF request.
938 *
939 * @param pUVM Pointer to the user mode VM structure.
940 * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
941 */
942VMMR3DECL(void) VMR3NotifyCpuFFU(PUVMCPU pUVCpu, uint32_t fFlags)
943{
944 PUVM pUVM = pUVCpu->pUVM;
945
946 LogFlow(("VMR3NotifyCpuFFU:\n"));
947 g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnNotifyCpuFF(pUVCpu, fFlags);
948}
949
950
951/**
952 * Halted VM Wait.
953 * Any external event will unblock the thread.
954 *
955 * @returns VINF_SUCCESS unless a fatal error occured. In the latter
956 * case an appropriate status code is returned.
957 * @param pVM VM handle.
958 * @param pVCpu VMCPU handle.
959 * @param fIgnoreInterrupts If set the VM_FF_INTERRUPT flags is ignored.
960 * @thread The emulation thread.
961 */
962VMMR3DECL(int) VMR3WaitHalted(PVM pVM, PVMCPU pVCpu, bool fIgnoreInterrupts)
963{
964 LogFlow(("VMR3WaitHalted: fIgnoreInterrupts=%d\n", fIgnoreInterrupts));
965
966 /*
967 * Check Relevant FFs.
968 */
969 const uint32_t fMask = !fIgnoreInterrupts
970 ? VMCPU_FF_EXTERNAL_HALTED_MASK
971 : VMCPU_FF_EXTERNAL_HALTED_MASK & ~(VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC);
972 if ( VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_HALTED_MASK)
973 || VMCPU_FF_ISPENDING(pVCpu, fMask))
974 {
975 LogFlow(("VMR3WaitHalted: returns VINF_SUCCESS (FF %#x FFCPU %#x)\n", pVM->fGlobalForcedActions, pVCpu->fLocalForcedActions));
976 return VINF_SUCCESS;
977 }
978
979 /*
980 * The yielder is suspended while we're halting, while TM might have clock(s) running
981 * only at certain times and need to be notified..
982 */
983 VMMR3YieldSuspend(pVM);
984 TMNotifyStartOfHalt(pVCpu);
985
986 /*
987 * Record halt averages for the last second.
988 */
989 PUVMCPU pUVCpu = pVCpu->pUVCpu;
990 uint64_t u64Now = RTTimeNanoTS();
991 int64_t off = u64Now - pUVCpu->vm.s.u64HaltsStartTS;
992 if (off > 1000000000)
993 {
994 if (off > _4G || !pUVCpu->vm.s.cHalts)
995 {
996 pUVCpu->vm.s.HaltInterval = 1000000000 /* 1 sec */;
997 pUVCpu->vm.s.HaltFrequency = 1;
998 }
999 else
1000 {
1001 pUVCpu->vm.s.HaltInterval = (uint32_t)off / pUVCpu->vm.s.cHalts;
1002 pUVCpu->vm.s.HaltFrequency = ASMMultU64ByU32DivByU32(pUVCpu->vm.s.cHalts, 1000000000, (uint32_t)off);
1003 }
1004 pUVCpu->vm.s.u64HaltsStartTS = u64Now;
1005 pUVCpu->vm.s.cHalts = 0;
1006 }
1007 pUVCpu->vm.s.cHalts++;
1008
1009 /*
1010 * Do the halt.
1011 */
1012 Assert(VMCPU_GET_STATE(pVCpu) == VMCPUSTATE_STARTED);
1013 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED_HALTED);
1014 PUVM pUVM = pUVCpu->pUVM;
1015 int rc = g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnHalt(pUVCpu, fMask, u64Now);
1016 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED);
1017
1018 /*
1019 * Notify TM and resume the yielder
1020 */
1021 TMNotifyEndOfHalt(pVCpu);
1022 VMMR3YieldResume(pVM);
1023
1024 LogFlow(("VMR3WaitHalted: returns %Rrc (FF %#x)\n", rc, pVM->fGlobalForcedActions));
1025 return rc;
1026}
1027
1028
1029/**
1030 * Suspended VM Wait.
1031 * Only a handful of forced actions will cause the function to
1032 * return to the caller.
1033 *
1034 * @returns VINF_SUCCESS unless a fatal error occured. In the latter
1035 * case an appropriate status code is returned.
1036 * @param pUVCpu Pointer to the user mode VMCPU structure.
1037 * @thread The emulation thread.
1038 */
1039VMMR3DECL(int) VMR3WaitU(PUVMCPU pUVCpu)
1040{
1041 LogFlow(("VMR3WaitU:\n"));
1042
1043 /*
1044 * Check Relevant FFs.
1045 */
1046 PVM pVM = pUVCpu->pVM;
1047 PVMCPU pVCpu = pUVCpu->pVCpu;
1048
1049 if ( pVM
1050 && ( VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_SUSPENDED_MASK)
1051 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_EXTERNAL_SUSPENDED_MASK)
1052 )
1053 )
1054 {
1055 LogFlow(("VMR3Wait: returns VINF_SUCCESS (FF %#x)\n", pVM->fGlobalForcedActions));
1056 return VINF_SUCCESS;
1057 }
1058
1059 /*
1060 * Do waiting according to the halt method (so VMR3NotifyFF
1061 * doesn't have to special case anything).
1062 */
1063 PUVM pUVM = pUVCpu->pUVM;
1064 int rc = g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnWait(pUVCpu);
1065 LogFlow(("VMR3WaitU: returns %Rrc (FF %#x)\n", rc, pVM ? pVM->fGlobalForcedActions : 0));
1066 return rc;
1067}
1068
1069
1070/**
1071 * Changes the halt method.
1072 *
1073 * @returns VBox status code.
1074 * @param pUVM Pointer to the user mode VM structure.
1075 * @param enmHaltMethod The new halt method.
1076 * @thread EMT.
1077 */
1078int vmR3SetHaltMethodU(PUVM pUVM, VMHALTMETHOD enmHaltMethod)
1079{
1080 PVM pVM = pUVM->pVM; Assert(pVM);
1081 VM_ASSERT_EMT(pVM);
1082 AssertReturn(enmHaltMethod > VMHALTMETHOD_INVALID && enmHaltMethod < VMHALTMETHOD_END, VERR_INVALID_PARAMETER);
1083
1084 /*
1085 * Resolve default (can be overridden in the configuration).
1086 */
1087 if (enmHaltMethod == VMHALTMETHOD_DEFAULT)
1088 {
1089 uint32_t u32;
1090 int rc = CFGMR3QueryU32(CFGMR3GetChild(CFGMR3GetRoot(pVM), "VM"), "HaltMethod", &u32);
1091 if (RT_SUCCESS(rc))
1092 {
1093 enmHaltMethod = (VMHALTMETHOD)u32;
1094 if (enmHaltMethod <= VMHALTMETHOD_INVALID || enmHaltMethod >= VMHALTMETHOD_END)
1095 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, N_("Invalid VM/HaltMethod value %d"), enmHaltMethod);
1096 }
1097 else if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_CHILD_NOT_FOUND)
1098 return VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to Query VM/HaltMethod as uint32_t"));
1099 else
1100 enmHaltMethod = VMHALTMETHOD_GLOBAL_1;
1101 //enmHaltMethod = VMHALTMETHOD_1;
1102 //enmHaltMethod = VMHALTMETHOD_OLD;
1103 }
1104 LogRel(("VM: Halt method %s (%d)\n", vmR3GetHaltMethodName(enmHaltMethod), enmHaltMethod));
1105
1106 /*
1107 * Find the descriptor.
1108 */
1109 unsigned i = 0;
1110 while ( i < RT_ELEMENTS(g_aHaltMethods)
1111 && g_aHaltMethods[i].enmHaltMethod != enmHaltMethod)
1112 i++;
1113 AssertReturn(i < RT_ELEMENTS(g_aHaltMethods), VERR_INVALID_PARAMETER);
1114
1115 /*
1116 * Terminate the old one.
1117 */
1118 if ( pUVM->vm.s.enmHaltMethod != VMHALTMETHOD_INVALID
1119 && g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnTerm)
1120 {
1121 g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnTerm(pUVM);
1122 pUVM->vm.s.enmHaltMethod = VMHALTMETHOD_INVALID;
1123 }
1124
1125/** @todo SMP: Need rendezvous thing here, the other EMTs must not be
1126 * sleeping when we switch the notification method or we'll never
1127 * manage to wake them up properly and end up relying on timeouts... */
1128
1129 /*
1130 * Init the new one.
1131 */
1132 memset(&pUVM->vm.s.Halt, 0, sizeof(pUVM->vm.s.Halt));
1133 if (g_aHaltMethods[i].pfnInit)
1134 {
1135 int rc = g_aHaltMethods[i].pfnInit(pUVM);
1136 AssertRCReturn(rc, rc);
1137 }
1138 pUVM->vm.s.enmHaltMethod = enmHaltMethod;
1139
1140 ASMAtomicWriteU32(&pUVM->vm.s.iHaltMethod, i);
1141 return VINF_SUCCESS;
1142}
1143
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