blob: 46dd483fdd2c1719d62659d01a054578b526c03e [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 Bonzinidb1a4972010-03-10 11:38:55 +0100104/* enable cpu_get_ticks() */
105void cpu_enable_ticks(void)
106{
107 if (!timers_state.cpu_ticks_enabled) {
108 timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
109 timers_state.cpu_clock_offset -= get_clock();
110 timers_state.cpu_ticks_enabled = 1;
111 }
112}
113
114/* disable cpu_get_ticks() : the clock is stopped. You must not call
115 cpu_get_ticks() after that. */
116void cpu_disable_ticks(void)
117{
118 if (timers_state.cpu_ticks_enabled) {
119 timers_state.cpu_ticks_offset = cpu_get_ticks();
120 timers_state.cpu_clock_offset = cpu_get_clock();
121 timers_state.cpu_ticks_enabled = 0;
122 }
123}
124
125/***********************************************************/
126/* timers */
127
128#define QEMU_CLOCK_REALTIME 0
129#define QEMU_CLOCK_VIRTUAL 1
130#define QEMU_CLOCK_HOST 2
131
132struct QEMUClock {
133 int type;
134 int enabled;
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200135
136 QEMUTimer *warp_timer;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200137
138 NotifierList reset_notifiers;
139 int64_t last;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100140};
141
142struct QEMUTimer {
143 QEMUClock *clock;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100144 int64_t expire_time; /* in nanoseconds */
145 int scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100146 QEMUTimerCB *cb;
147 void *opaque;
148 struct QEMUTimer *next;
149};
150
151struct qemu_alarm_timer {
152 char const *name;
153 int (*start)(struct qemu_alarm_timer *t);
154 void (*stop)(struct qemu_alarm_timer *t);
155 void (*rearm)(struct qemu_alarm_timer *t);
Stefan Weilcd0544e2011-04-10 20:15:09 +0200156#if defined(__linux__)
157 int fd;
158 timer_t timer;
159#elif defined(_WIN32)
160 HANDLE timer;
161#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100162 char expired;
163 char pending;
164};
165
166static struct qemu_alarm_timer *alarm_timer;
167
Stefan Weil45c7b372011-03-24 21:31:24 +0100168static bool qemu_timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
169{
170 return timer_head && (timer_head->expire_time <= current_time);
171}
172
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100173int qemu_alarm_pending(void)
174{
175 return alarm_timer->pending;
176}
177
178static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
179{
180 return !!t->rearm;
181}
182
183static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
184{
185 if (!alarm_has_dynticks(t))
186 return;
187
188 t->rearm(t);
189}
190
Paolo Bonzini9c132462011-02-03 14:48:59 +0100191/* TODO: MIN_TIMER_REARM_NS should be optimized */
192#define MIN_TIMER_REARM_NS 250000
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100193
194#ifdef _WIN32
195
Stefan Weil2f9cba02011-04-05 18:34:21 +0200196static int mm_start_timer(struct qemu_alarm_timer *t);
197static void mm_stop_timer(struct qemu_alarm_timer *t);
198static void mm_rearm_timer(struct qemu_alarm_timer *t);
199
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100200static int win32_start_timer(struct qemu_alarm_timer *t);
201static void win32_stop_timer(struct qemu_alarm_timer *t);
202static void win32_rearm_timer(struct qemu_alarm_timer *t);
203
204#else
205
206static int unix_start_timer(struct qemu_alarm_timer *t);
207static void unix_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzini84682832011-06-09 13:10:25 +0200208static void unix_rearm_timer(struct qemu_alarm_timer *t);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100209
210#ifdef __linux__
211
212static int dynticks_start_timer(struct qemu_alarm_timer *t);
213static void dynticks_stop_timer(struct qemu_alarm_timer *t);
214static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
215
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100216#endif /* __linux__ */
217
218#endif /* _WIN32 */
219
220/* Correlation between real and virtual time is always going to be
221 fairly approximate, so ignore small variation.
222 When the guest is idle real and virtual time will be aligned in
223 the IO wait loop. */
224#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
225
226static void icount_adjust(void)
227{
228 int64_t cur_time;
229 int64_t cur_icount;
230 int64_t delta;
231 static int64_t last_delta;
232 /* If the VM is not running, then do nothing. */
233 if (!vm_running)
234 return;
235
236 cur_time = cpu_get_clock();
Paolo Bonzini74475452011-03-11 16:47:48 +0100237 cur_icount = qemu_get_clock_ns(vm_clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100238 delta = cur_icount - cur_time;
239 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
240 if (delta > 0
241 && last_delta + ICOUNT_WOBBLE < delta * 2
242 && icount_time_shift > 0) {
243 /* The guest is getting too far ahead. Slow time down. */
244 icount_time_shift--;
245 }
246 if (delta < 0
247 && last_delta - ICOUNT_WOBBLE > delta * 2
248 && icount_time_shift < MAX_ICOUNT_SHIFT) {
249 /* The guest is getting too far behind. Speed time up. */
250 icount_time_shift++;
251 }
252 last_delta = delta;
253 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
254}
255
256static void icount_adjust_rt(void * opaque)
257{
258 qemu_mod_timer(icount_rt_timer,
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100259 qemu_get_clock_ms(rt_clock) + 1000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100260 icount_adjust();
261}
262
263static void icount_adjust_vm(void * opaque)
264{
265 qemu_mod_timer(icount_vm_timer,
Paolo Bonzini74475452011-03-11 16:47:48 +0100266 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100267 icount_adjust();
268}
269
270int64_t qemu_icount_round(int64_t count)
271{
272 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
273}
274
275static struct qemu_alarm_timer alarm_timers[] = {
276#ifndef _WIN32
277#ifdef __linux__
278 {"dynticks", dynticks_start_timer,
Stefan Weilcd0544e2011-04-10 20:15:09 +0200279 dynticks_stop_timer, dynticks_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100280#endif
Paolo Bonzini84682832011-06-09 13:10:25 +0200281 {"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100282#else
Stefan Weil2f9cba02011-04-05 18:34:21 +0200283 {"mmtimer", mm_start_timer, mm_stop_timer, NULL},
284 {"mmtimer2", mm_start_timer, mm_stop_timer, mm_rearm_timer},
Stefan Weilcd0544e2011-04-10 20:15:09 +0200285 {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer},
286 {"win32", win32_start_timer, win32_stop_timer, NULL},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100287#endif
288 {NULL, }
289};
290
291static void show_available_alarms(void)
292{
293 int i;
294
295 printf("Available alarm timers, in order of precedence:\n");
296 for (i = 0; alarm_timers[i].name; i++)
297 printf("%s\n", alarm_timers[i].name);
298}
299
300void configure_alarms(char const *opt)
301{
302 int i;
303 int cur = 0;
304 int count = ARRAY_SIZE(alarm_timers) - 1;
305 char *arg;
306 char *name;
307 struct qemu_alarm_timer tmp;
308
309 if (!strcmp(opt, "?")) {
310 show_available_alarms();
311 exit(0);
312 }
313
Anthony Liguori7267c092011-08-20 22:09:37 -0500314 arg = g_strdup(opt);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100315
316 /* Reorder the array */
317 name = strtok(arg, ",");
318 while (name) {
319 for (i = 0; i < count && alarm_timers[i].name; i++) {
320 if (!strcmp(alarm_timers[i].name, name))
321 break;
322 }
323
324 if (i == count) {
325 fprintf(stderr, "Unknown clock %s\n", name);
326 goto next;
327 }
328
329 if (i < cur)
330 /* Ignore */
331 goto next;
332
333 /* Swap */
334 tmp = alarm_timers[i];
335 alarm_timers[i] = alarm_timers[cur];
336 alarm_timers[cur] = tmp;
337
338 cur++;
339next:
340 name = strtok(NULL, ",");
341 }
342
Anthony Liguori7267c092011-08-20 22:09:37 -0500343 g_free(arg);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100344
345 if (cur) {
346 /* Disable remaining timers */
347 for (i = cur; i < count; i++)
348 alarm_timers[i].name = NULL;
349 } else {
350 show_available_alarms();
351 exit(1);
352 }
353}
354
355#define QEMU_NUM_CLOCKS 3
356
357QEMUClock *rt_clock;
358QEMUClock *vm_clock;
359QEMUClock *host_clock;
360
361static QEMUTimer *active_timers[QEMU_NUM_CLOCKS];
362
363static QEMUClock *qemu_new_clock(int type)
364{
365 QEMUClock *clock;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200366
Anthony Liguori7267c092011-08-20 22:09:37 -0500367 clock = g_malloc0(sizeof(QEMUClock));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100368 clock->type = type;
369 clock->enabled = 1;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200370 notifier_list_init(&clock->reset_notifiers);
371 /* required to detect & report backward jumps */
372 if (type == QEMU_CLOCK_HOST) {
373 clock->last = get_clock_realtime();
374 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100375 return clock;
376}
377
378void qemu_clock_enable(QEMUClock *clock, int enabled)
379{
380 clock->enabled = enabled;
381}
382
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200383static int64_t vm_clock_warp_start;
384
385static void icount_warp_rt(void *opaque)
386{
387 if (vm_clock_warp_start == -1) {
388 return;
389 }
390
391 if (vm_running) {
392 int64_t clock = qemu_get_clock_ns(rt_clock);
393 int64_t warp_delta = clock - vm_clock_warp_start;
394 if (use_icount == 1) {
395 qemu_icount_bias += warp_delta;
396 } else {
397 /*
398 * In adaptive mode, do not let the vm_clock run too
399 * far ahead of real time.
400 */
401 int64_t cur_time = cpu_get_clock();
402 int64_t cur_icount = qemu_get_clock_ns(vm_clock);
403 int64_t delta = cur_time - cur_icount;
404 qemu_icount_bias += MIN(warp_delta, delta);
405 }
406 if (qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL],
407 qemu_get_clock_ns(vm_clock))) {
408 qemu_notify_event();
409 }
410 }
411 vm_clock_warp_start = -1;
412}
413
414void qemu_clock_warp(QEMUClock *clock)
415{
416 int64_t deadline;
417
418 if (!clock->warp_timer) {
419 return;
420 }
421
422 /*
423 * There are too many global variables to make the "warp" behavior
424 * applicable to other clocks. But a clock argument removes the
425 * need for if statements all over the place.
426 */
427 assert(clock == vm_clock);
428
429 /*
430 * If the CPUs have been sleeping, advance the vm_clock timer now. This
431 * ensures that the deadline for the timer is computed correctly below.
432 * This also makes sure that the insn counter is synchronized before the
433 * CPU starts running, in case the CPU is woken by an event other than
434 * the earliest vm_clock timer.
435 */
436 icount_warp_rt(NULL);
437 if (!all_cpu_threads_idle() || !active_timers[clock->type]) {
438 qemu_del_timer(clock->warp_timer);
439 return;
440 }
441
442 vm_clock_warp_start = qemu_get_clock_ns(rt_clock);
Paolo Bonzinicb842c92011-04-13 10:03:46 +0200443 deadline = qemu_next_icount_deadline();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200444 if (deadline > 0) {
445 /*
446 * Ensure the vm_clock proceeds even when the virtual CPU goes to
447 * sleep. Otherwise, the CPU might be waiting for a future timer
448 * interrupt to wake it up, but the interrupt never comes because
449 * the vCPU isn't running any insns and thus doesn't advance the
450 * vm_clock.
451 *
452 * An extreme solution for this problem would be to never let VCPUs
453 * sleep in icount mode if there is a pending vm_clock timer; rather
454 * time could just advance to the next vm_clock event. Instead, we
455 * do stop VCPUs and only advance vm_clock after some "real" time,
456 * (related to the time left until the next event) has passed. This
457 * rt_clock timer will do this. This avoids that the warps are too
458 * visible externally---for example, you will not be sending network
459 * packets continously instead of every 100ms.
460 */
461 qemu_mod_timer(clock->warp_timer, vm_clock_warp_start + deadline);
462 } else {
463 qemu_notify_event();
464 }
465}
466
Paolo Bonzini4a998742011-03-11 16:33:58 +0100467QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
468 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100469{
470 QEMUTimer *ts;
471
Anthony Liguori7267c092011-08-20 22:09:37 -0500472 ts = g_malloc0(sizeof(QEMUTimer));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100473 ts->clock = clock;
474 ts->cb = cb;
475 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100476 ts->scale = scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100477 return ts;
478}
479
480void qemu_free_timer(QEMUTimer *ts)
481{
Anthony Liguori7267c092011-08-20 22:09:37 -0500482 g_free(ts);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100483}
484
485/* stop a timer, but do not dealloc it */
486void qemu_del_timer(QEMUTimer *ts)
487{
488 QEMUTimer **pt, *t;
489
490 /* NOTE: this code must be signal safe because
491 qemu_timer_expired() can be called from a signal. */
492 pt = &active_timers[ts->clock->type];
493 for(;;) {
494 t = *pt;
495 if (!t)
496 break;
497 if (t == ts) {
498 *pt = t->next;
499 break;
500 }
501 pt = &t->next;
502 }
503}
504
505/* modify the current timer so that it will be fired when current_time
506 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini4a998742011-03-11 16:33:58 +0100507static void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100508{
509 QEMUTimer **pt, *t;
510
511 qemu_del_timer(ts);
512
513 /* add the timer in the sorted list */
514 /* NOTE: this code must be signal safe because
515 qemu_timer_expired() can be called from a signal. */
516 pt = &active_timers[ts->clock->type];
517 for(;;) {
518 t = *pt;
Stefan Weil45c7b372011-03-24 21:31:24 +0100519 if (!qemu_timer_expired_ns(t, expire_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100520 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100521 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100522 pt = &t->next;
523 }
524 ts->expire_time = expire_time;
525 ts->next = *pt;
526 *pt = ts;
527
528 /* Rearm if necessary */
529 if (pt == &active_timers[ts->clock->type]) {
530 if (!alarm_timer->pending) {
531 qemu_rearm_alarm_timer(alarm_timer);
532 }
533 /* Interrupt execution to force deadline recalculation. */
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200534 qemu_clock_warp(ts->clock);
535 if (use_icount) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100536 qemu_notify_event();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200537 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100538 }
539}
540
Paolo Bonzini4a998742011-03-11 16:33:58 +0100541/* modify the current timer so that it will be fired when current_time
542 >= expire_time. The corresponding callback will be called. */
543void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
544{
545 qemu_mod_timer_ns(ts, expire_time * ts->scale);
546}
547
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100548int qemu_timer_pending(QEMUTimer *ts)
549{
550 QEMUTimer *t;
551 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
552 if (t == ts)
553 return 1;
554 }
555 return 0;
556}
557
558int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
559{
Stefan Weil45c7b372011-03-24 21:31:24 +0100560 return qemu_timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100561}
562
563static void qemu_run_timers(QEMUClock *clock)
564{
565 QEMUTimer **ptimer_head, *ts;
566 int64_t current_time;
567
568 if (!clock->enabled)
569 return;
570
Paolo Bonzini4a998742011-03-11 16:33:58 +0100571 current_time = qemu_get_clock_ns(clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100572 ptimer_head = &active_timers[clock->type];
573 for(;;) {
574 ts = *ptimer_head;
Stefan Weil45c7b372011-03-24 21:31:24 +0100575 if (!qemu_timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100576 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100577 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100578 /* remove timer from the list before calling the callback */
579 *ptimer_head = ts->next;
580 ts->next = NULL;
581
582 /* run the callback (the timer list can be modified) */
583 ts->cb(ts->opaque);
584 }
585}
586
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100587int64_t qemu_get_clock_ns(QEMUClock *clock)
588{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200589 int64_t now, last;
590
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100591 switch(clock->type) {
592 case QEMU_CLOCK_REALTIME:
593 return get_clock();
594 default:
595 case QEMU_CLOCK_VIRTUAL:
596 if (use_icount) {
597 return cpu_get_icount();
598 } else {
599 return cpu_get_clock();
600 }
601 case QEMU_CLOCK_HOST:
Jan Kiszka691a0c92011-06-20 14:06:27 +0200602 now = get_clock_realtime();
603 last = clock->last;
604 clock->last = now;
605 if (now < last) {
606 notifier_list_notify(&clock->reset_notifiers, &now);
607 }
608 return now;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100609 }
610}
611
Jan Kiszka691a0c92011-06-20 14:06:27 +0200612void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
613{
614 notifier_list_add(&clock->reset_notifiers, notifier);
615}
616
617void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
618{
619 notifier_list_remove(&clock->reset_notifiers, notifier);
620}
621
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100622void init_clocks(void)
623{
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100624 rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
625 vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
626 host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
627
628 rtc_clock = host_clock;
629}
630
631/* save a timer */
632void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
633{
634 uint64_t expire_time;
635
636 if (qemu_timer_pending(ts)) {
637 expire_time = ts->expire_time;
638 } else {
639 expire_time = -1;
640 }
641 qemu_put_be64(f, expire_time);
642}
643
644void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
645{
646 uint64_t expire_time;
647
648 expire_time = qemu_get_be64(f);
649 if (expire_time != -1) {
Paolo Bonzini4a998742011-03-11 16:33:58 +0100650 qemu_mod_timer_ns(ts, expire_time);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100651 } else {
652 qemu_del_timer(ts);
653 }
654}
655
656static const VMStateDescription vmstate_timers = {
657 .name = "timer",
658 .version_id = 2,
659 .minimum_version_id = 1,
660 .minimum_version_id_old = 1,
661 .fields = (VMStateField []) {
662 VMSTATE_INT64(cpu_ticks_offset, TimersState),
663 VMSTATE_INT64(dummy, TimersState),
664 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
665 VMSTATE_END_OF_LIST()
666 }
667};
668
669void configure_icount(const char *option)
670{
Alex Williamson0be71e32010-06-25 11:09:07 -0600671 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100672 if (!option)
673 return;
674
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200675 vm_clock->warp_timer = qemu_new_timer_ns(rt_clock, icount_warp_rt, NULL);
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200676
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100677 if (strcmp(option, "auto") != 0) {
678 icount_time_shift = strtol(option, NULL, 0);
679 use_icount = 1;
680 return;
681 }
682
683 use_icount = 2;
684
685 /* 125MIPS seems a reasonable initial guess at the guest speed.
686 It will be corrected fairly quickly anyway. */
687 icount_time_shift = 3;
688
689 /* Have both realtime and virtual time triggers for speed adjustment.
690 The realtime trigger catches emulated time passing too slowly,
691 the virtual time trigger catches emulated time passing too fast.
692 Realtime triggers occur even when idle, so use them less frequently
693 than VM triggers. */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100694 icount_rt_timer = qemu_new_timer_ms(rt_clock, icount_adjust_rt, NULL);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100695 qemu_mod_timer(icount_rt_timer,
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100696 qemu_get_clock_ms(rt_clock) + 1000);
Paolo Bonzini74475452011-03-11 16:47:48 +0100697 icount_vm_timer = qemu_new_timer_ns(vm_clock, icount_adjust_vm, NULL);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100698 qemu_mod_timer(icount_vm_timer,
Paolo Bonzini74475452011-03-11 16:47:48 +0100699 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100700}
701
702void qemu_run_all_timers(void)
703{
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100704 alarm_timer->pending = 0;
705
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100706 /* rearm timer, if not periodic */
707 if (alarm_timer->expired) {
708 alarm_timer->expired = 0;
709 qemu_rearm_alarm_timer(alarm_timer);
710 }
711
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100712 /* vm time timers */
713 if (vm_running) {
714 qemu_run_timers(vm_clock);
715 }
716
717 qemu_run_timers(rt_clock);
718 qemu_run_timers(host_clock);
719}
720
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100721static int64_t qemu_next_alarm_deadline(void);
722
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100723#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100724static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100725#else
726static void host_alarm_handler(int host_signum)
727#endif
728{
729 struct qemu_alarm_timer *t = alarm_timer;
730 if (!t)
731 return;
732
733#if 0
734#define DISP_FREQ 1000
735 {
736 static int64_t delta_min = INT64_MAX;
737 static int64_t delta_max, delta_cum, last_clock, delta, ti;
738 static int count;
Paolo Bonzini74475452011-03-11 16:47:48 +0100739 ti = qemu_get_clock_ns(vm_clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100740 if (last_clock != 0) {
741 delta = ti - last_clock;
742 if (delta < delta_min)
743 delta_min = delta;
744 if (delta > delta_max)
745 delta_max = delta;
746 delta_cum += delta;
747 if (++count == DISP_FREQ) {
748 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
749 muldiv64(delta_min, 1000000, get_ticks_per_sec()),
750 muldiv64(delta_max, 1000000, get_ticks_per_sec()),
751 muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()),
752 (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ));
753 count = 0;
754 delta_min = INT64_MAX;
755 delta_max = 0;
756 delta_cum = 0;
757 }
758 }
759 last_clock = ti;
760 }
761#endif
762 if (alarm_has_dynticks(t) ||
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100763 qemu_next_alarm_deadline () <= 0) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100764 t->expired = alarm_has_dynticks(t);
765 t->pending = 1;
766 qemu_notify_event();
767 }
768}
769
Paolo Bonzinicb842c92011-04-13 10:03:46 +0200770int64_t qemu_next_icount_deadline(void)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100771{
772 /* To avoid problems with overflow limit this to 2^32. */
773 int64_t delta = INT32_MAX;
774
Paolo Bonzinicb842c92011-04-13 10:03:46 +0200775 assert(use_icount);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100776 if (active_timers[QEMU_CLOCK_VIRTUAL]) {
777 delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
Paolo Bonzini9c132462011-02-03 14:48:59 +0100778 qemu_get_clock_ns(vm_clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100779 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100780
781 if (delta < 0)
782 delta = 0;
783
784 return delta;
785}
786
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100787static int64_t qemu_next_alarm_deadline(void)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100788{
789 int64_t delta;
790 int64_t rtdelta;
791
Paolo Bonzini6ad0a1e2011-02-03 14:49:00 +0100792 if (!use_icount && active_timers[QEMU_CLOCK_VIRTUAL]) {
793 delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
Paolo Bonzini74475452011-03-11 16:47:48 +0100794 qemu_get_clock_ns(vm_clock);
Paolo Bonzini6ad0a1e2011-02-03 14:49:00 +0100795 } else {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100796 delta = INT32_MAX;
Paolo Bonzini6ad0a1e2011-02-03 14:49:00 +0100797 }
798 if (active_timers[QEMU_CLOCK_HOST]) {
799 int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
800 qemu_get_clock_ns(host_clock);
801 if (hdelta < delta)
802 delta = hdelta;
803 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100804 if (active_timers[QEMU_CLOCK_REALTIME]) {
Paolo Bonzini4a998742011-03-11 16:33:58 +0100805 rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time -
Paolo Bonzini9c132462011-02-03 14:48:59 +0100806 qemu_get_clock_ns(rt_clock));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100807 if (rtdelta < delta)
808 delta = rtdelta;
809 }
810
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100811 return delta;
812}
813
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100814#if defined(__linux__)
815
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200816#include "compatfd.h"
817
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100818static int dynticks_start_timer(struct qemu_alarm_timer *t)
819{
820 struct sigevent ev;
821 timer_t host_timer;
822 struct sigaction act;
823
824 sigfillset(&act.sa_mask);
825 act.sa_flags = 0;
826 act.sa_handler = host_alarm_handler;
827
828 sigaction(SIGALRM, &act, NULL);
829
830 /*
831 * Initialize ev struct to 0 to avoid valgrind complaining
832 * about uninitialized data in timer_create call
833 */
834 memset(&ev, 0, sizeof(ev));
835 ev.sigev_value.sival_int = 0;
836 ev.sigev_notify = SIGEV_SIGNAL;
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200837#ifdef SIGEV_THREAD_ID
838 if (qemu_signalfd_available()) {
839 ev.sigev_notify = SIGEV_THREAD_ID;
840 ev._sigev_un._tid = qemu_get_thread_id();
841 }
842#endif /* SIGEV_THREAD_ID */
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100843 ev.sigev_signo = SIGALRM;
844
845 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
846 perror("timer_create");
847
848 /* disable dynticks */
849 fprintf(stderr, "Dynamic Ticks disabled\n");
850
851 return -1;
852 }
853
Stefan Weilcd0544e2011-04-10 20:15:09 +0200854 t->timer = host_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100855
856 return 0;
857}
858
859static void dynticks_stop_timer(struct qemu_alarm_timer *t)
860{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200861 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100862
863 timer_delete(host_timer);
864}
865
866static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
867{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200868 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100869 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100870 int64_t nearest_delta_ns = INT64_MAX;
871 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100872
873 assert(alarm_has_dynticks(t));
874 if (!active_timers[QEMU_CLOCK_REALTIME] &&
875 !active_timers[QEMU_CLOCK_VIRTUAL] &&
876 !active_timers[QEMU_CLOCK_HOST])
877 return;
878
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100879 nearest_delta_ns = qemu_next_alarm_deadline();
880 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
881 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100882
883 /* check whether a timer is already running */
884 if (timer_gettime(host_timer, &timeout)) {
885 perror("gettime");
886 fprintf(stderr, "Internal timer error: aborting\n");
887 exit(1);
888 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100889 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
890 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100891 return;
892
893 timeout.it_interval.tv_sec = 0;
894 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100895 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
896 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100897 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
898 perror("settime");
899 fprintf(stderr, "Internal timer error: aborting\n");
900 exit(1);
901 }
902}
903
904#endif /* defined(__linux__) */
905
Stefan Weilf26e5a52011-02-04 22:01:32 +0100906#if !defined(_WIN32)
907
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100908static int unix_start_timer(struct qemu_alarm_timer *t)
909{
910 struct sigaction act;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100911
912 /* timer signal */
913 sigfillset(&act.sa_mask);
914 act.sa_flags = 0;
915 act.sa_handler = host_alarm_handler;
916
917 sigaction(SIGALRM, &act, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200918 return 0;
919}
920
921static void unix_rearm_timer(struct qemu_alarm_timer *t)
922{
923 struct itimerval itv;
924 int64_t nearest_delta_ns = INT64_MAX;
925 int err;
926
927 assert(alarm_has_dynticks(t));
928 if (!active_timers[QEMU_CLOCK_REALTIME] &&
929 !active_timers[QEMU_CLOCK_VIRTUAL] &&
930 !active_timers[QEMU_CLOCK_HOST])
931 return;
932
933 nearest_delta_ns = qemu_next_alarm_deadline();
934 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
935 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100936
937 itv.it_interval.tv_sec = 0;
Paolo Bonzini84682832011-06-09 13:10:25 +0200938 itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */
939 itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
940 itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100941 err = setitimer(ITIMER_REAL, &itv, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200942 if (err) {
943 perror("setitimer");
944 fprintf(stderr, "Internal timer error: aborting\n");
945 exit(1);
946 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100947}
948
949static void unix_stop_timer(struct qemu_alarm_timer *t)
950{
951 struct itimerval itv;
952
953 memset(&itv, 0, sizeof(itv));
954 setitimer(ITIMER_REAL, &itv, NULL);
955}
956
957#endif /* !defined(_WIN32) */
958
959
960#ifdef _WIN32
961
Stefan Weil2f9cba02011-04-05 18:34:21 +0200962static MMRESULT mm_timer;
963static unsigned mm_period;
964
965static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
966 DWORD_PTR dwUser, DWORD_PTR dw1,
967 DWORD_PTR dw2)
968{
969 struct qemu_alarm_timer *t = alarm_timer;
970 if (!t) {
971 return;
972 }
973 if (alarm_has_dynticks(t) || qemu_next_alarm_deadline() <= 0) {
974 t->expired = alarm_has_dynticks(t);
975 t->pending = 1;
976 qemu_notify_event();
977 }
978}
979
980static int mm_start_timer(struct qemu_alarm_timer *t)
981{
982 TIMECAPS tc;
983 UINT flags;
984
985 memset(&tc, 0, sizeof(tc));
986 timeGetDevCaps(&tc, sizeof(tc));
987
988 mm_period = tc.wPeriodMin;
989 timeBeginPeriod(mm_period);
990
991 flags = TIME_CALLBACK_FUNCTION;
992 if (alarm_has_dynticks(t)) {
993 flags |= TIME_ONESHOT;
994 } else {
995 flags |= TIME_PERIODIC;
996 }
997
998 mm_timer = timeSetEvent(1, /* interval (ms) */
999 mm_period, /* resolution */
1000 mm_alarm_handler, /* function */
1001 (DWORD_PTR)t, /* parameter */
1002 flags);
1003
1004 if (!mm_timer) {
1005 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
1006 GetLastError());
1007 timeEndPeriod(mm_period);
1008 return -1;
1009 }
1010
1011 return 0;
1012}
1013
1014static void mm_stop_timer(struct qemu_alarm_timer *t)
1015{
1016 timeKillEvent(mm_timer);
1017 timeEndPeriod(mm_period);
1018}
1019
1020static void mm_rearm_timer(struct qemu_alarm_timer *t)
1021{
1022 int nearest_delta_ms;
1023
1024 assert(alarm_has_dynticks(t));
1025 if (!active_timers[QEMU_CLOCK_REALTIME] &&
1026 !active_timers[QEMU_CLOCK_VIRTUAL] &&
1027 !active_timers[QEMU_CLOCK_HOST]) {
1028 return;
1029 }
1030
1031 timeKillEvent(mm_timer);
1032
1033 nearest_delta_ms = (qemu_next_alarm_deadline() + 999999) / 1000000;
1034 if (nearest_delta_ms < 1) {
1035 nearest_delta_ms = 1;
1036 }
1037 mm_timer = timeSetEvent(nearest_delta_ms,
1038 mm_period,
1039 mm_alarm_handler,
1040 (DWORD_PTR)t,
1041 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
1042
1043 if (!mm_timer) {
1044 fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
1045 GetLastError());
1046
1047 timeEndPeriod(mm_period);
1048 exit(1);
1049 }
1050}
1051
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001052static int win32_start_timer(struct qemu_alarm_timer *t)
1053{
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001054 HANDLE hTimer;
1055 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001056
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001057 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
1058 is zero) that has already expired, the timer is not updated. Since
1059 creating a new timer is relatively expensive, set a bogus one-hour
1060 interval in the dynticks case. */
1061 success = CreateTimerQueueTimer(&hTimer,
1062 NULL,
1063 host_alarm_handler,
1064 t,
1065 1,
1066 alarm_has_dynticks(t) ? 3600000 : 1,
1067 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001068
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001069 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001070 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
1071 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001072 return -1;
1073 }
1074
Stefan Weilcd0544e2011-04-10 20:15:09 +02001075 t->timer = hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001076 return 0;
1077}
1078
1079static void win32_stop_timer(struct qemu_alarm_timer *t)
1080{
Stefan Weilcd0544e2011-04-10 20:15:09 +02001081 HANDLE hTimer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001082
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001083 if (hTimer) {
1084 DeleteTimerQueueTimer(NULL, hTimer, NULL);
1085 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001086}
1087
1088static void win32_rearm_timer(struct qemu_alarm_timer *t)
1089{
Stefan Weilcd0544e2011-04-10 20:15:09 +02001090 HANDLE hTimer = t->timer;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +01001091 int nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001092 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001093
1094 assert(alarm_has_dynticks(t));
1095 if (!active_timers[QEMU_CLOCK_REALTIME] &&
1096 !active_timers[QEMU_CLOCK_VIRTUAL] &&
1097 !active_timers[QEMU_CLOCK_HOST])
1098 return;
1099
Paolo Bonzinicfced5b2011-03-12 17:43:49 +01001100 nearest_delta_ms = (qemu_next_alarm_deadline() + 999999) / 1000000;
1101 if (nearest_delta_ms < 1) {
1102 nearest_delta_ms = 1;
1103 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001104 success = ChangeTimerQueueTimer(NULL,
1105 hTimer,
1106 nearest_delta_ms,
1107 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001108
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001109 if (!success) {
1110 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001111 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001112 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001113 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001114
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001115}
1116
1117#endif /* _WIN32 */
1118
1119static void alarm_timer_on_change_state_rearm(void *opaque, int running, int reason)
1120{
1121 if (running)
1122 qemu_rearm_alarm_timer((struct qemu_alarm_timer *) opaque);
1123}
1124
1125int init_timer_alarm(void)
1126{
1127 struct qemu_alarm_timer *t = NULL;
1128 int i, err = -1;
1129
1130 for (i = 0; alarm_timers[i].name; i++) {
1131 t = &alarm_timers[i];
1132
1133 err = t->start(t);
1134 if (!err)
1135 break;
1136 }
1137
1138 if (err) {
1139 err = -ENOENT;
1140 goto fail;
1141 }
1142
1143 /* first event is at time 0 */
1144 t->pending = 1;
1145 alarm_timer = t;
1146 qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm, t);
1147
1148 return 0;
1149
1150fail:
1151 return err;
1152}
1153
1154void quit_timers(void)
1155{
1156 struct qemu_alarm_timer *t = alarm_timer;
1157 alarm_timer = NULL;
1158 t->stop(t);
1159}
1160
1161int qemu_calculate_timeout(void)
1162{
Paolo Bonzini1ece93a2011-04-13 10:03:45 +02001163 return 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001164}
1165