VirtualBox

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

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

UVM splitup for SMP guests. Global and local request packets supported.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette