blob: c5e456e0b14687e250f1350a27547b9d02c2f407 [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;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010076};
77
78struct qemu_alarm_timer {
79 char const *name;
80 int (*start)(struct qemu_alarm_timer *t);
81 void (*stop)(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010082 void (*rearm)(struct qemu_alarm_timer *t, int64_t nearest_delta_ns);
Stefan Weilcd0544e2011-04-10 20:15:09 +020083#if defined(__linux__)
Stefan Weilcd0544e2011-04-10 20:15:09 +020084 timer_t timer;
Stefan Weil9a14b292012-04-20 11:51:58 +020085 int fd;
Stefan Weilcd0544e2011-04-10 20:15:09 +020086#elif defined(_WIN32)
87 HANDLE timer;
88#endif
Stefan Weil5e1ec7b2012-04-20 10:45:48 +020089 bool expired;
90 bool pending;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010091};
92
93static struct qemu_alarm_timer *alarm_timer;
94
Alex Blighe93379b2013-08-21 16:02:39 +010095static bool timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
Stefan Weil45c7b372011-03-24 21:31:24 +010096{
97 return timer_head && (timer_head->expire_time <= current_time);
98}
99
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100100static int64_t qemu_next_alarm_deadline(void)
101{
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +0100102 int64_t delta = INT64_MAX;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100103 int64_t rtdelta;
Alex Blighff83c662013-08-21 16:02:46 +0100104 int64_t hdelta;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100105
Alex Blighff83c662013-08-21 16:02:46 +0100106 if (!use_icount && vm_clock->enabled &&
107 vm_clock->main_loop_timerlist->active_timers) {
108 delta = vm_clock->main_loop_timerlist->active_timers->expire_time -
109 qemu_get_clock_ns(vm_clock);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100110 }
Alex Blighff83c662013-08-21 16:02:46 +0100111 if (host_clock->enabled &&
112 host_clock->main_loop_timerlist->active_timers) {
113 hdelta = host_clock->main_loop_timerlist->active_timers->expire_time -
114 qemu_get_clock_ns(host_clock);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100115 if (hdelta < delta) {
116 delta = hdelta;
117 }
118 }
Alex Blighff83c662013-08-21 16:02:46 +0100119 if (rt_clock->enabled &&
120 rt_clock->main_loop_timerlist->active_timers) {
121 rtdelta = (rt_clock->main_loop_timerlist->active_timers->expire_time -
122 qemu_get_clock_ns(rt_clock));
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100123 if (rtdelta < delta) {
124 delta = rtdelta;
125 }
126 }
127
128 return delta;
129}
130
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100131static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
132{
Stefano Stabellini82274212012-05-29 03:35:24 +0000133 int64_t nearest_delta_ns = qemu_next_alarm_deadline();
134 if (nearest_delta_ns < INT64_MAX) {
135 t->rearm(t, nearest_delta_ns);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100136 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100137}
138
Paolo Bonzini9c132462011-02-03 14:48:59 +0100139/* TODO: MIN_TIMER_REARM_NS should be optimized */
140#define MIN_TIMER_REARM_NS 250000
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100141
142#ifdef _WIN32
143
Stefan Weil2f9cba02011-04-05 18:34:21 +0200144static int mm_start_timer(struct qemu_alarm_timer *t);
145static void mm_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100146static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200147
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100148static int win32_start_timer(struct qemu_alarm_timer *t);
149static void win32_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100150static void win32_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100151
152#else
153
154static int unix_start_timer(struct qemu_alarm_timer *t);
155static void unix_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100156static void unix_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100157
158#ifdef __linux__
159
160static int dynticks_start_timer(struct qemu_alarm_timer *t);
161static void dynticks_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100162static void dynticks_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100163
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100164#endif /* __linux__ */
165
166#endif /* _WIN32 */
167
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100168static struct qemu_alarm_timer alarm_timers[] = {
169#ifndef _WIN32
170#ifdef __linux__
171 {"dynticks", dynticks_start_timer,
Stefan Weilcd0544e2011-04-10 20:15:09 +0200172 dynticks_stop_timer, dynticks_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100173#endif
Paolo Bonzini84682832011-06-09 13:10:25 +0200174 {"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100175#else
Paolo Bonzinicca5de72011-11-09 12:46:56 +0100176 {"mmtimer", mm_start_timer, mm_stop_timer, mm_rearm_timer},
Stefan Weilcd0544e2011-04-10 20:15:09 +0200177 {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100178#endif
179 {NULL, }
180};
181
182static void show_available_alarms(void)
183{
184 int i;
185
186 printf("Available alarm timers, in order of precedence:\n");
187 for (i = 0; alarm_timers[i].name; i++)
188 printf("%s\n", alarm_timers[i].name);
189}
190
191void configure_alarms(char const *opt)
192{
193 int i;
194 int cur = 0;
195 int count = ARRAY_SIZE(alarm_timers) - 1;
196 char *arg;
197 char *name;
198 struct qemu_alarm_timer tmp;
199
Peter Maydellc8057f92012-08-02 13:45:54 +0100200 if (is_help_option(opt)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100201 show_available_alarms();
202 exit(0);
203 }
204
Anthony Liguori7267c092011-08-20 22:09:37 -0500205 arg = g_strdup(opt);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100206
207 /* Reorder the array */
208 name = strtok(arg, ",");
209 while (name) {
210 for (i = 0; i < count && alarm_timers[i].name; i++) {
211 if (!strcmp(alarm_timers[i].name, name))
212 break;
213 }
214
215 if (i == count) {
216 fprintf(stderr, "Unknown clock %s\n", name);
217 goto next;
218 }
219
220 if (i < cur)
221 /* Ignore */
222 goto next;
223
224 /* Swap */
225 tmp = alarm_timers[i];
226 alarm_timers[i] = alarm_timers[cur];
227 alarm_timers[cur] = tmp;
228
229 cur++;
230next:
231 name = strtok(NULL, ",");
232 }
233
Anthony Liguori7267c092011-08-20 22:09:37 -0500234 g_free(arg);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100235
236 if (cur) {
237 /* Disable remaining timers */
238 for (i = cur; i < count; i++)
239 alarm_timers[i].name = NULL;
240 } else {
241 show_available_alarms();
242 exit(1);
243 }
244}
245
Alex Blighff83c662013-08-21 16:02:46 +0100246static QEMUTimerList *timerlist_new_from_clock(QEMUClock *clock)
247{
248 QEMUTimerList *timer_list;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100249
Alex Blighff83c662013-08-21 16:02:46 +0100250 /* Assert if we do not have a clock. If you see this
251 * assertion in means that the clocks have not been
252 * initialised before a timerlist is needed. This
253 * normally happens if an AioContext is used before
254 * init_clocks() is called within main().
255 */
256 assert(clock);
257
258 timer_list = g_malloc0(sizeof(QEMUTimerList));
259 timer_list->clock = clock;
260 QLIST_INSERT_HEAD(&clock->timerlists, timer_list, list);
261 return timer_list;
262}
263
264QEMUTimerList *timerlist_new(QEMUClockType type)
265{
266 return timerlist_new_from_clock(qemu_clock_ptr(type));
267}
268
269void timerlist_free(QEMUTimerList *timer_list)
270{
271 assert(!timerlist_has_timers(timer_list));
272 if (timer_list->clock) {
273 QLIST_REMOVE(timer_list, list);
274 if (timer_list->clock->main_loop_timerlist == timer_list) {
275 timer_list->clock->main_loop_timerlist = NULL;
276 }
277 }
278 g_free(timer_list);
279}
280
281static QEMUClock *qemu_clock_new(QEMUClockType type)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100282{
283 QEMUClock *clock;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200284
Anthony Liguori7267c092011-08-20 22:09:37 -0500285 clock = g_malloc0(sizeof(QEMUClock));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100286 clock->type = type;
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200287 clock->enabled = true;
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200288 clock->last = INT64_MIN;
Alex Blighff83c662013-08-21 16:02:46 +0100289 QLIST_INIT(&clock->timerlists);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200290 notifier_list_init(&clock->reset_notifiers);
Alex Blighff83c662013-08-21 16:02:46 +0100291 clock->main_loop_timerlist = timerlist_new_from_clock(clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100292 return clock;
293}
294
Alex Blighff83c662013-08-21 16:02:46 +0100295bool qemu_clock_use_for_deadline(QEMUClock *clock)
296{
297 return !(use_icount && (clock->type == QEMU_CLOCK_VIRTUAL));
298}
299
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200300void qemu_clock_enable(QEMUClock *clock, bool enabled)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100301{
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200302 bool old = clock->enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100303 clock->enabled = enabled;
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200304 if (enabled && !old) {
305 qemu_rearm_alarm_timer(alarm_timer);
306 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100307}
308
Alex Blighff83c662013-08-21 16:02:46 +0100309bool timerlist_has_timers(QEMUTimerList *timer_list)
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200310{
Alex Blighff83c662013-08-21 16:02:46 +0100311 return !!timer_list->active_timers;
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200312}
313
Alex Blighff83c662013-08-21 16:02:46 +0100314bool qemu_clock_has_timers(QEMUClock *clock)
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200315{
Alex Blighff83c662013-08-21 16:02:46 +0100316 return timerlist_has_timers(clock->main_loop_timerlist);
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200317}
318
Alex Blighff83c662013-08-21 16:02:46 +0100319bool timerlist_expired(QEMUTimerList *timer_list)
320{
321 return (timer_list->active_timers &&
322 timer_list->active_timers->expire_time <
323 qemu_get_clock_ns(timer_list->clock));
324}
325
326bool qemu_clock_expired(QEMUClock *clock)
327{
328 return timerlist_expired(clock->main_loop_timerlist);
329}
330
331int64_t timerlist_deadline(QEMUTimerList *timer_list)
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200332{
333 /* To avoid problems with overflow limit this to 2^32. */
334 int64_t delta = INT32_MAX;
335
Alex Blighff83c662013-08-21 16:02:46 +0100336 if (timer_list->clock->enabled && timer_list->active_timers) {
337 delta = timer_list->active_timers->expire_time -
338 qemu_get_clock_ns(timer_list->clock);
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200339 }
340 if (delta < 0) {
341 delta = 0;
342 }
343 return delta;
344}
345
Alex Blighff83c662013-08-21 16:02:46 +0100346int64_t qemu_clock_deadline(QEMUClock *clock)
347{
348 return timerlist_deadline(clock->main_loop_timerlist);
349}
350
Alex Bligh02a03a92013-08-21 16:02:41 +0100351/*
352 * As above, but return -1 for no deadline, and do not cap to 2^32
353 * as we know the result is always positive.
354 */
355
Alex Blighff83c662013-08-21 16:02:46 +0100356int64_t timerlist_deadline_ns(QEMUTimerList *timer_list)
Alex Bligh02a03a92013-08-21 16:02:41 +0100357{
358 int64_t delta;
359
Alex Blighff83c662013-08-21 16:02:46 +0100360 if (!timer_list->clock->enabled || !timer_list->active_timers) {
Alex Bligh02a03a92013-08-21 16:02:41 +0100361 return -1;
362 }
363
Alex Blighff83c662013-08-21 16:02:46 +0100364 delta = timer_list->active_timers->expire_time -
365 qemu_get_clock_ns(timer_list->clock);
Alex Bligh02a03a92013-08-21 16:02:41 +0100366
367 if (delta <= 0) {
368 return 0;
369 }
370
371 return delta;
372}
373
Alex Blighff83c662013-08-21 16:02:46 +0100374int64_t qemu_clock_deadline_ns(QEMUClock *clock)
375{
376 return timerlist_deadline_ns(clock->main_loop_timerlist);
377}
378
379QEMUClock *timerlist_get_clock(QEMUTimerList *timer_list)
380{
381 return timer_list->clock;
382}
383
384QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClock *clock)
385{
386 return clock->main_loop_timerlist;
387}
388
Alex Bligh02a03a92013-08-21 16:02:41 +0100389/* Transition function to convert a nanosecond timeout to ms
390 * This is used where a system does not support ppoll
391 */
392int qemu_timeout_ns_to_ms(int64_t ns)
393{
394 int64_t ms;
395 if (ns < 0) {
396 return -1;
397 }
398
399 if (!ns) {
400 return 0;
401 }
402
403 /* Always round up, because it's better to wait too long than to wait too
404 * little and effectively busy-wait
405 */
406 ms = (ns + SCALE_MS - 1) / SCALE_MS;
407
408 /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
409 if (ms > (int64_t) INT32_MAX) {
410 ms = INT32_MAX;
411 }
412
413 return (int) ms;
414}
415
416
Alex Bligh4e0c6522013-08-21 16:02:43 +0100417/* qemu implementation of g_poll which uses a nanosecond timeout but is
418 * otherwise identical to g_poll
419 */
420int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout)
421{
422#ifdef CONFIG_PPOLL
423 if (timeout < 0) {
424 return ppoll((struct pollfd *)fds, nfds, NULL, NULL);
425 } else {
426 struct timespec ts;
427 ts.tv_sec = timeout / 1000000000LL;
428 ts.tv_nsec = timeout % 1000000000LL;
429 return ppoll((struct pollfd *)fds, nfds, &ts, NULL);
430 }
431#else
432 return g_poll(fds, nfds, qemu_timeout_ns_to_ms(timeout));
433#endif
434}
435
436
Alex Blighff83c662013-08-21 16:02:46 +0100437void timer_init(QEMUTimer *ts,
438 QEMUTimerList *timer_list, int scale,
439 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100440{
Alex Blighff83c662013-08-21 16:02:46 +0100441 ts->timer_list = timer_list;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100442 ts->cb = cb;
443 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100444 ts->scale = scale;
Alex Blighff83c662013-08-21 16:02:46 +0100445}
446
447QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
448 QEMUTimerCB *cb, void *opaque)
449{
450 return timer_new_tl(clock->main_loop_timerlist,
451 scale, cb, opaque);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100452}
453
454void qemu_free_timer(QEMUTimer *ts)
455{
Anthony Liguori7267c092011-08-20 22:09:37 -0500456 g_free(ts);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100457}
458
459/* stop a timer, but do not dealloc it */
460void qemu_del_timer(QEMUTimer *ts)
461{
462 QEMUTimer **pt, *t;
463
464 /* NOTE: this code must be signal safe because
Alex Blighe93379b2013-08-21 16:02:39 +0100465 timer_expired() can be called from a signal. */
Alex Blighff83c662013-08-21 16:02:46 +0100466 pt = &ts->timer_list->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100467 for(;;) {
468 t = *pt;
469 if (!t)
470 break;
471 if (t == ts) {
472 *pt = t->next;
473 break;
474 }
475 pt = &t->next;
476 }
477}
478
479/* modify the current timer so that it will be fired when current_time
480 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200481void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100482{
483 QEMUTimer **pt, *t;
484
485 qemu_del_timer(ts);
486
487 /* add the timer in the sorted list */
488 /* NOTE: this code must be signal safe because
Alex Blighe93379b2013-08-21 16:02:39 +0100489 timer_expired() can be called from a signal. */
Alex Blighff83c662013-08-21 16:02:46 +0100490 pt = &ts->timer_list->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100491 for(;;) {
492 t = *pt;
Alex Blighe93379b2013-08-21 16:02:39 +0100493 if (!timer_expired_ns(t, expire_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100494 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100495 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100496 pt = &t->next;
497 }
498 ts->expire_time = expire_time;
499 ts->next = *pt;
500 *pt = ts;
501
502 /* Rearm if necessary */
Alex Blighff83c662013-08-21 16:02:46 +0100503 if (pt == &ts->timer_list->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100504 if (!alarm_timer->pending) {
505 qemu_rearm_alarm_timer(alarm_timer);
506 }
507 /* Interrupt execution to force deadline recalculation. */
Alex Blighff83c662013-08-21 16:02:46 +0100508 qemu_clock_warp(ts->timer_list->clock);
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200509 if (use_icount) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100510 qemu_notify_event();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200511 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100512 }
513}
514
Paolo Bonzini4a998742011-03-11 16:33:58 +0100515void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
516{
517 qemu_mod_timer_ns(ts, expire_time * ts->scale);
518}
519
Alex Blighe93379b2013-08-21 16:02:39 +0100520bool timer_pending(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100521{
522 QEMUTimer *t;
Alex Blighff83c662013-08-21 16:02:46 +0100523 for (t = ts->timer_list->active_timers; t != NULL; t = t->next) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200524 if (t == ts) {
525 return true;
526 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100527 }
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200528 return false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100529}
530
Alex Blighe93379b2013-08-21 16:02:39 +0100531bool timer_expired(QEMUTimer *timer_head, int64_t current_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100532{
Alex Blighe93379b2013-08-21 16:02:39 +0100533 return timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100534}
535
Alex Blighff83c662013-08-21 16:02:46 +0100536bool timerlist_run_timers(QEMUTimerList *timer_list)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100537{
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200538 QEMUTimer *ts;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100539 int64_t current_time;
Alex Blighf9a976b2013-08-21 16:02:45 +0100540 bool progress = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100541
Alex Blighff83c662013-08-21 16:02:46 +0100542 if (!timer_list->clock->enabled) {
Alex Blighf9a976b2013-08-21 16:02:45 +0100543 return progress;
Alex Blighff83c662013-08-21 16:02:46 +0100544 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100545
Alex Blighff83c662013-08-21 16:02:46 +0100546 current_time = qemu_get_clock_ns(timer_list->clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100547 for(;;) {
Alex Blighff83c662013-08-21 16:02:46 +0100548 ts = timer_list->active_timers;
Alex Blighe93379b2013-08-21 16:02:39 +0100549 if (!timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100550 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100551 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100552 /* remove timer from the list before calling the callback */
Alex Blighff83c662013-08-21 16:02:46 +0100553 timer_list->active_timers = ts->next;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100554 ts->next = NULL;
555
556 /* run the callback (the timer list can be modified) */
557 ts->cb(ts->opaque);
Alex Blighf9a976b2013-08-21 16:02:45 +0100558 progress = true;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100559 }
Alex Blighf9a976b2013-08-21 16:02:45 +0100560 return progress;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100561}
562
Alex Blighff83c662013-08-21 16:02:46 +0100563bool qemu_run_timers(QEMUClock *clock)
564{
565 return timerlist_run_timers(clock->main_loop_timerlist);
566}
567
Alex Bligh754d6a52013-08-21 16:02:48 +0100568void timerlistgroup_init(QEMUTimerListGroup *tlg)
569{
570 QEMUClockType type;
571 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
572 tlg->tl[type] = timerlist_new(type);
573 }
574}
575
576void timerlistgroup_deinit(QEMUTimerListGroup *tlg)
577{
578 QEMUClockType type;
579 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
580 timerlist_free(tlg->tl[type]);
581 }
582}
583
584bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg)
585{
586 QEMUClockType type;
587 bool progress = false;
588 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
589 progress |= timerlist_run_timers(tlg->tl[type]);
590 }
591 return progress;
592}
593
594int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg)
595{
596 int64_t deadline = -1;
597 QEMUClockType type;
598 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
599 if (qemu_clock_use_for_deadline(tlg->tl[type]->clock)) {
600 deadline = qemu_soonest_timeout(deadline,
601 timerlist_deadline_ns(
602 tlg->tl[type]));
603 }
604 }
605 return deadline;
606}
607
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100608int64_t qemu_get_clock_ns(QEMUClock *clock)
609{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200610 int64_t now, last;
611
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100612 switch(clock->type) {
613 case QEMU_CLOCK_REALTIME:
614 return get_clock();
615 default:
616 case QEMU_CLOCK_VIRTUAL:
617 if (use_icount) {
618 return cpu_get_icount();
619 } else {
620 return cpu_get_clock();
621 }
622 case QEMU_CLOCK_HOST:
Jan Kiszka691a0c92011-06-20 14:06:27 +0200623 now = get_clock_realtime();
624 last = clock->last;
625 clock->last = now;
626 if (now < last) {
627 notifier_list_notify(&clock->reset_notifiers, &now);
628 }
629 return now;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100630 }
631}
632
Jan Kiszka691a0c92011-06-20 14:06:27 +0200633void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
634{
635 notifier_list_add(&clock->reset_notifiers, notifier);
636}
637
638void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
639{
Paolo Bonzini31552522012-01-13 17:34:01 +0100640 notifier_remove(notifier);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200641}
642
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100643void init_clocks(void)
644{
Alex Blighff83c662013-08-21 16:02:46 +0100645 QEMUClockType type;
646 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
647 if (!qemu_clocks[type]) {
648 qemu_clocks[type] = qemu_clock_new(type);
Alex Bligh754d6a52013-08-21 16:02:48 +0100649 main_loop_tlg.tl[type] = qemu_clocks[type]->main_loop_timerlist;
Alex Blighff83c662013-08-21 16:02:46 +0100650 }
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100651 }
Alex Blighff83c662013-08-21 16:02:46 +0100652
Alex Blighcd758dd2013-08-21 16:02:44 +0100653#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
654 prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0);
655#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100656}
657
Alex Blighe93379b2013-08-21 16:02:39 +0100658uint64_t timer_expire_time_ns(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100659{
Alex Blighe93379b2013-08-21 16:02:39 +0100660 return timer_pending(ts) ? ts->expire_time : -1;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100661}
662
Alex Blighf9a976b2013-08-21 16:02:45 +0100663bool qemu_run_all_timers(void)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100664{
Alex Blighf9a976b2013-08-21 16:02:45 +0100665 bool progress = false;
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200666 alarm_timer->pending = false;
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100667
Peter Portante158fd3c2012-04-05 11:00:45 -0400668 /* vm time timers */
Alex Blighff83c662013-08-21 16:02:46 +0100669 QEMUClockType type;
670 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
671 progress |= qemu_run_timers(qemu_clock_ptr(type));
672 }
Peter Portante158fd3c2012-04-05 11:00:45 -0400673
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100674 /* rearm timer, if not periodic */
675 if (alarm_timer->expired) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200676 alarm_timer->expired = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100677 qemu_rearm_alarm_timer(alarm_timer);
678 }
Alex Blighf9a976b2013-08-21 16:02:45 +0100679
680 return progress;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100681}
682
683#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100684static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100685#else
686static void host_alarm_handler(int host_signum)
687#endif
688{
689 struct qemu_alarm_timer *t = alarm_timer;
690 if (!t)
691 return;
692
Stefan Weil82051992012-04-20 11:27:24 +0200693 t->expired = true;
694 t->pending = true;
695 qemu_notify_event();
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100696}
697
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100698#if defined(__linux__)
699
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100700#include "qemu/compatfd.h"
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200701
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100702static int dynticks_start_timer(struct qemu_alarm_timer *t)
703{
704 struct sigevent ev;
705 timer_t host_timer;
706 struct sigaction act;
707
708 sigfillset(&act.sa_mask);
709 act.sa_flags = 0;
710 act.sa_handler = host_alarm_handler;
711
712 sigaction(SIGALRM, &act, NULL);
713
714 /*
715 * Initialize ev struct to 0 to avoid valgrind complaining
716 * about uninitialized data in timer_create call
717 */
718 memset(&ev, 0, sizeof(ev));
719 ev.sigev_value.sival_int = 0;
720 ev.sigev_notify = SIGEV_SIGNAL;
Richard Henderson1e9737d2012-10-23 07:33:00 +1000721#ifdef CONFIG_SIGEV_THREAD_ID
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200722 if (qemu_signalfd_available()) {
723 ev.sigev_notify = SIGEV_THREAD_ID;
724 ev._sigev_un._tid = qemu_get_thread_id();
725 }
Richard Henderson1e9737d2012-10-23 07:33:00 +1000726#endif /* CONFIG_SIGEV_THREAD_ID */
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100727 ev.sigev_signo = SIGALRM;
728
729 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
730 perror("timer_create");
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100731 return -1;
732 }
733
Stefan Weilcd0544e2011-04-10 20:15:09 +0200734 t->timer = host_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100735
736 return 0;
737}
738
739static void dynticks_stop_timer(struct qemu_alarm_timer *t)
740{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200741 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100742
743 timer_delete(host_timer);
744}
745
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100746static void dynticks_rearm_timer(struct qemu_alarm_timer *t,
747 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100748{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200749 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100750 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100751 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100752
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100753 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
754 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100755
756 /* check whether a timer is already running */
757 if (timer_gettime(host_timer, &timeout)) {
758 perror("gettime");
759 fprintf(stderr, "Internal timer error: aborting\n");
760 exit(1);
761 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100762 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
763 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100764 return;
765
766 timeout.it_interval.tv_sec = 0;
767 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100768 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
769 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100770 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
771 perror("settime");
772 fprintf(stderr, "Internal timer error: aborting\n");
773 exit(1);
774 }
775}
776
777#endif /* defined(__linux__) */
778
Stefan Weilf26e5a52011-02-04 22:01:32 +0100779#if !defined(_WIN32)
780
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100781static int unix_start_timer(struct qemu_alarm_timer *t)
782{
783 struct sigaction act;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100784
785 /* timer signal */
786 sigfillset(&act.sa_mask);
787 act.sa_flags = 0;
788 act.sa_handler = host_alarm_handler;
789
790 sigaction(SIGALRM, &act, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200791 return 0;
792}
793
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100794static void unix_rearm_timer(struct qemu_alarm_timer *t,
795 int64_t nearest_delta_ns)
Paolo Bonzini84682832011-06-09 13:10:25 +0200796{
797 struct itimerval itv;
Paolo Bonzini84682832011-06-09 13:10:25 +0200798 int err;
799
Paolo Bonzini84682832011-06-09 13:10:25 +0200800 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
801 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100802
803 itv.it_interval.tv_sec = 0;
Paolo Bonzini84682832011-06-09 13:10:25 +0200804 itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */
805 itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
806 itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100807 err = setitimer(ITIMER_REAL, &itv, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200808 if (err) {
809 perror("setitimer");
810 fprintf(stderr, "Internal timer error: aborting\n");
811 exit(1);
812 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100813}
814
815static void unix_stop_timer(struct qemu_alarm_timer *t)
816{
817 struct itimerval itv;
818
819 memset(&itv, 0, sizeof(itv));
820 setitimer(ITIMER_REAL, &itv, NULL);
821}
822
823#endif /* !defined(_WIN32) */
824
825
826#ifdef _WIN32
827
Stefan Weil2f9cba02011-04-05 18:34:21 +0200828static MMRESULT mm_timer;
Stefan Weil40f08e82012-04-27 05:34:40 +0000829static TIMECAPS mm_tc;
Stefan Weil2f9cba02011-04-05 18:34:21 +0200830
831static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
832 DWORD_PTR dwUser, DWORD_PTR dw1,
833 DWORD_PTR dw2)
834{
835 struct qemu_alarm_timer *t = alarm_timer;
836 if (!t) {
837 return;
838 }
Stefan Weil82051992012-04-20 11:27:24 +0200839 t->expired = true;
840 t->pending = true;
841 qemu_notify_event();
Stefan Weil2f9cba02011-04-05 18:34:21 +0200842}
843
844static int mm_start_timer(struct qemu_alarm_timer *t)
845{
Stefan Weil40f08e82012-04-27 05:34:40 +0000846 timeGetDevCaps(&mm_tc, sizeof(mm_tc));
Stefan Weil2f9cba02011-04-05 18:34:21 +0200847 return 0;
848}
849
850static void mm_stop_timer(struct qemu_alarm_timer *t)
851{
Paolo Bonzini0727b862013-02-20 14:43:31 +0100852 if (mm_timer) {
853 timeKillEvent(mm_timer);
854 }
Stefan Weil2f9cba02011-04-05 18:34:21 +0200855}
856
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100857static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta)
Stefan Weil2f9cba02011-04-05 18:34:21 +0200858{
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100859 int64_t nearest_delta_ms = delta / 1000000;
Stefan Weil40f08e82012-04-27 05:34:40 +0000860 if (nearest_delta_ms < mm_tc.wPeriodMin) {
861 nearest_delta_ms = mm_tc.wPeriodMin;
862 } else if (nearest_delta_ms > mm_tc.wPeriodMax) {
863 nearest_delta_ms = mm_tc.wPeriodMax;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100864 }
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100865
Paolo Bonzini0727b862013-02-20 14:43:31 +0100866 if (mm_timer) {
867 timeKillEvent(mm_timer);
868 }
Stefan Weil40f08e82012-04-27 05:34:40 +0000869 mm_timer = timeSetEvent((UINT)nearest_delta_ms,
870 mm_tc.wPeriodMin,
Stefan Weil2f9cba02011-04-05 18:34:21 +0200871 mm_alarm_handler,
872 (DWORD_PTR)t,
873 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
874
875 if (!mm_timer) {
Stefan Weil52ef6512012-05-08 19:14:43 +0200876 fprintf(stderr, "Failed to re-arm win32 alarm timer\n");
Stefan Weil40f08e82012-04-27 05:34:40 +0000877 timeEndPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200878 exit(1);
879 }
880}
881
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100882static int win32_start_timer(struct qemu_alarm_timer *t)
883{
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100884 HANDLE hTimer;
885 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100886
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100887 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
888 is zero) that has already expired, the timer is not updated. Since
889 creating a new timer is relatively expensive, set a bogus one-hour
890 interval in the dynticks case. */
891 success = CreateTimerQueueTimer(&hTimer,
892 NULL,
893 host_alarm_handler,
894 t,
895 1,
Stefan Weil82051992012-04-20 11:27:24 +0200896 3600000,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100897 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100898
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100899 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100900 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
901 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100902 return -1;
903 }
904
Stefan Weilcd0544e2011-04-10 20:15:09 +0200905 t->timer = hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100906 return 0;
907}
908
909static void win32_stop_timer(struct qemu_alarm_timer *t)
910{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200911 HANDLE hTimer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100912
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100913 if (hTimer) {
914 DeleteTimerQueueTimer(NULL, hTimer, NULL);
915 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100916}
917
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100918static void win32_rearm_timer(struct qemu_alarm_timer *t,
919 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100920{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200921 HANDLE hTimer = t->timer;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100922 int64_t nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100923 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100924
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100925 nearest_delta_ms = nearest_delta_ns / 1000000;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +0100926 if (nearest_delta_ms < 1) {
927 nearest_delta_ms = 1;
928 }
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100929 /* ULONG_MAX can be 32 bit */
930 if (nearest_delta_ms > ULONG_MAX) {
931 nearest_delta_ms = ULONG_MAX;
932 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100933 success = ChangeTimerQueueTimer(NULL,
934 hTimer,
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100935 (unsigned long) nearest_delta_ms,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100936 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100937
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100938 if (!success) {
939 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100940 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100941 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100942 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100943
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100944}
945
946#endif /* _WIN32 */
947
Paolo Bonzini4260a732011-09-19 10:18:51 +0200948static void quit_timers(void)
949{
950 struct qemu_alarm_timer *t = alarm_timer;
951 alarm_timer = NULL;
952 t->stop(t);
953}
954
Stefan Weil253ecf82012-11-04 21:42:08 +0100955#ifdef CONFIG_POSIX
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100956static void reinit_timers(void)
957{
958 struct qemu_alarm_timer *t = alarm_timer;
959 t->stop(t);
960 if (t->start(t)) {
961 fprintf(stderr, "Internal timer error: aborting\n");
962 exit(1);
963 }
964 qemu_rearm_alarm_timer(t);
965}
Stefan Weil253ecf82012-11-04 21:42:08 +0100966#endif /* CONFIG_POSIX */
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100967
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100968int init_timer_alarm(void)
969{
970 struct qemu_alarm_timer *t = NULL;
971 int i, err = -1;
972
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100973 if (alarm_timer) {
974 return 0;
975 }
976
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100977 for (i = 0; alarm_timers[i].name; i++) {
978 t = &alarm_timers[i];
979
980 err = t->start(t);
981 if (!err)
982 break;
983 }
984
985 if (err) {
986 err = -ENOENT;
987 goto fail;
988 }
989
Paolo Bonzini4260a732011-09-19 10:18:51 +0200990 atexit(quit_timers);
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100991#ifdef CONFIG_POSIX
992 pthread_atfork(NULL, NULL, reinit_timers);
993#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100994 alarm_timer = t;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100995 return 0;
996
997fail:
998 return err;
999}
1000