blob: 8fb5c75df73b8838b58995222b008a11356237d6 [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
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010040/***********************************************************/
41/* timers */
42
43#define QEMU_CLOCK_REALTIME 0
44#define QEMU_CLOCK_VIRTUAL 1
45#define QEMU_CLOCK_HOST 2
46
47struct QEMUClock {
Paolo Bonzini688eb382011-09-13 11:42:26 +020048 QEMUTimer *active_timers;
Jan Kiszka691a0c92011-06-20 14:06:27 +020049
50 NotifierList reset_notifiers;
51 int64_t last;
Stefan Weil9a14b292012-04-20 11:51:58 +020052
53 int type;
54 bool enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010055};
56
57struct QEMUTimer {
Paolo Bonzini4a998742011-03-11 16:33:58 +010058 int64_t expire_time; /* in nanoseconds */
Stefan Weil9a14b292012-04-20 11:51:58 +020059 QEMUClock *clock;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010060 QEMUTimerCB *cb;
61 void *opaque;
Stefan Weil9a14b292012-04-20 11:51:58 +020062 QEMUTimer *next;
63 int scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010064};
65
66struct qemu_alarm_timer {
67 char const *name;
68 int (*start)(struct qemu_alarm_timer *t);
69 void (*stop)(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010070 void (*rearm)(struct qemu_alarm_timer *t, int64_t nearest_delta_ns);
Stefan Weilcd0544e2011-04-10 20:15:09 +020071#if defined(__linux__)
Stefan Weilcd0544e2011-04-10 20:15:09 +020072 timer_t timer;
Stefan Weil9a14b292012-04-20 11:51:58 +020073 int fd;
Stefan Weilcd0544e2011-04-10 20:15:09 +020074#elif defined(_WIN32)
75 HANDLE timer;
76#endif
Stefan Weil5e1ec7b2012-04-20 10:45:48 +020077 bool expired;
78 bool pending;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010079};
80
81static struct qemu_alarm_timer *alarm_timer;
82
Stefan Weil45c7b372011-03-24 21:31:24 +010083static bool qemu_timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
84{
85 return timer_head && (timer_head->expire_time <= current_time);
86}
87
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010088static int64_t qemu_next_alarm_deadline(void)
89{
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +010090 int64_t delta = INT64_MAX;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010091 int64_t rtdelta;
92
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +010093 if (!use_icount && vm_clock->enabled && vm_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010094 delta = vm_clock->active_timers->expire_time -
95 qemu_get_clock_ns(vm_clock);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010096 }
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +010097 if (host_clock->enabled && host_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010098 int64_t hdelta = host_clock->active_timers->expire_time -
99 qemu_get_clock_ns(host_clock);
100 if (hdelta < delta) {
101 delta = hdelta;
102 }
103 }
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +0100104 if (rt_clock->enabled && rt_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100105 rtdelta = (rt_clock->active_timers->expire_time -
106 qemu_get_clock_ns(rt_clock));
107 if (rtdelta < delta) {
108 delta = rtdelta;
109 }
110 }
111
112 return delta;
113}
114
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100115static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
116{
Stefano Stabellini82274212012-05-29 03:35:24 +0000117 int64_t nearest_delta_ns = qemu_next_alarm_deadline();
118 if (nearest_delta_ns < INT64_MAX) {
119 t->rearm(t, nearest_delta_ns);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100120 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100121}
122
Paolo Bonzini9c132462011-02-03 14:48:59 +0100123/* TODO: MIN_TIMER_REARM_NS should be optimized */
124#define MIN_TIMER_REARM_NS 250000
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100125
126#ifdef _WIN32
127
Stefan Weil2f9cba02011-04-05 18:34:21 +0200128static int mm_start_timer(struct qemu_alarm_timer *t);
129static void mm_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100130static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200131
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100132static int win32_start_timer(struct qemu_alarm_timer *t);
133static void win32_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100134static void win32_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100135
136#else
137
138static int unix_start_timer(struct qemu_alarm_timer *t);
139static void unix_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100140static void unix_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100141
142#ifdef __linux__
143
144static int dynticks_start_timer(struct qemu_alarm_timer *t);
145static void dynticks_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100146static void dynticks_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100147
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100148#endif /* __linux__ */
149
150#endif /* _WIN32 */
151
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100152static struct qemu_alarm_timer alarm_timers[] = {
153#ifndef _WIN32
154#ifdef __linux__
155 {"dynticks", dynticks_start_timer,
Stefan Weilcd0544e2011-04-10 20:15:09 +0200156 dynticks_stop_timer, dynticks_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100157#endif
Paolo Bonzini84682832011-06-09 13:10:25 +0200158 {"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100159#else
Paolo Bonzinicca5de72011-11-09 12:46:56 +0100160 {"mmtimer", mm_start_timer, mm_stop_timer, mm_rearm_timer},
Stefan Weilcd0544e2011-04-10 20:15:09 +0200161 {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100162#endif
163 {NULL, }
164};
165
166static void show_available_alarms(void)
167{
168 int i;
169
170 printf("Available alarm timers, in order of precedence:\n");
171 for (i = 0; alarm_timers[i].name; i++)
172 printf("%s\n", alarm_timers[i].name);
173}
174
175void configure_alarms(char const *opt)
176{
177 int i;
178 int cur = 0;
179 int count = ARRAY_SIZE(alarm_timers) - 1;
180 char *arg;
181 char *name;
182 struct qemu_alarm_timer tmp;
183
Peter Maydellc8057f92012-08-02 13:45:54 +0100184 if (is_help_option(opt)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100185 show_available_alarms();
186 exit(0);
187 }
188
Anthony Liguori7267c092011-08-20 22:09:37 -0500189 arg = g_strdup(opt);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100190
191 /* Reorder the array */
192 name = strtok(arg, ",");
193 while (name) {
194 for (i = 0; i < count && alarm_timers[i].name; i++) {
195 if (!strcmp(alarm_timers[i].name, name))
196 break;
197 }
198
199 if (i == count) {
200 fprintf(stderr, "Unknown clock %s\n", name);
201 goto next;
202 }
203
204 if (i < cur)
205 /* Ignore */
206 goto next;
207
208 /* Swap */
209 tmp = alarm_timers[i];
210 alarm_timers[i] = alarm_timers[cur];
211 alarm_timers[cur] = tmp;
212
213 cur++;
214next:
215 name = strtok(NULL, ",");
216 }
217
Anthony Liguori7267c092011-08-20 22:09:37 -0500218 g_free(arg);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100219
220 if (cur) {
221 /* Disable remaining timers */
222 for (i = cur; i < count; i++)
223 alarm_timers[i].name = NULL;
224 } else {
225 show_available_alarms();
226 exit(1);
227 }
228}
229
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100230QEMUClock *rt_clock;
231QEMUClock *vm_clock;
232QEMUClock *host_clock;
233
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100234static QEMUClock *qemu_new_clock(int type)
235{
236 QEMUClock *clock;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200237
Anthony Liguori7267c092011-08-20 22:09:37 -0500238 clock = g_malloc0(sizeof(QEMUClock));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100239 clock->type = type;
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200240 clock->enabled = true;
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200241 clock->last = INT64_MIN;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200242 notifier_list_init(&clock->reset_notifiers);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100243 return clock;
244}
245
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200246void qemu_clock_enable(QEMUClock *clock, bool enabled)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100247{
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200248 bool old = clock->enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100249 clock->enabled = enabled;
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200250 if (enabled && !old) {
251 qemu_rearm_alarm_timer(alarm_timer);
252 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100253}
254
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200255int64_t qemu_clock_has_timers(QEMUClock *clock)
256{
257 return !!clock->active_timers;
258}
259
260int64_t qemu_clock_expired(QEMUClock *clock)
261{
262 return (clock->active_timers &&
263 clock->active_timers->expire_time < qemu_get_clock_ns(clock));
264}
265
266int64_t qemu_clock_deadline(QEMUClock *clock)
267{
268 /* To avoid problems with overflow limit this to 2^32. */
269 int64_t delta = INT32_MAX;
270
271 if (clock->active_timers) {
272 delta = clock->active_timers->expire_time - qemu_get_clock_ns(clock);
273 }
274 if (delta < 0) {
275 delta = 0;
276 }
277 return delta;
278}
279
Paolo Bonzini4a998742011-03-11 16:33:58 +0100280QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
281 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100282{
283 QEMUTimer *ts;
284
Anthony Liguori7267c092011-08-20 22:09:37 -0500285 ts = g_malloc0(sizeof(QEMUTimer));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100286 ts->clock = clock;
287 ts->cb = cb;
288 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100289 ts->scale = scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100290 return ts;
291}
292
293void qemu_free_timer(QEMUTimer *ts)
294{
Anthony Liguori7267c092011-08-20 22:09:37 -0500295 g_free(ts);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100296}
297
298/* stop a timer, but do not dealloc it */
299void qemu_del_timer(QEMUTimer *ts)
300{
301 QEMUTimer **pt, *t;
302
303 /* NOTE: this code must be signal safe because
304 qemu_timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200305 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100306 for(;;) {
307 t = *pt;
308 if (!t)
309 break;
310 if (t == ts) {
311 *pt = t->next;
312 break;
313 }
314 pt = &t->next;
315 }
316}
317
318/* modify the current timer so that it will be fired when current_time
319 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200320void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100321{
322 QEMUTimer **pt, *t;
323
324 qemu_del_timer(ts);
325
326 /* add the timer in the sorted list */
327 /* NOTE: this code must be signal safe because
328 qemu_timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200329 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100330 for(;;) {
331 t = *pt;
Stefan Weil45c7b372011-03-24 21:31:24 +0100332 if (!qemu_timer_expired_ns(t, expire_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100333 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100334 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100335 pt = &t->next;
336 }
337 ts->expire_time = expire_time;
338 ts->next = *pt;
339 *pt = ts;
340
341 /* Rearm if necessary */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200342 if (pt == &ts->clock->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100343 if (!alarm_timer->pending) {
344 qemu_rearm_alarm_timer(alarm_timer);
345 }
346 /* Interrupt execution to force deadline recalculation. */
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200347 qemu_clock_warp(ts->clock);
348 if (use_icount) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100349 qemu_notify_event();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200350 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100351 }
352}
353
Paolo Bonzini4a998742011-03-11 16:33:58 +0100354void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
355{
356 qemu_mod_timer_ns(ts, expire_time * ts->scale);
357}
358
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200359bool qemu_timer_pending(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100360{
361 QEMUTimer *t;
Paolo Bonzini688eb382011-09-13 11:42:26 +0200362 for (t = ts->clock->active_timers; t != NULL; t = t->next) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200363 if (t == ts) {
364 return true;
365 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100366 }
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200367 return false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100368}
369
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200370bool qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100371{
Stefan Weil45c7b372011-03-24 21:31:24 +0100372 return qemu_timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100373}
374
Paolo Bonzini8156be52012-03-28 15:42:04 +0200375void qemu_run_timers(QEMUClock *clock)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100376{
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200377 QEMUTimer *ts;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100378 int64_t current_time;
379
380 if (!clock->enabled)
381 return;
382
Paolo Bonzini4a998742011-03-11 16:33:58 +0100383 current_time = qemu_get_clock_ns(clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100384 for(;;) {
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200385 ts = clock->active_timers;
Stefan Weil45c7b372011-03-24 21:31:24 +0100386 if (!qemu_timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100387 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100388 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100389 /* remove timer from the list before calling the callback */
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200390 clock->active_timers = ts->next;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100391 ts->next = NULL;
392
393 /* run the callback (the timer list can be modified) */
394 ts->cb(ts->opaque);
395 }
396}
397
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100398int64_t qemu_get_clock_ns(QEMUClock *clock)
399{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200400 int64_t now, last;
401
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100402 switch(clock->type) {
403 case QEMU_CLOCK_REALTIME:
404 return get_clock();
405 default:
406 case QEMU_CLOCK_VIRTUAL:
407 if (use_icount) {
408 return cpu_get_icount();
409 } else {
410 return cpu_get_clock();
411 }
412 case QEMU_CLOCK_HOST:
Jan Kiszka691a0c92011-06-20 14:06:27 +0200413 now = get_clock_realtime();
414 last = clock->last;
415 clock->last = now;
416 if (now < last) {
417 notifier_list_notify(&clock->reset_notifiers, &now);
418 }
419 return now;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100420 }
421}
422
Jan Kiszka691a0c92011-06-20 14:06:27 +0200423void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
424{
425 notifier_list_add(&clock->reset_notifiers, notifier);
426}
427
428void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
429{
Paolo Bonzini31552522012-01-13 17:34:01 +0100430 notifier_remove(notifier);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200431}
432
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100433void init_clocks(void)
434{
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100435 if (!rt_clock) {
436 rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
437 vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
438 host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
439 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100440}
441
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200442uint64_t qemu_timer_expire_time_ns(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100443{
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200444 return qemu_timer_pending(ts) ? ts->expire_time : -1;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100445}
446
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100447void qemu_run_all_timers(void)
448{
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200449 alarm_timer->pending = false;
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100450
Peter Portante158fd3c2012-04-05 11:00:45 -0400451 /* vm time timers */
452 qemu_run_timers(vm_clock);
453 qemu_run_timers(rt_clock);
454 qemu_run_timers(host_clock);
455
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100456 /* rearm timer, if not periodic */
457 if (alarm_timer->expired) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200458 alarm_timer->expired = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100459 qemu_rearm_alarm_timer(alarm_timer);
460 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100461}
462
463#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100464static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100465#else
466static void host_alarm_handler(int host_signum)
467#endif
468{
469 struct qemu_alarm_timer *t = alarm_timer;
470 if (!t)
471 return;
472
Stefan Weil82051992012-04-20 11:27:24 +0200473 t->expired = true;
474 t->pending = true;
475 qemu_notify_event();
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100476}
477
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100478#if defined(__linux__)
479
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100480#include "qemu/compatfd.h"
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200481
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100482static int dynticks_start_timer(struct qemu_alarm_timer *t)
483{
484 struct sigevent ev;
485 timer_t host_timer;
486 struct sigaction act;
487
488 sigfillset(&act.sa_mask);
489 act.sa_flags = 0;
490 act.sa_handler = host_alarm_handler;
491
492 sigaction(SIGALRM, &act, NULL);
493
494 /*
495 * Initialize ev struct to 0 to avoid valgrind complaining
496 * about uninitialized data in timer_create call
497 */
498 memset(&ev, 0, sizeof(ev));
499 ev.sigev_value.sival_int = 0;
500 ev.sigev_notify = SIGEV_SIGNAL;
Richard Henderson1e9737d2012-10-23 07:33:00 +1000501#ifdef CONFIG_SIGEV_THREAD_ID
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200502 if (qemu_signalfd_available()) {
503 ev.sigev_notify = SIGEV_THREAD_ID;
504 ev._sigev_un._tid = qemu_get_thread_id();
505 }
Richard Henderson1e9737d2012-10-23 07:33:00 +1000506#endif /* CONFIG_SIGEV_THREAD_ID */
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100507 ev.sigev_signo = SIGALRM;
508
509 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
510 perror("timer_create");
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100511 return -1;
512 }
513
Stefan Weilcd0544e2011-04-10 20:15:09 +0200514 t->timer = host_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100515
516 return 0;
517}
518
519static void dynticks_stop_timer(struct qemu_alarm_timer *t)
520{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200521 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100522
523 timer_delete(host_timer);
524}
525
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100526static void dynticks_rearm_timer(struct qemu_alarm_timer *t,
527 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100528{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200529 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100530 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100531 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100532
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100533 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
534 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100535
536 /* check whether a timer is already running */
537 if (timer_gettime(host_timer, &timeout)) {
538 perror("gettime");
539 fprintf(stderr, "Internal timer error: aborting\n");
540 exit(1);
541 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100542 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
543 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100544 return;
545
546 timeout.it_interval.tv_sec = 0;
547 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100548 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
549 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100550 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
551 perror("settime");
552 fprintf(stderr, "Internal timer error: aborting\n");
553 exit(1);
554 }
555}
556
557#endif /* defined(__linux__) */
558
Stefan Weilf26e5a52011-02-04 22:01:32 +0100559#if !defined(_WIN32)
560
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100561static int unix_start_timer(struct qemu_alarm_timer *t)
562{
563 struct sigaction act;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100564
565 /* timer signal */
566 sigfillset(&act.sa_mask);
567 act.sa_flags = 0;
568 act.sa_handler = host_alarm_handler;
569
570 sigaction(SIGALRM, &act, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200571 return 0;
572}
573
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100574static void unix_rearm_timer(struct qemu_alarm_timer *t,
575 int64_t nearest_delta_ns)
Paolo Bonzini84682832011-06-09 13:10:25 +0200576{
577 struct itimerval itv;
Paolo Bonzini84682832011-06-09 13:10:25 +0200578 int err;
579
Paolo Bonzini84682832011-06-09 13:10:25 +0200580 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
581 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100582
583 itv.it_interval.tv_sec = 0;
Paolo Bonzini84682832011-06-09 13:10:25 +0200584 itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */
585 itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
586 itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100587 err = setitimer(ITIMER_REAL, &itv, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200588 if (err) {
589 perror("setitimer");
590 fprintf(stderr, "Internal timer error: aborting\n");
591 exit(1);
592 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100593}
594
595static void unix_stop_timer(struct qemu_alarm_timer *t)
596{
597 struct itimerval itv;
598
599 memset(&itv, 0, sizeof(itv));
600 setitimer(ITIMER_REAL, &itv, NULL);
601}
602
603#endif /* !defined(_WIN32) */
604
605
606#ifdef _WIN32
607
Stefan Weil2f9cba02011-04-05 18:34:21 +0200608static MMRESULT mm_timer;
Stefan Weil40f08e82012-04-27 05:34:40 +0000609static TIMECAPS mm_tc;
Stefan Weil2f9cba02011-04-05 18:34:21 +0200610
611static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
612 DWORD_PTR dwUser, DWORD_PTR dw1,
613 DWORD_PTR dw2)
614{
615 struct qemu_alarm_timer *t = alarm_timer;
616 if (!t) {
617 return;
618 }
Stefan Weil82051992012-04-20 11:27:24 +0200619 t->expired = true;
620 t->pending = true;
621 qemu_notify_event();
Stefan Weil2f9cba02011-04-05 18:34:21 +0200622}
623
624static int mm_start_timer(struct qemu_alarm_timer *t)
625{
Stefan Weil40f08e82012-04-27 05:34:40 +0000626 timeGetDevCaps(&mm_tc, sizeof(mm_tc));
Stefan Weil2f9cba02011-04-05 18:34:21 +0200627
Stefan Weil40f08e82012-04-27 05:34:40 +0000628 timeBeginPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200629
Stefan Weil40f08e82012-04-27 05:34:40 +0000630 mm_timer = timeSetEvent(mm_tc.wPeriodMin, /* interval (ms) */
631 mm_tc.wPeriodMin, /* resolution */
Stefan Weil2f9cba02011-04-05 18:34:21 +0200632 mm_alarm_handler, /* function */
633 (DWORD_PTR)t, /* parameter */
Stefan Weil82051992012-04-20 11:27:24 +0200634 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200635
636 if (!mm_timer) {
Stefan Weil52ef6512012-05-08 19:14:43 +0200637 fprintf(stderr, "Failed to initialize win32 alarm timer\n");
Stefan Weil40f08e82012-04-27 05:34:40 +0000638 timeEndPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200639 return -1;
640 }
641
642 return 0;
643}
644
645static void mm_stop_timer(struct qemu_alarm_timer *t)
646{
647 timeKillEvent(mm_timer);
Stefan Weil40f08e82012-04-27 05:34:40 +0000648 timeEndPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200649}
650
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100651static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta)
Stefan Weil2f9cba02011-04-05 18:34:21 +0200652{
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100653 int64_t nearest_delta_ms = delta / 1000000;
Stefan Weil40f08e82012-04-27 05:34:40 +0000654 if (nearest_delta_ms < mm_tc.wPeriodMin) {
655 nearest_delta_ms = mm_tc.wPeriodMin;
656 } else if (nearest_delta_ms > mm_tc.wPeriodMax) {
657 nearest_delta_ms = mm_tc.wPeriodMax;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100658 }
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100659
660 timeKillEvent(mm_timer);
Stefan Weil40f08e82012-04-27 05:34:40 +0000661 mm_timer = timeSetEvent((UINT)nearest_delta_ms,
662 mm_tc.wPeriodMin,
Stefan Weil2f9cba02011-04-05 18:34:21 +0200663 mm_alarm_handler,
664 (DWORD_PTR)t,
665 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
666
667 if (!mm_timer) {
Stefan Weil52ef6512012-05-08 19:14:43 +0200668 fprintf(stderr, "Failed to re-arm win32 alarm timer\n");
Stefan Weil40f08e82012-04-27 05:34:40 +0000669 timeEndPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200670 exit(1);
671 }
672}
673
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100674static int win32_start_timer(struct qemu_alarm_timer *t)
675{
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100676 HANDLE hTimer;
677 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100678
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100679 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
680 is zero) that has already expired, the timer is not updated. Since
681 creating a new timer is relatively expensive, set a bogus one-hour
682 interval in the dynticks case. */
683 success = CreateTimerQueueTimer(&hTimer,
684 NULL,
685 host_alarm_handler,
686 t,
687 1,
Stefan Weil82051992012-04-20 11:27:24 +0200688 3600000,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100689 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100690
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100691 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100692 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
693 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100694 return -1;
695 }
696
Stefan Weilcd0544e2011-04-10 20:15:09 +0200697 t->timer = hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100698 return 0;
699}
700
701static void win32_stop_timer(struct qemu_alarm_timer *t)
702{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200703 HANDLE hTimer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100704
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100705 if (hTimer) {
706 DeleteTimerQueueTimer(NULL, hTimer, NULL);
707 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100708}
709
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100710static void win32_rearm_timer(struct qemu_alarm_timer *t,
711 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100712{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200713 HANDLE hTimer = t->timer;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100714 int64_t nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100715 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100716
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100717 nearest_delta_ms = nearest_delta_ns / 1000000;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +0100718 if (nearest_delta_ms < 1) {
719 nearest_delta_ms = 1;
720 }
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100721 /* ULONG_MAX can be 32 bit */
722 if (nearest_delta_ms > ULONG_MAX) {
723 nearest_delta_ms = ULONG_MAX;
724 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100725 success = ChangeTimerQueueTimer(NULL,
726 hTimer,
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100727 (unsigned long) nearest_delta_ms,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100728 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100729
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100730 if (!success) {
731 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100732 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100733 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100734 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100735
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100736}
737
738#endif /* _WIN32 */
739
Paolo Bonzini4260a732011-09-19 10:18:51 +0200740static void quit_timers(void)
741{
742 struct qemu_alarm_timer *t = alarm_timer;
743 alarm_timer = NULL;
744 t->stop(t);
745}
746
Stefan Weil253ecf82012-11-04 21:42:08 +0100747#ifdef CONFIG_POSIX
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100748static void reinit_timers(void)
749{
750 struct qemu_alarm_timer *t = alarm_timer;
751 t->stop(t);
752 if (t->start(t)) {
753 fprintf(stderr, "Internal timer error: aborting\n");
754 exit(1);
755 }
756 qemu_rearm_alarm_timer(t);
757}
Stefan Weil253ecf82012-11-04 21:42:08 +0100758#endif /* CONFIG_POSIX */
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100759
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100760int init_timer_alarm(void)
761{
762 struct qemu_alarm_timer *t = NULL;
763 int i, err = -1;
764
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100765 if (alarm_timer) {
766 return 0;
767 }
768
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100769 for (i = 0; alarm_timers[i].name; i++) {
770 t = &alarm_timers[i];
771
772 err = t->start(t);
773 if (!err)
774 break;
775 }
776
777 if (err) {
778 err = -ENOENT;
779 goto fail;
780 }
781
Paolo Bonzini4260a732011-09-19 10:18:51 +0200782 atexit(quit_timers);
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100783#ifdef CONFIG_POSIX
784 pthread_atfork(NULL, NULL, reinit_timers);
785#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100786 alarm_timer = t;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100787 return 0;
788
789fail:
790 return err;
791}
792