blob: 08c45ab0ae497cf9152ccb7dcbdfacd280c6d324 [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
Stefan Weilbff9f8b2012-04-20 10:27:06 +020032#include "qemu-timer.h"
33
Juergen Lock44459342010-03-25 22:35:03 +010034#ifdef __FreeBSD__
35#include <sys/param.h>
36#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010037
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010038#ifdef _WIN32
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010039#include <mmsystem.h>
40#endif
41
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010042/***********************************************************/
43/* timers */
44
45#define QEMU_CLOCK_REALTIME 0
46#define QEMU_CLOCK_VIRTUAL 1
47#define QEMU_CLOCK_HOST 2
48
49struct QEMUClock {
50 int type;
Stefan Weil5e1ec7b2012-04-20 10:45:48 +020051 bool enabled;
Paolo Bonziniab33fcd2011-04-13 10:03:44 +020052
Paolo Bonzini688eb382011-09-13 11:42:26 +020053 QEMUTimer *active_timers;
Jan Kiszka691a0c92011-06-20 14:06:27 +020054
55 NotifierList reset_notifiers;
56 int64_t last;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010057};
58
59struct QEMUTimer {
60 QEMUClock *clock;
Paolo Bonzini4a998742011-03-11 16:33:58 +010061 int64_t expire_time; /* in nanoseconds */
62 int scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010063 QEMUTimerCB *cb;
64 void *opaque;
65 struct QEMUTimer *next;
66};
67
68struct qemu_alarm_timer {
69 char const *name;
70 int (*start)(struct qemu_alarm_timer *t);
71 void (*stop)(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010072 void (*rearm)(struct qemu_alarm_timer *t, int64_t nearest_delta_ns);
Stefan Weilcd0544e2011-04-10 20:15:09 +020073#if defined(__linux__)
74 int fd;
75 timer_t timer;
76#elif defined(_WIN32)
77 HANDLE timer;
78#endif
Stefan Weil5e1ec7b2012-04-20 10:45:48 +020079 bool expired;
80 bool pending;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010081};
82
83static struct qemu_alarm_timer *alarm_timer;
84
Stefan Weil45c7b372011-03-24 21:31:24 +010085static bool qemu_timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
86{
87 return timer_head && (timer_head->expire_time <= current_time);
88}
89
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010090static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
91{
92 return !!t->rearm;
93}
94
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010095static int64_t qemu_next_alarm_deadline(void)
96{
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +010097 int64_t delta = INT64_MAX;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010098 int64_t rtdelta;
99
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +0100100 if (!use_icount && vm_clock->enabled && vm_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100101 delta = vm_clock->active_timers->expire_time -
102 qemu_get_clock_ns(vm_clock);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100103 }
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +0100104 if (host_clock->enabled && host_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100105 int64_t hdelta = host_clock->active_timers->expire_time -
106 qemu_get_clock_ns(host_clock);
107 if (hdelta < delta) {
108 delta = hdelta;
109 }
110 }
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +0100111 if (rt_clock->enabled && rt_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100112 rtdelta = (rt_clock->active_timers->expire_time -
113 qemu_get_clock_ns(rt_clock));
114 if (rtdelta < delta) {
115 delta = rtdelta;
116 }
117 }
118
119 return delta;
120}
121
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100122static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
123{
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100124 int64_t nearest_delta_ns;
125 assert(alarm_has_dynticks(t));
126 if (!rt_clock->active_timers &&
127 !vm_clock->active_timers &&
128 !host_clock->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100129 return;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100130 }
131 nearest_delta_ns = qemu_next_alarm_deadline();
132 t->rearm(t, nearest_delta_ns);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100133}
134
Paolo Bonzini9c132462011-02-03 14:48:59 +0100135/* TODO: MIN_TIMER_REARM_NS should be optimized */
136#define MIN_TIMER_REARM_NS 250000
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100137
138#ifdef _WIN32
139
Stefan Weil2f9cba02011-04-05 18:34:21 +0200140static int mm_start_timer(struct qemu_alarm_timer *t);
141static void mm_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100142static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200143
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100144static int win32_start_timer(struct qemu_alarm_timer *t);
145static void win32_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100146static void win32_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100147
148#else
149
150static int unix_start_timer(struct qemu_alarm_timer *t);
151static void unix_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100152static void unix_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100153
154#ifdef __linux__
155
156static int dynticks_start_timer(struct qemu_alarm_timer *t);
157static void dynticks_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100158static void dynticks_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100159
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100160#endif /* __linux__ */
161
162#endif /* _WIN32 */
163
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100164static struct qemu_alarm_timer alarm_timers[] = {
165#ifndef _WIN32
166#ifdef __linux__
167 {"dynticks", dynticks_start_timer,
Stefan Weilcd0544e2011-04-10 20:15:09 +0200168 dynticks_stop_timer, dynticks_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100169#endif
Paolo Bonzini84682832011-06-09 13:10:25 +0200170 {"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100171#else
Paolo Bonzinicca5de72011-11-09 12:46:56 +0100172 {"mmtimer", mm_start_timer, mm_stop_timer, mm_rearm_timer},
Stefan Weilcd0544e2011-04-10 20:15:09 +0200173 {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100174#endif
175 {NULL, }
176};
177
178static void show_available_alarms(void)
179{
180 int i;
181
182 printf("Available alarm timers, in order of precedence:\n");
183 for (i = 0; alarm_timers[i].name; i++)
184 printf("%s\n", alarm_timers[i].name);
185}
186
187void configure_alarms(char const *opt)
188{
189 int i;
190 int cur = 0;
191 int count = ARRAY_SIZE(alarm_timers) - 1;
192 char *arg;
193 char *name;
194 struct qemu_alarm_timer tmp;
195
196 if (!strcmp(opt, "?")) {
197 show_available_alarms();
198 exit(0);
199 }
200
Anthony Liguori7267c092011-08-20 22:09:37 -0500201 arg = g_strdup(opt);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100202
203 /* Reorder the array */
204 name = strtok(arg, ",");
205 while (name) {
206 for (i = 0; i < count && alarm_timers[i].name; i++) {
207 if (!strcmp(alarm_timers[i].name, name))
208 break;
209 }
210
211 if (i == count) {
212 fprintf(stderr, "Unknown clock %s\n", name);
213 goto next;
214 }
215
216 if (i < cur)
217 /* Ignore */
218 goto next;
219
220 /* Swap */
221 tmp = alarm_timers[i];
222 alarm_timers[i] = alarm_timers[cur];
223 alarm_timers[cur] = tmp;
224
225 cur++;
226next:
227 name = strtok(NULL, ",");
228 }
229
Anthony Liguori7267c092011-08-20 22:09:37 -0500230 g_free(arg);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100231
232 if (cur) {
233 /* Disable remaining timers */
234 for (i = cur; i < count; i++)
235 alarm_timers[i].name = NULL;
236 } else {
237 show_available_alarms();
238 exit(1);
239 }
240}
241
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100242QEMUClock *rt_clock;
243QEMUClock *vm_clock;
244QEMUClock *host_clock;
245
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100246static QEMUClock *qemu_new_clock(int type)
247{
248 QEMUClock *clock;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200249
Anthony Liguori7267c092011-08-20 22:09:37 -0500250 clock = g_malloc0(sizeof(QEMUClock));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100251 clock->type = type;
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200252 clock->enabled = true;
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200253 clock->last = INT64_MIN;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200254 notifier_list_init(&clock->reset_notifiers);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100255 return clock;
256}
257
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200258void qemu_clock_enable(QEMUClock *clock, bool enabled)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100259{
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200260 bool old = clock->enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100261 clock->enabled = enabled;
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200262 if (enabled && !old) {
263 qemu_rearm_alarm_timer(alarm_timer);
264 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100265}
266
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200267int64_t qemu_clock_has_timers(QEMUClock *clock)
268{
269 return !!clock->active_timers;
270}
271
272int64_t qemu_clock_expired(QEMUClock *clock)
273{
274 return (clock->active_timers &&
275 clock->active_timers->expire_time < qemu_get_clock_ns(clock));
276}
277
278int64_t qemu_clock_deadline(QEMUClock *clock)
279{
280 /* To avoid problems with overflow limit this to 2^32. */
281 int64_t delta = INT32_MAX;
282
283 if (clock->active_timers) {
284 delta = clock->active_timers->expire_time - qemu_get_clock_ns(clock);
285 }
286 if (delta < 0) {
287 delta = 0;
288 }
289 return delta;
290}
291
Paolo Bonzini4a998742011-03-11 16:33:58 +0100292QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
293 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100294{
295 QEMUTimer *ts;
296
Anthony Liguori7267c092011-08-20 22:09:37 -0500297 ts = g_malloc0(sizeof(QEMUTimer));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100298 ts->clock = clock;
299 ts->cb = cb;
300 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100301 ts->scale = scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100302 return ts;
303}
304
305void qemu_free_timer(QEMUTimer *ts)
306{
Anthony Liguori7267c092011-08-20 22:09:37 -0500307 g_free(ts);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100308}
309
310/* stop a timer, but do not dealloc it */
311void qemu_del_timer(QEMUTimer *ts)
312{
313 QEMUTimer **pt, *t;
314
315 /* NOTE: this code must be signal safe because
316 qemu_timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200317 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100318 for(;;) {
319 t = *pt;
320 if (!t)
321 break;
322 if (t == ts) {
323 *pt = t->next;
324 break;
325 }
326 pt = &t->next;
327 }
328}
329
330/* modify the current timer so that it will be fired when current_time
331 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200332void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100333{
334 QEMUTimer **pt, *t;
335
336 qemu_del_timer(ts);
337
338 /* add the timer in the sorted list */
339 /* NOTE: this code must be signal safe because
340 qemu_timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200341 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100342 for(;;) {
343 t = *pt;
Stefan Weil45c7b372011-03-24 21:31:24 +0100344 if (!qemu_timer_expired_ns(t, expire_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100345 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100346 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100347 pt = &t->next;
348 }
349 ts->expire_time = expire_time;
350 ts->next = *pt;
351 *pt = ts;
352
353 /* Rearm if necessary */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200354 if (pt == &ts->clock->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100355 if (!alarm_timer->pending) {
356 qemu_rearm_alarm_timer(alarm_timer);
357 }
358 /* Interrupt execution to force deadline recalculation. */
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200359 qemu_clock_warp(ts->clock);
360 if (use_icount) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100361 qemu_notify_event();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200362 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100363 }
364}
365
Paolo Bonzini4a998742011-03-11 16:33:58 +0100366void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
367{
368 qemu_mod_timer_ns(ts, expire_time * ts->scale);
369}
370
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200371bool qemu_timer_pending(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100372{
373 QEMUTimer *t;
Paolo Bonzini688eb382011-09-13 11:42:26 +0200374 for (t = ts->clock->active_timers; t != NULL; t = t->next) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200375 if (t == ts) {
376 return true;
377 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100378 }
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200379 return false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100380}
381
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200382bool qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100383{
Stefan Weil45c7b372011-03-24 21:31:24 +0100384 return qemu_timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100385}
386
Paolo Bonzini8156be52012-03-28 15:42:04 +0200387void qemu_run_timers(QEMUClock *clock)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100388{
389 QEMUTimer **ptimer_head, *ts;
390 int64_t current_time;
391
392 if (!clock->enabled)
393 return;
394
Paolo Bonzini4a998742011-03-11 16:33:58 +0100395 current_time = qemu_get_clock_ns(clock);
Paolo Bonzini688eb382011-09-13 11:42:26 +0200396 ptimer_head = &clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100397 for(;;) {
398 ts = *ptimer_head;
Stefan Weil45c7b372011-03-24 21:31:24 +0100399 if (!qemu_timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100400 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100401 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100402 /* remove timer from the list before calling the callback */
403 *ptimer_head = ts->next;
404 ts->next = NULL;
405
406 /* run the callback (the timer list can be modified) */
407 ts->cb(ts->opaque);
408 }
409}
410
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100411int64_t qemu_get_clock_ns(QEMUClock *clock)
412{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200413 int64_t now, last;
414
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100415 switch(clock->type) {
416 case QEMU_CLOCK_REALTIME:
417 return get_clock();
418 default:
419 case QEMU_CLOCK_VIRTUAL:
420 if (use_icount) {
421 return cpu_get_icount();
422 } else {
423 return cpu_get_clock();
424 }
425 case QEMU_CLOCK_HOST:
Jan Kiszka691a0c92011-06-20 14:06:27 +0200426 now = get_clock_realtime();
427 last = clock->last;
428 clock->last = now;
429 if (now < last) {
430 notifier_list_notify(&clock->reset_notifiers, &now);
431 }
432 return now;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100433 }
434}
435
Jan Kiszka691a0c92011-06-20 14:06:27 +0200436void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
437{
438 notifier_list_add(&clock->reset_notifiers, notifier);
439}
440
441void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
442{
Paolo Bonzini31552522012-01-13 17:34:01 +0100443 notifier_remove(notifier);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200444}
445
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100446void init_clocks(void)
447{
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100448 rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
449 vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
450 host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100451}
452
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200453uint64_t qemu_timer_expire_time_ns(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100454{
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200455 return qemu_timer_pending(ts) ? ts->expire_time : -1;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100456}
457
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100458void qemu_run_all_timers(void)
459{
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200460 alarm_timer->pending = false;
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100461
Peter Portante158fd3c2012-04-05 11:00:45 -0400462 /* vm time timers */
463 qemu_run_timers(vm_clock);
464 qemu_run_timers(rt_clock);
465 qemu_run_timers(host_clock);
466
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100467 /* rearm timer, if not periodic */
468 if (alarm_timer->expired) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200469 alarm_timer->expired = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100470 qemu_rearm_alarm_timer(alarm_timer);
471 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100472}
473
474#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100475static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100476#else
477static void host_alarm_handler(int host_signum)
478#endif
479{
480 struct qemu_alarm_timer *t = alarm_timer;
481 if (!t)
482 return;
483
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100484 if (alarm_has_dynticks(t) ||
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100485 qemu_next_alarm_deadline () <= 0) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100486 t->expired = alarm_has_dynticks(t);
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200487 t->pending = true;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100488 qemu_notify_event();
489 }
490}
491
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100492#if defined(__linux__)
493
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200494#include "compatfd.h"
495
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100496static int dynticks_start_timer(struct qemu_alarm_timer *t)
497{
498 struct sigevent ev;
499 timer_t host_timer;
500 struct sigaction act;
501
502 sigfillset(&act.sa_mask);
503 act.sa_flags = 0;
504 act.sa_handler = host_alarm_handler;
505
506 sigaction(SIGALRM, &act, NULL);
507
508 /*
509 * Initialize ev struct to 0 to avoid valgrind complaining
510 * about uninitialized data in timer_create call
511 */
512 memset(&ev, 0, sizeof(ev));
513 ev.sigev_value.sival_int = 0;
514 ev.sigev_notify = SIGEV_SIGNAL;
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200515#ifdef SIGEV_THREAD_ID
516 if (qemu_signalfd_available()) {
517 ev.sigev_notify = SIGEV_THREAD_ID;
518 ev._sigev_un._tid = qemu_get_thread_id();
519 }
520#endif /* SIGEV_THREAD_ID */
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100521 ev.sigev_signo = SIGALRM;
522
523 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
524 perror("timer_create");
525
526 /* disable dynticks */
527 fprintf(stderr, "Dynamic Ticks disabled\n");
528
529 return -1;
530 }
531
Stefan Weilcd0544e2011-04-10 20:15:09 +0200532 t->timer = host_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100533
534 return 0;
535}
536
537static void dynticks_stop_timer(struct qemu_alarm_timer *t)
538{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200539 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100540
541 timer_delete(host_timer);
542}
543
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100544static void dynticks_rearm_timer(struct qemu_alarm_timer *t,
545 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100546{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200547 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100548 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100549 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100550
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100551 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
552 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100553
554 /* check whether a timer is already running */
555 if (timer_gettime(host_timer, &timeout)) {
556 perror("gettime");
557 fprintf(stderr, "Internal timer error: aborting\n");
558 exit(1);
559 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100560 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
561 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100562 return;
563
564 timeout.it_interval.tv_sec = 0;
565 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100566 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
567 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100568 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
569 perror("settime");
570 fprintf(stderr, "Internal timer error: aborting\n");
571 exit(1);
572 }
573}
574
575#endif /* defined(__linux__) */
576
Stefan Weilf26e5a52011-02-04 22:01:32 +0100577#if !defined(_WIN32)
578
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100579static int unix_start_timer(struct qemu_alarm_timer *t)
580{
581 struct sigaction act;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100582
583 /* timer signal */
584 sigfillset(&act.sa_mask);
585 act.sa_flags = 0;
586 act.sa_handler = host_alarm_handler;
587
588 sigaction(SIGALRM, &act, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200589 return 0;
590}
591
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100592static void unix_rearm_timer(struct qemu_alarm_timer *t,
593 int64_t nearest_delta_ns)
Paolo Bonzini84682832011-06-09 13:10:25 +0200594{
595 struct itimerval itv;
Paolo Bonzini84682832011-06-09 13:10:25 +0200596 int err;
597
Paolo Bonzini84682832011-06-09 13:10:25 +0200598 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
599 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100600
601 itv.it_interval.tv_sec = 0;
Paolo Bonzini84682832011-06-09 13:10:25 +0200602 itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */
603 itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
604 itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100605 err = setitimer(ITIMER_REAL, &itv, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200606 if (err) {
607 perror("setitimer");
608 fprintf(stderr, "Internal timer error: aborting\n");
609 exit(1);
610 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100611}
612
613static void unix_stop_timer(struct qemu_alarm_timer *t)
614{
615 struct itimerval itv;
616
617 memset(&itv, 0, sizeof(itv));
618 setitimer(ITIMER_REAL, &itv, NULL);
619}
620
621#endif /* !defined(_WIN32) */
622
623
624#ifdef _WIN32
625
Stefan Weil2f9cba02011-04-05 18:34:21 +0200626static MMRESULT mm_timer;
627static unsigned mm_period;
628
629static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
630 DWORD_PTR dwUser, DWORD_PTR dw1,
631 DWORD_PTR dw2)
632{
633 struct qemu_alarm_timer *t = alarm_timer;
634 if (!t) {
635 return;
636 }
637 if (alarm_has_dynticks(t) || qemu_next_alarm_deadline() <= 0) {
638 t->expired = alarm_has_dynticks(t);
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200639 t->pending = true;
Stefan Weil2f9cba02011-04-05 18:34:21 +0200640 qemu_notify_event();
641 }
642}
643
644static int mm_start_timer(struct qemu_alarm_timer *t)
645{
646 TIMECAPS tc;
647 UINT flags;
648
649 memset(&tc, 0, sizeof(tc));
650 timeGetDevCaps(&tc, sizeof(tc));
651
652 mm_period = tc.wPeriodMin;
653 timeBeginPeriod(mm_period);
654
655 flags = TIME_CALLBACK_FUNCTION;
656 if (alarm_has_dynticks(t)) {
657 flags |= TIME_ONESHOT;
658 } else {
659 flags |= TIME_PERIODIC;
660 }
661
662 mm_timer = timeSetEvent(1, /* interval (ms) */
663 mm_period, /* resolution */
664 mm_alarm_handler, /* function */
665 (DWORD_PTR)t, /* parameter */
666 flags);
667
668 if (!mm_timer) {
669 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
670 GetLastError());
671 timeEndPeriod(mm_period);
672 return -1;
673 }
674
675 return 0;
676}
677
678static void mm_stop_timer(struct qemu_alarm_timer *t)
679{
680 timeKillEvent(mm_timer);
681 timeEndPeriod(mm_period);
682}
683
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100684static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta)
Stefan Weil2f9cba02011-04-05 18:34:21 +0200685{
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100686 int64_t nearest_delta_ms = delta / 1000000;
Stefan Weil2f9cba02011-04-05 18:34:21 +0200687 if (nearest_delta_ms < 1) {
688 nearest_delta_ms = 1;
689 }
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100690 /* UINT_MAX can be 32 bit */
691 if (nearest_delta_ms > UINT_MAX) {
692 nearest_delta_ms = UINT_MAX;
693 }
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100694
695 timeKillEvent(mm_timer);
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100696 mm_timer = timeSetEvent((unsigned int) nearest_delta_ms,
Stefan Weil2f9cba02011-04-05 18:34:21 +0200697 mm_period,
698 mm_alarm_handler,
699 (DWORD_PTR)t,
700 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
701
702 if (!mm_timer) {
703 fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
704 GetLastError());
705
706 timeEndPeriod(mm_period);
707 exit(1);
708 }
709}
710
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100711static int win32_start_timer(struct qemu_alarm_timer *t)
712{
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100713 HANDLE hTimer;
714 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100715
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100716 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
717 is zero) that has already expired, the timer is not updated. Since
718 creating a new timer is relatively expensive, set a bogus one-hour
719 interval in the dynticks case. */
720 success = CreateTimerQueueTimer(&hTimer,
721 NULL,
722 host_alarm_handler,
723 t,
724 1,
725 alarm_has_dynticks(t) ? 3600000 : 1,
726 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100727
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100728 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100729 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
730 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100731 return -1;
732 }
733
Stefan Weilcd0544e2011-04-10 20:15:09 +0200734 t->timer = hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100735 return 0;
736}
737
738static void win32_stop_timer(struct qemu_alarm_timer *t)
739{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200740 HANDLE hTimer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100741
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100742 if (hTimer) {
743 DeleteTimerQueueTimer(NULL, hTimer, NULL);
744 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100745}
746
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100747static void win32_rearm_timer(struct qemu_alarm_timer *t,
748 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100749{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200750 HANDLE hTimer = t->timer;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100751 int64_t nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100752 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100753
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100754 nearest_delta_ms = nearest_delta_ns / 1000000;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +0100755 if (nearest_delta_ms < 1) {
756 nearest_delta_ms = 1;
757 }
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100758 /* ULONG_MAX can be 32 bit */
759 if (nearest_delta_ms > ULONG_MAX) {
760 nearest_delta_ms = ULONG_MAX;
761 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100762 success = ChangeTimerQueueTimer(NULL,
763 hTimer,
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100764 (unsigned long) nearest_delta_ms,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100765 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100766
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100767 if (!success) {
768 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100769 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100770 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100771 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100772
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100773}
774
775#endif /* _WIN32 */
776
Paolo Bonzini4260a732011-09-19 10:18:51 +0200777static void quit_timers(void)
778{
779 struct qemu_alarm_timer *t = alarm_timer;
780 alarm_timer = NULL;
781 t->stop(t);
782}
783
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100784int init_timer_alarm(void)
785{
786 struct qemu_alarm_timer *t = NULL;
787 int i, err = -1;
788
789 for (i = 0; alarm_timers[i].name; i++) {
790 t = &alarm_timers[i];
791
792 err = t->start(t);
793 if (!err)
794 break;
795 }
796
797 if (err) {
798 err = -ENOENT;
799 goto fail;
800 }
801
802 /* first event is at time 0 */
Paolo Bonzini4260a732011-09-19 10:18:51 +0200803 atexit(quit_timers);
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200804 t->pending = true;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100805 alarm_timer = t;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100806
807 return 0;
808
809fail:
810 return err;
811}
812