blob: 25d5fdcc289774b5e4d7a95f1f717640b37530ec [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;
51 int 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
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010079 char expired;
80 char pending;
81};
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 Bonzinidb1a4972010-03-10 11:38:55 +010090int qemu_alarm_pending(void)
91{
92 return alarm_timer->pending;
93}
94
95static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
96{
97 return !!t->rearm;
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;
104
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +0100105 if (!use_icount && vm_clock->enabled && vm_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100106 delta = vm_clock->active_timers->expire_time -
107 qemu_get_clock_ns(vm_clock);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100108 }
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +0100109 if (host_clock->enabled && host_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100110 int64_t hdelta = host_clock->active_timers->expire_time -
111 qemu_get_clock_ns(host_clock);
112 if (hdelta < delta) {
113 delta = hdelta;
114 }
115 }
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +0100116 if (rt_clock->enabled && rt_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100117 rtdelta = (rt_clock->active_timers->expire_time -
118 qemu_get_clock_ns(rt_clock));
119 if (rtdelta < delta) {
120 delta = rtdelta;
121 }
122 }
123
124 return delta;
125}
126
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100127static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
128{
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100129 int64_t nearest_delta_ns;
130 assert(alarm_has_dynticks(t));
131 if (!rt_clock->active_timers &&
132 !vm_clock->active_timers &&
133 !host_clock->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100134 return;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100135 }
136 nearest_delta_ns = qemu_next_alarm_deadline();
137 t->rearm(t, nearest_delta_ns);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100138}
139
Paolo Bonzini9c132462011-02-03 14:48:59 +0100140/* TODO: MIN_TIMER_REARM_NS should be optimized */
141#define MIN_TIMER_REARM_NS 250000
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100142
143#ifdef _WIN32
144
Stefan Weil2f9cba02011-04-05 18:34:21 +0200145static int mm_start_timer(struct qemu_alarm_timer *t);
146static void mm_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100147static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200148
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100149static int win32_start_timer(struct qemu_alarm_timer *t);
150static void win32_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100151static void win32_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100152
153#else
154
155static int unix_start_timer(struct qemu_alarm_timer *t);
156static void unix_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100157static void unix_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100158
159#ifdef __linux__
160
161static int dynticks_start_timer(struct qemu_alarm_timer *t);
162static void dynticks_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100163static void dynticks_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100164
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100165#endif /* __linux__ */
166
167#endif /* _WIN32 */
168
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100169static struct qemu_alarm_timer alarm_timers[] = {
170#ifndef _WIN32
171#ifdef __linux__
172 {"dynticks", dynticks_start_timer,
Stefan Weilcd0544e2011-04-10 20:15:09 +0200173 dynticks_stop_timer, dynticks_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100174#endif
Paolo Bonzini84682832011-06-09 13:10:25 +0200175 {"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100176#else
Paolo Bonzinicca5de72011-11-09 12:46:56 +0100177 {"mmtimer", mm_start_timer, mm_stop_timer, mm_rearm_timer},
Stefan Weilcd0544e2011-04-10 20:15:09 +0200178 {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100179#endif
180 {NULL, }
181};
182
183static void show_available_alarms(void)
184{
185 int i;
186
187 printf("Available alarm timers, in order of precedence:\n");
188 for (i = 0; alarm_timers[i].name; i++)
189 printf("%s\n", alarm_timers[i].name);
190}
191
192void configure_alarms(char const *opt)
193{
194 int i;
195 int cur = 0;
196 int count = ARRAY_SIZE(alarm_timers) - 1;
197 char *arg;
198 char *name;
199 struct qemu_alarm_timer tmp;
200
201 if (!strcmp(opt, "?")) {
202 show_available_alarms();
203 exit(0);
204 }
205
Anthony Liguori7267c092011-08-20 22:09:37 -0500206 arg = g_strdup(opt);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100207
208 /* Reorder the array */
209 name = strtok(arg, ",");
210 while (name) {
211 for (i = 0; i < count && alarm_timers[i].name; i++) {
212 if (!strcmp(alarm_timers[i].name, name))
213 break;
214 }
215
216 if (i == count) {
217 fprintf(stderr, "Unknown clock %s\n", name);
218 goto next;
219 }
220
221 if (i < cur)
222 /* Ignore */
223 goto next;
224
225 /* Swap */
226 tmp = alarm_timers[i];
227 alarm_timers[i] = alarm_timers[cur];
228 alarm_timers[cur] = tmp;
229
230 cur++;
231next:
232 name = strtok(NULL, ",");
233 }
234
Anthony Liguori7267c092011-08-20 22:09:37 -0500235 g_free(arg);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100236
237 if (cur) {
238 /* Disable remaining timers */
239 for (i = cur; i < count; i++)
240 alarm_timers[i].name = NULL;
241 } else {
242 show_available_alarms();
243 exit(1);
244 }
245}
246
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100247QEMUClock *rt_clock;
248QEMUClock *vm_clock;
249QEMUClock *host_clock;
250
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100251static QEMUClock *qemu_new_clock(int type)
252{
253 QEMUClock *clock;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200254
Anthony Liguori7267c092011-08-20 22:09:37 -0500255 clock = g_malloc0(sizeof(QEMUClock));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100256 clock->type = type;
257 clock->enabled = 1;
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200258 clock->last = INT64_MIN;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200259 notifier_list_init(&clock->reset_notifiers);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100260 return clock;
261}
262
263void qemu_clock_enable(QEMUClock *clock, int enabled)
264{
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200265 bool old = clock->enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100266 clock->enabled = enabled;
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200267 if (enabled && !old) {
268 qemu_rearm_alarm_timer(alarm_timer);
269 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100270}
271
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200272int64_t qemu_clock_has_timers(QEMUClock *clock)
273{
274 return !!clock->active_timers;
275}
276
277int64_t qemu_clock_expired(QEMUClock *clock)
278{
279 return (clock->active_timers &&
280 clock->active_timers->expire_time < qemu_get_clock_ns(clock));
281}
282
283int64_t qemu_clock_deadline(QEMUClock *clock)
284{
285 /* To avoid problems with overflow limit this to 2^32. */
286 int64_t delta = INT32_MAX;
287
288 if (clock->active_timers) {
289 delta = clock->active_timers->expire_time - qemu_get_clock_ns(clock);
290 }
291 if (delta < 0) {
292 delta = 0;
293 }
294 return delta;
295}
296
Paolo Bonzini4a998742011-03-11 16:33:58 +0100297QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
298 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100299{
300 QEMUTimer *ts;
301
Anthony Liguori7267c092011-08-20 22:09:37 -0500302 ts = g_malloc0(sizeof(QEMUTimer));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100303 ts->clock = clock;
304 ts->cb = cb;
305 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100306 ts->scale = scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100307 return ts;
308}
309
310void qemu_free_timer(QEMUTimer *ts)
311{
Anthony Liguori7267c092011-08-20 22:09:37 -0500312 g_free(ts);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100313}
314
315/* stop a timer, but do not dealloc it */
316void qemu_del_timer(QEMUTimer *ts)
317{
318 QEMUTimer **pt, *t;
319
320 /* NOTE: this code must be signal safe because
321 qemu_timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200322 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100323 for(;;) {
324 t = *pt;
325 if (!t)
326 break;
327 if (t == ts) {
328 *pt = t->next;
329 break;
330 }
331 pt = &t->next;
332 }
333}
334
335/* modify the current timer so that it will be fired when current_time
336 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200337void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100338{
339 QEMUTimer **pt, *t;
340
341 qemu_del_timer(ts);
342
343 /* add the timer in the sorted list */
344 /* NOTE: this code must be signal safe because
345 qemu_timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200346 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100347 for(;;) {
348 t = *pt;
Stefan Weil45c7b372011-03-24 21:31:24 +0100349 if (!qemu_timer_expired_ns(t, expire_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100350 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100351 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100352 pt = &t->next;
353 }
354 ts->expire_time = expire_time;
355 ts->next = *pt;
356 *pt = ts;
357
358 /* Rearm if necessary */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200359 if (pt == &ts->clock->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100360 if (!alarm_timer->pending) {
361 qemu_rearm_alarm_timer(alarm_timer);
362 }
363 /* Interrupt execution to force deadline recalculation. */
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200364 qemu_clock_warp(ts->clock);
365 if (use_icount) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100366 qemu_notify_event();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200367 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100368 }
369}
370
Paolo Bonzini4a998742011-03-11 16:33:58 +0100371void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
372{
373 qemu_mod_timer_ns(ts, expire_time * ts->scale);
374}
375
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100376int qemu_timer_pending(QEMUTimer *ts)
377{
378 QEMUTimer *t;
Paolo Bonzini688eb382011-09-13 11:42:26 +0200379 for (t = ts->clock->active_timers; t != NULL; t = t->next) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100380 if (t == ts)
381 return 1;
382 }
383 return 0;
384}
385
386int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
387{
Stefan Weil45c7b372011-03-24 21:31:24 +0100388 return qemu_timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100389}
390
Paolo Bonzini8156be52012-03-28 15:42:04 +0200391void qemu_run_timers(QEMUClock *clock)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100392{
393 QEMUTimer **ptimer_head, *ts;
394 int64_t current_time;
395
396 if (!clock->enabled)
397 return;
398
Paolo Bonzini4a998742011-03-11 16:33:58 +0100399 current_time = qemu_get_clock_ns(clock);
Paolo Bonzini688eb382011-09-13 11:42:26 +0200400 ptimer_head = &clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100401 for(;;) {
402 ts = *ptimer_head;
Stefan Weil45c7b372011-03-24 21:31:24 +0100403 if (!qemu_timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100404 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100405 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100406 /* remove timer from the list before calling the callback */
407 *ptimer_head = ts->next;
408 ts->next = NULL;
409
410 /* run the callback (the timer list can be modified) */
411 ts->cb(ts->opaque);
412 }
413}
414
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100415int64_t qemu_get_clock_ns(QEMUClock *clock)
416{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200417 int64_t now, last;
418
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100419 switch(clock->type) {
420 case QEMU_CLOCK_REALTIME:
421 return get_clock();
422 default:
423 case QEMU_CLOCK_VIRTUAL:
424 if (use_icount) {
425 return cpu_get_icount();
426 } else {
427 return cpu_get_clock();
428 }
429 case QEMU_CLOCK_HOST:
Jan Kiszka691a0c92011-06-20 14:06:27 +0200430 now = get_clock_realtime();
431 last = clock->last;
432 clock->last = now;
433 if (now < last) {
434 notifier_list_notify(&clock->reset_notifiers, &now);
435 }
436 return now;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100437 }
438}
439
Jan Kiszka691a0c92011-06-20 14:06:27 +0200440void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
441{
442 notifier_list_add(&clock->reset_notifiers, notifier);
443}
444
445void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
446{
Paolo Bonzini31552522012-01-13 17:34:01 +0100447 notifier_remove(notifier);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200448}
449
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100450void init_clocks(void)
451{
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100452 rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
453 vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
454 host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100455}
456
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200457uint64_t qemu_timer_expire_time_ns(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100458{
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200459 return qemu_timer_pending(ts) ? ts->expire_time : -1;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100460}
461
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100462void qemu_run_all_timers(void)
463{
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100464 alarm_timer->pending = 0;
465
Peter Portante158fd3c2012-04-05 11:00:45 -0400466 /* vm time timers */
467 qemu_run_timers(vm_clock);
468 qemu_run_timers(rt_clock);
469 qemu_run_timers(host_clock);
470
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100471 /* rearm timer, if not periodic */
472 if (alarm_timer->expired) {
473 alarm_timer->expired = 0;
474 qemu_rearm_alarm_timer(alarm_timer);
475 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100476}
477
478#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100479static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100480#else
481static void host_alarm_handler(int host_signum)
482#endif
483{
484 struct qemu_alarm_timer *t = alarm_timer;
485 if (!t)
486 return;
487
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100488 if (alarm_has_dynticks(t) ||
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100489 qemu_next_alarm_deadline () <= 0) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100490 t->expired = alarm_has_dynticks(t);
491 t->pending = 1;
492 qemu_notify_event();
493 }
494}
495
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100496#if defined(__linux__)
497
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200498#include "compatfd.h"
499
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100500static int dynticks_start_timer(struct qemu_alarm_timer *t)
501{
502 struct sigevent ev;
503 timer_t host_timer;
504 struct sigaction act;
505
506 sigfillset(&act.sa_mask);
507 act.sa_flags = 0;
508 act.sa_handler = host_alarm_handler;
509
510 sigaction(SIGALRM, &act, NULL);
511
512 /*
513 * Initialize ev struct to 0 to avoid valgrind complaining
514 * about uninitialized data in timer_create call
515 */
516 memset(&ev, 0, sizeof(ev));
517 ev.sigev_value.sival_int = 0;
518 ev.sigev_notify = SIGEV_SIGNAL;
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200519#ifdef SIGEV_THREAD_ID
520 if (qemu_signalfd_available()) {
521 ev.sigev_notify = SIGEV_THREAD_ID;
522 ev._sigev_un._tid = qemu_get_thread_id();
523 }
524#endif /* SIGEV_THREAD_ID */
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100525 ev.sigev_signo = SIGALRM;
526
527 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
528 perror("timer_create");
529
530 /* disable dynticks */
531 fprintf(stderr, "Dynamic Ticks disabled\n");
532
533 return -1;
534 }
535
Stefan Weilcd0544e2011-04-10 20:15:09 +0200536 t->timer = host_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100537
538 return 0;
539}
540
541static void dynticks_stop_timer(struct qemu_alarm_timer *t)
542{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200543 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100544
545 timer_delete(host_timer);
546}
547
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100548static void dynticks_rearm_timer(struct qemu_alarm_timer *t,
549 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100550{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200551 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100552 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100553 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100554
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100555 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
556 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100557
558 /* check whether a timer is already running */
559 if (timer_gettime(host_timer, &timeout)) {
560 perror("gettime");
561 fprintf(stderr, "Internal timer error: aborting\n");
562 exit(1);
563 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100564 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
565 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100566 return;
567
568 timeout.it_interval.tv_sec = 0;
569 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100570 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
571 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100572 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
573 perror("settime");
574 fprintf(stderr, "Internal timer error: aborting\n");
575 exit(1);
576 }
577}
578
579#endif /* defined(__linux__) */
580
Stefan Weilf26e5a52011-02-04 22:01:32 +0100581#if !defined(_WIN32)
582
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100583static int unix_start_timer(struct qemu_alarm_timer *t)
584{
585 struct sigaction act;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100586
587 /* timer signal */
588 sigfillset(&act.sa_mask);
589 act.sa_flags = 0;
590 act.sa_handler = host_alarm_handler;
591
592 sigaction(SIGALRM, &act, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200593 return 0;
594}
595
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100596static void unix_rearm_timer(struct qemu_alarm_timer *t,
597 int64_t nearest_delta_ns)
Paolo Bonzini84682832011-06-09 13:10:25 +0200598{
599 struct itimerval itv;
Paolo Bonzini84682832011-06-09 13:10:25 +0200600 int err;
601
Paolo Bonzini84682832011-06-09 13:10:25 +0200602 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
603 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100604
605 itv.it_interval.tv_sec = 0;
Paolo Bonzini84682832011-06-09 13:10:25 +0200606 itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */
607 itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
608 itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100609 err = setitimer(ITIMER_REAL, &itv, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200610 if (err) {
611 perror("setitimer");
612 fprintf(stderr, "Internal timer error: aborting\n");
613 exit(1);
614 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100615}
616
617static void unix_stop_timer(struct qemu_alarm_timer *t)
618{
619 struct itimerval itv;
620
621 memset(&itv, 0, sizeof(itv));
622 setitimer(ITIMER_REAL, &itv, NULL);
623}
624
625#endif /* !defined(_WIN32) */
626
627
628#ifdef _WIN32
629
Stefan Weil2f9cba02011-04-05 18:34:21 +0200630static MMRESULT mm_timer;
631static unsigned mm_period;
632
633static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
634 DWORD_PTR dwUser, DWORD_PTR dw1,
635 DWORD_PTR dw2)
636{
637 struct qemu_alarm_timer *t = alarm_timer;
638 if (!t) {
639 return;
640 }
641 if (alarm_has_dynticks(t) || qemu_next_alarm_deadline() <= 0) {
642 t->expired = alarm_has_dynticks(t);
643 t->pending = 1;
644 qemu_notify_event();
645 }
646}
647
648static int mm_start_timer(struct qemu_alarm_timer *t)
649{
650 TIMECAPS tc;
651 UINT flags;
652
653 memset(&tc, 0, sizeof(tc));
654 timeGetDevCaps(&tc, sizeof(tc));
655
656 mm_period = tc.wPeriodMin;
657 timeBeginPeriod(mm_period);
658
659 flags = TIME_CALLBACK_FUNCTION;
660 if (alarm_has_dynticks(t)) {
661 flags |= TIME_ONESHOT;
662 } else {
663 flags |= TIME_PERIODIC;
664 }
665
666 mm_timer = timeSetEvent(1, /* interval (ms) */
667 mm_period, /* resolution */
668 mm_alarm_handler, /* function */
669 (DWORD_PTR)t, /* parameter */
670 flags);
671
672 if (!mm_timer) {
673 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
674 GetLastError());
675 timeEndPeriod(mm_period);
676 return -1;
677 }
678
679 return 0;
680}
681
682static void mm_stop_timer(struct qemu_alarm_timer *t)
683{
684 timeKillEvent(mm_timer);
685 timeEndPeriod(mm_period);
686}
687
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100688static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta)
Stefan Weil2f9cba02011-04-05 18:34:21 +0200689{
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100690 int64_t nearest_delta_ms = delta / 1000000;
Stefan Weil2f9cba02011-04-05 18:34:21 +0200691 if (nearest_delta_ms < 1) {
692 nearest_delta_ms = 1;
693 }
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100694 /* UINT_MAX can be 32 bit */
695 if (nearest_delta_ms > UINT_MAX) {
696 nearest_delta_ms = UINT_MAX;
697 }
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100698
699 timeKillEvent(mm_timer);
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100700 mm_timer = timeSetEvent((unsigned int) nearest_delta_ms,
Stefan Weil2f9cba02011-04-05 18:34:21 +0200701 mm_period,
702 mm_alarm_handler,
703 (DWORD_PTR)t,
704 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
705
706 if (!mm_timer) {
707 fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
708 GetLastError());
709
710 timeEndPeriod(mm_period);
711 exit(1);
712 }
713}
714
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100715static int win32_start_timer(struct qemu_alarm_timer *t)
716{
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100717 HANDLE hTimer;
718 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100719
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100720 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
721 is zero) that has already expired, the timer is not updated. Since
722 creating a new timer is relatively expensive, set a bogus one-hour
723 interval in the dynticks case. */
724 success = CreateTimerQueueTimer(&hTimer,
725 NULL,
726 host_alarm_handler,
727 t,
728 1,
729 alarm_has_dynticks(t) ? 3600000 : 1,
730 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100731
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100732 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100733 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
734 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100735 return -1;
736 }
737
Stefan Weilcd0544e2011-04-10 20:15:09 +0200738 t->timer = hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100739 return 0;
740}
741
742static void win32_stop_timer(struct qemu_alarm_timer *t)
743{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200744 HANDLE hTimer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100745
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100746 if (hTimer) {
747 DeleteTimerQueueTimer(NULL, hTimer, NULL);
748 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100749}
750
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100751static void win32_rearm_timer(struct qemu_alarm_timer *t,
752 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100753{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200754 HANDLE hTimer = t->timer;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100755 int64_t nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100756 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100757
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100758 nearest_delta_ms = nearest_delta_ns / 1000000;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +0100759 if (nearest_delta_ms < 1) {
760 nearest_delta_ms = 1;
761 }
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100762 /* ULONG_MAX can be 32 bit */
763 if (nearest_delta_ms > ULONG_MAX) {
764 nearest_delta_ms = ULONG_MAX;
765 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100766 success = ChangeTimerQueueTimer(NULL,
767 hTimer,
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100768 (unsigned long) nearest_delta_ms,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100769 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100770
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100771 if (!success) {
772 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100773 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100774 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100775 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100776
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100777}
778
779#endif /* _WIN32 */
780
Paolo Bonzini4260a732011-09-19 10:18:51 +0200781static void quit_timers(void)
782{
783 struct qemu_alarm_timer *t = alarm_timer;
784 alarm_timer = NULL;
785 t->stop(t);
786}
787
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100788int init_timer_alarm(void)
789{
790 struct qemu_alarm_timer *t = NULL;
791 int i, err = -1;
792
793 for (i = 0; alarm_timers[i].name; i++) {
794 t = &alarm_timers[i];
795
796 err = t->start(t);
797 if (!err)
798 break;
799 }
800
801 if (err) {
802 err = -ENOENT;
803 goto fail;
804 }
805
806 /* first event is at time 0 */
Paolo Bonzini4260a732011-09-19 10:18:51 +0200807 atexit(quit_timers);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100808 t->pending = 1;
809 alarm_timer = t;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100810
811 return 0;
812
813fail:
814 return err;
815}
816