blob: b56bfde7065647153b74b4cb970ca9e91ba8f547 [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
Alex Blighb1bbfe72013-08-21 16:02:55 +0100307void qemu_clock_notify(QEMUClock *clock)
308{
309 QEMUTimerList *timer_list;
310 QLIST_FOREACH(timer_list, &clock->timerlists, list) {
311 timerlist_notify(timer_list);
312 }
313}
314
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200315void qemu_clock_enable(QEMUClock *clock, bool enabled)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100316{
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200317 bool old = clock->enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100318 clock->enabled = enabled;
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200319 if (enabled && !old) {
Alex Blighb1bbfe72013-08-21 16:02:55 +0100320 qemu_clock_notify(clock);
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200321 qemu_rearm_alarm_timer(alarm_timer);
322 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100323}
324
Alex Blighff83c662013-08-21 16:02:46 +0100325bool timerlist_has_timers(QEMUTimerList *timer_list)
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200326{
Alex Blighff83c662013-08-21 16:02:46 +0100327 return !!timer_list->active_timers;
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200328}
329
Alex Blighff83c662013-08-21 16:02:46 +0100330bool qemu_clock_has_timers(QEMUClock *clock)
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200331{
Alex Blighff83c662013-08-21 16:02:46 +0100332 return timerlist_has_timers(clock->main_loop_timerlist);
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200333}
334
Alex Blighff83c662013-08-21 16:02:46 +0100335bool timerlist_expired(QEMUTimerList *timer_list)
336{
337 return (timer_list->active_timers &&
338 timer_list->active_timers->expire_time <
339 qemu_get_clock_ns(timer_list->clock));
340}
341
342bool qemu_clock_expired(QEMUClock *clock)
343{
344 return timerlist_expired(clock->main_loop_timerlist);
345}
346
347int64_t timerlist_deadline(QEMUTimerList *timer_list)
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200348{
349 /* To avoid problems with overflow limit this to 2^32. */
350 int64_t delta = INT32_MAX;
351
Alex Blighff83c662013-08-21 16:02:46 +0100352 if (timer_list->clock->enabled && timer_list->active_timers) {
353 delta = timer_list->active_timers->expire_time -
354 qemu_get_clock_ns(timer_list->clock);
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200355 }
356 if (delta < 0) {
357 delta = 0;
358 }
359 return delta;
360}
361
Alex Blighff83c662013-08-21 16:02:46 +0100362int64_t qemu_clock_deadline(QEMUClock *clock)
363{
364 return timerlist_deadline(clock->main_loop_timerlist);
365}
366
Alex Bligh02a03a92013-08-21 16:02:41 +0100367/*
368 * As above, but return -1 for no deadline, and do not cap to 2^32
369 * as we know the result is always positive.
370 */
371
Alex Blighff83c662013-08-21 16:02:46 +0100372int64_t timerlist_deadline_ns(QEMUTimerList *timer_list)
Alex Bligh02a03a92013-08-21 16:02:41 +0100373{
374 int64_t delta;
375
Alex Blighff83c662013-08-21 16:02:46 +0100376 if (!timer_list->clock->enabled || !timer_list->active_timers) {
Alex Bligh02a03a92013-08-21 16:02:41 +0100377 return -1;
378 }
379
Alex Blighff83c662013-08-21 16:02:46 +0100380 delta = timer_list->active_timers->expire_time -
381 qemu_get_clock_ns(timer_list->clock);
Alex Bligh02a03a92013-08-21 16:02:41 +0100382
383 if (delta <= 0) {
384 return 0;
385 }
386
387 return delta;
388}
389
Alex Blighff83c662013-08-21 16:02:46 +0100390int64_t qemu_clock_deadline_ns(QEMUClock *clock)
391{
392 return timerlist_deadline_ns(clock->main_loop_timerlist);
393}
394
Alex Blighac70aaf2013-08-21 16:02:57 +0100395/* Calculate the soonest deadline across all timerlists attached
396 * to the clock. This is used for the icount timeout so we
397 * ignore whether or not the clock should be used in deadline
398 * calculations.
399 */
400int64_t qemu_clock_deadline_ns_all(QEMUClock *clock)
401{
402 int64_t deadline = -1;
403 QEMUTimerList *timer_list;
404 QLIST_FOREACH(timer_list, &clock->timerlists, list) {
405 deadline = qemu_soonest_timeout(deadline,
406 timerlist_deadline_ns(timer_list));
407 }
408 return deadline;
409}
410
Alex Blighff83c662013-08-21 16:02:46 +0100411QEMUClock *timerlist_get_clock(QEMUTimerList *timer_list)
412{
413 return timer_list->clock;
414}
415
416QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClock *clock)
417{
418 return clock->main_loop_timerlist;
419}
420
Alex Blighd5541d82013-08-21 16:02:50 +0100421void timerlist_notify(QEMUTimerList *timer_list)
422{
423 if (timer_list->notify_cb) {
424 timer_list->notify_cb(timer_list->notify_opaque);
425 } else {
426 qemu_notify_event();
427 }
428}
429
Alex Bligh02a03a92013-08-21 16:02:41 +0100430/* Transition function to convert a nanosecond timeout to ms
431 * This is used where a system does not support ppoll
432 */
433int qemu_timeout_ns_to_ms(int64_t ns)
434{
435 int64_t ms;
436 if (ns < 0) {
437 return -1;
438 }
439
440 if (!ns) {
441 return 0;
442 }
443
444 /* Always round up, because it's better to wait too long than to wait too
445 * little and effectively busy-wait
446 */
447 ms = (ns + SCALE_MS - 1) / SCALE_MS;
448
449 /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
450 if (ms > (int64_t) INT32_MAX) {
451 ms = INT32_MAX;
452 }
453
454 return (int) ms;
455}
456
457
Alex Bligh4e0c6522013-08-21 16:02:43 +0100458/* qemu implementation of g_poll which uses a nanosecond timeout but is
459 * otherwise identical to g_poll
460 */
461int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout)
462{
463#ifdef CONFIG_PPOLL
464 if (timeout < 0) {
465 return ppoll((struct pollfd *)fds, nfds, NULL, NULL);
466 } else {
467 struct timespec ts;
468 ts.tv_sec = timeout / 1000000000LL;
469 ts.tv_nsec = timeout % 1000000000LL;
470 return ppoll((struct pollfd *)fds, nfds, &ts, NULL);
471 }
472#else
473 return g_poll(fds, nfds, qemu_timeout_ns_to_ms(timeout));
474#endif
475}
476
477
Alex Blighff83c662013-08-21 16:02:46 +0100478void timer_init(QEMUTimer *ts,
479 QEMUTimerList *timer_list, int scale,
480 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100481{
Alex Blighff83c662013-08-21 16:02:46 +0100482 ts->timer_list = timer_list;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100483 ts->cb = cb;
484 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100485 ts->scale = scale;
Alex Blighff83c662013-08-21 16:02:46 +0100486}
487
488QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
489 QEMUTimerCB *cb, void *opaque)
490{
491 return timer_new_tl(clock->main_loop_timerlist,
492 scale, cb, opaque);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100493}
494
495void qemu_free_timer(QEMUTimer *ts)
496{
Anthony Liguori7267c092011-08-20 22:09:37 -0500497 g_free(ts);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100498}
499
500/* stop a timer, but do not dealloc it */
501void qemu_del_timer(QEMUTimer *ts)
502{
503 QEMUTimer **pt, *t;
504
505 /* NOTE: this code must be signal safe because
Alex Blighe93379b2013-08-21 16:02:39 +0100506 timer_expired() can be called from a signal. */
Alex Blighff83c662013-08-21 16:02:46 +0100507 pt = &ts->timer_list->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100508 for(;;) {
509 t = *pt;
510 if (!t)
511 break;
512 if (t == ts) {
513 *pt = t->next;
514 break;
515 }
516 pt = &t->next;
517 }
518}
519
520/* modify the current timer so that it will be fired when current_time
521 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200522void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100523{
524 QEMUTimer **pt, *t;
525
526 qemu_del_timer(ts);
527
528 /* add the timer in the sorted list */
529 /* NOTE: this code must be signal safe because
Alex Blighe93379b2013-08-21 16:02:39 +0100530 timer_expired() can be called from a signal. */
Alex Blighff83c662013-08-21 16:02:46 +0100531 pt = &ts->timer_list->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100532 for(;;) {
533 t = *pt;
Alex Blighe93379b2013-08-21 16:02:39 +0100534 if (!timer_expired_ns(t, expire_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100535 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100536 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100537 pt = &t->next;
538 }
539 ts->expire_time = expire_time;
540 ts->next = *pt;
541 *pt = ts;
542
543 /* Rearm if necessary */
Alex Blighff83c662013-08-21 16:02:46 +0100544 if (pt == &ts->timer_list->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100545 if (!alarm_timer->pending) {
546 qemu_rearm_alarm_timer(alarm_timer);
547 }
548 /* Interrupt execution to force deadline recalculation. */
Alex Blighff83c662013-08-21 16:02:46 +0100549 qemu_clock_warp(ts->timer_list->clock);
Alex Blighb1bbfe72013-08-21 16:02:55 +0100550 timerlist_notify(ts->timer_list);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100551 }
552}
553
Paolo Bonzini4a998742011-03-11 16:33:58 +0100554void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
555{
556 qemu_mod_timer_ns(ts, expire_time * ts->scale);
557}
558
Alex Blighe93379b2013-08-21 16:02:39 +0100559bool timer_pending(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100560{
561 QEMUTimer *t;
Alex Blighff83c662013-08-21 16:02:46 +0100562 for (t = ts->timer_list->active_timers; t != NULL; t = t->next) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200563 if (t == ts) {
564 return true;
565 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100566 }
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200567 return false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100568}
569
Alex Blighe93379b2013-08-21 16:02:39 +0100570bool timer_expired(QEMUTimer *timer_head, int64_t current_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100571{
Alex Blighe93379b2013-08-21 16:02:39 +0100572 return timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100573}
574
Alex Blighff83c662013-08-21 16:02:46 +0100575bool timerlist_run_timers(QEMUTimerList *timer_list)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100576{
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200577 QEMUTimer *ts;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100578 int64_t current_time;
Alex Blighf9a976b2013-08-21 16:02:45 +0100579 bool progress = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100580
Alex Blighff83c662013-08-21 16:02:46 +0100581 if (!timer_list->clock->enabled) {
Alex Blighf9a976b2013-08-21 16:02:45 +0100582 return progress;
Alex Blighff83c662013-08-21 16:02:46 +0100583 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100584
Alex Blighff83c662013-08-21 16:02:46 +0100585 current_time = qemu_get_clock_ns(timer_list->clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100586 for(;;) {
Alex Blighff83c662013-08-21 16:02:46 +0100587 ts = timer_list->active_timers;
Alex Blighe93379b2013-08-21 16:02:39 +0100588 if (!timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100589 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100590 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100591 /* remove timer from the list before calling the callback */
Alex Blighff83c662013-08-21 16:02:46 +0100592 timer_list->active_timers = ts->next;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100593 ts->next = NULL;
594
595 /* run the callback (the timer list can be modified) */
596 ts->cb(ts->opaque);
Alex Blighf9a976b2013-08-21 16:02:45 +0100597 progress = true;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100598 }
Alex Blighf9a976b2013-08-21 16:02:45 +0100599 return progress;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100600}
601
Alex Blighff83c662013-08-21 16:02:46 +0100602bool qemu_run_timers(QEMUClock *clock)
603{
604 return timerlist_run_timers(clock->main_loop_timerlist);
605}
606
Alex Blighd5541d82013-08-21 16:02:50 +0100607void timerlistgroup_init(QEMUTimerListGroup *tlg,
608 QEMUTimerListNotifyCB *cb, void *opaque)
Alex Bligh754d6a52013-08-21 16:02:48 +0100609{
610 QEMUClockType type;
611 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
Alex Blighd5541d82013-08-21 16:02:50 +0100612 tlg->tl[type] = timerlist_new(type, cb, opaque);
Alex Bligh754d6a52013-08-21 16:02:48 +0100613 }
614}
615
616void timerlistgroup_deinit(QEMUTimerListGroup *tlg)
617{
618 QEMUClockType type;
619 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
620 timerlist_free(tlg->tl[type]);
621 }
622}
623
624bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg)
625{
626 QEMUClockType type;
627 bool progress = false;
628 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
629 progress |= timerlist_run_timers(tlg->tl[type]);
630 }
631 return progress;
632}
633
634int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg)
635{
636 int64_t deadline = -1;
637 QEMUClockType type;
638 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
639 if (qemu_clock_use_for_deadline(tlg->tl[type]->clock)) {
640 deadline = qemu_soonest_timeout(deadline,
641 timerlist_deadline_ns(
642 tlg->tl[type]));
643 }
644 }
645 return deadline;
646}
647
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100648int64_t qemu_get_clock_ns(QEMUClock *clock)
649{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200650 int64_t now, last;
651
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100652 switch(clock->type) {
653 case QEMU_CLOCK_REALTIME:
654 return get_clock();
655 default:
656 case QEMU_CLOCK_VIRTUAL:
657 if (use_icount) {
658 return cpu_get_icount();
659 } else {
660 return cpu_get_clock();
661 }
662 case QEMU_CLOCK_HOST:
Jan Kiszka691a0c92011-06-20 14:06:27 +0200663 now = get_clock_realtime();
664 last = clock->last;
665 clock->last = now;
666 if (now < last) {
667 notifier_list_notify(&clock->reset_notifiers, &now);
668 }
669 return now;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100670 }
671}
672
Jan Kiszka691a0c92011-06-20 14:06:27 +0200673void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
674{
675 notifier_list_add(&clock->reset_notifiers, notifier);
676}
677
678void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
679{
Paolo Bonzini31552522012-01-13 17:34:01 +0100680 notifier_remove(notifier);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200681}
682
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100683void init_clocks(void)
684{
Alex Blighff83c662013-08-21 16:02:46 +0100685 QEMUClockType type;
686 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
687 if (!qemu_clocks[type]) {
688 qemu_clocks[type] = qemu_clock_new(type);
Alex Bligh754d6a52013-08-21 16:02:48 +0100689 main_loop_tlg.tl[type] = qemu_clocks[type]->main_loop_timerlist;
Alex Blighff83c662013-08-21 16:02:46 +0100690 }
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100691 }
Alex Blighff83c662013-08-21 16:02:46 +0100692
Alex Blighcd758dd2013-08-21 16:02:44 +0100693#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
694 prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0);
695#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100696}
697
Alex Blighe93379b2013-08-21 16:02:39 +0100698uint64_t timer_expire_time_ns(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100699{
Alex Blighe93379b2013-08-21 16:02:39 +0100700 return timer_pending(ts) ? ts->expire_time : -1;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100701}
702
Alex Blighf9a976b2013-08-21 16:02:45 +0100703bool qemu_run_all_timers(void)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100704{
Alex Blighf9a976b2013-08-21 16:02:45 +0100705 bool progress = false;
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200706 alarm_timer->pending = false;
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100707
Peter Portante158fd3c2012-04-05 11:00:45 -0400708 /* vm time timers */
Alex Blighff83c662013-08-21 16:02:46 +0100709 QEMUClockType type;
710 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
711 progress |= qemu_run_timers(qemu_clock_ptr(type));
712 }
Peter Portante158fd3c2012-04-05 11:00:45 -0400713
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100714 /* rearm timer, if not periodic */
715 if (alarm_timer->expired) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200716 alarm_timer->expired = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100717 qemu_rearm_alarm_timer(alarm_timer);
718 }
Alex Blighf9a976b2013-08-21 16:02:45 +0100719
720 return progress;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100721}
722
723#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100724static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100725#else
726static void host_alarm_handler(int host_signum)
727#endif
728{
729 struct qemu_alarm_timer *t = alarm_timer;
730 if (!t)
731 return;
732
Stefan Weil82051992012-04-20 11:27:24 +0200733 t->expired = true;
734 t->pending = true;
735 qemu_notify_event();
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100736}
737
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100738#if defined(__linux__)
739
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100740#include "qemu/compatfd.h"
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200741
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100742static int dynticks_start_timer(struct qemu_alarm_timer *t)
743{
744 struct sigevent ev;
745 timer_t host_timer;
746 struct sigaction act;
747
748 sigfillset(&act.sa_mask);
749 act.sa_flags = 0;
750 act.sa_handler = host_alarm_handler;
751
752 sigaction(SIGALRM, &act, NULL);
753
754 /*
755 * Initialize ev struct to 0 to avoid valgrind complaining
756 * about uninitialized data in timer_create call
757 */
758 memset(&ev, 0, sizeof(ev));
759 ev.sigev_value.sival_int = 0;
760 ev.sigev_notify = SIGEV_SIGNAL;
Richard Henderson1e9737d2012-10-23 07:33:00 +1000761#ifdef CONFIG_SIGEV_THREAD_ID
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200762 if (qemu_signalfd_available()) {
763 ev.sigev_notify = SIGEV_THREAD_ID;
764 ev._sigev_un._tid = qemu_get_thread_id();
765 }
Richard Henderson1e9737d2012-10-23 07:33:00 +1000766#endif /* CONFIG_SIGEV_THREAD_ID */
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100767 ev.sigev_signo = SIGALRM;
768
769 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
770 perror("timer_create");
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100771 return -1;
772 }
773
Stefan Weilcd0544e2011-04-10 20:15:09 +0200774 t->timer = host_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100775
776 return 0;
777}
778
779static void dynticks_stop_timer(struct qemu_alarm_timer *t)
780{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200781 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100782
783 timer_delete(host_timer);
784}
785
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100786static void dynticks_rearm_timer(struct qemu_alarm_timer *t,
787 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100788{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200789 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100790 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100791 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100792
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100793 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
794 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100795
796 /* check whether a timer is already running */
797 if (timer_gettime(host_timer, &timeout)) {
798 perror("gettime");
799 fprintf(stderr, "Internal timer error: aborting\n");
800 exit(1);
801 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100802 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
803 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100804 return;
805
806 timeout.it_interval.tv_sec = 0;
807 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100808 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
809 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100810 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
811 perror("settime");
812 fprintf(stderr, "Internal timer error: aborting\n");
813 exit(1);
814 }
815}
816
817#endif /* defined(__linux__) */
818
Stefan Weilf26e5a52011-02-04 22:01:32 +0100819#if !defined(_WIN32)
820
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100821static int unix_start_timer(struct qemu_alarm_timer *t)
822{
823 struct sigaction act;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100824
825 /* timer signal */
826 sigfillset(&act.sa_mask);
827 act.sa_flags = 0;
828 act.sa_handler = host_alarm_handler;
829
830 sigaction(SIGALRM, &act, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200831 return 0;
832}
833
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100834static void unix_rearm_timer(struct qemu_alarm_timer *t,
835 int64_t nearest_delta_ns)
Paolo Bonzini84682832011-06-09 13:10:25 +0200836{
837 struct itimerval itv;
Paolo Bonzini84682832011-06-09 13:10:25 +0200838 int err;
839
Paolo Bonzini84682832011-06-09 13:10:25 +0200840 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
841 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100842
843 itv.it_interval.tv_sec = 0;
Paolo Bonzini84682832011-06-09 13:10:25 +0200844 itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */
845 itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
846 itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100847 err = setitimer(ITIMER_REAL, &itv, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200848 if (err) {
849 perror("setitimer");
850 fprintf(stderr, "Internal timer error: aborting\n");
851 exit(1);
852 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100853}
854
855static void unix_stop_timer(struct qemu_alarm_timer *t)
856{
857 struct itimerval itv;
858
859 memset(&itv, 0, sizeof(itv));
860 setitimer(ITIMER_REAL, &itv, NULL);
861}
862
863#endif /* !defined(_WIN32) */
864
865
866#ifdef _WIN32
867
Stefan Weil2f9cba02011-04-05 18:34:21 +0200868static MMRESULT mm_timer;
Stefan Weil40f08e82012-04-27 05:34:40 +0000869static TIMECAPS mm_tc;
Stefan Weil2f9cba02011-04-05 18:34:21 +0200870
871static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
872 DWORD_PTR dwUser, DWORD_PTR dw1,
873 DWORD_PTR dw2)
874{
875 struct qemu_alarm_timer *t = alarm_timer;
876 if (!t) {
877 return;
878 }
Stefan Weil82051992012-04-20 11:27:24 +0200879 t->expired = true;
880 t->pending = true;
881 qemu_notify_event();
Stefan Weil2f9cba02011-04-05 18:34:21 +0200882}
883
884static int mm_start_timer(struct qemu_alarm_timer *t)
885{
Stefan Weil40f08e82012-04-27 05:34:40 +0000886 timeGetDevCaps(&mm_tc, sizeof(mm_tc));
Stefan Weil2f9cba02011-04-05 18:34:21 +0200887 return 0;
888}
889
890static void mm_stop_timer(struct qemu_alarm_timer *t)
891{
Paolo Bonzini0727b862013-02-20 14:43:31 +0100892 if (mm_timer) {
893 timeKillEvent(mm_timer);
894 }
Stefan Weil2f9cba02011-04-05 18:34:21 +0200895}
896
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100897static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta)
Stefan Weil2f9cba02011-04-05 18:34:21 +0200898{
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100899 int64_t nearest_delta_ms = delta / 1000000;
Stefan Weil40f08e82012-04-27 05:34:40 +0000900 if (nearest_delta_ms < mm_tc.wPeriodMin) {
901 nearest_delta_ms = mm_tc.wPeriodMin;
902 } else if (nearest_delta_ms > mm_tc.wPeriodMax) {
903 nearest_delta_ms = mm_tc.wPeriodMax;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100904 }
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100905
Paolo Bonzini0727b862013-02-20 14:43:31 +0100906 if (mm_timer) {
907 timeKillEvent(mm_timer);
908 }
Stefan Weil40f08e82012-04-27 05:34:40 +0000909 mm_timer = timeSetEvent((UINT)nearest_delta_ms,
910 mm_tc.wPeriodMin,
Stefan Weil2f9cba02011-04-05 18:34:21 +0200911 mm_alarm_handler,
912 (DWORD_PTR)t,
913 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
914
915 if (!mm_timer) {
Stefan Weil52ef6512012-05-08 19:14:43 +0200916 fprintf(stderr, "Failed to re-arm win32 alarm timer\n");
Stefan Weil40f08e82012-04-27 05:34:40 +0000917 timeEndPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200918 exit(1);
919 }
920}
921
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100922static int win32_start_timer(struct qemu_alarm_timer *t)
923{
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100924 HANDLE hTimer;
925 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100926
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100927 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
928 is zero) that has already expired, the timer is not updated. Since
929 creating a new timer is relatively expensive, set a bogus one-hour
930 interval in the dynticks case. */
931 success = CreateTimerQueueTimer(&hTimer,
932 NULL,
933 host_alarm_handler,
934 t,
935 1,
Stefan Weil82051992012-04-20 11:27:24 +0200936 3600000,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100937 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100938
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100939 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100940 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
941 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100942 return -1;
943 }
944
Stefan Weilcd0544e2011-04-10 20:15:09 +0200945 t->timer = hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100946 return 0;
947}
948
949static void win32_stop_timer(struct qemu_alarm_timer *t)
950{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200951 HANDLE hTimer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100952
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100953 if (hTimer) {
954 DeleteTimerQueueTimer(NULL, hTimer, NULL);
955 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100956}
957
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100958static void win32_rearm_timer(struct qemu_alarm_timer *t,
959 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100960{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200961 HANDLE hTimer = t->timer;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100962 int64_t nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100963 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100964
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100965 nearest_delta_ms = nearest_delta_ns / 1000000;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +0100966 if (nearest_delta_ms < 1) {
967 nearest_delta_ms = 1;
968 }
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100969 /* ULONG_MAX can be 32 bit */
970 if (nearest_delta_ms > ULONG_MAX) {
971 nearest_delta_ms = ULONG_MAX;
972 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100973 success = ChangeTimerQueueTimer(NULL,
974 hTimer,
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100975 (unsigned long) nearest_delta_ms,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100976 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100977
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100978 if (!success) {
979 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100980 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100981 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100982 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100983
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100984}
985
986#endif /* _WIN32 */
987
Paolo Bonzini4260a732011-09-19 10:18:51 +0200988static void quit_timers(void)
989{
990 struct qemu_alarm_timer *t = alarm_timer;
991 alarm_timer = NULL;
992 t->stop(t);
993}
994
Stefan Weil253ecf82012-11-04 21:42:08 +0100995#ifdef CONFIG_POSIX
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100996static void reinit_timers(void)
997{
998 struct qemu_alarm_timer *t = alarm_timer;
999 t->stop(t);
1000 if (t->start(t)) {
1001 fprintf(stderr, "Internal timer error: aborting\n");
1002 exit(1);
1003 }
1004 qemu_rearm_alarm_timer(t);
1005}
Stefan Weil253ecf82012-11-04 21:42:08 +01001006#endif /* CONFIG_POSIX */
Paolo Bonzinic8122c32012-11-02 15:43:22 +01001007
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001008int init_timer_alarm(void)
1009{
1010 struct qemu_alarm_timer *t = NULL;
1011 int i, err = -1;
1012
Paolo Bonzini744ca8e2012-10-29 15:26:28 +01001013 if (alarm_timer) {
1014 return 0;
1015 }
1016
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001017 for (i = 0; alarm_timers[i].name; i++) {
1018 t = &alarm_timers[i];
1019
1020 err = t->start(t);
1021 if (!err)
1022 break;
1023 }
1024
1025 if (err) {
1026 err = -ENOENT;
1027 goto fail;
1028 }
1029
Paolo Bonzini4260a732011-09-19 10:18:51 +02001030 atexit(quit_timers);
Paolo Bonzinic8122c32012-11-02 15:43:22 +01001031#ifdef CONFIG_POSIX
1032 pthread_atfork(NULL, NULL, reinit_timers);
1033#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001034 alarm_timer = t;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001035 return 0;
1036
1037fail:
1038 return err;
1039}
1040