blob: e2551f32985a73b40b976c23beaa7965992b2d58 [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;
Paolo Bonzini688eb382011-09-13 11:42:26 +0200137 QEMUTimer *active_timers;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200138
139 NotifierList reset_notifiers;
140 int64_t last;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100141};
142
143struct QEMUTimer {
144 QEMUClock *clock;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100145 int64_t expire_time; /* in nanoseconds */
146 int scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100147 QEMUTimerCB *cb;
148 void *opaque;
149 struct QEMUTimer *next;
150};
151
152struct qemu_alarm_timer {
153 char const *name;
154 int (*start)(struct qemu_alarm_timer *t);
155 void (*stop)(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100156 void (*rearm)(struct qemu_alarm_timer *t, int64_t nearest_delta_ns);
Stefan Weilcd0544e2011-04-10 20:15:09 +0200157#if defined(__linux__)
158 int fd;
159 timer_t timer;
160#elif defined(_WIN32)
161 HANDLE timer;
162#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100163 char expired;
164 char pending;
165};
166
167static struct qemu_alarm_timer *alarm_timer;
168
Stefan Weil45c7b372011-03-24 21:31:24 +0100169static bool qemu_timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
170{
171 return timer_head && (timer_head->expire_time <= current_time);
172}
173
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100174int qemu_alarm_pending(void)
175{
176 return alarm_timer->pending;
177}
178
179static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
180{
181 return !!t->rearm;
182}
183
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100184static int64_t qemu_next_alarm_deadline(void)
185{
186 int64_t delta;
187 int64_t rtdelta;
188
189 if (!use_icount && vm_clock->active_timers) {
190 delta = vm_clock->active_timers->expire_time -
191 qemu_get_clock_ns(vm_clock);
192 } else {
193 delta = INT32_MAX;
194 }
195 if (host_clock->active_timers) {
196 int64_t hdelta = host_clock->active_timers->expire_time -
197 qemu_get_clock_ns(host_clock);
198 if (hdelta < delta) {
199 delta = hdelta;
200 }
201 }
202 if (rt_clock->active_timers) {
203 rtdelta = (rt_clock->active_timers->expire_time -
204 qemu_get_clock_ns(rt_clock));
205 if (rtdelta < delta) {
206 delta = rtdelta;
207 }
208 }
209
210 return delta;
211}
212
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100213static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
214{
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100215 int64_t nearest_delta_ns;
216 assert(alarm_has_dynticks(t));
217 if (!rt_clock->active_timers &&
218 !vm_clock->active_timers &&
219 !host_clock->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100220 return;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100221 }
222 nearest_delta_ns = qemu_next_alarm_deadline();
223 t->rearm(t, nearest_delta_ns);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100224}
225
Paolo Bonzini9c132462011-02-03 14:48:59 +0100226/* TODO: MIN_TIMER_REARM_NS should be optimized */
227#define MIN_TIMER_REARM_NS 250000
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100228
229#ifdef _WIN32
230
Stefan Weil2f9cba02011-04-05 18:34:21 +0200231static int mm_start_timer(struct qemu_alarm_timer *t);
232static void mm_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100233static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200234
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100235static int win32_start_timer(struct qemu_alarm_timer *t);
236static void win32_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100237static void win32_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100238
239#else
240
241static int unix_start_timer(struct qemu_alarm_timer *t);
242static void unix_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100243static void unix_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100244
245#ifdef __linux__
246
247static int dynticks_start_timer(struct qemu_alarm_timer *t);
248static void dynticks_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100249static void dynticks_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100250
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100251#endif /* __linux__ */
252
253#endif /* _WIN32 */
254
255/* Correlation between real and virtual time is always going to be
256 fairly approximate, so ignore small variation.
257 When the guest is idle real and virtual time will be aligned in
258 the IO wait loop. */
259#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
260
261static void icount_adjust(void)
262{
263 int64_t cur_time;
264 int64_t cur_icount;
265 int64_t delta;
266 static int64_t last_delta;
267 /* If the VM is not running, then do nothing. */
Luiz Capitulino13548692011-07-29 15:36:43 -0300268 if (!runstate_is_running())
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100269 return;
270
271 cur_time = cpu_get_clock();
Paolo Bonzini74475452011-03-11 16:47:48 +0100272 cur_icount = qemu_get_clock_ns(vm_clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100273 delta = cur_icount - cur_time;
274 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
275 if (delta > 0
276 && last_delta + ICOUNT_WOBBLE < delta * 2
277 && icount_time_shift > 0) {
278 /* The guest is getting too far ahead. Slow time down. */
279 icount_time_shift--;
280 }
281 if (delta < 0
282 && last_delta - ICOUNT_WOBBLE > delta * 2
283 && icount_time_shift < MAX_ICOUNT_SHIFT) {
284 /* The guest is getting too far behind. Speed time up. */
285 icount_time_shift++;
286 }
287 last_delta = delta;
288 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
289}
290
291static void icount_adjust_rt(void * opaque)
292{
293 qemu_mod_timer(icount_rt_timer,
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100294 qemu_get_clock_ms(rt_clock) + 1000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100295 icount_adjust();
296}
297
298static void icount_adjust_vm(void * opaque)
299{
300 qemu_mod_timer(icount_vm_timer,
Paolo Bonzini74475452011-03-11 16:47:48 +0100301 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100302 icount_adjust();
303}
304
305int64_t qemu_icount_round(int64_t count)
306{
307 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
308}
309
310static struct qemu_alarm_timer alarm_timers[] = {
311#ifndef _WIN32
312#ifdef __linux__
313 {"dynticks", dynticks_start_timer,
Stefan Weilcd0544e2011-04-10 20:15:09 +0200314 dynticks_stop_timer, dynticks_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100315#endif
Paolo Bonzini84682832011-06-09 13:10:25 +0200316 {"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100317#else
Stefan Weil2f9cba02011-04-05 18:34:21 +0200318 {"mmtimer", mm_start_timer, mm_stop_timer, NULL},
319 {"mmtimer2", mm_start_timer, mm_stop_timer, mm_rearm_timer},
Stefan Weilcd0544e2011-04-10 20:15:09 +0200320 {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer},
321 {"win32", win32_start_timer, win32_stop_timer, NULL},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100322#endif
323 {NULL, }
324};
325
326static void show_available_alarms(void)
327{
328 int i;
329
330 printf("Available alarm timers, in order of precedence:\n");
331 for (i = 0; alarm_timers[i].name; i++)
332 printf("%s\n", alarm_timers[i].name);
333}
334
335void configure_alarms(char const *opt)
336{
337 int i;
338 int cur = 0;
339 int count = ARRAY_SIZE(alarm_timers) - 1;
340 char *arg;
341 char *name;
342 struct qemu_alarm_timer tmp;
343
344 if (!strcmp(opt, "?")) {
345 show_available_alarms();
346 exit(0);
347 }
348
Anthony Liguori7267c092011-08-20 22:09:37 -0500349 arg = g_strdup(opt);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100350
351 /* Reorder the array */
352 name = strtok(arg, ",");
353 while (name) {
354 for (i = 0; i < count && alarm_timers[i].name; i++) {
355 if (!strcmp(alarm_timers[i].name, name))
356 break;
357 }
358
359 if (i == count) {
360 fprintf(stderr, "Unknown clock %s\n", name);
361 goto next;
362 }
363
364 if (i < cur)
365 /* Ignore */
366 goto next;
367
368 /* Swap */
369 tmp = alarm_timers[i];
370 alarm_timers[i] = alarm_timers[cur];
371 alarm_timers[cur] = tmp;
372
373 cur++;
374next:
375 name = strtok(NULL, ",");
376 }
377
Anthony Liguori7267c092011-08-20 22:09:37 -0500378 g_free(arg);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100379
380 if (cur) {
381 /* Disable remaining timers */
382 for (i = cur; i < count; i++)
383 alarm_timers[i].name = NULL;
384 } else {
385 show_available_alarms();
386 exit(1);
387 }
388}
389
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100390QEMUClock *rt_clock;
391QEMUClock *vm_clock;
392QEMUClock *host_clock;
393
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100394static QEMUClock *qemu_new_clock(int type)
395{
396 QEMUClock *clock;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200397
Anthony Liguori7267c092011-08-20 22:09:37 -0500398 clock = g_malloc0(sizeof(QEMUClock));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100399 clock->type = type;
400 clock->enabled = 1;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200401 notifier_list_init(&clock->reset_notifiers);
402 /* required to detect & report backward jumps */
403 if (type == QEMU_CLOCK_HOST) {
404 clock->last = get_clock_realtime();
405 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100406 return clock;
407}
408
409void qemu_clock_enable(QEMUClock *clock, int enabled)
410{
411 clock->enabled = enabled;
412}
413
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200414static int64_t vm_clock_warp_start;
415
416static void icount_warp_rt(void *opaque)
417{
418 if (vm_clock_warp_start == -1) {
419 return;
420 }
421
Luiz Capitulino13548692011-07-29 15:36:43 -0300422 if (runstate_is_running()) {
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200423 int64_t clock = qemu_get_clock_ns(rt_clock);
424 int64_t warp_delta = clock - vm_clock_warp_start;
425 if (use_icount == 1) {
426 qemu_icount_bias += warp_delta;
427 } else {
428 /*
429 * In adaptive mode, do not let the vm_clock run too
430 * far ahead of real time.
431 */
432 int64_t cur_time = cpu_get_clock();
433 int64_t cur_icount = qemu_get_clock_ns(vm_clock);
434 int64_t delta = cur_time - cur_icount;
435 qemu_icount_bias += MIN(warp_delta, delta);
436 }
Paolo Bonzini688eb382011-09-13 11:42:26 +0200437 if (qemu_timer_expired(vm_clock->active_timers,
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200438 qemu_get_clock_ns(vm_clock))) {
439 qemu_notify_event();
440 }
441 }
442 vm_clock_warp_start = -1;
443}
444
445void qemu_clock_warp(QEMUClock *clock)
446{
447 int64_t deadline;
448
449 if (!clock->warp_timer) {
450 return;
451 }
452
453 /*
454 * There are too many global variables to make the "warp" behavior
455 * applicable to other clocks. But a clock argument removes the
456 * need for if statements all over the place.
457 */
458 assert(clock == vm_clock);
459
460 /*
461 * If the CPUs have been sleeping, advance the vm_clock timer now. This
462 * ensures that the deadline for the timer is computed correctly below.
463 * This also makes sure that the insn counter is synchronized before the
464 * CPU starts running, in case the CPU is woken by an event other than
465 * the earliest vm_clock timer.
466 */
467 icount_warp_rt(NULL);
Paolo Bonzini688eb382011-09-13 11:42:26 +0200468 if (!all_cpu_threads_idle() || !clock->active_timers) {
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200469 qemu_del_timer(clock->warp_timer);
470 return;
471 }
472
473 vm_clock_warp_start = qemu_get_clock_ns(rt_clock);
Paolo Bonzinicb842c92011-04-13 10:03:46 +0200474 deadline = qemu_next_icount_deadline();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200475 if (deadline > 0) {
476 /*
477 * Ensure the vm_clock proceeds even when the virtual CPU goes to
478 * sleep. Otherwise, the CPU might be waiting for a future timer
479 * interrupt to wake it up, but the interrupt never comes because
480 * the vCPU isn't running any insns and thus doesn't advance the
481 * vm_clock.
482 *
483 * An extreme solution for this problem would be to never let VCPUs
484 * sleep in icount mode if there is a pending vm_clock timer; rather
485 * time could just advance to the next vm_clock event. Instead, we
486 * do stop VCPUs and only advance vm_clock after some "real" time,
487 * (related to the time left until the next event) has passed. This
488 * rt_clock timer will do this. This avoids that the warps are too
489 * visible externally---for example, you will not be sending network
490 * packets continously instead of every 100ms.
491 */
492 qemu_mod_timer(clock->warp_timer, vm_clock_warp_start + deadline);
493 } else {
494 qemu_notify_event();
495 }
496}
497
Paolo Bonzini4a998742011-03-11 16:33:58 +0100498QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
499 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100500{
501 QEMUTimer *ts;
502
Anthony Liguori7267c092011-08-20 22:09:37 -0500503 ts = g_malloc0(sizeof(QEMUTimer));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100504 ts->clock = clock;
505 ts->cb = cb;
506 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100507 ts->scale = scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100508 return ts;
509}
510
511void qemu_free_timer(QEMUTimer *ts)
512{
Anthony Liguori7267c092011-08-20 22:09:37 -0500513 g_free(ts);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100514}
515
516/* stop a timer, but do not dealloc it */
517void qemu_del_timer(QEMUTimer *ts)
518{
519 QEMUTimer **pt, *t;
520
521 /* NOTE: this code must be signal safe because
522 qemu_timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200523 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100524 for(;;) {
525 t = *pt;
526 if (!t)
527 break;
528 if (t == ts) {
529 *pt = t->next;
530 break;
531 }
532 pt = &t->next;
533 }
534}
535
536/* modify the current timer so that it will be fired when current_time
537 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini4a998742011-03-11 16:33:58 +0100538static void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100539{
540 QEMUTimer **pt, *t;
541
542 qemu_del_timer(ts);
543
544 /* add the timer in the sorted list */
545 /* NOTE: this code must be signal safe because
546 qemu_timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200547 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100548 for(;;) {
549 t = *pt;
Stefan Weil45c7b372011-03-24 21:31:24 +0100550 if (!qemu_timer_expired_ns(t, expire_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100551 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100552 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100553 pt = &t->next;
554 }
555 ts->expire_time = expire_time;
556 ts->next = *pt;
557 *pt = ts;
558
559 /* Rearm if necessary */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200560 if (pt == &ts->clock->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100561 if (!alarm_timer->pending) {
562 qemu_rearm_alarm_timer(alarm_timer);
563 }
564 /* Interrupt execution to force deadline recalculation. */
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200565 qemu_clock_warp(ts->clock);
566 if (use_icount) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100567 qemu_notify_event();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200568 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100569 }
570}
571
Paolo Bonzini4a998742011-03-11 16:33:58 +0100572/* modify the current timer so that it will be fired when current_time
573 >= expire_time. The corresponding callback will be called. */
574void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
575{
576 qemu_mod_timer_ns(ts, expire_time * ts->scale);
577}
578
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100579int qemu_timer_pending(QEMUTimer *ts)
580{
581 QEMUTimer *t;
Paolo Bonzini688eb382011-09-13 11:42:26 +0200582 for (t = ts->clock->active_timers; t != NULL; t = t->next) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100583 if (t == ts)
584 return 1;
585 }
586 return 0;
587}
588
589int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
590{
Stefan Weil45c7b372011-03-24 21:31:24 +0100591 return qemu_timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100592}
593
594static void qemu_run_timers(QEMUClock *clock)
595{
596 QEMUTimer **ptimer_head, *ts;
597 int64_t current_time;
598
599 if (!clock->enabled)
600 return;
601
Paolo Bonzini4a998742011-03-11 16:33:58 +0100602 current_time = qemu_get_clock_ns(clock);
Paolo Bonzini688eb382011-09-13 11:42:26 +0200603 ptimer_head = &clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100604 for(;;) {
605 ts = *ptimer_head;
Stefan Weil45c7b372011-03-24 21:31:24 +0100606 if (!qemu_timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100607 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100608 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100609 /* remove timer from the list before calling the callback */
610 *ptimer_head = ts->next;
611 ts->next = NULL;
612
613 /* run the callback (the timer list can be modified) */
614 ts->cb(ts->opaque);
615 }
616}
617
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100618int64_t qemu_get_clock_ns(QEMUClock *clock)
619{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200620 int64_t now, last;
621
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100622 switch(clock->type) {
623 case QEMU_CLOCK_REALTIME:
624 return get_clock();
625 default:
626 case QEMU_CLOCK_VIRTUAL:
627 if (use_icount) {
628 return cpu_get_icount();
629 } else {
630 return cpu_get_clock();
631 }
632 case QEMU_CLOCK_HOST:
Jan Kiszka691a0c92011-06-20 14:06:27 +0200633 now = get_clock_realtime();
634 last = clock->last;
635 clock->last = now;
636 if (now < last) {
637 notifier_list_notify(&clock->reset_notifiers, &now);
638 }
639 return now;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100640 }
641}
642
Jan Kiszka691a0c92011-06-20 14:06:27 +0200643void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
644{
645 notifier_list_add(&clock->reset_notifiers, notifier);
646}
647
648void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
649{
650 notifier_list_remove(&clock->reset_notifiers, notifier);
651}
652
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100653void init_clocks(void)
654{
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100655 rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
656 vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
657 host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
658
659 rtc_clock = host_clock;
660}
661
662/* save a timer */
663void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
664{
665 uint64_t expire_time;
666
667 if (qemu_timer_pending(ts)) {
668 expire_time = ts->expire_time;
669 } else {
670 expire_time = -1;
671 }
672 qemu_put_be64(f, expire_time);
673}
674
675void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
676{
677 uint64_t expire_time;
678
679 expire_time = qemu_get_be64(f);
680 if (expire_time != -1) {
Paolo Bonzini4a998742011-03-11 16:33:58 +0100681 qemu_mod_timer_ns(ts, expire_time);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100682 } else {
683 qemu_del_timer(ts);
684 }
685}
686
687static const VMStateDescription vmstate_timers = {
688 .name = "timer",
689 .version_id = 2,
690 .minimum_version_id = 1,
691 .minimum_version_id_old = 1,
692 .fields = (VMStateField []) {
693 VMSTATE_INT64(cpu_ticks_offset, TimersState),
694 VMSTATE_INT64(dummy, TimersState),
695 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
696 VMSTATE_END_OF_LIST()
697 }
698};
699
700void configure_icount(const char *option)
701{
Alex Williamson0be71e32010-06-25 11:09:07 -0600702 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100703 if (!option)
704 return;
705
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200706 vm_clock->warp_timer = qemu_new_timer_ns(rt_clock, icount_warp_rt, NULL);
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200707
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100708 if (strcmp(option, "auto") != 0) {
709 icount_time_shift = strtol(option, NULL, 0);
710 use_icount = 1;
711 return;
712 }
713
714 use_icount = 2;
715
716 /* 125MIPS seems a reasonable initial guess at the guest speed.
717 It will be corrected fairly quickly anyway. */
718 icount_time_shift = 3;
719
720 /* Have both realtime and virtual time triggers for speed adjustment.
721 The realtime trigger catches emulated time passing too slowly,
722 the virtual time trigger catches emulated time passing too fast.
723 Realtime triggers occur even when idle, so use them less frequently
724 than VM triggers. */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100725 icount_rt_timer = qemu_new_timer_ms(rt_clock, icount_adjust_rt, NULL);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100726 qemu_mod_timer(icount_rt_timer,
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100727 qemu_get_clock_ms(rt_clock) + 1000);
Paolo Bonzini74475452011-03-11 16:47:48 +0100728 icount_vm_timer = qemu_new_timer_ns(vm_clock, icount_adjust_vm, NULL);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100729 qemu_mod_timer(icount_vm_timer,
Paolo Bonzini74475452011-03-11 16:47:48 +0100730 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100731}
732
733void qemu_run_all_timers(void)
734{
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100735 alarm_timer->pending = 0;
736
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100737 /* rearm timer, if not periodic */
738 if (alarm_timer->expired) {
739 alarm_timer->expired = 0;
740 qemu_rearm_alarm_timer(alarm_timer);
741 }
742
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100743 /* vm time timers */
Luiz Capitulino13548692011-07-29 15:36:43 -0300744 if (runstate_is_running()) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100745 qemu_run_timers(vm_clock);
746 }
747
748 qemu_run_timers(rt_clock);
749 qemu_run_timers(host_clock);
750}
751
752#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100753static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100754#else
755static void host_alarm_handler(int host_signum)
756#endif
757{
758 struct qemu_alarm_timer *t = alarm_timer;
759 if (!t)
760 return;
761
762#if 0
763#define DISP_FREQ 1000
764 {
765 static int64_t delta_min = INT64_MAX;
766 static int64_t delta_max, delta_cum, last_clock, delta, ti;
767 static int count;
Paolo Bonzini74475452011-03-11 16:47:48 +0100768 ti = qemu_get_clock_ns(vm_clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100769 if (last_clock != 0) {
770 delta = ti - last_clock;
771 if (delta < delta_min)
772 delta_min = delta;
773 if (delta > delta_max)
774 delta_max = delta;
775 delta_cum += delta;
776 if (++count == DISP_FREQ) {
777 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
778 muldiv64(delta_min, 1000000, get_ticks_per_sec()),
779 muldiv64(delta_max, 1000000, get_ticks_per_sec()),
780 muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()),
781 (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ));
782 count = 0;
783 delta_min = INT64_MAX;
784 delta_max = 0;
785 delta_cum = 0;
786 }
787 }
788 last_clock = ti;
789 }
790#endif
791 if (alarm_has_dynticks(t) ||
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100792 qemu_next_alarm_deadline () <= 0) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100793 t->expired = alarm_has_dynticks(t);
794 t->pending = 1;
795 qemu_notify_event();
796 }
797}
798
Paolo Bonzinicb842c92011-04-13 10:03:46 +0200799int64_t qemu_next_icount_deadline(void)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100800{
801 /* To avoid problems with overflow limit this to 2^32. */
802 int64_t delta = INT32_MAX;
803
Paolo Bonzinicb842c92011-04-13 10:03:46 +0200804 assert(use_icount);
Paolo Bonzini688eb382011-09-13 11:42:26 +0200805 if (vm_clock->active_timers) {
806 delta = vm_clock->active_timers->expire_time -
Paolo Bonzini9c132462011-02-03 14:48:59 +0100807 qemu_get_clock_ns(vm_clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100808 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100809
810 if (delta < 0)
811 delta = 0;
812
813 return delta;
814}
815
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100816#if defined(__linux__)
817
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200818#include "compatfd.h"
819
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100820static int dynticks_start_timer(struct qemu_alarm_timer *t)
821{
822 struct sigevent ev;
823 timer_t host_timer;
824 struct sigaction act;
825
826 sigfillset(&act.sa_mask);
827 act.sa_flags = 0;
828 act.sa_handler = host_alarm_handler;
829
830 sigaction(SIGALRM, &act, NULL);
831
832 /*
833 * Initialize ev struct to 0 to avoid valgrind complaining
834 * about uninitialized data in timer_create call
835 */
836 memset(&ev, 0, sizeof(ev));
837 ev.sigev_value.sival_int = 0;
838 ev.sigev_notify = SIGEV_SIGNAL;
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200839#ifdef SIGEV_THREAD_ID
840 if (qemu_signalfd_available()) {
841 ev.sigev_notify = SIGEV_THREAD_ID;
842 ev._sigev_un._tid = qemu_get_thread_id();
843 }
844#endif /* SIGEV_THREAD_ID */
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100845 ev.sigev_signo = SIGALRM;
846
847 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
848 perror("timer_create");
849
850 /* disable dynticks */
851 fprintf(stderr, "Dynamic Ticks disabled\n");
852
853 return -1;
854 }
855
Stefan Weilcd0544e2011-04-10 20:15:09 +0200856 t->timer = host_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100857
858 return 0;
859}
860
861static void dynticks_stop_timer(struct qemu_alarm_timer *t)
862{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200863 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100864
865 timer_delete(host_timer);
866}
867
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100868static void dynticks_rearm_timer(struct qemu_alarm_timer *t,
869 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100870{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200871 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100872 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100873 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100874
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100875 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
876 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100877
878 /* check whether a timer is already running */
879 if (timer_gettime(host_timer, &timeout)) {
880 perror("gettime");
881 fprintf(stderr, "Internal timer error: aborting\n");
882 exit(1);
883 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100884 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
885 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100886 return;
887
888 timeout.it_interval.tv_sec = 0;
889 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100890 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
891 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100892 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
893 perror("settime");
894 fprintf(stderr, "Internal timer error: aborting\n");
895 exit(1);
896 }
897}
898
899#endif /* defined(__linux__) */
900
Stefan Weilf26e5a52011-02-04 22:01:32 +0100901#if !defined(_WIN32)
902
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100903static int unix_start_timer(struct qemu_alarm_timer *t)
904{
905 struct sigaction act;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100906
907 /* timer signal */
908 sigfillset(&act.sa_mask);
909 act.sa_flags = 0;
910 act.sa_handler = host_alarm_handler;
911
912 sigaction(SIGALRM, &act, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200913 return 0;
914}
915
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100916static void unix_rearm_timer(struct qemu_alarm_timer *t,
917 int64_t nearest_delta_ns)
Paolo Bonzini84682832011-06-09 13:10:25 +0200918{
919 struct itimerval itv;
Paolo Bonzini84682832011-06-09 13:10:25 +0200920 int err;
921
Paolo Bonzini84682832011-06-09 13:10:25 +0200922 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
923 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100924
925 itv.it_interval.tv_sec = 0;
Paolo Bonzini84682832011-06-09 13:10:25 +0200926 itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */
927 itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
928 itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100929 err = setitimer(ITIMER_REAL, &itv, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200930 if (err) {
931 perror("setitimer");
932 fprintf(stderr, "Internal timer error: aborting\n");
933 exit(1);
934 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100935}
936
937static void unix_stop_timer(struct qemu_alarm_timer *t)
938{
939 struct itimerval itv;
940
941 memset(&itv, 0, sizeof(itv));
942 setitimer(ITIMER_REAL, &itv, NULL);
943}
944
945#endif /* !defined(_WIN32) */
946
947
948#ifdef _WIN32
949
Stefan Weil2f9cba02011-04-05 18:34:21 +0200950static MMRESULT mm_timer;
951static unsigned mm_period;
952
953static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
954 DWORD_PTR dwUser, DWORD_PTR dw1,
955 DWORD_PTR dw2)
956{
957 struct qemu_alarm_timer *t = alarm_timer;
958 if (!t) {
959 return;
960 }
961 if (alarm_has_dynticks(t) || qemu_next_alarm_deadline() <= 0) {
962 t->expired = alarm_has_dynticks(t);
963 t->pending = 1;
964 qemu_notify_event();
965 }
966}
967
968static int mm_start_timer(struct qemu_alarm_timer *t)
969{
970 TIMECAPS tc;
971 UINT flags;
972
973 memset(&tc, 0, sizeof(tc));
974 timeGetDevCaps(&tc, sizeof(tc));
975
976 mm_period = tc.wPeriodMin;
977 timeBeginPeriod(mm_period);
978
979 flags = TIME_CALLBACK_FUNCTION;
980 if (alarm_has_dynticks(t)) {
981 flags |= TIME_ONESHOT;
982 } else {
983 flags |= TIME_PERIODIC;
984 }
985
986 mm_timer = timeSetEvent(1, /* interval (ms) */
987 mm_period, /* resolution */
988 mm_alarm_handler, /* function */
989 (DWORD_PTR)t, /* parameter */
990 flags);
991
992 if (!mm_timer) {
993 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
994 GetLastError());
995 timeEndPeriod(mm_period);
996 return -1;
997 }
998
999 return 0;
1000}
1001
1002static void mm_stop_timer(struct qemu_alarm_timer *t)
1003{
1004 timeKillEvent(mm_timer);
1005 timeEndPeriod(mm_period);
1006}
1007
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +01001008static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta)
Stefan Weil2f9cba02011-04-05 18:34:21 +02001009{
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +01001010 int nearest_delta_ms = (delta + 999999) / 1000000;
Stefan Weil2f9cba02011-04-05 18:34:21 +02001011 if (nearest_delta_ms < 1) {
1012 nearest_delta_ms = 1;
1013 }
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +01001014
1015 timeKillEvent(mm_timer);
Stefan Weil2f9cba02011-04-05 18:34:21 +02001016 mm_timer = timeSetEvent(nearest_delta_ms,
1017 mm_period,
1018 mm_alarm_handler,
1019 (DWORD_PTR)t,
1020 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
1021
1022 if (!mm_timer) {
1023 fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
1024 GetLastError());
1025
1026 timeEndPeriod(mm_period);
1027 exit(1);
1028 }
1029}
1030
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001031static int win32_start_timer(struct qemu_alarm_timer *t)
1032{
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001033 HANDLE hTimer;
1034 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001035
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001036 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
1037 is zero) that has already expired, the timer is not updated. Since
1038 creating a new timer is relatively expensive, set a bogus one-hour
1039 interval in the dynticks case. */
1040 success = CreateTimerQueueTimer(&hTimer,
1041 NULL,
1042 host_alarm_handler,
1043 t,
1044 1,
1045 alarm_has_dynticks(t) ? 3600000 : 1,
1046 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001047
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001048 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001049 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
1050 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001051 return -1;
1052 }
1053
Stefan Weilcd0544e2011-04-10 20:15:09 +02001054 t->timer = hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001055 return 0;
1056}
1057
1058static void win32_stop_timer(struct qemu_alarm_timer *t)
1059{
Stefan Weilcd0544e2011-04-10 20:15:09 +02001060 HANDLE hTimer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001061
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001062 if (hTimer) {
1063 DeleteTimerQueueTimer(NULL, hTimer, NULL);
1064 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001065}
1066
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +01001067static void win32_rearm_timer(struct qemu_alarm_timer *t,
1068 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001069{
Stefan Weilcd0544e2011-04-10 20:15:09 +02001070 HANDLE hTimer = t->timer;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +01001071 int nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001072 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001073
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +01001074 nearest_delta_ms = (nearest_delta_ns + 999999) / 1000000;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +01001075 if (nearest_delta_ms < 1) {
1076 nearest_delta_ms = 1;
1077 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001078 success = ChangeTimerQueueTimer(NULL,
1079 hTimer,
1080 nearest_delta_ms,
1081 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001082
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001083 if (!success) {
1084 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001085 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001086 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001087 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001088
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001089}
1090
1091#endif /* _WIN32 */
1092
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001093static void alarm_timer_on_change_state_rearm(void *opaque, int running,
1094 RunState state)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001095{
1096 if (running)
1097 qemu_rearm_alarm_timer((struct qemu_alarm_timer *) opaque);
1098}
1099
1100int init_timer_alarm(void)
1101{
1102 struct qemu_alarm_timer *t = NULL;
1103 int i, err = -1;
1104
1105 for (i = 0; alarm_timers[i].name; i++) {
1106 t = &alarm_timers[i];
1107
1108 err = t->start(t);
1109 if (!err)
1110 break;
1111 }
1112
1113 if (err) {
1114 err = -ENOENT;
1115 goto fail;
1116 }
1117
1118 /* first event is at time 0 */
1119 t->pending = 1;
1120 alarm_timer = t;
1121 qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm, t);
1122
1123 return 0;
1124
1125fail:
1126 return err;
1127}
1128
1129void quit_timers(void)
1130{
1131 struct qemu_alarm_timer *t = alarm_timer;
1132 alarm_timer = NULL;
1133 t->stop(t);
1134}
1135
1136int qemu_calculate_timeout(void)
1137{
Paolo Bonzini1ece93a2011-04-13 10:03:45 +02001138 return 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001139}
1140