blob: 67c2974959891ff022f47307671afb46127a81cf [file] [log] [blame]
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#include "sysemu.h"
26#include "net.h"
27#include "monitor.h"
28#include "console.h"
29
30#include "hw/hw.h"
31
32#include <unistd.h>
33#include <fcntl.h>
34#include <time.h>
35#include <errno.h>
36#include <sys/time.h>
37#include <signal.h>
Juergen Lock44459342010-03-25 22:35:03 +010038#ifdef __FreeBSD__
39#include <sys/param.h>
40#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010041
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010042#ifdef _WIN32
43#include <windows.h>
44#include <mmsystem.h>
45#endif
46
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010047#include "qemu-timer.h"
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010048
49/* Conversion factor from emulated instructions to virtual clock ticks. */
Blue Swirl29e922b2010-03-29 19:24:00 +000050int icount_time_shift;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010051/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
52#define MAX_ICOUNT_SHIFT 10
53/* Compensate for varying guest execution speed. */
Blue Swirl29e922b2010-03-29 19:24:00 +000054int64_t qemu_icount_bias;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010055static QEMUTimer *icount_rt_timer;
56static QEMUTimer *icount_vm_timer;
57
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010058/***********************************************************/
59/* guest cycle counter */
60
61typedef struct TimersState {
62 int64_t cpu_ticks_prev;
63 int64_t cpu_ticks_offset;
64 int64_t cpu_clock_offset;
65 int32_t cpu_ticks_enabled;
66 int64_t dummy;
67} TimersState;
68
69TimersState timers_state;
70
71/* return the host CPU cycle counter and handle stop/restart */
72int64_t cpu_get_ticks(void)
73{
74 if (use_icount) {
75 return cpu_get_icount();
76 }
77 if (!timers_state.cpu_ticks_enabled) {
78 return timers_state.cpu_ticks_offset;
79 } else {
80 int64_t ticks;
81 ticks = cpu_get_real_ticks();
82 if (timers_state.cpu_ticks_prev > ticks) {
83 /* Note: non increasing ticks may happen if the host uses
84 software suspend */
85 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
86 }
87 timers_state.cpu_ticks_prev = ticks;
88 return ticks + timers_state.cpu_ticks_offset;
89 }
90}
91
92/* return the host CPU monotonic timer and handle stop/restart */
93static int64_t cpu_get_clock(void)
94{
95 int64_t ti;
96 if (!timers_state.cpu_ticks_enabled) {
97 return timers_state.cpu_clock_offset;
98 } else {
99 ti = get_clock();
100 return ti + timers_state.cpu_clock_offset;
101 }
102}
103
Paolo Bonzini1ece93a2011-04-13 10:03:45 +0200104#ifndef CONFIG_IOTHREAD
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100105static int64_t qemu_icount_delta(void)
106{
Paolo Bonzini1ece93a2011-04-13 10:03:45 +0200107 if (!use_icount) {
108 return 5000 * (int64_t) 1000000;
109 } else if (use_icount == 1) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100110 /* When not using an adaptive execution frequency
111 we tend to get badly out of sync with real time,
112 so just delay for a reasonable amount of time. */
113 return 0;
114 } else {
115 return cpu_get_icount() - cpu_get_clock();
116 }
117}
Paolo Bonzini1ece93a2011-04-13 10:03:45 +0200118#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100119
120/* enable cpu_get_ticks() */
121void cpu_enable_ticks(void)
122{
123 if (!timers_state.cpu_ticks_enabled) {
124 timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
125 timers_state.cpu_clock_offset -= get_clock();
126 timers_state.cpu_ticks_enabled = 1;
127 }
128}
129
130/* disable cpu_get_ticks() : the clock is stopped. You must not call
131 cpu_get_ticks() after that. */
132void cpu_disable_ticks(void)
133{
134 if (timers_state.cpu_ticks_enabled) {
135 timers_state.cpu_ticks_offset = cpu_get_ticks();
136 timers_state.cpu_clock_offset = cpu_get_clock();
137 timers_state.cpu_ticks_enabled = 0;
138 }
139}
140
141/***********************************************************/
142/* timers */
143
144#define QEMU_CLOCK_REALTIME 0
145#define QEMU_CLOCK_VIRTUAL 1
146#define QEMU_CLOCK_HOST 2
147
148struct QEMUClock {
149 int type;
150 int enabled;
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200151
152 QEMUTimer *warp_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100153};
154
155struct QEMUTimer {
156 QEMUClock *clock;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100157 int64_t expire_time; /* in nanoseconds */
158 int scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100159 QEMUTimerCB *cb;
160 void *opaque;
161 struct QEMUTimer *next;
162};
163
164struct qemu_alarm_timer {
165 char const *name;
166 int (*start)(struct qemu_alarm_timer *t);
167 void (*stop)(struct qemu_alarm_timer *t);
168 void (*rearm)(struct qemu_alarm_timer *t);
Stefan Weilcd0544e2011-04-10 20:15:09 +0200169#if defined(__linux__)
170 int fd;
171 timer_t timer;
172#elif defined(_WIN32)
173 HANDLE timer;
174#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100175 char expired;
176 char pending;
177};
178
179static struct qemu_alarm_timer *alarm_timer;
180
Stefan Weil45c7b372011-03-24 21:31:24 +0100181static bool qemu_timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
182{
183 return timer_head && (timer_head->expire_time <= current_time);
184}
185
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100186int qemu_alarm_pending(void)
187{
188 return alarm_timer->pending;
189}
190
191static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
192{
193 return !!t->rearm;
194}
195
196static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
197{
198 if (!alarm_has_dynticks(t))
199 return;
200
201 t->rearm(t);
202}
203
Paolo Bonzini9c132462011-02-03 14:48:59 +0100204/* TODO: MIN_TIMER_REARM_NS should be optimized */
205#define MIN_TIMER_REARM_NS 250000
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100206
207#ifdef _WIN32
208
Stefan Weil2f9cba02011-04-05 18:34:21 +0200209static int mm_start_timer(struct qemu_alarm_timer *t);
210static void mm_stop_timer(struct qemu_alarm_timer *t);
211static void mm_rearm_timer(struct qemu_alarm_timer *t);
212
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100213static int win32_start_timer(struct qemu_alarm_timer *t);
214static void win32_stop_timer(struct qemu_alarm_timer *t);
215static void win32_rearm_timer(struct qemu_alarm_timer *t);
216
217#else
218
219static int unix_start_timer(struct qemu_alarm_timer *t);
220static void unix_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzini84682832011-06-09 13:10:25 +0200221static void unix_rearm_timer(struct qemu_alarm_timer *t);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100222
223#ifdef __linux__
224
225static int dynticks_start_timer(struct qemu_alarm_timer *t);
226static void dynticks_stop_timer(struct qemu_alarm_timer *t);
227static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
228
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100229#endif /* __linux__ */
230
231#endif /* _WIN32 */
232
233/* Correlation between real and virtual time is always going to be
234 fairly approximate, so ignore small variation.
235 When the guest is idle real and virtual time will be aligned in
236 the IO wait loop. */
237#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
238
239static void icount_adjust(void)
240{
241 int64_t cur_time;
242 int64_t cur_icount;
243 int64_t delta;
244 static int64_t last_delta;
245 /* If the VM is not running, then do nothing. */
246 if (!vm_running)
247 return;
248
249 cur_time = cpu_get_clock();
Paolo Bonzini74475452011-03-11 16:47:48 +0100250 cur_icount = qemu_get_clock_ns(vm_clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100251 delta = cur_icount - cur_time;
252 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
253 if (delta > 0
254 && last_delta + ICOUNT_WOBBLE < delta * 2
255 && icount_time_shift > 0) {
256 /* The guest is getting too far ahead. Slow time down. */
257 icount_time_shift--;
258 }
259 if (delta < 0
260 && last_delta - ICOUNT_WOBBLE > delta * 2
261 && icount_time_shift < MAX_ICOUNT_SHIFT) {
262 /* The guest is getting too far behind. Speed time up. */
263 icount_time_shift++;
264 }
265 last_delta = delta;
266 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
267}
268
269static void icount_adjust_rt(void * opaque)
270{
271 qemu_mod_timer(icount_rt_timer,
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100272 qemu_get_clock_ms(rt_clock) + 1000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100273 icount_adjust();
274}
275
276static void icount_adjust_vm(void * opaque)
277{
278 qemu_mod_timer(icount_vm_timer,
Paolo Bonzini74475452011-03-11 16:47:48 +0100279 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100280 icount_adjust();
281}
282
283int64_t qemu_icount_round(int64_t count)
284{
285 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
286}
287
288static struct qemu_alarm_timer alarm_timers[] = {
289#ifndef _WIN32
290#ifdef __linux__
291 {"dynticks", dynticks_start_timer,
Stefan Weilcd0544e2011-04-10 20:15:09 +0200292 dynticks_stop_timer, dynticks_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100293#endif
Paolo Bonzini84682832011-06-09 13:10:25 +0200294 {"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100295#else
Stefan Weil2f9cba02011-04-05 18:34:21 +0200296 {"mmtimer", mm_start_timer, mm_stop_timer, NULL},
297 {"mmtimer2", mm_start_timer, mm_stop_timer, mm_rearm_timer},
Stefan Weilcd0544e2011-04-10 20:15:09 +0200298 {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer},
299 {"win32", win32_start_timer, win32_stop_timer, NULL},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100300#endif
301 {NULL, }
302};
303
304static void show_available_alarms(void)
305{
306 int i;
307
308 printf("Available alarm timers, in order of precedence:\n");
309 for (i = 0; alarm_timers[i].name; i++)
310 printf("%s\n", alarm_timers[i].name);
311}
312
313void configure_alarms(char const *opt)
314{
315 int i;
316 int cur = 0;
317 int count = ARRAY_SIZE(alarm_timers) - 1;
318 char *arg;
319 char *name;
320 struct qemu_alarm_timer tmp;
321
322 if (!strcmp(opt, "?")) {
323 show_available_alarms();
324 exit(0);
325 }
326
327 arg = qemu_strdup(opt);
328
329 /* Reorder the array */
330 name = strtok(arg, ",");
331 while (name) {
332 for (i = 0; i < count && alarm_timers[i].name; i++) {
333 if (!strcmp(alarm_timers[i].name, name))
334 break;
335 }
336
337 if (i == count) {
338 fprintf(stderr, "Unknown clock %s\n", name);
339 goto next;
340 }
341
342 if (i < cur)
343 /* Ignore */
344 goto next;
345
346 /* Swap */
347 tmp = alarm_timers[i];
348 alarm_timers[i] = alarm_timers[cur];
349 alarm_timers[cur] = tmp;
350
351 cur++;
352next:
353 name = strtok(NULL, ",");
354 }
355
356 qemu_free(arg);
357
358 if (cur) {
359 /* Disable remaining timers */
360 for (i = cur; i < count; i++)
361 alarm_timers[i].name = NULL;
362 } else {
363 show_available_alarms();
364 exit(1);
365 }
366}
367
368#define QEMU_NUM_CLOCKS 3
369
370QEMUClock *rt_clock;
371QEMUClock *vm_clock;
372QEMUClock *host_clock;
373
374static QEMUTimer *active_timers[QEMU_NUM_CLOCKS];
375
376static QEMUClock *qemu_new_clock(int type)
377{
378 QEMUClock *clock;
379 clock = qemu_mallocz(sizeof(QEMUClock));
380 clock->type = type;
381 clock->enabled = 1;
382 return clock;
383}
384
385void qemu_clock_enable(QEMUClock *clock, int enabled)
386{
387 clock->enabled = enabled;
388}
389
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200390static int64_t vm_clock_warp_start;
391
392static void icount_warp_rt(void *opaque)
393{
394 if (vm_clock_warp_start == -1) {
395 return;
396 }
397
398 if (vm_running) {
399 int64_t clock = qemu_get_clock_ns(rt_clock);
400 int64_t warp_delta = clock - vm_clock_warp_start;
401 if (use_icount == 1) {
402 qemu_icount_bias += warp_delta;
403 } else {
404 /*
405 * In adaptive mode, do not let the vm_clock run too
406 * far ahead of real time.
407 */
408 int64_t cur_time = cpu_get_clock();
409 int64_t cur_icount = qemu_get_clock_ns(vm_clock);
410 int64_t delta = cur_time - cur_icount;
411 qemu_icount_bias += MIN(warp_delta, delta);
412 }
413 if (qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL],
414 qemu_get_clock_ns(vm_clock))) {
415 qemu_notify_event();
416 }
417 }
418 vm_clock_warp_start = -1;
419}
420
421void qemu_clock_warp(QEMUClock *clock)
422{
423 int64_t deadline;
424
425 if (!clock->warp_timer) {
426 return;
427 }
428
429 /*
430 * There are too many global variables to make the "warp" behavior
431 * applicable to other clocks. But a clock argument removes the
432 * need for if statements all over the place.
433 */
434 assert(clock == vm_clock);
435
436 /*
437 * If the CPUs have been sleeping, advance the vm_clock timer now. This
438 * ensures that the deadline for the timer is computed correctly below.
439 * This also makes sure that the insn counter is synchronized before the
440 * CPU starts running, in case the CPU is woken by an event other than
441 * the earliest vm_clock timer.
442 */
443 icount_warp_rt(NULL);
444 if (!all_cpu_threads_idle() || !active_timers[clock->type]) {
445 qemu_del_timer(clock->warp_timer);
446 return;
447 }
448
449 vm_clock_warp_start = qemu_get_clock_ns(rt_clock);
Paolo Bonzinicb842c92011-04-13 10:03:46 +0200450 deadline = qemu_next_icount_deadline();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200451 if (deadline > 0) {
452 /*
453 * Ensure the vm_clock proceeds even when the virtual CPU goes to
454 * sleep. Otherwise, the CPU might be waiting for a future timer
455 * interrupt to wake it up, but the interrupt never comes because
456 * the vCPU isn't running any insns and thus doesn't advance the
457 * vm_clock.
458 *
459 * An extreme solution for this problem would be to never let VCPUs
460 * sleep in icount mode if there is a pending vm_clock timer; rather
461 * time could just advance to the next vm_clock event. Instead, we
462 * do stop VCPUs and only advance vm_clock after some "real" time,
463 * (related to the time left until the next event) has passed. This
464 * rt_clock timer will do this. This avoids that the warps are too
465 * visible externally---for example, you will not be sending network
466 * packets continously instead of every 100ms.
467 */
468 qemu_mod_timer(clock->warp_timer, vm_clock_warp_start + deadline);
469 } else {
470 qemu_notify_event();
471 }
472}
473
Paolo Bonzini4a998742011-03-11 16:33:58 +0100474QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
475 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100476{
477 QEMUTimer *ts;
478
479 ts = qemu_mallocz(sizeof(QEMUTimer));
480 ts->clock = clock;
481 ts->cb = cb;
482 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100483 ts->scale = scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100484 return ts;
485}
486
487void qemu_free_timer(QEMUTimer *ts)
488{
489 qemu_free(ts);
490}
491
492/* stop a timer, but do not dealloc it */
493void qemu_del_timer(QEMUTimer *ts)
494{
495 QEMUTimer **pt, *t;
496
497 /* NOTE: this code must be signal safe because
498 qemu_timer_expired() can be called from a signal. */
499 pt = &active_timers[ts->clock->type];
500 for(;;) {
501 t = *pt;
502 if (!t)
503 break;
504 if (t == ts) {
505 *pt = t->next;
506 break;
507 }
508 pt = &t->next;
509 }
510}
511
512/* modify the current timer so that it will be fired when current_time
513 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini4a998742011-03-11 16:33:58 +0100514static void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100515{
516 QEMUTimer **pt, *t;
517
518 qemu_del_timer(ts);
519
520 /* add the timer in the sorted list */
521 /* NOTE: this code must be signal safe because
522 qemu_timer_expired() can be called from a signal. */
523 pt = &active_timers[ts->clock->type];
524 for(;;) {
525 t = *pt;
Stefan Weil45c7b372011-03-24 21:31:24 +0100526 if (!qemu_timer_expired_ns(t, expire_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100527 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100528 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100529 pt = &t->next;
530 }
531 ts->expire_time = expire_time;
532 ts->next = *pt;
533 *pt = ts;
534
535 /* Rearm if necessary */
536 if (pt == &active_timers[ts->clock->type]) {
537 if (!alarm_timer->pending) {
538 qemu_rearm_alarm_timer(alarm_timer);
539 }
540 /* Interrupt execution to force deadline recalculation. */
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200541 qemu_clock_warp(ts->clock);
542 if (use_icount) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100543 qemu_notify_event();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200544 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100545 }
546}
547
Paolo Bonzini4a998742011-03-11 16:33:58 +0100548/* modify the current timer so that it will be fired when current_time
549 >= expire_time. The corresponding callback will be called. */
550void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
551{
552 qemu_mod_timer_ns(ts, expire_time * ts->scale);
553}
554
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100555int qemu_timer_pending(QEMUTimer *ts)
556{
557 QEMUTimer *t;
558 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
559 if (t == ts)
560 return 1;
561 }
562 return 0;
563}
564
565int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
566{
Stefan Weil45c7b372011-03-24 21:31:24 +0100567 return qemu_timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100568}
569
570static void qemu_run_timers(QEMUClock *clock)
571{
572 QEMUTimer **ptimer_head, *ts;
573 int64_t current_time;
574
575 if (!clock->enabled)
576 return;
577
Paolo Bonzini4a998742011-03-11 16:33:58 +0100578 current_time = qemu_get_clock_ns(clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100579 ptimer_head = &active_timers[clock->type];
580 for(;;) {
581 ts = *ptimer_head;
Stefan Weil45c7b372011-03-24 21:31:24 +0100582 if (!qemu_timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100583 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100584 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100585 /* remove timer from the list before calling the callback */
586 *ptimer_head = ts->next;
587 ts->next = NULL;
588
589 /* run the callback (the timer list can be modified) */
590 ts->cb(ts->opaque);
591 }
592}
593
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100594int64_t qemu_get_clock_ns(QEMUClock *clock)
595{
596 switch(clock->type) {
597 case QEMU_CLOCK_REALTIME:
598 return get_clock();
599 default:
600 case QEMU_CLOCK_VIRTUAL:
601 if (use_icount) {
602 return cpu_get_icount();
603 } else {
604 return cpu_get_clock();
605 }
606 case QEMU_CLOCK_HOST:
607 return get_clock_realtime();
608 }
609}
610
611void init_clocks(void)
612{
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100613 rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
614 vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
615 host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
616
617 rtc_clock = host_clock;
618}
619
620/* save a timer */
621void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
622{
623 uint64_t expire_time;
624
625 if (qemu_timer_pending(ts)) {
626 expire_time = ts->expire_time;
627 } else {
628 expire_time = -1;
629 }
630 qemu_put_be64(f, expire_time);
631}
632
633void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
634{
635 uint64_t expire_time;
636
637 expire_time = qemu_get_be64(f);
638 if (expire_time != -1) {
Paolo Bonzini4a998742011-03-11 16:33:58 +0100639 qemu_mod_timer_ns(ts, expire_time);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100640 } else {
641 qemu_del_timer(ts);
642 }
643}
644
645static const VMStateDescription vmstate_timers = {
646 .name = "timer",
647 .version_id = 2,
648 .minimum_version_id = 1,
649 .minimum_version_id_old = 1,
650 .fields = (VMStateField []) {
651 VMSTATE_INT64(cpu_ticks_offset, TimersState),
652 VMSTATE_INT64(dummy, TimersState),
653 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
654 VMSTATE_END_OF_LIST()
655 }
656};
657
658void configure_icount(const char *option)
659{
Alex Williamson0be71e32010-06-25 11:09:07 -0600660 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100661 if (!option)
662 return;
663
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200664#ifdef CONFIG_IOTHREAD
665 vm_clock->warp_timer = qemu_new_timer_ns(rt_clock, icount_warp_rt, NULL);
666#endif
667
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100668 if (strcmp(option, "auto") != 0) {
669 icount_time_shift = strtol(option, NULL, 0);
670 use_icount = 1;
671 return;
672 }
673
674 use_icount = 2;
675
676 /* 125MIPS seems a reasonable initial guess at the guest speed.
677 It will be corrected fairly quickly anyway. */
678 icount_time_shift = 3;
679
680 /* Have both realtime and virtual time triggers for speed adjustment.
681 The realtime trigger catches emulated time passing too slowly,
682 the virtual time trigger catches emulated time passing too fast.
683 Realtime triggers occur even when idle, so use them less frequently
684 than VM triggers. */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100685 icount_rt_timer = qemu_new_timer_ms(rt_clock, icount_adjust_rt, NULL);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100686 qemu_mod_timer(icount_rt_timer,
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100687 qemu_get_clock_ms(rt_clock) + 1000);
Paolo Bonzini74475452011-03-11 16:47:48 +0100688 icount_vm_timer = qemu_new_timer_ns(vm_clock, icount_adjust_vm, NULL);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100689 qemu_mod_timer(icount_vm_timer,
Paolo Bonzini74475452011-03-11 16:47:48 +0100690 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100691}
692
693void qemu_run_all_timers(void)
694{
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100695 alarm_timer->pending = 0;
696
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100697 /* rearm timer, if not periodic */
698 if (alarm_timer->expired) {
699 alarm_timer->expired = 0;
700 qemu_rearm_alarm_timer(alarm_timer);
701 }
702
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100703 /* vm time timers */
704 if (vm_running) {
705 qemu_run_timers(vm_clock);
706 }
707
708 qemu_run_timers(rt_clock);
709 qemu_run_timers(host_clock);
710}
711
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100712static int64_t qemu_next_alarm_deadline(void);
713
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100714#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100715static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100716#else
717static void host_alarm_handler(int host_signum)
718#endif
719{
720 struct qemu_alarm_timer *t = alarm_timer;
721 if (!t)
722 return;
723
724#if 0
725#define DISP_FREQ 1000
726 {
727 static int64_t delta_min = INT64_MAX;
728 static int64_t delta_max, delta_cum, last_clock, delta, ti;
729 static int count;
Paolo Bonzini74475452011-03-11 16:47:48 +0100730 ti = qemu_get_clock_ns(vm_clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100731 if (last_clock != 0) {
732 delta = ti - last_clock;
733 if (delta < delta_min)
734 delta_min = delta;
735 if (delta > delta_max)
736 delta_max = delta;
737 delta_cum += delta;
738 if (++count == DISP_FREQ) {
739 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
740 muldiv64(delta_min, 1000000, get_ticks_per_sec()),
741 muldiv64(delta_max, 1000000, get_ticks_per_sec()),
742 muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()),
743 (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ));
744 count = 0;
745 delta_min = INT64_MAX;
746 delta_max = 0;
747 delta_cum = 0;
748 }
749 }
750 last_clock = ti;
751 }
752#endif
753 if (alarm_has_dynticks(t) ||
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100754 qemu_next_alarm_deadline () <= 0) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100755 t->expired = alarm_has_dynticks(t);
756 t->pending = 1;
757 qemu_notify_event();
758 }
759}
760
Paolo Bonzinicb842c92011-04-13 10:03:46 +0200761int64_t qemu_next_icount_deadline(void)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100762{
763 /* To avoid problems with overflow limit this to 2^32. */
764 int64_t delta = INT32_MAX;
765
Paolo Bonzinicb842c92011-04-13 10:03:46 +0200766 assert(use_icount);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100767 if (active_timers[QEMU_CLOCK_VIRTUAL]) {
768 delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
Paolo Bonzini9c132462011-02-03 14:48:59 +0100769 qemu_get_clock_ns(vm_clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100770 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100771
772 if (delta < 0)
773 delta = 0;
774
775 return delta;
776}
777
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100778static int64_t qemu_next_alarm_deadline(void)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100779{
780 int64_t delta;
781 int64_t rtdelta;
782
Paolo Bonzini6ad0a1e2011-02-03 14:49:00 +0100783 if (!use_icount && active_timers[QEMU_CLOCK_VIRTUAL]) {
784 delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
Paolo Bonzini74475452011-03-11 16:47:48 +0100785 qemu_get_clock_ns(vm_clock);
Paolo Bonzini6ad0a1e2011-02-03 14:49:00 +0100786 } else {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100787 delta = INT32_MAX;
Paolo Bonzini6ad0a1e2011-02-03 14:49:00 +0100788 }
789 if (active_timers[QEMU_CLOCK_HOST]) {
790 int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
791 qemu_get_clock_ns(host_clock);
792 if (hdelta < delta)
793 delta = hdelta;
794 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100795 if (active_timers[QEMU_CLOCK_REALTIME]) {
Paolo Bonzini4a998742011-03-11 16:33:58 +0100796 rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time -
Paolo Bonzini9c132462011-02-03 14:48:59 +0100797 qemu_get_clock_ns(rt_clock));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100798 if (rtdelta < delta)
799 delta = rtdelta;
800 }
801
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100802 return delta;
803}
804
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100805#if defined(__linux__)
806
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100807static int dynticks_start_timer(struct qemu_alarm_timer *t)
808{
809 struct sigevent ev;
810 timer_t host_timer;
811 struct sigaction act;
812
813 sigfillset(&act.sa_mask);
814 act.sa_flags = 0;
815 act.sa_handler = host_alarm_handler;
816
817 sigaction(SIGALRM, &act, NULL);
818
819 /*
820 * Initialize ev struct to 0 to avoid valgrind complaining
821 * about uninitialized data in timer_create call
822 */
823 memset(&ev, 0, sizeof(ev));
824 ev.sigev_value.sival_int = 0;
825 ev.sigev_notify = SIGEV_SIGNAL;
826 ev.sigev_signo = SIGALRM;
827
828 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
829 perror("timer_create");
830
831 /* disable dynticks */
832 fprintf(stderr, "Dynamic Ticks disabled\n");
833
834 return -1;
835 }
836
Stefan Weilcd0544e2011-04-10 20:15:09 +0200837 t->timer = host_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100838
839 return 0;
840}
841
842static void dynticks_stop_timer(struct qemu_alarm_timer *t)
843{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200844 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100845
846 timer_delete(host_timer);
847}
848
849static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
850{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200851 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100852 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100853 int64_t nearest_delta_ns = INT64_MAX;
854 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100855
856 assert(alarm_has_dynticks(t));
857 if (!active_timers[QEMU_CLOCK_REALTIME] &&
858 !active_timers[QEMU_CLOCK_VIRTUAL] &&
859 !active_timers[QEMU_CLOCK_HOST])
860 return;
861
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100862 nearest_delta_ns = qemu_next_alarm_deadline();
863 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
864 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100865
866 /* check whether a timer is already running */
867 if (timer_gettime(host_timer, &timeout)) {
868 perror("gettime");
869 fprintf(stderr, "Internal timer error: aborting\n");
870 exit(1);
871 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100872 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
873 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100874 return;
875
876 timeout.it_interval.tv_sec = 0;
877 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100878 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
879 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100880 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
881 perror("settime");
882 fprintf(stderr, "Internal timer error: aborting\n");
883 exit(1);
884 }
885}
886
887#endif /* defined(__linux__) */
888
Stefan Weilf26e5a52011-02-04 22:01:32 +0100889#if !defined(_WIN32)
890
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100891static int unix_start_timer(struct qemu_alarm_timer *t)
892{
893 struct sigaction act;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100894
895 /* timer signal */
896 sigfillset(&act.sa_mask);
897 act.sa_flags = 0;
898 act.sa_handler = host_alarm_handler;
899
900 sigaction(SIGALRM, &act, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200901 return 0;
902}
903
904static void unix_rearm_timer(struct qemu_alarm_timer *t)
905{
906 struct itimerval itv;
907 int64_t nearest_delta_ns = INT64_MAX;
908 int err;
909
910 assert(alarm_has_dynticks(t));
911 if (!active_timers[QEMU_CLOCK_REALTIME] &&
912 !active_timers[QEMU_CLOCK_VIRTUAL] &&
913 !active_timers[QEMU_CLOCK_HOST])
914 return;
915
916 nearest_delta_ns = qemu_next_alarm_deadline();
917 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
918 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100919
920 itv.it_interval.tv_sec = 0;
Paolo Bonzini84682832011-06-09 13:10:25 +0200921 itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */
922 itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
923 itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100924 err = setitimer(ITIMER_REAL, &itv, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200925 if (err) {
926 perror("setitimer");
927 fprintf(stderr, "Internal timer error: aborting\n");
928 exit(1);
929 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100930}
931
932static void unix_stop_timer(struct qemu_alarm_timer *t)
933{
934 struct itimerval itv;
935
936 memset(&itv, 0, sizeof(itv));
937 setitimer(ITIMER_REAL, &itv, NULL);
938}
939
940#endif /* !defined(_WIN32) */
941
942
943#ifdef _WIN32
944
Stefan Weil2f9cba02011-04-05 18:34:21 +0200945static MMRESULT mm_timer;
946static unsigned mm_period;
947
948static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
949 DWORD_PTR dwUser, DWORD_PTR dw1,
950 DWORD_PTR dw2)
951{
952 struct qemu_alarm_timer *t = alarm_timer;
953 if (!t) {
954 return;
955 }
956 if (alarm_has_dynticks(t) || qemu_next_alarm_deadline() <= 0) {
957 t->expired = alarm_has_dynticks(t);
958 t->pending = 1;
959 qemu_notify_event();
960 }
961}
962
963static int mm_start_timer(struct qemu_alarm_timer *t)
964{
965 TIMECAPS tc;
966 UINT flags;
967
968 memset(&tc, 0, sizeof(tc));
969 timeGetDevCaps(&tc, sizeof(tc));
970
971 mm_period = tc.wPeriodMin;
972 timeBeginPeriod(mm_period);
973
974 flags = TIME_CALLBACK_FUNCTION;
975 if (alarm_has_dynticks(t)) {
976 flags |= TIME_ONESHOT;
977 } else {
978 flags |= TIME_PERIODIC;
979 }
980
981 mm_timer = timeSetEvent(1, /* interval (ms) */
982 mm_period, /* resolution */
983 mm_alarm_handler, /* function */
984 (DWORD_PTR)t, /* parameter */
985 flags);
986
987 if (!mm_timer) {
988 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
989 GetLastError());
990 timeEndPeriod(mm_period);
991 return -1;
992 }
993
994 return 0;
995}
996
997static void mm_stop_timer(struct qemu_alarm_timer *t)
998{
999 timeKillEvent(mm_timer);
1000 timeEndPeriod(mm_period);
1001}
1002
1003static void mm_rearm_timer(struct qemu_alarm_timer *t)
1004{
1005 int nearest_delta_ms;
1006
1007 assert(alarm_has_dynticks(t));
1008 if (!active_timers[QEMU_CLOCK_REALTIME] &&
1009 !active_timers[QEMU_CLOCK_VIRTUAL] &&
1010 !active_timers[QEMU_CLOCK_HOST]) {
1011 return;
1012 }
1013
1014 timeKillEvent(mm_timer);
1015
1016 nearest_delta_ms = (qemu_next_alarm_deadline() + 999999) / 1000000;
1017 if (nearest_delta_ms < 1) {
1018 nearest_delta_ms = 1;
1019 }
1020 mm_timer = timeSetEvent(nearest_delta_ms,
1021 mm_period,
1022 mm_alarm_handler,
1023 (DWORD_PTR)t,
1024 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
1025
1026 if (!mm_timer) {
1027 fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
1028 GetLastError());
1029
1030 timeEndPeriod(mm_period);
1031 exit(1);
1032 }
1033}
1034
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001035static int win32_start_timer(struct qemu_alarm_timer *t)
1036{
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001037 HANDLE hTimer;
1038 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001039
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001040 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
1041 is zero) that has already expired, the timer is not updated. Since
1042 creating a new timer is relatively expensive, set a bogus one-hour
1043 interval in the dynticks case. */
1044 success = CreateTimerQueueTimer(&hTimer,
1045 NULL,
1046 host_alarm_handler,
1047 t,
1048 1,
1049 alarm_has_dynticks(t) ? 3600000 : 1,
1050 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001051
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001052 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001053 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
1054 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001055 return -1;
1056 }
1057
Stefan Weilcd0544e2011-04-10 20:15:09 +02001058 t->timer = hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001059 return 0;
1060}
1061
1062static void win32_stop_timer(struct qemu_alarm_timer *t)
1063{
Stefan Weilcd0544e2011-04-10 20:15:09 +02001064 HANDLE hTimer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001065
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001066 if (hTimer) {
1067 DeleteTimerQueueTimer(NULL, hTimer, NULL);
1068 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001069}
1070
1071static void win32_rearm_timer(struct qemu_alarm_timer *t)
1072{
Stefan Weilcd0544e2011-04-10 20:15:09 +02001073 HANDLE hTimer = t->timer;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +01001074 int nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001075 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001076
1077 assert(alarm_has_dynticks(t));
1078 if (!active_timers[QEMU_CLOCK_REALTIME] &&
1079 !active_timers[QEMU_CLOCK_VIRTUAL] &&
1080 !active_timers[QEMU_CLOCK_HOST])
1081 return;
1082
Paolo Bonzinicfced5b2011-03-12 17:43:49 +01001083 nearest_delta_ms = (qemu_next_alarm_deadline() + 999999) / 1000000;
1084 if (nearest_delta_ms < 1) {
1085 nearest_delta_ms = 1;
1086 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001087 success = ChangeTimerQueueTimer(NULL,
1088 hTimer,
1089 nearest_delta_ms,
1090 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001091
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001092 if (!success) {
1093 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001094 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001095 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001096 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001097
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001098}
1099
1100#endif /* _WIN32 */
1101
1102static void alarm_timer_on_change_state_rearm(void *opaque, int running, int reason)
1103{
1104 if (running)
1105 qemu_rearm_alarm_timer((struct qemu_alarm_timer *) opaque);
1106}
1107
1108int init_timer_alarm(void)
1109{
1110 struct qemu_alarm_timer *t = NULL;
1111 int i, err = -1;
1112
1113 for (i = 0; alarm_timers[i].name; i++) {
1114 t = &alarm_timers[i];
1115
1116 err = t->start(t);
1117 if (!err)
1118 break;
1119 }
1120
1121 if (err) {
1122 err = -ENOENT;
1123 goto fail;
1124 }
1125
1126 /* first event is at time 0 */
1127 t->pending = 1;
1128 alarm_timer = t;
1129 qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm, t);
1130
1131 return 0;
1132
1133fail:
1134 return err;
1135}
1136
1137void quit_timers(void)
1138{
1139 struct qemu_alarm_timer *t = alarm_timer;
1140 alarm_timer = NULL;
1141 t->stop(t);
1142}
1143
1144int qemu_calculate_timeout(void)
1145{
Paolo Bonzini1ece93a2011-04-13 10:03:45 +02001146#ifndef CONFIG_IOTHREAD
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001147 int timeout;
1148
Paolo Bonzini1ece93a2011-04-13 10:03:45 +02001149 if (!vm_running)
1150 timeout = 5000;
1151 else {
1152 /* XXX: use timeout computed from timers */
1153 int64_t add;
1154 int64_t delta;
1155 /* Advance virtual time to the next event. */
1156 delta = qemu_icount_delta();
1157 if (delta > 0) {
1158 /* If virtual time is ahead of real time then just
1159 wait for IO. */
1160 timeout = (delta + 999999) / 1000000;
1161 } else {
1162 /* Wait for either IO to occur or the next
1163 timer event. */
Paolo Bonzinicb842c92011-04-13 10:03:46 +02001164 add = qemu_next_icount_deadline();
Paolo Bonzini1ece93a2011-04-13 10:03:45 +02001165 /* We advance the timer before checking for IO.
1166 Limit the amount we advance so that early IO
1167 activity won't get the guest too far ahead. */
1168 if (add > 10000000)
1169 add = 10000000;
1170 delta += add;
1171 qemu_icount += qemu_icount_round (add);
1172 timeout = delta / 1000000;
1173 if (timeout < 0)
1174 timeout = 0;
1175 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001176 }
1177
1178 return timeout;
Paolo Bonzini1ece93a2011-04-13 10:03:45 +02001179#else /* CONFIG_IOTHREAD */
1180 return 1000;
1181#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001182}
1183