blob: b045184c048a7273903724ca5afc2f008164bbed [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
Paolo Bonzini9c17d612012-12-17 18:20:04 +010025#include "sysemu/sysemu.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010026#include "monitor/monitor.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010027#include "ui/console.h"
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010028
29#include "hw/hw.h"
30
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010031#include "qemu/timer.h"
Anthony Liguori30ea8332012-11-02 16:12:53 -050032#ifdef CONFIG_POSIX
33#include <pthread.h>
34#endif
Stefan Weilbff9f8b2012-04-20 10:27:06 +020035
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010036#ifdef _WIN32
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010037#include <mmsystem.h>
38#endif
39
Alex Bligh4e0c6522013-08-21 16:02:43 +010040#ifdef CONFIG_PPOLL
41#include <poll.h>
42#endif
43
Alex Blighcd758dd2013-08-21 16:02:44 +010044#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
45#include <sys/prctl.h>
46#endif
47
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010048/***********************************************************/
49/* timers */
50
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010051struct QEMUClock {
Alex Blighff83c662013-08-21 16:02:46 +010052 QEMUTimerList *main_loop_timerlist;
53 QLIST_HEAD(, QEMUTimerList) timerlists;
Jan Kiszka691a0c92011-06-20 14:06:27 +020054
55 NotifierList reset_notifiers;
56 int64_t last;
Stefan Weil9a14b292012-04-20 11:51:58 +020057
Alex Blighff83c662013-08-21 16:02:46 +010058 QEMUClockType type;
Stefan Weil9a14b292012-04-20 11:51:58 +020059 bool enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010060};
61
Alex Blighff83c662013-08-21 16:02:46 +010062QEMUClock *qemu_clocks[QEMU_CLOCK_MAX];
63
64/* A QEMUTimerList is a list of timers attached to a clock. More
65 * than one QEMUTimerList can be attached to each clock, for instance
66 * used by different AioContexts / threads. Each clock also has
67 * a list of the QEMUTimerLists associated with it, in order that
68 * reenabling the clock can call all the notifiers.
69 */
70
71struct QEMUTimerList {
Stefan Weil9a14b292012-04-20 11:51:58 +020072 QEMUClock *clock;
Alex Blighff83c662013-08-21 16:02:46 +010073 QEMUTimer *active_timers;
74 QLIST_ENTRY(QEMUTimerList) list;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010075};
76
77struct qemu_alarm_timer {
78 char const *name;
79 int (*start)(struct qemu_alarm_timer *t);
80 void (*stop)(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010081 void (*rearm)(struct qemu_alarm_timer *t, int64_t nearest_delta_ns);
Stefan Weilcd0544e2011-04-10 20:15:09 +020082#if defined(__linux__)
Stefan Weilcd0544e2011-04-10 20:15:09 +020083 timer_t timer;
Stefan Weil9a14b292012-04-20 11:51:58 +020084 int fd;
Stefan Weilcd0544e2011-04-10 20:15:09 +020085#elif defined(_WIN32)
86 HANDLE timer;
87#endif
Stefan Weil5e1ec7b2012-04-20 10:45:48 +020088 bool expired;
89 bool pending;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010090};
91
92static struct qemu_alarm_timer *alarm_timer;
93
Alex Blighe93379b2013-08-21 16:02:39 +010094static bool timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
Stefan Weil45c7b372011-03-24 21:31:24 +010095{
96 return timer_head && (timer_head->expire_time <= current_time);
97}
98
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010099static int64_t qemu_next_alarm_deadline(void)
100{
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +0100101 int64_t delta = INT64_MAX;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100102 int64_t rtdelta;
Alex Blighff83c662013-08-21 16:02:46 +0100103 int64_t hdelta;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100104
Alex Blighff83c662013-08-21 16:02:46 +0100105 if (!use_icount && vm_clock->enabled &&
106 vm_clock->main_loop_timerlist->active_timers) {
107 delta = vm_clock->main_loop_timerlist->active_timers->expire_time -
108 qemu_get_clock_ns(vm_clock);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100109 }
Alex Blighff83c662013-08-21 16:02:46 +0100110 if (host_clock->enabled &&
111 host_clock->main_loop_timerlist->active_timers) {
112 hdelta = host_clock->main_loop_timerlist->active_timers->expire_time -
113 qemu_get_clock_ns(host_clock);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100114 if (hdelta < delta) {
115 delta = hdelta;
116 }
117 }
Alex Blighff83c662013-08-21 16:02:46 +0100118 if (rt_clock->enabled &&
119 rt_clock->main_loop_timerlist->active_timers) {
120 rtdelta = (rt_clock->main_loop_timerlist->active_timers->expire_time -
121 qemu_get_clock_ns(rt_clock));
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100122 if (rtdelta < delta) {
123 delta = rtdelta;
124 }
125 }
126
127 return delta;
128}
129
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100130static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
131{
Stefano Stabellini82274212012-05-29 03:35:24 +0000132 int64_t nearest_delta_ns = qemu_next_alarm_deadline();
133 if (nearest_delta_ns < INT64_MAX) {
134 t->rearm(t, nearest_delta_ns);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100135 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100136}
137
Paolo Bonzini9c132462011-02-03 14:48:59 +0100138/* TODO: MIN_TIMER_REARM_NS should be optimized */
139#define MIN_TIMER_REARM_NS 250000
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100140
141#ifdef _WIN32
142
Stefan Weil2f9cba02011-04-05 18:34:21 +0200143static int mm_start_timer(struct qemu_alarm_timer *t);
144static void mm_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100145static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200146
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100147static int win32_start_timer(struct qemu_alarm_timer *t);
148static void win32_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100149static void win32_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100150
151#else
152
153static int unix_start_timer(struct qemu_alarm_timer *t);
154static void unix_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100155static void unix_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100156
157#ifdef __linux__
158
159static int dynticks_start_timer(struct qemu_alarm_timer *t);
160static void dynticks_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100161static void dynticks_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100162
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100163#endif /* __linux__ */
164
165#endif /* _WIN32 */
166
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100167static struct qemu_alarm_timer alarm_timers[] = {
168#ifndef _WIN32
169#ifdef __linux__
170 {"dynticks", dynticks_start_timer,
Stefan Weilcd0544e2011-04-10 20:15:09 +0200171 dynticks_stop_timer, dynticks_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100172#endif
Paolo Bonzini84682832011-06-09 13:10:25 +0200173 {"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100174#else
Paolo Bonzinicca5de72011-11-09 12:46:56 +0100175 {"mmtimer", mm_start_timer, mm_stop_timer, mm_rearm_timer},
Stefan Weilcd0544e2011-04-10 20:15:09 +0200176 {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100177#endif
178 {NULL, }
179};
180
181static void show_available_alarms(void)
182{
183 int i;
184
185 printf("Available alarm timers, in order of precedence:\n");
186 for (i = 0; alarm_timers[i].name; i++)
187 printf("%s\n", alarm_timers[i].name);
188}
189
190void configure_alarms(char const *opt)
191{
192 int i;
193 int cur = 0;
194 int count = ARRAY_SIZE(alarm_timers) - 1;
195 char *arg;
196 char *name;
197 struct qemu_alarm_timer tmp;
198
Peter Maydellc8057f92012-08-02 13:45:54 +0100199 if (is_help_option(opt)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100200 show_available_alarms();
201 exit(0);
202 }
203
Anthony Liguori7267c092011-08-20 22:09:37 -0500204 arg = g_strdup(opt);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100205
206 /* Reorder the array */
207 name = strtok(arg, ",");
208 while (name) {
209 for (i = 0; i < count && alarm_timers[i].name; i++) {
210 if (!strcmp(alarm_timers[i].name, name))
211 break;
212 }
213
214 if (i == count) {
215 fprintf(stderr, "Unknown clock %s\n", name);
216 goto next;
217 }
218
219 if (i < cur)
220 /* Ignore */
221 goto next;
222
223 /* Swap */
224 tmp = alarm_timers[i];
225 alarm_timers[i] = alarm_timers[cur];
226 alarm_timers[cur] = tmp;
227
228 cur++;
229next:
230 name = strtok(NULL, ",");
231 }
232
Anthony Liguori7267c092011-08-20 22:09:37 -0500233 g_free(arg);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100234
235 if (cur) {
236 /* Disable remaining timers */
237 for (i = cur; i < count; i++)
238 alarm_timers[i].name = NULL;
239 } else {
240 show_available_alarms();
241 exit(1);
242 }
243}
244
Alex Blighff83c662013-08-21 16:02:46 +0100245static QEMUTimerList *timerlist_new_from_clock(QEMUClock *clock)
246{
247 QEMUTimerList *timer_list;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100248
Alex Blighff83c662013-08-21 16:02:46 +0100249 /* Assert if we do not have a clock. If you see this
250 * assertion in means that the clocks have not been
251 * initialised before a timerlist is needed. This
252 * normally happens if an AioContext is used before
253 * init_clocks() is called within main().
254 */
255 assert(clock);
256
257 timer_list = g_malloc0(sizeof(QEMUTimerList));
258 timer_list->clock = clock;
259 QLIST_INSERT_HEAD(&clock->timerlists, timer_list, list);
260 return timer_list;
261}
262
263QEMUTimerList *timerlist_new(QEMUClockType type)
264{
265 return timerlist_new_from_clock(qemu_clock_ptr(type));
266}
267
268void timerlist_free(QEMUTimerList *timer_list)
269{
270 assert(!timerlist_has_timers(timer_list));
271 if (timer_list->clock) {
272 QLIST_REMOVE(timer_list, list);
273 if (timer_list->clock->main_loop_timerlist == timer_list) {
274 timer_list->clock->main_loop_timerlist = NULL;
275 }
276 }
277 g_free(timer_list);
278}
279
280static QEMUClock *qemu_clock_new(QEMUClockType type)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100281{
282 QEMUClock *clock;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200283
Anthony Liguori7267c092011-08-20 22:09:37 -0500284 clock = g_malloc0(sizeof(QEMUClock));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100285 clock->type = type;
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200286 clock->enabled = true;
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200287 clock->last = INT64_MIN;
Alex Blighff83c662013-08-21 16:02:46 +0100288 QLIST_INIT(&clock->timerlists);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200289 notifier_list_init(&clock->reset_notifiers);
Alex Blighff83c662013-08-21 16:02:46 +0100290 clock->main_loop_timerlist = timerlist_new_from_clock(clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100291 return clock;
292}
293
Alex Blighff83c662013-08-21 16:02:46 +0100294bool qemu_clock_use_for_deadline(QEMUClock *clock)
295{
296 return !(use_icount && (clock->type == QEMU_CLOCK_VIRTUAL));
297}
298
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200299void qemu_clock_enable(QEMUClock *clock, bool enabled)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100300{
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200301 bool old = clock->enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100302 clock->enabled = enabled;
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200303 if (enabled && !old) {
304 qemu_rearm_alarm_timer(alarm_timer);
305 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100306}
307
Alex Blighff83c662013-08-21 16:02:46 +0100308bool timerlist_has_timers(QEMUTimerList *timer_list)
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200309{
Alex Blighff83c662013-08-21 16:02:46 +0100310 return !!timer_list->active_timers;
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200311}
312
Alex Blighff83c662013-08-21 16:02:46 +0100313bool qemu_clock_has_timers(QEMUClock *clock)
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200314{
Alex Blighff83c662013-08-21 16:02:46 +0100315 return timerlist_has_timers(clock->main_loop_timerlist);
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200316}
317
Alex Blighff83c662013-08-21 16:02:46 +0100318bool timerlist_expired(QEMUTimerList *timer_list)
319{
320 return (timer_list->active_timers &&
321 timer_list->active_timers->expire_time <
322 qemu_get_clock_ns(timer_list->clock));
323}
324
325bool qemu_clock_expired(QEMUClock *clock)
326{
327 return timerlist_expired(clock->main_loop_timerlist);
328}
329
330int64_t timerlist_deadline(QEMUTimerList *timer_list)
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200331{
332 /* To avoid problems with overflow limit this to 2^32. */
333 int64_t delta = INT32_MAX;
334
Alex Blighff83c662013-08-21 16:02:46 +0100335 if (timer_list->clock->enabled && timer_list->active_timers) {
336 delta = timer_list->active_timers->expire_time -
337 qemu_get_clock_ns(timer_list->clock);
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200338 }
339 if (delta < 0) {
340 delta = 0;
341 }
342 return delta;
343}
344
Alex Blighff83c662013-08-21 16:02:46 +0100345int64_t qemu_clock_deadline(QEMUClock *clock)
346{
347 return timerlist_deadline(clock->main_loop_timerlist);
348}
349
Alex Bligh02a03a92013-08-21 16:02:41 +0100350/*
351 * As above, but return -1 for no deadline, and do not cap to 2^32
352 * as we know the result is always positive.
353 */
354
Alex Blighff83c662013-08-21 16:02:46 +0100355int64_t timerlist_deadline_ns(QEMUTimerList *timer_list)
Alex Bligh02a03a92013-08-21 16:02:41 +0100356{
357 int64_t delta;
358
Alex Blighff83c662013-08-21 16:02:46 +0100359 if (!timer_list->clock->enabled || !timer_list->active_timers) {
Alex Bligh02a03a92013-08-21 16:02:41 +0100360 return -1;
361 }
362
Alex Blighff83c662013-08-21 16:02:46 +0100363 delta = timer_list->active_timers->expire_time -
364 qemu_get_clock_ns(timer_list->clock);
Alex Bligh02a03a92013-08-21 16:02:41 +0100365
366 if (delta <= 0) {
367 return 0;
368 }
369
370 return delta;
371}
372
Alex Blighff83c662013-08-21 16:02:46 +0100373int64_t qemu_clock_deadline_ns(QEMUClock *clock)
374{
375 return timerlist_deadline_ns(clock->main_loop_timerlist);
376}
377
378QEMUClock *timerlist_get_clock(QEMUTimerList *timer_list)
379{
380 return timer_list->clock;
381}
382
383QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClock *clock)
384{
385 return clock->main_loop_timerlist;
386}
387
Alex Bligh02a03a92013-08-21 16:02:41 +0100388/* Transition function to convert a nanosecond timeout to ms
389 * This is used where a system does not support ppoll
390 */
391int qemu_timeout_ns_to_ms(int64_t ns)
392{
393 int64_t ms;
394 if (ns < 0) {
395 return -1;
396 }
397
398 if (!ns) {
399 return 0;
400 }
401
402 /* Always round up, because it's better to wait too long than to wait too
403 * little and effectively busy-wait
404 */
405 ms = (ns + SCALE_MS - 1) / SCALE_MS;
406
407 /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
408 if (ms > (int64_t) INT32_MAX) {
409 ms = INT32_MAX;
410 }
411
412 return (int) ms;
413}
414
415
Alex Bligh4e0c6522013-08-21 16:02:43 +0100416/* qemu implementation of g_poll which uses a nanosecond timeout but is
417 * otherwise identical to g_poll
418 */
419int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout)
420{
421#ifdef CONFIG_PPOLL
422 if (timeout < 0) {
423 return ppoll((struct pollfd *)fds, nfds, NULL, NULL);
424 } else {
425 struct timespec ts;
426 ts.tv_sec = timeout / 1000000000LL;
427 ts.tv_nsec = timeout % 1000000000LL;
428 return ppoll((struct pollfd *)fds, nfds, &ts, NULL);
429 }
430#else
431 return g_poll(fds, nfds, qemu_timeout_ns_to_ms(timeout));
432#endif
433}
434
435
Alex Blighff83c662013-08-21 16:02:46 +0100436void timer_init(QEMUTimer *ts,
437 QEMUTimerList *timer_list, int scale,
438 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100439{
Alex Blighff83c662013-08-21 16:02:46 +0100440 ts->timer_list = timer_list;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100441 ts->cb = cb;
442 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100443 ts->scale = scale;
Alex Blighff83c662013-08-21 16:02:46 +0100444}
445
446QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
447 QEMUTimerCB *cb, void *opaque)
448{
449 return timer_new_tl(clock->main_loop_timerlist,
450 scale, cb, opaque);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100451}
452
453void qemu_free_timer(QEMUTimer *ts)
454{
Anthony Liguori7267c092011-08-20 22:09:37 -0500455 g_free(ts);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100456}
457
458/* stop a timer, but do not dealloc it */
459void qemu_del_timer(QEMUTimer *ts)
460{
461 QEMUTimer **pt, *t;
462
463 /* NOTE: this code must be signal safe because
Alex Blighe93379b2013-08-21 16:02:39 +0100464 timer_expired() can be called from a signal. */
Alex Blighff83c662013-08-21 16:02:46 +0100465 pt = &ts->timer_list->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100466 for(;;) {
467 t = *pt;
468 if (!t)
469 break;
470 if (t == ts) {
471 *pt = t->next;
472 break;
473 }
474 pt = &t->next;
475 }
476}
477
478/* modify the current timer so that it will be fired when current_time
479 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200480void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100481{
482 QEMUTimer **pt, *t;
483
484 qemu_del_timer(ts);
485
486 /* add the timer in the sorted list */
487 /* NOTE: this code must be signal safe because
Alex Blighe93379b2013-08-21 16:02:39 +0100488 timer_expired() can be called from a signal. */
Alex Blighff83c662013-08-21 16:02:46 +0100489 pt = &ts->timer_list->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100490 for(;;) {
491 t = *pt;
Alex Blighe93379b2013-08-21 16:02:39 +0100492 if (!timer_expired_ns(t, expire_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100493 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100494 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100495 pt = &t->next;
496 }
497 ts->expire_time = expire_time;
498 ts->next = *pt;
499 *pt = ts;
500
501 /* Rearm if necessary */
Alex Blighff83c662013-08-21 16:02:46 +0100502 if (pt == &ts->timer_list->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100503 if (!alarm_timer->pending) {
504 qemu_rearm_alarm_timer(alarm_timer);
505 }
506 /* Interrupt execution to force deadline recalculation. */
Alex Blighff83c662013-08-21 16:02:46 +0100507 qemu_clock_warp(ts->timer_list->clock);
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200508 if (use_icount) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100509 qemu_notify_event();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200510 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100511 }
512}
513
Paolo Bonzini4a998742011-03-11 16:33:58 +0100514void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
515{
516 qemu_mod_timer_ns(ts, expire_time * ts->scale);
517}
518
Alex Blighe93379b2013-08-21 16:02:39 +0100519bool timer_pending(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100520{
521 QEMUTimer *t;
Alex Blighff83c662013-08-21 16:02:46 +0100522 for (t = ts->timer_list->active_timers; t != NULL; t = t->next) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200523 if (t == ts) {
524 return true;
525 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100526 }
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200527 return false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100528}
529
Alex Blighe93379b2013-08-21 16:02:39 +0100530bool timer_expired(QEMUTimer *timer_head, int64_t current_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100531{
Alex Blighe93379b2013-08-21 16:02:39 +0100532 return timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100533}
534
Alex Blighff83c662013-08-21 16:02:46 +0100535bool timerlist_run_timers(QEMUTimerList *timer_list)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100536{
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200537 QEMUTimer *ts;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100538 int64_t current_time;
Alex Blighf9a976b2013-08-21 16:02:45 +0100539 bool progress = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100540
Alex Blighff83c662013-08-21 16:02:46 +0100541 if (!timer_list->clock->enabled) {
Alex Blighf9a976b2013-08-21 16:02:45 +0100542 return progress;
Alex Blighff83c662013-08-21 16:02:46 +0100543 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100544
Alex Blighff83c662013-08-21 16:02:46 +0100545 current_time = qemu_get_clock_ns(timer_list->clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100546 for(;;) {
Alex Blighff83c662013-08-21 16:02:46 +0100547 ts = timer_list->active_timers;
Alex Blighe93379b2013-08-21 16:02:39 +0100548 if (!timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100549 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100550 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100551 /* remove timer from the list before calling the callback */
Alex Blighff83c662013-08-21 16:02:46 +0100552 timer_list->active_timers = ts->next;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100553 ts->next = NULL;
554
555 /* run the callback (the timer list can be modified) */
556 ts->cb(ts->opaque);
Alex Blighf9a976b2013-08-21 16:02:45 +0100557 progress = true;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100558 }
Alex Blighf9a976b2013-08-21 16:02:45 +0100559 return progress;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100560}
561
Alex Blighff83c662013-08-21 16:02:46 +0100562bool qemu_run_timers(QEMUClock *clock)
563{
564 return timerlist_run_timers(clock->main_loop_timerlist);
565}
566
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100567int64_t qemu_get_clock_ns(QEMUClock *clock)
568{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200569 int64_t now, last;
570
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100571 switch(clock->type) {
572 case QEMU_CLOCK_REALTIME:
573 return get_clock();
574 default:
575 case QEMU_CLOCK_VIRTUAL:
576 if (use_icount) {
577 return cpu_get_icount();
578 } else {
579 return cpu_get_clock();
580 }
581 case QEMU_CLOCK_HOST:
Jan Kiszka691a0c92011-06-20 14:06:27 +0200582 now = get_clock_realtime();
583 last = clock->last;
584 clock->last = now;
585 if (now < last) {
586 notifier_list_notify(&clock->reset_notifiers, &now);
587 }
588 return now;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100589 }
590}
591
Jan Kiszka691a0c92011-06-20 14:06:27 +0200592void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
593{
594 notifier_list_add(&clock->reset_notifiers, notifier);
595}
596
597void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
598{
Paolo Bonzini31552522012-01-13 17:34:01 +0100599 notifier_remove(notifier);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200600}
601
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100602void init_clocks(void)
603{
Alex Blighff83c662013-08-21 16:02:46 +0100604 QEMUClockType type;
605 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
606 if (!qemu_clocks[type]) {
607 qemu_clocks[type] = qemu_clock_new(type);
608 }
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100609 }
Alex Blighff83c662013-08-21 16:02:46 +0100610
Alex Blighcd758dd2013-08-21 16:02:44 +0100611#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
612 prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0);
613#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100614}
615
Alex Blighe93379b2013-08-21 16:02:39 +0100616uint64_t timer_expire_time_ns(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100617{
Alex Blighe93379b2013-08-21 16:02:39 +0100618 return timer_pending(ts) ? ts->expire_time : -1;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100619}
620
Alex Blighf9a976b2013-08-21 16:02:45 +0100621bool qemu_run_all_timers(void)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100622{
Alex Blighf9a976b2013-08-21 16:02:45 +0100623 bool progress = false;
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200624 alarm_timer->pending = false;
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100625
Peter Portante158fd3c2012-04-05 11:00:45 -0400626 /* vm time timers */
Alex Blighff83c662013-08-21 16:02:46 +0100627 QEMUClockType type;
628 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
629 progress |= qemu_run_timers(qemu_clock_ptr(type));
630 }
Peter Portante158fd3c2012-04-05 11:00:45 -0400631
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100632 /* rearm timer, if not periodic */
633 if (alarm_timer->expired) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200634 alarm_timer->expired = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100635 qemu_rearm_alarm_timer(alarm_timer);
636 }
Alex Blighf9a976b2013-08-21 16:02:45 +0100637
638 return progress;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100639}
640
641#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100642static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100643#else
644static void host_alarm_handler(int host_signum)
645#endif
646{
647 struct qemu_alarm_timer *t = alarm_timer;
648 if (!t)
649 return;
650
Stefan Weil82051992012-04-20 11:27:24 +0200651 t->expired = true;
652 t->pending = true;
653 qemu_notify_event();
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100654}
655
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100656#if defined(__linux__)
657
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100658#include "qemu/compatfd.h"
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200659
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100660static int dynticks_start_timer(struct qemu_alarm_timer *t)
661{
662 struct sigevent ev;
663 timer_t host_timer;
664 struct sigaction act;
665
666 sigfillset(&act.sa_mask);
667 act.sa_flags = 0;
668 act.sa_handler = host_alarm_handler;
669
670 sigaction(SIGALRM, &act, NULL);
671
672 /*
673 * Initialize ev struct to 0 to avoid valgrind complaining
674 * about uninitialized data in timer_create call
675 */
676 memset(&ev, 0, sizeof(ev));
677 ev.sigev_value.sival_int = 0;
678 ev.sigev_notify = SIGEV_SIGNAL;
Richard Henderson1e9737d2012-10-23 07:33:00 +1000679#ifdef CONFIG_SIGEV_THREAD_ID
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200680 if (qemu_signalfd_available()) {
681 ev.sigev_notify = SIGEV_THREAD_ID;
682 ev._sigev_un._tid = qemu_get_thread_id();
683 }
Richard Henderson1e9737d2012-10-23 07:33:00 +1000684#endif /* CONFIG_SIGEV_THREAD_ID */
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100685 ev.sigev_signo = SIGALRM;
686
687 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
688 perror("timer_create");
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100689 return -1;
690 }
691
Stefan Weilcd0544e2011-04-10 20:15:09 +0200692 t->timer = host_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100693
694 return 0;
695}
696
697static void dynticks_stop_timer(struct qemu_alarm_timer *t)
698{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200699 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100700
701 timer_delete(host_timer);
702}
703
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100704static void dynticks_rearm_timer(struct qemu_alarm_timer *t,
705 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100706{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200707 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100708 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100709 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100710
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100711 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
712 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100713
714 /* check whether a timer is already running */
715 if (timer_gettime(host_timer, &timeout)) {
716 perror("gettime");
717 fprintf(stderr, "Internal timer error: aborting\n");
718 exit(1);
719 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100720 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
721 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100722 return;
723
724 timeout.it_interval.tv_sec = 0;
725 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100726 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
727 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100728 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
729 perror("settime");
730 fprintf(stderr, "Internal timer error: aborting\n");
731 exit(1);
732 }
733}
734
735#endif /* defined(__linux__) */
736
Stefan Weilf26e5a52011-02-04 22:01:32 +0100737#if !defined(_WIN32)
738
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100739static int unix_start_timer(struct qemu_alarm_timer *t)
740{
741 struct sigaction act;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100742
743 /* timer signal */
744 sigfillset(&act.sa_mask);
745 act.sa_flags = 0;
746 act.sa_handler = host_alarm_handler;
747
748 sigaction(SIGALRM, &act, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200749 return 0;
750}
751
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100752static void unix_rearm_timer(struct qemu_alarm_timer *t,
753 int64_t nearest_delta_ns)
Paolo Bonzini84682832011-06-09 13:10:25 +0200754{
755 struct itimerval itv;
Paolo Bonzini84682832011-06-09 13:10:25 +0200756 int err;
757
Paolo Bonzini84682832011-06-09 13:10:25 +0200758 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
759 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100760
761 itv.it_interval.tv_sec = 0;
Paolo Bonzini84682832011-06-09 13:10:25 +0200762 itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */
763 itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
764 itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100765 err = setitimer(ITIMER_REAL, &itv, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200766 if (err) {
767 perror("setitimer");
768 fprintf(stderr, "Internal timer error: aborting\n");
769 exit(1);
770 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100771}
772
773static void unix_stop_timer(struct qemu_alarm_timer *t)
774{
775 struct itimerval itv;
776
777 memset(&itv, 0, sizeof(itv));
778 setitimer(ITIMER_REAL, &itv, NULL);
779}
780
781#endif /* !defined(_WIN32) */
782
783
784#ifdef _WIN32
785
Stefan Weil2f9cba02011-04-05 18:34:21 +0200786static MMRESULT mm_timer;
Stefan Weil40f08e82012-04-27 05:34:40 +0000787static TIMECAPS mm_tc;
Stefan Weil2f9cba02011-04-05 18:34:21 +0200788
789static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
790 DWORD_PTR dwUser, DWORD_PTR dw1,
791 DWORD_PTR dw2)
792{
793 struct qemu_alarm_timer *t = alarm_timer;
794 if (!t) {
795 return;
796 }
Stefan Weil82051992012-04-20 11:27:24 +0200797 t->expired = true;
798 t->pending = true;
799 qemu_notify_event();
Stefan Weil2f9cba02011-04-05 18:34:21 +0200800}
801
802static int mm_start_timer(struct qemu_alarm_timer *t)
803{
Stefan Weil40f08e82012-04-27 05:34:40 +0000804 timeGetDevCaps(&mm_tc, sizeof(mm_tc));
Stefan Weil2f9cba02011-04-05 18:34:21 +0200805 return 0;
806}
807
808static void mm_stop_timer(struct qemu_alarm_timer *t)
809{
Paolo Bonzini0727b862013-02-20 14:43:31 +0100810 if (mm_timer) {
811 timeKillEvent(mm_timer);
812 }
Stefan Weil2f9cba02011-04-05 18:34:21 +0200813}
814
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100815static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta)
Stefan Weil2f9cba02011-04-05 18:34:21 +0200816{
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100817 int64_t nearest_delta_ms = delta / 1000000;
Stefan Weil40f08e82012-04-27 05:34:40 +0000818 if (nearest_delta_ms < mm_tc.wPeriodMin) {
819 nearest_delta_ms = mm_tc.wPeriodMin;
820 } else if (nearest_delta_ms > mm_tc.wPeriodMax) {
821 nearest_delta_ms = mm_tc.wPeriodMax;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100822 }
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100823
Paolo Bonzini0727b862013-02-20 14:43:31 +0100824 if (mm_timer) {
825 timeKillEvent(mm_timer);
826 }
Stefan Weil40f08e82012-04-27 05:34:40 +0000827 mm_timer = timeSetEvent((UINT)nearest_delta_ms,
828 mm_tc.wPeriodMin,
Stefan Weil2f9cba02011-04-05 18:34:21 +0200829 mm_alarm_handler,
830 (DWORD_PTR)t,
831 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
832
833 if (!mm_timer) {
Stefan Weil52ef6512012-05-08 19:14:43 +0200834 fprintf(stderr, "Failed to re-arm win32 alarm timer\n");
Stefan Weil40f08e82012-04-27 05:34:40 +0000835 timeEndPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200836 exit(1);
837 }
838}
839
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100840static int win32_start_timer(struct qemu_alarm_timer *t)
841{
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100842 HANDLE hTimer;
843 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100844
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100845 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
846 is zero) that has already expired, the timer is not updated. Since
847 creating a new timer is relatively expensive, set a bogus one-hour
848 interval in the dynticks case. */
849 success = CreateTimerQueueTimer(&hTimer,
850 NULL,
851 host_alarm_handler,
852 t,
853 1,
Stefan Weil82051992012-04-20 11:27:24 +0200854 3600000,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100855 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100856
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100857 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100858 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
859 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100860 return -1;
861 }
862
Stefan Weilcd0544e2011-04-10 20:15:09 +0200863 t->timer = hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100864 return 0;
865}
866
867static void win32_stop_timer(struct qemu_alarm_timer *t)
868{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200869 HANDLE hTimer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100870
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100871 if (hTimer) {
872 DeleteTimerQueueTimer(NULL, hTimer, NULL);
873 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100874}
875
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100876static void win32_rearm_timer(struct qemu_alarm_timer *t,
877 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100878{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200879 HANDLE hTimer = t->timer;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100880 int64_t nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100881 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100882
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100883 nearest_delta_ms = nearest_delta_ns / 1000000;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +0100884 if (nearest_delta_ms < 1) {
885 nearest_delta_ms = 1;
886 }
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100887 /* ULONG_MAX can be 32 bit */
888 if (nearest_delta_ms > ULONG_MAX) {
889 nearest_delta_ms = ULONG_MAX;
890 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100891 success = ChangeTimerQueueTimer(NULL,
892 hTimer,
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100893 (unsigned long) nearest_delta_ms,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100894 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100895
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100896 if (!success) {
897 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100898 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100899 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100900 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100901
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100902}
903
904#endif /* _WIN32 */
905
Paolo Bonzini4260a732011-09-19 10:18:51 +0200906static void quit_timers(void)
907{
908 struct qemu_alarm_timer *t = alarm_timer;
909 alarm_timer = NULL;
910 t->stop(t);
911}
912
Stefan Weil253ecf82012-11-04 21:42:08 +0100913#ifdef CONFIG_POSIX
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100914static void reinit_timers(void)
915{
916 struct qemu_alarm_timer *t = alarm_timer;
917 t->stop(t);
918 if (t->start(t)) {
919 fprintf(stderr, "Internal timer error: aborting\n");
920 exit(1);
921 }
922 qemu_rearm_alarm_timer(t);
923}
Stefan Weil253ecf82012-11-04 21:42:08 +0100924#endif /* CONFIG_POSIX */
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100925
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100926int init_timer_alarm(void)
927{
928 struct qemu_alarm_timer *t = NULL;
929 int i, err = -1;
930
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100931 if (alarm_timer) {
932 return 0;
933 }
934
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100935 for (i = 0; alarm_timers[i].name; i++) {
936 t = &alarm_timers[i];
937
938 err = t->start(t);
939 if (!err)
940 break;
941 }
942
943 if (err) {
944 err = -ENOENT;
945 goto fail;
946 }
947
Paolo Bonzini4260a732011-09-19 10:18:51 +0200948 atexit(quit_timers);
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100949#ifdef CONFIG_POSIX
950 pthread_atfork(NULL, NULL, reinit_timers);
951#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100952 alarm_timer = t;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100953 return 0;
954
955fail:
956 return err;
957}
958