blob: 1f7952d3502f4410df15308fecee12637f7b4ca1 [file] [log] [blame]
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#include "sysemu.h"
26#include "net.h"
27#include "monitor.h"
28#include "console.h"
29
30#include "hw/hw.h"
31
Stefan Weilbff9f8b2012-04-20 10:27:06 +020032#include "qemu-timer.h"
33
Juergen Lock44459342010-03-25 22:35:03 +010034#ifdef __FreeBSD__
35#include <sys/param.h>
36#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010037
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010038#ifdef _WIN32
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010039#include <mmsystem.h>
40#endif
41
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010042/***********************************************************/
43/* timers */
44
45#define QEMU_CLOCK_REALTIME 0
46#define QEMU_CLOCK_VIRTUAL 1
47#define QEMU_CLOCK_HOST 2
48
49struct QEMUClock {
50 int type;
Stefan Weil5e1ec7b2012-04-20 10:45:48 +020051 bool enabled;
Paolo Bonziniab33fcd2011-04-13 10:03:44 +020052
Paolo Bonzini688eb382011-09-13 11:42:26 +020053 QEMUTimer *active_timers;
Jan Kiszka691a0c92011-06-20 14:06:27 +020054
55 NotifierList reset_notifiers;
56 int64_t last;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010057};
58
59struct QEMUTimer {
60 QEMUClock *clock;
Paolo Bonzini4a998742011-03-11 16:33:58 +010061 int64_t expire_time; /* in nanoseconds */
62 int scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010063 QEMUTimerCB *cb;
64 void *opaque;
65 struct QEMUTimer *next;
66};
67
68struct qemu_alarm_timer {
69 char const *name;
70 int (*start)(struct qemu_alarm_timer *t);
71 void (*stop)(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010072 void (*rearm)(struct qemu_alarm_timer *t, int64_t nearest_delta_ns);
Stefan Weilcd0544e2011-04-10 20:15:09 +020073#if defined(__linux__)
74 int fd;
75 timer_t timer;
76#elif defined(_WIN32)
77 HANDLE timer;
78#endif
Stefan Weil5e1ec7b2012-04-20 10:45:48 +020079 bool expired;
80 bool pending;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010081};
82
83static struct qemu_alarm_timer *alarm_timer;
84
Stefan Weil45c7b372011-03-24 21:31:24 +010085static bool qemu_timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
86{
87 return timer_head && (timer_head->expire_time <= current_time);
88}
89
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010090static int64_t qemu_next_alarm_deadline(void)
91{
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +010092 int64_t delta = INT64_MAX;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010093 int64_t rtdelta;
94
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +010095 if (!use_icount && vm_clock->enabled && vm_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010096 delta = vm_clock->active_timers->expire_time -
97 qemu_get_clock_ns(vm_clock);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010098 }
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +010099 if (host_clock->enabled && host_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100100 int64_t hdelta = host_clock->active_timers->expire_time -
101 qemu_get_clock_ns(host_clock);
102 if (hdelta < delta) {
103 delta = hdelta;
104 }
105 }
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +0100106 if (rt_clock->enabled && rt_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100107 rtdelta = (rt_clock->active_timers->expire_time -
108 qemu_get_clock_ns(rt_clock));
109 if (rtdelta < delta) {
110 delta = rtdelta;
111 }
112 }
113
114 return delta;
115}
116
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100117static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
118{
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100119 int64_t nearest_delta_ns;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100120 if (!rt_clock->active_timers &&
121 !vm_clock->active_timers &&
122 !host_clock->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100123 return;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100124 }
125 nearest_delta_ns = qemu_next_alarm_deadline();
126 t->rearm(t, nearest_delta_ns);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100127}
128
Paolo Bonzini9c132462011-02-03 14:48:59 +0100129/* TODO: MIN_TIMER_REARM_NS should be optimized */
130#define MIN_TIMER_REARM_NS 250000
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100131
132#ifdef _WIN32
133
Stefan Weil2f9cba02011-04-05 18:34:21 +0200134static int mm_start_timer(struct qemu_alarm_timer *t);
135static void mm_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100136static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200137
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100138static int win32_start_timer(struct qemu_alarm_timer *t);
139static void win32_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100140static void win32_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100141
142#else
143
144static int unix_start_timer(struct qemu_alarm_timer *t);
145static void unix_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100146static void unix_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100147
148#ifdef __linux__
149
150static int dynticks_start_timer(struct qemu_alarm_timer *t);
151static void dynticks_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100152static void dynticks_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100153
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100154#endif /* __linux__ */
155
156#endif /* _WIN32 */
157
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100158static struct qemu_alarm_timer alarm_timers[] = {
159#ifndef _WIN32
160#ifdef __linux__
161 {"dynticks", dynticks_start_timer,
Stefan Weilcd0544e2011-04-10 20:15:09 +0200162 dynticks_stop_timer, dynticks_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100163#endif
Paolo Bonzini84682832011-06-09 13:10:25 +0200164 {"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100165#else
Paolo Bonzinicca5de72011-11-09 12:46:56 +0100166 {"mmtimer", mm_start_timer, mm_stop_timer, mm_rearm_timer},
Stefan Weilcd0544e2011-04-10 20:15:09 +0200167 {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100168#endif
169 {NULL, }
170};
171
172static void show_available_alarms(void)
173{
174 int i;
175
176 printf("Available alarm timers, in order of precedence:\n");
177 for (i = 0; alarm_timers[i].name; i++)
178 printf("%s\n", alarm_timers[i].name);
179}
180
181void configure_alarms(char const *opt)
182{
183 int i;
184 int cur = 0;
185 int count = ARRAY_SIZE(alarm_timers) - 1;
186 char *arg;
187 char *name;
188 struct qemu_alarm_timer tmp;
189
190 if (!strcmp(opt, "?")) {
191 show_available_alarms();
192 exit(0);
193 }
194
Anthony Liguori7267c092011-08-20 22:09:37 -0500195 arg = g_strdup(opt);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100196
197 /* Reorder the array */
198 name = strtok(arg, ",");
199 while (name) {
200 for (i = 0; i < count && alarm_timers[i].name; i++) {
201 if (!strcmp(alarm_timers[i].name, name))
202 break;
203 }
204
205 if (i == count) {
206 fprintf(stderr, "Unknown clock %s\n", name);
207 goto next;
208 }
209
210 if (i < cur)
211 /* Ignore */
212 goto next;
213
214 /* Swap */
215 tmp = alarm_timers[i];
216 alarm_timers[i] = alarm_timers[cur];
217 alarm_timers[cur] = tmp;
218
219 cur++;
220next:
221 name = strtok(NULL, ",");
222 }
223
Anthony Liguori7267c092011-08-20 22:09:37 -0500224 g_free(arg);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100225
226 if (cur) {
227 /* Disable remaining timers */
228 for (i = cur; i < count; i++)
229 alarm_timers[i].name = NULL;
230 } else {
231 show_available_alarms();
232 exit(1);
233 }
234}
235
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100236QEMUClock *rt_clock;
237QEMUClock *vm_clock;
238QEMUClock *host_clock;
239
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100240static QEMUClock *qemu_new_clock(int type)
241{
242 QEMUClock *clock;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200243
Anthony Liguori7267c092011-08-20 22:09:37 -0500244 clock = g_malloc0(sizeof(QEMUClock));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100245 clock->type = type;
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200246 clock->enabled = true;
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200247 clock->last = INT64_MIN;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200248 notifier_list_init(&clock->reset_notifiers);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100249 return clock;
250}
251
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200252void qemu_clock_enable(QEMUClock *clock, bool enabled)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100253{
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200254 bool old = clock->enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100255 clock->enabled = enabled;
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200256 if (enabled && !old) {
257 qemu_rearm_alarm_timer(alarm_timer);
258 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100259}
260
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200261int64_t qemu_clock_has_timers(QEMUClock *clock)
262{
263 return !!clock->active_timers;
264}
265
266int64_t qemu_clock_expired(QEMUClock *clock)
267{
268 return (clock->active_timers &&
269 clock->active_timers->expire_time < qemu_get_clock_ns(clock));
270}
271
272int64_t qemu_clock_deadline(QEMUClock *clock)
273{
274 /* To avoid problems with overflow limit this to 2^32. */
275 int64_t delta = INT32_MAX;
276
277 if (clock->active_timers) {
278 delta = clock->active_timers->expire_time - qemu_get_clock_ns(clock);
279 }
280 if (delta < 0) {
281 delta = 0;
282 }
283 return delta;
284}
285
Paolo Bonzini4a998742011-03-11 16:33:58 +0100286QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
287 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100288{
289 QEMUTimer *ts;
290
Anthony Liguori7267c092011-08-20 22:09:37 -0500291 ts = g_malloc0(sizeof(QEMUTimer));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100292 ts->clock = clock;
293 ts->cb = cb;
294 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100295 ts->scale = scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100296 return ts;
297}
298
299void qemu_free_timer(QEMUTimer *ts)
300{
Anthony Liguori7267c092011-08-20 22:09:37 -0500301 g_free(ts);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100302}
303
304/* stop a timer, but do not dealloc it */
305void qemu_del_timer(QEMUTimer *ts)
306{
307 QEMUTimer **pt, *t;
308
309 /* NOTE: this code must be signal safe because
310 qemu_timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200311 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100312 for(;;) {
313 t = *pt;
314 if (!t)
315 break;
316 if (t == ts) {
317 *pt = t->next;
318 break;
319 }
320 pt = &t->next;
321 }
322}
323
324/* modify the current timer so that it will be fired when current_time
325 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200326void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100327{
328 QEMUTimer **pt, *t;
329
330 qemu_del_timer(ts);
331
332 /* add the timer in the sorted list */
333 /* NOTE: this code must be signal safe because
334 qemu_timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200335 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100336 for(;;) {
337 t = *pt;
Stefan Weil45c7b372011-03-24 21:31:24 +0100338 if (!qemu_timer_expired_ns(t, expire_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100339 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100340 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100341 pt = &t->next;
342 }
343 ts->expire_time = expire_time;
344 ts->next = *pt;
345 *pt = ts;
346
347 /* Rearm if necessary */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200348 if (pt == &ts->clock->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100349 if (!alarm_timer->pending) {
350 qemu_rearm_alarm_timer(alarm_timer);
351 }
352 /* Interrupt execution to force deadline recalculation. */
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200353 qemu_clock_warp(ts->clock);
354 if (use_icount) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100355 qemu_notify_event();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200356 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100357 }
358}
359
Paolo Bonzini4a998742011-03-11 16:33:58 +0100360void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
361{
362 qemu_mod_timer_ns(ts, expire_time * ts->scale);
363}
364
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200365bool qemu_timer_pending(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100366{
367 QEMUTimer *t;
Paolo Bonzini688eb382011-09-13 11:42:26 +0200368 for (t = ts->clock->active_timers; t != NULL; t = t->next) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200369 if (t == ts) {
370 return true;
371 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100372 }
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200373 return false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100374}
375
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200376bool qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100377{
Stefan Weil45c7b372011-03-24 21:31:24 +0100378 return qemu_timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100379}
380
Paolo Bonzini8156be52012-03-28 15:42:04 +0200381void qemu_run_timers(QEMUClock *clock)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100382{
383 QEMUTimer **ptimer_head, *ts;
384 int64_t current_time;
385
386 if (!clock->enabled)
387 return;
388
Paolo Bonzini4a998742011-03-11 16:33:58 +0100389 current_time = qemu_get_clock_ns(clock);
Paolo Bonzini688eb382011-09-13 11:42:26 +0200390 ptimer_head = &clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100391 for(;;) {
392 ts = *ptimer_head;
Stefan Weil45c7b372011-03-24 21:31:24 +0100393 if (!qemu_timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100394 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100395 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100396 /* remove timer from the list before calling the callback */
397 *ptimer_head = ts->next;
398 ts->next = NULL;
399
400 /* run the callback (the timer list can be modified) */
401 ts->cb(ts->opaque);
402 }
403}
404
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100405int64_t qemu_get_clock_ns(QEMUClock *clock)
406{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200407 int64_t now, last;
408
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100409 switch(clock->type) {
410 case QEMU_CLOCK_REALTIME:
411 return get_clock();
412 default:
413 case QEMU_CLOCK_VIRTUAL:
414 if (use_icount) {
415 return cpu_get_icount();
416 } else {
417 return cpu_get_clock();
418 }
419 case QEMU_CLOCK_HOST:
Jan Kiszka691a0c92011-06-20 14:06:27 +0200420 now = get_clock_realtime();
421 last = clock->last;
422 clock->last = now;
423 if (now < last) {
424 notifier_list_notify(&clock->reset_notifiers, &now);
425 }
426 return now;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100427 }
428}
429
Jan Kiszka691a0c92011-06-20 14:06:27 +0200430void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
431{
432 notifier_list_add(&clock->reset_notifiers, notifier);
433}
434
435void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
436{
Paolo Bonzini31552522012-01-13 17:34:01 +0100437 notifier_remove(notifier);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200438}
439
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100440void init_clocks(void)
441{
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100442 rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
443 vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
444 host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100445}
446
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200447uint64_t qemu_timer_expire_time_ns(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100448{
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200449 return qemu_timer_pending(ts) ? ts->expire_time : -1;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100450}
451
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100452void qemu_run_all_timers(void)
453{
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200454 alarm_timer->pending = false;
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100455
Peter Portante158fd3c2012-04-05 11:00:45 -0400456 /* vm time timers */
457 qemu_run_timers(vm_clock);
458 qemu_run_timers(rt_clock);
459 qemu_run_timers(host_clock);
460
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100461 /* rearm timer, if not periodic */
462 if (alarm_timer->expired) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200463 alarm_timer->expired = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100464 qemu_rearm_alarm_timer(alarm_timer);
465 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100466}
467
468#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100469static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100470#else
471static void host_alarm_handler(int host_signum)
472#endif
473{
474 struct qemu_alarm_timer *t = alarm_timer;
475 if (!t)
476 return;
477
Stefan Weil82051992012-04-20 11:27:24 +0200478 t->expired = true;
479 t->pending = true;
480 qemu_notify_event();
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100481}
482
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100483#if defined(__linux__)
484
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200485#include "compatfd.h"
486
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100487static int dynticks_start_timer(struct qemu_alarm_timer *t)
488{
489 struct sigevent ev;
490 timer_t host_timer;
491 struct sigaction act;
492
493 sigfillset(&act.sa_mask);
494 act.sa_flags = 0;
495 act.sa_handler = host_alarm_handler;
496
497 sigaction(SIGALRM, &act, NULL);
498
499 /*
500 * Initialize ev struct to 0 to avoid valgrind complaining
501 * about uninitialized data in timer_create call
502 */
503 memset(&ev, 0, sizeof(ev));
504 ev.sigev_value.sival_int = 0;
505 ev.sigev_notify = SIGEV_SIGNAL;
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200506#ifdef SIGEV_THREAD_ID
507 if (qemu_signalfd_available()) {
508 ev.sigev_notify = SIGEV_THREAD_ID;
509 ev._sigev_un._tid = qemu_get_thread_id();
510 }
511#endif /* SIGEV_THREAD_ID */
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100512 ev.sigev_signo = SIGALRM;
513
514 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
515 perror("timer_create");
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100516 return -1;
517 }
518
Stefan Weilcd0544e2011-04-10 20:15:09 +0200519 t->timer = host_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100520
521 return 0;
522}
523
524static void dynticks_stop_timer(struct qemu_alarm_timer *t)
525{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200526 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100527
528 timer_delete(host_timer);
529}
530
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100531static void dynticks_rearm_timer(struct qemu_alarm_timer *t,
532 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100533{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200534 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100535 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100536 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100537
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100538 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
539 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100540
541 /* check whether a timer is already running */
542 if (timer_gettime(host_timer, &timeout)) {
543 perror("gettime");
544 fprintf(stderr, "Internal timer error: aborting\n");
545 exit(1);
546 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100547 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
548 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100549 return;
550
551 timeout.it_interval.tv_sec = 0;
552 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100553 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
554 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100555 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
556 perror("settime");
557 fprintf(stderr, "Internal timer error: aborting\n");
558 exit(1);
559 }
560}
561
562#endif /* defined(__linux__) */
563
Stefan Weilf26e5a52011-02-04 22:01:32 +0100564#if !defined(_WIN32)
565
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100566static int unix_start_timer(struct qemu_alarm_timer *t)
567{
568 struct sigaction act;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100569
570 /* timer signal */
571 sigfillset(&act.sa_mask);
572 act.sa_flags = 0;
573 act.sa_handler = host_alarm_handler;
574
575 sigaction(SIGALRM, &act, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200576 return 0;
577}
578
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100579static void unix_rearm_timer(struct qemu_alarm_timer *t,
580 int64_t nearest_delta_ns)
Paolo Bonzini84682832011-06-09 13:10:25 +0200581{
582 struct itimerval itv;
Paolo Bonzini84682832011-06-09 13:10:25 +0200583 int err;
584
Paolo Bonzini84682832011-06-09 13:10:25 +0200585 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
586 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100587
588 itv.it_interval.tv_sec = 0;
Paolo Bonzini84682832011-06-09 13:10:25 +0200589 itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */
590 itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
591 itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100592 err = setitimer(ITIMER_REAL, &itv, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200593 if (err) {
594 perror("setitimer");
595 fprintf(stderr, "Internal timer error: aborting\n");
596 exit(1);
597 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100598}
599
600static void unix_stop_timer(struct qemu_alarm_timer *t)
601{
602 struct itimerval itv;
603
604 memset(&itv, 0, sizeof(itv));
605 setitimer(ITIMER_REAL, &itv, NULL);
606}
607
608#endif /* !defined(_WIN32) */
609
610
611#ifdef _WIN32
612
Stefan Weil2f9cba02011-04-05 18:34:21 +0200613static MMRESULT mm_timer;
614static unsigned mm_period;
615
616static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
617 DWORD_PTR dwUser, DWORD_PTR dw1,
618 DWORD_PTR dw2)
619{
620 struct qemu_alarm_timer *t = alarm_timer;
621 if (!t) {
622 return;
623 }
Stefan Weil82051992012-04-20 11:27:24 +0200624 t->expired = true;
625 t->pending = true;
626 qemu_notify_event();
Stefan Weil2f9cba02011-04-05 18:34:21 +0200627}
628
629static int mm_start_timer(struct qemu_alarm_timer *t)
630{
631 TIMECAPS tc;
Stefan Weil2f9cba02011-04-05 18:34:21 +0200632
633 memset(&tc, 0, sizeof(tc));
634 timeGetDevCaps(&tc, sizeof(tc));
635
636 mm_period = tc.wPeriodMin;
637 timeBeginPeriod(mm_period);
638
Stefan Weil2f9cba02011-04-05 18:34:21 +0200639 mm_timer = timeSetEvent(1, /* interval (ms) */
640 mm_period, /* resolution */
641 mm_alarm_handler, /* function */
642 (DWORD_PTR)t, /* parameter */
Stefan Weil82051992012-04-20 11:27:24 +0200643 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200644
645 if (!mm_timer) {
646 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
647 GetLastError());
648 timeEndPeriod(mm_period);
649 return -1;
650 }
651
652 return 0;
653}
654
655static void mm_stop_timer(struct qemu_alarm_timer *t)
656{
657 timeKillEvent(mm_timer);
658 timeEndPeriod(mm_period);
659}
660
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100661static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta)
Stefan Weil2f9cba02011-04-05 18:34:21 +0200662{
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100663 int64_t nearest_delta_ms = delta / 1000000;
Stefan Weil2f9cba02011-04-05 18:34:21 +0200664 if (nearest_delta_ms < 1) {
665 nearest_delta_ms = 1;
666 }
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100667 /* UINT_MAX can be 32 bit */
668 if (nearest_delta_ms > UINT_MAX) {
669 nearest_delta_ms = UINT_MAX;
670 }
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100671
672 timeKillEvent(mm_timer);
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100673 mm_timer = timeSetEvent((unsigned int) nearest_delta_ms,
Stefan Weil2f9cba02011-04-05 18:34:21 +0200674 mm_period,
675 mm_alarm_handler,
676 (DWORD_PTR)t,
677 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
678
679 if (!mm_timer) {
680 fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
681 GetLastError());
682
683 timeEndPeriod(mm_period);
684 exit(1);
685 }
686}
687
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100688static int win32_start_timer(struct qemu_alarm_timer *t)
689{
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100690 HANDLE hTimer;
691 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100692
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100693 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
694 is zero) that has already expired, the timer is not updated. Since
695 creating a new timer is relatively expensive, set a bogus one-hour
696 interval in the dynticks case. */
697 success = CreateTimerQueueTimer(&hTimer,
698 NULL,
699 host_alarm_handler,
700 t,
701 1,
Stefan Weil82051992012-04-20 11:27:24 +0200702 3600000,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100703 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100704
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100705 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100706 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
707 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100708 return -1;
709 }
710
Stefan Weilcd0544e2011-04-10 20:15:09 +0200711 t->timer = hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100712 return 0;
713}
714
715static void win32_stop_timer(struct qemu_alarm_timer *t)
716{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200717 HANDLE hTimer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100718
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100719 if (hTimer) {
720 DeleteTimerQueueTimer(NULL, hTimer, NULL);
721 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100722}
723
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100724static void win32_rearm_timer(struct qemu_alarm_timer *t,
725 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100726{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200727 HANDLE hTimer = t->timer;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100728 int64_t nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100729 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100730
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100731 nearest_delta_ms = nearest_delta_ns / 1000000;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +0100732 if (nearest_delta_ms < 1) {
733 nearest_delta_ms = 1;
734 }
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100735 /* ULONG_MAX can be 32 bit */
736 if (nearest_delta_ms > ULONG_MAX) {
737 nearest_delta_ms = ULONG_MAX;
738 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100739 success = ChangeTimerQueueTimer(NULL,
740 hTimer,
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100741 (unsigned long) nearest_delta_ms,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100742 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100743
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100744 if (!success) {
745 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100746 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100747 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100748 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100749
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100750}
751
752#endif /* _WIN32 */
753
Paolo Bonzini4260a732011-09-19 10:18:51 +0200754static void quit_timers(void)
755{
756 struct qemu_alarm_timer *t = alarm_timer;
757 alarm_timer = NULL;
758 t->stop(t);
759}
760
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100761int init_timer_alarm(void)
762{
763 struct qemu_alarm_timer *t = NULL;
764 int i, err = -1;
765
766 for (i = 0; alarm_timers[i].name; i++) {
767 t = &alarm_timers[i];
768
769 err = t->start(t);
770 if (!err)
771 break;
772 }
773
774 if (err) {
775 err = -ENOENT;
776 goto fail;
777 }
778
779 /* first event is at time 0 */
Paolo Bonzini4260a732011-09-19 10:18:51 +0200780 atexit(quit_timers);
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200781 t->pending = true;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100782 alarm_timer = t;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100783
784 return 0;
785
786fail:
787 return err;
788}
789