blob: ffdc28a264504d02c6e2443c156cf2cd56d095dc [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 Bligh754d6a52013-08-21 16:02:48 +010062QEMUTimerListGroup main_loop_tlg;
Alex Blighff83c662013-08-21 16:02:46 +010063QEMUClock *qemu_clocks[QEMU_CLOCK_MAX];
64
65/* A QEMUTimerList is a list of timers attached to a clock. More
66 * than one QEMUTimerList can be attached to each clock, for instance
67 * used by different AioContexts / threads. Each clock also has
68 * a list of the QEMUTimerLists associated with it, in order that
69 * reenabling the clock can call all the notifiers.
70 */
71
72struct QEMUTimerList {
Stefan Weil9a14b292012-04-20 11:51:58 +020073 QEMUClock *clock;
Alex Blighff83c662013-08-21 16:02:46 +010074 QEMUTimer *active_timers;
75 QLIST_ENTRY(QEMUTimerList) list;
Alex Blighd5541d82013-08-21 16:02:50 +010076 QEMUTimerListNotifyCB *notify_cb;
77 void *notify_opaque;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010078};
79
80struct qemu_alarm_timer {
81 char const *name;
82 int (*start)(struct qemu_alarm_timer *t);
83 void (*stop)(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010084 void (*rearm)(struct qemu_alarm_timer *t, int64_t nearest_delta_ns);
Stefan Weilcd0544e2011-04-10 20:15:09 +020085#if defined(__linux__)
Stefan Weilcd0544e2011-04-10 20:15:09 +020086 timer_t timer;
Stefan Weil9a14b292012-04-20 11:51:58 +020087 int fd;
Stefan Weilcd0544e2011-04-10 20:15:09 +020088#elif defined(_WIN32)
89 HANDLE timer;
90#endif
Stefan Weil5e1ec7b2012-04-20 10:45:48 +020091 bool expired;
92 bool pending;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010093};
94
95static struct qemu_alarm_timer *alarm_timer;
96
Alex Blighe93379b2013-08-21 16:02:39 +010097static bool timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
Stefan Weil45c7b372011-03-24 21:31:24 +010098{
99 return timer_head && (timer_head->expire_time <= current_time);
100}
101
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100102static int64_t qemu_next_alarm_deadline(void)
103{
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +0100104 int64_t delta = INT64_MAX;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100105 int64_t rtdelta;
Alex Blighff83c662013-08-21 16:02:46 +0100106 int64_t hdelta;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100107
Alex Blighff83c662013-08-21 16:02:46 +0100108 if (!use_icount && vm_clock->enabled &&
109 vm_clock->main_loop_timerlist->active_timers) {
110 delta = vm_clock->main_loop_timerlist->active_timers->expire_time -
111 qemu_get_clock_ns(vm_clock);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100112 }
Alex Blighff83c662013-08-21 16:02:46 +0100113 if (host_clock->enabled &&
114 host_clock->main_loop_timerlist->active_timers) {
115 hdelta = host_clock->main_loop_timerlist->active_timers->expire_time -
116 qemu_get_clock_ns(host_clock);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100117 if (hdelta < delta) {
118 delta = hdelta;
119 }
120 }
Alex Blighff83c662013-08-21 16:02:46 +0100121 if (rt_clock->enabled &&
122 rt_clock->main_loop_timerlist->active_timers) {
123 rtdelta = (rt_clock->main_loop_timerlist->active_timers->expire_time -
124 qemu_get_clock_ns(rt_clock));
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100125 if (rtdelta < delta) {
126 delta = rtdelta;
127 }
128 }
129
130 return delta;
131}
132
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100133static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
134{
Stefano Stabellini82274212012-05-29 03:35:24 +0000135 int64_t nearest_delta_ns = qemu_next_alarm_deadline();
136 if (nearest_delta_ns < INT64_MAX) {
137 t->rearm(t, nearest_delta_ns);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100138 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100139}
140
Paolo Bonzini9c132462011-02-03 14:48:59 +0100141/* TODO: MIN_TIMER_REARM_NS should be optimized */
142#define MIN_TIMER_REARM_NS 250000
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100143
144#ifdef _WIN32
145
Stefan Weil2f9cba02011-04-05 18:34:21 +0200146static int mm_start_timer(struct qemu_alarm_timer *t);
147static void mm_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100148static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200149
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100150static int win32_start_timer(struct qemu_alarm_timer *t);
151static void win32_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100152static void win32_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100153
154#else
155
156static int unix_start_timer(struct qemu_alarm_timer *t);
157static void unix_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100158static void unix_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100159
160#ifdef __linux__
161
162static int dynticks_start_timer(struct qemu_alarm_timer *t);
163static void dynticks_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100164static void dynticks_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100165
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100166#endif /* __linux__ */
167
168#endif /* _WIN32 */
169
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100170static struct qemu_alarm_timer alarm_timers[] = {
171#ifndef _WIN32
172#ifdef __linux__
173 {"dynticks", dynticks_start_timer,
Stefan Weilcd0544e2011-04-10 20:15:09 +0200174 dynticks_stop_timer, dynticks_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100175#endif
Paolo Bonzini84682832011-06-09 13:10:25 +0200176 {"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100177#else
Paolo Bonzinicca5de72011-11-09 12:46:56 +0100178 {"mmtimer", mm_start_timer, mm_stop_timer, mm_rearm_timer},
Stefan Weilcd0544e2011-04-10 20:15:09 +0200179 {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100180#endif
181 {NULL, }
182};
183
184static void show_available_alarms(void)
185{
186 int i;
187
188 printf("Available alarm timers, in order of precedence:\n");
189 for (i = 0; alarm_timers[i].name; i++)
190 printf("%s\n", alarm_timers[i].name);
191}
192
193void configure_alarms(char const *opt)
194{
195 int i;
196 int cur = 0;
197 int count = ARRAY_SIZE(alarm_timers) - 1;
198 char *arg;
199 char *name;
200 struct qemu_alarm_timer tmp;
201
Peter Maydellc8057f92012-08-02 13:45:54 +0100202 if (is_help_option(opt)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100203 show_available_alarms();
204 exit(0);
205 }
206
Anthony Liguori7267c092011-08-20 22:09:37 -0500207 arg = g_strdup(opt);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100208
209 /* Reorder the array */
210 name = strtok(arg, ",");
211 while (name) {
212 for (i = 0; i < count && alarm_timers[i].name; i++) {
213 if (!strcmp(alarm_timers[i].name, name))
214 break;
215 }
216
217 if (i == count) {
218 fprintf(stderr, "Unknown clock %s\n", name);
219 goto next;
220 }
221
222 if (i < cur)
223 /* Ignore */
224 goto next;
225
226 /* Swap */
227 tmp = alarm_timers[i];
228 alarm_timers[i] = alarm_timers[cur];
229 alarm_timers[cur] = tmp;
230
231 cur++;
232next:
233 name = strtok(NULL, ",");
234 }
235
Anthony Liguori7267c092011-08-20 22:09:37 -0500236 g_free(arg);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100237
238 if (cur) {
239 /* Disable remaining timers */
240 for (i = cur; i < count; i++)
241 alarm_timers[i].name = NULL;
242 } else {
243 show_available_alarms();
244 exit(1);
245 }
246}
247
Alex Blighd5541d82013-08-21 16:02:50 +0100248static QEMUTimerList *timerlist_new_from_clock(QEMUClock *clock,
249 QEMUTimerListNotifyCB *cb,
250 void *opaque)
Alex Blighff83c662013-08-21 16:02:46 +0100251{
252 QEMUTimerList *timer_list;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100253
Alex Blighff83c662013-08-21 16:02:46 +0100254 /* Assert if we do not have a clock. If you see this
255 * assertion in means that the clocks have not been
256 * initialised before a timerlist is needed. This
257 * normally happens if an AioContext is used before
258 * init_clocks() is called within main().
259 */
260 assert(clock);
261
262 timer_list = g_malloc0(sizeof(QEMUTimerList));
263 timer_list->clock = clock;
Alex Blighd5541d82013-08-21 16:02:50 +0100264 timer_list->notify_cb = cb;
265 timer_list->notify_opaque = opaque;
Alex Blighff83c662013-08-21 16:02:46 +0100266 QLIST_INSERT_HEAD(&clock->timerlists, timer_list, list);
267 return timer_list;
268}
269
Alex Blighd5541d82013-08-21 16:02:50 +0100270QEMUTimerList *timerlist_new(QEMUClockType type,
271 QEMUTimerListNotifyCB *cb, void *opaque)
Alex Blighff83c662013-08-21 16:02:46 +0100272{
Alex Blighd5541d82013-08-21 16:02:50 +0100273 return timerlist_new_from_clock(qemu_clock_ptr(type), cb, opaque);
Alex Blighff83c662013-08-21 16:02:46 +0100274}
275
276void timerlist_free(QEMUTimerList *timer_list)
277{
278 assert(!timerlist_has_timers(timer_list));
279 if (timer_list->clock) {
280 QLIST_REMOVE(timer_list, list);
281 if (timer_list->clock->main_loop_timerlist == timer_list) {
282 timer_list->clock->main_loop_timerlist = NULL;
283 }
284 }
285 g_free(timer_list);
286}
287
288static QEMUClock *qemu_clock_new(QEMUClockType type)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100289{
290 QEMUClock *clock;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200291
Anthony Liguori7267c092011-08-20 22:09:37 -0500292 clock = g_malloc0(sizeof(QEMUClock));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100293 clock->type = type;
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200294 clock->enabled = true;
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200295 clock->last = INT64_MIN;
Alex Blighff83c662013-08-21 16:02:46 +0100296 QLIST_INIT(&clock->timerlists);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200297 notifier_list_init(&clock->reset_notifiers);
Alex Blighd5541d82013-08-21 16:02:50 +0100298 clock->main_loop_timerlist = timerlist_new_from_clock(clock, NULL, NULL);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100299 return clock;
300}
301
Alex Blighff83c662013-08-21 16:02:46 +0100302bool qemu_clock_use_for_deadline(QEMUClock *clock)
303{
304 return !(use_icount && (clock->type == QEMU_CLOCK_VIRTUAL));
305}
306
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200307void qemu_clock_enable(QEMUClock *clock, bool enabled)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100308{
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200309 bool old = clock->enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100310 clock->enabled = enabled;
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200311 if (enabled && !old) {
312 qemu_rearm_alarm_timer(alarm_timer);
313 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100314}
315
Alex Blighff83c662013-08-21 16:02:46 +0100316bool timerlist_has_timers(QEMUTimerList *timer_list)
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200317{
Alex Blighff83c662013-08-21 16:02:46 +0100318 return !!timer_list->active_timers;
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200319}
320
Alex Blighff83c662013-08-21 16:02:46 +0100321bool qemu_clock_has_timers(QEMUClock *clock)
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200322{
Alex Blighff83c662013-08-21 16:02:46 +0100323 return timerlist_has_timers(clock->main_loop_timerlist);
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200324}
325
Alex Blighff83c662013-08-21 16:02:46 +0100326bool timerlist_expired(QEMUTimerList *timer_list)
327{
328 return (timer_list->active_timers &&
329 timer_list->active_timers->expire_time <
330 qemu_get_clock_ns(timer_list->clock));
331}
332
333bool qemu_clock_expired(QEMUClock *clock)
334{
335 return timerlist_expired(clock->main_loop_timerlist);
336}
337
338int64_t timerlist_deadline(QEMUTimerList *timer_list)
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200339{
340 /* To avoid problems with overflow limit this to 2^32. */
341 int64_t delta = INT32_MAX;
342
Alex Blighff83c662013-08-21 16:02:46 +0100343 if (timer_list->clock->enabled && timer_list->active_timers) {
344 delta = timer_list->active_timers->expire_time -
345 qemu_get_clock_ns(timer_list->clock);
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200346 }
347 if (delta < 0) {
348 delta = 0;
349 }
350 return delta;
351}
352
Alex Blighff83c662013-08-21 16:02:46 +0100353int64_t qemu_clock_deadline(QEMUClock *clock)
354{
355 return timerlist_deadline(clock->main_loop_timerlist);
356}
357
Alex Bligh02a03a92013-08-21 16:02:41 +0100358/*
359 * As above, but return -1 for no deadline, and do not cap to 2^32
360 * as we know the result is always positive.
361 */
362
Alex Blighff83c662013-08-21 16:02:46 +0100363int64_t timerlist_deadline_ns(QEMUTimerList *timer_list)
Alex Bligh02a03a92013-08-21 16:02:41 +0100364{
365 int64_t delta;
366
Alex Blighff83c662013-08-21 16:02:46 +0100367 if (!timer_list->clock->enabled || !timer_list->active_timers) {
Alex Bligh02a03a92013-08-21 16:02:41 +0100368 return -1;
369 }
370
Alex Blighff83c662013-08-21 16:02:46 +0100371 delta = timer_list->active_timers->expire_time -
372 qemu_get_clock_ns(timer_list->clock);
Alex Bligh02a03a92013-08-21 16:02:41 +0100373
374 if (delta <= 0) {
375 return 0;
376 }
377
378 return delta;
379}
380
Alex Blighff83c662013-08-21 16:02:46 +0100381int64_t qemu_clock_deadline_ns(QEMUClock *clock)
382{
383 return timerlist_deadline_ns(clock->main_loop_timerlist);
384}
385
386QEMUClock *timerlist_get_clock(QEMUTimerList *timer_list)
387{
388 return timer_list->clock;
389}
390
391QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClock *clock)
392{
393 return clock->main_loop_timerlist;
394}
395
Alex Blighd5541d82013-08-21 16:02:50 +0100396void timerlist_notify(QEMUTimerList *timer_list)
397{
398 if (timer_list->notify_cb) {
399 timer_list->notify_cb(timer_list->notify_opaque);
400 } else {
401 qemu_notify_event();
402 }
403}
404
Alex Bligh02a03a92013-08-21 16:02:41 +0100405/* Transition function to convert a nanosecond timeout to ms
406 * This is used where a system does not support ppoll
407 */
408int qemu_timeout_ns_to_ms(int64_t ns)
409{
410 int64_t ms;
411 if (ns < 0) {
412 return -1;
413 }
414
415 if (!ns) {
416 return 0;
417 }
418
419 /* Always round up, because it's better to wait too long than to wait too
420 * little and effectively busy-wait
421 */
422 ms = (ns + SCALE_MS - 1) / SCALE_MS;
423
424 /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
425 if (ms > (int64_t) INT32_MAX) {
426 ms = INT32_MAX;
427 }
428
429 return (int) ms;
430}
431
432
Alex Bligh4e0c6522013-08-21 16:02:43 +0100433/* qemu implementation of g_poll which uses a nanosecond timeout but is
434 * otherwise identical to g_poll
435 */
436int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout)
437{
438#ifdef CONFIG_PPOLL
439 if (timeout < 0) {
440 return ppoll((struct pollfd *)fds, nfds, NULL, NULL);
441 } else {
442 struct timespec ts;
443 ts.tv_sec = timeout / 1000000000LL;
444 ts.tv_nsec = timeout % 1000000000LL;
445 return ppoll((struct pollfd *)fds, nfds, &ts, NULL);
446 }
447#else
448 return g_poll(fds, nfds, qemu_timeout_ns_to_ms(timeout));
449#endif
450}
451
452
Alex Blighff83c662013-08-21 16:02:46 +0100453void timer_init(QEMUTimer *ts,
454 QEMUTimerList *timer_list, int scale,
455 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100456{
Alex Blighff83c662013-08-21 16:02:46 +0100457 ts->timer_list = timer_list;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100458 ts->cb = cb;
459 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100460 ts->scale = scale;
Alex Blighff83c662013-08-21 16:02:46 +0100461}
462
463QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
464 QEMUTimerCB *cb, void *opaque)
465{
466 return timer_new_tl(clock->main_loop_timerlist,
467 scale, cb, opaque);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100468}
469
470void qemu_free_timer(QEMUTimer *ts)
471{
Anthony Liguori7267c092011-08-20 22:09:37 -0500472 g_free(ts);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100473}
474
475/* stop a timer, but do not dealloc it */
476void qemu_del_timer(QEMUTimer *ts)
477{
478 QEMUTimer **pt, *t;
479
480 /* NOTE: this code must be signal safe because
Alex Blighe93379b2013-08-21 16:02:39 +0100481 timer_expired() can be called from a signal. */
Alex Blighff83c662013-08-21 16:02:46 +0100482 pt = &ts->timer_list->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100483 for(;;) {
484 t = *pt;
485 if (!t)
486 break;
487 if (t == ts) {
488 *pt = t->next;
489 break;
490 }
491 pt = &t->next;
492 }
493}
494
495/* modify the current timer so that it will be fired when current_time
496 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200497void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100498{
499 QEMUTimer **pt, *t;
500
501 qemu_del_timer(ts);
502
503 /* add the timer in the sorted list */
504 /* NOTE: this code must be signal safe because
Alex Blighe93379b2013-08-21 16:02:39 +0100505 timer_expired() can be called from a signal. */
Alex Blighff83c662013-08-21 16:02:46 +0100506 pt = &ts->timer_list->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100507 for(;;) {
508 t = *pt;
Alex Blighe93379b2013-08-21 16:02:39 +0100509 if (!timer_expired_ns(t, expire_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100510 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100511 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100512 pt = &t->next;
513 }
514 ts->expire_time = expire_time;
515 ts->next = *pt;
516 *pt = ts;
517
518 /* Rearm if necessary */
Alex Blighff83c662013-08-21 16:02:46 +0100519 if (pt == &ts->timer_list->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100520 if (!alarm_timer->pending) {
521 qemu_rearm_alarm_timer(alarm_timer);
522 }
523 /* Interrupt execution to force deadline recalculation. */
Alex Blighff83c662013-08-21 16:02:46 +0100524 qemu_clock_warp(ts->timer_list->clock);
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200525 if (use_icount) {
Alex Blighd5541d82013-08-21 16:02:50 +0100526 timerlist_notify(ts->timer_list);
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200527 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100528 }
529}
530
Paolo Bonzini4a998742011-03-11 16:33:58 +0100531void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
532{
533 qemu_mod_timer_ns(ts, expire_time * ts->scale);
534}
535
Alex Blighe93379b2013-08-21 16:02:39 +0100536bool timer_pending(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100537{
538 QEMUTimer *t;
Alex Blighff83c662013-08-21 16:02:46 +0100539 for (t = ts->timer_list->active_timers; t != NULL; t = t->next) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200540 if (t == ts) {
541 return true;
542 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100543 }
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200544 return false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100545}
546
Alex Blighe93379b2013-08-21 16:02:39 +0100547bool timer_expired(QEMUTimer *timer_head, int64_t current_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100548{
Alex Blighe93379b2013-08-21 16:02:39 +0100549 return timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100550}
551
Alex Blighff83c662013-08-21 16:02:46 +0100552bool timerlist_run_timers(QEMUTimerList *timer_list)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100553{
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200554 QEMUTimer *ts;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100555 int64_t current_time;
Alex Blighf9a976b2013-08-21 16:02:45 +0100556 bool progress = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100557
Alex Blighff83c662013-08-21 16:02:46 +0100558 if (!timer_list->clock->enabled) {
Alex Blighf9a976b2013-08-21 16:02:45 +0100559 return progress;
Alex Blighff83c662013-08-21 16:02:46 +0100560 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100561
Alex Blighff83c662013-08-21 16:02:46 +0100562 current_time = qemu_get_clock_ns(timer_list->clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100563 for(;;) {
Alex Blighff83c662013-08-21 16:02:46 +0100564 ts = timer_list->active_timers;
Alex Blighe93379b2013-08-21 16:02:39 +0100565 if (!timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100566 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100567 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100568 /* remove timer from the list before calling the callback */
Alex Blighff83c662013-08-21 16:02:46 +0100569 timer_list->active_timers = ts->next;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100570 ts->next = NULL;
571
572 /* run the callback (the timer list can be modified) */
573 ts->cb(ts->opaque);
Alex Blighf9a976b2013-08-21 16:02:45 +0100574 progress = true;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100575 }
Alex Blighf9a976b2013-08-21 16:02:45 +0100576 return progress;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100577}
578
Alex Blighff83c662013-08-21 16:02:46 +0100579bool qemu_run_timers(QEMUClock *clock)
580{
581 return timerlist_run_timers(clock->main_loop_timerlist);
582}
583
Alex Blighd5541d82013-08-21 16:02:50 +0100584void timerlistgroup_init(QEMUTimerListGroup *tlg,
585 QEMUTimerListNotifyCB *cb, void *opaque)
Alex Bligh754d6a52013-08-21 16:02:48 +0100586{
587 QEMUClockType type;
588 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
Alex Blighd5541d82013-08-21 16:02:50 +0100589 tlg->tl[type] = timerlist_new(type, cb, opaque);
Alex Bligh754d6a52013-08-21 16:02:48 +0100590 }
591}
592
593void timerlistgroup_deinit(QEMUTimerListGroup *tlg)
594{
595 QEMUClockType type;
596 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
597 timerlist_free(tlg->tl[type]);
598 }
599}
600
601bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg)
602{
603 QEMUClockType type;
604 bool progress = false;
605 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
606 progress |= timerlist_run_timers(tlg->tl[type]);
607 }
608 return progress;
609}
610
611int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg)
612{
613 int64_t deadline = -1;
614 QEMUClockType type;
615 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
616 if (qemu_clock_use_for_deadline(tlg->tl[type]->clock)) {
617 deadline = qemu_soonest_timeout(deadline,
618 timerlist_deadline_ns(
619 tlg->tl[type]));
620 }
621 }
622 return deadline;
623}
624
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100625int64_t qemu_get_clock_ns(QEMUClock *clock)
626{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200627 int64_t now, last;
628
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100629 switch(clock->type) {
630 case QEMU_CLOCK_REALTIME:
631 return get_clock();
632 default:
633 case QEMU_CLOCK_VIRTUAL:
634 if (use_icount) {
635 return cpu_get_icount();
636 } else {
637 return cpu_get_clock();
638 }
639 case QEMU_CLOCK_HOST:
Jan Kiszka691a0c92011-06-20 14:06:27 +0200640 now = get_clock_realtime();
641 last = clock->last;
642 clock->last = now;
643 if (now < last) {
644 notifier_list_notify(&clock->reset_notifiers, &now);
645 }
646 return now;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100647 }
648}
649
Jan Kiszka691a0c92011-06-20 14:06:27 +0200650void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
651{
652 notifier_list_add(&clock->reset_notifiers, notifier);
653}
654
655void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
656{
Paolo Bonzini31552522012-01-13 17:34:01 +0100657 notifier_remove(notifier);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200658}
659
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100660void init_clocks(void)
661{
Alex Blighff83c662013-08-21 16:02:46 +0100662 QEMUClockType type;
663 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
664 if (!qemu_clocks[type]) {
665 qemu_clocks[type] = qemu_clock_new(type);
Alex Bligh754d6a52013-08-21 16:02:48 +0100666 main_loop_tlg.tl[type] = qemu_clocks[type]->main_loop_timerlist;
Alex Blighff83c662013-08-21 16:02:46 +0100667 }
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100668 }
Alex Blighff83c662013-08-21 16:02:46 +0100669
Alex Blighcd758dd2013-08-21 16:02:44 +0100670#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
671 prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0);
672#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100673}
674
Alex Blighe93379b2013-08-21 16:02:39 +0100675uint64_t timer_expire_time_ns(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100676{
Alex Blighe93379b2013-08-21 16:02:39 +0100677 return timer_pending(ts) ? ts->expire_time : -1;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100678}
679
Alex Blighf9a976b2013-08-21 16:02:45 +0100680bool qemu_run_all_timers(void)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100681{
Alex Blighf9a976b2013-08-21 16:02:45 +0100682 bool progress = false;
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200683 alarm_timer->pending = false;
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100684
Peter Portante158fd3c2012-04-05 11:00:45 -0400685 /* vm time timers */
Alex Blighff83c662013-08-21 16:02:46 +0100686 QEMUClockType type;
687 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
688 progress |= qemu_run_timers(qemu_clock_ptr(type));
689 }
Peter Portante158fd3c2012-04-05 11:00:45 -0400690
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100691 /* rearm timer, if not periodic */
692 if (alarm_timer->expired) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200693 alarm_timer->expired = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100694 qemu_rearm_alarm_timer(alarm_timer);
695 }
Alex Blighf9a976b2013-08-21 16:02:45 +0100696
697 return progress;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100698}
699
700#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100701static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100702#else
703static void host_alarm_handler(int host_signum)
704#endif
705{
706 struct qemu_alarm_timer *t = alarm_timer;
707 if (!t)
708 return;
709
Stefan Weil82051992012-04-20 11:27:24 +0200710 t->expired = true;
711 t->pending = true;
712 qemu_notify_event();
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100713}
714
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100715#if defined(__linux__)
716
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100717#include "qemu/compatfd.h"
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200718
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100719static int dynticks_start_timer(struct qemu_alarm_timer *t)
720{
721 struct sigevent ev;
722 timer_t host_timer;
723 struct sigaction act;
724
725 sigfillset(&act.sa_mask);
726 act.sa_flags = 0;
727 act.sa_handler = host_alarm_handler;
728
729 sigaction(SIGALRM, &act, NULL);
730
731 /*
732 * Initialize ev struct to 0 to avoid valgrind complaining
733 * about uninitialized data in timer_create call
734 */
735 memset(&ev, 0, sizeof(ev));
736 ev.sigev_value.sival_int = 0;
737 ev.sigev_notify = SIGEV_SIGNAL;
Richard Henderson1e9737d2012-10-23 07:33:00 +1000738#ifdef CONFIG_SIGEV_THREAD_ID
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200739 if (qemu_signalfd_available()) {
740 ev.sigev_notify = SIGEV_THREAD_ID;
741 ev._sigev_un._tid = qemu_get_thread_id();
742 }
Richard Henderson1e9737d2012-10-23 07:33:00 +1000743#endif /* CONFIG_SIGEV_THREAD_ID */
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100744 ev.sigev_signo = SIGALRM;
745
746 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
747 perror("timer_create");
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100748 return -1;
749 }
750
Stefan Weilcd0544e2011-04-10 20:15:09 +0200751 t->timer = host_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100752
753 return 0;
754}
755
756static void dynticks_stop_timer(struct qemu_alarm_timer *t)
757{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200758 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100759
760 timer_delete(host_timer);
761}
762
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100763static void dynticks_rearm_timer(struct qemu_alarm_timer *t,
764 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100765{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200766 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100767 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100768 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100769
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100770 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
771 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100772
773 /* check whether a timer is already running */
774 if (timer_gettime(host_timer, &timeout)) {
775 perror("gettime");
776 fprintf(stderr, "Internal timer error: aborting\n");
777 exit(1);
778 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100779 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
780 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100781 return;
782
783 timeout.it_interval.tv_sec = 0;
784 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100785 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
786 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100787 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
788 perror("settime");
789 fprintf(stderr, "Internal timer error: aborting\n");
790 exit(1);
791 }
792}
793
794#endif /* defined(__linux__) */
795
Stefan Weilf26e5a52011-02-04 22:01:32 +0100796#if !defined(_WIN32)
797
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100798static int unix_start_timer(struct qemu_alarm_timer *t)
799{
800 struct sigaction act;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100801
802 /* timer signal */
803 sigfillset(&act.sa_mask);
804 act.sa_flags = 0;
805 act.sa_handler = host_alarm_handler;
806
807 sigaction(SIGALRM, &act, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200808 return 0;
809}
810
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100811static void unix_rearm_timer(struct qemu_alarm_timer *t,
812 int64_t nearest_delta_ns)
Paolo Bonzini84682832011-06-09 13:10:25 +0200813{
814 struct itimerval itv;
Paolo Bonzini84682832011-06-09 13:10:25 +0200815 int err;
816
Paolo Bonzini84682832011-06-09 13:10:25 +0200817 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
818 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100819
820 itv.it_interval.tv_sec = 0;
Paolo Bonzini84682832011-06-09 13:10:25 +0200821 itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */
822 itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
823 itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100824 err = setitimer(ITIMER_REAL, &itv, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200825 if (err) {
826 perror("setitimer");
827 fprintf(stderr, "Internal timer error: aborting\n");
828 exit(1);
829 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100830}
831
832static void unix_stop_timer(struct qemu_alarm_timer *t)
833{
834 struct itimerval itv;
835
836 memset(&itv, 0, sizeof(itv));
837 setitimer(ITIMER_REAL, &itv, NULL);
838}
839
840#endif /* !defined(_WIN32) */
841
842
843#ifdef _WIN32
844
Stefan Weil2f9cba02011-04-05 18:34:21 +0200845static MMRESULT mm_timer;
Stefan Weil40f08e82012-04-27 05:34:40 +0000846static TIMECAPS mm_tc;
Stefan Weil2f9cba02011-04-05 18:34:21 +0200847
848static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
849 DWORD_PTR dwUser, DWORD_PTR dw1,
850 DWORD_PTR dw2)
851{
852 struct qemu_alarm_timer *t = alarm_timer;
853 if (!t) {
854 return;
855 }
Stefan Weil82051992012-04-20 11:27:24 +0200856 t->expired = true;
857 t->pending = true;
858 qemu_notify_event();
Stefan Weil2f9cba02011-04-05 18:34:21 +0200859}
860
861static int mm_start_timer(struct qemu_alarm_timer *t)
862{
Stefan Weil40f08e82012-04-27 05:34:40 +0000863 timeGetDevCaps(&mm_tc, sizeof(mm_tc));
Stefan Weil2f9cba02011-04-05 18:34:21 +0200864 return 0;
865}
866
867static void mm_stop_timer(struct qemu_alarm_timer *t)
868{
Paolo Bonzini0727b862013-02-20 14:43:31 +0100869 if (mm_timer) {
870 timeKillEvent(mm_timer);
871 }
Stefan Weil2f9cba02011-04-05 18:34:21 +0200872}
873
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100874static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta)
Stefan Weil2f9cba02011-04-05 18:34:21 +0200875{
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100876 int64_t nearest_delta_ms = delta / 1000000;
Stefan Weil40f08e82012-04-27 05:34:40 +0000877 if (nearest_delta_ms < mm_tc.wPeriodMin) {
878 nearest_delta_ms = mm_tc.wPeriodMin;
879 } else if (nearest_delta_ms > mm_tc.wPeriodMax) {
880 nearest_delta_ms = mm_tc.wPeriodMax;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100881 }
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100882
Paolo Bonzini0727b862013-02-20 14:43:31 +0100883 if (mm_timer) {
884 timeKillEvent(mm_timer);
885 }
Stefan Weil40f08e82012-04-27 05:34:40 +0000886 mm_timer = timeSetEvent((UINT)nearest_delta_ms,
887 mm_tc.wPeriodMin,
Stefan Weil2f9cba02011-04-05 18:34:21 +0200888 mm_alarm_handler,
889 (DWORD_PTR)t,
890 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
891
892 if (!mm_timer) {
Stefan Weil52ef6512012-05-08 19:14:43 +0200893 fprintf(stderr, "Failed to re-arm win32 alarm timer\n");
Stefan Weil40f08e82012-04-27 05:34:40 +0000894 timeEndPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200895 exit(1);
896 }
897}
898
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100899static int win32_start_timer(struct qemu_alarm_timer *t)
900{
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100901 HANDLE hTimer;
902 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100903
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100904 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
905 is zero) that has already expired, the timer is not updated. Since
906 creating a new timer is relatively expensive, set a bogus one-hour
907 interval in the dynticks case. */
908 success = CreateTimerQueueTimer(&hTimer,
909 NULL,
910 host_alarm_handler,
911 t,
912 1,
Stefan Weil82051992012-04-20 11:27:24 +0200913 3600000,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100914 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100915
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100916 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100917 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
918 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100919 return -1;
920 }
921
Stefan Weilcd0544e2011-04-10 20:15:09 +0200922 t->timer = hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100923 return 0;
924}
925
926static void win32_stop_timer(struct qemu_alarm_timer *t)
927{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200928 HANDLE hTimer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100929
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100930 if (hTimer) {
931 DeleteTimerQueueTimer(NULL, hTimer, NULL);
932 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100933}
934
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100935static void win32_rearm_timer(struct qemu_alarm_timer *t,
936 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100937{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200938 HANDLE hTimer = t->timer;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100939 int64_t nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100940 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100941
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100942 nearest_delta_ms = nearest_delta_ns / 1000000;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +0100943 if (nearest_delta_ms < 1) {
944 nearest_delta_ms = 1;
945 }
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100946 /* ULONG_MAX can be 32 bit */
947 if (nearest_delta_ms > ULONG_MAX) {
948 nearest_delta_ms = ULONG_MAX;
949 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100950 success = ChangeTimerQueueTimer(NULL,
951 hTimer,
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100952 (unsigned long) nearest_delta_ms,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100953 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100954
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100955 if (!success) {
956 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100957 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100958 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100959 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100960
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100961}
962
963#endif /* _WIN32 */
964
Paolo Bonzini4260a732011-09-19 10:18:51 +0200965static void quit_timers(void)
966{
967 struct qemu_alarm_timer *t = alarm_timer;
968 alarm_timer = NULL;
969 t->stop(t);
970}
971
Stefan Weil253ecf82012-11-04 21:42:08 +0100972#ifdef CONFIG_POSIX
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100973static void reinit_timers(void)
974{
975 struct qemu_alarm_timer *t = alarm_timer;
976 t->stop(t);
977 if (t->start(t)) {
978 fprintf(stderr, "Internal timer error: aborting\n");
979 exit(1);
980 }
981 qemu_rearm_alarm_timer(t);
982}
Stefan Weil253ecf82012-11-04 21:42:08 +0100983#endif /* CONFIG_POSIX */
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100984
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100985int init_timer_alarm(void)
986{
987 struct qemu_alarm_timer *t = NULL;
988 int i, err = -1;
989
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100990 if (alarm_timer) {
991 return 0;
992 }
993
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100994 for (i = 0; alarm_timers[i].name; i++) {
995 t = &alarm_timers[i];
996
997 err = t->start(t);
998 if (!err)
999 break;
1000 }
1001
1002 if (err) {
1003 err = -ENOENT;
1004 goto fail;
1005 }
1006
Paolo Bonzini4260a732011-09-19 10:18:51 +02001007 atexit(quit_timers);
Paolo Bonzinic8122c32012-11-02 15:43:22 +01001008#ifdef CONFIG_POSIX
1009 pthread_atfork(NULL, NULL, reinit_timers);
1010#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001011 alarm_timer = t;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001012 return 0;
1013
1014fail:
1015 return err;
1016}
1017