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