VirtualBox

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

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

VMEmt.cpp: Implemented VMNOTIFYFF_FLAGS_POKE. (not working though as we're not yet working the VMCPU::enmState I think, fixing that and the other missing bits tomorrow.)

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