1 | /* $Id: VMEmt.cpp 106589 2024-10-22 23:56:48Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VM - Virtual Machine, The Emulation Thread.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2024 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 | *********************************************************************************************************************************/
|
---|
57 | int 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 | */
|
---|
67 | DECLCALLBACK(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 | */
|
---|
82 | int 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 | */
|
---|
325 | static 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 | */
|
---|
349 | static 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 | */
|
---|
364 | static DECLCALLBACK(int) vmR3HaltOldDoHalt(PUVMCPU pUVCpu, const uint64_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 | */
|
---|
455 | static 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 | */
|
---|
512 | static 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 | */
|
---|
523 | static DECLCALLBACK(int) vmR3HaltMethod1Halt(PUVMCPU pUVCpu, const uint64_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 | #if defined(VBOX_VMM_TARGET_ARMV8)
|
---|
604 | cNsVTimerActivate -= cNsElapsedTimers;
|
---|
605 | #endif
|
---|
606 |
|
---|
607 | /*
|
---|
608 | * Estimate time left to the next event.
|
---|
609 | */
|
---|
610 | uint64_t u64NanoTS;
|
---|
611 | TMTimerPollGIP(pVM, pVCpu, &u64NanoTS);
|
---|
612 | if ( VM_FF_IS_ANY_SET(pVM, VM_FF_EXTERNAL_HALTED_MASK)
|
---|
613 | || VMCPU_FF_IS_ANY_SET(pVCpu, fMask))
|
---|
614 | break;
|
---|
615 |
|
---|
616 | #if defined(VBOX_VMM_TARGET_ARMV8)
|
---|
617 | u64NanoTS = RT_MIN(cNsVTimerActivate, u64NanoTS);
|
---|
618 | #endif
|
---|
619 |
|
---|
620 | /*
|
---|
621 | * Block if we're not spinning and the interval isn't all that small.
|
---|
622 | */
|
---|
623 | if ( ( !fSpinning
|
---|
624 | || fBlockOnce)
|
---|
625 | #if 1 /* DEBUGGING STUFF - REMOVE LATER */
|
---|
626 | && u64NanoTS >= 100000) /* 0.100 ms */
|
---|
627 | #else
|
---|
628 | && u64NanoTS >= 250000) /* 0.250 ms */
|
---|
629 | #endif
|
---|
630 | {
|
---|
631 | const uint64_t Start = pUVCpu->vm.s.Halt.Method12.u64LastBlockTS = RTTimeNanoTS();
|
---|
632 | VMMR3YieldStop(pVM);
|
---|
633 |
|
---|
634 | uint32_t cMilliSecs = RT_MIN(u64NanoTS / RT_NS_1MS, 15);
|
---|
635 | if (cMilliSecs <= pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLongAvg / RT_NS_1MS)
|
---|
636 | cMilliSecs = 1;
|
---|
637 | else
|
---|
638 | cMilliSecs -= pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLongAvg / RT_NS_1MS;
|
---|
639 |
|
---|
640 | //RTLogRelPrintf("u64NanoTS=%RI64 cLoops=%3d sleep %02dms (%7RU64) ", u64NanoTS, cLoops, cMilliSecs, u64NanoTS);
|
---|
641 | uint64_t const u64StartSchedHalt = RTTimeNanoTS();
|
---|
642 | rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, cMilliSecs);
|
---|
643 | uint64_t const cNsElapsedSchedHalt = RTTimeNanoTS() - u64StartSchedHalt;
|
---|
644 | STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltBlock, cNsElapsedSchedHalt);
|
---|
645 |
|
---|
646 | if (rc == VERR_TIMEOUT)
|
---|
647 | rc = VINF_SUCCESS;
|
---|
648 | else if (RT_FAILURE(rc))
|
---|
649 | {
|
---|
650 | rc = vmR3FatalWaitError(pUVCpu, "RTSemEventWait->%Rrc\n", rc);
|
---|
651 | break;
|
---|
652 | }
|
---|
653 |
|
---|
654 | /*
|
---|
655 | * Calc the statistics.
|
---|
656 | * Update averages every 16th time, and flush parts of the history every 64th time.
|
---|
657 | */
|
---|
658 | const uint64_t Elapsed = RTTimeNanoTS() - Start;
|
---|
659 | pUVCpu->vm.s.Halt.Method12.cNSBlocked += Elapsed;
|
---|
660 | if (Elapsed > u64NanoTS)
|
---|
661 | pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLong += Elapsed - u64NanoTS;
|
---|
662 | pUVCpu->vm.s.Halt.Method12.cBlocks++;
|
---|
663 | if (!(pUVCpu->vm.s.Halt.Method12.cBlocks & 0xf))
|
---|
664 | {
|
---|
665 | pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLongAvg = pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLong / pUVCpu->vm.s.Halt.Method12.cBlocks;
|
---|
666 | if (!(pUVCpu->vm.s.Halt.Method12.cBlocks & 0x3f))
|
---|
667 | {
|
---|
668 | pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLong = pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLongAvg * 0x40;
|
---|
669 | pUVCpu->vm.s.Halt.Method12.cBlocks = 0x40;
|
---|
670 | }
|
---|
671 | }
|
---|
672 | //RTLogRelPrintf(" -> %7RU64 ns / %7RI64 ns delta%s\n", Elapsed, Elapsed - u64NanoTS, fBlockOnce ? " (block once)" : "");
|
---|
673 |
|
---|
674 | /*
|
---|
675 | * Clear the block once flag if we actually blocked.
|
---|
676 | */
|
---|
677 | if ( fBlockOnce
|
---|
678 | && Elapsed > 100000 /* 0.1 ms */)
|
---|
679 | fBlockOnce = false;
|
---|
680 |
|
---|
681 | #if defined(VBOX_VMM_TARGET_ARMV8)
|
---|
682 | cNsVTimerActivate -= RT_MIN(cNsVTimerActivate, Elapsed);
|
---|
683 | /* Did the vTimer expire? */
|
---|
684 | if (!cNsVTimerActivate)
|
---|
685 | break;
|
---|
686 | #endif
|
---|
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 | #if defined(VBOX_VMM_TARGET_ARMV8)
|
---|
692 | if (fVTimerActive)
|
---|
693 | {
|
---|
694 | if (!cNsVTimerActivate)
|
---|
695 | VMCPU_FF_SET(pVCpu, VMCPU_FF_VTIMER_ACTIVATED);
|
---|
696 |
|
---|
697 | TMCpuSetVTimerNextActivation(pVCpu, cNsVTimerActivate);
|
---|
698 | }
|
---|
699 | #endif
|
---|
700 | ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
|
---|
701 | return rc;
|
---|
702 | }
|
---|
703 |
|
---|
704 |
|
---|
705 | /**
|
---|
706 | * Initialize the global 1 halt method.
|
---|
707 | *
|
---|
708 | * @return VBox status code.
|
---|
709 | * @param pUVM Pointer to the user mode VM structure.
|
---|
710 | */
|
---|
711 | static DECLCALLBACK(int) vmR3HaltGlobal1Init(PUVM pUVM)
|
---|
712 | {
|
---|
713 | /*
|
---|
714 | * The defaults.
|
---|
715 | */
|
---|
716 | uint32_t cNsResolution = SUPSemEventMultiGetResolution(pUVM->vm.s.pSession);
|
---|
717 | if (cNsResolution > 5*RT_NS_100US)
|
---|
718 | pUVM->vm.s.Halt.Global1.cNsSpinBlockThresholdCfg = 50000;
|
---|
719 | else if (cNsResolution > RT_NS_100US)
|
---|
720 | pUVM->vm.s.Halt.Global1.cNsSpinBlockThresholdCfg = cNsResolution / 4;
|
---|
721 | else
|
---|
722 | pUVM->vm.s.Halt.Global1.cNsSpinBlockThresholdCfg = 2000;
|
---|
723 |
|
---|
724 | /*
|
---|
725 | * Query overrides.
|
---|
726 | *
|
---|
727 | * I don't have time to bother with niceties such as invalid value checks
|
---|
728 | * here right now. sorry.
|
---|
729 | */
|
---|
730 | PCFGMNODE pCfg = CFGMR3GetChild(CFGMR3GetRoot(pUVM->pVM), "/VMM/HaltedGlobal1");
|
---|
731 | if (pCfg)
|
---|
732 | {
|
---|
733 | uint32_t u32;
|
---|
734 | if (RT_SUCCESS(CFGMR3QueryU32(pCfg, "SpinBlockThreshold", &u32)))
|
---|
735 | pUVM->vm.s.Halt.Global1.cNsSpinBlockThresholdCfg = u32;
|
---|
736 | }
|
---|
737 | LogRel(("VMEmt: HaltedGlobal1 config: cNsSpinBlockThresholdCfg=%u\n",
|
---|
738 | pUVM->vm.s.Halt.Global1.cNsSpinBlockThresholdCfg));
|
---|
739 | return VINF_SUCCESS;
|
---|
740 | }
|
---|
741 |
|
---|
742 |
|
---|
743 | /**
|
---|
744 | * The global 1 halt method - Block in GMM (ring-0) and let it
|
---|
745 | * try take care of the global scheduling of EMT threads.
|
---|
746 | */
|
---|
747 | static DECLCALLBACK(int) vmR3HaltGlobal1Halt(PUVMCPU pUVCpu, const uint64_t fMask, uint64_t u64Now)
|
---|
748 | {
|
---|
749 | PUVM pUVM = pUVCpu->pUVM;
|
---|
750 | PVMCPU pVCpu = pUVCpu->pVCpu;
|
---|
751 | PVM pVM = pUVCpu->pVM;
|
---|
752 | Assert(VMMGetCpu(pVM) == pVCpu);
|
---|
753 | NOREF(u64Now);
|
---|
754 |
|
---|
755 | /*
|
---|
756 | * Halt loop.
|
---|
757 | */
|
---|
758 | //uint64_t u64NowLog, u64Start;
|
---|
759 | //u64Start = u64NowLog = RTTimeNanoTS();
|
---|
760 | int rc = VINF_SUCCESS;
|
---|
761 | ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
|
---|
762 | unsigned cLoops = 0;
|
---|
763 | for (;; cLoops++)
|
---|
764 | {
|
---|
765 | /*
|
---|
766 | * Work the timers and check if we can exit.
|
---|
767 | */
|
---|
768 | uint64_t const u64StartTimers = RTTimeNanoTS();
|
---|
769 | TMR3TimerQueuesDo(pVM);
|
---|
770 | uint64_t const cNsElapsedTimers = RTTimeNanoTS() - u64StartTimers;
|
---|
771 | STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltTimers, cNsElapsedTimers);
|
---|
772 | if ( VM_FF_IS_ANY_SET(pVM, VM_FF_EXTERNAL_HALTED_MASK)
|
---|
773 | || VMCPU_FF_IS_ANY_SET(pVCpu, fMask))
|
---|
774 | break;
|
---|
775 |
|
---|
776 | /*
|
---|
777 | * Estimate time left to the next event.
|
---|
778 | */
|
---|
779 | //u64NowLog = RTTimeNanoTS();
|
---|
780 | uint64_t u64Delta;
|
---|
781 | uint64_t u64GipTime = TMTimerPollGIP(pVM, pVCpu, &u64Delta);
|
---|
782 | if ( VM_FF_IS_ANY_SET(pVM, VM_FF_EXTERNAL_HALTED_MASK)
|
---|
783 | || VMCPU_FF_IS_ANY_SET(pVCpu, fMask))
|
---|
784 | break;
|
---|
785 |
|
---|
786 | /*
|
---|
787 | * Block if we're not spinning and the interval isn't all that small.
|
---|
788 | */
|
---|
789 | if (u64Delta >= pUVM->vm.s.Halt.Global1.cNsSpinBlockThresholdCfg)
|
---|
790 | {
|
---|
791 | VMMR3YieldStop(pVM);
|
---|
792 | if ( VM_FF_IS_ANY_SET(pVM, VM_FF_EXTERNAL_HALTED_MASK)
|
---|
793 | || VMCPU_FF_IS_ANY_SET(pVCpu, fMask))
|
---|
794 | break;
|
---|
795 |
|
---|
796 | //RTLogPrintf("loop=%-3d u64GipTime=%'llu / %'llu now=%'llu / %'llu\n", cLoops, u64GipTime, u64Delta, u64NowLog, u64GipTime - u64NowLog);
|
---|
797 | uint64_t const u64StartSchedHalt = RTTimeNanoTS();
|
---|
798 | rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), pVCpu->idCpu, VMMR0_DO_GVMM_SCHED_HALT, u64GipTime, NULL);
|
---|
799 | uint64_t const u64EndSchedHalt = RTTimeNanoTS();
|
---|
800 | uint64_t const cNsElapsedSchedHalt = u64EndSchedHalt - u64StartSchedHalt;
|
---|
801 | STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltBlock, cNsElapsedSchedHalt);
|
---|
802 |
|
---|
803 | if (rc == VERR_INTERRUPTED)
|
---|
804 | rc = VINF_SUCCESS;
|
---|
805 | else if (RT_FAILURE(rc))
|
---|
806 | {
|
---|
807 | rc = vmR3FatalWaitError(pUVCpu, "vmR3HaltGlobal1Halt: VMMR0_DO_GVMM_SCHED_HALT->%Rrc\n", rc);
|
---|
808 | break;
|
---|
809 | }
|
---|
810 | else
|
---|
811 | {
|
---|
812 | int64_t const cNsOverslept = u64EndSchedHalt - u64GipTime;
|
---|
813 | if (cNsOverslept > 50000)
|
---|
814 | STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltBlockOverslept, cNsOverslept);
|
---|
815 | else if (cNsOverslept < -50000)
|
---|
816 | STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltBlockInsomnia, cNsElapsedSchedHalt);
|
---|
817 | else
|
---|
818 | STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltBlockOnTime, cNsElapsedSchedHalt);
|
---|
819 | }
|
---|
820 | }
|
---|
821 | /*
|
---|
822 | * When spinning call upon the GVMM and do some wakups once
|
---|
823 | * in a while, it's not like we're actually busy or anything.
|
---|
824 | */
|
---|
825 | else if (!(cLoops & 0x1fff))
|
---|
826 | {
|
---|
827 | uint64_t const u64StartSchedYield = RTTimeNanoTS();
|
---|
828 | rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), pVCpu->idCpu, VMMR0_DO_GVMM_SCHED_POLL, false /* don't yield */, NULL);
|
---|
829 | uint64_t const cNsElapsedSchedYield = RTTimeNanoTS() - u64StartSchedYield;
|
---|
830 | STAM_REL_PROFILE_ADD_PERIOD(&pUVCpu->vm.s.StatHaltYield, cNsElapsedSchedYield);
|
---|
831 | }
|
---|
832 | }
|
---|
833 | //RTLogPrintf("*** %u loops %'llu; lag=%RU64\n", cLoops, u64NowLog - u64Start, TMVirtualSyncGetLag(pVM));
|
---|
834 |
|
---|
835 | ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
|
---|
836 | return rc;
|
---|
837 | }
|
---|
838 |
|
---|
839 |
|
---|
840 | /**
|
---|
841 | * The global 1 halt method - VMR3Wait() worker.
|
---|
842 | *
|
---|
843 | * @returns VBox status code.
|
---|
844 | * @param pUVCpu Pointer to the user mode VMCPU structure.
|
---|
845 | */
|
---|
846 | static DECLCALLBACK(int) vmR3HaltGlobal1Wait(PUVMCPU pUVCpu)
|
---|
847 | {
|
---|
848 | ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
|
---|
849 |
|
---|
850 | PVM pVM = pUVCpu->pUVM->pVM;
|
---|
851 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
852 | Assert(pVCpu->idCpu == pUVCpu->idCpu);
|
---|
853 |
|
---|
854 | int rc = VINF_SUCCESS;
|
---|
855 | for (;;)
|
---|
856 | {
|
---|
857 | /*
|
---|
858 | * Check Relevant FFs.
|
---|
859 | */
|
---|
860 | if ( VM_FF_IS_ANY_SET(pVM, VM_FF_EXTERNAL_SUSPENDED_MASK)
|
---|
861 | || VMCPU_FF_IS_ANY_SET(pVCpu, VMCPU_FF_EXTERNAL_SUSPENDED_MASK))
|
---|
862 | break;
|
---|
863 |
|
---|
864 | /*
|
---|
865 | * Wait for a while. Someone will wake us up or interrupt the call if
|
---|
866 | * anything needs our attention.
|
---|
867 | */
|
---|
868 | rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), pVCpu->idCpu, VMMR0_DO_GVMM_SCHED_HALT, RTTimeNanoTS() + 1000000000 /* +1s */, NULL);
|
---|
869 | if (rc == VERR_INTERRUPTED)
|
---|
870 | rc = VINF_SUCCESS;
|
---|
871 | else if (RT_FAILURE(rc))
|
---|
872 | {
|
---|
873 | rc = vmR3FatalWaitError(pUVCpu, "vmR3HaltGlobal1Wait: VMMR0_DO_GVMM_SCHED_HALT->%Rrc\n", rc);
|
---|
874 | break;
|
---|
875 | }
|
---|
876 | }
|
---|
877 |
|
---|
878 | ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
|
---|
879 | return rc;
|
---|
880 | }
|
---|
881 |
|
---|
882 |
|
---|
883 | /**
|
---|
884 | * The global 1 halt method - VMR3NotifyFF() worker.
|
---|
885 | *
|
---|
886 | * @param pUVCpu Pointer to the user mode VMCPU structure.
|
---|
887 | * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
|
---|
888 | */
|
---|
889 | static DECLCALLBACK(void) vmR3HaltGlobal1NotifyCpuFF(PUVMCPU pUVCpu, uint32_t fFlags)
|
---|
890 | {
|
---|
891 | /*
|
---|
892 | * With ring-0 halting, the fWait flag isn't set, so we have to check the
|
---|
893 | * CPU state to figure out whether to do a wakeup call.
|
---|
894 | */
|
---|
895 | PVMCPU pVCpu = pUVCpu->pVCpu;
|
---|
896 | if (pVCpu)
|
---|
897 | {
|
---|
898 | VMCPUSTATE enmState = VMCPU_GET_STATE(pVCpu);
|
---|
899 | if (enmState == VMCPUSTATE_STARTED_HALTED || pUVCpu->vm.s.fWait)
|
---|
900 | {
|
---|
901 | int rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pUVCpu->pVM), pUVCpu->idCpu, VMMR0_DO_GVMM_SCHED_WAKE_UP, 0, NULL);
|
---|
902 | AssertRC(rc);
|
---|
903 |
|
---|
904 | }
|
---|
905 | else if ( (fFlags & VMNOTIFYFF_FLAGS_POKE)
|
---|
906 | || !(fFlags & VMNOTIFYFF_FLAGS_DONE_REM))
|
---|
907 | {
|
---|
908 | if (enmState == VMCPUSTATE_STARTED_EXEC)
|
---|
909 | {
|
---|
910 | if (fFlags & VMNOTIFYFF_FLAGS_POKE)
|
---|
911 | {
|
---|
912 | int rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pUVCpu->pVM), pUVCpu->idCpu, VMMR0_DO_GVMM_SCHED_POKE, 0, NULL);
|
---|
913 | AssertRC(rc);
|
---|
914 | }
|
---|
915 | }
|
---|
916 | else if ( enmState == VMCPUSTATE_STARTED_EXEC_NEM
|
---|
917 | || enmState == VMCPUSTATE_STARTED_EXEC_NEM_WAIT)
|
---|
918 | NEMR3NotifyFF(pUVCpu->pVM, pVCpu, fFlags);
|
---|
919 | }
|
---|
920 | }
|
---|
921 | /* This probably makes little sense: */
|
---|
922 | else if (pUVCpu->vm.s.fWait)
|
---|
923 | {
|
---|
924 | int rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pUVCpu->pVM), pUVCpu->idCpu, VMMR0_DO_GVMM_SCHED_WAKE_UP, 0, NULL);
|
---|
925 | AssertRC(rc);
|
---|
926 | }
|
---|
927 | }
|
---|
928 |
|
---|
929 |
|
---|
930 | /**
|
---|
931 | * Bootstrap VMR3Wait() worker.
|
---|
932 | *
|
---|
933 | * @returns VBox status code.
|
---|
934 | * @param pUVCpu Pointer to the user mode VMCPU structure.
|
---|
935 | */
|
---|
936 | static DECLCALLBACK(int) vmR3BootstrapWait(PUVMCPU pUVCpu)
|
---|
937 | {
|
---|
938 | PUVM pUVM = pUVCpu->pUVM;
|
---|
939 |
|
---|
940 | ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
|
---|
941 |
|
---|
942 | int rc = VINF_SUCCESS;
|
---|
943 | for (;;)
|
---|
944 | {
|
---|
945 | /*
|
---|
946 | * Check Relevant FFs.
|
---|
947 | */
|
---|
948 | if (pUVM->vm.s.pNormalReqs || pUVM->vm.s.pPriorityReqs) /* global requests pending? */
|
---|
949 | break;
|
---|
950 | if (pUVCpu->vm.s.pNormalReqs || pUVCpu->vm.s.pPriorityReqs) /* local requests pending? */
|
---|
951 | break;
|
---|
952 |
|
---|
953 | if ( pUVCpu->pVM
|
---|
954 | && ( VM_FF_IS_ANY_SET(pUVCpu->pVM, VM_FF_EXTERNAL_SUSPENDED_MASK)
|
---|
955 | || VMCPU_FF_IS_ANY_SET(VMMGetCpu(pUVCpu->pVM), VMCPU_FF_EXTERNAL_SUSPENDED_MASK)
|
---|
956 | )
|
---|
957 | )
|
---|
958 | break;
|
---|
959 | if (pUVM->vm.s.fTerminateEMT)
|
---|
960 | break;
|
---|
961 |
|
---|
962 | /*
|
---|
963 | * Wait for a while. Someone will wake us up or interrupt the call if
|
---|
964 | * anything needs our attention.
|
---|
965 | */
|
---|
966 | rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, 1000);
|
---|
967 | if (rc == VERR_TIMEOUT)
|
---|
968 | rc = VINF_SUCCESS;
|
---|
969 | else if (RT_FAILURE(rc))
|
---|
970 | {
|
---|
971 | rc = vmR3FatalWaitError(pUVCpu, "RTSemEventWait->%Rrc\n", rc);
|
---|
972 | break;
|
---|
973 | }
|
---|
974 | }
|
---|
975 |
|
---|
976 | ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
|
---|
977 | return rc;
|
---|
978 | }
|
---|
979 |
|
---|
980 |
|
---|
981 | /**
|
---|
982 | * Bootstrap VMR3NotifyFF() worker.
|
---|
983 | *
|
---|
984 | * @param pUVCpu Pointer to the user mode VMCPU structure.
|
---|
985 | * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
|
---|
986 | */
|
---|
987 | static DECLCALLBACK(void) vmR3BootstrapNotifyCpuFF(PUVMCPU pUVCpu, uint32_t fFlags)
|
---|
988 | {
|
---|
989 | if (pUVCpu->vm.s.fWait)
|
---|
990 | {
|
---|
991 | int rc = RTSemEventSignal(pUVCpu->vm.s.EventSemWait);
|
---|
992 | AssertRC(rc);
|
---|
993 | }
|
---|
994 | NOREF(fFlags);
|
---|
995 | }
|
---|
996 |
|
---|
997 |
|
---|
998 | /**
|
---|
999 | * Default VMR3Wait() worker.
|
---|
1000 | *
|
---|
1001 | * @returns VBox status code.
|
---|
1002 | * @param pUVCpu Pointer to the user mode VMCPU structure.
|
---|
1003 | */
|
---|
1004 | static DECLCALLBACK(int) vmR3DefaultWait(PUVMCPU pUVCpu)
|
---|
1005 | {
|
---|
1006 | ASMAtomicWriteBool(&pUVCpu->vm.s.fWait, true);
|
---|
1007 |
|
---|
1008 | PVM pVM = pUVCpu->pVM;
|
---|
1009 | PVMCPU pVCpu = pUVCpu->pVCpu;
|
---|
1010 | int rc = VINF_SUCCESS;
|
---|
1011 | for (;;)
|
---|
1012 | {
|
---|
1013 | /*
|
---|
1014 | * Check Relevant FFs.
|
---|
1015 | */
|
---|
1016 | if ( VM_FF_IS_ANY_SET(pVM, VM_FF_EXTERNAL_SUSPENDED_MASK)
|
---|
1017 | || VMCPU_FF_IS_ANY_SET(pVCpu, VMCPU_FF_EXTERNAL_SUSPENDED_MASK))
|
---|
1018 | break;
|
---|
1019 |
|
---|
1020 | /*
|
---|
1021 | * Wait for a while. Someone will wake us up or interrupt the call if
|
---|
1022 | * anything needs our attention.
|
---|
1023 | */
|
---|
1024 | rc = RTSemEventWait(pUVCpu->vm.s.EventSemWait, 1000);
|
---|
1025 | if (rc == VERR_TIMEOUT)
|
---|
1026 | rc = VINF_SUCCESS;
|
---|
1027 | else if (RT_FAILURE(rc))
|
---|
1028 | {
|
---|
1029 | rc = vmR3FatalWaitError(pUVCpu, "RTSemEventWait->%Rrc", rc);
|
---|
1030 | break;
|
---|
1031 | }
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | ASMAtomicUoWriteBool(&pUVCpu->vm.s.fWait, false);
|
---|
1035 | return rc;
|
---|
1036 | }
|
---|
1037 |
|
---|
1038 |
|
---|
1039 | /**
|
---|
1040 | * Default VMR3NotifyFF() worker.
|
---|
1041 | *
|
---|
1042 | * @param pUVCpu Pointer to the user mode VMCPU structure.
|
---|
1043 | * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
|
---|
1044 | */
|
---|
1045 | static DECLCALLBACK(void) vmR3DefaultNotifyCpuFF(PUVMCPU pUVCpu, uint32_t fFlags)
|
---|
1046 | {
|
---|
1047 | if (pUVCpu->vm.s.fWait)
|
---|
1048 | {
|
---|
1049 | int rc = RTSemEventSignal(pUVCpu->vm.s.EventSemWait);
|
---|
1050 | AssertRC(rc);
|
---|
1051 | }
|
---|
1052 | else
|
---|
1053 | {
|
---|
1054 | PVMCPU pVCpu = pUVCpu->pVCpu;
|
---|
1055 | if (pVCpu)
|
---|
1056 | {
|
---|
1057 | VMCPUSTATE enmState = pVCpu->enmState;
|
---|
1058 | if ( enmState == VMCPUSTATE_STARTED_EXEC_NEM
|
---|
1059 | || enmState == VMCPUSTATE_STARTED_EXEC_NEM_WAIT)
|
---|
1060 | NEMR3NotifyFF(pUVCpu->pVM, pVCpu, fFlags);
|
---|
1061 | }
|
---|
1062 | }
|
---|
1063 | }
|
---|
1064 |
|
---|
1065 |
|
---|
1066 | /**
|
---|
1067 | * Array with halt method descriptors.
|
---|
1068 | * VMINT::iHaltMethod contains an index into this array.
|
---|
1069 | */
|
---|
1070 | static const struct VMHALTMETHODDESC
|
---|
1071 | {
|
---|
1072 | /** The halt method ID. */
|
---|
1073 | VMHALTMETHOD enmHaltMethod;
|
---|
1074 | /** Set if the method support halting directly in ring-0. */
|
---|
1075 | bool fMayHaltInRing0;
|
---|
1076 | /** The init function for loading config and initialize variables. */
|
---|
1077 | DECLR3CALLBACKMEMBER(int, pfnInit,(PUVM pUVM));
|
---|
1078 | /** The term function. */
|
---|
1079 | DECLR3CALLBACKMEMBER(void, pfnTerm,(PUVM pUVM));
|
---|
1080 | /** The VMR3WaitHaltedU function. */
|
---|
1081 | DECLR3CALLBACKMEMBER(int, pfnHalt,(PUVMCPU pUVCpu, const uint64_t fMask, uint64_t u64Now));
|
---|
1082 | /** The VMR3WaitU function. */
|
---|
1083 | DECLR3CALLBACKMEMBER(int, pfnWait,(PUVMCPU pUVCpu));
|
---|
1084 | /** The VMR3NotifyCpuFFU function. */
|
---|
1085 | DECLR3CALLBACKMEMBER(void, pfnNotifyCpuFF,(PUVMCPU pUVCpu, uint32_t fFlags));
|
---|
1086 | /** The VMR3NotifyGlobalFFU function. */
|
---|
1087 | DECLR3CALLBACKMEMBER(void, pfnNotifyGlobalFF,(PUVM pUVM, uint32_t fFlags));
|
---|
1088 | } g_aHaltMethods[] =
|
---|
1089 | {
|
---|
1090 | { VMHALTMETHOD_BOOTSTRAP, false, NULL, NULL, NULL, vmR3BootstrapWait, vmR3BootstrapNotifyCpuFF, NULL },
|
---|
1091 | { VMHALTMETHOD_OLD, false, NULL, NULL, vmR3HaltOldDoHalt, vmR3DefaultWait, vmR3DefaultNotifyCpuFF, NULL },
|
---|
1092 | { VMHALTMETHOD_1, false, vmR3HaltMethod1Init, NULL, vmR3HaltMethod1Halt, vmR3DefaultWait, vmR3DefaultNotifyCpuFF, NULL },
|
---|
1093 | { VMHALTMETHOD_GLOBAL_1, true, vmR3HaltGlobal1Init, NULL, vmR3HaltGlobal1Halt, vmR3HaltGlobal1Wait, vmR3HaltGlobal1NotifyCpuFF, NULL },
|
---|
1094 | };
|
---|
1095 |
|
---|
1096 |
|
---|
1097 | /**
|
---|
1098 | * Notify the emulation thread (EMT) about pending Forced Action (FF).
|
---|
1099 | *
|
---|
1100 | * This function is called by thread other than EMT to make
|
---|
1101 | * sure EMT wakes up and promptly service an FF request.
|
---|
1102 | *
|
---|
1103 | * @param pUVM Pointer to the user mode VM structure.
|
---|
1104 | * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
|
---|
1105 | * @internal
|
---|
1106 | */
|
---|
1107 | VMMR3_INT_DECL(void) VMR3NotifyGlobalFFU(PUVM pUVM, uint32_t fFlags)
|
---|
1108 | {
|
---|
1109 | LogFlow(("VMR3NotifyGlobalFFU:\n"));
|
---|
1110 | uint32_t iHaltMethod = pUVM->vm.s.iHaltMethod;
|
---|
1111 |
|
---|
1112 | if (g_aHaltMethods[iHaltMethod].pfnNotifyGlobalFF) /** @todo make mandatory. */
|
---|
1113 | g_aHaltMethods[iHaltMethod].pfnNotifyGlobalFF(pUVM, fFlags);
|
---|
1114 | else
|
---|
1115 | for (VMCPUID iCpu = 0; iCpu < pUVM->cCpus; iCpu++)
|
---|
1116 | g_aHaltMethods[iHaltMethod].pfnNotifyCpuFF(&pUVM->aCpus[iCpu], fFlags);
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 |
|
---|
1120 | /**
|
---|
1121 | * Notify the emulation thread (EMT) about pending Forced Action (FF).
|
---|
1122 | *
|
---|
1123 | * This function is called by thread other than EMT to make
|
---|
1124 | * sure EMT wakes up and promptly service an FF request.
|
---|
1125 | *
|
---|
1126 | * @param pUVCpu Pointer to the user mode per CPU VM structure.
|
---|
1127 | * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
|
---|
1128 | * @internal
|
---|
1129 | */
|
---|
1130 | VMMR3_INT_DECL(void) VMR3NotifyCpuFFU(PUVMCPU pUVCpu, uint32_t fFlags)
|
---|
1131 | {
|
---|
1132 | PUVM pUVM = pUVCpu->pUVM;
|
---|
1133 |
|
---|
1134 | LogFlow(("VMR3NotifyCpuFFU:\n"));
|
---|
1135 | g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnNotifyCpuFF(pUVCpu, fFlags);
|
---|
1136 | }
|
---|
1137 |
|
---|
1138 |
|
---|
1139 | /**
|
---|
1140 | * Halted VM Wait.
|
---|
1141 | * Any external event will unblock the thread.
|
---|
1142 | *
|
---|
1143 | * @returns VINF_SUCCESS unless a fatal error occurred. In the latter
|
---|
1144 | * case an appropriate status code is returned.
|
---|
1145 | * @param pVM The cross context VM structure.
|
---|
1146 | * @param pVCpu The cross context virtual CPU structure.
|
---|
1147 | * @param fFlags Combination of VMWAITHALTED_F_XXX.
|
---|
1148 | * @thread The emulation thread.
|
---|
1149 | * @remarks Made visible for implementing vmsvga sync register.
|
---|
1150 | * @internal
|
---|
1151 | */
|
---|
1152 | VMMR3_INT_DECL(int) VMR3WaitHalted(PVM pVM, PVMCPU pVCpu, uint32_t fFlags)
|
---|
1153 | {
|
---|
1154 | LogFlow(("VMR3WaitHalted: fFlags=%#x\n", fFlags));
|
---|
1155 |
|
---|
1156 | /*
|
---|
1157 | * Check Relevant FFs.
|
---|
1158 | */
|
---|
1159 | #if defined(VBOX_VMM_TARGET_ARMV8)
|
---|
1160 | const uint64_t fMaskInterrupts = ((fFlags & VMWAITHALTED_F_IGNORE_IRQS) ? VMCPU_FF_INTERRUPT_IRQ : 0)
|
---|
1161 | | ((fFlags & VMWAITHALTED_F_IGNORE_FIQS) ? VMCPU_FF_INTERRUPT_FIQ : 0);
|
---|
1162 | const uint64_t fMask = VMCPU_FF_EXTERNAL_HALTED_MASK & ~fMaskInterrupts;
|
---|
1163 | #else
|
---|
1164 | const uint64_t fMask = !(fFlags & VMWAITHALTED_F_IGNORE_IRQS)
|
---|
1165 | ? VMCPU_FF_EXTERNAL_HALTED_MASK
|
---|
1166 | : VMCPU_FF_EXTERNAL_HALTED_MASK & ~(VMCPU_FF_UPDATE_APIC | VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC);
|
---|
1167 | #endif
|
---|
1168 |
|
---|
1169 | if ( VM_FF_IS_ANY_SET(pVM, VM_FF_EXTERNAL_HALTED_MASK)
|
---|
1170 | || VMCPU_FF_IS_ANY_SET(pVCpu, fMask))
|
---|
1171 | {
|
---|
1172 | LogFlow(("VMR3WaitHalted: returns VINF_SUCCESS (FF %#x FFCPU %#RX64)\n", pVM->fGlobalForcedActions, (uint64_t)pVCpu->fLocalForcedActions));
|
---|
1173 | return VINF_SUCCESS;
|
---|
1174 | }
|
---|
1175 |
|
---|
1176 | /*
|
---|
1177 | * The yielder is suspended while we're halting, while TM might have clock(s) running
|
---|
1178 | * only at certain times and need to be notified..
|
---|
1179 | */
|
---|
1180 | if (pVCpu->idCpu == 0)
|
---|
1181 | VMMR3YieldSuspend(pVM);
|
---|
1182 | TMNotifyStartOfHalt(pVCpu);
|
---|
1183 |
|
---|
1184 | /*
|
---|
1185 | * Record halt averages for the last second.
|
---|
1186 | */
|
---|
1187 | PUVMCPU pUVCpu = pVCpu->pUVCpu;
|
---|
1188 | uint64_t u64Now = RTTimeNanoTS();
|
---|
1189 | int64_t off = u64Now - pUVCpu->vm.s.u64HaltsStartTS;
|
---|
1190 | if (off > 1000000000)
|
---|
1191 | {
|
---|
1192 | if (off > _4G || !pUVCpu->vm.s.cHalts)
|
---|
1193 | {
|
---|
1194 | pUVCpu->vm.s.HaltInterval = 1000000000 /* 1 sec */;
|
---|
1195 | pUVCpu->vm.s.HaltFrequency = 1;
|
---|
1196 | }
|
---|
1197 | else
|
---|
1198 | {
|
---|
1199 | pUVCpu->vm.s.HaltInterval = (uint32_t)off / pUVCpu->vm.s.cHalts;
|
---|
1200 | pUVCpu->vm.s.HaltFrequency = ASMMultU64ByU32DivByU32(pUVCpu->vm.s.cHalts, 1000000000, (uint32_t)off);
|
---|
1201 | }
|
---|
1202 | pUVCpu->vm.s.u64HaltsStartTS = u64Now;
|
---|
1203 | pUVCpu->vm.s.cHalts = 0;
|
---|
1204 | }
|
---|
1205 | pUVCpu->vm.s.cHalts++;
|
---|
1206 |
|
---|
1207 | /*
|
---|
1208 | * Do the halt.
|
---|
1209 | */
|
---|
1210 | VMCPU_ASSERT_STATE_2(pVCpu, VMCPUSTATE_STARTED, VMCPUSTATE_STARTED_EXEC_NEM);
|
---|
1211 | VMCPUSTATE enmStateOld = VMCPU_GET_STATE(pVCpu);
|
---|
1212 | VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED_HALTED);
|
---|
1213 | PUVM pUVM = pUVCpu->pUVM;
|
---|
1214 | int rc = g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnHalt(pUVCpu, fMask, u64Now);
|
---|
1215 | VMCPU_SET_STATE(pVCpu, enmStateOld);
|
---|
1216 |
|
---|
1217 | /*
|
---|
1218 | * Notify TM and resume the yielder
|
---|
1219 | */
|
---|
1220 | TMNotifyEndOfHalt(pVCpu);
|
---|
1221 | if (pVCpu->idCpu == 0)
|
---|
1222 | VMMR3YieldResume(pVM);
|
---|
1223 |
|
---|
1224 | LogFlow(("VMR3WaitHalted: returns %Rrc (FF %#x)\n", rc, pVM->fGlobalForcedActions));
|
---|
1225 | return rc;
|
---|
1226 | }
|
---|
1227 |
|
---|
1228 |
|
---|
1229 | /**
|
---|
1230 | * Suspended VM Wait.
|
---|
1231 | * Only a handful of forced actions will cause the function to
|
---|
1232 | * return to the caller.
|
---|
1233 | *
|
---|
1234 | * @returns VINF_SUCCESS unless a fatal error occurred. In the latter
|
---|
1235 | * case an appropriate status code is returned.
|
---|
1236 | * @param pUVCpu Pointer to the user mode VMCPU structure.
|
---|
1237 | * @thread The emulation thread.
|
---|
1238 | * @internal
|
---|
1239 | */
|
---|
1240 | VMMR3_INT_DECL(int) VMR3WaitU(PUVMCPU pUVCpu)
|
---|
1241 | {
|
---|
1242 | LogFlow(("VMR3WaitU:\n"));
|
---|
1243 |
|
---|
1244 | /*
|
---|
1245 | * Check Relevant FFs.
|
---|
1246 | */
|
---|
1247 | PVM pVM = pUVCpu->pVM;
|
---|
1248 | PVMCPU pVCpu = pUVCpu->pVCpu;
|
---|
1249 |
|
---|
1250 | if ( pVM
|
---|
1251 | && ( VM_FF_IS_ANY_SET(pVM, VM_FF_EXTERNAL_SUSPENDED_MASK)
|
---|
1252 | || VMCPU_FF_IS_ANY_SET(pVCpu, VMCPU_FF_EXTERNAL_SUSPENDED_MASK)
|
---|
1253 | )
|
---|
1254 | )
|
---|
1255 | {
|
---|
1256 | LogFlow(("VMR3Wait: returns VINF_SUCCESS (FF %#x)\n", pVM->fGlobalForcedActions));
|
---|
1257 | return VINF_SUCCESS;
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 | /*
|
---|
1261 | * Do waiting according to the halt method (so VMR3NotifyFF
|
---|
1262 | * doesn't have to special case anything).
|
---|
1263 | */
|
---|
1264 | PUVM pUVM = pUVCpu->pUVM;
|
---|
1265 | int rc = g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnWait(pUVCpu);
|
---|
1266 | LogFlow(("VMR3WaitU: returns %Rrc (FF %#x)\n", rc, pUVM->pVM ? pUVM->pVM->fGlobalForcedActions : 0));
|
---|
1267 | return rc;
|
---|
1268 | }
|
---|
1269 |
|
---|
1270 |
|
---|
1271 | /**
|
---|
1272 | * Interface that PDMR3Suspend, PDMR3PowerOff and PDMR3Reset uses when they wait
|
---|
1273 | * for the handling of asynchronous notifications to complete.
|
---|
1274 | *
|
---|
1275 | * @returns VINF_SUCCESS unless a fatal error occurred. In the latter
|
---|
1276 | * case an appropriate status code is returned.
|
---|
1277 | * @param pUVCpu Pointer to the user mode VMCPU structure.
|
---|
1278 | * @thread The emulation thread.
|
---|
1279 | */
|
---|
1280 | VMMR3_INT_DECL(int) VMR3AsyncPdmNotificationWaitU(PUVMCPU pUVCpu)
|
---|
1281 | {
|
---|
1282 | LogFlow(("VMR3AsyncPdmNotificationWaitU:\n"));
|
---|
1283 | return VMR3WaitU(pUVCpu);
|
---|
1284 | }
|
---|
1285 |
|
---|
1286 |
|
---|
1287 | /**
|
---|
1288 | * Interface that PDM the helper asynchronous notification completed methods
|
---|
1289 | * uses for EMT0 when it is waiting inside VMR3AsyncPdmNotificationWaitU().
|
---|
1290 | *
|
---|
1291 | * @param pUVM Pointer to the user mode VM structure.
|
---|
1292 | */
|
---|
1293 | VMMR3_INT_DECL(void) VMR3AsyncPdmNotificationWakeupU(PUVM pUVM)
|
---|
1294 | {
|
---|
1295 | LogFlow(("VMR3AsyncPdmNotificationWakeupU:\n"));
|
---|
1296 | VM_FF_SET(pUVM->pVM, VM_FF_REQUEST); /* this will have to do for now. */
|
---|
1297 | g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnNotifyCpuFF(&pUVM->aCpus[0], 0 /*fFlags*/);
|
---|
1298 | }
|
---|
1299 |
|
---|
1300 |
|
---|
1301 | /**
|
---|
1302 | * Rendezvous callback that will be called once.
|
---|
1303 | *
|
---|
1304 | * @returns VBox strict status code.
|
---|
1305 | * @param pVM The cross context VM structure.
|
---|
1306 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
1307 | * @param pvUser The new g_aHaltMethods index.
|
---|
1308 | */
|
---|
1309 | static DECLCALLBACK(VBOXSTRICTRC) vmR3SetHaltMethodCallback(PVM pVM, PVMCPU pVCpu, void *pvUser)
|
---|
1310 | {
|
---|
1311 | PUVM pUVM = pVM->pUVM;
|
---|
1312 | int rc = VINF_SUCCESS;
|
---|
1313 | uintptr_t i = (uintptr_t)pvUser;
|
---|
1314 | Assert(i < RT_ELEMENTS(g_aHaltMethods));
|
---|
1315 |
|
---|
1316 | /*
|
---|
1317 | * Main job is done once on EMT0 (it goes thru here first).
|
---|
1318 | */
|
---|
1319 | if (pVCpu->idCpu == 0)
|
---|
1320 | {
|
---|
1321 | /*
|
---|
1322 | * Terminate the old one.
|
---|
1323 | */
|
---|
1324 | if ( pUVM->vm.s.enmHaltMethod != VMHALTMETHOD_INVALID
|
---|
1325 | && g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnTerm)
|
---|
1326 | {
|
---|
1327 | g_aHaltMethods[pUVM->vm.s.iHaltMethod].pfnTerm(pUVM);
|
---|
1328 | pUVM->vm.s.enmHaltMethod = VMHALTMETHOD_INVALID;
|
---|
1329 | }
|
---|
1330 |
|
---|
1331 | /* Assert that the failure fallback is where we expect. */
|
---|
1332 | Assert(g_aHaltMethods[0].enmHaltMethod == VMHALTMETHOD_BOOTSTRAP);
|
---|
1333 | Assert(!g_aHaltMethods[0].pfnTerm && !g_aHaltMethods[0].pfnInit);
|
---|
1334 |
|
---|
1335 | /*
|
---|
1336 | * Init the new one.
|
---|
1337 | */
|
---|
1338 | memset(&pUVM->vm.s.Halt, 0, sizeof(pUVM->vm.s.Halt));
|
---|
1339 | if (g_aHaltMethods[i].pfnInit)
|
---|
1340 | {
|
---|
1341 | rc = g_aHaltMethods[i].pfnInit(pUVM);
|
---|
1342 | if (RT_FAILURE(rc))
|
---|
1343 | {
|
---|
1344 | /* Fall back on the bootstrap method. This requires no
|
---|
1345 | init/term (see assertion above), and will always work. */
|
---|
1346 | AssertLogRelRC(rc);
|
---|
1347 | i = 0;
|
---|
1348 | }
|
---|
1349 | }
|
---|
1350 |
|
---|
1351 | /*
|
---|
1352 | * Commit it.
|
---|
1353 | */
|
---|
1354 | pUVM->vm.s.enmHaltMethod = g_aHaltMethods[i].enmHaltMethod;
|
---|
1355 | ASMAtomicWriteU32(&pUVM->vm.s.iHaltMethod, i);
|
---|
1356 | }
|
---|
1357 | else
|
---|
1358 | i = pUVM->vm.s.iHaltMethod;
|
---|
1359 |
|
---|
1360 | /*
|
---|
1361 | * All EMTs must update their ring-0 halt configuration.
|
---|
1362 | */
|
---|
1363 | VMMR3SetMayHaltInRing0(pVCpu, g_aHaltMethods[i].fMayHaltInRing0,
|
---|
1364 | g_aHaltMethods[i].enmHaltMethod == VMHALTMETHOD_GLOBAL_1
|
---|
1365 | ? pUVM->vm.s.Halt.Global1.cNsSpinBlockThresholdCfg : 0);
|
---|
1366 |
|
---|
1367 | return rc;
|
---|
1368 | }
|
---|
1369 |
|
---|
1370 |
|
---|
1371 | /**
|
---|
1372 | * Changes the halt method.
|
---|
1373 | *
|
---|
1374 | * @returns VBox status code.
|
---|
1375 | * @param pUVM Pointer to the user mode VM structure.
|
---|
1376 | * @param enmHaltMethod The new halt method.
|
---|
1377 | * @thread EMT.
|
---|
1378 | */
|
---|
1379 | int vmR3SetHaltMethodU(PUVM pUVM, VMHALTMETHOD enmHaltMethod)
|
---|
1380 | {
|
---|
1381 | PVM pVM = pUVM->pVM; Assert(pVM);
|
---|
1382 | VM_ASSERT_EMT(pVM);
|
---|
1383 | AssertReturn(enmHaltMethod > VMHALTMETHOD_INVALID && enmHaltMethod < VMHALTMETHOD_END, VERR_INVALID_PARAMETER);
|
---|
1384 |
|
---|
1385 | /*
|
---|
1386 | * Resolve default (can be overridden in the configuration).
|
---|
1387 | */
|
---|
1388 | if (enmHaltMethod == VMHALTMETHOD_DEFAULT)
|
---|
1389 | {
|
---|
1390 | uint32_t u32;
|
---|
1391 | int rc = CFGMR3QueryU32(CFGMR3GetChild(CFGMR3GetRoot(pVM), "VM"), "HaltMethod", &u32);
|
---|
1392 | if (RT_SUCCESS(rc))
|
---|
1393 | {
|
---|
1394 | enmHaltMethod = (VMHALTMETHOD)u32;
|
---|
1395 | if (enmHaltMethod <= VMHALTMETHOD_INVALID || enmHaltMethod >= VMHALTMETHOD_END)
|
---|
1396 | return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, N_("Invalid VM/HaltMethod value %d"), enmHaltMethod);
|
---|
1397 | }
|
---|
1398 | else if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_CHILD_NOT_FOUND)
|
---|
1399 | return VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to Query VM/HaltMethod as uint32_t"));
|
---|
1400 | else
|
---|
1401 | enmHaltMethod = VMHALTMETHOD_GLOBAL_1;
|
---|
1402 | //enmHaltMethod = VMHALTMETHOD_1;
|
---|
1403 | //enmHaltMethod = VMHALTMETHOD_OLD;
|
---|
1404 | }
|
---|
1405 |
|
---|
1406 | /*
|
---|
1407 | * The global halt method doesn't work in driverless mode, so fall back on
|
---|
1408 | * method #1 instead.
|
---|
1409 | */
|
---|
1410 | if (!SUPR3IsDriverless() || enmHaltMethod != VMHALTMETHOD_GLOBAL_1)
|
---|
1411 | LogRel(("VMEmt: Halt method %s (%d)\n", vmR3GetHaltMethodName(enmHaltMethod), enmHaltMethod));
|
---|
1412 | else
|
---|
1413 | {
|
---|
1414 | LogRel(("VMEmt: Halt method %s (%d) not available in driverless mode, using %s (%d) instead\n",
|
---|
1415 | vmR3GetHaltMethodName(enmHaltMethod), enmHaltMethod, vmR3GetHaltMethodName(VMHALTMETHOD_1), VMHALTMETHOD_1));
|
---|
1416 | enmHaltMethod = VMHALTMETHOD_1;
|
---|
1417 | }
|
---|
1418 |
|
---|
1419 |
|
---|
1420 | /*
|
---|
1421 | * Find the descriptor.
|
---|
1422 | */
|
---|
1423 | unsigned i = 0;
|
---|
1424 | while ( i < RT_ELEMENTS(g_aHaltMethods)
|
---|
1425 | && g_aHaltMethods[i].enmHaltMethod != enmHaltMethod)
|
---|
1426 | i++;
|
---|
1427 | AssertReturn(i < RT_ELEMENTS(g_aHaltMethods), VERR_INVALID_PARAMETER);
|
---|
1428 |
|
---|
1429 | /*
|
---|
1430 | * This needs to be done while the other EMTs are not sleeping or otherwise messing around.
|
---|
1431 | */
|
---|
1432 | return VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING, vmR3SetHaltMethodCallback, (void *)(uintptr_t)i);
|
---|
1433 | }
|
---|
1434 |
|
---|
1435 |
|
---|
1436 | /**
|
---|
1437 | * Special interface for implementing a HLT-like port on a device.
|
---|
1438 | *
|
---|
1439 | * This can be called directly from device code, provide the device is trusted
|
---|
1440 | * to access the VMM directly. Since we may not have an accurate register set
|
---|
1441 | * and the caller certainly shouldn't (device code does not access CPU
|
---|
1442 | * registers), this function will return when interrupts are pending regardless
|
---|
1443 | * of the actual EFLAGS.IF state.
|
---|
1444 | *
|
---|
1445 | * @returns VBox error status (never informational statuses).
|
---|
1446 | * @param pVM The cross context VM structure.
|
---|
1447 | * @param idCpu The id of the calling EMT.
|
---|
1448 | */
|
---|
1449 | VMMR3DECL(int) VMR3WaitForDeviceReady(PVM pVM, VMCPUID idCpu)
|
---|
1450 | {
|
---|
1451 | /*
|
---|
1452 | * Validate caller and resolve the CPU ID.
|
---|
1453 | */
|
---|
1454 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
1455 | AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
|
---|
1456 | PVMCPU pVCpu = pVM->apCpusR3[idCpu];
|
---|
1457 | VMCPU_ASSERT_EMT_RETURN(pVCpu, VERR_VM_THREAD_NOT_EMT);
|
---|
1458 |
|
---|
1459 | /*
|
---|
1460 | * Tag along with the HLT mechanics for now.
|
---|
1461 | */
|
---|
1462 | int rc = VMR3WaitHalted(pVM, pVCpu, false /*fIgnoreInterrupts*/);
|
---|
1463 | if (RT_SUCCESS(rc))
|
---|
1464 | return VINF_SUCCESS;
|
---|
1465 | return rc;
|
---|
1466 | }
|
---|
1467 |
|
---|
1468 |
|
---|
1469 | /**
|
---|
1470 | * Wakes up a CPU that has called VMR3WaitForDeviceReady.
|
---|
1471 | *
|
---|
1472 | * @returns VBox error status (never informational statuses).
|
---|
1473 | * @param pVM The cross context VM structure.
|
---|
1474 | * @param idCpu The id of the calling EMT.
|
---|
1475 | */
|
---|
1476 | VMMR3DECL(int) VMR3NotifyCpuDeviceReady(PVM pVM, VMCPUID idCpu)
|
---|
1477 | {
|
---|
1478 | /*
|
---|
1479 | * Validate caller and resolve the CPU ID.
|
---|
1480 | */
|
---|
1481 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
1482 | AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
|
---|
1483 | PVMCPU pVCpu = pVM->apCpusR3[idCpu];
|
---|
1484 |
|
---|
1485 | /*
|
---|
1486 | * Pretend it was an FF that got set since we've got logic for that already.
|
---|
1487 | */
|
---|
1488 | VMR3NotifyCpuFFU(pVCpu->pUVCpu, VMNOTIFYFF_FLAGS_DONE_REM);
|
---|
1489 | return VINF_SUCCESS;
|
---|
1490 | }
|
---|
1491 |
|
---|
1492 |
|
---|
1493 | /**
|
---|
1494 | * Returns the number of active EMTs.
|
---|
1495 | *
|
---|
1496 | * This is used by the rendezvous code during VM destruction to avoid waiting
|
---|
1497 | * for EMTs that aren't around any more.
|
---|
1498 | *
|
---|
1499 | * @returns Number of active EMTs. 0 if invalid parameter.
|
---|
1500 | * @param pUVM The user mode VM structure.
|
---|
1501 | */
|
---|
1502 | VMMR3_INT_DECL(uint32_t) VMR3GetActiveEmts(PUVM pUVM)
|
---|
1503 | {
|
---|
1504 | UVM_ASSERT_VALID_EXT_RETURN(pUVM, 0);
|
---|
1505 | return pUVM->vm.s.cActiveEmts;
|
---|
1506 | }
|
---|
1507 |
|
---|