blob: be29adf9ebad0bbddaaca8b871c9d13c3a342367 [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
Alex Bligh043a7e12013-08-21 16:02:42 +0100267 if (clock->enabled && clock->active_timers) {
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200268 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
Alex Bligh02a03a92013-08-21 16:02:41 +0100276/*
277 * As above, but return -1 for no deadline, and do not cap to 2^32
278 * as we know the result is always positive.
279 */
280
281int64_t qemu_clock_deadline_ns(QEMUClock *clock)
282{
283 int64_t delta;
284
285 if (!clock->enabled || !clock->active_timers) {
286 return -1;
287 }
288
289 delta = clock->active_timers->expire_time - qemu_get_clock_ns(clock);
290
291 if (delta <= 0) {
292 return 0;
293 }
294
295 return delta;
296}
297
298/* Transition function to convert a nanosecond timeout to ms
299 * This is used where a system does not support ppoll
300 */
301int qemu_timeout_ns_to_ms(int64_t ns)
302{
303 int64_t ms;
304 if (ns < 0) {
305 return -1;
306 }
307
308 if (!ns) {
309 return 0;
310 }
311
312 /* Always round up, because it's better to wait too long than to wait too
313 * little and effectively busy-wait
314 */
315 ms = (ns + SCALE_MS - 1) / SCALE_MS;
316
317 /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
318 if (ms > (int64_t) INT32_MAX) {
319 ms = INT32_MAX;
320 }
321
322 return (int) ms;
323}
324
325
Paolo Bonzini4a998742011-03-11 16:33:58 +0100326QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
327 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100328{
329 QEMUTimer *ts;
330
Anthony Liguori7267c092011-08-20 22:09:37 -0500331 ts = g_malloc0(sizeof(QEMUTimer));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100332 ts->clock = clock;
333 ts->cb = cb;
334 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100335 ts->scale = scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100336 return ts;
337}
338
339void qemu_free_timer(QEMUTimer *ts)
340{
Anthony Liguori7267c092011-08-20 22:09:37 -0500341 g_free(ts);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100342}
343
344/* stop a timer, but do not dealloc it */
345void qemu_del_timer(QEMUTimer *ts)
346{
347 QEMUTimer **pt, *t;
348
349 /* NOTE: this code must be signal safe because
Alex Blighe93379b2013-08-21 16:02:39 +0100350 timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200351 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100352 for(;;) {
353 t = *pt;
354 if (!t)
355 break;
356 if (t == ts) {
357 *pt = t->next;
358 break;
359 }
360 pt = &t->next;
361 }
362}
363
364/* modify the current timer so that it will be fired when current_time
365 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200366void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100367{
368 QEMUTimer **pt, *t;
369
370 qemu_del_timer(ts);
371
372 /* add the timer in the sorted list */
373 /* NOTE: this code must be signal safe because
Alex Blighe93379b2013-08-21 16:02:39 +0100374 timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200375 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100376 for(;;) {
377 t = *pt;
Alex Blighe93379b2013-08-21 16:02:39 +0100378 if (!timer_expired_ns(t, expire_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100379 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100380 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100381 pt = &t->next;
382 }
383 ts->expire_time = expire_time;
384 ts->next = *pt;
385 *pt = ts;
386
387 /* Rearm if necessary */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200388 if (pt == &ts->clock->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100389 if (!alarm_timer->pending) {
390 qemu_rearm_alarm_timer(alarm_timer);
391 }
392 /* Interrupt execution to force deadline recalculation. */
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200393 qemu_clock_warp(ts->clock);
394 if (use_icount) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100395 qemu_notify_event();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200396 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100397 }
398}
399
Paolo Bonzini4a998742011-03-11 16:33:58 +0100400void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
401{
402 qemu_mod_timer_ns(ts, expire_time * ts->scale);
403}
404
Alex Blighe93379b2013-08-21 16:02:39 +0100405bool timer_pending(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100406{
407 QEMUTimer *t;
Paolo Bonzini688eb382011-09-13 11:42:26 +0200408 for (t = ts->clock->active_timers; t != NULL; t = t->next) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200409 if (t == ts) {
410 return true;
411 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100412 }
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200413 return false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100414}
415
Alex Blighe93379b2013-08-21 16:02:39 +0100416bool timer_expired(QEMUTimer *timer_head, int64_t current_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100417{
Alex Blighe93379b2013-08-21 16:02:39 +0100418 return timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100419}
420
Paolo Bonzini8156be52012-03-28 15:42:04 +0200421void qemu_run_timers(QEMUClock *clock)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100422{
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200423 QEMUTimer *ts;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100424 int64_t current_time;
425
426 if (!clock->enabled)
427 return;
428
Paolo Bonzini4a998742011-03-11 16:33:58 +0100429 current_time = qemu_get_clock_ns(clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100430 for(;;) {
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200431 ts = clock->active_timers;
Alex Blighe93379b2013-08-21 16:02:39 +0100432 if (!timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100433 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100434 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100435 /* remove timer from the list before calling the callback */
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200436 clock->active_timers = ts->next;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100437 ts->next = NULL;
438
439 /* run the callback (the timer list can be modified) */
440 ts->cb(ts->opaque);
441 }
442}
443
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100444int64_t qemu_get_clock_ns(QEMUClock *clock)
445{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200446 int64_t now, last;
447
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100448 switch(clock->type) {
449 case QEMU_CLOCK_REALTIME:
450 return get_clock();
451 default:
452 case QEMU_CLOCK_VIRTUAL:
453 if (use_icount) {
454 return cpu_get_icount();
455 } else {
456 return cpu_get_clock();
457 }
458 case QEMU_CLOCK_HOST:
Jan Kiszka691a0c92011-06-20 14:06:27 +0200459 now = get_clock_realtime();
460 last = clock->last;
461 clock->last = now;
462 if (now < last) {
463 notifier_list_notify(&clock->reset_notifiers, &now);
464 }
465 return now;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100466 }
467}
468
Jan Kiszka691a0c92011-06-20 14:06:27 +0200469void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
470{
471 notifier_list_add(&clock->reset_notifiers, notifier);
472}
473
474void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
475{
Paolo Bonzini31552522012-01-13 17:34:01 +0100476 notifier_remove(notifier);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200477}
478
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100479void init_clocks(void)
480{
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100481 if (!rt_clock) {
Alex Bligh58ac56b2013-08-21 16:02:40 +0100482 rt_clock = qemu_clock_new(QEMU_CLOCK_REALTIME);
483 vm_clock = qemu_clock_new(QEMU_CLOCK_VIRTUAL);
484 host_clock = qemu_clock_new(QEMU_CLOCK_HOST);
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100485 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100486}
487
Alex Blighe93379b2013-08-21 16:02:39 +0100488uint64_t timer_expire_time_ns(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100489{
Alex Blighe93379b2013-08-21 16:02:39 +0100490 return timer_pending(ts) ? ts->expire_time : -1;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100491}
492
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100493void qemu_run_all_timers(void)
494{
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200495 alarm_timer->pending = false;
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100496
Peter Portante158fd3c2012-04-05 11:00:45 -0400497 /* vm time timers */
498 qemu_run_timers(vm_clock);
499 qemu_run_timers(rt_clock);
500 qemu_run_timers(host_clock);
501
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100502 /* rearm timer, if not periodic */
503 if (alarm_timer->expired) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200504 alarm_timer->expired = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100505 qemu_rearm_alarm_timer(alarm_timer);
506 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100507}
508
509#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100510static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100511#else
512static void host_alarm_handler(int host_signum)
513#endif
514{
515 struct qemu_alarm_timer *t = alarm_timer;
516 if (!t)
517 return;
518
Stefan Weil82051992012-04-20 11:27:24 +0200519 t->expired = true;
520 t->pending = true;
521 qemu_notify_event();
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100522}
523
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100524#if defined(__linux__)
525
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100526#include "qemu/compatfd.h"
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200527
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100528static int dynticks_start_timer(struct qemu_alarm_timer *t)
529{
530 struct sigevent ev;
531 timer_t host_timer;
532 struct sigaction act;
533
534 sigfillset(&act.sa_mask);
535 act.sa_flags = 0;
536 act.sa_handler = host_alarm_handler;
537
538 sigaction(SIGALRM, &act, NULL);
539
540 /*
541 * Initialize ev struct to 0 to avoid valgrind complaining
542 * about uninitialized data in timer_create call
543 */
544 memset(&ev, 0, sizeof(ev));
545 ev.sigev_value.sival_int = 0;
546 ev.sigev_notify = SIGEV_SIGNAL;
Richard Henderson1e9737d2012-10-23 07:33:00 +1000547#ifdef CONFIG_SIGEV_THREAD_ID
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200548 if (qemu_signalfd_available()) {
549 ev.sigev_notify = SIGEV_THREAD_ID;
550 ev._sigev_un._tid = qemu_get_thread_id();
551 }
Richard Henderson1e9737d2012-10-23 07:33:00 +1000552#endif /* CONFIG_SIGEV_THREAD_ID */
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100553 ev.sigev_signo = SIGALRM;
554
555 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
556 perror("timer_create");
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100557 return -1;
558 }
559
Stefan Weilcd0544e2011-04-10 20:15:09 +0200560 t->timer = host_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100561
562 return 0;
563}
564
565static void dynticks_stop_timer(struct qemu_alarm_timer *t)
566{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200567 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100568
569 timer_delete(host_timer);
570}
571
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100572static void dynticks_rearm_timer(struct qemu_alarm_timer *t,
573 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100574{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200575 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100576 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100577 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100578
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100579 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
580 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100581
582 /* check whether a timer is already running */
583 if (timer_gettime(host_timer, &timeout)) {
584 perror("gettime");
585 fprintf(stderr, "Internal timer error: aborting\n");
586 exit(1);
587 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100588 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
589 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100590 return;
591
592 timeout.it_interval.tv_sec = 0;
593 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100594 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
595 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100596 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
597 perror("settime");
598 fprintf(stderr, "Internal timer error: aborting\n");
599 exit(1);
600 }
601}
602
603#endif /* defined(__linux__) */
604
Stefan Weilf26e5a52011-02-04 22:01:32 +0100605#if !defined(_WIN32)
606
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100607static int unix_start_timer(struct qemu_alarm_timer *t)
608{
609 struct sigaction act;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100610
611 /* timer signal */
612 sigfillset(&act.sa_mask);
613 act.sa_flags = 0;
614 act.sa_handler = host_alarm_handler;
615
616 sigaction(SIGALRM, &act, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200617 return 0;
618}
619
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100620static void unix_rearm_timer(struct qemu_alarm_timer *t,
621 int64_t nearest_delta_ns)
Paolo Bonzini84682832011-06-09 13:10:25 +0200622{
623 struct itimerval itv;
Paolo Bonzini84682832011-06-09 13:10:25 +0200624 int err;
625
Paolo Bonzini84682832011-06-09 13:10:25 +0200626 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
627 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100628
629 itv.it_interval.tv_sec = 0;
Paolo Bonzini84682832011-06-09 13:10:25 +0200630 itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */
631 itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
632 itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100633 err = setitimer(ITIMER_REAL, &itv, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200634 if (err) {
635 perror("setitimer");
636 fprintf(stderr, "Internal timer error: aborting\n");
637 exit(1);
638 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100639}
640
641static void unix_stop_timer(struct qemu_alarm_timer *t)
642{
643 struct itimerval itv;
644
645 memset(&itv, 0, sizeof(itv));
646 setitimer(ITIMER_REAL, &itv, NULL);
647}
648
649#endif /* !defined(_WIN32) */
650
651
652#ifdef _WIN32
653
Stefan Weil2f9cba02011-04-05 18:34:21 +0200654static MMRESULT mm_timer;
Stefan Weil40f08e82012-04-27 05:34:40 +0000655static TIMECAPS mm_tc;
Stefan Weil2f9cba02011-04-05 18:34:21 +0200656
657static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
658 DWORD_PTR dwUser, DWORD_PTR dw1,
659 DWORD_PTR dw2)
660{
661 struct qemu_alarm_timer *t = alarm_timer;
662 if (!t) {
663 return;
664 }
Stefan Weil82051992012-04-20 11:27:24 +0200665 t->expired = true;
666 t->pending = true;
667 qemu_notify_event();
Stefan Weil2f9cba02011-04-05 18:34:21 +0200668}
669
670static int mm_start_timer(struct qemu_alarm_timer *t)
671{
Stefan Weil40f08e82012-04-27 05:34:40 +0000672 timeGetDevCaps(&mm_tc, sizeof(mm_tc));
Stefan Weil2f9cba02011-04-05 18:34:21 +0200673 return 0;
674}
675
676static void mm_stop_timer(struct qemu_alarm_timer *t)
677{
Paolo Bonzini0727b862013-02-20 14:43:31 +0100678 if (mm_timer) {
679 timeKillEvent(mm_timer);
680 }
Stefan Weil2f9cba02011-04-05 18:34:21 +0200681}
682
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100683static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta)
Stefan Weil2f9cba02011-04-05 18:34:21 +0200684{
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100685 int64_t nearest_delta_ms = delta / 1000000;
Stefan Weil40f08e82012-04-27 05:34:40 +0000686 if (nearest_delta_ms < mm_tc.wPeriodMin) {
687 nearest_delta_ms = mm_tc.wPeriodMin;
688 } else if (nearest_delta_ms > mm_tc.wPeriodMax) {
689 nearest_delta_ms = mm_tc.wPeriodMax;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100690 }
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100691
Paolo Bonzini0727b862013-02-20 14:43:31 +0100692 if (mm_timer) {
693 timeKillEvent(mm_timer);
694 }
Stefan Weil40f08e82012-04-27 05:34:40 +0000695 mm_timer = timeSetEvent((UINT)nearest_delta_ms,
696 mm_tc.wPeriodMin,
Stefan Weil2f9cba02011-04-05 18:34:21 +0200697 mm_alarm_handler,
698 (DWORD_PTR)t,
699 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
700
701 if (!mm_timer) {
Stefan Weil52ef6512012-05-08 19:14:43 +0200702 fprintf(stderr, "Failed to re-arm win32 alarm timer\n");
Stefan Weil40f08e82012-04-27 05:34:40 +0000703 timeEndPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200704 exit(1);
705 }
706}
707
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100708static int win32_start_timer(struct qemu_alarm_timer *t)
709{
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100710 HANDLE hTimer;
711 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100712
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100713 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
714 is zero) that has already expired, the timer is not updated. Since
715 creating a new timer is relatively expensive, set a bogus one-hour
716 interval in the dynticks case. */
717 success = CreateTimerQueueTimer(&hTimer,
718 NULL,
719 host_alarm_handler,
720 t,
721 1,
Stefan Weil82051992012-04-20 11:27:24 +0200722 3600000,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100723 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100724
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100725 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100726 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
727 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100728 return -1;
729 }
730
Stefan Weilcd0544e2011-04-10 20:15:09 +0200731 t->timer = hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100732 return 0;
733}
734
735static void win32_stop_timer(struct qemu_alarm_timer *t)
736{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200737 HANDLE hTimer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100738
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100739 if (hTimer) {
740 DeleteTimerQueueTimer(NULL, hTimer, NULL);
741 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100742}
743
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100744static void win32_rearm_timer(struct qemu_alarm_timer *t,
745 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100746{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200747 HANDLE hTimer = t->timer;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100748 int64_t nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100749 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100750
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100751 nearest_delta_ms = nearest_delta_ns / 1000000;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +0100752 if (nearest_delta_ms < 1) {
753 nearest_delta_ms = 1;
754 }
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100755 /* ULONG_MAX can be 32 bit */
756 if (nearest_delta_ms > ULONG_MAX) {
757 nearest_delta_ms = ULONG_MAX;
758 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100759 success = ChangeTimerQueueTimer(NULL,
760 hTimer,
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100761 (unsigned long) nearest_delta_ms,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100762 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100763
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100764 if (!success) {
765 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100766 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100767 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100768 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100769
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100770}
771
772#endif /* _WIN32 */
773
Paolo Bonzini4260a732011-09-19 10:18:51 +0200774static void quit_timers(void)
775{
776 struct qemu_alarm_timer *t = alarm_timer;
777 alarm_timer = NULL;
778 t->stop(t);
779}
780
Stefan Weil253ecf82012-11-04 21:42:08 +0100781#ifdef CONFIG_POSIX
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100782static void reinit_timers(void)
783{
784 struct qemu_alarm_timer *t = alarm_timer;
785 t->stop(t);
786 if (t->start(t)) {
787 fprintf(stderr, "Internal timer error: aborting\n");
788 exit(1);
789 }
790 qemu_rearm_alarm_timer(t);
791}
Stefan Weil253ecf82012-11-04 21:42:08 +0100792#endif /* CONFIG_POSIX */
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100793
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100794int init_timer_alarm(void)
795{
796 struct qemu_alarm_timer *t = NULL;
797 int i, err = -1;
798
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100799 if (alarm_timer) {
800 return 0;
801 }
802
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100803 for (i = 0; alarm_timers[i].name; i++) {
804 t = &alarm_timers[i];
805
806 err = t->start(t);
807 if (!err)
808 break;
809 }
810
811 if (err) {
812 err = -ENOENT;
813 goto fail;
814 }
815
Paolo Bonzini4260a732011-09-19 10:18:51 +0200816 atexit(quit_timers);
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100817#ifdef CONFIG_POSIX
818 pthread_atfork(NULL, NULL, reinit_timers);
819#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100820 alarm_timer = t;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100821 return 0;
822
823fail:
824 return err;
825}
826