blob: c7a1551a3603db20b5ac08408a73d97a95c1f77b [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
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010034#ifdef _WIN32
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010035#include <mmsystem.h>
36#endif
37
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010038/***********************************************************/
39/* timers */
40
41#define QEMU_CLOCK_REALTIME 0
42#define QEMU_CLOCK_VIRTUAL 1
43#define QEMU_CLOCK_HOST 2
44
45struct QEMUClock {
Paolo Bonzini688eb382011-09-13 11:42:26 +020046 QEMUTimer *active_timers;
Jan Kiszka691a0c92011-06-20 14:06:27 +020047
48 NotifierList reset_notifiers;
49 int64_t last;
Stefan Weil9a14b292012-04-20 11:51:58 +020050
51 int type;
52 bool enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010053};
54
55struct QEMUTimer {
Paolo Bonzini4a998742011-03-11 16:33:58 +010056 int64_t expire_time; /* in nanoseconds */
Stefan Weil9a14b292012-04-20 11:51:58 +020057 QEMUClock *clock;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010058 QEMUTimerCB *cb;
59 void *opaque;
Stefan Weil9a14b292012-04-20 11:51:58 +020060 QEMUTimer *next;
61 int scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010062};
63
64struct qemu_alarm_timer {
65 char const *name;
66 int (*start)(struct qemu_alarm_timer *t);
67 void (*stop)(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010068 void (*rearm)(struct qemu_alarm_timer *t, int64_t nearest_delta_ns);
Stefan Weilcd0544e2011-04-10 20:15:09 +020069#if defined(__linux__)
Stefan Weilcd0544e2011-04-10 20:15:09 +020070 timer_t timer;
Stefan Weil9a14b292012-04-20 11:51:58 +020071 int fd;
Stefan Weilcd0544e2011-04-10 20:15:09 +020072#elif defined(_WIN32)
73 HANDLE timer;
74#endif
Stefan Weil5e1ec7b2012-04-20 10:45:48 +020075 bool expired;
76 bool pending;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010077};
78
79static struct qemu_alarm_timer *alarm_timer;
80
Stefan Weil45c7b372011-03-24 21:31:24 +010081static bool qemu_timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
82{
83 return timer_head && (timer_head->expire_time <= current_time);
84}
85
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010086static int64_t qemu_next_alarm_deadline(void)
87{
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +010088 int64_t delta = INT64_MAX;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010089 int64_t rtdelta;
90
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +010091 if (!use_icount && vm_clock->enabled && vm_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010092 delta = vm_clock->active_timers->expire_time -
93 qemu_get_clock_ns(vm_clock);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010094 }
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +010095 if (host_clock->enabled && host_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010096 int64_t hdelta = host_clock->active_timers->expire_time -
97 qemu_get_clock_ns(host_clock);
98 if (hdelta < delta) {
99 delta = hdelta;
100 }
101 }
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +0100102 if (rt_clock->enabled && rt_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100103 rtdelta = (rt_clock->active_timers->expire_time -
104 qemu_get_clock_ns(rt_clock));
105 if (rtdelta < delta) {
106 delta = rtdelta;
107 }
108 }
109
110 return delta;
111}
112
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100113static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
114{
Stefano Stabellini82274212012-05-29 03:35:24 +0000115 int64_t nearest_delta_ns = qemu_next_alarm_deadline();
116 if (nearest_delta_ns < INT64_MAX) {
117 t->rearm(t, nearest_delta_ns);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100118 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100119}
120
Paolo Bonzini9c132462011-02-03 14:48:59 +0100121/* TODO: MIN_TIMER_REARM_NS should be optimized */
122#define MIN_TIMER_REARM_NS 250000
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100123
124#ifdef _WIN32
125
Stefan Weil2f9cba02011-04-05 18:34:21 +0200126static int mm_start_timer(struct qemu_alarm_timer *t);
127static void mm_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100128static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200129
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100130static int win32_start_timer(struct qemu_alarm_timer *t);
131static void win32_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100132static void win32_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100133
134#else
135
136static int unix_start_timer(struct qemu_alarm_timer *t);
137static void unix_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100138static void unix_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100139
140#ifdef __linux__
141
142static int dynticks_start_timer(struct qemu_alarm_timer *t);
143static void dynticks_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100144static void dynticks_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100145
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100146#endif /* __linux__ */
147
148#endif /* _WIN32 */
149
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100150static struct qemu_alarm_timer alarm_timers[] = {
151#ifndef _WIN32
152#ifdef __linux__
153 {"dynticks", dynticks_start_timer,
Stefan Weilcd0544e2011-04-10 20:15:09 +0200154 dynticks_stop_timer, dynticks_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100155#endif
Paolo Bonzini84682832011-06-09 13:10:25 +0200156 {"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100157#else
Paolo Bonzinicca5de72011-11-09 12:46:56 +0100158 {"mmtimer", mm_start_timer, mm_stop_timer, mm_rearm_timer},
Stefan Weilcd0544e2011-04-10 20:15:09 +0200159 {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100160#endif
161 {NULL, }
162};
163
164static void show_available_alarms(void)
165{
166 int i;
167
168 printf("Available alarm timers, in order of precedence:\n");
169 for (i = 0; alarm_timers[i].name; i++)
170 printf("%s\n", alarm_timers[i].name);
171}
172
173void configure_alarms(char const *opt)
174{
175 int i;
176 int cur = 0;
177 int count = ARRAY_SIZE(alarm_timers) - 1;
178 char *arg;
179 char *name;
180 struct qemu_alarm_timer tmp;
181
Peter Maydellc8057f92012-08-02 13:45:54 +0100182 if (is_help_option(opt)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100183 show_available_alarms();
184 exit(0);
185 }
186
Anthony Liguori7267c092011-08-20 22:09:37 -0500187 arg = g_strdup(opt);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100188
189 /* Reorder the array */
190 name = strtok(arg, ",");
191 while (name) {
192 for (i = 0; i < count && alarm_timers[i].name; i++) {
193 if (!strcmp(alarm_timers[i].name, name))
194 break;
195 }
196
197 if (i == count) {
198 fprintf(stderr, "Unknown clock %s\n", name);
199 goto next;
200 }
201
202 if (i < cur)
203 /* Ignore */
204 goto next;
205
206 /* Swap */
207 tmp = alarm_timers[i];
208 alarm_timers[i] = alarm_timers[cur];
209 alarm_timers[cur] = tmp;
210
211 cur++;
212next:
213 name = strtok(NULL, ",");
214 }
215
Anthony Liguori7267c092011-08-20 22:09:37 -0500216 g_free(arg);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100217
218 if (cur) {
219 /* Disable remaining timers */
220 for (i = cur; i < count; i++)
221 alarm_timers[i].name = NULL;
222 } else {
223 show_available_alarms();
224 exit(1);
225 }
226}
227
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100228QEMUClock *rt_clock;
229QEMUClock *vm_clock;
230QEMUClock *host_clock;
231
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100232static QEMUClock *qemu_new_clock(int type)
233{
234 QEMUClock *clock;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200235
Anthony Liguori7267c092011-08-20 22:09:37 -0500236 clock = g_malloc0(sizeof(QEMUClock));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100237 clock->type = type;
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200238 clock->enabled = true;
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200239 clock->last = INT64_MIN;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200240 notifier_list_init(&clock->reset_notifiers);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100241 return clock;
242}
243
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200244void qemu_clock_enable(QEMUClock *clock, bool enabled)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100245{
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200246 bool old = clock->enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100247 clock->enabled = enabled;
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200248 if (enabled && !old) {
249 qemu_rearm_alarm_timer(alarm_timer);
250 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100251}
252
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200253int64_t qemu_clock_has_timers(QEMUClock *clock)
254{
255 return !!clock->active_timers;
256}
257
258int64_t qemu_clock_expired(QEMUClock *clock)
259{
260 return (clock->active_timers &&
261 clock->active_timers->expire_time < qemu_get_clock_ns(clock));
262}
263
264int64_t qemu_clock_deadline(QEMUClock *clock)
265{
266 /* To avoid problems with overflow limit this to 2^32. */
267 int64_t delta = INT32_MAX;
268
269 if (clock->active_timers) {
270 delta = clock->active_timers->expire_time - qemu_get_clock_ns(clock);
271 }
272 if (delta < 0) {
273 delta = 0;
274 }
275 return delta;
276}
277
Paolo Bonzini4a998742011-03-11 16:33:58 +0100278QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
279 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100280{
281 QEMUTimer *ts;
282
Anthony Liguori7267c092011-08-20 22:09:37 -0500283 ts = g_malloc0(sizeof(QEMUTimer));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100284 ts->clock = clock;
285 ts->cb = cb;
286 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100287 ts->scale = scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100288 return ts;
289}
290
291void qemu_free_timer(QEMUTimer *ts)
292{
Anthony Liguori7267c092011-08-20 22:09:37 -0500293 g_free(ts);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100294}
295
296/* stop a timer, but do not dealloc it */
297void qemu_del_timer(QEMUTimer *ts)
298{
299 QEMUTimer **pt, *t;
300
301 /* NOTE: this code must be signal safe because
302 qemu_timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200303 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100304 for(;;) {
305 t = *pt;
306 if (!t)
307 break;
308 if (t == ts) {
309 *pt = t->next;
310 break;
311 }
312 pt = &t->next;
313 }
314}
315
316/* modify the current timer so that it will be fired when current_time
317 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200318void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100319{
320 QEMUTimer **pt, *t;
321
322 qemu_del_timer(ts);
323
324 /* add the timer in the sorted list */
325 /* NOTE: this code must be signal safe because
326 qemu_timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200327 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100328 for(;;) {
329 t = *pt;
Stefan Weil45c7b372011-03-24 21:31:24 +0100330 if (!qemu_timer_expired_ns(t, expire_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100331 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100332 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100333 pt = &t->next;
334 }
335 ts->expire_time = expire_time;
336 ts->next = *pt;
337 *pt = ts;
338
339 /* Rearm if necessary */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200340 if (pt == &ts->clock->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100341 if (!alarm_timer->pending) {
342 qemu_rearm_alarm_timer(alarm_timer);
343 }
344 /* Interrupt execution to force deadline recalculation. */
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200345 qemu_clock_warp(ts->clock);
346 if (use_icount) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100347 qemu_notify_event();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200348 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100349 }
350}
351
Paolo Bonzini4a998742011-03-11 16:33:58 +0100352void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
353{
354 qemu_mod_timer_ns(ts, expire_time * ts->scale);
355}
356
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200357bool qemu_timer_pending(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100358{
359 QEMUTimer *t;
Paolo Bonzini688eb382011-09-13 11:42:26 +0200360 for (t = ts->clock->active_timers; t != NULL; t = t->next) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200361 if (t == ts) {
362 return true;
363 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100364 }
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200365 return false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100366}
367
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200368bool qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100369{
Stefan Weil45c7b372011-03-24 21:31:24 +0100370 return qemu_timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100371}
372
Paolo Bonzini8156be52012-03-28 15:42:04 +0200373void qemu_run_timers(QEMUClock *clock)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100374{
375 QEMUTimer **ptimer_head, *ts;
376 int64_t current_time;
377
378 if (!clock->enabled)
379 return;
380
Paolo Bonzini4a998742011-03-11 16:33:58 +0100381 current_time = qemu_get_clock_ns(clock);
Paolo Bonzini688eb382011-09-13 11:42:26 +0200382 ptimer_head = &clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100383 for(;;) {
384 ts = *ptimer_head;
Stefan Weil45c7b372011-03-24 21:31:24 +0100385 if (!qemu_timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100386 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100387 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100388 /* remove timer from the list before calling the callback */
389 *ptimer_head = ts->next;
390 ts->next = NULL;
391
392 /* run the callback (the timer list can be modified) */
393 ts->cb(ts->opaque);
394 }
395}
396
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100397int64_t qemu_get_clock_ns(QEMUClock *clock)
398{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200399 int64_t now, last;
400
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100401 switch(clock->type) {
402 case QEMU_CLOCK_REALTIME:
403 return get_clock();
404 default:
405 case QEMU_CLOCK_VIRTUAL:
406 if (use_icount) {
407 return cpu_get_icount();
408 } else {
409 return cpu_get_clock();
410 }
411 case QEMU_CLOCK_HOST:
Jan Kiszka691a0c92011-06-20 14:06:27 +0200412 now = get_clock_realtime();
413 last = clock->last;
414 clock->last = now;
415 if (now < last) {
416 notifier_list_notify(&clock->reset_notifiers, &now);
417 }
418 return now;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100419 }
420}
421
Jan Kiszka691a0c92011-06-20 14:06:27 +0200422void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
423{
424 notifier_list_add(&clock->reset_notifiers, notifier);
425}
426
427void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
428{
Paolo Bonzini31552522012-01-13 17:34:01 +0100429 notifier_remove(notifier);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200430}
431
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100432void init_clocks(void)
433{
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100434 rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
435 vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
436 host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100437}
438
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200439uint64_t qemu_timer_expire_time_ns(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100440{
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200441 return qemu_timer_pending(ts) ? ts->expire_time : -1;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100442}
443
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100444void qemu_run_all_timers(void)
445{
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200446 alarm_timer->pending = false;
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100447
Peter Portante158fd3c2012-04-05 11:00:45 -0400448 /* vm time timers */
449 qemu_run_timers(vm_clock);
450 qemu_run_timers(rt_clock);
451 qemu_run_timers(host_clock);
452
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100453 /* rearm timer, if not periodic */
454 if (alarm_timer->expired) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200455 alarm_timer->expired = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100456 qemu_rearm_alarm_timer(alarm_timer);
457 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100458}
459
460#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100461static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100462#else
463static void host_alarm_handler(int host_signum)
464#endif
465{
466 struct qemu_alarm_timer *t = alarm_timer;
467 if (!t)
468 return;
469
Stefan Weil82051992012-04-20 11:27:24 +0200470 t->expired = true;
471 t->pending = true;
472 qemu_notify_event();
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100473}
474
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100475#if defined(__linux__)
476
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200477#include "compatfd.h"
478
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100479static int dynticks_start_timer(struct qemu_alarm_timer *t)
480{
481 struct sigevent ev;
482 timer_t host_timer;
483 struct sigaction act;
484
485 sigfillset(&act.sa_mask);
486 act.sa_flags = 0;
487 act.sa_handler = host_alarm_handler;
488
489 sigaction(SIGALRM, &act, NULL);
490
491 /*
492 * Initialize ev struct to 0 to avoid valgrind complaining
493 * about uninitialized data in timer_create call
494 */
495 memset(&ev, 0, sizeof(ev));
496 ev.sigev_value.sival_int = 0;
497 ev.sigev_notify = SIGEV_SIGNAL;
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200498#ifdef SIGEV_THREAD_ID
499 if (qemu_signalfd_available()) {
500 ev.sigev_notify = SIGEV_THREAD_ID;
501 ev._sigev_un._tid = qemu_get_thread_id();
502 }
503#endif /* SIGEV_THREAD_ID */
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100504 ev.sigev_signo = SIGALRM;
505
506 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
507 perror("timer_create");
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100508 return -1;
509 }
510
Stefan Weilcd0544e2011-04-10 20:15:09 +0200511 t->timer = host_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100512
513 return 0;
514}
515
516static void dynticks_stop_timer(struct qemu_alarm_timer *t)
517{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200518 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100519
520 timer_delete(host_timer);
521}
522
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100523static void dynticks_rearm_timer(struct qemu_alarm_timer *t,
524 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100525{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200526 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100527 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100528 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100529
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100530 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
531 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100532
533 /* check whether a timer is already running */
534 if (timer_gettime(host_timer, &timeout)) {
535 perror("gettime");
536 fprintf(stderr, "Internal timer error: aborting\n");
537 exit(1);
538 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100539 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
540 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100541 return;
542
543 timeout.it_interval.tv_sec = 0;
544 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100545 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
546 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100547 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
548 perror("settime");
549 fprintf(stderr, "Internal timer error: aborting\n");
550 exit(1);
551 }
552}
553
554#endif /* defined(__linux__) */
555
Stefan Weilf26e5a52011-02-04 22:01:32 +0100556#if !defined(_WIN32)
557
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100558static int unix_start_timer(struct qemu_alarm_timer *t)
559{
560 struct sigaction act;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100561
562 /* timer signal */
563 sigfillset(&act.sa_mask);
564 act.sa_flags = 0;
565 act.sa_handler = host_alarm_handler;
566
567 sigaction(SIGALRM, &act, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200568 return 0;
569}
570
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100571static void unix_rearm_timer(struct qemu_alarm_timer *t,
572 int64_t nearest_delta_ns)
Paolo Bonzini84682832011-06-09 13:10:25 +0200573{
574 struct itimerval itv;
Paolo Bonzini84682832011-06-09 13:10:25 +0200575 int err;
576
Paolo Bonzini84682832011-06-09 13:10:25 +0200577 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
578 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100579
580 itv.it_interval.tv_sec = 0;
Paolo Bonzini84682832011-06-09 13:10:25 +0200581 itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */
582 itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
583 itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100584 err = setitimer(ITIMER_REAL, &itv, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200585 if (err) {
586 perror("setitimer");
587 fprintf(stderr, "Internal timer error: aborting\n");
588 exit(1);
589 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100590}
591
592static void unix_stop_timer(struct qemu_alarm_timer *t)
593{
594 struct itimerval itv;
595
596 memset(&itv, 0, sizeof(itv));
597 setitimer(ITIMER_REAL, &itv, NULL);
598}
599
600#endif /* !defined(_WIN32) */
601
602
603#ifdef _WIN32
604
Stefan Weil2f9cba02011-04-05 18:34:21 +0200605static MMRESULT mm_timer;
Stefan Weil40f08e82012-04-27 05:34:40 +0000606static TIMECAPS mm_tc;
Stefan Weil2f9cba02011-04-05 18:34:21 +0200607
608static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
609 DWORD_PTR dwUser, DWORD_PTR dw1,
610 DWORD_PTR dw2)
611{
612 struct qemu_alarm_timer *t = alarm_timer;
613 if (!t) {
614 return;
615 }
Stefan Weil82051992012-04-20 11:27:24 +0200616 t->expired = true;
617 t->pending = true;
618 qemu_notify_event();
Stefan Weil2f9cba02011-04-05 18:34:21 +0200619}
620
621static int mm_start_timer(struct qemu_alarm_timer *t)
622{
Stefan Weil40f08e82012-04-27 05:34:40 +0000623 timeGetDevCaps(&mm_tc, sizeof(mm_tc));
Stefan Weil2f9cba02011-04-05 18:34:21 +0200624
Stefan Weil40f08e82012-04-27 05:34:40 +0000625 timeBeginPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200626
Stefan Weil40f08e82012-04-27 05:34:40 +0000627 mm_timer = timeSetEvent(mm_tc.wPeriodMin, /* interval (ms) */
628 mm_tc.wPeriodMin, /* resolution */
Stefan Weil2f9cba02011-04-05 18:34:21 +0200629 mm_alarm_handler, /* function */
630 (DWORD_PTR)t, /* parameter */
Stefan Weil82051992012-04-20 11:27:24 +0200631 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200632
633 if (!mm_timer) {
Stefan Weil52ef6512012-05-08 19:14:43 +0200634 fprintf(stderr, "Failed to initialize win32 alarm timer\n");
Stefan Weil40f08e82012-04-27 05:34:40 +0000635 timeEndPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200636 return -1;
637 }
638
639 return 0;
640}
641
642static void mm_stop_timer(struct qemu_alarm_timer *t)
643{
644 timeKillEvent(mm_timer);
Stefan Weil40f08e82012-04-27 05:34:40 +0000645 timeEndPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200646}
647
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100648static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta)
Stefan Weil2f9cba02011-04-05 18:34:21 +0200649{
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100650 int64_t nearest_delta_ms = delta / 1000000;
Stefan Weil40f08e82012-04-27 05:34:40 +0000651 if (nearest_delta_ms < mm_tc.wPeriodMin) {
652 nearest_delta_ms = mm_tc.wPeriodMin;
653 } else if (nearest_delta_ms > mm_tc.wPeriodMax) {
654 nearest_delta_ms = mm_tc.wPeriodMax;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100655 }
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100656
657 timeKillEvent(mm_timer);
Stefan Weil40f08e82012-04-27 05:34:40 +0000658 mm_timer = timeSetEvent((UINT)nearest_delta_ms,
659 mm_tc.wPeriodMin,
Stefan Weil2f9cba02011-04-05 18:34:21 +0200660 mm_alarm_handler,
661 (DWORD_PTR)t,
662 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
663
664 if (!mm_timer) {
Stefan Weil52ef6512012-05-08 19:14:43 +0200665 fprintf(stderr, "Failed to re-arm win32 alarm timer\n");
Stefan Weil40f08e82012-04-27 05:34:40 +0000666 timeEndPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200667 exit(1);
668 }
669}
670
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100671static int win32_start_timer(struct qemu_alarm_timer *t)
672{
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100673 HANDLE hTimer;
674 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100675
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100676 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
677 is zero) that has already expired, the timer is not updated. Since
678 creating a new timer is relatively expensive, set a bogus one-hour
679 interval in the dynticks case. */
680 success = CreateTimerQueueTimer(&hTimer,
681 NULL,
682 host_alarm_handler,
683 t,
684 1,
Stefan Weil82051992012-04-20 11:27:24 +0200685 3600000,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100686 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100687
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100688 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100689 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
690 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100691 return -1;
692 }
693
Stefan Weilcd0544e2011-04-10 20:15:09 +0200694 t->timer = hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100695 return 0;
696}
697
698static void win32_stop_timer(struct qemu_alarm_timer *t)
699{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200700 HANDLE hTimer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100701
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100702 if (hTimer) {
703 DeleteTimerQueueTimer(NULL, hTimer, NULL);
704 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100705}
706
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100707static void win32_rearm_timer(struct qemu_alarm_timer *t,
708 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100709{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200710 HANDLE hTimer = t->timer;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100711 int64_t nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100712 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100713
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100714 nearest_delta_ms = nearest_delta_ns / 1000000;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +0100715 if (nearest_delta_ms < 1) {
716 nearest_delta_ms = 1;
717 }
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100718 /* ULONG_MAX can be 32 bit */
719 if (nearest_delta_ms > ULONG_MAX) {
720 nearest_delta_ms = ULONG_MAX;
721 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100722 success = ChangeTimerQueueTimer(NULL,
723 hTimer,
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100724 (unsigned long) nearest_delta_ms,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100725 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100726
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100727 if (!success) {
728 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100729 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100730 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100731 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100732
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100733}
734
735#endif /* _WIN32 */
736
Paolo Bonzini4260a732011-09-19 10:18:51 +0200737static void quit_timers(void)
738{
739 struct qemu_alarm_timer *t = alarm_timer;
740 alarm_timer = NULL;
741 t->stop(t);
742}
743
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100744int init_timer_alarm(void)
745{
746 struct qemu_alarm_timer *t = NULL;
747 int i, err = -1;
748
749 for (i = 0; alarm_timers[i].name; i++) {
750 t = &alarm_timers[i];
751
752 err = t->start(t);
753 if (!err)
754 break;
755 }
756
757 if (err) {
758 err = -ENOENT;
759 goto fail;
760 }
761
Paolo Bonzini4260a732011-09-19 10:18:51 +0200762 atexit(quit_timers);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100763 alarm_timer = t;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100764 return 0;
765
766fail:
767 return err;
768}
769