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