1 | /* $Id: VMEmt.cpp 23 2007-01-15 14:08:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VM - Virtual Machine, The Emulation Thread.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
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/pdm.h>
|
---|
31 | #include <VBox/rem.h>
|
---|
32 | #include "VMInternal.h"
|
---|
33 | #include <VBox/vm.h>
|
---|
34 |
|
---|
35 | #include <VBox/err.h>
|
---|
36 | #include <VBox/log.h>
|
---|
37 | #include <iprt/assert.h>
|
---|
38 | #include <iprt/asm.h>
|
---|
39 | #include <iprt/semaphore.h>
|
---|
40 | #include <iprt/thread.h>
|
---|
41 |
|
---|
42 |
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * The emulation thread.
|
---|
46 | *
|
---|
47 | * @returns Thread exit code.
|
---|
48 | * @param ThreadSelf The handle to the executing thread.
|
---|
49 | * @param pvArgs Pointer to a VMEMULATIONTHREADARGS structure.
|
---|
50 | */
|
---|
51 | DECLCALLBACK(int) vmR3EmulationThread(RTTHREAD ThreadSelf, void *pvArgs)
|
---|
52 | {
|
---|
53 | PVMEMULATIONTHREADARGS pArgs = (PVMEMULATIONTHREADARGS)pvArgs;
|
---|
54 | AssertReleaseMsg(pArgs && pArgs->pVM, ("Invalid arguments to the emulation thread!\n"));
|
---|
55 |
|
---|
56 | /*
|
---|
57 | * Init the native thread member.
|
---|
58 | */
|
---|
59 | PVM pVM = pArgs->pVM;
|
---|
60 | pVM->NativeThreadEMT = RTThreadGetNative(ThreadSelf);
|
---|
61 |
|
---|
62 | /*
|
---|
63 | * The request loop.
|
---|
64 | */
|
---|
65 | VMSTATE enmBefore;
|
---|
66 | int rc;
|
---|
67 | Log(("vmR3EmulationThread: Emulation thread starting the days work... Thread=%#x pVM=%p\n", ThreadSelf, pVM));
|
---|
68 | for (;;)
|
---|
69 | {
|
---|
70 |
|
---|
71 | /*
|
---|
72 | * Pending requests which needs servicing?
|
---|
73 | *
|
---|
74 | * We check for state changes in addition to status codes when
|
---|
75 | * servicing requests. (Look after the ifs.)
|
---|
76 | */
|
---|
77 | enmBefore = pVM->enmVMState;
|
---|
78 | if (VM_FF_ISSET(pVM, VM_FF_TERMINATE))
|
---|
79 | {
|
---|
80 | rc = VINF_EM_TERMINATE;
|
---|
81 | break;
|
---|
82 | }
|
---|
83 | else if (pVM->vm.s.pReqs)
|
---|
84 | {
|
---|
85 | /*
|
---|
86 | * Service execute in EMT request.
|
---|
87 | */
|
---|
88 | rc = VMR3ReqProcess(pVM);
|
---|
89 | Log(("vmR3EmulationThread: Req rc=%Vrc, VM state %d -> %d\n", rc, enmBefore, pVM->enmVMState));
|
---|
90 | }
|
---|
91 | else if (VM_FF_ISSET(pVM, VM_FF_DBGF))
|
---|
92 | {
|
---|
93 | /*
|
---|
94 | * Service the debugger request.
|
---|
95 | */
|
---|
96 | rc = DBGFR3VMMForcedAction(pVM);
|
---|
97 | Log(("vmR3EmulationThread: Dbg rc=%Vrc, VM state %d -> %d\n", rc, enmBefore, pVM->enmVMState));
|
---|
98 | }
|
---|
99 | else if (VM_FF_ISSET(pVM, VM_FF_RESET))
|
---|
100 | {
|
---|
101 | /*
|
---|
102 | * Service a delay reset request.
|
---|
103 | */
|
---|
104 | rc = VMR3Reset(pVM);
|
---|
105 | VM_FF_CLEAR(pVM, VM_FF_RESET);
|
---|
106 | Log(("vmR3EmulationThread: Reset rc=%Vrc, VM state %d -> %d\n", rc, enmBefore, pVM->enmVMState));
|
---|
107 | }
|
---|
108 | else
|
---|
109 | {
|
---|
110 | /*
|
---|
111 | * Nothing important is pending, so wait for something.
|
---|
112 | */
|
---|
113 | rc = VMR3Wait(pVM);
|
---|
114 | if (VBOX_FAILURE(rc))
|
---|
115 | break;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /*
|
---|
119 | * Check for termination requests, these are extremely high priority.
|
---|
120 | */
|
---|
121 | if ( rc == VINF_EM_TERMINATE
|
---|
122 | || VM_FF_ISSET(pVM, VM_FF_TERMINATE))
|
---|
123 | break;
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * Some requests (both VMR3Req* and the DBGF) can potentially
|
---|
127 | * resume or start the VM, in that case we'll get a change in
|
---|
128 | * VM status indicating that we're no running.
|
---|
129 | */
|
---|
130 | if ( VBOX_SUCCESS(rc)
|
---|
131 | && enmBefore != pVM->enmVMState
|
---|
132 | && (pVM->enmVMState == VMSTATE_RUNNING))
|
---|
133 | {
|
---|
134 | rc = EMR3ExecuteVM(pVM);
|
---|
135 | Log(("vmR3EmulationThread: EMR3ExecuteVM() -> rc=%Vrc, enmVMState=%d\n", rc, pVM->enmVMState));
|
---|
136 | }
|
---|
137 |
|
---|
138 | } /* forever */
|
---|
139 |
|
---|
140 |
|
---|
141 | /*
|
---|
142 | * Exitting.
|
---|
143 | */
|
---|
144 | Log(("vmR3EmulationThread: Terminating emulation thread! Thread=%#x pVM=%p rc=%Vrc enmBefore=%d enmVMState=%d\n",
|
---|
145 | ThreadSelf, pVM, rc, enmBefore, pVM->enmVMState));
|
---|
146 | if (pVM->vm.s.fEMTDoesTheCleanup)
|
---|
147 | {
|
---|
148 | Log(("vmR3EmulationThread: executing delayed Destroy\n"));
|
---|
149 | vmR3Destroy(pVM);
|
---|
150 | vmR3DestroyFinalBit(pVM);
|
---|
151 | Log(("vmR3EmulationThread: EMT is terminated.\n"));
|
---|
152 | }
|
---|
153 | else
|
---|
154 | {
|
---|
155 | /* we don't reset ThreadEMT here because it's used in waiting. */
|
---|
156 | pVM->NativeThreadEMT = NIL_RTNATIVETHREAD;
|
---|
157 | }
|
---|
158 | return rc;
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Notify the emulation thread (EMT) about pending Forced Action (FF).
|
---|
164 | *
|
---|
165 | * This function is called by thread other than EMT to make
|
---|
166 | * sure EMT wakes up and promptly service an FF request.
|
---|
167 | *
|
---|
168 | * @param pVM VM handle.
|
---|
169 | * @param fNotifiedREM Set if REM have already been notified. If clear the
|
---|
170 | * generic REMR3NotifyFF() method is called.
|
---|
171 | */
|
---|
172 | VMR3DECL(void) VMR3NotifyFF(PVM pVM, bool fNotifiedREM)
|
---|
173 | {
|
---|
174 | LogFlow(("VMR3NotifyFF:\n"));
|
---|
175 | if (pVM->vm.s.fWait)
|
---|
176 | {
|
---|
177 | int rc = RTSemEventSignal(pVM->vm.s.EventSemWait);
|
---|
178 | AssertRC(rc);
|
---|
179 | }
|
---|
180 | else if (!fNotifiedREM)
|
---|
181 | REMR3NotifyFF(pVM);
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Halted VM Wait.
|
---|
187 | * Any external event will unblock the thread.
|
---|
188 | *
|
---|
189 | * @returns VINF_SUCCESS unless a fatal error occured. In the latter
|
---|
190 | * case an appropriate status code is returned.
|
---|
191 | * @param pVM VM handle.
|
---|
192 | * @param fIgnoreInterrupts If set the VM_FF_INTERRUPT flags is ignored.
|
---|
193 | * @thread The emulation thread.
|
---|
194 | */
|
---|
195 | VMR3DECL(int) VMR3WaitHalted(PVM pVM, bool fIgnoreInterrupts)
|
---|
196 | {
|
---|
197 | LogFlow(("VMR3WaitHalted: fIgnoreInterrupts=%d\n", fIgnoreInterrupts));
|
---|
198 |
|
---|
199 | /*
|
---|
200 | * Check Relevant FFs.
|
---|
201 | */
|
---|
202 | const uint32_t fMask = !fIgnoreInterrupts
|
---|
203 | ? VM_FF_EXTERNAL_HALTED_MASK
|
---|
204 | : VM_FF_EXTERNAL_HALTED_MASK & ~(VM_FF_INTERRUPT_APIC | VM_FF_INTERRUPT_PIC);
|
---|
205 | if (VM_FF_ISPENDING(pVM, fMask))
|
---|
206 | {
|
---|
207 | LogFlow(("VMR3WaitHalted: returns VINF_SUCCESS (FF %#x)\n", pVM->fForcedActions));
|
---|
208 | return VINF_SUCCESS;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /*
|
---|
212 | * The CPU TSC is running while halted,
|
---|
213 | * and the yielder is suspended.
|
---|
214 | */
|
---|
215 | TMCpuTickResume(pVM);
|
---|
216 | VMMR3YieldSuspend(pVM);
|
---|
217 |
|
---|
218 | /*
|
---|
219 | * Halt loop.
|
---|
220 | */
|
---|
221 | int rc = VINF_SUCCESS;
|
---|
222 | ASMAtomicXchgU32(&pVM->vm.s.fWait, 1);
|
---|
223 | //unsigned cLoops = 0;
|
---|
224 | for (;;)
|
---|
225 | {
|
---|
226 | #ifdef VBOX_HIGH_RES_TIMERS_HACK
|
---|
227 | /*
|
---|
228 | * Work the timers and check if we can exit.
|
---|
229 | * The poll call gives us the ticks left to the next event in
|
---|
230 | * addition to perhaps set an FF.
|
---|
231 | */
|
---|
232 | STAM_PROFILE_ADV_START(&pVM->vm.s.StatHaltPoll, a);
|
---|
233 | PDMR3Poll(pVM);
|
---|
234 | STAM_PROFILE_ADV_STOP(&pVM->vm.s.StatHaltPoll, a);
|
---|
235 | STAM_PROFILE_ADV_START(&pVM->vm.s.StatHaltTimers, b);
|
---|
236 | TMR3TimerQueuesDo(pVM);
|
---|
237 | STAM_PROFILE_ADV_STOP(&pVM->vm.s.StatHaltTimers, b);
|
---|
238 | if (VM_FF_ISPENDING(pVM, fMask))
|
---|
239 | break;
|
---|
240 | uint64_t u64NanoTS = TMVirtualToNano(pVM, TMTimerPoll(pVM));
|
---|
241 | if (VM_FF_ISPENDING(pVM, fMask))
|
---|
242 | break;
|
---|
243 |
|
---|
244 | /*
|
---|
245 | * Wait for a while. Someone will wake us up or interrupt the call if
|
---|
246 | * anything needs our attention.
|
---|
247 | */
|
---|
248 | if (u64NanoTS < 50000)
|
---|
249 | {
|
---|
250 | //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d spin\n", u64NanoTS, cLoops++);
|
---|
251 | /* spin */;
|
---|
252 | }
|
---|
253 | else
|
---|
254 | {
|
---|
255 | VMMR3YieldStop(pVM);
|
---|
256 | if (u64NanoTS < 870000) /* this is a bit speculative... works fine on linux. */
|
---|
257 | {
|
---|
258 | //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d yield\n", u64NanoTS, cLoops++);
|
---|
259 | STAM_PROFILE_ADV_START(&pVM->vm.s.StatHaltYield, a);
|
---|
260 | RTThreadYield(); /* this is the best we can do here */
|
---|
261 | STAM_PROFILE_ADV_STOP(&pVM->vm.s.StatHaltYield, a);
|
---|
262 | }
|
---|
263 | else if (u64NanoTS < 2000000)
|
---|
264 | {
|
---|
265 | //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d sleep 1ms\n", u64NanoTS, cLoops++);
|
---|
266 | STAM_PROFILE_ADV_START(&pVM->vm.s.StatHaltBlock, a);
|
---|
267 | rc = RTSemEventWait(pVM->vm.s.EventSemWait, 1);
|
---|
268 | STAM_PROFILE_ADV_STOP(&pVM->vm.s.StatHaltBlock, a);
|
---|
269 | }
|
---|
270 | else
|
---|
271 | {
|
---|
272 | //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d sleep %dms\n", u64NanoTS, cLoops++, (uint32_t)RT_MIN(u64NanoTS / 1000000, 15));
|
---|
273 | STAM_PROFILE_ADV_START(&pVM->vm.s.StatHaltBlock, a);
|
---|
274 | rc = RTSemEventWait(pVM->vm.s.EventSemWait, RT_MIN(u64NanoTS / 1000000, 15));
|
---|
275 | STAM_PROFILE_ADV_STOP(&pVM->vm.s.StatHaltBlock, a);
|
---|
276 | }
|
---|
277 | }
|
---|
278 | #else
|
---|
279 |
|
---|
280 | /*
|
---|
281 | * We have to check if we can exit, run timers, and then recheck.
|
---|
282 | */
|
---|
283 | /** @todo
|
---|
284 | * The other thing we have to check is how long it is till the next timer
|
---|
285 | * can be serviced and not wait any longer than that.
|
---|
286 | */
|
---|
287 | if (VM_FF_ISPENDING(pVM, fMask))
|
---|
288 | break;
|
---|
289 | STAM_PROFILE_ADV_START(&pVM->vm.s.StatHaltTimers, b);
|
---|
290 | TMR3TimerQueuesDo(pVM);
|
---|
291 | STAM_PROFILE_ADV_STOP(&pVM->vm.s.StatHaltTimers, b);
|
---|
292 | if (VM_FF_ISPENDING(pVM, fMask))
|
---|
293 | break;
|
---|
294 | /* hacking */
|
---|
295 | RTThreadYield();
|
---|
296 | TMR3TimerQueuesDo(pVM);
|
---|
297 | if (VM_FF_ISPENDING(pVM, fMask))
|
---|
298 | break;
|
---|
299 |
|
---|
300 | /*
|
---|
301 | * Wait for a while. Someone will wake us up or interrupt the call if
|
---|
302 | * anything needs our attention.
|
---|
303 | */
|
---|
304 | STAM_PROFILE_ADV_START(&pVM->vm.s.StatHaltBlock, a);
|
---|
305 | rc = RTSemEventWait(pVM->vm.s.EventSemWait, 10);
|
---|
306 | STAM_PROFILE_ADV_STOP(&pVM->vm.s.StatHaltBlock, a);
|
---|
307 | #endif
|
---|
308 | if (rc == VERR_TIMEOUT)
|
---|
309 | rc = VINF_SUCCESS;
|
---|
310 | else if (VBOX_FAILURE(rc))
|
---|
311 | {
|
---|
312 | AssertRC(rc != VERR_INTERRUPTED);
|
---|
313 | AssertMsgFailed(("RTSemEventWait->%Vrc\n", rc));
|
---|
314 | VM_FF_SET(pVM, VM_FF_TERMINATE);
|
---|
315 | rc = VERR_INTERNAL_ERROR;
|
---|
316 | break;
|
---|
317 | }
|
---|
318 | }
|
---|
319 | ASMAtomicXchgU32(&pVM->vm.s.fWait, 0);
|
---|
320 |
|
---|
321 | /*
|
---|
322 | *
|
---|
323 | * Pause the TSC, it's restarted when we start executing,
|
---|
324 | * and resume the yielder.
|
---|
325 | */
|
---|
326 | TMCpuTickPause(pVM);
|
---|
327 | VMMR3YieldResume(pVM);
|
---|
328 |
|
---|
329 |
|
---|
330 | LogFlow(("VMR3WaitHalted: returns %Vrc (FF %#x)\n", rc, pVM->fForcedActions ));
|
---|
331 | return rc;
|
---|
332 | }
|
---|
333 |
|
---|
334 |
|
---|
335 | /**
|
---|
336 | * Suspended VM Wait.
|
---|
337 | * Only a handful of forced actions will cause the function to
|
---|
338 | * return to the caller.
|
---|
339 | *
|
---|
340 | * @returns VINF_SUCCESS unless a fatal error occured. In the latter
|
---|
341 | * case an appropriate status code is returned.
|
---|
342 | * @param pVM VM handle.
|
---|
343 | * @thread The emulation thread.
|
---|
344 | */
|
---|
345 | VMR3DECL(int) VMR3Wait(PVM pVM)
|
---|
346 | {
|
---|
347 | LogFlow(("VMR3Wait:\n"));
|
---|
348 |
|
---|
349 | /*
|
---|
350 | * Check Relevant FFs.
|
---|
351 | */
|
---|
352 | if (VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_SUSPENDED_MASK))
|
---|
353 | {
|
---|
354 | LogFlow(("VMR3Wait: returns VINF_SUCCESS (FF %#x)\n", pVM->fForcedActions));
|
---|
355 | return VINF_SUCCESS;
|
---|
356 | }
|
---|
357 |
|
---|
358 | int rc = VINF_SUCCESS;
|
---|
359 | ASMAtomicXchgU32(&pVM->vm.s.fWait, 1);
|
---|
360 | for (;;)
|
---|
361 | {
|
---|
362 | /*
|
---|
363 | * Check Relevant FFs.
|
---|
364 | */
|
---|
365 | if (VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_SUSPENDED_MASK))
|
---|
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 | rc = RTSemEventWait(pVM->vm.s.EventSemWait, 1000);
|
---|
373 | if (rc == VERR_TIMEOUT)
|
---|
374 | rc = VINF_SUCCESS;
|
---|
375 | else if (VBOX_FAILURE(rc))
|
---|
376 | {
|
---|
377 | AssertMsgFailed(("RTSemEventWait->%Vrc\n", rc));
|
---|
378 | VM_FF_SET(pVM, VM_FF_TERMINATE);
|
---|
379 | rc = VERR_INTERNAL_ERROR;
|
---|
380 | break;
|
---|
381 | }
|
---|
382 |
|
---|
383 | }
|
---|
384 | ASMAtomicXchgU32(&pVM->vm.s.fWait, 0);
|
---|
385 |
|
---|
386 | LogFlow(("VMR3Wait: returns %Vrc (FF %#x)\n", rc, pVM->fForcedActions));
|
---|
387 | return rc;
|
---|
388 | }
|
---|
389 |
|
---|