blob: 0d2bb94289bdd92fb4581996d2b691ed2d2e1196 [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"
Anthony Liguori30ea8332012-11-02 16:12:53 -050033#ifdef CONFIG_POSIX
34#include <pthread.h>
35#endif
Stefan Weilbff9f8b2012-04-20 10:27:06 +020036
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010037#ifdef _WIN32
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010038#include <mmsystem.h>
39#endif
40
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010041/***********************************************************/
42/* timers */
43
44#define QEMU_CLOCK_REALTIME 0
45#define QEMU_CLOCK_VIRTUAL 1
46#define QEMU_CLOCK_HOST 2
47
48struct QEMUClock {
Paolo Bonzini688eb382011-09-13 11:42:26 +020049 QEMUTimer *active_timers;
Jan Kiszka691a0c92011-06-20 14:06:27 +020050
51 NotifierList reset_notifiers;
52 int64_t last;
Stefan Weil9a14b292012-04-20 11:51:58 +020053
54 int type;
55 bool enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010056};
57
58struct QEMUTimer {
Paolo Bonzini4a998742011-03-11 16:33:58 +010059 int64_t expire_time; /* in nanoseconds */
Stefan Weil9a14b292012-04-20 11:51:58 +020060 QEMUClock *clock;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010061 QEMUTimerCB *cb;
62 void *opaque;
Stefan Weil9a14b292012-04-20 11:51:58 +020063 QEMUTimer *next;
64 int scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010065};
66
67struct qemu_alarm_timer {
68 char const *name;
69 int (*start)(struct qemu_alarm_timer *t);
70 void (*stop)(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010071 void (*rearm)(struct qemu_alarm_timer *t, int64_t nearest_delta_ns);
Stefan Weilcd0544e2011-04-10 20:15:09 +020072#if defined(__linux__)
Stefan Weilcd0544e2011-04-10 20:15:09 +020073 timer_t timer;
Stefan Weil9a14b292012-04-20 11:51:58 +020074 int fd;
Stefan Weilcd0544e2011-04-10 20:15:09 +020075#elif defined(_WIN32)
76 HANDLE timer;
77#endif
Stefan Weil5e1ec7b2012-04-20 10:45:48 +020078 bool expired;
79 bool pending;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010080};
81
82static struct qemu_alarm_timer *alarm_timer;
83
Stefan Weil45c7b372011-03-24 21:31:24 +010084static bool qemu_timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
85{
86 return timer_head && (timer_head->expire_time <= current_time);
87}
88
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010089static int64_t qemu_next_alarm_deadline(void)
90{
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +010091 int64_t delta = INT64_MAX;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010092 int64_t rtdelta;
93
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +010094 if (!use_icount && vm_clock->enabled && vm_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010095 delta = vm_clock->active_timers->expire_time -
96 qemu_get_clock_ns(vm_clock);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010097 }
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +010098 if (host_clock->enabled && host_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010099 int64_t hdelta = host_clock->active_timers->expire_time -
100 qemu_get_clock_ns(host_clock);
101 if (hdelta < delta) {
102 delta = hdelta;
103 }
104 }
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +0100105 if (rt_clock->enabled && rt_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100106 rtdelta = (rt_clock->active_timers->expire_time -
107 qemu_get_clock_ns(rt_clock));
108 if (rtdelta < delta) {
109 delta = rtdelta;
110 }
111 }
112
113 return delta;
114}
115
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100116static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
117{
Stefano Stabellini82274212012-05-29 03:35:24 +0000118 int64_t nearest_delta_ns = qemu_next_alarm_deadline();
119 if (nearest_delta_ns < INT64_MAX) {
120 t->rearm(t, nearest_delta_ns);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100121 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100122}
123
Paolo Bonzini9c132462011-02-03 14:48:59 +0100124/* TODO: MIN_TIMER_REARM_NS should be optimized */
125#define MIN_TIMER_REARM_NS 250000
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100126
127#ifdef _WIN32
128
Stefan Weil2f9cba02011-04-05 18:34:21 +0200129static int mm_start_timer(struct qemu_alarm_timer *t);
130static void mm_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100131static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200132
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100133static int win32_start_timer(struct qemu_alarm_timer *t);
134static void win32_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100135static void win32_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100136
137#else
138
139static int unix_start_timer(struct qemu_alarm_timer *t);
140static void unix_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100141static void unix_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100142
143#ifdef __linux__
144
145static int dynticks_start_timer(struct qemu_alarm_timer *t);
146static void dynticks_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100147static void dynticks_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100148
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100149#endif /* __linux__ */
150
151#endif /* _WIN32 */
152
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100153static struct qemu_alarm_timer alarm_timers[] = {
154#ifndef _WIN32
155#ifdef __linux__
156 {"dynticks", dynticks_start_timer,
Stefan Weilcd0544e2011-04-10 20:15:09 +0200157 dynticks_stop_timer, dynticks_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100158#endif
Paolo Bonzini84682832011-06-09 13:10:25 +0200159 {"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100160#else
Paolo Bonzinicca5de72011-11-09 12:46:56 +0100161 {"mmtimer", mm_start_timer, mm_stop_timer, mm_rearm_timer},
Stefan Weilcd0544e2011-04-10 20:15:09 +0200162 {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100163#endif
164 {NULL, }
165};
166
167static void show_available_alarms(void)
168{
169 int i;
170
171 printf("Available alarm timers, in order of precedence:\n");
172 for (i = 0; alarm_timers[i].name; i++)
173 printf("%s\n", alarm_timers[i].name);
174}
175
176void configure_alarms(char const *opt)
177{
178 int i;
179 int cur = 0;
180 int count = ARRAY_SIZE(alarm_timers) - 1;
181 char *arg;
182 char *name;
183 struct qemu_alarm_timer tmp;
184
Peter Maydellc8057f92012-08-02 13:45:54 +0100185 if (is_help_option(opt)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100186 show_available_alarms();
187 exit(0);
188 }
189
Anthony Liguori7267c092011-08-20 22:09:37 -0500190 arg = g_strdup(opt);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100191
192 /* Reorder the array */
193 name = strtok(arg, ",");
194 while (name) {
195 for (i = 0; i < count && alarm_timers[i].name; i++) {
196 if (!strcmp(alarm_timers[i].name, name))
197 break;
198 }
199
200 if (i == count) {
201 fprintf(stderr, "Unknown clock %s\n", name);
202 goto next;
203 }
204
205 if (i < cur)
206 /* Ignore */
207 goto next;
208
209 /* Swap */
210 tmp = alarm_timers[i];
211 alarm_timers[i] = alarm_timers[cur];
212 alarm_timers[cur] = tmp;
213
214 cur++;
215next:
216 name = strtok(NULL, ",");
217 }
218
Anthony Liguori7267c092011-08-20 22:09:37 -0500219 g_free(arg);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100220
221 if (cur) {
222 /* Disable remaining timers */
223 for (i = cur; i < count; i++)
224 alarm_timers[i].name = NULL;
225 } else {
226 show_available_alarms();
227 exit(1);
228 }
229}
230
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100231QEMUClock *rt_clock;
232QEMUClock *vm_clock;
233QEMUClock *host_clock;
234
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100235static QEMUClock *qemu_new_clock(int type)
236{
237 QEMUClock *clock;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200238
Anthony Liguori7267c092011-08-20 22:09:37 -0500239 clock = g_malloc0(sizeof(QEMUClock));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100240 clock->type = type;
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200241 clock->enabled = true;
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200242 clock->last = INT64_MIN;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200243 notifier_list_init(&clock->reset_notifiers);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100244 return clock;
245}
246
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200247void qemu_clock_enable(QEMUClock *clock, bool enabled)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100248{
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200249 bool old = clock->enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100250 clock->enabled = enabled;
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200251 if (enabled && !old) {
252 qemu_rearm_alarm_timer(alarm_timer);
253 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100254}
255
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200256int64_t qemu_clock_has_timers(QEMUClock *clock)
257{
258 return !!clock->active_timers;
259}
260
261int64_t qemu_clock_expired(QEMUClock *clock)
262{
263 return (clock->active_timers &&
264 clock->active_timers->expire_time < qemu_get_clock_ns(clock));
265}
266
267int64_t qemu_clock_deadline(QEMUClock *clock)
268{
269 /* To avoid problems with overflow limit this to 2^32. */
270 int64_t delta = INT32_MAX;
271
272 if (clock->active_timers) {
273 delta = clock->active_timers->expire_time - qemu_get_clock_ns(clock);
274 }
275 if (delta < 0) {
276 delta = 0;
277 }
278 return delta;
279}
280
Paolo Bonzini4a998742011-03-11 16:33:58 +0100281QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
282 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100283{
284 QEMUTimer *ts;
285
Anthony Liguori7267c092011-08-20 22:09:37 -0500286 ts = g_malloc0(sizeof(QEMUTimer));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100287 ts->clock = clock;
288 ts->cb = cb;
289 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100290 ts->scale = scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100291 return ts;
292}
293
294void qemu_free_timer(QEMUTimer *ts)
295{
Anthony Liguori7267c092011-08-20 22:09:37 -0500296 g_free(ts);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100297}
298
299/* stop a timer, but do not dealloc it */
300void qemu_del_timer(QEMUTimer *ts)
301{
302 QEMUTimer **pt, *t;
303
304 /* NOTE: this code must be signal safe because
305 qemu_timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200306 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100307 for(;;) {
308 t = *pt;
309 if (!t)
310 break;
311 if (t == ts) {
312 *pt = t->next;
313 break;
314 }
315 pt = &t->next;
316 }
317}
318
319/* modify the current timer so that it will be fired when current_time
320 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200321void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100322{
323 QEMUTimer **pt, *t;
324
325 qemu_del_timer(ts);
326
327 /* add the timer in the sorted list */
328 /* NOTE: this code must be signal safe because
329 qemu_timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200330 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100331 for(;;) {
332 t = *pt;
Stefan Weil45c7b372011-03-24 21:31:24 +0100333 if (!qemu_timer_expired_ns(t, expire_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100334 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100335 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100336 pt = &t->next;
337 }
338 ts->expire_time = expire_time;
339 ts->next = *pt;
340 *pt = ts;
341
342 /* Rearm if necessary */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200343 if (pt == &ts->clock->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100344 if (!alarm_timer->pending) {
345 qemu_rearm_alarm_timer(alarm_timer);
346 }
347 /* Interrupt execution to force deadline recalculation. */
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200348 qemu_clock_warp(ts->clock);
349 if (use_icount) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100350 qemu_notify_event();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200351 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100352 }
353}
354
Paolo Bonzini4a998742011-03-11 16:33:58 +0100355void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
356{
357 qemu_mod_timer_ns(ts, expire_time * ts->scale);
358}
359
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200360bool qemu_timer_pending(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100361{
362 QEMUTimer *t;
Paolo Bonzini688eb382011-09-13 11:42:26 +0200363 for (t = ts->clock->active_timers; t != NULL; t = t->next) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200364 if (t == ts) {
365 return true;
366 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100367 }
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200368 return false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100369}
370
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200371bool qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100372{
Stefan Weil45c7b372011-03-24 21:31:24 +0100373 return qemu_timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100374}
375
Paolo Bonzini8156be52012-03-28 15:42:04 +0200376void qemu_run_timers(QEMUClock *clock)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100377{
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200378 QEMUTimer *ts;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100379 int64_t current_time;
380
381 if (!clock->enabled)
382 return;
383
Paolo Bonzini4a998742011-03-11 16:33:58 +0100384 current_time = qemu_get_clock_ns(clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100385 for(;;) {
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200386 ts = clock->active_timers;
Stefan Weil45c7b372011-03-24 21:31:24 +0100387 if (!qemu_timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100388 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100389 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100390 /* remove timer from the list before calling the callback */
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200391 clock->active_timers = ts->next;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100392 ts->next = NULL;
393
394 /* run the callback (the timer list can be modified) */
395 ts->cb(ts->opaque);
396 }
397}
398
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100399int64_t qemu_get_clock_ns(QEMUClock *clock)
400{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200401 int64_t now, last;
402
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100403 switch(clock->type) {
404 case QEMU_CLOCK_REALTIME:
405 return get_clock();
406 default:
407 case QEMU_CLOCK_VIRTUAL:
408 if (use_icount) {
409 return cpu_get_icount();
410 } else {
411 return cpu_get_clock();
412 }
413 case QEMU_CLOCK_HOST:
Jan Kiszka691a0c92011-06-20 14:06:27 +0200414 now = get_clock_realtime();
415 last = clock->last;
416 clock->last = now;
417 if (now < last) {
418 notifier_list_notify(&clock->reset_notifiers, &now);
419 }
420 return now;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100421 }
422}
423
Jan Kiszka691a0c92011-06-20 14:06:27 +0200424void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
425{
426 notifier_list_add(&clock->reset_notifiers, notifier);
427}
428
429void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
430{
Paolo Bonzini31552522012-01-13 17:34:01 +0100431 notifier_remove(notifier);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200432}
433
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100434void init_clocks(void)
435{
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100436 if (!rt_clock) {
437 rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
438 vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
439 host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
440 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100441}
442
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200443uint64_t qemu_timer_expire_time_ns(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100444{
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200445 return qemu_timer_pending(ts) ? ts->expire_time : -1;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100446}
447
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100448void qemu_run_all_timers(void)
449{
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200450 alarm_timer->pending = false;
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100451
Peter Portante158fd3c2012-04-05 11:00:45 -0400452 /* vm time timers */
453 qemu_run_timers(vm_clock);
454 qemu_run_timers(rt_clock);
455 qemu_run_timers(host_clock);
456
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100457 /* rearm timer, if not periodic */
458 if (alarm_timer->expired) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200459 alarm_timer->expired = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100460 qemu_rearm_alarm_timer(alarm_timer);
461 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100462}
463
464#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100465static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100466#else
467static void host_alarm_handler(int host_signum)
468#endif
469{
470 struct qemu_alarm_timer *t = alarm_timer;
471 if (!t)
472 return;
473
Stefan Weil82051992012-04-20 11:27:24 +0200474 t->expired = true;
475 t->pending = true;
476 qemu_notify_event();
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100477}
478
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100479#if defined(__linux__)
480
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200481#include "compatfd.h"
482
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100483static int dynticks_start_timer(struct qemu_alarm_timer *t)
484{
485 struct sigevent ev;
486 timer_t host_timer;
487 struct sigaction act;
488
489 sigfillset(&act.sa_mask);
490 act.sa_flags = 0;
491 act.sa_handler = host_alarm_handler;
492
493 sigaction(SIGALRM, &act, NULL);
494
495 /*
496 * Initialize ev struct to 0 to avoid valgrind complaining
497 * about uninitialized data in timer_create call
498 */
499 memset(&ev, 0, sizeof(ev));
500 ev.sigev_value.sival_int = 0;
501 ev.sigev_notify = SIGEV_SIGNAL;
Richard Henderson1e9737d2012-10-23 07:33:00 +1000502#ifdef CONFIG_SIGEV_THREAD_ID
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200503 if (qemu_signalfd_available()) {
504 ev.sigev_notify = SIGEV_THREAD_ID;
505 ev._sigev_un._tid = qemu_get_thread_id();
506 }
Richard Henderson1e9737d2012-10-23 07:33:00 +1000507#endif /* CONFIG_SIGEV_THREAD_ID */
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100508 ev.sigev_signo = SIGALRM;
509
510 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
511 perror("timer_create");
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100512 return -1;
513 }
514
Stefan Weilcd0544e2011-04-10 20:15:09 +0200515 t->timer = host_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100516
517 return 0;
518}
519
520static void dynticks_stop_timer(struct qemu_alarm_timer *t)
521{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200522 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100523
524 timer_delete(host_timer);
525}
526
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100527static void dynticks_rearm_timer(struct qemu_alarm_timer *t,
528 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100529{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200530 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100531 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100532 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100533
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100534 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
535 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100536
537 /* check whether a timer is already running */
538 if (timer_gettime(host_timer, &timeout)) {
539 perror("gettime");
540 fprintf(stderr, "Internal timer error: aborting\n");
541 exit(1);
542 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100543 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
544 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100545 return;
546
547 timeout.it_interval.tv_sec = 0;
548 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100549 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
550 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100551 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
552 perror("settime");
553 fprintf(stderr, "Internal timer error: aborting\n");
554 exit(1);
555 }
556}
557
558#endif /* defined(__linux__) */
559
Stefan Weilf26e5a52011-02-04 22:01:32 +0100560#if !defined(_WIN32)
561
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100562static int unix_start_timer(struct qemu_alarm_timer *t)
563{
564 struct sigaction act;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100565
566 /* timer signal */
567 sigfillset(&act.sa_mask);
568 act.sa_flags = 0;
569 act.sa_handler = host_alarm_handler;
570
571 sigaction(SIGALRM, &act, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200572 return 0;
573}
574
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100575static void unix_rearm_timer(struct qemu_alarm_timer *t,
576 int64_t nearest_delta_ns)
Paolo Bonzini84682832011-06-09 13:10:25 +0200577{
578 struct itimerval itv;
Paolo Bonzini84682832011-06-09 13:10:25 +0200579 int err;
580
Paolo Bonzini84682832011-06-09 13:10:25 +0200581 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
582 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100583
584 itv.it_interval.tv_sec = 0;
Paolo Bonzini84682832011-06-09 13:10:25 +0200585 itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */
586 itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
587 itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100588 err = setitimer(ITIMER_REAL, &itv, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200589 if (err) {
590 perror("setitimer");
591 fprintf(stderr, "Internal timer error: aborting\n");
592 exit(1);
593 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100594}
595
596static void unix_stop_timer(struct qemu_alarm_timer *t)
597{
598 struct itimerval itv;
599
600 memset(&itv, 0, sizeof(itv));
601 setitimer(ITIMER_REAL, &itv, NULL);
602}
603
604#endif /* !defined(_WIN32) */
605
606
607#ifdef _WIN32
608
Stefan Weil2f9cba02011-04-05 18:34:21 +0200609static MMRESULT mm_timer;
Stefan Weil40f08e82012-04-27 05:34:40 +0000610static TIMECAPS mm_tc;
Stefan Weil2f9cba02011-04-05 18:34:21 +0200611
612static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
613 DWORD_PTR dwUser, DWORD_PTR dw1,
614 DWORD_PTR dw2)
615{
616 struct qemu_alarm_timer *t = alarm_timer;
617 if (!t) {
618 return;
619 }
Stefan Weil82051992012-04-20 11:27:24 +0200620 t->expired = true;
621 t->pending = true;
622 qemu_notify_event();
Stefan Weil2f9cba02011-04-05 18:34:21 +0200623}
624
625static int mm_start_timer(struct qemu_alarm_timer *t)
626{
Stefan Weil40f08e82012-04-27 05:34:40 +0000627 timeGetDevCaps(&mm_tc, sizeof(mm_tc));
Stefan Weil2f9cba02011-04-05 18:34:21 +0200628
Stefan Weil40f08e82012-04-27 05:34:40 +0000629 timeBeginPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200630
Stefan Weil40f08e82012-04-27 05:34:40 +0000631 mm_timer = timeSetEvent(mm_tc.wPeriodMin, /* interval (ms) */
632 mm_tc.wPeriodMin, /* resolution */
Stefan Weil2f9cba02011-04-05 18:34:21 +0200633 mm_alarm_handler, /* function */
634 (DWORD_PTR)t, /* parameter */
Stefan Weil82051992012-04-20 11:27:24 +0200635 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200636
637 if (!mm_timer) {
Stefan Weil52ef6512012-05-08 19:14:43 +0200638 fprintf(stderr, "Failed to initialize win32 alarm timer\n");
Stefan Weil40f08e82012-04-27 05:34:40 +0000639 timeEndPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200640 return -1;
641 }
642
643 return 0;
644}
645
646static void mm_stop_timer(struct qemu_alarm_timer *t)
647{
648 timeKillEvent(mm_timer);
Stefan Weil40f08e82012-04-27 05:34:40 +0000649 timeEndPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200650}
651
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100652static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta)
Stefan Weil2f9cba02011-04-05 18:34:21 +0200653{
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100654 int64_t nearest_delta_ms = delta / 1000000;
Stefan Weil40f08e82012-04-27 05:34:40 +0000655 if (nearest_delta_ms < mm_tc.wPeriodMin) {
656 nearest_delta_ms = mm_tc.wPeriodMin;
657 } else if (nearest_delta_ms > mm_tc.wPeriodMax) {
658 nearest_delta_ms = mm_tc.wPeriodMax;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100659 }
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100660
661 timeKillEvent(mm_timer);
Stefan Weil40f08e82012-04-27 05:34:40 +0000662 mm_timer = timeSetEvent((UINT)nearest_delta_ms,
663 mm_tc.wPeriodMin,
Stefan Weil2f9cba02011-04-05 18:34:21 +0200664 mm_alarm_handler,
665 (DWORD_PTR)t,
666 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
667
668 if (!mm_timer) {
Stefan Weil52ef6512012-05-08 19:14:43 +0200669 fprintf(stderr, "Failed to re-arm win32 alarm timer\n");
Stefan Weil40f08e82012-04-27 05:34:40 +0000670 timeEndPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200671 exit(1);
672 }
673}
674
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100675static int win32_start_timer(struct qemu_alarm_timer *t)
676{
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100677 HANDLE hTimer;
678 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100679
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100680 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
681 is zero) that has already expired, the timer is not updated. Since
682 creating a new timer is relatively expensive, set a bogus one-hour
683 interval in the dynticks case. */
684 success = CreateTimerQueueTimer(&hTimer,
685 NULL,
686 host_alarm_handler,
687 t,
688 1,
Stefan Weil82051992012-04-20 11:27:24 +0200689 3600000,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100690 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100691
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100692 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100693 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
694 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100695 return -1;
696 }
697
Stefan Weilcd0544e2011-04-10 20:15:09 +0200698 t->timer = hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100699 return 0;
700}
701
702static void win32_stop_timer(struct qemu_alarm_timer *t)
703{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200704 HANDLE hTimer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100705
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100706 if (hTimer) {
707 DeleteTimerQueueTimer(NULL, hTimer, NULL);
708 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100709}
710
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100711static void win32_rearm_timer(struct qemu_alarm_timer *t,
712 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100713{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200714 HANDLE hTimer = t->timer;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100715 int64_t nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100716 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100717
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100718 nearest_delta_ms = nearest_delta_ns / 1000000;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +0100719 if (nearest_delta_ms < 1) {
720 nearest_delta_ms = 1;
721 }
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100722 /* ULONG_MAX can be 32 bit */
723 if (nearest_delta_ms > ULONG_MAX) {
724 nearest_delta_ms = ULONG_MAX;
725 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100726 success = ChangeTimerQueueTimer(NULL,
727 hTimer,
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100728 (unsigned long) nearest_delta_ms,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100729 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100730
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100731 if (!success) {
732 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100733 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100734 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100735 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100736
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100737}
738
739#endif /* _WIN32 */
740
Paolo Bonzini4260a732011-09-19 10:18:51 +0200741static void quit_timers(void)
742{
743 struct qemu_alarm_timer *t = alarm_timer;
744 alarm_timer = NULL;
745 t->stop(t);
746}
747
Stefan Weil253ecf82012-11-04 21:42:08 +0100748#ifdef CONFIG_POSIX
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100749static void reinit_timers(void)
750{
751 struct qemu_alarm_timer *t = alarm_timer;
752 t->stop(t);
753 if (t->start(t)) {
754 fprintf(stderr, "Internal timer error: aborting\n");
755 exit(1);
756 }
757 qemu_rearm_alarm_timer(t);
758}
Stefan Weil253ecf82012-11-04 21:42:08 +0100759#endif /* CONFIG_POSIX */
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100760
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100761int init_timer_alarm(void)
762{
763 struct qemu_alarm_timer *t = NULL;
764 int i, err = -1;
765
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100766 if (alarm_timer) {
767 return 0;
768 }
769
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100770 for (i = 0; alarm_timers[i].name; i++) {
771 t = &alarm_timers[i];
772
773 err = t->start(t);
774 if (!err)
775 break;
776 }
777
778 if (err) {
779 err = -ENOENT;
780 goto fail;
781 }
782
Paolo Bonzini4260a732011-09-19 10:18:51 +0200783 atexit(quit_timers);
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100784#ifdef CONFIG_POSIX
785 pthread_atfork(NULL, NULL, reinit_timers);
786#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100787 alarm_timer = t;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100788 return 0;
789
790fail:
791 return err;
792}
793