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