blob: ebb50890e88c6e29695966992feb127b5f6abe3f [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 Bonzinidc2dfcf2011-09-12 15:50:16 +0200498int64_t qemu_clock_has_timers(QEMUClock *clock)
499{
500 return !!clock->active_timers;
501}
502
503int64_t qemu_clock_expired(QEMUClock *clock)
504{
505 return (clock->active_timers &&
506 clock->active_timers->expire_time < qemu_get_clock_ns(clock));
507}
508
509int64_t qemu_clock_deadline(QEMUClock *clock)
510{
511 /* To avoid problems with overflow limit this to 2^32. */
512 int64_t delta = INT32_MAX;
513
514 if (clock->active_timers) {
515 delta = clock->active_timers->expire_time - qemu_get_clock_ns(clock);
516 }
517 if (delta < 0) {
518 delta = 0;
519 }
520 return delta;
521}
522
Paolo Bonzini4a998742011-03-11 16:33:58 +0100523QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
524 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100525{
526 QEMUTimer *ts;
527
Anthony Liguori7267c092011-08-20 22:09:37 -0500528 ts = g_malloc0(sizeof(QEMUTimer));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100529 ts->clock = clock;
530 ts->cb = cb;
531 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100532 ts->scale = scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100533 return ts;
534}
535
536void qemu_free_timer(QEMUTimer *ts)
537{
Anthony Liguori7267c092011-08-20 22:09:37 -0500538 g_free(ts);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100539}
540
541/* stop a timer, but do not dealloc it */
542void qemu_del_timer(QEMUTimer *ts)
543{
544 QEMUTimer **pt, *t;
545
546 /* NOTE: this code must be signal safe because
547 qemu_timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200548 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100549 for(;;) {
550 t = *pt;
551 if (!t)
552 break;
553 if (t == ts) {
554 *pt = t->next;
555 break;
556 }
557 pt = &t->next;
558 }
559}
560
561/* modify the current timer so that it will be fired when current_time
562 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini4a998742011-03-11 16:33:58 +0100563static void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100564{
565 QEMUTimer **pt, *t;
566
567 qemu_del_timer(ts);
568
569 /* add the timer in the sorted list */
570 /* NOTE: this code must be signal safe because
571 qemu_timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200572 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100573 for(;;) {
574 t = *pt;
Stefan Weil45c7b372011-03-24 21:31:24 +0100575 if (!qemu_timer_expired_ns(t, expire_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 pt = &t->next;
579 }
580 ts->expire_time = expire_time;
581 ts->next = *pt;
582 *pt = ts;
583
584 /* Rearm if necessary */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200585 if (pt == &ts->clock->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100586 if (!alarm_timer->pending) {
587 qemu_rearm_alarm_timer(alarm_timer);
588 }
589 /* Interrupt execution to force deadline recalculation. */
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200590 qemu_clock_warp(ts->clock);
591 if (use_icount) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100592 qemu_notify_event();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200593 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100594 }
595}
596
Paolo Bonzini4a998742011-03-11 16:33:58 +0100597/* modify the current timer so that it will be fired when current_time
598 >= expire_time. The corresponding callback will be called. */
599void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
600{
601 qemu_mod_timer_ns(ts, expire_time * ts->scale);
602}
603
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100604int qemu_timer_pending(QEMUTimer *ts)
605{
606 QEMUTimer *t;
Paolo Bonzini688eb382011-09-13 11:42:26 +0200607 for (t = ts->clock->active_timers; t != NULL; t = t->next) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100608 if (t == ts)
609 return 1;
610 }
611 return 0;
612}
613
614int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
615{
Stefan Weil45c7b372011-03-24 21:31:24 +0100616 return qemu_timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100617}
618
619static void qemu_run_timers(QEMUClock *clock)
620{
621 QEMUTimer **ptimer_head, *ts;
622 int64_t current_time;
623
624 if (!clock->enabled)
625 return;
626
Paolo Bonzini4a998742011-03-11 16:33:58 +0100627 current_time = qemu_get_clock_ns(clock);
Paolo Bonzini688eb382011-09-13 11:42:26 +0200628 ptimer_head = &clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100629 for(;;) {
630 ts = *ptimer_head;
Stefan Weil45c7b372011-03-24 21:31:24 +0100631 if (!qemu_timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100632 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100633 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100634 /* remove timer from the list before calling the callback */
635 *ptimer_head = ts->next;
636 ts->next = NULL;
637
638 /* run the callback (the timer list can be modified) */
639 ts->cb(ts->opaque);
640 }
641}
642
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100643int64_t qemu_get_clock_ns(QEMUClock *clock)
644{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200645 int64_t now, last;
646
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100647 switch(clock->type) {
648 case QEMU_CLOCK_REALTIME:
649 return get_clock();
650 default:
651 case QEMU_CLOCK_VIRTUAL:
652 if (use_icount) {
653 return cpu_get_icount();
654 } else {
655 return cpu_get_clock();
656 }
657 case QEMU_CLOCK_HOST:
Jan Kiszka691a0c92011-06-20 14:06:27 +0200658 now = get_clock_realtime();
659 last = clock->last;
660 clock->last = now;
661 if (now < last) {
662 notifier_list_notify(&clock->reset_notifiers, &now);
663 }
664 return now;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100665 }
666}
667
Jan Kiszka691a0c92011-06-20 14:06:27 +0200668void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
669{
670 notifier_list_add(&clock->reset_notifiers, notifier);
671}
672
673void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
674{
675 notifier_list_remove(&clock->reset_notifiers, notifier);
676}
677
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100678void init_clocks(void)
679{
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100680 rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
681 vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
682 host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
683
684 rtc_clock = host_clock;
685}
686
687/* save a timer */
688void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
689{
690 uint64_t expire_time;
691
692 if (qemu_timer_pending(ts)) {
693 expire_time = ts->expire_time;
694 } else {
695 expire_time = -1;
696 }
697 qemu_put_be64(f, expire_time);
698}
699
700void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
701{
702 uint64_t expire_time;
703
704 expire_time = qemu_get_be64(f);
705 if (expire_time != -1) {
Paolo Bonzini4a998742011-03-11 16:33:58 +0100706 qemu_mod_timer_ns(ts, expire_time);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100707 } else {
708 qemu_del_timer(ts);
709 }
710}
711
712static const VMStateDescription vmstate_timers = {
713 .name = "timer",
714 .version_id = 2,
715 .minimum_version_id = 1,
716 .minimum_version_id_old = 1,
717 .fields = (VMStateField []) {
718 VMSTATE_INT64(cpu_ticks_offset, TimersState),
719 VMSTATE_INT64(dummy, TimersState),
720 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
721 VMSTATE_END_OF_LIST()
722 }
723};
724
725void configure_icount(const char *option)
726{
Alex Williamson0be71e32010-06-25 11:09:07 -0600727 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100728 if (!option)
729 return;
730
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200731 vm_clock->warp_timer = qemu_new_timer_ns(rt_clock, icount_warp_rt, NULL);
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200732
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100733 if (strcmp(option, "auto") != 0) {
734 icount_time_shift = strtol(option, NULL, 0);
735 use_icount = 1;
736 return;
737 }
738
739 use_icount = 2;
740
741 /* 125MIPS seems a reasonable initial guess at the guest speed.
742 It will be corrected fairly quickly anyway. */
743 icount_time_shift = 3;
744
745 /* Have both realtime and virtual time triggers for speed adjustment.
746 The realtime trigger catches emulated time passing too slowly,
747 the virtual time trigger catches emulated time passing too fast.
748 Realtime triggers occur even when idle, so use them less frequently
749 than VM triggers. */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100750 icount_rt_timer = qemu_new_timer_ms(rt_clock, icount_adjust_rt, NULL);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100751 qemu_mod_timer(icount_rt_timer,
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100752 qemu_get_clock_ms(rt_clock) + 1000);
Paolo Bonzini74475452011-03-11 16:47:48 +0100753 icount_vm_timer = qemu_new_timer_ns(vm_clock, icount_adjust_vm, NULL);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100754 qemu_mod_timer(icount_vm_timer,
Paolo Bonzini74475452011-03-11 16:47:48 +0100755 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100756}
757
758void qemu_run_all_timers(void)
759{
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100760 alarm_timer->pending = 0;
761
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100762 /* rearm timer, if not periodic */
763 if (alarm_timer->expired) {
764 alarm_timer->expired = 0;
765 qemu_rearm_alarm_timer(alarm_timer);
766 }
767
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100768 /* vm time timers */
Luiz Capitulino13548692011-07-29 15:36:43 -0300769 if (runstate_is_running()) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100770 qemu_run_timers(vm_clock);
771 }
772
773 qemu_run_timers(rt_clock);
774 qemu_run_timers(host_clock);
775}
776
777#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100778static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100779#else
780static void host_alarm_handler(int host_signum)
781#endif
782{
783 struct qemu_alarm_timer *t = alarm_timer;
784 if (!t)
785 return;
786
787#if 0
788#define DISP_FREQ 1000
789 {
790 static int64_t delta_min = INT64_MAX;
791 static int64_t delta_max, delta_cum, last_clock, delta, ti;
792 static int count;
Paolo Bonzini74475452011-03-11 16:47:48 +0100793 ti = qemu_get_clock_ns(vm_clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100794 if (last_clock != 0) {
795 delta = ti - last_clock;
796 if (delta < delta_min)
797 delta_min = delta;
798 if (delta > delta_max)
799 delta_max = delta;
800 delta_cum += delta;
801 if (++count == DISP_FREQ) {
802 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
803 muldiv64(delta_min, 1000000, get_ticks_per_sec()),
804 muldiv64(delta_max, 1000000, get_ticks_per_sec()),
805 muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()),
806 (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ));
807 count = 0;
808 delta_min = INT64_MAX;
809 delta_max = 0;
810 delta_cum = 0;
811 }
812 }
813 last_clock = ti;
814 }
815#endif
816 if (alarm_has_dynticks(t) ||
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100817 qemu_next_alarm_deadline () <= 0) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100818 t->expired = alarm_has_dynticks(t);
819 t->pending = 1;
820 qemu_notify_event();
821 }
822}
823
Paolo Bonzinicb842c92011-04-13 10:03:46 +0200824int64_t qemu_next_icount_deadline(void)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100825{
826 /* To avoid problems with overflow limit this to 2^32. */
827 int64_t delta = INT32_MAX;
828
Paolo Bonzinicb842c92011-04-13 10:03:46 +0200829 assert(use_icount);
Paolo Bonzini688eb382011-09-13 11:42:26 +0200830 if (vm_clock->active_timers) {
831 delta = vm_clock->active_timers->expire_time -
Paolo Bonzini9c132462011-02-03 14:48:59 +0100832 qemu_get_clock_ns(vm_clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100833 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100834
835 if (delta < 0)
836 delta = 0;
837
838 return delta;
839}
840
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100841#if defined(__linux__)
842
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200843#include "compatfd.h"
844
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100845static int dynticks_start_timer(struct qemu_alarm_timer *t)
846{
847 struct sigevent ev;
848 timer_t host_timer;
849 struct sigaction act;
850
851 sigfillset(&act.sa_mask);
852 act.sa_flags = 0;
853 act.sa_handler = host_alarm_handler;
854
855 sigaction(SIGALRM, &act, NULL);
856
857 /*
858 * Initialize ev struct to 0 to avoid valgrind complaining
859 * about uninitialized data in timer_create call
860 */
861 memset(&ev, 0, sizeof(ev));
862 ev.sigev_value.sival_int = 0;
863 ev.sigev_notify = SIGEV_SIGNAL;
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200864#ifdef SIGEV_THREAD_ID
865 if (qemu_signalfd_available()) {
866 ev.sigev_notify = SIGEV_THREAD_ID;
867 ev._sigev_un._tid = qemu_get_thread_id();
868 }
869#endif /* SIGEV_THREAD_ID */
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100870 ev.sigev_signo = SIGALRM;
871
872 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
873 perror("timer_create");
874
875 /* disable dynticks */
876 fprintf(stderr, "Dynamic Ticks disabled\n");
877
878 return -1;
879 }
880
Stefan Weilcd0544e2011-04-10 20:15:09 +0200881 t->timer = host_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100882
883 return 0;
884}
885
886static void dynticks_stop_timer(struct qemu_alarm_timer *t)
887{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200888 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100889
890 timer_delete(host_timer);
891}
892
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100893static void dynticks_rearm_timer(struct qemu_alarm_timer *t,
894 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100895{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200896 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100897 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100898 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100899
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100900 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
901 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100902
903 /* check whether a timer is already running */
904 if (timer_gettime(host_timer, &timeout)) {
905 perror("gettime");
906 fprintf(stderr, "Internal timer error: aborting\n");
907 exit(1);
908 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100909 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
910 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100911 return;
912
913 timeout.it_interval.tv_sec = 0;
914 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100915 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
916 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100917 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
918 perror("settime");
919 fprintf(stderr, "Internal timer error: aborting\n");
920 exit(1);
921 }
922}
923
924#endif /* defined(__linux__) */
925
Stefan Weilf26e5a52011-02-04 22:01:32 +0100926#if !defined(_WIN32)
927
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100928static int unix_start_timer(struct qemu_alarm_timer *t)
929{
930 struct sigaction act;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100931
932 /* timer signal */
933 sigfillset(&act.sa_mask);
934 act.sa_flags = 0;
935 act.sa_handler = host_alarm_handler;
936
937 sigaction(SIGALRM, &act, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200938 return 0;
939}
940
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100941static void unix_rearm_timer(struct qemu_alarm_timer *t,
942 int64_t nearest_delta_ns)
Paolo Bonzini84682832011-06-09 13:10:25 +0200943{
944 struct itimerval itv;
Paolo Bonzini84682832011-06-09 13:10:25 +0200945 int err;
946
Paolo Bonzini84682832011-06-09 13:10:25 +0200947 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
948 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100949
950 itv.it_interval.tv_sec = 0;
Paolo Bonzini84682832011-06-09 13:10:25 +0200951 itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */
952 itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
953 itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100954 err = setitimer(ITIMER_REAL, &itv, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200955 if (err) {
956 perror("setitimer");
957 fprintf(stderr, "Internal timer error: aborting\n");
958 exit(1);
959 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100960}
961
962static void unix_stop_timer(struct qemu_alarm_timer *t)
963{
964 struct itimerval itv;
965
966 memset(&itv, 0, sizeof(itv));
967 setitimer(ITIMER_REAL, &itv, NULL);
968}
969
970#endif /* !defined(_WIN32) */
971
972
973#ifdef _WIN32
974
Stefan Weil2f9cba02011-04-05 18:34:21 +0200975static MMRESULT mm_timer;
976static unsigned mm_period;
977
978static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
979 DWORD_PTR dwUser, DWORD_PTR dw1,
980 DWORD_PTR dw2)
981{
982 struct qemu_alarm_timer *t = alarm_timer;
983 if (!t) {
984 return;
985 }
986 if (alarm_has_dynticks(t) || qemu_next_alarm_deadline() <= 0) {
987 t->expired = alarm_has_dynticks(t);
988 t->pending = 1;
989 qemu_notify_event();
990 }
991}
992
993static int mm_start_timer(struct qemu_alarm_timer *t)
994{
995 TIMECAPS tc;
996 UINT flags;
997
998 memset(&tc, 0, sizeof(tc));
999 timeGetDevCaps(&tc, sizeof(tc));
1000
1001 mm_period = tc.wPeriodMin;
1002 timeBeginPeriod(mm_period);
1003
1004 flags = TIME_CALLBACK_FUNCTION;
1005 if (alarm_has_dynticks(t)) {
1006 flags |= TIME_ONESHOT;
1007 } else {
1008 flags |= TIME_PERIODIC;
1009 }
1010
1011 mm_timer = timeSetEvent(1, /* interval (ms) */
1012 mm_period, /* resolution */
1013 mm_alarm_handler, /* function */
1014 (DWORD_PTR)t, /* parameter */
1015 flags);
1016
1017 if (!mm_timer) {
1018 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
1019 GetLastError());
1020 timeEndPeriod(mm_period);
1021 return -1;
1022 }
1023
1024 return 0;
1025}
1026
1027static void mm_stop_timer(struct qemu_alarm_timer *t)
1028{
1029 timeKillEvent(mm_timer);
1030 timeEndPeriod(mm_period);
1031}
1032
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +01001033static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta)
Stefan Weil2f9cba02011-04-05 18:34:21 +02001034{
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +01001035 int nearest_delta_ms = (delta + 999999) / 1000000;
Stefan Weil2f9cba02011-04-05 18:34:21 +02001036 if (nearest_delta_ms < 1) {
1037 nearest_delta_ms = 1;
1038 }
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +01001039
1040 timeKillEvent(mm_timer);
Stefan Weil2f9cba02011-04-05 18:34:21 +02001041 mm_timer = timeSetEvent(nearest_delta_ms,
1042 mm_period,
1043 mm_alarm_handler,
1044 (DWORD_PTR)t,
1045 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
1046
1047 if (!mm_timer) {
1048 fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
1049 GetLastError());
1050
1051 timeEndPeriod(mm_period);
1052 exit(1);
1053 }
1054}
1055
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001056static int win32_start_timer(struct qemu_alarm_timer *t)
1057{
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001058 HANDLE hTimer;
1059 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001060
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001061 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
1062 is zero) that has already expired, the timer is not updated. Since
1063 creating a new timer is relatively expensive, set a bogus one-hour
1064 interval in the dynticks case. */
1065 success = CreateTimerQueueTimer(&hTimer,
1066 NULL,
1067 host_alarm_handler,
1068 t,
1069 1,
1070 alarm_has_dynticks(t) ? 3600000 : 1,
1071 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001072
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001073 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001074 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
1075 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001076 return -1;
1077 }
1078
Stefan Weilcd0544e2011-04-10 20:15:09 +02001079 t->timer = hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001080 return 0;
1081}
1082
1083static void win32_stop_timer(struct qemu_alarm_timer *t)
1084{
Stefan Weilcd0544e2011-04-10 20:15:09 +02001085 HANDLE hTimer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001086
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001087 if (hTimer) {
1088 DeleteTimerQueueTimer(NULL, hTimer, NULL);
1089 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001090}
1091
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +01001092static void win32_rearm_timer(struct qemu_alarm_timer *t,
1093 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001094{
Stefan Weilcd0544e2011-04-10 20:15:09 +02001095 HANDLE hTimer = t->timer;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +01001096 int nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001097 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001098
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +01001099 nearest_delta_ms = (nearest_delta_ns + 999999) / 1000000;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +01001100 if (nearest_delta_ms < 1) {
1101 nearest_delta_ms = 1;
1102 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001103 success = ChangeTimerQueueTimer(NULL,
1104 hTimer,
1105 nearest_delta_ms,
1106 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001107
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001108 if (!success) {
1109 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001110 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001111 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001112 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001113
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001114}
1115
1116#endif /* _WIN32 */
1117
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001118static void alarm_timer_on_change_state_rearm(void *opaque, int running,
1119 RunState state)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001120{
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