VirtualBox

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

Last change on this file since 4284 was 4071, checked in by vboxsync, 17 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 25.0 KB
Line 
1/* $Id: VMEmt.cpp 4071 2007-08-07 17:07:59Z vboxsync $ */
2/** @file
3 * VM - Virtual Machine, The Emulation Thread.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * 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 "VMInternal.h"
29#include <VBox/vm.h>
30
31#include <VBox/err.h>
32#include <VBox/log.h>
33#include <iprt/assert.h>
34#include <iprt/asm.h>
35#include <iprt/semaphore.h>
36#include <iprt/string.h>
37#include <iprt/thread.h>
38#include <iprt/time.h>
39
40
41
42
43/**
44 * The emulation thread.
45 *
46 * @returns Thread exit code.
47 * @param ThreadSelf The handle to the executing thread.
48 * @param pvArgs Pointer to a VMEMULATIONTHREADARGS structure.
49 */
50DECLCALLBACK(int) vmR3EmulationThread(RTTHREAD ThreadSelf, void *pvArgs)
51{
52 PVMEMULATIONTHREADARGS pArgs = (PVMEMULATIONTHREADARGS)pvArgs;
53 AssertReleaseMsg(pArgs && pArgs->pVM, ("Invalid arguments to the emulation thread!\n"));
54
55 /*
56 * Init the native thread member.
57 */
58 PVM pVM = pArgs->pVM;
59 pVM->NativeThreadEMT = RTThreadGetNative(ThreadSelf);
60
61 /*
62 * The request loop.
63 */
64 VMSTATE enmBefore;
65 int rc;
66 Log(("vmR3EmulationThread: Emulation thread starting the days work... Thread=%#x pVM=%p\n", ThreadSelf, pVM));
67 for (;;)
68 {
69 /* Requested to exit the EMT thread out of sync? (currently only VMR3WaitForResume) */
70 if (setjmp(pVM->vm.s.emtJumpEnv) != 0)
71 {
72 rc = VINF_SUCCESS;
73 break;
74 }
75
76 /*
77 * Pending requests which needs servicing?
78 *
79 * We check for state changes in addition to status codes when
80 * servicing requests. (Look after the ifs.)
81 */
82 enmBefore = pVM->enmVMState;
83 if (VM_FF_ISSET(pVM, VM_FF_TERMINATE))
84 {
85 rc = VINF_EM_TERMINATE;
86 break;
87 }
88 else if (pVM->vm.s.pReqs)
89 {
90 /*
91 * Service execute in EMT request.
92 */
93 rc = VMR3ReqProcess(pVM);
94 Log(("vmR3EmulationThread: Req rc=%Vrc, VM state %d -> %d\n", rc, enmBefore, pVM->enmVMState));
95 }
96 else if (VM_FF_ISSET(pVM, VM_FF_DBGF))
97 {
98 /*
99 * Service the debugger request.
100 */
101 rc = DBGFR3VMMForcedAction(pVM);
102 Log(("vmR3EmulationThread: Dbg rc=%Vrc, VM state %d -> %d\n", rc, enmBefore, pVM->enmVMState));
103 }
104 else if (VM_FF_ISSET(pVM, VM_FF_RESET))
105 {
106 /*
107 * Service a delay reset request.
108 */
109 rc = VMR3Reset(pVM);
110 VM_FF_CLEAR(pVM, VM_FF_RESET);
111 Log(("vmR3EmulationThread: Reset rc=%Vrc, VM state %d -> %d\n", rc, enmBefore, pVM->enmVMState));
112 }
113 else
114 {
115 /*
116 * Nothing important is pending, so wait for something.
117 */
118 rc = VMR3Wait(pVM);
119 if (VBOX_FAILURE(rc))
120 break;
121 }
122
123 /*
124 * Check for termination requests, these are extremely high priority.
125 */
126 if ( rc == VINF_EM_TERMINATE
127 || VM_FF_ISSET(pVM, VM_FF_TERMINATE))
128 break;
129
130 /*
131 * Some requests (both VMR3Req* and the DBGF) can potentially
132 * resume or start the VM, in that case we'll get a change in
133 * VM status indicating that we're now running.
134 */
135 if ( VBOX_SUCCESS(rc)
136 && enmBefore != pVM->enmVMState
137 && (pVM->enmVMState == VMSTATE_RUNNING))
138 {
139 rc = EMR3ExecuteVM(pVM);
140 Log(("vmR3EmulationThread: EMR3ExecuteVM() -> rc=%Vrc, enmVMState=%d\n", rc, pVM->enmVMState));
141 }
142
143 } /* forever */
144
145
146 /*
147 * Exiting.
148 */
149 Log(("vmR3EmulationThread: Terminating emulation thread! Thread=%#x pVM=%p rc=%Vrc enmBefore=%d enmVMState=%d\n",
150 ThreadSelf, pVM, rc, enmBefore, pVM->enmVMState));
151 if (pVM->vm.s.fEMTDoesTheCleanup)
152 {
153 Log(("vmR3EmulationThread: executing delayed Destroy\n"));
154 vmR3Destroy(pVM);
155 vmR3DestroyFinalBit(pVM);
156 Log(("vmR3EmulationThread: EMT is terminated.\n"));
157 }
158 else
159 {
160 /* we don't reset ThreadEMT here because it's used in waiting. */
161 pVM->NativeThreadEMT = NIL_RTNATIVETHREAD;
162 }
163 return rc;
164}
165
166
167/**
168 * Wait for VM to be resumed. Handle events like vmR3EmulationThread does.
169 * In case the VM is stopped, clean up and long jump to the main EMT loop.
170 *
171 * @returns VINF_SUCCESS or doesn't return
172 * @param pVM VM handle.
173 */
174VMR3DECL(int) VMR3WaitForResume(PVM pVM)
175{
176 /*
177 * The request loop.
178 */
179 VMSTATE enmBefore;
180 int rc;
181 for (;;)
182 {
183
184 /*
185 * Pending requests which needs servicing?
186 *
187 * We check for state changes in addition to status codes when
188 * servicing requests. (Look after the ifs.)
189 */
190 enmBefore = pVM->enmVMState;
191 if (VM_FF_ISSET(pVM, VM_FF_TERMINATE))
192 {
193 rc = VINF_EM_TERMINATE;
194 break;
195 }
196 else if (pVM->vm.s.pReqs)
197 {
198 /*
199 * Service execute in EMT request.
200 */
201 rc = VMR3ReqProcess(pVM);
202 Log(("vmR3EmulationThread: Req rc=%Vrc, VM state %d -> %d\n", rc, enmBefore, pVM->enmVMState));
203 }
204 else if (VM_FF_ISSET(pVM, VM_FF_DBGF))
205 {
206 /*
207 * Service the debugger request.
208 */
209 rc = DBGFR3VMMForcedAction(pVM);
210 Log(("vmR3EmulationThread: Dbg rc=%Vrc, VM state %d -> %d\n", rc, enmBefore, pVM->enmVMState));
211 }
212 else if (VM_FF_ISSET(pVM, VM_FF_RESET))
213 {
214 /*
215 * Service a delay reset request.
216 */
217 rc = VMR3Reset(pVM);
218 VM_FF_CLEAR(pVM, VM_FF_RESET);
219 Log(("vmR3EmulationThread: Reset rc=%Vrc, VM state %d -> %d\n", rc, enmBefore, pVM->enmVMState));
220 }
221 else
222 {
223 /*
224 * Nothing important is pending, so wait for something.
225 */
226 rc = VMR3Wait(pVM);
227 if (VBOX_FAILURE(rc))
228 break;
229 }
230
231 /*
232 * Check for termination requests, these are extremely high priority.
233 */
234 if ( rc == VINF_EM_TERMINATE
235 || VM_FF_ISSET(pVM, VM_FF_TERMINATE))
236 break;
237
238 /*
239 * Some requests (both VMR3Req* and the DBGF) can potentially
240 * resume or start the VM, in that case we'll get a change in
241 * VM status indicating that we're now running.
242 */
243 if ( VBOX_SUCCESS(rc)
244 && enmBefore != pVM->enmVMState
245 && (pVM->enmVMState == VMSTATE_RUNNING))
246 {
247 /* Only valid exit reason. */
248 return VINF_SUCCESS;
249 }
250
251 } /* forever */
252
253 /* Return to the main loop in vmR3EmulationThread, which will clean up for us. */
254 longjmp(pVM->vm.s.emtJumpEnv, 1);
255}
256
257
258/**
259 * The old halt loop.
260 */
261static DECLCALLBACK(int) vmR3HaltOldDoHalt(PVM pVM, const uint32_t fMask, uint64_t /* u64Now*/)
262{
263 /*
264 * Halt loop.
265 */
266 int rc = VINF_SUCCESS;
267 ASMAtomicXchgU32(&pVM->vm.s.fWait, 1);
268 //unsigned cLoops = 0;
269 for (;;)
270 {
271 /*
272 * Work the timers and check if we can exit.
273 * The poll call gives us the ticks left to the next event in
274 * addition to perhaps set an FF.
275 */
276 STAM_REL_PROFILE_START(&pVM->vm.s.StatHaltPoll, a);
277 PDMR3Poll(pVM);
278 STAM_REL_PROFILE_STOP(&pVM->vm.s.StatHaltPoll, a);
279 STAM_REL_PROFILE_START(&pVM->vm.s.StatHaltTimers, b);
280 TMR3TimerQueuesDo(pVM);
281 STAM_REL_PROFILE_STOP(&pVM->vm.s.StatHaltTimers, b);
282 if (VM_FF_ISPENDING(pVM, fMask))
283 break;
284 uint64_t u64NanoTS = TMVirtualToNano(pVM, TMTimerPoll(pVM));
285 if (VM_FF_ISPENDING(pVM, fMask))
286 break;
287
288 /*
289 * Wait for a while. Someone will wake us up or interrupt the call if
290 * anything needs our attention.
291 */
292 if (u64NanoTS < 50000)
293 {
294 //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d spin\n", u64NanoTS, cLoops++);
295 /* spin */;
296 }
297 else
298 {
299 VMMR3YieldStop(pVM);
300 //uint64_t u64Start = RTTimeNanoTS();
301 if (u64NanoTS < 870000) /* this is a bit speculative... works fine on linux. */
302 {
303 //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d yield", u64NanoTS, cLoops++);
304 STAM_REL_PROFILE_START(&pVM->vm.s.StatHaltYield, a);
305 RTThreadYield(); /* this is the best we can do here */
306 STAM_REL_PROFILE_STOP(&pVM->vm.s.StatHaltYield, a);
307 }
308 else if (u64NanoTS < 2000000)
309 {
310 //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d sleep 1ms", u64NanoTS, cLoops++);
311 STAM_REL_PROFILE_START(&pVM->vm.s.StatHaltBlock, a);
312 rc = RTSemEventWait(pVM->vm.s.EventSemWait, 1);
313 STAM_REL_PROFILE_STOP(&pVM->vm.s.StatHaltBlock, a);
314 }
315 else
316 {
317 //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d sleep %dms", u64NanoTS, cLoops++, (uint32_t)RT_MIN((u64NanoTS - 500000) / 1000000, 15));
318 STAM_REL_PROFILE_START(&pVM->vm.s.StatHaltBlock, a);
319 rc = RTSemEventWait(pVM->vm.s.EventSemWait, RT_MIN((u64NanoTS - 1000000) / 1000000, 15));
320 STAM_REL_PROFILE_STOP(&pVM->vm.s.StatHaltBlock, a);
321 }
322 //uint64_t u64Slept = RTTimeNanoTS() - u64Start;
323 //RTLogPrintf(" -> rc=%Vrc in %RU64 ns / %RI64 ns delta\n", rc, u64Slept, u64NanoTS - u64Slept);
324 }
325 if (rc == VERR_TIMEOUT)
326 rc = VINF_SUCCESS;
327 else if (VBOX_FAILURE(rc))
328 {
329 AssertRC(rc != VERR_INTERRUPTED);
330 AssertMsgFailed(("RTSemEventWait->%Vrc\n", rc));
331 VM_FF_SET(pVM, VM_FF_TERMINATE);
332 rc = VERR_INTERNAL_ERROR;
333 break;
334 }
335 }
336
337 return rc;
338}
339
340
341/**
342 * Initialize the configuration of halt method 1 & 2.
343 *
344 * @return VBox status code. Failure on invalid CFGM data.
345 * @param pVM The VM handle.
346 */
347static int vmR3HaltMethod12ReadConfig(PVM pVM)
348{
349 /*
350 * The defaults.
351 */
352 pVM->vm.s.Halt.Method12.u32LagBlockIntervalDivisorCfg = 4;
353 pVM->vm.s.Halt.Method12.u32MinBlockIntervalCfg = 5*1000000;
354 pVM->vm.s.Halt.Method12.u32MaxBlockIntervalCfg = 200*1000000;
355 pVM->vm.s.Halt.Method12.u32StartSpinningCfg = 20*1000000;
356 pVM->vm.s.Halt.Method12.u32StopSpinningCfg = 2*1000000;
357
358 /*
359 * Query overrides.
360 */
361 PCFGMNODE pCfg = CFGMR3GetChild(CFGMR3GetRoot(pVM), "/VMM/HaltedMethod1");
362 if (pCfg)
363 {
364
365 }
366
367 return VINF_SUCCESS;
368}
369
370
371/**
372 * Initialize halt method 1.
373 *
374 * @return VBox status code.
375 * @param pVM The VM handle.
376 */
377static DECLCALLBACK(int) vmR3HaltMethod1Init(PVM pVM)
378{
379 return vmR3HaltMethod12ReadConfig(pVM);
380}
381
382
383/**
384 * Method 1 - Block whenever possible, and when lagging behind
385 * switch to spinning for 10-30ms with occational blocking until
386 * the lag has been eliminated.
387 */
388static DECLCALLBACK(int) vmR3HaltMethod1DoHalt(PVM pVM, const uint32_t fMask, uint64_t u64Now)
389{
390 /*
391 * To simplify things, we decide up-front whether we should switch to spinning or
392 * not. This makes some ASSUMPTIONS about the cause of the spinning (PIT/RTC/PCNet)
393 * and that it will generate interrupts or other events that will cause us to exit
394 * the halt loop.
395 */
396 bool fBlockOnce = false;
397 bool fSpinning = false;
398 uint32_t u32CatchUpPct = TMVirtualSyncGetCatchUpPct(pVM);
399 if (u32CatchUpPct /* non-zero if catching up */)
400 {
401 if (pVM->vm.s.Halt.Method12.u64StartSpinTS)
402 {
403 fSpinning = TMVirtualSyncGetLag(pVM) >= pVM->vm.s.Halt.Method12.u32StopSpinningCfg;
404 if (fSpinning)
405 {
406 uint64_t u64Lag = TMVirtualSyncGetLag(pVM);
407 fBlockOnce = u64Now - pVM->vm.s.Halt.Method12.u64LastBlockTS
408 > RT_MAX(pVM->vm.s.Halt.Method12.u32MinBlockIntervalCfg,
409 RT_MIN(u64Lag / pVM->vm.s.Halt.Method12.u32LagBlockIntervalDivisorCfg,
410 pVM->vm.s.Halt.Method12.u32MaxBlockIntervalCfg));
411 }
412 else
413 {
414 //RTLogRelPrintf("Stopped spinning (%u ms)\n", (u64Now - pVM->vm.s.Halt.Method12.u64StartSpinTS) / 1000000);
415 pVM->vm.s.Halt.Method12.u64StartSpinTS = 0;
416 }
417 }
418 else
419 {
420 fSpinning = TMVirtualSyncGetLag(pVM) >= pVM->vm.s.Halt.Method12.u32StartSpinningCfg;
421 if (fSpinning)
422 pVM->vm.s.Halt.Method12.u64StartSpinTS = u64Now;
423 }
424 }
425 else if (pVM->vm.s.Halt.Method12.u64StartSpinTS)
426 {
427 //RTLogRelPrintf("Stopped spinning (%u ms)\n", (u64Now - pVM->vm.s.Halt.Method12.u64StartSpinTS) / 1000000);
428 pVM->vm.s.Halt.Method12.u64StartSpinTS = 0;
429 }
430
431 /*
432 * Halt loop.
433 */
434 int rc = VINF_SUCCESS;
435 ASMAtomicXchgU32(&pVM->vm.s.fWait, 1);
436 unsigned cLoops = 0;
437 for (;; cLoops++)
438 {
439 /*
440 * Work the timers and check if we can exit.
441 */
442 STAM_REL_PROFILE_START(&pVM->vm.s.StatHaltPoll, a);
443 PDMR3Poll(pVM);
444 STAM_REL_PROFILE_STOP(&pVM->vm.s.StatHaltPoll, a);
445 STAM_REL_PROFILE_START(&pVM->vm.s.StatHaltTimers, b);
446 TMR3TimerQueuesDo(pVM);
447 STAM_REL_PROFILE_STOP(&pVM->vm.s.StatHaltTimers, b);
448 if (VM_FF_ISPENDING(pVM, fMask))
449 break;
450
451 /*
452 * Estimate time left to the next event.
453 */
454 uint64_t u64NanoTS = TMVirtualToNano(pVM, TMTimerPoll(pVM));
455 if (VM_FF_ISPENDING(pVM, fMask))
456 break;
457
458 /*
459 * Block if we're not spinning and the interval isn't all that small.
460 */
461 if ( ( !fSpinning
462 || fBlockOnce)
463 && u64NanoTS >= 250000) /* 0.250 ms */
464 {
465 const uint64_t Start = pVM->vm.s.Halt.Method12.u64LastBlockTS = RTTimeNanoTS();
466 VMMR3YieldStop(pVM);
467
468 uint32_t cMilliSecs = RT_MIN(u64NanoTS / 1000000, 15);
469 if (cMilliSecs <= pVM->vm.s.Halt.Method12.cNSBlockedTooLongAvg)
470 cMilliSecs = 1;
471 else
472 cMilliSecs -= pVM->vm.s.Halt.Method12.cNSBlockedTooLongAvg;
473 //RTLogRelPrintf("u64NanoTS=%RI64 cLoops=%3d sleep %02dms (%7RU64) ", u64NanoTS, cLoops, cMilliSecs, u64NanoTS);
474 STAM_REL_PROFILE_START(&pVM->vm.s.StatHaltBlock, a);
475 rc = RTSemEventWait(pVM->vm.s.EventSemWait, cMilliSecs);
476 STAM_REL_PROFILE_STOP(&pVM->vm.s.StatHaltBlock, a);
477 if (rc == VERR_TIMEOUT)
478 rc = VINF_SUCCESS;
479 else if (VBOX_FAILURE(rc))
480 {
481 AssertRC(rc != VERR_INTERRUPTED);
482 AssertMsgFailed(("RTSemEventWait->%Vrc\n", rc));
483 VM_FF_SET(pVM, VM_FF_TERMINATE);
484 rc = VERR_INTERNAL_ERROR;
485 break;
486 }
487
488 /*
489 * Calc the statistics.
490 * Update averages every 16th time, and flush parts of the history every 64th time.
491 */
492 const uint64_t Elapsed = RTTimeNanoTS() - Start;
493 pVM->vm.s.Halt.Method12.cNSBlocked += Elapsed;
494 if (Elapsed > u64NanoTS)
495 pVM->vm.s.Halt.Method12.cNSBlockedTooLong += Elapsed - u64NanoTS;
496 pVM->vm.s.Halt.Method12.cBlocks++;
497 if (!(pVM->vm.s.Halt.Method12.cBlocks & 0xf))
498 {
499 pVM->vm.s.Halt.Method12.cNSBlockedTooLongAvg = pVM->vm.s.Halt.Method12.cNSBlockedTooLong / pVM->vm.s.Halt.Method12.cBlocks;
500 if (!(pVM->vm.s.Halt.Method12.cBlocks & 0x3f))
501 {
502 pVM->vm.s.Halt.Method12.cNSBlockedTooLong = pVM->vm.s.Halt.Method12.cNSBlockedTooLongAvg * 0x40;
503 pVM->vm.s.Halt.Method12.cBlocks = 0x40;
504 }
505 }
506 //RTLogRelPrintf(" -> %7RU64 ns / %7RI64 ns delta%s\n", Elapsed, Elapsed - u64NanoTS, fBlockOnce ? " (block once)" : "");
507
508 /*
509 * Clear the block once flag if we actually blocked.
510 */
511 if ( fBlockOnce
512 && Elapsed > 100000 /* 0.1 ms */)
513 fBlockOnce = false;
514 }
515 }
516 //if (fSpinning) RTLogRelPrintf("spun for %RU64 ns %u loops; lag=%RU64 pct=%d\n", RTTimeNanoTS() - u64Now, cLoops, TMVirtualSyncGetLag(pVM), u32CatchUpPct);
517
518 return rc;
519}
520
521
522/**
523 * Default VMR3Wait() worker.
524 *
525 * @returns VBox status code.
526 * @param pVM The VM handle.
527 */
528static DECLCALLBACK(int) vmR3DefaultWait(PVM pVM)
529{
530 int rc = VINF_SUCCESS;
531 ASMAtomicXchgU32(&pVM->vm.s.fWait, 1);
532 for (;;)
533 {
534 /*
535 * Check Relevant FFs.
536 */
537 if (VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_SUSPENDED_MASK))
538 break;
539
540 /*
541 * Wait for a while. Someone will wake us up or interrupt the call if
542 * anything needs our attention.
543 */
544 rc = RTSemEventWait(pVM->vm.s.EventSemWait, 1000);
545 if (rc == VERR_TIMEOUT)
546 rc = VINF_SUCCESS;
547 else if (VBOX_FAILURE(rc))
548 {
549 AssertMsgFailed(("RTSemEventWait->%Vrc\n", rc));
550 VM_FF_SET(pVM, VM_FF_TERMINATE);
551 rc = VERR_INTERNAL_ERROR;
552 break;
553 }
554
555 }
556 ASMAtomicXchgU32(&pVM->vm.s.fWait, 0);
557 return rc;
558}
559
560
561/**
562 * Default VMR3NotifyFF() worker.
563 *
564 * @param pVM The VM handle.
565 * @param fNotifiedREM Se VMR3NotifyFF().
566 */
567static DECLCALLBACK(void) vmR3DefaultNotifyFF(PVM pVM, bool fNotifiedREM)
568{
569 if (pVM->vm.s.fWait)
570 {
571 int rc = RTSemEventSignal(pVM->vm.s.EventSemWait);
572 AssertRC(rc);
573 }
574 else if (!fNotifiedREM)
575 REMR3NotifyFF(pVM);
576}
577
578
579/**
580 * Array with halt method descriptors.
581 * VMINT::iHaltMethod contains an index into this array.
582 */
583static const struct VMHALTMETHODDESC
584{
585 /** The halt method id. */
586 VMHALTMETHOD enmHaltMethod;
587 /** The init function for loading config and initialize variables. */
588 DECLR3CALLBACKMEMBER(int, pfnInit,(PVM pVM));
589 /** The term function. */
590 DECLR3CALLBACKMEMBER(void, pfnTerm,(PVM pVM));
591 /** The halt function. */
592 DECLR3CALLBACKMEMBER(int, pfnHalt,(PVM pVM, const uint32_t fMask, uint64_t u64Now));
593 /** The wait function. */
594 DECLR3CALLBACKMEMBER(int, pfnWait,(PVM pVM));
595 /** The notifyFF function. */
596 DECLR3CALLBACKMEMBER(void, pfnNotifyFF,(PVM pVM, bool fNotifiedREM));
597} g_aHaltMethods[] =
598{
599 { VMHALTMETHOD_OLD, NULL, NULL, vmR3HaltOldDoHalt, vmR3DefaultWait, vmR3DefaultNotifyFF },
600 { VMHALTMETHOD_1, vmR3HaltMethod1Init, NULL, vmR3HaltMethod1DoHalt, vmR3DefaultWait, vmR3DefaultNotifyFF },
601 //{ VMHALTMETHOD_2, vmR3HaltMethod2Init, vmR3HaltMethod2Term, vmR3HaltMethod2DoWait, vmR3HaltMethod2Wait, vmR3HaltMethod2NotifyFF },
602};
603
604
605/**
606 * Notify the emulation thread (EMT) about pending Forced Action (FF).
607 *
608 * This function is called by thread other than EMT to make
609 * sure EMT wakes up and promptly service an FF request.
610 *
611 * @param pVM VM handle.
612 * @param fNotifiedREM Set if REM have already been notified. If clear the
613 * generic REMR3NotifyFF() method is called.
614 */
615VMR3DECL(void) VMR3NotifyFF(PVM pVM, bool fNotifiedREM)
616{
617 LogFlow(("VMR3NotifyFF:\n"));
618 g_aHaltMethods[pVM->vm.s.iHaltMethod].pfnNotifyFF(pVM, fNotifiedREM);
619}
620
621
622/**
623 * Halted VM Wait.
624 * Any external event will unblock the thread.
625 *
626 * @returns VINF_SUCCESS unless a fatal error occured. In the latter
627 * case an appropriate status code is returned.
628 * @param pVM VM handle.
629 * @param fIgnoreInterrupts If set the VM_FF_INTERRUPT flags is ignored.
630 * @thread The emulation thread.
631 */
632VMR3DECL(int) VMR3WaitHalted(PVM pVM, bool fIgnoreInterrupts)
633{
634 LogFlow(("VMR3WaitHalted: fIgnoreInterrupts=%d\n", fIgnoreInterrupts));
635
636 /*
637 * Check Relevant FFs.
638 */
639 const uint32_t fMask = !fIgnoreInterrupts
640 ? VM_FF_EXTERNAL_HALTED_MASK
641 : VM_FF_EXTERNAL_HALTED_MASK & ~(VM_FF_INTERRUPT_APIC | VM_FF_INTERRUPT_PIC);
642 if (VM_FF_ISPENDING(pVM, fMask))
643 {
644 LogFlow(("VMR3WaitHalted: returns VINF_SUCCESS (FF %#x)\n", pVM->fForcedActions));
645 return VINF_SUCCESS;
646 }
647
648 /*
649 * The yielder is suspended while we're halting.
650 */
651 VMMR3YieldSuspend(pVM);
652
653 /*
654 * Record halt averages for the last second.
655 */
656 uint64_t u64Now = RTTimeNanoTS();
657 int64_t off = u64Now - pVM->vm.s.u64HaltsStartTS;
658 if (off > 1000000000)
659 {
660 if (off > _4G || !pVM->vm.s.cHalts)
661 {
662 pVM->vm.s.HaltInterval = 1000000000 /* 1 sec */;
663 pVM->vm.s.HaltFrequency = 1;
664 }
665 else
666 {
667 pVM->vm.s.HaltInterval = (uint32_t)off / pVM->vm.s.cHalts;
668 pVM->vm.s.HaltFrequency = ASMMultU64ByU32DivByU32(pVM->vm.s.cHalts, 1000000000, (uint32_t)off);
669 }
670 pVM->vm.s.u64HaltsStartTS = u64Now;
671 pVM->vm.s.cHalts = 0;
672 }
673 pVM->vm.s.cHalts++;
674
675 /*
676 * Do the halt.
677 */
678 int rc = g_aHaltMethods[pVM->vm.s.iHaltMethod].pfnHalt(pVM, fMask, u64Now);
679
680 /*
681 * Resume the yielder and tell the world we're not blocking.
682 */
683 ASMAtomicXchgU32(&pVM->vm.s.fWait, 0);
684 VMMR3YieldResume(pVM);
685
686 LogFlow(("VMR3WaitHalted: returns %Vrc (FF %#x)\n", rc, pVM->fForcedActions));
687 return rc;
688}
689
690
691/**
692 * Suspended VM Wait.
693 * Only a handful of forced actions will cause the function to
694 * return to the caller.
695 *
696 * @returns VINF_SUCCESS unless a fatal error occured. In the latter
697 * case an appropriate status code is returned.
698 * @param pVM VM handle.
699 * @thread The emulation thread.
700 */
701VMR3DECL(int) VMR3Wait(PVM pVM)
702{
703 LogFlow(("VMR3Wait:\n"));
704
705 /*
706 * Check Relevant FFs.
707 */
708 if (VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_SUSPENDED_MASK))
709 {
710 LogFlow(("VMR3Wait: returns VINF_SUCCESS (FF %#x)\n", pVM->fForcedActions));
711 return VINF_SUCCESS;
712 }
713
714 /*
715 * Do waiting according to the halt method (so VMR3NotifyFF
716 * doesn't have to special case anything).
717 */
718 int rc = g_aHaltMethods[pVM->vm.s.iHaltMethod].pfnWait(pVM);
719 LogFlow(("VMR3Wait: returns %Vrc (FF %#x)\n", rc, pVM->fForcedActions));
720 return rc;
721}
722
723
724/**
725 * Changes the halt method.
726 *
727 * @returns VBox status code.
728 * @param pVM The VM handle.
729 * @param enmHaltMethod The new halt method.
730 * @thread EMT.
731 */
732int vmR3SetHaltMethod(PVM pVM, VMHALTMETHOD enmHaltMethod)
733{
734 VM_ASSERT_EMT(pVM);
735 AssertReturn(enmHaltMethod > VMHALTMETHOD_INVALID && enmHaltMethod < VMHALTMETHOD_END, VERR_INVALID_PARAMETER);
736
737 /*
738 * Resolve default (can be overridden in the configuration).
739 */
740 if (enmHaltMethod == VMHALTMETHOD_DEFAULT)
741 {
742 uint32_t u32;
743 int rc = CFGMR3QueryU32(CFGMR3GetChild(CFGMR3GetRoot(pVM), "VM"), "HaltMethod", &u32);
744 if (VBOX_SUCCESS(rc))
745 {
746 enmHaltMethod = (VMHALTMETHOD)u32;
747 if (enmHaltMethod <= VMHALTMETHOD_INVALID || enmHaltMethod >= VMHALTMETHOD_END)
748 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, N_("Invalid VM/HaltMethod value %d."), enmHaltMethod);
749 }
750 else if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_CHILD_NOT_FOUND)
751 return VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to Query VM/HaltMethod as uint32_t."));
752 else
753 enmHaltMethod = VMHALTMETHOD_1;
754 //enmHaltMethod = VMHALTMETHOD_OLD;
755 }
756
757 /*
758 * Find the descriptor.
759 */
760 unsigned i = 0;
761 while ( i < RT_ELEMENTS(g_aHaltMethods)
762 && g_aHaltMethods[i].enmHaltMethod != enmHaltMethod)
763 i++;
764 AssertReturn(i < RT_ELEMENTS(g_aHaltMethods), VERR_INVALID_PARAMETER);
765
766 /*
767 * Terminate the old one.
768 */
769 if ( pVM->vm.s.enmHaltMethod != VMHALTMETHOD_INVALID
770 && g_aHaltMethods[pVM->vm.s.iHaltMethod].pfnTerm)
771 {
772 g_aHaltMethods[pVM->vm.s.iHaltMethod].pfnTerm(pVM);
773 pVM->vm.s.enmHaltMethod = VMHALTMETHOD_INVALID;
774 }
775
776 /*
777 * Init the new one.
778 */
779 memset(&pVM->vm.s.Halt, 0, sizeof(pVM->vm.s.Halt));
780 if (g_aHaltMethods[i].pfnInit)
781 {
782 int rc = g_aHaltMethods[i].pfnInit(pVM);
783 AssertRCReturn(rc, rc);
784 }
785 pVM->vm.s.enmHaltMethod = enmHaltMethod;
786 ASMAtomicXchgU32(&pVM->vm.s.iHaltMethod, i);
787 return VINF_SUCCESS;
788}
789
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