blob: acf7a154221c8e8d0a65115426a0c639163fdaa3 [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);
156 void (*rearm)(struct qemu_alarm_timer *t);
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
184static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
185{
186 if (!alarm_has_dynticks(t))
187 return;
188
189 t->rearm(t);
190}
191
Paolo Bonzini9c132462011-02-03 14:48:59 +0100192/* TODO: MIN_TIMER_REARM_NS should be optimized */
193#define MIN_TIMER_REARM_NS 250000
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100194
195#ifdef _WIN32
196
Stefan Weil2f9cba02011-04-05 18:34:21 +0200197static int mm_start_timer(struct qemu_alarm_timer *t);
198static void mm_stop_timer(struct qemu_alarm_timer *t);
199static void mm_rearm_timer(struct qemu_alarm_timer *t);
200
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100201static int win32_start_timer(struct qemu_alarm_timer *t);
202static void win32_stop_timer(struct qemu_alarm_timer *t);
203static void win32_rearm_timer(struct qemu_alarm_timer *t);
204
205#else
206
207static int unix_start_timer(struct qemu_alarm_timer *t);
208static void unix_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzini84682832011-06-09 13:10:25 +0200209static void unix_rearm_timer(struct qemu_alarm_timer *t);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100210
211#ifdef __linux__
212
213static int dynticks_start_timer(struct qemu_alarm_timer *t);
214static void dynticks_stop_timer(struct qemu_alarm_timer *t);
215static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
216
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100217#endif /* __linux__ */
218
219#endif /* _WIN32 */
220
221/* Correlation between real and virtual time is always going to be
222 fairly approximate, so ignore small variation.
223 When the guest is idle real and virtual time will be aligned in
224 the IO wait loop. */
225#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
226
227static void icount_adjust(void)
228{
229 int64_t cur_time;
230 int64_t cur_icount;
231 int64_t delta;
232 static int64_t last_delta;
233 /* If the VM is not running, then do nothing. */
Luiz Capitulino13548692011-07-29 15:36:43 -0300234 if (!runstate_is_running())
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100235 return;
236
237 cur_time = cpu_get_clock();
Paolo Bonzini74475452011-03-11 16:47:48 +0100238 cur_icount = qemu_get_clock_ns(vm_clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100239 delta = cur_icount - cur_time;
240 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
241 if (delta > 0
242 && last_delta + ICOUNT_WOBBLE < delta * 2
243 && icount_time_shift > 0) {
244 /* The guest is getting too far ahead. Slow time down. */
245 icount_time_shift--;
246 }
247 if (delta < 0
248 && last_delta - ICOUNT_WOBBLE > delta * 2
249 && icount_time_shift < MAX_ICOUNT_SHIFT) {
250 /* The guest is getting too far behind. Speed time up. */
251 icount_time_shift++;
252 }
253 last_delta = delta;
254 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
255}
256
257static void icount_adjust_rt(void * opaque)
258{
259 qemu_mod_timer(icount_rt_timer,
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100260 qemu_get_clock_ms(rt_clock) + 1000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100261 icount_adjust();
262}
263
264static void icount_adjust_vm(void * opaque)
265{
266 qemu_mod_timer(icount_vm_timer,
Paolo Bonzini74475452011-03-11 16:47:48 +0100267 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100268 icount_adjust();
269}
270
271int64_t qemu_icount_round(int64_t count)
272{
273 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
274}
275
276static struct qemu_alarm_timer alarm_timers[] = {
277#ifndef _WIN32
278#ifdef __linux__
279 {"dynticks", dynticks_start_timer,
Stefan Weilcd0544e2011-04-10 20:15:09 +0200280 dynticks_stop_timer, dynticks_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100281#endif
Paolo Bonzini84682832011-06-09 13:10:25 +0200282 {"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100283#else
Stefan Weil2f9cba02011-04-05 18:34:21 +0200284 {"mmtimer", mm_start_timer, mm_stop_timer, NULL},
285 {"mmtimer2", mm_start_timer, mm_stop_timer, mm_rearm_timer},
Stefan Weilcd0544e2011-04-10 20:15:09 +0200286 {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer},
287 {"win32", win32_start_timer, win32_stop_timer, NULL},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100288#endif
289 {NULL, }
290};
291
292static void show_available_alarms(void)
293{
294 int i;
295
296 printf("Available alarm timers, in order of precedence:\n");
297 for (i = 0; alarm_timers[i].name; i++)
298 printf("%s\n", alarm_timers[i].name);
299}
300
301void configure_alarms(char const *opt)
302{
303 int i;
304 int cur = 0;
305 int count = ARRAY_SIZE(alarm_timers) - 1;
306 char *arg;
307 char *name;
308 struct qemu_alarm_timer tmp;
309
310 if (!strcmp(opt, "?")) {
311 show_available_alarms();
312 exit(0);
313 }
314
Anthony Liguori7267c092011-08-20 22:09:37 -0500315 arg = g_strdup(opt);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100316
317 /* Reorder the array */
318 name = strtok(arg, ",");
319 while (name) {
320 for (i = 0; i < count && alarm_timers[i].name; i++) {
321 if (!strcmp(alarm_timers[i].name, name))
322 break;
323 }
324
325 if (i == count) {
326 fprintf(stderr, "Unknown clock %s\n", name);
327 goto next;
328 }
329
330 if (i < cur)
331 /* Ignore */
332 goto next;
333
334 /* Swap */
335 tmp = alarm_timers[i];
336 alarm_timers[i] = alarm_timers[cur];
337 alarm_timers[cur] = tmp;
338
339 cur++;
340next:
341 name = strtok(NULL, ",");
342 }
343
Anthony Liguori7267c092011-08-20 22:09:37 -0500344 g_free(arg);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100345
346 if (cur) {
347 /* Disable remaining timers */
348 for (i = cur; i < count; i++)
349 alarm_timers[i].name = NULL;
350 } else {
351 show_available_alarms();
352 exit(1);
353 }
354}
355
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100356QEMUClock *rt_clock;
357QEMUClock *vm_clock;
358QEMUClock *host_clock;
359
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100360static QEMUClock *qemu_new_clock(int type)
361{
362 QEMUClock *clock;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200363
Anthony Liguori7267c092011-08-20 22:09:37 -0500364 clock = g_malloc0(sizeof(QEMUClock));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100365 clock->type = type;
366 clock->enabled = 1;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200367 notifier_list_init(&clock->reset_notifiers);
368 /* required to detect & report backward jumps */
369 if (type == QEMU_CLOCK_HOST) {
370 clock->last = get_clock_realtime();
371 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100372 return clock;
373}
374
375void qemu_clock_enable(QEMUClock *clock, int enabled)
376{
377 clock->enabled = enabled;
378}
379
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200380static int64_t vm_clock_warp_start;
381
382static void icount_warp_rt(void *opaque)
383{
384 if (vm_clock_warp_start == -1) {
385 return;
386 }
387
Luiz Capitulino13548692011-07-29 15:36:43 -0300388 if (runstate_is_running()) {
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200389 int64_t clock = qemu_get_clock_ns(rt_clock);
390 int64_t warp_delta = clock - vm_clock_warp_start;
391 if (use_icount == 1) {
392 qemu_icount_bias += warp_delta;
393 } else {
394 /*
395 * In adaptive mode, do not let the vm_clock run too
396 * far ahead of real time.
397 */
398 int64_t cur_time = cpu_get_clock();
399 int64_t cur_icount = qemu_get_clock_ns(vm_clock);
400 int64_t delta = cur_time - cur_icount;
401 qemu_icount_bias += MIN(warp_delta, delta);
402 }
Paolo Bonzini688eb382011-09-13 11:42:26 +0200403 if (qemu_timer_expired(vm_clock->active_timers,
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200404 qemu_get_clock_ns(vm_clock))) {
405 qemu_notify_event();
406 }
407 }
408 vm_clock_warp_start = -1;
409}
410
411void qemu_clock_warp(QEMUClock *clock)
412{
413 int64_t deadline;
414
415 if (!clock->warp_timer) {
416 return;
417 }
418
419 /*
420 * There are too many global variables to make the "warp" behavior
421 * applicable to other clocks. But a clock argument removes the
422 * need for if statements all over the place.
423 */
424 assert(clock == vm_clock);
425
426 /*
427 * If the CPUs have been sleeping, advance the vm_clock timer now. This
428 * ensures that the deadline for the timer is computed correctly below.
429 * This also makes sure that the insn counter is synchronized before the
430 * CPU starts running, in case the CPU is woken by an event other than
431 * the earliest vm_clock timer.
432 */
433 icount_warp_rt(NULL);
Paolo Bonzini688eb382011-09-13 11:42:26 +0200434 if (!all_cpu_threads_idle() || !clock->active_timers) {
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200435 qemu_del_timer(clock->warp_timer);
436 return;
437 }
438
439 vm_clock_warp_start = qemu_get_clock_ns(rt_clock);
Paolo Bonzinicb842c92011-04-13 10:03:46 +0200440 deadline = qemu_next_icount_deadline();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200441 if (deadline > 0) {
442 /*
443 * Ensure the vm_clock proceeds even when the virtual CPU goes to
444 * sleep. Otherwise, the CPU might be waiting for a future timer
445 * interrupt to wake it up, but the interrupt never comes because
446 * the vCPU isn't running any insns and thus doesn't advance the
447 * vm_clock.
448 *
449 * An extreme solution for this problem would be to never let VCPUs
450 * sleep in icount mode if there is a pending vm_clock timer; rather
451 * time could just advance to the next vm_clock event. Instead, we
452 * do stop VCPUs and only advance vm_clock after some "real" time,
453 * (related to the time left until the next event) has passed. This
454 * rt_clock timer will do this. This avoids that the warps are too
455 * visible externally---for example, you will not be sending network
456 * packets continously instead of every 100ms.
457 */
458 qemu_mod_timer(clock->warp_timer, vm_clock_warp_start + deadline);
459 } else {
460 qemu_notify_event();
461 }
462}
463
Paolo Bonzini4a998742011-03-11 16:33:58 +0100464QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
465 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100466{
467 QEMUTimer *ts;
468
Anthony Liguori7267c092011-08-20 22:09:37 -0500469 ts = g_malloc0(sizeof(QEMUTimer));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100470 ts->clock = clock;
471 ts->cb = cb;
472 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100473 ts->scale = scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100474 return ts;
475}
476
477void qemu_free_timer(QEMUTimer *ts)
478{
Anthony Liguori7267c092011-08-20 22:09:37 -0500479 g_free(ts);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100480}
481
482/* stop a timer, but do not dealloc it */
483void qemu_del_timer(QEMUTimer *ts)
484{
485 QEMUTimer **pt, *t;
486
487 /* NOTE: this code must be signal safe because
488 qemu_timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200489 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100490 for(;;) {
491 t = *pt;
492 if (!t)
493 break;
494 if (t == ts) {
495 *pt = t->next;
496 break;
497 }
498 pt = &t->next;
499 }
500}
501
502/* modify the current timer so that it will be fired when current_time
503 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini4a998742011-03-11 16:33:58 +0100504static void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100505{
506 QEMUTimer **pt, *t;
507
508 qemu_del_timer(ts);
509
510 /* add the timer in the sorted list */
511 /* NOTE: this code must be signal safe because
512 qemu_timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200513 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100514 for(;;) {
515 t = *pt;
Stefan Weil45c7b372011-03-24 21:31:24 +0100516 if (!qemu_timer_expired_ns(t, expire_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100517 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100518 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100519 pt = &t->next;
520 }
521 ts->expire_time = expire_time;
522 ts->next = *pt;
523 *pt = ts;
524
525 /* Rearm if necessary */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200526 if (pt == &ts->clock->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100527 if (!alarm_timer->pending) {
528 qemu_rearm_alarm_timer(alarm_timer);
529 }
530 /* Interrupt execution to force deadline recalculation. */
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200531 qemu_clock_warp(ts->clock);
532 if (use_icount) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100533 qemu_notify_event();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200534 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100535 }
536}
537
Paolo Bonzini4a998742011-03-11 16:33:58 +0100538/* modify the current timer so that it will be fired when current_time
539 >= expire_time. The corresponding callback will be called. */
540void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
541{
542 qemu_mod_timer_ns(ts, expire_time * ts->scale);
543}
544
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100545int qemu_timer_pending(QEMUTimer *ts)
546{
547 QEMUTimer *t;
Paolo Bonzini688eb382011-09-13 11:42:26 +0200548 for (t = ts->clock->active_timers; t != NULL; t = t->next) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100549 if (t == ts)
550 return 1;
551 }
552 return 0;
553}
554
555int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
556{
Stefan Weil45c7b372011-03-24 21:31:24 +0100557 return qemu_timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100558}
559
560static void qemu_run_timers(QEMUClock *clock)
561{
562 QEMUTimer **ptimer_head, *ts;
563 int64_t current_time;
564
565 if (!clock->enabled)
566 return;
567
Paolo Bonzini4a998742011-03-11 16:33:58 +0100568 current_time = qemu_get_clock_ns(clock);
Paolo Bonzini688eb382011-09-13 11:42:26 +0200569 ptimer_head = &clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100570 for(;;) {
571 ts = *ptimer_head;
Stefan Weil45c7b372011-03-24 21:31:24 +0100572 if (!qemu_timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100573 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100574 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100575 /* remove timer from the list before calling the callback */
576 *ptimer_head = ts->next;
577 ts->next = NULL;
578
579 /* run the callback (the timer list can be modified) */
580 ts->cb(ts->opaque);
581 }
582}
583
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100584int64_t qemu_get_clock_ns(QEMUClock *clock)
585{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200586 int64_t now, last;
587
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100588 switch(clock->type) {
589 case QEMU_CLOCK_REALTIME:
590 return get_clock();
591 default:
592 case QEMU_CLOCK_VIRTUAL:
593 if (use_icount) {
594 return cpu_get_icount();
595 } else {
596 return cpu_get_clock();
597 }
598 case QEMU_CLOCK_HOST:
Jan Kiszka691a0c92011-06-20 14:06:27 +0200599 now = get_clock_realtime();
600 last = clock->last;
601 clock->last = now;
602 if (now < last) {
603 notifier_list_notify(&clock->reset_notifiers, &now);
604 }
605 return now;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100606 }
607}
608
Jan Kiszka691a0c92011-06-20 14:06:27 +0200609void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
610{
611 notifier_list_add(&clock->reset_notifiers, notifier);
612}
613
614void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
615{
616 notifier_list_remove(&clock->reset_notifiers, notifier);
617}
618
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100619void init_clocks(void)
620{
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100621 rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
622 vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
623 host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
624
625 rtc_clock = host_clock;
626}
627
628/* save a timer */
629void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
630{
631 uint64_t expire_time;
632
633 if (qemu_timer_pending(ts)) {
634 expire_time = ts->expire_time;
635 } else {
636 expire_time = -1;
637 }
638 qemu_put_be64(f, expire_time);
639}
640
641void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
642{
643 uint64_t expire_time;
644
645 expire_time = qemu_get_be64(f);
646 if (expire_time != -1) {
Paolo Bonzini4a998742011-03-11 16:33:58 +0100647 qemu_mod_timer_ns(ts, expire_time);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100648 } else {
649 qemu_del_timer(ts);
650 }
651}
652
653static const VMStateDescription vmstate_timers = {
654 .name = "timer",
655 .version_id = 2,
656 .minimum_version_id = 1,
657 .minimum_version_id_old = 1,
658 .fields = (VMStateField []) {
659 VMSTATE_INT64(cpu_ticks_offset, TimersState),
660 VMSTATE_INT64(dummy, TimersState),
661 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
662 VMSTATE_END_OF_LIST()
663 }
664};
665
666void configure_icount(const char *option)
667{
Alex Williamson0be71e32010-06-25 11:09:07 -0600668 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100669 if (!option)
670 return;
671
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200672 vm_clock->warp_timer = qemu_new_timer_ns(rt_clock, icount_warp_rt, NULL);
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200673
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100674 if (strcmp(option, "auto") != 0) {
675 icount_time_shift = strtol(option, NULL, 0);
676 use_icount = 1;
677 return;
678 }
679
680 use_icount = 2;
681
682 /* 125MIPS seems a reasonable initial guess at the guest speed.
683 It will be corrected fairly quickly anyway. */
684 icount_time_shift = 3;
685
686 /* Have both realtime and virtual time triggers for speed adjustment.
687 The realtime trigger catches emulated time passing too slowly,
688 the virtual time trigger catches emulated time passing too fast.
689 Realtime triggers occur even when idle, so use them less frequently
690 than VM triggers. */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100691 icount_rt_timer = qemu_new_timer_ms(rt_clock, icount_adjust_rt, NULL);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100692 qemu_mod_timer(icount_rt_timer,
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100693 qemu_get_clock_ms(rt_clock) + 1000);
Paolo Bonzini74475452011-03-11 16:47:48 +0100694 icount_vm_timer = qemu_new_timer_ns(vm_clock, icount_adjust_vm, NULL);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100695 qemu_mod_timer(icount_vm_timer,
Paolo Bonzini74475452011-03-11 16:47:48 +0100696 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100697}
698
699void qemu_run_all_timers(void)
700{
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100701 alarm_timer->pending = 0;
702
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100703 /* rearm timer, if not periodic */
704 if (alarm_timer->expired) {
705 alarm_timer->expired = 0;
706 qemu_rearm_alarm_timer(alarm_timer);
707 }
708
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100709 /* vm time timers */
Luiz Capitulino13548692011-07-29 15:36:43 -0300710 if (runstate_is_running()) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100711 qemu_run_timers(vm_clock);
712 }
713
714 qemu_run_timers(rt_clock);
715 qemu_run_timers(host_clock);
716}
717
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100718static int64_t qemu_next_alarm_deadline(void);
719
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100720#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100721static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100722#else
723static void host_alarm_handler(int host_signum)
724#endif
725{
726 struct qemu_alarm_timer *t = alarm_timer;
727 if (!t)
728 return;
729
730#if 0
731#define DISP_FREQ 1000
732 {
733 static int64_t delta_min = INT64_MAX;
734 static int64_t delta_max, delta_cum, last_clock, delta, ti;
735 static int count;
Paolo Bonzini74475452011-03-11 16:47:48 +0100736 ti = qemu_get_clock_ns(vm_clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100737 if (last_clock != 0) {
738 delta = ti - last_clock;
739 if (delta < delta_min)
740 delta_min = delta;
741 if (delta > delta_max)
742 delta_max = delta;
743 delta_cum += delta;
744 if (++count == DISP_FREQ) {
745 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
746 muldiv64(delta_min, 1000000, get_ticks_per_sec()),
747 muldiv64(delta_max, 1000000, get_ticks_per_sec()),
748 muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()),
749 (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ));
750 count = 0;
751 delta_min = INT64_MAX;
752 delta_max = 0;
753 delta_cum = 0;
754 }
755 }
756 last_clock = ti;
757 }
758#endif
759 if (alarm_has_dynticks(t) ||
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100760 qemu_next_alarm_deadline () <= 0) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100761 t->expired = alarm_has_dynticks(t);
762 t->pending = 1;
763 qemu_notify_event();
764 }
765}
766
Paolo Bonzinicb842c92011-04-13 10:03:46 +0200767int64_t qemu_next_icount_deadline(void)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100768{
769 /* To avoid problems with overflow limit this to 2^32. */
770 int64_t delta = INT32_MAX;
771
Paolo Bonzinicb842c92011-04-13 10:03:46 +0200772 assert(use_icount);
Paolo Bonzini688eb382011-09-13 11:42:26 +0200773 if (vm_clock->active_timers) {
774 delta = vm_clock->active_timers->expire_time -
Paolo Bonzini9c132462011-02-03 14:48:59 +0100775 qemu_get_clock_ns(vm_clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100776 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100777
778 if (delta < 0)
779 delta = 0;
780
781 return delta;
782}
783
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100784static int64_t qemu_next_alarm_deadline(void)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100785{
786 int64_t delta;
787 int64_t rtdelta;
788
Paolo Bonzini688eb382011-09-13 11:42:26 +0200789 if (!use_icount && vm_clock->active_timers) {
790 delta = vm_clock->active_timers->expire_time -
Paolo Bonzini74475452011-03-11 16:47:48 +0100791 qemu_get_clock_ns(vm_clock);
Paolo Bonzini6ad0a1e2011-02-03 14:49:00 +0100792 } else {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100793 delta = INT32_MAX;
Paolo Bonzini6ad0a1e2011-02-03 14:49:00 +0100794 }
Paolo Bonzini688eb382011-09-13 11:42:26 +0200795 if (host_clock->active_timers) {
796 int64_t hdelta = host_clock->active_timers->expire_time -
Paolo Bonzini6ad0a1e2011-02-03 14:49:00 +0100797 qemu_get_clock_ns(host_clock);
798 if (hdelta < delta)
799 delta = hdelta;
800 }
Paolo Bonzini688eb382011-09-13 11:42:26 +0200801 if (rt_clock->active_timers) {
802 rtdelta = (rt_clock->active_timers->expire_time -
Paolo Bonzini9c132462011-02-03 14:48:59 +0100803 qemu_get_clock_ns(rt_clock));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100804 if (rtdelta < delta)
805 delta = rtdelta;
806 }
807
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100808 return delta;
809}
810
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100811#if defined(__linux__)
812
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200813#include "compatfd.h"
814
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100815static int dynticks_start_timer(struct qemu_alarm_timer *t)
816{
817 struct sigevent ev;
818 timer_t host_timer;
819 struct sigaction act;
820
821 sigfillset(&act.sa_mask);
822 act.sa_flags = 0;
823 act.sa_handler = host_alarm_handler;
824
825 sigaction(SIGALRM, &act, NULL);
826
827 /*
828 * Initialize ev struct to 0 to avoid valgrind complaining
829 * about uninitialized data in timer_create call
830 */
831 memset(&ev, 0, sizeof(ev));
832 ev.sigev_value.sival_int = 0;
833 ev.sigev_notify = SIGEV_SIGNAL;
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200834#ifdef SIGEV_THREAD_ID
835 if (qemu_signalfd_available()) {
836 ev.sigev_notify = SIGEV_THREAD_ID;
837 ev._sigev_un._tid = qemu_get_thread_id();
838 }
839#endif /* SIGEV_THREAD_ID */
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100840 ev.sigev_signo = SIGALRM;
841
842 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
843 perror("timer_create");
844
845 /* disable dynticks */
846 fprintf(stderr, "Dynamic Ticks disabled\n");
847
848 return -1;
849 }
850
Stefan Weilcd0544e2011-04-10 20:15:09 +0200851 t->timer = host_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100852
853 return 0;
854}
855
856static void dynticks_stop_timer(struct qemu_alarm_timer *t)
857{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200858 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100859
860 timer_delete(host_timer);
861}
862
863static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
864{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200865 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100866 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100867 int64_t nearest_delta_ns = INT64_MAX;
868 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100869
870 assert(alarm_has_dynticks(t));
Paolo Bonzini688eb382011-09-13 11:42:26 +0200871 if (!rt_clock->active_timers &&
872 !vm_clock->active_timers &&
873 !host_clock->active_timers)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100874 return;
875
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100876 nearest_delta_ns = qemu_next_alarm_deadline();
877 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
878 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100879
880 /* check whether a timer is already running */
881 if (timer_gettime(host_timer, &timeout)) {
882 perror("gettime");
883 fprintf(stderr, "Internal timer error: aborting\n");
884 exit(1);
885 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100886 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
887 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100888 return;
889
890 timeout.it_interval.tv_sec = 0;
891 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100892 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
893 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100894 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
895 perror("settime");
896 fprintf(stderr, "Internal timer error: aborting\n");
897 exit(1);
898 }
899}
900
901#endif /* defined(__linux__) */
902
Stefan Weilf26e5a52011-02-04 22:01:32 +0100903#if !defined(_WIN32)
904
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100905static int unix_start_timer(struct qemu_alarm_timer *t)
906{
907 struct sigaction act;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100908
909 /* timer signal */
910 sigfillset(&act.sa_mask);
911 act.sa_flags = 0;
912 act.sa_handler = host_alarm_handler;
913
914 sigaction(SIGALRM, &act, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200915 return 0;
916}
917
918static void unix_rearm_timer(struct qemu_alarm_timer *t)
919{
920 struct itimerval itv;
921 int64_t nearest_delta_ns = INT64_MAX;
922 int err;
923
924 assert(alarm_has_dynticks(t));
Paolo Bonzini688eb382011-09-13 11:42:26 +0200925 if (!rt_clock->active_timers &&
926 !vm_clock->active_timers &&
927 !host_clock->active_timers)
Paolo Bonzini84682832011-06-09 13:10:25 +0200928 return;
929
930 nearest_delta_ns = qemu_next_alarm_deadline();
931 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
932 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100933
934 itv.it_interval.tv_sec = 0;
Paolo Bonzini84682832011-06-09 13:10:25 +0200935 itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */
936 itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
937 itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100938 err = setitimer(ITIMER_REAL, &itv, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200939 if (err) {
940 perror("setitimer");
941 fprintf(stderr, "Internal timer error: aborting\n");
942 exit(1);
943 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100944}
945
946static void unix_stop_timer(struct qemu_alarm_timer *t)
947{
948 struct itimerval itv;
949
950 memset(&itv, 0, sizeof(itv));
951 setitimer(ITIMER_REAL, &itv, NULL);
952}
953
954#endif /* !defined(_WIN32) */
955
956
957#ifdef _WIN32
958
Stefan Weil2f9cba02011-04-05 18:34:21 +0200959static MMRESULT mm_timer;
960static unsigned mm_period;
961
962static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
963 DWORD_PTR dwUser, DWORD_PTR dw1,
964 DWORD_PTR dw2)
965{
966 struct qemu_alarm_timer *t = alarm_timer;
967 if (!t) {
968 return;
969 }
970 if (alarm_has_dynticks(t) || qemu_next_alarm_deadline() <= 0) {
971 t->expired = alarm_has_dynticks(t);
972 t->pending = 1;
973 qemu_notify_event();
974 }
975}
976
977static int mm_start_timer(struct qemu_alarm_timer *t)
978{
979 TIMECAPS tc;
980 UINT flags;
981
982 memset(&tc, 0, sizeof(tc));
983 timeGetDevCaps(&tc, sizeof(tc));
984
985 mm_period = tc.wPeriodMin;
986 timeBeginPeriod(mm_period);
987
988 flags = TIME_CALLBACK_FUNCTION;
989 if (alarm_has_dynticks(t)) {
990 flags |= TIME_ONESHOT;
991 } else {
992 flags |= TIME_PERIODIC;
993 }
994
995 mm_timer = timeSetEvent(1, /* interval (ms) */
996 mm_period, /* resolution */
997 mm_alarm_handler, /* function */
998 (DWORD_PTR)t, /* parameter */
999 flags);
1000
1001 if (!mm_timer) {
1002 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
1003 GetLastError());
1004 timeEndPeriod(mm_period);
1005 return -1;
1006 }
1007
1008 return 0;
1009}
1010
1011static void mm_stop_timer(struct qemu_alarm_timer *t)
1012{
1013 timeKillEvent(mm_timer);
1014 timeEndPeriod(mm_period);
1015}
1016
1017static void mm_rearm_timer(struct qemu_alarm_timer *t)
1018{
1019 int nearest_delta_ms;
1020
1021 assert(alarm_has_dynticks(t));
Paolo Bonzini688eb382011-09-13 11:42:26 +02001022 if (!rt_clock->active_timers &&
1023 !vm_clock->active_timers &&
1024 !host_clock->active_timers) {
Stefan Weil2f9cba02011-04-05 18:34:21 +02001025 return;
1026 }
1027
1028 timeKillEvent(mm_timer);
1029
1030 nearest_delta_ms = (qemu_next_alarm_deadline() + 999999) / 1000000;
1031 if (nearest_delta_ms < 1) {
1032 nearest_delta_ms = 1;
1033 }
1034 mm_timer = timeSetEvent(nearest_delta_ms,
1035 mm_period,
1036 mm_alarm_handler,
1037 (DWORD_PTR)t,
1038 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
1039
1040 if (!mm_timer) {
1041 fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
1042 GetLastError());
1043
1044 timeEndPeriod(mm_period);
1045 exit(1);
1046 }
1047}
1048
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001049static int win32_start_timer(struct qemu_alarm_timer *t)
1050{
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001051 HANDLE hTimer;
1052 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001053
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001054 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
1055 is zero) that has already expired, the timer is not updated. Since
1056 creating a new timer is relatively expensive, set a bogus one-hour
1057 interval in the dynticks case. */
1058 success = CreateTimerQueueTimer(&hTimer,
1059 NULL,
1060 host_alarm_handler,
1061 t,
1062 1,
1063 alarm_has_dynticks(t) ? 3600000 : 1,
1064 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001065
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001066 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001067 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
1068 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001069 return -1;
1070 }
1071
Stefan Weilcd0544e2011-04-10 20:15:09 +02001072 t->timer = hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001073 return 0;
1074}
1075
1076static void win32_stop_timer(struct qemu_alarm_timer *t)
1077{
Stefan Weilcd0544e2011-04-10 20:15:09 +02001078 HANDLE hTimer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001079
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001080 if (hTimer) {
1081 DeleteTimerQueueTimer(NULL, hTimer, NULL);
1082 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001083}
1084
1085static void win32_rearm_timer(struct qemu_alarm_timer *t)
1086{
Stefan Weilcd0544e2011-04-10 20:15:09 +02001087 HANDLE hTimer = t->timer;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +01001088 int nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001089 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001090
1091 assert(alarm_has_dynticks(t));
Paolo Bonzini688eb382011-09-13 11:42:26 +02001092 if (!rt_clock->active_timers &&
1093 !vm_clock->active_timers &&
1094 !host_clock->active_timers)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001095 return;
1096
Paolo Bonzinicfced5b2011-03-12 17:43:49 +01001097 nearest_delta_ms = (qemu_next_alarm_deadline() + 999999) / 1000000;
1098 if (nearest_delta_ms < 1) {
1099 nearest_delta_ms = 1;
1100 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001101 success = ChangeTimerQueueTimer(NULL,
1102 hTimer,
1103 nearest_delta_ms,
1104 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001105
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001106 if (!success) {
1107 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001108 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001109 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001110 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001111
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001112}
1113
1114#endif /* _WIN32 */
1115
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001116static void alarm_timer_on_change_state_rearm(void *opaque, int running,
1117 RunState state)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001118{
1119 if (running)
1120 qemu_rearm_alarm_timer((struct qemu_alarm_timer *) opaque);
1121}
1122
1123int init_timer_alarm(void)
1124{
1125 struct qemu_alarm_timer *t = NULL;
1126 int i, err = -1;
1127
1128 for (i = 0; alarm_timers[i].name; i++) {
1129 t = &alarm_timers[i];
1130
1131 err = t->start(t);
1132 if (!err)
1133 break;
1134 }
1135
1136 if (err) {
1137 err = -ENOENT;
1138 goto fail;
1139 }
1140
1141 /* first event is at time 0 */
1142 t->pending = 1;
1143 alarm_timer = t;
1144 qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm, t);
1145
1146 return 0;
1147
1148fail:
1149 return err;
1150}
1151
1152void quit_timers(void)
1153{
1154 struct qemu_alarm_timer *t = alarm_timer;
1155 alarm_timer = NULL;
1156 t->stop(t);
1157}
1158
1159int qemu_calculate_timeout(void)
1160{
Paolo Bonzini1ece93a2011-04-13 10:03:45 +02001161 return 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001162}
1163