blob: 4117addcdef14eab817fe050a6914829ea2f742c [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
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010043struct QEMUClock {
Paolo Bonzini688eb382011-09-13 11:42:26 +020044 QEMUTimer *active_timers;
Jan Kiszka691a0c92011-06-20 14:06:27 +020045
46 NotifierList reset_notifiers;
47 int64_t last;
Stefan Weil9a14b292012-04-20 11:51:58 +020048
49 int type;
50 bool enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010051};
52
53struct QEMUTimer {
Paolo Bonzini4a998742011-03-11 16:33:58 +010054 int64_t expire_time; /* in nanoseconds */
Stefan Weil9a14b292012-04-20 11:51:58 +020055 QEMUClock *clock;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010056 QEMUTimerCB *cb;
57 void *opaque;
Stefan Weil9a14b292012-04-20 11:51:58 +020058 QEMUTimer *next;
59 int scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010060};
61
62struct qemu_alarm_timer {
63 char const *name;
64 int (*start)(struct qemu_alarm_timer *t);
65 void (*stop)(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010066 void (*rearm)(struct qemu_alarm_timer *t, int64_t nearest_delta_ns);
Stefan Weilcd0544e2011-04-10 20:15:09 +020067#if defined(__linux__)
Stefan Weilcd0544e2011-04-10 20:15:09 +020068 timer_t timer;
Stefan Weil9a14b292012-04-20 11:51:58 +020069 int fd;
Stefan Weilcd0544e2011-04-10 20:15:09 +020070#elif defined(_WIN32)
71 HANDLE timer;
72#endif
Stefan Weil5e1ec7b2012-04-20 10:45:48 +020073 bool expired;
74 bool pending;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010075};
76
77static struct qemu_alarm_timer *alarm_timer;
78
Alex Blighe93379b2013-08-21 16:02:39 +010079static bool timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
Stefan Weil45c7b372011-03-24 21:31:24 +010080{
81 return timer_head && (timer_head->expire_time <= current_time);
82}
83
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010084static int64_t qemu_next_alarm_deadline(void)
85{
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +010086 int64_t delta = INT64_MAX;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010087 int64_t rtdelta;
88
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +010089 if (!use_icount && vm_clock->enabled && vm_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010090 delta = vm_clock->active_timers->expire_time -
91 qemu_get_clock_ns(vm_clock);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010092 }
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +010093 if (host_clock->enabled && host_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010094 int64_t hdelta = host_clock->active_timers->expire_time -
95 qemu_get_clock_ns(host_clock);
96 if (hdelta < delta) {
97 delta = hdelta;
98 }
99 }
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +0100100 if (rt_clock->enabled && rt_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100101 rtdelta = (rt_clock->active_timers->expire_time -
102 qemu_get_clock_ns(rt_clock));
103 if (rtdelta < delta) {
104 delta = rtdelta;
105 }
106 }
107
108 return delta;
109}
110
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100111static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
112{
Stefano Stabellini82274212012-05-29 03:35:24 +0000113 int64_t nearest_delta_ns = qemu_next_alarm_deadline();
114 if (nearest_delta_ns < INT64_MAX) {
115 t->rearm(t, nearest_delta_ns);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100116 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100117}
118
Paolo Bonzini9c132462011-02-03 14:48:59 +0100119/* TODO: MIN_TIMER_REARM_NS should be optimized */
120#define MIN_TIMER_REARM_NS 250000
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100121
122#ifdef _WIN32
123
Stefan Weil2f9cba02011-04-05 18:34:21 +0200124static int mm_start_timer(struct qemu_alarm_timer *t);
125static void mm_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100126static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200127
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100128static int win32_start_timer(struct qemu_alarm_timer *t);
129static void win32_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100130static void win32_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100131
132#else
133
134static int unix_start_timer(struct qemu_alarm_timer *t);
135static void unix_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100136static void unix_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100137
138#ifdef __linux__
139
140static int dynticks_start_timer(struct qemu_alarm_timer *t);
141static void dynticks_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100142static void dynticks_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100143
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100144#endif /* __linux__ */
145
146#endif /* _WIN32 */
147
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100148static struct qemu_alarm_timer alarm_timers[] = {
149#ifndef _WIN32
150#ifdef __linux__
151 {"dynticks", dynticks_start_timer,
Stefan Weilcd0544e2011-04-10 20:15:09 +0200152 dynticks_stop_timer, dynticks_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100153#endif
Paolo Bonzini84682832011-06-09 13:10:25 +0200154 {"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100155#else
Paolo Bonzinicca5de72011-11-09 12:46:56 +0100156 {"mmtimer", mm_start_timer, mm_stop_timer, mm_rearm_timer},
Stefan Weilcd0544e2011-04-10 20:15:09 +0200157 {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100158#endif
159 {NULL, }
160};
161
162static void show_available_alarms(void)
163{
164 int i;
165
166 printf("Available alarm timers, in order of precedence:\n");
167 for (i = 0; alarm_timers[i].name; i++)
168 printf("%s\n", alarm_timers[i].name);
169}
170
171void configure_alarms(char const *opt)
172{
173 int i;
174 int cur = 0;
175 int count = ARRAY_SIZE(alarm_timers) - 1;
176 char *arg;
177 char *name;
178 struct qemu_alarm_timer tmp;
179
Peter Maydellc8057f92012-08-02 13:45:54 +0100180 if (is_help_option(opt)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100181 show_available_alarms();
182 exit(0);
183 }
184
Anthony Liguori7267c092011-08-20 22:09:37 -0500185 arg = g_strdup(opt);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100186
187 /* Reorder the array */
188 name = strtok(arg, ",");
189 while (name) {
190 for (i = 0; i < count && alarm_timers[i].name; i++) {
191 if (!strcmp(alarm_timers[i].name, name))
192 break;
193 }
194
195 if (i == count) {
196 fprintf(stderr, "Unknown clock %s\n", name);
197 goto next;
198 }
199
200 if (i < cur)
201 /* Ignore */
202 goto next;
203
204 /* Swap */
205 tmp = alarm_timers[i];
206 alarm_timers[i] = alarm_timers[cur];
207 alarm_timers[cur] = tmp;
208
209 cur++;
210next:
211 name = strtok(NULL, ",");
212 }
213
Anthony Liguori7267c092011-08-20 22:09:37 -0500214 g_free(arg);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100215
216 if (cur) {
217 /* Disable remaining timers */
218 for (i = cur; i < count; i++)
219 alarm_timers[i].name = NULL;
220 } else {
221 show_available_alarms();
222 exit(1);
223 }
224}
225
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100226QEMUClock *rt_clock;
227QEMUClock *vm_clock;
228QEMUClock *host_clock;
229
Alex Bligh58ac56b2013-08-21 16:02:40 +0100230static QEMUClock *qemu_clock_new(int type)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100231{
232 QEMUClock *clock;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200233
Anthony Liguori7267c092011-08-20 22:09:37 -0500234 clock = g_malloc0(sizeof(QEMUClock));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100235 clock->type = type;
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200236 clock->enabled = true;
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200237 clock->last = INT64_MIN;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200238 notifier_list_init(&clock->reset_notifiers);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100239 return clock;
240}
241
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200242void qemu_clock_enable(QEMUClock *clock, bool enabled)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100243{
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200244 bool old = clock->enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100245 clock->enabled = enabled;
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200246 if (enabled && !old) {
247 qemu_rearm_alarm_timer(alarm_timer);
248 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100249}
250
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200251int64_t qemu_clock_has_timers(QEMUClock *clock)
252{
253 return !!clock->active_timers;
254}
255
256int64_t qemu_clock_expired(QEMUClock *clock)
257{
258 return (clock->active_timers &&
259 clock->active_timers->expire_time < qemu_get_clock_ns(clock));
260}
261
262int64_t qemu_clock_deadline(QEMUClock *clock)
263{
264 /* To avoid problems with overflow limit this to 2^32. */
265 int64_t delta = INT32_MAX;
266
267 if (clock->active_timers) {
268 delta = clock->active_timers->expire_time - qemu_get_clock_ns(clock);
269 }
270 if (delta < 0) {
271 delta = 0;
272 }
273 return delta;
274}
275
Paolo Bonzini4a998742011-03-11 16:33:58 +0100276QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
277 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100278{
279 QEMUTimer *ts;
280
Anthony Liguori7267c092011-08-20 22:09:37 -0500281 ts = g_malloc0(sizeof(QEMUTimer));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100282 ts->clock = clock;
283 ts->cb = cb;
284 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100285 ts->scale = scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100286 return ts;
287}
288
289void qemu_free_timer(QEMUTimer *ts)
290{
Anthony Liguori7267c092011-08-20 22:09:37 -0500291 g_free(ts);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100292}
293
294/* stop a timer, but do not dealloc it */
295void qemu_del_timer(QEMUTimer *ts)
296{
297 QEMUTimer **pt, *t;
298
299 /* NOTE: this code must be signal safe because
Alex Blighe93379b2013-08-21 16:02:39 +0100300 timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200301 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100302 for(;;) {
303 t = *pt;
304 if (!t)
305 break;
306 if (t == ts) {
307 *pt = t->next;
308 break;
309 }
310 pt = &t->next;
311 }
312}
313
314/* modify the current timer so that it will be fired when current_time
315 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200316void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100317{
318 QEMUTimer **pt, *t;
319
320 qemu_del_timer(ts);
321
322 /* add the timer in the sorted list */
323 /* NOTE: this code must be signal safe because
Alex Blighe93379b2013-08-21 16:02:39 +0100324 timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200325 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100326 for(;;) {
327 t = *pt;
Alex Blighe93379b2013-08-21 16:02:39 +0100328 if (!timer_expired_ns(t, expire_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100329 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100330 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100331 pt = &t->next;
332 }
333 ts->expire_time = expire_time;
334 ts->next = *pt;
335 *pt = ts;
336
337 /* Rearm if necessary */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200338 if (pt == &ts->clock->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100339 if (!alarm_timer->pending) {
340 qemu_rearm_alarm_timer(alarm_timer);
341 }
342 /* Interrupt execution to force deadline recalculation. */
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200343 qemu_clock_warp(ts->clock);
344 if (use_icount) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100345 qemu_notify_event();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200346 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100347 }
348}
349
Paolo Bonzini4a998742011-03-11 16:33:58 +0100350void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
351{
352 qemu_mod_timer_ns(ts, expire_time * ts->scale);
353}
354
Alex Blighe93379b2013-08-21 16:02:39 +0100355bool timer_pending(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100356{
357 QEMUTimer *t;
Paolo Bonzini688eb382011-09-13 11:42:26 +0200358 for (t = ts->clock->active_timers; t != NULL; t = t->next) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200359 if (t == ts) {
360 return true;
361 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100362 }
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200363 return false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100364}
365
Alex Blighe93379b2013-08-21 16:02:39 +0100366bool timer_expired(QEMUTimer *timer_head, int64_t current_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100367{
Alex Blighe93379b2013-08-21 16:02:39 +0100368 return timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100369}
370
Paolo Bonzini8156be52012-03-28 15:42:04 +0200371void qemu_run_timers(QEMUClock *clock)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100372{
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200373 QEMUTimer *ts;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100374 int64_t current_time;
375
376 if (!clock->enabled)
377 return;
378
Paolo Bonzini4a998742011-03-11 16:33:58 +0100379 current_time = qemu_get_clock_ns(clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100380 for(;;) {
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200381 ts = clock->active_timers;
Alex Blighe93379b2013-08-21 16:02:39 +0100382 if (!timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100383 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100384 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100385 /* remove timer from the list before calling the callback */
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200386 clock->active_timers = ts->next;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100387 ts->next = NULL;
388
389 /* run the callback (the timer list can be modified) */
390 ts->cb(ts->opaque);
391 }
392}
393
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100394int64_t qemu_get_clock_ns(QEMUClock *clock)
395{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200396 int64_t now, last;
397
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100398 switch(clock->type) {
399 case QEMU_CLOCK_REALTIME:
400 return get_clock();
401 default:
402 case QEMU_CLOCK_VIRTUAL:
403 if (use_icount) {
404 return cpu_get_icount();
405 } else {
406 return cpu_get_clock();
407 }
408 case QEMU_CLOCK_HOST:
Jan Kiszka691a0c92011-06-20 14:06:27 +0200409 now = get_clock_realtime();
410 last = clock->last;
411 clock->last = now;
412 if (now < last) {
413 notifier_list_notify(&clock->reset_notifiers, &now);
414 }
415 return now;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100416 }
417}
418
Jan Kiszka691a0c92011-06-20 14:06:27 +0200419void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
420{
421 notifier_list_add(&clock->reset_notifiers, notifier);
422}
423
424void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
425{
Paolo Bonzini31552522012-01-13 17:34:01 +0100426 notifier_remove(notifier);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200427}
428
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100429void init_clocks(void)
430{
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100431 if (!rt_clock) {
Alex Bligh58ac56b2013-08-21 16:02:40 +0100432 rt_clock = qemu_clock_new(QEMU_CLOCK_REALTIME);
433 vm_clock = qemu_clock_new(QEMU_CLOCK_VIRTUAL);
434 host_clock = qemu_clock_new(QEMU_CLOCK_HOST);
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100435 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100436}
437
Alex Blighe93379b2013-08-21 16:02:39 +0100438uint64_t timer_expire_time_ns(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100439{
Alex Blighe93379b2013-08-21 16:02:39 +0100440 return timer_pending(ts) ? ts->expire_time : -1;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100441}
442
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100443void qemu_run_all_timers(void)
444{
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200445 alarm_timer->pending = false;
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100446
Peter Portante158fd3c2012-04-05 11:00:45 -0400447 /* vm time timers */
448 qemu_run_timers(vm_clock);
449 qemu_run_timers(rt_clock);
450 qemu_run_timers(host_clock);
451
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100452 /* rearm timer, if not periodic */
453 if (alarm_timer->expired) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200454 alarm_timer->expired = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100455 qemu_rearm_alarm_timer(alarm_timer);
456 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100457}
458
459#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100460static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100461#else
462static void host_alarm_handler(int host_signum)
463#endif
464{
465 struct qemu_alarm_timer *t = alarm_timer;
466 if (!t)
467 return;
468
Stefan Weil82051992012-04-20 11:27:24 +0200469 t->expired = true;
470 t->pending = true;
471 qemu_notify_event();
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100472}
473
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100474#if defined(__linux__)
475
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100476#include "qemu/compatfd.h"
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200477
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100478static int dynticks_start_timer(struct qemu_alarm_timer *t)
479{
480 struct sigevent ev;
481 timer_t host_timer;
482 struct sigaction act;
483
484 sigfillset(&act.sa_mask);
485 act.sa_flags = 0;
486 act.sa_handler = host_alarm_handler;
487
488 sigaction(SIGALRM, &act, NULL);
489
490 /*
491 * Initialize ev struct to 0 to avoid valgrind complaining
492 * about uninitialized data in timer_create call
493 */
494 memset(&ev, 0, sizeof(ev));
495 ev.sigev_value.sival_int = 0;
496 ev.sigev_notify = SIGEV_SIGNAL;
Richard Henderson1e9737d2012-10-23 07:33:00 +1000497#ifdef CONFIG_SIGEV_THREAD_ID
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200498 if (qemu_signalfd_available()) {
499 ev.sigev_notify = SIGEV_THREAD_ID;
500 ev._sigev_un._tid = qemu_get_thread_id();
501 }
Richard Henderson1e9737d2012-10-23 07:33:00 +1000502#endif /* CONFIG_SIGEV_THREAD_ID */
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100503 ev.sigev_signo = SIGALRM;
504
505 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
506 perror("timer_create");
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100507 return -1;
508 }
509
Stefan Weilcd0544e2011-04-10 20:15:09 +0200510 t->timer = host_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100511
512 return 0;
513}
514
515static void dynticks_stop_timer(struct qemu_alarm_timer *t)
516{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200517 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100518
519 timer_delete(host_timer);
520}
521
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100522static void dynticks_rearm_timer(struct qemu_alarm_timer *t,
523 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100524{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200525 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100526 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100527 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100528
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100529 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
530 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100531
532 /* check whether a timer is already running */
533 if (timer_gettime(host_timer, &timeout)) {
534 perror("gettime");
535 fprintf(stderr, "Internal timer error: aborting\n");
536 exit(1);
537 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100538 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
539 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100540 return;
541
542 timeout.it_interval.tv_sec = 0;
543 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100544 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
545 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100546 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
547 perror("settime");
548 fprintf(stderr, "Internal timer error: aborting\n");
549 exit(1);
550 }
551}
552
553#endif /* defined(__linux__) */
554
Stefan Weilf26e5a52011-02-04 22:01:32 +0100555#if !defined(_WIN32)
556
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100557static int unix_start_timer(struct qemu_alarm_timer *t)
558{
559 struct sigaction act;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100560
561 /* timer signal */
562 sigfillset(&act.sa_mask);
563 act.sa_flags = 0;
564 act.sa_handler = host_alarm_handler;
565
566 sigaction(SIGALRM, &act, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200567 return 0;
568}
569
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100570static void unix_rearm_timer(struct qemu_alarm_timer *t,
571 int64_t nearest_delta_ns)
Paolo Bonzini84682832011-06-09 13:10:25 +0200572{
573 struct itimerval itv;
Paolo Bonzini84682832011-06-09 13:10:25 +0200574 int err;
575
Paolo Bonzini84682832011-06-09 13:10:25 +0200576 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
577 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100578
579 itv.it_interval.tv_sec = 0;
Paolo Bonzini84682832011-06-09 13:10:25 +0200580 itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */
581 itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
582 itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100583 err = setitimer(ITIMER_REAL, &itv, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200584 if (err) {
585 perror("setitimer");
586 fprintf(stderr, "Internal timer error: aborting\n");
587 exit(1);
588 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100589}
590
591static void unix_stop_timer(struct qemu_alarm_timer *t)
592{
593 struct itimerval itv;
594
595 memset(&itv, 0, sizeof(itv));
596 setitimer(ITIMER_REAL, &itv, NULL);
597}
598
599#endif /* !defined(_WIN32) */
600
601
602#ifdef _WIN32
603
Stefan Weil2f9cba02011-04-05 18:34:21 +0200604static MMRESULT mm_timer;
Stefan Weil40f08e82012-04-27 05:34:40 +0000605static TIMECAPS mm_tc;
Stefan Weil2f9cba02011-04-05 18:34:21 +0200606
607static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
608 DWORD_PTR dwUser, DWORD_PTR dw1,
609 DWORD_PTR dw2)
610{
611 struct qemu_alarm_timer *t = alarm_timer;
612 if (!t) {
613 return;
614 }
Stefan Weil82051992012-04-20 11:27:24 +0200615 t->expired = true;
616 t->pending = true;
617 qemu_notify_event();
Stefan Weil2f9cba02011-04-05 18:34:21 +0200618}
619
620static int mm_start_timer(struct qemu_alarm_timer *t)
621{
Stefan Weil40f08e82012-04-27 05:34:40 +0000622 timeGetDevCaps(&mm_tc, sizeof(mm_tc));
Stefan Weil2f9cba02011-04-05 18:34:21 +0200623 return 0;
624}
625
626static void mm_stop_timer(struct qemu_alarm_timer *t)
627{
Paolo Bonzini0727b862013-02-20 14:43:31 +0100628 if (mm_timer) {
629 timeKillEvent(mm_timer);
630 }
Stefan Weil2f9cba02011-04-05 18:34:21 +0200631}
632
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100633static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta)
Stefan Weil2f9cba02011-04-05 18:34:21 +0200634{
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100635 int64_t nearest_delta_ms = delta / 1000000;
Stefan Weil40f08e82012-04-27 05:34:40 +0000636 if (nearest_delta_ms < mm_tc.wPeriodMin) {
637 nearest_delta_ms = mm_tc.wPeriodMin;
638 } else if (nearest_delta_ms > mm_tc.wPeriodMax) {
639 nearest_delta_ms = mm_tc.wPeriodMax;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100640 }
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100641
Paolo Bonzini0727b862013-02-20 14:43:31 +0100642 if (mm_timer) {
643 timeKillEvent(mm_timer);
644 }
Stefan Weil40f08e82012-04-27 05:34:40 +0000645 mm_timer = timeSetEvent((UINT)nearest_delta_ms,
646 mm_tc.wPeriodMin,
Stefan Weil2f9cba02011-04-05 18:34:21 +0200647 mm_alarm_handler,
648 (DWORD_PTR)t,
649 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
650
651 if (!mm_timer) {
Stefan Weil52ef6512012-05-08 19:14:43 +0200652 fprintf(stderr, "Failed to re-arm win32 alarm timer\n");
Stefan Weil40f08e82012-04-27 05:34:40 +0000653 timeEndPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200654 exit(1);
655 }
656}
657
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100658static int win32_start_timer(struct qemu_alarm_timer *t)
659{
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100660 HANDLE hTimer;
661 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100662
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100663 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
664 is zero) that has already expired, the timer is not updated. Since
665 creating a new timer is relatively expensive, set a bogus one-hour
666 interval in the dynticks case. */
667 success = CreateTimerQueueTimer(&hTimer,
668 NULL,
669 host_alarm_handler,
670 t,
671 1,
Stefan Weil82051992012-04-20 11:27:24 +0200672 3600000,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100673 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100674
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100675 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100676 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
677 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100678 return -1;
679 }
680
Stefan Weilcd0544e2011-04-10 20:15:09 +0200681 t->timer = hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100682 return 0;
683}
684
685static void win32_stop_timer(struct qemu_alarm_timer *t)
686{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200687 HANDLE hTimer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100688
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100689 if (hTimer) {
690 DeleteTimerQueueTimer(NULL, hTimer, NULL);
691 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100692}
693
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100694static void win32_rearm_timer(struct qemu_alarm_timer *t,
695 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100696{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200697 HANDLE hTimer = t->timer;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100698 int64_t nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100699 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100700
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100701 nearest_delta_ms = nearest_delta_ns / 1000000;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +0100702 if (nearest_delta_ms < 1) {
703 nearest_delta_ms = 1;
704 }
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100705 /* ULONG_MAX can be 32 bit */
706 if (nearest_delta_ms > ULONG_MAX) {
707 nearest_delta_ms = ULONG_MAX;
708 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100709 success = ChangeTimerQueueTimer(NULL,
710 hTimer,
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100711 (unsigned long) nearest_delta_ms,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100712 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100713
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100714 if (!success) {
715 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100716 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100717 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100718 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100719
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100720}
721
722#endif /* _WIN32 */
723
Paolo Bonzini4260a732011-09-19 10:18:51 +0200724static void quit_timers(void)
725{
726 struct qemu_alarm_timer *t = alarm_timer;
727 alarm_timer = NULL;
728 t->stop(t);
729}
730
Stefan Weil253ecf82012-11-04 21:42:08 +0100731#ifdef CONFIG_POSIX
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100732static void reinit_timers(void)
733{
734 struct qemu_alarm_timer *t = alarm_timer;
735 t->stop(t);
736 if (t->start(t)) {
737 fprintf(stderr, "Internal timer error: aborting\n");
738 exit(1);
739 }
740 qemu_rearm_alarm_timer(t);
741}
Stefan Weil253ecf82012-11-04 21:42:08 +0100742#endif /* CONFIG_POSIX */
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100743
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100744int init_timer_alarm(void)
745{
746 struct qemu_alarm_timer *t = NULL;
747 int i, err = -1;
748
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100749 if (alarm_timer) {
750 return 0;
751 }
752
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100753 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
Paolo Bonzini4260a732011-09-19 10:18:51 +0200766 atexit(quit_timers);
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100767#ifdef CONFIG_POSIX
768 pthread_atfork(NULL, NULL, reinit_timers);
769#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100770 alarm_timer = t;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100771 return 0;
772
773fail:
774 return err;
775}
776