VirtualBox

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

Last change on this file since 100101 was 100002, checked in by vboxsync, 18 months ago

VMM: Take the vTimer expiration into account when halting due to a WFI/WFE instruction so the guest gets woken up if no other event is pending, bugref:10389 [scm]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 52.8 KB
Line 
1/* $Id: VMEmt.cpp 100002 2023-05-30 06:16:14Z vboxsync $ */
2/** @file
3 * VM - Virtual Machine, The Emulation Thread.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_VM
33#include <VBox/vmm/tm.h>
34#include <VBox/vmm/dbgf.h>
35#include <VBox/vmm/em.h>
36#include <VBox/vmm/gvmm.h>
37#include <VBox/vmm/nem.h>
38#include <VBox/vmm/pdmapi.h>
39#include <VBox/vmm/tm.h>
40#include "VMInternal.h"
41#include <VBox/vmm/vmcc.h>
42
43#include <VBox/err.h>
44#include <VBox/log.h>
45#include <iprt/assert.h>
46#include <iprt/asm.h>
47#include <iprt/asm-math.h>
48#include <iprt/semaphore.h>
49#include <iprt/string.h>
50#include <iprt/thread.h>
51#include <iprt/time.h>
52
53
54/*********************************************************************************************************************************
55* Internal Functions *
56*********************************************************************************************************************************/
57int vmR3EmulationThreadWithId(RTTHREAD hThreadSelf, PUVMCPU pUVCpu, VMCPUID idCpu);
58
59
60/**
61 * The emulation thread main function.
62 *
63 * @returns Thread exit code.
64 * @param hThreadSelf The handle to the executing thread.
65 * @param pvArgs Pointer to the user mode per-VCpu structure (UVMPCU).
66 */
67DECLCALLBACK(int) vmR3EmulationThread(RTTHREAD hThreadSelf, void *pvArgs)
68{
69 PUVMCPU pUVCpu = (PUVMCPU)pvArgs;
70 return vmR3EmulationThreadWithId(hThreadSelf, pUVCpu, pUVCpu->idCpu);
71}
72
73
74/**
75 * The emulation thread main function, with Virtual CPU ID for debugging.
76 *
77 * @returns Thread exit code.
78 * @param hThreadSelf The handle to the executing thread.
79 * @param pUVCpu Pointer to the user mode per-VCpu structure.
80 * @param idCpu The virtual CPU ID, for backtrace purposes.
81 */
82int vmR3EmulationThreadWithId(RTTHREAD hThreadSelf, PUVMCPU pUVCpu, VMCPUID idCpu)
83{
84 PUVM pUVM = pUVCpu->pUVM;
85 int rc;
86 RT_NOREF_PV(hThreadSelf);
87
88 AssertReleaseMsg(RT_VALID_PTR(pUVM) && pUVM->u32Magic == UVM_MAGIC,
89 ("Invalid arguments to the emulation thread!\n"));
90
91 rc = RTTlsSet(pUVM->vm.s.idxTLS, pUVCpu);
92 AssertReleaseMsgRCReturn(rc, ("RTTlsSet %x failed with %Rrc\n", pUVM->vm.s.idxTLS, rc), rc);
93
94 if ( pUVM->pVmm2UserMethods
95 && pUVM->pVmm2UserMethods->pfnNotifyEmtInit)
96 pUVM->pVmm2UserMethods->pfnNotifyEmtInit(pUVM->pVmm2UserMethods, pUVM, pUVCpu);
97
98 /*
99 * The request loop.
100 */
101 rc = VINF_SUCCESS;
102 Log(("vmR3EmulationThread: Emulation thread starting the days work... Thread=%#x pUVM=%p\n", hThreadSelf, pUVM));
103 VMSTATE enmBefore = VMSTATE_CREATED; /* (only used for logging atm.) */
104 ASMAtomicIncU32(&pUVM->vm.s.cActiveEmts);
105 for (;;)
106 {
107 /*
108 * During early init there is no pVM and/or pVCpu, so make a special path
109 * for that to keep things clearly separate.
110 */
111 PVM pVM = pUVM->pVM;
112 PVMCPU pVCpu = pUVCpu->pVCpu;
113 if (!pVCpu || !pVM)
114 {
115 /*
116 * Check for termination first.
117 */
118 if (pUVM->vm.s.fTerminateEMT)
119 {
120 rc = VINF_EM_TERMINATE;
121 break;
122 }
123
124 /*
125 * Only the first VCPU may initialize the VM during early init
126 * and must therefore service all VMCPUID_ANY requests.
127 * See also VMR3Create
128 */
129 if ( (pUVM->vm.s.pNormalReqs || pUVM->vm.s.pPriorityReqs)
130 && pUVCpu->idCpu == 0)
131 {
132 /*
133 * Service execute in any EMT request.
134 */
135 rc = VMR3ReqProcessU(pUVM, VMCPUID_ANY, false /*fPriorityOnly*/);
136 Log(("vmR3EmulationThread: Req rc=%Rrc, VM state %s -> %s\n", rc, VMR3GetStateName(enmBefore), pUVM->pVM ? VMR3GetStateName(pUVM->pVM->enmVMState) : "CREATING"));
137 }
138 else if (pUVCpu->vm.s.pNormalReqs || pUVCpu->vm.s.pPriorityReqs)
139 {
140 /*
141 * Service execute in specific EMT request.
142 */
143 rc = VMR3ReqProcessU(pUVM, pUVCpu->idCpu, false /*fPriorityOnly*/);
144 Log(("vmR3EmulationThread: Req (cpu=%u) rc=%Rrc, VM state %s -> %s\n", pUVCpu->idCpu, rc, VMR3GetStateName(enmBefore), pUVM->pVM ? VMR3GetStateName(pUVM->pVM->enmVMState) : "CREATING"));
145 }
146 else
147 {
148 /*
149 * Nothing important is pending, so wait for something.
150 */
151 rc = VMR3WaitU(pUVCpu);
152 if (RT_FAILURE(rc))
153 {
154 AssertLogRelMsgFailed(("VMR3WaitU failed with %Rrc\n", rc));
155 break;
156 }
157 }
158 }
159 else
160 {
161 /*
162 * Pending requests which needs servicing?
163 *
164 * We check for state changes in addition to status codes when
165 * servicing requests. (Look after the ifs.)
166 */
167 enmBefore = pVM->enmVMState;
168 if (pUVM->vm.s.fTerminateEMT)
169 {
170 rc = VINF_EM_TERMINATE;
171 break;
172 }
173
174 if (VM_FF_IS_SET(pVM, VM_FF_EMT_RENDEZVOUS))
175 {
176 rc = VMMR3EmtRendezvousFF(pVM, pVM->apCpusR3[idCpu]);
177 Log(("vmR3EmulationThread: Rendezvous rc=%Rrc, VM state %s -> %s\n", rc, VMR3GetStateName(enmBefore), VMR3GetStateName(pVM->enmVMState)));
178 }
179 else if (pUVM->vm.s.pNormalReqs || pUVM->vm.s.pPriorityReqs)
180 {
181 /*
182 * Service execute in any EMT request.
183 */
184 rc = VMR3ReqProcessU(pUVM, VMCPUID_ANY, false /*fPriorityOnly*/);
185 Log(("vmR3EmulationThread: Req rc=%Rrc, VM state %s -> %s\n", rc, VMR3GetStateName(enmBefore), VMR3GetStateName(pVM->enmVMState)));
186 }
187 else if (pUVCpu->vm.s.pNormalReqs || pUVCpu->vm.s.pPriorityReqs)
188 {
189 /*
190 * Service execute in specific EMT request.
191 */
192 rc = VMR3ReqProcessU(pUVM, pUVCpu->idCpu, false /*fPriorityOnly*/);
193 Log(("vmR3EmulationThread: Req (cpu=%u) rc=%Rrc, VM state %s -> %s\n", pUVCpu->idCpu, rc, VMR3GetStateName(enmBefore), VMR3GetStateName(pVM->enmVMState)));
194 }
195 else if ( VM_FF_IS_SET(pVM, VM_FF_DBGF)
196 || VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_DBGF))
197 {
198 /*
199 * Service the debugger request.
200 */
201 rc = DBGFR3VMMForcedAction(pVM, pVCpu);
202 Log(("vmR3EmulationThread: Dbg rc=%Rrc, VM state %s -> %s\n", rc, VMR3GetStateName(enmBefore), VMR3GetStateName(pVM->enmVMState)));
203 }
204 else if (VM_FF_TEST_AND_CLEAR(pVM, VM_FF_RESET))
205 {
206 /*
207 * Service a delayed reset request.
208 */
209 rc = VBOXSTRICTRC_VAL(VMR3ResetFF(pVM));
210 VM_FF_CLEAR(pVM, VM_FF_RESET);
211 Log(("vmR3EmulationThread: Reset rc=%Rrc, VM state %s -> %s\n", rc, VMR3GetStateName(enmBefore), VMR3GetStateName(pVM->enmVMState)));
212 }
213 else
214 {
215 /*
216 * Nothing important is pending, so wait for something.
217 */
218 rc = VMR3WaitU(pUVCpu);
219 if (RT_FAILURE(rc))
220 {
221 AssertLogRelMsgFailed(("VMR3WaitU failed with %Rrc\n", rc));
222 break;
223 }
224 }
225
226 /*
227 * Check for termination requests, these have extremely high priority.
228 */
229 if ( rc == VINF_EM_TERMINATE
230 || pUVM->vm.s.fTerminateEMT)
231 break;
232 }
233
234 /*
235 * Some requests (both VMR3Req* and the DBGF) can potentially resume
236 * or start the VM, in that case we'll get a change in VM status
237 * indicating that we're now running.
238 */
239 if (RT_SUCCESS(rc))
240 {
241 pVM = pUVM->pVM;
242 if (pVM)
243 {
244 pVCpu = pVM->apCpusR3[idCpu];
245 if ( pVM->enmVMState == VMSTATE_RUNNING
246 && VMCPUSTATE_IS_STARTED(VMCPU_GET_STATE(pVCpu)))
247 {
248 rc = EMR3ExecuteVM(pVM, pVCpu);
249 Log(("vmR3EmulationThread: EMR3ExecuteVM() -> rc=%Rrc, enmVMState=%d\n", rc, pVM->enmVMState));
250 }
251 }
252 }
253
254 } /* forever */
255
256
257 /*
258 * Decrement the active EMT count if we haven't done it yet in vmR3Destroy.
259 */
260 if (!pUVCpu->vm.s.fBeenThruVmDestroy)
261 ASMAtomicDecU32(&pUVM->vm.s.cActiveEmts);
262
263
264 /*
265 * Cleanup and exit.
266 * EMT0 does the VM destruction after all other EMTs have deregistered and terminated.
267 */
268 Log(("vmR3EmulationThread: Terminating emulation thread! Thread=%#x pUVM=%p rc=%Rrc enmBefore=%d enmVMState=%d\n",
269 hThreadSelf, pUVM, rc, enmBefore, pUVM->pVM ? pUVM->pVM->enmVMState : VMSTATE_TERMINATED));
270 PVM pVM;
271 if ( idCpu == 0
272 && (pVM = pUVM->pVM) != NULL)
273 {
274 /* Wait for any other EMTs to terminate before we destroy the VM (see vmR3DestroyVM). */
275 for (VMCPUID iCpu = 1; iCpu < pUVM->cCpus; iCpu++)
276 {
277 RTTHREAD hThread;
278 ASMAtomicXchgHandle(&pUVM->aCpus[iCpu].vm.s.ThreadEMT, NIL_RTTHREAD, &hThread);
279 if (hThread != NIL_RTTHREAD)
280 {
281 int rc2 = RTThreadWait(hThread, 5 * RT_MS_1SEC, NULL);
282 AssertLogRelMsgRC(rc2, ("iCpu=%u rc=%Rrc\n", iCpu, rc2));
283 if (RT_FAILURE(rc2))
284 pUVM->aCpus[iCpu].vm.s.ThreadEMT = hThread;
285 }
286 }
287
288 /* Switch to the terminated state, clearing the VM pointer and finally destroy the VM. */
289 vmR3SetTerminated(pVM);
290
291 pUVM->pVM = NULL;
292 for (VMCPUID iCpu = 0; iCpu < pUVM->cCpus; iCpu++)
293 {
294 pUVM->aCpus[iCpu].pVM = NULL;
295 pUVM->aCpus[iCpu].pVCpu = NULL;
296 }
297
298 int rc2 = GVMMR3DestroyVM(pUVM, pVM);
299 AssertLogRelRC(rc2);
300 }
301 /* Deregister the EMT with VMMR0. */
302 else if ( idCpu != 0
303 && (pVM = pUVM->pVM) != NULL)
304 {
305 int rc2 = GVMMR3DeregisterVCpu(pVM, idCpu);
306 AssertLogRelRC(rc2);
307 }
308
309 if ( pUVM->pVmm2UserMethods
310 && pUVM->pVmm2UserMethods->pfnNotifyEmtTerm)
311 pUVM->pVmm2UserMethods->pfnNotifyEmtTerm(pUVM->pVmm2UserMethods, pUVM, pUVCpu);
312
313 pUVCpu->vm.s.NativeThreadEMT = NIL_RTNATIVETHREAD;
314 Log(("vmR3EmulationThread: EMT is terminated.\n"));
315 return rc;
316}
317
318
319/**
320 * Gets the name of a halt method.
321 *
322 * @returns Pointer to a read only string.
323 * @param enmMethod The method.
324 */
325static const char *vmR3GetHaltMethodName(VMHALTMETHOD enmMethod)
326{
327 switch (enmMethod)
328 {
329 case VMHALTMETHOD_BOOTSTRAP: return "bootstrap";
330 case VMHALTMETHOD_DEFAULT: return "default";
331 case VMHALTMETHOD_OLD: return "old";
332 case VMHALTMETHOD_1: return "method1";
333 //case VMHALTMETHOD_2: return "method2";
334 case VMHALTMETHOD_GLOBAL_1: return "global1";
335 default: return "unknown";
336 }
337}
338
339
340/**
341 * Signal a fatal wait error.
342 *
343 * @returns Fatal error code to be propagated up the call stack.
344 * @param pUVCpu The user mode per CPU structure of the calling
345 * EMT.
346 * @param pszFmt The error format with a single %Rrc in it.
347 * @param rcFmt The status code to format.
348 */
349static int vmR3FatalWaitError(PUVMCPU pUVCpu, const char *pszFmt, int rcFmt)
350{
351 /** @todo This is wrong ... raise a fatal error / guru meditation
352 * instead. */
353 AssertLogRelMsgFailed((pszFmt, rcFmt));
354 ASMAtomicUoWriteBool(&pUVCpu->pUVM->vm.s.fTerminateEMT, true);
355 if (pUVCpu->pVM)
356 VM_FF_SET(pUVCpu->pVM, VM_FF_CHECK_VM_STATE);
357 return VERR_VM_FATAL_WAIT_ERROR;
358}
359
360
361/**
362 * The old halt loop.
363 */
364static DECLCALLBACK(int) vmR3HaltOldDoHalt(PUVMCPU pUVCpu, const uint32_t fMask, uint64_t /* u64Now*/)
365{
366 /*
367 * Halt loop.
368 */
369 PVM pVM = pUVCpu->pVM;
370 PVMCPU pVCpu = pUVCpu->pVCpu;
371
372 int rc = VINF_SUCCESS;
373 ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
374 //unsigned cLoops = 0;
375 for (;;)
376 {
377 /*
378 * Work the timers and check if we can exit.
379 * The poll call gives us the ticks left to the next event in
380 * addition to perhaps set an FF.
381 */
382 uint64_t const u64StartTimers = RTTimeNanoTS();
383 TMR3TimerQueuesDo(pVM);
384 uint64_t const cNsElapsedTimers = RTTimeNanoTS() - u64StartTimers;
385 STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltTimers, cNsElapsedTimers);
386 if ( VM_FF_IS_ANY_SET(pVM, VM_FF_EXTERNAL_HALTED_MASK)
387 || VMCPU_FF_IS_ANY_SET(pVCpu, fMask))
388 break;
389 uint64_t u64NanoTS;
390 TMTimerPollGIP(pVM, pVCpu, &u64NanoTS);
391 if ( VM_FF_IS_ANY_SET(pVM, VM_FF_EXTERNAL_HALTED_MASK)
392 || VMCPU_FF_IS_ANY_SET(pVCpu, fMask))
393 break;
394
395 /*
396 * Wait for a while. Someone will wake us up or interrupt the call if
397 * anything needs our attention.
398 */
399 if (u64NanoTS < 50000)
400 {
401 //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d spin\n", u64NanoTS, cLoops++);
402 /* spin */;
403 }
404 else
405 {
406 VMMR3YieldStop(pVM);
407 //uint64_t u64Start = RTTimeNanoTS();
408 if (u64NanoTS < 870000) /* this is a bit speculative... works fine on linux. */
409 {
410 //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d yield", u64NanoTS, cLoops++);
411 uint64_t const u64StartSchedYield = RTTimeNanoTS();
412 RTThreadYield(); /* this is the best we can do here */
413 uint64_t const cNsElapsedSchedYield = RTTimeNanoTS() - u64StartSchedYield;
414 STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltYield, cNsElapsedSchedYield);
415 }
416 else if (u64NanoTS < 2000000)
417 {
418 //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d sleep 1ms", u64NanoTS, cLoops++);
419 uint64_t const u64StartSchedHalt = RTTimeNanoTS();
420 rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, 1);
421 uint64_t const cNsElapsedSchedHalt = RTTimeNanoTS() - u64StartSchedHalt;
422 STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltBlock, cNsElapsedSchedHalt);
423 }
424 else
425 {
426 //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d sleep %dms", u64NanoTS, cLoops++, (uint32_t)RT_MIN((u64NanoTS - 500000) / 1000000, 15));
427 uint64_t const u64StartSchedHalt = RTTimeNanoTS();
428 rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, RT_MIN((u64NanoTS - 1000000) / 1000000, 15));
429 uint64_t const cNsElapsedSchedHalt = RTTimeNanoTS() - u64StartSchedHalt;
430 STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltBlock, cNsElapsedSchedHalt);
431 }
432 //uint64_t u64Slept = RTTimeNanoTS() - u64Start;
433 //RTLogPrintf(" -> rc=%Rrc in %RU64 ns / %RI64 ns delta\n", rc, u64Slept, u64NanoTS - u64Slept);
434 }
435 if (rc == VERR_TIMEOUT)
436 rc = VINF_SUCCESS;
437 else if (RT_FAILURE(rc))
438 {
439 rc = vmR3FatalWaitError(pUVCpu, "RTSemEventWait->%Rrc\n", rc);
440 break;
441 }
442 }
443
444 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
445 return rc;
446}
447
448
449/**
450 * Initialize the configuration of halt method 1 & 2.
451 *
452 * @return VBox status code. Failure on invalid CFGM data.
453 * @param pUVM The user mode VM structure.
454 */
455static int vmR3HaltMethod12ReadConfigU(PUVM pUVM)
456{
457 /*
458 * The defaults.
459 */
460#if 1 /* DEBUGGING STUFF - REMOVE LATER */
461 pUVM->vm.s.Halt.Method12.u32LagBlockIntervalDivisorCfg = 4;
462 pUVM->vm.s.Halt.Method12.u32MinBlockIntervalCfg = 2*1000000;
463 pUVM->vm.s.Halt.Method12.u32MaxBlockIntervalCfg = 75*1000000;
464 pUVM->vm.s.Halt.Method12.u32StartSpinningCfg = 30*1000000;
465 pUVM->vm.s.Halt.Method12.u32StopSpinningCfg = 20*1000000;
466#else
467 pUVM->vm.s.Halt.Method12.u32LagBlockIntervalDivisorCfg = 4;
468 pUVM->vm.s.Halt.Method12.u32MinBlockIntervalCfg = 5*1000000;
469 pUVM->vm.s.Halt.Method12.u32MaxBlockIntervalCfg = 200*1000000;
470 pUVM->vm.s.Halt.Method12.u32StartSpinningCfg = 20*1000000;
471 pUVM->vm.s.Halt.Method12.u32StopSpinningCfg = 2*1000000;
472#endif
473
474 /*
475 * Query overrides.
476 *
477 * I don't have time to bother with niceties such as invalid value checks
478 * here right now. sorry.
479 */
480 PCFGMNODE pCfg = CFGMR3GetChild(CFGMR3GetRoot(pUVM->pVM), "/VMM/HaltedMethod1");
481 if (pCfg)
482 {
483 uint32_t u32;
484 if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "LagBlockIntervalDivisor", &u32)))
485 pUVM->vm.s.Halt.Method12.u32LagBlockIntervalDivisorCfg = u32;
486 if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "MinBlockInterval", &u32)))
487 pUVM->vm.s.Halt.Method12.u32MinBlockIntervalCfg = u32;
488 if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "MaxBlockInterval", &u32)))
489 pUVM->vm.s.Halt.Method12.u32MaxBlockIntervalCfg = u32;
490 if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "StartSpinning", &u32)))
491 pUVM->vm.s.Halt.Method12.u32StartSpinningCfg = u32;
492 if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "StopSpinning", &u32)))
493 pUVM->vm.s.Halt.Method12.u32StopSpinningCfg = u32;
494 LogRel(("VMEmt: HaltedMethod1 config: %d/%d/%d/%d/%d\n",
495 pUVM->vm.s.Halt.Method12.u32LagBlockIntervalDivisorCfg,
496 pUVM->vm.s.Halt.Method12.u32MinBlockIntervalCfg,
497 pUVM->vm.s.Halt.Method12.u32MaxBlockIntervalCfg,
498 pUVM->vm.s.Halt.Method12.u32StartSpinningCfg,
499 pUVM->vm.s.Halt.Method12.u32StopSpinningCfg));
500 }
501
502 return VINF_SUCCESS;
503}
504
505
506/**
507 * Initialize halt method 1.
508 *
509 * @return VBox status code.
510 * @param pUVM Pointer to the user mode VM structure.
511 */
512static DECLCALLBACK(int) vmR3HaltMethod1Init(PUVM pUVM)
513{
514 return vmR3HaltMethod12ReadConfigU(pUVM);
515}
516
517
518/**
519 * Method 1 - Block whenever possible, and when lagging behind
520 * switch to spinning for 10-30ms with occasional blocking until
521 * the lag has been eliminated.
522 */
523static DECLCALLBACK(int) vmR3HaltMethod1Halt(PUVMCPU pUVCpu, const uint32_t fMask, uint64_t u64Now)
524{
525 PUVM pUVM = pUVCpu->pUVM;
526 PVMCPU pVCpu = pUVCpu->pVCpu;
527 PVM pVM = pUVCpu->pVM;
528
529 /*
530 * To simplify things, we decide up-front whether we should switch to spinning or
531 * not. This makes some ASSUMPTIONS about the cause of the spinning (PIT/RTC/PCNet)
532 * and that it will generate interrupts or other events that will cause us to exit
533 * the halt loop.
534 */
535 bool fBlockOnce = false;
536 bool fSpinning = false;
537 uint32_t u32CatchUpPct = TMVirtualSyncGetCatchUpPct(pVM);
538 if (u32CatchUpPct /* non-zero if catching up */)
539 {
540 if (pUVCpu->vm.s.Halt.Method12.u64StartSpinTS)
541 {
542 fSpinning = TMVirtualSyncGetLag(pVM) >= pUVM->vm.s.Halt.Method12.u32StopSpinningCfg;
543 if (fSpinning)
544 {
545 uint64_t u64Lag = TMVirtualSyncGetLag(pVM);
546 fBlockOnce = u64Now - pUVCpu->vm.s.Halt.Method12.u64LastBlockTS
547 > RT_MAX(pUVM->vm.s.Halt.Method12.u32MinBlockIntervalCfg,
548 RT_MIN(u64Lag / pUVM->vm.s.Halt.Method12.u32LagBlockIntervalDivisorCfg,
549 pUVM->vm.s.Halt.Method12.u32MaxBlockIntervalCfg));
550 }
551 else
552 {
553 //RTLogRelPrintf("Stopped spinning (%u ms)\n", (u64Now - pUVCpu->vm.s.Halt.Method12.u64StartSpinTS) / 1000000);
554 pUVCpu->vm.s.Halt.Method12.u64StartSpinTS = 0;
555 }
556 }
557 else
558 {
559 fSpinning = TMVirtualSyncGetLag(pVM) >= pUVM->vm.s.Halt.Method12.u32StartSpinningCfg;
560 if (fSpinning)
561 pUVCpu->vm.s.Halt.Method12.u64StartSpinTS = u64Now;
562 }
563 }
564 else if (pUVCpu->vm.s.Halt.Method12.u64StartSpinTS)
565 {
566 //RTLogRelPrintf("Stopped spinning (%u ms)\n", (u64Now - pUVCpu->vm.s.Halt.Method12.u64StartSpinTS) / 1000000);
567 pUVCpu->vm.s.Halt.Method12.u64StartSpinTS = 0;
568 }
569
570#if defined(VBOX_VMM_TARGET_ARMV8)
571 uint64_t cNsVTimerActivate = TMCpuGetVTimerActivationNano(pVCpu);
572 const bool fVTimerActive = cNsVTimerActivate != UINT64_MAX;
573#endif
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 uint64_t const u64StartTimers = RTTimeNanoTS();
587 TMR3TimerQueuesDo(pVM);
588 uint64_t const cNsElapsedTimers = RTTimeNanoTS() - u64StartTimers;
589 STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltTimers, cNsElapsedTimers);
590 if ( VM_FF_IS_ANY_SET(pVM, VM_FF_EXTERNAL_HALTED_MASK)
591 || VMCPU_FF_IS_ANY_SET(pVCpu, fMask)
592#if defined(VBOX_VMM_TARGET_ARMV8)
593 || cNsElapsedTimers >= cNsVTimerActivate
594#endif
595 )
596 {
597#if defined(VBOX_VMM_TARGET_ARMV8)
598 cNsVTimerActivate = 0;
599#endif
600 break;
601 }
602
603 /*
604 * Estimate time left to the next event.
605 */
606 uint64_t u64NanoTS;
607 TMTimerPollGIP(pVM, pVCpu, &u64NanoTS);
608 if ( VM_FF_IS_ANY_SET(pVM, VM_FF_EXTERNAL_HALTED_MASK)
609 || VMCPU_FF_IS_ANY_SET(pVCpu, fMask))
610 break;
611
612#if defined(VBOX_VMM_TARGET_ARMV8)
613 u64NanoTS = RT_MIN(cNsVTimerActivate, u64NanoTS);
614#endif
615
616 /*
617 * Block if we're not spinning and the interval isn't all that small.
618 */
619 if ( ( !fSpinning
620 || fBlockOnce)
621#if 1 /* DEBUGGING STUFF - REMOVE LATER */
622 && u64NanoTS >= 100000) /* 0.100 ms */
623#else
624 && u64NanoTS >= 250000) /* 0.250 ms */
625#endif
626 {
627 const uint64_t Start = pUVCpu->vm.s.Halt.Method12.u64LastBlockTS = RTTimeNanoTS();
628 VMMR3YieldStop(pVM);
629
630 uint32_t cMilliSecs = RT_MIN(u64NanoTS / 1000000, 15);
631 if (cMilliSecs <= pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLongAvg)
632 cMilliSecs = 1;
633 else
634 cMilliSecs -= pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLongAvg;
635
636 //RTLogRelPrintf("u64NanoTS=%RI64 cLoops=%3d sleep %02dms (%7RU64) ", u64NanoTS, cLoops, cMilliSecs, u64NanoTS);
637 uint64_t const u64StartSchedHalt = RTTimeNanoTS();
638 rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, cMilliSecs);
639 uint64_t const cNsElapsedSchedHalt = RTTimeNanoTS() - u64StartSchedHalt;
640 STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltBlock, cNsElapsedSchedHalt);
641
642 if (rc == VERR_TIMEOUT)
643 rc = VINF_SUCCESS;
644 else if (RT_FAILURE(rc))
645 {
646 rc = vmR3FatalWaitError(pUVCpu, "RTSemEventWait->%Rrc\n", rc);
647 break;
648 }
649
650 /*
651 * Calc the statistics.
652 * Update averages every 16th time, and flush parts of the history every 64th time.
653 */
654 const uint64_t Elapsed = RTTimeNanoTS() - Start;
655 pUVCpu->vm.s.Halt.Method12.cNSBlocked += Elapsed;
656 if (Elapsed > u64NanoTS)
657 pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLong += Elapsed - u64NanoTS;
658 pUVCpu->vm.s.Halt.Method12.cBlocks++;
659 if (!(pUVCpu->vm.s.Halt.Method12.cBlocks & 0xf))
660 {
661 pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLongAvg = pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLong / pUVCpu->vm.s.Halt.Method12.cBlocks;
662 if (!(pUVCpu->vm.s.Halt.Method12.cBlocks & 0x3f))
663 {
664 pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLong = pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLongAvg * 0x40;
665 pUVCpu->vm.s.Halt.Method12.cBlocks = 0x40;
666 }
667 }
668 //RTLogRelPrintf(" -> %7RU64 ns / %7RI64 ns delta%s\n", Elapsed, Elapsed - u64NanoTS, fBlockOnce ? " (block once)" : "");
669
670 /*
671 * Clear the block once flag if we actually blocked.
672 */
673 if ( fBlockOnce
674 && Elapsed > 100000 /* 0.1 ms */)
675 fBlockOnce = false;
676
677#if defined(VBOX_VMM_TARGET_ARMV8)
678 cNsVTimerActivate -= RT_MIN(cNsVTimerActivate, Elapsed);
679 /* Did the vTimer expire? */
680 if (!cNsVTimerActivate)
681 break;
682#endif
683 }
684 }
685 //if (fSpinning) RTLogRelPrintf("spun for %RU64 ns %u loops; lag=%RU64 pct=%d\n", RTTimeNanoTS() - u64Now, cLoops, TMVirtualSyncGetLag(pVM), u32CatchUpPct);
686
687#if defined(VBOX_VMM_TARGET_ARMV8)
688 if (fVTimerActive)
689 {
690 if (!cNsVTimerActivate)
691 VMCPU_FF_SET(pVCpu, VMCPU_FF_VTIMER_ACTIVATED);
692
693 TMCpuSetVTimerNextActivation(pVCpu, cNsVTimerActivate);
694 }
695#endif
696 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
697 return rc;
698}
699
700
701/**
702 * Initialize the global 1 halt method.
703 *
704 * @return VBox status code.
705 * @param pUVM Pointer to the user mode VM structure.
706 */
707static DECLCALLBACK(int) vmR3HaltGlobal1Init(PUVM pUVM)
708{
709 /*
710 * The defaults.
711 */
712 uint32_t cNsResolution = SUPSemEventMultiGetResolution(pUVM->vm.s.pSession);
713 if (cNsResolution > 5*RT_NS_100US)
714 pUVM->vm.s.Halt.Global1.cNsSpinBlockThresholdCfg = 50000;
715 else if (cNsResolution > RT_NS_100US)
716 pUVM->vm.s.Halt.Global1.cNsSpinBlockThresholdCfg = cNsResolution / 4;
717 else
718 pUVM->vm.s.Halt.Global1.cNsSpinBlockThresholdCfg = 2000;
719
720 /*
721 * Query overrides.
722 *
723 * I don't have time to bother with niceties such as invalid value checks
724 * here right now. sorry.
725 */
726 PCFGMNODE pCfg = CFGMR3GetChild(CFGMR3GetRoot(pUVM->pVM), "/VMM/HaltedGlobal1");
727 if (pCfg)
728 {
729 uint32_t u32;
730 if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "SpinBlockThreshold", &u32)))
731 pUVM->vm.s.Halt.Global1.cNsSpinBlockThresholdCfg = u32;
732 }
733 LogRel(("VMEmt: HaltedGlobal1 config: cNsSpinBlockThresholdCfg=%u\n",
734 pUVM->vm.s.Halt.Global1.cNsSpinBlockThresholdCfg));
735 return VINF_SUCCESS;
736}
737
738
739/**
740 * The global 1 halt method - Block in GMM (ring-0) and let it
741 * try take care of the global scheduling of EMT threads.
742 */
743static DECLCALLBACK(int) vmR3HaltGlobal1Halt(PUVMCPU pUVCpu, const uint32_t fMask, uint64_t u64Now)
744{
745 PUVM pUVM = pUVCpu->pUVM;
746 PVMCPU pVCpu = pUVCpu->pVCpu;
747 PVM pVM = pUVCpu->pVM;
748 Assert(VMMGetCpu(pVM) == pVCpu);
749 NOREF(u64Now);
750
751 /*
752 * Halt loop.
753 */
754 //uint64_t u64NowLog, u64Start;
755 //u64Start = u64NowLog = RTTimeNanoTS();
756 int rc = VINF_SUCCESS;
757 ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
758 unsigned cLoops = 0;
759 for (;; cLoops++)
760 {
761 /*
762 * Work the timers and check if we can exit.
763 */
764 uint64_t const u64StartTimers = RTTimeNanoTS();
765 TMR3TimerQueuesDo(pVM);
766 uint64_t const cNsElapsedTimers = RTTimeNanoTS() - u64StartTimers;
767 STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltTimers, cNsElapsedTimers);
768 if ( VM_FF_IS_ANY_SET(pVM, VM_FF_EXTERNAL_HALTED_MASK)
769 || VMCPU_FF_IS_ANY_SET(pVCpu, fMask))
770 break;
771
772 /*
773 * Estimate time left to the next event.
774 */
775 //u64NowLog = RTTimeNanoTS();
776 uint64_t u64Delta;
777 uint64_t u64GipTime = TMTimerPollGIP(pVM, pVCpu, &u64Delta);
778 if ( VM_FF_IS_ANY_SET(pVM, VM_FF_EXTERNAL_HALTED_MASK)
779 || VMCPU_FF_IS_ANY_SET(pVCpu, fMask))
780 break;
781
782 /*
783 * Block if we're not spinning and the interval isn't all that small.
784 */
785 if (u64Delta >= pUVM->vm.s.Halt.Global1.cNsSpinBlockThresholdCfg)
786 {
787 VMMR3YieldStop(pVM);
788 if ( VM_FF_IS_ANY_SET(pVM, VM_FF_EXTERNAL_HALTED_MASK)
789 || VMCPU_FF_IS_ANY_SET(pVCpu, fMask))
790 break;
791
792 //RTLogPrintf("loop=%-3d u64GipTime=%'llu / %'llu now=%'llu / %'llu\n", cLoops, u64GipTime, u64Delta, u64NowLog, u64GipTime - u64NowLog);
793 uint64_t const u64StartSchedHalt = RTTimeNanoTS();
794 rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), pVCpu->idCpu, VMMR0_DO_GVMM_SCHED_HALT, u64GipTime, NULL);
795 uint64_t const u64EndSchedHalt = RTTimeNanoTS();
796 uint64_t const cNsElapsedSchedHalt = u64EndSchedHalt - u64StartSchedHalt;
797 STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltBlock, cNsElapsedSchedHalt);
798
799 if (rc == VERR_INTERRUPTED)
800 rc = VINF_SUCCESS;
801 else if (RT_FAILURE(rc))
802 {
803 rc = vmR3FatalWaitError(pUVCpu, "vmR3HaltGlobal1Halt: VMMR0_DO_GVMM_SCHED_HALT->%Rrc\n", rc);
804 break;
805 }
806 else
807 {
808 int64_t const cNsOverslept = u64EndSchedHalt - u64GipTime;
809 if (cNsOverslept > 50000)
810 STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltBlockOverslept, cNsOverslept);
811 else if (cNsOverslept < -50000)
812 STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltBlockInsomnia, cNsElapsedSchedHalt);
813 else
814 STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltBlockOnTime, cNsElapsedSchedHalt);
815 }
816 }
817 /*
818 * When spinning call upon the GVMM and do some wakups once
819 * in a while, it's not like we're actually busy or anything.
820 */
821 else if (!(cLoops & 0x1fff))
822 {
823 uint64_t const u64StartSchedYield = RTTimeNanoTS();
824 rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), pVCpu->idCpu, VMMR0_DO_GVMM_SCHED_POLL, false /* don't yield */, NULL);
825 uint64_t const cNsElapsedSchedYield = RTTimeNanoTS() - u64StartSchedYield;
826 STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltYield, cNsElapsedSchedYield);
827 }
828 }
829 //RTLogPrintf("*** %u loops %'llu; lag=%RU64\n", cLoops, u64NowLog - u64Start, TMVirtualSyncGetLag(pVM));
830
831 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
832 return rc;
833}
834
835
836/**
837 * The global 1 halt method - VMR3Wait() worker.
838 *
839 * @returns VBox status code.
840 * @param pUVCpu Pointer to the user mode VMCPU structure.
841 */
842static DECLCALLBACK(int) vmR3HaltGlobal1Wait(PUVMCPU pUVCpu)
843{
844 ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
845
846 PVM pVM = pUVCpu->pUVM->pVM;
847 PVMCPU pVCpu = VMMGetCpu(pVM);
848 Assert(pVCpu->idCpu == pUVCpu->idCpu);
849
850 int rc = VINF_SUCCESS;
851 for (;;)
852 {
853 /*
854 * Check Relevant FFs.
855 */
856 if ( VM_FF_IS_ANY_SET(pVM, VM_FF_EXTERNAL_SUSPENDED_MASK)
857 || VMCPU_FF_IS_ANY_SET(pVCpu, VMCPU_FF_EXTERNAL_SUSPENDED_MASK))
858 break;
859
860 /*
861 * Wait for a while. Someone will wake us up or interrupt the call if
862 * anything needs our attention.
863 */
864 rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), pVCpu->idCpu, VMMR0_DO_GVMM_SCHED_HALT, RTTimeNanoTS() + 1000000000 /* +1s */, NULL);
865 if (rc == VERR_INTERRUPTED)
866 rc = VINF_SUCCESS;
867 else if (RT_FAILURE(rc))
868 {
869 rc = vmR3FatalWaitError(pUVCpu, "vmR3HaltGlobal1Wait: VMMR0_DO_GVMM_SCHED_HALT->%Rrc\n", rc);
870 break;
871 }
872 }
873
874 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
875 return rc;
876}
877
878
879/**
880 * The global 1 halt method - VMR3NotifyFF() worker.
881 *
882 * @param pUVCpu Pointer to the user mode VMCPU structure.
883 * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
884 */
885static DECLCALLBACK(void) vmR3HaltGlobal1NotifyCpuFF(PUVMCPU pUVCpu, uint32_t fFlags)
886{
887 /*
888 * With ring-0 halting, the fWait flag isn't set, so we have to check the
889 * CPU state to figure out whether to do a wakeup call.
890 */
891 PVMCPU pVCpu = pUVCpu->pVCpu;
892 if (pVCpu)
893 {
894 VMCPUSTATE enmState = VMCPU_GET_STATE(pVCpu);
895 if (enmState == VMCPUSTATE_STARTED_HALTED || pUVCpu->vm.s.fWait)
896 {
897 int rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pUVCpu->pVM), pUVCpu->idCpu, VMMR0_DO_GVMM_SCHED_WAKE_UP, 0, NULL);
898 AssertRC(rc);
899
900 }
901 else if ( (fFlags & VMNOTIFYFF_FLAGS_POKE)
902 || !(fFlags & VMNOTIFYFF_FLAGS_DONE_REM))
903 {
904 if (enmState == VMCPUSTATE_STARTED_EXEC)
905 {
906 if (fFlags & VMNOTIFYFF_FLAGS_POKE)
907 {
908 int rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pUVCpu->pVM), pUVCpu->idCpu, VMMR0_DO_GVMM_SCHED_POKE, 0, NULL);
909 AssertRC(rc);
910 }
911 }
912 else if ( enmState == VMCPUSTATE_STARTED_EXEC_NEM
913 || enmState == VMCPUSTATE_STARTED_EXEC_NEM_WAIT)
914 NEMR3NotifyFF(pUVCpu->pVM, pVCpu, fFlags);
915 }
916 }
917 /* This probably makes little sense: */
918 else if (pUVCpu->vm.s.fWait)
919 {
920 int rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pUVCpu->pVM), pUVCpu->idCpu, VMMR0_DO_GVMM_SCHED_WAKE_UP, 0, NULL);
921 AssertRC(rc);
922 }
923}
924
925
926/**
927 * Bootstrap VMR3Wait() worker.
928 *
929 * @returns VBox status code.
930 * @param pUVCpu Pointer to the user mode VMCPU structure.
931 */
932static DECLCALLBACK(int) vmR3BootstrapWait(PUVMCPU pUVCpu)
933{
934 PUVM pUVM = pUVCpu->pUVM;
935
936 ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
937
938 int rc = VINF_SUCCESS;
939 for (;;)
940 {
941 /*
942 * Check Relevant FFs.
943 */
944 if (pUVM->vm.s.pNormalReqs || pUVM->vm.s.pPriorityReqs) /* global requests pending? */
945 break;
946 if (pUVCpu->vm.s.pNormalReqs || pUVCpu->vm.s.pPriorityReqs) /* local requests pending? */
947 break;
948
949 if ( pUVCpu->pVM
950 && ( VM_FF_IS_ANY_SET(pUVCpu->pVM, VM_FF_EXTERNAL_SUSPENDED_MASK)
951 || VMCPU_FF_IS_ANY_SET(VMMGetCpu(pUVCpu->pVM), VMCPU_FF_EXTERNAL_SUSPENDED_MASK)
952 )
953 )
954 break;
955 if (pUVM->vm.s.fTerminateEMT)
956 break;
957
958 /*
959 * Wait for a while. Someone will wake us up or interrupt the call if
960 * anything needs our attention.
961 */
962 rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, 1000);
963 if (rc == VERR_TIMEOUT)
964 rc = VINF_SUCCESS;
965 else if (RT_FAILURE(rc))
966 {
967 rc = vmR3FatalWaitError(pUVCpu, "RTSemEventWait->%Rrc\n", rc);
968 break;
969 }
970 }
971
972 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
973 return rc;
974}
975
976
977/**
978 * Bootstrap VMR3NotifyFF() worker.
979 *
980 * @param pUVCpu Pointer to the user mode VMCPU structure.
981 * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
982 */
983static DECLCALLBACK(void) vmR3BootstrapNotifyCpuFF(PUVMCPU pUVCpu, uint32_t fFlags)
984{
985 if (pUVCpu->vm.s.fWait)
986 {
987 int rc = RTSemEventSignal(pUVCpu->vm.s.EventSemWait);
988 AssertRC(rc);
989 }
990 NOREF(fFlags);
991}
992
993
994/**
995 * Default VMR3Wait() worker.
996 *
997 * @returns VBox status code.
998 * @param pUVCpu Pointer to the user mode VMCPU structure.
999 */
1000static DECLCALLBACK(int) vmR3DefaultWait(PUVMCPU pUVCpu)
1001{
1002 ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
1003
1004 PVM pVM = pUVCpu->pVM;
1005 PVMCPU pVCpu = pUVCpu->pVCpu;
1006 int rc = VINF_SUCCESS;
1007 for (;;)
1008 {
1009 /*
1010 * Check Relevant FFs.
1011 */
1012 if ( VM_FF_IS_ANY_SET(pVM, VM_FF_EXTERNAL_SUSPENDED_MASK)
1013 || VMCPU_FF_IS_ANY_SET(pVCpu, VMCPU_FF_EXTERNAL_SUSPENDED_MASK))
1014 break;
1015
1016 /*
1017 * Wait for a while. Someone will wake us up or interrupt the call if
1018 * anything needs our attention.
1019 */
1020 rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, 1000);
1021 if (rc == VERR_TIMEOUT)
1022 rc = VINF_SUCCESS;
1023 else if (RT_FAILURE(rc))
1024 {
1025 rc = vmR3FatalWaitError(pUVCpu, "RTSemEventWait->%Rrc", rc);
1026 break;
1027 }
1028 }
1029
1030 ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
1031 return rc;
1032}
1033
1034
1035/**
1036 * Default VMR3NotifyFF() worker.
1037 *
1038 * @param pUVCpu Pointer to the user mode VMCPU structure.
1039 * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
1040 */
1041static DECLCALLBACK(void) vmR3DefaultNotifyCpuFF(PUVMCPU pUVCpu, uint32_t fFlags)
1042{
1043 if (pUVCpu->vm.s.fWait)
1044 {
1045 int rc = RTSemEventSignal(pUVCpu->vm.s.EventSemWait);
1046 AssertRC(rc);
1047 }
1048 else
1049 {
1050 PVMCPU pVCpu = pUVCpu->pVCpu;
1051 if (pVCpu)
1052 {
1053 VMCPUSTATE enmState = pVCpu->enmState;
1054 if ( enmState == VMCPUSTATE_STARTED_EXEC_NEM
1055 || enmState == VMCPUSTATE_STARTED_EXEC_NEM_WAIT)
1056 NEMR3NotifyFF(pUVCpu->pVM, pVCpu, fFlags);
1057 }
1058 }
1059}
1060
1061
1062/**
1063 * Array with halt method descriptors.
1064 * VMINT::iHaltMethod contains an index into this array.
1065 */
1066static const struct VMHALTMETHODDESC
1067{
1068 /** The halt method ID. */
1069 VMHALTMETHOD enmHaltMethod;
1070 /** Set if the method support halting directly in ring-0. */
1071 bool fMayHaltInRing0;
1072 /** The init function for loading config and initialize variables. */
1073 DECLR3CALLBACKMEMBER(int, pfnInit,(PUVM pUVM));
1074 /** The term function. */
1075 DECLR3CALLBACKMEMBER(void, pfnTerm,(PUVM pUVM));
1076 /** The VMR3WaitHaltedU function. */
1077 DECLR3CALLBACKMEMBER(int, pfnHalt,(PUVMCPU pUVCpu, const uint32_t fMask, uint64_t u64Now));
1078 /** The VMR3WaitU function. */
1079 DECLR3CALLBACKMEMBER(int, pfnWait,(PUVMCPU pUVCpu));
1080 /** The VMR3NotifyCpuFFU function. */
1081 DECLR3CALLBACKMEMBER(void, pfnNotifyCpuFF,(PUVMCPU pUVCpu, uint32_t fFlags));
1082 /** The VMR3NotifyGlobalFFU function. */
1083 DECLR3CALLBACKMEMBER(void, pfnNotifyGlobalFF,(PUVM pUVM, uint32_t fFlags));
1084} g_aHaltMethods[] =
1085{
1086 { VMHALTMETHOD_BOOTSTRAP, false, NULL, NULL, NULL, vmR3BootstrapWait, vmR3BootstrapNotifyCpuFF, NULL },
1087 { VMHALTMETHOD_OLD, false, NULL, NULL, vmR3HaltOldDoHalt, vmR3DefaultWait, vmR3DefaultNotifyCpuFF, NULL },
1088 { VMHALTMETHOD_1, false, vmR3HaltMethod1Init, NULL, vmR3HaltMethod1Halt, vmR3DefaultWait, vmR3DefaultNotifyCpuFF, NULL },
1089 { VMHALTMETHOD_GLOBAL_1, true, vmR3HaltGlobal1Init, NULL, vmR3HaltGlobal1Halt, vmR3HaltGlobal1Wait, vmR3HaltGlobal1NotifyCpuFF, NULL },
1090};
1091
1092
1093/**
1094 * Notify the emulation thread (EMT) about pending Forced Action (FF).
1095 *
1096 * This function is called by thread other than EMT to make
1097 * sure EMT wakes up and promptly service an FF request.
1098 *
1099 * @param pUVM Pointer to the user mode VM structure.
1100 * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
1101 * @internal
1102 */
1103VMMR3_INT_DECL(void) VMR3NotifyGlobalFFU(PUVM pUVM, uint32_t fFlags)
1104{
1105 LogFlow(("VMR3NotifyGlobalFFU:\n"));
1106 uint32_t iHaltMethod = pUVM->vm.s.iHaltMethod;
1107
1108 if (g_aHaltMethods[iHaltMethod].pfnNotifyGlobalFF) /** @todo make mandatory. */
1109 g_aHaltMethods[iHaltMethod].pfnNotifyGlobalFF(pUVM, fFlags);
1110 else
1111 for (VMCPUID iCpu = 0; iCpu < pUVM->cCpus; iCpu++)
1112 g_aHaltMethods[iHaltMethod].pfnNotifyCpuFF(&pUVM->aCpus[iCpu], fFlags);
1113}
1114
1115
1116/**
1117 * Notify the emulation thread (EMT) about pending Forced Action (FF).
1118 *
1119 * This function is called by thread other than EMT to make
1120 * sure EMT wakes up and promptly service an FF request.
1121 *
1122 * @param pUVCpu Pointer to the user mode per CPU VM structure.
1123 * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
1124 * @internal
1125 */
1126VMMR3_INT_DECL(void) VMR3NotifyCpuFFU(PUVMCPU pUVCpu, uint32_t fFlags)
1127{
1128 PUVM pUVM = pUVCpu->pUVM;
1129
1130 LogFlow(("VMR3NotifyCpuFFU:\n"));
1131 g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnNotifyCpuFF(pUVCpu, fFlags);
1132}
1133
1134
1135/**
1136 * Halted VM Wait.
1137 * Any external event will unblock the thread.
1138 *
1139 * @returns VINF_SUCCESS unless a fatal error occurred. In the latter
1140 * case an appropriate status code is returned.
1141 * @param pVM The cross context VM structure.
1142 * @param pVCpu The cross context virtual CPU structure.
1143 * @param fFlags Combination of VMWAITHALTED_F_XXX.
1144 * @thread The emulation thread.
1145 * @remarks Made visible for implementing vmsvga sync register.
1146 * @internal
1147 */
1148VMMR3_INT_DECL(int) VMR3WaitHalted(PVM pVM, PVMCPU pVCpu, uint32_t fFlags)
1149{
1150 LogFlow(("VMR3WaitHalted: fFlags=%#x\n", fFlags));
1151
1152 /*
1153 * Check Relevant FFs.
1154 */
1155#if defined(VBOX_VMM_TARGET_ARMV8)
1156 const uint32_t fMaskInterrupts = ((fFlags & VMWAITHALTED_F_IGNORE_IRQS) ? VMCPU_FF_INTERRUPT_IRQ : 0)
1157 | ((fFlags & VMWAITHALTED_F_IGNORE_FIQS) ? VMCPU_FF_INTERRUPT_FIQ : 0);
1158 const uint32_t fMask = VMCPU_FF_EXTERNAL_HALTED_MASK & ~fMaskInterrupts;
1159#else
1160 const uint32_t fMask = !(fFlags & VMWAITHALTED_F_IGNORE_IRQS)
1161 ? VMCPU_FF_EXTERNAL_HALTED_MASK
1162 : VMCPU_FF_EXTERNAL_HALTED_MASK & ~(VMCPU_FF_UPDATE_APIC | VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC);
1163#endif
1164
1165 if ( VM_FF_IS_ANY_SET(pVM, VM_FF_EXTERNAL_HALTED_MASK)
1166 || VMCPU_FF_IS_ANY_SET(pVCpu, fMask))
1167 {
1168 LogFlow(("VMR3WaitHalted: returns VINF_SUCCESS (FF %#x FFCPU %#RX64)\n", pVM->fGlobalForcedActions, (uint64_t)pVCpu->fLocalForcedActions));
1169 return VINF_SUCCESS;
1170 }
1171
1172 /*
1173 * The yielder is suspended while we're halting, while TM might have clock(s) running
1174 * only at certain times and need to be notified..
1175 */
1176 if (pVCpu->idCpu == 0)
1177 VMMR3YieldSuspend(pVM);
1178 TMNotifyStartOfHalt(pVCpu);
1179
1180 /*
1181 * Record halt averages for the last second.
1182 */
1183 PUVMCPU pUVCpu = pVCpu->pUVCpu;
1184 uint64_t u64Now = RTTimeNanoTS();
1185 int64_t off = u64Now - pUVCpu->vm.s.u64HaltsStartTS;
1186 if (off > 1000000000)
1187 {
1188 if (off > _4G || !pUVCpu->vm.s.cHalts)
1189 {
1190 pUVCpu->vm.s.HaltInterval = 1000000000 /* 1 sec */;
1191 pUVCpu->vm.s.HaltFrequency = 1;
1192 }
1193 else
1194 {
1195 pUVCpu->vm.s.HaltInterval = (uint32_t)off / pUVCpu->vm.s.cHalts;
1196 pUVCpu->vm.s.HaltFrequency = ASMMultU64ByU32DivByU32(pUVCpu->vm.s.cHalts, 1000000000, (uint32_t)off);
1197 }
1198 pUVCpu->vm.s.u64HaltsStartTS = u64Now;
1199 pUVCpu->vm.s.cHalts = 0;
1200 }
1201 pUVCpu->vm.s.cHalts++;
1202
1203 /*
1204 * Do the halt.
1205 */
1206 VMCPU_ASSERT_STATE_2(pVCpu, VMCPUSTATE_STARTED, VMCPUSTATE_STARTED_EXEC_NEM);
1207 VMCPUSTATE enmStateOld = VMCPU_GET_STATE(pVCpu);
1208 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED_HALTED);
1209 PUVM pUVM = pUVCpu->pUVM;
1210 int rc = g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnHalt(pUVCpu, fMask, u64Now);
1211 VMCPU_SET_STATE(pVCpu, enmStateOld);
1212
1213 /*
1214 * Notify TM and resume the yielder
1215 */
1216 TMNotifyEndOfHalt(pVCpu);
1217 if (pVCpu->idCpu == 0)
1218 VMMR3YieldResume(pVM);
1219
1220 LogFlow(("VMR3WaitHalted: returns %Rrc (FF %#x)\n", rc, pVM->fGlobalForcedActions));
1221 return rc;
1222}
1223
1224
1225/**
1226 * Suspended VM Wait.
1227 * Only a handful of forced actions will cause the function to
1228 * return to the caller.
1229 *
1230 * @returns VINF_SUCCESS unless a fatal error occurred. In the latter
1231 * case an appropriate status code is returned.
1232 * @param pUVCpu Pointer to the user mode VMCPU structure.
1233 * @thread The emulation thread.
1234 * @internal
1235 */
1236VMMR3_INT_DECL(int) VMR3WaitU(PUVMCPU pUVCpu)
1237{
1238 LogFlow(("VMR3WaitU:\n"));
1239
1240 /*
1241 * Check Relevant FFs.
1242 */
1243 PVM pVM = pUVCpu->pVM;
1244 PVMCPU pVCpu = pUVCpu->pVCpu;
1245
1246 if ( pVM
1247 && ( VM_FF_IS_ANY_SET(pVM, VM_FF_EXTERNAL_SUSPENDED_MASK)
1248 || VMCPU_FF_IS_ANY_SET(pVCpu, VMCPU_FF_EXTERNAL_SUSPENDED_MASK)
1249 )
1250 )
1251 {
1252 LogFlow(("VMR3Wait: returns VINF_SUCCESS (FF %#x)\n", pVM->fGlobalForcedActions));
1253 return VINF_SUCCESS;
1254 }
1255
1256 /*
1257 * Do waiting according to the halt method (so VMR3NotifyFF
1258 * doesn't have to special case anything).
1259 */
1260 PUVM pUVM = pUVCpu->pUVM;
1261 int rc = g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnWait(pUVCpu);
1262 LogFlow(("VMR3WaitU: returns %Rrc (FF %#x)\n", rc, pUVM->pVM ? pUVM->pVM->fGlobalForcedActions : 0));
1263 return rc;
1264}
1265
1266
1267/**
1268 * Interface that PDMR3Suspend, PDMR3PowerOff and PDMR3Reset uses when they wait
1269 * for the handling of asynchronous notifications to complete.
1270 *
1271 * @returns VINF_SUCCESS unless a fatal error occurred. In the latter
1272 * case an appropriate status code is returned.
1273 * @param pUVCpu Pointer to the user mode VMCPU structure.
1274 * @thread The emulation thread.
1275 */
1276VMMR3_INT_DECL(int) VMR3AsyncPdmNotificationWaitU(PUVMCPU pUVCpu)
1277{
1278 LogFlow(("VMR3AsyncPdmNotificationWaitU:\n"));
1279 return VMR3WaitU(pUVCpu);
1280}
1281
1282
1283/**
1284 * Interface that PDM the helper asynchronous notification completed methods
1285 * uses for EMT0 when it is waiting inside VMR3AsyncPdmNotificationWaitU().
1286 *
1287 * @param pUVM Pointer to the user mode VM structure.
1288 */
1289VMMR3_INT_DECL(void) VMR3AsyncPdmNotificationWakeupU(PUVM pUVM)
1290{
1291 LogFlow(("VMR3AsyncPdmNotificationWakeupU:\n"));
1292 VM_FF_SET(pUVM->pVM, VM_FF_REQUEST); /* this will have to do for now. */
1293 g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnNotifyCpuFF(&pUVM->aCpus[0], 0 /*fFlags*/);
1294}
1295
1296
1297/**
1298 * Rendezvous callback that will be called once.
1299 *
1300 * @returns VBox strict status code.
1301 * @param pVM The cross context VM structure.
1302 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1303 * @param pvUser The new g_aHaltMethods index.
1304 */
1305static DECLCALLBACK(VBOXSTRICTRC) vmR3SetHaltMethodCallback(PVM pVM, PVMCPU pVCpu, void *pvUser)
1306{
1307 PUVM pUVM = pVM->pUVM;
1308 int rc = VINF_SUCCESS;
1309 uintptr_t i = (uintptr_t)pvUser;
1310 Assert(i < RT_ELEMENTS(g_aHaltMethods));
1311
1312 /*
1313 * Main job is done once on EMT0 (it goes thru here first).
1314 */
1315 if (pVCpu->idCpu == 0)
1316 {
1317 /*
1318 * Terminate the old one.
1319 */
1320 if ( pUVM->vm.s.enmHaltMethod != VMHALTMETHOD_INVALID
1321 && g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnTerm)
1322 {
1323 g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnTerm(pUVM);
1324 pUVM->vm.s.enmHaltMethod = VMHALTMETHOD_INVALID;
1325 }
1326
1327 /* Assert that the failure fallback is where we expect. */
1328 Assert(g_aHaltMethods[0].enmHaltMethod == VMHALTMETHOD_BOOTSTRAP);
1329 Assert(!g_aHaltMethods[0].pfnTerm && !g_aHaltMethods[0].pfnInit);
1330
1331 /*
1332 * Init the new one.
1333 */
1334 memset(&pUVM->vm.s.Halt, 0, sizeof(pUVM->vm.s.Halt));
1335 if (g_aHaltMethods[i].pfnInit)
1336 {
1337 rc = g_aHaltMethods[i].pfnInit(pUVM);
1338 if (RT_FAILURE(rc))
1339 {
1340 /* Fall back on the bootstrap method. This requires no
1341 init/term (see assertion above), and will always work. */
1342 AssertLogRelRC(rc);
1343 i = 0;
1344 }
1345 }
1346
1347 /*
1348 * Commit it.
1349 */
1350 pUVM->vm.s.enmHaltMethod = g_aHaltMethods[i].enmHaltMethod;
1351 ASMAtomicWriteU32(&pUVM->vm.s.iHaltMethod, i);
1352 }
1353 else
1354 i = pUVM->vm.s.iHaltMethod;
1355
1356 /*
1357 * All EMTs must update their ring-0 halt configuration.
1358 */
1359 VMMR3SetMayHaltInRing0(pVCpu, g_aHaltMethods[i].fMayHaltInRing0,
1360 g_aHaltMethods[i].enmHaltMethod == VMHALTMETHOD_GLOBAL_1
1361 ? pUVM->vm.s.Halt.Global1.cNsSpinBlockThresholdCfg : 0);
1362
1363 return rc;
1364}
1365
1366
1367/**
1368 * Changes the halt method.
1369 *
1370 * @returns VBox status code.
1371 * @param pUVM Pointer to the user mode VM structure.
1372 * @param enmHaltMethod The new halt method.
1373 * @thread EMT.
1374 */
1375int vmR3SetHaltMethodU(PUVM pUVM, VMHALTMETHOD enmHaltMethod)
1376{
1377 PVM pVM = pUVM->pVM; Assert(pVM);
1378 VM_ASSERT_EMT(pVM);
1379 AssertReturn(enmHaltMethod > VMHALTMETHOD_INVALID && enmHaltMethod < VMHALTMETHOD_END, VERR_INVALID_PARAMETER);
1380
1381 /*
1382 * Resolve default (can be overridden in the configuration).
1383 */
1384 if (enmHaltMethod == VMHALTMETHOD_DEFAULT)
1385 {
1386 uint32_t u32;
1387 int rc = CFGMR3QueryU32(CFGMR3GetChild(CFGMR3GetRoot(pVM), "VM"), "HaltMethod", &u32);
1388 if (RT_SUCCESS(rc))
1389 {
1390 enmHaltMethod = (VMHALTMETHOD)u32;
1391 if (enmHaltMethod <= VMHALTMETHOD_INVALID || enmHaltMethod >= VMHALTMETHOD_END)
1392 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, N_("Invalid VM/HaltMethod value %d"), enmHaltMethod);
1393 }
1394 else if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_CHILD_NOT_FOUND)
1395 return VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to Query VM/HaltMethod as uint32_t"));
1396 else
1397 enmHaltMethod = VMHALTMETHOD_GLOBAL_1;
1398 //enmHaltMethod = VMHALTMETHOD_1;
1399 //enmHaltMethod = VMHALTMETHOD_OLD;
1400 }
1401
1402 /*
1403 * The global halt method doesn't work in driverless mode, so fall back on
1404 * method #1 instead.
1405 */
1406 if (!SUPR3IsDriverless() || enmHaltMethod != VMHALTMETHOD_GLOBAL_1)
1407 LogRel(("VMEmt: Halt method %s (%d)\n", vmR3GetHaltMethodName(enmHaltMethod), enmHaltMethod));
1408 else
1409 {
1410 LogRel(("VMEmt: Halt method %s (%d) not available in driverless mode, using %s (%d) instead\n",
1411 vmR3GetHaltMethodName(enmHaltMethod), enmHaltMethod, vmR3GetHaltMethodName(VMHALTMETHOD_1), VMHALTMETHOD_1));
1412 enmHaltMethod = VMHALTMETHOD_1;
1413 }
1414
1415
1416 /*
1417 * Find the descriptor.
1418 */
1419 unsigned i = 0;
1420 while ( i < RT_ELEMENTS(g_aHaltMethods)
1421 && g_aHaltMethods[i].enmHaltMethod != enmHaltMethod)
1422 i++;
1423 AssertReturn(i < RT_ELEMENTS(g_aHaltMethods), VERR_INVALID_PARAMETER);
1424
1425 /*
1426 * This needs to be done while the other EMTs are not sleeping or otherwise messing around.
1427 */
1428 return VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING, vmR3SetHaltMethodCallback, (void *)(uintptr_t)i);
1429}
1430
1431
1432/**
1433 * Special interface for implementing a HLT-like port on a device.
1434 *
1435 * This can be called directly from device code, provide the device is trusted
1436 * to access the VMM directly. Since we may not have an accurate register set
1437 * and the caller certainly shouldn't (device code does not access CPU
1438 * registers), this function will return when interrupts are pending regardless
1439 * of the actual EFLAGS.IF state.
1440 *
1441 * @returns VBox error status (never informational statuses).
1442 * @param pVM The cross context VM structure.
1443 * @param idCpu The id of the calling EMT.
1444 */
1445VMMR3DECL(int) VMR3WaitForDeviceReady(PVM pVM, VMCPUID idCpu)
1446{
1447 /*
1448 * Validate caller and resolve the CPU ID.
1449 */
1450 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1451 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
1452 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
1453 VMCPU_ASSERT_EMT_RETURN(pVCpu, VERR_VM_THREAD_NOT_EMT);
1454
1455 /*
1456 * Tag along with the HLT mechanics for now.
1457 */
1458 int rc = VMR3WaitHalted(pVM, pVCpu, false /*fIgnoreInterrupts*/);
1459 if (RT_SUCCESS(rc))
1460 return VINF_SUCCESS;
1461 return rc;
1462}
1463
1464
1465/**
1466 * Wakes up a CPU that has called VMR3WaitForDeviceReady.
1467 *
1468 * @returns VBox error status (never informational statuses).
1469 * @param pVM The cross context VM structure.
1470 * @param idCpu The id of the calling EMT.
1471 */
1472VMMR3DECL(int) VMR3NotifyCpuDeviceReady(PVM pVM, VMCPUID idCpu)
1473{
1474 /*
1475 * Validate caller and resolve the CPU ID.
1476 */
1477 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1478 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
1479 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
1480
1481 /*
1482 * Pretend it was an FF that got set since we've got logic for that already.
1483 */
1484 VMR3NotifyCpuFFU(pVCpu->pUVCpu, VMNOTIFYFF_FLAGS_DONE_REM);
1485 return VINF_SUCCESS;
1486}
1487
1488
1489/**
1490 * Returns the number of active EMTs.
1491 *
1492 * This is used by the rendezvous code during VM destruction to avoid waiting
1493 * for EMTs that aren't around any more.
1494 *
1495 * @returns Number of active EMTs. 0 if invalid parameter.
1496 * @param pUVM The user mode VM structure.
1497 */
1498VMMR3_INT_DECL(uint32_t) VMR3GetActiveEmts(PUVM pUVM)
1499{
1500 UVM_ASSERT_VALID_EXT_RETURN(pUVM, 0);
1501 return pUVM->vm.s.cActiveEmts;
1502}
1503
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