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