blob: 120d58fb2a88e3ead0b59462ae4a8cc2f9cb2e81 [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
Alex Bligh4e0c6522013-08-21 16:02:43 +010040#ifdef CONFIG_PPOLL
41#include <poll.h>
42#endif
43
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010044/***********************************************************/
45/* timers */
46
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010047struct 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
Alex Blighe93379b2013-08-21 16:02:39 +010083static bool timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
Stefan Weil45c7b372011-03-24 21:31:24 +010084{
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
Alex Bligh58ac56b2013-08-21 16:02:40 +0100234static QEMUClock *qemu_clock_new(int type)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100235{
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
Alex Bligh043a7e12013-08-21 16:02:42 +0100271 if (clock->enabled && clock->active_timers) {
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200272 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
Alex Bligh02a03a92013-08-21 16:02:41 +0100280/*
281 * As above, but return -1 for no deadline, and do not cap to 2^32
282 * as we know the result is always positive.
283 */
284
285int64_t qemu_clock_deadline_ns(QEMUClock *clock)
286{
287 int64_t delta;
288
289 if (!clock->enabled || !clock->active_timers) {
290 return -1;
291 }
292
293 delta = clock->active_timers->expire_time - qemu_get_clock_ns(clock);
294
295 if (delta <= 0) {
296 return 0;
297 }
298
299 return delta;
300}
301
302/* Transition function to convert a nanosecond timeout to ms
303 * This is used where a system does not support ppoll
304 */
305int qemu_timeout_ns_to_ms(int64_t ns)
306{
307 int64_t ms;
308 if (ns < 0) {
309 return -1;
310 }
311
312 if (!ns) {
313 return 0;
314 }
315
316 /* Always round up, because it's better to wait too long than to wait too
317 * little and effectively busy-wait
318 */
319 ms = (ns + SCALE_MS - 1) / SCALE_MS;
320
321 /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
322 if (ms > (int64_t) INT32_MAX) {
323 ms = INT32_MAX;
324 }
325
326 return (int) ms;
327}
328
329
Alex Bligh4e0c6522013-08-21 16:02:43 +0100330/* qemu implementation of g_poll which uses a nanosecond timeout but is
331 * otherwise identical to g_poll
332 */
333int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout)
334{
335#ifdef CONFIG_PPOLL
336 if (timeout < 0) {
337 return ppoll((struct pollfd *)fds, nfds, NULL, NULL);
338 } else {
339 struct timespec ts;
340 ts.tv_sec = timeout / 1000000000LL;
341 ts.tv_nsec = timeout % 1000000000LL;
342 return ppoll((struct pollfd *)fds, nfds, &ts, NULL);
343 }
344#else
345 return g_poll(fds, nfds, qemu_timeout_ns_to_ms(timeout));
346#endif
347}
348
349
Paolo Bonzini4a998742011-03-11 16:33:58 +0100350QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
351 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100352{
353 QEMUTimer *ts;
354
Anthony Liguori7267c092011-08-20 22:09:37 -0500355 ts = g_malloc0(sizeof(QEMUTimer));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100356 ts->clock = clock;
357 ts->cb = cb;
358 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100359 ts->scale = scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100360 return ts;
361}
362
363void qemu_free_timer(QEMUTimer *ts)
364{
Anthony Liguori7267c092011-08-20 22:09:37 -0500365 g_free(ts);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100366}
367
368/* stop a timer, but do not dealloc it */
369void qemu_del_timer(QEMUTimer *ts)
370{
371 QEMUTimer **pt, *t;
372
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;
378 if (!t)
379 break;
380 if (t == ts) {
381 *pt = t->next;
382 break;
383 }
384 pt = &t->next;
385 }
386}
387
388/* modify the current timer so that it will be fired when current_time
389 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200390void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100391{
392 QEMUTimer **pt, *t;
393
394 qemu_del_timer(ts);
395
396 /* add the timer in the sorted list */
397 /* NOTE: this code must be signal safe because
Alex Blighe93379b2013-08-21 16:02:39 +0100398 timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200399 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100400 for(;;) {
401 t = *pt;
Alex Blighe93379b2013-08-21 16:02:39 +0100402 if (!timer_expired_ns(t, expire_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100403 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100404 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100405 pt = &t->next;
406 }
407 ts->expire_time = expire_time;
408 ts->next = *pt;
409 *pt = ts;
410
411 /* Rearm if necessary */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200412 if (pt == &ts->clock->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100413 if (!alarm_timer->pending) {
414 qemu_rearm_alarm_timer(alarm_timer);
415 }
416 /* Interrupt execution to force deadline recalculation. */
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200417 qemu_clock_warp(ts->clock);
418 if (use_icount) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100419 qemu_notify_event();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200420 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100421 }
422}
423
Paolo Bonzini4a998742011-03-11 16:33:58 +0100424void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
425{
426 qemu_mod_timer_ns(ts, expire_time * ts->scale);
427}
428
Alex Blighe93379b2013-08-21 16:02:39 +0100429bool timer_pending(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100430{
431 QEMUTimer *t;
Paolo Bonzini688eb382011-09-13 11:42:26 +0200432 for (t = ts->clock->active_timers; t != NULL; t = t->next) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200433 if (t == ts) {
434 return true;
435 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100436 }
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200437 return false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100438}
439
Alex Blighe93379b2013-08-21 16:02:39 +0100440bool timer_expired(QEMUTimer *timer_head, int64_t current_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100441{
Alex Blighe93379b2013-08-21 16:02:39 +0100442 return timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100443}
444
Paolo Bonzini8156be52012-03-28 15:42:04 +0200445void qemu_run_timers(QEMUClock *clock)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100446{
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200447 QEMUTimer *ts;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100448 int64_t current_time;
449
450 if (!clock->enabled)
451 return;
452
Paolo Bonzini4a998742011-03-11 16:33:58 +0100453 current_time = qemu_get_clock_ns(clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100454 for(;;) {
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200455 ts = clock->active_timers;
Alex Blighe93379b2013-08-21 16:02:39 +0100456 if (!timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100457 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100458 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100459 /* remove timer from the list before calling the callback */
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200460 clock->active_timers = ts->next;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100461 ts->next = NULL;
462
463 /* run the callback (the timer list can be modified) */
464 ts->cb(ts->opaque);
465 }
466}
467
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100468int64_t qemu_get_clock_ns(QEMUClock *clock)
469{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200470 int64_t now, last;
471
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100472 switch(clock->type) {
473 case QEMU_CLOCK_REALTIME:
474 return get_clock();
475 default:
476 case QEMU_CLOCK_VIRTUAL:
477 if (use_icount) {
478 return cpu_get_icount();
479 } else {
480 return cpu_get_clock();
481 }
482 case QEMU_CLOCK_HOST:
Jan Kiszka691a0c92011-06-20 14:06:27 +0200483 now = get_clock_realtime();
484 last = clock->last;
485 clock->last = now;
486 if (now < last) {
487 notifier_list_notify(&clock->reset_notifiers, &now);
488 }
489 return now;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100490 }
491}
492
Jan Kiszka691a0c92011-06-20 14:06:27 +0200493void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
494{
495 notifier_list_add(&clock->reset_notifiers, notifier);
496}
497
498void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
499{
Paolo Bonzini31552522012-01-13 17:34:01 +0100500 notifier_remove(notifier);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200501}
502
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100503void init_clocks(void)
504{
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100505 if (!rt_clock) {
Alex Bligh58ac56b2013-08-21 16:02:40 +0100506 rt_clock = qemu_clock_new(QEMU_CLOCK_REALTIME);
507 vm_clock = qemu_clock_new(QEMU_CLOCK_VIRTUAL);
508 host_clock = qemu_clock_new(QEMU_CLOCK_HOST);
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100509 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100510}
511
Alex Blighe93379b2013-08-21 16:02:39 +0100512uint64_t timer_expire_time_ns(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100513{
Alex Blighe93379b2013-08-21 16:02:39 +0100514 return timer_pending(ts) ? ts->expire_time : -1;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100515}
516
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100517void qemu_run_all_timers(void)
518{
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200519 alarm_timer->pending = false;
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100520
Peter Portante158fd3c2012-04-05 11:00:45 -0400521 /* vm time timers */
522 qemu_run_timers(vm_clock);
523 qemu_run_timers(rt_clock);
524 qemu_run_timers(host_clock);
525
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100526 /* rearm timer, if not periodic */
527 if (alarm_timer->expired) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200528 alarm_timer->expired = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100529 qemu_rearm_alarm_timer(alarm_timer);
530 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100531}
532
533#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100534static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100535#else
536static void host_alarm_handler(int host_signum)
537#endif
538{
539 struct qemu_alarm_timer *t = alarm_timer;
540 if (!t)
541 return;
542
Stefan Weil82051992012-04-20 11:27:24 +0200543 t->expired = true;
544 t->pending = true;
545 qemu_notify_event();
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100546}
547
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100548#if defined(__linux__)
549
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100550#include "qemu/compatfd.h"
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200551
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100552static int dynticks_start_timer(struct qemu_alarm_timer *t)
553{
554 struct sigevent ev;
555 timer_t host_timer;
556 struct sigaction act;
557
558 sigfillset(&act.sa_mask);
559 act.sa_flags = 0;
560 act.sa_handler = host_alarm_handler;
561
562 sigaction(SIGALRM, &act, NULL);
563
564 /*
565 * Initialize ev struct to 0 to avoid valgrind complaining
566 * about uninitialized data in timer_create call
567 */
568 memset(&ev, 0, sizeof(ev));
569 ev.sigev_value.sival_int = 0;
570 ev.sigev_notify = SIGEV_SIGNAL;
Richard Henderson1e9737d2012-10-23 07:33:00 +1000571#ifdef CONFIG_SIGEV_THREAD_ID
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200572 if (qemu_signalfd_available()) {
573 ev.sigev_notify = SIGEV_THREAD_ID;
574 ev._sigev_un._tid = qemu_get_thread_id();
575 }
Richard Henderson1e9737d2012-10-23 07:33:00 +1000576#endif /* CONFIG_SIGEV_THREAD_ID */
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100577 ev.sigev_signo = SIGALRM;
578
579 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
580 perror("timer_create");
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100581 return -1;
582 }
583
Stefan Weilcd0544e2011-04-10 20:15:09 +0200584 t->timer = host_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100585
586 return 0;
587}
588
589static void dynticks_stop_timer(struct qemu_alarm_timer *t)
590{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200591 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100592
593 timer_delete(host_timer);
594}
595
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100596static void dynticks_rearm_timer(struct qemu_alarm_timer *t,
597 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100598{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200599 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100600 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100601 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100602
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100603 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
604 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100605
606 /* check whether a timer is already running */
607 if (timer_gettime(host_timer, &timeout)) {
608 perror("gettime");
609 fprintf(stderr, "Internal timer error: aborting\n");
610 exit(1);
611 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100612 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
613 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100614 return;
615
616 timeout.it_interval.tv_sec = 0;
617 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100618 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
619 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100620 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
621 perror("settime");
622 fprintf(stderr, "Internal timer error: aborting\n");
623 exit(1);
624 }
625}
626
627#endif /* defined(__linux__) */
628
Stefan Weilf26e5a52011-02-04 22:01:32 +0100629#if !defined(_WIN32)
630
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100631static int unix_start_timer(struct qemu_alarm_timer *t)
632{
633 struct sigaction act;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100634
635 /* timer signal */
636 sigfillset(&act.sa_mask);
637 act.sa_flags = 0;
638 act.sa_handler = host_alarm_handler;
639
640 sigaction(SIGALRM, &act, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200641 return 0;
642}
643
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100644static void unix_rearm_timer(struct qemu_alarm_timer *t,
645 int64_t nearest_delta_ns)
Paolo Bonzini84682832011-06-09 13:10:25 +0200646{
647 struct itimerval itv;
Paolo Bonzini84682832011-06-09 13:10:25 +0200648 int err;
649
Paolo Bonzini84682832011-06-09 13:10:25 +0200650 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
651 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100652
653 itv.it_interval.tv_sec = 0;
Paolo Bonzini84682832011-06-09 13:10:25 +0200654 itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */
655 itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
656 itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100657 err = setitimer(ITIMER_REAL, &itv, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200658 if (err) {
659 perror("setitimer");
660 fprintf(stderr, "Internal timer error: aborting\n");
661 exit(1);
662 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100663}
664
665static void unix_stop_timer(struct qemu_alarm_timer *t)
666{
667 struct itimerval itv;
668
669 memset(&itv, 0, sizeof(itv));
670 setitimer(ITIMER_REAL, &itv, NULL);
671}
672
673#endif /* !defined(_WIN32) */
674
675
676#ifdef _WIN32
677
Stefan Weil2f9cba02011-04-05 18:34:21 +0200678static MMRESULT mm_timer;
Stefan Weil40f08e82012-04-27 05:34:40 +0000679static TIMECAPS mm_tc;
Stefan Weil2f9cba02011-04-05 18:34:21 +0200680
681static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
682 DWORD_PTR dwUser, DWORD_PTR dw1,
683 DWORD_PTR dw2)
684{
685 struct qemu_alarm_timer *t = alarm_timer;
686 if (!t) {
687 return;
688 }
Stefan Weil82051992012-04-20 11:27:24 +0200689 t->expired = true;
690 t->pending = true;
691 qemu_notify_event();
Stefan Weil2f9cba02011-04-05 18:34:21 +0200692}
693
694static int mm_start_timer(struct qemu_alarm_timer *t)
695{
Stefan Weil40f08e82012-04-27 05:34:40 +0000696 timeGetDevCaps(&mm_tc, sizeof(mm_tc));
Stefan Weil2f9cba02011-04-05 18:34:21 +0200697 return 0;
698}
699
700static void mm_stop_timer(struct qemu_alarm_timer *t)
701{
Paolo Bonzini0727b862013-02-20 14:43:31 +0100702 if (mm_timer) {
703 timeKillEvent(mm_timer);
704 }
Stefan Weil2f9cba02011-04-05 18:34:21 +0200705}
706
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100707static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta)
Stefan Weil2f9cba02011-04-05 18:34:21 +0200708{
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100709 int64_t nearest_delta_ms = delta / 1000000;
Stefan Weil40f08e82012-04-27 05:34:40 +0000710 if (nearest_delta_ms < mm_tc.wPeriodMin) {
711 nearest_delta_ms = mm_tc.wPeriodMin;
712 } else if (nearest_delta_ms > mm_tc.wPeriodMax) {
713 nearest_delta_ms = mm_tc.wPeriodMax;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100714 }
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100715
Paolo Bonzini0727b862013-02-20 14:43:31 +0100716 if (mm_timer) {
717 timeKillEvent(mm_timer);
718 }
Stefan Weil40f08e82012-04-27 05:34:40 +0000719 mm_timer = timeSetEvent((UINT)nearest_delta_ms,
720 mm_tc.wPeriodMin,
Stefan Weil2f9cba02011-04-05 18:34:21 +0200721 mm_alarm_handler,
722 (DWORD_PTR)t,
723 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
724
725 if (!mm_timer) {
Stefan Weil52ef6512012-05-08 19:14:43 +0200726 fprintf(stderr, "Failed to re-arm win32 alarm timer\n");
Stefan Weil40f08e82012-04-27 05:34:40 +0000727 timeEndPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200728 exit(1);
729 }
730}
731
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100732static int win32_start_timer(struct qemu_alarm_timer *t)
733{
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100734 HANDLE hTimer;
735 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100736
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100737 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
738 is zero) that has already expired, the timer is not updated. Since
739 creating a new timer is relatively expensive, set a bogus one-hour
740 interval in the dynticks case. */
741 success = CreateTimerQueueTimer(&hTimer,
742 NULL,
743 host_alarm_handler,
744 t,
745 1,
Stefan Weil82051992012-04-20 11:27:24 +0200746 3600000,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100747 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100748
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100749 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100750 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
751 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100752 return -1;
753 }
754
Stefan Weilcd0544e2011-04-10 20:15:09 +0200755 t->timer = hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100756 return 0;
757}
758
759static void win32_stop_timer(struct qemu_alarm_timer *t)
760{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200761 HANDLE hTimer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100762
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100763 if (hTimer) {
764 DeleteTimerQueueTimer(NULL, hTimer, NULL);
765 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100766}
767
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100768static void win32_rearm_timer(struct qemu_alarm_timer *t,
769 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100770{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200771 HANDLE hTimer = t->timer;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100772 int64_t nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100773 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100774
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100775 nearest_delta_ms = nearest_delta_ns / 1000000;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +0100776 if (nearest_delta_ms < 1) {
777 nearest_delta_ms = 1;
778 }
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100779 /* ULONG_MAX can be 32 bit */
780 if (nearest_delta_ms > ULONG_MAX) {
781 nearest_delta_ms = ULONG_MAX;
782 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100783 success = ChangeTimerQueueTimer(NULL,
784 hTimer,
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100785 (unsigned long) nearest_delta_ms,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100786 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100787
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100788 if (!success) {
789 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100790 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100791 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100792 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100793
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100794}
795
796#endif /* _WIN32 */
797
Paolo Bonzini4260a732011-09-19 10:18:51 +0200798static void quit_timers(void)
799{
800 struct qemu_alarm_timer *t = alarm_timer;
801 alarm_timer = NULL;
802 t->stop(t);
803}
804
Stefan Weil253ecf82012-11-04 21:42:08 +0100805#ifdef CONFIG_POSIX
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100806static void reinit_timers(void)
807{
808 struct qemu_alarm_timer *t = alarm_timer;
809 t->stop(t);
810 if (t->start(t)) {
811 fprintf(stderr, "Internal timer error: aborting\n");
812 exit(1);
813 }
814 qemu_rearm_alarm_timer(t);
815}
Stefan Weil253ecf82012-11-04 21:42:08 +0100816#endif /* CONFIG_POSIX */
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100817
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100818int init_timer_alarm(void)
819{
820 struct qemu_alarm_timer *t = NULL;
821 int i, err = -1;
822
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100823 if (alarm_timer) {
824 return 0;
825 }
826
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100827 for (i = 0; alarm_timers[i].name; i++) {
828 t = &alarm_timers[i];
829
830 err = t->start(t);
831 if (!err)
832 break;
833 }
834
835 if (err) {
836 err = -ENOENT;
837 goto fail;
838 }
839
Paolo Bonzini4260a732011-09-19 10:18:51 +0200840 atexit(quit_timers);
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100841#ifdef CONFIG_POSIX
842 pthread_atfork(NULL, NULL, reinit_timers);
843#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100844 alarm_timer = t;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100845 return 0;
846
847fail:
848 return err;
849}
850