blob: de9897788de59a78130874f1cceb26e3d1a0edd2 [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
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010034#ifdef _WIN32
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010035#include <mmsystem.h>
36#endif
37
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010038/***********************************************************/
39/* timers */
40
41#define QEMU_CLOCK_REALTIME 0
42#define QEMU_CLOCK_VIRTUAL 1
43#define QEMU_CLOCK_HOST 2
44
45struct QEMUClock {
Paolo Bonzini688eb382011-09-13 11:42:26 +020046 QEMUTimer *active_timers;
Jan Kiszka691a0c92011-06-20 14:06:27 +020047
48 NotifierList reset_notifiers;
49 int64_t last;
Stefan Weil9a14b292012-04-20 11:51:58 +020050
51 int type;
52 bool enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010053};
54
55struct QEMUTimer {
Paolo Bonzini4a998742011-03-11 16:33:58 +010056 int64_t expire_time; /* in nanoseconds */
Stefan Weil9a14b292012-04-20 11:51:58 +020057 QEMUClock *clock;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010058 QEMUTimerCB *cb;
59 void *opaque;
Stefan Weil9a14b292012-04-20 11:51:58 +020060 QEMUTimer *next;
61 int scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010062};
63
64struct qemu_alarm_timer {
65 char const *name;
66 int (*start)(struct qemu_alarm_timer *t);
67 void (*stop)(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010068 void (*rearm)(struct qemu_alarm_timer *t, int64_t nearest_delta_ns);
Stefan Weilcd0544e2011-04-10 20:15:09 +020069#if defined(__linux__)
Stefan Weilcd0544e2011-04-10 20:15:09 +020070 timer_t timer;
Stefan Weil9a14b292012-04-20 11:51:58 +020071 int fd;
Stefan Weilcd0544e2011-04-10 20:15:09 +020072#elif defined(_WIN32)
73 HANDLE timer;
74#endif
Stefan Weil5e1ec7b2012-04-20 10:45:48 +020075 bool expired;
76 bool pending;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010077};
78
79static struct qemu_alarm_timer *alarm_timer;
80
Stefan Weil45c7b372011-03-24 21:31:24 +010081static bool qemu_timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
82{
83 return timer_head && (timer_head->expire_time <= current_time);
84}
85
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010086static int64_t qemu_next_alarm_deadline(void)
87{
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +010088 int64_t delta = INT64_MAX;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010089 int64_t rtdelta;
90
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +010091 if (!use_icount && vm_clock->enabled && vm_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010092 delta = vm_clock->active_timers->expire_time -
93 qemu_get_clock_ns(vm_clock);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010094 }
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +010095 if (host_clock->enabled && host_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010096 int64_t hdelta = host_clock->active_timers->expire_time -
97 qemu_get_clock_ns(host_clock);
98 if (hdelta < delta) {
99 delta = hdelta;
100 }
101 }
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +0100102 if (rt_clock->enabled && rt_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100103 rtdelta = (rt_clock->active_timers->expire_time -
104 qemu_get_clock_ns(rt_clock));
105 if (rtdelta < delta) {
106 delta = rtdelta;
107 }
108 }
109
110 return delta;
111}
112
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100113static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
114{
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100115 int64_t nearest_delta_ns;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100116 if (!rt_clock->active_timers &&
117 !vm_clock->active_timers &&
118 !host_clock->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100119 return;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100120 }
121 nearest_delta_ns = qemu_next_alarm_deadline();
122 t->rearm(t, nearest_delta_ns);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100123}
124
Paolo Bonzini9c132462011-02-03 14:48:59 +0100125/* TODO: MIN_TIMER_REARM_NS should be optimized */
126#define MIN_TIMER_REARM_NS 250000
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100127
128#ifdef _WIN32
129
Stefan Weil2f9cba02011-04-05 18:34:21 +0200130static int mm_start_timer(struct qemu_alarm_timer *t);
131static void mm_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100132static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200133
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100134static int win32_start_timer(struct qemu_alarm_timer *t);
135static void win32_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100136static void win32_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100137
138#else
139
140static int unix_start_timer(struct qemu_alarm_timer *t);
141static void unix_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100142static void unix_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100143
144#ifdef __linux__
145
146static int dynticks_start_timer(struct qemu_alarm_timer *t);
147static void dynticks_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100148static void dynticks_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100149
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100150#endif /* __linux__ */
151
152#endif /* _WIN32 */
153
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100154static struct qemu_alarm_timer alarm_timers[] = {
155#ifndef _WIN32
156#ifdef __linux__
157 {"dynticks", dynticks_start_timer,
Stefan Weilcd0544e2011-04-10 20:15:09 +0200158 dynticks_stop_timer, dynticks_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100159#endif
Paolo Bonzini84682832011-06-09 13:10:25 +0200160 {"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100161#else
Paolo Bonzinicca5de72011-11-09 12:46:56 +0100162 {"mmtimer", mm_start_timer, mm_stop_timer, mm_rearm_timer},
Stefan Weilcd0544e2011-04-10 20:15:09 +0200163 {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100164#endif
165 {NULL, }
166};
167
168static void show_available_alarms(void)
169{
170 int i;
171
172 printf("Available alarm timers, in order of precedence:\n");
173 for (i = 0; alarm_timers[i].name; i++)
174 printf("%s\n", alarm_timers[i].name);
175}
176
177void configure_alarms(char const *opt)
178{
179 int i;
180 int cur = 0;
181 int count = ARRAY_SIZE(alarm_timers) - 1;
182 char *arg;
183 char *name;
184 struct qemu_alarm_timer tmp;
185
186 if (!strcmp(opt, "?")) {
187 show_available_alarms();
188 exit(0);
189 }
190
Anthony Liguori7267c092011-08-20 22:09:37 -0500191 arg = g_strdup(opt);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100192
193 /* Reorder the array */
194 name = strtok(arg, ",");
195 while (name) {
196 for (i = 0; i < count && alarm_timers[i].name; i++) {
197 if (!strcmp(alarm_timers[i].name, name))
198 break;
199 }
200
201 if (i == count) {
202 fprintf(stderr, "Unknown clock %s\n", name);
203 goto next;
204 }
205
206 if (i < cur)
207 /* Ignore */
208 goto next;
209
210 /* Swap */
211 tmp = alarm_timers[i];
212 alarm_timers[i] = alarm_timers[cur];
213 alarm_timers[cur] = tmp;
214
215 cur++;
216next:
217 name = strtok(NULL, ",");
218 }
219
Anthony Liguori7267c092011-08-20 22:09:37 -0500220 g_free(arg);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100221
222 if (cur) {
223 /* Disable remaining timers */
224 for (i = cur; i < count; i++)
225 alarm_timers[i].name = NULL;
226 } else {
227 show_available_alarms();
228 exit(1);
229 }
230}
231
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100232QEMUClock *rt_clock;
233QEMUClock *vm_clock;
234QEMUClock *host_clock;
235
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100236static QEMUClock *qemu_new_clock(int type)
237{
238 QEMUClock *clock;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200239
Anthony Liguori7267c092011-08-20 22:09:37 -0500240 clock = g_malloc0(sizeof(QEMUClock));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100241 clock->type = type;
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200242 clock->enabled = true;
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200243 clock->last = INT64_MIN;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200244 notifier_list_init(&clock->reset_notifiers);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100245 return clock;
246}
247
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200248void qemu_clock_enable(QEMUClock *clock, bool enabled)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100249{
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200250 bool old = clock->enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100251 clock->enabled = enabled;
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200252 if (enabled && !old) {
253 qemu_rearm_alarm_timer(alarm_timer);
254 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100255}
256
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200257int64_t qemu_clock_has_timers(QEMUClock *clock)
258{
259 return !!clock->active_timers;
260}
261
262int64_t qemu_clock_expired(QEMUClock *clock)
263{
264 return (clock->active_timers &&
265 clock->active_timers->expire_time < qemu_get_clock_ns(clock));
266}
267
268int64_t qemu_clock_deadline(QEMUClock *clock)
269{
270 /* To avoid problems with overflow limit this to 2^32. */
271 int64_t delta = INT32_MAX;
272
273 if (clock->active_timers) {
274 delta = clock->active_timers->expire_time - qemu_get_clock_ns(clock);
275 }
276 if (delta < 0) {
277 delta = 0;
278 }
279 return delta;
280}
281
Paolo Bonzini4a998742011-03-11 16:33:58 +0100282QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
283 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100284{
285 QEMUTimer *ts;
286
Anthony Liguori7267c092011-08-20 22:09:37 -0500287 ts = g_malloc0(sizeof(QEMUTimer));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100288 ts->clock = clock;
289 ts->cb = cb;
290 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100291 ts->scale = scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100292 return ts;
293}
294
295void qemu_free_timer(QEMUTimer *ts)
296{
Anthony Liguori7267c092011-08-20 22:09:37 -0500297 g_free(ts);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100298}
299
300/* stop a timer, but do not dealloc it */
301void qemu_del_timer(QEMUTimer *ts)
302{
303 QEMUTimer **pt, *t;
304
305 /* NOTE: this code must be signal safe because
306 qemu_timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200307 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100308 for(;;) {
309 t = *pt;
310 if (!t)
311 break;
312 if (t == ts) {
313 *pt = t->next;
314 break;
315 }
316 pt = &t->next;
317 }
318}
319
320/* modify the current timer so that it will be fired when current_time
321 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200322void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100323{
324 QEMUTimer **pt, *t;
325
326 qemu_del_timer(ts);
327
328 /* add the timer in the sorted list */
329 /* NOTE: this code must be signal safe because
330 qemu_timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200331 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100332 for(;;) {
333 t = *pt;
Stefan Weil45c7b372011-03-24 21:31:24 +0100334 if (!qemu_timer_expired_ns(t, expire_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100335 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100336 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100337 pt = &t->next;
338 }
339 ts->expire_time = expire_time;
340 ts->next = *pt;
341 *pt = ts;
342
343 /* Rearm if necessary */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200344 if (pt == &ts->clock->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100345 if (!alarm_timer->pending) {
346 qemu_rearm_alarm_timer(alarm_timer);
347 }
348 /* Interrupt execution to force deadline recalculation. */
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200349 qemu_clock_warp(ts->clock);
350 if (use_icount) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100351 qemu_notify_event();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200352 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100353 }
354}
355
Paolo Bonzini4a998742011-03-11 16:33:58 +0100356void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
357{
358 qemu_mod_timer_ns(ts, expire_time * ts->scale);
359}
360
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200361bool qemu_timer_pending(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100362{
363 QEMUTimer *t;
Paolo Bonzini688eb382011-09-13 11:42:26 +0200364 for (t = ts->clock->active_timers; t != NULL; t = t->next) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200365 if (t == ts) {
366 return true;
367 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100368 }
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200369 return false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100370}
371
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200372bool qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100373{
Stefan Weil45c7b372011-03-24 21:31:24 +0100374 return qemu_timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100375}
376
Paolo Bonzini8156be52012-03-28 15:42:04 +0200377void qemu_run_timers(QEMUClock *clock)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100378{
379 QEMUTimer **ptimer_head, *ts;
380 int64_t current_time;
381
382 if (!clock->enabled)
383 return;
384
Paolo Bonzini4a998742011-03-11 16:33:58 +0100385 current_time = qemu_get_clock_ns(clock);
Paolo Bonzini688eb382011-09-13 11:42:26 +0200386 ptimer_head = &clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100387 for(;;) {
388 ts = *ptimer_head;
Stefan Weil45c7b372011-03-24 21:31:24 +0100389 if (!qemu_timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100390 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100391 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100392 /* remove timer from the list before calling the callback */
393 *ptimer_head = ts->next;
394 ts->next = NULL;
395
396 /* run the callback (the timer list can be modified) */
397 ts->cb(ts->opaque);
398 }
399}
400
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100401int64_t qemu_get_clock_ns(QEMUClock *clock)
402{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200403 int64_t now, last;
404
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100405 switch(clock->type) {
406 case QEMU_CLOCK_REALTIME:
407 return get_clock();
408 default:
409 case QEMU_CLOCK_VIRTUAL:
410 if (use_icount) {
411 return cpu_get_icount();
412 } else {
413 return cpu_get_clock();
414 }
415 case QEMU_CLOCK_HOST:
Jan Kiszka691a0c92011-06-20 14:06:27 +0200416 now = get_clock_realtime();
417 last = clock->last;
418 clock->last = now;
419 if (now < last) {
420 notifier_list_notify(&clock->reset_notifiers, &now);
421 }
422 return now;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100423 }
424}
425
Jan Kiszka691a0c92011-06-20 14:06:27 +0200426void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
427{
428 notifier_list_add(&clock->reset_notifiers, notifier);
429}
430
431void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
432{
Paolo Bonzini31552522012-01-13 17:34:01 +0100433 notifier_remove(notifier);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200434}
435
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100436void init_clocks(void)
437{
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100438 rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
439 vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
440 host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100441}
442
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200443uint64_t qemu_timer_expire_time_ns(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100444{
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200445 return qemu_timer_pending(ts) ? ts->expire_time : -1;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100446}
447
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100448void qemu_run_all_timers(void)
449{
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200450 alarm_timer->pending = false;
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100451
Peter Portante158fd3c2012-04-05 11:00:45 -0400452 /* vm time timers */
453 qemu_run_timers(vm_clock);
454 qemu_run_timers(rt_clock);
455 qemu_run_timers(host_clock);
456
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100457 /* rearm timer, if not periodic */
458 if (alarm_timer->expired) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200459 alarm_timer->expired = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100460 qemu_rearm_alarm_timer(alarm_timer);
461 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100462}
463
464#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100465static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100466#else
467static void host_alarm_handler(int host_signum)
468#endif
469{
470 struct qemu_alarm_timer *t = alarm_timer;
471 if (!t)
472 return;
473
Stefan Weil82051992012-04-20 11:27:24 +0200474 t->expired = true;
475 t->pending = true;
476 qemu_notify_event();
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100477}
478
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100479#if defined(__linux__)
480
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200481#include "compatfd.h"
482
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100483static int dynticks_start_timer(struct qemu_alarm_timer *t)
484{
485 struct sigevent ev;
486 timer_t host_timer;
487 struct sigaction act;
488
489 sigfillset(&act.sa_mask);
490 act.sa_flags = 0;
491 act.sa_handler = host_alarm_handler;
492
493 sigaction(SIGALRM, &act, NULL);
494
495 /*
496 * Initialize ev struct to 0 to avoid valgrind complaining
497 * about uninitialized data in timer_create call
498 */
499 memset(&ev, 0, sizeof(ev));
500 ev.sigev_value.sival_int = 0;
501 ev.sigev_notify = SIGEV_SIGNAL;
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200502#ifdef SIGEV_THREAD_ID
503 if (qemu_signalfd_available()) {
504 ev.sigev_notify = SIGEV_THREAD_ID;
505 ev._sigev_un._tid = qemu_get_thread_id();
506 }
507#endif /* SIGEV_THREAD_ID */
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100508 ev.sigev_signo = SIGALRM;
509
510 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
511 perror("timer_create");
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100512 return -1;
513 }
514
Stefan Weilcd0544e2011-04-10 20:15:09 +0200515 t->timer = host_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100516
517 return 0;
518}
519
520static void dynticks_stop_timer(struct qemu_alarm_timer *t)
521{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200522 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100523
524 timer_delete(host_timer);
525}
526
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100527static void dynticks_rearm_timer(struct qemu_alarm_timer *t,
528 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100529{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200530 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100531 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100532 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100533
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100534 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
535 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100536
537 /* check whether a timer is already running */
538 if (timer_gettime(host_timer, &timeout)) {
539 perror("gettime");
540 fprintf(stderr, "Internal timer error: aborting\n");
541 exit(1);
542 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100543 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
544 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100545 return;
546
547 timeout.it_interval.tv_sec = 0;
548 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100549 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
550 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100551 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
552 perror("settime");
553 fprintf(stderr, "Internal timer error: aborting\n");
554 exit(1);
555 }
556}
557
558#endif /* defined(__linux__) */
559
Stefan Weilf26e5a52011-02-04 22:01:32 +0100560#if !defined(_WIN32)
561
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100562static int unix_start_timer(struct qemu_alarm_timer *t)
563{
564 struct sigaction act;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100565
566 /* timer signal */
567 sigfillset(&act.sa_mask);
568 act.sa_flags = 0;
569 act.sa_handler = host_alarm_handler;
570
571 sigaction(SIGALRM, &act, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200572 return 0;
573}
574
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100575static void unix_rearm_timer(struct qemu_alarm_timer *t,
576 int64_t nearest_delta_ns)
Paolo Bonzini84682832011-06-09 13:10:25 +0200577{
578 struct itimerval itv;
Paolo Bonzini84682832011-06-09 13:10:25 +0200579 int err;
580
Paolo Bonzini84682832011-06-09 13:10:25 +0200581 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
582 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100583
584 itv.it_interval.tv_sec = 0;
Paolo Bonzini84682832011-06-09 13:10:25 +0200585 itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */
586 itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
587 itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100588 err = setitimer(ITIMER_REAL, &itv, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200589 if (err) {
590 perror("setitimer");
591 fprintf(stderr, "Internal timer error: aborting\n");
592 exit(1);
593 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100594}
595
596static void unix_stop_timer(struct qemu_alarm_timer *t)
597{
598 struct itimerval itv;
599
600 memset(&itv, 0, sizeof(itv));
601 setitimer(ITIMER_REAL, &itv, NULL);
602}
603
604#endif /* !defined(_WIN32) */
605
606
607#ifdef _WIN32
608
Stefan Weil2f9cba02011-04-05 18:34:21 +0200609static MMRESULT mm_timer;
Stefan Weil40f08e82012-04-27 05:34:40 +0000610static TIMECAPS mm_tc;
Stefan Weil2f9cba02011-04-05 18:34:21 +0200611
612static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
613 DWORD_PTR dwUser, DWORD_PTR dw1,
614 DWORD_PTR dw2)
615{
616 struct qemu_alarm_timer *t = alarm_timer;
617 if (!t) {
618 return;
619 }
Stefan Weil82051992012-04-20 11:27:24 +0200620 t->expired = true;
621 t->pending = true;
622 qemu_notify_event();
Stefan Weil2f9cba02011-04-05 18:34:21 +0200623}
624
625static int mm_start_timer(struct qemu_alarm_timer *t)
626{
Stefan Weil40f08e82012-04-27 05:34:40 +0000627 timeGetDevCaps(&mm_tc, sizeof(mm_tc));
Stefan Weil2f9cba02011-04-05 18:34:21 +0200628
Stefan Weil40f08e82012-04-27 05:34:40 +0000629 timeBeginPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200630
Stefan Weil40f08e82012-04-27 05:34:40 +0000631 mm_timer = timeSetEvent(mm_tc.wPeriodMin, /* interval (ms) */
632 mm_tc.wPeriodMin, /* resolution */
Stefan Weil2f9cba02011-04-05 18:34:21 +0200633 mm_alarm_handler, /* function */
634 (DWORD_PTR)t, /* parameter */
Stefan Weil82051992012-04-20 11:27:24 +0200635 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200636
637 if (!mm_timer) {
Stefan Weil52ef6512012-05-08 19:14:43 +0200638 fprintf(stderr, "Failed to initialize win32 alarm timer\n");
Stefan Weil40f08e82012-04-27 05:34:40 +0000639 timeEndPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200640 return -1;
641 }
642
643 return 0;
644}
645
646static void mm_stop_timer(struct qemu_alarm_timer *t)
647{
648 timeKillEvent(mm_timer);
Stefan Weil40f08e82012-04-27 05:34:40 +0000649 timeEndPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200650}
651
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100652static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta)
Stefan Weil2f9cba02011-04-05 18:34:21 +0200653{
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100654 int64_t nearest_delta_ms = delta / 1000000;
Stefan Weil40f08e82012-04-27 05:34:40 +0000655 if (nearest_delta_ms < mm_tc.wPeriodMin) {
656 nearest_delta_ms = mm_tc.wPeriodMin;
657 } else if (nearest_delta_ms > mm_tc.wPeriodMax) {
658 nearest_delta_ms = mm_tc.wPeriodMax;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100659 }
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100660
661 timeKillEvent(mm_timer);
Stefan Weil40f08e82012-04-27 05:34:40 +0000662 mm_timer = timeSetEvent((UINT)nearest_delta_ms,
663 mm_tc.wPeriodMin,
Stefan Weil2f9cba02011-04-05 18:34:21 +0200664 mm_alarm_handler,
665 (DWORD_PTR)t,
666 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
667
668 if (!mm_timer) {
Stefan Weil52ef6512012-05-08 19:14:43 +0200669 fprintf(stderr, "Failed to re-arm win32 alarm timer\n");
Stefan Weil40f08e82012-04-27 05:34:40 +0000670 timeEndPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200671 exit(1);
672 }
673}
674
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100675static int win32_start_timer(struct qemu_alarm_timer *t)
676{
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100677 HANDLE hTimer;
678 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100679
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100680 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
681 is zero) that has already expired, the timer is not updated. Since
682 creating a new timer is relatively expensive, set a bogus one-hour
683 interval in the dynticks case. */
684 success = CreateTimerQueueTimer(&hTimer,
685 NULL,
686 host_alarm_handler,
687 t,
688 1,
Stefan Weil82051992012-04-20 11:27:24 +0200689 3600000,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100690 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100691
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100692 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100693 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
694 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100695 return -1;
696 }
697
Stefan Weilcd0544e2011-04-10 20:15:09 +0200698 t->timer = hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100699 return 0;
700}
701
702static void win32_stop_timer(struct qemu_alarm_timer *t)
703{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200704 HANDLE hTimer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100705
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100706 if (hTimer) {
707 DeleteTimerQueueTimer(NULL, hTimer, NULL);
708 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100709}
710
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100711static void win32_rearm_timer(struct qemu_alarm_timer *t,
712 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100713{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200714 HANDLE hTimer = t->timer;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100715 int64_t nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100716 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100717
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100718 nearest_delta_ms = nearest_delta_ns / 1000000;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +0100719 if (nearest_delta_ms < 1) {
720 nearest_delta_ms = 1;
721 }
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100722 /* ULONG_MAX can be 32 bit */
723 if (nearest_delta_ms > ULONG_MAX) {
724 nearest_delta_ms = ULONG_MAX;
725 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100726 success = ChangeTimerQueueTimer(NULL,
727 hTimer,
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100728 (unsigned long) nearest_delta_ms,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100729 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100730
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100731 if (!success) {
732 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100733 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100734 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100735 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100736
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100737}
738
739#endif /* _WIN32 */
740
Paolo Bonzini4260a732011-09-19 10:18:51 +0200741static void quit_timers(void)
742{
743 struct qemu_alarm_timer *t = alarm_timer;
744 alarm_timer = NULL;
745 t->stop(t);
746}
747
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100748int init_timer_alarm(void)
749{
750 struct qemu_alarm_timer *t = NULL;
751 int i, err = -1;
752
753 for (i = 0; alarm_timers[i].name; i++) {
754 t = &alarm_timers[i];
755
756 err = t->start(t);
757 if (!err)
758 break;
759 }
760
761 if (err) {
762 err = -ENOENT;
763 goto fail;
764 }
765
766 /* first event is at time 0 */
Paolo Bonzini4260a732011-09-19 10:18:51 +0200767 atexit(quit_timers);
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200768 t->pending = true;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100769 alarm_timer = t;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100770
771 return 0;
772
773fail:
774 return err;
775}
776