VirtualBox

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

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

Reapplied 46658 + fix

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