VirtualBox

source: vbox/trunk/src/VBox/VMM/VMEmt.cpp@ 2273

Last change on this file since 2273 was 1633, checked in by vboxsync, 18 years ago

warning.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 14.8 KB
Line 
1/* $Id: VMEmt.cpp 1633 2007-03-22 17:14:53Z 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 */
51DECLCALLBACK(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 /* Requested to exit the EMT thread out of sync? (currently only VMR3WaitForResume) */
71 if (setjmp(pVM->vm.s.emtJumpEnv) != 0)
72 {
73 rc = VINF_SUCCESS;
74 break;
75 }
76
77 /*
78 * Pending requests which needs servicing?
79 *
80 * We check for state changes in addition to status codes when
81 * servicing requests. (Look after the ifs.)
82 */
83 enmBefore = pVM->enmVMState;
84 if (VM_FF_ISSET(pVM, VM_FF_TERMINATE))
85 {
86 rc = VINF_EM_TERMINATE;
87 break;
88 }
89 else if (pVM->vm.s.pReqs)
90 {
91 /*
92 * Service execute in EMT request.
93 */
94 rc = VMR3ReqProcess(pVM);
95 Log(("vmR3EmulationThread: Req rc=%Vrc, VM state %d -> %d\n", rc, enmBefore, pVM->enmVMState));
96 }
97 else if (VM_FF_ISSET(pVM, VM_FF_DBGF))
98 {
99 /*
100 * Service the debugger request.
101 */
102 rc = DBGFR3VMMForcedAction(pVM);
103 Log(("vmR3EmulationThread: Dbg rc=%Vrc, VM state %d -> %d\n", rc, enmBefore, pVM->enmVMState));
104 }
105 else if (VM_FF_ISSET(pVM, VM_FF_RESET))
106 {
107 /*
108 * Service a delay reset request.
109 */
110 rc = VMR3Reset(pVM);
111 VM_FF_CLEAR(pVM, VM_FF_RESET);
112 Log(("vmR3EmulationThread: Reset rc=%Vrc, VM state %d -> %d\n", rc, enmBefore, pVM->enmVMState));
113 }
114 else
115 {
116 /*
117 * Nothing important is pending, so wait for something.
118 */
119 rc = VMR3Wait(pVM);
120 if (VBOX_FAILURE(rc))
121 break;
122 }
123
124 /*
125 * Check for termination requests, these are extremely high priority.
126 */
127 if ( rc == VINF_EM_TERMINATE
128 || VM_FF_ISSET(pVM, VM_FF_TERMINATE))
129 break;
130
131 /*
132 * Some requests (both VMR3Req* and the DBGF) can potentially
133 * resume or start the VM, in that case we'll get a change in
134 * VM status indicating that we're now running.
135 */
136 if ( VBOX_SUCCESS(rc)
137 && enmBefore != pVM->enmVMState
138 && (pVM->enmVMState == VMSTATE_RUNNING))
139 {
140 rc = EMR3ExecuteVM(pVM);
141 Log(("vmR3EmulationThread: EMR3ExecuteVM() -> rc=%Vrc, enmVMState=%d\n", rc, pVM->enmVMState));
142 }
143
144 } /* forever */
145
146
147 /*
148 * Exiting.
149 */
150 Log(("vmR3EmulationThread: Terminating emulation thread! Thread=%#x pVM=%p rc=%Vrc enmBefore=%d enmVMState=%d\n",
151 ThreadSelf, pVM, rc, enmBefore, pVM->enmVMState));
152 if (pVM->vm.s.fEMTDoesTheCleanup)
153 {
154 Log(("vmR3EmulationThread: executing delayed Destroy\n"));
155 vmR3Destroy(pVM);
156 vmR3DestroyFinalBit(pVM);
157 Log(("vmR3EmulationThread: EMT is terminated.\n"));
158 }
159 else
160 {
161 /* we don't reset ThreadEMT here because it's used in waiting. */
162 pVM->NativeThreadEMT = NIL_RTNATIVETHREAD;
163 }
164 return rc;
165}
166
167/**
168 * Wait for VM to be resumed. Handle events like vmR3EmulationThread does.
169 * In case the VM is stopped, clean up and long jump to the main EMT loop.
170 *
171 * @returns VINF_SUCCESS or doesn't return
172 * @param pVM VM handle.
173 */
174VMR3DECL(int) VMR3WaitForResume(PVM pVM)
175{
176 /*
177 * The request loop.
178 */
179 VMSTATE enmBefore;
180 int rc;
181 for (;;)
182 {
183
184 /*
185 * Pending requests which needs servicing?
186 *
187 * We check for state changes in addition to status codes when
188 * servicing requests. (Look after the ifs.)
189 */
190 enmBefore = pVM->enmVMState;
191 if (VM_FF_ISSET(pVM, VM_FF_TERMINATE))
192 {
193 rc = VINF_EM_TERMINATE;
194 break;
195 }
196 else if (pVM->vm.s.pReqs)
197 {
198 /*
199 * Service execute in EMT request.
200 */
201 rc = VMR3ReqProcess(pVM);
202 Log(("vmR3EmulationThread: Req rc=%Vrc, VM state %d -> %d\n", rc, enmBefore, pVM->enmVMState));
203 }
204 else if (VM_FF_ISSET(pVM, VM_FF_DBGF))
205 {
206 /*
207 * Service the debugger request.
208 */
209 rc = DBGFR3VMMForcedAction(pVM);
210 Log(("vmR3EmulationThread: Dbg rc=%Vrc, VM state %d -> %d\n", rc, enmBefore, pVM->enmVMState));
211 }
212 else if (VM_FF_ISSET(pVM, VM_FF_RESET))
213 {
214 /*
215 * Service a delay reset request.
216 */
217 rc = VMR3Reset(pVM);
218 VM_FF_CLEAR(pVM, VM_FF_RESET);
219 Log(("vmR3EmulationThread: Reset rc=%Vrc, VM state %d -> %d\n", rc, enmBefore, pVM->enmVMState));
220 }
221 else
222 {
223 /*
224 * Nothing important is pending, so wait for something.
225 */
226 rc = VMR3Wait(pVM);
227 if (VBOX_FAILURE(rc))
228 break;
229 }
230
231 /*
232 * Check for termination requests, these are extremely high priority.
233 */
234 if ( rc == VINF_EM_TERMINATE
235 || VM_FF_ISSET(pVM, VM_FF_TERMINATE))
236 break;
237
238 /*
239 * Some requests (both VMR3Req* and the DBGF) can potentially
240 * resume or start the VM, in that case we'll get a change in
241 * VM status indicating that we're now running.
242 */
243 if ( VBOX_SUCCESS(rc)
244 && enmBefore != pVM->enmVMState
245 && (pVM->enmVMState == VMSTATE_RUNNING))
246 {
247 /* Only valid exit reason. */
248 return VINF_SUCCESS;
249 }
250
251 } /* forever */
252
253 /* Return to the main loop in vmR3EmulationThread, which will clean up for us. */
254 longjmp(pVM->vm.s.emtJumpEnv, 1);
255}
256
257/**
258 * Notify the emulation thread (EMT) about pending Forced Action (FF).
259 *
260 * This function is called by thread other than EMT to make
261 * sure EMT wakes up and promptly service an FF request.
262 *
263 * @param pVM VM handle.
264 * @param fNotifiedREM Set if REM have already been notified. If clear the
265 * generic REMR3NotifyFF() method is called.
266 */
267VMR3DECL(void) VMR3NotifyFF(PVM pVM, bool fNotifiedREM)
268{
269 LogFlow(("VMR3NotifyFF:\n"));
270 if (pVM->vm.s.fWait)
271 {
272 int rc = RTSemEventSignal(pVM->vm.s.EventSemWait);
273 AssertRC(rc);
274 }
275 else if (!fNotifiedREM)
276 REMR3NotifyFF(pVM);
277}
278
279
280/**
281 * Halted VM Wait.
282 * Any external event will unblock the thread.
283 *
284 * @returns VINF_SUCCESS unless a fatal error occured. In the latter
285 * case an appropriate status code is returned.
286 * @param pVM VM handle.
287 * @param fIgnoreInterrupts If set the VM_FF_INTERRUPT flags is ignored.
288 * @thread The emulation thread.
289 */
290VMR3DECL(int) VMR3WaitHalted(PVM pVM, bool fIgnoreInterrupts)
291{
292 LogFlow(("VMR3WaitHalted: fIgnoreInterrupts=%d\n", fIgnoreInterrupts));
293
294 /*
295 * Check Relevant FFs.
296 */
297 const uint32_t fMask = !fIgnoreInterrupts
298 ? VM_FF_EXTERNAL_HALTED_MASK
299 : VM_FF_EXTERNAL_HALTED_MASK & ~(VM_FF_INTERRUPT_APIC | VM_FF_INTERRUPT_PIC);
300 if (VM_FF_ISPENDING(pVM, fMask))
301 {
302 LogFlow(("VMR3WaitHalted: returns VINF_SUCCESS (FF %#x)\n", pVM->fForcedActions));
303 return VINF_SUCCESS;
304 }
305
306 /*
307 * The CPU TSC is running while halted,
308 * and the yielder is suspended.
309 */
310// TMCpuTickResume(pVM);
311 VMMR3YieldSuspend(pVM);
312
313 /*
314 * Halt loop.
315 */
316 int rc = VINF_SUCCESS;
317 ASMAtomicXchgU32(&pVM->vm.s.fWait, 1);
318 //unsigned cLoops = 0;
319 for (;;)
320 {
321#ifdef VBOX_HIGH_RES_TIMERS_HACK
322 /*
323 * Work the timers and check if we can exit.
324 * The poll call gives us the ticks left to the next event in
325 * addition to perhaps set an FF.
326 */
327 STAM_PROFILE_ADV_START(&pVM->vm.s.StatHaltPoll, a);
328 PDMR3Poll(pVM);
329 STAM_PROFILE_ADV_STOP(&pVM->vm.s.StatHaltPoll, a);
330 STAM_PROFILE_ADV_START(&pVM->vm.s.StatHaltTimers, b);
331 TMR3TimerQueuesDo(pVM);
332 STAM_PROFILE_ADV_STOP(&pVM->vm.s.StatHaltTimers, b);
333 if (VM_FF_ISPENDING(pVM, fMask))
334 break;
335 uint64_t u64NanoTS = TMVirtualToNano(pVM, TMTimerPoll(pVM));
336 if (VM_FF_ISPENDING(pVM, fMask))
337 break;
338
339 /*
340 * Wait for a while. Someone will wake us up or interrupt the call if
341 * anything needs our attention.
342 */
343 if (u64NanoTS < 50000)
344 {
345 //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d spin\n", u64NanoTS, cLoops++);
346 /* spin */;
347 }
348 else
349 {
350 VMMR3YieldStop(pVM);
351 if (u64NanoTS < 870000) /* this is a bit speculative... works fine on linux. */
352 {
353 //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d yield\n", u64NanoTS, cLoops++);
354 STAM_PROFILE_ADV_START(&pVM->vm.s.StatHaltYield, a);
355 RTThreadYield(); /* this is the best we can do here */
356 STAM_PROFILE_ADV_STOP(&pVM->vm.s.StatHaltYield, a);
357 }
358 else if (u64NanoTS < 2000000)
359 {
360 //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d sleep 1ms\n", u64NanoTS, cLoops++);
361 STAM_PROFILE_ADV_START(&pVM->vm.s.StatHaltBlock, a);
362 rc = RTSemEventWait(pVM->vm.s.EventSemWait, 1);
363 STAM_PROFILE_ADV_STOP(&pVM->vm.s.StatHaltBlock, a);
364 }
365 else
366 {
367 //RTLogPrintf("u64NanoTS=%RI64 cLoops=%d sleep %dms\n", u64NanoTS, cLoops++, (uint32_t)RT_MIN(u64NanoTS / 1000000, 15));
368 STAM_PROFILE_ADV_START(&pVM->vm.s.StatHaltBlock, a);
369 rc = RTSemEventWait(pVM->vm.s.EventSemWait, RT_MIN(u64NanoTS / 1000000, 15));
370 STAM_PROFILE_ADV_STOP(&pVM->vm.s.StatHaltBlock, a);
371 }
372 }
373#else
374
375 /*
376 * We have to check if we can exit, run timers, and then recheck.
377 */
378 /** @todo
379 * The other thing we have to check is how long it is till the next timer
380 * can be serviced and not wait any longer than that.
381 */
382 if (VM_FF_ISPENDING(pVM, fMask))
383 break;
384 STAM_PROFILE_ADV_START(&pVM->vm.s.StatHaltTimers, b);
385 TMR3TimerQueuesDo(pVM);
386 STAM_PROFILE_ADV_STOP(&pVM->vm.s.StatHaltTimers, b);
387 if (VM_FF_ISPENDING(pVM, fMask))
388 break;
389 /* hacking */
390 RTThreadYield();
391 TMR3TimerQueuesDo(pVM);
392 if (VM_FF_ISPENDING(pVM, fMask))
393 break;
394
395 /*
396 * Wait for a while. Someone will wake us up or interrupt the call if
397 * anything needs our attention.
398 */
399 STAM_PROFILE_ADV_START(&pVM->vm.s.StatHaltBlock, a);
400 rc = RTSemEventWait(pVM->vm.s.EventSemWait, 10);
401 STAM_PROFILE_ADV_STOP(&pVM->vm.s.StatHaltBlock, a);
402#endif
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 VM_FF_SET(pVM, VM_FF_TERMINATE);
410 rc = VERR_INTERNAL_ERROR;
411 break;
412 }
413 }
414 ASMAtomicXchgU32(&pVM->vm.s.fWait, 0);
415
416 /*
417 *
418 * Pause the TSC, it's restarted when we start executing,
419 * and resume the yielder.
420 */
421// TMCpuTickPause(pVM);
422 VMMR3YieldResume(pVM);
423
424
425 LogFlow(("VMR3WaitHalted: returns %Vrc (FF %#x)\n", rc, pVM->fForcedActions ));
426 return rc;
427}
428
429
430/**
431 * Suspended VM Wait.
432 * Only a handful of forced actions will cause the function to
433 * return to the caller.
434 *
435 * @returns VINF_SUCCESS unless a fatal error occured. In the latter
436 * case an appropriate status code is returned.
437 * @param pVM VM handle.
438 * @thread The emulation thread.
439 */
440VMR3DECL(int) VMR3Wait(PVM pVM)
441{
442 LogFlow(("VMR3Wait:\n"));
443
444 /*
445 * Check Relevant FFs.
446 */
447 if (VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_SUSPENDED_MASK))
448 {
449 LogFlow(("VMR3Wait: returns VINF_SUCCESS (FF %#x)\n", pVM->fForcedActions));
450 return VINF_SUCCESS;
451 }
452
453 int rc = VINF_SUCCESS;
454 ASMAtomicXchgU32(&pVM->vm.s.fWait, 1);
455 for (;;)
456 {
457 /*
458 * Check Relevant FFs.
459 */
460 if (VM_FF_ISPENDING(pVM, VM_FF_EXTERNAL_SUSPENDED_MASK))
461 break;
462
463 /*
464 * Wait for a while. Someone will wake us up or interrupt the call if
465 * anything needs our attention.
466 */
467 rc = RTSemEventWait(pVM->vm.s.EventSemWait, 1000);
468 if (rc == VERR_TIMEOUT)
469 rc = VINF_SUCCESS;
470 else if (VBOX_FAILURE(rc))
471 {
472 AssertMsgFailed(("RTSemEventWait->%Vrc\n", rc));
473 VM_FF_SET(pVM, VM_FF_TERMINATE);
474 rc = VERR_INTERNAL_ERROR;
475 break;
476 }
477
478 }
479 ASMAtomicXchgU32(&pVM->vm.s.fWait, 0);
480
481 LogFlow(("VMR3Wait: returns %Vrc (FF %#x)\n", rc, pVM->fForcedActions));
482 return rc;
483}
484
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette